diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-04-19 19:30:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-19 19:30:49 +0200 |
| commit | 26d4b1bfbae5d3e752359cd1367e16271c5f36e1 (patch) | |
| tree | 069ad4428e0fa931d822a6eaa70e09da0d85832d | |
| parent | b1eee427a0bc0459ec5176b71ff71797727a1794 (diff) | |
| parent | ffb42065774b5d500ef5649d4caa91e9e344ff46 (diff) | |
| download | rust-26d4b1bfbae5d3e752359cd1367e16271c5f36e1.tar.gz rust-26d4b1bfbae5d3e752359cd1367e16271c5f36e1.zip | |
Rollup merge of #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
| -rw-r--r-- | compiler/rustc_hir_analysis/src/collect/generics_of.rs | 15 | ||||
| -rw-r--r-- | tests/ui/type-alias-impl-trait/variance.rs | 24 | ||||
| -rw-r--r-- | tests/ui/type-alias-impl-trait/variance.stderr | 56 |
3 files changed, 88 insertions, 7 deletions
diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index 4d6a02f50bf..f83ddc51c76 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -195,16 +195,19 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { } Some(fn_def_id.to_def_id()) } - ItemKind::OpaqueTy(hir::OpaqueTy { - origin: hir::OpaqueTyOrigin::TyAlias { .. }, + ItemKind::OpaqueTy(&hir::OpaqueTy { + origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty }, .. }) => { - let parent_id = tcx.hir().get_parent_item(hir_id); - assert_ne!(parent_id, hir::CRATE_OWNER_ID); - debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id); + if in_assoc_ty { + assert!(matches!(tcx.def_kind(parent), DefKind::AssocTy)); + } else { + assert!(matches!(tcx.def_kind(parent), DefKind::TyAlias)); + } + debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent); // Opaque types are always nested within another item, and // inherit the generics of the item. - Some(parent_id.to_def_id()) + Some(parent.to_def_id()) } _ => None, }, diff --git a/tests/ui/type-alias-impl-trait/variance.rs b/tests/ui/type-alias-impl-trait/variance.rs index 4b9fa67fd64..ba52eaa0359 100644 --- a/tests/ui/type-alias-impl-trait/variance.rs +++ b/tests/ui/type-alias-impl-trait/variance.rs @@ -52,4 +52,28 @@ impl<'i> Foo<'i> for () { //~^ ERROR: unconstrained opaque type } +trait Nesting<'a> { + type Output; +} +impl<'a> Nesting<'a> for &'a () { + type Output = &'a (); +} +type NestedDeeply<'a> = + impl Nesting< //~ [*, o] + 'a, + Output = impl Nesting< //~ [*, o] + 'a, + Output = impl Nesting< //~ [*, o] + 'a, + Output = impl Nesting< //~ [*, o] + 'a, + Output = impl Nesting<'a> //~ [*, o] + > + >, + >, + >; +fn test<'a>() -> NestedDeeply<'a> { + &() +} + fn main() {} diff --git a/tests/ui/type-alias-impl-trait/variance.stderr b/tests/ui/type-alias-impl-trait/variance.stderr index 1aaf36223b7..e5ced7a4981 100644 --- a/tests/ui/type-alias-impl-trait/variance.stderr +++ b/tests/ui/type-alias-impl-trait/variance.stderr @@ -176,6 +176,60 @@ error: [*, *, o, o] LL | type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>; | ^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 24 previous errors +error: [*, o] + --> $DIR/variance.rs:62:5 + | +LL | / impl Nesting< +LL | | 'a, +LL | | Output = impl Nesting< +LL | | 'a, +... | +LL | | >, +LL | | >; + | |_____^ + +error: [*, o] + --> $DIR/variance.rs:64:18 + | +LL | Output = impl Nesting< + | __________________^ +LL | | 'a, +LL | | Output = impl Nesting< +LL | | 'a, +... | +LL | | >, +LL | | >, + | |_________^ + +error: [*, o] + --> $DIR/variance.rs:66:22 + | +LL | Output = impl Nesting< + | ______________________^ +LL | | 'a, +LL | | Output = impl Nesting< +LL | | 'a, +LL | | Output = impl Nesting<'a> +LL | | > +LL | | >, + | |_____________^ + +error: [*, o] + --> $DIR/variance.rs:68:26 + | +LL | Output = impl Nesting< + | __________________________^ +LL | | 'a, +LL | | Output = impl Nesting<'a> +LL | | > + | |_________________^ + +error: [*, o] + --> $DIR/variance.rs:70:30 + | +LL | Output = impl Nesting<'a> + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 29 previous errors For more information about this error, try `rustc --explain E0657`. |
