Skip to content
Draft
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 @@ -74,7 +74,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
let assumptions =
elaborate::elaborate_outlives_assumptions(self.infcx.tcx, assumptions.iter().copied());

for &(constraint, constraint_category) in constraints {
for &(constraint, constraint_category, _) in constraints {
constraint.iter_outlives().for_each(|predicate| {
self.convert(predicate, constraint_category, &assumptions);
});
Expand Down Expand Up @@ -205,6 +205,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
t1,
r2,
constraint_category,
ty::VisibleForLeakCheck::Yes,
);
}

Expand Down Expand Up @@ -296,7 +297,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
// FIXME(higher_ranked_auto): What should we do with the assumptions here?
if let Some(QueryRegionConstraints { constraints, assumptions: _ }) = constraints {
next_outlives_predicates.extend(constraints.iter().flat_map(
|(constraint, category)| {
|(constraint, category, _)| {
constraint.iter_outlives().map(|outlives| (outlives, *category))
},
));
Expand All @@ -315,6 +316,7 @@ impl<'a, 'b, 'tcx> TypeOutlivesDelegate<'tcx> for &'a mut ConstraintConversion<'
a: ty::Region<'tcx>,
b: ty::Region<'tcx>,
constraint_category: ConstraintCategory<'tcx>,
_vis: ty::VisibleForLeakCheck,
) {
let b = self.to_region_vid(b);
let a = self.to_region_vid(a);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ fn ty_known_to_outlive<'tcx>(
sub_region: region,
sup_type: ty,
origin: SubregionOrigin::RelateParamBound(DUMMY_SP, ty, None),
visible_for_leak_check: ty::VisibleForLeakCheck::Yes,
});
})
}
Expand All @@ -728,6 +729,7 @@ fn region_known_to_outlive<'tcx>(
SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
region_b,
region_a,
ty::VisibleForLeakCheck::Yes,
);
})
}
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,12 @@ pub(crate) fn coerce_unsized_info<'tcx>(
}

(&ty::Ref(r_a, ty_a, mutbl_a), &ty::Ref(r_b, ty_b, mutbl_b)) => {
infcx.sub_regions(SubregionOrigin::RelateObjectBound(span), r_b, r_a);
infcx.sub_regions(
SubregionOrigin::RelateObjectBound(span),
r_b,
r_a,
ty::VisibleForLeakCheck::Yes,
);
let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
let mt_b = ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b };
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ref(tcx, r_b, ty))
Expand Down
30 changes: 17 additions & 13 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ impl CanonicalizeMode for CanonicalizeUserTypeAnnotation {
| ty::ReErased
| ty::ReStatic
| ty::ReError(_) => r,
ty::ReVar(_) => canonicalizer.canonical_var_for_region_in_root_universe(r),
ty::ReVar(_) => canonicalizer
.canonical_var_for_region(CanonicalVarKind::Region(ty::UniverseIndex::ROOT), r),
ty::RePlaceholder(..) | ty::ReBound(..) => {
// We only expect region names that the user can type.
bug!("unexpected region in query response: `{:?}`", r)
Expand Down Expand Up @@ -706,23 +707,26 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
}

/// Shorthand helper that creates a canonical region variable for
/// `r` (always in the root universe). The reason that we always
/// put these variables into the root universe is because this
/// method is used during **query construction:** in that case, we
/// are taking all the regions and just putting them into the most
/// generic context we can. This may generate solutions that don't
/// fit (e.g., that equate some region variable with a placeholder
/// it can't name) on the caller side, but that's ok, the caller
/// can figure that out. In the meantime, it maximizes our
/// `r` (always as a placeholder in the root universe). The reason
/// that we always put these variables into the root universe as a
/// placeholder is because this method is used during
/// **query construction:** in that case, we are taking all the
/// free regions and just putting them into the most generic context
/// we can. This makes any region constraints between them, including
/// region equalities, to be preserved and propagated to the caller
/// instead of unifying them. In the meantime, it maximizes our
/// caching.
///
/// (This works because unification never fails -- and hence trait
/// selection is never affected -- due to a universe mismatch.)
fn canonical_var_for_region_in_root_universe(
&mut self,
r: ty::Region<'tcx>,
) -> ty::Region<'tcx> {
self.canonical_var_for_region(CanonicalVarKind::Region(ty::UniverseIndex::ROOT), r)
self.canonical_var_for_region(
CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
ty::UniverseIndex::ROOT,
self.var_kinds.len().into(),
)),
r,
)
}

