Contracts core intrinsics.

These are hooks to:

  1. control whether contract checks are run
  2. allow 3rd party tools to intercept and reintepret the results of running contracts.
This commit is contained in:
Felix S. Klock II 2024-12-02 20:35:13 +00:00 committed by Celina G. Val
parent 534d79adf9
commit bcb8565f30
30 changed files with 183 additions and 6 deletions

View file

@ -0,0 +1,23 @@
//@ run-pass
//@ revisions: yes no none
//@ [yes] compile-flags: -Zcontract-checks=yes
//@ [no] compile-flags: -Zcontract-checks=no
#![feature(cfg_contract_checks, rustc_contracts, core_intrinsics)]
fn main() {
#[cfg(none)] // default: disabled
assert_eq!(core::intrinsics::contract_checks(), false);
#[cfg(yes)] // explicitly enabled
assert_eq!(core::intrinsics::contract_checks(), true);
#[cfg(no)] // explicitly disabled
assert_eq!(core::intrinsics::contract_checks(), false);
assert_eq!(core::intrinsics::contract_check_requires(|| true), true);
assert_eq!(core::intrinsics::contract_check_requires(|| false), false);
let doubles_to_two = { let old = 2; move |ret| ret + ret == old };
assert_eq!(core::intrinsics::contract_check_ensures(&1, doubles_to_two), true);
assert_eq!(core::intrinsics::contract_check_ensures(&2, doubles_to_two), false);
}

View file

@ -0,0 +1,5 @@
#![crate_type = "lib"]
pub fn contract_checks_are_enabled() -> bool {
cfg!(contract_checks) //~ ERROR `cfg(contract_checks)` is experimental
}

View file

@ -0,0 +1,13 @@
error[E0658]: `cfg(contract_checks)` is experimental and subject to change
--> $DIR/feature-gate-cfg-contract-checks.rs:4:10
|
LL | cfg!(contract_checks)
| ^^^^^^^^^^^^^^^
|
= note: see issue #133866 <https://github.com/rust-lang/rust/issues/133866> for more information
= help: add `#![feature(cfg_contract_checks)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.