Skip to content

Commit

Permalink
Rollup merge of #80394 - RalfJung:const-err-future, r=oli-obk
Browse files Browse the repository at this point in the history
make const_err a future incompat lint

This is the first step for #71800: make const_err a future-incompat lint. I also rewrote the const_err lint description as the old one seemed wrong.

This has the unfortunate side-effect of making const-eval error even more verbose by making the const_err message longer without fixing the redundancy caused by additionally emitting an error on each use site of the constant. We cannot fix that redundancy until const_err is a *hard* error (at that point the error-on-use-site can be turned into a `delay_span_bug!` for uses of monomorphic consts, and into a nicely rendered error for [lazily / post-monomorhization evaluated] associated consts).

~~The one annoying effect of this PR is that `let _x = &(1/(1-1));` now also shows the future-incompat warning, even though of course we will *not* make this a hard error. We'll instead (hopefully) stop promoting it -- see rust-lang/rfcs#3027. The only way I see to avoid the future-incompat warning is to use a different lint for "failure to evaluate promoted".~~

Cc `@rust-lang/wg-const-eval`
  • Loading branch information
m-ou-se committed Feb 3, 2021
2 parents 6ad11e2 + 8477d35 commit 00dabfb
Show file tree
Hide file tree
Showing 107 changed files with 952 additions and 261 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ declare_lint! {
pub CONST_ERR,
Deny,
"constant evaluation encountered erroneous expression",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #71800 <https://github.com/rust-lang/rust/issues/71800>",
edition: None,
};
report_in_external_macro
}

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/array-slice-vec/array_const_index-0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const A: &'static [i32] = &[];
const B: i32 = (&A)[1];
//~^ index out of bounds: the length is 0 but the index is 1
//~| ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
let _ = B;
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/array-slice-vec/array_const_index-0.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LL | const B: i32 = (&A)[1];
| index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/array-slice-vec/array_const_index-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const A: [i32; 0] = [];
const B: i32 = A[1];
//~^ index out of bounds: the length is 0 but the index is 1
//~| ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
let _ = B;
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/array-slice-vec/array_const_index-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LL | const B: i32 = A[1];
| index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to previous error

2 changes: 2 additions & 0 deletions src/test/ui/associated-consts/defaults-not-assumed-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ trait Tr {
// `Self::A` must not be assumed to hold inside the trait.
const B: u8 = Self::A + 1;
//~^ ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
}

