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

Error on struct pattern with enum #74863

Closed
ricvelozo opened this issue Jul 28, 2020 · 2 comments · Fixed by #134284
Closed

Error on struct pattern with enum #74863

ricvelozo opened this issue Jul 28, 2020 · 2 comments · Fixed by #134284
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ricvelozo
Copy link

The following code doesn't compiles:

struct Website {
    url: String,
    title: Option<String>,
}

fn main() {
    let website = Website {
        url: "http://www.example.com".into(),
        title: Some("Example Domain".into()),
    };

    if let Website { url, Some(title) } = website {
        println!("[{}]({})", title, url);
    }
}

The compile error:

error: expected `,`
  --> src/main.rs:14:27
   |
14 |     if let Website { url, Some(title) } = website {
   |                           ^^^^

error[E0425]: cannot find value `title` in this scope
  --> src/main.rs:15:30
   |
15 |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope

error[E0423]: expected value, found crate `url`
  --> src/main.rs:15:37
   |
15 |         println!("[{}]({})", title, url);
   |                                     ^^^ not a value

error: aborting due to 3 previous errors

If I use an tuple instead, the code compiles:

struct Website(String, Option<String>);

fn main() {
    let website = Website(
        "http://www.example.com".into(),
        Some("Example Domain".into()),
    );

    if let Website(url, Some(title)) = website {
        println!("[{}]({})", title, url);
    }
}

Meta

rustc --version --verbose:

rustc 1.45.0 (5c1f21c3b 2020-07-13)
binary: rustc
commit-hash: 5c1f21c3b82297671ad3ae1e8c942d2ca92e84f2
commit-date: 2020-07-13
host: x86_64-unknown-linux-gnu
release: 1.45.0
LLVM version: 10.0
@ricvelozo ricvelozo added the C-bug Category: This is a bug. label Jul 28, 2020
@tesuji
Copy link
Contributor

tesuji commented Jul 28, 2020

It works as expected. Change the if-let expression to:

    if let Website { url, title: Some(title) } = website {

code will compile.

@tesuji
Copy link
Contributor

tesuji commented Jul 28, 2020

Maybe the diagnostic could be changed to guide users.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. A-parser Area: The parsing of Rust source code to an AST and removed C-bug Category: This is a bug. labels Jul 28, 2020
@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 28, 2020
estebank added a commit to estebank/rust that referenced this issue Oct 18, 2023
@estebank estebank added the D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. label Dec 12, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 16, 2024
Keep track of patterns that could have introduced a binding, but didn't

When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error.

```
error[E0425]: cannot find value `title` in this scope
  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
   |
LL |     if let Website { url, .. } = website {
   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
LL |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope
```

Fix rust-lang#74863.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 16, 2024
Keep track of patterns that could have introduced a binding, but didn't

When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error.

```
error[E0425]: cannot find value `title` in this scope
  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
   |
LL |     if let Website { url, .. } = website {
   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
LL |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope
```

Fix rust-lang#74863.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 16, 2024
Keep track of patterns that could have introduced a binding, but didn't

When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error.

```
error[E0425]: cannot find value `title` in this scope
  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
   |
LL |     if let Website { url, .. } = website {
   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
LL |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope
```

Fix rust-lang#74863.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 16, 2024
Keep track of patterns that could have introduced a binding, but didn't

When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error.

```
error[E0425]: cannot find value `title` in this scope
  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
   |
LL |     if let Website { url, .. } = website {
   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
LL |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope
```

Fix rust-lang#74863.
@bors bors closed this as completed in 0f82cff Dec 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 16, 2024
Rollup merge of rust-lang#134284 - estebank:issue-74863, r=lcnr

Keep track of patterns that could have introduced a binding, but didn't

When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error.

```
error[E0425]: cannot find value `title` in this scope
  --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:18:30
   |
LL |     if let Website { url, .. } = website {
   |            ------------------- this pattern doesn't include `title`, which is available in `Website`
LL |         println!("[{}]({})", title, url);
   |                              ^^^^^ not found in this scope
```

Fix rust-lang#74863.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants