Skip to content

Commit cc60a22

Browse files
committed
Get rid of scalar_size
1 parent 0da702a commit cc60a22

File tree

18 files changed

+108
-85
lines changed

18 files changed

+108
-85
lines changed

src/librustc/mir/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,8 +1906,12 @@ pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Resul
19061906
(Value::Scalar(Scalar::Bits { bits, defined: 64 }), &TyFloat(ast::FloatTy::F64)) =>
19071907
write!(f, "{}f64", Double::from_bits(bits)),
19081908
(Value::Scalar(Scalar::Bits { bits, .. }), &TyUint(ui)) => write!(f, "{:?}{}", bits, ui),
1909-
(Value::Scalar(Scalar::Bits { bits, defined }), &TyInt(i)) => {
1910-
let amt = 128 - defined;
1909+
(Value::Scalar(Scalar::Bits { bits, .. }), &TyInt(i)) => {
1910+
let bit_width = ty::tls::with(|tcx| {
1911+
let ty = tcx.global_tcx().lift(&ty).unwrap();
1912+
tcx.global_tcx().layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size.bits()
1913+
});
1914+
let amt = 128 - bit_width;
19111915
write!(f, "{:?}{}", ((bits as i128) << amt) >> amt, i)
19121916
},
19131917
(Value::Scalar(Scalar::Bits { bits, defined: 32 }), &TyChar) =>

src/librustc/ty/layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,28 +1514,28 @@ impl<'a, 'tcx> LayoutOf for LayoutCx<'tcx, ty::maps::TyCtxtAt<'a, 'tcx, 'tcx>> {
15141514
}
15151515

15161516
// Helper (inherent) `layout_of` methods to avoid pushing `LayoutCx` to users.
1517-
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
1517+
impl<'a, 'tcx, 'empty> TyCtxt<'a, 'tcx, 'empty> {
15181518
/// Computes the layout of a type. Note that this implicitly
15191519
/// executes in "reveal all" mode.
15201520
#[inline]
15211521
pub fn layout_of(self, param_env_and_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
15221522
-> Result<TyLayout<'tcx>, LayoutError<'tcx>> {
15231523
let cx = LayoutCx {
1524-
tcx: self,
1524+
tcx: self.global_tcx(),
15251525
param_env: param_env_and_ty.param_env
15261526
};
15271527
cx.layout_of(param_env_and_ty.value)
15281528
}
15291529
}
15301530

1531-
impl<'a, 'tcx> ty::maps::TyCtxtAt<'a, 'tcx, 'tcx> {
1531+
impl<'a, 'tcx, 'empty> ty::maps::TyCtxtAt<'a, 'tcx, 'empty> {
15321532
/// Computes the layout of a type. Note that this implicitly
15331533
/// executes in "reveal all" mode.
15341534
#[inline]
15351535
pub fn layout_of(self, param_env_and_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
15361536
-> Result<TyLayout<'tcx>, LayoutError<'tcx>> {
15371537
let cx = LayoutCx {
1538-
tcx: self,
1538+
tcx: self.global_tcx().at(self.span),
15391539
param_env: param_env_and_ty.param_env
15401540
};
15411541
cx.layout_of(param_env_and_ty.value)

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
19821982
match tcx.const_eval(param_env.and(cid)) {
19831983
Ok(val) => {
19841984
// FIXME: Find the right type and use it instead of `val.ty` here
1985-
if let Some(b) = val.assert_bits(tcx, val.ty) {
1985+
if let Some(b) = val.assert_bits(tcx.global_tcx(), param_env.and(val.ty)) {
19861986
trace!("discriminants: {} ({:?})", b, repr_type);
19871987
Some(Discr {
19881988
val: b,

src/librustc/ty/sty.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ use middle::region;
1717
use rustc_data_structures::indexed_vec::Idx;
1818
use ty::subst::{Substs, Subst, Kind, UnpackedKind};
1919
use ty::{self, AdtDef, TypeFlags, Ty, TyCtxt, TypeFoldable};
20-
use ty::{Slice, TyS, layout};
20+
use ty::{Slice, TyS, ParamEnvAnd, ParamEnv};
2121
use util::captures::Captures;
2222
use mir::interpret::{Scalar, Pointer, Value, ConstValue};
23-
use rustc_target::abi::{Size, HasDataLayout};
2423

2524
use std::iter;
2625
use std::cmp::Ordering;
2726
use rustc_target::spec::abi;
2827
use syntax::ast::{self, Name};
2928
use syntax::symbol::{keywords, InternedString};
30-
use syntax::attr;
3129

3230
use serialize;
3331

@@ -1757,25 +1755,6 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
17571755
_ => bug!("cannot convert type `{:?}` to a closure kind", self),
17581756
}
17591757
}
1760-
1761-
/// If this type is a scalar, compute its size without
1762-
/// going through `tcx.layout_of`
1763-
pub fn scalar_size<C: HasDataLayout>(
1764-
&self,
1765-
cx: C,
1766-
) -> Option<Size> {
1767-
let ty = match self.sty {
1768-
ty::TyBool => return Some(Size::from_bytes(1)),
1769-
ty::TyChar => return Some(Size::from_bytes(4)),
1770-
ty::TyInt(ity) => attr::IntType::SignedInt(ity),
1771-
ty::TyUint(uty) => attr::IntType::UnsignedInt(uty),
1772-
ty::TyFloat(ast::FloatTy::F32) => return Some(Size::from_bytes(4)),
1773-
ty::TyFloat(ast::FloatTy::F64) => return Some(Size::from_bytes(8)),
1774-
_ => return None,
1775-
};
1776-
use ty::layout::IntegerExt;
1777-
Some(layout::Integer::from_attr(cx, ty).size())
1778-
}
17791758
}
17801759

17811760
/// Typed constant value.
@@ -1842,12 +1821,13 @@ impl<'tcx> Const<'tcx> {
18421821
pub fn from_bits(
18431822
tcx: TyCtxt<'_, '_, 'tcx>,
18441823
bits: u128,
1845-
ty: Ty<'tcx>,
1824+
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
18461825
) -> &'tcx Self {
1847-
let defined = ty.scalar_size(tcx).unwrap_or_else(|| {
1848-
panic!("non-scalar type in from_bits: {:?}", ty)
1849-
}).bits() as u8;
1850-
Self::from_scalar(tcx, Scalar::Bits { bits, defined }, ty)
1826+
let ty = tcx.global_tcx().lift(&ty).unwrap();
1827+
let defined = tcx.global_tcx().layout_of(ty).unwrap_or_else(|e| {
1828+
panic!("could not compute layout for {:?}: {:?}", ty, e)
1829+
}).size.bits() as u8;
1830+
Self::from_scalar(tcx, Scalar::Bits { bits, defined }, ty.value)
18511831
}
18521832

18531833
#[inline]
@@ -1857,20 +1837,25 @@ impl<'tcx> Const<'tcx> {
18571837

18581838
#[inline]
18591839
pub fn from_bool(tcx: TyCtxt<'_, '_, 'tcx>, v: bool) -> &'tcx Self {
1860-
Self::from_bits(tcx, v as u128, tcx.types.bool)
1840+
Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool))
18611841
}
18621842

18631843
#[inline]
18641844
pub fn from_usize(tcx: TyCtxt<'_, '_, 'tcx>, n: u64) -> &'tcx Self {
1865-
Self::from_bits(tcx, n as u128, tcx.types.usize)
1845+
Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize))
18661846
}
18671847

18681848
#[inline]
1869-
pub fn to_bits<C: HasDataLayout>(&self, cx: C, ty: Ty<'tcx>) -> Option<u128> {
1870-
if self.ty != ty {
1849+
pub fn to_bits(
1850+
&self,
1851+
tcx: TyCtxt<'_, '_, 'tcx>,
1852+
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
1853+
) -> Option<u128> {
1854+
if self.ty != ty.value {
18711855
return None;
18721856
}
1873-
let size = ty.scalar_size(cx)?;
1857+
let ty = tcx.global_tcx().lift(&ty).unwrap();
1858+
let size = tcx.global_tcx().layout_of(ty).ok()?.size;
18741859
match self.val {
18751860
ConstVal::Value(val) => val.to_bits(size),
18761861
_ => None,
@@ -1902,9 +1887,14 @@ impl<'tcx> Const<'tcx> {
19021887
}
19031888

19041889
#[inline]
1905-
pub fn assert_bits<C: HasDataLayout>(&self, cx: C, ty: Ty<'tcx>) -> Option<u128> {
1906-
assert_eq!(self.ty, ty);
1907-
let size = ty.scalar_size(cx)?;
1890+
pub fn assert_bits(
1891+
&self,
1892+
tcx: TyCtxt<'_, '_, '_>,
1893+
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
1894+
) -> Option<u128> {
1895+
assert_eq!(self.ty, ty.value);
1896+
let ty = tcx.global_tcx().lift(&ty).unwrap();
1897+
let size = tcx.global_tcx().layout_of(ty).ok()?.size;
19081898
match self.val {
19091899
ConstVal::Value(val) => val.to_bits(size),
19101900
_ => None,
@@ -1913,7 +1903,7 @@ impl<'tcx> Const<'tcx> {
19131903

19141904
#[inline]
19151905
pub fn assert_bool(&self, tcx: TyCtxt<'_, '_, '_>) -> Option<bool> {
1916-
self.assert_bits(tcx, tcx.types.bool).and_then(|v| match v {
1906+
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.bool)).and_then(|v| match v {
19171907
0 => Some(false),
19181908
1 => Some(true),
19191909
_ => None,
@@ -1922,14 +1912,18 @@ impl<'tcx> Const<'tcx> {
19221912

19231913
#[inline]
19241914
pub fn assert_usize(&self, tcx: TyCtxt<'_, '_, '_>) -> Option<u64> {
1925-
self.assert_bits(tcx, tcx.types.usize).map(|v| v as u64)
1915+
self.assert_bits(tcx, ParamEnv::empty().and(tcx.types.usize)).map(|v| v as u64)
19261916
}
19271917

19281918
#[inline]
1929-
pub fn unwrap_bits(&self, tcx: TyCtxt<'_, '_, '_>, ty: Ty<'tcx>) -> u128 {
1919+
pub fn unwrap_bits(
1920+
&self,
1921+
tcx: TyCtxt<'_, '_, '_>,
1922+
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
1923+
) -> u128 {
19301924
match self.assert_bits(tcx, ty) {
19311925
Some(val) => val,
1932-
None => bug!("expected bits of {}, got {:#?}", ty, self),
1926+
None => bug!("expected bits of {}, got {:#?}", ty.value, self),
19331927
}
19341928
}
19351929

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
202202
value: ty::Const::from_bits(
203203
this.hir.tcx(),
204204
0,
205-
this.hir.tcx().types.u32),
205+
ty::ParamEnv::empty().and(this.hir.tcx().types.u32)),
206206
},
207207
}));
208208
box AggregateKind::Generator(closure_id, substs, movability)
@@ -374,22 +374,26 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
374374

375375
// Helper to get a `-1` value of the appropriate type
376376
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
377-
let bits = ty.scalar_size(self.hir.tcx()).expect("neg_1_literal expects integers").bits();
377+
let gcx = self.hir.tcx().global_tcx();
378+
let param_ty = ty::ParamEnv::empty().and(gcx.lift(&ty).unwrap());
379+
let bits = gcx.layout_of(param_ty).unwrap().size.bits();
378380
let n = (!0u128) >> (128 - bits);
379381
let literal = Literal::Value {
380-
value: ty::Const::from_bits(self.hir.tcx(), n, ty)
382+
value: ty::Const::from_bits(self.hir.tcx(), n, param_ty)
381383
};
382384

383385
self.literal_operand(span, ty, literal)
384386
}
385387

386388
// Helper to get the minimum value of the appropriate type
387389
fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
390+
let gcx = self.hir.tcx().global_tcx();
388391
assert!(ty.is_signed());
389-
let bits = ty.scalar_size(self.hir.tcx()).expect("minval_literal expects integers").bits();
392+
let param_ty = ty::ParamEnv::empty().and(gcx.lift(&ty).unwrap());
393+
let bits = gcx.layout_of(param_ty).unwrap().size.bits();
390394
let n = 1 << (bits - 1);
391395
let literal = Literal::Value {
392-
value: ty::Const::from_bits(self.hir.tcx(), n, ty)
396+
value: ty::Const::from_bits(self.hir.tcx(), n, param_ty)
393397
};
394398

395399
self.literal_operand(span, ty, literal)

src/librustc_mir/build/matches/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
122122

123123
match *match_pair.pattern.kind {
124124
PatternKind::Constant { value } => {
125+
let switch_ty = ty::ParamEnv::empty().and(switch_ty);
125126
indices.entry(value)
126127
.or_insert_with(|| {
127128
options.push(value.unwrap_bits(self.hir.tcx(), switch_ty));

src/librustc_mir/build/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
5353
// bool, char and integers.
5454
pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
5555
let literal = Literal::Value {
56-
value: ty::Const::from_bits(self.hir.tcx(), 0, ty)
56+
value: ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty))
5757
};
5858

5959
self.literal_operand(span, ty, literal)

src/librustc_mir/hair/cx/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,11 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
635635
},
636636
},
637637
}.to_ref();
638-
let offset = mk_const(ty::Const::from_bits(cx.tcx, offset as u128, ty));
638+
let offset = mk_const(ty::Const::from_bits(
639+
cx.tcx,
640+
offset as u128,
641+
cx.param_env.and(ty),
642+
));
639643
match did {
640644
Some(did) => {
641645
// in case we are offsetting from a computed discriminant

src/librustc_mir/hair/cx/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,16 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
156156
};
157157

158158
let clamp = |n| {
159-
let size = ty.scalar_size(self.tcx).expect("const_eval_lit::clamp expects ints").bits();
160-
trace!("clamp {} with size {} and amt {}", n, size, 128 - size);
161-
let amt = 128 - size;
159+
let gcx = self.tcx.global_tcx();
160+
let param_ty = self.param_env.and(gcx.lift(&ty).unwrap());
161+
let bit_width = gcx.layout_of(param_ty).unwrap().size.bits();
162+
trace!("clamp {} with size {} and amt {}", n, bit_width, 128 - bit_width);
163+
let amt = 128 - bit_width;
162164
let result = (n << amt) >> amt;
163165
trace!("clamp result: {}", result);
164166
ConstValue::Scalar(Scalar::Bits {
165167
bits: result,
166-
defined: size as u8,
168+
defined: bit_width as u8,
167169
})
168170
};
169171

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
198198
value: ty::Const::from_bits(
199199
tcx,
200200
*b as u128,
201-
tcx.types.u8)
201+
ty::ParamEnv::empty().and(tcx.types.u8))
202202
}
203203
})
204204
}).collect()
@@ -958,7 +958,7 @@ fn slice_pat_covered_by_constructor<'tcx>(
958958
{
959959
match pat.kind {
960960
box PatternKind::Constant { value } => {
961-
let b = value.unwrap_bits(tcx, pat.ty);
961+
let b = value.unwrap_bits(tcx, ty::ParamEnv::empty().and(pat.ty));
962962
assert_eq!(b as u8 as u128, b);
963963
if b as u8 != *ch {
964964
return Ok(false);
@@ -979,9 +979,9 @@ fn constructor_covered_by_range<'a, 'tcx>(
979979
ty: Ty<'tcx>,
980980
) -> Result<bool, ErrorReported> {
981981
trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);
982-
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty)
982+
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, ty::ParamEnv::empty().and(ty))
983983
.map(|res| res != Ordering::Less);
984-
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty);
984+
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, ty::ParamEnv::empty().and(ty));
985985
macro_rules! some_or_ok {
986986
($e:expr) => {
987987
match $e {

0 commit comments

Comments
 (0)