Skip to content

Commit e7a4882

Browse files
committed
rustc_const_eval: always demand typeck_tables for evaluating constants.
1 parent c832e6f commit e7a4882

Some content is hidden

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

60 files changed

+705
-1221
lines changed

src/librustc/diagnostics.rs

-73
Original file line numberDiff line numberDiff line change
@@ -390,44 +390,6 @@ RFC. It is, however, [currently unimplemented][iss15872].
390390
[iss15872]: https://github.com/rust-lang/rust/issues/15872
391391
"##,
392392

393-
E0109: r##"
394-
You tried to give a type parameter to a type which doesn't need it. Erroneous
395-
code example:
396-
397-
```compile_fail,E0109
398-
type X = u32<i32>; // error: type parameters are not allowed on this type
399-
```
400-
401-
Please check that you used the correct type and recheck its definition. Perhaps
402-
it doesn't need the type parameter.
403-
404-
Example:
405-
406-
```
407-
type X = u32; // this compiles
408-
```
409-
410-
Note that type parameters for enum-variant constructors go after the variant,
411-
not after the enum (Option::None::<u32>, not Option::<u32>::None).
412-
"##,
413-
414-
E0110: r##"
415-
You tried to give a lifetime parameter to a type which doesn't need it.
416-
Erroneous code example:
417-
418-
```compile_fail,E0110
419-
type X = u32<'static>; // error: lifetime parameters are not allowed on
420-
// this type
421-
```
422-
423-
Please check that the correct type was used and recheck its definition; perhaps
424-
it doesn't need the lifetime parameter. Example:
425-
426-
```
427-
type X = u32; // ok!
428-
```
429-
"##,
430-
431393
E0133: r##"
432394
Unsafe code was used outside of an unsafe function or block.
433395
@@ -627,41 +589,6 @@ attributes:
627589
See also https://doc.rust-lang.org/book/no-stdlib.html
628590
"##,
629591

630-
E0229: r##"
631-
An associated type binding was done outside of the type parameter declaration
632-
and `where` clause. Erroneous code example:
633-
634-
```compile_fail,E0229
635-
pub trait Foo {
636-
type A;
637-
fn boo(&self) -> <Self as Foo>::A;
638-
}
639-
640-
struct Bar;
641-
642-
impl Foo for isize {
643-
type A = usize;
644-
fn boo(&self) -> usize { 42 }
645-
}
646-
647-
fn baz<I>(x: &<I as Foo<A=Bar>>::A) {}
648-
// error: associated type bindings are not allowed here
649-
```
650-
651-
To solve this error, please move the type bindings in the type parameter
652-
declaration:
653-
654-
```ignore
655-
fn baz<I: Foo<A=Bar>>(x: &<I as Foo>::A) {} // ok!
656-
```
657-
658-
Or in the `where` clause:
659-
660-
```ignore
661-
fn baz<I>(x: &<I as Foo>::A) where I: Foo<A=Bar> {}
662-
```
663-
"##,
664-
665592
E0261: r##"
666593
When using a lifetime like `'a` in a type, it must be declared before being
667594
used.

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub mod infer;
7676
pub mod lint;
7777

