Skip to content

Commit cde2274

Browse files
authored
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 6085739 + da8a8c9 commit cde2274

13 files changed

+137
-5
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
@@ -825,14 +825,17 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
825825
fn visit_constant(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
826826
let const_ = self.monomorphize(constant.const_);
827827
let param_env = ty::ParamEnv::reveal_all();
828-
let val = match const_.eval(self.tcx, param_env, None) {
828+
let val = match const_.eval(self.tcx, param_env, Some(constant.span)) {
829829
Ok(v) => v,
830-
Err(ErrorHandled::Reported(..)) => return,
831830
Err(ErrorHandled::TooGeneric(..)) => span_bug!(
832831
self.body.source_info(location).span,
833832
"collection encountered polymorphic constant: {:?}",
834833
const_
835834
),
835+
Err(err @ ErrorHandled::Reported(..)) => {
836+
err.emit_note(self.tcx);
837+
return;
838+
}
836839
};
837840
collect_const_value(self.tcx, val, self.output);
838841
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/const-eval/unused-broken-const-late.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const VOID: () = 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/unused-broken-const-late.rs:15:17
11+
|
12+
LL | let _ = PrintName::<T>::VOID;
13+
| ^^^^^^^^^^^^^^^^^^^^
14+
915
error: aborting due to 1 previous error
1016

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

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`.

0 commit comments

Comments
 (0)