Skip to content

Commit ed34cd1

Browse files
committed
Better error message when using vectors in statics
Closes #10487
1 parent 5d466ff commit ed34cd1

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/librustc/middle/check_const.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
117117
ExprUnary(_, UnDeref, _) => { }
118118
ExprUnary(_, UnBox(_), _) | ExprUnary(_, UnUniq, _) => {
119119
sess.span_err(e.span,
120-
"disallowed operator in constant expression");
120+
"cannot do allocations in constant expressions");
121121
return;
122122
}
123123
ExprLit(@codemap::Spanned {node: lit_str(*), _}) => { }
@@ -191,7 +191,13 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
191191
e.span,
192192
"borrowed pointers in constants may only refer to \
193193
immutable values");
194-
}
194+
},
195+
ExprVstore(_, ExprVstoreUniq) |
196+
ExprVstore(_, ExprVstoreBox) |
197+
ExprVstore(_, ExprVstoreMutBox) => {
198+
sess.span_err(e.span, "cannot allocate vectors in constant expressions")
199+
},
200+
195201
_ => {
196202
sess.span_err(e.span,
197203
"constant contains unimplemented expression type");

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 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(managed_boxes)];
12+
13+
static x: ~[int] = ~[123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
14+
static y: @[int] = @[123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
15+
static z: @mut [int] = @mut [123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
16+
17+
fn main() {}

src/test/compile-fail/static-mut-not-constant.rs

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

11-
static mut a: ~int = ~3; //~ ERROR: disallowed operator in constant
11+
static mut a: ~int = ~3; //~ ERROR: cannot do allocations in constant expressions
1212

1313
fn main() {}

0 commit comments

Comments
 (0)