Skip to content

GROOVY-12741: reject contracts on traits and trait methods with a body - #3276

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/contracts-trait-methods
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/contracts-trait-methods

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Fixes GROOVY-12741.

The contracts transform runs in semantic analysis, before the trait transform, and treated a trait as a class: it generated the condition class from the trait method's parameters and marked the trait @Contracted. The trait transform then moved each method with a body into the helper class as a static method with a leading $self parameter, and the helper was woven in instruction selection with one argument more than the condition class accepts. The effects: @Requires on a trait method with a parameter crashed class generation (ArrayIndexOutOfBoundsException in InvocationWriter.loadArguments, or StaticInvocationWriter under @CompileStatic); @Requires without parameters and @Ensures compiled, then every call failed with No such property: $GCONTRACTS_ENABLED for class: Bank$Trait$Helper; a trait @Invariant was inherited by the implementing class and failed while the trait's fields were still being initialised. @ThrowsIf and @Decreases on a trait method with a body are woven into the helper class after the trait transform has rewritten the body, so a condition that refers to trait state has no receiver there.

Supporting contracts on trait methods needs a design (where the check runs, what this is, how a trait invariant interacts with trait field initialisation, private and static trait methods). This change makes the module report a compile error instead of crashing or failing at run time. ClosureExpressionEvaluationASTTransformation rejects, for every trait in the unit, a class-level contract annotation (@Invariant is not supported on a trait (Bank).) and a contract, @ThrowsIf or @Decreases on a method with a body (@Requires is not supported on a trait method with a body (Bank.deposit); only an abstract trait method may carry a contract.). A contract on an abstract trait method keeps working, as does one on an interface method a trait implements.

The crash that @ThrowsIf and @Decreases on a trait method hit first, in core TraitComposer, is GROOVY-12743 with its own pull request; this change rejects them before that point is reached, so the two are independent.

Tests: a new TraitContractValidationTests (six tests: precondition with and without parameters, postcondition, trait invariant, statically compiled trait, and the abstract trait method that keeps working), two tests in ThrowsIfTests and one in MethodDecreasesTests. The groovy-contracts module passes with the change (405 tests).

The contracts transform runs before the trait transform and treated a
trait as a class, so @requires on a trait method with a parameter crashed
instruction selection, @requires without parameters and @ensures failed
every call with a missing $GCONTRACTS_ENABLED property on the helper
class, and a trait @invariant failed while the trait's fields were still
being initialised. @ThrowsIf and @decreases on a trait method with a body
are woven into the helper class, where a condition on trait state has no
receiver.

All of them are now rejected in semantic analysis with a message that
names the annotation and the method. A contract on an abstract trait
method keeps working, as does one on an interface method a trait
implements.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.4159%. Comparing base (df1fe66) to head (136028e).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3276        +/-   ##
==================================================
+ Coverage     74.4140%   74.4159%   +0.0019%     
- Complexity      44379      44410        +31     
==================================================
  Files            1699       1699                
  Lines          151067     151180       +113     
  Branches        28790      28805        +15     
==================================================
+ Hits           112415     112502        +87     
- Misses          28380      28393        +13     
- Partials        10272      10285        +13     
Files with missing lines Coverage Δ
.../ClosureExpressionEvaluationASTTransformation.java 93.6842% <100.0000%> (+2.9150%) ⬆️

... and 20 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 8, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 136028e
▶️ Tests: 118108 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
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.

2 participants