Skip to content

Commit aa8ea65

Browse files
GrayJackemilio
authored andcommitted
deps: Update syn to 2.0
The variadic function information now is found on its own field, outside of `Signature.inputs` Closes #961
1 parent 9f63284 commit aa8ea65

9 files changed

Lines changed: 211 additions & 187 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ quote = "1"
3131
heck = "0.4"
3232

3333
[dependencies.syn]
34-
version = "1.0.88"
34+
version = "2.0.64"
3535
default-features = false
3636
features = ["clone-impls", "extra-traits", "fold", "full", "parsing", "printing"]
3737

src/bindgen/ir/cfg.rs

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,52 @@ impl fmt::Display for Cfg {
8181
}
8282
}
8383

84+
impl syn::parse::Parse for Cfg {
85+
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
86+
let arg: syn::Meta = input.parse()?;
87+
88+
match arg {
89+
syn::Meta::Path(path) => path
90+
.get_ident()
91+
.map(|ident| Cfg::Boolean(ident.to_string()))
92+
.ok_or_else(|| input.error("path must be identifier")),
93+
syn::Meta::NameValue(syn::MetaNameValue {
94+
path,
95+
value:
96+
syn::Expr::Lit(syn::ExprLit {
97+
lit: syn::Lit::Str(lit),
98+
..
99+
}),
100+
..
101+
}) => path
102+
.get_ident()
103+
.map(|ident| Cfg::Named(ident.to_string(), lit.value()))
104+
.ok_or_else(|| input.error("path must be identifier")),
105+
syn::Meta::List(meta) => {
106+
if meta.path.is_ident("not") {
107+
let cfg = meta.parse_args()?;
108+
Ok(Cfg::Not(Box::new(cfg)))
109+
} else if meta.path.is_ident("all") {
110+
let cfgs = meta.parse_args_with(
111+
syn::punctuated::Punctuated::<Cfg, syn::Token![,]>::parse_terminated,
112+
)?;
113+
114+
Ok(Cfg::All(cfgs.into_iter().collect()))
115+
} else if meta.path.is_ident("any") {
116+
let cfgs = meta.parse_args_with(
117+
syn::punctuated::Punctuated::<Cfg, syn::Token![,]>::parse_terminated,
118+
)?;
119+
120+
Ok(Cfg::Any(cfgs.into_iter().collect()))
121+
} else {
122+
Err(input.error("invalid list argument"))
123+
}
124+
}
125+
_ => Err(input.error("Failed to parse cfg")),
126+
}
127+
}
128+
}
129+
84130
impl Cfg {
85131
pub fn join(cfgs: &[Cfg]) -> Option<Cfg> {
86132
if cfgs.is_empty() {
@@ -103,12 +149,14 @@ impl Cfg {
103149
let mut configs = Vec::new();
104150

105151
for attr in attrs {
106-
if let Ok(syn::Meta::List(syn::MetaList { path, nested, .. })) = attr.parse_meta() {
107-
if !path.is_ident("cfg") || nested.len() != 1 {
152+
if let syn::Meta::List(meta @ syn::MetaList { path, .. }) = &attr.meta {
153+
if !path.is_ident("cfg") {
108154
continue;
109155
}
110156

111-
if let Some(config) = Cfg::load_single(nested.first().unwrap()) {
157+
let cfg = meta.parse_args().ok();
158+
159+
if let Some(config) = cfg {
112160
configs.push(config);
113161
}
114162
}
@@ -126,73 +174,20 @@ impl Cfg {
126174
match syn::parse_str::<syn::Meta>(target) {
127175
Ok(target) => {
128176
// Parsing succeeded using the #[cfg] syntax
129-
if let syn::Meta::List(syn::MetaList { path, nested, .. }) = target {
130-
if !path.is_ident("cfg") || nested.len() != 1 {
177+
if let syn::Meta::List(meta) = target {
178+
if !meta.path.is_ident("cfg") {
131179
return None;
132180
}
133-
Cfg::load_single(nested.first().unwrap())
181+
meta.parse_args().ok()
134182
} else {
135183
None
136184
}
137185
}
138186
Err(_) => {
139187
// Parsing failed using #[cfg], this may be a literal target
140188
// name
141-
Cfg::load_single(&syn::NestedMeta::Lit(syn::Lit::Str(syn::LitStr::new(
142-
target,
143-
proc_macro2::Span::call_site(),
144-
))))
145-
}
146-
}
147-
}
148-
149-
fn load_single(item: &syn::NestedMeta) -> Option<Cfg> {
150-
Some(match *item {
151-
syn::NestedMeta::Meta(syn::Meta::Path(ref path)) => {
152-
Cfg::Boolean(format!("{}", path.segments.first().unwrap().ident))
189+
Some(Cfg::Boolean(target.clone()))
153190
}
154-
syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
155-
ref path,
156-
lit: syn::Lit::Str(ref value),
157-
..
158-
})) => Cfg::Named(
159-
format!("{}", path.segments.first().unwrap().ident),
160-
value.value(),
161-
),
162-
syn::NestedMeta::Meta(syn::Meta::List(syn::MetaList {
163-
ref path,
164-
ref nested,
165-
..
166-
})) => {
167-
if path.is_ident("any") {
168-
Cfg::Any(Cfg::load_list(nested.iter())?)
169-
} else if path.is_ident("all") {
170-
Cfg::All(Cfg::load_list(nested.iter())?)
171-
} else if path.is_ident("not") {
172-
if nested.len() != 1 {
173-
return None;
174-
}
175-
176-
Cfg::Not(Box::new(Cfg::load_single(&nested[0])?))
177-
} else {
178-
return None;
179-
}
180-
}
181-
_ => return None,
182-
})
183-
}
184-
185-
fn load_list<'a, I: Iterator<Item = &'a syn::NestedMeta>>(attrs: I) -> Option<Vec<Cfg>> {
186-
let mut configs = Vec::new();
187-
188-
for attr in attrs {
189-
configs.push(Cfg::load_single(attr)?);
190-
}
191-
192-
if configs.is_empty() {
193-
None
194-
} else {
195-
Some(configs)
196191
}
197192
}
198193
}

src/bindgen/ir/constant.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,22 @@ impl Literal {
314314
syn::BinOp::Ne(..) => "!=",
315315
syn::BinOp::Ge(..) => ">=",
316316
syn::BinOp::Gt(..) => ">",
317-
syn::BinOp::AddEq(..) => "+=",
318-
syn::BinOp::SubEq(..) => "-=",
319-
syn::BinOp::MulEq(..) => "*=",
320-
syn::BinOp::DivEq(..) => "/=",
321-
syn::BinOp::RemEq(..) => "%=",
322-
syn::BinOp::BitXorEq(..) => "^=",
323-
syn::BinOp::BitAndEq(..) => "&=",
324-
syn::BinOp::BitOrEq(..) => "|=",
325-
syn::BinOp::ShlEq(..) => "<<=",
326-
syn::BinOp::ShrEq(..) => ">>=",
317+
syn::BinOp::AddAssign(..) => "+=",
318+
syn::BinOp::SubAssign(..) => "-=",
319+
syn::BinOp::MulAssign(..) => "*=",
320+
syn::BinOp::DivAssign(..) => "/=",
321+
syn::BinOp::RemAssign(..) => "%=",
322+
syn::BinOp::BitXorAssign(..) => "^=",
323+
syn::BinOp::BitAndAssign(..) => "&=",
324+
syn::BinOp::BitOrAssign(..) => "|=",
325+
syn::BinOp::ShlAssign(..) => "<<=",
326+
syn::BinOp::ShrAssign(..) => ">>=",
327+
currently_unknown => {
328+
return Err(format!(
329+
"unsupported binary operator: {:?}",
330+
currently_unknown
331+
))
332+
}
327333
};
328334
Ok(Literal::BinOp {
329335
left: Box::new(l),

src/bindgen/ir/function.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ impl Function {
4848
mod_cfg: Option<&Cfg>,
4949
) -> Result<Function, String> {
5050
let mut args = sig.inputs.iter().try_skip_map(|x| x.as_argument())?;
51+
if sig.variadic.is_some() {
52+
args.push(FunctionArgument {
53+
name: None,
54+
ty: Type::Primitive(super::PrimitiveType::VaList),
55+
array_length: None,
56+
})
57+
}
5158

5259
let (mut ret, never_return) = Type::load_from_output(&sig.output)?;
5360

@@ -218,19 +225,25 @@ trait SynFnArgHelpers {
218225
fn as_argument(&self) -> Result<Option<FunctionArgument>, String>;
219226
}
220227

221-
fn gen_self_type(receiver: &syn::Receiver) -> Type {
222-
let self_ty = Type::Path(GenericPath::self_path());
228+
fn gen_self_type(receiver: &syn::Receiver) -> Result<Type, String> {
229+
let mut self_ty = Type::Path(GenericPath::self_path());
230+
231+
// Custom self type
232+
if receiver.colon_token.is_some() {
233+
self_ty = Type::load(receiver.ty.as_ref())?.unwrap_or(self_ty);
234+
}
235+
223236
if receiver.reference.is_none() {
224-
return self_ty;
237+
return Ok(self_ty);
225238
}
226239

227240
let is_const = receiver.mutability.is_none();
228-
Type::Ptr {
241+
Ok(Type::Ptr {
229242
ty: Box::new(self_ty),
230243
is_const,
231244
is_nullable: false,
232245
is_ref: false,
233-
}
246+
})
234247
}
235248

236249
impl SynFnArgHelpers for syn::FnArg {
@@ -270,7 +283,7 @@ impl SynFnArgHelpers for syn::FnArg {
270283
}
271284
syn::FnArg::Receiver(ref receiver) => Ok(Some(FunctionArgument {
272285
name: Some("self".to_string()),
273-
ty: gen_self_type(receiver),
286+
ty: gen_self_type(receiver)?,
274287
array_length: None,
275288
})),
276289
}

src/bindgen/ir/global.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Static {
3636
Ok(Static::new(
3737
path,
3838
ty.unwrap(),
39-
item.mutability.is_some(),
39+
matches!(item.mutability, syn::StaticMutability::Mut(_)),
4040
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
4141
AnnotationSet::load(&item.attrs)?,
4242
Documentation::load(&item.attrs),

0 commit comments

Comments
 (0)