Skip to content

track_caller does not work with closures and Fn* types #115302

Closed
@hymm

Description

@hymm

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

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-track_caller`#![feature(track_caller)]`T-libsRelevant to the library 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