make dropck_outlives into a proper canonicalized type query
This commit is contained in:
parent
ed6a7cc228
commit
63217e08cc
1 changed files with 88 additions and 101 deletions
|
@ -2,20 +2,18 @@ use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
|
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
|
||||||
use rustc_infer::infer::TyCtxtInferExt;
|
use rustc_infer::infer::TyCtxtInferExt;
|
||||||
use rustc_infer::traits::TraitEngineExt as _;
|
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::InternalSubsts;
|
use rustc_middle::ty::InternalSubsts;
|
||||||
use rustc_middle::ty::{self, EarlyBinder, ParamEnvAnd, Ty, TyCtxt};
|
use rustc_middle::ty::{self, EarlyBinder, ParamEnvAnd, Ty, TyCtxt};
|
||||||
use rustc_span::source_map::{Span, DUMMY_SP};
|
use rustc_span::source_map::{Span, DUMMY_SP};
|
||||||
|
use rustc_trait_selection::infer::InferCtxtBuilderExt;
|
||||||
use rustc_trait_selection::traits::query::dropck_outlives::trivial_dropck_outlives;
|
use rustc_trait_selection::traits::query::dropck_outlives::trivial_dropck_outlives;
|
||||||
use rustc_trait_selection::traits::query::dropck_outlives::{
|
use rustc_trait_selection::traits::query::dropck_outlives::{
|
||||||
DropckConstraint, DropckOutlivesResult,
|
DropckConstraint, DropckOutlivesResult,
|
||||||
};
|
};
|
||||||
use rustc_trait_selection::traits::query::normalize::AtExt;
|
use rustc_trait_selection::traits::query::normalize::AtExt;
|
||||||
use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution};
|
use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution};
|
||||||
use rustc_trait_selection::traits::{
|
use rustc_trait_selection::traits::{Normalized, ObligationCause};
|
||||||
Normalized, ObligationCause, TraitEngine, TraitEngineExt as _,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) fn provide(p: &mut Providers) {
|
pub(crate) fn provide(p: &mut Providers) {
|
||||||
*p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p };
|
*p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p };
|
||||||
|
@ -27,120 +25,109 @@ fn dropck_outlives<'tcx>(
|
||||||
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> {
|
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> {
|
||||||
debug!("dropck_outlives(goal={:#?})", canonical_goal);
|
debug!("dropck_outlives(goal={:#?})", canonical_goal);
|
||||||
|
|
||||||
let (ref infcx, goal, canonical_inference_vars) =
|
tcx.infer_ctxt().enter_canonical_trait_query(&canonical_goal, |ocx, goal| {
|
||||||
tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &canonical_goal);
|
let tcx = ocx.infcx.tcx;
|
||||||
let tcx = infcx.tcx;
|
let ParamEnvAnd { param_env, value: for_ty } = goal;
|
||||||
let ParamEnvAnd { param_env, value: for_ty } = goal;
|
|
||||||
|
|
||||||
let mut result = DropckOutlivesResult { kinds: vec![], overflows: vec![] };
|
let mut result = DropckOutlivesResult { kinds: vec![], overflows: vec![] };
|
||||||
|
|
||||||
// A stack of types left to process. Each round, we pop
|
// A stack of types left to process. Each round, we pop
|
||||||
// something from the stack and invoke
|
// something from the stack and invoke
|
||||||
// `dtorck_constraint_for_ty`. This may produce new types that
|
// `dtorck_constraint_for_ty`. This may produce new types that
|
||||||
// have to be pushed on the stack. This continues until we have explored
|
// have to be pushed on the stack. This continues until we have explored
|
||||||
// all the reachable types from the type `for_ty`.
|
// all the reachable types from the type `for_ty`.
|
||||||
//
|
//
|
||||||
// Example: Imagine that we have the following code:
|
// Example: Imagine that we have the following code:
|
||||||
//
|
//
|
||||||
// ```rust
|
// ```rust
|
||||||
// struct A {
|
// struct A {
|
||||||
// value: B,
|
// value: B,
|
||||||
// children: Vec<A>,
|
// children: Vec<A>,
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// struct B {
|
// struct B {
|
||||||
// value: u32
|
// value: u32
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// fn f() {
|
// fn f() {
|
||||||
// let a: A = ...;
|
// let a: A = ...;
|
||||||
// ..
|
// ..
|
||||||
// } // here, `a` is dropped
|
// } // here, `a` is dropped
|
||||||
// ```
|
// ```
|
||||||
//
|
//
|
||||||
// at the point where `a` is dropped, we need to figure out
|
// at the point where `a` is dropped, we need to figure out
|
||||||
// which types inside of `a` contain region data that may be
|
// which types inside of `a` contain region data that may be
|
||||||
// accessed by any destructors in `a`. We begin by pushing `A`
|
// accessed by any destructors in `a`. We begin by pushing `A`
|
||||||
// onto the stack, as that is the type of `a`. We will then
|
// onto the stack, as that is the type of `a`. We will then
|
||||||
// invoke `dtorck_constraint_for_ty` which will expand `A`
|
// invoke `dtorck_constraint_for_ty` which will expand `A`
|
||||||
// into the types of its fields `(B, Vec<A>)`. These will get
|
// into the types of its fields `(B, Vec<A>)`. These will get
|
||||||
// pushed onto the stack. Eventually, expanding `Vec<A>` will
|
// pushed onto the stack. Eventually, expanding `Vec<A>` will
|
||||||
// lead to us trying to push `A` a second time -- to prevent
|
// lead to us trying to push `A` a second time -- to prevent
|
||||||
// infinite recursion, we notice that `A` was already pushed
|
// infinite recursion, we notice that `A` was already pushed
|
||||||
// once and stop.
|
// once and stop.
|
||||||
let mut ty_stack = vec![(for_ty, 0)];
|
let mut ty_stack = vec![(for_ty, 0)];
|
||||||
|
|
||||||
// Set used to detect infinite recursion.
|
// Set used to detect infinite recursion.
|
||||||
let mut ty_set = FxHashSet::default();
|
let mut ty_set = FxHashSet::default();
|
||||||
|
|
||||||
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
|
let cause = ObligationCause::dummy();
|
||||||
|
let mut constraints = DropckConstraint::empty();
|
||||||
|
while let Some((ty, depth)) = ty_stack.pop() {
|
||||||
|
debug!(
|
||||||
|
"{} kinds, {} overflows, {} ty_stack",
|
||||||
|
result.kinds.len(),
|
||||||
|
result.overflows.len(),
|
||||||
|
ty_stack.len()
|
||||||
|
);
|
||||||
|
dtorck_constraint_for_ty(tcx, DUMMY_SP, for_ty, depth, ty, &mut constraints)?;
|
||||||
|
|
||||||
let cause = ObligationCause::dummy();
|
// "outlives" represent types/regions that may be touched
|
||||||
let mut constraints = DropckConstraint::empty();
|
// by a destructor.
|
||||||
while let Some((ty, depth)) = ty_stack.pop() {
|
result.kinds.append(&mut constraints.outlives);
|
||||||
debug!(
|
result.overflows.append(&mut constraints.overflows);
|
||||||
"{} kinds, {} overflows, {} ty_stack",
|
|
||||||
result.kinds.len(),
|
|
||||||
result.overflows.len(),
|
|
||||||
ty_stack.len()
|
|
||||||
);
|
|
||||||
dtorck_constraint_for_ty(tcx, DUMMY_SP, for_ty, depth, ty, &mut constraints)?;
|
|
||||||
|
|
||||||
// "outlives" represent types/regions that may be touched
|
// If we have even one overflow, we should stop trying to evaluate further --
|
||||||
// by a destructor.
|
// chances are, the subsequent overflows for this evaluation won't provide useful
|
||||||
result.kinds.append(&mut constraints.outlives);
|
// information and will just decrease the speed at which we can emit these errors
|
||||||
result.overflows.append(&mut constraints.overflows);
|
// (since we'll be printing for just that much longer for the often enormous types
|
||||||
|
// that result here).
|
||||||
|
if !result.overflows.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// If we have even one overflow, we should stop trying to evaluate further --
|
// dtorck types are "types that will get dropped but which
|
||||||
// chances are, the subsequent overflows for this evaluation won't provide useful
|
// do not themselves define a destructor", more or less. We have
|
||||||
// information and will just decrease the speed at which we can emit these errors
|
// to push them onto the stack to be expanded.
|
||||||
// (since we'll be printing for just that much longer for the often enormous types
|
for ty in constraints.dtorck_types.drain(..) {
|
||||||
// that result here).
|
let Normalized { value: ty, obligations } =
|
||||||
if !result.overflows.is_empty() {
|
ocx.infcx.at(&cause, param_env).normalize(ty)?;
|
||||||
break;
|
ocx.register_obligations(obligations);
|
||||||
}
|
|
||||||
|
|
||||||
// dtorck types are "types that will get dropped but which
|
debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
|
||||||
// do not themselves define a destructor", more or less. We have
|
|
||||||
// to push them onto the stack to be expanded.
|
|
||||||
for ty in constraints.dtorck_types.drain(..) {
|
|
||||||
match infcx.at(&cause, param_env).normalize(ty) {
|
|
||||||
Ok(Normalized { value: ty, obligations }) => {
|
|
||||||
fulfill_cx.register_predicate_obligations(infcx, obligations);
|
|
||||||
|
|
||||||
debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
|
match ty.kind() {
|
||||||
|
// All parameters live for the duration of the
|
||||||
|
// function.
|
||||||
|
ty::Param(..) => {}
|
||||||
|
|
||||||
match ty.kind() {
|
// A projection that we couldn't resolve - it
|
||||||
// All parameters live for the duration of the
|
// might have a destructor.
|
||||||
// function.
|
ty::Projection(..) | ty::Opaque(..) => {
|
||||||
ty::Param(..) => {}
|
result.kinds.push(ty.into());
|
||||||
|
}
|
||||||
|
|
||||||
// A projection that we couldn't resolve - it
|
_ => {
|
||||||
// might have a destructor.
|
if ty_set.insert(ty) {
|
||||||
ty::Projection(..) | ty::Opaque(..) => {
|
ty_stack.push((ty, depth + 1));
|
||||||
result.kinds.push(ty.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
if ty_set.insert(ty) {
|
|
||||||
ty_stack.push((ty, depth + 1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't actually expect to fail to normalize.
|
|
||||||
// That implies a WF error somewhere else.
|
|
||||||
Err(NoSolution) => {
|
|
||||||
return Err(NoSolution);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
debug!("dropck_outlives: result = {:#?}", result);
|
debug!("dropck_outlives: result = {:#?}", result);
|
||||||
|
Ok(result)
|
||||||
infcx.make_canonicalized_query_response(canonical_inference_vars, result, &mut *fulfill_cx)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a set of constraints that needs to be satisfied in
|
/// Returns a set of constraints that needs to be satisfied in
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue