Skip to content

Commit 18f8143

Browse files
committed
Auto merge of #30714 - wesleywiser:fix_29914, r=arielb1
The issue was that the const evaluator was returning an error because the feature flag const_indexing wasn't turned on. The error was then reported as a bug. Fixes #29914
2 parents 94ecd48 + 271777c commit 18f8143

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

src/librustc_trans/trans/consts.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use middle::const_eval::{const_int_checked_div, const_uint_checked_div};
2424
use middle::const_eval::{const_int_checked_rem, const_uint_checked_rem};
2525
use middle::const_eval::{const_int_checked_shl, const_uint_checked_shl};
2626
use middle::const_eval::{const_int_checked_shr, const_uint_checked_shr};
27-
use middle::const_eval::EvalHint::ExprTypeChecked;
28-
use middle::const_eval::eval_const_expr_partial;
2927
use middle::def::Def;
3028
use middle::def_id::DefId;
3129
use trans::{adt, closure, debuginfo, expr, inline, machine};
@@ -261,7 +259,7 @@ impl ConstEvalFailure {
261259
}
262260
}
263261

264-
#[derive(Copy, Clone)]
262+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
265263
pub enum TrueConst {
266264
Yes, No
267265
}
@@ -665,11 +663,11 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
665663
},
666664
hir::ExprIndex(ref base, ref index) => {
667665
let (bv, bt) = try!(const_expr(cx, &base, param_substs, fn_args, trueconst));
668-
let iv = match eval_const_expr_partial(cx.tcx(), &index, ExprTypeChecked, None) {
669-
Ok(ConstVal::Int(i)) => i as u64,
670-
Ok(ConstVal::Uint(u)) => u,
671-
_ => cx.sess().span_bug(index.span,
672-
"index is not an integer-constant expression")
666+
let iv = try!(const_expr(cx, &index, param_substs, fn_args, TrueConst::Yes)).0;
667+
let iv = if let Some(iv) = const_to_opt_uint(iv) {
668+
iv
669+
} else {
670+
cx.sess().span_bug(index.span, "index is not an integer-constant expression");
673671
};
674672
let (arr, len) = match bt.sty {
675673
ty::TyArray(_, u) => (bv, C_uint(cx, u)),

src/test/run-pass/issue-29914-2.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const ARR: [usize; 5] = [5, 4, 3, 2, 1];
12+
13+
fn main() {
14+
assert_eq!(3, ARR[ARR[3]]);
15+
}

src/test/run-pass/issue-29914-3.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const ARR: [usize; 5] = [5, 4, 3, 2, 1];
12+
const BLA: usize = ARR[ARR[3]];
13+
14+
fn main() {
15+
assert_eq!(3, BLA);
16+
}

src/test/run-pass/issue-29914.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(const_indexing)]
11+
12+
const ARR: [usize; 5] = [5, 4, 3, 2, 1];
13+
14+
fn main() {
15+
assert_eq!(3, ARR[ARR[3]]);
16+
}

0 commit comments

Comments
 (0)