Skip to content

Commit e4fabaa

Browse files
authored
Rollup merge of #155987 - nnethercote:infallible-Lifting, r=oli-obk
Make lifting infallible Details in individual commits. r? @oli-obk
2 parents 2ce7b84 + a762dbf commit e4fabaa

25 files changed

Lines changed: 97 additions & 121 deletions

File tree

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,11 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
216216
ty::tls::with(|tcx| {
217217
match self.imm {
218218
Immediate::Scalar(s) => {
219-
if let Some(ty) = tcx.lift(self.layout.ty) {
220-
let s = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
221-
print_scalar(p, s, ty)
222-
})?;
223-
f.write_str(&s)?;
224-
return Ok(());
225-
}
226-
write!(f, "{:x}: {}", s, self.layout.ty)
219+
let ty = tcx.lift(self.layout.ty);
220+
let s = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
221+
print_scalar(p, s, ty)
222+
})?;
223+
f.write_str(&s)
227224
}
228225
Immediate::ScalarPair(a, b) => {
229226
// FIXME(oli-obk): at least print tuples and slices nicely

compiler/rustc_macros/src/lift.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,19 @@ pub(super) fn lift_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
3434
let bindings = &vi.bindings();
3535
vi.construct(|_, index| {
3636
let bi = &bindings[index];
37-
quote! { __tcx.lift(#bi)? }
37+
quote! { __tcx.lift(#bi) }
3838
})
3939
});
4040

4141
s.add_impl_generic(newtcx);
42-
s.bound_impl(quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>), quote! {
43-
type Lifted = #lifted;
42+
s.bound_impl(
43+
quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>),
44+
quote! {
45+
type Lifted = #lifted;
4446

45-
fn lift_to_interner(self, __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>) -> Option<#lifted> {
46-
Some(match self { #body })
47-
}
48-
})
47+
fn lift_to_interner(self, __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>) -> #lifted {
48+
match self { #body }
49+
}
50+
},
51+
)
4952
}

compiler/rustc_middle/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ macro_rules! TrivialLiftImpls {
4646
$(
4747
impl<'tcx> $crate::ty::Lift<$crate::ty::TyCtxt<'tcx>> for $ty {
4848
type Lifted = Self;
49-
fn lift_to_interner(self, _: $crate::ty::TyCtxt<'tcx>) -> Option<Self> {
50-
Some(self)
49+
fn lift_to_interner(self, _: $crate::ty::TyCtxt<'tcx>) -> Self {
50+
self
5151
}
5252
}
5353
)+

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl<'tcx> Display for Const<'tcx> {
493493
// FIXME(valtrees): Correctly print mir constants.
494494
Const::Unevaluated(c, _ty) => {
495495
ty::tls::with(move |tcx| {
496-
let c = tcx.lift(c).unwrap();
496+
let c = tcx.lift(c);
497497
// Matches `GlobalId` printing.
498498
let instance =
499499
with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11651165
AggregateKind::Adt(adt_did, variant, args, _user_ty, _) => {
11661166
ty::tls::with(|tcx| {
11671167
let variant_def = &tcx.adt_def(adt_did).variant(variant);
1168-
let args = tcx.lift(args).expect("could not lift for printing");
1168+
let args = tcx.lift(args);
11691169
let name = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
11701170
p.print_def_path(variant_def.def_id, args)
11711171
})?;
@@ -1187,7 +1187,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11871187
AggregateKind::Closure(def_id, args)
11881188
| AggregateKind::CoroutineClosure(def_id, args) => ty::tls::with(|tcx| {
11891189
let name = if tcx.sess.opts.unstable_opts.span_free_formats {
1190-
let args = tcx.lift(args).unwrap();
1190+
let args = tcx.lift(args);
11911191
format!("{{closure@{}}}", tcx.def_path_str_with_args(def_id, args),)
11921192
} else {
11931193
let span = tcx.def_span(def_id);
@@ -1911,8 +1911,6 @@ fn pretty_print_const_value_tcx<'tcx>(
19111911
// E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
19121912
// to be able to destructure the tuple into `(0u8, *mut T)`
19131913
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_non_region_param() => {
1914-
let ct = tcx.lift(ct).unwrap();
1915-
let ty = tcx.lift(ty).unwrap();
19161914
if let Some(contents) = tcx.try_destructure_mir_constant_for_user_output(ct, ty) {
19171915
let fields: Vec<(ConstValue, Ty<'_>)> = contents.fields.to_vec();
19181916
match *ty.kind() {
@@ -1937,7 +1935,6 @@ fn pretty_print_const_value_tcx<'tcx>(
19371935
.variant
19381936
.expect("destructed mir constant of adt without variant idx");
19391937
let variant_def = &def.variant(variant_idx);
1940-
let args = tcx.lift(args).unwrap();
19411938
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
19421939
p.print_alloc_ids = true;
19431940
p.pretty_print_value_path(variant_def.def_id, args)?;
@@ -1974,7 +1971,6 @@ fn pretty_print_const_value_tcx<'tcx>(
19741971
(ConstValue::Scalar(scalar), _) => {
19751972
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
19761973
p.print_alloc_ids = true;
1977-
let ty = tcx.lift(ty).unwrap();
19781974
p.pretty_print_const_scalar(scalar, ty)?;
19791975
fmt.write_str(&p.into_buffer())?;
19801976
return Ok(());
@@ -2000,8 +1996,7 @@ pub(crate) fn pretty_print_const_value<'tcx>(
20001996
fmt: &mut Formatter<'_>,
20011997
) -> fmt::Result {
20021998
ty::tls::with(|tcx| {
2003-
let ct = tcx.lift(ct).unwrap();
2004-
let ty = tcx.lift(ty).unwrap();
1999+
let ty = tcx.lift(ty);
20052000
pretty_print_const_value_tcx(tcx, ct, ty, fmt)
20062001
})
20072002
}

compiler/rustc_middle/src/ty/consts/valtree.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,8 @@ impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {
238238
impl<'tcx> fmt::Display for Value<'tcx> {
239239
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240240
ty::tls::with(move |tcx| {
241-
let cv = tcx.lift(*self).unwrap();
242241
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
243-
p.pretty_print_const_valtree(cv, /*print_ty*/ true)?;
242+
p.pretty_print_const_valtree(tcx.lift(*self), /*print_ty*/ true)?;
244243
f.write_str(&p.into_buffer())
245244
})
246245
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ impl<'tcx> TyCtxt<'tcx> {
958958
(start, end)
959959
}
960960

961-
pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted> {
961+
pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> T::Lifted {
962962
value.lift_to_interner(self)
963963
}
964964

@@ -1689,7 +1689,8 @@ macro_rules! nop_lift {
16891689
($set:ident; $ty:ty => $lifted:ty) => {
16901690
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for $ty {
16911691
type Lifted = $lifted;
1692-
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1692+
#[track_caller]
1693+
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
16931694
// Assert that the set has the right type.
16941695
// Given an argument that has an interned type, the return type has the type of
16951696
// the corresponding interner set. This won't actually return anything, we're
@@ -1709,12 +1710,10 @@ macro_rules! nop_lift {
17091710
_type_eq(&interner, &tcx.interners.$set);
17101711
}
17111712

1712-
tcx.interners
1713-
.$set
1714-
.contains_pointer_to(&InternedInSet(&*self.0.0))
1715-
// SAFETY: `self` is interned and therefore valid
1716-
// for the entire lifetime of the `TyCtxt`.
1717-
.then(|| unsafe { mem::transmute(self) })
1713+
assert!(tcx.interners.$set.contains_pointer_to(&InternedInSet(&*self.0.0)));
1714+
// SAFETY: we just checked that `self` is interned and therefore is valid for the
1715+
// entire lifetime of the `TyCtxt`.
1716+
unsafe { mem::transmute(self) }
17181717
}
17191718
}
17201719
};
@@ -1724,19 +1723,19 @@ macro_rules! nop_list_lift {
17241723
($set:ident; $ty:ty => $lifted:ty) => {
17251724
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a List<$ty> {
17261725
type Lifted = &'tcx List<$lifted>;
1727-
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1726+
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
17281727
// Assert that the set has the right type.
17291728
if false {
17301729
let _x: &InternedSet<'tcx, List<$lifted>> = &tcx.interners.$set;
17311730
}
17321731

17331732
if self.is_empty() {
1734-
return Some(List::empty());
1733+
return List::empty();
17351734
}
1736-
tcx.interners
1737-
.$set
1738-
.contains_pointer_to(&InternedInSet(self))
1739-
.then(|| unsafe { mem::transmute(self) })
1735+
assert!(tcx.interners.$set.contains_pointer_to(&InternedInSet(self)));
1736+
// SAFETY: we just checked that `self` is interned and therefore is valid for the
1737+
// entire lifetime of the `TyCtxt`.
1738+
unsafe { mem::transmute(self) }
17401739
}
17411740
}
17421741
};

compiler/rustc_middle/src/ty/error.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,8 @@ impl<'tcx> TyCtxt<'tcx> {
223223
T: Copy + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
224224
{
225225
let mut type_limit = 50;
226-
let regular = FmtPrinter::print_string(self, ns, |p| {
227-
self.lift(t).expect("could not lift for printing").print(p)
228-
})
229-
.expect("could not write to `String`");
226+
let regular = FmtPrinter::print_string(self, ns, |p| self.lift(t).print(p))
227+
.expect("could not write to `String`");
230228
if regular.len() <= length_limit {
231229
return regular;
232230
}
@@ -235,10 +233,7 @@ impl<'tcx> TyCtxt<'tcx> {
235233
// Look for the longest properly trimmed path that still fits in length_limit.
236234
short = with_forced_trimmed_paths!({
237235
let mut p = FmtPrinter::new_with_limit(self, ns, Limit(type_limit));
238-
self.lift(t)
239-
.expect("could not lift for printing")
240-
.print(&mut p)
241-
.expect("could not print type");
236+
self.lift(t).print(&mut p).expect("could not print type");
242237
p.into_buffer()
243238
});
244239
if short.len() <= length_limit || type_limit == 0 {
@@ -273,10 +268,8 @@ impl<'tcx> TyCtxt<'tcx> {
273268
where
274269
T: Copy + Hash + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
275270
{
276-
let regular = FmtPrinter::print_string(self, namespace, |p| {
277-
self.lift(t).expect("could not lift for printing").print(p)
278-
})
279-
.expect("could not write to `String`");
271+
let regular = FmtPrinter::print_string(self, namespace, |p| self.lift(t).print(p))
272+
.expect("could not write to `String`");
280273

281274
if !self.sess.opts.unstable_opts.write_long_types_to_disk || self.sess.opts.verbose {
282275
return regular;

compiler/rustc_middle/src/ty/generic_args.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ impl<'tcx> GenericArg<'tcx> {
320320
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {
321321
type Lifted = GenericArg<'tcx>;
322322

323-
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
323+
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
324324
match self.kind() {
325-
GenericArgKind::Lifetime(lt) => tcx.lift(lt).map(|lt| lt.into()),
326-
GenericArgKind::Type(ty) => tcx.lift(ty).map(|ty| ty.into()),
327-
GenericArgKind::Const(ct) => tcx.lift(ct).map(|ct| ct.into()),
325+
GenericArgKind::Lifetime(lt) => tcx.lift(lt).into(),
326+
GenericArgKind::Type(ty) => tcx.lift(ty).into(),
327+
GenericArgKind::Const(ct) => tcx.lift(ct).into(),
328328
}
329329
}
330330
}

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
381381
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
382382
ty::tls::with(|tcx| {
383383
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
384-
let instance = tcx.lift(*self).expect("could not lift for printing");
385-
instance.print(&mut p)?;
384+
tcx.lift(*self).print(&mut p)?;
386385
let s = p.into_buffer();
387386
f.write_str(&s)
388387
})

0 commit comments

Comments
 (0)