// An impl that doesn't override any constant will NOT cause a const eval error
Expand All @@ -33,6 +34,7 @@ fn main() {
assert_eq!(<() as Tr>::B, 0); // causes the error above
//~^ ERROR evaluation of constant value failed
//~| ERROR erroneous constant used
//~| WARN this was previously accepted by the compiler but is being phased out

assert_eq!(<u8 as Tr>::A, 254);
assert_eq!(<u8 as Tr>::B, 255);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ LL | const B: u8 = Self::A + 1;
| attempt to compute `u8::MAX + 1_u8`, which would overflow
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: evaluation of constant value failed
--> $DIR/defaults-not-assumed-fail.rs:33:16
--> $DIR/defaults-not-assumed-fail.rs:34:16
|
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
| ^^^^^^^^^^^^^ referenced constant has errors

error: erroneous constant used
--> $DIR/defaults-not-assumed-fail.rs:33:5
--> $DIR/defaults-not-assumed-fail.rs:34:5
|
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/const-ptr/out_of_bounds_read.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
| ------------------------------------------------------
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
Expand All @@ -32,6 +34,9 @@ LL | unsafe { copy_nonoverlapping(src, dst, count) }
|
LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
| --------------------------------------------------------
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
Expand All @@ -49,6 +54,9 @@ LL | unsafe { copy_nonoverlapping(src, dst, count) }
|
LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
| --------------------------------------------------------------------
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to 3 previous errors

1 change: 1 addition & 0 deletions src/test/ui/consts/assoc_const_generic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trait ZeroSized: Sized {

impl<T: Sized> ZeroSized for T {
const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()]; //~ WARN any use of this value
//~| WARN this was previously accepted by the compiler but is being phased out
fn requires_zero_size(self) {
let () = Self::I_AM_ZERO_SIZED; //~ ERROR erroneous constant encountered
println!("requires_zero_size called");
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/consts/assoc_const_generic_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ note: the lint level is defined here
|
LL | #![warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: erroneous constant encountered
--> $DIR/assoc_const_generic_impl.rs:13:18
--> $DIR/assoc_const_generic_impl.rs:14:18
|
LL | let () = Self::I_AM_ZERO_SIZED;
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/consts/assume-type-intrinsics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ LL | | };
| |______-
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to previous error

5 changes: 5 additions & 0 deletions src/test/ui/consts/const-err-early.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#![deny(const_err)]

pub const A: i8 = -i8::MIN; //~ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const B: u8 = 200u8 + 200u8; //~ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const C: u8 = 200u8 * 4; //~ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const E: u8 = [5u8][1]; //~ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
let _a = A;
Expand Down
22 changes: 18 additions & 4 deletions src/test/ui/consts/const-err-early.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,52 @@ note: the lint level is defined here
|
LL | #![deny(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-early.rs:4:19
--> $DIR/const-err-early.rs:5:19
|
LL | pub const B: u8 = 200u8 + 200u8;
| ------------------^^^^^^^^^^^^^-
| |
| attempt to compute `200_u8 + 200_u8`, which would overflow
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-early.rs:5:19
--> $DIR/const-err-early.rs:7:19
|
LL | pub const C: u8 = 200u8 * 4;
| ------------------^^^^^^^^^-
| |
| attempt to compute `200_u8 * 4_u8`, which would overflow
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-early.rs:6:19
--> $DIR/const-err-early.rs:9:19
|
LL | pub const D: u8 = 42u8 - (42u8 + 1);
| ------------------^^^^^^^^^^^^^^^^^-
| |
| attempt to compute `42_u8 - 43_u8`, which would overflow
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-early.rs:7:19
--> $DIR/const-err-early.rs:11:19
|
LL | pub const E: u8 = [5u8][1];
| ------------------^^^^^^^^-
| |
| index out of bounds: the length is 1 but the index is 1
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to 5 previous errors

4 changes: 4 additions & 0 deletions src/test/ui/consts/const-err-multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

pub const A: i8 = -i8::MIN;
//~^ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const B: i8 = A;
//~^ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const C: u8 = A as u8;
//~^ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out
pub const D: i8 = 50 - A;
//~^ ERROR const_err
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
let _ = (A, B, C, D);
Expand Down
17 changes: 14 additions & 3 deletions src/test/ui/consts/const-err-multi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,41 @@ note: the lint level is defined here
|
LL | #![deny(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-multi.rs:5:19
--> $DIR/const-err-multi.rs:6:19
|
LL | pub const B: i8 = A;
| ------------------^-
| |
| referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-multi.rs:7:19
--> $DIR/const-err-multi.rs:9:19
|
LL | pub const C: u8 = A as u8;
| ------------------^-------
| |
| referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/const-err-multi.rs:9:24
--> $DIR/const-err-multi.rs:12:24
|
LL | pub const D: i8 = 50 - A;
| -----------------------^-
| |
| referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to 4 previous errors

1 change: 1 addition & 0 deletions src/test/ui/consts/const-err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn black_box<T>(_: T) {

const FOO: u8 = [5u8][1];
//~^ WARN any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
black_box((FOO, FOO));
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/consts/const-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ note: the lint level is defined here
|
LL | #![warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: erroneous constant used
--> $DIR/const-err.rs:15:16
--> $DIR/const-err.rs:16:16
|
LL | black_box((FOO, FOO));
| ^^^ referenced constant has errors

error[E0080]: erroneous constant used
--> $DIR/const-err.rs:15:21
--> $DIR/const-err.rs:16:21
|
LL | black_box((FOO, FOO));
| ^^^ referenced constant has errors
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/consts/const-eval/conditional_array_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const X: u32 = 5;
const Y: u32 = 6;
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
//~^ WARN any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out

fn main() {
println!("{}", FOO);
//~^ ERROR
//~| WARN erroneous constant used [const_err]
//~| WARN this was previously accepted by the compiler but is being phased out
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ note: the lint level is defined here
|
LL | #![warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: evaluation of constant value failed
--> $DIR/conditional_array_execution.rs:11:20
--> $DIR/conditional_array_execution.rs:12:20
|
LL | println!("{}", FOO);
| ^^^ referenced constant has errors

warning: erroneous constant used
--> $DIR/conditional_array_execution.rs:11:20
--> $DIR/conditional_array_execution.rs:12:20
|
LL | println!("{}", FOO);
| ^^^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to previous error; 2 warnings emitted

Expand Down
Loading

0 comments on commit 00dabfb

Please sign in to comment.