Skip to content

Commit 9cb47de

Browse files
committed
Referring to erroneous constants in promoteds must abort the build
1 parent 5c0d135 commit 9cb47de

14 files changed

+291
-33
lines changed

src/librustc_mir/monomorphize/collector.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,25 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11901190
let param_env = ty::ParamEnv::reveal_all();
11911191
for i in 0..mir.promoted.len() {
11921192
use rustc_data_structures::indexed_vec::Idx;
1193+
let i = Promoted::new(i);
11931194
let cid = GlobalId {
11941195
instance,
1195-
promoted: Some(Promoted::new(i)),
1196+
promoted: Some(i),
11961197
};
11971198
match tcx.const_eval(param_env.and(cid)) {
11981199
Ok(val) => collect_const(tcx, val, instance.substs, output),
1199-
Err(_) => {},
1200+
Err(err) => {
1201+
use rustc::middle::const_val::ErrKind;
1202+
use rustc::mir::interpret::EvalErrorKind;
1203+
if let ErrKind::Miri(ref miri, ..) = *err.kind {
1204+
if let EvalErrorKind::ReferencedConstant = miri.kind {
1205+
err.report_as_error(
1206+
tcx.at(mir.promoted[i].span),
1207+
"erroneous constant used",
1208+
);
1209+
}
1210+
}
1211+
},
12001212
}
12011213
}
12021214
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
warning: attempt to subtract with overflow
2+
--> $DIR/conditional_array_execution.rs:15:19
3+
|
4+
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5+
| ^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/conditional_array_execution.rs:11:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
12+
13+
warning: this constant cannot be used
14+
--> $DIR/conditional_array_execution.rs:15:1
15+
|
16+
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
17+
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
| |
19+
| attempt to subtract with overflow
20+
21+
warning: this expression will panic at runtime
22+
--> $DIR/conditional_array_execution.rs:20:20
23+
|
24+
LL | println!("{}", FOO);
25+
| ^^^ referenced constant has errors
26+
27+
error[E0080]: erroneous constant used
28+
--> $DIR/conditional_array_execution.rs:20:5
29+
|
30+
LL | println!("{}", FOO);
31+
| ^^^^^^^^^^^^^^^---^^
32+
| |
33+
| referenced constant has errors
34+
|
35+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
36+
37+
error[E0080]: erroneous constant used
38+
--> $DIR/conditional_array_execution.rs:20:20
39+
|
40+
LL | println!("{}", FOO);
41+
| ^^^ referenced constant has errors
42+
43+
error: aborting due to 2 previous errors
44+
45+
For more information about this error, try `rustc --explain E0080`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-pass
1211
#![warn(const_err)]
1312

