Skip to content

Commit b011a0a

Browse files
committed
Reduce double errors for invalid let expressions
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.
1 parent 2d7a5f5 commit b011a0a

File tree

7 files changed

+1249
-140
lines changed

7 files changed

+1249
-140
lines changed

compiler/rustc_parse/src/parser/expr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2485,9 +2485,6 @@ impl<'a> Parser<'a> {
24852485
}
24862486
let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())?;
24872487
let span = lo.to(expr.span);
2488-
if is_recovered.is_none() {
2489-
self.sess.gated_spans.gate(sym::let_chains, span);
2490-
}
24912488
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, is_recovered)))
24922489
}
24932490

@@ -3460,6 +3457,8 @@ impl MutVisitor for CondChecker<'_> {
34603457
.sess
34613458
.emit_err(errors::ExpectedExpressionFoundLet { span, reason }),
34623459
);
3460+
} else {
3461+
self.parser.sess.gated_spans.gate(sym::let_chains, span);
34633462
}
34643463
}
34653464
ExprKind::Binary(Spanned { node: BinOpKind::And, .. }, _, _) => {

tests/ui/expr/if/bad-if-let-suggestion.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
fn a() {
55
if let x = 1 && i = 2 {}
66
//~^ ERROR cannot find value `i` in this scope
7-
//~| ERROR `let` expressions in this position are unstable
87
//~| ERROR mismatched types
98
//~| ERROR expected expression, found `let` statement
109
}

tests/ui/expr/if/bad-if-let-suggestion.stderr

+6-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | if let x = 1 && i = 2 {}
1111
| ^ not found in this scope
1212

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

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

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

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

49-
error[E0658]: `let` expressions in this position are unstable
50-
--> $DIR/bad-if-let-suggestion.rs:5:8
51-
|
52-
LL | if let x = 1 && i = 2 {}
53-
| ^^^^^^^^^
54-
|
55-
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
56-
= help: add `#![feature(let_chains)]` to the crate attributes to enable
57-
5849
error[E0308]: mismatched types
5950
--> $DIR/bad-if-let-suggestion.rs:5:8
6051
|
@@ -66,7 +57,7 @@ help: you might have meant to compare for equality
6657
LL | if let x = 1 && i == 2 {}
6758
| +
6859

69-
error: aborting due to 8 previous errors
60+
error: aborting due to 7 previous errors
7061

71-
Some errors have detailed explanations: E0308, E0425, E0658.
62+
Some errors have detailed explanations: E0308, E0425.
7263
For more information about an error, try `rustc --explain E0308`.

tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.rs

-9
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ fn _if_let_guard() {
99

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

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

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

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

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

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

4034
() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
4135
//~^ ERROR `if let` guards are experimental
4236
//~| ERROR `let` expressions in this position are unstable
4337
//~| ERROR `let` expressions in this position are unstable
44-
//~| ERROR `let` expressions in this position are unstable
45-
//~| ERROR `let` expressions in this position are unstable
46-
//~| ERROR `let` expressions in this position are unstable
4738
//~| ERROR expected expression, found `let` statement
4839
//~| ERROR expected expression, found `let` statement
4940
//~| ERROR expected expression, found `let` statement

0 commit comments

Comments
 (0)