Skip to content

Tracking Issue for cfg(overflow_checks) #111466

Open

Description

@AngelicosPhosphoros

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]
fn cast(v: i64)->u32{
    v.try_into().unwrap()
}

#[cfg(not(overflow_checks))]
fn cast(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.

Steps

Unresolved Questions

Implementation history

#111096
rust-lang/reference#1355

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on May 11, 2023
  2. CAD97 commented on Apr 23, 2024

    @CAD97
    Contributor

    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.

  3. bjoernager commented on Oct 18, 2025

    @bjoernager
    Contributor

    Doesn't seem to have any blockers, no?

  4. nxsaken commented on Nov 4, 2025

    @nxsaken
    Contributor

    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

    const fn add_unsigned(a: i16, b: u16) -> i16 {
        if cfg!(overflow_checks) {
            a.strict_add_unsigned(b)
        } else {
            a.wrapping_add_unsigned(b)
        }
    }

    but without cfg!(overflow_checks), it's impossible to write in user code.

  5. hanna-kruppe commented on Nov 4, 2025

    @hanna-kruppe
    Contributor

    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.

  6. nxsaken commented on Nov 4, 2025

    @nxsaken
    Contributor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions