Repository navigation
GROOVY-12741: reject contracts on traits and trait methods with a body - #3276
Open
codeconsole wants to merge 1 commit into
Open
codeconsole wants to merge 1 commit into
codeconsole wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 136028e Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
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.
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$selfparameter, and the helper was woven in instruction selection with one argument more than the condition class accepts. The effects:@Requireson a trait method with a parameter crashed class generation (ArrayIndexOutOfBoundsExceptioninInvocationWriter.loadArguments, orStaticInvocationWriterunder@CompileStatic);@Requireswithout parameters and@Ensurescompiled, then every call failed withNo such property: $GCONTRACTS_ENABLED for class: Bank$Trait$Helper; a trait@Invariantwas inherited by the implementing class and failed while the trait's fields were still being initialised.@ThrowsIfand@Decreaseson 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
thisis, 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.ClosureExpressionEvaluationASTTransformationrejects, for every trait in the unit, a class-level contract annotation (@Invariant is not supported on a trait (Bank).) and a contract,@ThrowsIfor@Decreaseson 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
@ThrowsIfand@Decreaseson a trait method hit first, in coreTraitComposer, 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 inThrowsIfTestsand one inMethodDecreasesTests. Thegroovy-contractsmodule passes with the change (405 tests).