Skip to content

Commit 133bfbe

Browse files
committed
Remove 'static bound on contract ensures closure
The `'static` bound on the closure of `build_check_ensures` prevented some patterns of contracts from type checking, without a clear reason for doing so. As such, this change removes the `'static` bound.
1 parent 8e0b68e commit 133bfbe

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

library/core/src/contracts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_require
1818
#[lang = "contract_build_check_ensures"]
1919
pub const fn build_check_ensures<Ret, C>(cond: C) -> C
2020
where
21-
C: Fn(&Ret) -> bool + Copy + 'static,
21+
C: Fn(&Ret) -> bool + Copy,
2222
{
2323
cond
2424
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-pass
2+
//@ compile-flags: -Zcontract-checks=yes
3+
4+
#![feature(contracts)]
5+
//~^ WARN the feature `contracts` is incomplete and may not be safe to use
6+
//and/or cause compiler crashes [incomplete_features]
7+
#![allow(unused)]
8+
9+
// Regression test to allow contract `ensures` clauses to reference non-static
10+
// references and non-static types. Previously, contracts in the below functions
11+
// would raise type/lifetime errors due a `'static` bound on the `ensures`
12+
// closure.
13+
14+
extern crate core;
15+
use core::contracts::ensures;
16+
17+
#[ensures(|_| { x; true })]
18+
pub fn noop<T>(x: &T) {}
19+
20+
#[ensures(move |_| { x; true })]
21+
pub fn noop_mv<T>(x: &T) {}
22+
23+
#[ensures(|_| { x; true })]
24+
pub fn noop_ptr<T>(x: *const T) {}
25+
26+
#[ensures(move |_| { x; true })]
27+
pub fn noop_ptr_mv<T>(x: *const T) {}
28+
29+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/ensures-lifetime.rs:4:12
3+
|
4+
LL | #![feature(contracts)]
5+
| ^^^^^^^^^
6+
|
7+
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)