Skip to content

Commit 8e04395

Browse files
committed
Swap const evaluation lint spans to point at problem in primary span
1 parent 1b0ab0b commit 8e04395

35 files changed

+358
-304
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
100100
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
101101
message: &str,
102102
lint_root: hir::HirId,
103+
span: Option<Span>,
103104
) -> ErrorHandled {
104105
let lint = self.struct_generic(
105106
tcx,
@@ -108,6 +109,16 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
108109
);
109110
match lint {
110111
Ok(mut lint) => {
112+
if let Some(span) = span {
113+
let primary_spans = lint.span.primary_spans().to_vec();
114+
// point at the actual error as the primary span
115+
lint.replace_span_with(span);
116+
// point to the `const` statement as a secondary span
117+
// they don't have any label
118+
for sp in primary_spans {
119+
lint.span_label(sp, "");
120+
}
121+
}
111122
lint.emit();
112123
ErrorHandled::Reported
113124
},

src/librustc_mir/const_eval.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
668668
tcx.at(tcx.def_span(def_id)),
669669
"any use of this value will cause an error",
670670
hir_id,
671+
Some(err.span),
671672
)
672673
},
673674
// promoting runtime code is only allowed to error if it references broken constants
@@ -684,6 +685,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
684685
tcx.at(span),
685686
"reaching this expression at runtime will panic or abort",
686687
tcx.hir().as_local_hir_id(def_id).unwrap(),
688+
Some(err.span),
687689
)
688690
}
689691
// anything else (array lengths, enum initializers, constant patterns) are reported

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
237237
self.ecx.tcx,
238238
"this expression will panic at runtime",
239239
lint_root,
240+
None,
240241
);
241242
}
242243
}

src/test/ui/array_const_index-0.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/array_const_index-0.rs:2:1
2+
--> $DIR/array_const_index-0.rs:2:16
33
|
44
LL | const B: i32 = (&A)[1];
5-
| ^^^^^^^^^^^^^^^-------^
5+
| ---------------^^^^^^^-
66
| |
77
| index out of bounds: the len is 0 but the index is 1
88
|

src/test/ui/array_const_index-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/array_const_index-1.rs:2:1
2+
--> $DIR/array_const_index-1.rs:2:16
33
|
44
LL | const B: i32 = A[1];
5-
| ^^^^^^^^^^^^^^^----^
5+
| ---------------^^^^-
66
| |
77
| index out of bounds: the len is 0 but the index is 1
88
|

src/test/ui/consts/const-err-early.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const-err-early.rs:3:1
2+
--> $DIR/const-err-early.rs:3:19
33
|
44
LL | pub const A: i8 = -std::i8::MIN;
5-
| ^^^^^^^^^^^^^^^^^^-------------^
5+
| ------------------^^^^^^^^^^^^^-
66
| |
77
| attempt to negate with overflow
88
|
@@ -13,34 +13,34 @@ LL | #![deny(const_err)]
1313
| ^^^^^^^^^
1414

1515
error: any use of this value will cause an error
16-
--> $DIR/const-err-early.rs:4:1
16+
--> $DIR/const-err-early.rs:4:19
1717
|
1818
LL | pub const B: u8 = 200u8 + 200u8;
19-
| ^^^^^^^^^^^^^^^^^^-------------^
19+
| ------------------^^^^^^^^^^^^^-
2020
| |
2121
| attempt to add with overflow
2222

2323
error: any use of this value will cause an error
24-
--> $DIR/const-err-early.rs:5:1
24+
--> $DIR/const-err-early.rs:5:19
2525
|
2626
LL | pub const C: u8 = 200u8 * 4;
27-
| ^^^^^^^^^^^^^^^^^^---------^
27+
| ------------------^^^^^^^^^-
2828
| |
2929
| attempt to multiply with overflow
3030

3131
error: any use of this value will cause an error
32-
--> $DIR/const-err-early.rs:6:1
32+
--> $DIR/const-err-early.rs:6:19
3333
|
3434
LL | pub const D: u8 = 42u8 - (42u8 + 1);
35-
| ^^^^^^^^^^^^^^^^^^-----------------^
35+
| ------------------^^^^^^^^^^^^^^^^^-
3636
| |
3737
| attempt to subtract with overflow
3838

