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

Incorrect constraints on const generic parameters #7420

Closed
afetisov opened this issue Jun 28, 2021 · 0 comments
Closed

Incorrect constraints on const generic parameters #7420

afetisov opened this issue Jun 28, 2021 · 0 comments
Assignees
Labels
bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.

Comments

@afetisov
Copy link
Contributor

Environment

  • IntelliJ Rust plugin version: 0.4.149.3939-211
  • Rust toolchain version: 1.53.0 (53cb7b09b 2021-06-17) x86_64-unknown-linux-gnu
  • IDE name and version: CLion 2021.1.2 (CL-211.7442.42)
  • Operating system: Linux 5.4.0-74-generic
  • Macro expansion engine: new
  • Name resolution engine: new

Problem description

The constraints on const generic parameters are incorrectly inferred when only some of the parameters are generic.

Steps to reproduce

The code:

struct Foo<const N: u8, const S: bool>(u32);

impl<const N: u8> From<u32> for Foo<N, true> {
                                    // ^^^^ error
    fn from(val: u32) -> Self { Foo(val) }
}

Error: mismatched types [E0308] expected u8, found bool .

The constraint applied to S is always that of the preceding type. For example:

struct Foo<const N: usize, const S: u32>(u32);

impl<const N: usize> From<u32> for Foo<N, 1u32> {
                                       // ^^^^ error
    fn from(val: u32) -> Self { Foo(val) }
}

fn foo<const N: usize>() -> Foo<N, 1u32> {
                                // ^^^^ error
    Foo(0)
}

Error: mismatched types [E0308] expected usize, found u32.

No error occurs if an impl block has either no or all specified generic parameters. E.g.:

impl<const N: usize, const S: u32> From<bool> for Foo<N, S> {
    fn from(_: bool) -> Self {
        Foo(0)
    }
}

No error.

impl From<bool> for Foo<0usize, 1u32> {
    fn from(_: bool) -> Self {
        Foo(0)
    }
}

No error.

Extra error

The following error is probably related:

struct Foo<const N: usize, const S: u32>(u32);
fn foo<const S: u32>() -> Foo<1usize, S> {
                                   // ^ error
    Foo(0)
}

Error: Type arguments must be declared prior to const arguments.

@mchernyavsky mchernyavsky self-assigned this Jun 28, 2021
@mchernyavsky mchernyavsky added bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc. labels Jun 28, 2021
bors bot added a commit that referenced this issue Jul 23, 2021
7479: TY&RES: support const arguments that looks like type arguments r=mchernyavsky a=vlad20012

Related to #3985, fixes (partially, except `Extra error` case) #7420



Co-authored-by: vlad20012 <beskvlad@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.
Projects
None yet
Development

No branches or pull requests

2 participants