Skip to content

Commit b52d9f8

Browse files
Auto merge of #142093 - yaahc:track-map-error, r=<try>
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
2 parents 27eb269 + 0eaeb6c commit b52d9f8

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

library/core/src/option.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ impl<T> Option<T> {
13051305
/// assert_eq!(x.ok_or(0), Err(0));
13061306
/// ```
13071307
#[inline]
1308+
#[track_caller]
13081309
#[stable(feature = "rust1", since = "1.0.0")]
13091310
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
13101311
match self {
@@ -1330,6 +1331,7 @@ impl<T> Option<T> {
13301331
/// assert_eq!(x.ok_or_else(|| 0), Err(0));
13311332
/// ```
13321333
#[inline]
1334+
#[track_caller]
13331335
#[stable(feature = "rust1", since = "1.0.0")]
13341336
pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>
13351337
where

library/core/src/result.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ impl<T, E> Result<T, E> {
907907
/// assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
908908
/// ```
909909
#[inline]
910+
#[track_caller]
910911
#[stable(feature = "rust1", since = "1.0.0")]
911912
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
912913
match self {

0 commit comments

Comments
 (0)