Skip to content

Commit 3937a5e

Browse files
committed
transition various normalization functions to the new methods
In particular: - `fully_normalize_monormophic_ty` => `normalize_erasing_regions` - `normalize_associated_type_in_env` => `normalize_erasing_regions` - `fully_normalize_associated_types_in` => `normalize_erasing_regions` - `erase_late_bound_regions_and_normalize` => `normalize_erasing_late_bound_regions`
1 parent 55fc165 commit 3937a5e

File tree

28 files changed

+97
-281
lines changed

28 files changed

+97
-281
lines changed

src/librustc/infer/mod.rs

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use hir::def_id::DefId;
2121
use middle::free_region::RegionRelations;
2222
use middle::region;
2323
use middle::lang_items;
24-
use mir::tcx::PlaceTy;
2524
use ty::subst::{Kind, Subst, Substs};
2625
use ty::{TyVid, IntVid, FloatVid};
2726
use ty::{self, Ty, TyCtxt};
@@ -35,7 +34,7 @@ use std::collections::BTreeMap;
3534
use std::fmt;
3635
use syntax::ast;
3736
use errors::DiagnosticBuilder;
38-
use syntax_pos::{self, Span, DUMMY_SP};
37+
use syntax_pos::{self, Span};
3938
use util::nodemap::FxHashMap;
4039
use arena::DroplessArena;
4140

@@ -493,140 +492,7 @@ pub struct CombinedSnapshot<'a, 'tcx:'a> {
493492
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
494493
}
495494

