]> git.lizzy.rs Git - rust.git/commitdiff
add feature gate `const_indexing`
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 20 Nov 2015 09:43:04 +0000 (10:43 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 20 Nov 2015 09:43:04 +0000 (10:43 +0100)
tracking issue is #29947

src/librustc/middle/const_eval.rs
src/libsyntax/feature_gate.rs
src/test/compile-fail/const-array-oob-arith.rs
src/test/compile-fail/const-array-oob.rs
src/test/run-pass/array_const_index-1.rs

index f90fdd1d4b3ef842831aba7934a5665e260507cc..90c1cfda2620a68a77a97689a446f9a62b29e670 100644 (file)
@@ -1137,6 +1137,17 @@ fn fromb(b: bool) -> ConstVal { Int(b as i64) }
       hir::ExprTup(_) => Tuple(e.id),
       hir::ExprStruct(..) => Struct(e.id),
       hir::ExprIndex(ref arr, ref idx) => {
+        if !tcx.sess.features.borrow().const_indexing {
+            tcx.sess.span_err(
+                e.span,
+                "const indexing is an unstable feature");
+            fileline_help!(
+                tcx.sess,
+                e.span,
+                "in Nightly builds, add `#![feature(const_indexing)]` to the crate \
+                 attributes to enable");
+            signal!(e, NonConstPath)
+        }
         let arr_hint = if let ExprTypeChecked = ty_hint {
             ExprTypeChecked
         } else {
index 64429336562c56dafe56567aabd75993f5d19f69..0dbd69c0da6be0ceefb21dfd4f0bea4290d9d2da 100644 (file)
     // Allows the definition of `const fn` functions.
     ("const_fn", "1.2.0", Some(24111), Active),
 
+    // Allows indexing into constant arrays.
+    ("const_indexing", "1.4.0", Some(29947), Active),
+
     // Allows using #[prelude_import] on glob `use` items.
     //
     // rustc internal
@@ -494,6 +497,7 @@ pub struct Features {
     /// #![feature] attrs for non-language (library) features
     pub declared_lib_features: Vec<(InternedString, Span)>,
     pub const_fn: bool,
+    pub const_indexing: bool,
     pub static_recursion: bool,
     pub default_type_parameter_fallback: bool,
     pub type_macros: bool,
@@ -525,6 +529,7 @@ pub fn new() -> Features {
             declared_stable_lang_features: Vec::new(),
             declared_lib_features: Vec::new(),
             const_fn: false,
+            const_indexing: false,
             static_recursion: false,
             default_type_parameter_fallback: false,
             type_macros: false,
@@ -1097,6 +1102,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
         declared_stable_lang_features: accepted_features,
         declared_lib_features: unknown_features,
         const_fn: cx.has_feature("const_fn"),
+        const_indexing: cx.has_feature("const_indexing"),
         static_recursion: cx.has_feature("static_recursion"),
         default_type_parameter_fallback: cx.has_feature("default_type_parameter_fallback"),
         type_macros: cx.has_feature("type_macros"),
index bca3cd74d24607185c447c220a92883ba376c5c3..9c07abdc36df7b6447576487ffd4156b2c424f8f 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(const_indexing)]
+
 const ARR: [i32; 6] = [42, 43, 44, 45, 46, 47];
 const IDX: usize = 3;
 const VAL: i32 = ARR[IDX];
index da9f6bd247e0ffa036c3eca9a3cef1b7d5a9412c..15426febbcdd7aa4ae75651c6f1489b4f15b4cae 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(const_indexing)]
+
 const FOO: [u32; 3] = [1, 2, 3];
 const BAR: u32 = FOO[5]; // no error, because the error below occurs before regular const eval
 
index 63f0a844880eaa000c9debebda157bf48903f82c..3e4504eb19575738ae0669f1c8532b2a543ae666 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(const_indexing)]
 
 fn main() {
     const ARR: [i32; 6] = [42, 43, 44, 45, 46, 47];