3939
error: any use of this value will cause an error
40-
--> $DIR/const-err-early.rs:7:1
40+
--> $DIR/const-err-early.rs:7:19
4141
|
4242
LL | pub const E: u8 = [5u8][1];
43-
| ^^^^^^^^^^^^^^^^^^--------^
43+
| ------------------^^^^^^^^-
4444
| |
4545
| index out of bounds: the len is 1 but the index is 1
4646

src/test/ui/consts/const-err-multi.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const-err-multi.rs:3:1
2+
--> $DIR/const-err-multi.rs:3:19
33
|
44
LL | pub const A: i8 = -std::i8::MIN;
5-
| ^^^^^^^^^^^^^^^^^^-------------^
5+
| ------------------^^^^^^^^^^^^^-
66
| |
77
| attempt to negate with overflow
88
|
@@ -13,26 +13,26 @@ LL | #![deny(const_err)]
1313
| ^^^^^^^^^
1414

1515
error: any use of this value will cause an error
16-
--> $DIR/const-err-multi.rs:5:1
16+
--> $DIR/const-err-multi.rs:5:19
1717
|
1818
LL | pub const B: i8 = A;
19-
| ^^^^^^^^^^^^^^^^^^-^
19+
| ------------------^-
2020
| |
2121
| referenced constant has errors
2222

2323
error: any use of this value will cause an error
24-
--> $DIR/const-err-multi.rs:7:1
24+
--> $DIR/const-err-multi.rs:7:19
2525
|
2626
LL | pub const C: u8 = A as u8;
27-
| ^^^^^^^^^^^^^^^^^^-------^
27+
| ------------------^^^^^^^-
2828
| |
2929
| referenced constant has errors
3030

3131
error: any use of this value will cause an error
32-
--> $DIR/const-err-multi.rs:9:1
32+
--> $DIR/const-err-multi.rs:9:19
3333
|
3434
LL | pub const D: i8 = 50 - A;
35-
| ^^^^^^^^^^^^^^^^^^------^
35+
| ------------------^^^^^^-
3636
| |
3737
| referenced constant has errors
3838

src/test/ui/consts/const-err.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: any use of this value will cause an error
2-
--> $DIR/const-err.rs:10:1
2+
--> $DIR/const-err.rs:10:17
33
|
44
LL | const FOO: u8 = [5u8][1];
5-
| ^^^^^^^^^^^^^^^^--------^
5+
| ----------------^^^^^^^^-
66
| |
77
| index out of bounds: the len is 1 but the index is 1
88
|

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: any use of this value will cause an error
2-
--> $DIR/conditional_array_execution.rs:5:1
2+
--> $DIR/conditional_array_execution.rs:5:19
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5-
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ------------------^^^^^---------------------------
66
| |
77
| attempt to subtract with overflow
88
|

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,51 @@ use std::fmt;
1111
use std::{i8, i16, i32, i64, isize};
1212
use std::{u8, u16, u32, u64, usize};
1313

14-
const VALS_I8: (i8,) = //~ ERROR any use of this value will cause an error
14+
const VALS_I8: (i8,) =
1515
(
1616
i8::MIN - 1,
1717
);
18+
//~^^ ERROR any use of this value will cause an error
1819

19-
const VALS_I16: (i16,) = //~ ERROR any use of this value will cause an error
20+
const VALS_I16: (i16,) =
2021
(
2122
i16::MIN - 1,
2223
);
24+
//~^^ ERROR any use of this value will cause an error
2325

24-
const VALS_I32: (i32,) = //~ ERROR any use of this value will cause an error
26+
const VALS_I32: (i32,) =
2527
(
2628
i32::MIN - 1,
2729
);
30+
//~^^ ERROR any use of this value will cause an error
2831

29-
const VALS_I64: (i64,) = //~ ERROR any use of this value will cause an error
32+
const VALS_I64: (i64,) =
3033
(
3134
i64::MIN - 1,
3235
);
36+
//~^^ ERROR any use of this value will cause an error
3337

34-
const VALS_U8: (u8,) = //~ ERROR any use of this value will cause an error
38+
const VALS_U8: (u8,) =
3539
(
3640
u8::MIN - 1,
3741
);
42+
//~^^ ERROR any use of this value will cause an error
3843

