Skip to content

[experiment] Consider WF of coroutine witness when proving outlives assumptions #143545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {

#[instrument(skip(self), level = "debug")]
pub(super) fn convert_all(&mut self, query_constraints: &QueryRegionConstraints<'tcx>) {
let QueryRegionConstraints { outlives } = query_constraints;
let QueryRegionConstraints { outlives, assumptions } = query_constraints;

for &(predicate, constraint_category) in outlives {
if assumptions.contains(&predicate) {
continue;
}

self.convert(predicate, constraint_category);
}
}
Expand Down Expand Up @@ -270,7 +274,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
match self.param_env.and(DeeplyNormalize { value: ty }).fully_perform(self.infcx, self.span)
{
Ok(TypeOpOutput { output: ty, constraints, .. }) => {
if let Some(QueryRegionConstraints { outlives }) = constraints {
// FIXME(higher_ranked_auto): WTF
if let Some(QueryRegionConstraints { outlives, assumptions: _ }) = constraints {
next_outlives_predicates.extend(outlives.iter().copied());
}
ty
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ pub(crate) fn type_check<'tcx>(
pre_obligations.is_empty(),
"there should be no incoming region obligations = {pre_obligations:#?}",
);
let pre_assumptions = infcx.take_registered_region_assumptions();
assert!(
pre_assumptions.is_empty(),
"there should be no incoming region assumptions = {pre_assumptions:#?}",
);

debug!(?normalized_inputs_and_output);

Expand Down
20 changes: 19 additions & 1 deletion compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<'tcx> InferCtxt<'tcx> {
}

let region_obligations = self.take_registered_region_obligations();
let region_assumptions = self.take_registered_region_assumptions();
debug!(?region_obligations);
let region_constraints = self.with_region_constraints(|region_constraints| {
make_query_region_constraints(
Expand All @@ -123,6 +124,7 @@ impl<'tcx> InferCtxt<'tcx> {
.iter()
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())),
region_constraints,
region_assumptions,
)
});
debug!(?region_constraints);
Expand Down Expand Up @@ -174,6 +176,11 @@ impl<'tcx> InferCtxt<'tcx> {
self.register_outlives_constraint(predicate, cause);
}

for assumption in &query_response.value.region_constraints.assumptions {
let assumption = instantiate_value(self.tcx, &result_args, *assumption);
self.register_region_assumption(assumption);
}

let user_result: R =
query_response.instantiate_projected(self.tcx, &result_args, |q_r| q_r.value.clone());

Expand Down Expand Up @@ -297,6 +304,16 @@ impl<'tcx> InferCtxt<'tcx> {
}),
);

// FIXME(higher_ranked_auto): Optimize?
output_query_region_constraints.assumptions.extend(
query_response
.value
.region_constraints
.assumptions
.iter()
.map(|&r_c| instantiate_value(self.tcx, &result_args, r_c)),
);

let user_result: R =
query_response.instantiate_projected(self.tcx, &result_args, |q_r| q_r.value.clone());

Expand Down Expand Up @@ -572,6 +589,7 @@ pub fn make_query_region_constraints<'tcx>(
tcx: TyCtxt<'tcx>,
outlives_obligations: impl Iterator<Item = (Ty<'tcx>, ty::Region<'tcx>, ConstraintCategory<'tcx>)>,
region_constraints: &RegionConstraintData<'tcx>,
assumptions: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,
) -> QueryRegionConstraints<'tcx> {
let RegionConstraintData { constraints, verifys } = region_constraints;

Expand Down Expand Up @@ -604,5 +622,5 @@ pub fn make_query_region_constraints<'tcx>(
}))
.collect();

QueryRegionConstraints { outlives }
QueryRegionConstraints { outlives, assumptions }
}
11 changes: 10 additions & 1 deletion compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub struct InferCtxtInner<'tcx> {
/// that all type inference variables have been bound and so forth.
region_obligations: Vec<TypeOutlivesConstraint<'tcx>>,

/// UwU
region_assumptions: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,

/// Caches for opaque type inference.
opaque_type_storage: OpaqueTypeStorage<'tcx>,
}
Expand All @@ -164,7 +167,8 @@ impl<'tcx> InferCtxtInner<'tcx> {
int_unification_storage: Default::default(),
float_unification_storage: Default::default(),
region_constraint_storage: Some(Default::default()),
region_obligations: vec![],
region_obligations: Default::default(),
region_assumptions: Default::default(),
opaque_type_storage: Default::default(),
}
}
Expand All @@ -174,6 +178,11 @@ impl<'tcx> InferCtxtInner<'tcx> {
&self.region_obligations
}

#[inline]
pub fn region_assumptions(&self) -> &[ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>] {
&self.region_assumptions
}

