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

spurious type-mismatch error in code with generics with value parameters #11913

Closed
fhars opened this issue Apr 6, 2022 · 1 comment · Fixed by #11920
Closed

spurious type-mismatch error in code with generics with value parameters #11913

fhars opened this issue Apr 6, 2022 · 1 comment · Fixed by #11920
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@fhars
Copy link

fhars commented Apr 6, 2022

The following code (a caricature of what the fugit crate does) compiles without warnings, yet rust analyzer flags expected Rate<u32, NOM, DENOM>, found Rate<u32, _, _> [type-mismatch] in the definition of the new() method, and in the call to run()at the end:

  pub struct Rate<T, const NOM: u32, const DENOM: u32> {
    raw: T,
}

impl<const NOM: u32, const DENOM: u32> Rate<u32, NOM, DENOM> {
    fn new(raw: u32) -> Self {
        Rate { raw }
    }

    fn to_raw(&self) -> u32 {
        self.raw
    }
}

trait ExtU32 {
    fn f<const NOM: u32, const DENOM: u32>(self) -> Rate<u32, NOM, DENOM>;
}

impl ExtU32 for u32 {
    fn f<const NOM: u32, const DENOM: u32>(self) -> Rate<u32, NOM, DENOM> {
        Rate::<u32, NOM, DENOM>::new(self)
    }
}

fn run(t: Rate<u32, 1, 1>) {
    println!("Hello, {}", t.to_raw());
}

fn main() {
    run(100.f());
}

Relevant info:

 ✔ user@dev-vm:~ $ rustc --version
rustc 1.59.0 (9d1b2106e 2022-02-23)

Someone has been working on this in the last few days, but it is not fixed:

 ✔ user@dev-vm:~ $ rust-analyzer --version
rust-analyzer bc08b8eff 2022-04-02 dev
 ✔ user@dev-vm:~ $ rust-analyzer diagnostics ra-test/
processing crate: ra_test, module: /home/user/ra-test/src/main.rs
Diagnostic { code: DiagnosticCode("type-mismatch"), message: "expected Rate<u32, 1, 1>, found Rate<u32, _, _>", range: 582..589, severity: Error, unused: false, experimental: true, fixes: None }
Diagnostic { code: DiagnosticCode("type-mismatch"), message: "expected Rate<u32, NOM, DENOM>, found Rate<u32, _, _>", range: 172..184, severity: Error, unused: false, experimental: true, fixes: None }
Diagnostic { code: DiagnosticCode("type-mismatch"), message: "expected Rate<u32, NOM, DENOM>, found Rate<u32, _, _>", range: 162..190, severity: Error, unused: false, experimental: true, fixes: None }

diagnostic scan complete

[ERROR rust_analyzer] Unexpected error: diagnostic error detected
diagnostic error detected
 ✘ user@dev-vm:~ $ 

Today's nightly has one error less:

 ✔ user@dev-vm:~ $ rust-analyzer --version
rust-analyzer 2366d8e05 2022-04-06 dev
 ✔ user@dev-vm:~ $ rust-analyzer diagnostics ra-test/
processing crate: ra_test, module: /home/user/ra-test/src/main.rs
Diagnostic { code: DiagnosticCode("type-mismatch"), message: "expected Rate<u32, 1, 1>, found Rate<u32, _, _>", range: 582..589, severity: Error, unused: false, experimental: true, fixes: None }
Diagnostic { code: DiagnosticCode("type-mismatch"), message: "expected Rate<u32, NOM, DENOM>, found Rate<u32, _, _>", range: 172..184, severity: Error, unused: false, experimental: true, fixes: None }

diagnostic scan complete

[ERROR rust_analyzer] Unexpected error: diagnostic error detected
diagnostic error detected
 ✘ user@dev-vm:~ $ 
@flodiebold flodiebold added A-ty type system / type inference / traits / method resolution C-bug Category: bug labels Apr 6, 2022
@flodiebold
Copy link
Member

Support for const generics is still very WIP, CC #8655.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants