[
https://issues.apache.org/jira/browse/GROOVY-12843?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152469#comment-18152469
]
ASF GitHub Bot commented on GROOVY-12843:
-----------------------------------------
paulk-asert opened a new pull request, #3371:
URL: https://github.com/apache/groovy/pull/3371
A @Requires or @Ensures on a default or static interface method was woven
into the method, but the check reads a $GCONTRACTS_ENABLED field that was added
only to classes: every call failed with a missing property, and static
compilation was refused. An interface with a concrete method now has the field,
and the check reads it from the interface.
A postcondition also called the old-variables method, which only a class
compiled with contracts has, and which holds that class's fields. In an
interface method with a body, old.name is now the entry value of a parameter or
of a property of the interface, read through its getter, so lambdas, coerced
maps and Java classes work however they store it. Any other name is a compile
error. A name used only by a postcondition inherited from a separately compiled
interface cannot be seen by the compiler; reading it throws an exception that
says so.
> groovy-contracts: @Requires and @Ensures on interface methods with a body fail
> ------------------------------------------------------------------------------
>
> Key: GROOVY-12843
> URL: https://issues.apache.org/jira/browse/GROOVY-12843
> Project: Groovy
> Issue Type: Bug
> Components: groovy-contracts
> Affects Versions: 6.0.0, 6.0.1
> Reporter: Paul King
> Priority: Major
>
> {{@Requires}} and {{@Ensures}} work on an abstract interface method, where
> the check is woven into the implementing class. On an interface method that
> has a body (a default or static method) they compile in dynamic code but
> every call fails, whether or not the condition holds. Under
> {{@CompileStatic}} they don't compile.
> {code:groovy}
> import groovy.contracts.*
> interface Limited {
> int getMax()
> @Requires({ x <= max })
> @Ensures({ result <= max })
> default int twice(int x) { x * 2 }
> }
> class Five implements Limited {
> int getMax() { 5 }
> }
> assert new Five().twice(2) == 4
> {code}
> The call fails with:
> {noformat}
> groovy.lang.MissingPropertyException: No such property: $GCONTRACTS_ENABLED
> for class: Five
> {noformat}
> With {{@CompileStatic}} on the interface, compilation fails with:
> {noformat}
> Access to Limited#$GCONTRACTS_ENABLED is forbidden
> {noformat}
> A contract expressed in terms of the method's parameters, its result and
> other interface members is well defined here: an implementer supplies the
> members, and the contract holds for whatever they return.
> h3. Cause
> The condition class is generated and the check is woven into the interface
> method, but the check is guarded by a {{$GCONTRACTS_ENABLED}} field that
> {{ClosureExpressionEvaluationASTTransformation}} adds only to classes.
> Declaring the field by hand as an interface constant makes the example work,
> but a postcondition has a second problem. Every instance method with
> {{@Ensures}} calls {{$_gc_computeOldVariables()}}, even when {{old}} isn't
> used. That method exists only on classes compiled with contracts, so an
> implementer such as a map coerced to the interface fails with a
> {{MissingMethodException}}. Where the method does exist, {{old}} holds the
> implementer's fields rather than the interface's properties, so {{old.n}} is
> null unless the implementer happens to have a field named {{n}}.
> h3. Proposed behaviour
> * {{@Requires}} and {{@Ensures}} are checked on default and static interface
> methods, in dynamic and statically compiled code. This includes calls through
> a lambda, a coerced map, an enum, a record or a separately compiled
> implementer, and methods that override the default.
> * In a postcondition of a default method, {{old.name}} is the entry value of
> a parameter, or of a property of the interface read through its getter. How
> the implementer stores the property doesn't matter.
> * Any other {{old.name}} in such a postcondition is a compile error.
> * A postcondition inherited from a separately compiled interface may use an
> {{old}} name that the compiler can't see when an interface overrides the
> method. Reading that name throws an {{IllegalStateException}} that says which
> name was not recorded and how to fix it. Inherited contracts that don't use
> {{old}} are unaffected.
> * Enabling and disabling assertions goes by the name of the interface that
> declares the method.
> h3. Compatibility
> An interface that declares a method with a body and has contracts gains a
> synthetic {{public static final}} field, {{$GCONTRACTS_ENABLED}}. Generated
> code links to one new {{@GroovyABI}} constructor,
> {{org.apache.groovy.contracts.util.OldProperties}}. Classes, traits and
> interfaces with only abstract methods are unchanged.
> h3. Not covered
> * A class that overrides a contracted interface method still takes {{old}}
> from its own fields, as it does now for abstract methods.
> * A mutable value that can't be cloned, such as a property of type {{List}},
> is recorded by reference, as parameters are now.
> * Traits. A contract on a trait method with a body is rejected since
> GROOVY-12741.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)