496-
/// Helper trait for shortening the lifetimes inside a
497-
/// value for post-type-checking normalization.
498-
///
499-
/// This trait offers a normalization method where the inputs and
500-
/// outputs both have the `'gcx` lifetime; the implementations
501-
/// internally create inference contexts and/or lift as needed.
502-
pub trait TransNormalize<'gcx>: TypeFoldable<'gcx> {
503-
fn trans_normalize<'a, 'tcx>(&self,
504-
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
505-
param_env: ty::ParamEnv<'tcx>)
506-
-> Self;
507-
}
508-
509-
macro_rules! items { ($($item:item)+) => ($($item)+) }
510-
macro_rules! impl_trans_normalize {
511-
($lt_gcx:tt, $($ty:ty),+) => {
512-
items!($(impl<$lt_gcx> TransNormalize<$lt_gcx> for $ty {
513-
fn trans_normalize<'a, 'tcx>(&self,
514-
infcx: &InferCtxt<'a, $lt_gcx, 'tcx>,
515-
param_env: ty::ParamEnv<'tcx>)
516-
-> Self {
517-
infcx.normalize_projections_in(param_env, self)
518-
}
519-
})+);
520-
}
521-
}
522-
523-
impl_trans_normalize!('gcx,
524-
Ty<'gcx>,
525-
&'gcx ty::Const<'gcx>,
526-
&'gcx Substs<'gcx>,
527-
ty::FnSig<'gcx>,
528-
ty::PolyFnSig<'gcx>,
529-
ty::ClosureSubsts<'gcx>,
530-
ty::PolyTraitRef<'gcx>,
531-
ty::ExistentialTraitRef<'gcx>
532-
);
533-
534-
impl<'gcx> TransNormalize<'gcx> for PlaceTy<'gcx> {
535-
fn trans_normalize<'a, 'tcx>(&self,
536-
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
537-
param_env: ty::ParamEnv<'tcx>)
538-
-> Self {
539-
match *self {
540-
PlaceTy::Ty { ty } => PlaceTy::Ty { ty: ty.trans_normalize(infcx, param_env) },
541-
PlaceTy::Downcast { adt_def, substs, variant_index } => {
542-
PlaceTy::Downcast {
543-
adt_def,
544-
substs: substs.trans_normalize(infcx, param_env),
545-
variant_index,
546-
}
547-
}
548-
}
549-
}
550-
}
551-
552-
// NOTE: Callable from trans only!
553-
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
554-
/// Currently, higher-ranked type bounds inhibit normalization. Therefore,
555-
/// each time we erase them in translation, we need to normalize
556-
/// the contents.
557-
pub fn erase_late_bound_regions_and_normalize<T>(self, value: &ty::Binder<T>)
558-
-> T
559-
where T: TransNormalize<'tcx>
560-
{
561-
assert!(!value.needs_subst());
562-
let value = self.erase_late_bound_regions(value);
563-
self.fully_normalize_associated_types_in(&value)
564-
}
565-
566-
/// Fully normalizes any associated types in `value`, using an
567-
/// empty environment and `Reveal::All` mode (therefore, suitable
568-
/// only for monomorphized code during trans, basically).
569-
pub fn fully_normalize_associated_types_in<T>(self, value: &T) -> T
570-
where T: TransNormalize<'tcx>
571-
{
572-
debug!("fully_normalize_associated_types_in(t={:?})", value);
573-
574-
let param_env = ty::ParamEnv::reveal_all();
575-
let value = self.erase_regions(value);
576-
577-
if !value.has_projections() {
578-
return value;
579-
}
580-
581-
self.infer_ctxt().enter(|infcx| {
582-
value.trans_normalize(&infcx, param_env)
583-
})
584-
}
585-
586-
/// Does a best-effort to normalize any associated types in
587-
/// `value`; this includes revealing specializable types, so this
588-
/// should be not be used during type-checking, but only during
589-
/// optimization and code generation.
590-
pub fn normalize_associated_type_in_env<T>(
591-
self, value: &T, env: ty::ParamEnv<'tcx>
592-
) -> T
593-
where T: TransNormalize<'tcx>
594-
{
595-
debug!("normalize_associated_type_in_env(t={:?})", value);
596-
597-
let value = self.erase_regions(value);
598-
599-
if !value.has_projections() {
600-
return value;
601-
}
602-
603-
self.infer_ctxt().enter(|infcx| {
604-
value.trans_normalize(&infcx, env.with_reveal_all())
605-
})
606-
}
607-
}
608-
609495
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
610-
fn normalize_projections_in<T>(&self, param_env: ty::ParamEnv<'tcx>, value: &T) -> T::Lifted
611-
where T: TypeFoldable<'tcx> + ty::Lift<'gcx>
612-
{
613-
let mut selcx = traits::SelectionContext::new(self);
614-
let cause = traits::ObligationCause::dummy();
615-
let traits::Normalized { value: result, obligations } =
616-
traits::normalize(&mut selcx, param_env, cause, value);
617-
618-
debug!("normalize_projections_in: result={:?} obligations={:?}",
619-
result, obligations);
620-
621-
let mut fulfill_cx = traits::FulfillmentContext::new();
622-
623-
for obligation in obligations {
624-
fulfill_cx.register_predicate_obligation(self, obligation);
625-
}
626-
627-
self.drain_fulfillment_cx_or_panic(DUMMY_SP, &mut fulfill_cx, &result)
628-
}
629-
630496
/// Finishes processes any obligations that remain in the
631497
/// fulfillment context, and then returns the result with all type
632498
/// variables removed and regions erased. Because this is intended

src/librustc/traits/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,10 @@ fn vtable_methods<'a, 'tcx>(
756756
// the trait type may have higher-ranked lifetimes in it;
757757
// so erase them if they appear, so that we get the type
758758
// at some particular call site
759-
let substs = tcx.erase_late_bound_regions_and_normalize(&ty::Binder(substs));
759+
let substs = tcx.normalize_erasing_late_bound_regions(
760+
ty::ParamEnv::reveal_all(),
761+
&ty::Binder(substs),
762+
);
760763

761764
// It's possible that the method relies on where clauses that
762765
// do not hold for this particular set of type parameters.

src/librustc/traits/trans/mod.rs

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
// general routines.
1515

1616
use dep_graph::{DepKind, DepTrackingMapConfig};
17-
use infer::TransNormalize;
1817
use std::marker::PhantomData;
1918
use syntax_pos::DUMMY_SP;
2019
use hir::def_id::DefId;
2120
use traits::{FulfillmentContext, Obligation, ObligationCause, SelectionContext, Vtable};
2221
use ty::{self, Ty, TyCtxt};
2322
use ty::subst::{Subst, Substs};
24-
use ty::fold::{TypeFoldable, TypeFolder};
23+
use ty::fold::TypeFoldable;
2524

2625
/// Attempts to resolve an obligation to a vtable.. The result is
2726
/// a shallow vtable resolution -- meaning that we do not
@@ -93,12 +92,11 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
9392
param_substs: &Substs<'tcx>,
9493
value: &T)
9594
-> T
96-
where T: TransNormalize<'tcx>
95+
where T: TypeFoldable<'tcx>
9796
{
9897
debug!("apply_param_substs(param_substs={:?}, value={:?})", param_substs, value);
9998
let substituted = value.subst(self, param_substs);
100-
let substituted = self.erase_regions(&substituted);
101-
AssociatedTypeNormalizer::new(self).fold(&substituted)
99+
self.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substituted)
102100
}
103101

104102
pub fn trans_apply_param_substs_env<T>(
@@ -108,7 +106,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
108106
value: &T,
109107
) -> T
110108
where
111-
T: TransNormalize<'tcx>,
109+
T: TypeFoldable<'tcx>,
112110
{
113111
debug!(
114112
"apply_param_substs_env(param_substs={:?}, value={:?}, param_env={:?})",
@@ -117,8 +115,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
117115
param_env,
118116
);
119117
let substituted = value.subst(self, param_substs);
120-
let substituted = self.erase_regions(&substituted);
121-
AssociatedTypeNormalizerEnv::new(self, param_env).fold(&substituted)
118+
self.normalize_erasing_regions(param_env, substituted)
122119
}
123120

124121
pub fn trans_impl_self_ty(&self, def_id: DefId, substs: &'tcx Substs<'tcx>)
@@ -128,73 +125,6 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
128125
}
129126
}
130127

