Skip to content

Commit 30b2c55

Browse files
authored
fiat-constify/zeroize_derive: bump syn to v2 (#858)
1 parent fc010a5 commit 30b2c55

File tree

5 files changed

+380
-376
lines changed

5 files changed

+380
-376
lines changed

Cargo.lock

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

fiat-constify/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ rust-version = "1.56"
1818
[dependencies]
1919
proc-macro2 = "1"
2020
quote = "1"
21-
syn = { version = "1", features = ["extra-traits", "full"] }
21+
syn = { version = "2", features = ["extra-traits", "full"] }

fiat-constify/src/main.rs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
66
#![allow(clippy::single_match, clippy::new_without_default)]
77

8-
use proc_macro2::Span;
8+
use proc_macro2::{Literal, Span};
99
use quote::{quote, ToTokens};
1010
use std::{collections::BTreeMap as Map, env, fs, ops::Deref};
1111
use syn::{
1212
punctuated::Punctuated,
13-
token::{Bang, Brace, Bracket, Colon, Const, Eq, Let, Mut, Paren, Pound, RArrow, Semi},
13+
token::{Brace, Bracket, Colon, Const, Eq, Let, Mut, Not, Paren, Pound, RArrow, Semi},
1414
AttrStyle, Attribute, Block, Expr, ExprAssign, ExprCall, ExprLit, ExprPath, ExprReference,
15-
ExprRepeat, ExprTuple, FnArg, Ident, Item, ItemFn, ItemType, Lit, LitInt, Local, Pat, PatIdent,
16-
PatTuple, PatType, Path, PathArguments, PathSegment, ReturnType, Stmt, Type, TypeArray,
17-
TypePath, TypeReference, TypeTuple, UnOp,
15+
ExprRepeat, ExprTuple, FnArg, Ident, Item, ItemFn, ItemType, Lit, LitInt, Local, LocalInit,
16+
MacroDelimiter, Meta, MetaList, Pat, PatIdent, PatTuple, PatType, Path, PathArguments,
17+
PathSegment, ReturnType, Stmt, Type, TypeArray, TypePath, TypeReference, TypeTuple, UnOp,
1818
};
1919

2020
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -59,29 +59,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5959

6060
/// Build a toplevel attribute with the given name and comma-separated values.
6161
fn build_attribute(name: &str, values: &[&str]) -> Attribute {
62-
let span = Span::call_site();
6362
let values = values
6463
.iter()
6564
.map(|value| build_path(value))
6665
.collect::<Vec<_>>();
66+
let path = build_path(name);
67+
let tokens = quote! { #(#values),* };
68+
let delimiter = MacroDelimiter::Paren(Paren::default());
6769

6870
Attribute {
69-
pound_token: Pound { spans: [span] },
70-
style: AttrStyle::Inner(Bang { spans: [span] }),
71-
bracket_token: Bracket { span },
72-
path: build_path(name),
73-
tokens: quote! { (#(#values),*) },
71+
pound_token: Pound::default(),
72+
style: AttrStyle::Inner(Not::default()),
73+
bracket_token: Bracket::default(),
74+
meta: Meta::List(MetaList {
75+
path,
76+
delimiter,
77+
tokens,
78+
}),
7479
}
7580
}
7681

7782
/// Parse a path from a double-colon-delimited string.
7883
fn build_path(path: &str) -> Path {
79-
let span = Span::call_site();
8084
let mut segments = Punctuated::new();
8185

8286
for segment in path.split("::") {
8387
segments.push(PathSegment {
84-
ident: Ident::new(segment, span),
88+
ident: Ident::new(segment, Span::call_site()),
8589
arguments: PathArguments::None,
8690
});
8791
}
@@ -103,10 +107,8 @@ fn get_ident_from_pat(pat: &Pat) -> Ident {
103107
/// Rewrite a fiat-crypto generated `fn` as a `const fn`, making the necessary
104108
/// transformations to the code in order for it to work in that context.
105109
fn rewrite_fn_as_const(func: &mut ItemFn, type_registry: &TypeRegistry) {
106-
let span = Span::call_site();
107-
108110
// Mark function as being `const fn`.
109-
func.sig.constness = Some(Const { span });
111+
func.sig.constness = Some(Const::default());
110112

111113
// Transform mutable arguments into return values.
112114
let mut inputs = Punctuated::new();
@@ -142,7 +144,6 @@ fn rewrite_fn_as_const(func: &mut ItemFn, type_registry: &TypeRegistry) {
142144
/// values for outputs, removing mutable references, and adding a return
143145
/// value/tuple.
144146
fn rewrite_fn_body(statements: &[Stmt], outputs: &Outputs, registry: &TypeRegistry) -> Block {
145-
let span = Span::call_site();
146147
let mut stmts = Vec::new();
147148

148149
stmts.extend(outputs.to_let_bindings(registry).into_iter());
@@ -156,7 +157,7 @@ fn rewrite_fn_body(statements: &[Stmt], outputs: &Outputs, registry: &TypeRegist
156157
stmts.push(outputs.to_return_value());
157158

158159
Block {
159-
brace_token: Brace { span },
160+
brace_token: Brace::default(),
160161
stmts,
161162
}
162163
}
@@ -165,7 +166,7 @@ fn rewrite_fn_body(statements: &[Stmt], outputs: &Outputs, registry: &TypeRegist
165166
/// operations into value assignments.
166167
fn rewrite_fn_stmt(stmt: &mut Stmt) {
167168
match stmt {
168-
Stmt::Semi(expr, _) => match expr {
169+
Stmt::Expr(expr, Some(_)) => match expr {
169170
Expr::Assign(ExprAssign { left, .. }) => match *left.clone() {
170171
Expr::Unary(unary) => {
171172
// Remove deref since we're removing mutable references
@@ -225,20 +226,22 @@ fn rewrite_fn_call(mut call: ExprCall) -> Local {
225226
// Overwrite call arguments with the ones that aren't mutable references
226227
call.args = args;
227228

228-
let span = Span::call_site();
229-
230229
let pat = Pat::Tuple(PatTuple {
231230
attrs: Vec::new(),
232-
paren_token: Paren { span },
231+
paren_token: Paren::default(),
233232
elems: output,
234233
});
235234

236235
Local {
237236
attrs: Vec::new(),
238-
let_token: Let { span },
237+
let_token: Let::default(),
239238
pat,
240-
init: Some((Eq { spans: [span] }, Box::new(Expr::Call(call)))),
241-
semi_token: Semi { spans: [span] },
239+
init: Some(LocalInit {
240+
eq_token: Eq::default(),
241+
expr: Box::new(Expr::Call(call)),
242+
diverge: None,
243+
}),
244+
semi_token: Semi::default(),
242245
}
243246
}
244247

@@ -299,39 +302,38 @@ impl Outputs {
299302
/// Generate `let mut outN: Ty = <zero>` bindings at the start
300303
/// of the function.
301304
pub fn to_let_bindings(&self, registry: &TypeRegistry) -> Vec<Stmt> {
302-
let span = Span::call_site();
303-
304305
self.0
305306
.iter()
306307
.map(|(ident, ty)| {
307308
Stmt::Local(Local {
308309
attrs: Vec::new(),
309-
let_token: Let { span },
310+
let_token: Let::default(),
310311
pat: Pat::Type(PatType {
311312
attrs: Vec::new(),
312313
pat: Box::new(Pat::Ident(PatIdent {
313314
attrs: Vec::new(),
314315
by_ref: None,
315-
mutability: Some(Mut { span }),
316+
mutability: Some(Mut::default()),
316317
ident: ident.clone(),
317318
subpat: None,
318319
})),
319-
colon_token: Colon { spans: [span] },
320+
colon_token: Colon::default(),
320321
ty: Box::new(ty.clone()),
321322
}),
322-
init: Some((Eq { spans: [span] }, Box::new(default_for(ty, registry)))),
323-
semi_token: Semi { spans: [span] },
323+
init: Some(LocalInit {
324+
eq_token: Eq::default(),
325+
expr: Box::new(default_for(ty, registry)),
326+
diverge: None,
327+
}),
328+
semi_token: Semi::default(),
324329
})
325330
})
326331
.collect()
327332
}
328333

329334
/// Finish annotating outputs, updating the provided `Signature`.
330335
pub fn to_return_type(&self) -> ReturnType {
331-
let span = Span::call_site();
332-
let rarrow = RArrow {
333-
spans: [span, span],
334-
};
336+
let rarrow = RArrow::default();
335337

336338
let ret = match self.0.len() {
337339
0 => panic!("expected at least one output"),
@@ -344,7 +346,7 @@ impl Outputs {
344346
}
345347

346348
Type::Tuple(TypeTuple {
347-
paren_token: Paren { span },
349+
paren_token: Paren::default(),
348350
elems,
349351
})
350352
}
@@ -355,8 +357,6 @@ impl Outputs {
355357

356358
/// Generate the return value for the statement as a tuple of the outputs.
357359
pub fn to_return_value(&self) -> Stmt {
358-
let span = Span::call_site();
359-
360360
let mut elems = self.0.keys().map(|ident| {
361361
let mut segments = Punctuated::new();
362362
segments.push(PathSegment {
@@ -377,31 +377,33 @@ impl Outputs {
377377
});
378378

379379
if elems.len() == 1 {
380-
Stmt::Expr(elems.next().unwrap())
380+
Stmt::Expr(elems.next().unwrap(), None)
381381
} else {
382-
Stmt::Expr(Expr::Tuple(ExprTuple {
383-
attrs: Vec::new(),
384-
paren_token: Paren { span },
385-
elems: elems.collect(),
386-
}))
382+
Stmt::Expr(
383+
Expr::Tuple(ExprTuple {
384+
attrs: Vec::new(),
385+
paren_token: Paren::default(),
386+
elems: elems.collect(),
387+
}),
388+
None,
389+
)
387390
}
388391
}
389392
}
390393

391394
/// Get a default value for the given type.
392395
fn default_for(ty: &Type, registry: &TypeRegistry) -> Expr {
393-
let span = Span::call_site();
394396
let zero = Expr::Lit(ExprLit {
395397
attrs: Vec::new(),
396-
lit: Lit::Int(LitInt::new("0", span)),
398+
lit: Lit::Int(LitInt::from(Literal::u8_unsuffixed(0))),
397399
});
398400

399401
match ty {
400402
Type::Array(TypeArray { len, .. }) => Expr::Repeat(ExprRepeat {
401403
attrs: Vec::new(),
402-
bracket_token: Bracket { span },
404+
bracket_token: Bracket::default(),
403405
expr: Box::new(zero),
404-
semi_token: Semi { spans: [span] },
406+
semi_token: Semi::default(),
405407
len: Box::new(len.clone()),
406408
}),
407409
Type::Path(TypePath { path, .. }) => {

zeroize/derive/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ proc-macro = true
1717
[dependencies]
1818
proc-macro2 = "1"
1919
quote = "1"
20-
syn = "1"
21-
synstructure = "0.12.2"
20+
syn = {version = "2", features = ["full", "extra-traits"]}
2221

2322
[package.metadata.docs.rs]
2423
rustdoc-args = ["--document-private-items"]

0 commit comments

Comments
 (0)