Fix ICE for issue 2594
This commit is contained in:
parent
21e783d3b6
commit
6eb07cc5b6
@ -64,7 +64,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
|
||||
let size = size.assert_usize(cx.tcx).unwrap().into();
|
||||
|
||||
// Index is a constant uint
|
||||
if let Some((Constant::Int(const_index), _)) = constant(cx, index) {
|
||||
if let Some((Constant::Int(const_index), _)) = constant(cx, cx.tables, index) {
|
||||
if size <= const_index {
|
||||
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
|
||||
}
|
||||
@ -101,14 +101,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
|
||||
/// Returns an option containing a tuple with the start and end (exclusive) of
|
||||
/// the range.
|
||||
fn to_const_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, range: Range, array_size: u128) -> Option<(u128, u128)> {
|
||||
let s = range.start.map(|expr| constant(cx, expr).map(|(c, _)| c));
|
||||
let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
|
||||
let start = match s {
|
||||
Some(Some(Constant::Int(x))) => x,
|
||||
Some(_) => return None,
|
||||
None => 0,
|
||||
};
|
||||
|
||||
let e = range.end.map(|expr| constant(cx, expr).map(|(c, _)| c));
|
||||
let e = range.end.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
|
||||
let end = match e {
|
||||
Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed {
|
||||
x + 1
|
||||
|
@ -301,7 +301,7 @@ fn check_ineffective_gt(cx: &LateContext, span: Span, m: u128, c: u128, op: &str
|
||||
}
|
||||
|
||||
fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u128> {
|
||||
match constant(cx, lit)?.0 {
|
||||
match constant(cx, cx.tables, lit)?.0 {
|
||||
Constant::Int(n) => Some(n),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -163,10 +163,10 @@ pub fn lit_to_constant<'tcx>(lit: &LitKind, ty: Ty<'tcx>) -> Constant {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn constant(lcx: &LateContext, e: &Expr) -> Option<(Constant, bool)> {
|
||||
pub fn constant<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>, e: &Expr) -> Option<(Constant, bool)> {
|
||||
let mut cx = ConstEvalLateContext {
|
||||
tcx: lcx.tcx,
|
||||
tables: lcx.tables,
|
||||
tables,
|
||||
param_env: lcx.param_env,
|
||||
needed_resolution: false,
|
||||
substs: lcx.tcx.intern_substs(&[]),
|
||||
@ -174,12 +174,12 @@ pub fn constant(lcx: &LateContext, e: &Expr) -> Option<(Constant, bool)> {
|
||||
cx.expr(e).map(|cst| (cst, cx.needed_resolution))
|
||||
}
|
||||
|
||||
pub fn constant_simple(lcx: &LateContext, e: &Expr) -> Option<Constant> {
|
||||
constant(lcx, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
|
||||
pub fn constant_simple<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>, e: &Expr) -> Option<Constant> {
|
||||
constant(lcx, tables, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
|
||||
}
|
||||
|
||||
/// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`
|
||||
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'cc ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
|
||||
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
|
||||
ConstEvalLateContext {
|
||||
tcx: lcx.tcx,
|
||||
tables,
|
||||
|
@ -151,7 +151,7 @@ fn lint_same_then_else(cx: &LateContext, blocks: &[&Block]) {
|
||||
/// Implementation of `IFS_SAME_COND`.
|
||||
fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
|
||||
let hash: &Fn(&&Expr) -> u64 = &|expr| -> u64 {
|
||||
let mut h = SpanlessHash::new(cx);
|
||||
let mut h = SpanlessHash::new(cx, cx.tables);
|
||||
h.hash_expr(expr);
|
||||
h.finish()
|
||||
};
|
||||
@ -174,7 +174,7 @@ fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
|
||||
fn lint_match_arms(cx: &LateContext, expr: &Expr) {
|
||||
if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node {
|
||||
let hash = |&(_, arm): &(usize, &Arm)| -> u64 {
|
||||
let mut h = SpanlessHash::new(cx);
|
||||
let mut h = SpanlessHash::new(cx, cx.tables);
|
||||
h.hash_expr(&arm.body);
|
||||
h.finish()
|
||||
};
|
||||
|
@ -50,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
|
||||
}
|
||||
|
||||
fn check(cx: &LateContext, e: &Expr, span: Span) {
|
||||
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
|
||||
if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
|
||||
if v == 0 {
|
||||
span_lint(
|
||||
cx,
|
||||
|
@ -60,7 +60,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
|
||||
|
||||
#[allow(cast_possible_wrap)]
|
||||
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
|
||||
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
|
||||
if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
|
||||
let check = match cx.tables.expr_ty(e).sty {
|
||||
ty::TyInt(ity) => unsext(cx.tcx, -1i128, ity),
|
||||
ty::TyUint(uty) => clip(cx.tcx, !0, uty),
|
||||
|
@ -1120,8 +1120,8 @@ fn check_for_loop_reverse_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arg: &'tcx
|
||||
}) = higher::range(cx, arg)
|
||||
{
|
||||
// ...and both sides are compile-time constant integers...
|
||||
if let Some((start_idx, _)) = constant(cx, start) {
|
||||
if let Some((end_idx, _)) = constant(cx, end) {
|
||||
if let Some((start_idx, _)) = constant(cx, cx.tables, start) {
|
||||
if let Some((end_idx, _)) = constant(cx, cx.tables, end) {
|
||||
// ...and the start index is greater than the end index,
|
||||
// this loop will never run. This is often confusing for developers
|
||||
// who think that this will iterate from the larger value to the
|
||||
@ -2146,7 +2146,7 @@ fn path_name(e: &Expr) -> Option<Name> {
|
||||
}
|
||||
|
||||
fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, block: &'tcx Block, expr: &'tcx Expr) {
|
||||
if constant(cx, cond).is_some() {
|
||||
if constant(cx, cx.tables, cond).is_some() {
|
||||
// A pure constant condition (e.g. while false) is not linted.
|
||||
return;
|
||||
}
|
||||
|
@ -470,8 +470,8 @@ fn all_ranges<'a, 'tcx>(
|
||||
[].iter()
|
||||
}.filter_map(|pat| {
|
||||
if let PatKind::Range(ref lhs, ref rhs, ref range_end) = pat.node {
|
||||
let lhs = constant(cx, lhs)?.0;
|
||||
let rhs = constant(cx, rhs)?.0;
|
||||
let lhs = constant(cx, cx.tables, lhs)?.0;
|
||||
let rhs = constant(cx, cx.tables, rhs)?.0;
|
||||
let rhs = match *range_end {
|
||||
RangeEnd::Included => Bound::Included(rhs),
|
||||
RangeEnd::Excluded => Bound::Excluded(rhs),
|
||||
@ -480,7 +480,7 @@ fn all_ranges<'a, 'tcx>(
|
||||
}
|
||||
|
||||
if let PatKind::Lit(ref value) = pat.node {
|
||||
let value = constant(cx, value)?.0;
|
||||
let value = constant(cx, cx.tables, value)?.0;
|
||||
return Some(SpannedRange { span: pat.span, node: (value.clone(), Bound::Included(value)) });
|
||||
}
|
||||
|
||||
|
@ -1751,7 +1751,7 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
|
||||
|
||||
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
|
||||
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
|
||||
if let Some((Constant::Str(r), _)) = constant(cx, arg) {
|
||||
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
|
||||
if r.len() == 1 {
|
||||
let c = r.chars().next().unwrap();
|
||||
let snip = snippet(cx, expr.span, "..");
|
||||
|
@ -81,14 +81,14 @@ fn fetch_const<'a>(cx: &LateContext, args: &'a [Expr], m: MinMax) -> Option<(Min
|
||||
if args.len() != 2 {
|
||||
return None;
|
||||
}
|
||||
if let Some(c) = constant_simple(cx, &args[0]) {
|
||||
if constant_simple(cx, &args[1]).is_none() {
|
||||
if let Some(c) = constant_simple(cx, cx.tables, &args[0]) {
|
||||
if constant_simple(cx, cx.tables, &args[1]).is_none() {
|
||||
// otherwise ignore
|
||||
Some((m, c, &args[1]))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else if let Some(c) = constant_simple(cx, &args[1]) {
|
||||
} else if let Some(c) = constant_simple(cx, cx.tables, &args[1]) {
|
||||
Some((m, c, &args[0]))
|
||||
} else {
|
||||
None
|
||||
|
@ -446,7 +446,7 @@ fn check_nan(cx: &LateContext, path: &Path, expr: &Expr) {
|
||||
}
|
||||
|
||||
fn is_named_constant<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
|
||||
if let Some((_, res)) = constant(cx, expr) {
|
||||
if let Some((_, res)) = constant(cx, cx.tables, expr) {
|
||||
res
|
||||
} else {
|
||||
false
|
||||
@ -454,7 +454,7 @@ fn is_named_constant<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) ->
|
||||
}
|
||||
|
||||
fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
|
||||
match constant(cx, expr) {
|
||||
match constant(cx, cx.tables, expr) {
|
||||
Some((Constant::F32(f), _)) => f == 0.0 || f.is_infinite(),
|
||||
Some((Constant::F64(f), _)) => f == 0.0 || f.is_infinite(),
|
||||
_ => false,
|
||||
|
@ -94,7 +94,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
// Range with step_by(0).
|
||||
if name == "step_by" && args.len() == 2 && has_step_by(cx, &args[0]) {
|
||||
use consts::{constant, Constant};
|
||||
if let Some((Constant::Int(0), _)) = constant(cx, &args[1]) {
|
||||
if let Some((Constant::Int(0), _)) = constant(cx, cx.tables, &args[1]) {
|
||||
span_lint(
|
||||
cx,
|
||||
ITERATOR_STEP_BY_ZERO,
|
||||
|
@ -138,7 +138,7 @@ fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
|
||||
}
|
||||
|
||||
fn const_str<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) -> Option<String> {
|
||||
constant(cx, e).and_then(|(c, _)| match c {
|
||||
constant(cx, cx.tables, e).and_then(|(c, _)| match c {
|
||||
Constant::Str(s) => Some(s),
|
||||
_ => None,
|
||||
})
|
||||
|
@ -1341,7 +1341,7 @@ fn detect_extreme_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -
|
||||
|
||||
let ty = cx.tables.expr_ty(expr);
|
||||
|
||||
let cv = constant(cx, expr)?.0;
|
||||
let cv = constant(cx, cx.tables, expr)?.0;
|
||||
|
||||
let which = match (&ty.sty, cv) {
|
||||
(&ty::TyBool, Constant::Bool(false)) |
|
||||
@ -1526,7 +1526,7 @@ fn numeric_cast_precast_bounds<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(
|
||||
}
|
||||
|
||||
fn node_as_const_fullint<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<FullInt> {
|
||||
let val = constant(cx, expr)?.0;
|
||||
let val = constant(cx, cx.tables, expr)?.0;
|
||||
if let Constant::Int(const_int) = val {
|
||||
match cx.tables.expr_ty(expr).sty {
|
||||
ty::TyInt(ity) => Some(FullInt::S(sext(cx.tcx, const_int, ity))),
|
||||
|
@ -1,6 +1,7 @@
|
||||
use consts::{constant_simple, constant_context};
|
||||
use rustc::lint::*;
|
||||
use rustc::hir::*;
|
||||
use rustc::ty::{TypeckTables};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use syntax::ast::Name;
|
||||
@ -16,6 +17,7 @@ use utils::differing_macro_contexts;
|
||||
pub struct SpanlessEq<'a, 'tcx: 'a> {
|
||||
/// Context used to evaluate constant expressions.
|
||||
cx: &'a LateContext<'a, 'tcx>,
|
||||
tables: &'a TypeckTables<'tcx>,
|
||||
/// If is true, never consider as equal expressions containing function
|
||||
/// calls.
|
||||
ignore_fn: bool,
|
||||
@ -25,6 +27,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
|
||||
pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
|
||||
Self {
|
||||
cx,
|
||||
tables: cx.tables,
|
||||
ignore_fn: false,
|
||||
}
|
||||
}
|
||||
@ -32,6 +35,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
|
||||
pub fn ignore_fn(self) -> Self {
|
||||
Self {
|
||||
cx: self.cx,
|
||||
tables: self.cx.tables,
|
||||
ignore_fn: true,
|
||||
}
|
||||
}
|
||||
@ -64,7 +68,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
|
||||
return false;
|
||||
}
|
||||
|
||||
if let (Some(l), Some(r)) = (constant_simple(self.cx, left), constant_simple(self.cx, right)) {
|
||||
if let (Some(l), Some(r)) = (constant_simple(self.cx, self.tables, left), constant_simple(self.cx, self.tables, right)) {
|
||||
if l == r {
|
||||
return true;
|
||||
}
|
||||
@ -288,13 +292,15 @@ where
|
||||
pub struct SpanlessHash<'a, 'tcx: 'a> {
|
||||
/// Context used to evaluate constant expressions.
|
||||
cx: &'a LateContext<'a, 'tcx>,
|
||||
tables: &'a TypeckTables<'tcx>,
|
||||
s: DefaultHasher,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
|
||||
pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
|
||||
pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
|
||||
Self {
|
||||
cx,
|
||||
tables,
|
||||
s: DefaultHasher::new(),
|
||||
}
|
||||
}
|
||||
@ -317,7 +323,7 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
|
||||
|
||||
#[allow(many_single_char_names)]
|
||||
pub fn hash_expr(&mut self, e: &Expr) {
|
||||
if let Some(e) = constant_simple(self.cx, e) {
|
||||
if let Some(e) = constant_simple(self.cx, self.tables, e) {
|
||||
return e.hash(&mut self.s);
|
||||
}
|
||||
|
||||
@ -461,6 +467,7 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
|
||||
let c: fn(_, _) -> _ = ExprRepeat;
|
||||
c.hash(&mut self.s);
|
||||
self.hash_expr(e);
|
||||
self.tables = self.cx.tcx.body_tables(l_id);
|
||||
self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
|
||||
},
|
||||
ExprRet(ref e) => {
|
||||
|
@ -66,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
fn check_vec_macro<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
|
||||
let snippet = match *vec_args {
|
||||
higher::VecArgs::Repeat(elem, len) => {
|
||||
if constant(cx, len).is_some() {
|
||||
if constant(cx, cx.tables, len).is_some() {
|
||||
format!("&[{}; {}]", snippet(cx, elem.span, "elem"), snippet(cx, len.span, "len"))
|
||||
} else {
|
||||
return;
|
||||
|
@ -37,8 +37,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
// TODO - constant_simple does not fold many operations involving floats.
|
||||
// That's probably fine for this lint - it's pretty unlikely that someone would
|
||||
// do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.
|
||||
if let Some(lhs_value) = constant_simple(cx, left);
|
||||
if let Some(rhs_value) = constant_simple(cx, right);
|
||||
if let Some(lhs_value) = constant_simple(cx, cx.tables, left);
|
||||
if let Some(rhs_value) = constant_simple(cx, cx.tables, right);
|
||||
if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value;
|
||||
if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value;
|
||||
then {
|
||||
|
Loading…
Reference in New Issue
Block a user