Skip to content
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

Add contracts for all functions in Alignment #136578

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tautschnig
Copy link

Uses the contracts syntax introduced in #128045.

@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tgross35 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 5, 2025
@rust-log-analyzer

This comment has been minimized.

@jieyouxu jieyouxu added the F-contracts `#![feature(contracts)]` label Feb 5, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Feb 5, 2025

You could also tag contracts-related PRs with F-contracts in the future via

@rustbot label: +F-contracts

in a comment / PR description.

@jieyouxu jieyouxu mentioned this pull request Feb 5, 2025
8 tasks
@tgross35
Copy link
Contributor

tgross35 commented Feb 5, 2025

Could you say some more about the motivation? This feature was merged only a number of hours ago, that seems soon to start using in std. Even once it is more stable, what should and shouldn't get contracts is probably something that needs to get discussed.

@rust-lang/libs any thoughts here?

@jieyouxu
Copy link
Member

jieyouxu commented Feb 5, 2025

Yeah, this was only approved as a lang experiment on the lang/compiler side, no idea what the situation is libs side (which certainly should be discussed).

cc @pnkfelix @nikomatsakis @celinval (lang liason and ppl who were implementing this feature compiler side)

@Noratrieb Noratrieb added the I-libs-nominated Nominated for discussion during a libs team meeting. label Feb 5, 2025
@Noratrieb
Copy link
Member

I'll libs nominate this for discussion (this should only be discussed after the contracts people mentioned above have replied).

@celinval
Copy link
Contributor

celinval commented Feb 5, 2025

Totally worth discussion, and it would be great to get some feedback on the features that are needed to meet the libs team bar. I am especially curious to see how you think contracts and ub_checks will interact.

@Noratrieb
Copy link
Member

It would be useful to know what your plan/intention is with adding contracts here. Is it just to try them out or is annotating the standard library with contracts for downstream consumers part of the goal of contracts?

@tautschnig
Copy link
Author

Could you say some more about the motivation? This feature was merged only a number of hours ago, that seems soon to start using in std. Even once it is more stable, what should and shouldn't get contracts is probably something that needs to get discussed.

As far as the motivation is concerned: we're working towards https://rust-lang.github.io/rust-project-goals/2025h1/std-contracts.html, and I created this (draft) PR with the hope to initiate discussion while knowing there is a lot more work to be done on our end.

As I am new contributor: is there other information that I can provide or another forum that I should use rather than this PR?

@tautschnig
Copy link
Author

It would be useful to know what your plan/intention is with adding contracts here. Is it just to try them out or is annotating the standard library with contracts for downstream consumers part of the goal of contracts?

Our current goal is https://rust-lang.github.io/rust-project-goals/2025h1/std-contracts.html, though we are in early stages and it indeed is about trying out what works best. Eventually we want to enable verification of downstream consumers, but a lot more work will be required before we get there.

@tautschnig tautschnig force-pushed the upstream-contracts/alignment branch from d46f868 to 7288286 Compare February 5, 2025 22:06
@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor

tgross35 commented Feb 5, 2025

As far as the motivation is concerned: we're working towards https://rust-lang.github.io/rust-project-goals/2025h1/std-contracts.html, and I created this (draft) PR with the hope to initiate discussion while knowing there is a lot more work to be done on our end.

As I am new contributor: is there other information that I can provide or another forum that I should use rather than this PR?

Are there any docs on how contracts work as they exist today? Ignoring the stability question, we need something to refer to about how to use these properly, e.g. in https://doc.rust-lang.org/nightly/unstable-book/. Also, how are contracts that get merged into r-l/rust being verified?

The linked page is somewhat vague about what this means for std. I think we could also use a policy page in the library dev guide (https://std-dev-guide.rust-lang.org) giving guidelines for their use, based on the outcome of any discussion that happens.

Bringing this up on the libs zulip would be a good idea to get the ball rolling https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs.

@@ -43,6 +43,8 @@ impl Alignment {
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
#[must_use]
#[cfg_attr(not(bootstrap), contracts::requires(mem::align_of::<T>().is_power_of_two()))]

This comment was marked as resolved.

@tautschnig tautschnig force-pushed the upstream-contracts/alignment branch from 7288286 to c180bd0 Compare February 6, 2025 13:23
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Amanieu
Copy link
Member

Amanieu commented Feb 12, 2025

We discussed this in the libs meeting today. We're happy to add contracts to the standard library as an experiment, as long as this doesn't get in the way of normal standard library development. Depending on how the contracts feature evolves, we may reconsider our level of support.

@Amanieu Amanieu removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Feb 12, 2025
@@ -73,6 +80,10 @@ impl Alignment {
/// It must *not* be zero.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
#[cfg_attr(not(bootstrap), core::contracts::requires(align > 0 && (align & (align - 1)) == 0))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not requires(align.is_power_of_two())? What is it about this attribute that needs to write out the implementation instead of using the method like how the ensures on the line below does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time of originally writing this I was wondering about performance (mainly in the context of verification tools). It seemed unnecessary to do a full popcount when a subtraction and bitwise and would also do the trick. But maybe we should first focus on readability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time of originally writing this I was wondering about performance (mainly in the context of verification tools). It seemed unnecessary to do a full popcount when a subtraction and bitwise and would also do the trick.

Seems is_power_of_two actually give more optimal (branchless) codegen than the manual version: https://godbolt.org/z/c5McsafYj
Even in rustc 1.32, the earliest version with usize::is_power_of_two, it does not do popcount, once you have opt-level >=1.

But maybe we should first focus on readability.

And also for consistency with other contracts.

Uses the contracts syntax introduced in rust-lang#128045.
@tautschnig tautschnig force-pushed the upstream-contracts/alignment branch from 4322394 to dfd17f1 Compare February 12, 2025 22:22
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#22 exporting to docker image format
#22 sending tarball 28.9s done
#22 DONE 35.1s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
   Compiling object v0.36.7
   Compiling std v0.0.0 (/checkout/library/std)
   Compiling cc v1.2.0
   Compiling compiler_builtins v0.1.146
error[E0015]: cannot call non-const function `build_check_ensures::<usize, {closure@library/core/src/mem/mod.rs:489:53: 489:69}>` in constant functions
    |
    |
489 | #[cfg_attr(not(bootstrap), core::contracts::ensures(|result: &usize| result.is_power_of_two()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
   --> library/core/src/mem/mod.rs:489:28
    |
489 | #[cfg_attr(not(bootstrap), core::contracts::ensures(|result: &usize| result.is_power_of_two()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants


error[E0015]: cannot call non-const function `build_check_ensures::<alignment::Alignment, {closure@library/core/src/ptr/alignment.rs:47:13: 47:33}>` in constant functions
   |
   |
46 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
   |  ________________________________^
47 | |             |result: &Alignment| result.as_usize().is_power_of_two()))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
  --> library/core/src/ptr/alignment.rs:46:32
   |
46 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
   |  ________________________________^
47 | |             |result: &Alignment| result.as_usize().is_power_of_two()))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants


