-
Notifications
You must be signed in to change notification settings - Fork 14k
Description
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