Skip to content

Commit 909574e

Browse files
authored
Rollup merge of #132559 - bvanjoi:fix-132534, r=compiler-errors
find the generic container rather than simply looking up for the assoc with const arg Fixes #132534 This issue is caused by mismatched generic parameters. Previously, it tried to find `T` in `trait X`, but after this change, it will find `T` in `fn a`. r? `@compiler-errors` as this assertion was introduced by you.
2 parents b9db639 + 6026a0f commit 909574e

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,13 @@ fn const_arg_anon_type_of<'tcx>(tcx: TyCtxt<'tcx>, arg_hir_id: HirId, span: Span
175175
// arm would handle this.
176176
//
177177
// I believe this match arm is only needed for GAT but I am not 100% sure - BoxyUwU
178-
Node::Ty(hir_ty @ hir::Ty { kind: TyKind::Path(QPath::TypeRelative(_, segment)), .. }) => {
178+
Node::Ty(hir_ty @ hir::Ty { kind: TyKind::Path(QPath::TypeRelative(ty, segment)), .. }) => {
179179
// Find the Item containing the associated type so we can create an ItemCtxt.
180180
// Using the ItemCtxt lower the HIR for the unresolved assoc type into a
181181
// ty which is a fully resolved projection.
182182
// For the code example above, this would mean lowering `Self::Assoc<3>`
183183
// to a ty::Alias(ty::Projection, `<Self as Foo>::Assoc<3>`).
184-
let item_def_id = tcx
185-
.hir()
186-
.parent_owner_iter(arg_hir_id)
187-
.find(|(_, node)| matches!(node, OwnerNode::Item(_)))
188-
.unwrap()
189-
.0
190-
.def_id;
184+
let item_def_id = tcx.hir().get_parent_item(ty.hir_id).def_id;
191185
let ty = ItemCtxt::new(tcx, item_def_id).lower_ty(hir_ty);
192186

193187
// Iterate through the generics of the projection to find the one that corresponds to
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// issue#132534
2+
3+
trait X {
4+
fn a<T>() -> T::unknown<{}> {}
5+
//~^ ERROR: associated type `unknown` not found for `T`
6+
//~| ERROR: associated type `unknown` not found for `T`
7+
}
8+
9+
trait Y {
10+
fn a() -> NOT_EXIST::unknown<{}> {}
11+
//~^ ERROR: failed to resolve: use of undeclared type `NOT_EXIST`
12+
}
13+
14+
trait Z<T> {
15+
fn a() -> T::unknown<{}> {}
16+
//~^ ERROR: associated type `unknown` not found for `T`
17+
//~| ERROR: associated type `unknown` not found for `T`
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0220]: associated type `unknown` not found for `T`
2+
--> $DIR/unknown-assoc-with-const-arg.rs:4:21
3+
|
4+
LL | fn a<T>() -> T::unknown<{}> {}
5+
| ^^^^^^^ associated type `unknown` not found
6+
7+
error[E0220]: associated type `unknown` not found for `T`
8+
--> $DIR/unknown-assoc-with-const-arg.rs:15:18
9+
|
10+
LL | fn a() -> T::unknown<{}> {}
11+
| ^^^^^^^ associated type `unknown` not found
12+
13+
error[E0220]: associated type `unknown` not found for `T`
14+
--> $DIR/unknown-assoc-with-const-arg.rs:4:21
15+
|
16+
LL | fn a<T>() -> T::unknown<{}> {}
17+
| ^^^^^^^ associated type `unknown` not found
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
error[E0220]: associated type `unknown` not found for `T`
22+
--> $DIR/unknown-assoc-with-const-arg.rs:15:18
23+
|
24+
LL | fn a() -> T::unknown<{}> {}
25+
| ^^^^^^^ associated type `unknown` not found
26+
|
27+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
28+
29+
error[E0433]: failed to resolve: use of undeclared type `NOT_EXIST`
30+
--> $DIR/unknown-assoc-with-const-arg.rs:10:15
31+
|
32+
LL | fn a() -> NOT_EXIST::unknown<{}> {}
33+
| ^^^^^^^^^ use of undeclared type `NOT_EXIST`
34+
35+
error: aborting due to 5 previous errors
36+
37+
Some errors have detailed explanations: E0220, E0433.
38+
For more information about an error, try `rustc --explain E0220`.

0 commit comments

Comments
 (0)