Skip to content

Commit 37a0df3

Browse files
committed
<&'tcx ty::Const as Deref>::deref
1 parent bb9f717 commit 37a0df3

File tree

20 files changed

+86
-108
lines changed

20 files changed

+86
-108
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ErrorHandled {
3737
}
3838

3939
pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
40-
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
40+
pub type ConstEvalResult<'tcx> = Result<ty::Const<'tcx>, ErrorHandled>;
4141

4242
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
4343
pub struct ConstEvalErr<'tcx> {

src/librustc/mir/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ impl<'tcx> TerminatorKind<'tcx> {
16661666
),
16671667
ty: switch_ty,
16681668
};
1669-
fmt_const_val(&mut s, &c).unwrap();
1669+
fmt_const_val(&mut s, c).unwrap();
16701670
s.into()
16711671
}).chain(iter::once("otherwise".into()))
16721672
.collect()
@@ -2155,7 +2155,7 @@ impl<'tcx> Operand<'tcx> {
21552155
ty,
21562156
user_ty: None,
21572157
literal: tcx.intern_lazy_const(
2158-
ty::LazyConst::Evaluated(ty::Const::zero_sized(tcx, ty)),
2158+
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
21592159
),
21602160
})
21612161
}
@@ -2663,14 +2663,14 @@ impl<'tcx> Debug for Constant<'tcx> {
26632663

26642664
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
26652665
pub fn fmt_lazy_const_val(f: &mut impl Write, const_val: &ty::LazyConst<'_>) -> fmt::Result {
2666-
match const_val {
2666+
match *const_val {
26672667
ty::LazyConst::Unevaluated(..) => write!(f, "{:?}", const_val),
26682668
ty::LazyConst::Evaluated(c) => fmt_const_val(f, c),
26692669
}
26702670
}
26712671

26722672
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2673-
pub fn fmt_const_val(f: &mut impl Write, const_val: &ty::Const<'_>) -> fmt::Result {
2673+
pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Result {
26742674
use ty::TyKind::*;
26752675
let value = const_val.val;
26762676
let ty = const_val.ty;

src/librustc/ty/codec.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,6 @@ pub fn decode_canonical_var_infos<'a, 'tcx, D>(decoder: &mut D)
246246
.intern_canonical_var_infos(interned?.as_slice()))
247247
}
248248

249-
#[inline]
250-
pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
251-
-> Result<&'tcx ty::Const<'tcx>, D::Error>
252-
where D: TyDecoder<'a, 'tcx>,
253-
'tcx: 'a,
254-
{
255-
Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
256-
}
257-
258249
#[inline]
259250
pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
260251
-> Result<&'tcx ty::LazyConst<'tcx>, D::Error>
@@ -398,13 +389,6 @@ macro_rules! implement_ty_decoder {
398389
}
399390
}
400391

401-
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
402-
for $DecoderName<$($typaram),*> {
403-
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
404-
decode_const(self)
405-
}
406-
}
407-
408392
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::LazyConst<'tcx>>
409393
for $DecoderName<$($typaram),*> {
410394
fn specialized_decode(&mut self) -> Result<&'tcx ty::LazyConst<'tcx>, Self::Error> {

src/librustc/ty/context.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ pub struct CtxtInterners<'tcx> {
122122
region: InternedSet<'tcx, RegionKind>,
123123
existential_predicates: InternedSet<'tcx, List<ExistentialPredicate<'tcx>>>,
124124
predicates: InternedSet<'tcx, List<Predicate<'tcx>>>,
125-
const_: InternedSet<'tcx, Const<'tcx>>,
126125
clauses: InternedSet<'tcx, List<Clause<'tcx>>>,
127126
goal: InternedSet<'tcx, GoalKind<'tcx>>,
128127
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
@@ -140,7 +139,6 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
140139
existential_predicates: Default::default(),
141140
canonical_var_infos: Default::default(),
142141
predicates: Default::default(),
143-
const_: Default::default(),
144142
clauses: Default::default(),
145143
goal: Default::default(),
146144
goal_list: Default::default(),
@@ -1071,24 +1069,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10711069
}
10721070
}
10731071

1074-
pub fn alloc_const_slice(self, values: &[&'tcx ty::Const<'tcx>])
1075-
-> &'tcx [&'tcx ty::Const<'tcx>] {
1076-
if values.is_empty() {
1077-
&[]
1078-
} else {
1079-
self.interners.arena.alloc_slice(values)
1080-
}
1081-
}
1082-
1083-
pub fn alloc_name_const_slice(self, values: &[(ast::Name, &'tcx ty::Const<'tcx>)])
1084-
-> &'tcx [(ast::Name, &'tcx ty::Const<'tcx>)] {
1085-
if values.is_empty() {
1086-
&[]
1087-
} else {
1088-
self.interners.arena.alloc_slice(values)
1089-
}
1090-
}
1091-
10921072
pub fn intern_const_alloc(
10931073
self,
10941074
alloc: Allocation,
@@ -1833,9 +1813,9 @@ impl<'a, 'tcx> Lift<'tcx> for &'a LazyConst<'a> {
18331813
}
18341814
}
18351815

