Closed
Description
I tried this code:
fn main() {
my_function();
}
#[track_caller]
fn my_function() {
my_result().unwrap_or_else(|e| panic!("{}", e));
}
fn my_result() -> Result<(), String> {
Err("this is an error".to_string())
}
I expected the panic to be reported at line 2
Instead, it still was reported at line 7.
I also tried.
fn main() {
my_function();
}
#[track_caller]
fn my_function() {
#[track_caller]
fn test(e: String) {
panic!("{}", e);
}
my_result().unwrap_or_else(test);
}
fn my_result() -> Result<(), String> {
Err("this is an error".to_string())
}
but this reported as an error in core
thread 'main' panicked at 'this is an error', /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/core/src/ops/function.rs:250:5
I was able to work around the issue with code like this:
fn main() {
my_function();
}
#[track_caller]
fn my_function() {
if let Err(e) = my_result() {
panic!("{}", e);
}
}
fn my_result() -> Result<(), String> {
Err("this is an error".to_string())
}
Meta
rustc --version --verbose
:
rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-pc-windows-msvc
release: 1.72.0
LLVM version: 16.0.5