Skip to content

Commit 50a6e46

Browse files
committed
or-let-chain-fix
1 parent b5e8f1f commit 50a6e46

11 files changed

+71
-63
lines changed

compiler/rustc_parse/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ parse_expected_expression_found_let = expected expression, found `let` statement
209209
.not_supported_or = `||` operators are not supported in let chain expressions
210210
.not_supported_parentheses = `let`s wrapped in parentheses are not supported in a context with let chains
211211
212+
parse_or_in_let_chain = `||` operators are not supported in let chain expressions
213+
212214
parse_expected_fn_path_found_fn_keyword = expected identifier, found keyword `fn`
213215
.suggestion = use `Fn` to refer to the trait
214216

compiler/rustc_parse/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ pub(crate) struct ExpectedExpressionFoundLet {
478478
pub comparison: Option<MaybeComparison>,
479479
}
480480

481+
#[derive(Diagnostic)]
482+
#[diag(parse_or_in_let_chain)]
483+
pub(crate) struct OrInLetChain {
484+
#[primary_span]
485+
pub span: Span,
486+
}
487+
481488
#[derive(Subdiagnostic, Clone, Copy)]
482489
#[multipart_suggestion(
483490
parse_maybe_missing_let,

compiler/rustc_parse/src/parser/expr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -4070,14 +4070,18 @@ impl MutVisitor for CondChecker<'_> {
40704070
match e.kind {
40714071
ExprKind::Let(_, _, _, ref mut recovered @ Recovered::No) => {
40724072
if let Some(reason) = self.forbid_let_reason {
4073-
*recovered = Recovered::Yes(self.parser.dcx().emit_err(
4074-
errors::ExpectedExpressionFoundLet {
4073+
let error = match reason {
4074+
NotSupportedOr(or_span) => {
4075+
self.parser.dcx().emit_err(errors::OrInLetChain { span: or_span })
4076+
}
4077+
_ => self.parser.dcx().emit_err(errors::ExpectedExpressionFoundLet {
40754078
span,
40764079
reason,
40774080
missing_let: self.missing_let,
40784081
comparison: self.comparison,
4079-
},
4080-
));
4082+
}),
4083+
};
4084+
*recovered = Recovered::Yes(error);
40814085
} else {
40824086
self.parser.psess.gated_spans.gate(sym::let_chains, span);
40834087
}

tests/ui/parser/or-in-let-chain.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn main() {
2+
if let true = true || false {}
3+
//~^ ERROR `||` operators are not supported in let chain expressions
4+
5+
// With parentheses
6+
if (let true = true) || false {}
7+
//~^ ERROR expected expression, found `let` statement
8+
9+
// Multiple || operators
10+
if let true = true || false || true {}
11+
//~^ ERROR `||` operators are not supported in let chain expressions
12+
13+
// Mixed operators (should still show error for ||)
14+
if let true = true && false || true {}
15+
//~^ ERROR `||` operators are not supported in let chain expressions
16+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: `||` operators are not supported in let chain expressions
2+
--> $DIR/or-in-let-chain.rs:2:24
3+
|
4+
LL | if let true = true || false {}
5+
| ^^
6+
7+
error: expected expression, found `let` statement
8+
--> $DIR/or-in-let-chain.rs:6:9
9+
|
10+
LL | if (let true = true) || false {}
11+
| ^^^^^^^^^^^^^^^
12+
|
13+
= note: only supported directly in conditions of `if` and `while` expressions
14+
15+
error: `||` operators are not supported in let chain expressions
16+
--> $DIR/or-in-let-chain.rs:10:24
17+
|
18+
LL | if let true = true || false || true {}
19+
| ^^
20+
21+
error: `||` operators are not supported in let chain expressions
22+
--> $DIR/or-in-let-chain.rs:14:33
23+
|
24+
LL | if let true = true && false || true {}
25+
| ^^
26+
27+
error: aborting due to 4 previous errors
28+

tests/ui/rfcs/rfc-2497-if-let-chains/ast-validate-guards.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
fn let_or_guard(x: Result<Option<i32>, ()>) {
44
match x {
55
Ok(opt) if let Some(4) = opt || false => {}
6-
//~^ ERROR expected expression, found `let` statement
6+
//~^ ERROR `||` operators are not supported in let chain expressions
77
_ => {}
88
}
99
}

tests/ui/rfcs/rfc-2497-if-let-chains/ast-validate-guards.stderr

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
error: expected expression, found `let` statement
2-
--> $DIR/ast-validate-guards.rs:5:20
3-
|
4-
LL | Ok(opt) if let Some(4) = opt || false => {}
5-
| ^^^^^^^^^^^^^^^^^
6-
|
7-
= note: only supported directly in conditions of `if` and `while` expressions
8-
note: `||` operators are not supported in let chain expressions
1+
error: `||` operators are not supported in let chain expressions
92
--> $DIR/ast-validate-guards.rs:5:38
103
|
114
LL | Ok(opt) if let Some(4) = opt || false => {}

tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.feature.stderr

+2-16
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
272272
|
273273
= note: only supported directly in conditions of `if` and `while` expressions
274274

275-
error: expected expression, found `let` statement
276-
--> $DIR/disallowed-positions.rs:121:16
277-
|
278-
LL | if true || let 0 = 0 {}
279-
| ^^^^^^^^^
280-
|
281-
= note: only supported directly in conditions of `if` and `while` expressions
282-
note: `||` operators are not supported in let chain expressions
275+
error: `||` operators are not supported in let chain expressions
283276
--> $DIR/disallowed-positions.rs:121:13
284277
|
285278
LL | if true || let 0 = 0 {}
@@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
485478
|
486479
= note: only supported directly in conditions of `if` and `while` expressions
487480

488-
error: expected expression, found `let` statement
489-
--> $DIR/disallowed-positions.rs:212:19
490-
|
491-
LL | while true || let 0 = 0 {}
492-
| ^^^^^^^^^
493-
|
494-
= note: only supported directly in conditions of `if` and `while` expressions
495-
note: `||` operators are not supported in let chain expressions
481+
error: `||` operators are not supported in let chain expressions
496482
--> $DIR/disallowed-positions.rs:212:16
497483
|
498484
LL | while true || let 0 = 0 {}

tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.no_feature.stderr

+2-16
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
272272
|
273273
= note: only supported directly in conditions of `if` and `while` expressions
274274

275-
error: expected expression, found `let` statement
276-
--> $DIR/disallowed-positions.rs:121:16
277-
|
278-
LL | if true || let 0 = 0 {}
279-
| ^^^^^^^^^
280-
|
281-
= note: only supported directly in conditions of `if` and `while` expressions
282-
note: `||` operators are not supported in let chain expressions
275+
error: `||` operators are not supported in let chain expressions
283276
--> $DIR/disallowed-positions.rs:121:13
284277
|
285278
LL | if true || let 0 = 0 {}
@@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
485478
|
486479
= note: only supported directly in conditions of `if` and `while` expressions
487480

488-
error: expected expression, found `let` statement
489-
--> $DIR/disallowed-positions.rs:212:19
490-
|
491-
LL | while true || let 0 = 0 {}
492-
| ^^^^^^^^^
493-
|
494-
= note: only supported directly in conditions of `if` and `while` expressions
495-
note: `||` operators are not supported in let chain expressions
481+
error: `||` operators are not supported in let chain expressions
496482
--> $DIR/disallowed-positions.rs:212:16
497483
|
498484
LL | while true || let 0 = 0 {}

tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.nothing.stderr

+2-16
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
272272
|
273273
= note: only supported directly in conditions of `if` and `while` expressions
274274

275-
error: expected expression, found `let` statement
276-
--> $DIR/disallowed-positions.rs:121:16
277-
|
278-
LL | if true || let 0 = 0 {}
279-
| ^^^^^^^^^
280-
|
281-
= note: only supported directly in conditions of `if` and `while` expressions
282-
note: `||` operators are not supported in let chain expressions
275+
error: `||` operators are not supported in let chain expressions
283276
--> $DIR/disallowed-positions.rs:121:13
284277
|
285278
LL | if true || let 0 = 0 {}
@@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
485478
|
486479
= note: only supported directly in conditions of `if` and `while` expressions
487480

488-
error: expected expression, found `let` statement
489-
--> $DIR/disallowed-positions.rs:212:19
490-
|
491-
LL | while true || let 0 = 0 {}
492-
| ^^^^^^^^^
493-
|
494-
= note: only supported directly in conditions of `if` and `while` expressions
495-
note: `||` operators are not supported in let chain expressions
481+
error: `||` operators are not supported in let chain expressions
496482
--> $DIR/disallowed-positions.rs:212:16
497483
|
498484
LL | while true || let 0 = 0 {}

tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn nested_within_if_expr() {
119119
//~^ ERROR expected expression, found `let` statement
120120

121121
if true || let 0 = 0 {}
122-
//~^ ERROR expected expression, found `let` statement
122+
//~^ ERROR `||` operators are not supported in let chain expressions
123123
if (true || let 0 = 0) {}
124124
//~^ ERROR expected expression, found `let` statement
125125
if true && (true || let 0 = 0) {}
@@ -210,7 +210,7 @@ fn nested_within_while_expr() {
210210
//~^ ERROR expected expression, found `let` statement
211211

212212
while true || let 0 = 0 {}
213-
//~^ ERROR expected expression, found `let` statement
213+
//~^ ERROR `||` operators are not supported in let chain expressions
214214
while (true || let 0 = 0) {}
215215
//~^ ERROR expected expression, found `let` statement
216216
while true && (true || let 0 = 0) {}

0 commit comments

Comments
 (0)