5
5
6
6
#![ allow( clippy:: single_match, clippy:: new_without_default) ]
7
7
8
- use proc_macro2:: Span ;
8
+ use proc_macro2:: { Literal , Span } ;
9
9
use quote:: { quote, ToTokens } ;
10
10
use std:: { collections:: BTreeMap as Map , env, fs, ops:: Deref } ;
11
11
use syn:: {
12
12
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 } ,
14
14
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 ,
18
18
} ;
19
19
20
20
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
@@ -59,29 +59,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
59
59
60
60
/// Build a toplevel attribute with the given name and comma-separated values.
61
61
fn build_attribute ( name : & str , values : & [ & str ] ) -> Attribute {
62
- let span = Span :: call_site ( ) ;
63
62
let values = values
64
63
. iter ( )
65
64
. map ( |value| build_path ( value) )
66
65
. collect :: < Vec < _ > > ( ) ;
66
+ let path = build_path ( name) ;
67
+ let tokens = quote ! { #( #values) , * } ;
68
+ let delimiter = MacroDelimiter :: Paren ( Paren :: default ( ) ) ;
67
69
68
70
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
+ } ) ,
74
79
}
75
80
}
76
81
77
82
/// Parse a path from a double-colon-delimited string.
78
83
fn build_path ( path : & str ) -> Path {
79
- let span = Span :: call_site ( ) ;
80
84
let mut segments = Punctuated :: new ( ) ;
81
85
82
86
for segment in path. split ( "::" ) {
83
87
segments. push ( PathSegment {
84
- ident : Ident :: new ( segment, span ) ,
88
+ ident : Ident :: new ( segment, Span :: call_site ( ) ) ,
85
89
arguments : PathArguments :: None ,
86
90
} ) ;
87
91
}
@@ -103,10 +107,8 @@ fn get_ident_from_pat(pat: &Pat) -> Ident {
103
107
/// Rewrite a fiat-crypto generated `fn` as a `const fn`, making the necessary
104
108
/// transformations to the code in order for it to work in that context.
105
109
fn rewrite_fn_as_const ( func : & mut ItemFn , type_registry : & TypeRegistry ) {
106
- let span = Span :: call_site ( ) ;
107
-
108
110
// Mark function as being `const fn`.
109
- func. sig . constness = Some ( Const { span } ) ;
111
+ func. sig . constness = Some ( Const :: default ( ) ) ;
110
112
111
113
// Transform mutable arguments into return values.
112
114
let mut inputs = Punctuated :: new ( ) ;
@@ -142,7 +144,6 @@ fn rewrite_fn_as_const(func: &mut ItemFn, type_registry: &TypeRegistry) {
142
144
/// values for outputs, removing mutable references, and adding a return
143
145
/// value/tuple.
144
146
fn rewrite_fn_body ( statements : & [ Stmt ] , outputs : & Outputs , registry : & TypeRegistry ) -> Block {
145
- let span = Span :: call_site ( ) ;
146
147
let mut stmts = Vec :: new ( ) ;
147
148
148
149
stmts. extend ( outputs. to_let_bindings ( registry) . into_iter ( ) ) ;
@@ -156,7 +157,7 @@ fn rewrite_fn_body(statements: &[Stmt], outputs: &Outputs, registry: &TypeRegist
156
157
stmts. push ( outputs. to_return_value ( ) ) ;
157
158
158
159
Block {
159
- brace_token : Brace { span } ,
160
+ brace_token : Brace :: default ( ) ,
160
161
stmts,
161
162
}
162
163
}
@@ -165,7 +166,7 @@ fn rewrite_fn_body(statements: &[Stmt], outputs: &Outputs, registry: &TypeRegist
165
166
/// operations into value assignments.
166
167
fn rewrite_fn_stmt ( stmt : & mut Stmt ) {
167
168
match stmt {
168
- Stmt :: Semi ( expr, _ ) => match expr {
169
+ Stmt :: Expr ( expr, Some ( _ ) ) => match expr {
169
170
Expr :: Assign ( ExprAssign { left, .. } ) => match * left. clone ( ) {
170
171
Expr :: Unary ( unary) => {
171
172
// Remove deref since we're removing mutable references
@@ -225,20 +226,22 @@ fn rewrite_fn_call(mut call: ExprCall) -> Local {
225
226
// Overwrite call arguments with the ones that aren't mutable references
226
227
call. args = args;
227
228
228
- let span = Span :: call_site ( ) ;
229
-
230
229
let pat = Pat :: Tuple ( PatTuple {
231
230
attrs : Vec :: new ( ) ,
232
- paren_token : Paren { span } ,
231
+ paren_token : Paren :: default ( ) ,
233
232
elems : output,
234
233
} ) ;
235
234
236
235
Local {
237
236
attrs : Vec :: new ( ) ,
238
- let_token : Let { span } ,
237
+ let_token : Let :: default ( ) ,
239
238
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 ( ) ,
242
245
}
243
246
}
244
247
@@ -299,39 +302,38 @@ impl Outputs {
299
302
/// Generate `let mut outN: Ty = <zero>` bindings at the start
300
303
/// of the function.
301
304
pub fn to_let_bindings ( & self , registry : & TypeRegistry ) -> Vec < Stmt > {
302
- let span = Span :: call_site ( ) ;
303
-
304
305
self . 0
305
306
. iter ( )
306
307
. map ( |( ident, ty) | {
307
308
Stmt :: Local ( Local {
308
309
attrs : Vec :: new ( ) ,
309
- let_token : Let { span } ,
310
+ let_token : Let :: default ( ) ,
310
311
pat : Pat :: Type ( PatType {
311
312
attrs : Vec :: new ( ) ,
312
313
pat : Box :: new ( Pat :: Ident ( PatIdent {
313
314
attrs : Vec :: new ( ) ,
314
315
by_ref : None ,
315
- mutability : Some ( Mut { span } ) ,
316
+ mutability : Some ( Mut :: default ( ) ) ,
316
317
ident : ident. clone ( ) ,
317
318
subpat : None ,
318
319
} ) ) ,
319
- colon_token : Colon { spans : [ span ] } ,
320
+ colon_token : Colon :: default ( ) ,
320
321
ty : Box :: new ( ty. clone ( ) ) ,
321
322
} ) ,
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 ( ) ,
324
329
} )
325
330
} )
326
331
. collect ( )
327
332
}
328
333
329
334
/// Finish annotating outputs, updating the provided `Signature`.
330
335
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 ( ) ;
335
337
336
338
let ret = match self . 0 . len ( ) {
337
339
0 => panic ! ( "expected at least one output" ) ,
@@ -344,7 +346,7 @@ impl Outputs {
344
346
}
345
347
346
348
Type :: Tuple ( TypeTuple {
347
- paren_token : Paren { span } ,
349
+ paren_token : Paren :: default ( ) ,
348
350
elems,
349
351
} )
350
352
}
@@ -355,8 +357,6 @@ impl Outputs {
355
357
356
358
/// Generate the return value for the statement as a tuple of the outputs.
357
359
pub fn to_return_value ( & self ) -> Stmt {
358
- let span = Span :: call_site ( ) ;
359
-
360
360
let mut elems = self . 0 . keys ( ) . map ( |ident| {
361
361
let mut segments = Punctuated :: new ( ) ;
362
362
segments. push ( PathSegment {
@@ -377,31 +377,33 @@ impl Outputs {
377
377
} ) ;
378
378
379
379
if elems. len ( ) == 1 {
380
- Stmt :: Expr ( elems. next ( ) . unwrap ( ) )
380
+ Stmt :: Expr ( elems. next ( ) . unwrap ( ) , None )
381
381
} 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
+ )
387
390
}
388
391
}
389
392
}
390
393
391
394
/// Get a default value for the given type.
392
395
fn default_for ( ty : & Type , registry : & TypeRegistry ) -> Expr {
393
- let span = Span :: call_site ( ) ;
394
396
let zero = Expr :: Lit ( ExprLit {
395
397
attrs : Vec :: new ( ) ,
396
- lit : Lit :: Int ( LitInt :: new ( "0" , span ) ) ,
398
+ lit : Lit :: Int ( LitInt :: from ( Literal :: u8_unsuffixed ( 0 ) ) ) ,
397
399
} ) ;
398
400
399
401
match ty {
400
402
Type :: Array ( TypeArray { len, .. } ) => Expr :: Repeat ( ExprRepeat {
401
403
attrs : Vec :: new ( ) ,
402
- bracket_token : Bracket { span } ,
404
+ bracket_token : Bracket :: default ( ) ,
403
405
expr : Box :: new ( zero) ,
404
- semi_token : Semi { spans : [ span ] } ,
406
+ semi_token : Semi :: default ( ) ,
405
407
len : Box :: new ( len. clone ( ) ) ,
406
408
} ) ,
407
409
Type :: Path ( TypePath { path, .. } ) => {
0 commit comments