Skip to content

Commit 36125c4

Browse files
authored
Rollup merge of #111096 - AngelicosPhosphoros:overflow_checks_issue_91130, r=petrochenkov
Add support for `cfg(overflow_checks)` This PR adds support for detecting if overflow checks are enabled in similar fashion as `debug_assertions` are detected. Possible use-case of this, for example, if we want to use checked integer casts in builds with overflow checks, e.g. ```rust pub fn cast(val: usize)->u16 { if cfg!(overflow_checks) { val.try_into().unwrap() } else{ vas as _ } } ``` Resolves #91130.
2 parents 6cb1358 + 7c263ad commit 36125c4

File tree

8 files changed

+65
-0
lines changed

8 files changed

+65
-0
lines changed

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ declare_features! (
321321
(active, c_unwind, "1.52.0", Some(74990), None),
322322
/// Allows using C-variadics.
323323
(active, c_variadic, "1.34.0", Some(44930), None),
324+
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
325+
(active, cfg_overflow_checks, "CURRENT_RUSTC_VERSION", Some(111466), None),
324326
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
325327
(active, cfg_sanitize, "1.41.0", Some(39699), None),
326328
/// Allows `cfg(target_abi = "...")`.

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
2424
/// `cfg(...)`'s that are feature gated.
2525
const GATED_CFGS: &[GatedCfg] = &[
2626
// (name in cfg, feature, function to check if the feature is enabled)
27+
(sym::overflow_checks, sym::cfg_overflow_checks, cfg_fn!(cfg_overflow_checks)),
2728
(sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
2829
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
2930
(

compiler/rustc_session/src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
10601060
if sess.opts.debug_assertions {
10611061
ret.insert((sym::debug_assertions, None));
10621062
}
1063+
if sess.overflow_checks() {
1064+
ret.insert((sym::overflow_checks, None));
1065+
}
10631066
// JUSTIFICATION: before wrapper fn is available
10641067
#[allow(rustc::bad_opt_access)]
10651068
if sess.opts.crate_types.contains(&CrateType::ProcMacro) {
@@ -1209,6 +1212,7 @@ impl CrateCheckConfig {
12091212
sym::windows,
12101213
sym::proc_macro,
12111214
sym::debug_assertions,
1215+
sym::overflow_checks,
12121216
sym::target_thread_local,
12131217
] {
12141218
self.expecteds.entry(name).or_insert_with(no_values);

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ symbols! {
463463
cfg_doctest,
464464
cfg_eval,
465465
cfg_hide,
466+
cfg_overflow_checks,
466467
cfg_panic,
467468
cfg_sanitize,
468469
cfg_target_abi,
@@ -1065,6 +1066,7 @@ symbols! {
10651066
or_patterns,
10661067
other,
10671068
out,
1069+
overflow_checks,
10681070
overlapping_marker_traits,
10691071
owned_box,
10701072
packed,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_type = "lib"]
2+
3+
#[cfg(overflow_checks)] //~ ERROR `cfg(overflow_checks)` is experimental
4+
pub fn cast(v: i64)->u32{
5+
todo!()
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: `cfg(overflow_checks)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg_overflow_checks.rs:3:7
3+
|
4+
LL | #[cfg(overflow_checks)]
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #111466 <https://github.com/rust-lang/rust/issues/111466> for more information
8+
= help: add `#![feature(cfg_overflow_checks)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-pass
2+
// compile-flags: -C overflow_checks=true
3+
4+
#![feature(cfg_overflow_checks)]
5+
6+
fn main() {
7+
assert!(cfg!(overflow_checks));
8+
assert!(compiles_differently());
9+
}
10+
11+
#[cfg(overflow_checks)]
12+
fn compiles_differently()->bool {
13+
true
14+
}
15+
16+
#[cfg(not(overflow_checks))]
17+
fn compiles_differently()->bool {
18+
false
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-pass
2+
// compile-flags: -C overflow_checks=false
3+
4+
#![feature(cfg_overflow_checks)]
5+
6+
fn main() {
7+
assert!(!cfg!(overflow_checks));
8+
assert!(!compiles_differently());
9+
}
10+
11+
#[cfg(overflow_checks)]
12+
fn compiles_differently()->bool {
13+
true
14+
}
15+
16+
#[cfg(not(overflow_checks))]
17+
fn compiles_differently()->bool {
18+
false
19+
}

0 commit comments

Comments
 (0)