Skip to content

Better message when trying to assign two variables at once / match on a tuple without using parentheses #140615

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

Open
xuiqzy opened this issue May 3, 2025 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@xuiqzy
Copy link

xuiqzy commented May 3, 2025

Code

fn main() {
    let (x, y) = 10 / 5, 10 % 5;
}

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=b8d43356c6ba14b240ab12eea1f883da

Current output

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,`
 --> src/main.rs:2:24
  |
2 |     let (x, y) = 10 / 5, 10 % 5;
  |                        ^ expected one of `.`, `;`, `?`, `else`, or an operator

Desired output

Something along the lines of:

If trying to match a tuple on the left side, you have to create a tuple on the right side by adding parentheses.
Or, if providing the error already in the case where the left side isn’t a match on a tuple yet (see below): It could recognise that's what the user is trying to do and suggest creating a tuple on the right and match on it on the left "to assign two values at the same time".

Rationale and extra context

No response

Other cases

For this:

fn main() {
    let x, y = 10 / 5, 10 % 5;
}

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=2cb37a7ed277f032bcae34c793cdfcfb

The error is already helpful by telling us how to match a tuple:

error: unexpected `,` in pattern
 --> src/main.rs:2:10
  |
2 |     let x, y = 10 / 5, 10 % 5;
  |          ^
  |
help: try adding parentheses to match on a tuple
  |
2 |     let (x, y) = 10 / 5, 10 % 5;
  |         +    +

Ideally, the error output of this code would already include the hint of how to achieve the whole thing (adding parentheses on both sides).

Rust Version

rustc 1.88.0-nightly (4824c2bb7 2025-05-02)
binary: rustc
commit-hash: 4824c2bb7445cb2478aab0190c268c939d77a0f6
commit-date: 2025-05-02
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

Motivation is this use case: https://stackoverflow.com/questions/69051429/what-is-the-function-to-get-the-quotient-and-remainder-divmod-for-rust
Also, the concern that it isn’t optimised away into one divmod if spread out across different lines.

Not a huge issue in case it's hard to recognise this case in the compiler. It’d just be a nicer error message helping to save a bit of time or helping someone not familiar with tuples and tuple destructuring.

@xuiqzy xuiqzy added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 3, 2025
@leonardo-m
Copy link

This mistake could be done when you translate Python code to Rust.

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants