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: unclear why "type annotations needed" error #72787

Closed
tspiteri opened this issue May 30, 2020 · 1 comment · Fixed by #75165
Closed

const generic: unclear why "type annotations needed" error #72787

tspiteri opened this issue May 30, 2020 · 1 comment · Fixed by #75165
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tspiteri
Copy link
Contributor

I'm experimenting with replacing typenum with const generics, and replacing constraints involving constant generics being compared to other constant generics. I have gotten a little further recently, probably because of the recent merging of lazy normalization PRs. I have just stumbled upon a new issue.

#![feature(const_generics)]

pub struct IsLessOrEqual<const LHS: u32, const RHS: u32>;
pub struct Condition<const CONDITION: bool>;
pub trait True {}

impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
    Condition<{ LHS <= RHS }>: True
{
}
impl True for Condition<true> {}

struct S<const I: u32, const J: u32>;
impl<const I: u32, const J: u32> S<I, J>
where
    IsLessOrEqual<I, 8>: True,
    IsLessOrEqual<J, 8>: True,
    IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
    // Condition<{ 8 - I <= 8 - J }>: True,
{
    fn print() {
        println!("I {} J {}", I, J);
    }
}

This gives the error

error[E0283]: type annotations needed
  --> src/lib.rs:16:26
   |
5  | pub trait True {}
   | -------------- required by this bound in `True`
...
16 |     IsLessOrEqual<I, 8>: True,
   |                          ^^^^ cannot infer type for struct `IsLessOrEqual<I, 8u32>`
   |
   = note: cannot satisfy `IsLessOrEqual<I, 8u32>: True

In this specific case, replacing IsLessOrEqual<{ 8 - I }, { 8 - J }>: True with the commented-out Condition<{ 8 - I <= 8 - J }>: True makes the compilation work. However that is still not very robust: changing all three IsLessOrEqual constraints to the equivalent Condition constraints would again lead to compilation failure.

@tspiteri tspiteri added the C-bug Category: This is a bug. label May 30, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2020
@lcnr
Copy link
Contributor

lcnr commented Aug 4, 2020

This now fails with

warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information

error: constant expression depends on a generic parameter
 --> src/lib.rs:8:32
  |
8 |     Condition<{ LHS <= RHS }>: True
  |                                ^^^^
  |
  = note: this may fail depending on what value the parameter takes

error: constant expression depends on a generic parameter
  --> src/lib.rs:18:42
   |
18 |     IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
   |                                          ^^^^
   |
   = note: this may fail depending on what value the parameter takes

error: aborting due to 2 previous errors; 1 warning emitted

We should add this as a regression test to check the error messages once const wf bounds are implemented, marking E-needs-test

@lcnr lcnr added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 4, 2020
@bors bors closed this as completed in ec9d524 Aug 5, 2020
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. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants