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

Add #[track_caller] to methods which panic #1442

Merged
merged 2 commits into from
May 5, 2024
Merged

Add #[track_caller] to methods which panic #1442

merged 2 commits into from
May 5, 2024

Conversation

josephlr
Copy link
Member

Fixes #1437

This makes it easier to diagnose problems when the preconditions of these methods are not met.

I think I found most of the cases where we can panic based on bad user input. Let me know if I missed any.

We don't use #[track_caller] for the panics originating from failing to initialized thread_rng as this is more of a "global" problem than a "local" problem.

This makes it easier to diagnose problems when the preconidtions of
these methods are not met.

Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
@josephlr josephlr requested a review from dhardy April 29, 2024 22:42
Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

The reference uses std::panic::Location::caller. Is it worth doing this too for better error messages?

Comment on lines +388 to 389
#[track_caller]
fn from_entropy() -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

The potential for failure is due to getrandom, so probably this shouldn't track the caller.

@dhardy
Copy link
Member

dhardy commented Apr 30, 2024

Also see:

  • src/seq/index.rs:225, 288
  • rand_core/src/le.rs

@josephlr
Copy link
Member Author

josephlr commented Apr 30, 2024

The reference uses std::panic::Location::caller. Is it worth doing this too for better error messages?

I think that's mostly if we wanted to print out the location, or use the location for something else. If we are just panicking anyway, I think just using #[track_caller] is fine, that's what libstd does for things like .unwrap().

@dhardy
Copy link
Member

dhardy commented Apr 30, 2024

BTW, I was hoping you'd give #1424 a review — not sure if you missed the comment there?

@josephlr
Copy link
Member Author

BTW, I was hoping you'd give #1424 a review — not sure if you missed the comment there?

Whoops, I did miss that. Ill try to take a look sometime this week.

@dhardy dhardy merged commit a2375dc into master May 5, 2024
12 checks passed
josephlr added a commit that referenced this pull request May 7, 2024
This is stuff I missed in #1442

Signed-off-by: Joe Richey <joerichey@google.com>
@josephlr josephlr deleted the track_caller branch May 7, 2024 11:16
josephlr added a commit that referenced this pull request May 7, 2024
This is stuff I missed in #1442

Signed-off-by: Joe Richey <joerichey@google.com>
josephlr added a commit that referenced this pull request May 8, 2024
This is stuff I missed in #1442

Signed-off-by: Joe Richey <joerichey@google.com>
dhardy added a commit that referenced this pull request May 8, 2024
This is stuff I missed in #1442

Signed-off-by: Joe Richey <joerichey@google.com>
Co-authored-by: Diggory Hardy <git@dhardy.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotate functions with #[track_caller]
2 participants