You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The feature gate for the issue is #![feature(cfg_overflow_checks)].
It allows write code that detects if compiler emits code for integer overflow checks similarly to how cfg(debug_assertions) allow to detect if debug assertions enabled.
Example:
#[cfg(overflow_checks)]#[track_caller]fncast(v:i64)->u32{
v.try_into().unwrap()}#[cfg(not(overflow_checks))]fncast(v:i64)->u32{
v as_}
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Note: this feature is observable on nightly without #![feature] via build scripts checking $CARGO_CFG_OVERFLOW_CHECKS. The env var isn't set for beta/stable, so this is fine, but imho still warrants mentioning here.
I'd love to have this stabilized. There's no iN::{add,sub}_unsigned or uN::{add,sub}_signed that inherit overflow checks. The next best thing seems to be
Note that cfg!(overflow_checks) is not quite the same as the standard library’s notion of inheriting overflow checks. The cfg answers “are overflow checks enabled for this crate” while std’s [rustc_inherit_overflow_checks] is based on the overflow checks setting in other crates that call the “inheriting” function. This distinction doesn’t matter when caller and callee are compiled with the same compiler flags, but Cargo profile overrides can change this for individual crates.
I don’t think this is a blocker for stabilization. In fact, it’s a simpler mental model than the std weirdness. But I do wonder whether prospective users are aware of this and whether it would matter to them.
Activity
Add support for `cfg(overflow_checks)`
CAD97 commentedon Apr 23, 2024
Note: this feature is observable on nightly without
#![feature]via build scripts checking$CARGO_CFG_OVERFLOW_CHECKS. The env var isn't set for beta/stable, so this is fine, but imho still warrants mentioning here.bjoernager commentedon Oct 18, 2025
Doesn't seem to have any blockers, no?
nxsaken commentedon Nov 4, 2025
I'd love to have this stabilized. There's no
iN::{add,sub}_unsignedoruN::{add,sub}_signedthat inherit overflow checks. The next best thing seems to bebut without
cfg!(overflow_checks), it's impossible to write in user code.hanna-kruppe commentedon Nov 4, 2025
Note that
cfg!(overflow_checks)is not quite the same as the standard library’s notion of inheriting overflow checks. The cfg answers “are overflow checks enabled for this crate” while std’s[rustc_inherit_overflow_checks]is based on the overflow checks setting in other crates that call the “inheriting” function. This distinction doesn’t matter when caller and callee are compiled with the same compiler flags, but Cargo profile overrides can change this for individual crates.I don’t think this is a blocker for stabilization. In fact, it’s a simpler mental model than the std weirdness. But I do wonder whether prospective users are aware of this and whether it would matter to them.
nxsaken commentedon Nov 4, 2025
@hanna-kruppe thanks! I was wondering about it. Btw, there's an open issue on the lack of documentation for this attribute: rust-lang/std-dev-guide#13. Also: #81721.