error[E0015]: cannot call non-const function `build_check_ensures::<Option<alignment::Alignment>, {closure@library/core/src/ptr/alignment.rs:60:13: 60:41}>` in constant functions
   |
   |
59 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
   |  ________________________________^
60 | |             |result: &Option<Alignment>| align.is_power_of_two() == result.is_some() &&
61 | |             (result.is_none() || result.unwrap().as_usize() == align)))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
  --> library/core/src/ptr/alignment.rs:59:32
   |
59 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
   |  ________________________________^
60 | |             |result: &Option<Alignment>| align.is_power_of_two() == result.is_some() &&
61 | |             (result.is_none() || result.unwrap().as_usize() == align)))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: cannot call non-const intrinsic `contract_check_requires` in constant functions
error: cannot call non-const intrinsic `contract_check_requires` in constant functions
  --> library/core/src/ptr/alignment.rs:81:32
   |
81 |     #[cfg_attr(not(bootstrap), core::contracts::requires(align > 0 && (align & (align - 1)) == 0))]


error[E0015]: cannot call non-const function `build_check_ensures::<alignment::Alignment, {closure@library/core/src/ptr/alignment.rs:83:13: 83:33}>` in constant functions
   |
   |
82 | /     #[cfg_attr(not(bootstrap), core::contracts::ensures(
83 | |             |result: &Alignment| result.as_usize() == align &&
84 | |             result.as_usize().is_power_of_two()))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
  --> library/core/src/ptr/alignment.rs:82:5
   |
82 | /     #[cfg_attr(not(bootstrap), core::contracts::ensures(
83 | |             |result: &Alignment| result.as_usize() == align &&
84 | |             result.as_usize().is_power_of_two()))]
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants


error[E0015]: cannot call non-const function `build_check_ensures::<usize, {closure@library/core/src/ptr/alignment.rs:101:13: 101:29}>` in constant functions
    |
    |
100 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
101 | |             |result: &usize| result.is_power_of_two()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
   --> library/core/src/ptr/alignment.rs:100:32
    |
100 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
101 | |             |result: &usize| result.is_power_of_two()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants


error[E0015]: cannot call non-const function `build_check_ensures::<NonZero<usize>, {closure@library/core/src/ptr/alignment.rs:110:13: 110:38}>` in constant functions
    |
    |
109 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
110 | |             |result: &NonZero<usize>| result.get().is_power_of_two() &&
111 | |             result.get() == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
   --> library/core/src/ptr/alignment.rs:109:32
    |
109 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
110 | |             |result: &NonZero<usize>| result.get().is_power_of_two() &&
111 | |             result.get() == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: cannot call non-const intrinsic `contract_check_requires` in constant functions
error: cannot call non-const intrinsic `contract_check_requires` in constant functions
   --> library/core/src/ptr/alignment.rs:137:32
    |
137 |     #[cfg_attr(not(bootstrap), core::contracts::requires(self.as_usize().is_power_of_two()))]


error[E0015]: cannot call non-const function `build_check_ensures::<u32, {closure@library/core/src/ptr/alignment.rs:139:13: 139:27}>` in constant functions
    |
    |
138 | /     #[cfg_attr(not(bootstrap), core::contracts::ensures(
139 | |             |result: &u32| *result < usize::BITS &&
140 | |             (1usize << *result) == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
   --> library/core/src/ptr/alignment.rs:138:5
    |
138 | /     #[cfg_attr(not(bootstrap), core::contracts::ensures(
139 | |             |result: &u32| *result < usize::BITS &&
140 | |             (1usize << *result) == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants


error[E0015]: cannot call non-const function `build_check_ensures::<usize, {closure@library/core/src/ptr/alignment.rs:171:13: 171:29}>` in constant functions
    |
    |
170 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
171 | |             |result: &usize| *result > 0 &&
172 | |             *result == !(self.as_usize() -1) &&
173 | |             self.as_usize() & *result == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const closure in constant functions
error[E0015]: cannot call non-const closure in constant functions
   --> library/core/src/ptr/alignment.rs:170:32
    |
170 |       #[cfg_attr(not(bootstrap), core::contracts::ensures(
    |  ________________________________^
171 | |             |result: &usize| *result > 0 &&
172 | |             *result == !(self.as_usize() -1) &&
173 | |             self.as_usize() & *result == self.as_usize()))]
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

For more information about this error, try `rustc --explain E0015`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-contracts `#![feature(contracts)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.