2018-02-25 10:58:54 -05:00
|
|
|
//! Code for the 'normalization' query. This consists of a wrapper
|
|
|
|
//! which folds deeply, invoking the underlying
|
|
|
|
//! `normalize_projection_ty` query when it encounters projections.
|
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::infer::at::At;
|
|
|
|
use crate::infer::canonical::OriginalQueryValues;
|
|
|
|
use crate::infer::{InferCtxt, InferOk};
|
|
|
|
use crate::traits::project::Normalized;
|
|
|
|
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
|
|
|
|
use crate::ty::fold::{TypeFoldable, TypeFolder};
|
2019-08-23 00:48:52 +02:00
|
|
|
use crate::ty::subst::Subst;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ty::{self, Ty, TyCtxt};
|
2018-02-25 10:58:54 -05:00
|
|
|
|
|
|
|
use super::NoSolution;
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> At<'cx, 'tcx> {
|
2018-02-25 10:58:54 -05:00
|
|
|
/// Normalize `value` in the context of the inference context,
|
|
|
|
/// yielding a resulting type, or an error if `value` cannot be
|
|
|
|
/// normalized. If you don't care about regions, you should prefer
|
|
|
|
/// `normalize_erasing_regions`, which is more efficient.
|
|
|
|
///
|
2018-06-02 00:20:00 -07:00
|
|
|
/// If the normalization succeeds and is unambiguous, returns back
|
2018-02-25 10:58:54 -05:00
|
|
|
/// the normalized value along with various outlives relations (in
|
|
|
|
/// the form of obligations that must be discharged).
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// N.B., this will *eventually* be the main means of
|
2018-02-25 10:58:54 -05:00
|
|
|
/// normalizing, but for now should be used only when we actually
|
|
|
|
/// know that normalization will succeed, since error reporting
|
|
|
|
/// and other details are still "under development".
|
|
|
|
pub fn normalize<T>(&self, value: &T) -> Result<Normalized<'tcx, T>, NoSolution>
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2018-02-26 11:14:16 -05:00
|
|
|
debug!(
|
|
|
|
"normalize::<{}>(value={:?}, param_env={:?})",
|
2019-04-17 19:38:17 -07:00
|
|
|
::std::any::type_name::<T>(),
|
2018-02-26 11:14:16 -05:00
|
|
|
value,
|
|
|
|
self.param_env,
|
|
|
|
);
|
2018-09-12 16:57:19 +02:00
|
|
|
if !value.has_projections() {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(Normalized { value: value.clone(), obligations: vec![] });
|
2018-09-12 16:57:19 +02:00
|
|
|
}
|
|
|
|
|
2018-02-25 10:58:54 -05:00
|
|
|
let mut normalizer = QueryNormalizer {
|
|
|
|
infcx: self.infcx,
|
|
|
|
cause: self.cause,
|
|
|
|
param_env: self.param_env,
|
|
|
|
obligations: vec![],
|
|
|
|
error: false,
|
|
|
|
anon_depth: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let value1 = value.fold_with(&mut normalizer);
|
|
|
|
if normalizer.error {
|
|
|
|
Err(NoSolution)
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(Normalized { value: value1, obligations: normalizer.obligations })
|
2018-02-25 10:58:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Result from the `normalize_projection_ty` query.
|
2019-11-09 23:17:42 +01:00
|
|
|
#[derive(Clone, Debug, HashStable, TypeFoldable, Lift)]
|
2018-02-25 10:58:54 -05:00
|
|
|
pub struct NormalizationResult<'tcx> {
|
|
|
|
/// Result of normalization.
|
|
|
|
pub normalized_ty: Ty<'tcx>,
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct QueryNormalizer<'cx, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
infcx: &'cx InferCtxt<'cx, 'tcx>,
|
2018-02-25 10:58:54 -05:00
|
|
|
cause: &'cx ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
obligations: Vec<PredicateObligation<'tcx>>,
|
|
|
|
error: bool,
|
|
|
|
anon_depth: usize,
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
|
|
|
|
fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
|
2018-02-25 10:58:54 -05:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2020-01-28 00:52:21 +01:00
|
|
|
if !ty.has_projections() {
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2018-02-25 10:58:54 -05:00
|
|
|
let ty = ty.super_fold_with(self);
|
2019-09-16 19:08:35 +01:00
|
|
|
match ty.kind {
|
2018-10-22 22:38:51 +02:00
|
|
|
ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
|
2018-02-25 10:58:54 -05:00
|
|
|
// (*)
|
2018-05-08 16:10:16 +03:00
|
|
|
// Only normalize `impl Trait` after type-checking, usually in codegen.
|
2018-02-25 10:58:54 -05:00
|
|
|
match self.param_env.reveal {
|
|
|
|
Reveal::UserFacing => ty,
|
|
|
|
|
|
|
|
Reveal::All => {
|
2018-04-01 08:15:59 +02:00
|
|
|
let recursion_limit = *self.tcx().sess.recursion_limit.get();
|
2018-02-25 10:58:54 -05:00
|
|
|
if self.anon_depth >= recursion_limit {
|
|
|
|
let obligation = Obligation::with_depth(
|
|
|
|
self.cause.clone(),
|
|
|
|
recursion_limit,
|
|
|
|
self.param_env,
|
|
|
|
ty,
|
|
|
|
);
|
|
|
|
self.infcx.report_overflow_error(&obligation, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let generic_ty = self.tcx().type_of(def_id);
|
|
|
|
let concrete_ty = generic_ty.subst(self.tcx(), substs);
|
|
|
|
self.anon_depth += 1;
|
2018-05-22 14:31:56 +02:00
|
|
|
if concrete_ty == ty {
|
2018-08-22 00:24:03 -04:00
|
|
|
bug!(
|
|
|
|
"infinite recursion generic_ty: {:#?}, substs: {:#?}, \
|
|
|
|
concrete_ty: {:#?}, ty: {:#?}",
|
|
|
|
generic_ty,
|
|
|
|
substs,
|
|
|
|
concrete_ty,
|
|
|
|
ty
|
|
|
|
);
|
2018-05-22 14:31:56 +02:00
|
|
|
}
|
2018-02-25 10:58:54 -05:00
|
|
|
let folded_ty = self.fold_ty(concrete_ty);
|
|
|
|
self.anon_depth -= 1;
|
|
|
|
folded_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 22:38:51 +02:00
|
|
|
ty::Projection(ref data) if !data.has_escaping_bound_vars() => {
|
2018-02-25 10:58:54 -05:00
|
|
|
// (*)
|
|
|
|
// (*) This is kind of hacky -- we need to be able to
|
|
|
|
// handle normalization within binders because
|
|
|
|
// otherwise we wind up a need to normalize when doing
|
|
|
|
// trait matching (since you can have a trait
|
|
|
|
// obligation like `for<'a> T::B : Fn(&'a int)`), but
|
|
|
|
// we can't normalize with bound regions in scope. So
|
|
|
|
// far now we just ignore binders but only normalize
|
|
|
|
// if all bound regions are gone (and then we still
|
|
|
|
// have to renormalize whenever we instantiate a
|
|
|
|
// binder). It would be better to normalize in a
|
|
|
|
// binding-aware fashion.
|
|
|
|
|
2019-09-25 15:36:14 -04:00
|
|
|
let tcx = self.infcx.tcx;
|
2018-02-25 10:58:54 -05:00
|
|
|
|
2018-09-26 15:49:41 -04:00
|
|
|
let mut orig_values = OriginalQueryValues::default();
|
2019-06-03 15:04:40 +01:00
|
|
|
// HACK(matthewjasper) `'static` is special-cased in selection,
|
|
|
|
// so we cannot canonicalize it.
|
2019-12-22 17:42:04 -05:00
|
|
|
let c_data = self
|
|
|
|
.infcx
|
|
|
|
.canonicalize_hr_query_hack(&self.param_env.and(*data), &mut orig_values);
|
2018-02-25 10:58:54 -05:00
|
|
|
debug!("QueryNormalizer: c_data = {:#?}", c_data);
|
|
|
|
debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
|
2019-09-25 15:36:14 -04:00
|
|
|
match tcx.normalize_projection_ty(c_data) {
|
2018-02-25 10:58:54 -05:00
|
|
|
Ok(result) => {
|
|
|
|
// We don't expect ambiguity.
|
|
|
|
if result.is_ambiguous() {
|
|
|
|
self.error = true;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:27:47 -04:00
|
|
|
match self.infcx.instantiate_query_response_and_region_obligations(
|
2018-02-25 10:58:54 -05:00
|
|
|
self.cause,
|
|
|
|
self.param_env,
|
|
|
|
&orig_values,
|
2019-12-22 17:42:04 -05:00
|
|
|
&result,
|
|
|
|
) {
|
2018-09-12 16:57:19 +02:00
|
|
|
Ok(InferOk { value: result, obligations }) => {
|
2018-02-25 10:58:54 -05:00
|
|
|
debug!("QueryNormalizer: result = {:#?}", result);
|
|
|
|
debug!("QueryNormalizer: obligations = {:#?}", obligations);
|
|
|
|
self.obligations.extend(obligations);
|
|
|
|
return result.normalized_ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(_) => {
|
|
|
|
self.error = true;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(NoSolution) => {
|
|
|
|
self.error = true;
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
|
2019-08-23 00:48:52 +02:00
|
|
|
constant.eval(self.infcx.tcx, self.param_env)
|
2018-02-25 10:58:54 -05:00
|
|
|
}
|
|
|
|
}
|