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 )
This commit is contained in:
commit
de62f9d885
@ -1182,46 +1182,40 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
|
||||
},
|
||||
hir::ExprTupField(ref base, index) => {
|
||||
let base_hint = ty_hint.erase_hint();
|
||||
if let Ok(c) = eval_const_expr_partial(tcx, base, base_hint, fn_args) {
|
||||
if let Tuple(tup_id) = c {
|
||||
if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
|
||||
if index.node < fields.len() {
|
||||
return eval_const_expr_partial(tcx, &fields[index.node], base_hint, fn_args)
|
||||
} else {
|
||||
signal!(e, TupleIndexOutOfBounds);
|
||||
}
|
||||
let c = try!(eval_const_expr_partial(tcx, base, base_hint, fn_args));
|
||||
if let Tuple(tup_id) = c {
|
||||
if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
|
||||
if index.node < fields.len() {
|
||||
return eval_const_expr_partial(tcx, &fields[index.node], base_hint, fn_args)
|
||||
} else {
|
||||
unreachable!()
|
||||
signal!(e, TupleIndexOutOfBounds);
|
||||
}
|
||||
} else {
|
||||
signal!(base, ExpectedConstTuple);
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
signal!(base, NonConstPath)
|
||||
signal!(base, ExpectedConstTuple);
|
||||
}
|
||||
}
|
||||
hir::ExprField(ref base, field_name) => {
|
||||
let base_hint = ty_hint.erase_hint();
|
||||
// Get the base expression if it is a struct and it is constant
|
||||
if let Ok(c) = eval_const_expr_partial(tcx, base, base_hint, fn_args) {
|
||||
if let Struct(struct_id) = c {
|
||||
if let hir::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
|
||||
// Check that the given field exists and evaluate it
|
||||
// if the idents are compared run-pass/issue-19244 fails
|
||||
if let Some(f) = fields.iter().find(|f| f.name.node
|
||||
== field_name.node) {
|
||||
return eval_const_expr_partial(tcx, &*f.expr, base_hint, fn_args)
|
||||
} else {
|
||||
signal!(e, MissingStructField);
|
||||
}
|
||||
let c = try!(eval_const_expr_partial(tcx, base, base_hint, fn_args));
|
||||
if let Struct(struct_id) = c {
|
||||
if let hir::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
|
||||
// Check that the given field exists and evaluate it
|
||||
// if the idents are compared run-pass/issue-19244 fails
|
||||
if let Some(f) = fields.iter().find(|f| f.name.node
|
||||
== field_name.node) {
|
||||
return eval_const_expr_partial(tcx, &*f.expr, base_hint, fn_args)
|
||||
} else {
|
||||
unreachable!()
|
||||
signal!(e, MissingStructField);
|
||||
}
|
||||
} else {
|
||||
signal!(base, ExpectedConstStruct);
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
signal!(base, NonConstPath);
|
||||
signal!(base, ExpectedConstStruct);
|
||||
}
|
||||
}
|
||||
_ => signal!(e, MiscCatchAll)
|
||||
|
18
src/test/compile-fail/const-tup-index-span.rs
Normal file
18
src/test/compile-fail/const-tup-index-span.rs
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test spans of errors
|
||||
|
||||
const TUP: (usize,) = 5 << 64;
|
||||
//~^ ERROR: attempted left shift with overflow [E0250]
|
||||
const ARR: [i32; TUP.0] = [];
|
||||
|
||||
fn main() {
|
||||
}
|
Loading…
Reference in New Issue
Block a user