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

Ambiguous error reporting when trying to infer const generic type #93750

Open
Joeyh021 opened this issue Feb 7, 2022 · 1 comment
Open

Ambiguous error reporting when trying to infer const generic type #93750

Joeyh021 opened this issue Feb 7, 2022 · 1 comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. 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

@Joeyh021
Copy link

Joeyh021 commented Feb 7, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=659888b8e5f59352118b3c7b39187f29

#![feature(adt_const_params)]
#![feature(generic_const_exprs)]
#![feature(generic_arg_infer)]

#[derive(PartialEq, Eq)]
#[allow(dead_code)]
enum Colour {
    R,
    G,
    B,
    W,
}

struct Led<const C: Colour> {}

impl<const C: Colour> Led<C> {
    pub fn new() -> Led<{ Colour::W }> {
        Led::<{ Colour::W }> {}
    }
}

fn main() {
    let l = Led::<_>::new();
}

The current output is:

error[E0284]: type annotations needed: cannot satisfy `the constant `Led::<{_: Colour}>::new::{constant#0}` can be evaluated`
  --> src\main.rs:24:13
   |
24 |     let l = Led::<_>::new();
   |             ^^^^^^^^^^^^^ cannot satisfy `the constant `Led::<{_: Colour}>::new::{constant#0}` can be evaluated`
   |
note: required by a bound in `Led::<C>::new`
  --> src\main.rs:18:25
   |
18 |     pub fn new() -> Led<{ Colour::Off }> {
   |                         ^^^^^^^^^^^^^^^ required by this bound in `Led::<C>::new`

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

The error message being cannot satisfy 'the constant 'Led::<{_: Colour}>::new::{constant#0}' can be evaluated' doesn't explain the error at all, and is confusing in the way it is reported. I'm not entirely sure what is going on internally, but getting the compiler to emit a proper error message explaining that the const generic type cannot be inferred would be useful.

@Joeyh021 Joeyh021 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 7, 2022
@BoxyUwU BoxyUwU added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Feb 8, 2022
@lcnr
Copy link
Contributor

lcnr commented Jun 24, 2022

same issue as #92186

@workingjubilee workingjubilee added requires-nightly This issue requires a nightly compiler in some way. requires-incomplete-features This issue requires the use of incomplete features. labels Mar 4, 2023
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) A-diagnostics Area: Messages for errors, warnings, and lints F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. 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

No branches or pull requests

4 participants