1413
const X: u32 = 5;
@@ -20,4 +19,5 @@ const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
2019
fn main() {
2120
println!("{}", FOO);
2221
//~^ WARN this expression will panic at runtime
22+
//~| ERROR erroneous constant used
2323
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/conditional_array_execution.rs:16:19
2+
--> $DIR/conditional_array_execution.rs:15:19
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
55
| ^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/conditional_array_execution.rs:12:9
8+
--> $DIR/conditional_array_execution.rs:11:9
99
|
1010
LL | #![warn(const_err)]
1111
| ^^^^^^^^^
1212

1313
warning: this constant cannot be used
14-
--> $DIR/conditional_array_execution.rs:16:1
14+
--> $DIR/conditional_array_execution.rs:15:1
1515
|
1616
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1717
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
| |
1919
| attempt to subtract with overflow
2020

2121
warning: this expression will panic at runtime
22-
--> $DIR/conditional_array_execution.rs:21:20
22+
--> $DIR/conditional_array_execution.rs:20:20
2323
|
2424
LL | println!("{}", FOO);
2525
| ^^^ referenced constant has errors
2626

27+
error[E0080]: erroneous constant used
28+
--> $DIR/conditional_array_execution.rs:20:20
29+
|
30+
LL | println!("{}", FOO);
31+
| ^^^ referenced constant has errors
32+
33+
error: aborting due to previous error
34+
35+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
warning: attempt to subtract with overflow
2+
--> $DIR/issue-43197.rs:20:20
3+
|
4+
LL | const X: u32 = 0-1;
5+
| ^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/issue-43197.rs:11:9
9+
|
10+
LL | #![warn(const_err)]
11+
| ^^^^^^^^^
12+
13+
warning: this constant cannot be used
14+
--> $DIR/issue-43197.rs:20:5
15+
|
16+
LL | const X: u32 = 0-1;
17+
| ^^^^^^^^^^^^^^^---^
18+
| |
19+
| attempt to subtract with overflow
20+
21+
warning: attempt to subtract with overflow
22+
--> $DIR/issue-43197.rs:23:24
23+
|
24+
LL | const Y: u32 = foo(0-1);
25+
| ^^^
26+
27+
warning: this constant cannot be used
28+
--> $DIR/issue-43197.rs:23:5
29+
|
30+
LL | const Y: u32 = foo(0-1);
31+
| ^^^^^^^^^^^^^^^^^^^---^^
32+
| |
33+
| attempt to subtract with overflow
34+
35+
warning: this expression will panic at runtime
36+
--> $DIR/issue-43197.rs:26:23
37+
|
38+
LL | println!("{} {}", X, Y);
39+
| ^ referenced constant has errors
40+
41+
warning: this expression will panic at runtime
42+
--> $DIR/issue-43197.rs:26:26
43+
|
44+
LL | println!("{} {}", X, Y);
45+
| ^ referenced constant has errors
46+
47+
error[E0080]: erroneous constant used
48+
--> $DIR/issue-43197.rs:26:5
49+
|
50+
LL | println!("{} {}", X, Y);
51+
| ^^^^^^^^^^^^^^^^^^-^^^^^
52+
| |
53+
| referenced constant has errors
54+
|
55+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
56+
57+
error[E0080]: erroneous constant used
58+
--> $DIR/issue-43197.rs:26:26
59+
|
60+
LL | println!("{} {}", X, Y);
61+
| ^ referenced constant has errors
62+
63+
error[E0080]: erroneous constant used
64+
--> $DIR/issue-43197.rs:26:23
65+
|
66+
LL | println!("{} {}", X, Y);
67+
| ^ referenced constant has errors
68+
69+
error: aborting due to 3 previous errors
70+
71+
For more information about this error, try `rustc --explain E0080`.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-pass
1211
#![warn(const_err)]
1312

1413
#![feature(const_fn)]
@@ -27,4 +26,6 @@ fn main() {
2726
println!("{} {}", X, Y);
2827
//~^ WARN this expression will panic at runtime
2928
//~| WARN this expression will panic at runtime
29+
//~| ERROR erroneous constant used
30+
//~| ERROR erroneous constant used
3031
}
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/issue-43197.rs:21:20
2+
--> $DIR/issue-43197.rs:20:20
33
|
44
LL | const X: u32 = 0-1;
55
| ^^^
66
|
77
note: lint level defined here
8-
--> $DIR/issue-43197.rs:12:9
8+
--> $DIR/issue-43197.rs:11:9
99
|
1010
LL | #![warn(const_err)]
1111
| ^^^^^^^^^
1212

1313
warning: this constant cannot be used
14-
--> $DIR/issue-43197.rs:21:5
14+
--> $DIR/issue-43197.rs:20:5
1515
|
1616
LL | const X: u32 = 0-1;
1717
| ^^^^^^^^^^^^^^^---^
1818
| |
1919
| attempt to subtract with overflow
2020

2121
warning: attempt to subtract with overflow
22-
--> $DIR/issue-43197.rs:24:24
22+
--> $DIR/issue-43197.rs:23:24
2323
|
2424
LL | const Y: u32 = foo(0-1);
2525
| ^^^
2626

2727
warning: this constant cannot be used
28-
--> $DIR/issue-43197.rs:24:5
28+
--> $DIR/issue-43197.rs:23:5
2929
|
3030
LL | const Y: u32 = foo(0-1);
3131
| ^^^^^^^^^^^^^^^^^^^---^^
3232
| |
3333
| attempt to subtract with overflow
3434

3535
warning: this expression will panic at runtime
36-
--> $DIR/issue-43197.rs:27:23
36+
--> $DIR/issue-43197.rs:26:23
3737
|
3838
LL | println!("{} {}", X, Y);
3939
| ^ referenced constant has errors
4040

4141
warning: this expression will panic at runtime
42-
--> $DIR/issue-43197.rs:27:26
42+
--> $DIR/issue-43197.rs:26:26
4343
|
4444
LL | println!("{} {}", X, Y);
4545
| ^ referenced constant has errors
4646

47+
error[E0080]: erroneous constant used
48+
--> $DIR/issue-43197.rs:26:26
49+
|
50+
LL | println!("{} {}", X, Y);
51+
| ^ referenced constant has errors
52+
53+
error[E0080]: erroneous constant used
54+
--> $DIR/issue-43197.rs:26:23
55+
|
56+
LL | println!("{} {}", X, Y);
57+
| ^ referenced constant has errors
58+
59+
error: aborting due to 2 previous errors
60+
61+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0080]: erroneous constant used
2+
--> $DIR/issue-44578.rs:35:5
3+
|
4+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
5+
| ^^^^^^^^^^^^^^^--------------------------^^
6+
| |
7+
| referenced constant has errors
8+
|
9+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
10+
11+
error[E0080]: erroneous constant used
12+
--> $DIR/issue-44578.rs:35:20
13+
|
14+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0080`.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-pass
12-
#![warn(const_err)]
11+
#![allow(const_err)]
1312

1413
trait Foo {
1514
const AMT: usize;
@@ -33,6 +32,6 @@ impl Foo for u16 {
3332
}
3433

3534
fn main() {
36-
println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
37-
//~^ WARN const_err
35+
println!("{}", <Bar<u16, u8> as Foo>::AMT);
36+
//~^ ERROR erroneous constant used
3837
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
warning: this expression will panic at runtime
2-
--> $DIR/issue-44578.rs:36:20
1+
error[E0080]: erroneous constant used
2+
--> $DIR/issue-44578.rs:35:20
33
|
4-
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
4+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
6-
|
7-
note: lint level defined here
8-
--> $DIR/issue-44578.rs:12:9
9-
|
10-
LL | #![warn(const_err)]
11-
| ^^^^^^^^^
126

13-
warning: this expression will panic at runtime
14-
--> $DIR/issue-44578.rs:36:20
15-
|
16-
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
7+
error: aborting due to previous error
188

9+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait C {
12+
const BOO: usize;
13+
}
14+
15+
trait Foo<T> {
16+
const BAR: usize;
17+
}
18+
19+
struct A<T>(T);
20+
21+
impl<T: C> Foo<T> for A<T> {
22+
const BAR: usize = [5, 6, 7][T::BOO];
23+
}
24+
25+
fn foo<T: C>() -> &'static usize {
26+
&<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
27+
}
28+
29+
impl C for () {
30+
const BOO: usize = 42;
31+
}
32+
33+
impl C for u32 {
34+
const BOO: usize = 1;
35+
}
36+
37+
fn main() {
38+
println!("{:x}", foo::<()>() as *const usize as usize);
39+
println!("{:x}", foo::<u32>() as *const usize as usize);
40+
println!("{:x}", foo::<()>());
41+
println!("{:x}", foo::<u32>());
42+
}

0 commit comments

Comments
 (0)