Skip to content

Suboptimal diagnostic if const eval error site is in a different crate #80380

Open
@oli-obk

Description

@oli-obk

We currently produce fun diagnostics like

error: any use of this value will cause an error
  --> $SRC_DIR/core/src/intrinsics.rs:LL:COL
   |
LL |     unsafe { copy_nonoverlapping(src, dst, count) }
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |              |
   |              memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4
   |              inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
   |              inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
   |              inside `ptr::mut_ptr::<impl *mut u32>::read` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
   |              inside `_MUT_READ` at $DIR/out_of_bounds_read.rs:15:37
   | 
  ::: $DIR/out_of_bounds_read.rs:15:5
   |
LL |     const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
   |     --------------------------------------------------------------------

Which I have multiple problems with (all of which are my fault 😛)

  1. the main message only makes sense when you realize this is a lint
  2. the diagnostic points to another crate's internals as the main span
  3. the local crate's span is pointing at the entire item, instead of the useful span that we have (last element in the stack trace)

I think we should adjust the diagnostic to report on the site in the currently compiling crate while still mentioning the backtrace in full, so something like

error: memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 
  --> $SRC_DIR/core/src/intrinsics.rs:LL:COL
   |
LL |     const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
   |                                                               ^^^^^^^ in function call here
   |     
LL |     unsafe { copy_nonoverlapping(src, dst, count) }
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ failure in external crate here
   |              inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
   |              inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
   |              inside `ptr::mut_ptr::<impl *mut u32>::read` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
   |              inside `_MUT_READ` at $DIR/out_of_bounds_read.rs:15:37

cc @rust-lang/wg-diagnostics @rust-lang/wg-const-eval

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions