Skip to content
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

Confusing error message when trying to pattern match on slice containing String #90121

Closed
sivertjoe opened this issue Oct 21, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-patterns Relating to patterns and pattern matching T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sivertjoe
Copy link

Given the following code:

fn main()
{
    let foo = vec!["foo".to_string()];
    
    match foo.as_slice()
    {
        &["foo".to_string()] => {}
        _ => {}
    };
}

The current output is:

error: expected one of `,`, `...`, `..=`, `..`, `]`, or `|`, found `.`
 --> src/main.rs:8:16
  |
8 |         &["foo".to_string()] => {}
  |                ^
  |                |
  |                expected one of `,`, `...`, `..=`, `..`, `]`, or `|`
  |                help: missing `,`

error[E0531]: cannot find tuple struct or tuple variant `to_string` in this scope
 --> src/main.rs:8:17
  |
8 |         &["foo".to_string()] => {}
  |                 ^^^^^^^^^ not found in this scope

error[E0308]: mismatched types
 --> src/main.rs:8:11
  |
6 |     match foo.as_slice()
  |           -------------- this expression has type `&[String]`
7 |     {
8 |         &["foo".to_string()] => {}
  |           ^^^^^ expected struct `String`, found `&str`

Some errors have detailed explanations: E0308, E0531.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `playground` due to 3 previous errors

Ideally the output should look the same as if I did:

match foo.as_slice()
{
    &[String::from("foo")] => {}
    _ => {}
};

which is:

error[E0164]: expected tuple struct or tuple variant, found associated function `String::from`
 --> src/main.rs:8:11
  |
8 |         &[String::from("foo")] => {}
  |           ^^^^^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
  |
  = help: for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html

So maybe a better output for my first example would be

error[E0164]: expected tuple struct or tuple variant, found associated function `std::str::to_string`
 --> src/main.rs:8:11
  |
8 |          &["foo".to_string()] => {}
  |           ^^^^^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
  |
  = help: for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html

@sivertjoe sivertjoe 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 Oct 21, 2021
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST A-patterns Relating to patterns and pattern matching labels Jul 31, 2023
@Nadrieril
Copy link
Member

Closing as a duplicate of #112593 (which has mentoring instructions)

@Nadrieril Nadrieril closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2023
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-parser Area: The parsing of Rust source code to an AST A-patterns Relating to patterns and pattern matching 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

3 participants