Skip to content

std::panic::Location::caller().file() uses absolute path when monomorphized in a different crate. #148328

@antonWetzel

Description

@antonWetzel

I want to create a const &str based on the current location, but compilation fails with an out of bounds exception.

The following is a minimal reproduction for the core problem: The &str returned by std::panic::Location::caller().file() sometimes is the full path for const evaluation with async.

pub async fn async_example() {
    const LOCATION: &std::panic::Location = std::panic::Location::caller();
    const FILE: &str = LOCATION.file();

    // (expected) evaluation panicked: dependency/src/lib.rs 
    // const VALUE: () = panic!("{}", FILE);

    // (unexpected) evaluation panicked: /Users/anton.wetzel/projects/locate_what/dependency/src/lib.rs 
    const LENGTH: usize = FILE.len();
    const VALUE: &str = in_sync::<LENGTH>(FILE);

    println!("{:?}", VALUE);
}

pub const fn in_sync<const N: usize>(file: &str) -> &str {
    if N != file.len() {
        panic!("{}", file);
    }
    // create a [u8; N] and fill it with the chars or something similar...
    file
}

I expected no panic, because N is the length of FILE.
Instead, the function is evaluated with the length of the relative file name as N and the complete file name as file.

Without the panic! Value is the relative name in the compiled binary.

If the function is not async, the problem does not occur.
If the function is not a dependency, the problem does not occur.

The complete reproduction: https://github.com/antonWetzel/locate_what

Meta

rustc --version --verbose:

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: aarch64-apple-darwin
release: 1.91.0
LLVM version: 21.1.2

rustc +nightly --version --verbose:

rustc 1.93.0-nightly (d5419f1e9 2025-10-30)
binary: rustc
commit-hash: d5419f1e97b90741d51841f800d3c697c662567d
commit-date: 2025-10-30
host: aarch64-apple-darwin
release: 1.93.0-nightly
LLVM version: 21.1.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-monomorphizationArea: MonomorphizationA-panicArea: Panicking machineryA-path-remappingArea: path remapping, --remap-path-prefix, --remap-cwd-prefix, --remap-diagnostics-scope etc.C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️I-prioritizeIssue: Indicates that prioritization has been requested for this issue.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-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