Skip to content

Commit 2e3d105

Browse files
committed
Auto merge of #61842 - Zoxc:trim-lift, r=<try>
[WIP] Remove unnecessary lift calls Note that some of these might be useful for sanity checking that there's no infer types or regions. r? @eddyb
2 parents 9606f6f + 007aaba commit 2e3d105

File tree

22 files changed

+50
-89
lines changed

22 files changed

+50
-89
lines changed

src/librustc/infer/canonical/canonicalizer.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::mir::interpret::ConstValue;
1414
use std::sync::atomic::Ordering;
1515
use crate::ty::fold::{TypeFoldable, TypeFolder};
1616
use crate::ty::subst::Kind;
17-
use crate::ty::{self, BoundVar, InferConst, Lift, List, Ty, TyCtxt, TypeFlags};
17+
use crate::ty::{self, BoundVar, InferConst, List, Ty, TyCtxt, TypeFlags};
1818
use crate::ty::flags::FlagComputation;
1919

2020
use rustc_data_structures::fx::FxHashMap;
@@ -43,7 +43,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
4343
query_state: &mut OriginalQueryValues<'tcx>,
4444
) -> Canonicalized<'tcx, V>
4545
where
46-
V: TypeFoldable<'tcx> + Lift<'tcx>,
46+
V: TypeFoldable<'tcx>,
4747
{
4848
self.tcx
4949
.sess
@@ -87,7 +87,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
8787
/// [c]: https://rust-lang.github.io/rustc-guide/traits/canonicalization.html#canonicalizing-the-query-result
8888
pub fn canonicalize_response<V>(&self, value: &V) -> Canonicalized<'tcx, V>
8989
where
90-
V: TypeFoldable<'tcx> + Lift<'tcx>,
90+
V: TypeFoldable<'tcx>,
9191
{
9292
let mut query_state = OriginalQueryValues::default();
9393
Canonicalizer::canonicalize(
@@ -101,7 +101,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
101101

102102
pub fn canonicalize_user_type_annotation<V>(&self, value: &V) -> Canonicalized<'tcx, V>
103103
where
104-
V: TypeFoldable<'tcx> + Lift<'tcx>,
104+
V: TypeFoldable<'tcx>,
105105
{
106106
let mut query_state = OriginalQueryValues::default();
107107
Canonicalizer::canonicalize(
@@ -132,7 +132,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
132132
query_state: &mut OriginalQueryValues<'tcx>,
133133
) -> Canonicalized<'tcx, V>
134134
where
135-
V: TypeFoldable<'tcx> + Lift<'tcx>,
135+
V: TypeFoldable<'tcx>,
136136
{
137137
self.tcx
138138
.sess
@@ -506,7 +506,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
506506
query_state: &mut OriginalQueryValues<'tcx>,
507507
) -> Canonicalized<'tcx, V>
508508
where
509-
V: TypeFoldable<'tcx> + Lift<'tcx>,
509+
V: TypeFoldable<'tcx>,
510510
{
511511
let needs_canonical_flags = if canonicalize_region_mode.any() {
512512
TypeFlags::KEEP_IN_LOCAL_TCX |
@@ -520,20 +520,12 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
520520
TypeFlags::HAS_CT_PLACEHOLDER
521521
};
522522

523-
let gcx = tcx.global_tcx();
524-
525523
// Fast path: nothing that needs to be canonicalized.
526524
if !value.has_type_flags(needs_canonical_flags) {
527-
let out_value = gcx.lift(value).unwrap_or_else(|| {
528-
bug!(
529-
"failed to lift `{:?}` (nothing to canonicalize)",
530-
value
531-
)
532-
});
533525
let canon_value = Canonical {
534526
max_universe: ty::UniverseIndex::ROOT,
535527
variables: List::empty(),
536-
value: out_value,
528+
value: value.clone(),
537529
};
538530
return canon_value;
539531
}
@@ -553,13 +545,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
553545
// Once we have canonicalized `out_value`, it should not
554546
// contain anything that ties it to this inference context
555547
// anymore, so it should live in the global arena.
556-
let out_value = gcx.lift(&out_value).unwrap_or_else(|| {
557-
bug!(
558-
"failed to lift `{:?}`, canonicalized from `{:?}`",
559-
out_value,
560-
value
561-
)
562-
});
548+
debug_assert!(!out_value.has_type_flags(TypeFlags::KEEP_IN_LOCAL_TCX));
563549

564550
let canonical_variables = tcx.intern_canonical_var_infos(&canonicalizer.variables);
565551

src/librustc/infer/canonical/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ pub struct QueryResponse<'tcx, R> {
194194
pub value: R,
195195
}
196196

197-
pub type Canonicalized<'tcx, V> = Canonical<'tcx, <V as Lift<'tcx>>::Lifted>;
197+
pub type Canonicalized<'tcx, V> = Canonical<'tcx, V>;
198198

199199
pub type CanonicalizedQueryResponse<'tcx, T> =
200-
&'tcx Canonical<'tcx, QueryResponse<'tcx, <T as Lift<'tcx>>::Lifted>>;
200+
&'tcx Canonical<'tcx, QueryResponse<'tcx, T>>;
201201

202202
/// Indicates whether or not we were able to prove the query to be
203203
/// true.

src/librustc/infer/canonical/query_response.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::traits::TraitEngine;
2626
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
2727
use crate::ty::fold::TypeFoldable;
2828
use crate::ty::subst::{Kind, UnpackedKind};
29-
use crate::ty::{self, BoundVar, InferConst, Lift, Ty, TyCtxt};
29+
use crate::ty::{self, BoundVar, InferConst, Ty, TyCtxt};
3030
use crate::util::captures::Captures;
3131

3232
impl<'tcx> InferCtxtBuilder<'tcx> {
@@ -53,8 +53,8 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
5353
) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
5454
where
5555
K: TypeFoldable<'tcx>,
56-
R: Debug + Lift<'tcx> + TypeFoldable<'tcx>,
57-
Canonical<'tcx, <QueryResponse<'tcx, R> as Lift<'tcx>>::Lifted>: ArenaAllocatable,
56+
R: Debug + TypeFoldable<'tcx>,
57+
Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable,
5858
{
5959
self.enter_with_canonical(
6060
DUMMY_SP,
@@ -99,8 +99,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
9999
fulfill_cx: &mut dyn TraitEngine<'tcx>,
100100
) -> Fallible<CanonicalizedQueryResponse<'tcx, T>>
101101
where
102-
T: Debug + Lift<'tcx> + TypeFoldable<'tcx>,
103-
Canonical<'tcx, <QueryResponse<'tcx, T> as Lift<'tcx>>::Lifted>: ArenaAllocatable,
102+
T: Debug + TypeFoldable<'tcx>,
103+
Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable,
104104
{
105105
let query_response = self.make_query_response(inference_vars, answer, fulfill_cx)?;
106106
let canonical_result = self.canonicalize_response(&query_response);
@@ -126,9 +126,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
126126
&self,
127127
inference_vars: CanonicalVarValues<'tcx>,
128128
answer: T,
129-
) -> Canonical<'tcx, QueryResponse<'tcx, <T as Lift<'tcx>>::Lifted>>
129+
) -> Canonical<'tcx, QueryResponse<'tcx, T>>
130130
where
131-
T: Debug + Lift<'tcx> + TypeFoldable<'tcx>,
131+
T: Debug + TypeFoldable<'tcx>,
132132
{
133133
self.canonicalize_response(&QueryResponse {
134134
var_values: inference_vars,
@@ -147,7 +147,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
147147
fulfill_cx: &mut dyn TraitEngine<'tcx>,
148148
) -> Result<QueryResponse<'tcx, T>, NoSolution>
149149
where
150-
T: Debug + TypeFoldable<'tcx> + Lift<'tcx>,
150+
T: Debug + TypeFoldable<'tcx>,
151151
{
152152
let tcx = self.tcx;
153153

src/librustc/infer/opaque_types/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
469469
definition_ty
470470
);
471471

472-
// We can unwrap here because our reverse mapper always
473-
// produces things with 'tcx lifetime, though the type folder
474-
// obscures that.
475-
let definition_ty = gcx.lift(&definition_ty).unwrap();
476-
477472
definition_ty
478473
}
479474
}

src/librustc/middle/mem_categorization.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
819819
.unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
820820

821821
None =>
822-
self.tcx.global_tcx()
823-
.lift(&closure_substs)
824-
.expect("no inference cx, but inference variables in closure ty")
825-
.closure_kind(closure_def_id, self.tcx.global_tcx()),
822+
closure_substs.closure_kind(closure_def_id, self.tcx.global_tcx()),
826823
}
827824
}
828825
_ => span_bug!(span, "unexpected type for fn in mem_categorization: {:?}", ty),

src/librustc/traits/codegen/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
141141
&self,
142142
fulfill_cx: &mut FulfillmentContext<'tcx>,
143143
result: &T,
144-
) -> T::Lifted
144+
) -> T
145145
where
146-
T: TypeFoldable<'tcx> + ty::Lift<'tcx>,
146+
T: TypeFoldable<'tcx>,
147147
{
148148
debug!("drain_fulfillment_cx_or_panic()");
149149

@@ -155,10 +155,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
155155
}
156156

