Skip to content

Commit 4652f5e

Browse files
committed
Auto merge of #100865 - compiler-errors:parent-substs-still, r=cjgillot
Don't drop parent substs when we have no generic parameters in `create_substs_for_ast_path` This bug is being shadowed by an explicit check for `generics.params.is_empty()` in the only parent caller that could trigger it (`create_substs_for_associated_item`). I triggered it on another branch where I'm messing around with astconv stuff. Also, the second commit simplifies `create_substs_for_associated_item`. Removing that explicit check I mentioned above^ and also the special case call to `Astconv::prohibit_generics` causes the UI test `src/test/ui/structs/struct-path-associated-type.stderr` to change, but I think that it's clearer now. The suggestion to remove the generics is actually useful.
2 parents 6f6010b + 102c61f commit 4652f5e

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
365365
// here and so associated type bindings will be handled regardless of whether there are any
366366
// non-`Self` generic parameters.
367367
if generics.params.is_empty() {
368-
return (tcx.intern_substs(&[]), arg_count);
368+
return (tcx.intern_substs(parent_substs), arg_count);
369369
}
370370

371371
struct SubstsForAstPathCtxt<'a, 'tcx> {
@@ -586,7 +586,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
586586

587587
pub(crate) fn create_substs_for_associated_item(
588588
&self,
589-
tcx: TyCtxt<'tcx>,
590589
span: Span,
591590
item_def_id: DefId,
592591
item_segment: &hir::PathSegment<'_>,
@@ -596,22 +595,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
596595
"create_substs_for_associated_item(span: {:?}, item_def_id: {:?}, item_segment: {:?}",
597596
span, item_def_id, item_segment
598597
);
599-
if tcx.generics_of(item_def_id).params.is_empty() {
600-
self.prohibit_generics(slice::from_ref(item_segment).iter(), |_| {});
601-
602-
parent_substs
603-
} else {
604-
self.create_substs_for_ast_path(
605-
span,
606-
item_def_id,
607-
parent_substs,
608-
item_segment,
609-
item_segment.args(),
610-
item_segment.infer_args,
611-
None,
612-
)
613-
.0
614-
}
598+
self.create_substs_for_ast_path(
599+
span,
600+
item_def_id,
601+
parent_substs,
602+
item_segment,
603+
item_segment.args(),
604+
item_segment.infer_args,
605+
None,
606+
)
607+
.0
615608
}
616609

617610
/// Instantiates the path for the given trait reference, assuming that it's
@@ -1121,7 +1114,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11211114
};
11221115

11231116
let substs_trait_ref_and_assoc_item = self.create_substs_for_associated_item(
1124-
tcx,
11251117
path_span,
11261118
assoc_item.def_id,
11271119
&item_segment,
@@ -2100,7 +2092,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21002092
self.ast_path_to_mono_trait_ref(span, trait_def_id, self_ty, trait_segment, false);
21012093

21022094
let item_substs = self.create_substs_for_associated_item(
2103-
tcx,
21042095
span,
21052096
item_def_id,
21062097
item_segment,

compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
276276

277277
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
278278
self,
279-
self.tcx,
280279
span,
281280
item_def_id,
282281
item_segment,

compiler/rustc_typeck/src/collect.rs

-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
430430
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
431431
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
432432
self,
433-
self.tcx,
434433
span,
435434
item_def_id,
436435
item_segment,

src/test/ui/structs/struct-path-associated-type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn f<T: Tr>() {
1313
//~^ ERROR expected struct, variant or union type, found associated type
1414
let z = T::A::<u8> {};
1515
//~^ ERROR expected struct, variant or union type, found associated type
16-
//~| ERROR type arguments are not allowed on this type
16+
//~| ERROR this associated type takes 0 generic arguments but 1 generic argument was supplied
1717
match S {
1818
T::A {} => {}
1919
//~^ ERROR expected struct, variant or union type, found associated type
@@ -22,7 +22,7 @@ fn f<T: Tr>() {
2222

2323
fn g<T: Tr<A = S>>() {
2424
let s = T::A {}; // OK
25-
let z = T::A::<u8> {}; //~ ERROR type arguments are not allowed on this type
25+
let z = T::A::<u8> {}; //~ ERROR this associated type takes 0 generic arguments but 1 generic argument was supplied
2626
match S {
2727
T::A {} => {} // OK
2828
}

src/test/ui/structs/struct-path-associated-type.stderr

+21-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ error[E0071]: expected struct, variant or union type, found associated type
44
LL | let s = T::A {};
55
| ^^^^ not a struct
66

7-
error[E0109]: type arguments are not allowed on this type
8-
--> $DIR/struct-path-associated-type.rs:14:20
7+
error[E0107]: this associated type takes 0 generic arguments but 1 generic argument was supplied
8+
--> $DIR/struct-path-associated-type.rs:14:16
99
|
1010
LL | let z = T::A::<u8> {};
11-
| - ^^ type argument not allowed
11+
| ^------ help: remove these generics
1212
| |
13-
| not allowed on this type
13+
| expected 0 generic arguments
14+
|
15+
note: associated type defined here, with 0 generic parameters
16+
--> $DIR/struct-path-associated-type.rs:4:10
17+
|
18+
LL | type A;
19+
| ^
1420

1521
error[E0071]: expected struct, variant or union type, found associated type
1622
--> $DIR/struct-path-associated-type.rs:14:13
@@ -24,13 +30,19 @@ error[E0071]: expected struct, variant or union type, found associated type
2430
LL | T::A {} => {}
2531
| ^^^^ not a struct
2632

27-
error[E0109]: type arguments are not allowed on this type
28-
--> $DIR/struct-path-associated-type.rs:25:20
33+
error[E0107]: this associated type takes 0 generic arguments but 1 generic argument was supplied
34+
--> $DIR/struct-path-associated-type.rs:25:16
2935
|
3036
LL | let z = T::A::<u8> {};
31-
| - ^^ type argument not allowed
37+
| ^------ help: remove these generics
3238
| |
33-
| not allowed on this type
39+
| expected 0 generic arguments
40+
|
41+
note: associated type defined here, with 0 generic parameters
42+
--> $DIR/struct-path-associated-type.rs:4:10
43+
|
44+
LL | type A;
45+
| ^
3446

3547
error[E0223]: ambiguous associated type
3648
--> $DIR/struct-path-associated-type.rs:32:13
@@ -52,5 +64,5 @@ LL | S::A {} => {}
5264

5365
error: aborting due to 8 previous errors
5466

55-
Some errors have detailed explanations: E0071, E0109, E0223.
67+
Some errors have detailed explanations: E0071, E0107, E0223.
5668
For more information about an error, try `rustc --explain E0071`.

0 commit comments

Comments
 (0)