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

Const Generic value declarations are not required to be used in struct definition #83330

Closed
jamesmunns opened this issue Mar 20, 2021 · 5 comments
Labels
C-bug Category: This is a bug.

Comments

@jamesmunns
Copy link
Member

I tried this code:

Playground link

// Doesn't compile: Unused parameter
// pub struct Nothing<ANY>
// {
//     nothing: ()
// }

// Use of N not enforced
pub struct JustNum<const N: usize> {
    nothing: ()
}

// Use of N not enforced
pub struct Generic<ANY, const N: usize>
{
    any: ANY
}

// Use of N not enforced
impl<const N: usize> JustNum<{ N }> {
    fn new() -> Self {
        Self { nothing: () } 
    }
}

// Does not compile: Extra "M" 
// impl<const N: usize, const M: usize> JustNum<{ N }, { M }> {
//     fn new() -> Self {
//         Self { nothing: () } 
//     }
// }

I expected to see this happen:

For the following definition:

pub struct JustNum<const N: usize> {
    nothing: ()
}

I expect a compiler error, due to the unused const-generic argument

Instead, this happened:

No error was emitted

Meta

This occurs in the current beta and nightly:

  • 1.51.0-beta.8 (73f48e5 2021-03-18)
  • 1.52.0-nightly (f5f33ec 2021-03-19)

I'm not sure if const generic arguments are required to be used, but I could see this as an "odd" way to tag extra parameters on without them being bound to the actual impls.

@jamesmunns jamesmunns added the C-bug Category: This is a bug. label Mar 20, 2021
@jamesmunns
Copy link
Member Author

@jonas-schievink mentions this is intended behavior due to consts not having variance (here on twitter), if this is correct, I'm happy to close the issue.

I can't see anything "wrong" this could allow (other than acting as an associated const implicitly, I guess?), it was just a surprise for me.

@JulianKnodt
Copy link
Contributor

I think maybe that's from a very compiler-oriented standpoint, for consts at least having it not be required to be instantiated allows for compile-time configuration, which for me personally is convenient, thinking of mainly linear algebra use-cases or passing compile time flags with structs. If they did have to be used, they would always have to result in some sort of [T; N] somewhere.

@ehuss
Copy link
Contributor

ehuss commented Mar 20, 2021

It was explicitly enabled in #60058 (see #60047). It would be nice to have an example of where that is actually useful. Otherwise, I don't see why it shouldn't at least trigger an unused lint just like an unused function parameter does.

@Lokathor
Copy link
Contributor

Sure, here's an example of a type that uses a const but doesn't store the const in a field: https://github.com/rust-console/voladdress/blob/main/src/volblock.rs

@jamesmunns
Copy link
Member Author

Awesome, thanks for the clarification! Closing as this is intended behavior.

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

No branches or pull requests

4 participants