Skip to content

Add trybuild test for Captured::state lifetime correctness (#314)#378

Draft
leynos wants to merge 1 commit into
mainfrom
issue-314-trybuild-captured-lifetime
Draft

Add trybuild test for Captured::state lifetime correctness (#314)#378
leynos wants to merge 1 commit into
mainfrom
issue-314-trybuild-captured-lifetime

Conversation

@leynos

@leynos leynos commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #314

Adds the trybuild scaffolding the issue specifies:

  1. tests/compile_fail_tests.rs with captured_state_lifetime_tests running both UI cases.
  2. tests/ui/captured_state_outlives_captured.rs — holds a &State beyond the owning Captured; rejected by the borrow checker (E0597, "captured does not live long enough").
  3. tests/ui/captured_state_within_scope.rs — uses captured.state() only within the owning Captured's lifetime; compiles.
  4. trybuild = { version = "1", features = ["diff"] } dev-dependency.
  5. Expected .stderr recorded once with TRYBUILD=overwrite and committed.

Acceptance checks: fd -e rs . tests/ui | wc -l → 2; rg trybuild Cargo.toml → the new dev-dependency.

Validation

  • make check-fmt / make lint / make test — pass (38 suites, including the new compile-fail target)

🤖 Generated with Claude Code

Summary by Sourcery

Add compile-time UI tests to validate the lifetime correctness of Captured::state() using trybuild.

Build:

  • Add trybuild as a dev-dependency with the diff feature enabled.

Tests:

  • Introduce a trybuild-based compile-fail test suite covering Captured::state() lifetime behavior, including a failing outlives case and a passing well-scoped case.

`MacroStateGuard::as_ref()` returns a `&State` derived from a
`NonNull<Captured<'static>>`, relying on the type system to prevent a
`&State` outliving its owning `Captured`. No compile-time test pinned
that guarantee after the migration from `eval_to_state` to
`render_captured`.

Introduce trybuild infrastructure:

- `tests/compile_fail_tests.rs` runs the two UI cases;
- `tests/ui/captured_state_outlives_captured.rs` holds a `&State`
  beyond its `Captured` and must fail with E0597 (committed `.stderr`
  records the expected diagnostic);
- `tests/ui/captured_state_within_scope.rs` uses `captured.state()`
  inside the owning scope and must compile;
- `trybuild` added as a dev-dependency with the `diff` feature.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c059bf54-4578-4541-ac87-bead05ffdb68

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-314-trybuild-captured-lifetime

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai

sourcery-ai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds trybuild-based UI tests to pin the borrow-checker guarantees around Captured::state() lifetimes, by introducing compile-fail and pass cases plus the required dev-dependency wiring.

Sequence diagram for trybuild-based Captured::state lifetime tests

sequenceDiagram
    actor Developer
    participant compile_fail_tests as compile_fail_tests_rs
    participant TestCases as trybuild_TestCases
    participant rustc

    Developer->>compile_fail_tests: run captured_state_lifetime_tests
    compile_fail_tests->>TestCases: new()

    compile_fail_tests->>TestCases: compile_fail("tests/ui/captured_state_outlives_captured.rs")
    TestCases->>rustc: compile captured_state_outlives_captured.rs
    rustc-->>TestCases: E0597 matches captured_state_outlives_captured.stderr

    compile_fail_tests->>TestCases: pass("tests/ui/captured_state_within_scope.rs")
    TestCases->>rustc: compile captured_state_within_scope.rs
    rustc-->>TestCases: success (no diagnostics)
Loading

File-Level Changes

Change Details Files
Introduce trybuild-based compile-time tests for Captured::state() lifetime behavior.
  • Add a new test harness that runs a failing UI test and a passing UI test via trybuild
  • Create a UI test that intentionally lets a &State outlive its Captured owner and must fail to compile
  • Create a UI test that uses Captured::state() only within the Captured's lifetime and must compile
  • Record the expected compiler stderr for the failing UI test
tests/compile_fail_tests.rs
tests/ui/captured_state_outlives_captured.rs
tests/ui/captured_state_within_scope.rs
tests/ui/captured_state_outlives_captured.stderr
Wire up the trybuild test framework as a dev-dependency.
  • Add trybuild with the diff feature to dev-dependencies so UI tests can be compiled and compared
Cargo.toml
Cargo.lock

Assessment against linked issues

Issue Objective Addressed Explanation
#314 Add trybuild compile-time test scaffolding, including tests/compile_fail_tests.rs with a captured_state_lifetime_tests function that runs a failing UI test for captured_state_outlives_captured.rs and a passing UI test for captured_state_within_scope.rs, and add trybuild as a dev-dependency in Cargo.toml.
#314 Create tests/ui/captured_state_outlives_captured.rs that attempts to hold a &State reference beyond the lifetime of the owning Captured and is rejected by the borrow checker, and record the corresponding expected stderr file tests/ui/captured_state_outlives_captured.stderr.
#314 Create tests/ui/captured_state_within_scope.rs that uses Captured::state() only within the lifetime of the owning Captured and compiles successfully.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add trybuild compile-time test for Captured::state() lifetime correctness

1 participant