Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,11 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
ty::tls::with(|tcx| {
match self.imm {
Immediate::Scalar(s) => {
if let Some(ty) = tcx.lift(self.layout.ty) {
let s = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
print_scalar(p, s, ty)
})?;
f.write_str(&s)?;
return Ok(());
}
write!(f, "{:x}: {}", s, self.layout.ty)
let ty = tcx.lift(self.layout.ty);
let s = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
print_scalar(p, s, ty)
})?;
f.write_str(&s)
}
Immediate::ScalarPair(a, b) => {
// FIXME(oli-obk): at least print tuples and slices nicely
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_macros/src/lift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ pub(super) fn lift_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
let bindings = &vi.bindings();
vi.construct(|_, index| {
let bi = &bindings[index];
quote! { __tcx.lift(#bi)? }
quote! { __tcx.lift(#bi) }
})
});

s.add_impl_generic(newtcx);
s.bound_impl(quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>), quote! {
type Lifted = #lifted;
s.bound_impl(
quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>),
quote! {
type Lifted = #lifted;

fn lift_to_interner(self, __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>) -> Option<#lifted> {
Some(match self { #body })
}
})
fn lift_to_interner(self, __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>) -> #lifted {
match self { #body }
}
},
)
}
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ macro_rules! TrivialLiftImpls {
$(
impl<'tcx> $crate::ty::Lift<$crate::ty::TyCtxt<'tcx>> for $ty {
type Lifted = Self;
fn lift_to_interner(self, _: $crate::ty::TyCtxt<'tcx>) -> Option<Self> {
Some(self)
fn lift_to_interner(self, _: $crate::ty::TyCtxt<'tcx>) -> Self {
self
}
}
)+
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl<'tcx> Display for Const<'tcx> {
// FIXME(valtrees): Correctly print mir constants.
Const::Unevaluated(c, _ty) => {
ty::tls::with(move |tcx| {
let c = tcx.lift(c).unwrap();
let c = tcx.lift(c);
// Matches `GlobalId` printing.
let instance =
with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
AggregateKind::Adt(adt_did, variant, args, _user_ty, _) => {
ty::tls::with(|tcx| {
let variant_def = &tcx.adt_def(adt_did).variant(variant);
let args = tcx.lift(args).expect("could not lift for printing");
let args = tcx.lift(args);
let name = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
p.print_def_path(variant_def.def_id, args)
})?;
Expand All @@ -1187,7 +1187,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
AggregateKind::Closure(def_id, args)
| AggregateKind::CoroutineClosure(def_id, args) => ty::tls::with(|tcx| {
let name = if tcx.sess.opts.unstable_opts.span_free_formats {
let args = tcx.lift(args).unwrap();
let args = tcx.lift(args);
format!("{{closure@{}}}", tcx.def_path_str_with_args(def_id, args),)
} else {
let span = tcx.def_span(def_id);
Expand Down Expand Up @@ -1911,8 +1911,6 @@ fn pretty_print_const_value_tcx<'tcx>(
// E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
// to be able to destructure the tuple into `(0u8, *mut T)`
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_non_region_param() => {
let ct = tcx.lift(ct).unwrap();
let ty = tcx.lift(ty).unwrap();
if let Some(contents) = tcx.try_destructure_mir_constant_for_user_output(ct, ty) {
let fields: Vec<(ConstValue, Ty<'_>)> = contents.fields.to_vec();
match *ty.kind() {
Expand All @@ -1937,7 +1935,6 @@ fn pretty_print_const_value_tcx<'tcx>(
.variant
.expect("destructed mir constant of adt without variant idx");
let variant_def = &def.variant(variant_idx);
let args = tcx.lift(args).unwrap();
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
p.print_alloc_ids = true;
p.pretty_print_value_path(variant_def.def_id, args)?;
Expand Down Expand Up @@ -1974,7 +1971,6 @@ fn pretty_print_const_value_tcx<'tcx>(
(ConstValue::Scalar(scalar), _) => {
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
p.print_alloc_ids = true;
let ty = tcx.lift(ty).unwrap();
p.pretty_print_const_scalar(scalar, ty)?;
fmt.write_str(&p.into_buffer())?;
return Ok(());
Expand All @@ -2000,8 +1996,7 @@ pub(crate) fn pretty_print_const_value<'tcx>(
fmt: &mut Formatter<'_>,
) -> fmt::Result {
ty::tls::with(|tcx| {
let ct = tcx.lift(ct).unwrap();
let ty = tcx.lift(ty).unwrap();
let ty = tcx.lift(ty);
pretty_print_const_value_tcx(tcx, ct, ty, fmt)
})
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/ty/consts/valtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,8 @@ impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {
impl<'tcx> fmt::Display for Value<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(move |tcx| {
let cv = tcx.lift(*self).unwrap();
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
p.pretty_print_const_valtree(cv, /*print_ty*/ true)?;
p.pretty_print_const_valtree(tcx.lift(*self), /*print_ty*/ true)?;
f.write_str(&p.into_buffer())
})
}
Expand Down
27 changes: 13 additions & 14 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ impl<'tcx> TyCtxt<'tcx> {
(start, end)
}

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

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

tcx.interners
.$set
.contains_pointer_to(&InternedInSet(&*self.0.0))
// SAFETY: `self` is interned and therefore valid
// for the entire lifetime of the `TyCtxt`.
.then(|| unsafe { mem::transmute(self) })
assert!(tcx.interners.$set.contains_pointer_to(&InternedInSet(&*self.0.0)));
// SAFETY: we just checked that `self` is interned and therefore is valid for the
// entire lifetime of the `TyCtxt`.
unsafe { mem::transmute(self) }
}
}
};
Expand All @@ -1724,19 +1723,19 @@ macro_rules! nop_list_lift {
($set:ident; $ty:ty => $lifted:ty) => {
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a List<$ty> {
type Lifted = &'tcx List<$lifted>;
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
// Assert that the set has the right type.
if false {
let _x: &InternedSet<'tcx, List<$lifted>> = &tcx.interners.$set;
}

if self.is_empty() {
return Some(List::empty());
return List::empty();
}
tcx.interners
.$set
.contains_pointer_to(&InternedInSet(self))
.then(|| unsafe { mem::transmute(self) })
assert!(tcx.interners.$set.contains_pointer_to(&InternedInSet(self)));
// SAFETY: we just checked that `self` is interned and therefore is valid for the
// entire lifetime of the `TyCtxt`.
unsafe { mem::transmute(self) }
}
}
};
Expand Down
17 changes: 5 additions & 12 deletions compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,8 @@ impl<'tcx> TyCtxt<'tcx> {
T: Copy + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
{
let mut type_limit = 50;
let regular = FmtPrinter::print_string(self, ns, |p| {
self.lift(t).expect("could not lift for printing").print(p)
})
.expect("could not write to `String`");
let regular = FmtPrinter::print_string(self, ns, |p| self.lift(t).print(p))
.expect("could not write to `String`");
if regular.len() <= length_limit {
return regular;
}
Expand All @@ -235,10 +233,7 @@ impl<'tcx> TyCtxt<'tcx> {
// Look for the longest properly trimmed path that still fits in length_limit.
short = with_forced_trimmed_paths!({
let mut p = FmtPrinter::new_with_limit(self, ns, Limit(type_limit));
self.lift(t)
.expect("could not lift for printing")
.print(&mut p)
.expect("could not print type");
self.lift(t).print(&mut p).expect("could not print type");
p.into_buffer()
});
if short.len() <= length_limit || type_limit == 0 {
Expand Down Expand Up @@ -273,10 +268,8 @@ impl<'tcx> TyCtxt<'tcx> {
where
T: Copy + Hash + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
{
let regular = FmtPrinter::print_string(self, namespace, |p| {
self.lift(t).expect("could not lift for printing").print(p)
})
.expect("could not write to `String`");
let regular = FmtPrinter::print_string(self, namespace, |p| self.lift(t).print(p))
.expect("could not write to `String`");

if !self.sess.opts.unstable_opts.write_long_types_to_disk || self.sess.opts.verbose {
return regular;
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ impl<'tcx> GenericArg<'tcx> {
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {
type Lifted = GenericArg<'tcx>;

fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
match self.kind() {
GenericArgKind::Lifetime(lt) => tcx.lift(lt).map(|lt| lt.into()),
GenericArgKind::Type(ty) => tcx.lift(ty).map(|ty| ty.into()),
GenericArgKind::Const(ct) => tcx.lift(ct).map(|ct| ct.into()),
GenericArgKind::Lifetime(lt) => tcx.lift(lt).into(),
GenericArgKind::Type(ty) => tcx.lift(ty).into(),
GenericArgKind::Const(ct) => tcx.lift(ct).into(),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
let instance = tcx.lift(*self).expect("could not lift for printing");
instance.print(&mut p)?;
tcx.lift(*self).print(&mut p)?;
let s = p.into_buffer();
f.write_str(&s)
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ where
fn print(t: &T, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
ty::tls::with(|tcx| {
let mut p = FmtPrinter::new(tcx, Namespace::TypeNS);
tcx.lift(*t).expect("could not lift for printing").print(&mut p)?;
tcx.lift(*t).print(&mut p)?;
fmt.write_str(&p.into_buffer())?;
Ok(())
})
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2082,10 +2082,9 @@ pub(crate) fn pretty_print_const<'tcx>(
print_types: bool,
) -> fmt::Result {
ty::tls::with(|tcx| {
let literal = tcx.lift(c).unwrap();
let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
p.print_alloc_ids = true;
p.pretty_print_const(literal, print_types)?;
p.pretty_print_const(tcx.lift(c), print_types)?;
fmt.write_str(&p.into_buffer())?;
Ok(())
})
Expand Down Expand Up @@ -3098,7 +3097,6 @@ macro_rules! forward_display_to_print {
ty::tls::with(|tcx| {
let mut p = FmtPrinter::new(tcx, Namespace::TypeNS);
tcx.lift(*self)
.expect("could not lift for printing")
.print(&mut p)?;
f.write_str(&p.into_buffer())?;
Ok(())
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,20 +271,17 @@ TrivialTypeTraversalAndLiftImpls! {

impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
type Lifted = Option<T::Lifted>;
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
Some(match self {
Some(x) => Some(tcx.lift(x)?),
None => None,
})
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
self.map(|x| tcx.lift(x))
}
}

impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
type Lifted = ty::Term<'tcx>;
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Self::Lifted {
match self.kind() {
TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
TermKind::Const(c) => tcx.lift(c).map(Into::into),
TermKind::Ty(ty) => tcx.lift(ty).into(),
TermKind::Const(c) => tcx.lift(c).into(),
}
}
}
Expand Down
Loading
Loading