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
@@ -2,6 +2,8 @@ use crate::num::NonZero;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{cmp, fmt, hash, mem, num};

#![feature(contracts)]
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be in library/core/src/lib.rs I think

@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.

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

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

Click to see the possible cause of the failure (guessed by this bot)
   Compiling compiler_builtins v0.1.143
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
   --> library/core/src/ptr/alignment.rs:167:32
    |
167 |     #[cfg_attr(not(bootstrap), contracts::ensures(
    |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
   --> library/core/src/ptr/alignment.rs:134:32
    |
    |
134 |     #[cfg_attr(not(bootstrap), contracts::requires(self.as_usize().is_power_of_two()))]
    |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
   --> library/core/src/ptr/alignment.rs:135:32
    |
    |
135 |     #[cfg_attr(not(bootstrap), contracts::ensures(
    |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
   --> library/core/src/ptr/alignment.rs:107:32
    |
    |
107 |     #[cfg_attr(not(bootstrap), contracts::ensures(
    |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:99:32
   |
   |
99 |     #[cfg_attr(not(bootstrap), contracts::ensures(|result| result.is_power_of_two()))]
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:81:32
   |
   |
81 |     #[cfg_attr(not(bootstrap), contracts::requires(align > 0 && (align & (align - 1)) == 0))]
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:82:32
   |
   |
82 |     #[cfg_attr(not(bootstrap), contracts::ensures(
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:59:32
   |
   |
59 |     #[cfg_attr(not(bootstrap), contracts::ensures(
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:46:32
   |
   |
46 |     #[cfg_attr(not(bootstrap), contracts::requires(mem::align_of::<T>().is_power_of_two()))]
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `contracts`
  --> library/core/src/ptr/alignment.rs:47:32
   |
   |
47 |     #[cfg_attr(not(bootstrap), contracts::ensures(|result| result.as_usize().is_power_of_two()))]
   |                                ^^^^^^^^^ use of unresolved module or unlinked crate `contracts`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `core` (lib) due to 10 previous errors
Build completed unsuccessfully in 0:05:26
  local time: Wed Feb  5 22:16:10 UTC 2025

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-contracts `#![feature(contracts)]` I-libs-nominated Nominated for discussion during a libs team meeting. 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.

8 participants