Skip to content

Commit ecb118e

Browse files
committed
Handle trait/impl GAC mismatches when inferring missing/placeholder types
1 parent e10e9fe commit ecb118e

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
236236
&& let ty::AssocKind::Const = item.kind
237237
&& let ty::ImplContainer = item.container
238238
&& let Some(trait_item) = item.trait_item_def_id
239+
// We don't want to call `tcx.compare_impl_const` here to avoid cycle errors. This is a
240+
// very naive check to prevent us from messing up the generic param instantiation below.
241+
&& tcx.generics_of(def_id).own_counts() == tcx.generics_of(trait_item).own_counts()
239242
{
240243
let impl_item = item.container_id(tcx);
241244
let impl_trait_ref = tcx.impl_trait_ref(impl_item).unwrap().instantiate_identity();

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl GenericParamDef {
116116
}
117117
}
118118

119-
#[derive(Default)]
119+
#[derive(Default, PartialEq, Eq)]
120120
pub struct GenericParamCount {
121121
pub lifetimes: usize,
122122
pub types: usize,

tests/crashes/124833.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 21 additions & 5 deletions
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)