@@ -327,7 +327,9 @@ impl<'a> Parser<'a> {
327
327
this. parse_expr_assoc_with ( prec + prec_adjustment, LhsExpr :: NotYetParsed )
328
328
} ) ?;
329
329
330
- let span = self . mk_expr_sp ( & lhs, lhs_span, rhs. span ) ;
330
+ self . error_ambiguous_outer_attrs ( & lhs, lhs_span, rhs. span ) ;
331
+ let span = lhs_span. to ( rhs. span ) ;
332
+
331
333
lhs = match op {
332
334
AssocOp :: Add
333
335
| AssocOp :: Subtract
@@ -426,6 +428,18 @@ impl<'a> Parser<'a> {
426
428
} ) ;
427
429
}
428
430
431
+ fn error_ambiguous_outer_attrs ( & self , lhs : & P < Expr > , lhs_span : Span , rhs_span : Span ) {
432
+ if let Some ( attr) = lhs. attrs . iter ( ) . find ( |a| a. style == AttrStyle :: Outer ) {
433
+ self . dcx ( ) . emit_err ( errors:: AmbiguousOuterAttributes {
434
+ span : attr. span . to ( rhs_span) ,
435
+ sugg : errors:: WrapInParentheses :: Expression {
436
+ left : attr. span . shrink_to_lo ( ) ,
437
+ right : lhs_span. shrink_to_hi ( ) ,
438
+ } ,
439
+ } ) ;
440
+ }
441
+ }
442
+
429
443
/// Possibly translate the current token to an associative operator.
430
444
/// The method does not advance the current token.
431
445
///
@@ -506,7 +520,8 @@ impl<'a> Parser<'a> {
506
520
None
507
521
} ;
508
522
let rhs_span = rhs. as_ref ( ) . map_or ( cur_op_span, |x| x. span ) ;
509
- let span = self . mk_expr_sp ( & lhs, lhs. span , rhs_span) ;
523
+ self . error_ambiguous_outer_attrs ( & lhs, lhs. span , rhs_span) ;
524
+ let span = lhs. span . to ( rhs_span) ;
510
525
let limits =
511
526
if op == AssocOp :: DotDot { RangeLimits :: HalfOpen } else { RangeLimits :: Closed } ;
512
527
let range = self . mk_range ( Some ( lhs) , rhs, limits) ;
@@ -722,7 +737,8 @@ impl<'a> Parser<'a> {
722
737
expr_kind : fn ( P < Expr > , P < Ty > ) -> ExprKind ,
723
738
) -> PResult < ' a , P < Expr > > {
724
739
let mk_expr = |this : & mut Self , lhs : P < Expr > , rhs : P < Ty > | {
725
- this. mk_expr ( this. mk_expr_sp ( & lhs, lhs_span, rhs. span ) , expr_kind ( lhs, rhs) )
740
+ this. error_ambiguous_outer_attrs ( & lhs, lhs_span, rhs. span ) ;
741
+ this. mk_expr ( lhs_span. to ( rhs. span ) , expr_kind ( lhs, rhs) )
726
742
} ;
727
743
728
744
// Save the state of the parser before parsing type normally, in case there is a
@@ -3807,16 +3823,6 @@ impl<'a> Parser<'a> {
3807
3823
self . mk_expr ( span, ExprKind :: Err ( guar) )
3808
3824
}
3809
3825
3810
- /// Create expression span ensuring the span of the parent node
3811
- /// is larger than the span of lhs and rhs, including the attributes.
3812
- fn mk_expr_sp ( & self , lhs : & P < Expr > , lhs_span : Span , rhs_span : Span ) -> Span {
3813
- lhs. attrs
3814
- . iter ( )
3815
- . find ( |a| a. style == AttrStyle :: Outer )
3816
- . map_or ( lhs_span, |a| a. span )
3817
- . to ( rhs_span)
3818
- }
3819
-
3820
3826
fn collect_tokens_for_expr (
3821
3827
& mut self ,
3822
3828
attrs : AttrWrapper ,
0 commit comments