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

Inconsistent availability of constant fields as const generic values #92776

Closed
jamesmunns opened this issue Jan 11, 2022 · 2 comments · Fixed by #92884
Closed

Inconsistent availability of constant fields as const generic values #92776

jamesmunns opened this issue Jan 11, 2022 · 2 comments · Fixed by #92884
Labels
C-bug Category: This is a bug.

Comments

@jamesmunns
Copy link
Member

jamesmunns commented Jan 11, 2022

I tried this code:

struct Example {
    foo: usize
}

const EXAMPLE: Example = Example { foo: 42 };

struct Other {
    // works
    field: [u8; EXAMPLE.foo],
}

struct Wow<const N: usize> {
    field: [u8; N]
}

impl<const N: usize> Wow<N> {
    fn new() -> Self {
        Self {
            field: [0u8; N]
        }
    }
}

fn main() {
    // works
    let x = [0u8; EXAMPLE.foo];
    
    // doesn't work
    let y: Wow<EXAMPLE.foo> = Wow::new();
    
    // does work
    const EXAMPLE_FOO: usize = EXAMPLE.foo;
    // does work
    let z: Wow<EXAMPLE_FOO> = Wow::new();
}

I expected to see this happen:

I expect all uses of EXAMPLE.foo to be acceptable

Instead, this happened: let y: Wow<EXAMPLE.foo> = Wow::new(); is rejected with the following error:

error: expected one of `!`, `(`, `+`, `,`, `::`, `:`, `<`, `=`, or `>`, found `.`
  --> src/main.rs:29:23
   |
29 |     let y: Wow<EXAMPLE.foo> = Wow::new();
   |         -             ^ expected one of 9 possible tokens
   |         |
   |         while parsing the type for `y`

error: could not compile `playground` due to previous error

Meta

This is present in the current stable (1.57.0), as well as the latest nightly (1.60.0 2022-01-10)

Backtrace

<backtrace>

@jamesmunns jamesmunns added the C-bug Category: This is a bug. label Jan 11, 2022
@jamesmunns
Copy link
Member Author

jamesmunns commented Jan 11, 2022

As a quick update, this DOES work when adding curly braces:

// does work
let y: Wow<{EXAMPLE.foo}> = Wow::new();

So I suppose this might be considered a diagnostics bug instead/as well, as this was not mentioned in the error output.

@compiler-errors
Copy link
Member

As a quick update, this DOES work when adding curly braces

Yeah, I think this is a diagnostics issue.

We should probably recover parsing <foo.bar> and suggest wrapping in { .. }. We already do this in other cases with const generics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants