rust/tests/ui/contracts/internal_machinery/contract-intrinsics.rs
Celina G. Val 3feac59b79 Fix unreachable expression warning
Invert the order that we pass the arguments to the
`contract_check_ensures` function to avoid the warning when the tail
of the function is unreachable.

Note that the call itself is also unreachable, but we have already
handled that case by ignoring unreachable call for contract calls.
2025-04-08 10:46:31 -07:00

37 lines
1.2 KiB
Rust

//@ revisions: default unchk_pass chk_pass chk_fail_ensures chk_fail_requires
//
//@ [default] run-pass
//@ [unchk_pass] run-pass
//@ [chk_pass] run-pass
//@ [chk_fail_requires] run-fail
//@ [chk_fail_ensures] run-fail
//
//@ [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)]
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);
// fail if enabled
#[cfg(any(default, unchk_pass, chk_fail_requires))]
core::intrinsics::contract_check_requires(|| false);
let doubles_to_two = { let old = 2; move |ret: &u32 | ret + ret == old };
// Always pass
core::intrinsics::contract_check_ensures(doubles_to_two, 1);
// Fail if enabled
#[cfg(any(default, unchk_pass, chk_fail_ensures))]
core::intrinsics::contract_check_ensures(doubles_to_two, 2);
}