summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_infer/src/infer/outlives
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_infer/src/infer/outlives')
-rw-r--r--compiler/rustc_infer/src/infer/outlives/components.rs8
-rw-r--r--compiler/rustc_infer/src/infer/outlives/for_liveness.rs129
-rw-r--r--compiler/rustc_infer/src/infer/outlives/mod.rs1
-rw-r--r--compiler/rustc_infer/src/infer/outlives/test_type_match.rs28
-rw-r--r--compiler/rustc_infer/src/infer/outlives/verify.rs28
5 files changed, 167 insertions, 27 deletions
diff --git a/compiler/rustc_infer/src/infer/outlives/components.rs b/compiler/rustc_infer/src/infer/outlives/components.rs
index 6a9d40daa..38819e8ad 100644
--- a/compiler/rustc_infer/src/infer/outlives/components.rs
+++ b/compiler/rustc_infer/src/infer/outlives/components.rs
@@ -102,17 +102,17 @@ fn compute_components<'tcx>(
compute_components(tcx, tupled_ty, out, visited);
}
- ty::Generator(_, ref args, _) => {
+ ty::Coroutine(_, ref args, _) => {
// Same as the closure case
- let tupled_ty = args.as_generator().tupled_upvars_ty();
+ let tupled_ty = args.as_coroutine().tupled_upvars_ty();
compute_components(tcx, tupled_ty, out, visited);
- // We ignore regions in the generator interior as we don't
+ // We ignore regions in the coroutine interior as we don't
// want these to affect region inference
}
// All regions are bound inside a witness
- ty::GeneratorWitness(..) => (),
+ ty::CoroutineWitness(..) => (),
// OutlivesTypeParameterEnv -- the actual checking that `X:'a`
// is implied by the environment is done in regionck.
diff --git a/compiler/rustc_infer/src/infer/outlives/for_liveness.rs b/compiler/rustc_infer/src/infer/outlives/for_liveness.rs
new file mode 100644
index 000000000..398ac94ee
--- /dev/null
+++ b/compiler/rustc_infer/src/infer/outlives/for_liveness.rs
@@ -0,0 +1,129 @@
+use rustc_middle::ty::{
+ self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
+};
+
+use std::ops::ControlFlow;
+
+use crate::infer::outlives::test_type_match;
+use crate::infer::region_constraints::VerifyIfEq;
+
+/// Visits free regions in the type that are relevant for liveness computation.
+/// These regions are passed to `OP`.
+///
+/// Specifically, we visit all of the regions of types recursively, except if
+/// the type is an alias, we look at the outlives bounds in the param-env
+/// and alias's item bounds. If there is a unique outlives bound, then visit
+/// that instead. If there is not a unique but there is a `'static` outlives
+/// bound, then don't visit anything. Otherwise, walk through the opaque's
+/// regions structurally.
+pub struct FreeRegionsVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> {
+ pub tcx: TyCtxt<'tcx>,
+ pub param_env: ty::ParamEnv<'tcx>,
+ pub op: OP,
+}
+
+impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for FreeRegionsVisitor<'tcx, OP>
+where
+ OP: FnMut(ty::Region<'tcx>),
+{
+ fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
+ &mut self,
+ t: &ty::Binder<'tcx, T>,
+ ) -> ControlFlow<Self::BreakTy> {
+ t.super_visit_with(self);
+ ControlFlow::Continue(())
+ }
+
+ fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
+ match *r {
+ // ignore bound regions, keep visiting
+ ty::ReLateBound(_, _) => ControlFlow::Continue(()),
+ _ => {
+ (self.op)(r);
+ ControlFlow::Continue(())
+ }
+ }
+ }
+
+ fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
+ // We're only interested in types involving regions
+ if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) {
+ return ControlFlow::Continue(());
+ }
+
+ // FIXME: Don't consider alias bounds on types that have escaping bound
+ // vars. See #117455.
+ if ty.has_escaping_bound_vars() {
+ return ty.super_visit_with(self);
+ }
+
+ match ty.kind() {
+ // We can prove that an alias is live two ways:
+ // 1. All the components are live.
+ //
+ // 2. There is a known outlives bound or where-clause, and that
+ // region is live.
+ //
+ // We search through the item bounds and where clauses for
+ // either `'static` or a unique outlives region, and if one is
+ // found, we just need to prove that that region is still live.
+ // If one is not found, then we continue to walk through the alias.
+ ty::Alias(kind, ty::AliasTy { def_id, args, .. }) => {
+ let tcx = self.tcx;
+ let param_env = self.param_env;
+ let outlives_bounds: Vec<_> = tcx
+ .item_bounds(def_id)
+ .iter_instantiated(tcx, args)
+ .chain(param_env.caller_bounds())
+ .filter_map(|clause| {
+ let outlives = clause.as_type_outlives_clause()?;
+ if let Some(outlives) = outlives.no_bound_vars()
+ && outlives.0 == ty
+ {
+ Some(outlives.1)
+ } else {
+ test_type_match::extract_verify_if_eq(
+ tcx,
+ param_env,
+ &outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| {
+ VerifyIfEq { ty, bound }
+ }),
+ ty,
+ )
+ }
+ })
+ .collect();
+ // If we find `'static`, then we know the alias doesn't capture *any* regions.
+ // Otherwise, all of the outlives regions should be equal -- if they're not,
+ // we don't really know how to proceed, so we continue recursing through the
+ // alias.
+ if outlives_bounds.contains(&tcx.lifetimes.re_static) {
+ // no
+ } else if let Some(r) = outlives_bounds.first()
+ && outlives_bounds[1..].iter().all(|other_r| other_r == r)
+ {
+ assert!(r.type_flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS));
+ r.visit_with(self)?;
+ } else {
+ // Skip lifetime parameters that are not captures.
+ let variances = match kind {
+ ty::Opaque => Some(self.tcx.variances_of(*def_id)),
+ _ => None,
+ };
+
+ for (idx, s) in args.iter().enumerate() {
+ if variances.map(|variances| variances[idx]) != Some(ty::Variance::Bivariant) {
+ s.visit_with(self)?;
+ }
+ }
+ }
+ }
+
+ _ => {
+ ty.super_visit_with(self)?;
+ }
+ }
+
+ ControlFlow::Continue(())
+ }
+}
diff --git a/compiler/rustc_infer/src/infer/outlives/mod.rs b/compiler/rustc_infer/src/infer/outlives/mod.rs
index cb92fc6dd..0987915f4 100644
--- a/compiler/rustc_infer/src/infer/outlives/mod.rs
+++ b/compiler/rustc_infer/src/infer/outlives/mod.rs
@@ -9,6 +9,7 @@ use rustc_middle::ty;
pub mod components;
pub mod env;
+pub mod for_liveness;
pub mod obligations;
pub mod test_type_match;
pub mod verify;
diff --git a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs
index fefa89595..6f973ee37 100644
--- a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs
+++ b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs
@@ -44,7 +44,7 @@ pub fn extract_verify_if_eq<'tcx>(
test_ty: Ty<'tcx>,
) -> Option<ty::Region<'tcx>> {
assert!(!verify_if_eq_b.has_escaping_bound_vars());
- let mut m = Match::new(tcx, param_env);
+ let mut m = MatchAgainstHigherRankedOutlives::new(tcx, param_env);
let verify_if_eq = verify_if_eq_b.skip_binder();
m.relate(verify_if_eq.ty, test_ty).ok()?;
@@ -87,24 +87,32 @@ pub(super) fn can_match_erased_ty<'tcx>(
// pointless micro-optimization
true
} else {
- Match::new(tcx, param_env).relate(outlives_ty, erased_ty).is_ok()
+ MatchAgainstHigherRankedOutlives::new(tcx, param_env).relate(outlives_ty, erased_ty).is_ok()
}
}
-struct Match<'tcx> {
+struct MatchAgainstHigherRankedOutlives<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
pattern_depth: ty::DebruijnIndex,
map: FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
}
-impl<'tcx> Match<'tcx> {
- fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Match<'tcx> {
- Match { tcx, param_env, pattern_depth: ty::INNERMOST, map: FxHashMap::default() }
+impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
+ fn new(
+ tcx: TyCtxt<'tcx>,
+ param_env: ty::ParamEnv<'tcx>,
+ ) -> MatchAgainstHigherRankedOutlives<'tcx> {
+ MatchAgainstHigherRankedOutlives {
+ tcx,
+ param_env,
+ pattern_depth: ty::INNERMOST,
+ map: FxHashMap::default(),
+ }
}
}
-impl<'tcx> Match<'tcx> {
+impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
/// Creates the "Error" variant that signals "no match".
fn no_match<T>(&self) -> RelateResult<'tcx, T> {
Err(TypeError::Mismatch)
@@ -134,7 +142,7 @@ impl<'tcx> Match<'tcx> {
}
}
-impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
+impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
fn tag(&self) -> &'static str {
"Match"
}
@@ -169,7 +177,9 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
value: ty::Region<'tcx>,
) -> RelateResult<'tcx, ty::Region<'tcx>> {
debug!("self.pattern_depth = {:?}", self.pattern_depth);
- if let ty::RegionKind::ReLateBound(depth, br) = pattern.kind() && depth == self.pattern_depth {
+ if let ty::RegionKind::ReLateBound(depth, br) = pattern.kind()
+ && depth == self.pattern_depth
+ {
self.bind(br, value)
} else if pattern == value {
Ok(pattern)
diff --git a/compiler/rustc_infer/src/infer/outlives/verify.rs b/compiler/rustc_infer/src/infer/outlives/verify.rs
index 4279d0ab7..7f0a4717d 100644
--- a/compiler/rustc_infer/src/infer/outlives/verify.rs
+++ b/compiler/rustc_infer/src/infer/outlives/verify.rs
@@ -108,20 +108,20 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
let alias_ty_as_ty = alias_ty.to_ty(self.tcx);
// Search the env for where clauses like `P: 'a`.
- let env_bounds = self
- .approx_declared_bounds_from_env(alias_ty)
- .into_iter()
- .map(|binder| {
- if let Some(ty::OutlivesPredicate(ty, r)) = binder.no_bound_vars() && ty == alias_ty_as_ty {
- // Micro-optimize if this is an exact match (this
- // occurs often when there are no region variables
- // involved).
- VerifyBound::OutlivedBy(r)
- } else {
- let verify_if_eq_b = binder.map_bound(|ty::OutlivesPredicate(ty, bound)| VerifyIfEq { ty, bound });
- VerifyBound::IfEq(verify_if_eq_b)
- }
- });
+ let env_bounds = self.approx_declared_bounds_from_env(alias_ty).into_iter().map(|binder| {
+ if let Some(ty::OutlivesPredicate(ty, r)) = binder.no_bound_vars()
+ && ty == alias_ty_as_ty
+ {
+ // Micro-optimize if this is an exact match (this
+ // occurs often when there are no region variables
+ // involved).
+ VerifyBound::OutlivedBy(r)
+ } else {
+ let verify_if_eq_b =
+ binder.map_bound(|ty::OutlivesPredicate(ty, bound)| VerifyIfEq { ty, bound });
+ VerifyBound::IfEq(verify_if_eq_b)
+ }
+ });
// Extend with bounds that we can find from the definition.
let definition_bounds =