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 support for cfg(overflow_checks) #111096

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ declare_features! (
(active, c_unwind, "1.52.0", Some(74990), None),
/// Allows using C-variadics.
(active, c_variadic, "1.34.0", Some(44930), None),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
(active, cfg_overflow_checks, "CURRENT_RUSTC_VERSION", Some(111466), None),
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
(active, cfg_sanitize, "1.41.0", Some(39699), None),
/// Allows `cfg(target_abi = "...")`.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
/// `cfg(...)`'s that are feature gated.
const GATED_CFGS: &[GatedCfg] = &[
// (name in cfg, feature, function to check if the feature is enabled)
(sym::overflow_checks, sym::cfg_overflow_checks, cfg_fn!(cfg_overflow_checks)),
(sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
if sess.opts.debug_assertions {
ret.insert((sym::debug_assertions, None));
}
if sess.overflow_checks() {
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
ret.insert((sym::overflow_checks, None));
}
// JUSTIFICATION: before wrapper fn is available
#[allow(rustc::bad_opt_access)]
if sess.opts.crate_types.contains(&CrateType::ProcMacro) {
Expand Down Expand Up @@ -1209,6 +1212,7 @@ impl CrateCheckConfig {
sym::windows,
sym::proc_macro,
sym::debug_assertions,
sym::overflow_checks,
sym::target_thread_local,
] {
self.expecteds.entry(name).or_insert_with(no_values);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ symbols! {
cfg_doctest,
cfg_eval,
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_sanitize,
cfg_target_abi,
Expand Down Expand Up @@ -1065,6 +1066,7 @@ symbols! {
or_patterns,
other,
out,
overflow_checks,
overlapping_marker_traits,
owned_box,
packed,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![crate_type = "lib"]

#[cfg(overflow_checks)] //~ ERROR `cfg(overflow_checks)` is experimental
pub fn cast(v: i64)->u32{
todo!()
}
12 changes: 12 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: `cfg(overflow_checks)` is experimental and subject to change
--> $DIR/feature-gate-cfg_overflow_checks.rs:3:7
|
LL | #[cfg(overflow_checks)]
| ^^^^^^^^^^^^^^^
|
= note: see issue #111466 <https://github.com/rust-lang/rust/issues/111466> for more information
= help: add `#![feature(cfg_overflow_checks)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
19 changes: 19 additions & 0 deletions tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-pass
// compile-flags: -C overflow_checks=true
jyn514 marked this conversation as resolved.
Show resolved Hide resolved

#![feature(cfg_overflow_checks)]

fn main() {
assert!(cfg!(overflow_checks));
assert!(compiles_differently());
}

#[cfg(overflow_checks)]
fn compiles_differently()->bool {
true
}

#[cfg(not(overflow_checks))]
fn compiles_differently()->bool {
false
}
19 changes: 19 additions & 0 deletions tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-pass
// compile-flags: -C overflow_checks=false

#![feature(cfg_overflow_checks)]

fn main() {
assert!(!cfg!(overflow_checks));
assert!(!compiles_differently());
}

#[cfg(overflow_checks)]
fn compiles_differently()->bool {
true
}

#[cfg(not(overflow_checks))]
fn compiles_differently()->bool {
false
}