Skip to content

Commit

Permalink
Update syn dependency (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored May 24, 2023
1 parent 88b1216 commit 72e9bf4
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 65 deletions.
2 changes: 1 addition & 1 deletion crates/libs/implement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ targets = []
proc-macro = true

[dependencies]
syn = { version = "1.0", default-features = false, features = ["parsing", "proc-macro", "printing", "full", "derive"] }
syn = { version = "2.0", default-features = false, features = ["parsing", "proc-macro", "printing", "full", "derive"] }
quote = "1.0"
proc-macro2 = "1.0"
4 changes: 2 additions & 2 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl UseTree2 {

Ok(ImplementType { type_name, generics })
}
UseTree2::Group(input) => Err(syn::parse::Error::new(input.brace_token.span, "Syntax not supported")),
UseTree2::Group(input) => Err(syn::parse::Error::new(input.brace_token.span.join(), "Syntax not supported")),
}
}
}
Expand Down Expand Up @@ -336,7 +336,7 @@ impl syn::parse::Parse for UseTree2 {
} else if lookahead.peek(syn::token::Brace) {
let content;
let brace_token = syn::braced!(content in input);
let items = content.parse_terminated(UseTree2::parse)?;
let items = content.parse_terminated(UseTree2::parse, syn::Token![,])?;

Ok(UseTree2::Group(UseGroup2 { brace_token, items }))
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ targets = []
proc-macro = true

[dependencies]
syn = { version = "1.0", default-features = false, features = ["parsing", "proc-macro", "printing", "full", "derive"] }
syn = { version = "2.0", default-features = false, features = ["parsing", "proc-macro", "printing", "full", "derive"] }
quote = "1.0"
proc-macro2 = "1.0"
6 changes: 3 additions & 3 deletions crates/libs/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl Parse for Interface {
let attributes = input.call(syn::Attribute::parse_outer)?;
let mut docs = Vec::new();
for attr in attributes.into_iter() {
let path = &attr.path;
let path = attr.path();
if path.is_ident("doc") {
docs.push(attr);
} else {
Expand Down Expand Up @@ -502,8 +502,8 @@ impl syn::parse::Parse for InterfaceMethod {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let docs = input.call(syn::Attribute::parse_outer)?;
let visibility = input.parse::<syn::Visibility>()?;
let method = input.parse::<syn::TraitItemMethod>()?;
unexpected_token!(docs.iter().find(|a| !a.path.is_ident("doc")), "attribute");
let method = input.parse::<syn::TraitItemFn>()?;
unexpected_token!(docs.iter().find(|a| !a.path().is_ident("doc")), "attribute");
unexpected_token!(method.default, "default method implementation");
let sig = method.sig;
unexpected_token!(sig.abi, "abi declaration");
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "../tokens"
version = "0.48.0"

[dependencies.syn]
version = "1.0"
version = "2.0"
features = ["full", "extra-traits"]

[dependencies.proc-macro2]
Expand Down
12 changes: 0 additions & 12 deletions crates/libs/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,3 @@ macro_rules! flags {
}

pub(crate) use flags;

#[derive(Copy, Clone, Debug)]
pub enum Integer {
U8(u8),
I8(i8),
U16(u16),
I16(i16),
U32(u32),
I32(i32),
U64(u64),
I64(i64),
}
18 changes: 9 additions & 9 deletions crates/libs/metadata/src/reader/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ impl<'a> Blob<'a> {
self.offset(8);
value
}
pub fn read_integer(&mut self, ty: Type) -> Integer {
pub fn read_integer(&mut self, ty: Type) -> Value {
match ty {
Type::I8 => Integer::I8(self.read_i8()),
Type::U8 => Integer::U8(self.read_u8()),
Type::I16 => Integer::I16(self.read_i16()),
Type::U16 => Integer::U16(self.read_u16()),
Type::I32 => Integer::I32(self.read_i32()),
Type::U32 => Integer::U32(self.read_u32()),
Type::I64 => Integer::I64(self.read_i64()),
Type::U64 => Integer::U64(self.read_u64()),
Type::I8 => Value::I8(self.read_i8()),
Type::U8 => Value::U8(self.read_u8()),
Type::I16 => Value::I16(self.read_i16()),
Type::U16 => Value::U16(self.read_u16()),
Type::I32 => Value::I32(self.read_i32()),
Type::U32 => Value::U32(self.read_u32()),
Type::I64 => Value::I64(self.read_i64()),
Type::U64 => Value::U64(self.read_u64()),
_ => panic!("Type is not an integer"),
}
}
Expand Down
36 changes: 20 additions & 16 deletions crates/libs/metadata/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub enum Value {
String(String),
TypeDef(TypeDef),
TypeRef(TypeDefOrRef),
EnumDef(TypeDef, Integer),
EnumRef(TypeDefOrRef, Integer),
EnumDef(TypeDef, Box<Self>),
EnumRef(TypeDefOrRef, Box<Self>),
}

pub struct Signature {
Expand Down Expand Up @@ -328,10 +328,10 @@ impl<'a> Reader<'a> {
Type::U64 => Value::U64(values.read_u64()),
Type::String => Value::String(values.read_str().to_string()),
Type::TypeName => Value::TypeDef(self.get(TypeName::parse(values.read_str())).next().expect("Type not found")),
Type::TypeDef((def, _)) => Value::EnumDef(def, values.read_integer(self.type_def_underlying_type(def))),
Type::TypeDef((def, _)) => Value::EnumDef(def, Box::new(values.read_integer(self.type_def_underlying_type(def)))),
// It's impossible to know the type of a TypeRef so we just assume 32-bit integer which covers System.* attribute args
// reasonably well but the solution is to follow the WinRT metadata and define replacements for those attribute types.
Type::TypeRef(code) => Value::EnumRef(code, values.read_integer(Type::I32)),
Type::TypeRef(code) => Value::EnumRef(code, Box::new(values.read_integer(Type::I32))),
rest => todo!("{:?}", rest),
};

Expand All @@ -355,7 +355,7 @@ impl<'a> Reader<'a> {
0x55 => {
let def = self.get(TypeName::parse(&name)).next().expect("Type not found");
name = values.read_str().into();
Value::EnumDef(def, values.read_integer(self.type_def_underlying_type(def)))
Value::EnumDef(def, Box::new(values.read_integer(self.type_def_underlying_type(def))))
}
_ => todo!("{:?}", arg_type),
};
Expand Down Expand Up @@ -1124,8 +1124,10 @@ impl<'a> Reader<'a> {
match self.attribute_name(attribute) {
"AgileAttribute" => return true,
"MarshalingBehaviorAttribute" => {
if let Some((_, Value::EnumDef(_, Integer::I32(2)))) = self.attribute_args(attribute).get(0) {
return true;
if let Some((_, Value::EnumDef(_, value))) = self.attribute_args(attribute).get(0) {
if let Value::I32(2) = **value {
return true;
}
}
}
_ => {}
Expand Down Expand Up @@ -1470,15 +1472,17 @@ impl<'a> Reader<'a> {
for attribute in attributes {
match self.attribute_name(attribute) {
"SupportedArchitectureAttribute" => {
if let Some((_, Value::EnumDef(_, Integer::I32(value)))) = self.attribute_args(attribute).get(0) {
if value & 1 == 1 {
cfg.arches.insert("x86");
}
if value & 2 == 2 {
cfg.arches.insert("x86_64");
}
if value & 4 == 4 {
cfg.arches.insert("aarch64");
if let Some((_, Value::EnumDef(_, value))) = self.attribute_args(attribute).get(0) {
if let Value::I32(value) = **value {
if value & 1 == 1 {
cfg.arches.insert("x86");
}
if value & 2 == 2 {
cfg.arches.insert("x86_64");
}
if value & 4 == 4 {
cfg.arches.insert("aarch64");
}
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions crates/libs/metadata/src/writer/idl/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Printer {
self.indent += 1;

for method in &member.methods {
self.trait_item_method(method);
self.trait_item_fn(method);
self.word(";");
self.newline();
}
Expand All @@ -100,11 +100,26 @@ impl Printer {

fn attr(&mut self, attr: &syn::Attribute) {
self.word("#[");
self.path(&attr.path);
self.meta(&attr.meta);
self.word("]");
self.newline();
}

fn meta(&mut self, meta: &syn::Meta) {
match meta {
syn::Meta::Path(path) => self.path(path),
syn::Meta::List(list) => self.meta_list(list),
rest => todo!("{:?}", rest),
}
}

fn meta_list(&mut self, meta_list: &syn::MetaList) {
self.path(&meta_list.path);
self.word("(");
self.word(&meta_list.tokens.to_string());
self.word(")");
}

fn idl_struct(&mut self, member: &IdlStruct) {
self.attrs(&member.item.attrs);

Expand Down Expand Up @@ -153,7 +168,7 @@ impl Printer {

fn idl_class(&mut self, _member: &IdlClass) {}

fn trait_item_method(&mut self, method: &syn::TraitItemMethod) {
fn trait_item_fn(&mut self, method: &syn::TraitItemFn) {
self.signature(&method.sig);
}

Expand Down Expand Up @@ -195,7 +210,7 @@ impl Printer {
fn pat(&mut self, pat: &syn::Pat) {
match pat {
syn::Pat::Ident(pat_ident) => self.pat_ident(pat_ident),
_ => todo!("{:?}", pat),
rest => todo!("{:?}", rest),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/writer/idl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ pub struct IdlClass {
pub struct IdlInterface {
attributes: Vec<syn::Attribute>,
ident: syn::Ident,
methods: Vec<syn::TraitItemMethod>,
methods: Vec<syn::TraitItemFn>,
}
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/writer/idl/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl IdlInterface {
syn::braced!(content in input);
let mut methods = vec![];
while !content.is_empty() {
methods.push(content.parse::<syn::TraitItemMethod>()?);
methods.push(content.parse::<syn::TraitItemFn>()?);
}
Ok(Self { attributes, ident, methods })
}
Expand Down
7 changes: 5 additions & 2 deletions crates/libs/metadata/src/writer/idl/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,17 @@ fn value_to_idl(module: &Module, value: &Value) -> TokenStream {
Value::I64(value) => format!("{value}i64").into(),
Value::F32(value) => format!("{value}f32").into(),
Value::F64(value) => format!("{value}f64").into(),
Value::String(value) => value.into(),
Value::String(value) => format!("\"{value}\"").into(),
Value::TypeName(type_name) => {
let type_name = reader::TypeName::parse(type_name);
let namespace = namespace_to_idl(&module.namespace, type_name.namespace);
let name = to_ident(type_name.name);
quote! { #namespace#name }
}
rest => todo!("{:?}", rest),
Value::Enum(_, value) => {
// TODO: replace the integer with the constant from the enum's definition
value_to_idl(module, value)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub enum Value {
F64(f64),
String(String),
TypeName(String),
Enum(String, Integer),
Enum(String, Box<Value>),
}

impl Value {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/metadata/src/writer/winmd/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ fn read_value(reader: &reader::Reader, value: &reader::Value) -> Result<Value> {
reader::Value::String(value) => Ok(Value::String(value.clone())),
reader::Value::TypeDef(def) => Ok(Value::TypeName(format!("{}", reader.type_def_type_name(*def)))),
reader::Value::TypeRef(code) => Ok(Value::TypeName(format!("{}", reader.type_def_or_ref(*code)))),
reader::Value::EnumDef(def, value) => Ok(Value::Enum(format!("{}", reader.type_def_type_name(*def)), *value)),
reader::Value::EnumRef(code, value) => Ok(Value::Enum(format!("{}", reader.type_def_or_ref(*code)), *value)),
reader::Value::EnumDef(def, value) => Ok(Value::Enum(format!("{}", reader.type_def_type_name(*def)), Box::new(read_value(reader, value)?))),
reader::Value::EnumRef(code, value) => Ok(Value::Enum(format!("{}", reader.type_def_or_ref(*code)), Box::new(read_value(reader, value)?))),
}
}

Expand Down
15 changes: 9 additions & 6 deletions crates/tests/metadata/tests/attribute_enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use metadata::reader::{Attribute, File, Integer, Reader, TypeName, Value};
use metadata::reader::{Attribute, File, Reader, TypeName, Value};

#[test]
fn attribute_enum() {
Expand Down Expand Up @@ -52,10 +52,13 @@ fn check_attr_arg_enum(
.find(|(name, _)| name == arg_name)
.unwrap();

if let Value::EnumDef(ty, Integer::I32(value)) = value {
assert_eq!(expected_type, reader.type_def_type_name(ty).to_string());
assert_eq!(expected_value, value);
} else {
panic!("Value not found");
if let Value::EnumDef(ty, value) = value {
if let Value::I32(value) = *value {
assert_eq!(expected_type, reader.type_def_type_name(ty).to_string());
assert_eq!(expected_value, value);
return;
}
}

panic!("Value not found");
}
1 change: 0 additions & 1 deletion crates/tools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ publish = false

[dependencies]
metadata = { package = "windows-metadata", path = "../../libs/metadata" }
syn = "1.0"
2 changes: 0 additions & 2 deletions crates/tools/riddle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ repository = "https://github.com/microsoft/windows-rs"

[dependencies]
metadata = { package = "windows-metadata", path = "../../libs/metadata", version = "0.49.0" }
syn = { version = "1.0", features = ["full", "extra-traits"] }
proc-macro2 = {version = "1.0", features = ["span-locations"] }

0 comments on commit 72e9bf4

Please sign in to comment.