Skip to content

Commit

Permalink
Reduce double errors for invalid let expressions
Browse files Browse the repository at this point in the history
Previously some invalid let expressions would result in both a feature
error and a parsing error. Avoid this and ensure that we only emit the
parsing error when this happens.
  • Loading branch information
matthewjasper committed Sep 11, 2023
1 parent 2d7a5f5 commit b011a0a
Show file tree
Hide file tree
Showing 7 changed files with 1,249 additions and 140 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2485,9 +2485,6 @@ impl<'a> Parser<'a> {
}
let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())?;
let span = lo.to(expr.span);
if is_recovered.is_none() {
self.sess.gated_spans.gate(sym::let_chains, span);
}
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, is_recovered)))
}

Expand Down Expand Up @@ -3460,6 +3457,8 @@ impl MutVisitor for CondChecker<'_> {
.sess
.emit_err(errors::ExpectedExpressionFoundLet { span, reason }),
);
} else {
self.parser.sess.gated_spans.gate(sym::let_chains, span);
}
}
ExprKind::Binary(Spanned { node: BinOpKind::And, .. }, _, _) => {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/expr/if/bad-if-let-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
fn a() {
if let x = 1 && i = 2 {}
//~^ ERROR cannot find value `i` in this scope
//~| ERROR `let` expressions in this position are unstable
//~| ERROR mismatched types
//~| ERROR expected expression, found `let` statement
}
Expand Down
21 changes: 6 additions & 15 deletions tests/ui/expr/if/bad-if-let-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | if let x = 1 && i = 2 {}
| ^ not found in this scope

error[E0425]: cannot find value `i` in this scope
--> $DIR/bad-if-let-suggestion.rs:13:9
--> $DIR/bad-if-let-suggestion.rs:12:9
|
LL | fn a() {
| ------ similarly named function `a` defined here
Expand All @@ -20,7 +20,7 @@ LL | if (i + j) = i {}
| ^ help: a function with a similar name exists: `a`

error[E0425]: cannot find value `j` in this scope
--> $DIR/bad-if-let-suggestion.rs:13:13
--> $DIR/bad-if-let-suggestion.rs:12:13
|
LL | fn a() {
| ------ similarly named function `a` defined here
Expand All @@ -29,7 +29,7 @@ LL | if (i + j) = i {}
| ^ help: a function with a similar name exists: `a`

error[E0425]: cannot find value `i` in this scope
--> $DIR/bad-if-let-suggestion.rs:13:18
--> $DIR/bad-if-let-suggestion.rs:12:18
|
LL | fn a() {
| ------ similarly named function `a` defined here
Expand All @@ -38,23 +38,14 @@ LL | if (i + j) = i {}
| ^ help: a function with a similar name exists: `a`

error[E0425]: cannot find value `x` in this scope
--> $DIR/bad-if-let-suggestion.rs:20:8
--> $DIR/bad-if-let-suggestion.rs:19:8
|
LL | fn a() {
| ------ similarly named function `a` defined here
...
LL | if x[0] = 1 {}
| ^ help: a function with a similar name exists: `a`

error[E0658]: `let` expressions in this position are unstable
--> $DIR/bad-if-let-suggestion.rs:5:8
|
LL | if let x = 1 && i = 2 {}
| ^^^^^^^^^
|
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0308]: mismatched types
--> $DIR/bad-if-let-suggestion.rs:5:8
|
Expand All @@ -66,7 +57,7 @@ help: you might have meant to compare for equality
LL | if let x = 1 && i == 2 {}
| +

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0308, E0425, E0658.
Some errors have detailed explanations: E0308, E0425.
For more information about an error, try `rustc --explain E0308`.
9 changes: 0 additions & 9 deletions tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ fn _if_let_guard() {

() if (let 0 = 1) => {}
//~^ ERROR expected expression, found `let` statement
//~| ERROR `let` expressions in this position are unstable

() if (((let 0 = 1))) => {}
//~^ ERROR expected expression, found `let` statement
//~| ERROR `let` expressions in this position are unstable

() if true && let 0 = 1 => {}
//~^ ERROR `if let` guards are experimental
Expand All @@ -25,25 +23,18 @@ fn _if_let_guard() {

() if (let 0 = 1) && true => {}
//~^ ERROR expected expression, found `let` statement
//~| ERROR `let` expressions in this position are unstable

() if true && (let 0 = 1) => {}
//~^ ERROR expected expression, found `let` statement
//~| ERROR `let` expressions in this position are unstable

() if (let 0 = 1) && (let 0 = 1) => {}
//~^ ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable

() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
//~^ ERROR `if let` guards are experimental
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
//~| ERROR expected expression, found `let` statement
Expand Down

0 comments on commit b011a0a

Please sign in to comment.