Skip to content

Commit e7dd3eb

Browse files

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+485
-395
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ __pycache__/
1212
.project
1313
.settings/
1414
.valgrindrc
15-
.vscode/
15+
.vscode
1616
.favorites.json
1717
/*-*-*-*/
1818
/*-*-*/

src/librustc/infer/error_reporting/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
462462
type Region = !;
463463
type Type = !;
464464
type DynExistential = !;
465+
type Const = !;
465466

466467
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx> {
467468
self.tcx
@@ -488,6 +489,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
488489
Err(NonTrivialPath)
489490
}
490491

492+
fn print_const(
493+
self,
494+
_ct: &'tcx ty::Const<'tcx>,
495+
) -> Result<Self::Const, Self::Error> {
496+
Err(NonTrivialPath)
497+
}
498+
491499
fn path_crate(
492500
self,
493501
cnum: CrateNum,

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
260260

261261
ConstValue::Param(_) |
262262
ConstValue::Scalar(_) |
263-
ConstValue::Slice(..) |
263+
ConstValue::Slice { .. } |
264264
ConstValue::ByRef(..) |
265265
ConstValue::Unevaluated(..) => {}
266266
}

src/librustc/lint/context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
791791
type Region = ();
792792
type Type = ();
793793
type DynExistential = ();
794+
type Const = ();
794795

795796
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
796797
self.tcx
@@ -807,7 +808,14 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
807808
fn print_dyn_existential(
808809
self,
809810
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
810-
) -> Result<Self::DynExistential, Self::Error> {
811+
) -> Result<Self::DynExistential, Self::Error> {
812+
Ok(())
813+
}
814+
815+
fn print_const(
816+
self,
817+
_ct: &'tcx ty::Const<'tcx>,
818+
) -> Result<Self::Const, Self::Error> {
811819
Ok(())
812820
}
813821

src/librustc/mir/interpret/error.rs

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

4040
pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
41-
pub type ConstEvalResult<'tcx> = Result<ty::Const<'tcx>, ErrorHandled>;
41+
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
4242

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

src/librustc/mir/interpret/value.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ pub enum ConstValue<'tcx> {
3535
/// Not using the enum `Value` to encode that this must not be `Undef`.
3636
Scalar(Scalar),
3737

38-
/// Used only for slices and strings (`&[T]`, `&str`, `*const [T]`, `*mut str`, `Box<str>`,
39-
/// etc.).
40-
///
41-
/// Empty slices don't necessarily have an address backed by an `AllocId`, thus we also need to
42-
/// enable integer pointers. The `Scalar` type covers exactly those two cases. While we could
43-
/// create dummy-`AllocId`s, the additional code effort for the conversions doesn't seem worth
44-
/// it.
45-
Slice(Scalar, u64),
38+
/// Used only for `&[u8]` and `&str`
39+
Slice {
40+
data: &'tcx Allocation,
41+
start: usize,
42+
end: usize,
43+
},
4644

4745
/// An allocation together with a pointer into the allocation.
4846
/// Invariant: the pointer's `AllocId` resolves to the allocation.
@@ -54,7 +52,7 @@ pub enum ConstValue<'tcx> {
5452
}
5553

5654
#[cfg(target_arch = "x86_64")]
57-
static_assert!(CONST_SIZE: ::std::mem::size_of::<ConstValue<'static>>() == 40);
55+
static_assert!(CONST_SIZE: ::std::mem::size_of::<ConstValue<'static>>() == 32);
5856

5957
impl<'tcx> ConstValue<'tcx> {
6058
#[inline]
@@ -65,7 +63,7 @@ impl<'tcx> ConstValue<'tcx> {
6563
ConstValue::Placeholder(_) |
6664
ConstValue::ByRef(..) |
6765
ConstValue::Unevaluated(..) |
68-
ConstValue::Slice(..) => None,
66+
ConstValue::Slice { .. } => None,
6967
ConstValue::Scalar(val) => Some(val),
7068
}
7169
}
@@ -79,14 +77,6 @@ impl<'tcx> ConstValue<'tcx> {
7977
pub fn try_to_ptr(&self) -> Option<Pointer> {
8078
self.try_to_scalar()?.to_ptr().ok()
8179
}
82-
83-
#[inline]
84-
pub fn new_slice(
85-
val: Scalar,
86-
len: u64,
87-
) -> Self {
88-
ConstValue::Slice(val, len)
89-
}
9080
}
9181

9282
/// A `Scalar` represents an immediate, primitive value existing outside of a

src/librustc/mir/mod.rs

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use crate::hir::def_id::DefId;
99
use crate::hir::{self, InlineAsm as HirInlineAsm};
1010
use crate::mir::interpret::{ConstValue, InterpError, Scalar};
1111
use crate::mir::visit::MirVisitable;
12-
use rustc_apfloat::ieee::{Double, Single};
13-
use rustc_apfloat::Float;
1412
use rustc_data_structures::fx::FxHashSet;
1513
use rustc_data_structures::graph::dominators::{dominators, Dominators};
1614
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
@@ -21,13 +19,13 @@ use rustc_macros::HashStable;
2119
use crate::rustc_serialize::{self as serialize};
2220
use smallvec::SmallVec;
2321
use std::borrow::Cow;
24-
use std::fmt::{self, Debug, Formatter, Write};
22+
use std::fmt::{self, Debug, Formatter, Write, Display};
2523
use std::iter::FusedIterator;
2624
use std::ops::{Index, IndexMut};
2725
use std::slice;
2826
use std::vec::IntoIter;
2927
use std::{iter, mem, option, u32};
30-
use syntax::ast::{self, Name};
28+
use syntax::ast::Name;
3129
use syntax::symbol::{InternedString, Symbol};
3230
use syntax_pos::{Span, DUMMY_SP};
3331
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
@@ -1655,28 +1653,25 @@ impl<'tcx> TerminatorKind<'tcx> {
16551653
switch_ty,
16561654
..
16571655
} => {
1658-
let size = ty::tls::with(|tcx| {
1656+
ty::tls::with(|tcx| {
16591657
let param_env = ty::ParamEnv::empty();
16601658
let switch_ty = tcx.lift_to_global(&switch_ty).unwrap();
1661-
tcx.layout_of(param_env.and(switch_ty)).unwrap().size
1662-
});
1663-
values
1664-
.iter()
1665-
.map(|&u| {
1666-
let mut s = String::new();
1667-
let c = ty::Const {
1668-
val: ConstValue::Scalar(
1669-
Scalar::Bits {
1670-
bits: u,
1671-
size: size.bytes() as u8,
1672-
}.into(),
1673-
),
1674-
ty: switch_ty,
1675-
};
1676-
fmt_const_val(&mut s, c).unwrap();
1677-
s.into()
1678-
}).chain(iter::once("otherwise".into()))
1679-
.collect()
1659+
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
1660+
values
1661+
.iter()
1662+
.map(|&u| {
1663+
tcx.mk_const(ty::Const {
1664+
val: ConstValue::Scalar(
1665+
Scalar::Bits {
1666+
bits: u,
1667+
size: size.bytes() as u8,
1668+
}.into(),
1669+
),
1670+
ty: switch_ty,
1671+
}).to_string().into()
1672+
}).chain(iter::once("otherwise".into()))
1673+
.collect()
1674+
})
16801675
}
16811676
Call {
16821677
destination: Some(_),
@@ -2332,9 +2327,7 @@ impl<'tcx> Operand<'tcx> {
23322327
span,
23332328
ty,
23342329
user_ty: None,
2335-
literal: tcx.mk_const(
2336-
ty::Const::zero_sized(ty),
2337-
),
2330+
literal: ty::Const::zero_sized(tcx, ty),
23382331
})
23392332
}
23402333

@@ -2828,67 +2821,15 @@ newtype_index! {
28282821

28292822
impl<'tcx> Debug for Constant<'tcx> {
28302823
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2831-
write!(fmt, "const ")?;
2832-
fmt_const_val(fmt, *self.literal)
2833-
}
2834-
}
2835-
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2836-
pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Result {
2837-
use crate::ty::TyKind::*;
2838-
let value = const_val.val;
2839-
let ty = const_val.ty;
2840-
// print some primitives
2841-
if let ConstValue::Scalar(Scalar::Bits { bits, .. }) = value {
2842-
match ty.sty {
2843-
Bool if bits == 0 => return write!(f, "false"),
2844-
Bool if bits == 1 => return write!(f, "true"),
2845-
Float(ast::FloatTy::F32) => return write!(f, "{}f32", Single::from_bits(bits)),
2846-
Float(ast::FloatTy::F64) => return write!(f, "{}f64", Double::from_bits(bits)),
2847-
Uint(ui) => return write!(f, "{:?}{}", bits, ui),
2848-
Int(i) => {
2849-
let bit_width = ty::tls::with(|tcx| {
2850-
let ty = tcx.lift_to_global(&ty).unwrap();
2851-
tcx.layout_of(ty::ParamEnv::empty().and(ty))
2852-
.unwrap()
2853-
.size
2854-
.bits()
2855-
});
2856-
let shift = 128 - bit_width;
2857-
return write!(f, "{:?}{}", ((bits as i128) << shift) >> shift, i);
2858-
}
2859-
Char => return write!(f, "{:?}", ::std::char::from_u32(bits as u32).unwrap()),
2860-
_ => {}
2861-
}
2824+
write!(fmt, "{}", self)
28622825
}
2863-
// print function definitions
2864-
if let FnDef(did, _) = ty.sty {
2865-
return write!(f, "{}", def_path_str(did));
2866-
}
2867-
// print string literals
2868-
if let ConstValue::Slice(ptr, len) = value {
2869-
if let Scalar::Ptr(ptr) = ptr {
2870-
if let Ref(_, &ty::TyS { sty: Str, .. }, _) = ty.sty {
2871-
return ty::tls::with(|tcx| {
2872-
let alloc = tcx.alloc_map.lock().get(ptr.alloc_id);
2873-
if let Some(interpret::AllocKind::Memory(alloc)) = alloc {
2874-
assert_eq!(len as usize as u64, len);
2875-
let slice =
2876-
&alloc.bytes[(ptr.offset.bytes() as usize)..][..(len as usize)];
2877-
let s = ::std::str::from_utf8(slice).expect("non utf8 str from miri");
2878-
write!(f, "{:?}", s)
2879-
} else {
2880-
write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len)
2881-
}
2882-
});
2883-
}
2884-
}
2885-
}
2886-
// just raw dump everything else
2887-
write!(f, "{:?} : {}", value, ty)
28882826
}
28892827