157157
let result = self.resolve_vars_if_possible(result);
158-
let result = self.tcx.erase_regions(&result);
159-
160-
self.tcx.lift_to_global(&result).unwrap_or_else(||
161-
bug!("Uninferred types/regions/consts in `{:?}`", result)
162-
)
158+
self.tcx.erase_regions(&result)
163159
}
164160
}

src/librustc/traits/project.rs

-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
409409
promoted: None
410410
};
411411
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
412-
let substs = tcx.lift_to_global(&substs).unwrap();
413412
let evaluated = evaluated.subst(tcx, substs);
414413
return evaluated;
415414
}

src/librustc/traits/query/normalize.rs

-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
203203
promoted: None,
204204
};
205205
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
206-
let substs = tcx.lift_to_global(&substs).unwrap();
207206
let evaluated = evaluated.subst(tcx, substs);
208207
return evaluated;
209208
}

src/librustc/traits/query/type_op/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::rc::Rc;
88
use crate::traits::query::Fallible;
99
use crate::traits::ObligationCause;
1010
use crate::ty::fold::TypeFoldable;
11-
use crate::ty::{Lift, ParamEnvAnd, TyCtxt};
11+
use crate::ty::{ParamEnvAnd, TyCtxt};
1212

1313
pub mod ascribe_user_type;
1414
pub mod custom;
@@ -44,8 +44,8 @@ pub trait TypeOp<'tcx>: Sized + fmt::Debug {
4444
/// which produces the resulting query region constraints.
4545
///
4646
/// [c]: https://rust-lang.github.io/rustc-guide/traits/canonicalization.html
47-
pub trait QueryTypeOp<'tcx>: fmt::Debug + Sized + TypeFoldable<'tcx> + Lift<'tcx> {
48-
type QueryResponse: TypeFoldable<'tcx> + Lift<'tcx>;
47+
pub trait QueryTypeOp<'tcx>: fmt::Debug + Sized + TypeFoldable<'tcx> + 'tcx {
48+
type QueryResponse: TypeFoldable<'tcx>;
4949

