Skip to content

Commit de62f9d

Browse files
committed
Auto merge of #30286 - oli-obk:const_error_span, r=nikomatsakis
previously the error was erased and a `non-const path` error was emitted at the location of the field access instead of at the overflow location (as can be seen in the playground: http://is.gd/EuAF5F )
2 parents f963eb2 + 8867593 commit de62f9d

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

src/librustc/middle/const_eval.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -1182,46 +1182,40 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11821182
},
11831183
hir::ExprTupField(ref base, index) => {
11841184
let base_hint = ty_hint.erase_hint();
1185-
if let Ok(c) = eval_const_expr_partial(tcx, base, base_hint, fn_args) {
1186-
if let Tuple(tup_id) = c {
1187-
if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
1188-
if index.node < fields.len() {
1189-
return eval_const_expr_partial(tcx, &fields[index.node], base_hint, fn_args)
1190-
} else {
1191-
signal!(e, TupleIndexOutOfBounds);
1192-
}
1185+
let c = try!(eval_const_expr_partial(tcx, base, base_hint, fn_args));
1186+
if let Tuple(tup_id) = c {
1187+
if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
1188+
if index.node < fields.len() {
1189+
return eval_const_expr_partial(tcx, &fields[index.node], base_hint, fn_args)
11931190
} else {
1194-
unreachable!()
1191+
signal!(e, TupleIndexOutOfBounds);
11951192
}
11961193
} else {
1197-
signal!(base, ExpectedConstTuple);
1194+
unreachable!()
11981195
}
11991196
} else {
1200-
signal!(base, NonConstPath)
1197+
signal!(base, ExpectedConstTuple);
12011198
}
12021199
}
12031200
hir::ExprField(ref base, field_name) => {
12041201
let base_hint = ty_hint.erase_hint();
12051202
// Get the base expression if it is a struct and it is constant
1206-
if let Ok(c) = eval_const_expr_partial(tcx, base, base_hint, fn_args) {
1207-
if let Struct(struct_id) = c {
1208-
if let hir::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
1209-
// Check that the given field exists and evaluate it
1210-
// if the idents are compared run-pass/issue-19244 fails
1211-
if let Some(f) = fields.iter().find(|f| f.name.node
1212-
== field_name.node) {
1213-
return eval_const_expr_partial(tcx, &*f.expr, base_hint, fn_args)
1214-
} else {
1215-
signal!(e, MissingStructField);
1216-
}
1203+
let c = try!(eval_const_expr_partial(tcx, base, base_hint, fn_args));
1204+
if let Struct(struct_id) = c {
1205+
if let hir::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
1206+
// Check that the given field exists and evaluate it
1207+
// if the idents are compared run-pass/issue-19244 fails
1208+
if let Some(f) = fields.iter().find(|f| f.name.node
1209+
== field_name.node) {
1210+
return eval_const_expr_partial(tcx, &*f.expr, base_hint, fn_args)
12171211
} else {
1218-
unreachable!()
1212+
signal!(e, MissingStructField);
12191213
}
12201214
} else {
1221-
signal!(base, ExpectedConstStruct);
1215+
unreachable!()
12221216
}
12231217
} else {
1224-
signal!(base, NonConstPath);
1218+
signal!(base, ExpectedConstStruct);
12251219
}
12261220
}
12271221
_ => signal!(e, MiscCatchAll)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
// Test spans of errors
12+
13+
const TUP: (usize,) = 5 << 64;
14+
//~^ ERROR: attempted left shift with overflow [E0250]
15+
const ARR: [i32; TUP.0] = [];
16+
17+
fn main() {
18+
}

0 commit comments

Comments
 (0)