Skip to content

Commit f07c2d9

Browse files
authored
Unrolled build for rust-lang#122471
Rollup merge of rust-lang#122471 - RalfJung:const-eval-span, r=oli-obk preserve span when evaluating mir::ConstOperand This lets us show to the user where they were using the faulty const (which can be quite relevant when generics are involved). I wonder if we should change "erroneous constant encountered" to something like "the above error was encountered while evaluating this constant" or so, to make this more similar to what the collector emits when showing a "backtrace" of where things get monomorphized? It seems a bit strange to rely on the order of emitted diagnostics for that but it seems the collector already [does that](https://github.com/rust-lang/rust/blob/da8a8c9223722e17cc0173ce9490076b4a6d263d/compiler/rustc_monomorphize/src/collector.rs#L472-L475).
2 parents ee03c28 + 48f2f0d commit f07c2d9

21 files changed

+192
-7
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
393393
}
394394
}
395395
Operand::Constant(box constant) => {
396-
if let Ok(constant) = self.ecx.eval_mir_constant(&constant.const_, None, None) {
396+
if let Ok(constant) =
397+
self.ecx.eval_mir_constant(&constant.const_, Some(constant.span), None)
398+
{
397399
self.assign_constant(state, place, constant, &[]);
398400
}
399401
}

compiler/rustc_mir_transform/src/jump_threading.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
416416
match rhs {
417417
// If we expect `lhs ?= A`, we have an opportunity if we assume `constant == A`.
418418
Operand::Constant(constant) => {
419-
let constant = self.ecx.eval_mir_constant(&constant.const_, None, None).ok()?;
419+
let constant =
420+
self.ecx.eval_mir_constant(&constant.const_, Some(constant.span), None).ok()?;
420421
self.process_constant(bb, lhs, constant, state);
421422
}
422423
// Transfer the conditions on the copied rhs.

compiler/rustc_monomorphize/src/collector.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,17 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
828828
// a codegen-time error). rustc stops after collection if there was an error, so this
829829
// ensures codegen never has to worry about failing consts.
830830
// (codegen relies on this and ICEs will happen if this is violated.)
831-
let val = match const_.eval(self.tcx, param_env, None) {
831+
let val = match const_.eval(self.tcx, param_env, Some(constant.span)) {
832832
Ok(v) => v,
833-
Err(ErrorHandled::Reported(..)) => return,
834833
Err(ErrorHandled::TooGeneric(..)) => span_bug!(
835834
self.body.source_info(location).span,
836835
"collection encountered polymorphic constant: {:?}",
837836
const_
838837
),
838+
Err(err @ ErrorHandled::Reported(..)) => {
839+
err.emit_note(self.tcx);
840+
return;
841+
}
839842
};
840843
collect_const_value(self.tcx, val, self.output);
841844
MirVisitor::visit_ty(self, const_.ty(), TyContext::Location(location));

compiler/rustc_smir/src/rustc_smir/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> MutVisitor<'tcx> for BodyBuilder<'tcx> {
5656

5757
fn visit_constant(&mut self, constant: &mut mir::ConstOperand<'tcx>, location: mir::Location) {
5858
let const_ = self.monomorphize(constant.const_);
59-
let val = match const_.eval(self.tcx, ty::ParamEnv::reveal_all(), None) {
59+
let val = match const_.eval(self.tcx, ty::ParamEnv::reveal_all(), Some(constant.span)) {
6060
Ok(v) => v,
6161
Err(mir::interpret::ErrorHandled::Reported(..)) => return,
6262
Err(mir::interpret::ErrorHandled::TooGeneric(..)) => {

tests/ui/consts/assoc_const_generic_impl.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0080]: evaluation of `<u32 as ZeroSized>::I_AM_ZERO_SIZED` failed
44
LL | const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()];
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 4
66

7+
note: erroneous constant encountered
8+
--> $DIR/assoc_const_generic_impl.rs:11:9
9+
|
10+
LL | Self::I_AM_ZERO_SIZED;
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
713
note: the above error was encountered while instantiating `fn <u32 as ZeroSized>::requires_zero_size`
814
--> $DIR/assoc_const_generic_impl.rs:18:5
915
|

tests/ui/consts/const-eval/index-out-of-bounds-never-type.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0080]: evaluation of `PrintName::<()>::VOID` failed
44
LL | const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
55
| ^^^^^ index out of bounds: the length is 0 but the index is 0
66

7+
note: erroneous constant encountered
8+
--> $DIR/index-out-of-bounds-never-type.rs:16:13
9+
|
10+
LL | let _ = PrintName::<T>::VOID;
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
713
note: the above error was encountered while instantiating `fn f::<()>`
814
--> $DIR/index-out-of-bounds-never-type.rs:20:5
915
|

tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr

+46
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,52 @@ note: erroneous constant encountered
1010
LL | &<A<T> as Foo<T>>::BAR
1111
| ^^^^^^^^^^^^^^^^^^^^^
1212

13+
note: erroneous constant encountered
14+
--> $DIR/issue-50814-2.rs:20:5
15+
|
16+
LL | &<A<T> as Foo<T>>::BAR
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
note: erroneous constant encountered
20+
--> $DIR/issue-50814-2.rs:20:5
21+
|
22+
LL | &<A<T> as Foo<T>>::BAR
23+
| ^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
26+
27+
note: erroneous constant encountered
28+
--> $DIR/issue-50814-2.rs:20:5
29+
|
30+
LL | &<A<T> as Foo<T>>::BAR
31+
| ^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
34+
35+
note: erroneous constant encountered
36+
--> $DIR/issue-50814-2.rs:20:5
37+
|
38+
LL | &<A<T> as Foo<T>>::BAR
39+
| ^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
42+
43+
note: erroneous constant encountered
44+
--> $DIR/issue-50814-2.rs:20:6
45+
|
46+
LL | &<A<T> as Foo<T>>::BAR
47+
| ^^^^^^^^^^^^^^^^^^^^^
48+
|
49+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
50+
51+
note: erroneous constant encountered
52+
--> $DIR/issue-50814-2.rs:20:6
53+
|
54+
LL | &<A<T> as Foo<T>>::BAR
55+
| ^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
58+
1359
error: aborting due to 1 previous error
1460

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

tests/ui/consts/const-eval/issue-50814-2.normal.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ note: erroneous constant encountered
1010
LL | &<A<T> as Foo<T>>::BAR
1111
| ^^^^^^^^^^^^^^^^^^^^^
1212

13+
note: erroneous constant encountered
14+
--> $DIR/issue-50814-2.rs:20:5
15+
|
16+
LL | &<A<T> as Foo<T>>::BAR
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
note: erroneous constant encountered
20+
--> $DIR/issue-50814-2.rs:20:6
21+
|
22+
LL | &<A<T> as Foo<T>>::BAR
23+
| ^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
26+
1327
note: the above error was encountered while instantiating `fn foo::<()>`
1428
--> $DIR/issue-50814-2.rs:32:22
1529
|

tests/ui/consts/const-eval/issue-50814.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ LL | &Sum::<U8, U8>::MAX
2626
|
2727
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2828

29+
note: erroneous constant encountered
30+
--> $DIR/issue-50814.rs:21:5
31+
|
32+
LL | &Sum::<U8, U8>::MAX
33+
| ^^^^^^^^^^^^^^^^^^^
34+
35+
note: erroneous constant encountered
36+
--> $DIR/issue-50814.rs:21:6
37+
|
38+
LL | &Sum::<U8, U8>::MAX
39+
| ^^^^^^^^^^^^^^^^^^
40+
|
41+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
42+
2943
note: the above error was encountered while instantiating `fn foo::<i32>`
3044
--> $DIR/issue-50814.rs:26:5
3145
|

tests/ui/consts/const-eval/issue-85155.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ error[E0080]: evaluation of `post_monomorphization_error::ValidateConstImm::<2,
44
LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero
66

7+
note: erroneous constant encountered
8+
--> $DIR/auxiliary/post_monomorphization_error.rs:19:5
9+
|
10+
LL | static_assert_imm1!(IMM1);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
|
13+
= note: this note originates in the macro `static_assert_imm1` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
715
note: the above error was encountered while instantiating `fn post_monomorphization_error::stdarch_intrinsic::<2>`
816
--> $DIR/issue-85155.rs:19:5
917
|

tests/ui/consts/required-consts/collect-in-called-fn.noopt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-called-fn.rs:18:17
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn called::<i32>`
1016
--> $DIR/collect-in-called-fn.rs:23:5
1117
|

tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-called-fn.rs:18:17
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn called::<i32>`
1016
--> $DIR/collect-in-called-fn.rs:23:5
1117
|

tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-dead-drop.rs:19:17
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn <Fail<i32> as std::ops::Drop>::drop`
1016
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
1117

tests/ui/consts/required-consts/collect-in-dead-fn.noopt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-dead-fn.rs:22:17
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn not_called::<i32>`
1016
--> $DIR/collect-in-dead-fn.rs:29:9
1117
|

tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-dead-move.rs:19:17
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn <Fail<i32> as std::ops::Drop>::drop`
1016
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
1117

tests/ui/consts/required-consts/collect-in-dead-vtable.noopt.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/collect-in-dead-vtable.rs:26:21
11+
|
12+
LL | let _ = Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn <std::vec::Vec<i32> as MyTrait>::not_called`
1016
--> $DIR/collect-in-dead-vtable.rs:35:40
1117
|

tests/ui/generics/post_monomorphization_error_backtrace.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ fn assert_zst<T>() {
1212
//~| NOTE: the evaluated program panicked
1313
}
1414
F::<T>::V;
15+
//~^NOTE: erroneous constant
16+
//~|NOTE: erroneous constant
17+
//~|NOTE: duplicate
1518
}
1619

1720
fn foo<U>() {

tests/ui/generics/post_monomorphization_error_backtrace.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/post_monomorphization_error_backtrace.rs:14:5
11+
|
12+
LL | F::<T>::V;
13+
| ^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn assert_zst::<u32>`
10-
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
16+
--> $DIR/post_monomorphization_error_backtrace.rs:21:5
1117
|
1218
LL | assert_zst::<U>()
1319
| ^^^^^^^^^^^^^^^^^
@@ -20,8 +26,16 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
2026
|
2127
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
2228

29+
note: erroneous constant encountered
30+
--> $DIR/post_monomorphization_error_backtrace.rs:14:5
31+
|
32+
LL | F::<T>::V;
33+
| ^^^^^^^^^
34+
|
35+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
36+
2337
note: the above error was encountered while instantiating `fn assert_zst::<i32>`
24-
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
38+
--> $DIR/post_monomorphization_error_backtrace.rs:21:5
2539
|
2640
LL | assert_zst::<U>()
2741
| ^^^^^^^^^^^^^^^^^

tests/ui/inline-const/const-expr-generic-err.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const { assert!(std::mem::size_of::<T>() == 0); }
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/const-expr-generic-err.rs:5:5
11+
|
12+
LL | const { assert!(std::mem::size_of::<T>() == 0); }
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn foo::<i32>`
1016
--> $DIR/const-expr-generic-err.rs:13:5
1117
|
@@ -18,6 +24,20 @@ error[E0080]: evaluation of `bar::<0>::{constant#0}` failed
1824
LL | const { N - 1 }
1925
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
2026

27+
note: erroneous constant encountered
28+
--> $DIR/const-expr-generic-err.rs:9:5
29+
|
30+
LL | const { N - 1 }
31+
| ^^^^^^^^^^^^^^^
32+
33+
note: erroneous constant encountered
34+
--> $DIR/const-expr-generic-err.rs:9:5
35+
|
36+
LL | const { N - 1 }
37+
| ^^^^^^^^^^^^^^^
38+
|
39+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
40+
2141
note: the above error was encountered while instantiating `fn bar::<0>`
2242
--> $DIR/const-expr-generic-err.rs:14:5
2343
|

tests/ui/inline-const/required-const.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const { panic!() }
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/required-const.rs:7:9
11+
|
12+
LL | const { panic!() }
13+
| ^^^^^^^^^^^^^^^^^^
14+
915
error: aborting due to 1 previous error
1016

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

tests/ui/simd/const-err-trumps-simd-err.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const { assert!(LANE < 4); } // the error should be here...
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: erroneous constant encountered
10+
--> $DIR/const-err-trumps-simd-err.rs:16:5
11+
|
12+
LL | const { assert!(LANE < 4); } // the error should be here...
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
915
note: the above error was encountered while instantiating `fn get_elem::<4>`
1016
--> $DIR/const-err-trumps-simd-err.rs:23:5
1117
|

0 commit comments

Comments
 (0)