Skip to content

Commit 7c54789

Browse files
committed
Auto merge of #125457 - fmease:gacs-diag-infer-plac-missing-ty, r=compiler-errors
Properly deal with missing/placeholder types inside GACs Fixes #124833. r? oli-obk (#123130)
2 parents 7601adc + 39d9b84 commit 7c54789

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

Diff for: compiler/rustc_hir_typeck/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,17 @@ 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+
tcx.check_args_compatible(trait_item_def_id, args)
248+
.then(|| tcx.type_of(trait_item_def_id).instantiate(tcx, args))
243249
} else {
244250
Some(fcx.next_ty_var(span))
245251
}

Diff for: tests/crashes/124833.rs

-10
This file was deleted.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
const Q<'a>: &'a str;
9+
}
10+
11+
impl Trait for () {
12+
const K<T> = ();
13+
//~^ ERROR missing type for `const` item
14+
//~| ERROR mismatched types
15+
//~| ERROR mismatched types
16+
const Q = "";
17+
//~^ ERROR missing type for `const` item
18+
//~| ERROR lifetime parameters or bounds on const `Q` do not match the trait declaration
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/assoc-const-missing-type.rs:12: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:12:15
14+
|
15+
LL | const K<T> = ();
16+
| ^ help: provide a type for the associated constant: `()`
17+
18+
error[E0195]: lifetime parameters or bounds on const `Q` do not match the trait declaration
19+
--> $DIR/assoc-const-missing-type.rs:16:12
20+
|
21+
LL | const Q<'a>: &'a str;
22+
| ---- lifetimes in impl do not match this const in trait
23+
...
24+
LL | const Q = "";
25+
| ^ lifetimes do not match const in trait
26+
27+
error: missing type for `const` item
28+
--> $DIR/assoc-const-missing-type.rs:16:12
29+
|
30+
LL | const Q = "";
31+
| ^ help: provide a type for the associated constant: `: &str`
32+
33+
error[E0308]: mismatched types
34+
--> $DIR/assoc-const-missing-type.rs:12:18
35+
|
36+
LL | const K<T> = ();
37+
| - ^^ expected type parameter `T`, found `()`
38+
| |
39+
| expected this type parameter
40+
|
41+
= note: expected type parameter `T`
42+
found unit type `()`
43+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
44+
45+
error: aborting due to 5 previous errors
46+
47+
Some errors have detailed explanations: E0195, E0308.
48+
For more information about an error, try `rustc --explain E0195`.

0 commit comments

Comments
 (0)