1836-
impl<'a, 'tcx> Lift<'tcx> for &'a Const<'a> {
1837-
type Lifted = &'tcx Const<'tcx>;
1838-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Const<'tcx>> {
1816+
impl<'a, 'tcx> Lift<'tcx> for &'a mir::interpret::Allocation {
1817+
type Lifted = &'tcx mir::interpret::Allocation;
1818+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
18391819
if tcx.interners.arena.in_arena(*self as *const _) {
18401820
return Some(unsafe { mem::transmute(*self) });
18411821
}
@@ -2516,7 +2496,6 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
25162496

25172497
direct_interners!('tcx,
25182498
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
2519-
const_: mk_const(|c: &Const<'_>| keep_local(&c.ty) || keep_local(&c.val)) -> Const<'tcx>,
25202499
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>
25212500
);
25222501

src/librustc/ty/query/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
136136
}
137137
}
138138

139-
impl<'tcx> Key for &'tcx ty::Const<'tcx> {
139+
impl<'tcx> Key for ty::Const<'tcx> {
140140
fn query_crate(&self) -> CrateNum {
141141
LOCAL_CRATE
142142
}

src/librustc/ty/structural_impls.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ CloneTypeFoldableAndLiftImpls! {
5353
::ty::UniverseIndex,
5454
::ty::Variance,
5555
::syntax_pos::Span,
56-
ConstValue<'tcx>,
5756
}
5857

5958
///////////////////////////////////////////////////////////////////////////
@@ -492,6 +491,26 @@ BraceStructLiftImpl! {
492491
}
493492
}
494493

494+
BraceStructLiftImpl! {
495+
impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
496+
type Lifted = ty::Const<'tcx>;
497+
val, ty
498+
}
499+
}
500+
501+
impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
502+
type Lifted = ConstValue<'tcx>;
503+
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
504+
match *self {
505+
ConstValue::Scalar(x) => Some(ConstValue::Scalar(x)),
506+
ConstValue::ScalarPair(x, y) => Some(ConstValue::ScalarPair(x, y)),
507+
ConstValue::ByRef(x, alloc, z) => Some(ConstValue::ByRef(
508+
x, alloc.lift_to_tcx(tcx)?, z,
509+
)),
510+
}
511+
}
512+
}
513+
495514
///////////////////////////////////////////////////////////////////////////
496515
// TypeFoldable implementations.
497516
//
@@ -1048,17 +1067,27 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
10481067
}
10491068
}
10501069

1051-
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1070+
impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
10521071
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
10531072
let ty = self.ty.fold_with(folder);
10541073
let val = self.val.fold_with(folder);
1055-
folder.tcx().mk_const(ty::Const {
1074+
ty::Const {
10561075
ty,
10571076
val
1058-
})
1077+
}
10591078
}
10601079

10611080
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
10621081
self.ty.visit_with(visitor) || self.val.visit_with(visitor)
10631082
}
10641083
}
1084+
1085+
impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
1086+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
1087+
*self
1088+
}
1089+
1090+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1091+
false
1092+
}
1093+
}

src/librustc/ty/sty.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,13 +2018,11 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
20182018
/// code is monomorphic enough for that.
20192019
pub enum LazyConst<'tcx> {
20202020
Unevaluated(DefId, &'tcx Substs<'tcx>),
2021-
Evaluated(&'tcx Const<'tcx>),
2021+
Evaluated(Const<'tcx>),
20222022
}
20232023

