From 8fee5480c34f97a1b970a44758c6617d293724e8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 6 Oct 2015 23:06:15 +0200 Subject: [PATCH 1/2] Add new error code --- src/librustc_trans/trans/consts.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs index 8e63f2788ed..0cae0ae59ba 100644 --- a/src/librustc_trans/trans/consts.rs +++ b/src/librustc_trans/trans/consts.rs @@ -628,8 +628,8 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, if iv >= len { // FIXME #3170: report this earlier on in the const-eval // pass. Reporting here is a bit late. - cx.sess().span_err(e.span, - "const index-expr is out of bounds"); + span_err!(cx.sess(), e.span, E0515, + "const index-expr is out of bounds"); C_undef(val_ty(arr).element_type()) } else { const_get_elt(cx, arr, &[iv as c_uint]) From a94f684129dbf8cb4c87abe69ff45765cf24e501 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 6 Oct 2015 23:08:33 +0200 Subject: [PATCH 2/2] Add error explanation for E0515 --- src/librustc_trans/diagnostics.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/librustc_trans/diagnostics.rs b/src/librustc_trans/diagnostics.rs index dd7c3834e56..05236a7a6fb 100644 --- a/src/librustc_trans/diagnostics.rs +++ b/src/librustc_trans/diagnostics.rs @@ -12,6 +12,21 @@ register_long_diagnostics! { +E0515: r##" +A constant index expression was out of bounds. Erroneous code example: + +``` +let x = &[0, 1, 2][7]; // error: const index-expr is out of bounds +``` + +Please specify a valid index (not inferior to 0 or superior to array length). +Example: + +``` +let x = &[0, 1, 2][2]; // ok! +``` +"##, + } register_diagnostics! {