Skip to content

Conditionally compile contracts instead of deciding at run-time #145229

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

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

Conversation

dawidl022
Copy link

@dawidl022 dawidl022 commented Aug 10, 2025

The initial implementation of contracts, despite requiring a compiler flag to enable runtime checking of contracts, still compiled contracts into function definitions, even when the compiler flag was disabled. This meant that contracts could not be safely added to functions without breaking optimisations, or even without potentially changing the behaviour of the function. This was blocking the addition of contracts to standard library functions in #136578.

This change guards macro expansion of the built-in contract macros with the contract-checks compiler flag. Additionally, it removes the contract_checks compiler intrinsic that was used to determine whether contract checks should be executed at runtime. Now, when contracts checks are compiled into the body of a function, they will always be executed.

The change is motivated by the following discussion: #144438 (comment)

Contracts tracking issue: #128044

Known limitations:

  • When contract-checks are disabled, contracts will not be parsed or type checked, meaning that they are susceptible to becoming out of sync with the rest of the codebase.

The initial implementation of contracts, despite requiring a
compiler flag to enable runtime checking of contracts, still
compiled contracts into function definitions, even when
the compiler flag was disabled. This meant that contracts
could not be safely added to functions without breaking
optimisations, or even without potentially changing
the behaviour of the function.

This change guards macro expansion of the built-in contract macros
with the contract-checks compiler flag. Additionally, it removes
the contract_checks compiler intrinsic that was used to determine
whether contract checks should be executed at runtime. Now,
when contracts checks are compiled into the body of a function,
they will always be executed.
@rustbot
Copy link
Collaborator

rustbot commented Aug 10, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 10, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING] core::build_steps::tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
fmt: checked 6297 files
tidy check
tidy error: found output file `/checkout/tests/ui/contracts/internal_machinery/contract-lang-items.unchk_fail_post.stderr` for unexpected revision `unchk_fail_post` of test `/checkout/tests/ui/contracts/internal_machinery/contract-lang-items.rs`
tidy error: found output file `/checkout/tests/ui/contracts/internal_machinery/contract-lang-items.unchk_pass.stderr` for unexpected revision `unchk_pass` of test `/checkout/tests/ui/contracts/internal_machinery/contract-lang-items.rs`
Checking tidy rustdoc_json...
No error code explanation was removed!
tidy: Skipping binary file check, read-only filesystem
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
---

added 291 packages in 35s
Running eslint on rustdoc JS files
info: ES-Check: there were no ES version matching errors!  🎉
some tidy checks failed
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/rust-tidy /checkout /checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo /checkout/obj/build 4 /node/bin/npm --extra-checks=py,cpp,js` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1634:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1239:29

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:57
  local time: Sun Aug 10 22:07:23 UTC 2025
  network time: Sun, 10 Aug 2025 22:07:24 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@saethlin
Copy link
Member

This was blocking the addition of contracts to standard library functions in #136578.

Hunh? All I can see in that PR is contract checks changing the optimized MIR of functions which is expected and should be fine. Disabled contract check MIR should be cleaned out as part of lowering to LLVM, or it should be immediately cleaned up by LLVM because it is under the equivalent of an if false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.

5 participants