Skip to content

Commit

Permalink
Revert order of constant evaluation errors
Browse files Browse the repository at this point in the history
Point at theh code the user wrote first and std functions last.

```
error[E0080]: evaluation of constant value failed
  --> $DIR/const-errs-dont-conflict-103369.rs:5:25
   |
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
   |                         ^^^^^^^^ Some error occurred
   |
note: called from `my_fn`
  --> $DIR/const-errs-dont-conflict-103369.rs:10:5
   |
LL |     panic!("Some error occurred");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = 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)
```
instead of
```
error[E0080]: evaluation of constant value failed
  --> $DIR/const-errs-dont-conflict-103369.rs:10:5
   |
LL |     panic!("Some error occurred");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Some error occurred
   |
note: called from `<() as ConstGenericTrait<{my_fn(1)}>>::{constant#0}`
  --> $DIR/const-errs-dont-conflict-103369.rs:5:25
   |
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
   |                         ^^^^^^^^
   = 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)
```
  • Loading branch information
estebank committed Feb 4, 2025
1 parent 474462f commit 64a7ed3
Show file tree
Hide file tree
Showing 71 changed files with 429 additions and 512 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn get_span_and_frames<'tcx>(
let mut stacktrace = Frame::generate_stacktrace_from_stack(stack);
// Filter out `requires_caller_location` frames.
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*tcx));
let span = stacktrace.first().map(|f| f.span).unwrap_or(tcx.span);
let span = stacktrace.last().map(|f| f.span).unwrap_or(tcx.span);

let mut frames = Vec::new();

Expand Down Expand Up @@ -111,6 +111,10 @@ pub fn get_span_and_frames<'tcx>(
add_frame(frame);
}
}
frames.reverse();
if frames.len() > 0 {
frames.remove(0);
}

(span, frames)
}
Expand Down Expand Up @@ -147,11 +151,8 @@ where
}
// Report remaining errors.
_ => {
let (our_span, mut frames) = get_span_and_frames();
let (our_span, frames) = get_span_and_frames();
let span = span.substitute_dummy(our_span);
if frames.len() > 0 {
frames.remove(0);
}
let err = mk(span, frames);
let mut err = tcx.dcx().create_err(err);
// We allow invalid programs in infallible promoteds since invalid layouts can occur
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/borrowck/issue-81899.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Regression test for #81899.
// The `panic!()` below is important to trigger the fixed ICE.

const _CONST: &[u8] = &f(&[], |_| {});
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR evaluation of constant value failed
//~^ constant

const fn f<F>(_: &[u8], _: F) -> &[u8]
where
F: FnMut(&u8),
{
panic!() //~ ERROR evaluation of constant value failed
//~^ panic
panic!() //~ panic
}

fn main() {}
14 changes: 7 additions & 7 deletions tests/ui/borrowck/issue-81899.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-81899.rs:11:5
|
LL | panic!()
| ^^^^^^^^ explicit panic
|
note: called from `_CONST`
--> $DIR/issue-81899.rs:4:24
|
LL | const _CONST: &[u8] = &f(&[], |_| {});
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ explicit panic
|
note: called from `f::<{closure@$DIR/issue-81899.rs:4:31: 4:34}>`
--> $DIR/issue-81899.rs:11:5
|
LL | 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
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/borrowck/issue-88434-minimal-example.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Regression test related to issue 88434

const _CONST: &() = &f(&|_| {});
const _CONST: &() = &f(&|_| {}); //~ ERROR evaluation of constant value failed
//~^ constant

const fn f<F>(_: &F)
where
F: FnMut(&u8),
{
panic!() //~ ERROR evaluation of constant value failed
//~^ panic
panic!() //~ panic
}

fn main() { }
14 changes: 7 additions & 7 deletions tests/ui/borrowck/issue-88434-minimal-example.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-88434-minimal-example.rs:10:5
|
LL | panic!()
| ^^^^^^^^ explicit panic
|
note: called from `_CONST`
--> $DIR/issue-88434-minimal-example.rs:3:22
|
LL | const _CONST: &() = &f(&|_| {});
| ^^^^^^^^^^
| ^^^^^^^^^^ explicit panic
|
note: called from `f::<{closure@$DIR/issue-88434-minimal-example.rs:3:25: 3:28}>`
--> $DIR/issue-88434-minimal-example.rs:10:5
|
LL | 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
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/borrowck/issue-88434-removal-index-should-be-less.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Regression test for issue 88434

const _CONST: &[u8] = &f(&[], |_| {});
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR evaluation of constant value failed
//~^ constant

const fn f<F>(_: &[u8], _: F) -> &[u8]
where
F: FnMut(&u8),
{
panic!() //~ ERROR evaluation of constant value failed
//~^ panic
panic!() //~ panic
}

fn main() { }
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
|
LL | panic!()
| ^^^^^^^^ explicit panic
|
note: called from `_CONST`
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
|
LL | const _CONST: &[u8] = &f(&[], |_| {});
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ explicit panic
|
note: called from `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>`
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
|
LL | 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
Expand Down
7 changes: 3 additions & 4 deletions tests/ui/coherence/const-errs-dont-conflict-103369.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

pub trait ConstGenericTrait<const N: u32> {}

impl ConstGenericTrait<{my_fn(1)}> for () {}
impl ConstGenericTrait<{my_fn(1)}> for () {} //~ ERROR E0080

impl ConstGenericTrait<{my_fn(2)}> for () {}
impl ConstGenericTrait<{my_fn(2)}> for () {} //~ ERROR E0080

const fn my_fn(v: u32) -> u32 {
panic!("Some error occurred"); //~ ERROR E0080
//~| ERROR E0080
panic!("Some error occurred");
}

fn main() {}
28 changes: 14 additions & 14 deletions tests/ui/coherence/const-errs-dont-conflict-103369.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
error[E0080]: evaluation of constant value failed
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
|
LL | panic!("Some error occurred");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Some error occurred
|
note: called from `<() as ConstGenericTrait<{my_fn(1)}>>::{constant#0}`
--> $DIR/const-errs-dont-conflict-103369.rs:5:25
|
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
| ^^^^^^^^
= 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)

