diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2022-10-23 12:59:40 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-11-12 10:00:28 +0000 |
| commit | 47de11f1ed133305a8dbd5be11b73acd0a085ba9 (patch) | |
| tree | 6374b9ee637ce1da4b90d4a0eaad549950a7f772 | |
| parent | b676fc4d8ee5a94cc4270d54005899c599b4d818 (diff) | |
| download | rust-47de11f1ed133305a8dbd5be11b73acd0a085ba9.tar.gz rust-47de11f1ed133305a8dbd5be11b73acd0a085ba9.zip | |
Use variances for defining use diag.
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 58 |
1 files changed, 5 insertions, 53 deletions
diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index bf000e4fd0e..169e8e792ab 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1345,61 +1345,13 @@ impl<'tcx> OpaqueHiddenType<'tcx> { // type Foo<'a, 'b, 'c> = impl Trait<'a> + 'b; // ``` // we may not use `'c` in the hidden type. - struct OpaqueTypeLifetimeCollector<'tcx> { - lifetimes: FxHashSet<ty::Region<'tcx>>, - } - - impl<'tcx> ty::TypeVisitor<'tcx> for OpaqueTypeLifetimeCollector<'tcx> { - fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> { - self.lifetimes.insert(r); - r.super_visit_with(self) - } - } + let variances = tcx.variances_of(def_id); + debug!(?variances); - let mut collector = OpaqueTypeLifetimeCollector { lifetimes: Default::default() }; - - for pred in tcx.bound_explicit_item_bounds(def_id.to_def_id()).transpose_iter() { - let pred = pred.map_bound(|(pred, _)| *pred).subst(tcx, id_substs); - - trace!(pred=?pred.kind()); - - // We only ignore opaque type substs if the opaque type is the outermost type. - // The opaque type may be nested within itself via recursion in e.g. - // type Foo<'a> = impl PartialEq<Foo<'a>>; - // which thus mentions `'a` and should thus accept hidden types that borrow 'a - // instead of requiring an additional `+ 'a`. - match pred.kind().skip_binder() { - ty::PredicateKind::Trait(TraitPredicate { - trait_ref: ty::TraitRef { def_id: _, substs }, - constness: _, - polarity: _, - }) => { - trace!(?substs); - for subst in &substs[1..] { - subst.visit_with(&mut collector); - } - } - ty::PredicateKind::Projection(ty::ProjectionPredicate { - projection_ty: ty::ProjectionTy { substs, item_def_id: _ }, - term, - }) => { - for subst in &substs[1..] { - subst.visit_with(&mut collector); - } - term.visit_with(&mut collector); - } - _ => { - pred.visit_with(&mut collector); - } - } - } - let lifetimes = collector.lifetimes; - trace!(?lifetimes); map.filter(|(_, v)| { - let ty::GenericArgKind::Lifetime(lt) = v.unpack() else { - return true; - }; - lifetimes.contains(<) + let ty::GenericArgKind::Lifetime(lt) = v.unpack() else { return true }; + let ty::ReEarlyBound(ebr) = lt.kind() else { bug!() }; + variances[ebr.index as usize] == ty::Variance::Invariant }) .collect() } |
