diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-06-30 08:01:14 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-30 08:01:14 +0200 |
| commit | 38e6bba9c1c859166bf8a3d77eefb28c5e30329d (patch) | |
| tree | dd691de801bbe8e2792d98a3af3de7d83793aae1 | |
| parent | c8f50ee11170166e86ab9c9b10c208f51ad998a1 (diff) | |
| parent | a10406318e0888b1356e88ec8866fe260a55e6aa (diff) | |
| download | rust-38e6bba9c1c859166bf8a3d77eefb28c5e30329d.tar.gz rust-38e6bba9c1c859166bf8a3d77eefb28c5e30329d.zip | |
Rollup merge of #113171 - spastorino:new-rpitit-25, r=compiler-errors
Properly implement variances_of for RPITIT GAT This fixes some of the issues found by crater run in https://github.com/rust-lang/rust/pull/112988#issuecomment-1610019572 r? ``@compiler-errors``
| -rw-r--r-- | compiler/rustc_hir_analysis/src/variance/mod.rs | 24 | ||||
| -rw-r--r-- | tests/ui/impl-trait/in-trait/variances-of-gat.rs | 19 |
2 files changed, 34 insertions, 9 deletions
diff --git a/compiler/rustc_hir_analysis/src/variance/mod.rs b/compiler/rustc_hir_analysis/src/variance/mod.rs index 23d8da88a45..49aee6b59a2 100644 --- a/compiler/rustc_hir_analysis/src/variance/mod.rs +++ b/compiler/rustc_hir_analysis/src/variance/mod.rs @@ -7,7 +7,7 @@ use rustc_arena::DroplessArena; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::query::Providers; -use rustc_middle::ty::{self, CrateVariancesMap, SubstsRef, Ty, TyCtxt}; +use rustc_middle::ty::{self, CrateVariancesMap, ImplTraitInTraitData, SubstsRef, Ty, TyCtxt}; use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable}; use std::ops::ControlFlow; @@ -51,20 +51,26 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] { | DefKind::Struct | DefKind::Union | DefKind::Variant - | DefKind::Ctor(..) => {} + | DefKind::Ctor(..) => { + // These are inferred. + let crate_map = tcx.crate_variances(()); + return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]); + } DefKind::OpaqueTy | DefKind::ImplTraitPlaceholder => { return variance_of_opaque(tcx, item_def_id); } - _ => { - // Variance not relevant. - span_bug!(tcx.def_span(item_def_id), "asked to compute variance for wrong kind of item") + DefKind::AssocTy => { + if let Some(ImplTraitInTraitData::Trait { .. }) = + tcx.opt_rpitit_info(item_def_id.to_def_id()) + { + return variance_of_opaque(tcx, item_def_id); + } } + _ => {} } - // Everything else must be inferred. - - let crate_map = tcx.crate_variances(()); - crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]) + // Variance not relevant. + span_bug!(tcx.def_span(item_def_id), "asked to compute variance for wrong kind of item"); } #[instrument(level = "trace", skip(tcx), ret)] diff --git a/tests/ui/impl-trait/in-trait/variances-of-gat.rs b/tests/ui/impl-trait/in-trait/variances-of-gat.rs new file mode 100644 index 00000000000..4008ece94da --- /dev/null +++ b/tests/ui/impl-trait/in-trait/variances-of-gat.rs @@ -0,0 +1,19 @@ +// check-pass +// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty +// revisions: current next + +#![feature(return_position_impl_trait_in_trait)] + +trait Foo {} + +impl Foo for () {} + +trait ThreeCellFragment { + fn ext_cells<'a>(&'a self) -> impl Foo + 'a { + self.ext_adjacent_cells() + } + + fn ext_adjacent_cells<'a>(&'a self) -> impl Foo + 'a; +} + +fn main() {} |
