diff options
| author | bors <bors@rust-lang.org> | 2016-03-14 11:38:23 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-03-14 11:38:23 -0700 |
| commit | 01118928fc2b280e96189ed394af749d65cbcffe (patch) | |
| tree | adbe12b4955b12035e86ca81a1735766a4b1e3b2 /src/test | |
| parent | d19f1b629918a556709e6b89a1929305db6449dc (diff) | |
| parent | f665c399a013ce27991fafb2203a5420e75f9cfc (diff) | |
| download | rust-01118928fc2b280e96189ed394af749d65cbcffe.tar.gz rust-01118928fc2b280e96189ed394af749d65cbcffe.zip | |
Auto merge of #30587 - oli-obk:eager_const_eval2, r=nikomatsakis
typestrong const integers
~~It would be great if someone could run crater on this PR, as this has a high danger of breaking valid code~~ Crater ran. Good to go.
----
So this PR does a few things:
1. ~~const eval array values when const evaluating an array expression~~
2. ~~const eval repeat value when const evaluating a repeat expression~~
3. ~~const eval all struct and tuple fields when evaluating a struct/tuple expression~~
4. remove the `ConstVal::Int` and `ConstVal::Uint` variants and replace them with a single enum (`ConstInt`) which has variants for all integral types
* `usize`/`isize` are also enums with variants for 32 and 64 bit. At creation and various usage steps there are assertions in place checking if the target bitwidth matches with the chosen enum variant
5. enum discriminants (`ty::Disr`) are now `ConstInt`
6. trans has its own `Disr` type now (newtype around `u64`)
This obviously can't be done without breaking changes (the ones that are noticable in stable)
We could probably write lints that find those situations and error on it for a cycle or two. But then again, those situations are rare and really bugs imo anyway:
```rust
let v10 = 10 as i8;
let v4 = 4 as isize;
assert_eq!(v10 << v4 as usize, 160 as i8);
```
stops compiling because 160 is not a valid i8
```rust
struct S<T, S> {
a: T,
b: u8,
c: S
}
let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 };
```
stops compiling because `0xaa_aa_aa_aa` is not a valid i32
----
cc @eddyb @pnkfelix
related: https://github.com/rust-lang/rfcs/issues/1071
Diffstat (limited to 'src/test')
30 files changed, 316 insertions, 135 deletions
diff --git a/src/test/auxiliary/dummy_mir_pass.rs b/src/test/auxiliary/dummy_mir_pass.rs index 89101fe709d..fdfbc98b007 100644 --- a/src/test/auxiliary/dummy_mir_pass.rs +++ b/src/test/auxiliary/dummy_mir_pass.rs @@ -16,6 +16,7 @@ #[macro_use] extern crate rustc; extern crate rustc_front; extern crate rustc_plugin; +extern crate rustc_const_eval; extern crate syntax; use rustc::mir::transform::{self, MirPass}; @@ -23,6 +24,7 @@ use rustc::mir::repr::{Mir, Literal}; use rustc::mir::visit::MutVisitor; use rustc::middle::ty; use rustc::middle::const_eval::ConstVal; +use rustc_const_eval::ConstInt; use rustc_plugin::Registry; use syntax::ast::NodeId; @@ -40,8 +42,10 @@ struct Visitor; impl<'tcx> MutVisitor<'tcx> for Visitor { fn visit_literal(&mut self, literal: &mut Literal<'tcx>) { - if let Literal::Value { value: ConstVal::Int(ref mut i @ 11) } = *literal { - *i = 42; + if let Literal::Value { ref mut value } = *literal { + if let ConstVal::Integral(ConstInt::I32(ref mut i @ 11)) = *value { + *i = 42; + } } } } diff --git a/src/test/compile-fail/const-err.rs b/src/test/compile-fail/const-err.rs index be67e06d99f..4d156a49192 100644 --- a/src/test/compile-fail/const-err.rs +++ b/src/test/compile-fail/const-err.rs @@ -25,9 +25,9 @@ fn main() { //~^ WARN attempted to add with overflow //~^^ WARN attempted to add with overflow let c = 200u8 * 4; - //~^ WARN attempted to mul with overflow + //~^ WARN attempted to multiply with overflow let d = 42u8 - (42u8 + 1); - //~^ WARN attempted to sub with overflow + //~^ WARN attempted to subtract with overflow let _e = BLA; black_box(a); black_box(b); diff --git a/src/test/compile-fail/const-eval-overflow-2.rs b/src/test/compile-fail/const-eval-overflow-2.rs index be04bc9bd3b..07e27a7dc9a 100644 --- a/src/test/compile-fail/const-eval-overflow-2.rs +++ b/src/test/compile-fail/const-eval-overflow-2.rs @@ -21,10 +21,11 @@ const NEG_128: i8 = -128; const NEG_NEG_128: i8 = -NEG_128; //~^ ERROR constant evaluation error: attempted to negate with overflow //~| ERROR attempted to negate with overflow +//~| ERROR attempted to negate with overflow fn main() { match -128i8 { - NEG_NEG_128 => println!("A"), + NEG_NEG_128 => println!("A"), //~ NOTE in pattern here _ => println!("B"), } } diff --git a/src/test/compile-fail/const-eval-overflow-3.rs b/src/test/compile-fail/const-eval-overflow-3.rs index c2bc5b2648a..c90ae045f96 100644 --- a/src/test/compile-fail/const-eval-overflow-3.rs +++ b/src/test/compile-fail/const-eval-overflow-3.rs @@ -36,4 +36,3 @@ fn main() { fn foo<T:fmt::Debug>(x: T) { println!("{:?}", x); } - diff --git a/src/test/compile-fail/const-eval-overflow-4b.rs b/src/test/compile-fail/const-eval-overflow-4b.rs index 253285d3919..5aa93cf6383 100644 --- a/src/test/compile-fail/const-eval-overflow-4b.rs +++ b/src/test/compile-fail/const-eval-overflow-4b.rs @@ -21,8 +21,9 @@ use std::{u8, u16, u32, u64, usize}; const A_I8_T : [u32; (i8::MAX as i8 + 1u8) as usize] - //~^ ERROR mismatched types - //~| ERROR the trait `core::ops::Add<u8>` is not implemented for the type `i8` + //~^ ERROR mismatched types: + //~| expected `i8`, + //~| found `u8` [E0250] = [0; (i8::MAX as usize) + 1]; fn main() { diff --git a/src/test/compile-fail/const-eval-overflow.rs b/src/test/compile-fail/const-eval-overflow.rs index 2a2fc2ef080..3dfcb5bb29a 100644 --- a/src/test/compile-fail/const-eval-overflow.rs +++ b/src/test/compile-fail/const-eval-overflow.rs @@ -23,84 +23,84 @@ const VALS_I8: (i8, i8, i8, i8) = (-i8::MIN, //~^ ERROR attempted to negate with overflow i8::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow i8::MAX + 1, //~^ ERROR attempted to add with overflow i8::MIN * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_I16: (i16, i16, i16, i16) = (-i16::MIN, //~^ ERROR attempted to negate with overflow i16::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow i16::MAX + 1, //~^ ERROR attempted to add with overflow i16::MIN * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_I32: (i32, i32, i32, i32) = (-i32::MIN, //~^ ERROR attempted to negate with overflow i32::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow i32::MAX + 1, //~^ ERROR attempted to add with overflow i32::MIN * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_I64: (i64, i64, i64, i64) = (-i64::MIN, //~^ ERROR attempted to negate with overflow i64::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow i64::MAX + 1, //~^ ERROR attempted to add with overflow i64::MAX * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_U8: (u8, u8, u8, u8) = (-(u8::MIN as i8) as u8, u8::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow u8::MAX + 1, //~^ ERROR attempted to add with overflow u8::MAX * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_U16: (u16, u16, u16, u16) = (-(u16::MIN as i16) as u16, u16::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow u16::MAX + 1, //~^ ERROR attempted to add with overflow u16::MAX * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_U32: (u32, u32, u32, u32) = (-(u32::MIN as i32) as u32, u32::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow u32::MAX + 1, //~^ ERROR attempted to add with overflow u32::MAX * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); const VALS_U64: (u64, u64, u64, u64) = (-(u64::MIN as i64) as u64, u64::MIN - 1, - //~^ ERROR attempted to sub with overflow + //~^ ERROR attempted to subtract with overflow u64::MAX + 1, //~^ ERROR attempted to add with overflow u64::MAX * 2, - //~^ ERROR attempted to mul with overflow + //~^ ERROR attempted to multiply with overflow ); fn main() { diff --git a/src/test/compile-fail/const-integer-bool-ops.rs b/src/test/compile-fail/const-integer-bool-ops.rs index 37a93ec954d..0d6cf3bab45 100644 --- a/src/test/compile-fail/const-integer-bool-ops.rs +++ b/src/test/compile-fail/const-integer-bool-ops.rs @@ -8,32 +8,30 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -const X: usize = 42 && 39; //~ ERROR: can't do this op on unsigned integrals +const X: usize = 42 && 39; //~ ERROR: can't do this op on integrals const ARR: [i32; X] = [99; 34]; //~ NOTE: for array length here -const X1: usize = 42 || 39; //~ ERROR: can't do this op on unsigned integrals +const X1: usize = 42 || 39; //~ ERROR: can't do this op on integrals const ARR1: [i32; X1] = [99; 47]; //~ NOTE: for array length here -// FIXME: the error should be `on signed integrals` -const X2: usize = -42 || -39; //~ ERROR: can't do this op on unsigned integrals +const X2: usize = -42 || -39; //~ ERROR: unary negation of unsigned integer const ARR2: [i32; X2] = [99; 18446744073709551607]; //~ NOTE: for array length here -// FIXME: the error should be `on signed integrals` -const X3: usize = -42 && -39; //~ ERROR: can't do this op on unsigned integrals +const X3: usize = -42 && -39; //~ ERROR: unary negation of unsigned integer const ARR3: [i32; X3] = [99; 6]; //~ NOTE: for array length here const Y: usize = 42.0 == 42.0; -const ARRR: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length +const ARRR: [i32; Y] = [99; 1]; //~ ERROR: expected usize value for array length const Y1: usize = 42.0 >= 42.0; -const ARRR1: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length +const ARRR1: [i32; Y] = [99; 1]; //~ ERROR: expected usize value for array length const Y2: usize = 42.0 <= 42.0; -const ARRR2: [i32; Y] = [99; 1]; //~ ERROR: expected constant integer expression for array length +const ARRR2: [i32; Y] = [99; 1]; //~ ERROR: expected usize value for array length const Y3: usize = 42.0 > 42.0; -const ARRR3: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length +const ARRR3: [i32; Y] = [99; 0]; //~ ERROR: expected usize value for array length const Y4: usize = 42.0 < 42.0; -const ARRR4: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length +const ARRR4: [i32; Y] = [99; 0]; //~ ERROR: expected usize value for array length const Y5: usize = 42.0 != 42.0; -const ARRR5: [i32; Y] = [99; 0]; //~ ERROR: expected constant integer expression for array length +const ARRR5: [i32; Y] = [99; 0]; //~ ERROR: expected usize value for array length fn main() { let _ = ARR; diff --git a/src/test/compile-fail/const-len-underflow-separate-spans.rs b/src/test/compile-fail/const-len-underflow-separate-spans.rs index 786c72b66f3..9c6b774b990 100644 --- a/src/test/compile-fail/const-len-underflow-separate-spans.rs +++ b/src/test/compile-fail/const-len-underflow-separate-spans.rs @@ -15,7 +15,7 @@ const ONE: usize = 1; const TWO: usize = 2; const LEN: usize = ONE - TWO; -//~^ ERROR array length constant evaluation error: attempted to sub with overflow [E0250] +//~^ ERROR array length constant evaluation error: attempted to subtract with overflow [E0250] fn main() { let a: [i8; LEN] = unimplemented!(); diff --git a/src/test/compile-fail/const-len-underflow-subspans.rs b/src/test/compile-fail/const-len-underflow-subspans.rs index 020717dc1e1..d51f31087d0 100644 --- a/src/test/compile-fail/const-len-underflow-subspans.rs +++ b/src/test/compile-fail/const-len-underflow-subspans.rs @@ -16,5 +16,5 @@ const TWO: usize = 2; fn main() { let a: [i8; ONE - TWO] = unimplemented!(); - //~^ ERROR array length constant evaluation error: attempted to sub with overflow [E0250] + //~^ ERROR array length constant evaluation error: attempted to subtract with overflow [E0250] } diff --git a/src/test/compile-fail/const-tup-index-span.rs b/src/test/compile-fail/const-tup-index-span.rs index 8c607fc7e32..9d3c432d148 100644 --- a/src/test/compile-fail/const-tup-index-span.rs +++ b/src/test/compile-fail/const-tup-index-span.rs @@ -11,7 +11,7 @@ // Test spans of errors const TUP: (usize,) = 5 << 64; -//~^ ERROR: attempted left shift with overflow [E0250] +//~^ ERROR: attempted to shift left with overflow [E0250] const ARR: [i32; TUP.0] = []; fn main() { diff --git a/src/test/compile-fail/discrim-overflow-2.rs b/src/test/compile-fail/discrim-overflow-2.rs index 76378d5c802..0ff740212e8 100644 --- a/src/test/compile-fail/discrim-overflow-2.rs +++ b/src/test/compile-fail/discrim-overflow-2.rs @@ -24,7 +24,7 @@ fn f_i8() { enum A { Ok = i8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed on value after 127i8; set explicitly via OhNo = -128i8 if that is desired outcome } } @@ -33,7 +33,7 @@ fn f_u8() { enum A { Ok = u8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed on value after 255u8; set explicitly via OhNo = 0u8 if that is desired outcome } } diff --git a/src/test/compile-fail/discrim-overflow.rs b/src/test/compile-fail/discrim-overflow.rs index 5d7e61e9d1e..7316e737b6d 100644 --- a/src/test/compile-fail/discrim-overflow.rs +++ b/src/test/compile-fail/discrim-overflow.rs @@ -22,7 +22,7 @@ fn f_i8() { enum A { Ok = i8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 127: i8; set explicitly via OhNo = -128 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed on value after 127i8; set explicitly via OhNo = -128i8 if that is desired outcome } let x = A::Ok; @@ -33,7 +33,7 @@ fn f_u8() { enum A { Ok = u8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 255: u8; set explicitly via OhNo = 0 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed on value after 255u8; set explicitly via OhNo = 0u8 if that is desired outcome } let x = A::Ok; diff --git a/src/test/compile-fail/enum-discrim-too-small.rs b/src/test/compile-fail/enum-discrim-too-small.rs index 84a27a38200..d6ba09bb4c5 100644 --- a/src/test/compile-fail/enum-discrim-too-small.rs +++ b/src/test/compile-fail/enum-discrim-too-small.rs @@ -9,46 +9,32 @@ // except according to those terms. -#[repr(u8)] //~ NOTE discriminant type specified here +#[repr(u8)] enum Eu8 { Au8 = 23, Bu8 = 223, - Cu8 = -23, //~ ERROR discriminant value outside specified type + Cu8 = -23, //~ ERROR unary negation of unsigned integer } -#[repr(i8)] //~ NOTE discriminant type specified here -enum Ei8 { - Ai8 = 23, - Bi8 = -23, - Ci8 = 223, //~ ERROR discriminant value outside specified type -} - -#[repr(u16)] //~ NOTE discriminant type specified here +#[repr(u16)] enum Eu16 { Au16 = 23, Bu16 = 55555, - Cu16 = -22333, //~ ERROR discriminant value outside specified type -} - -#[repr(i16)] //~ NOTE discriminant type specified here -enum Ei16 { - Ai16 = 23, - Bi16 = -22333, - Ci16 = 55555, //~ ERROR discriminant value outside specified type + Cu16 = -22333, //~ ERROR unary negation of unsigned integer } -#[repr(u32)] //~ NOTE discriminant type specified here +#[repr(u32)] enum Eu32 { Au32 = 23, Bu32 = 3_000_000_000, - Cu32 = -2_000_000_000, //~ ERROR discriminant value outside specified type + Cu32 = -2_000_000_000, //~ ERROR unary negation of unsigned integer } -#[repr(i32)] //~ NOTE discriminant type specified here -enum Ei32 { - Ai32 = 23, - Bi32 = -2_000_000_000, - Ci32 = 3_000_000_000, //~ ERROR discriminant value outside specified type +#[repr(u64)] +enum Eu64 { + Au32 = 23, + Bu32 = 3_000_000_000, + Cu32 = -2_000_000_000, //~ ERROR unary negation of unsigned integer } // u64 currently allows negative numbers, and i64 allows numbers greater than `1<<63`. This is a diff --git a/src/test/compile-fail/enum-discrim-too-small2.rs b/src/test/compile-fail/enum-discrim-too-small2.rs new file mode 100644 index 00000000000..d66716c14c3 --- /dev/null +++ b/src/test/compile-fail/enum-discrim-too-small2.rs @@ -0,0 +1,47 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(overflowing_literals)] +#![allow(dead_code)] + +#[repr(i8)] +enum Ei8 { + Ai8 = 23, + Bi8 = -23, + Ci8 = 223, //~ ERROR literal out of range for i8 +} + +#[repr(i16)] +enum Ei16 { + Ai16 = 23, + Bi16 = -22333, + Ci16 = 55555, //~ ERROR literal out of range for i16 +} + +#[repr(i32)] +enum Ei32 { + Ai32 = 23, + Bi32 = -2_000_000_000, + Ci32 = 3_000_000_000, //~ ERROR literal out of range for i32 +} + +#[repr(i64)] +enum Ei64 { + Ai64 = 23, + Bi64 = -9223372036854775808, + Ci64 = 9223372036854775809, //~ ERROR literal out of range for i64 +} + +// u64 currently allows negative numbers, and i64 allows numbers greater than `1<<63`. This is a +// little counterintuitive, but since the discriminant can store all the bits, and extracting it +// with a cast requires specifying the signedness, there is no loss of information in those cases. +// This also applies to isize and usize on 64-bit targets. + +pub fn main() { } diff --git a/src/test/compile-fail/eval-enum.rs b/src/test/compile-fail/eval-enum.rs index ed1327f3118..7ca274b81e5 100644 --- a/src/test/compile-fail/eval-enum.rs +++ b/src/test/compile-fail/eval-enum.rs @@ -10,7 +10,8 @@ enum test { div_zero = 1/0, //~ERROR constant evaluation error: attempted to divide by zero - rem_zero = 1%0 //~ERROR constant evaluation error: attempted remainder with a divisor of zero + rem_zero = 1%0, +//~^ ERROR constant evaluation error: attempted to calculate the remainder with a divisor of zero } fn main() {} diff --git a/src/test/compile-fail/feature-gate-negate-unsigned.rs b/src/test/compile-fail/feature-gate-negate-unsigned.rs index 15cc17b19db..546fc5e3c60 100644 --- a/src/test/compile-fail/feature-gate-negate-unsigned.rs +++ b/src/test/compile-fail/feature-gate-negate-unsigned.rs @@ -21,22 +21,7 @@ const _MAX: usize = -1; //~| HELP use a cast or the `!` operator fn main() { - let a = -1; - //~^ ERROR unary negation of unsigned integer - //~| HELP use a cast or the `!` operator - let _b : u8 = a; // for infering variable a to u8. - - -a; - //~^ ERROR unary negation of unsigned integer - //~| HELP use a cast or the `!` operator - - let _d = -1u8; - //~^ ERROR unary negation of unsigned integer - //~| HELP use a cast or the `!` operator - - for _ in -10..10u8 {} - //~^ ERROR unary negation of unsigned integer - //~| HELP use a cast or the `!` operator - + let x = 5u8; + let _y = -x; //~ ERROR unary negation of unsigned integer -S; // should not trigger the gate; issue 26840 } diff --git a/src/test/compile-fail/feature-gate-negate-unsigned0.rs b/src/test/compile-fail/feature-gate-negate-unsigned0.rs new file mode 100644 index 00000000000..05b194345d4 --- /dev/null +++ b/src/test/compile-fail/feature-gate-negate-unsigned0.rs @@ -0,0 +1,31 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that negating unsigned integers doesn't compile + +struct S; +impl std::ops::Neg for S { + type Output = u32; + fn neg(self) -> u32 { 0 } +} + +fn main() { + let a = -1; + //~^ ERROR unary negation of unsigned integer + let _b : u8 = a; // for infering variable a to u8. + + let _d = -1u8; + //~^ ERROR unary negation of unsigned integer + + for _ in -10..10u8 {} + //~^ ERROR unary negation of unsigned integer + + -S; // should not trigger the gate; issue 26840 +} diff --git a/src/test/compile-fail/issue-15524.rs b/src/test/compile-fail/issue-15524.rs index b378d2f885e..bdf344dcdfe 100644 --- a/src/test/compile-fail/issue-15524.rs +++ b/src/test/compile-fail/issue-15524.rs @@ -12,12 +12,12 @@ const N: isize = 1; enum Foo { A = 1, - B = 1, //~ ERROR discriminant value `1` already exists + B = 1, //~ ERROR discriminant value `1isize` already exists //~^^ NOTE conflicting C = 0, - D, //~ ERROR discriminant value `1` already exists + D, //~ ERROR discriminant value `1isize` already exists //~^^^^^ NOTE conflicting - E = N, //~ ERROR discriminant value `1` already exists + E = N, //~ ERROR discriminant value `1isize` already exists //~^^^^^^^ NOTE conflicting } diff --git a/src/test/compile-fail/issue-8460-const.rs b/src/test/compile-fail/issue-8460-const.rs index 95921556c7d..fe51d0b6998 100644 --- a/src/test/compile-fail/issue-8460-const.rs +++ b/src/test/compile-fail/issue-8460-const.rs @@ -35,23 +35,23 @@ fn main() { assert!(thread::spawn(move|| { 1i64 / 0; }).join().is_err()); //~^ ERROR attempted to divide by zero assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err()); - //~^ ERROR attempted remainder with overflow + //~^ ERROR attempted to calculate the remainder with overflow assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err()); - //~^ ERROR attempted remainder with overflow + //~^ ERROR attempted to calculate the remainder with overflow assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err()); - //~^ ERROR attempted remainder with overflow + //~^ ERROR attempted to calculate the remainder with overflow assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err()); - //~^ ERROR attempted remainder with overflow + //~^ ERROR attempted to calculate the remainder with overflow assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err()); - //~^ ERROR attempted remainder with overflow + //~^ ERROR attempted to calculate the remainder with overflow assert!(thread::spawn(move|| { 1isize % 0; }).join().is_err()); - //~^ ERROR attempted remainder with a divisor of zero + //~^ ERROR attempted to calculate the remainder with a divisor of zero assert!(thread::spawn(move|| { 1i8 % 0; }).join().is_err()); - //~^ ERROR attempted remainder with a divisor of zero + //~^ ERROR attempted to calculate the remainder with a divisor of zero assert!(thread::spawn(move|| { 1i16 % 0; }).join().is_err()); - //~^ ERROR attempted remainder with a divisor of zero + //~^ ERROR attempted to calculate the remainder with a divisor of zero assert!(thread::spawn(move|| { 1i32 % 0; }).join().is_err()); - //~^ ERROR attempted remainder with a divisor of zero + //~^ ERROR attempted to calculate the remainder with a divisor of zero assert!(thread::spawn(move|| { 1i64 % 0; }).join().is_err()); - //~^ ERROR attempted remainder with a divisor of zero + //~^ ERROR attempted to calculate the remainder with a divisor of zero } diff --git a/src/test/compile-fail/issue-8761.rs b/src/test/compile-fail/issue-8761.rs index 35be01970cb..1c98abce030 100644 --- a/src/test/compile-fail/issue-8761.rs +++ b/src/test/compile-fail/issue-8761.rs @@ -10,13 +10,13 @@ enum Foo { A = 1i64, - //~^ ERROR mismatched types - //~| expected `isize` - //~| found `i64` + //~^ ERROR mismatched types: + //~| expected `isize`, + //~| found `i64` [E0080] B = 2u8 - //~^ ERROR mismatched types - //~| expected `isize` - //~| found `u8` + //~^ ERROR mismatched types: + //~| expected `isize`, + //~| found `u8` [E0080] } fn main() {} diff --git a/src/test/compile-fail/lint-type-limits.rs b/src/test/compile-fail/lint-type-limits.rs index 0b414ad73db..29929c120c3 100644 --- a/src/test/compile-fail/lint-type-limits.rs +++ b/src/test/compile-fail/lint-type-limits.rs @@ -24,11 +24,6 @@ fn bar() -> i8 { return 123; } -fn baz() -> bool { - 128 > bar() //~ ERROR comparison is useless due to type limits - //~^ WARNING literal out of range for i8 -} - fn bleh() { let u = 42u8; let _ = u > 255; //~ ERROR comparison is useless due to type limits @@ -40,11 +35,3 @@ fn bleh() { let _ = u >= 0; //~ ERROR comparison is useless due to type limits let _ = 0 <= u; //~ ERROR comparison is useless due to type limits } - -fn qux() { - let mut i = 1i8; - while 200 != i { //~ ERROR comparison is useless due to type limits - //~^ WARNING literal out of range for i8 - i += 1; - } -} diff --git a/src/test/compile-fail/lint-type-limits2.rs b/src/test/compile-fail/lint-type-limits2.rs new file mode 100644 index 00000000000..8fc18d16469 --- /dev/null +++ b/src/test/compile-fail/lint-type-limits2.rs @@ -0,0 +1,24 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] + +// compile-flags: -D unused-comparisons +fn main() { } + + +fn bar() -> i8 { + return 123; +} + +fn baz() -> bool { + 128 > bar() //~ ERROR comparison is useless due to type limits + //~| WARN literal out of range for i8 +} diff --git a/src/test/compile-fail/lint-type-limits3.rs b/src/test/compile-fail/lint-type-limits3.rs new file mode 100644 index 00000000000..b09dc0acdbf --- /dev/null +++ b/src/test/compile-fail/lint-type-limits3.rs @@ -0,0 +1,22 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] + +// compile-flags: -D unused-comparisons +fn main() { } + +fn qux() { + let mut i = 1i8; + while 200 != i { //~ ERROR comparison is useless due to type limits + //~| WARN literal out of range for i8 + i += 1; + } +} diff --git a/src/test/compile-fail/lint-type-overflow.rs b/src/test/compile-fail/lint-type-overflow.rs index eb5b77f7a45..ce336905c01 100644 --- a/src/test/compile-fail/lint-type-overflow.rs +++ b/src/test/compile-fail/lint-type-overflow.rs @@ -25,7 +25,6 @@ fn main() { let x2: i8 = -128; // should be OK let x1: i8 = 128; //~ error: literal out of range for i8 - let x2: i8 = --128; //~ error: literal out of range for i8 let x3: i8 = -129; //~ error: literal out of range for i8 let x3: i8 = -(129); //~ error: literal out of range for i8 @@ -54,9 +53,4 @@ fn main() { let x = 18446744073709551615_i64; //~ error: literal out of range for i64 let x: i64 = -9223372036854775809; //~ error: literal out of range for i64 let x = -9223372036854775809_i64; //~ error: literal out of range for i64 - - let x = -3.40282348e+38_f32; //~ error: literal out of range for f32 - let x = 3.40282348e+38_f32; //~ error: literal out of range for f32 - let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for f64 - let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for f64 } diff --git a/src/test/compile-fail/lint-type-overflow2.rs b/src/test/compile-fail/lint-type-overflow2.rs new file mode 100644 index 00000000000..83300f18c3e --- /dev/null +++ b/src/test/compile-fail/lint-type-overflow2.rs @@ -0,0 +1,22 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// + +#![deny(overflowing_literals)] + +#[allow(unused_variables)] +fn main() { + let x2: i8 = --128; //~ error: literal out of range for i8 + + let x = -3.40282348e+38_f32; //~ error: literal out of range for f32 + let x = 3.40282348e+38_f32; //~ error: literal out of range for f32 + let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for f64 + let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for f64 +} diff --git a/src/test/compile-fail/repeat_count.rs b/src/test/compile-fail/repeat_count.rs index 9aa61418d6d..10b722946a8 100644 --- a/src/test/compile-fail/repeat_count.rs +++ b/src/test/compile-fail/repeat_count.rs @@ -43,13 +43,17 @@ fn main() { let f = [0; -4_isize]; //~^ ERROR mismatched types //~| expected `usize` - //~| found `isize` - //~| ERROR expected positive integer for repeat count, found negative integer [E0306] + //~| found `isize` [E0308] + //~| ERROR mismatched types: + //~| expected `usize`, + //~| found `isize` [E0307] let f = [0_usize; -1_isize]; //~^ ERROR mismatched types //~| expected `usize` - //~| found `isize` - //~| ERROR expected positive integer for repeat count, found negative integer [E0306] + //~| found `isize` [E0308] + //~| ERROR mismatched types + //~| expected `usize` + //~| found `isize` [E0307] struct G { g: (), } diff --git a/src/test/run-pass/const-fn.rs b/src/test/run-pass/const-fn.rs index 5961ed8d339..562040dc562 100644 --- a/src/test/run-pass/const-fn.rs +++ b/src/test/run-pass/const-fn.rs @@ -10,7 +10,7 @@ // A very basic test of const fn functionality. -#![feature(const_fn)] +#![feature(const_fn, const_indexing)] const fn add(x: u32, y: u32) -> u32 { x + y @@ -24,6 +24,14 @@ const unsafe fn div(x: u32, y: u32) -> u32 { x / y } +const fn generic<T>(t: T) -> T { + t +} + +const fn generic_arr<T: Copy>(t: [T; 1]) -> T { + t[0] +} + const SUM: u32 = add(44, 22); const DIFF: u32 = sub(44, 22); const DIV: u32 = unsafe{div(44, 22)}; @@ -36,4 +44,6 @@ fn main() { assert_eq!(DIV, 2); let _: [&'static str; sub(100, 99) as usize] = ["hi"]; + let _: [&'static str; generic(1)] = ["hi"]; + let _: [&'static str; generic_arr([1])] = ["hi"]; } diff --git a/src/test/run-pass/const-negation.rs b/src/test/run-pass/const-negation.rs new file mode 100644 index 00000000000..96f4217e4cb --- /dev/null +++ b/src/test/run-pass/const-negation.rs @@ -0,0 +1,42 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(stmt_expr_attributes)] + +#[deny(const_err)] + +fn main() { + #[cfg(target_pointer_width = "32")] + const I: isize = -2147483648isize; + #[cfg(target_pointer_width = "64")] + const I: isize = -9223372036854775808isize; + assert_eq!(::std::i32::MIN as u64, 0xffffffff80000000); + assert_eq!(-2147483648isize as u64, 0xffffffff80000000); + assert_eq!(::std::i64::MIN as u64, 0x8000000000000000); + #[cfg(target_pointer_width = "64")] + assert_eq!(-9223372036854775808isize as u64, 0x8000000000000000); + #[cfg(target_pointer_width = "32")] + assert_eq!(-9223372036854775808isize as u64, 0); + const J: usize = ::std::i32::MAX as usize; + const K: usize = -1i32 as u32 as usize; + const L: usize = ::std::i32::MIN as usize; + const M: usize = ::std::i64::MIN as usize; + match 5 { + J => {}, + K => {}, + L => {}, + M => {}, + _ => {} + } + match 5 { + I => {}, + _ => {} + } +} diff --git a/src/test/run-pass/enum-discrim-autosizing.rs b/src/test/run-pass/enum-discrim-autosizing.rs index 99e44735d0f..53c44f2bb24 100644 --- a/src/test/run-pass/enum-discrim-autosizing.rs +++ b/src/test/run-pass/enum-discrim-autosizing.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(stmt_expr_attributes)] use std::mem::size_of; @@ -46,11 +47,6 @@ enum Ei64 { Bi64 = 0x8000_0000 } -enum Eu64 { - Au64 = 0, - Bu64 = 0x8000_0000_0000_0000 -} - pub fn main() { assert_eq!(size_of::<Ei8>(), 1); assert_eq!(size_of::<Eu8>(), 1); @@ -58,6 +54,8 @@ pub fn main() { assert_eq!(size_of::<Eu16>(), 2); assert_eq!(size_of::<Ei32>(), 4); assert_eq!(size_of::<Eu32>(), 4); + #[cfg(target_pointer_width = "64")] assert_eq!(size_of::<Ei64>(), 8); - assert_eq!(size_of::<Eu64>(), 8); + #[cfg(target_pointer_width = "32")] + assert_eq!(size_of::<Ei64>(), 4); } diff --git a/src/test/run-pass/issue-23833.rs b/src/test/run-pass/issue-23833.rs new file mode 100644 index 00000000000..7d63c41eb26 --- /dev/null +++ b/src/test/run-pass/issue-23833.rs @@ -0,0 +1,25 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt; +use std::{i8, i16, i32, i64, isize}; +use std::{u8, u16, u32, u64, usize}; + +const A_I8_T + : [u32; (i8::MAX as i8 - 1i8) as usize] + = [0; (i8::MAX as usize) - 1]; + +fn main() { + foo(&A_I8_T[..]); +} + +fn foo<T:fmt::Debug>(x: T) { + println!("{:?}", x); +} |
