Skip to content

Commit c195783

Browse files
committed
Feature gate #[static_assert].
The API this exposes is a little strange (being attached to `static`s), so it makes sense to conservatively feature gate it. If it is highly popular, it is possible to reverse this gating.
1 parent 1576142 commit c195783

9 files changed

+36
-13
lines changed

src/doc/reference.md

+6
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,12 @@ The currently implemented features of the reference compiler are:
24952495

24962496
* `staged_api` - Allows usage of stability markers and `#![staged_api]` in a crate
24972497

2498+
* `static_assert` - The `#[static_assert]` functionality is experimental and
2499+
unstable. The attribute can be attached to a `static` of
2500+
type `bool` and the compiler will error if the `bool` is
2501+
`false` at compile time. This version of this functionality
2502+
is unintuitive and suboptimal.
2503+
24982504
* `start` - Allows use of the `#[start]` attribute, which changes the entry point
24992505
into a Rust program. This capabiilty, especially the signature for the
25002506
annotated function, is subject to change.

src/libsyntax/feature_gate.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ static KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[
139139

140140
// Allows the use of rustc_* attributes; RFC 572
141141
("rustc_attrs", "1.0.0", Active),
142+
143+
// Allows the use of `static_assert`
144+
("static_assert", "1.0.0", Active),
142145
];
143146
// (changing above list without updating src/doc/reference.md makes @cmr sad)
144147

@@ -241,7 +244,8 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[
241244
("no_split_stack", Whitelisted),
242245
("no_stack_check", Whitelisted),
243246
("packed", Whitelisted),
244-
("static_assert", Whitelisted),
247+
("static_assert", Gated("static_assert",
248+
"`#[static_assert]` is an experimental feature, and has a poor API")),
245249
("no_debug", Whitelisted),
246250
("omit_gdb_pretty_printer_section", Whitelisted),
247251
("unsafe_no_drop_flag", Gated("unsafe_no_drop_flag",
@@ -769,4 +773,3 @@ pub fn check_crate(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate)
769773
|ctx, krate| visit::walk_crate(&mut PostExpansionVisitor { context: ctx },
770774
krate))
771775
}
772-

src/test/compile-fail/asm-misplaced-option.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
// ignore-android
1212

13-
#![feature(asm)]
13+
#![feature(asm, rustc_attrs)]
1414

1515
#![allow(dead_code, non_upper_case_globals)]
1616

1717
#[cfg(any(target_arch = "x86",
1818
target_arch = "x86_64"))]
19-
pub fn main() {
19+
#[rustc_error]
20+
pub fn main() { //~ ERROR compilation successful
2021
// assignment not dead
2122
let mut x: isize = 0;
2223
unsafe {
@@ -33,7 +34,3 @@ pub fn main() {
3334
}
3435
assert_eq!(x, 13);
3536
}
36-
37-
// At least one error is needed so that compilation fails
38-
#[static_assert]
39-
static b: bool = false; //~ ERROR static assertion failed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
#[static_assert] //~ ERROR `#[static_assert]` is an experimental feature
12+
static X: bool = true;
13+
14+
fn main() {}

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

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

11+
#![feature(rustc_attrs)]
1112
#![allow(dead_code)]
1213

1314
// Matching against NaN should result in a warning
1415

1516
use std::f64::NAN;
1617

17-
fn main() {
18+
#[rustc_error]
19+
fn main() { //~ ERROR compilation successful
1820
let x = NAN;
1921
match x {
2022
NAN => {},
@@ -27,7 +29,3 @@ fn main() {
2729
};
2830
//~^^^ WARNING unmatchable NaN in pattern, use the is_nan method in a guard instead
2931
}
30-
31-
// At least one error is needed so that compilation fails
32-
#[static_assert]
33-
static B: bool = false; //~ ERROR static assertion failed

src/test/compile-fail/nonbool_static_assert.rs

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

11+
#![feature(static_assert)]
1112
#![allow(dead_code)]
1213

1314
#[static_assert]

src/test/compile-fail/static-assert.rs

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

11+
#![feature(static_assert)]
1112
#![allow(dead_code)]
1213

1314
#[static_assert]

src/test/compile-fail/static-assert2.rs

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

11+
#![feature(static_assert)]
1112
#![allow(dead_code)]
1213

1314
#[static_assert]

src/test/run-pass/static-assert.rs

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

11+
#![feature(static_assert)]
12+
1113
#[static_assert]
1214
static b: bool = true;
1315

0 commit comments

Comments
 (0)