Description
In general, if a library function panics due to invalid input, its best if the error message point to the function call and explain why the input was invalid. An overflow in a core library function with the #[rustc_inherit_overflow_checks]
attribute is probably due to invalid input.
Assuming a function could only panic due to incorrect input, the only potential drawback to #[track_caller]
I can see is the panic message not adequately explaining why the input was invalid. Until we get #111466, we can only pass the text of the overflow error. For div_euclid
*, this is perfect. For next_multiple_of
, this is sufficient. For advance_by
, I'm not sure.
Adding the attribute is trivial. I'm only making this issue to ask if/what functions I should add the attribute to and if I need to.
* Edit: The division function might be a confusing example since it doesn't care about #[rustc_inherit_overflow_checks]
. However, it should have #[track_caller]
like the others.
Activity
dtolnay commentedon Apr 5, 2024
Fixed by #119726.
zheland commentedon Dec 2, 2024
It doesn't seem like #119726 fixes all such missed
#[track_caller]
.The #119726 doesn't add
#[track_caller]
at least tonext_multiple_of
andadvance_by
that is mentioned in this issue.There are panicking functions with
#[rustc_inherit_overflow_checks]
that miss#[track_caller]
:pow
,next_multiple_of
,next_power_of_two
.pow
,next_multiple_of
,abs
.neg
.shl
,shr
,shl_assign
,shr_assign
.Functions for which
#[rustc_inherit_overflow_checks]
seems redundant:is_multiple_of
.position
.Additionally
#[rustc_inherit_overflow_checks]
without#[track_caller]
is often used incount
-like functions iterators functions in:Possibly all functions that require
#[rustc_inherit_overflow_checks]
should have#[track_caller]
and there are still some functions with redundant#[rustc_inherit_overflow_checks]
.Example:
With:
Results in:
Expected something like:
Version (last nightly):
zheland commentedon Jan 11, 2025
@dtolnay, could you please review my previous comment. I think that issue is not fixed and should be reopened.
And since it looks like a simple fix and I've already started getting into it, I can double-check the details and try to prepare a PR for the fix.