@@ -39,7 +39,9 @@ pub use crate::format::*;
39
39
use crate :: ptr:: P ;
40
40
use crate :: token:: { self , CommentKind , Delimiter } ;
41
41
use crate :: tokenstream:: { DelimSpan , LazyAttrTokenStream , TokenStream } ;
42
- pub use crate :: util:: parser:: ExprPrecedence ;
42
+ use crate :: util:: parser:: {
43
+ AssocOp , PREC_CLOSURE , PREC_JUMP , PREC_PREFIX , PREC_RANGE , PREC_UNAMBIGUOUS ,
44
+ } ;
43
45
44
46
/// A "Label" is an identifier of some point in sources,
45
47
/// e.g. in the following code:
@@ -428,7 +430,15 @@ impl Default for WhereClause {
428
430
429
431
/// A single predicate in a where-clause.
430
432
#[ derive( Clone , Encodable , Decodable , Debug ) ]
431
- pub enum WherePredicate {
433
+ pub struct WherePredicate {
434
+ pub kind : WherePredicateKind ,
435
+ pub id : NodeId ,
436
+ pub span : Span ,
437
+ }
438
+
439
+ /// Predicate kind in where-clause.
440
+ #[ derive( Clone , Encodable , Decodable , Debug ) ]
441
+ pub enum WherePredicateKind {
432
442
/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
433
443
BoundPredicate ( WhereBoundPredicate ) ,
434
444
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
@@ -437,22 +447,11 @@ pub enum WherePredicate {
437
447
EqPredicate ( WhereEqPredicate ) ,
438
448
}
439
449
440
- impl WherePredicate {
441
- pub fn span ( & self ) -> Span {
442
- match self {
443
- WherePredicate :: BoundPredicate ( p) => p. span ,
444
- WherePredicate :: RegionPredicate ( p) => p. span ,
445
- WherePredicate :: EqPredicate ( p) => p. span ,
446
- }
447
- }
448
- }
449
-
450
450
/// A type bound.
451
451
///
452
452
/// E.g., `for<'c> Foo: Send + Clone + 'c`.
453
453
#[ derive( Clone , Encodable , Decodable , Debug ) ]
454
454
pub struct WhereBoundPredicate {
455
- pub span : Span ,
456
455
/// Any generics from a `for` binding.
457
456
pub bound_generic_params : ThinVec < GenericParam > ,
458
457
/// The type being bounded.
@@ -466,7 +465,6 @@ pub struct WhereBoundPredicate {
466
465
/// E.g., `'a: 'b + 'c`.
467
466
#[ derive( Clone , Encodable , Decodable , Debug ) ]
468
467
pub struct WhereRegionPredicate {
469
- pub span : Span ,
470
468
pub lifetime : Lifetime ,
471
469
pub bounds : GenericBounds ,
472
470
}
@@ -476,7 +474,6 @@ pub struct WhereRegionPredicate {
476
474
/// E.g., `T = int`.
477
475
#[ derive( Clone , Encodable , Decodable , Debug ) ]
478
476
pub struct WhereEqPredicate {
479
- pub span : Span ,
480
477
pub lhs_ty : P < Ty > ,
481
478
pub rhs_ty : P < Ty > ,
482
479
}
@@ -1319,53 +1316,71 @@ impl Expr {
1319
1316
Some ( P ( Ty { kind, id : self . id , span : self . span , tokens : None } ) )
1320
1317
}
1321
1318
1322
- pub fn precedence ( & self ) -> ExprPrecedence {
1319
+ pub fn precedence ( & self ) -> i8 {
1323
1320
match self . kind {
1324
- ExprKind :: Array ( _) => ExprPrecedence :: Array ,
1325
- ExprKind :: ConstBlock ( _) => ExprPrecedence :: ConstBlock ,
1326
- ExprKind :: Call ( ..) => ExprPrecedence :: Call ,
1327
- ExprKind :: MethodCall ( ..) => ExprPrecedence :: MethodCall ,
1328
- ExprKind :: Tup ( _) => ExprPrecedence :: Tup ,
1329
- ExprKind :: Binary ( op, ..) => ExprPrecedence :: Binary ( op. node ) ,
1330
- ExprKind :: Unary ( ..) => ExprPrecedence :: Unary ,
1331
- ExprKind :: Lit ( _) | ExprKind :: IncludedBytes ( ..) => ExprPrecedence :: Lit ,
1332
- ExprKind :: Cast ( ..) => ExprPrecedence :: Cast ,
1333
- ExprKind :: Let ( ..) => ExprPrecedence :: Let ,
1334
- ExprKind :: If ( ..) => ExprPrecedence :: If ,
1335
- ExprKind :: While ( ..) => ExprPrecedence :: While ,
1336
- ExprKind :: ForLoop { .. } => ExprPrecedence :: ForLoop ,
1337
- ExprKind :: Loop ( ..) => ExprPrecedence :: Loop ,
1338
- ExprKind :: Match ( _, _, MatchKind :: Prefix ) => ExprPrecedence :: Match ,
1339
- ExprKind :: Match ( _, _, MatchKind :: Postfix ) => ExprPrecedence :: PostfixMatch ,
1340
- ExprKind :: Closure ( ..) => ExprPrecedence :: Closure ,
1341
- ExprKind :: Block ( ..) => ExprPrecedence :: Block ,
1342
- ExprKind :: TryBlock ( ..) => ExprPrecedence :: TryBlock ,
1343
- ExprKind :: Gen ( ..) => ExprPrecedence :: Gen ,
1344
- ExprKind :: Await ( ..) => ExprPrecedence :: Await ,
1345
- ExprKind :: Assign ( ..) => ExprPrecedence :: Assign ,
1346
- ExprKind :: AssignOp ( ..) => ExprPrecedence :: AssignOp ,
1347
- ExprKind :: Field ( ..) => ExprPrecedence :: Field ,
1348
- ExprKind :: Index ( ..) => ExprPrecedence :: Index ,
1349
- ExprKind :: Range ( ..) => ExprPrecedence :: Range ,
1350
- ExprKind :: Underscore => ExprPrecedence :: Path ,
1351
- ExprKind :: Path ( ..) => ExprPrecedence :: Path ,
1352
- ExprKind :: AddrOf ( ..) => ExprPrecedence :: AddrOf ,
1353
- ExprKind :: Break ( ..) => ExprPrecedence :: Break ,
1354
- ExprKind :: Continue ( ..) => ExprPrecedence :: Continue ,
1355
- ExprKind :: Ret ( ..) => ExprPrecedence :: Ret ,
1356
- ExprKind :: Struct ( ..) => ExprPrecedence :: Struct ,
1357
- ExprKind :: Repeat ( ..) => ExprPrecedence :: Repeat ,
1358
- ExprKind :: Paren ( ..) => ExprPrecedence :: Paren ,
1359
- ExprKind :: Try ( ..) => ExprPrecedence :: Try ,
1360
- ExprKind :: Yield ( ..) => ExprPrecedence :: Yield ,
1361
- ExprKind :: Yeet ( ..) => ExprPrecedence :: Yeet ,
1362
- ExprKind :: Become ( ..) => ExprPrecedence :: Become ,
1363
- ExprKind :: InlineAsm ( ..)
1364
- | ExprKind :: Type ( ..)
1365
- | ExprKind :: OffsetOf ( ..)
1321
+ ExprKind :: Closure ( ..) => PREC_CLOSURE ,
1322
+
1323
+ ExprKind :: Break ( ..)
1324
+ | ExprKind :: Continue ( ..)
1325
+ | ExprKind :: Ret ( ..)
1326
+ | ExprKind :: Yield ( ..)
1327
+ | ExprKind :: Yeet ( ..)
1328
+ | ExprKind :: Become ( ..) => PREC_JUMP ,
1329
+
1330
+ // `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
1331
+ // parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
1332
+ // ensures that `pprust` will add parentheses in the right places to get the desired
1333
+ // parse.
1334
+ ExprKind :: Range ( ..) => PREC_RANGE ,
1335
+
1336
+ // Binop-like expr kinds, handled by `AssocOp`.
1337
+ ExprKind :: Binary ( op, ..) => AssocOp :: from_ast_binop ( op. node ) . precedence ( ) as i8 ,
1338
+ ExprKind :: Cast ( ..) => AssocOp :: As . precedence ( ) as i8 ,
1339
+
1340
+ ExprKind :: Assign ( ..) |
1341
+ ExprKind :: AssignOp ( ..) => AssocOp :: Assign . precedence ( ) as i8 ,
1342
+
1343
+ // Unary, prefix
1344
+ ExprKind :: AddrOf ( ..)
1345
+ // Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
1346
+ // However, this is not exactly right. When `let _ = a` is the LHS of a binop we
1347
+ // need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
1348
+ // but we need to print `(let _ = a) < b` as-is with parens.
1349
+ | ExprKind :: Let ( ..)
1350
+ | ExprKind :: Unary ( ..) => PREC_PREFIX ,
1351
+
1352
+ // Never need parens
1353
+ ExprKind :: Array ( _)
1354
+ | ExprKind :: Await ( ..)
1355
+ | ExprKind :: Block ( ..)
1356
+ | ExprKind :: Call ( ..)
1357
+ | ExprKind :: ConstBlock ( _)
1358
+ | ExprKind :: Field ( ..)
1359
+ | ExprKind :: ForLoop { .. }
1366
1360
| ExprKind :: FormatArgs ( ..)
1367
- | ExprKind :: MacCall ( ..) => ExprPrecedence :: Mac ,
1368
- ExprKind :: Err ( _) | ExprKind :: Dummy => ExprPrecedence :: Err ,
1361
+ | ExprKind :: Gen ( ..)
1362
+ | ExprKind :: If ( ..)
1363
+ | ExprKind :: IncludedBytes ( ..)
1364
+ | ExprKind :: Index ( ..)
1365
+ | ExprKind :: InlineAsm ( ..)
1366
+ | ExprKind :: Lit ( _)
1367
+ | ExprKind :: Loop ( ..)
1368
+ | ExprKind :: MacCall ( ..)
1369
+ | ExprKind :: Match ( ..)
1370
+ | ExprKind :: MethodCall ( ..)
1371
+ | ExprKind :: OffsetOf ( ..)
1372
+ | ExprKind :: Paren ( ..)
1373
+ | ExprKind :: Path ( ..)
1374
+ | ExprKind :: Repeat ( ..)
1375
+ | ExprKind :: Struct ( ..)
1376
+ | ExprKind :: Try ( ..)
1377
+ | ExprKind :: TryBlock ( ..)
1378
+ | ExprKind :: Tup ( _)
1379
+ | ExprKind :: Type ( ..)
1380
+ | ExprKind :: Underscore
1381
+ | ExprKind :: While ( ..)
1382
+ | ExprKind :: Err ( _)
1383
+ | ExprKind :: Dummy => PREC_UNAMBIGUOUS ,
1369
1384
}
1370
1385
}
1371
1386
@@ -3063,6 +3078,7 @@ pub struct FieldDef {
3063
3078
pub id : NodeId ,
3064
3079
pub span : Span ,
3065
3080
pub vis : Visibility ,
3081
+ pub safety : Safety ,
3066
3082
pub ident : Option < Ident > ,
3067
3083
3068
3084
pub ty : P < Ty > ,
0 commit comments