Skip to content

Commit

Permalink
Rename rustc_contract to contract
Browse files Browse the repository at this point in the history
This has now been approved as a language feature and no longer needs
a `rustc_` prefix.

Also change the `contracts` feature to be marked as incomplete and
`contracts_internals` as internal.
  • Loading branch information
celinval committed Feb 3, 2025
1 parent 2c4923e commit ddbf54b
Show file tree
Hide file tree
Showing 65 changed files with 522 additions and 165 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
gate_all!(unsafe_binders, "unsafe binder types are experimental");
gate_all!(rustc_contracts, "contracts are experimental");
gate_all!(rustc_contracts_internals, "contract internal machinery is for internal use only");
gate_all!(contracts, "contracts are incomplete");
gate_all!(contracts_internals, "contract internal machinery is for internal use only");

if !visitor.features.never_patterns() {
if let Some(spans) = spans.get(&sym::never_patterns) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn expand_contract_clause(

// Record the span as a contract attribute expansion.
// This is used later to stop users from using the extended syntax directly
// which is gated via `rustc_contracts_internals`.
// which is gated via `contracts_internals`.
ecx.psess().contract_attribute_spans.push(attr_span);

Ok(new_tts)
Expand All @@ -137,7 +137,7 @@ fn expand_requires_tts(
) -> Result<TokenStream, ErrorGuaranteed> {
expand_contract_clause(_ecx, attr_span, annotated, |new_tts| {
new_tts.push_tree(TokenTree::Token(
token::Token::from_ast_ident(Ident::new(kw::RustcContractRequires, attr_span)),
token::Token::from_ast_ident(Ident::new(kw::ContractRequires, attr_span)),
Spacing::Joint,
));
new_tts.push_tree(TokenTree::Token(
Expand All @@ -162,7 +162,7 @@ fn expand_ensures_tts(
) -> Result<TokenStream, ErrorGuaranteed> {
expand_contract_clause(_ecx, attr_span, annotated, |new_tts| {
new_tts.push_tree(TokenTree::Token(
token::Token::from_ast_ident(Ident::new(kw::RustcContractEnsures, attr_span)),
token::Token::from_ast_ident(Ident::new(kw::ContractEnsures, attr_span)),
Spacing::Joint,
));
new_tts.push_tree(TokenTree::Delimited(
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ declare_features! (
/// Allows the use of `#[cfg(<true/false>)]`.
(unstable, cfg_boolean_literals, "1.83.0", Some(131204)),
/// Allows the use of `#[cfg(contract_checks)` to check if contract checks are enabled.
(unstable, cfg_contract_checks, "CURRENT_RUSTC_VERSION", Some(133866)),
(unstable, cfg_contract_checks, "CURRENT_RUSTC_VERSION", Some(128044)),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
(unstable, cfg_overflow_checks, "1.71.0", Some(111466)),
/// Provides the relocation model information as cfg entry
Expand Down Expand Up @@ -447,6 +447,10 @@ declare_features! (
(unstable, const_trait_impl, "1.42.0", Some(67792)),
/// Allows the `?` operator in const contexts.
(unstable, const_try, "1.56.0", Some(74935)),
/// Allows use of contracts attributes.
(incomplete, contracts, "CURRENT_RUSTC_VERSION", Some(128044)),
/// Allows access to internal machinery used to implement contracts.
(internal, contracts_internals, "CURRENT_RUSTC_VERSION", Some(128044)),
/// Allows coroutines to be cloned.
(unstable, coroutine_clone, "1.65.0", Some(95360)),
/// Allows defining coroutines.
Expand Down Expand Up @@ -608,10 +612,6 @@ declare_features! (
(unstable, return_type_notation, "1.70.0", Some(109417)),
/// Allows `extern "rust-cold"`.
(unstable, rust_cold_cc, "1.63.0", Some(97544)),
/// Allows use of contracts attributes.
(unstable, rustc_contracts, "CURRENT_RUSTC_VERSION", Some(133866)),
/// Allows access to internal machinery used to implement contracts.
(unstable, rustc_contracts_internals, "CURRENT_RUSTC_VERSION", Some(133866)),
/// Allows use of x86 SHA512, SM3 and SM4 target-features and intrinsics
(unstable, sha512_sm_x86, "1.82.0", Some(126624)),
/// Allows the use of SIMD types in functions declared in `extern` blocks.
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,29 +297,29 @@ impl<'a> Parser<'a> {
})
}

/// Parses a rustc-internal fn contract
/// (`rustc_contract_requires(WWW) rustc_contract_ensures(ZZZ)`)
/// Parses an experimental fn contract
/// (`contract_requires(WWW) contract_ensures(ZZZ)`)
pub(super) fn parse_contract(
&mut self,
) -> PResult<'a, Option<rustc_ast::ptr::P<ast::FnContract>>> {
let gate = |span| {
if self.psess.contract_attribute_spans.contains(span) {
// span was generated via a builtin contracts attribute, so gate as end-user visible
self.psess.gated_spans.gate(sym::rustc_contracts, span);
self.psess.gated_spans.gate(sym::contracts, span);
} else {
// span was not generated via a builtin contracts attribute, so gate as internal machinery
self.psess.gated_spans.gate(sym::rustc_contracts_internals, span);
self.psess.gated_spans.gate(sym::contracts_internals, span);
}
};

let requires = if self.eat_keyword_noexpect(exp!(RustcContractRequires).kw) {
let requires = if self.eat_keyword_noexpect(exp!(ContractRequires).kw) {
let precond = self.parse_expr()?;
gate(precond.span);
Some(precond)
} else {
None
};
let ensures = if self.eat_keyword_noexpect(exp!(RustcContractEnsures).kw) {
let ensures = if self.eat_keyword_noexpect(exp!(ContractEnsures).kw) {
let postcond = self.parse_expr()?;
gate(postcond.span);
Some(postcond)
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_parse/src/parser/token_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub enum TokenType {
KwCatch,
KwConst,
KwContinue,
KwContractEnsures,
KwContractRequires,
KwCrate,
KwDefault,
KwDyn,
Expand All @@ -108,8 +110,6 @@ pub enum TokenType {
KwRef,
KwReturn,
KwReuse,
KwRustcContractEnsures,
KwRustcContractRequires,
KwSafe,
KwSelfUpper,
KwStatic,
Expand Down Expand Up @@ -219,6 +219,8 @@ impl TokenType {
KwCatch,
KwConst,
KwContinue,
KwContractEnsures,
KwContractRequires,
KwCrate,
KwDefault,
KwDyn,
Expand All @@ -244,8 +246,6 @@ impl TokenType {
KwRef,
KwReturn,
KwReuse,
KwRustcContractEnsures,
KwRustcContractRequires,
KwSafe,
KwSelfUpper,
KwStatic,
Expand Down Expand Up @@ -293,6 +293,8 @@ impl TokenType {
TokenType::KwCatch => Some(kw::Catch),
TokenType::KwConst => Some(kw::Const),
TokenType::KwContinue => Some(kw::Continue),
TokenType::KwContractEnsures => Some(kw::ContractEnsures),
TokenType::KwContractRequires => Some(kw::ContractRequires),
TokenType::KwCrate => Some(kw::Crate),
TokenType::KwDefault => Some(kw::Default),
TokenType::KwDyn => Some(kw::Dyn),
Expand All @@ -318,8 +320,6 @@ impl TokenType {
TokenType::KwRef => Some(kw::Ref),
TokenType::KwReturn => Some(kw::Return),
TokenType::KwReuse => Some(kw::Reuse),
TokenType::KwRustcContractEnsures => Some(kw::RustcContractEnsures),
TokenType::KwRustcContractRequires => Some(kw::RustcContractRequires),
TokenType::KwSafe => Some(kw::Safe),
TokenType::KwSelfUpper => Some(kw::SelfUpper),
TokenType::KwStatic => Some(kw::Static),
Expand Down Expand Up @@ -525,6 +525,8 @@ macro_rules! exp {
(Catch) => { exp!(@kw, Catch, KwCatch) };
(Const) => { exp!(@kw, Const, KwConst) };
(Continue) => { exp!(@kw, Continue, KwContinue) };
(ContractEnsures) => { exp!(@kw, ContractEnsures, KwContractEnsures) };
(ContractRequires) => { exp!(@kw, ContractRequires, KwContractRequires) };
(Crate) => { exp!(@kw, Crate, KwCrate) };
(Default) => { exp!(@kw, Default, KwDefault) };
(Dyn) => { exp!(@kw, Dyn, KwDyn) };
Expand All @@ -550,8 +552,6 @@ macro_rules! exp {
(Ref) => { exp!(@kw, Ref, KwRef) };
(Return) => { exp!(@kw, Return, KwReturn) };
(Reuse) => { exp!(@kw, Reuse, KwReuse) };
(RustcContractEnsures) => { exp!(@kw, RustcContractEnsures, KwRustcContractEnsures) };
(RustcContractRequires) => { exp!(@kw, RustcContractRequires, KwRustcContractRequires) };
(Safe) => { exp!(@kw, Safe, KwSafe) };
(SelfUpper) => { exp!(@kw, SelfUpper, KwSelfUpper) };
(Static) => { exp!(@kw, Static, KwStatic) };
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ symbols! {
MacroRules: "macro_rules",
Raw: "raw",
Reuse: "reuse",
RustcContractEnsures: "rustc_contract_ensures",
RustcContractRequires: "rustc_contract_requires",
ContractEnsures: "contract_ensures",
ContractRequires: "contract_requires",
Safe: "safe",
Union: "union",
Yeet: "yeet",
Expand Down Expand Up @@ -682,7 +682,9 @@ symbols! {
contract_check_ensures,
contract_check_requires,
contract_checks,
contracts,
contracts_ensures,
contracts_internals,
contracts_requires,
convert_identity,
copy,
Expand Down Expand Up @@ -1716,8 +1718,6 @@ symbols! {
rustc_const_stable,
rustc_const_stable_indirect,
rustc_const_unstable,
rustc_contracts,
rustc_contracts_internals,
rustc_conversion_suggestion,
rustc_deallocator,
rustc_def_path,
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_require
/// Emitted by rustc as a desugaring of `#[ensures(PRED)] fn foo() -> R { ... [return R;] ... }`
/// into: `fn foo() { let _check = build_check_ensures(|ret| PRED) ... [return _check(R);] ... }`
/// (including the implicit return of the tail expression, if any).
#[unstable(feature = "rustc_contracts_internals", issue = "133866" /* compiler-team#759 */)]
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[lang = "contract_build_check_ensures"]
#[track_caller]
pub fn build_check_ensures<Ret, C>(cond: C) -> impl (Fn(Ret) -> Ret) + Copy
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4051,8 +4051,8 @@ pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize)
/// checking is turned on, so that we can specify contracts in libstd
/// and let an end user opt into turning them on.
#[cfg(not(bootstrap))]
#[rustc_const_unstable(feature = "rustc_contracts_internals", issue = "133866" /* compiler-team#759 */)]
#[unstable(feature = "rustc_contracts_internals", issue = "133866" /* compiler-team#759 */)]
#[rustc_const_unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[inline(always)]
#[rustc_intrinsic]
pub const fn contract_checks() -> bool {
Expand All @@ -4067,7 +4067,7 @@ pub const fn contract_checks() -> bool {
/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
/// returns false.
#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts_internals", issue = "133866" /* compiler-team#759 */)]
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[lang = "contract_check_requires"]
#[rustc_intrinsic]
pub fn contract_check_requires<C: Fn() -> bool>(cond: C) {
Expand All @@ -4082,7 +4082,7 @@ pub fn contract_check_requires<C: Fn() -> bool>(cond: C) {
/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
/// returns false.
#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts_internals", issue = "133866" /* compiler-team#759 */)]
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
#[rustc_intrinsic]
pub fn contract_check_ensures<'a, Ret, C: Fn(&'a Ret) -> bool>(ret: &'a Ret, cond: C) {
if contract_checks() && !cond(ret) {
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub mod autodiff {
}

#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts", issue = "133866")]
#[unstable(feature = "contracts", issue = "128044")]
pub mod contracts;

#[unstable(feature = "cfg_match", issue = "115585")]
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,8 +1783,8 @@ pub(crate) mod builtin {
/// 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 = "133866")]
#[allow_internal_unstable(rustc_contracts_internals)]
#[unstable(feature = "contracts", issue = "128044")]
#[allow_internal_unstable(contracts_internals)]
#[rustc_builtin_macro]
pub macro contracts_ensures($item:item) {
/* compiler built-in */
Expand All @@ -1796,8 +1796,8 @@ pub(crate) mod builtin {
/// eventually parsed as an boolean expression with access to the
/// function's formal parameters
#[cfg(not(bootstrap))]
#[unstable(feature = "rustc_contracts", issue = "133866")]
#[allow_internal_unstable(rustc_contracts_internals)]
#[unstable(feature = "contracts", issue = "128044")]
#[allow_internal_unstable(contracts_internals)]
#[rustc_builtin_macro]
pub macro contracts_requires($item:item) {
/* compiler built-in */
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/contracts/contract-annotation-limitations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Test for some of the existing limitations and the current error messages.
//! Some of these limitations may be removed in the future.
#![feature(rustc_contracts)]
#![feature(contracts)]
//~^ WARN the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]
#![allow(dead_code)]

/// Represent a 5-star system.
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/contracts/contract-annotation-limitations.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
error: contract annotations is only supported in functions with bodies
--> $DIR/contract-annotation-limitations.rs:17:5
--> $DIR/contract-annotation-limitations.rs:18:5
|
LL | #[core::contracts::ensures(|ret| ret.is_none_or(Stars::is_valid))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: contract annotations is only supported in functions with bodies
--> $DIR/contract-annotation-limitations.rs:21:5
--> $DIR/contract-annotation-limitations.rs:22:5
|
LL | #[core::contracts::ensures(|ret| ret.is_none_or(Stars::is_valid))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-annotation-limitations.rs:4:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 2 previous errors; 1 warning emitted

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-generics.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-generics.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-generics.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

11 changes: 11 additions & 0 deletions tests/ui/contracts/contract-attributes-generics.chk_pass.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-generics.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

3 changes: 2 additions & 1 deletion tests/ui/contracts/contract-attributes-generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
//@ [chk_fail_post] compile-flags: -Zcontract-checks=yes
//@ [chk_const_fail] compile-flags: -Zcontract-checks=yes

#![feature(rustc_contracts)]
#![feature(contracts)]
//~^ WARN the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]

use std::ops::Sub;

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/contracts/contract-attributes-generics.unchk_pass.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-generics.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

11 changes: 11 additions & 0 deletions tests/ui/contracts/contract-attributes-nest.chk_fail_post.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/contract-attributes-nest.rs:19:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

Loading

0 comments on commit ddbf54b

Please sign in to comment.