7878
pub mod middle {
79-
pub mod astconv_util;
8079
pub mod expr_use_visitor;
8180
pub mod const_val;
8281
pub mod cstore;

src/librustc/middle/astconv_util.rs

-83
This file was deleted.

src/librustc/middle/const_val.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@ use syntax::symbol::InternedString;
1212
use syntax::ast;
1313
use std::rc::Rc;
1414
use hir::def_id::DefId;
15+
use ty::subst::Substs;
1516
use rustc_const_math::*;
17+
1618
use self::ConstVal::*;
1719
pub use rustc_const_math::ConstInt;
1820

1921
use std::collections::BTreeMap;
2022

2123
#[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)]
22-
pub enum ConstVal {
24+
pub enum ConstVal<'tcx> {
2325
Float(ConstFloat),
2426
Integral(ConstInt),
2527
Str(InternedString),
2628
ByteStr(Rc<Vec<u8>>),
2729
Bool(bool),
28-
Function(DefId),
29-
Struct(BTreeMap<ast::Name, ConstVal>),
30-
Tuple(Vec<ConstVal>),
31-
Array(Vec<ConstVal>),
32-
Repeat(Box<ConstVal>, u64),
30+
Function(DefId, &'tcx Substs<'tcx>),
31+
Struct(BTreeMap<ast::Name, ConstVal<'tcx>>),
32+
Tuple(Vec<ConstVal<'tcx>>),
33+
Array(Vec<ConstVal<'tcx>>),
34+
Repeat(Box<ConstVal<'tcx>>, u64),
3335
Char(char),
3436
}
3537

36-
impl ConstVal {
38+
impl<'tcx> ConstVal<'tcx> {
3739
pub fn description(&self) -> &'static str {
3840
match *self {
3941
Float(f) => f.description(),
@@ -43,7 +45,7 @@ impl ConstVal {
4345
Bool(_) => "boolean",
4446
Struct(_) => "struct",
4547
Tuple(_) => "tuple",
46-
Function(_) => "function definition",
48+
Function(..) => "function definition",
4749
Array(..) => "array",
4850
Repeat(..) => "repeat",
4951
Char(..) => "char",
@@ -53,8 +55,7 @@ impl ConstVal {
5355
pub fn to_const_int(&self) -> Option<ConstInt> {
5456
match *self {
5557
ConstVal::Integral(i) => Some(i),
56-
ConstVal::Bool(true) => Some(ConstInt::Infer(1)),
57-
ConstVal::Bool(false) => Some(ConstInt::Infer(0)),
58+
ConstVal::Bool(b) => Some(ConstInt::U8(b as u8)),
5859
ConstVal::Char(ch) => Some(ConstInt::U32(ch as u32)),
5960
_ => None
6061
}

src/librustc/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl<'tcx> Terminator<'tcx> {
542542
impl<'tcx> TerminatorKind<'tcx> {
543543
pub fn if_<'a, 'gcx>(tcx: ty::TyCtxt<'a, 'gcx, 'tcx>, cond: Operand<'tcx>,
544544
t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
545-
static BOOL_SWITCH_FALSE: &'static [ConstInt] = &[ConstInt::Infer(0)];
545+
static BOOL_SWITCH_FALSE: &'static [ConstInt] = &[ConstInt::U8(0)];
546546
TerminatorKind::SwitchInt {
547547
discr: cond,
548548
switch_ty: tcx.types.bool,
@@ -1224,7 +1224,7 @@ pub enum Literal<'tcx> {
12241224
substs: &'tcx Substs<'tcx>,
12251225
},
12261226
Value {
1227-
value: ConstVal,
1227+
value: ConstVal<'tcx>,
12281228
},
12291229
Promoted {
12301230
// Index into the `promoted` vector of `Mir`.
@@ -1271,7 +1271,7 @@ fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
12711271
write!(fmt, "b\"{}\"", escaped)
12721272
}
12731273
Bool(b) => write!(fmt, "{:?}", b),
1274-
Function(def_id) => write!(fmt, "{}", item_path_str(def_id)),
1274+
Function(def_id, _) => write!(fmt, "{}", item_path_str(def_id)),
12751275
Struct(_) | Tuple(_) | Array(_) | Repeat(..) =>
12761276
bug!("ConstVal `{:?}` should not be in MIR", const_val),
12771277
Char(c) => write!(fmt, "{:?}", c),

src/librustc/ty/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ pub struct TypeckTables<'tcx> {
244244
/// Set of trait imports actually used in the method resolution.
245245
/// This is used for warning unused imports.
246246
pub used_trait_imports: DefIdSet,
247+
248+
/// If any errors occurred while type-checking this body,
249+
/// this field will be set to `true`.
250+
pub tainted_by_errors: bool,
247251
}
248252

249253
impl<'tcx> TypeckTables<'tcx> {
@@ -262,6 +266,7 @@ impl<'tcx> TypeckTables<'tcx> {
262266
cast_kinds: NodeMap(),
263267
lints: lint::LintTable::new(),
264268
used_trait_imports: DefIdSet(),
269+
tainted_by_errors: false,
265270
}
266271
}
267272

src/librustc/ty/layout.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use ty::{self, Ty, TyCtxt, TypeFoldable, ReprOptions};
2020
use syntax::ast::{FloatTy, IntTy, UintTy};
2121
use syntax::attr;
2222
use syntax_pos::DUMMY_SP;
23-
use rustc_const_math::ConstInt;
2423

2524
use std::cmp;
2625
use std::fmt;
@@ -1183,11 +1182,7 @@ impl<'a, 'gcx, 'tcx> Layout {
11831182
i64::min_value(),
11841183
true);
11851184
for discr in def.discriminants(tcx) {
1186-
let x = match discr.erase_type() {
1187-
ConstInt::InferSigned(i) => i as i64,
1188-
ConstInt::Infer(i) => i as u64 as i64,
1189-
_ => bug!()
1190-
};
1185+
let x = discr.to_u128_unchecked() as i64;
11911186
if x == 0 { non_zero = false; }
11921187
if x < min { min = x; }
11931188
if x > max { max = x; }

src/librustc/ty/maps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ define_maps! { <'tcx>
381381

382382
/// Results of evaluating monomorphic constants embedded in
383383
/// other items, such as enum variant explicit discriminants.
384-
pub monomorphic_const_eval: MonomorphicConstEval(DefId) -> Result<ConstVal, ()>
384+
pub monomorphic_const_eval: MonomorphicConstEval(DefId) -> Result<ConstVal<'tcx>, ()>
385385
}
386386

387387
fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {

src/librustc/ty/util.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ type Disr = ConstInt;
4747
}
4848

4949

50+
macro_rules! typed_literal {
51+
($tcx:expr, $ty:expr, $lit:expr) => {
52+
match $ty {
53+
SignedInt(ast::IntTy::I8) => ConstInt::I8($lit),
54+
SignedInt(ast::IntTy::I16) => ConstInt::I16($lit),
55+
SignedInt(ast::IntTy::I32) => ConstInt::I32($lit),
56+
SignedInt(ast::IntTy::I64) => ConstInt::I64($lit),
57+
SignedInt(ast::IntTy::I128) => ConstInt::I128($lit),
58+
SignedInt(ast::IntTy::Is) => match $tcx.sess.target.int_type {
59+
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16($lit)),
60+
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32($lit)),
61+
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64($lit)),
62+
_ => bug!(),
63+
},
64+
UnsignedInt(ast::UintTy::U8) => ConstInt::U8($lit),
65+
UnsignedInt(ast::UintTy::U16) => ConstInt::U16($lit),
66+
UnsignedInt(ast::UintTy::U32) => ConstInt::U32($lit),
67+
UnsignedInt(ast::UintTy::U64) => ConstInt::U64($lit),
68+
UnsignedInt(ast::UintTy::U128) => ConstInt::U128($lit),
69+
UnsignedInt(ast::UintTy::Us) => match $tcx.sess.target.uint_type {
70+
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16($lit)),
71+
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32($lit)),
72+
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64($lit)),
73+
_ => bug!(),
74+
},
75+
}
76+
}
77+
}
78+
5079
impl IntTypeExt for attr::IntType {
5180
fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
5281
match *self {
@@ -66,30 +95,7 @@ impl IntTypeExt for attr::IntType {
6695
}
6796

6897
fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr {
69-
match *self {
70-
SignedInt(ast::IntTy::I8) => ConstInt::I8(0),
71-
SignedInt(ast::IntTy::I16) => ConstInt::I16(0),
72-
SignedInt(ast::IntTy::I32) => ConstInt::I32(0),
73-
SignedInt(ast::IntTy::I64) => ConstInt::I64(0),
74-
SignedInt(ast::IntTy::I128) => ConstInt::I128(0),
75-
SignedInt(ast::IntTy::Is) => match tcx.sess.target.int_type {
76-
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16(0)),
77-
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32(0)),
78-
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64(0)),
79-
_ => bug!(),
80-
},
81-
UnsignedInt(ast::UintTy::U8) => ConstInt::U8(0),
82-
UnsignedInt(ast::UintTy::U16) => ConstInt::U16(0),
83-
UnsignedInt(ast::UintTy::U32) => ConstInt::U32(0),
84-
UnsignedInt(ast::UintTy::U64) => ConstInt::U64(0),
85-
UnsignedInt(ast::UintTy::U128) => ConstInt::U128(0),
86-
UnsignedInt(ast::UintTy::Us) => match tcx.sess.target.uint_type {
87-
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(0)),
88-
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(0)),
89-
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(0)),
90-
_ => bug!(),
91-
},
92-
}
98+
typed_literal!(tcx, *self, 0)
9399
}
94100

95101
fn assert_ty_matches(&self, val: Disr) {
@@ -114,7 +120,7 @@ impl IntTypeExt for attr::IntType {
114120
-> Option<Disr> {
115121
if let Some(val) = val {
116122
self.assert_ty_matches(val);
117-
(val + ConstInt::Infer(1)).ok()
123+
(val + typed_literal!(tcx, *self, 1)).ok()
118124
} else {
119125
Some(self.initial_discriminant(tcx))
120126
}

0 commit comments

Comments
 (0)