-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #111096 - AngelicosPhosphoros:overflow_checks_issue_9…
…1130, 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.
- Loading branch information
Showing
8 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// run-pass | ||
// compile-flags: -C overflow_checks=true | ||
|
||
#![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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |