Skip to content

Commit 8a5eb68

Browse files
committed
Report let bindings and statements as unstable
1 parent d7bf358 commit 8a5eb68

16 files changed

+150
-65
lines changed

src/librustc_mir/transform/qualify_consts.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc::middle::lang_items;
3232
use rustc_target::spec::abi::Abi;
3333
use syntax::attr;
3434
use syntax::ast::LitKind;
35-
use syntax::feature_gate::{UnstableFeatures, emit_feature_err, GateIssue};
35+
use syntax::feature_gate::{UnstableFeatures, feature_err, emit_feature_err, GateIssue};
3636
use syntax_pos::{Span, DUMMY_SP};
3737

3838
use std::fmt;
@@ -189,17 +189,12 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
189189
fn statement_like(&mut self) {
190190
self.add(Qualif::NOT_CONST);
191191
if self.mode != Mode::Fn {
192-
if self.span.allows_unstable() {
193-
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
194-
self.span, GateIssue::Language,
195-
"statements in const fn are unstable");
196-
}
197-
let mut err = struct_span_err!(
198-
self.tcx.sess,
192+
let mut err = feature_err(
193+
&self.tcx.sess.parse_sess,
194+
"const_let",
199195
self.span,
200-
E0016,
201-
"blocks in {}s are limited to items and tail expressions",
202-
self.mode
196+
GateIssue::Language,
197+
&format!("statements in {}s are unstable", self.mode),
203198
);
204199
if self.tcx.sess.teach(&err.get_code().unwrap()) {
205200
err.note("Blocks in constants may only contain items (such as constant, function \
@@ -365,7 +360,7 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
365360
TerminatorKind::FalseUnwind { .. } => None,
366361

367362
TerminatorKind::Return => {
368-
if self.tcx.sess.features_untracked().const_let {
363+
if !self.tcx.sess.features_untracked().const_let {
369364
// Check for unused values. This usually means
370365
// there are extra statements in the AST.
371366
for temp in mir.temps_iter() {
@@ -466,10 +461,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
466461
self.not_const();
467462
}
468463
LocalKind::Var if !self.tcx.sess.features_untracked().const_let => {
469-
if self.mode != Mode::Fn && self.span.allows_unstable() {
464+
if self.mode != Mode::Fn {
470465
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
471466
self.span, GateIssue::Language,
472-
"let bindings in const fn are unstable");
467+
&format!("let bindings in {}s are unstable",self.mode));
473468
}
474469
self.add(Qualif::NOT_CONST);
475470
}
@@ -1105,15 +1100,11 @@ This does not pose a problem by itself because they can't be accessed directly."
11051100
// Avoid a generic error for other uses of arguments.
11061101
if self.qualif.intersects(Qualif::FN_ARGUMENT) {
11071102
let decl = &self.mir.local_decls[index];
1108-
if decl.source_info.span.allows_unstable() {
1109-
emit_feature_err(&self.tcx.sess.parse_sess, "const_let",
1110-
decl.source_info.span, GateIssue::Language,
1111-
"locals and patterns in const fn are unstable");
1112-
}
1113-
let mut err = struct_span_err!(
1114-
self.tcx.sess,
1103+
let mut err = feature_err(
1104+
&self.tcx.sess.parse_sess,
1105+
"const_let",
11151106
decl.source_info.span,
1116-
E0022,
1107+
GateIssue::Language,
11171108
"arguments of constant functions can only be immutable by-value bindings"
11181109
);
11191110
if self.tcx.sess.teach(&err.get_code().unwrap()) {

src/test/compile-fail/const-block-non-item-statement-2.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
// except according to those terms.
1010

1111
const A: usize = { 1; 2 };
12-
//~^ ERROR: blocks in constants are limited to items and tail expressions
12+
//~^ ERROR statements in constants are unstable
1313

1414
const B: usize = { { } 2 };
15-
//~^ ERROR: blocks in constants are limited to items and tail expressions
15+
//~^ ERROR statements in constants are unstable
1616

1717
macro_rules! foo {
18-
() => (()) //~ ERROR: blocks in constants are limited to items and tail expressions
18+
() => (()) //~ ERROR statements in constants are unstable
1919
}
2020
const C: usize = { foo!(); 2 };
2121

2222
const D: usize = { let x = 4; 2 };
23-
//~^ ERROR: blocks in constants are limited to items and tail expressions
24-
//~^^ ERROR: blocks in constants are limited to items and tail expressions
23+
//~^ ERROR let bindings in constants are unstable
24+
//~| ERROR statements in constants are unstable
25+
//~| ERROR let bindings in constants are unstable
26+
//~| ERROR statements in constants are unstable
2527

2628
pub fn main() {}

src/test/compile-fail/const-block-non-item-statement-3.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
// except according to those terms.
1010

1111
type Array = [u32; { let x = 2; 5 }];
12-
//~^ ERROR: blocks in constants are limited to items and tail expressions
13-
//~^^ ERROR: blocks in constants are limited to items and tail expressions
12+
//~^ ERROR let bindings in constants are unstable
13+
//~| ERROR statements in constants are unstable
14+
//~| ERROR let bindings in constants are unstable
15+
//~| ERROR statements in constants are unstable
1416

1517
pub fn main() {}

src/test/compile-fail/const-block-non-item-statement.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
enum Foo {
1212
Bar = { let x = 1; 3 }
13-
//~^ ERROR: blocks in constants are limited to items and tail expressions
14-
//~^^ ERROR: blocks in constants are limited to items and tail expressions
13+
//~^ ERROR let bindings in constants are unstable
14+
//~| ERROR statements in constants are unstable
15+
//~| ERROR let bindings in constants are unstable
16+
//~| ERROR statements in constants are unstable
1517
}
1618

1719
pub fn main() {}

src/test/compile-fail/const-fn-destructuring-arg.rs

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

11-
// test that certain things are disallowed in const fn signatures
11+
// test that certain things are disallowed in constant functionssignatures
1212

1313
#![feature(const_fn)]
1414

1515
// no destructuring
1616
const fn i((
17-
a, //~ ERROR: E0022
18-
b //~ ERROR: E0022
17+
a,
18+
//~^ ERROR arguments of constant functions can only be immutable by-value bindings
19+
b
20+
//~^ ERROR arguments of constant functions can only be immutable by-value bindings
1921
): (u32, u32)) -> u32 {
2022
a + b
23+
//~^ ERROR let bindings in constant functions are unstable
24+
//~| ERROR let bindings in constant functions are unstable
2125
}
2226

2327
fn main() {}

src/test/compile-fail/const-fn-not-safe-for-const.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ const fn get_Y_addr() -> &'static u32 {
3838
}
3939

4040
const fn get() -> u32 {
41-
let x = 22; //~ ERROR E0016
42-
let y = 44; //~ ERROR E0016
41+
let x = 22;
42+
//~^ ERROR let bindings in constant functions are unstable
43+
//~| ERROR statements in constant functions are unstable
44+
let y = 44;
45+
//~^ ERROR let bindings in constant functions are unstable
46+
//~| ERROR statements in constant functions are unstable
4347
x + y
48+
//~^ ERROR let bindings in constant functions are unstable
49+
//~| ERROR let bindings in constant functions are unstable
4450
}
4551

4652
fn main() {

src/test/compile-fail/issue-18118.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
pub fn main() {
1212
const z: &'static isize = {
13-
//~^ ERROR blocks in constants are limited to items and tail expressions
13+
//~^ ERROR let bindings in constants are unstable
14+
//~| ERROR statements in constants are unstable
1415
let p = 3;
15-
//~^ ERROR blocks in constants are limited to items and tail expressions
16+
//~^ ERROR let bindings in constants are unstable
17+
//~| ERROR statements in constants are unstable
1618
&p //~ ERROR `p` does not live long enough
19+
//~^ ERROR let bindings in constants are unstable
1720
};
1821
}

src/test/compile-fail/issue-37550.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
#![feature(const_fn)]
1212

1313
const fn x() {
14-
let t = true; //~ ERROR blocks in constant functions are limited to items and tail expressions
15-
let x = || t; //~ ERROR blocks in constant functions are limited to items and tail expressions
14+
let t = true;
15+
//~^ ERROR let bindings in constant functions are unstable
16+
//~| ERROR statements in constant functions are unstable
17+
let x = || t;
18+
//~^ ERROR let bindings in constant functions are unstable
19+
//~| ERROR statements in constant functions are unstable
1620
}
1721

1822
fn main() {}

src/test/compile-fail/issue32829.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414

1515
const bad : u32 = {
1616
{
17-
5; //~ ERROR: blocks in constants are limited to items and tail expressions
17+
5;
18+
//~^ ERROR statements in constants are unstable
1819
0
1920
}
2021
};
2122

2223
const bad_two : u32 = {
2324
{
2425
invalid();
25-
//~^ ERROR: blocks in constants are limited to items and tail expressions
26+
//~^ ERROR statements in constants are unstable
2627
//~^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
2728
0
2829
}
@@ -31,55 +32,57 @@ const bad_two : u32 = {
3132
const bad_three : u32 = {
3233
{
3334
valid();
34-
//~^ ERROR: blocks in constants are limited to items and tail expressions
35+
//~^ ERROR statements in constants are unstable
3536
0
3637
}
3738
};
3839

3940
static bad_four : u32 = {
4041
{
41-
5; //~ ERROR: blocks in statics are limited to items and tail expressions
42+
5;
43+
//~^ ERROR statements in statics are unstable
4244
0
4345
}
4446
};
4547

4648
static bad_five : u32 = {
4749
{
4850
invalid();
49-
//~^ ERROR: blocks in statics are limited to items and tail expressions
50-
//~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
51+
//~^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
52+
//~| ERROR statements in statics are unstable
5153
0
5254
}
5355
};
5456

5557
static bad_six : u32 = {
5658
{
5759
valid();
58-
//~^ ERROR: blocks in statics are limited to items and tail expressions
60+
//~^ ERROR statements in statics are unstable
5961
0
6062
}
6163
};
6264

6365
static mut bad_seven : u32 = {
6466
{
65-
5; //~ ERROR: blocks in statics are limited to items and tail expressions
67+
5;
68+
//~^ ERROR statements in statics are unstable
6669
0
6770
}
6871
};
6972

7073
static mut bad_eight : u32 = {
7174
{
7275
invalid();
73-
//~^ ERROR: blocks in statics are limited to items and tail expressions
74-
//~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
76+
//~^ ERROR statements in statics are unstable
77+
//~| ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
7578
0
7679
}
7780
};
7881

7982
static mut bad_nine : u32 = {
8083
{
8184
valid();
82-
//~^ ERROR: blocks in statics are limited to items and tail expressions
85+
//~^ ERROR statements in statics are unstable
8386
0
8487
}
8588
};

src/test/run-pass/ctfe/const-fn-destructuring-arg.rs

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

11-
// test that certain things are disallowed in const fn signatures
11+
// test that certain things are disallowed in constant functionssignatures
1212

1313
#![feature(const_fn, const_let)]
1414

src/test/ui/const-eval/const_let.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 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+
#![feature(const_let)]
12+
13+
fn main() {}
14+
15+
struct FakeNeedsDrop;
16+
17+
impl Drop for FakeNeedsDrop {
18+
fn drop(&mut self) {}
19+
}
20+
21+
// ok
22+
const X: FakeNeedsDrop = { let x = FakeNeedsDrop; x };
23+
24+
// error
25+
const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
26+
//~^ ERROR constant contains unimplemented expression type
27+
28+
// error
29+
const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
30+
//~^ ERROR constant contains unimplemented expression type
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0019]: constant contains unimplemented expression type
2+
--> $DIR/const_let.rs:25:55
3+
|
4+
LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
5+
| ^
6+
7+
error[E0019]: constant contains unimplemented expression type
8+
--> $DIR/const_let.rs:29:35
9+
|
10+
LL | const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
11+
| ^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0019`.

src/test/ui/const-fn-error.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const X : usize = 2;
1414

1515
const fn f(x: usize) -> usize {
1616
let mut sum = 0;
17-
//~^ ERROR E0016
17+
//~^ let bindings in constant functions are unstable
18+
//~| statements in constant functions are unstable
1819
for i in 0..x {
1920
//~^ ERROR E0015
2021
//~| ERROR E0019

0 commit comments

Comments
 (0)