#[inline]
pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
self.projection_cache.with_log(&mut self.undo_log)
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_infer/src/infer/outlives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'tcx> InferCtxt<'tcx> {
assert!(
self.tainted_by_errors().is_some() || inner.region_obligations.is_empty(),
"region_obligations not empty: {:#?}",
inner.region_obligations
inner.region_obligations,
);
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
inner.region_constraint_storage.take().expect("regions already resolved")
Expand Down Expand Up @@ -93,6 +93,11 @@ impl<'tcx> InferCtxt<'tcx> {
"region_obligations not empty: {:#?}",
self.inner.borrow().region_obligations
);
assert!(
self.inner.borrow().region_assumptions.is_empty(),
"region_assumptions not empty: {:#?}",
self.inner.borrow().region_assumptions
);

self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
}
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_infer/src/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ impl<'tcx> InferCtxt<'tcx> {
std::mem::take(&mut self.inner.borrow_mut().region_obligations)
}

pub fn register_region_assumption(
&self,
assumption: ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
) {
let mut inner = self.inner.borrow_mut();
inner.undo_log.push(UndoLog::PushRegionAssumption);
inner.region_assumptions.push(assumption);
}

pub fn take_registered_region_assumptions(
&self,
) -> Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> {
std::mem::take(&mut self.inner.borrow_mut().region_assumptions)
}

