Skip to content

Preserve specialization default in function contract attributes - #5153

Merged
feliperodri merged 1 commit into
model-checking:mainfrom
Tiramisu-Cake:fix/default-fn-contracts-5116-v2
Nov 13, 2026
Merged

feliperodri merged 1 commit into
model-checking:mainfrom
Tiramisu-Cake:fix/default-fn-contracts-5116-v2

Conversation

@Tiramisu-Cake

Copy link
Copy Markdown
Contributor

#[kani::requires], #[kani::ensures], and #[kani::modifies] fail to parse a specialization default fn because syn::ItemFn does not accept the default keyword. Parse the function with syn::ImplItemFn and preserve default through the initial and subsequent macro expansions. An override still needs its own contract to be a proof_for_contract target.

The regression tests reproduce the blanket SmallSort impl from #5116 and cover individual and stacked clauses, old, and generic methods with proof_for_contract and stub_verified. The expected-output tests check contract violations and the diagnostic for an override without a contract.

Testing

  • The regression suite passed on Ubuntu 22.04, Ubuntu 24.04, Ubuntu 24.04 ARM, and macOS 15 Intel in fork CI on the final commit. The macOS 14 job was cancelled because the runner image was retired.
  • An earlier revision of this change (before parsing directly into syn::ImplItemFn) also passed the nine new cases and ten existing tests in a targeted compiletest run on Linux with the patch applied to f046b70644.

Resolves #5116

Disclosure: this change and description were prepared with AI assistance.

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

Parse annotated functions directly as syn::ImplItemFn and preserve default through initial and stacked contract expansions. Keep contracts attached to their annotated implementation.

Cover specialization contract clauses, both clause orders with verified stubs, generic methods, old expressions, and expected contract violations. Disable contract assertions in the invalid-caller regression so its expected precondition failure depends on the verified stub.

Resolves model-checking#5116
@Tiramisu-Cake
Tiramisu-Cake requested review from a team as code owners November 11, 2026 15:16
@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 Nov 11, 2026

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Tiramisu-Cake, this is a clean fix for #5116.

syn::ImplItemFn is ItemFn plus defaultness, so parsing the annotated item as ImplItemFn and emitting #vis #defaultness #sig keeps default through the first expansion. The later expansions (handle_expanded) re-emit the whole item, so default survives when there are several contract attributes. Free functions still parse the same way, and a stray default on a free function still gets rustc's usual error, because we pass it through instead of dropping it.

I checked it locally:

  • All 9 new tests pass (14 harnesses plus the 5 expected-output tests).
  • With the base macros, specialization_default_fn_5116.rs fails with E0520 (small_sort … not marked default), so the test does exercise the fix.
  • No regressions in the existing contract suites: kani/FunctionContracts has 22 passed and 0 failed; expected/function-contract has 134 passed and 0 failed.

The tests are thorough. I liked that they cover stub_verified on the default impl, an override with its own contract, an override without a contract, old(), and a method-local generic FnMut.

The red macos-14 jobs are a runner-configuration issue (macos-14-arm64-us-default is not supported) and don't block this PR.

@feliperodri feliperodri added the Z-Contracts Issue related to code contracts label Nov 12, 2026
@feliperodri
feliperodri added this pull request to the merge queue Nov 13, 2026
Merged via the queue into model-checking:main with commit 063204a Nov 13, 2026
25 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-Contracts Issue related to code contracts Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Contract attributes on a specialization default fn fail to parse ("expected fn")

2 participants