Remove no longer used contract_checks intrinsic

The contract_checks compiler flag is now used to determine
if runtime contract checks should be enabled, as opposed
to the compiler intrinsic as previously.
This commit is contained in:
Dawid Lachowicz
2025-09-04 08:24:00 +01:00
parent 2d87527e42
commit 2a5dac7682
11 changed files with 16 additions and 84 deletions
-1
View File
@@ -429,7 +429,6 @@ pub fn extract(attrs: &[impl AttributeExt]) -> Option<(Symbol, Span)> {
// Experimental lang items for implementing contract pre- and post-condition checking.
ContractBuildCheckEnsures, sym::contract_build_check_ensures, contract_build_check_ensures_fn, Target::Fn, GenericRequirement::None;
ContractCheckRequires, sym::contract_check_requires, contract_check_requires_fn, Target::Fn, GenericRequirement::None;
ContractChecks, sym::contract_checks, contract_checks_fn, Target::Fn, GenericRequirement::None;
// Experimental lang items for `MCP: Low level components for async drop`(https://github.com/rust-lang/compiler-team/issues/727)
DefaultTrait4, sym::default_trait4, default_trait4_trait, Target::Trait, GenericRequirement::None;
@@ -647,8 +647,6 @@ pub(crate) fn check_intrinsic_type(
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
// contract_checks() -> bool
sym::contract_checks => (0, 0, Vec::new(), tcx.types.bool),
// contract_check_requires::<C>(C) -> bool, where C: impl Fn() -> bool
sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
sym::contract_check_ensures => {
@@ -34,17 +34,6 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
));
terminator.kind = TerminatorKind::Goto { target };
}
sym::contract_checks => {
let target = target.unwrap();
block.statements.push(Statement::new(
terminator.source_info,
StatementKind::Assign(Box::new((
*destination,
Rvalue::NullaryOp(NullOp::ContractChecks, tcx.types.bool),
))),
));
terminator.kind = TerminatorKind::Goto { target };
}
sym::forget => {
let target = target.unwrap();
block.statements.push(Statement::new(
-18
View File
@@ -2631,24 +2631,6 @@ pub const fn ub_checks() -> bool {
ptr
}
/// Returns whether we should perform contract-checking at runtime.
///
/// This is meant to be similar to the ub_checks intrinsic, in terms
/// of not prematurely committing at compile-time to whether contract
/// checking is turned on, so that we can specify contracts in libstd
/// and let an end user opt into turning them on.
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[rustc_const_unstable(feature = "contracts", issue = "128044")]
#[inline(always)]
#[lang = "contract_checks"]
#[rustc_intrinsic]
pub const fn contract_checks() -> bool {
// FIXME: should this be `false` or `cfg!(contract_checks)`?
// cfg!(contract_checks)
false
}
/// Check if the pre-condition `cond` has been met.
///
/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
@@ -1,29 +1,16 @@
//@ revisions: default unchk_pass chk_pass chk_fail_ensures chk_fail_requires
//@ revisions: default chk_fail_ensures chk_fail_requires
//
//@ [default] run-pass
//@ [unchk_pass] run-pass
//@ [chk_pass] run-pass
//@ [chk_fail_requires] run-crash
//@ [chk_fail_ensures] run-crash
//
//@ [unchk_pass] compile-flags: -Zcontract-checks=no
//@ [chk_pass] compile-flags: -Zcontract-checks=yes
//@ [chk_fail_requires] compile-flags: -Zcontract-checks=yes
//@ [chk_fail_ensures] compile-flags: -Zcontract-checks=yes
#![feature(cfg_contract_checks, contracts_internals, core_intrinsics)]
#![feature(contracts_internals, core_intrinsics)]
fn main() {
#[cfg(any(default, unchk_pass))] // default: disabled
assert_eq!(core::intrinsics::contract_checks(), false);
#[cfg(chk_pass)] // explicitly enabled
assert_eq!(core::intrinsics::contract_checks(), true);
// always pass
core::intrinsics::contract_check_requires(|| true);
// always fail
#[cfg(any(chk_fail_requires))]
#[cfg(chk_fail_requires)]
core::intrinsics::contract_check_requires(|| false);
let doubles_to_two = { let old = 2; move |ret: &u32 | ret + ret == old };
@@ -31,6 +18,6 @@ fn main() {
core::intrinsics::contract_check_ensures(Some(doubles_to_two), 1);
// always fail
#[cfg(any(chk_fail_ensures))]
#[cfg(chk_fail_ensures)]
core::intrinsics::contract_check_ensures(Some(doubles_to_two), 2);
}
@@ -1,5 +1,5 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-lang-items.rs:15:12
--> $DIR/contract-lang-items.rs:8:12
|
LL | #![feature(contracts)] // to access core::contracts
| ^^^^^^^^^
@@ -1,5 +1,5 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-lang-items.rs:15:12
--> $DIR/contract-lang-items.rs:8:12
|
LL | #![feature(contracts)] // to access core::contracts
| ^^^^^^^^^
@@ -1,27 +1,16 @@
//@ revisions: unchk_pass unchk_fail_post chk_pass chk_fail_post
//@ revisions: unchk_pass chk_pass chk_fail_post
//
//@ [unchk_pass] run-pass
//@ [unchk_fail_post] run-pass
//@ [chk_pass] run-pass
//
//@ [chk_fail_post] run-crash
//
//@ [unchk_pass] compile-flags: -Zcontract-checks=no
//@ [unchk_fail_post] compile-flags: -Zcontract-checks=no
//
//@ [chk_pass] compile-flags: -Zcontract-checks=yes
//@ [chk_fail_post] compile-flags: -Zcontract-checks=yes
#![feature(contracts)] // to access core::contracts
//~^ WARN the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]
#![feature(contracts_internals)] // to access check_requires lang item
#![feature(core_intrinsics)]
fn foo(x: Baz) -> i32 {
let injected_checker = if core::intrinsics::contract_checks() {
Some(core::contracts::build_check_ensures(|ret| *ret > 100))
} else {
None
};
let injected_checker = Some(core::contracts::build_check_ensures(|ret| *ret > 100));
let ret = x.baz + 50;
core::intrinsics::contract_check_ensures(injected_checker, ret)
@@ -31,11 +20,11 @@ struct Baz { baz: i32 }
const BAZ_PASS_PRE_POST: Baz = Baz { baz: 100 };
#[cfg(any(unchk_fail_post, chk_fail_post))]
#[cfg(chk_fail_post)]
const BAZ_FAIL_POST: Baz = Baz { baz: 10 };
fn main() {
assert_eq!(foo(BAZ_PASS_PRE_POST), 150);
#[cfg(any(unchk_fail_post, chk_fail_post))]
#[cfg(chk_fail_post)]
foo(BAZ_FAIL_POST);
}
@@ -1,5 +1,5 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-lang-items.rs:15:12
--> $DIR/contract-lang-items.rs:8:12
|
LL | #![feature(contracts)] // to access core::contracts
| ^^^^^^^^^
@@ -2,8 +2,6 @@
fn main() {
// intrinsics are guarded by contracts_internals feature gate.
core::intrinsics::contract_checks();
//~^ ERROR use of unstable library feature `contracts_internals`
core::intrinsics::contract_check_requires(|| true);
//~^ ERROR use of unstable library feature `contracts_internals`
core::intrinsics::contract_check_ensures(Some(|_: &&u32| true), &1);
@@ -1,5 +1,5 @@
error[E0658]: contract internal machinery is for internal use only
--> $DIR/internal-feature-gating.rs:16:28
--> $DIR/internal-feature-gating.rs:14:28
|
LL | fn identity_1() -> i32 contract_requires(|| true) { 10 }
| ^^^^^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | fn identity_1() -> i32 contract_requires(|| true) { 10 }
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: contract internal machinery is for internal use only
--> $DIR/internal-feature-gating.rs:18:28
--> $DIR/internal-feature-gating.rs:16:28
|
LL | fn identity_2() -> i32 contract_ensures(|_| true) { 10 }
| ^^^^^^^^^^^^^^^^
@@ -21,16 +21,6 @@ LL | fn identity_2() -> i32 contract_ensures(|_| true) { 10 }
error[E0658]: use of unstable library feature `contracts_internals`
--> $DIR/internal-feature-gating.rs:5:5
|
LL | core::intrinsics::contract_checks();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= help: add `#![feature(contracts_internals)]` 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[E0658]: use of unstable library feature `contracts_internals`
--> $DIR/internal-feature-gating.rs:7:5
|
LL | core::intrinsics::contract_check_requires(|| true);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
@@ -39,7 +29,7 @@ LL | core::intrinsics::contract_check_requires(|| true);
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature `contracts_internals`
--> $DIR/internal-feature-gating.rs:9:5
--> $DIR/internal-feature-gating.rs:7:5
|
LL | core::intrinsics::contract_check_ensures(Some(|_: &&u32| true), &1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -49,7 +39,7 @@ LL | core::intrinsics::contract_check_ensures(Some(|_: &&u32| true), &1);
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature `contracts_internals`
--> $DIR/internal-feature-gating.rs:12:5
--> $DIR/internal-feature-gating.rs:10:5
|
LL | core::contracts::build_check_ensures(|_: &()| true);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -58,6 +48,6 @@ LL | core::contracts::build_check_ensures(|_: &()| true);
= help: add `#![feature(contracts_internals)]` 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 6 previous errors
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0658`.