Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hymm opened this issue Aug 28, 2023 · 3 comments · Fixed by #116795
Closed

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

hymm opened this issue Aug 28, 2023 · 3 comments · Fixed by #116795
Assignees
Labels
A-closures Area: Closures (`|…| { … }`) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-track_caller `#![feature(track_caller)]` T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@hymm
Copy link

hymm commented Aug 28, 2023

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
@hymm hymm added the C-bug Category: This is a bug. label Aug 28, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 28, 2023
@RalfJung RalfJung changed the title Couldn't get track_caller + unwrap_or_else to report error on the correct line Couldn't get track_caller + unwrap_or_else to report error on the correct line: track_caller does not work with closures Aug 28, 2023
@RalfJung
Copy link
Member

The core problem here is that track_caller is effectively part of the ABI, so when you have a function like impl FnOnce(T) -> U, you'd have to somehow know whether it is track_caller or not -- this would have to be added to the type explicitly. Currently it is just never made track_caller. There's no easy fix here that I can see, this requires a language extension.

@RalfJung RalfJung changed the title Couldn't get track_caller + unwrap_or_else to report error on the correct line: track_caller does not work with closures track_caller does not work with closures and Fn* types Aug 28, 2023
@RalfJung
Copy link
Member

Oh looks like that language design work was already done: #87417. So on nightly, this works:

#![feature(closure_track_caller)]
fn main() {
    my_function();
}

#[track_caller]
fn my_function() {
    my_result().unwrap_or_else(#[track_caller] |e| panic!("{}", e));
}
 
fn my_result() -> Result<(), String> {
    Err("this is an error".to_string())
}

The only piece missing is adding track_caller to unwrap_or_else, which is a trivial PR.

@fmease fmease added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-closures Area: Closures (`|…| { … }`) F-track_caller `#![feature(track_caller)]` T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 29, 2023
@ravenclaw900
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 80c9588 Oct 19, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 19, 2023
Rollup merge of rust-lang#116795 - DaniPopes:track-caller-option, r=cuviper

Add `#[track_caller]` to `Option::unwrap_or_else`

Same as rust-lang#116317 but for `Option`.

Closes rust-lang#115302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-track_caller `#![feature(track_caller)]` T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants