Debian Codegraph @

debian/rust-vendor/prettyplease/tests/test_precedence.rs

raw
1 use proc_macro2::{Ident, Span, TokenStream};
2 use quote::ToTokens as _;
3 use std::mem;
4 use std::process::ExitCode;
5 use syn::punctuated::Punctuated;
6 use syn::visit_mut::{self, VisitMut};
7 use syn::{
8 token, AngleBracketedGenericArguments, Arm, BinOp, Block, Expr, ExprArray, ExprAssign,
9 ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast, ExprClosure,
10 ExprConst, ExprContinue, ExprField, ExprForLoop, ExprIf, ExprIndex, ExprLet, ExprLit, ExprLoop,
11 ExprMacro, ExprMatch, ExprMethodCall, ExprPath, ExprRange, ExprRawAddr, ExprReference,
12 ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprUnary, ExprUnsafe, ExprWhile, ExprYield,
13 File, GenericArgument, Generics, Item, ItemConst, Label, Lifetime, LifetimeParam, Lit, LitInt,
14 Macro, MacroDelimiter, Member, Pat, PatWild, Path, PathArguments, PathSegment,
15 PointerMutability, QSelf, RangeLimits, ReturnType, Stmt, StmtMacro, Token, Type, TypeInfer,
16 TypeParam, TypePath, UnOp, Visibility,
17 };
18
19 struct FlattenParens;
20
21 impl VisitMut for FlattenParens {
22 fn visit_expr_mut(&mut self, e: &mut Expr) {
23 while let Expr::Paren(paren) = e {
24 *e = mem::replace(&mut *paren.expr, Expr::PLACEHOLDER);
25 }
26 visit_mut::visit_expr_mut(self, e);
27 }
28 }
29
30 struct AsIfPrinted;
31
32 impl VisitMut for AsIfPrinted {
33 fn visit_generics_mut(&mut self, generics: &mut Generics) {
34 if generics.params.is_empty() {
35 generics.lt_token = None;
36 generics.gt_token = None;
37 }
38 if let Some(where_clause) = &generics.where_clause {
39 if where_clause.predicates.is_empty() {
40 generics.where_clause = None;
41 }
42 }
43 visit_mut::visit_generics_mut(self, generics);
44 }
45
46 fn visit_lifetime_param_mut(&mut self, param: &mut LifetimeParam) {
47 if param.bounds.is_empty() {
48 param.colon_token = None;
49 }
50 visit_mut::visit_lifetime_param_mut(self, param);
51 }
52
53 fn visit_stmt_mut(&mut self, stmt: &mut Stmt) {
54 if let Stmt::Expr(expr, semi) = stmt {
55 if let Expr::Macro(e) = expr {
56 if match e.mac.delimiter {
57 MacroDelimiter::Brace(_) => true,
58 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => semi.is_some(),
59 } {
60 let expr = match mem::replace(expr, Expr::PLACEHOLDER) {
61 Expr::Macro(expr) => expr,
62 _ => unreachable!(),
63 };
64 *stmt = Stmt::Macro(StmtMacro {
65 attrs: expr.attrs,
66 mac: expr.mac,
67 semi_token: *semi,
68 });
69 }
70 }
71 }
72 visit_mut::visit_stmt_mut(self, stmt);
73 }
74
75 fn visit_type_param_mut(&mut self, param: &mut TypeParam) {
76 if param.bounds.is_empty() {
77 param.colon_token = None;
78 }
79 visit_mut::visit_type_param_mut(self, param);
80 }
81 }
82
83 #[test]
84 fn test_permutations() -> ExitCode {
85 fn iter(depth: usize, f: &mut dyn FnMut(Expr)) {
86 let span = Span::call_site();
87
88 // Expr::Path
89 f(Expr::Path(ExprPath {
90 // `x`
91 attrs: Vec::new(),
92 qself: None,
93 path: Path::from(Ident::new("x", span)),
94 }));
95 if false {
96 f(Expr::Path(ExprPath {
97 // `x::<T>`
98 attrs: Vec::new(),
99 qself: None,
100 path: Path {
101 leading_colon: None,
102 segments: Punctuated::from_iter([PathSegment {
103 ident: Ident::new("x", span),
104 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
105 colon2_token: Some(Token![::](span)),
106 lt_token: Token![<](span),
107 args: Punctuated::from_iter([GenericArgument::Type(Type::Path(
108 TypePath {
109 qself: None,
110 path: Path::from(Ident::new("T", span)),
111 },
112 ))]),
113 gt_token: Token![>](span),
114 }),
115 }]),
116 },
117 }));
118 f(Expr::Path(ExprPath {
119 // `<T as Trait>::CONST`
120 attrs: Vec::new(),
121 qself: Some(QSelf {
122 lt_token: Token![<](span),
123 ty: Box::new(Type::Path(TypePath {
124 qself: None,
125 path: Path::from(Ident::new("T", span)),
126 })),
127 position: 1,
128 as_token: Some(Token![as](span)),
129 gt_token: Token![>](span),
130 }),
131 path: Path {
132 leading_colon: None,
133 segments: Punctuated::from_iter([
134 PathSegment::from(Ident::new("Trait", span)),
135 PathSegment::from(Ident::new("CONST", span)),
136 ]),
137 },
138 }));
139 }
140
141 let depth = match depth.checked_sub(1) {
142 Some(depth) => depth,
143 None => return,
144 };
145
146 // Expr::Assign
147 iter(depth, &mut |expr| {
148 iter(0, &mut |simple| {
149 f(Expr::Assign(ExprAssign {
150 // `x = $expr`
151 attrs: Vec::new(),
152 left: Box::new(simple.clone()),
153 eq_token: Token![=](span),
154 right: Box::new(expr.clone()),
155 }));
156 f(Expr::Assign(ExprAssign {
157 // `$expr = x`
158 attrs: Vec::new(),
159 left: Box::new(expr.clone()),
160 eq_token: Token![=](span),
161 right: Box::new(simple),
162 }));
163 });
164 });
165
166 // Expr::Binary
167 iter(depth, &mut |expr| {
168 iter(0, &mut |simple| {
169 for op in [
170 BinOp::Add(Token![+](span)),
171 //BinOp::Sub(Token![-](span)),
172 //BinOp::Mul(Token![*](span)),
173 //BinOp::Div(Token![/](span)),
174 //BinOp::Rem(Token![%](span)),
175 //BinOp::And(Token![&&](span)),
176 //BinOp::Or(Token![||](span)),
177 //BinOp::BitXor(Token![^](span)),
178 //BinOp::BitAnd(Token![&](span)),
179 //BinOp::BitOr(Token![|](span)),
180 //BinOp::Shl(Token![<<](span)),
181 //BinOp::Shr(Token![>>](span)),
182 //BinOp::Eq(Token![==](span)),
183 BinOp::Lt(Token![<](span)),
184 //BinOp::Le(Token![<=](span)),
185 //BinOp::Ne(Token![!=](span)),
186 //BinOp::Ge(Token![>=](span)),
187 //BinOp::Gt(Token![>](span)),
188 BinOp::ShlAssign(Token![<<=](span)),
189 ] {
190 f(Expr::Binary(ExprBinary {
191 // `x + $expr`
192 attrs: Vec::new(),
193 left: Box::new(simple.clone()),
194 op,
195 right: Box::new(expr.clone()),
196 }));
197 f(Expr::Binary(ExprBinary {
198 // `$expr + x`
199 attrs: Vec::new(),
200 left: Box::new(expr.clone()),
201 op,
202 right: Box::new(simple.clone()),
203 }));
204 }
205 });
206 });
207
208 // Expr::Block
209 f(Expr::Block(ExprBlock {
210 // `{}`
211 attrs: Vec::new(),
212 label: None,
213 block: Block {
214 brace_token: token::Brace(span),
215 stmts: Vec::new(),
216 },
217 }));
218
219 // Expr::Break
220 f(Expr::Break(ExprBreak {
221 // `break`
222 attrs: Vec::new(),
223 break_token: Token![break](span),
224 label: None,
225 expr: None,
226 }));
227 iter(depth, &mut |expr| {
228 f(Expr::Break(ExprBreak {
229 // `break $expr`
230 attrs: Vec::new(),
231 break_token: Token![break](span),
232 label: None,
233 expr: Some(Box::new(expr)),
234 }));
235 });
236
237 // Expr::Call
238 iter(depth, &mut |expr| {
239 f(Expr::Call(ExprCall {
240 // `$expr()`
241 attrs: Vec::new(),
242 func: Box::new(expr),
243 paren_token: token::Paren(span),
244 args: Punctuated::new(),
245 }));
246 });
247
248 // Expr::Cast
249 iter(depth, &mut |expr| {
250 f(Expr::Cast(ExprCast {
251 // `$expr as T`
252 attrs: Vec::new(),
253 expr: Box::new(expr),
254 as_token: Token![as](span),
255 ty: Box::new(Type::Path(TypePath {
256 qself: None,
257 path: Path::from(Ident::new("T", span)),
258 })),
259 }));
260 });
261
262 // Expr::Closure
263 iter(depth, &mut |expr| {
264 f(Expr::Closure(ExprClosure {
265 // `|| $expr`
266 attrs: Vec::new(),
267 lifetimes: None,
268 constness: None,
269 movability: None,
270 asyncness: None,
271 capture: None,
272 or1_token: Token![|](span),
273 inputs: Punctuated::new(),
274 or2_token: Token![|](span),
275 output: ReturnType::Default,
276 body: Box::new(expr),
277 }));
278 });
279
280 // Expr::Field
281 iter(depth, &mut |expr| {
282 f(Expr::Field(ExprField {
283 // `$expr.field`
284 attrs: Vec::new(),
285 base: Box::new(expr),
286 dot_token: Token![.](span),
287 member: Member::Named(Ident::new("field", span)),
288 }));
289 });
290
291 // Expr::If
292 iter(depth, &mut |expr| {
293 f(Expr::If(ExprIf {
294 // `if $expr {}`
295 attrs: Vec::new(),
296 if_token: Token![if](span),
297 cond: Box::new(expr),
298 then_branch: Block {
299 brace_token: token::Brace(span),
300 stmts: Vec::new(),
301 },
302 else_branch: None,
303 }));
304 });
305
306 // Expr::Let
307 iter(depth, &mut |expr| {
308 f(Expr::Let(ExprLet {
309 attrs: Vec::new(),
310 let_token: Token![let](span),
311 pat: Box::new(Pat::Wild(PatWild {
312 attrs: Vec::new(),
313 underscore_token: Token![_](span),
314 })),
315 eq_token: Token![=](span),
316 expr: Box::new(expr),
317 }));
318 });
319
320 // Expr::Range
321 f(Expr::Range(ExprRange {
322 // `..`
323 attrs: Vec::new(),
324 start: None,
325 limits: RangeLimits::HalfOpen(Token![..](span)),
326 end: None,
327 }));
328 iter(depth, &mut |expr| {
329 f(Expr::Range(ExprRange {
330 // `..$expr`
331 attrs: Vec::new(),
332 start: None,
333 limits: RangeLimits::HalfOpen(Token![..](span)),
334 end: Some(Box::new(expr.clone())),
335 }));
336 f(Expr::Range(ExprRange {
337 // `$expr..`
338 attrs: Vec::new(),
339 start: Some(Box::new(expr)),
340 limits: RangeLimits::HalfOpen(Token![..](span)),
341 end: None,
342 }));
343 });
344
345 // Expr::Reference
346 iter(depth, &mut |expr| {
347 f(Expr::Reference(ExprReference {
348 // `&$expr`
349 attrs: Vec::new(),
350 and_token: Token![&](span),
351 mutability: None,
352 expr: Box::new(expr),
353 }));
354 });
355
356 // Expr::Return
357 f(Expr::Return(ExprReturn {
358 // `return`
359 attrs: Vec::new(),
360 return_token: Token![return](span),
361 expr: None,
362 }));
363 iter(depth, &mut |expr| {
364 f(Expr::Return(ExprReturn {
365 // `return $expr`
366 attrs: Vec::new(),
367 return_token: Token![return](span),
368 expr: Some(Box::new(expr)),
369 }));
370 });
371
372 // Expr::Try
373 iter(depth, &mut |expr| {
374 f(Expr::Try(ExprTry {
375 // `$expr?`
376 attrs: Vec::new(),
377 expr: Box::new(expr),
378 question_token: Token![?](span),
379 }));
380 });
381
382 // Expr::Unary
383 iter(depth, &mut |expr| {
384 for op in [
385 UnOp::Deref(Token![*](span)),
386 //UnOp::Not(Token![!](span)),
387 //UnOp::Neg(Token![-](span)),
388 ] {
389 f(Expr::Unary(ExprUnary {
390 // `*$expr`
391 attrs: Vec::new(),
392 op,
393 expr: Box::new(expr.clone()),
394 }));
395 }
396 });
397
398 if false {
399 // Expr::Array
400 f(Expr::Array(ExprArray {
401 // `[]`
402 attrs: Vec::new(),
403 bracket_token: token::Bracket(span),
404 elems: Punctuated::new(),
405 }));
406
407 // Expr::Async
408 f(Expr::Async(ExprAsync {
409 // `async {}`
410 attrs: Vec::new(),
411 async_token: Token![async](span),
412 capture: None,
413 block: Block {
414 brace_token: token::Brace(span),
415 stmts: Vec::new(),
416 },
417 }));
418
419 // Expr::Await
420 iter(depth, &mut |expr| {
421 f(Expr::Await(ExprAwait {
422 // `$expr.await`
423 attrs: Vec::new(),
424 base: Box::new(expr),
425 dot_token: Token![.](span),
426 await_token: Token![await](span),
427 }));
428 });
429
430 // Expr::Block
431 f(Expr::Block(ExprBlock {
432 // `'a: {}`
433 attrs: Vec::new(),
434 label: Some(Label {
435 name: Lifetime::new("'a", span),
436 colon_token: Token![:](span),
437 }),
438 block: Block {
439 brace_token: token::Brace(span),
440 stmts: Vec::new(),
441 },
442 }));
443 iter(depth, &mut |expr| {
444 f(Expr::Block(ExprBlock {
445 // `{ $expr }`
446 attrs: Vec::new(),
447 label: None,
448 block: Block {
449 brace_token: token::Brace(span),
450 stmts: Vec::from([Stmt::Expr(expr.clone(), None)]),
451 },
452 }));
453 f(Expr::Block(ExprBlock {
454 // `{ $expr; }`
455 attrs: Vec::new(),
456 label: None,
457 block: Block {
458 brace_token: token::Brace(span),
459 stmts: Vec::from([Stmt::Expr(expr, Some(Token![;](span)))]),
460 },
461 }));
462 });
463
464 // Expr::Break
465 f(Expr::Break(ExprBreak {
466 // `break 'a`
467 attrs: Vec::new(),
468 break_token: Token![break](span),
469 label: Some(Lifetime::new("'a", span)),
470 expr: None,
471 }));
472 iter(depth, &mut |expr| {
473 f(Expr::Break(ExprBreak {
474 // `break 'a $expr`
475 attrs: Vec::new(),
476 break_token: Token![break](span),
477 label: Some(Lifetime::new("'a", span)),
478 expr: Some(Box::new(expr)),
479 }));
480 });
481
482 // Expr::Closure
483 f(Expr::Closure(ExprClosure {
484 // `|| -> T {}`
485 attrs: Vec::new(),
486 lifetimes: None,
487 constness: None,
488 movability: None,
489 asyncness: None,
490 capture: None,
491 or1_token: Token![|](span),
492 inputs: Punctuated::new(),
493 or2_token: Token![|](span),
494 output: ReturnType::Type(
495 Token![->](span),
496 Box::new(Type::Path(TypePath {
497 qself: None,
498 path: Path::from(Ident::new("T", span)),
499 })),
500 ),
501 body: Box::new(Expr::Block(ExprBlock {
502 attrs: Vec::new(),
503 label: None,
504 block: Block {
505 brace_token: token::Brace(span),
506 stmts: Vec::new(),
507 },
508 })),
509 }));
510
511 // Expr::Const
512 f(Expr::Const(ExprConst {
513 // `const {}`
514 attrs: Vec::new(),
515 const_token: Token![const](span),
516 block: Block {
517 brace_token: token::Brace(span),
518 stmts: Vec::new(),
519 },
520 }));
521
522 // Expr::Continue
523 f(Expr::Continue(ExprContinue {
524 // `continue`
525 attrs: Vec::new(),
526 continue_token: Token![continue](span),
527 label: None,
528 }));
529 f(Expr::Continue(ExprContinue {
530 // `continue 'a`
531 attrs: Vec::new(),
532 continue_token: Token![continue](span),
533 label: Some(Lifetime::new("'a", span)),
534 }));
535
536 // Expr::ForLoop
537 iter(depth, &mut |expr| {
538 f(Expr::ForLoop(ExprForLoop {
539 // `for _ in $expr {}`
540 attrs: Vec::new(),
541 label: None,
542 for_token: Token![for](span),
543 pat: Box::new(Pat::Wild(PatWild {
544 attrs: Vec::new(),
545 underscore_token: Token![_](span),
546 })),
547 in_token: Token![in](span),
548 expr: Box::new(expr.clone()),
549 body: Block {
550 brace_token: token::Brace(span),
551 stmts: Vec::new(),
552 },
553 }));
554 f(Expr::ForLoop(ExprForLoop {
555 // `'a: for _ in $expr {}`
556 attrs: Vec::new(),
557 label: Some(Label {
558 name: Lifetime::new("'a", span),
559 colon_token: Token![:](span),
560 }),
561 for_token: Token![for](span),
562 pat: Box::new(Pat::Wild(PatWild {
563 attrs: Vec::new(),
564 underscore_token: Token![_](span),
565 })),
566 in_token: Token![in](span),
567 expr: Box::new(expr),
568 body: Block {
569 brace_token: token::Brace(span),
570 stmts: Vec::new(),
571 },
572 }));
573 });
574
575 // Expr::Index
576 iter(depth, &mut |expr| {
577 f(Expr::Index(ExprIndex {
578 // `$expr[0]`
579 attrs: Vec::new(),
580 expr: Box::new(expr),
581 bracket_token: token::Bracket(span),
582 index: Box::new(Expr::Lit(ExprLit {
583 attrs: Vec::new(),
584 lit: Lit::Int(LitInt::new("0", span)),
585 })),
586 }));
587 });
588
589 // Expr::Loop
590 f(Expr::Loop(ExprLoop {
591 // `loop {}`
592 attrs: Vec::new(),
593 label: None,
594 loop_token: Token![loop](span),
595 body: Block {
596 brace_token: token::Brace(span),
597 stmts: Vec::new(),
598 },
599 }));
600 f(Expr::Loop(ExprLoop {
601 // `'a: loop {}`
602 attrs: Vec::new(),
603 label: Some(Label {
604 name: Lifetime::new("'a", span),
605 colon_token: Token![:](span),
606 }),
607 loop_token: Token![loop](span),
608 body: Block {
609 brace_token: token::Brace(span),
610 stmts: Vec::new(),
611 },
612 }));
613
614 // Expr::Macro
615 f(Expr::Macro(ExprMacro {
616 // `m!()`
617 attrs: Vec::new(),
618 mac: Macro {
619 path: Path::from(Ident::new("m", span)),
620 bang_token: Token![!](span),
621 delimiter: MacroDelimiter::Paren(token::Paren(span)),
622 tokens: TokenStream::new(),
623 },
624 }));
625 f(Expr::Macro(ExprMacro {
626 // `m! {}`
627 attrs: Vec::new(),
628 mac: Macro {
629 path: Path::from(Ident::new("m", span)),
630 bang_token: Token![!](span),
631 delimiter: MacroDelimiter::Brace(token::Brace(span)),
632 tokens: TokenStream::new(),
633 },
634 }));
635
636 // Expr::Match
637 iter(depth, &mut |expr| {
638 f(Expr::Match(ExprMatch {
639 // `match $expr {}`
640 attrs: Vec::new(),
641 match_token: Token![match](span),
642 expr: Box::new(expr.clone()),
643 brace_token: token::Brace(span),
644 arms: Vec::new(),
645 }));
646 f(Expr::Match(ExprMatch {
647 // `match x { _ => $expr }`
648 attrs: Vec::new(),
649 match_token: Token![match](span),
650 expr: Box::new(Expr::Path(ExprPath {
651 attrs: Vec::new(),
652 qself: None,
653 path: Path::from(Ident::new("x", span)),
654 })),
655 brace_token: token::Brace(span),
656 arms: Vec::from([Arm {
657 attrs: Vec::new(),
658 pat: Pat::Wild(PatWild {
659 attrs: Vec::new(),
660 underscore_token: Token![_](span),
661 }),
662 guard: None,
663 fat_arrow_token: Token![=>](span),
664 body: Box::new(expr.clone()),
665 comma: None,
666 }]),
667 }));
668 f(Expr::Match(ExprMatch {
669 // `match x { _ if $expr => {} }`
670 attrs: Vec::new(),
671 match_token: Token![match](span),
672 expr: Box::new(Expr::Path(ExprPath {
673 attrs: Vec::new(),
674 qself: None,
675 path: Path::from(Ident::new("x", span)),
676 })),
677 brace_token: token::Brace(span),
678 arms: Vec::from([Arm {
679 attrs: Vec::new(),
680 pat: Pat::Wild(PatWild {
681 attrs: Vec::new(),
682 underscore_token: Token![_](span),
683 }),
684 guard: Some((Token![if](span), Box::new(expr))),
685 fat_arrow_token: Token![=>](span),
686 body: Box::new(Expr::Block(ExprBlock {
687 attrs: Vec::new(),
688 label: None,
689 block: Block {
690 brace_token: token::Brace(span),
691 stmts: Vec::new(),
692 },
693 })),
694 comma: None,
695 }]),
696 }));
697 });
698
699 // Expr::MethodCall
700 iter(depth, &mut |expr| {
701 f(Expr::MethodCall(ExprMethodCall {
702 // `$expr.method()`
703 attrs: Vec::new(),
704 receiver: Box::new(expr.clone()),
705 dot_token: Token![.](span),
706 method: Ident::new("method", span),
707 turbofish: None,
708 paren_token: token::Paren(span),
709 args: Punctuated::new(),
710 }));
711 f(Expr::MethodCall(ExprMethodCall {
712 // `$expr.method::<T>()`
713 attrs: Vec::new(),
714 receiver: Box::new(expr),
715 dot_token: Token![.](span),
716 method: Ident::new("method", span),
717 turbofish: Some(AngleBracketedGenericArguments {
718 colon2_token: Some(Token![::](span)),
719 lt_token: Token![<](span),
720 args: Punctuated::from_iter([GenericArgument::Type(Type::Path(
721 TypePath {
722 qself: None,
723 path: Path::from(Ident::new("T", span)),
724 },
725 ))]),
726 gt_token: Token![>](span),
727 }),
728 paren_token: token::Paren(span),
729 args: Punctuated::new(),
730 }));
731 });
732
733 // Expr::RawAddr
734 iter(depth, &mut |expr| {
735 f(Expr::RawAddr(ExprRawAddr {
736 // `&raw const $expr`
737 attrs: Vec::new(),
738 and_token: Token![&](span),
739 raw: Token![raw](span),
740 mutability: PointerMutability::Const(Token![const](span)),
741 expr: Box::new(expr),
742 }));
743 });
744
745 // Expr::Struct
746 f(Expr::Struct(ExprStruct {
747 // `Struct {}`
748 attrs: Vec::new(),
749 qself: None,
750 path: Path::from(Ident::new("Struct", span)),
751 brace_token: token::Brace(span),
752 fields: Punctuated::new(),
753 dot2_token: None,
754 rest: None,
755 }));
756
757 // Expr::TryBlock
758 f(Expr::TryBlock(ExprTryBlock {
759 // `try {}`
760 attrs: Vec::new(),
761 try_token: Token![try](span),
762 block: Block {
763 brace_token: token::Brace(span),
764 stmts: Vec::new(),
765 },
766 }));
767
768 // Expr::Unsafe
769 f(Expr::Unsafe(ExprUnsafe {
770 // `unsafe {}`
771 attrs: Vec::new(),
772 unsafe_token: Token![unsafe](span),
773 block: Block {
774 brace_token: token::Brace(span),
775 stmts: Vec::new(),
776 },
777 }));
778
779 // Expr::While
780 iter(depth, &mut |expr| {
781 f(Expr::While(ExprWhile {
782 // `while $expr {}`
783 attrs: Vec::new(),
784 label: None,
785 while_token: Token![while](span),
786 cond: Box::new(expr.clone()),
787 body: Block {
788 brace_token: token::Brace(span),
789 stmts: Vec::new(),
790 },
791 }));
792 f(Expr::While(ExprWhile {
793 // `'a: while $expr {}`
794 attrs: Vec::new(),
795 label: Some(Label {
796 name: Lifetime::new("'a", span),
797 colon_token: Token![:](span),
798 }),
799 while_token: Token![while](span),
800 cond: Box::new(expr),
801 body: Block {
802 brace_token: token::Brace(span),
803 stmts: Vec::new(),
804 },
805 }));
806 });
807
808 // Expr::Yield
809 f(Expr::Yield(ExprYield {
810 // `yield`
811 attrs: Vec::new(),
812 yield_token: Token![yield](span),
813 expr: None,
814 }));
815 iter(depth, &mut |expr| {
816 f(Expr::Yield(ExprYield {
817 // `yield $expr`
818 attrs: Vec::new(),
819 yield_token: Token![yield](span),
820 expr: Some(Box::new(expr)),
821 }));
822 });
823 }
824 }
825
826 let mut failures = 0;
827 macro_rules! fail {
828 ($($message:tt)*) => {{
829 eprintln!($($message)*);
830 failures += 1;
831 return;
832 }};
833 }
834 let mut assert = |mut original: Expr| {
835 let span = Span::call_site();
836 // `const _: () = $expr;`
837 let pretty = prettyplease::unparse(&File {
838 shebang: None,
839 attrs: Vec::new(),
840 items: Vec::from([Item::Const(ItemConst {
841 attrs: Vec::new(),
842 vis: Visibility::Inherited,
843 const_token: Token![const](span),
844 ident: Ident::from(Token![_](span)),
845 generics: Generics::default(),
846 colon_token: Token![:](span),
847 ty: Box::new(Type::Infer(TypeInfer {
848 underscore_token: Token![_](span),
849 })),
850 eq_token: Token![=](span),
851 expr: Box::new(original.clone()),
852 semi_token: Token![;](span),
853 })]),
854 });
855 let mut parsed = match syn::parse_file(&pretty) {
856 Ok(parsed) => parsed,
857 _ => fail!("failed to parse: {pretty}{original:#?}"),
858 };
859 let item = match parsed.items.as_mut_slice() {
860 [Item::Const(item)] => item,
861 _ => unreachable!(),
862 };
863 let mut parsed = mem::replace(&mut *item.expr, Expr::PLACEHOLDER);
864 AsIfPrinted.visit_expr_mut(&mut original);
865 FlattenParens.visit_expr_mut(&mut parsed);
866 if original != parsed {
867 fail!(
868 "before: {}\n{:#?}\nafter: {}\n{:#?}",
869 original.to_token_stream(),
870 original,
871 parsed.to_token_stream(),
872 parsed,
873 );
874 }
875 if pretty.contains("(||") {
876 // https://github.com/dtolnay/prettyplease/issues/99
877 return;
878 }
879 let no_paren = pretty.replace(['(', ')'], "");
880 if pretty != no_paren {
881 if let Ok(mut parsed2) = syn::parse_file(&no_paren) {
882 let item = match parsed2.items.as_mut_slice() {
883 [Item::Const(item)] => item,
884 _ => unreachable!(),
885 };
886 if original == *item.expr {
887 fail!("redundant parens: {}", pretty);
888 }
889 }
890 }
891 };
892
893 iter(if cfg!(debug_assertions) { 3 } else { 4 }, &mut assert);
894 if failures > 0 {
895 eprintln!("FAILURES: {failures}");
896 ExitCode::FAILURE
897 } else {
898 ExitCode::SUCCESS
899 }
900 }