1+ // ignore-tidy-filelength
12use super :: diagnostics:: SnapshotParser ;
23use super :: pat:: { CommaRecoveryMode , Expected , RecoverColon , RecoverComma } ;
34use super :: ty:: { AllowPlus , RecoverQPath , RecoverReturnSign } ;
@@ -2477,7 +2478,7 @@ impl<'a> Parser<'a> {
24772478 let mut cond =
24782479 self . parse_expr_res ( Restrictions :: NO_STRUCT_LITERAL | Restrictions :: ALLOW_LET , None ) ?;
24792480
2480- CondChecker { parser : self , forbid_let_reason : None } . visit_expr ( & mut cond) ;
2481+ CondChecker :: new ( self ) . visit_expr ( & mut cond) ;
24812482
24822483 if let ExprKind :: Let ( _, _, _, None ) = cond. kind {
24832484 // Remove the last feature gating of a `let` expression since it's stable.
@@ -2493,6 +2494,8 @@ impl<'a> Parser<'a> {
24932494 let err = errors:: ExpectedExpressionFoundLet {
24942495 span : self . token . span ,
24952496 reason : ForbiddenLetReason :: OtherForbidden ,
2497+ missing_let : None ,
2498+ comparison : None ,
24962499 } ;
24972500 if self . prev_token . kind == token:: BinOp ( token:: Or ) {
24982501 // This was part of a closure, the that part of the parser recover.
@@ -2876,7 +2879,7 @@ impl<'a> Parser<'a> {
28762879 let if_span = this. prev_token . span ;
28772880 let mut cond = this. parse_match_guard_condition ( ) ?;
28782881
2879- CondChecker { parser : this , forbid_let_reason : None } . visit_expr ( & mut cond) ;
2882+ CondChecker :: new ( this ) . visit_expr ( & mut cond) ;
28802883
28812884 let ( has_let_expr, does_not_have_bin_op) = check_let_expr ( & cond) ;
28822885 if has_let_expr {
@@ -3552,6 +3555,14 @@ pub(crate) enum ForbiddenLetReason {
35523555struct CondChecker < ' a > {
35533556 parser : & ' a Parser < ' a > ,
35543557 forbid_let_reason : Option < ForbiddenLetReason > ,
3558+ missing_let : Option < errors:: MaybeMissingLet > ,
3559+ comparison : Option < errors:: MaybeComparison > ,
3560+ }
3561+
3562+ impl < ' a > CondChecker < ' a > {
3563+ fn new ( parser : & ' a Parser < ' a > ) -> Self {
3564+ CondChecker { parser, forbid_let_reason : None , missing_let : None , comparison : None }
3565+ }
35553566}
35563567
35573568impl MutVisitor for CondChecker < ' _ > {
@@ -3562,11 +3573,13 @@ impl MutVisitor for CondChecker<'_> {
35623573 match e. kind {
35633574 ExprKind :: Let ( _, _, _, ref mut is_recovered @ None ) => {
35643575 if let Some ( reason) = self . forbid_let_reason {
3565- * is_recovered = Some (
3566- self . parser
3567- . sess
3568- . emit_err ( errors:: ExpectedExpressionFoundLet { span, reason } ) ,
3569- ) ;
3576+ * is_recovered =
3577+ Some ( self . parser . sess . emit_err ( errors:: ExpectedExpressionFoundLet {
3578+ span,
3579+ reason,
3580+ missing_let : self . missing_let ,
3581+ comparison : self . comparison ,
3582+ } ) ) ;
35703583 } else {
35713584 self . parser . sess . gated_spans . gate ( sym:: let_chains, span) ;
35723585 }
@@ -3590,9 +3603,28 @@ impl MutVisitor for CondChecker<'_> {
35903603 noop_visit_expr ( e, self ) ;
35913604 self . forbid_let_reason = forbid_let_reason;
35923605 }
3606+ ExprKind :: Assign ( ref lhs, _, span) => {
3607+ let forbid_let_reason = self . forbid_let_reason ;
3608+ self . forbid_let_reason = Some ( OtherForbidden ) ;
3609+ let missing_let = self . missing_let ;
3610+ if let ExprKind :: Binary ( _, _, rhs) = & lhs. kind
3611+ && let ExprKind :: Path ( _, _)
3612+ | ExprKind :: Struct ( _)
3613+ | ExprKind :: Call ( _, _)
3614+ | ExprKind :: Array ( _) = rhs. kind
3615+ {
3616+ self . missing_let =
3617+ Some ( errors:: MaybeMissingLet { span : rhs. span . shrink_to_lo ( ) } ) ;
3618+ }
3619+ let comparison = self . comparison ;
3620+ self . comparison = Some ( errors:: MaybeComparison { span : span. shrink_to_hi ( ) } ) ;
3621+ noop_visit_expr ( e, self ) ;
3622+ self . forbid_let_reason = forbid_let_reason;
3623+ self . missing_let = missing_let;
3624+ self . comparison = comparison;
3625+ }
35933626 ExprKind :: Unary ( _, _)
35943627 | ExprKind :: Await ( _, _)
3595- | ExprKind :: Assign ( _, _, _)
35963628 | ExprKind :: AssignOp ( _, _, _)
35973629 | ExprKind :: Range ( _, _, _)
35983630 | ExprKind :: Try ( _)
0 commit comments