Skip to content

Commit d659533

Browse files
committed
rustup const eval changes
1 parent 20123ee commit d659533

File tree

7 files changed

+71
-104
lines changed

7 files changed

+71
-104
lines changed

src/array_indexing.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
33
use rustc::middle::const_eval::{eval_const_expr_partial, ConstVal};
44
use rustc::middle::ty::TyArray;
55
use rustc_front::hir::*;
6+
use rustc_const_eval::ConstInt;
67
use syntax::ast::RangeLimits;
78
use utils;
89

@@ -62,11 +63,11 @@ impl LateLintPass for ArrayIndexing {
6263
// Array with known size can be checked statically
6364
let ty = cx.tcx.expr_ty(array);
6465
if let TyArray(_, size) = ty.sty {
65-
let size = size as u64;
66+
let size = ConstInt::Infer(size as u64);
6667

6768
// Index is a constant uint
6869
let const_index = eval_const_expr_partial(cx.tcx, &index, ExprTypeChecked, None);
69-
if let Ok(ConstVal::Uint(const_index)) = const_index {
70+
if let Ok(ConstVal::Integral(const_index)) = const_index {
7071
if size <= const_index {
7172
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
7273
}
@@ -115,24 +116,24 @@ impl LateLintPass for ArrayIndexing {
115116
fn to_const_range(start: Option<Option<ConstVal>>,
116117
end: Option<Option<ConstVal>>,
117118
limits: RangeLimits,
118-
array_size: u64)
119-
-> Option<(u64, u64)> {
119+
array_size: ConstInt)
120+
-> Option<(ConstInt, ConstInt)> {
120121
let start = match start {
121-
Some(Some(ConstVal::Uint(x))) => x,
122+
Some(Some(ConstVal::Integral(x))) => x,
122123
Some(_) => return None,
123-
None => 0,
124+
None => ConstInt::Infer(0),
124125
};
125126

126127
let end = match end {
127-
Some(Some(ConstVal::Uint(x))) => {
128+
Some(Some(ConstVal::Integral(x))) => {
128129
if limits == RangeLimits::Closed {
129130
x
130131
} else {
131-
x - 1
132+
(x - ConstInt::Infer(1)).expect("x > 0")
132133
}
133134
}
134135
Some(_) => return None,
135-
None => array_size - 1,
136+
None => (array_size - ConstInt::Infer(1)).expect("array_size > 0"),
136137
};
137138

138139
Some((start, end))

src/bit_mask.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u64> {
271271
}
272272
}
273273
.and_then(|def_id| lookup_const_by_id(cx.tcx, def_id, None, None))
274-
.and_then(|l| fetch_int_literal(cx, l))
274+
.and_then(|(l, _ty)| fetch_int_literal(cx, l))
275275
}
276276
_ => None,
277277
}

src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
347347
}
348348
// separate if lets to avoid double borrowing the def_map
349349
if let Some(id) = maybe_id {
350-
if let Some(const_expr) = lookup_const_by_id(lcx.tcx, id, None, None) {
350+
if let Some((const_expr, _ty)) = lookup_const_by_id(lcx.tcx, id, None, None) {
351351
let ret = self.expr(const_expr);
352352
if ret.is_some() {
353353
self.needed_resolution = true;

src/enum_clike.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! lint on C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`
22
33
use rustc::lint::*;
4-
use syntax::ast::{IntTy, UintTy};
54
use syntax::attr::*;
65
use rustc_front::hir::*;
76
use rustc::middle::const_eval::{ConstVal, EvalHint, eval_const_expr_partial};
8-
use rustc::middle::ty;
97
use utils::span_lint;
108

119
/// **What it does:** Lints on C-like enums that are `repr(isize/usize)` and have values that don't fit into an `i32`.
@@ -35,12 +33,10 @@ impl LateLintPass for EnumClikeUnportableVariant {
3533
for var in &def.variants {
3634
let variant = &var.node;
3735
if let Some(ref disr) = variant.disr_expr {
38-
let cv = eval_const_expr_partial(cx.tcx, &**disr, EvalHint::ExprTypeChecked, None);
39-
let bad = match (cv, &cx.tcx.expr_ty(&**disr).sty) {
40-
(Ok(ConstVal::Int(i)), &ty::TyInt(IntTy::Is)) => i as i32 as i64 != i,
41-
(Ok(ConstVal::Uint(i)), &ty::TyInt(IntTy::Is)) => i as i32 as u64 != i,
42-
(Ok(ConstVal::Int(i)), &ty::TyUint(UintTy::Us)) => (i < 0) || (i as u32 as i64 != i),
43-
(Ok(ConstVal::Uint(i)), &ty::TyUint(UintTy::Us)) => i as u32 as u64 != i,
36+
use rustc_const_eval::*;
37+
let bad = match eval_const_expr_partial(cx.tcx, &**disr, EvalHint::ExprTypeChecked, None) {
38+
Ok(ConstVal::Integral(Usize(Us64(i)))) => i as u32 as u64 != i,
39+
Ok(ConstVal::Integral(Isize(Is64(i)))) => i as i32 as i64 != i,
4440
_ => false,
4541
};
4642
if bad {

src/loops.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,7 @@ fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
429429
// who think that this will iterate from the larger value to the
430430
// smaller value.
431431
let (sup, eq) = match (start_idx, end_idx) {
432-
(ConstVal::Int(start_idx), ConstVal::Int(end_idx)) => {
433-
(start_idx > end_idx, start_idx == end_idx)
434-
}
435-
(ConstVal::Uint(start_idx), ConstVal::Uint(end_idx)) => {
432+
(ConstVal::Integral(start_idx), ConstVal::Integral(end_idx)) => {
436433
(start_idx > end_idx, start_idx == end_idx)
437434
}
438435
_ => (false, false),

src/matches.rs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use rustc::lint::*;
2-
use rustc::middle::const_eval::ConstVal::{Int, Uint};
32
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
43
use rustc::middle::const_eval::{eval_const_expr_partial, ConstVal};
54
use rustc::middle::ty;
65
use rustc_front::hir::*;
6+
use rustc_const_eval::ConstInt;
77
use std::cmp::Ordering;
88
use syntax::ast::LitKind;
99
use syntax::codemap::Span;
@@ -288,19 +288,16 @@ fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
288288
fn check_overlapping_arms(cx: &LateContext, ex: &Expr, arms: &[Arm]) {
289289
if arms.len() >= 2 && cx.tcx.expr_ty(ex).is_integral() {
290290
let ranges = all_ranges(cx, arms);
291-
let overlap = match type_ranges(&ranges) {
292-
TypedRanges::IntRanges(ranges) => overlapping(&ranges).map(|(start, end)| (start.span, end.span)),
293-
TypedRanges::UintRanges(ranges) => overlapping(&ranges).map(|(start, end)| (start.span, end.span)),
294-
TypedRanges::None => None,
295-
};
296-
297-
if let Some((start, end)) = overlap {
298-
span_note_and_lint(cx,
299-
MATCH_OVERLAPPING_ARM,
300-
start,
301-
"some ranges overlap",
302-
end,
303-
"overlaps with this");
291+
let type_ranges = type_ranges(&ranges);
292+
if !type_ranges.is_empty() {
293+
if let Some((start, end)) = overlapping(&type_ranges) {
294+
span_note_and_lint(cx,
295+
MATCH_OVERLAPPING_ARM,
296+
start.span,
297+
"some ranges overlap",
298+
end.span,
299+
"overlaps with this");
300+
}
304301
}
305302
}
306303
}
@@ -370,51 +367,22 @@ pub struct SpannedRange<T> {
370367
pub node: (T, T),
371368
}
372369

373-
#[derive(Debug)]
374-
enum TypedRanges {
375-
IntRanges(Vec<SpannedRange<i64>>),
376-
UintRanges(Vec<SpannedRange<u64>>),
377-
None,
378-
}
370+
type TypedRanges = Vec<SpannedRange<ConstInt>>;
379371

380372
/// Get all `Int` ranges or all `Uint` ranges. Mixed types are an error anyway and other types than
381373
/// `Uint` and `Int` probably don't make sense.
382374
fn type_ranges(ranges: &[SpannedRange<ConstVal>]) -> TypedRanges {
383-
if ranges.is_empty() {
384-
TypedRanges::None
385-
} else {
386-
match ranges[0].node {
387-
(Int(_), Int(_)) => {
388-
TypedRanges::IntRanges(ranges.iter()
389-
.filter_map(|range| {
390-
if let (Int(start), Int(end)) = range.node {
391-
Some(SpannedRange {
392-
span: range.span,
393-
node: (start, end),
394-
})
395-
} else {
396-
None
397-
}
398-
})
399-
.collect())
400-
}
401-
(Uint(_), Uint(_)) => {
402-
TypedRanges::UintRanges(ranges.iter()
403-
.filter_map(|range| {
404-
if let (Uint(start), Uint(end)) = range.node {
405-
Some(SpannedRange {
406-
span: range.span,
407-
node: (start, end),
408-
})
409-
} else {
410-
None
411-
}
412-
})
413-
.collect())
414-
}
415-
_ => TypedRanges::None,
375+
ranges.iter().filter_map(|range| {
376+
if let (ConstVal::Integral(start), ConstVal::Integral(end)) = range.node {
377+
Some(SpannedRange {
378+
span: range.span,
379+
node: (start, end),
380+
})
381+
} else {
382+
None
416383
}
417-
}
384+
})
385+
.collect()
418386
}
419387

420388
fn is_unit_expr(expr: &Expr) -> bool {

src/types.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ fn detect_extreme_expr<'a>(cx: &LateContext, expr: &'a Expr) -> Option<ExtremeEx
673673
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
674674
use types::ExtremeType::*;
675675
use rustc::middle::const_eval::ConstVal::*;
676+
use rustc_const_eval::*;
676677

677678
let ty = &cx.tcx.expr_ty(expr).sty;
678679

@@ -687,33 +688,37 @@ fn detect_extreme_expr<'a>(cx: &LateContext, expr: &'a Expr) -> Option<ExtremeEx
687688
};
688689

689690
let which = match (ty, cv) {
690-
(&ty::TyBool, Bool(false)) => Minimum,
691-
692-
(&ty::TyInt(IntTy::Is), Int(x)) if x == ::std::isize::MIN as i64 => Minimum,
693-
(&ty::TyInt(IntTy::I8), Int(x)) if x == ::std::i8::MIN as i64 => Minimum,
694-
(&ty::TyInt(IntTy::I16), Int(x)) if x == ::std::i16::MIN as i64 => Minimum,
695-
(&ty::TyInt(IntTy::I32), Int(x)) if x == ::std::i32::MIN as i64 => Minimum,
696-
(&ty::TyInt(IntTy::I64), Int(x)) if x == ::std::i64::MIN as i64 => Minimum,
697-
698-
(&ty::TyUint(UintTy::Us), Uint(x)) if x == ::std::usize::MIN as u64 => Minimum,
699-
(&ty::TyUint(UintTy::U8), Uint(x)) if x == ::std::u8::MIN as u64 => Minimum,
700-
(&ty::TyUint(UintTy::U16), Uint(x)) if x == ::std::u16::MIN as u64 => Minimum,
701-
(&ty::TyUint(UintTy::U32), Uint(x)) if x == ::std::u32::MIN as u64 => Minimum,
702-
(&ty::TyUint(UintTy::U64), Uint(x)) if x == ::std::u64::MIN as u64 => Minimum,
703-
704-
(&ty::TyBool, Bool(true)) => Maximum,
705-
706-
(&ty::TyInt(IntTy::Is), Int(x)) if x == ::std::isize::MAX as i64 => Maximum,
707-
(&ty::TyInt(IntTy::I8), Int(x)) if x == ::std::i8::MAX as i64 => Maximum,
708-
(&ty::TyInt(IntTy::I16), Int(x)) if x == ::std::i16::MAX as i64 => Maximum,
709-
(&ty::TyInt(IntTy::I32), Int(x)) if x == ::std::i32::MAX as i64 => Maximum,
710-
(&ty::TyInt(IntTy::I64), Int(x)) if x == ::std::i64::MAX as i64 => Maximum,
711-
712-
(&ty::TyUint(UintTy::Us), Uint(x)) if x == ::std::usize::MAX as u64 => Maximum,
713-
(&ty::TyUint(UintTy::U8), Uint(x)) if x == ::std::u8::MAX as u64 => Maximum,
714-
(&ty::TyUint(UintTy::U16), Uint(x)) if x == ::std::u16::MAX as u64 => Maximum,
715-
(&ty::TyUint(UintTy::U32), Uint(x)) if x == ::std::u32::MAX as u64 => Maximum,
716-
(&ty::TyUint(UintTy::U64), Uint(x)) if x == ::std::u64::MAX as u64 => Maximum,
691+
(&ty::TyBool, Bool(false)) |
692+
693+
(&ty::TyInt(IntTy::Is), Integral(Isize(Is32(::std::i32::MIN)))) |
694+
(&ty::TyInt(IntTy::Is), Integral(Isize(Is64(::std::i64::MIN)))) |
695+
(&ty::TyInt(IntTy::I8), Integral(I8(::std::i8::MIN))) |
696+
(&ty::TyInt(IntTy::I16), Integral(I16(::std::i16::MIN))) |
697+
(&ty::TyInt(IntTy::I32), Integral(I32(::std::i32::MIN))) |
698+
(&ty::TyInt(IntTy::I64), Integral(I64(::std::i64::MIN))) |
699+
700+
(&ty::TyUint(UintTy::Us), Integral(Usize(Us32(::std::u32::MIN)))) |
701+
(&ty::TyUint(UintTy::Us), Integral(Usize(Us64(::std::u64::MIN)))) |
702+
(&ty::TyUint(UintTy::U8), Integral(U8(::std::u8::MIN))) |
703+
(&ty::TyUint(UintTy::U16), Integral(U16(::std::u16::MIN))) |
704+
(&ty::TyUint(UintTy::U32), Integral(U32(::std::u32::MIN))) |
705+
(&ty::TyUint(UintTy::U64), Integral(U64(::std::u64::MIN))) => Minimum,
706+
707+
(&ty::TyBool, Bool(true)) |
708+
709+
(&ty::TyInt(IntTy::Is), Integral(Isize(Is32(::std::i32::MAX)))) |
710+
(&ty::TyInt(IntTy::Is), Integral(Isize(Is64(::std::i64::MAX)))) |
711+
(&ty::TyInt(IntTy::I8), Integral(I8(::std::i8::MAX))) |
712+
(&ty::TyInt(IntTy::I16), Integral(I16(::std::i16::MAX))) |
713+
(&ty::TyInt(IntTy::I32), Integral(I32(::std::i32::MAX))) |
714+
(&ty::TyInt(IntTy::I64), Integral(I64(::std::i64::MAX))) |
715+
716+
(&ty::TyUint(UintTy::Us), Integral(Usize(Us32(::std::u32::MAX)))) |
717+
(&ty::TyUint(UintTy::Us), Integral(Usize(Us64(::std::u64::MAX)))) |
718+
(&ty::TyUint(UintTy::U8), Integral(U8(::std::u8::MAX))) |
719+
(&ty::TyUint(UintTy::U16), Integral(U16(::std::u16::MAX))) |
720+
(&ty::TyUint(UintTy::U32), Integral(U32(::std::u32::MAX))) |
721+
(&ty::TyUint(UintTy::U64), Integral(U64(::std::u64::MAX))) => Maximum,
717722

718723
_ => return None,
719724
};

0 commit comments

Comments
 (0)