Skip to content
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

Account for opaque variance for region outlives and liveness. #106729

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![allow(rustc::potential_query_instability)]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(let_chains)]
#![feature(min_specialization)]
#![feature(never_type)]
Expand Down
69 changes: 58 additions & 11 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use rustc_index::bit_set::HybridBitSet;
use rustc_index::interval::IntervalSet;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, Local, Location};
use rustc_middle::ty::{Ty, TypeVisitable};
use rustc_middle::ty::{self, Ty, TypeSuperVisitable, TypeVisitable, TypeVisitor};
use rustc_trait_selection::traits::query::dropck_outlives::DropckOutlivesResult;
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 @@ -551,16 +552,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
values::location_set_str(elements, live_at.iter()),
);

let tcx = typeck.tcx();
tcx.for_each_free_region(&value, |live_region| {
let live_region_vid =
typeck.borrowck_context.universal_regions.to_region_vid(live_region);
typeck
.borrowck_context
.constraints
.liveness_constraints
.add_elements(live_region_vid, live_at);
});
value.visit_with(&mut LivenessMarker { live_at, typeck, outer_index: ty::INNERMOST });
}

fn compute_drop_data(
Expand All @@ -576,3 +568,58 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
DropData { dropck_result: output, region_constraint_data: constraints }
}
}

struct LivenessMarker<'tcx, 'a, 'b, 'c> {
live_at: &'a IntervalSet<PointIndex>,
typeck: &'b mut TypeChecker<'c, 'tcx>,
outer_index: ty::DebruijnIndex,
}

impl<'tcx> TypeVisitor<'tcx> for LivenessMarker<'tcx, '_, '_, '_> {
type BreakTy = !;

fn visit_binder<T: TypeVisitable<'tcx>>(
&mut self,
t: &ty::Binder<'tcx, T>,
) -> ControlFlow<Self::BreakTy> {
self.outer_index.shift_in(1);
let result = t.super_visit_with(self);
self.outer_index.shift_out(1);
result
}

fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
match *r {
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => ControlFlow::CONTINUE,
_ => {
let live_region_vid =
self.typeck.borrowck_context.universal_regions.to_region_vid(r);
self.typeck
.borrowck_context
.constraints
.liveness_constraints
.add_elements(live_region_vid, self.live_at);
ControlFlow::CONTINUE
}
}
}

fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
// We're only interested in types involving regions
if ty.has_free_regions() {
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = ty.kind() {
let variances = self.typeck.tcx().variances_of(def_id);
for (&v, s) in std::iter::zip(variances, *substs) {
if v != ty::Bivariant {
s.visit_with(self);
}
}
ControlFlow::CONTINUE
} else {
ty.super_visit_with(self)
}
} else {
ControlFlow::CONTINUE
}
}
}
26 changes: 18 additions & 8 deletions compiler/rustc_infer/src/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ where
if approx_env_bounds.is_empty() && trait_bounds.is_empty() && (needs_infer || is_opaque) {
debug!("no declared bounds");

self.substs_must_outlive(substs, origin, region);
let opt_variances = is_opaque.then(|| self.tcx.variances_of(def_id));
self.substs_must_outlive(substs, origin, region, opt_variances);

return;
}
Expand Down Expand Up @@ -498,22 +499,31 @@ where
self.delegate.push_verify(origin, generic, region, verify_bound);
}

#[instrument(level = "debug", skip(self))]
fn substs_must_outlive(
&mut self,
substs: SubstsRef<'tcx>,
origin: infer::SubregionOrigin<'tcx>,
region: ty::Region<'tcx>,
opt_variances: Option<&[ty::Variance]>,
) {
let constraint = origin.to_constraint_category();
for k in substs {
for (index, k) in substs.iter().enumerate() {
match k.unpack() {
GenericArgKind::Lifetime(lt) => {
self.delegate.push_sub_region_constraint(
origin.clone(),
region,
lt,
constraint,
);
let variance = if let Some(variances) = opt_variances {
variances[index]
} else {
ty::Invariant
};
Comment on lines +514 to +518
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that we only need to do this for lifetimes and not type substs, because this is only used for opaque types, which only generate interesting variance for lifetimes, not type parameters

if variance == ty::Invariant {
self.delegate.push_sub_region_constraint(
origin.clone(),
region,
lt,
constraint,
);
}
}
GenericArgKind::Type(ty) => {
self.type_must_outlive(origin.clone(), ty, region, constraint);
Expand Down