/// Process the region obligations that must be proven (during
/// `regionck`) for the given `body_id`, given information about
/// the region bounds in scope and so forth.
Expand All @@ -189,6 +204,7 @@ impl<'tcx> InferCtxt<'tcx> {
-> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
) -> Result<(), (PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>)> {
assert!(!self.in_snapshot(), "cannot process registered region obligations in a snapshot");
let assumptions = self.take_registered_region_assumptions();

// Must loop since the process of normalizing may itself register region obligations.
for iteration in 0.. {
Expand All @@ -207,6 +223,10 @@ impl<'tcx> InferCtxt<'tcx> {
}

for TypeOutlivesConstraint { sup_type, sub_region, origin } in my_region_obligations {
if assumptions.contains(&ty::OutlivesPredicate(sup_type.into(), sub_region)) {
continue;
}

let outlives = ty::Binder::dummy(ty::OutlivesPredicate(sup_type, sub_region));
let ty::OutlivesPredicate(sup_type, sub_region) =
deeply_normalize_ty(outlives, origin.clone())
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/infer/snapshot/undo_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) enum UndoLog<'tcx> {
RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
ProjectionCache(traits::UndoLog<'tcx>),
PushTypeOutlivesConstraint,
PushRegionAssumption,
}

macro_rules! impl_from {
Expand Down Expand Up @@ -75,6 +76,9 @@ impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
UndoLog::PushTypeOutlivesConstraint => {
self.region_obligations.pop();
}
UndoLog::PushRegionAssumption => {
self.region_assumptions.pop();
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_middle/src/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ pub struct QueryResponse<'tcx, R> {
#[derive(HashStable, TypeFoldable, TypeVisitable)]
pub struct QueryRegionConstraints<'tcx> {
pub outlives: Vec<QueryOutlivesConstraint<'tcx>>,
pub assumptions: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,
}

impl QueryRegionConstraints<'_> {
/// Represents an empty (trivially true) set of region
/// constraints.
/// Represents an empty (trivially true) set of region constraints.
// FIXME(higher_ranked_auto): This could still just be true if there are only assumptions?
pub fn is_empty(&self) -> bool {
self.outlives.is_empty()
self.outlives.is_empty() && self.assumptions.is_empty()
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ rustc_queries! {
}

query coroutine_hidden_types(
def_id: DefId
def_id: DefId,
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
desc { "looking up the hidden types stored across await points in a coroutine" }
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type BoundRegion = ty::BoundRegion;
type PlaceholderRegion = ty::PlaceholderRegion;

type RegionAssumptions = &'tcx ty::List<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>;

type ParamEnv = ty::ParamEnv<'tcx>;
type Predicate = Predicate<'tcx>;

Expand Down Expand Up @@ -874,6 +876,7 @@ pub struct CtxtInterners<'tcx> {
offset_of: InternedSet<'tcx, List<(VariantIdx, FieldIdx)>>,
valtree: InternedSet<'tcx, ty::ValTreeKind<'tcx>>,
patterns: InternedSet<'tcx, List<ty::Pattern<'tcx>>>,
outlives: InternedSet<'tcx, List<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>>,
}

impl<'tcx> CtxtInterners<'tcx> {
Expand Down Expand Up @@ -911,6 +914,7 @@ impl<'tcx> CtxtInterners<'tcx> {
offset_of: InternedSet::with_capacity(N),
valtree: InternedSet::with_capacity(N),
patterns: InternedSet::with_capacity(N),
outlives: InternedSet::with_capacity(N),
}
}

Expand Down Expand Up @@ -2692,6 +2696,7 @@ slice_interners!(
captures: intern_captures(&'tcx ty::CapturedPlace<'tcx>),
offset_of: pub mk_offset_of((VariantIdx, FieldIdx)),
patterns: pub mk_patterns(Pattern<'tcx>),
outlives: pub mk_outlives(ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>),
);

impl<'tcx> TyCtxt<'tcx> {
Expand Down Expand Up @@ -3107,6 +3112,17 @@ impl<'tcx> TyCtxt<'tcx> {
T::collect_and_apply(iter, |xs| self.mk_bound_variable_kinds(xs))
}

pub fn mk_outlives_from_iter<I, T>(self, iter: I) -> T::Output
where
I: Iterator<Item = T>,
T: CollectAndApply<
ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
&'tcx ty::List<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,
>,
{
T::collect_and_apply(iter, |xs| self.mk_outlives(xs))
}

/// Emit a lint at `span` from a lint struct (some type that implements `LintDiagnostic`,
/// typically generated by `#[derive(LintDiagnostic)]`).
#[track_caller]
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,5 @@ list_fold! {
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> : mk_poly_existential_predicates,
&'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems,
&'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns,
&'tcx ty::List<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> : mk_outlives,
}
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/src/solve/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,15 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
// Cannot use `take_registered_region_obligations` as we may compute the response
// inside of a `probe` whenever we have multiple choices inside of the solver.
let region_obligations = self.0.inner.borrow().region_obligations().to_owned();
let region_assumptions = self.0.inner.borrow().region_assumptions().to_owned();
let region_constraints = self.0.with_region_constraints(|region_constraints| {
make_query_region_constraints(
self.tcx,
region_obligations
.iter()
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())),
region_constraints,
region_assumptions,
)
});

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ fn impl_intersection_has_negative_obligation(
// requirements, when proving the negated where clauses below.
drop(equate_obligations);
drop(infcx.take_registered_region_obligations());
drop(infcx.take_registered_region_assumptions());
drop(infcx.take_and_reset_region_constraints());

plug_infer_with_placeholders(
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ fn implied_outlives_bounds<'a, 'tcx>(
bounds.retain(|bound| !bound.has_placeholders());

if !constraints.is_empty() {
let QueryRegionConstraints { outlives } = constraints;
// FIXME(higher_ranked_auto): ?
let QueryRegionConstraints { outlives, assumptions: _ } = constraints;
let cause = ObligationCause::misc(span, body_id);
for &(predicate, _) in &outlives {
infcx.register_outlives_constraint(predicate, &cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ where
pre_obligations.is_empty(),
"scrape_region_constraints: incoming region obligations = {pre_obligations:#?}",
);
let pre_assumptions = infcx.take_registered_region_assumptions();
assert!(
pre_assumptions.is_empty(),
"scrape_region_constraints: incoming region assumptions = {pre_assumptions:#?}",
);

let value = infcx.commit_if_ok(|_| {
let ocx = ObligationCtxt::new(infcx);
Expand All @@ -100,6 +105,7 @@ where
let value = infcx.resolve_vars_if_possible(value);

let region_obligations = infcx.take_registered_region_obligations();
let region_assumptions = infcx.take_registered_region_assumptions();
let region_constraint_data = infcx.take_and_reset_region_constraints();
let region_constraints = query_response::make_query_region_constraints(
infcx.tcx,
Expand All @@ -108,6 +114,7 @@ where
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category()))
.map(|(ty, r, cc)| (infcx.resolve_vars_if_possible(ty), r, cc)),
&region_constraint_data,
region_assumptions,
);

if region_constraints.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ where
span,
)?;
output.error_info = error_info;
if let Some(QueryRegionConstraints { outlives }) = output.constraints {
if let Some(QueryRegionConstraints { outlives, assumptions }) = output.constraints {
region_constraints.outlives.extend(outlives.iter().cloned());
region_constraints.assumptions.extend(assumptions.iter().cloned());
}
output.constraints = if region_constraints.is_empty() {
None
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

let self_ty =
obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
let self_ty = self.infcx.enter_forall_and_leak_universe(self_ty);

let types = self.constituent_types_for_ty(self_ty)?;
let types = self.infcx.enter_forall_and_leak_universe(types);
let constituents = self.constituent_types_for_auto_trait(self_ty)?;
let constituents = self.infcx.enter_forall_and_leak_universe(constituents);

for assumption in constituents.assumptions {
self.infcx.register_region_assumption(assumption);
}

let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
let obligations = self.collect_predicates_for_types(
obligation.param_env,
cause,
obligation.recursion_depth + 1,
obligation.predicate.def_id(),
types,
constituents.types,
);

Ok(obligations)
Expand Down
Loading
Loading