From a624df692c4f873a1c018f5f0bc48ea9e5d61064 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 21 Mar 2020 13:22:26 +0100 Subject: [PATCH 1/2] fix type of const params in associated types. --- src/librustc_typeck/collect/type_of.rs | 20 +++++++++++++++---- .../ui/const-generics/issues/issue-66906.rs | 12 +++++++++++ .../const-generics/issues/issue-66906.stderr | 8 ++++++++ .../ui/const-generics/issues/issue-70167.rs | 10 ++++++++++ .../const-generics/issues/issue-70167.stderr | 8 ++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/const-generics/issues/issue-66906.rs create mode 100644 src/test/ui/const-generics/issues/issue-66906.stderr create mode 100644 src/test/ui/const-generics/issues/issue-70167.rs create mode 100644 src/test/ui/const-generics/issues/issue-70167.stderr diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 44ef4ebd463d7..55642dfb4557b 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -256,15 +256,18 @@ 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::Ctor(..), def_id) + | Res::Def(DefKind::AssocTy, def_id) => { tcx.generics_of(tcx.parent(def_id).unwrap()) } Res::Def(_, def_id) => tcx.generics_of(def_id), - Res::Err => return tcx.types.err, res => { tcx.sess.delay_span_bug( DUMMY_SP, - &format!("unexpected const parent path def {:?}", res,), + &format!( + "unexpected const parent path def, parent: {:?}, def: {:?}", + parent_node, res + ), ); return tcx.types.err; } @@ -284,7 +287,16 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { .map(|param| tcx.type_of(param.def_id)) // This is no generic parameter associated with the arg. This is // probably from an extra arg where one is not needed. - .unwrap_or(tcx.types.err) + .unwrap_or_else(|| { + tcx.sess.delay_span_bug( + DUMMY_SP, + &format!( + "missing generic parameter for `AnonConst`, parent {:?}", + parent_node + ), + ); + tcx.types.err + }) } else { tcx.sess.delay_span_bug( DUMMY_SP, diff --git a/src/test/ui/const-generics/issues/issue-66906.rs b/src/test/ui/const-generics/issues/issue-66906.rs new file mode 100644 index 0000000000000..461fe837dac44 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-66906.rs @@ -0,0 +1,12 @@ +// check-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +pub struct Tuple; + +pub trait Trait { + type Input: From<>::Input>; +} + +fn main() {} diff --git a/src/test/ui/const-generics/issues/issue-66906.stderr b/src/test/ui/const-generics/issues/issue-66906.stderr new file mode 100644 index 0000000000000..f8710b67b687e --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-66906.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-66906.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + diff --git a/src/test/ui/const-generics/issues/issue-70167.rs b/src/test/ui/const-generics/issues/issue-70167.rs new file mode 100644 index 0000000000000..58fac8e05114a --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-70167.rs @@ -0,0 +1,10 @@ +// check-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +pub trait Trait: From<>::Item> { + type Item; +} + +fn main() {} diff --git a/src/test/ui/const-generics/issues/issue-70167.stderr b/src/test/ui/const-generics/issues/issue-70167.stderr new file mode 100644 index 0000000000000..4ba3c204097dc --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-70167.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-70167.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + From 2445375e2a2596ce27b1fff4cd448fcf7714e54c Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 22 Mar 2020 16:27:33 +0100 Subject: [PATCH 2/2] fix type of const params in assoc fn. --- src/librustc_typeck/collect/type_of.rs | 12 ++++++------ src/librustc_typeck/lib.rs | 1 + .../issues/issue70273-assoc-fn.rs | 17 +++++++++++++++++ .../issues/issue70273-assoc-fn.stderr | 8 ++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/const-generics/issues/issue70273-assoc-fn.rs create mode 100644 src/test/ui/const-generics/issues/issue70273-assoc-fn.stderr diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 55642dfb4557b..c8f1b20cab447 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -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, + 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( @@ -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 diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index e487e0d265c3e..fd854c750184e 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -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)] diff --git a/src/test/ui/const-generics/issues/issue70273-assoc-fn.rs b/src/test/ui/const-generics/issues/issue70273-assoc-fn.rs new file mode 100644 index 0000000000000..a192ddea9c6ab --- /dev/null +++ b/src/test/ui/const-generics/issues/issue70273-assoc-fn.rs @@ -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 { + fn f(); +} +struct S; + +impl T<0usize> for S { + fn f() {} +} + +fn main() { + let _err = >::f(); +} diff --git a/src/test/ui/const-generics/issues/issue70273-assoc-fn.stderr b/src/test/ui/const-generics/issues/issue70273-assoc-fn.stderr new file mode 100644 index 0000000000000..64007ade0f2ed --- /dev/null +++ b/src/test/ui/const-generics/issues/issue70273-assoc-fn.stderr @@ -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 +