@@ -39,7 +39,9 @@ pub use crate::format::*;
3939use crate :: ptr:: P ;
4040use crate :: token:: { self , CommentKind , Delimiter } ;
4141use 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+ } ;
4345
4446/// A "Label" is an identifier of some point in sources,
4547/// e.g. in the following code:
@@ -428,7 +430,15 @@ impl Default for WhereClause {
428430
429431/// A single predicate in a where-clause.
430432#[ 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 {
432442 /// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
433443 BoundPredicate ( WhereBoundPredicate ) ,
434444 /// A lifetime predicate (e.g., `'a: 'b + 'c`).
@@ -437,22 +447,11 @@ pub enum WherePredicate {
437447 EqPredicate ( WhereEqPredicate ) ,
438448}
439449
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-
450450/// A type bound.
451451///
452452/// E.g., `for<'c> Foo: Send + Clone + 'c`.
453453#[ derive( Clone , Encodable , Decodable , Debug ) ]
454454pub struct WhereBoundPredicate {
455- pub span : Span ,
456455 /// Any generics from a `for` binding.
457456 pub bound_generic_params : ThinVec < GenericParam > ,
458457 /// The type being bounded.
@@ -466,7 +465,6 @@ pub struct WhereBoundPredicate {
466465/// E.g., `'a: 'b + 'c`.
467466#[ derive( Clone , Encodable , Decodable , Debug ) ]
468467pub struct WhereRegionPredicate {
469- pub span : Span ,
470468 pub lifetime : Lifetime ,
471469 pub bounds : GenericBounds ,
472470}
@@ -476,7 +474,6 @@ pub struct WhereRegionPredicate {
476474/// E.g., `T = int`.
477475#[ derive( Clone , Encodable , Decodable , Debug ) ]
478476pub struct WhereEqPredicate {
479- pub span : Span ,
480477 pub lhs_ty : P < Ty > ,
481478 pub rhs_ty : P < Ty > ,
482479}
@@ -1319,53 +1316,71 @@ impl Expr {
13191316 Some ( P ( Ty { kind, id : self . id , span : self . span , tokens : None } ) )
13201317 }
13211318
1322- pub fn precedence ( & self ) -> ExprPrecedence {
1319+ pub fn precedence ( & self ) -> i8 {
13231320 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 { .. }
13661360 | 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 ,
13691384 }
13701385 }
13711386
@@ -3063,6 +3078,7 @@ pub struct FieldDef {
30633078 pub id : NodeId ,
30643079 pub span : Span ,
30653080 pub vis : Visibility ,
3081+ pub safety : Safety ,
30663082 pub ident : Option < Ident > ,
30673083
30683084 pub ty : P < Ty > ,
0 commit comments