2024-
static_assert!(MEM_SIZE_OF_LAZY_CONST: ::std::mem::size_of::<LazyConst<'_>>() <= 24);
2025-
20262024
impl<'tcx> LazyConst<'tcx> {
2027-
pub fn map_evaluated<R>(self, f: impl FnOnce(&'tcx Const<'tcx>) -> Option<R>) -> Option<R> {
2025+
pub fn map_evaluated<R>(self, f: impl FnOnce(Const<'tcx>) -> Option<R>) -> Option<R> {
20282026
match self {
20292027
LazyConst::Evaluated(c) => f(c),
20302028
LazyConst::Unevaluated(..) => None,
@@ -2050,55 +2048,45 @@ pub struct Const<'tcx> {
20502048
}
20512049

20522050
impl<'tcx> Const<'tcx> {
2053-
#[inline]
2054-
pub fn from_const_value(
2055-
tcx: TyCtxt<'_, '_, 'tcx>,
2056-
val: ConstValue<'tcx>,
2057-
ty: Ty<'tcx>,
2058-
) -> &'tcx Self {
2059-
tcx.mk_const(Const {
2060-
val,
2061-
ty,
2062-
})
2063-
}
2064-
20652051
#[inline]
20662052
pub fn from_scalar(
2067-
tcx: TyCtxt<'_, '_, 'tcx>,
20682053
val: Scalar,
20692054
ty: Ty<'tcx>,
2070-
) -> &'tcx Self {
2071-
Self::from_const_value(tcx, ConstValue::Scalar(val), ty)
2055+
) -> Self {
2056+
Self {
2057+
val: ConstValue::Scalar(val),
2058+
ty,
2059+
}
20722060
}
20732061

20742062
#[inline]
20752063
pub fn from_bits(
20762064
tcx: TyCtxt<'_, '_, 'tcx>,
20772065
bits: u128,
20782066
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
2079-
) -> &'tcx Self {
2067+
) -> Self {
20802068
let ty = tcx.lift_to_global(&ty).unwrap();
20812069
let size = tcx.layout_of(ty).unwrap_or_else(|e| {
20822070
panic!("could not compute layout for {:?}: {:?}", ty, e)
20832071
}).size;
20842072
let shift = 128 - size.bits();
20852073
let truncated = (bits << shift) >> shift;
20862074
assert_eq!(truncated, bits, "from_bits called with untruncated value");
2087-
Self::from_scalar(tcx, Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value)
2075+
Self::from_scalar(Scalar::Bits { bits, size: size.bytes() as u8 }, ty.value)
20882076
}
20892077

20902078
#[inline]
2091-
pub fn zero_sized(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
2092-
Self::from_scalar(tcx, Scalar::Bits { bits: 0, size: 0 }, ty)
2079+
pub fn zero_sized(ty: Ty<'tcx>) -> Self {
2080+
Self::from_scalar(Scalar::Bits { bits: 0, size: 0 }, ty)
20932081
}
20942082

20952083
#[inline]
2096-
pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> &'tcx Self {
2084+
pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> Self {
20972085
Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool))
20982086
}
20992087

21002088
#[inline]
2101-
pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> &'tcx Self {
2089+
pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> Self {
21022090
Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize))
21032091
}
21042092

@@ -2164,5 +2152,4 @@ impl<'tcx> Const<'tcx> {
21642152
}
21652153
}
21662154

2167-
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Const<'tcx> {}
21682155
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx LazyConst<'tcx> {}

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1515
&mut self,
1616
bx: &Bx,
1717
constant: &'tcx ty::LazyConst<'tcx>,
18-
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
18+
) -> Result<ty::Const<'tcx>, ErrorHandled> {
1919
match *constant {
2020
ty::LazyConst::Unevaluated(def_id, ref substs) => {
2121
let tcx = bx.tcx();
@@ -35,7 +35,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3535
&mut self,
3636
bx: &Bx,
3737
constant: &mir::Constant<'tcx>,
38-
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
38+
) -> Result<ty::Const<'tcx>, ErrorHandled> {
3939
let c = self.monomorphize(&constant.literal);
4040
self.fully_evaluate(bx, c)
4141
}
@@ -46,7 +46,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4646
bx: &Bx,
4747
span: Span,
4848
ty: Ty<'tcx>,
49-
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
49+
constant: Result<ty::Const<'tcx>, ErrorHandled>,
5050
) -> (Bx::Value, Ty<'tcx>) {
5151
constant
5252
.and_then(|c| {

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
6767

6868
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
6969
bx: &mut Bx,
70-
val: &'tcx ty::Const<'tcx>
70+
val: ty::Const<'tcx>
7171
) -> Result<Self, ErrorHandled> {
7272
let layout = bx.cx().layout_of(val.ty);
7373

src/librustc_mir/build/matches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,12 @@ enum TestKind<'tcx> {
658658
SwitchInt {
659659
switch_ty: Ty<'tcx>,
660660
options: Vec<u128>,
661-
indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
661+
indices: FxHashMap<ty::Const<'tcx>, usize>,
662662
},
663663

664664
// test for equality
665665
Eq {
666-
value: &'tcx ty::Const<'tcx>,
666+
value: ty::Const<'tcx>,
667667
ty: Ty<'tcx>,
668668
},
669669

0 commit comments

Comments
 (0)