Skip to content

Commit

Permalink
Errors in promoteds may only cause lints not hard errors
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 10, 2020
1 parent 6e1bbff commit ecd5852
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 176 deletions.
15 changes: 7 additions & 8 deletions src/librustc_codegen_ssa/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// use `get_static` to get at their id.
// FIXME(oli-obk): can we unify this somehow, maybe by making const eval of statics
// always produce `&STATIC`. This may also simplify how const eval works with statics.
ty::ConstKind::Unevaluated(def_id, substs, promoted)
if self.cx.tcx().is_static(def_id) =>
{
assert!(promoted.is_none());
ty::ConstKind::Unevaluated(def_id, substs, None) if self.cx.tcx().is_static(def_id) => {
assert!(substs.is_empty(), "we don't support generic statics yet");
let static_ = bx.get_static(def_id);
// we treat operands referring to statics as if they were `&STATIC` instead
Expand All @@ -49,10 +46,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
.tcx()
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
.map_err(|err| {
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
if promoted.is_none() {
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
}
err
})
}
Expand Down
18 changes: 13 additions & 5 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc::ty::layout::{
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
};
use rustc::ty::subst::{InternalSubsts, Subst};
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
use rustc::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -441,8 +441,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
Ok(op) => Some(op),
Err(error) => {
let err = error_to_const_error(&self.ecx, error);
match self.lint_root(source_info) {
Some(lint_root) if c.literal.needs_subst() => {
if let Some(lint_root) = self.lint_root(source_info) {
let lint_only = match c.literal.val {
// Promoteds must lint and not error as the user didn't ask for them
ConstKind::Unevaluated(_, _, Some(_)) => true,
// Out of backwards compatibility we cannot report hard errors in unused
// generic functions using associated constants of the generic parameters.
_ => c.literal.needs_subst(),
};
if lint_only {
// Out of backwards compatibility we cannot report hard errors in unused
// generic functions using associated constants of the generic parameters.
err.report_as_lint(
Expand All @@ -451,10 +458,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
lint_root,
Some(c.span),
);
}
_ => {
} else {
err.report_as_error(self.ecx.tcx, "erroneous constant used");
}
} else {
err.report_as_error(self.ecx.tcx, "erroneous constant used");
}
None
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(const_err)]

// error-pattern: referenced constant has errors
// error-pattern: attempt to divide by zero

fn main() {
let x = &(1 / (1 - 1));
Expand Down
12 changes: 7 additions & 5 deletions src/test/ui/consts/array-literal-index-oob.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// build-fail
// build-pass

#![warn(const_err)]

fn main() {
&{[1, 2, 3][4]};
//~^ ERROR index out of bounds
//~| ERROR reaching this expression at runtime will panic or abort
//~| ERROR erroneous constant used [E0080]
&{ [1, 2, 3][4] };
//~^ WARN index out of bounds
//~| WARN reaching this expression at runtime will panic or abort
//~| WARN erroneous constant used [const_err]
}
37 changes: 19 additions & 18 deletions src/test/ui/consts/array-literal-index-oob.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
error: index out of bounds: the len is 3 but the index is 4
--> $DIR/array-literal-index-oob.rs:4:7
warning: index out of bounds: the len is 3 but the index is 4
--> $DIR/array-literal-index-oob.rs:6:8
|
LL | &{[1, 2, 3][4]};
| ^^^^^^^^^^^^
LL | &{ [1, 2, 3][4] };
| ^^^^^^^^^^^^
|
= note: `#[deny(const_err)]` on by default

error: reaching this expression at runtime will panic or abort
--> $DIR/array-literal-index-oob.rs:4:7
note: lint level defined here
--> $DIR/array-literal-index-oob.rs:3:9
|
LL | &{[1, 2, 3][4]};
| --^^^^^^^^^^^^-
| |
| indexing out of bounds: the len is 3 but the index is 4
LL | #![warn(const_err)]
| ^^^^^^^^^

error[E0080]: erroneous constant used
--> $DIR/array-literal-index-oob.rs:4:5
warning: reaching this expression at runtime will panic or abort
--> $DIR/array-literal-index-oob.rs:6:8
|
LL | &{[1, 2, 3][4]};
| ^^^^^^^^^^^^^^^ referenced constant has errors
LL | &{ [1, 2, 3][4] };
| ---^^^^^^^^^^^^--
| |
| indexing out of bounds: the len is 3 but the index is 4

error: aborting due to 3 previous errors
warning: erroneous constant used
--> $DIR/array-literal-index-oob.rs:6:5
|
LL | &{ [1, 2, 3][4] };
| ^^^^^^^^^^^^^^^^^ referenced constant has errors

For more information about this error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
fn main() {
println!("{}", FOO);
//~^ ERROR
//~| ERROR erroneous constant used [E0080]
//~| WARN erroneous constant used [const_err]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ error[E0080]: evaluation of constant expression failed
LL | println!("{}", FOO);
| ^^^ referenced constant has errors

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

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#![feature(const_fn)]
#![allow(const_err)]

fn double(x: usize) -> usize { x * 2 }
fn double(x: usize) -> usize {
x * 2
}
const X: fn(usize) -> usize = double;

const fn bar(x: fn(usize) -> usize, y: usize) -> usize {
Expand All @@ -17,8 +19,6 @@ const Z: usize = bar(double, 2); // FIXME: should fail to typeck someday
fn main() {
assert_eq!(Y, 4);
//~^ ERROR evaluation of constant expression failed
//~| ERROR erroneous constant used [E0080]
assert_eq!(Z, 4);
//~^ ERROR evaluation of constant expression failed
//~| ERROR erroneous constant used [E0080]
}
24 changes: 4 additions & 20 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail2.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
warning: skipping const checks
--> $DIR/const_fn_ptr_fail2.rs:11:5
--> $DIR/const_fn_ptr_fail2.rs:13:5
|
LL | x(y)
| ^^^^

error[E0080]: evaluation of constant expression failed
--> $DIR/const_fn_ptr_fail2.rs:18:5
--> $DIR/const_fn_ptr_fail2.rs:20:5
|
LL | assert_eq!(Y, 4);
| ^^^^^^^^^^^-^^^^^
Expand All @@ -14,16 +14,8 @@ LL | assert_eq!(Y, 4);
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0080]: erroneous constant used
--> $DIR/const_fn_ptr_fail2.rs:18:5
|
LL | assert_eq!(Y, 4);
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0080]: evaluation of constant expression failed
--> $DIR/const_fn_ptr_fail2.rs:21:5
--> $DIR/const_fn_ptr_fail2.rs:22:5
|
LL | assert_eq!(Z, 4);
| ^^^^^^^^^^^-^^^^^
Expand All @@ -32,14 +24,6 @@ LL | assert_eq!(Z, 4);
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0080]: erroneous constant used
--> $DIR/const_fn_ptr_fail2.rs:21:5
|
LL | assert_eq!(Z, 4);
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
8 changes: 4 additions & 4 deletions src/test/ui/consts/const-eval/issue-43197.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const fn foo(x: u32) -> u32 {
}

fn main() {
const X: u32 = 0-1;
const X: u32 = 0 - 1;
//~^ WARN any use of this value will cause
const Y: u32 = foo(0-1);
const Y: u32 = foo(0 - 1);
//~^ WARN any use of this value will cause
println!("{} {}", X, Y);
//~^ ERROR evaluation of constant expression failed
//~| ERROR evaluation of constant expression failed
//~| ERROR erroneous constant used [E0080]
//~| ERROR erroneous constant used [E0080]
//~| WARN erroneous constant used [const_err]
//~| WARN erroneous constant used [const_err]
}
14 changes: 7 additions & 7 deletions src/test/ui/consts/const-eval/issue-43197.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: any use of this value will cause an error
--> $DIR/issue-43197.rs:10:20
|
LL | const X: u32 = 0-1;
| ---------------^^^-
LL | const X: u32 = 0 - 1;
| ---------------^^^^^-
| |
| attempt to subtract with overflow
|
Expand All @@ -15,8 +15,8 @@ LL | #![warn(const_err)]
warning: any use of this value will cause an error
--> $DIR/issue-43197.rs:12:24
|
LL | const Y: u32 = foo(0-1);
| -------------------^^^--
LL | const Y: u32 = foo(0 - 1);
| -------------------^^^^^--
| |
| attempt to subtract with overflow

Expand All @@ -26,7 +26,7 @@ error[E0080]: evaluation of constant expression failed
LL | println!("{} {}", X, Y);
| ^ referenced constant has errors

error[E0080]: erroneous constant used
warning: erroneous constant used
--> $DIR/issue-43197.rs:14:23
|
LL | println!("{} {}", X, Y);
Expand All @@ -38,12 +38,12 @@ error[E0080]: evaluation of constant expression failed
LL | println!("{} {}", X, Y);
| ^ referenced constant has errors

error[E0080]: erroneous constant used
warning: erroneous constant used
--> $DIR/issue-43197.rs:14:26
|
LL | println!("{} {}", X, Y);
| ^ referenced constant has errors

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
3 changes: 1 addition & 2 deletions src/test/ui/consts/const-eval/issue-44578.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ impl Foo for u16 {

fn main() {
println!("{}", <Bar<u16, u8> as Foo>::AMT);
//~^ ERROR erroneous constant used [E0080]
//~| ERROR evaluation of constant expression failed [E0080]
//~^ ERROR evaluation of constant expression failed [E0080]
}
8 changes: 1 addition & 7 deletions src/test/ui/consts/const-eval/issue-44578.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ error[E0080]: evaluation of constant expression failed
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors

error[E0080]: erroneous constant used
--> $DIR/issue-44578.rs:27:20
|
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
30 changes: 15 additions & 15 deletions src/test/ui/consts/const-eval/promoted_errors.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// build-fail
// build-pass
// compile-flags: -O

#![deny(const_err)]
#![warn(const_err)]

fn main() {
println!("{}", 0u32 - 1);
let _x = 0u32 - 1;
//~^ ERROR const_err
println!("{}", 1/(1-1));
//~^ ERROR attempt to divide by zero [const_err]
//~| ERROR const_err
//~| ERROR erroneous constant used [E0080]
let _x = 1/(1-1);
//~^ ERROR const_err
println!("{}", 1/(false as u32));
//~^ ERROR attempt to divide by zero [const_err]
//~| ERROR const_err
//~| ERROR erroneous constant used [E0080]
let _x = 1/(false as u32);
//~^ ERROR const_err
//~^ WARN const_err
println!("{}", 1 / (1 - 1));
//~^ WARN attempt to divide by zero [const_err]
//~| WARN const_err
//~| WARN erroneous constant used [const_err]
let _x = 1 / (1 - 1);
//~^ WARN const_err
println!("{}", 1 / (false as u32));
//~^ WARN attempt to divide by zero [const_err]
//~| WARN const_err
//~| WARN erroneous constant used [const_err]
let _x = 1 / (false as u32);
//~^ WARN const_err
}
Loading

0 comments on commit ecd5852

Please sign in to comment.