Skip to content

Commit 4dbdfb4

Browse files
committed
Auto merge of #30202 - oli-obk:fix/const_index_feature_gate, r=Aatch
see #29947 (comment) I also added some missing tests
2 parents 7b77f67 + baa8ce7 commit 4dbdfb4

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

src/librustc/middle/check_const.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
// by borrowck::gather_loans
2626

2727
use middle::ty::cast::{CastKind};
28-
use middle::const_eval;
28+
use middle::const_eval::{self, ConstEvalErr};
29+
use middle::const_eval::ErrKind::IndexOpFeatureGated;
2930
use middle::const_eval::EvalHint::ExprTypeChecked;
3031
use middle::def;
3132
use middle::def_id::DefId;
@@ -477,6 +478,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
477478
match const_eval::eval_const_expr_partial(
478479
self.tcx, ex, ExprTypeChecked, None) {
479480
Ok(_) => {}
481+
Err(ConstEvalErr { kind: IndexOpFeatureGated, ..}) => {},
480482
Err(msg) => {
481483
self.tcx.sess.add_lint(::lint::builtin::CONST_ERR, ex.id,
482484
msg.span,

src/librustc/middle/const_eval.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ pub enum ErrKind {
434434

435435
MiscBinaryOp,
436436
MiscCatchAll,
437+
438+
IndexOpFeatureGated,
437439
}
438440

439441
impl ConstEvalErr {
@@ -483,6 +485,7 @@ impl ConstEvalErr {
483485

484486
MiscBinaryOp => "bad operands for binary".into_cow(),
485487
MiscCatchAll => "unsupported constant expr".into_cow(),
488+
IndexOpFeatureGated => "the index operation on const values is unstable".into_cow(),
486489
}
487490
}
488491
}
@@ -1119,15 +1122,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11191122
hir::ExprStruct(..) => Struct(e.id),
11201123
hir::ExprIndex(ref arr, ref idx) => {
11211124
if !tcx.sess.features.borrow().const_indexing {
1122-
tcx.sess.span_err(
1123-
e.span,
1124-
"const indexing is an unstable feature");
1125-
fileline_help!(
1126-
tcx.sess,
1127-
e.span,
1128-
"in Nightly builds, add `#![feature(const_indexing)]` to the crate \
1129-
attributes to enable");
1130-
signal!(e, NonConstPath)
1125+
signal!(e, IndexOpFeatureGated);
11311126
}
11321127
let arr_hint = if let ExprTypeChecked = ty_hint {
11331128
ExprTypeChecked
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
const ARR: [usize; 1] = [2];
12+
const ARR2: [i32; ARR[0]] = [5, 6]; //~ ERROR unstable
13+
14+
fn main() {
15+
}

src/test/compile-fail/lint-exceeding-bitshifts.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![deny(exceeding_bitshifts)]
1212
#![allow(unused_variables)]
1313
#![allow(dead_code)]
14-
#![feature(num_bits_bytes)]
14+
#![feature(num_bits_bytes, const_indexing)]
1515

1616
fn main() {
1717
let n = 1u8 << 7;
@@ -62,4 +62,7 @@ fn main() {
6262

6363

6464
let n = 1i8<<(1isize+-1);
65+
66+
let n = 1i64 >> [63][0];
67+
let n = 1i64 >> [64][0]; //~ ERROR: bitshift exceeds the type's number of bits
6568
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
const ARR: [usize; 1] = [2];
12+
13+
fn main() {
14+
let _ = 5 << ARR[0];
15+
}

0 commit comments

Comments
 (0)