Skip to content

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

Closed

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 31, 2025
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    A-panicArea: Panicking machinery
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    on Oct 31, 2025
  3. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 31, 2025
  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 31, 2025
  5. theemathas commented on Oct 31, 2025

    @theemathas
    Contributor

    Inside the in_sync function, changing the != to == causes the code to panic with a different value for file. It seems like the VALUE const is being evaluated twice, with two different values for FILE.

  6. pitust commented on Oct 31, 2025

    @pitust

    You can reproduce this without async:

    // crate 1
    #[inline(always)]
    pub const fn the_path() -> &'static str {
        std::panic::Location::caller().file()
    }
    #[inline(never)]
    pub fn the_path2() -> &'static str {
        const { std::panic::Location::caller().file() }
    }
    // crate 2
    fn main() {
        println!("{}", depcrate::the_path());
        println!("{}", depcrate::the_path2());
    }
  7. added
    A-path-remappingArea: path remapping, --remap-path-prefix, --remap-cwd-prefix, --remap-diagnostics-scope etc.
    and removed on Oct 31, 2025
  8. 51 remaining items

  9. added 2 commits that reference this issue on Dec 10, 2025
  10. moved this from open/unblocked to new solver everywhere in T-types unsound issueson Dec 13, 2025
  11. moved this from new solver everywhere to Completed in T-types unsound issueson Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)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-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions