Skip to content

Commit

Permalink
Rollup merge of rust-lang#122471 - RalfJung:const-eval-span, r=oli-obk
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
matthiaskrgr authored Mar 15, 2024
2 parents bd376dc + 48f2f0d commit cc125a4
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 7 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &[]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..)) => {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/consts/assoc_const_generic_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
14 changes: 14 additions & 0 deletions tests/ui/consts/const-eval/issue-50814-2.normal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/consts/const-eval/issue-85155.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/generics/post_monomorphization_error_backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>() {
Expand Down
18 changes: 16 additions & 2 deletions tests/ui/generics/post_monomorphization_error_backtrace.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
| ^^^^^^^^^^^^^^^^^
Expand All @@ -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>()
| ^^^^^^^^^^^^^^^^^
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/inline-const/const-expr-generic-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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
|
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/inline-const/required-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 0 deletions tests/ui/simd/const-err-trumps-simd-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down

0 comments on commit cc125a4

Please sign in to comment.