/// Creates a canonical variable (with the given `info`)
Expand Down
27 changes: 16 additions & 11 deletions compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ impl<'tcx> InferCtxt<'tcx> {
let InferOk { value: result_args, obligations } =
self.query_response_instantiation(cause, param_env, original_values, query_response)?;

for (constraint, _category) in &query_response.value.region_constraints.constraints {
for (constraint, _category, vis) in &query_response.value.region_constraints.constraints {
let constraint = instantiate_value(self.tcx, &result_args, *constraint);
match constraint {
ty::RegionConstraint::Outlives(predicate) => {
self.register_outlives_constraint(predicate, cause);
self.register_outlives_constraint(predicate, *vis, cause);
}
ty::RegionConstraint::Eq(predicate) => {
self.register_region_eq_constraint(predicate, cause);
self.register_region_eq_constraint(predicate, *vis, cause);
}
}
}
Expand Down Expand Up @@ -288,6 +288,7 @@ impl<'tcx> InferCtxt<'tcx> {
output_query_region_constraints.constraints.push((
ty::RegionEqPredicate(v_o.into(), v_r).into(),
constraint_category,
ty::VisibleForLeakCheck::Yes,
));
}
}
Expand Down Expand Up @@ -586,6 +587,7 @@ impl<'tcx> InferCtxt<'tcx> {
SubregionOrigin::RelateRegionParamBound(cause.span, None),
v1,
v2,
ty::VisibleForLeakCheck::Yes,
);
}
(GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
Expand Down Expand Up @@ -623,20 +625,23 @@ pub fn make_query_region_constraints<'tcx>(
| ConstraintKind::RegSubReg => {
// Swap regions because we are going from sub (<=) to outlives (>=).
let constraint = ty::OutlivesPredicate(c.sup.into(), c.sub).into();
(constraint, origin.to_constraint_category())
(constraint, origin.to_constraint_category(), c.visible_for_leak_check)
}

ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
let constraint = ty::RegionEqPredicate(c.sup, c.sub).into();
(constraint, origin.to_constraint_category())
(constraint, origin.to_constraint_category(), c.visible_for_leak_check)
}
})
.chain(outlives_obligations.into_iter().map(|obl| {
(
ty::OutlivesPredicate(obl.sup_type.into(), obl.sub_region).into(),
obl.origin.to_constraint_category(),
)
}))
.chain(outlives_obligations.into_iter().map(
|TypeOutlivesConstraint { sub_region, sup_type, origin, visible_for_leak_check }| {
(
ty::OutlivesPredicate(sup_type.into(), sub_region).into(),
origin.to_constraint_category(),
visible_for_leak_check,
)
},
))
.collect();

QueryRegionConstraints { constraints, assumptions }
Expand Down
28 changes: 24 additions & 4 deletions compiler/rustc_infer/src/infer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,44 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
self.probe(|_| probe())
}

fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, span: Span) {
fn sub_regions(
&self,
sub: ty::Region<'tcx>,
sup: ty::Region<'tcx>,
vis: ty::VisibleForLeakCheck,
span: Span,
) {
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
SubregionOrigin::RelateRegionParamBound(span, None),
sub,
sup,
vis,
);
}

fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>, span: Span) {
fn equate_regions(
&self,
a: ty::Region<'tcx>,
b: ty::Region<'tcx>,
vis: ty::VisibleForLeakCheck,
span: Span,
) {
self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
SubregionOrigin::RelateRegionParamBound(span, None),
a,
b,
vis,
);
}

fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
self.register_type_outlives_constraint(ty, r, &ObligationCause::dummy_with_span(span));
fn register_ty_outlives(
&self,
ty: Ty<'tcx>,
r: ty::Region<'tcx>,
vis: ty::VisibleForLeakCheck,
span: Span,
) {
self.register_type_outlives_constraint(ty, r, vis, &ObligationCause::dummy_with_span(span));
}

type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries;
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ pub struct TypeOutlivesConstraint<'tcx> {
pub sub_region: ty::Region<'tcx>,
pub sup_type: Ty<'tcx>,
pub origin: SubregionOrigin<'tcx>,
pub visible_for_leak_check: ty::VisibleForLeakCheck,
}

/// Used to configure inference contexts before their creation.
Expand Down Expand Up @@ -698,8 +699,9 @@ impl<'tcx> InferCtxt<'tcx> {
origin: SubregionOrigin<'tcx>,
a: ty::Region<'tcx>,
b: ty::Region<'tcx>,
vis: ty::VisibleForLeakCheck,
) {
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b, vis);
}

#[instrument(skip(self), level = "debug")]
Expand All @@ -708,8 +710,9 @@ impl<'tcx> InferCtxt<'tcx> {
origin: SubregionOrigin<'tcx>,
a: ty::Region<'tcx>,
b: ty::Region<'tcx>,
vis: ty::VisibleForLeakCheck,
) {
self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(origin, a, b);
self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(origin, a, b, vis);
}

/// Processes a `Coerce` predicate from the fulfillment context.
Expand Down
Loading
Loading