Skip to content

add track_caller attribute to map_err and ok_or/_else - #142093

Open
yaahc wants to merge 1 commit into
rust-lang:mainfrom
yaahc:track-map-error
Open

add track_caller attribute to map_err and ok_or/_else#142093
yaahc wants to merge 1 commit into
rust-lang:mainfrom
yaahc:track-map-error

Conversation

@yaahc

@yaahc yaahc commented Jun 5, 2025

Copy link
Copy Markdown
Member

Motivation unicode-org/icu4x#4048

This PR resolves a common edge case where users attempting to manually track std::panic::Locations of their callers in their Error types end up with irrelevant locations inside of std.

The main concern with this approach that I'm aware of is that #[track_caller] increases the stack sizes of the functions it is applied to, though I think this is a non-issue since we already have #[track_caller] on Result's FromResidual impl which is used far more frequently than any of these APIs when converting errors between different types. This change brings these functions in line with that impl.

example demonstrating the issue: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8205bcd02b380d9fd02b1f1153ac9c4e

@rustbot

rustbot commented Jun 5, 2025

Copy link
Copy Markdown
Collaborator

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 5, 2025
@yaahc

yaahc commented Jun 5, 2025

Copy link
Copy Markdown
Member Author

Relevant previous discussion: #91752

@tgross35

Copy link
Copy Markdown
Member

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

βŒ› Trying commit 0eaeb6c with merge b52d9f8…

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jun 18, 2025
add track_caller attribute to map_err and ok_or/_else

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->
Motivation unicode-org/icu4x#4048

This PR resolves a common edge case where users attempting to manually track `std::panic::Location`s of their callers in their `Error` types end up with irrelevant locations inside of `std`.

The main concern with this approach that I'm aware of is that `#[track_caller]` increases the stack sizes of the functions it is applied to, though I think this is a non-issue since we already have `#[track_caller]` on `Result`'s `FromResidual` impl which is used far more frequently than any of these APIs when converting errors between different types. This change brings these functions in line with that impl.

example demonstrating the issue: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8205bcd02b380d9fd02b1f1153ac9c4e
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 18, 2025
@rust-bors

rust-bors Bot commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

β˜€οΈ Try build successful (CI)
Build commit: b52d9f8 (b52d9f8ec0bb49a50d8e93315ac4e6b222cd971c, parent: 27eb2690f4d78f0f41eaa7193a06cd49d74b2eb0)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (b52d9f8): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements βœ…
(primary)
- - 0
Improvements βœ…
(secondary)
- - 0
All βŒβœ… (primary) 2.9% [2.9%, 2.9%] 1

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (primary 2.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.5% [2.5%, 2.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements βœ…
(primary)
- - 0
Improvements βœ…
(secondary)
- - 0
All βŒβœ… (primary) 2.5% [2.5%, 2.5%] 1

Binary size

Results (primary 0.1%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.1%] 19
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 11
Improvements βœ…
(primary)
- - 0
Improvements βœ…
(secondary)
-0.0% [-0.0%, -0.0%] 1
All βŒβœ… (primary) 0.1% [0.0%, 0.1%] 19

Bootstrap: 693.336s -> 692.254s (-0.16%)
Artifact size: 372.12 MiB -> 372.09 MiB (-0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 18, 2025
@tgross35

Copy link
Copy Markdown
Member

The only regression is for an incremental build of syn. Cc @dtolnay if you have any thoughts here (I seem to remember you using a match workaround somewhere to avoid #[track_caller] expense?) but I think this is probably fine to merge given the effect isn't all that bad and won't be visible for any consumers of syn.

#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ok_or<E>(self, err: E) -> Result<T, E> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: What's the reason to add track_caller on this function? ok_or does not panic nor call any user code that would track the caller. Or do you expect the Drop from E to track the caller? but that seems far fetched. Am I missing something or is this an oversight? (map_or_else seems like a better candidate)

@yaahc yaahc Jun 30, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, that's an oversight. The only one that matters is ok_or_else due to it taking a closure. Ill also look at adding it to map_or_else

@tgross35 tgross35 assigned tgross35 and unassigned thomcc Jul 1, 2025
@tgross35

tgross35 commented Jul 4, 2025

Copy link
Copy Markdown
Member

@rustbot author for those adjustments

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2025
@tgross35

tgross35 commented Sep 6, 2025

Copy link
Copy Markdown
Member

Been a few months, gentle nudge @yaahc if you're able to do the updates here.

@freitagfelipe

Copy link
Copy Markdown

Gentle nudge @yaahc, would really like to see this merged.

@tiberiusferreira

Copy link
Copy Markdown

I hit this today, would love to see this merged.

Thank you for all the work here. ❀️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants