summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-05-13 11:05:33 +0530
committerGitHub <noreply@github.com>2023-05-13 11:05:33 +0530
commit36125c43da00a2c57452e72c3cf81b71b13de208 (patch)
treee5e827d0c93f013603b704607f326606a53f4fd1 /tests
parent6cb13585d050ed9c708b3d03ab2797ab16a78218 (diff)
parent7c263adb2afc310b96422bd33317407bc5385bf9 (diff)
downloadrust-36125c43da00a2c57452e72c3cf81b71b13de208.tar.gz
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs6
-rw-r--r--tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr12
-rw-r--r--tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs19
-rw-r--r--tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs19
4 files changed, 56 insertions, 0 deletions
diff --git a/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs b/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs
new file mode 100644
index 00000000000..cb265aa7f25
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.rs
@@ -0,0 +1,6 @@
+#![crate_type = "lib"]
+
+#[cfg(overflow_checks)] //~ ERROR `cfg(overflow_checks)` is experimental
+pub fn cast(v: i64)->u32{
+ todo!()
+}
diff --git a/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr b/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr
new file mode 100644
index 00000000000..79aba7945f6
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-cfg_overflow_checks.stderr
@@ -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`.
diff --git a/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs b/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
new file mode 100644
index 00000000000..318be2a6401
--- /dev/null
+++ b/tests/ui/numbers-arithmetic/overflow-attribute-works-1.rs
@@ -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
+}
diff --git a/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs b/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs
new file mode 100644
index 00000000000..0367d980a64
--- /dev/null
+++ b/tests/ui/numbers-arithmetic/overflow-attribute-works-2.rs
@@ -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
+}