Skip to content

Commit 24afa42

Browse files
committed
Properly deal with missing/placeholder types inside GACs
1 parent 791adf7 commit 24afa42

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

compiler/rustc_hir_typeck/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,16 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
235235
if let Some(item) = tcx.opt_associated_item(def_id.into())
236236
&& let ty::AssocKind::Const = item.kind
237237
&& let ty::ImplContainer = item.container
238-
&& let Some(trait_item) = item.trait_item_def_id
238+
&& let Some(trait_item_def_id) = item.trait_item_def_id
239239
{
240-
let args =
241-
tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
242-
Some(tcx.type_of(trait_item).instantiate(tcx, args))
240+
let impl_def_id = item.container_id(tcx);
241+
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
242+
let args = ty::GenericArgs::identity_for_item(tcx, def_id).rebase_onto(
243+
tcx,
244+
impl_def_id,
245+
impl_trait_ref.args,
246+
);
247+
Some(tcx.type_of(trait_item_def_id).instantiate(tcx, args))
243248
} else {
244249
Some(fcx.next_ty_var(span))
245250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Ensure that we properly deal with missing/placeholder types inside GACs.
2+
// issue: rust-lang/rust#124833
3+
#![feature(generic_const_items)]
4+
#![allow(incomplete_features)]
5+
6+
trait Trait {
7+
const K<T>: T;
8+
}
9+
10+
impl Trait for () {
11+
const K<T> = ();
12+
//~^ ERROR missing type for `const` item
13+
//~| ERROR mismatched types
14+
//~| ERROR mismatched types
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/assoc-const-missing-type.rs:11:18
3+
|
4+
LL | const K<T> = ();
5+
| - ^^ expected type parameter `T`, found `()`
6+
| |
7+
| expected this type parameter
8+
|
9+
= note: expected type parameter `T`
10+
found unit type `()`
11+
12+
error: missing type for `const` item
13+
--> $DIR/assoc-const-missing-type.rs:11:15
14+
|
15+
LL | const K<T> = ();
16+
| ^ help: provide a type for the associated constant: `()`
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/assoc-const-missing-type.rs:11:18
20+
|
21+
LL | const K<T> = ();
22+
| - ^^ expected type parameter `T`, found `()`
23+
| |
24+
| expected this type parameter
25+
|
26+
= note: expected type parameter `T`
27+
found unit type `()`
28+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
29+
30+
error: aborting due to 3 previous errors
31+
32+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)