Skip to content

Commit 5dbddfb

Browse files
committed
Auto merge of #26935 - oli-obk:const_val_description, r=eddyb
r? @eddyb Adding new variants is annoying as one needs to modify all these places that **don't** handle the new variant. I chose not to use `Display` as I don't think it is appropriate.
2 parents 90a3692 + 441b994 commit 5dbddfb

File tree

3 files changed

+51
-54
lines changed

3 files changed

+51
-54
lines changed

src/librustc/middle/const_eval.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ pub enum ConstVal {
271271
Tuple(ast::NodeId),
272272
}
273273

274+
impl ConstVal {
275+
pub fn description(&self) -> &'static str {
276+
match *self {
277+
Float(_) => "float",
278+
Int(i) if i < 0 => "negative integer",
279+
Int(_) => "positive integer",
280+
Uint(_) => "unsigned integer",
281+
Str(_) => "string literal",
282+
Binary(_) => "binary array",
283+
Bool(_) => "boolean",
284+
Struct(_) => "struct",
285+
Tuple(_) => "tuple",
286+
}
287+
}
288+
}
289+
274290
pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<ast::Pat> {
275291
let pat = match expr.node {
276292
ast::ExprTup(ref exprs) =>
@@ -350,16 +366,8 @@ pub enum ErrKind {
350366
InvalidOpForFloats(ast::BinOp_),
351367
InvalidOpForIntUint(ast::BinOp_),
352368
InvalidOpForUintInt(ast::BinOp_),
353-
NegateOnString,
354-
NegateOnBoolean,
355-
NegateOnBinary,
356-
NegateOnStruct,
357-
NegateOnTuple,
358-
NotOnFloat,
359-
NotOnString,
360-
NotOnBinary,
361-
NotOnStruct,
362-
NotOnTuple,
369+
NegateOn(ConstVal),
370+
NotOn(ConstVal),
363371

364372
NegateWithOverflow(i64),
365373
AddiWithOverflow(i64, i64),
@@ -395,16 +403,8 @@ impl ConstEvalErr {
395403
InvalidOpForFloats(_) => "can't do this op on floats".into_cow(),
396404
InvalidOpForIntUint(..) => "can't do this op on an isize and usize".into_cow(),
397405
InvalidOpForUintInt(..) => "can't do this op on a usize and isize".into_cow(),
398-
NegateOnString => "negate on string".into_cow(),
399-
NegateOnBoolean => "negate on boolean".into_cow(),
400-
NegateOnBinary => "negate on binary literal".into_cow(),
401-
NegateOnStruct => "negate on struct".into_cow(),
402-
NegateOnTuple => "negate on tuple".into_cow(),
403-
NotOnFloat => "not on float or string".into_cow(),
404-
NotOnString => "not on float or string".into_cow(),
405-
NotOnBinary => "not on binary literal".into_cow(),
406-
NotOnStruct => "not on struct".into_cow(),
407-
NotOnTuple => "not on tuple".into_cow(),
406+
NegateOn(ref const_val) => format!("negate on {}", const_val.description()).into_cow(),
407+
NotOn(ref const_val) => format!("not on {}", const_val.description()).into_cow(),
408408

409409
NegateWithOverflow(..) => "attempted to negate with overflow".into_cow(),
410410
AddiWithOverflow(..) => "attempted to add with overflow".into_cow(),
@@ -745,23 +745,15 @@ pub fn eval_const_expr_with_substs<'tcx, S>(tcx: &ty::ctxt<'tcx>,
745745
Uint(i) => {
746746
try!(const_uint_checked_neg(i, e, expr_uint_type))
747747
}
748-
Str(_) => signal!(e, NegateOnString),
749-
Bool(_) => signal!(e, NegateOnBoolean),
750-
Binary(_) => signal!(e, NegateOnBinary),
751-
Tuple(_) => signal!(e, NegateOnTuple),
752-
Struct(..) => signal!(e, NegateOnStruct),
748+
const_val => signal!(e, NegateOn(const_val)),
753749
}
754750
}
755751
ast::ExprUnary(ast::UnNot, ref inner) => {
756752
match try!(eval_const_expr_partial(tcx, &**inner, ety)) {
757753
Int(i) => Int(!i),
758754
Uint(i) => const_uint_not(i, expr_uint_type),
759755
Bool(b) => Bool(!b),
760-
Str(_) => signal!(e, NotOnString),
761-
Float(_) => signal!(e, NotOnFloat),
762-
Binary(_) => signal!(e, NotOnBinary),
763-
Tuple(_) => signal!(e, NotOnTuple),
764-
Struct(..) => signal!(e, NotOnStruct),
756+
const_val => signal!(e, NotOn(const_val)),
765757
}
766758
}
767759
ast::ExprBinary(op, ref a, ref b) => {

src/librustc/middle/ty.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -6091,13 +6091,7 @@ impl<'tcx> ctxt<'tcx> {
60916091
let found = match val {
60926092
ConstVal::Uint(count) => return count as usize,
60936093
ConstVal::Int(count) if count >= 0 => return count as usize,
6094-
ConstVal::Int(_) => "negative integer",
6095-
ConstVal::Float(_) => "float",
6096-
ConstVal::Str(_) => "string",
6097-
ConstVal::Bool(_) => "boolean",
6098-
ConstVal::Binary(_) => "binary array",
6099-
ConstVal::Struct(..) => "struct",
6100-
ConstVal::Tuple(_) => "tuple"
6094+
const_val => const_val.description(),
61016095
};
61026096
span_err!(self.sess, count_expr.span, E0306,
61036097
"expected positive integer for repeat count, found {}",

src/test/compile-fail/repeat_count.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,58 @@
1212

1313
fn main() {
1414
let n = 1;
15-
let a = [0; n]; //~ ERROR expected constant integer for repeat count, found variable
15+
let a = [0; n];
16+
//~^ ERROR expected constant integer for repeat count, found variable [E0307]
1617
let b = [0; ()];
17-
//~^ ERROR mismatched types
18-
//~| expected `usize`
19-
//~| found `()`
20-
//~| expected usize
21-
//~| found ()
22-
//~| ERROR expected positive integer for repeat count, found tuple
18+
//~^ ERROR mismatched types
19+
//~| expected `usize`
20+
//~| found `()`
21+
//~| expected usize
22+
//~| found ()) [E0308]
23+
//~| ERROR expected positive integer for repeat count, found tuple [E0306]
2324
let c = [0; true];
2425
//~^ ERROR mismatched types
2526
//~| expected `usize`
2627
//~| found `bool`
2728
//~| expected usize
28-
//~| found bool
29-
//~| ERROR expected positive integer for repeat count, found boolean
29+
//~| found bool) [E0308]
30+
//~| ERROR expected positive integer for repeat count, found boolean [E0306]
3031
let d = [0; 0.5];
3132
//~^ ERROR mismatched types
3233
//~| expected `usize`
3334
//~| found `_`
3435
//~| expected usize
35-
//~| found floating-point variable
36-
//~| ERROR expected positive integer for repeat count, found float
36+
//~| found floating-point variable) [E0308]
37+
//~| ERROR expected positive integer for repeat count, found float [E0306]
3738
let e = [0; "foo"];
3839
//~^ ERROR mismatched types
3940
//~| expected `usize`
4041
//~| found `&'static str`
4142
//~| expected usize
42-
//~| found &-ptr
43-
//~| ERROR expected positive integer for repeat count, found string
43+
//~| found &-ptr) [E0308]
44+
//~| ERROR expected positive integer for repeat count, found string literal [E0306]
4445
let f = [0; -4_isize];
4546
//~^ ERROR mismatched types
4647
//~| expected `usize`
4748
//~| found `isize`
4849
//~| expected usize
49-
//~| found isize
50-
//~| ERROR expected positive integer for repeat count, found negative integer
50+
//~| found isize) [E0308]
51+
//~| ERROR expected positive integer for repeat count, found negative integer [E0306]
5152
let f = [0_usize; -1_isize];
5253
//~^ ERROR mismatched types
5354
//~| expected `usize`
5455
//~| found `isize`
5556
//~| expected usize
56-
//~| found isize
57-
//~| ERROR expected positive integer for repeat count, found negative integer
57+
//~| found isize) [E0308]
58+
//~| ERROR expected positive integer for repeat count, found negative integer [E0306]
59+
struct G {
60+
g: (),
61+
}
62+
let g = [0; G { g: () }];
63+
//~^ ERROR mismatched types
64+
//~| expected `usize`
65+
//~| found `main::G`
66+
//~| expected usize
67+
//~| found struct `main::G`) [E0308]
68+
//~| ERROR expected positive integer for repeat count, found struct [E0306]
5869
}

0 commit comments

Comments
 (0)