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

bizarre error message for one of the universal constructor syntaxes #108224

Open
ijackson opened this issue Feb 18, 2023 · 2 comments
Open

bizarre error message for one of the universal constructor syntaxes #108224

ijackson opened this issue Feb 18, 2023 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ijackson
Copy link
Contributor

I tried this code:

#![allow(dead_code)]
#![allow(unused_variables)]
enum Enum<'l, F> {
    Tuple(&'l F),
    Struct { f: &'l F },
}
fn main(){
    let v = 42u32;
    let t = Enum::<u32>::Struct { f: &v };
}

I expected to see this happen: it compiles.

Instead, this happened (on the playground, with stable and with nightly):

error[E0109]: lifetime arguments are not allowed on variant `Struct`
 --> src/main.rs:9:26
  |
9 |     let t = Enum::<u32>::Struct { f: &v };
  |                          ^^^^^^
  |                          |
  |                          lifetime argument not allowed
  |                          not allowed on variant `Struct`
  |
  = note: enum variants can't have type parameters

error[E0107]: this enum takes 1 lifetime argument but 0 lifetime arguments were supplied
 --> src/main.rs:9:13
  |
9 |     let t = Enum::<u32>::Struct { f: &v };
  |             ^^^^ expected 1 lifetime argument
  |
note: enum defined here, with 1 lifetime parameter: `'l`
 --> src/main.rs:3:6
  |
3 | enum Enum<'l, F> {
  |      ^^^^ --
help: add missing lifetime argument
  |
9 |     let t = Enum::<'l, u32>::Struct { f: &v };
  |                    +++

It's not 100% clear to me that this syntax, with the generics applied to the enum type rather than the variant, is intended to be accepted. I think it probably ought to be, though. On this point I found #69363/#69356. The Reference doesn't seem to discuss it.

Also, the error messages are nonsensical, and the suggestion doesn't compile.

Variations:

    let t = Enum::<'l, u32>::Tuple { 0: &v }; // use of undeclared lifetime name `'l`  [obviously]
    let t = Enum::<u32>::Tuple { 0: &v }; //  lifetime arguments are not allowed on variant `Tuple`
    let t = Enum::<'_, u32>::Tuple { 0: &v }; // //  lifetime arguments are not allowed on variant `Tuple`
    let t = Enum::<u32>::Tuple(&v);; // compiles, but of course you can't use this for a named-fields variant
    let t = Enum::Struct::<u32> { f: &v }; // compiles
    let t = Enum::Tuple::<u32> { 0: &v }; // compiles

@ijackson ijackson added the C-bug Category: This is a bug. label Feb 18, 2023
@ijackson
Copy link
Contributor Author

Oh, and, if we take the lifetime out of the enum type and just use owned values, it comples just fine:

#![allow(dead_code)]
#![allow(unused_variables)]
enum Enum<F> {
    Tuple(F),
    Struct { f: F },
}
fn main(){
    let v = 42u32;
    let t = Enum::<u32>::Struct { f: v };
}

@clubby789
Copy link
Contributor

@rustbot label +A-diagnostics

I wonder if we should always suggest '_ when the lifetime is not in scope. The suggestion looks right at a glance which seems pretty confusing

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 18, 2023
@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 24, 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 C-bug Category: This is a bug. 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

4 participants