-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Enable contracts for const functions #138374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-const-fn.rs:17: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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Check if we can annotate a constant function with contracts. | ||
//! | ||
//! The contract is only checked at runtime, and it will not fail if evaluated statically. | ||
//! This is an existing limitation due to the existing architecture and the lack of constant | ||
//! closures. | ||
//! | ||
//@ revisions: all_pass runtime_fail_pre runtime_fail_post | ||
// | ||
//@ [all_pass] run-pass | ||
// | ||
//@ [runtime_fail_pre] run-fail | ||
//@ [runtime_fail_post] run-fail | ||
// | ||
//@ [all_pass] compile-flags: -Zcontract-checks=yes | ||
//@ [runtime_fail_pre] compile-flags: -Zcontract-checks=yes | ||
//@ [runtime_fail_post] 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::*; | ||
|
||
#[requires(x < 100)] | ||
const fn less_than_100(x: u8) -> u8 { | ||
x | ||
} | ||
|
||
// This is wrong on purpose. | ||
#[ensures(|ret| *ret)] | ||
const fn always_true(b: bool) -> bool { | ||
b | ||
} | ||
|
||
const ZERO: u8 = less_than_100(0); | ||
// This is no-op because the contract cannot be checked at compilation time. | ||
const TWO_HUNDRED: u8 = less_than_100(200); | ||
|
||
/// Example from <https://github.com/rust-lang/rust/issues/136925>. | ||
#[ensures(move |ret: &u32| *ret > x)] | ||
const fn broken_sum(x: u32, y: u32) -> u32 { | ||
x + y | ||
} | ||
|
||
fn main() { | ||
assert_eq!(ZERO, 0); | ||
assert_eq!(TWO_HUNDRED, 200); | ||
assert_eq!(broken_sum(0, 1), 1); | ||
assert_eq!(always_true(true), true); | ||
|
||
#[cfg(runtime_fail_post)] | ||
let _ok = always_true(false); | ||
|
||
// Runtime check should fail. | ||
#[cfg(runtime_fail_pre)] | ||
let _200 = less_than_100(200); | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/ui/contracts/contract-const-fn.runtime_fail_post.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-const-fn.rs:17: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
11
tests/ui/contracts/contract-const-fn.runtime_fail_pre.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-const-fn.rs:17: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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.