39-
const VALS_U16: (u16,) = ( //~ ERROR any use of this value will cause an error
44+
const VALS_U16: (u16,) = (
4045
u16::MIN - 1,
4146
);
47+
//~^^ ERROR any use of this value will cause an error
4248

43-
const VALS_U32: (u32,) = ( //~ ERROR any use of this value will cause an error
49+
const VALS_U32: (u32,) = (
4450
u32::MIN - 1,
4551
);
52+
//~^^ ERROR any use of this value will cause an error
4653

47-
const VALS_U64: (u64,) = //~ ERROR any use of this value will cause an error
54+
const VALS_U64: (u64,) =
4855
(
4956
u64::MIN - 1,
5057
);
58+
//~^^ ERROR any use of this value will cause an error
5159

5260
fn main() {
5361
foo(VALS_I8);
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const-eval-overflow2.rs:14:1
2+
--> $DIR/const-eval-overflow2.rs:16:6
33
|
44
LL | / const VALS_I8: (i8,) =
55
LL | | (
66
LL | | i8::MIN - 1,
7-
| | ----------- attempt to subtract with overflow
7+
| | ^^^^^^^^^^^ attempt to subtract with overflow
88
LL | | );
9-
| |_______^
9+
| |_______-
1010
|
1111
note: lint level defined here
1212
--> $DIR/const-eval-overflow2.rs:8:9
@@ -15,72 +15,72 @@ LL | #![deny(const_err)]
1515
| ^^^^^^^^^
1616

1717
error: any use of this value will cause an error
18-
--> $DIR/const-eval-overflow2.rs:19:1
18+
--> $DIR/const-eval-overflow2.rs:22:6
1919
|
2020
LL | / const VALS_I16: (i16,) =
2121
LL | | (
2222
LL | | i16::MIN - 1,
23-
| | ------------ attempt to subtract with overflow
23+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
2424
LL | | );
25-
| |_______^
25+
| |_______-
2626

2727
error: any use of this value will cause an error
28-
--> $DIR/const-eval-overflow2.rs:24:1
28+
--> $DIR/const-eval-overflow2.rs:28:6
2929
|
3030
LL | / const VALS_I32: (i32,) =
3131
LL | | (
3232
LL | | i32::MIN - 1,
33-
| | ------------ attempt to subtract with overflow
33+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
3434
LL | | );
35-
| |_______^
35+
| |_______-
3636

3737
error: any use of this value will cause an error
38-
--> $DIR/const-eval-overflow2.rs:29:1
38+
--> $DIR/const-eval-overflow2.rs:34:6
3939
|
4040
LL | / const VALS_I64: (i64,) =
4141
LL | | (
4242
LL | | i64::MIN - 1,
43-
| | ------------ attempt to subtract with overflow
43+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
4444
LL | | );
45-
| |_______^
45+
| |_______-
4646

4747
error: any use of this value will cause an error
48-
--> $DIR/const-eval-overflow2.rs:34:1
48+
--> $DIR/const-eval-overflow2.rs:40:6
4949
|
5050
LL | / const VALS_U8: (u8,) =
5151
LL | | (
5252
LL | | u8::MIN - 1,
53-
| | ----------- attempt to subtract with overflow
53+
| | ^^^^^^^^^^^ attempt to subtract with overflow
5454
LL | | );
55-
| |_______^
55+
| |_______-
5656

5757
error: any use of this value will cause an error
58-
--> $DIR/const-eval-overflow2.rs:39:1
58+
--> $DIR/const-eval-overflow2.rs:45:6
5959
|
6060
LL | / const VALS_U16: (u16,) = (
6161
LL | | u16::MIN - 1,
62-
| | ------------ attempt to subtract with overflow
62+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
6363
LL | | );
64-
| |_______^
64+
| |_______-
6565

6666
error: any use of this value will cause an error
67-
--> $DIR/const-eval-overflow2.rs:43:1
67+
--> $DIR/const-eval-overflow2.rs:50:6
6868
|
6969
LL | / const VALS_U32: (u32,) = (
7070
LL | | u32::MIN - 1,
71-
| | ------------ attempt to subtract with overflow
71+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
7272
LL | | );
73-
| |_______^
73+
| |_______-
7474

7575
error: any use of this value will cause an error
76-
--> $DIR/const-eval-overflow2.rs:47:1
76+
--> $DIR/const-eval-overflow2.rs:56:6
7777
|
7878
LL | / const VALS_U64: (u64,) =
7979
LL | | (
8080
LL | | u64::MIN - 1,
81-
| | ------------ attempt to subtract with overflow
81+
| | ^^^^^^^^^^^^ attempt to subtract with overflow
8282
LL | | );
83-
| |_______^
83+
| |_______-
8484

8585
error: aborting due to 8 previous errors
8686

0 commit comments

Comments
 (0)