Skip to content

Commit 850e3e6

Browse files
committed
bug!() on const inference variable in opaque type
Add some tests to verify that this case is never hit
1 parent a59b7ea commit 850e3e6

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,15 @@ fn fixup_opaque_types<'tcx, T>(tcx: TyCtxt<'tcx>, val: &T) -> T where T: TypeFol
907907
},
908908
GenericArgKind::Const(old_const) => {
909909
if let ConstValue::Infer(_) = old_const.val {
910-
self.tcx.mk_param_from_def(param)
910+
// This should never happen - we currently do not support
911+
// 'const projections', e.g.:
912+
// `impl<T: SomeTrait> MyTrait for T where <T as SomeTrait>::MyConst == 25`
913+
// which should be the only way for us to end up with a const inference
914+
// variable after projection. If Rust ever gains support for this kind
915+
// of projection, this should *probably* be changed to
916+
// `self.tcx.mk_param_from_def(param)`
917+
bug!("Found infer const: `{:?}` in opaque type: {:?}",
918+
old_const, ty);
911919
} else {
912920
old_param.fold_with(self)
913921
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Tests that we properly detect defining usages when using
2+
// const generics in an associated opaque type
3+
// check-pass
4+
5+
#![feature(type_alias_impl_trait)]
6+
#![feature(const_generics)]
7+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
8+
9+
trait UnwrapItemsExt<const C: usize> {
10+
type Iter;
11+
fn unwrap_items(self) -> Self::Iter;
12+
}
13+
14+
struct MyStruct<const C: usize> {}
15+
16+
trait MyTrait<'a, const C: usize> {
17+
type MyItem;
18+
const MY_CONST: usize;
19+
}
20+
21+
impl<'a, const C: usize> MyTrait<'a, {C}> for MyStruct<{C}> {
22+
type MyItem = u8;
23+
const MY_CONST: usize = C;
24+
}
25+
26+
impl<'a, I, const C: usize> UnwrapItemsExt<{C}> for I
27+
where
28+
{
29+
type Iter = impl MyTrait<'a, {C}>;
30+
31+
fn unwrap_items(self) -> Self::Iter {
32+
MyStruct::<{C}> {}
33+
}
34+
}
35+
36+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/assoc-type-const.rs:6:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Tests that we still detect defining usages when
2+
// lifetimes are used in an associated opaque type
3+
// check-pass
4+
5+
#![feature(type_alias_impl_trait)]
6+
7+
trait UnwrapItemsExt {
8+
type Iter;
9+
fn unwrap_items(self) -> Self::Iter;
10+
}
11+
12+
struct MyStruct {}
13+
14+
trait MyTrait<'a> {}
15+
16+
impl<'a> MyTrait<'a> for MyStruct {}
17+
18+
impl<'a, I> UnwrapItemsExt for I
19+
where
20+
{
21+
type Iter = impl MyTrait<'a>;
22+
23+
fn unwrap_items(self) -> Self::Iter {
24+
MyStruct {}
25+
}
26+
}
27+
28+
fn main() {}

0 commit comments

Comments
 (0)