5050
/// Give query the option for a simple fast path that never
5151
/// actually hits the tcx cache lookup etc. Return `Some(r)` with

src/librustc/traits/query/type_op/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ where
2020

2121
impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<T>
2222
where
23-
T: Normalizable<'tcx>,
23+
T: Normalizable<'tcx> + 'tcx,
2424
{
2525
type QueryResponse = T;
2626

src/librustc/traits/specialize/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ pub fn find_associated_item<'tcx>(
132132
let substs = substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
133133
let substs = translate_substs(&infcx, param_env, impl_data.impl_def_id,
134134
substs, node_item.node);
135-
let substs = infcx.tcx.erase_regions(&substs);
136-
tcx.lift(&substs).unwrap_or_else(||
137-
bug!("find_method: translate_substs \
138-
returned {:?} which contains inference types/regions",
139-
substs)
140-
)
135+
infcx.tcx.erase_regions(&substs)
141136
});
142137
(node_item.item.def_id, substs)
143138
}

src/librustc/ty/error.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ impl<'tcx> ty::TyS<'tcx> {
192192

193193
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
194194
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
195-
ty::Array(_, n) => match n.assert_usize(tcx) {
196-
Some(n) => format!("array of {} elements", n).into(),
197-
None => "array".into(),
195+
ty::Array(_, n) => {
196+
let n = tcx.lift_to_global(&n).unwrap();
197+
match n.assert_usize(tcx) {
198+
Some(n) => format!("array of {} elements", n).into(),
199+
None => "array".into(),
200+
}
198201
}
199202
ty::Slice(_) => "slice".into(),
200203
ty::RawPtr(_) => "*-ptr".into(),

src/librustc/ty/sty.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,6 @@ impl<'tcx> Const<'tcx> {
22622262

22632263
#[inline]
22642264
pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> &'tcx Self {
2265-
let ty = tcx.lift_to_global(&ty).unwrap();
22662265
let size = tcx.layout_of(ty).unwrap_or_else(|e| {
22672266
panic!("could not compute layout for {:?}: {:?}", ty, e)
22682267
}).size;
@@ -2289,7 +2288,6 @@ impl<'tcx> Const<'tcx> {
22892288
if self.ty != ty.value {
22902289
return None;
22912290
}
2292-
let ty = tcx.lift_to_global(&ty).unwrap();
22932291
let size = tcx.layout_of(ty).ok()?.size;
22942292
self.val.try_to_bits(size)
22952293
}
@@ -2300,15 +2298,14 @@ impl<'tcx> Const<'tcx> {
23002298
}
23012299

23022300
#[inline]
2303-
pub fn assert_bits(&self, tcx: TyCtxt<'_>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
2301+
pub fn assert_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Option<u128> {
23042302
assert_eq!(self.ty, ty.value);
2305-
let ty = tcx.lift_to_global(&ty).unwrap();
23062303
let size = tcx.layout_of(ty).ok()?.size;
23072304
self.val.try_to_bits(size)
23082305
}
23092306

23102307
#[inline]
2311-
pub fn assert_bool(&self, tcx: TyCtxt<'_>) -> Option<bool> {
2308+
pub fn assert_bool(&self, tcx: TyCtxt<'tcx>) -> Option<bool> {
23122309
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.bool)).and_then(|v| match v {
23132310
0 => Some(false),
23142311
1 => Some(true),
@@ -2317,18 +2314,18 @@ impl<'tcx> Const<'tcx> {
23172314
}
23182315

23192316
#[inline]
2320-
pub fn assert_usize(&self, tcx: TyCtxt<'_>) -> Option<u64> {
2317+
pub fn assert_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
23212318
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.usize)).map(|v| v as u64)
23222319
}
23232320

23242321
#[inline]
2325-
pub fn unwrap_bits(&self, tcx: TyCtxt<'_>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> u128 {
2322+
pub fn unwrap_bits(&self, tcx: TyCtxt<'tcx>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> u128 {
23262323
self.assert_bits(tcx, ty).unwrap_or_else(||
23272324
bug!("expected bits of {}, got {:#?}", ty.value, self))
23282325
}
23292326

23302327
#[inline]
2331-
pub fn unwrap_usize(&self, tcx: TyCtxt<'_>) -> u64 {
2328+
pub fn unwrap_usize(&self, tcx: TyCtxt<'tcx>) -> u64 {
23322329
self.assert_usize(tcx).unwrap_or_else(||
23332330
bug!("expected constant usize, got {:#?}", self))
23342331
}

src/librustc_mir/borrow_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ impl<'cx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx
670670
// "Lift" into the gcx -- once regions are erased, this type should be in the
671671
// global arenas; this "lift" operation basically just asserts that is true, but
672672
// that is useful later.
673-
let drop_place_ty = gcx.lift(&drop_place_ty).unwrap();
673+
gcx.lift_to_global(&drop_place_ty).unwrap();
674674

675675
debug!("visit_terminator_drop \
676676
loc: {:?} term: {:?} drop_place: {:?} drop_place_ty: {:?} span: {:?}",

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
863863
});
864864
debug!("try_promote_type_test_subject: folded ty = {:?}", ty);
865865

866-
// `lift` will only fail if we failed to promote some region.
867-
let ty = gcx.lift(&ty)?;
866+
// `lift_to_global` will only fail if we failed to promote some region.
867+
gcx.lift_to_global(&ty)?;
868868

869869
Some(ClosureOutlivesSubject::Ty(ty))
870870
}

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18661866
// `Sized` bound in no way depends on precise regions, so this
18671867
// shouldn't affect `is_sized`.
18681868
let gcx = tcx.global_tcx();
1869-
let erased_ty = gcx.lift(&tcx.erase_regions(&ty)).unwrap();
1869+
let erased_ty = tcx.erase_regions(&ty);
18701870
if !erased_ty.is_sized(gcx.at(span), self.param_env) {
18711871
// in current MIR construction, all non-control-flow rvalue
18721872
// expressions evaluate through `as_temp` or `into` a return
@@ -2652,7 +2652,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26522652

26532653
fn normalize<T>(&mut self, value: T, location: impl NormalizeLocation) -> T
26542654
where
2655-
T: type_op::normalize::Normalizable<'tcx> + Copy,
2655+
T: type_op::normalize::Normalizable<'tcx> + Copy + 'tcx,
26562656
{
26572657
debug!("normalize(value={:?}, location={:?})", value, location);
26582658
let param_env = self.param_env;

src/librustc_mir/build/expr/as_rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
569569

570570
// Helper to get a `-1` value of the appropriate type
571571
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
572-
let param_ty = ty::ParamEnv::empty().and(self.hir.tcx().lift_to_global(&ty).unwrap());
572+
let param_ty = ty::ParamEnv::empty().and(ty);
573573
let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
574574
let n = (!0u128) >> (128 - bits);
575575
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
@@ -580,7 +580,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
580580
// Helper to get the minimum value of the appropriate type
581581
fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
582582
assert!(ty.is_signed());
583-
let param_ty = ty::ParamEnv::empty().and(self.hir.tcx().lift_to_global(&ty).unwrap());
583+
let param_ty = ty::ParamEnv::empty().and(ty);
584584
let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
585585
let n = 1 << (bits - 1);
586586
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);

src/librustc_mir/dataflow/drop_flag_effects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub(crate) fn on_all_drop_children_bits<'tcx, F>(
151151
debug!("on_all_drop_children_bits({:?}, {:?} : {:?})", path, place, ty);
152152

153153
let gcx = tcx.global_tcx();
154-
let erased_ty = gcx.lift(&tcx.erase_regions(&ty)).unwrap();
154+
let erased_ty = tcx.erase_regions(&ty);
155155
if erased_ty.needs_drop(gcx, ctxt.param_env) {
156156
each_child(child);
157157
} else {

0 commit comments

Comments
 (0)