Skip to content

Commit ecd5852

Browse files
committed
Errors in promoteds may only cause lints not hard errors
1 parent 6e1bbff commit ecd5852

19 files changed

+157
-176
lines changed

src/librustc_codegen_ssa/mir/constant.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2020
// use `get_static` to get at their id.
2121
// FIXME(oli-obk): can we unify this somehow, maybe by making const eval of statics
2222
// always produce `&STATIC`. This may also simplify how const eval works with statics.
23-
ty::ConstKind::Unevaluated(def_id, substs, promoted)
24-
if self.cx.tcx().is_static(def_id) =>
25-
{
26-
assert!(promoted.is_none());
23+
ty::ConstKind::Unevaluated(def_id, substs, None) if self.cx.tcx().is_static(def_id) => {
2724
assert!(substs.is_empty(), "we don't support generic statics yet");
2825
let static_ = bx.get_static(def_id);
2926
// we treat operands referring to statics as if they were `&STATIC` instead
@@ -49,10 +46,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4946
.tcx()
5047
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
5148
.map_err(|err| {
52-
self.cx
53-
.tcx()
54-
.sess
55-
.span_err(constant.span, "erroneous constant encountered");
49+
if promoted.is_none() {
50+
self.cx
51+
.tcx()
52+
.sess
53+
.span_err(constant.span, "erroneous constant encountered");
54+
}
5655
err
5756
})
5857
}

src/librustc_mir/transform/const_prop.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::ty::layout::{
1818
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
1919
};
2020
use rustc::ty::subst::{InternalSubsts, Subst};
21-
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
21+
use rustc::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
2222
use rustc_data_structures::fx::FxHashMap;
2323
use rustc_hir::def::DefKind;
2424
use rustc_hir::def_id::DefId;
@@ -441,8 +441,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
441441
Ok(op) => Some(op),
442442
Err(error) => {
443443
let err = error_to_const_error(&self.ecx, error);
444-
match self.lint_root(source_info) {
445-
Some(lint_root) if c.literal.needs_subst() => {
444+
if let Some(lint_root) = self.lint_root(source_info) {
445+
let lint_only = match c.literal.val {
446+
// Promoteds must lint and not error as the user didn't ask for them
447+
ConstKind::Unevaluated(_, _, Some(_)) => true,
448+
// Out of backwards compatibility we cannot report hard errors in unused
449+
// generic functions using associated constants of the generic parameters.
450+
_ => c.literal.needs_subst(),
451+
};
452+
if lint_only {
446453
// Out of backwards compatibility we cannot report hard errors in unused
447454
// generic functions using associated constants of the generic parameters.
448455
err.report_as_lint(
@@ -451,10 +458,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
451458
lint_root,
452459
Some(c.span),
453460
);
454-
}
455-
_ => {
461+
} else {
456462
err.report_as_error(self.ecx.tcx, "erroneous constant used");
457463
}
464+
} else {
465+
err.report_as_error(self.ecx.tcx, "erroneous constant used");
458466
}
459467
None
460468
}

src/test/compile-fail/promoted_div_by_zero.rs renamed to src/test/run-fail/promoted_div_by_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(const_err)]
22

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

55
fn main() {
66
let x = &(1 / (1 - 1));
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// build-fail
1+
// build-pass
2+
3+
#![warn(const_err)]
24

35
fn main() {
4-
&{[1, 2, 3][4]};
5-
//~^ ERROR index out of bounds
6-
//~| ERROR reaching this expression at runtime will panic or abort
7-
//~| ERROR erroneous constant used [E0080]
6+
&{ [1, 2, 3][4] };
7+
//~^ WARN index out of bounds
8+
//~| WARN reaching this expression at runtime will panic or abort
9+
//~| WARN erroneous constant used [const_err]
810
}

src/test/ui/consts/array-literal-index-oob.stderr

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
error: index out of bounds: the len is 3 but the index is 4
2-
--> $DIR/array-literal-index-oob.rs:4:7
1+
warning: index out of bounds: the len is 3 but the index is 4
2+
--> $DIR/array-literal-index-oob.rs:6:8
33
|
4-
LL | &{[1, 2, 3][4]};
5-
| ^^^^^^^^^^^^
4+
LL | &{ [1, 2, 3][4] };
5+
| ^^^^^^^^^^^^
66
|
7-
= note: `#[deny(const_err)]` on by default
8-
9-
error: reaching this expression at runtime will panic or abort
10-
--> $DIR/array-literal-index-oob.rs:4:7
7+
note: lint level defined here
8+
--> $DIR/array-literal-index-oob.rs:3:9
119
|
12-
LL | &{[1, 2, 3][4]};
13-
| --^^^^^^^^^^^^-
14-
| |
15-
| indexing out of bounds: the len is 3 but the index is 4
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
1612

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

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

25-
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/conditional_array_execution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1010
fn main() {
1111
println!("{}", FOO);
1212
//~^ ERROR
13-
//~| ERROR erroneous constant used [E0080]
13+
//~| WARN erroneous constant used [const_err]
1414
}

src/test/ui/consts/const-eval/conditional_array_execution.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ error[E0080]: evaluation of constant expression failed
1818
LL | println!("{}", FOO);
1919
| ^^^ referenced constant has errors
2020

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

27-
error: aborting due to 2 previous errors
27+
error: aborting due to previous error
2828

2929
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#![feature(const_fn)]
55
#![allow(const_err)]
66

7-
fn double(x: usize) -> usize { x * 2 }
7+
fn double(x: usize) -> usize {
8+
x * 2
9+
}
810
const X: fn(usize) -> usize = double;
911

1012
const fn bar(x: fn(usize) -> usize, y: usize) -> usize {
@@ -17,8 +19,6 @@ const Z: usize = bar(double, 2); // FIXME: should fail to typeck someday
1719
fn main() {
1820
assert_eq!(Y, 4);
1921
//~^ ERROR evaluation of constant expression failed
20-
//~| ERROR erroneous constant used [E0080]
2122
assert_eq!(Z, 4);
2223
//~^ ERROR evaluation of constant expression failed
23-
//~| ERROR erroneous constant used [E0080]
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
warning: skipping const checks
2-
--> $DIR/const_fn_ptr_fail2.rs:11:5
2+
--> $DIR/const_fn_ptr_fail2.rs:13:5
33
|
44
LL | x(y)
55
| ^^^^
66

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

17-
error[E0080]: erroneous constant used
18-
--> $DIR/const_fn_ptr_fail2.rs:18:5
19-
|
20-
LL | assert_eq!(Y, 4);
21-
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
22-
|
23-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
24-
2517
error[E0080]: evaluation of constant expression failed
26-
--> $DIR/const_fn_ptr_fail2.rs:21:5
18+
--> $DIR/const_fn_ptr_fail2.rs:22:5
2719
|
2820
LL | assert_eq!(Z, 4);
2921
| ^^^^^^^^^^^-^^^^^
@@ -32,14 +24,6 @@ LL | assert_eq!(Z, 4);
3224
|
3325
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
3426

35-
error[E0080]: erroneous constant used
36-
--> $DIR/const_fn_ptr_fail2.rs:21:5
37-
|
38-
LL | assert_eq!(Z, 4);
39-
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
40-
|
41-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
42-
43-
error: aborting due to 4 previous errors
27+
error: aborting due to 2 previous errors
4428

4529
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/issue-43197.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const fn foo(x: u32) -> u32 {
77
}
88

99
fn main() {
10-
const X: u32 = 0-1;
10+
const X: u32 = 0 - 1;
1111
//~^ WARN any use of this value will cause
12-
const Y: u32 = foo(0-1);
12+
const Y: u32 = foo(0 - 1);
1313
//~^ WARN any use of this value will cause
1414
println!("{} {}", X, Y);
1515
//~^ ERROR evaluation of constant expression failed
1616
//~| ERROR evaluation of constant expression failed
17-
//~| ERROR erroneous constant used [E0080]
18-
//~| ERROR erroneous constant used [E0080]
17+
//~| WARN erroneous constant used [const_err]
18+
//~| WARN erroneous constant used [const_err]
1919
}

src/test/ui/consts/const-eval/issue-43197.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: any use of this value will cause an error
22
--> $DIR/issue-43197.rs:10:20
33
|
4-
LL | const X: u32 = 0-1;
5-
| ---------------^^^-
4+
LL | const X: u32 = 0 - 1;
5+
| ---------------^^^^^-
66
| |
77
| attempt to subtract with overflow
88
|
@@ -15,8 +15,8 @@ LL | #![warn(const_err)]
1515
warning: any use of this value will cause an error
1616
--> $DIR/issue-43197.rs:12:24
1717
|
18-
LL | const Y: u32 = foo(0-1);
19-
| -------------------^^^--
18+
LL | const Y: u32 = foo(0 - 1);
19+
| -------------------^^^^^--
2020
| |
2121
| attempt to subtract with overflow
2222

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

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

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

47-
error: aborting due to 4 previous errors
47+
error: aborting due to 2 previous errors
4848

4949
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/issue-44578.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ impl Foo for u16 {
2525

2626
fn main() {
2727
println!("{}", <Bar<u16, u8> as Foo>::AMT);
28-
//~^ ERROR erroneous constant used [E0080]
29-
//~| ERROR evaluation of constant expression failed [E0080]
28+
//~^ ERROR evaluation of constant expression failed [E0080]
3029
}

src/test/ui/consts/const-eval/issue-44578.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0080]: evaluation of constant expression failed
44
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
66

7-
error[E0080]: erroneous constant used
8-
--> $DIR/issue-44578.rs:27:20
9-
|
10-
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

159
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// build-fail
1+
// build-pass
22
// compile-flags: -O
33

4-
#![deny(const_err)]
4+
#![warn(const_err)]
55

66
fn main() {
77
println!("{}", 0u32 - 1);
88
let _x = 0u32 - 1;
9-
//~^ ERROR const_err
10-
println!("{}", 1/(1-1));
11-
//~^ ERROR attempt to divide by zero [const_err]
12-
//~| ERROR const_err
13-
//~| ERROR erroneous constant used [E0080]
14-
let _x = 1/(1-1);
15-
//~^ ERROR const_err
16-
println!("{}", 1/(false as u32));
17-
//~^ ERROR attempt to divide by zero [const_err]
18-
//~| ERROR const_err
19-
//~| ERROR erroneous constant used [E0080]
20-
let _x = 1/(false as u32);
21-
//~^ ERROR const_err
9+
//~^ WARN const_err
10+
println!("{}", 1 / (1 - 1));
11+
//~^ WARN attempt to divide by zero [const_err]
12+
//~| WARN const_err
13+
//~| WARN erroneous constant used [const_err]
14+
let _x = 1 / (1 - 1);
15+
//~^ WARN const_err
16+
println!("{}", 1 / (false as u32));
17+
//~^ WARN attempt to divide by zero [const_err]
18+
//~| WARN const_err
19+
//~| WARN erroneous constant used [const_err]
20+
let _x = 1 / (false as u32);
21+
//~^ WARN const_err
2222
}

0 commit comments

Comments
 (0)