Skip to content

Autoharness: do not synthesize Arbitrary for structs with reference fields - #4694

Merged
feliperodri merged 5 commits into
model-checking:mainfrom
tautschnig:fix-ref-field-derive
Aug 23, 2026
Merged

feliperodri merged 5 commits into
model-checking:mainfrom
tautschnig:fix-ref-field-derive

Conversation

@tautschnig

Copy link
Copy Markdown
Member

Description

can_derive_arbitrary recursed into reference field types, so a struct with a &'static reference field (reference fields with lifetime parameters were already rejected via the ADT's generic arguments) was deemed derivable. The compiler-synthesized any() then created the referent's storage inside its own body and returned a dangling reference, so the generated harness failed spuriously with "dereference failure: dead object" — a false alarm the user can do nothing about.

Reject reference fields in the derivability check; such functions are now skipped with the usual "Missing Arbitrary implementation" reason. Top-level reference arguments are unaffected: for those the generated harness itself owns the referent's storage, which outlives the call.

Found while implementing #4691 (the storage-lifetime asymmetry between argument position and field position is exactly what that PR documents for slices).

Testing

New script-based test cargo_autoharness_ref_field: asserts the struct-with-reference-field function is skipped (previously: spurious dead-object failure) while a plain &u32 argument remains supported. Formatting and the compiler unit tests pass.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

…ields

can_derive_arbitrary recursed into reference field types (reachable for
'static references; reference fields with a lifetime parameter were already
rejected via the ADT's generic arguments), so a struct like

    struct HasStaticRef { r: &'static u32 }

was deemed derivable. The synthesized any() then created the referent's
storage inside its own body and returned a dangling reference, producing
spurious 'dereference failure: dead object' verification failures in the
generated harness.

Reject reference fields in the derivability check. Note the asymmetry with
top-level argument references, which remain supported: for those, the
generated harness itself owns the referent's storage, which therefore
outlives the call.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig
tautschnig requested a review from a team as a code owner July 29, 2026 09:43
Copilot AI review requested due to automatic review settings July 29, 2026 09:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Jul 29, 2026
@feliperodri feliperodri added the Z-Autoharness Issue related to autoharness subcommand label Jul 29, 2026
The test's RefStruct/RefRefStruct cases move from derived (17 harnesses) to
skipped (Missing Arbitrary implementation), matching this PR's behavior
change; document the rationale in the test source.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

# Conflicts:
#	tests/script-based-pre/autoderive_arbitrary_structs/structs.expected
The behavior change (structs with reference fields are not derived) is
already covered by autoderive_arbitrary_structs (RefStruct/RefRefStruct now
skipped as 'Missing Arbitrary implementation'). The preserved support for
top-level reference arguments is already covered by autoharness-refs_immutable
and autoharness-refs_mutable (takes_ref/takes_mut_ref etc.). The dedicated test
added no unique coverage.
@feliperodri
feliperodri requested a balanced review from Copilot August 23, 2026 16:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread kani-compiler/src/kani_middle/mod.rs Resolved
… derivability

can_derive_arbitrary recursed into an ADT field (and implements_arbitrary
into a top-level &ADT pointee) without first checking whether that type
already implements Arbitrary. A type with a hand-written Arbitrary impl that
contains a reference field is not compiler-derivable, so nesting it in another
struct -- or passing it behind a top-level reference -- was wrongly skipped,
even though the synthesized any() would just call the hand-written impl via
kani::any::<Field>().

Check implements_arbitrary before falling back to can_derive_arbitrary on both
paths. A *bare* reference field is still rejected outright: it cannot carry a
hand-written impl (orphan rule) and synthesizing it would dangle.

Addresses review feedback on model-checking#4694.
@feliperodri
feliperodri added this pull request to the merge queue Aug 23, 2026
Merged via the queue into model-checking:main with commit 484938e Aug 23, 2026
34 checks passed
@feliperodri feliperodri added the [C] Feature / Enhancement A new feature request or enhancement to an existing feature. label Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-Autoharness Issue related to autoharness subcommand Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants