Skip to content

Commit f492693

Browse files

Some content is hidden

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

51 files changed

+488
-397
lines changed

.gitignore

+1-1
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

+8
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

+1-1
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

+9-1
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

+1-1
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

+8-18
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_size!(ConstValue<'_>, 40);
55+
static_assert_size!(ConstValue<'_>, 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

+26-85
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};
@@ -1662,28 +1660,25 @@ impl<'tcx> TerminatorKind<'tcx> {
16621660
switch_ty,
16631661
..
16641662
} => {
1665-
let size = ty::tls::with(|tcx| {
1663+
ty::tls::with(|tcx| {
16661664
let param_env = ty::ParamEnv::empty();
16671665
let switch_ty = tcx.lift_to_global(&switch_ty).unwrap();
1668-
tcx.layout_of(param_env.and(switch_ty)).unwrap().size
1669-
});
1670-
values
1671-
.iter()
1672-
.map(|&u| {
1673-
let mut s = String::new();
1674-
let c = ty::Const {
1675-
val: ConstValue::Scalar(
1676-
Scalar::Bits {
1677-
bits: u,
1678-
size: size.bytes() as u8,
1679-
}.into(),
1680-
),
1681-
ty: switch_ty,
1682-
};
1683-
fmt_const_val(&mut s, c).unwrap();
1684-
s.into()
1685-
}).chain(iter::once("otherwise".into()))
1686-
.collect()
1666+
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
1667+
values
1668+
.iter()
1669+
.map(|&u| {
1670+
tcx.mk_const(ty::Const {
1671+
val: ConstValue::Scalar(
1672+
Scalar::Bits {
1673+
bits: u,
1674+
size: size.bytes() as u8,
1675+
}.into(),
1676+
),
1677+
ty: switch_ty,
1678+
}).to_string().into()
1679+
}).chain(iter::once("otherwise".into()))
1680+
.collect()
1681+
})
16871682
}
16881683
Call {
16891684
destination: Some(_),
@@ -2331,9 +2326,7 @@ impl<'tcx> Operand<'tcx> {
23312326
span,
23322327
ty,
23332328
user_ty: None,
2334-
literal: tcx.mk_const(
2335-
ty::Const::zero_sized(ty),
2336-
),
2329+
literal: ty::Const::zero_sized(tcx, ty),
23372330
})
23382331
}
23392332

@@ -2827,67 +2820,15 @@ newtype_index! {
28272820

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

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

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

src/librustc/traits/project.rs

+1-2
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

+1-2
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

+6-13
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;
@@ -1000,7 +1000,10 @@ impl<'tcx> CommonConsts<'tcx> {
10001000
};
10011001

10021002
CommonConsts {
1003-
err: mk_const(ty::Const::zero_sized(types.err)),
1003+
err: mk_const(ty::Const {
1004+
val: ConstValue::Scalar(Scalar::Bits { bits: 0, size: 0 }),
1005+
ty: types.err,
1006+
}),
10041007
}
10051008
}
10061009
}
@@ -1822,14 +1825,6 @@ nop_list_lift!{ProjectionKind => ProjectionKind}
18221825
// this is the impl for `&'a InternalSubsts<'a>`
18231826
nop_list_lift!{Kind<'a> => Kind<'tcx>}
18241827

1825-
impl<'a, 'tcx> Lift<'tcx> for &'a mir::interpret::Allocation {
1826-
type Lifted = &'tcx mir::interpret::Allocation;
1827-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
1828-
assert!(tcx.global_arenas.const_allocs.in_arena(*self as *const _));
1829-
Some(unsafe { mem::transmute(*self) })
1830-
}
1831-
}
1832-
18331828
pub mod tls {
18341829
use super::{GlobalCtxt, TyCtxt, ptr_eq};
18351830

@@ -2594,9 +2589,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25942589

25952590
#[inline]
25962591
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2597-
self.mk_ty(Array(ty, self.mk_const(
2598-
ty::Const::from_usize(self.global_tcx(), n)
2599-
)))
2592+
self.mk_ty(Array(ty, ty::Const::from_usize(self.global_tcx(), n)))
26002593
}
26012594

26022595
#[inline]

src/librustc/ty/fold.rs

+1-1
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)