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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/librustc_typeck/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
// figure out which generic parameter it corresponds to and return
// the relevant type.
let generics = match path.res {
Res::Def(DefKind::Ctor(..), def_id)
| Res::Def(DefKind::AssocTy, def_id) => {
tcx.generics_of(tcx.parent(def_id).unwrap())
}
Res::Def(
DefKind::Ctor(..) | DefKind::AssocTy | DefKind::AssocFn,
lcnr marked this conversation as resolved.
Show resolved Hide resolved
def_id,
) => tcx.generics_of(tcx.parent(def_id).unwrap()),
Res::Def(_, def_id) => tcx.generics_of(def_id),
res => {
tcx.sess.delay_span_bug(
Expand Down Expand Up @@ -291,8 +291,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
tcx.sess.delay_span_bug(
DUMMY_SP,
&format!(
"missing generic parameter for `AnonConst`, parent {:?}",
parent_node
"missing generic parameter for `AnonConst`, parent: {:?}, path.res: {:?}",
parent_node, path.res
),
);
tcx.types.err
Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
#![feature(crate_visibility_modifier)]
#![feature(in_band_lifetimes)]
#![feature(nll)]
#![feature(or_patterns)]
#![feature(try_blocks)]
#![feature(never_type)]
#![feature(slice_partition_dedup)]
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/const-generics/issues/issue70273-assoc-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-pass

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

trait T<const A: usize> {
fn f();
}
struct S;

impl T<0usize> for S {
fn f() {}
}

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.

}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issues/issue70273-assoc-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/issue70273-assoc-fn.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default