131-
struct AssociatedTypeNormalizer<'a, 'gcx: 'a> {
132-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
133-
}
134-
135-
impl<'a, 'gcx> AssociatedTypeNormalizer<'a, 'gcx> {
136-
fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>) -> Self {
137-
AssociatedTypeNormalizer { tcx }
138-
}
139-
140-
fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
141-
if !value.has_projections() {
142-
value.clone()
143-
} else {
144-
value.fold_with(self)
145-
}
146-
}
147-
}
148-
149-
impl<'a, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'gcx> {
150-
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
151-
self.tcx
152-
}
153-
154-
fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
155-
if !ty.has_projections() {
156-
ty
157-
} else {
158-
debug!("AssociatedTypeNormalizer: ty={:?}", ty);
159-
self.tcx.fully_normalize_monormophic_ty(ty)
160-
}
161-
}
162-
}
163-
164-
struct AssociatedTypeNormalizerEnv<'a, 'gcx: 'a> {
165-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
166-
param_env: ty::ParamEnv<'gcx>,
167-
}
168-
169-
impl<'a, 'gcx> AssociatedTypeNormalizerEnv<'a, 'gcx> {
170-
fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>, param_env: ty::ParamEnv<'gcx>) -> Self {
171-
Self { tcx, param_env }
172-
}
173-
174-
fn fold<T: TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
175-
if !value.has_projections() {
176-
value.clone()
177-
} else {
178-
value.fold_with(self)
179-
}
180-
}
181-
}
182-
183-
impl<'a, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizerEnv<'a, 'gcx> {
184-
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
185-
self.tcx
186-
}
187-
188-
fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
189-
if !ty.has_projections() {
190-
ty
191-
} else {
192-
debug!("AssociatedTypeNormalizerEnv: ty={:?}", ty);
193-
self.tcx.normalize_associated_type_in_env(&ty, self.param_env)
194-
}
195-
}
196-
}
197-
198128
// Implement DepTrackingMapConfig for `trait_cache`
199129
pub struct TraitSelectionCache<'tcx> {
200130
data: PhantomData<&'tcx ()>

src/librustc/ty/context.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,9 +2463,6 @@ pub fn provide(providers: &mut ty::maps::Providers) {
24632463
assert_eq!(cnum, LOCAL_CRATE);
24642464
tcx.sess.features.borrow().clone_closures
24652465
};
2466-
providers.fully_normalize_monormophic_ty = |tcx, ty| {
2467-
tcx.fully_normalize_associated_types_in(&ty)
2468-
};
24692466
}
24702467

24712468
/// A trait implemented by types that can be canonicalized.

src/librustc/ty/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn fn_once_adapter_instance<'a, 'tcx>(
353353
closure_did, substs);
354354