2890-
fn def_path_str(def_id: DefId) -> String {
2891-
ty::tls::with(|tcx| tcx.def_path_str(def_id))
2828+
impl<'tcx> Display for Constant<'tcx> {
2829+
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2830+
write!(fmt, "const ")?;
2831+
write!(fmt, "{}", self.literal)
2832+
}
28922833
}
28932834

28942835
impl<'tcx> graph::DirectedGraph for Mir<'tcx> {

src/librustc/traits/project.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
412412
};
413413
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
414414
let substs = tcx.lift_to_global(&substs).unwrap();
415-
let evaluated = tcx.mk_const(evaluated);
416415
let evaluated = evaluated.subst(tcx, substs);
417416
return evaluated;
418417
}
@@ -426,7 +425,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
426425
promoted: None
427426
};
428427
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
429-
return tcx.mk_const(evaluated);
428+
return evaluated;
430429
}
431430
}
432431
}

src/librustc/traits/query/normalize.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
202202
};
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204204
let substs = tcx.lift_to_global(&substs).unwrap();
205-
let evaluated = tcx.mk_const(evaluated);
206205
let evaluated = evaluated.subst(tcx, substs);
207206
return evaluated;
208207
}
@@ -216,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
216215
promoted: None,
217216
};
218217
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
219-
return tcx.mk_const(evaluated);
218+
return evaluated;
220219
}
221220
}
222221
}

src/librustc/ty/context.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::middle::lang_items;
2424
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2525
use crate::middle::stability;
2626
use crate::mir::{self, Mir, interpret, ProjectionKind};
27-
use crate::mir::interpret::{ConstValue, Allocation};
27+
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
2828
use crate::ty::subst::{Kind, InternalSubsts, SubstsRef, Subst};
2929
use crate::ty::ReprOptions;
3030
use crate::traits;
@@ -996,7 +996,10 @@ impl<'tcx> CommonConsts<'tcx> {
996996
};
997997

998998
CommonConsts {
999-
err: mk_const(ty::Const::zero_sized(types.err)),
999+
err: mk_const(ty::Const {
1000+
val: ConstValue::Scalar(Scalar::Bits { bits: 0, size: 0 }),
1001+
ty: types.err,
1002+
}),
10001003
}
10011004
}
10021005
}
@@ -1810,14 +1813,6 @@ nop_list_lift!{ProjectionKind => ProjectionKind}
18101813
// this is the impl for `&'a InternalSubsts<'a>`
18111814
nop_list_lift!{Kind<'a> => Kind<'tcx>}
18121815

1813-
impl<'a, 'tcx> Lift<'tcx> for &'a mir::interpret::Allocation {
1814-
type Lifted = &'tcx mir::interpret::Allocation;
1815-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
1816-
assert!(tcx.global_arenas.const_allocs.in_arena(*self as *const _));
1817-
Some(unsafe { mem::transmute(*self) })
1818-
}
1819-
}
1820-
18211816
pub mod tls {
18221817
use super::{GlobalCtxt, TyCtxt, ptr_eq};
18231818

@@ -2582,9 +2577,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25822577

25832578
#[inline]
25842579
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2585-
self.mk_ty(Array(ty, self.mk_const(
2586-
ty::Const::from_usize(self.global_tcx(), n)
2587-
)))
2580+
self.mk_ty(Array(ty, ty::Const::from_usize(self.global_tcx(), n)))
25882581
}
25892582

25902583
#[inline]

src/librustc/ty/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
939939
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
940940
let flags = FlagComputation::for_const(c);
941941
debug!("HasTypeFlagsVisitor: c={:?} c.flags={:?} self.flags={:?}", c, flags, self.flags);
942-
flags.intersects(self.flags) || c.super_visit_with(self)
942+
flags.intersects(self.flags)
943943
}
944944
}
945945

0 commit comments

Comments
 (0)