Skip to content

add track_caller attribute to map_err and ok_or/_else #142093

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,7 @@ impl<T> Option<T> {
/// assert_eq!(x.ok_or(0), Err(0));
/// ```
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
Copy link
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)

Copy link
Member Author

@yaahc yaahc Jun 30, 2025

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

match self {
Expand All @@ -1330,6 +1331,7 @@ impl<T> Option<T> {
/// assert_eq!(x.ok_or_else(|| 0), Err(0));
/// ```
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>
where
Expand Down
1 change: 1 addition & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ impl<T, E> Result<T, E> {
/// assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
/// ```
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
match self {
Expand Down
Loading