Skip to content

[E0532] Error when using functions in patterns should give an explanation of patterns #97200

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
porglezomp opened this issue May 20, 2022 · 9 comments · Fixed by #131312
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@porglezomp
Copy link
Contributor

porglezomp commented May 20, 2022

Given the following code: (Playground)

fn main() {
    let (x, drop(y)) = (1, 2);
}

The current output is:

error[E0532]: expected tuple struct or tuple variant, found function `drop`
 --> src/main.rs:2:13
  |
2 |     let (x, drop(y)) = (1, 2);
  |             ^^^^ not a tuple struct or tuple variant

The error message should give/link to an explanation of what kinds of things are expected in patterns in general, maybe in a note.

(Filed at request of @estebank)

@porglezomp porglezomp 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 20, 2022
@Milo123459
Copy link
Contributor

What should be linked? Something in Rust by Example or the Rust docs (or something else?)

@Milo123459
Copy link
Contributor

@rustbot claim

@porglezomp
Copy link
Contributor Author

porglezomp commented May 22, 2022

@estebank would know better what the precedent for existing errors is, but I'd expect it to link to the reference page for patterns, https://doc.rust-lang.org/reference/patterns.html

I'm slightly unsure about this because of the intimidating grammar block at the top if it's gonna end up pointing beginners to that, but it's probably the best reference for understanding all the details of patterns…

If not that, then I'd expect the Book chapter on patterns https://doc.rust-lang.org/book/ch18-00-patterns.html

@Milo123459
Copy link
Contributor

I'll wait for him to respond before starting.

@compiler-errors
Copy link
Member

compiler-errors commented May 28, 2022

@Milo123459, there are a few existing error notes that point to book chapters in the compiler, search for references to doc.rust-lang.org/book under the compiler/ subdirectory.

I can review a PR attaches such a note to some cases. You might need to be careful about only suggesting it for items that aren't valid in patterns but not* those used incorrectly. For example, E0532 fires if you try to use a unit enum variant as a tuple enum variant. I think we only want to suggest it when we're trying to use, e.g., a function or method name as a pattern variant.

@Milo123459
Copy link
Contributor

Milo123459 commented May 28, 2022

For example, E0532 fires if you try to use a unit enum variant as a tuple enum variant. I think we only want to suggest it when we're trying to use, e.g., a function or method name as a pattern variant.

Hmm, how do we control that / know when it is only invalid?

@compiler-errors
Copy link
Member

You should be able to check the res of the item?

You might need to do some experimentation -- if you can't, then feel free to just fall back to suggesting it always, but leave a FIXME.

@estebank
Copy link
Contributor

estebank commented Jun 1, 2022

I'd expect it to link to the reference page for patterns

I'm slightly unsure about this because of the intimidating grammar block at the top if it's gonna end up pointing beginners to that, but it's probably the best reference for understanding all the details of patterns…

If not that, then I'd expect the Book chapter on patterns https://doc.rust-lang.org/book/ch18-00-patterns.html

I would likely link at https://doc.rust-lang.org/book/ch18-00-patterns.html, precisely because it is more conversational than the reference, but we could link to both, if that seemed necessary.

You should be able to check the res of the item?

The Res contains gets populated by resolve and associates to the underlying type of the thing being evaluated, including whether they are a struct or enum or enum variant, for example. From it you can get the DefId of a user writable type (some things like i8 don't have DefIds, don't worry too much about them). Once you have a DefId you can get the item itself or the Span that lets you point at them.

For the wording changes, I would borrow heavily from the reference and book, and at least add a help with something along the lines of "a pattern can only be composed of literals (like 42 or "hi"), arrays (like [a, b, c]), enums, structs, tuples, variable names (like foo), wildcards (like ..), and placeholders (like _)". I would likely change the main label (in this case) to say "function calls are not allowed in patterns".

@Milo123459
Copy link
Contributor

@rustbot release-assignment

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. A-patterns Relating to patterns and pattern matching labels Jul 18, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 6, 2024
…-errors

On function and method calls in patterns, link to the book

```
error: expected a pattern, found an expression
 --> f889.rs:3:13
  |
3 |     let (x, y.drop()) = (1, 2);
  |             ^^^^^^^^ not a pattern
  |
  = note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error[E0532]: expected a pattern, found a function call
 --> f889.rs:2:13
  |
2 |     let (x, drop(y)) = (1, 2);
  |             ^^^^ not a tuple struct or tuple variant
  |
  = note: function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
```

Fix rust-lang#97200.
@bors bors closed this as completed in 7f5548f Oct 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 6, 2024
Rollup merge of rust-lang#131312 - estebank:fn-in-pattern, r=compiler-errors

On function and method calls in patterns, link to the book

```
error: expected a pattern, found an expression
 --> f889.rs:3:13
  |
3 |     let (x, y.drop()) = (1, 2);
  |             ^^^^^^^^ not a pattern
  |
  = note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error[E0532]: expected a pattern, found a function call
 --> f889.rs:2:13
  |
2 |     let (x, drop(y)) = (1, 2);
  |             ^^^^ not a tuple struct or tuple variant
  |
  = note: function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
```

Fix rust-lang#97200.
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 A-patterns Relating to patterns and pattern matching D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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