Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix const index feature-gate regression #30202

Merged
merged 1 commit into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
// by borrowck::gather_loans

use middle::ty::cast::{CastKind};
use middle::const_eval;
use middle::const_eval::{self, ConstEvalErr};
use middle::const_eval::ErrKind::IndexOpFeatureGated;
use middle::const_eval::EvalHint::ExprTypeChecked;
use middle::def;
use middle::def_id::DefId;
Expand Down Expand Up @@ -477,6 +478,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
match const_eval::eval_const_expr_partial(
self.tcx, ex, ExprTypeChecked, None) {
Ok(_) => {}
Err(ConstEvalErr { kind: IndexOpFeatureGated, ..}) => {},
Err(msg) => {
self.tcx.sess.add_lint(::lint::builtin::CONST_ERR, ex.id,
msg.span,
Expand Down
13 changes: 4 additions & 9 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ pub enum ErrKind {

MiscBinaryOp,
MiscCatchAll,

IndexOpFeatureGated,
}

impl ConstEvalErr {
Expand Down Expand Up @@ -483,6 +485,7 @@ impl ConstEvalErr {

MiscBinaryOp => "bad operands for binary".into_cow(),
MiscCatchAll => "unsupported constant expr".into_cow(),
IndexOpFeatureGated => "the index operation on const values is unstable".into_cow(),
}
}
}
Expand Down Expand Up @@ -1119,15 +1122,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
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)
signal!(e, IndexOpFeatureGated);
}
let arr_hint = if let ExprTypeChecked = ty_hint {
ExprTypeChecked
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/const-index-feature-gate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

const ARR: [usize; 1] = [2];
const ARR2: [i32; ARR[0]] = [5, 6]; //~ ERROR unstable

fn main() {
}
5 changes: 4 additions & 1 deletion src/test/compile-fail/lint-exceeding-bitshifts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![deny(exceeding_bitshifts)]
#![allow(unused_variables)]
#![allow(dead_code)]
#![feature(num_bits_bytes)]
#![feature(num_bits_bytes, const_indexing)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting ... am I right in inferring that this program will compile successfully (and the error occurs at runtime, assuming the checks are turned on), if the feature gate is not included here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(because I thought we had decided that such errors in such contexts should not cause compilation errors, at least not ones that cannot be controlled via lint-style allow/warn/deny switches, but instead should just generate code to panic at runtime?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bitshift checks have been errors since before 1.0. I'll investigate if anything assumes they errored in check_const.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a problem. The "error" is actually the exceeding_bitshifts lint which is set to deny here. So it's already behind a lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Sorry, I was not clear about what I was saying.

I was worried about the fact that you added the feature gate opt-in, since this very bug (that this PR is addressing) is about the fact that people shouldn't have to add this feature gate to get this code to work.

But it seems like you are saying that you need the feature gate there in order to see the compile-time error on just the line that you added, and without the feature gate, there would be no compile-time error -- i.e. the lint wouldn't detect the excessive bitshift at compile-time.

I would have preferred such a subtle issue be broken out into a separate test. But its not worth blocking the PR at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so a run-fail test that does the same as this file just without the feature gate? I think there's a bitshift overflow run-fail test. I'll add it there.


fn main() {
let n = 1u8 << 7;
Expand Down Expand Up @@ -62,4 +62,7 @@ fn main() {


let n = 1i8<<(1isize+-1);

let n = 1i64 >> [63][0];
let n = 1i64 >> [64][0]; //~ ERROR: bitshift exceeds the type's number of bits
}
15 changes: 15 additions & 0 deletions src/test/run-pass/check_const-feature-gated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

const ARR: [usize; 1] = [2];

fn main() {
let _ = 5 << ARR[0];
}