rust/tests/ui/contracts/declared-vars-used-in-requires-and-ensures.rs
Dawid Lachowicz aeae085dc3
Add contract variable declarations
Contract variables can be declared in the `requires` clause and
can be referenced both in `requires` and `ensures`, subject to usual
borrow checking rules.

This allows any setup common to both the `requires` and `ensures`
clauses to only be done once.
2025-10-18 15:00:34 +01:00

20 lines
528 B
Rust

//@ run-pass
//@ compile-flags: -Zcontract-checks=yes
#![feature(contracts)]
//~^ WARN the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]
extern crate core;
use core::contracts::{ensures, requires};
// checks that variable declarations are lowered properly, with the ability to
// refer to them *both* in requires and ensures
#[requires(let y = 2 * x; y > 0)]
#[ensures(move |ret| { *ret == y })]
fn foo(x: u32) -> u32 {
x * 2
}
fn main() {
foo(1);
}