Report let bindings and statements as unstable

This commit is contained in:
Oliver Schneider 2018-05-08 13:59:26 +02:00
parent d7bf358dbd
commit 8a5eb68958
No known key found for this signature in database
GPG Key ID: 1D5CB4FC597C3004
16 changed files with 150 additions and 65 deletions

View File

@ -32,7 +32,7 @@ use rustc::middle::lang_items;
use rustc_target::spec::abi::Abi;
use syntax::attr;
use syntax::ast::LitKind;
use syntax::feature_gate::{UnstableFeatures, emit_feature_err, GateIssue};
use syntax::feature_gate::{UnstableFeatures, feature_err, emit_feature_err, GateIssue};
use syntax_pos::{Span, DUMMY_SP};
use std::fmt;
@ -189,17 +189,12 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
fn statement_like(&mut self) {
self.add(Qualif::NOT_CONST);
if self.mode != Mode::Fn {
if self.span.allows_unstable() {
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
self.span, GateIssue::Language,
"statements in const fn are unstable");
}
let mut err = struct_span_err!(
self.tcx.sess,
let mut err = feature_err(
&self.tcx.sess.parse_sess,
"const_let",
self.span,
E0016,
"blocks in {}s are limited to items and tail expressions",
self.mode
GateIssue::Language,
&format!("statements in {}s are unstable", self.mode),
);
if self.tcx.sess.teach(&err.get_code().unwrap()) {
err.note("Blocks in constants may only contain items (such as constant, function \
@ -365,7 +360,7 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
TerminatorKind::FalseUnwind { .. } => None,
TerminatorKind::Return => {
if self.tcx.sess.features_untracked().const_let {
if !self.tcx.sess.features_untracked().const_let {
// Check for unused values. This usually means
// there are extra statements in the AST.
for temp in mir.temps_iter() {
@ -466,10 +461,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
self.not_const();
}
LocalKind::Var if !self.tcx.sess.features_untracked().const_let => {
if self.mode != Mode::Fn && self.span.allows_unstable() {
if self.mode != Mode::Fn {
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
self.span, GateIssue::Language,
"let bindings in const fn are unstable");
&format!("let bindings in {}s are unstable",self.mode));
}
self.add(Qualif::NOT_CONST);
}
@ -1105,15 +1100,11 @@ This does not pose a problem by itself because they can't be accessed directly."
// Avoid a generic error for other uses of arguments.
if self.qualif.intersects(Qualif::FN_ARGUMENT) {
let decl = &self.mir.local_decls[index];
if decl.source_info.span.allows_unstable() {
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
decl.source_info.span, GateIssue::Language,
"locals and patterns in const fn are unstable");
}
let mut err = struct_span_err!(
self.tcx.sess,
let mut err = feature_err(
&self.tcx.sess.parse_sess,
"const_let",
decl.source_info.span,
E0022,
GateIssue::Language,
"arguments of constant functions can only be immutable by-value bindings"
);
if self.tcx.sess.teach(&err.get_code().unwrap()) {

View File

@ -9,18 +9,20 @@
// except according to those terms.
const A: usize = { 1; 2 };
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR statements in constants are unstable
const B: usize = { { } 2 };
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR statements in constants are unstable
macro_rules! foo {
() => (()) //~ ERROR: blocks in constants are limited to items and tail expressions
() => (()) //~ ERROR statements in constants are unstable
}
const C: usize = { foo!(); 2 };
const D: usize = { let x = 4; 2 };
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
//~| ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
pub fn main() {}

View File

@ -9,7 +9,9 @@
// except according to those terms.
type Array = [u32; { let x = 2; 5 }];
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
//~| ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
pub fn main() {}

View File

@ -10,8 +10,10 @@
enum Foo {
Bar = { let x = 1; 3 }
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
//~| ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
}
pub fn main() {}

View File

@ -8,16 +8,20 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// test that certain things are disallowed in const fn signatures
// test that certain things are disallowed in constant functionssignatures
#![feature(const_fn)]
// no destructuring
const fn i((
a, //~ ERROR: E0022
b //~ ERROR: E0022
a,
//~^ ERROR arguments of constant functions can only be immutable by-value bindings
b
//~^ ERROR arguments of constant functions can only be immutable by-value bindings
): (u32, u32)) -> u32 {
a + b
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR let bindings in constant functions are unstable
}
fn main() {}

View File

@ -38,9 +38,15 @@ const fn get_Y_addr() -> &'static u32 {
}
const fn get() -> u32 {
let x = 22; //~ ERROR E0016
let y = 44; //~ ERROR E0016
let x = 22;
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR statements in constant functions are unstable
let y = 44;
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR statements in constant functions are unstable
x + y
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR let bindings in constant functions are unstable
}
fn main() {

View File

@ -10,9 +10,12 @@
pub fn main() {
const z: &'static isize = {
//~^ ERROR blocks in constants are limited to items and tail expressions
//~^ ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
let p = 3;
//~^ ERROR blocks in constants are limited to items and tail expressions
//~^ ERROR let bindings in constants are unstable
//~| ERROR statements in constants are unstable
&p //~ ERROR `p` does not live long enough
//~^ ERROR let bindings in constants are unstable
};
}

View File

@ -11,8 +11,12 @@
#![feature(const_fn)]
const fn x() {
let t = true; //~ ERROR blocks in constant functions are limited to items and tail expressions
let x = || t; //~ ERROR blocks in constant functions are limited to items and tail expressions
let t = true;
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR statements in constant functions are unstable
let x = || t;
//~^ ERROR let bindings in constant functions are unstable
//~| ERROR statements in constant functions are unstable
}
fn main() {}

View File

@ -14,7 +14,8 @@
const bad : u32 = {
{
5; //~ ERROR: blocks in constants are limited to items and tail expressions
5;
//~^ ERROR statements in constants are unstable
0
}
};
@ -22,7 +23,7 @@ const bad : u32 = {
const bad_two : u32 = {
{
invalid();
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR statements in constants are unstable
//~^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
0
}
@ -31,14 +32,15 @@ const bad_two : u32 = {
const bad_three : u32 = {
{
valid();
//~^ ERROR: blocks in constants are limited to items and tail expressions
//~^ ERROR statements in constants are unstable
0
}
};
static bad_four : u32 = {
{
5; //~ ERROR: blocks in statics are limited to items and tail expressions
5;
//~^ ERROR statements in statics are unstable
0
}
};
@ -46,8 +48,8 @@ static bad_four : u32 = {
static bad_five : u32 = {
{
invalid();
//~^ ERROR: blocks in statics are limited to items and tail expressions
//~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
//~^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
//~| ERROR statements in statics are unstable
0
}
};
@ -55,14 +57,15 @@ static bad_five : u32 = {
static bad_six : u32 = {
{
valid();
//~^ ERROR: blocks in statics are limited to items and tail expressions
//~^ ERROR statements in statics are unstable
0
}
};
static mut bad_seven : u32 = {
{
5; //~ ERROR: blocks in statics are limited to items and tail expressions
5;
//~^ ERROR statements in statics are unstable
0
}
};
@ -70,8 +73,8 @@ static mut bad_seven : u32 = {
static mut bad_eight : u32 = {
{
invalid();
//~^ ERROR: blocks in statics are limited to items and tail expressions
//~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
//~^ ERROR statements in statics are unstable
//~| ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
0
}
};
@ -79,7 +82,7 @@ static mut bad_eight : u32 = {
static mut bad_nine : u32 = {
{
valid();
//~^ ERROR: blocks in statics are limited to items and tail expressions
//~^ ERROR statements in statics are unstable
0
}
};

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// test that certain things are disallowed in const fn signatures
// test that certain things are disallowed in constant functionssignatures
#![feature(const_fn, const_let)]

View File

@ -0,0 +1,30 @@
// Copyright 2018 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.
#![feature(const_let)]
fn main() {}
struct FakeNeedsDrop;
impl Drop for FakeNeedsDrop {
fn drop(&mut self) {}
}
// ok
const X: FakeNeedsDrop = { let x = FakeNeedsDrop; x };
// error
const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
//~^ ERROR constant contains unimplemented expression type
// error
const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
//~^ ERROR constant contains unimplemented expression type

View File

@ -0,0 +1,15 @@
error[E0019]: constant contains unimplemented expression type
--> $DIR/const_let.rs:25:55
|
LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
| ^
error[E0019]: constant contains unimplemented expression type
--> $DIR/const_let.rs:29:35
|
LL | const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
| ^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0019`.

View File

@ -14,7 +14,8 @@ const X : usize = 2;
const fn f(x: usize) -> usize {
let mut sum = 0;
//~^ ERROR E0016
//~^ let bindings in constant functions are unstable
//~| statements in constant functions are unstable
for i in 0..x {
//~^ ERROR E0015
//~| ERROR E0019

View File

@ -1,23 +1,33 @@
error[E0016]: blocks in constant functions are limited to items and tail expressions
error[E0658]: let bindings in constant functions are unstable (see issue #48821)
--> $DIR/const-fn-error.rs:16:19
|
LL | let mut sum = 0;
| ^
|
= help: add #![feature(const_let)] to the crate attributes to enable
error[E0658]: statements in constant functions are unstable (see issue #48821)
--> $DIR/const-fn-error.rs:16:19
|
LL | let mut sum = 0;
| ^
|
= help: add #![feature(const_let)] to the crate attributes to enable
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/const-fn-error.rs:18:14
--> $DIR/const-fn-error.rs:19:14
|
LL | for i in 0..x {
| ^^^^
error[E0019]: constant function contains unimplemented expression type
--> $DIR/const-fn-error.rs:18:14
--> $DIR/const-fn-error.rs:19:14
|
LL | for i in 0..x {
| ^^^^
error[E0080]: constant evaluation error
--> $DIR/const-fn-error.rs:18:14
--> $DIR/const-fn-error.rs:19:14
|
LL | for i in 0..x {
| ^^^^ calling non-const fn `<I as std::iter::IntoIterator><std::ops::Range<usize>>::into_iter`
@ -26,12 +36,12 @@ LL | let a : [i32; f(X)];
| ---- inside call to `f`
|
note: for constant expression here
--> $DIR/const-fn-error.rs:29:13
--> $DIR/const-fn-error.rs:30:13
|
LL | let a : [i32; f(X)];
| ^^^^^^^^^^^
error: aborting due to 4 previous errors
error: aborting due to 5 previous errors
Some errors occurred: E0015, E0016, E0019, E0080.
Some errors occurred: E0015, E0019, E0080, E0658.
For more information about an error, try `rustc --explain E0015`.

View File

@ -13,7 +13,9 @@
#![feature(const_fn)]
const fn foo() -> usize {
let x = 42; //~ ERROR blocks in constant functions are limited to items and tail expressions
let x = 42;
//~^ ERROR statements in constant functions are unstable
//~| ERROR: let bindings in constant functions are unstable
42
}

View File

@ -1,9 +1,19 @@
error[E0016]: blocks in constant functions are limited to items and tail expressions
error[E0658]: let bindings in constant functions are unstable (see issue #48821)
--> $DIR/feature-gate-const_let.rs:16:13
|
LL | let x = 42; //~ ERROR blocks in constant functions are limited to items and tail expressions
LL | let x = 42;
| ^^
|
= help: add #![feature(const_let)] to the crate attributes to enable
error: aborting due to previous error
error[E0658]: statements in constant functions are unstable (see issue #48821)
--> $DIR/feature-gate-const_let.rs:16:13
|
LL | let x = 42;
| ^^
|
= help: add #![feature(const_let)] to the crate attributes to enable
For more information about this error, try `rustc --explain E0016`.
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0658`.