Type-check expectation arguments when precept is disabled - #57
Merged
Conversation
When the `enabled` feature is off, the stubs in `macros_stubs.rs` expanded to
`{}`, discarding their condition and details expressions entirely. Two problems
followed:
1. The arguments were never type-checked in the disabled build, so a typo or
type error in an `expect_*!` / `observe!` block only surfaced when the crate
was compiled with `enabled` — defeating the "same source compiles identically
in every configuration" guarantee that makes `observe!` blocks safe to leave
inline.
2. Any temporary that existed solely to feed an expectation became an unused
variable, tripping `unused_variables` / `unused_assignments` in downstream
crates built with `-D warnings`.
Rewrite each stub to reference its arguments inside a dead `if false { … }`
block: fully type- and borrow-checked (dead code is still checked), every
captured expression is marked used, and the branch is eliminated before codegen
— preserving the crate's zero-runtime-overhead promise (arguments are never
evaluated at runtime). Argument evaluation mirrors the enabled path in macros.rs
(by value, same moves/borrows).
Verified: `cargo test` and `cargo clippy --all-targets -- -D warnings` pass with
and without `enabled`; a type error inside a disabled `expect_always!` still
fails to compile; and a downstream crate with an `observe!`-only temporary no
longer warns under `-D warnings`.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When the
enabledfeature is off, every macro inmacros_stubs.rsexpands to{}, discarding its condition and details expressions entirely. That has two consequences:Arguments aren't type-checked in the disabled build. A typo or type error inside an
expect_*!/observe!block only surfaces when the crate is compiled withenabled. This defeats the "same source compiles identically in every configuration" guarantee that makesobserve!blocks safe to leave inline in the system under test.Temporaries that exist only to feed an expectation become unused. In a downstream crate built with
-D warnings, a perfectly ordinary pattern warns (and fails CI):With
enabledoff,expect_always!vanishes andsumis nowunused_variables/unused_assignments.Fix
Rewrite each disabled stub to reference its arguments inside a dead
if false { … }block:if false { … }is exactly the right tool here:let _ = $condition/json!($details)reference every temporary, silencing bothunused_variablesand the livenessassigned…never readlints.Argument evaluation mirrors the enabled path in
macros.rs(by value, same moves/borrows), so anything that compiles withenabledalso compiles without it.Only
macros_stubs.rschanges; the enabled path (macros.rs) is untouched.Verification
cargo testpasses with and withoutenabled.cargo clippy --all-targets -- -D warningsclean with and withoutenabled(modulo a pre-existingfault.rsassertions_on_constantslint unrelated to this change).expect_always!still fails to compile (E0277).observe!block has an assertion-only temporary no longer warns under-D warnings, in both feature configs.🤖 Generated with Claude Code