Skip to content

Commit 91ff63d

Browse files
pnkfelixgitbot
authored and
gitbot
committed
contracts: added lang items that act as hooks for rustc-injected code to invoke.
see test for an example of the kind of injected code that is anticipated here.
1 parent c38f8f0 commit 91ff63d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

core/src/contracts.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Unstable module containing the unstable contracts lang items and attribute macros.
2+
3+
/// Emitted by rustc as a desugaring of `#[requires(PRED)] fn foo(x: X) { ... }`
4+
/// into: `fn foo(x: X) { check_requires(|| PRED) ... }`
5+
#[cfg(not(bootstrap))]
6+
#[unstable(feature = "rustc_contracts", issue = "none" /* compiler-team#759 */)]
7+
#[lang = "contract_check_requires"]
8+
#[track_caller]
9+
pub fn check_requires<C: FnOnce() -> bool>(c: C) {
10+
if core::intrinsics::contract_checks() {
11+
assert!(core::intrinsics::contract_check_requires(c), "failed requires check");
12+
}
13+
}
14+
15+
/// Emitted by rustc as a desugaring of `#[ensures(PRED)] fn foo() -> R { ... [return R;] ... }`
16+
/// into: `fn foo() { let _check = build_check_ensures(|ret| PRED) ... [return _check(R);] ... }`
17+
/// (including the implicit return of the tail expression, if any).
18+
#[cfg(not(bootstrap))]
19+
#[unstable(feature = "rustc_contracts", issue = "none" /* compiler-team#759 */)]
20+
#[lang = "contract_build_check_ensures"]
21+
#[track_caller]
22+
pub fn build_check_ensures<Ret, C>(c: C) -> impl (FnOnce(Ret) -> Ret) + Copy
23+
where
24+
C: for<'a> FnOnce(&'a Ret) -> bool + Copy + 'static,
25+
{
26+
#[track_caller]
27+
move |ret| {
28+
if core::intrinsics::contract_checks() {
29+
assert!(core::intrinsics::contract_check_ensures(&ret, c), "failed ensures check");
30+
}
31+
ret
32+
}
33+
}

core/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
#![feature(bstr)]
115115
#![feature(bstr_internals)]
116116
#![feature(const_carrying_mul_add)]
117+
#![feature(closure_track_caller)]
117118
#![feature(const_eval_select)]
118119
#![feature(core_intrinsics)]
119120
#![feature(coverage_attribute)]
@@ -247,6 +248,10 @@ pub mod autodiff {
247248
pub use crate::macros::builtin::autodiff;
248249
}
249250

251+
#[cfg(not(bootstrap))]
252+
#[unstable(feature = "rustc_contracts", issue = "none")]
253+
pub mod contracts;
254+
250255
#[unstable(feature = "cfg_match", issue = "115585")]
251256
pub use crate::macros::cfg_match;
252257

0 commit comments

Comments
 (0)