Skip to content

Commit

Permalink
Desugars contract into the internal AST extensions
Browse files Browse the repository at this point in the history
Check ensures on early return due to Try / Yeet

Expand these two expressions to include a call to contract checking
  • Loading branch information
pnkfelix authored and gitbot committed Feb 20, 2025
1 parent 2b9391a commit e064caf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Unstable module containing the unstable contracts lang items and attribute macros.
#[cfg(not(bootstrap))]
pub use crate::macros::builtin::contracts_ensures as ensures;
#[cfg(not(bootstrap))]
pub use crate::macros::builtin::contracts_requires as requires;

/// Emitted by rustc as a desugaring of `#[requires(PRED)] fn foo(x: X) { ... }`
/// into: `fn foo(x: X) { check_requires(|| PRED) ... }`
#[cfg(not(bootstrap))]
Expand Down
26 changes: 26 additions & 0 deletions core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,32 @@ pub(crate) mod builtin {
/* compiler built-in */
}

/// Attribute macro applied to a function to give it a post-condition.
///
/// The attribute carries an argument token-tree which is
/// eventually parsed as a unary closure expression that is
/// invoked on a reference to the return value.
#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts", issue = "none")]
#[allow_internal_unstable(core_intrinsics)]
#[rustc_builtin_macro]
pub macro contracts_ensures($item:item) {
/* compiler built-in */
}

/// Attribute macro applied to a function to give it a precondition.
///
/// The attribute carries an argument token-tree which is
/// eventually parsed as an boolean expression with access to the
/// function's formal parameters
#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts", issue = "none")]
#[allow_internal_unstable(core_intrinsics)]
#[rustc_builtin_macro]
pub macro contracts_requires($item:item) {
/* compiler built-in */
}

/// Attribute macro applied to a function to register it as a handler for allocation failure.
///
/// See also [`std::alloc::handle_alloc_error`](../../../std/alloc/fn.handle_alloc_error.html).
Expand Down

0 comments on commit e064caf

Please sign in to comment.