Skip to content

Let chain error message with || is confusing and verbose #140263

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

Closed
ehuss opened this issue Apr 24, 2025 · 4 comments · Fixed by #140272
Closed

Let chain error message with || is confusing and verbose #140263

ehuss opened this issue Apr 24, 2025 · 4 comments · Fixed by #140272
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-let_chains `#![feature(let_chains)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Apr 24, 2025

Code

fn main() {
    if let true = true || false {}
}

Current output

error: expected expression, found `let` statement
 --> src/main.rs:6:8
  |
6 |     if let true = true || false {}
  |        ^^^^^^^^^^^^^^^
  |
  = note: only supported directly in conditions of `if` and `while` expressions
note: `||` operators are not supported in let chain expressions
 --> src/main.rs:6:24
  |
6 |     if let true = true || false {}
  |                        ^^

Desired output

error: `||` in an `if` expression with `let` is not allowed
 --> src/main.rs:2:17
  |
2 |     if let true = true || false {}
  |                        ^^
  |

Rationale and extra context

The current error message has several confusing factors:

  • The primary error message expected expression, found `let` statement which is confusing. I suppose this is technically correct (let is not allowed in an if with a ||), but muddles the problem. There's also the confusion of using the term `let` statement`, where the term "statement" is confusing here.
  • The note only supported directly in conditions of `if` and `while` expressions is confusing, since we are in an if expression.
  • The final note of note: `||` operators are not supported in let chain expressions shows the actual problem, but it is lost in a sea of other text.

Other cases

Rust Version

rustc 1.88.0-nightly (df35ff6c3 2025-04-23)
binary: rustc
commit-hash: df35ff6c354f1f1fbf430b84e7dea37dfe997f34
commit-date: 2025-04-23
host: aarch64-apple-darwin
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

@ehuss ehuss added A-diagnostics Area: Messages for errors, warnings, and lints F-let_chains `#![feature(let_chains)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Apr 24, 2025
@traviscross
Copy link
Contributor

cc @est31 @fee1-dead

@Kivooeo
Copy link
Contributor

Kivooeo commented Apr 24, 2025

@rustbot claim

@est31
Copy link
Member

est31 commented Apr 24, 2025

Agree that it could be improved. As you say, it's technically correct (explained how || in && chains plays together with precedence in more detail here), but the error is too close to the "language level" instead of the level the user is at.

There's also the confusion of using the term let statement`, where the term "statement" is confusing here.

Fine with dropping "statement" but the error message should not call it an expression in any case because the let chains RFC didn't make let into an expression in the general sense.

It's definitely implemented that way in the compiler AST, but compiler behaviour should follow the language level, including error message, even if colloquial understanding sees let inside while or if as an "expression".

@est31
Copy link
Member

est31 commented Apr 24, 2025

|| in an if expression with let is not allowed

I'd change it to:

|| in an if condition with let is not allowed

otherwise the suggestion is fine.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 25, 2025
Improve error message for `||` (or) in let chains

**Description**

This PR improves the error message when using `||` in an if let chain expression, addressing rust-lang#140263.

**Changes**

1. Creates a dedicated error message specifically for `||` usage in let chains
2. Points the primary span directly at the `||` operator
3. Removes confusing secondary notes about "let statements" and unsupported contexts
5. Adds UI tests verifying the new error message and valid cases

**Before**
```rust
error: expected expression, found let statement
 --> src/main.rs:2:8
  |
2 |     if let true = true || false {}
  |        ^^^^^^^^^^^^^^^
  |
  = note: only supported directly in conditions of if and while expressions
note: || operators are not supported in let chain expressions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |
```

**After**
```rust
error: `||` operators are not supported in let chain conditions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |                        ^^
```

**Implementation details**
1. Added new `OrInLetChain` diagnostic in errors.rs

2. Modified `CondChecker` in expr.rs to prioritize the `||` error

3. Updated fluent message definitions to use clearer wording

**Related issue**
Fixes rust-lang#140263

cc `@ehuss` (issue author)
jhpratt added a commit to jhpratt/rust that referenced this issue Apr 26, 2025
Improve error message for `||` (or) in let chains

**Description**

This PR improves the error message when using `||` in an if let chain expression, addressing rust-lang#140263.

**Changes**

1. Creates a dedicated error message specifically for `||` usage in let chains
2. Points the primary span directly at the `||` operator
3. Removes confusing secondary notes about "let statements" and unsupported contexts
5. Adds UI tests verifying the new error message and valid cases

**Before**
```rust
error: expected expression, found let statement
 --> src/main.rs:2:8
  |
2 |     if let true = true || false {}
  |        ^^^^^^^^^^^^^^^
  |
  = note: only supported directly in conditions of if and while expressions
note: || operators are not supported in let chain expressions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |
```

**After**
```rust
error: `||` operators are not supported in let chain conditions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |                        ^^
```

**Implementation details**
1. Added new `OrInLetChain` diagnostic in errors.rs

2. Modified `CondChecker` in expr.rs to prioritize the `||` error

3. Updated fluent message definitions to use clearer wording

**Related issue**
Fixes rust-lang#140263

cc ``@ehuss`` (issue author)
@bors bors closed this as completed in 6f6fa0f Apr 26, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 26, 2025
Rollup merge of rust-lang#140272 - Kivooeo:new-fix-four, r=est31

Improve error message for `||` (or) in let chains

**Description**

This PR improves the error message when using `||` in an if let chain expression, addressing rust-lang#140263.

**Changes**

1. Creates a dedicated error message specifically for `||` usage in let chains
2. Points the primary span directly at the `||` operator
3. Removes confusing secondary notes about "let statements" and unsupported contexts
5. Adds UI tests verifying the new error message and valid cases

**Before**
```rust
error: expected expression, found let statement
 --> src/main.rs:2:8
  |
2 |     if let true = true || false {}
  |        ^^^^^^^^^^^^^^^
  |
  = note: only supported directly in conditions of if and while expressions
note: || operators are not supported in let chain expressions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |
```

**After**
```rust
error: `||` operators are not supported in let chain conditions
 --> src/main.rs:2:24
  |
2 |     if let true = true || false {}
  |                        ^^
```

**Implementation details**
1. Added new `OrInLetChain` diagnostic in errors.rs

2. Modified `CondChecker` in expr.rs to prioritize the `||` error

3. Updated fluent message definitions to use clearer wording

**Related issue**
Fixes rust-lang#140263

cc ```@ehuss``` (issue author)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-let_chains `#![feature(let_chains)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants