Skip to content

Commit 6405122

Browse files
committed
add feature gate const_indexing
tracking issue is #29947
1 parent 6683fa4 commit 6405122

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

src/librustc/middle/const_eval.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,17 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11371137
hir::ExprTup(_) => Tuple(e.id),
11381138
hir::ExprStruct(..) => Struct(e.id),
11391139
hir::ExprIndex(ref arr, ref idx) => {
1140+
if !tcx.sess.features.borrow().const_indexing {
1141+
tcx.sess.span_err(
1142+
e.span,
1143+
"const indexing is an unstable feature");
1144+
fileline_help!(
1145+
tcx.sess,
1146+
e.span,
1147+
"in Nightly builds, add `#![feature(const_indexing)]` to the crate \
1148+
attributes to enable");
1149+
signal!(e, NonConstPath)
1150+
}
11401151
let arr_hint = if let ExprTypeChecked = ty_hint {
11411152
ExprTypeChecked
11421153
} else {

src/libsyntax/feature_gate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
179179
// Allows the definition of `const fn` functions.
180180
("const_fn", "1.2.0", Some(24111), Active),
181181

182+
// Allows indexing into constant arrays.
183+
("const_indexing", "1.4.0", Some(29947), Active),
184+
182185
// Allows using #[prelude_import] on glob `use` items.
183186
//
184187
// rustc internal
@@ -494,6 +497,7 @@ pub struct Features {
494497
/// #![feature] attrs for non-language (library) features
495498
pub declared_lib_features: Vec<(InternedString, Span)>,
496499
pub const_fn: bool,
500+
pub const_indexing: bool,
497501
pub static_recursion: bool,
498502
pub default_type_parameter_fallback: bool,
499503
pub type_macros: bool,
@@ -525,6 +529,7 @@ impl Features {
525529
declared_stable_lang_features: Vec::new(),
526530
declared_lib_features: Vec::new(),
527531
const_fn: false,
532+
const_indexing: false,
528533
static_recursion: false,
529534
default_type_parameter_fallback: false,
530535
type_macros: false,
@@ -1097,6 +1102,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
10971102
declared_stable_lang_features: accepted_features,
10981103
declared_lib_features: unknown_features,
10991104
const_fn: cx.has_feature("const_fn"),
1105+
const_indexing: cx.has_feature("const_indexing"),
11001106
static_recursion: cx.has_feature("static_recursion"),
11011107
default_type_parameter_fallback: cx.has_feature("default_type_parameter_fallback"),
11021108
type_macros: cx.has_feature("type_macros"),

src/test/compile-fail/const-array-oob-arith.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_indexing)]
12+
1113
const ARR: [i32; 6] = [42, 43, 44, 45, 46, 47];
1214
const IDX: usize = 3;
1315
const VAL: i32 = ARR[IDX];

src/test/compile-fail/const-array-oob.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_indexing)]
12+
1113
const FOO: [u32; 3] = [1, 2, 3];
1214
const BAR: u32 = FOO[5]; // no error, because the error below occurs before regular const eval
1315

src/test/run-pass/array_const_index-1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_indexing)]
1112

1213
fn main() {
1314
const ARR: [i32; 6] = [42, 43, 44, 45, 46, 47];

0 commit comments

Comments
 (0)