error[E0080]: evaluation of constant value failed
| ^^^^^^^^ Some error occurred
|
note: called from `my_fn`
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
|
LL | panic!("Some error occurred");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Some error occurred
|
note: called from `<() as ConstGenericTrait<{my_fn(2)}>>::{constant#0}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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)

error[E0080]: evaluation of constant value failed
--> $DIR/const-errs-dont-conflict-103369.rs:7:25
|
LL | impl ConstGenericTrait<{my_fn(2)}> for () {}
| ^^^^^^^^
| ^^^^^^^^ Some error occurred
|
note: called from `my_fn`
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
|
LL | panic!("Some error occurred");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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)

error: aborting due to 2 previous errors
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/const-generics/issues/issue-100313.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ impl<const B: &'static bool> T<B> {
const fn set_false(&self) {
unsafe {
*(B as *const bool as *mut bool) = false;
//~^ ERROR evaluation of constant value failed [E0080]
}
}
}

const _: () = {
let x = T::<{ &true }>;
x.set_false();
x.set_false(); //~ ERROR evaluation of constant value failed [E0080]
};

fn main() {}
14 changes: 7 additions & 7 deletions tests/ui/const-generics/issues/issue-100313.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-100313.rs:9:13
--> $DIR/issue-100313.rs:16:5
|
LL | *(B as *const bool as *mut bool) = false;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ writing to ALLOC0 which is read-only
LL | x.set_false();
| ^^^^^^^^^^^^^ writing to ALLOC0 which is read-only
|
note: called from `_`
--> $DIR/issue-100313.rs:17:5
note: called from `T::<&true>::set_false`
--> $DIR/issue-100313.rs:9:13
|
LL | x.set_false();
| ^^^^^^^^^^^^^
LL | *(B as *const bool as *mut bool) = false;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/const-ptr/forbidden_slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ pub static S8: &[u64] = unsafe {
pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
//~^ ERROR it is undefined behavior to use this value
pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; // errors inside libcore
//~^ ERROR could not evaluate static initializer
pub static R2: &[u32] = unsafe {
let ptr = &D0 as *const u32;
from_ptr_range(ptr..ptr.add(2)) // errors inside libcore
//~^ ERROR could not evaluate static initializer
};
pub static R4: &[u8] = unsafe {
//~^ ERROR: it is undefined behavior to use this value
Expand All @@ -74,13 +76,16 @@ pub static R7: &[u16] = unsafe {
};
pub static R8: &[u64] = unsafe {
let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::<u64>();
from_ptr_range(ptr..ptr.add(1)) //~ called from `R8`
from_ptr_range(ptr..ptr.add(1))
//~^ ERROR could not evaluate static initializer
};

// This is sneaky: &D0 and &D0 point to different objects
// (even if at runtime they have the same address)
pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) };
//~^ ERROR could not evaluate static initializer
pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
//~^ ERROR could not evaluate static initializer

const D0: u32 = 0x11111111; // Constant chosen for endianness-independent behavior.
const D1: MaybeUninit<&u32> = MaybeUninit::uninit();
Expand Down
Loading

0 comments on commit 64a7ed3

Please sign in to comment.