355355
let sig = substs.closure_sig(closure_did, tcx);
356-
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
356+
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
357357
assert_eq!(sig.inputs().len(), 1);
358358
let substs = tcx.mk_substs([
359359
Kind::from(self_ty),

src/librustc/ty/layout.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
12021202
data_ptr.valid_range.start = 1;
12031203
}
12041204

1205-
let pointee = tcx.normalize_associated_type_in_env(&pointee, param_env);
1205+
let pointee = tcx.normalize_erasing_regions(param_env, pointee);
12061206
if pointee.is_sized(tcx, param_env, DUMMY_SP) {
12071207
return Ok(tcx.intern_layout(LayoutDetails::scalar(self, data_ptr)));
12081208
}
@@ -1230,7 +1230,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
12301230
// Arrays and slices.
12311231
ty::TyArray(element, mut count) => {
12321232
if count.has_projections() {
1233-
count = tcx.normalize_associated_type_in_env(&count, param_env);
1233+
count = tcx.normalize_erasing_regions(param_env, count);
12341234
if count.has_projections() {
12351235
return Err(LayoutError::Unknown(ty));
12361236
}
@@ -1675,7 +1675,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
16751675

16761676
// Types with no meaningful known layout.
16771677
ty::TyProjection(_) | ty::TyAnon(..) => {
1678-
let normalized = tcx.normalize_associated_type_in_env(&ty, param_env);
1678+
let normalized = tcx.normalize_erasing_regions(param_env, ty);
16791679
if ty == normalized {
16801680
return Err(LayoutError::Unknown(ty));
16811681
}
@@ -1942,7 +1942,7 @@ impl<'a, 'tcx> SizeSkeleton<'tcx> {
19421942
}
19431943

19441944
ty::TyProjection(_) | ty::TyAnon(..) => {
1945-
let normalized = tcx.normalize_associated_type_in_env(&ty, param_env);
1945+
let normalized = tcx.normalize_erasing_regions(param_env, ty);
19461946
if ty == normalized {
19471947
Err(err)
19481948
} else {
@@ -2048,7 +2048,7 @@ impl<'a, 'tcx> LayoutOf<Ty<'tcx>> for LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
20482048
/// executes in "reveal all" mode.
20492049
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
20502050
let param_env = self.param_env.with_reveal_all();
2051-
let ty = self.tcx.normalize_associated_type_in_env(&ty, param_env);
2051+
let ty = self.tcx.normalize_erasing_regions(param_env, ty);
20522052
let details = self.tcx.layout_raw(param_env.and(ty))?;
20532053
let layout = TyLayout {
20542054
ty,
@@ -2074,7 +2074,7 @@ impl<'a, 'tcx> LayoutOf<Ty<'tcx>> for LayoutCx<'tcx, ty::maps::TyCtxtAt<'a, 'tcx
20742074
/// executes in "reveal all" mode.
20752075
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
20762076
let param_env = self.param_env.with_reveal_all();
2077-
let ty = self.tcx.normalize_associated_type_in_env(&ty, param_env);
2077+
let ty = self.tcx.normalize_erasing_regions(param_env, ty);
20782078
let details = self.tcx.layout_raw(param_env.and(ty))?;
20792079
let layout = TyLayout {
20802080
ty,

src/librustc/ty/maps/config.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::has_copy_closures<'tcx> {
609609
}
610610
}
611611

612-
impl<'tcx> QueryDescription<'tcx> for queries::fully_normalize_monormophic_ty<'tcx> {
613-
fn describe(_tcx: TyCtxt, _: Ty) -> String {
614-
format!("normalizing types")
615-
}
616-
}
617-
618612
impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> {
619613
#[inline]
620614
fn cache_on_disk(def_id: Self::Key) -> bool {

src/librustc/ty/maps/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ define_maps! { <'tcx>
368368
// Normally you would just use `tcx.erase_regions(&value)`,
369369
// however, which uses this query as a kind of cache.
370370
[] fn erase_regions_ty: erase_regions_ty(Ty<'tcx>) -> Ty<'tcx>,
371-
[] fn fully_normalize_monormophic_ty: normalize_ty_node(Ty<'tcx>) -> Ty<'tcx>,
372371

373372
/// Do not call this query directly: invoke `normalize` instead.
374373
[] fn normalize_projection_ty: normalize_projection_ty_node(

src/librustc_const_eval/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
266266
match cx.tables.qpath_def(qpath, e.hir_id) {
267267
Def::Const(def_id) |
268268
Def::AssociatedConst(def_id) => {
269-
let substs = tcx.normalize_associated_type_in_env(&substs, cx.param_env);
269+
let substs = tcx.normalize_erasing_regions(cx.param_env, substs);
270270
match tcx.at(e.span).const_eval(cx.param_env.and((def_id, substs))) {
271271
Ok(val) => val,
272272
Err(ConstEvalErr { kind: TypeckError, .. }) => {

0 commit comments

Comments
 (0)