Skip to content

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

Closed
@jamesmunns

Description

@jamesmunns

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions