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

fix type of const params in assoc fns. #70276

Closed
wants to merge 3 commits into from
Closed

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Mar 22, 2020

extends and blocked on #70223

fixes #70273

r? @eddyb cc @varkor

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 22, 2020
@lcnr lcnr changed the title fix type of const params in associated functions. fix type of const params in assoc fns. Mar 22, 2020
}

fn main() {
let _err = <S as T<0usize>>::f();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<S as T<123>>::f::<456>() should also work, if you added a const param to f as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add a test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you'll need to implement it, it won't work with the current approach.

Copy link
Contributor Author

@lcnr lcnr Mar 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for whatever reason adding a const param to f actually prevents the ice. not quite sure why, but this is new behavior so lets add a test for it

The following works on the current nightly.

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

trait T<const A: usize> {
    fn l<const N: usize>() -> usize;
    fn r<const N: usize>() -> usize;
}

struct S;

impl<const N: usize> T<N> for S {
    fn l<const M: usize>() -> usize { N }
    fn r<const M: usize>() -> usize { M }
}

fn main() {
   assert_eq!(<S as T<123>>::l::<456>(), 123);
   assert_eq!(<S as T<123>>::r::<456>(), 456);
}

playground

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, oops, it's because both the trait and the method have the exact same number of parameters and the same types. Make one of them a bool instead of usize, then you should see how it thinks the method segment has the trait's parameter (which is wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jup. Quite surprisingly, using usize and u16 works 🤔
but using bool and usize does not work on nightly rn

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because the integer literals will work with any integer type.

@varkor varkor added the F-const_generics `#![feature(const_generics)]` label Mar 22, 2020
@lcnr
Copy link
Contributor Author

lcnr commented Mar 22, 2020

I am going to experiment with using the res of the segment instead of the path.

Closing this for now as I don't know how long this will take.

@lcnr lcnr closed this Mar 22, 2020
Centril added a commit to Centril/rust that referenced this pull request Mar 24, 2020
…r=eddyb

correctly handle const params in type_of

extends rust-lang#70223, retry of rust-lang#70276

fixes rust-lang#70273

r? @eddyb cc @varkor
Centril added a commit to Centril/rust that referenced this pull request Mar 24, 2020
…r=eddyb

correctly handle const params in type_of

extends rust-lang#70223, retry of rust-lang#70276

fixes rust-lang#70273

r? @eddyb cc @varkor
Centril added a commit to Centril/rust that referenced this pull request Mar 24, 2020
…r=eddyb

correctly handle const params in type_of

extends rust-lang#70223, retry of rust-lang#70276

fixes rust-lang#70273

r? @eddyb cc @varkor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-const_generics `#![feature(const_generics)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE when using const generics with associated items
5 participants