Skip to content

Commit d11b48c

Browse files
committed
Error out when using static_assert on a non-boolean value
static_assert is documented as working on static with type `bool`, but we currently accept it on any const static and crash when the const has an non-integral type. This is a breaking-change for anyone who used static_assert on types likes i32, which happened to work but seems like an unintended consequence of the missing error checking. [breaking-change] Fixes rust-lang#22056
1 parent 48aeaba commit d11b48c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/librustc_trans/trans/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,11 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
23322332
// Do static_assert checking. It can't really be done much earlier
23332333
// because we need to get the value of the bool out of LLVM
23342334
if attr::contains_name(&item.attrs, "static_assert") {
2335+
if !ty::type_is_bool(ty::expr_ty(ccx.tcx(), expr)) {
2336+
ccx.sess().span_fatal(expr.span,
2337+
"can only have static_assert on a static \
2338+
with type `bool`");
2339+
}
23352340
if m == ast::MutMutable {
23362341
ccx.sess().span_fatal(expr.span,
23372342
"cannot have static_assert on a mutable \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
#![allow(dead_code)]
12+
13+
#[static_assert]
14+
static E: i32 = 1; //~ ERROR can only have static_assert on a static with type `bool`
15+
16+
fn main() {}

0 commit comments

Comments
 (0)