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

Cannot infer type with a default const parameter #95486

Closed
ejpcmac opened this issue Mar 30, 2022 · 5 comments
Closed

Cannot infer type with a default const parameter #95486

ejpcmac opened this issue Mar 30, 2022 · 5 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics_defaults `#![feature(const_generics_defaults)]` F-const_generics `#![feature(const_generics)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ejpcmac
Copy link

ejpcmac commented Mar 30, 2022

I tried this code:

#[derive(Default)]
pub struct TestStruct<const N: usize = 0>;

pub fn main() {
    let _ = TestStruct::default();
}

I expected this to compile properly, since without specifying the const parameter of the TestStruct<N> type, it should be inferred as TestStruct<0>.

Instead, this happened:

error[E0282]: type annotations needed for `TestStruct<N>`
 --> test.rs:5:13
  |
5 |     let _ = TestStruct::default();
  |         -   ^^^^^^^^^^^^^^^^^^^^^ cannot infer type for struct `TestStruct<{_: usize}>`
  |         |
  |         consider giving this pattern a type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.

If I replace my code with this, it compiles properly:

#[derive(Default)]
pub struct TestStruct<const N: usize = 0>;

pub fn main() {
    // Note the absence of the N parameter.
    let _: TestStruct = TestStruct::default();
}

Meta

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0

Also tested with:

rustc +nightly --version --verbose:

rustc 1.61.0-nightly (9c06e1ba4 2022-03-29)
binary: rustc
commit-hash: 9c06e1ba47e1077725a950e0b5d1870a89c8b536
commit-date: 2022-03-29
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0
@ejpcmac ejpcmac added the C-bug Category: This is a bug. label Mar 30, 2022
@frank-king
Copy link
Contributor

@rustbot claim

@frank-king
Copy link
Contributor

I'm afraid this is by design, since there is a similar ui test

// test that defaulted const params are not used to help type inference
struct Foo<const N: u32 = 2>;
impl<const N: u32> Foo<N> {
fn foo() -> Self { loop {} }
}
fn main() {
let foo = Foo::<1>::foo();
let foo = Foo::foo();
//~^ error: type annotations needed for `Foo<N>`
}

@rustbot release-assignment
@rustbot label: +A-const-generics +F-const_generics_defaults +F-const_generics +T-compiler
cc @rust-lang/project-const-generics

@rustbot rustbot added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` F-const_generics_defaults `#![feature(const_generics_defaults)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 5, 2022
@ejpcmac
Copy link
Author

ejpcmac commented Apr 5, 2022

Oh :( And what would be the rationale behind this decision? Because I feel this is kind a redundant to have to provide a type without the defaults. It does not look like usual Rust code.

@ejpcmac
Copy link
Author

ejpcmac commented Apr 18, 2022

Oh, apparently this is due to a tricky design issue: #27336. As it is known, I’m closing this one.

@ejpcmac ejpcmac closed this as completed Apr 18, 2022
@wyfo
Copy link

wyfo commented Jun 27, 2022

As I've just posted in #27336, you can,also use let _ = <TestStruct>::default().

yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 9, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
yodaldevoid added a commit to yodaldevoid/embassy that referenced this issue Dec 14, 2022
It would be nice if a default value could be used so old code would
mostly just work, but defaults are not used while determining types so
adding a default does nothing here.

It would also be nice if we could calculate the number of interfaces
needed at compile time, but I'll leave that as an exercise for later.

rust-lang/rust#95486
rust-lang/rust#99727
rust-lang/rust#27336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics_defaults `#![feature(const_generics_defaults)]` F-const_generics `#![feature(const_generics)]` 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