From 48f2f0d725e6d484af24e4666992cc66b6a31ebd Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 14 Mar 2024 08:09:21 +0100 Subject: [PATCH] preserve span when evaluating mir::ConstOperand --- .../src/dataflow_const_prop.rs | 4 +- .../rustc_mir_transform/src/jump_threading.rs | 3 +- compiler/rustc_monomorphize/src/collector.rs | 7 ++- compiler/rustc_smir/src/rustc_smir/builder.rs | 2 +- .../ui/consts/assoc_const_generic_impl.stderr | 6 +++ .../index-out-of-bounds-never-type.stderr | 6 +++ .../const-eval/issue-50814-2.mir-opt.stderr | 46 +++++++++++++++++++ .../const-eval/issue-50814-2.normal.stderr | 14 ++++++ tests/ui/consts/const-eval/issue-50814.stderr | 14 ++++++ tests/ui/consts/const-eval/issue-85155.stderr | 8 ++++ .../collect-in-called-fn.noopt.stderr | 6 +++ .../collect-in-called-fn.opt.stderr | 6 +++ .../collect-in-dead-drop.noopt.stderr | 6 +++ .../collect-in-dead-fn.noopt.stderr | 6 +++ .../collect-in-dead-move.noopt.stderr | 6 +++ .../collect-in-dead-vtable.noopt.stderr | 6 +++ .../post_monomorphization_error_backtrace.rs | 3 ++ ...st_monomorphization_error_backtrace.stderr | 18 +++++++- .../const-expr-generic-err.stderr | 20 ++++++++ tests/ui/inline-const/required-const.stderr | 6 +++ .../ui/simd/const-err-trumps-simd-err.stderr | 6 +++ 21 files changed, 192 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index c3e932fe187..f456196b282 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -393,7 +393,9 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> { } } Operand::Constant(box constant) => { - if let Ok(constant) = self.ecx.eval_mir_constant(&constant.const_, None, None) { + if let Ok(constant) = + self.ecx.eval_mir_constant(&constant.const_, Some(constant.span), None) + { self.assign_constant(state, place, constant, &[]); } } diff --git a/compiler/rustc_mir_transform/src/jump_threading.rs b/compiler/rustc_mir_transform/src/jump_threading.rs index ad8f21ffbda..6629face940 100644 --- a/compiler/rustc_mir_transform/src/jump_threading.rs +++ b/compiler/rustc_mir_transform/src/jump_threading.rs @@ -416,7 +416,8 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> { match rhs { // If we expect `lhs ?= A`, we have an opportunity if we assume `constant == A`. Operand::Constant(constant) => { - let constant = self.ecx.eval_mir_constant(&constant.const_, None, None).ok()?; + let constant = + self.ecx.eval_mir_constant(&constant.const_, Some(constant.span), None).ok()?; self.process_constant(bb, lhs, constant, state); } // Transfer the conditions on the copied rhs. diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 2465f9fbfa8..cd9eb4916ce 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -828,14 +828,17 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> { // a codegen-time error). rustc stops after collection if there was an error, so this // ensures codegen never has to worry about failing consts. // (codegen relies on this and ICEs will happen if this is violated.) - let val = match const_.eval(self.tcx, param_env, None) { + let val = match const_.eval(self.tcx, param_env, Some(constant.span)) { Ok(v) => v, - Err(ErrorHandled::Reported(..)) => return, Err(ErrorHandled::TooGeneric(..)) => span_bug!( self.body.source_info(location).span, "collection encountered polymorphic constant: {:?}", const_ ), + Err(err @ ErrorHandled::Reported(..)) => { + err.emit_note(self.tcx); + return; + } }; collect_const_value(self.tcx, val, self.output); MirVisitor::visit_ty(self, const_.ty(), TyContext::Location(location)); diff --git a/compiler/rustc_smir/src/rustc_smir/builder.rs b/compiler/rustc_smir/src/rustc_smir/builder.rs index 039bdec4c78..a13262cdcc4 100644 --- a/compiler/rustc_smir/src/rustc_smir/builder.rs +++ b/compiler/rustc_smir/src/rustc_smir/builder.rs @@ -56,7 +56,7 @@ impl<'tcx> MutVisitor<'tcx> for BodyBuilder<'tcx> { fn visit_constant(&mut self, constant: &mut mir::ConstOperand<'tcx>, location: mir::Location) { let const_ = self.monomorphize(constant.const_); - let val = match const_.eval(self.tcx, ty::ParamEnv::reveal_all(), None) { + let val = match const_.eval(self.tcx, ty::ParamEnv::reveal_all(), Some(constant.span)) { Ok(v) => v, Err(mir::interpret::ErrorHandled::Reported(..)) => return, Err(mir::interpret::ErrorHandled::TooGeneric(..)) => { diff --git a/tests/ui/consts/assoc_const_generic_impl.stderr b/tests/ui/consts/assoc_const_generic_impl.stderr index d826972ce9f..45219508396 100644 --- a/tests/ui/consts/assoc_const_generic_impl.stderr +++ b/tests/ui/consts/assoc_const_generic_impl.stderr @@ -4,6 +4,12 @@ error[E0080]: evaluation of `::I_AM_ZERO_SIZED` failed LL | const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::()]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 4 +note: erroneous constant encountered + --> $DIR/assoc_const_generic_impl.rs:11:9 + | +LL | Self::I_AM_ZERO_SIZED; + | ^^^^^^^^^^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn ::requires_zero_size` --> $DIR/assoc_const_generic_impl.rs:18:5 | diff --git a/tests/ui/consts/const-eval/index-out-of-bounds-never-type.stderr b/tests/ui/consts/const-eval/index-out-of-bounds-never-type.stderr index 4e7ef52f674..7facb2d1a5c 100644 --- a/tests/ui/consts/const-eval/index-out-of-bounds-never-type.stderr +++ b/tests/ui/consts/const-eval/index-out-of-bounds-never-type.stderr @@ -4,6 +4,12 @@ error[E0080]: evaluation of `PrintName::<()>::VOID` failed LL | const VOID: ! = { let x = 0 * std::mem::size_of::(); [][x] }; | ^^^^^ index out of bounds: the length is 0 but the index is 0 +note: erroneous constant encountered + --> $DIR/index-out-of-bounds-never-type.rs:16:13 + | +LL | let _ = PrintName::::VOID; + | ^^^^^^^^^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn f::<()>` --> $DIR/index-out-of-bounds-never-type.rs:20:5 | diff --git a/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr b/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr index 7e764ca7239..2de68d3fee9 100644 --- a/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr +++ b/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr @@ -10,6 +10,52 @@ note: erroneous constant encountered LL | & as Foo>::BAR | ^^^^^^^^^^^^^^^^^^^^^ +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:5 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:5 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:5 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:5 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:6 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:6 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/const-eval/issue-50814-2.normal.stderr b/tests/ui/consts/const-eval/issue-50814-2.normal.stderr index f552c8fde5b..4a7dfb19304 100644 --- a/tests/ui/consts/const-eval/issue-50814-2.normal.stderr +++ b/tests/ui/consts/const-eval/issue-50814-2.normal.stderr @@ -10,6 +10,20 @@ note: erroneous constant encountered LL | & as Foo>::BAR | ^^^^^^^^^^^^^^^^^^^^^ +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:5 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/issue-50814-2.rs:20:6 + | +LL | & as Foo>::BAR + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + note: the above error was encountered while instantiating `fn foo::<()>` --> $DIR/issue-50814-2.rs:32:22 | diff --git a/tests/ui/consts/const-eval/issue-50814.stderr b/tests/ui/consts/const-eval/issue-50814.stderr index 8d018161401..fe0e25b820f 100644 --- a/tests/ui/consts/const-eval/issue-50814.stderr +++ b/tests/ui/consts/const-eval/issue-50814.stderr @@ -26,6 +26,20 @@ LL | &Sum::::MAX | = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +note: erroneous constant encountered + --> $DIR/issue-50814.rs:21:5 + | +LL | &Sum::::MAX + | ^^^^^^^^^^^^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/issue-50814.rs:21:6 + | +LL | &Sum::::MAX + | ^^^^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + note: the above error was encountered while instantiating `fn foo::` --> $DIR/issue-50814.rs:26:5 | diff --git a/tests/ui/consts/const-eval/issue-85155.stderr b/tests/ui/consts/const-eval/issue-85155.stderr index a88e959a8a6..99836a3fac6 100644 --- a/tests/ui/consts/const-eval/issue-85155.stderr +++ b/tests/ui/consts/const-eval/issue-85155.stderr @@ -4,6 +4,14 @@ error[E0080]: evaluation of `post_monomorphization_error::ValidateConstImm::<2, LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero +note: erroneous constant encountered + --> $DIR/auxiliary/post_monomorphization_error.rs:19:5 + | +LL | static_assert_imm1!(IMM1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this note originates in the macro `static_assert_imm1` (in Nightly builds, run with -Z macro-backtrace for more info) + note: the above error was encountered while instantiating `fn post_monomorphization_error::stdarch_intrinsic::<2>` --> $DIR/issue-85155.rs:19:5 | diff --git a/tests/ui/consts/required-consts/collect-in-called-fn.noopt.stderr b/tests/ui/consts/required-consts/collect-in-called-fn.noopt.stderr index c7ff1328917..14a4cb0217f 100644 --- a/tests/ui/consts/required-consts/collect-in-called-fn.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-called-fn.noopt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-called-fn.rs:18:17 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn called::` --> $DIR/collect-in-called-fn.rs:23:5 | diff --git a/tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr b/tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr index c7ff1328917..14a4cb0217f 100644 --- a/tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr +++ b/tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-called-fn.rs:18:17 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn called::` --> $DIR/collect-in-called-fn.rs:23:5 | diff --git a/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr index b7010e78763..0bf231d09f1 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-dead-drop.rs:19:17 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn as std::ops::Drop>::drop` --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL diff --git a/tests/ui/consts/required-consts/collect-in-dead-fn.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-fn.noopt.stderr index 2162c35c837..8bb99efe8e4 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-fn.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-fn.noopt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-dead-fn.rs:22:17 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn not_called::` --> $DIR/collect-in-dead-fn.rs:29:9 | diff --git a/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr index 8c853127e04..5b1df78b232 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-dead-move.rs:19:17 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn as std::ops::Drop>::drop` --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL diff --git a/tests/ui/consts/required-consts/collect-in-dead-vtable.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-vtable.noopt.stderr index 6fd82777bd3..56b6989b441 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-vtable.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-vtable.noopt.stderr @@ -6,6 +6,12 @@ LL | const C: () = panic!(); | = 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) +note: erroneous constant encountered + --> $DIR/collect-in-dead-vtable.rs:26:21 + | +LL | let _ = Fail::::C; + | ^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn as MyTrait>::not_called` --> $DIR/collect-in-dead-vtable.rs:35:40 | diff --git a/tests/ui/generics/post_monomorphization_error_backtrace.rs b/tests/ui/generics/post_monomorphization_error_backtrace.rs index 56155ae2bd5..2c18f2b233a 100644 --- a/tests/ui/generics/post_monomorphization_error_backtrace.rs +++ b/tests/ui/generics/post_monomorphization_error_backtrace.rs @@ -12,6 +12,9 @@ fn assert_zst() { //~| NOTE: the evaluated program panicked } F::::V; + //~^NOTE: erroneous constant + //~|NOTE: erroneous constant + //~|NOTE: duplicate } fn foo() { diff --git a/tests/ui/generics/post_monomorphization_error_backtrace.stderr b/tests/ui/generics/post_monomorphization_error_backtrace.stderr index 0d707d83d26..8a57979bca7 100644 --- a/tests/ui/generics/post_monomorphization_error_backtrace.stderr +++ b/tests/ui/generics/post_monomorphization_error_backtrace.stderr @@ -6,8 +6,14 @@ LL | const V: () = assert!(std::mem::size_of::() == 0); | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) +note: erroneous constant encountered + --> $DIR/post_monomorphization_error_backtrace.rs:14:5 + | +LL | F::::V; + | ^^^^^^^^^ + note: the above error was encountered while instantiating `fn assert_zst::` - --> $DIR/post_monomorphization_error_backtrace.rs:18:5 + --> $DIR/post_monomorphization_error_backtrace.rs:21:5 | LL | assert_zst::() | ^^^^^^^^^^^^^^^^^ @@ -20,8 +26,16 @@ LL | const V: () = assert!(std::mem::size_of::() == 0); | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) +note: erroneous constant encountered + --> $DIR/post_monomorphization_error_backtrace.rs:14:5 + | +LL | F::::V; + | ^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + note: the above error was encountered while instantiating `fn assert_zst::` - --> $DIR/post_monomorphization_error_backtrace.rs:18:5 + --> $DIR/post_monomorphization_error_backtrace.rs:21:5 | LL | assert_zst::() | ^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/inline-const/const-expr-generic-err.stderr b/tests/ui/inline-const/const-expr-generic-err.stderr index fc0b6cc4451..7331c7f18e9 100644 --- a/tests/ui/inline-const/const-expr-generic-err.stderr +++ b/tests/ui/inline-const/const-expr-generic-err.stderr @@ -6,6 +6,12 @@ LL | const { assert!(std::mem::size_of::() == 0); } | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) +note: erroneous constant encountered + --> $DIR/const-expr-generic-err.rs:5:5 + | +LL | const { assert!(std::mem::size_of::() == 0); } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn foo::` --> $DIR/const-expr-generic-err.rs:13:5 | @@ -18,6 +24,20 @@ error[E0080]: evaluation of `bar::<0>::{constant#0}` failed LL | const { N - 1 } | ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow +note: erroneous constant encountered + --> $DIR/const-expr-generic-err.rs:9:5 + | +LL | const { N - 1 } + | ^^^^^^^^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/const-expr-generic-err.rs:9:5 + | +LL | const { N - 1 } + | ^^^^^^^^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + note: the above error was encountered while instantiating `fn bar::<0>` --> $DIR/const-expr-generic-err.rs:14:5 | diff --git a/tests/ui/inline-const/required-const.stderr b/tests/ui/inline-const/required-const.stderr index cd86020184d..2a13d18547c 100644 --- a/tests/ui/inline-const/required-const.stderr +++ b/tests/ui/inline-const/required-const.stderr @@ -6,6 +6,12 @@ LL | const { panic!() } | = 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) +note: erroneous constant encountered + --> $DIR/required-const.rs:7:9 + | +LL | const { panic!() } + | ^^^^^^^^^^^^^^^^^^ + error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/simd/const-err-trumps-simd-err.stderr b/tests/ui/simd/const-err-trumps-simd-err.stderr index 6e6aba8b6f1..1e46667cf4e 100644 --- a/tests/ui/simd/const-err-trumps-simd-err.stderr +++ b/tests/ui/simd/const-err-trumps-simd-err.stderr @@ -6,6 +6,12 @@ LL | const { assert!(LANE < 4); } // the error should be here... | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) +note: erroneous constant encountered + --> $DIR/const-err-trumps-simd-err.rs:16:5 + | +LL | const { assert!(LANE < 4); } // the error should be here... + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + note: the above error was encountered while instantiating `fn get_elem::<4>` --> $DIR/const-err-trumps-simd-err.rs:23:5 |