Skip to content

Commit de27064

Browse files
committed
Disallow negative counts in repeat expressions
1 parent 13aa188 commit de27064

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/librustc/middle/ty.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,23 +4315,30 @@ pub fn normalize_ty(cx: ctxt, t: t) -> t {
43154315
pub fn eval_repeat_count(tcx: ctxt, count_expr: @ast::expr) -> uint {
43164316
match const_eval::eval_const_expr_partial(tcx, count_expr) {
43174317
Ok(ref const_val) => match *const_val {
4318-
const_eval::const_int(count) => return count as uint,
4318+
const_eval::const_int(count) => if count < 0 {
4319+
tcx.sess.span_err(count_expr.span,
4320+
"expected positive integer for \
4321+
repeat count but found negative integer");
4322+
return 0;
4323+
} else {
4324+
return count as uint
4325+
},
43194326
const_eval::const_uint(count) => return count as uint,
43204327
const_eval::const_float(count) => {
43214328
tcx.sess.span_err(count_expr.span,
4322-
"expected signed or unsigned integer for \
4329+
"expected positive integer for \
43234330
repeat count but found float");
43244331
return count as uint;
43254332
}
43264333
const_eval::const_str(_) => {
43274334
tcx.sess.span_err(count_expr.span,
4328-
"expected signed or unsigned integer for \
4335+
"expected positive integer for \
43294336
repeat count but found string");
43304337
return 0;
43314338
}
43324339
const_eval::const_bool(_) => {
43334340
tcx.sess.span_err(count_expr.span,
4334-
"expected signed or unsigned integer for \
4341+
"expected positive integer for \
43354342
repeat count but found boolean");
43364343
return 0;
43374344
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//xfail-test
2+
3+
// Trying to create a fixed-length vector with a negative size
4+
5+
fn main() {
6+
let _x = [0,..-1];
7+
}

0 commit comments

Comments
 (0)