Skip to content

Commit 39d9b84

Browse files
committed
Handle trait/impl GAC mismatches when inferring missing/placeholder types
1 parent 24afa42 commit 39d9b84

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

compiler/rustc_hir_typeck/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
244244
impl_def_id,
245245
impl_trait_ref.args,
246246
);
247-
Some(tcx.type_of(trait_item_def_id).instantiate(tcx, args))
247+
tcx.check_args_compatible(trait_item_def_id, args)
248+
.then(|| tcx.type_of(trait_item_def_id).instantiate(tcx, args))
248249
} else {
249250
Some(fcx.next_ty_var(span))
250251
}

tests/crashes/124833.rs

-10
This file was deleted.

tests/ui/generic-const-items/assoc-const-missing-type.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
trait Trait {
77
const K<T>: T;
8+
const Q<'a>: &'a str;
89
}
910

1011
impl Trait for () {
1112
const K<T> = ();
1213
//~^ ERROR missing type for `const` item
1314
//~| ERROR mismatched types
1415
//~| 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
1519
}
1620

1721
fn main() {}

tests/ui/generic-const-items/assoc-const-missing-type.stderr

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/assoc-const-missing-type.rs:11:18
2+
--> $DIR/assoc-const-missing-type.rs:12:18
33
|
44
LL | const K<T> = ();
55
| - ^^ expected type parameter `T`, found `()`
@@ -10,13 +10,28 @@ LL | const K<T> = ();
1010
found unit type `()`
1111

1212
error: missing type for `const` item
13-
--> $DIR/assoc-const-missing-type.rs:11:15
13+
--> $DIR/assoc-const-missing-type.rs:12:15
1414
|
1515
LL | const K<T> = ();
1616
| ^ help: provide a type for the associated constant: `()`
1717

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+
1833
error[E0308]: mismatched types
19-
--> $DIR/assoc-const-missing-type.rs:11:18
34+
--> $DIR/assoc-const-missing-type.rs:12:18
2035
|
2136
LL | const K<T> = ();
2237
| - ^^ expected type parameter `T`, found `()`
@@ -27,6 +42,7 @@ LL | const K<T> = ();
2742
found unit type `()`
2843
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2944

30-
error: aborting due to 3 previous errors
45+
error: aborting due to 5 previous errors
3146

32-
For more information about this error, try `rustc --explain E0308`.
47+
Some errors have detailed explanations: E0195, E0308.
48+
For more information about an error, try `rustc --explain E0195`.

0 commit comments

Comments
 (0)