time: add track_caller to public APIs #4791
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
When a user of tokio calls a function that panics when misused (e.g. calling
time::intervalwith 0 duration) then the user currently sees the line numberof the panic call inside tokio. It would be more informative for the user to see
the place where they called the panicking function.
It is still possible for the user to see the full stack trace by setting the
environment variable RUST_BACKLOG=1, so no useful information is
hidden.
This change is the 4th in a series towards closing #4413 (starting with #4772),
this change is for the time functions in the main
tokiocrate.Solution
Functions that may panic can be annotated with #[track_caller] so that
in the event of a panic, the function where the user called the
panicking function is shown instead of the file and line within Tokio
source.
This change adds #[track_caller] to all the non-unstable public APIs in
tokio-util where the documentation describes how the function may panic
due to incorrect context or inputs.
In cases where
#[track_caller]does not work, it has been left out. Forexample, it currently does not work on async functions, blocks, or
closures. So any call stack that passes through one of these before
reaching the actual panic is not able to show the calling site outside
of tokio as the panic location.
The public functions that could not have
#[track_caller]added forthis reason are:
time::advanceTests are included to cover each potentially panicking function. In the
following cases,
#[track_caller]had already been added, and onlytests have been added:
time::intervaltime::interval_atRefs: #4413