Skip to content

Improve error message for || (or) in let chains #140272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ parse_note_pattern_alternatives_use_single_vert = alternatives in or-patterns ar
parse_nul_in_c_str = null characters in C string literals are not supported
parse_or_in_let_chain = `||` operators are not supported in let chain conditions
parse_or_pattern_not_allowed_in_fn_parameters = top-level or-patterns are not allowed in function parameters
parse_or_pattern_not_allowed_in_let_binding = top-level or-patterns are not allowed in `let` bindings
parse_out_of_range_hex_escape = out of range hex escape
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ pub(crate) struct ExpectedExpressionFoundLet {
pub comparison: Option<MaybeComparison>,
}

#[derive(Diagnostic)]
#[diag(parse_or_in_let_chain)]
pub(crate) struct OrInLetChain {
#[primary_span]
pub span: Span,
}

#[derive(Subdiagnostic, Clone, Copy)]
#[multipart_suggestion(
parse_maybe_missing_let,
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4073,14 +4073,18 @@ impl MutVisitor for CondChecker<'_> {
match e.kind {
ExprKind::Let(_, _, _, ref mut recovered @ Recovered::No) => {
if let Some(reason) = self.forbid_let_reason {
*recovered = Recovered::Yes(self.parser.dcx().emit_err(
errors::ExpectedExpressionFoundLet {
let error = match reason {
NotSupportedOr(or_span) => {
self.parser.dcx().emit_err(errors::OrInLetChain { span: or_span })
}
_ => self.parser.dcx().emit_err(errors::ExpectedExpressionFoundLet {
span,
reason,
missing_let: self.missing_let,
comparison: self.comparison,
},
));
}),
};
*recovered = Recovered::Yes(error);
} else if self.depth > 1 {
// Top level `let` is always allowed; only gate chains
match self.let_chains_policy {
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/parser/or-in-let-chain.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:6:24
|
LL | if let true = true || false {}
| ^^

error: expected expression, found `let` statement
--> $DIR/or-in-let-chain.rs:9:9
|
LL | if (let true = true) || false {}
| ^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions

error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:12:24
|
LL | if let true = true || false || true {}
| ^^

error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:15:33
|
LL | if let true = true && false || true {}
| ^^

error: aborting due to 4 previous errors

28 changes: 28 additions & 0 deletions tests/ui/parser/or-in-let-chain.edition2024.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:6:24
|
LL | if let true = true || false {}
| ^^

error: expected expression, found `let` statement
--> $DIR/or-in-let-chain.rs:9:9
|
LL | if (let true = true) || false {}
| ^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions

error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:12:24
|
LL | if let true = true || false || true {}
| ^^

error: `||` operators are not supported in let chain conditions
--> $DIR/or-in-let-chain.rs:15:33
|
LL | if let true = true && false || true {}
| ^^

error: aborting due to 4 previous errors

17 changes: 17 additions & 0 deletions tests/ui/parser/or-in-let-chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ revisions: edition2021 edition2024
//@ [edition2021] edition: 2021
//@ [edition2024] edition: 2024

fn main() {
if let true = true || false {}
//~^ ERROR `||` operators are not supported in let chain conditions
// With parentheses
if (let true = true) || false {}
//~^ ERROR expected expression, found `let` statement
// Multiple || operators
if let true = true || false || true {}
//~^ ERROR `||` operators are not supported in let chain conditions
// Mixed operators (should still show error for ||)
if let true = true && false || true {}
//~^ ERROR `||` operators are not supported in let chain conditions
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
fn let_or_guard(x: Result<Option<i32>, ()>) {
match x {
Ok(opt) if let Some(4) = opt || false => {}
//~^ ERROR expected expression, found `let` statement
//~^ ERROR `||` operators are not supported in let chain conditions
_ => {}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
error: expected expression, found `let` statement
--> $DIR/ast-validate-guards.rs:5:20
|
LL | Ok(opt) if let Some(4) = opt || false => {}
| ^^^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/ast-validate-guards.rs:5:38
|
LL | Ok(opt) if let Some(4) = opt || false => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:121:16
|
LL | if true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:121:13
|
LL | if true || let 0 = 0 {}
Expand Down Expand Up @@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:212:19
|
LL | while true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:212:16
|
LL | while true || let 0 = 0 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:121:16
|
LL | if true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:121:13
|
LL | if true || let 0 = 0 {}
Expand Down Expand Up @@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:212:19
|
LL | while true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:212:16
|
LL | while true || let 0 = 0 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,7 @@ LL | if (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:121:16
|
LL | if true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:121:13
|
LL | if true || let 0 = 0 {}
Expand Down Expand Up @@ -485,14 +478,7 @@ LL | while (let 0 = 0)? {}
|
= note: only supported directly in conditions of `if` and `while` expressions

error: expected expression, found `let` statement
--> $DIR/disallowed-positions.rs:212:19
|
LL | while true || let 0 = 0 {}
| ^^^^^^^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
error: `||` operators are not supported in let chain conditions
--> $DIR/disallowed-positions.rs:212:16
|
LL | while true || let 0 = 0 {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn nested_within_if_expr() {
//~^ ERROR expected expression, found `let` statement

if true || let 0 = 0 {}
//~^ ERROR expected expression, found `let` statement
//~^ ERROR `||` operators are not supported in let chain conditions
if (true || let 0 = 0) {}
//~^ ERROR expected expression, found `let` statement
if true && (true || let 0 = 0) {}
Expand Down Expand Up @@ -210,7 +210,7 @@ fn nested_within_while_expr() {
//~^ ERROR expected expression, found `let` statement

while true || let 0 = 0 {}
//~^ ERROR expected expression, found `let` statement
//~^ ERROR `||` operators are not supported in let chain conditions
while (true || let 0 = 0) {}
//~^ ERROR expected expression, found `let` statement
while true && (true || let 0 = 0) {}
Expand Down
Loading