Skip to content

Commit 26d4b1b

Browse files
authored
Rollup merge of rust-lang#124106 - compiler-errors:tait-lifetime-dedup, r=oli-obk
Don't repeatedly duplicate TAIT lifetimes for each subsequently nested TAIT Make it so that nested TAITs inherit the lifetimes from their parent item, not their parent TAIT. This is because we don't need to re-duplicate lifetimes for nested TAITs over and over, since the only lifetimes they can capture are from the parent item anyways. This mirrors how RPITs work. This is **not** a functional change that should be observable, since the whole point of duplicating lifetimes and marking the shadowed ones (and uncaptured ones) as bivariant is designed to *not* be observable. r? oli-obk
2 parents b1eee42 + ffb4206 commit 26d4b1b

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,19 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
195195
}
196196
Some(fn_def_id.to_def_id())
197197
}
198-
ItemKind::OpaqueTy(hir::OpaqueTy {
199-
origin: hir::OpaqueTyOrigin::TyAlias { .. },
198+
ItemKind::OpaqueTy(&hir::OpaqueTy {
199+
origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty },
200200
..
201201
}) => {
202-
let parent_id = tcx.hir().get_parent_item(hir_id);
203-
assert_ne!(parent_id, hir::CRATE_OWNER_ID);
204-
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id);
202+
if in_assoc_ty {
203+
assert!(matches!(tcx.def_kind(parent), DefKind::AssocTy));
204+
} else {
205+
assert!(matches!(tcx.def_kind(parent), DefKind::TyAlias));
206+
}
207+
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
205208
// Opaque types are always nested within another item, and
206209
// inherit the generics of the item.
207-
Some(parent_id.to_def_id())
210+
Some(parent.to_def_id())
208211
}
209212
_ => None,
210213
},

tests/ui/type-alias-impl-trait/variance.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ impl<'i> Foo<'i> for () {
5252
//~^ ERROR: unconstrained opaque type
5353
}
5454

55+
trait Nesting<'a> {
56+
type Output;
57+
}
58+
impl<'a> Nesting<'a> for &'a () {
59+
type Output = &'a ();
60+
}
61+
type NestedDeeply<'a> =
62+
impl Nesting< //~ [*, o]
63+
'a,
64+
Output = impl Nesting< //~ [*, o]
65+
'a,
66+
Output = impl Nesting< //~ [*, o]
67+
'a,
68+
Output = impl Nesting< //~ [*, o]
69+
'a,
70+
Output = impl Nesting<'a> //~ [*, o]
71+
>
72+
>,
73+
>,
74+
>;
75+
fn test<'a>() -> NestedDeeply<'a> {
76+
&()
77+
}
78+
5579
fn main() {}

tests/ui/type-alias-impl-trait/variance.stderr

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,60 @@ error: [*, *, o, o]
176176
LL | type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>;
177177
| ^^^^^^^^^^^^^^^^^^^^^^^^^
178178

179-
error: aborting due to 24 previous errors
179+
error: [*, o]
180+
--> $DIR/variance.rs:62:5
181+
|
182+
LL | / impl Nesting<
183+
LL | | 'a,
184+
LL | | Output = impl Nesting<
185+
LL | | 'a,
186+
... |
187+
LL | | >,
188+
LL | | >;
189+
| |_____^
190+
191+
error: [*, o]
192+
--> $DIR/variance.rs:64:18
193+
|
194+
LL | Output = impl Nesting<
195+
| __________________^
196+
LL | | 'a,
197+
LL | | Output = impl Nesting<
198+
LL | | 'a,
199+
... |
200+
LL | | >,
201+
LL | | >,
202+
| |_________^
203+
204+
error: [*, o]
205+
--> $DIR/variance.rs:66:22
206+
|
207+
LL | Output = impl Nesting<
208+
| ______________________^
209+
LL | | 'a,
210+
LL | | Output = impl Nesting<
211+
LL | | 'a,
212+
LL | | Output = impl Nesting<'a>
213+
LL | | >
214+
LL | | >,
215+
| |_____________^
216+
217+
error: [*, o]
218+
--> $DIR/variance.rs:68:26
219+
|
220+
LL | Output = impl Nesting<
221+
| __________________________^
222+
LL | | 'a,
223+
LL | | Output = impl Nesting<'a>
224+
LL | | >
225+
| |_________________^
226+
227+
error: [*, o]
228+
--> $DIR/variance.rs:70:30
229+
|
230+
LL | Output = impl Nesting<'a>
231+
| ^^^^^^^^^^^^^^^^
232+
233+
error: aborting due to 29 previous errors
180234

181235
For more information about this error, try `rustc --explain E0657`.

0 commit comments

Comments
 (0)