Rollup merge of #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](da8a8c9223/compiler/rustc_monomorphize/src/collector.rs (L472-L475)
).
This commit is contained in:
commit
f4afbe1389
21 changed files with 192 additions and 7 deletions
|
@ -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, &[]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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(..)) => {
|
||||
|
|
|
@ -4,6 +4,12 @@ error[E0080]: evaluation of `<u32 as ZeroSized>::I_AM_ZERO_SIZED` failed
|
|||
LL | const I_AM_ZERO_SIZED: () = [()][std::mem::size_of::<Self>()];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 <u32 as ZeroSized>::requires_zero_size`
|
||||
--> $DIR/assoc_const_generic_impl.rs:18:5
|
||||
|
|
||||
|
|
|
@ -4,6 +4,12 @@ error[E0080]: evaluation of `PrintName::<()>::VOID` failed
|
|||
LL | const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][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::<T>::VOID;
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn f::<()>`
|
||||
--> $DIR/index-out-of-bounds-never-type.rs:20:5
|
||||
|
|
||||
|
|
|
@ -10,6 +10,52 @@ note: erroneous constant encountered
|
|||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:5
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:5
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:5
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:5
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:6
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:6
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::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`.
|
||||
|
|
|
@ -10,6 +10,20 @@ note: erroneous constant encountered
|
|||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:5
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::BAR
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814-2.rs:20:6
|
||||
|
|
||||
LL | &<A<T> as Foo<T>>::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
|
||||
|
|
||||
|
|
|
@ -26,6 +26,20 @@ LL | &Sum::<U8, U8>::MAX
|
|||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814.rs:21:5
|
||||
|
|
||||
LL | &Sum::<U8, U8>::MAX
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/issue-50814.rs:21:6
|
||||
|
|
||||
LL | &Sum::<U8, U8>::MAX
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: the above error was encountered while instantiating `fn foo::<i32>`
|
||||
--> $DIR/issue-50814.rs:26:5
|
||||
|
|
||||
|
|
|
@ -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
|
||||
|
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn called::<i32>`
|
||||
--> $DIR/collect-in-called-fn.rs:23:5
|
||||
|
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn called::<i32>`
|
||||
--> $DIR/collect-in-called-fn.rs:23:5
|
||||
|
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn <Fail<i32> as std::ops::Drop>::drop`
|
||||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn not_called::<i32>`
|
||||
--> $DIR/collect-in-dead-fn.rs:29:9
|
||||
|
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn <Fail<i32> as std::ops::Drop>::drop`
|
||||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
||||
|
|
|
@ -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::<T>::C;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn <std::vec::Vec<i32> as MyTrait>::not_called`
|
||||
--> $DIR/collect-in-dead-vtable.rs:35:40
|
||||
|
|
||||
|
|
|
@ -12,6 +12,9 @@ fn assert_zst<T>() {
|
|||
//~| NOTE: the evaluated program panicked
|
||||
}
|
||||
F::<T>::V;
|
||||
//~^NOTE: erroneous constant
|
||||
//~|NOTE: erroneous constant
|
||||
//~|NOTE: duplicate
|
||||
}
|
||||
|
||||
fn foo<U>() {
|
||||
|
|
|
@ -6,8 +6,14 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 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::<T>::V;
|
||||
| ^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn assert_zst::<u32>`
|
||||
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
|
||||
--> $DIR/post_monomorphization_error_backtrace.rs:21:5
|
||||
|
|
||||
LL | assert_zst::<U>()
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
@ -20,8 +26,16 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 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::<T>::V;
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
note: the above error was encountered while instantiating `fn assert_zst::<i32>`
|
||||
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
|
||||
--> $DIR/post_monomorphization_error_backtrace.rs:21:5
|
||||
|
|
||||
LL | assert_zst::<U>()
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
|
@ -6,6 +6,12 @@ LL | const { assert!(std::mem::size_of::<T>() == 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::<T>() == 0); }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
note: the above error was encountered while instantiating `fn foo::<i32>`
|
||||
--> $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
|
||||
|
|
||||
|
|
|
@ -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`.
|
||||
|
|
|
@ -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
|
||||
|
|
||||
|
|
Loading…
Add table
Reference in a new issue