Description
(Note: this was originally posted as a comment on #62429 - also cc #35310 but not sure how relevant)
In #62429 (comment) @scottmcm said:
Might be worth double-checking there's a test for overflow checks here? I'm never confident in what
rustc_inherit_overflow_checks
does. (And I hear that one can useAdd::add(count, 1)
instead of using the attribute.)
While adding more tests is better, I don't agree with removing the attribute. If anything the attribute should be better documented, but relying on more implicit mechanisms that internally use the attribute obscures the fact that something unusual is happening.
(i.e. that some of the behavior isn't decided by the crate being compiled, but by a downstream using crate)
The reason Add::add
works at all is because the attribute is used in the impl
s` for the integer types, e.g.:
rust/library/core/src/ops/arith.rs
Lines 92 to 107 in 186f7ae
There's nothing magical about it being a trait call, you're just calling another function with #[rustc_inherit_overflow_checks]
on it, instead of using #[rustc_inherit_overflow_checks]
yourself, and relying on generic/#[inline]
instantiation to get the behavior.
The only reason the trait impls have the attribute is because of things generic over those traits, not to call directly.
EDIT: see also documentation issue by @m-ou-se at rust-lang/std-dev-guide#13