Skip to content

Commit

Permalink
Consider alias outlives bounds on opaque hidden type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 27, 2023
1 parent 0135ccd commit 2c82fdc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 110 deletions.
107 changes: 11 additions & 96 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_index::bit_set::HybridBitSet;
use rustc_index::interval::IntervalSet;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::test_type_match;
use rustc_infer::infer::region_constraints::VerifyIfEq;
use rustc_infer::infer::opaque_types::VisitFreeRegionsVisitor;
use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, Local, Location};
use rustc_middle::traits::query::DropckOutlivesResult;
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
};
use rustc_middle::ty::{Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
use rustc_span::DUMMY_SP;
use rustc_trait_selection::traits::query::type_op::outlives::DropckOutlives;
use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
use std::ops::ControlFlow;
use std::rc::Rc;

use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
Expand Down Expand Up @@ -559,99 +555,18 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
"make_all_regions_live: live_at={}",
values::location_set_str(elements, live_at.iter()),
);

struct MakeAllRegionsLive<'a, 'b, 'tcx> {
typeck: &'b mut TypeChecker<'a, 'tcx>,
live_at: &'b IntervalSet<PointIndex>,
}
impl<'tcx> MakeAllRegionsLive<'_, '_, 'tcx> {
/// 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.
fn make_alias_live(&mut self, t: Ty<'tcx>) -> ControlFlow<!> {
let ty::Alias(_kind, alias_ty) = t.kind() else {
bug!("`make_alias_live` only takes alias types");
};
let tcx = self.typeck.infcx.tcx;
let param_env = self.typeck.param_env;
let outlives_bounds: Vec<_> = tcx
.item_bounds(alias_ty.def_id)
.iter_instantiated(tcx, alias_ty.args)
.filter_map(|clause| {
if let Some(outlives) = clause.as_type_outlives_clause()
&& outlives.skip_binder().0 == t
{
Some(outlives.skip_binder().1)
} else {
None
}
})
.chain(param_env.caller_bounds().iter().filter_map(|clause| {
let outlives = clause.as_type_outlives_clause()?;
if let Some(outlives) = outlives.no_bound_vars()
&& outlives.0 == t
{
Some(outlives.1)
} else {
test_type_match::extract_verify_if_eq(
tcx,
param_env,
&outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| {
VerifyIfEq { ty, bound }
}),
t,
)
}
}))
.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) {
ControlFlow::Continue(())
} else if let Some(r) = outlives_bounds.first()
&& outlives_bounds[1..].iter().all(|other_r| other_r == r)
{
r.visit_with(self)
} else {
t.super_visit_with(self)
}
}
}
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MakeAllRegionsLive<'_, '_, 'tcx> {
type BreakTy = !;

fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
if r.is_late_bound() {
return ControlFlow::Continue(());
}
let live_region_vid =
self.typeck.borrowck_context.universal_regions.to_region_vid(r);
self.typeck
value.visit_with(&mut VisitFreeRegionsVisitor {
tcx: typeck.tcx(),
param_env: typeck.param_env,
op: |r| {
let live_region_vid = typeck.borrowck_context.universal_regions.to_region_vid(r);
typeck
.borrowck_context
.constraints
.liveness_constraints
.add_elements(live_region_vid, self.live_at);
ControlFlow::Continue(())
}

fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
if !t.has_free_regions() {
ControlFlow::Continue(())
} else if let ty::Alias(..) = t.kind() {
self.make_alias_live(t)
} else {
t.super_visit_with(self)
}
}
}
value.visit_with(&mut MakeAllRegionsLive { typeck, live_at });
.add_elements(live_region_vid, live_at);
},
});
}

fn compute_drop_data(
Expand Down
89 changes: 75 additions & 14 deletions compiler/rustc_infer/src/infer/opaque_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::outlives::test_type_match;
use super::region_constraints::VerifyIfEq;
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use super::{DefineOpaqueTypes, InferResult};
use crate::errors::OpaqueHiddenTypeDiag;
Expand Down Expand Up @@ -380,8 +382,9 @@ impl<'tcx> InferCtxt<'tcx> {
.collect(),
);

concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
concrete_ty.visit_with(&mut VisitFreeRegionsVisitor {
tcx: self.tcx,
param_env,
op: |r| self.member_constraint(opaque_type_key, span, concrete_ty, r, &choice_regions),
});
}
Expand Down Expand Up @@ -415,9 +418,11 @@ impl<'tcx> InferCtxt<'tcx> {
}
}

/// Visitor that requires that (almost) all regions in the type visited outlive
/// `least_region`. We cannot use `push_outlives_components` because regions in
/// closure signatures are not included in their outlives components. We need to
/// Visitor that requires that (almost) all regions in the type visited are
/// passed to `OP`.
///
/// We cannot use `push_outlives_components` because regions in closure
/// signatures are not included in their outlives components. We need to
/// ensure all regions outlive the given bound so that we don't end up with,
/// say, `ReVar` appearing in a return type and causing ICEs when other
/// functions end up with region constraints involving regions from other
Expand All @@ -428,12 +433,13 @@ impl<'tcx> InferCtxt<'tcx> {
///
/// We ignore any type parameters because impl trait values are assumed to
/// capture all the in-scope type parameters.
pub struct ConstrainOpaqueTypeRegionVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> {
pub struct VisitFreeRegionsVisitor<'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 ConstrainOpaqueTypeRegionVisitor<'tcx, OP>
impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for VisitFreeRegionsVisitor<'tcx, OP>
where
OP: FnMut(ty::Region<'tcx>),
{
Expand Down Expand Up @@ -484,19 +490,74 @@ where
args.as_generator().resume_ty().visit_with(self);
}

ty::Alias(ty::Opaque, ty::AliasTy { def_id, ref args, .. }) => {
// Skip lifetime parameters that are not captures.
let variances = self.tcx.variances_of(*def_id);

for (v, s) in std::iter::zip(variances, args.iter()) {
if *v != ty::Variance::Bivariant {
s.visit_with(self);
// 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)
.filter_map(|clause| {
if let Some(outlives) = clause.as_type_outlives_clause()
&& outlives.skip_binder().0 == ty
{
Some(outlives.skip_binder().1)
} else {
None
}
})
.chain(param_env.caller_bounds().iter().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)
{
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);
ty.super_visit_with(self)?;
}
}

Expand Down

0 comments on commit 2c82fdc

Please sign in to comment.