[
https://issues.apache.org/jira/browse/GROOVY-12761?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18143760#comment-18143760
]
ASF GitHub Bot commented on GROOVY-12761:
-----------------------------------------
codecov-commenter commented on PR #3294:
URL: https://github.com/apache/groovy/pull/3294#issuecomment-6823610418
##
[Codecov](https://app.codecov.io/gh/apache/groovy/pull/3294?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 74.4668%. Comparing base
([`f1c463f`](https://app.codecov.io/gh/apache/groovy/commit/f1c463f950e148172ff078b8025e2270da511066?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache))
to head
([`f953ebb`](https://app.codecov.io/gh/apache/groovy/commit/f953ebb17f7304c9ebf59789229b84153a40e388?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/groovy/pull/3294?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## master #3294 +/- ##
==================================================
- Coverage 74.4693% 74.4668% -0.0025%
- Complexity 44843 44845 +2
==================================================
Files 1702 1702
Lines 152009 152010 +1
Branches 29127 29127
==================================================
- Hits 113200 113197 -3
- Misses 28399 28402 +3
- Partials 10410 10411 +1
```
| [Files with missing
lines](https://app.codecov.io/gh/apache/groovy/pull/3294?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
| Coverage Δ | |
|---|---|---|
|
[...roovy/transform/stc/StaticTypeCheckingVisitor.java](https://app.codecov.io/gh/apache/groovy/pull/3294?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Ftransform%2Fstc%2FStaticTypeCheckingVisitor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3RyYW5zZm9ybS9zdGMvU3RhdGljVHlwZUNoZWNraW5nVmlzaXRvci5qYXZh)
| `87.9689% <100.0000%> (+0.0024%)` | :arrow_up: |
... and [17 files with indirect coverage
changes](https://app.codecov.io/gh/apache/groovy/pull/3294/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test
Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests,
report on failures, and find test suite problems.
- :package: [JS Bundle
Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save
yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
> SC: code in an instance initializer block is compiled dynamically (method
> calls since 5.0.0; anonymous inner classes created there since 6.0.0)
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12761
> URL: https://issues.apache.org/jira/browse/GROOVY-12761
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 5.1.3, 6.0.0
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> In a {{@CompileStatic}} class, method calls in an instance initializer block
> are dispatched dynamically, and since 6.0.0 so is the whole of any anonymous
> inner class created in such a block. The same code in a constructor or a
> field initializer is statically compiled. 4.0.x compiles all of it statically.
> This shows up as:
> * overloads chosen from the run-time argument types instead of the static ones
> * {{IncompatibleClassChangeError}} when a closure in the block calls a method
> on a non-Groovy delegate (GROOVY-12581)
> {code:groovy}
> class V {
> String kind
> V(Object o) { kind = 'Object' }
> V(String s) { kind = 'String' }
> static Object obj() { 'x' }
> static String m(Object o) { 'Object' }
> static String m(String s) { 'String' }
> }
> interface Greeter { String greet() }
> @groovy.transform.CompileStatic
> class C {
> List<String> fromBlock = [], fromCtor = [];
> {
> fromBlock << V.m(V.obj())
> fromBlock << new V(V.obj()).kind
> fromBlock << new Greeter() { String greet() { new V(V.obj()).kind +
> V.m(V.obj()) } }.greet()
> fromBlock << { -> new V(V.obj()).kind }()
> }
> C() {
> fromCtor << V.m(V.obj())
> fromCtor << new V(V.obj()).kind
> fromCtor << new Greeter() { String greet() { new V(V.obj()).kind +
> V.m(V.obj()) } }.greet()
> fromCtor << { -> new V(V.obj()).kind }()
> }
> }
> def c = new C()
> println "block ${c.fromBlock} ctor ${c.fromCtor}"
> @groovy.transform.CompileStatic
> class D { String r; { r = new StringBuilder('a').with { append('b')
> }.toString() } }
> println new D().r
> {code}
> The constructor prints {{[Object, Object, ObjectObject, Object]}} on every
> version. The initializer block prints:
> ||Version||static call||new||anonymous class||new in a closure||{{D}}||
> |4.0.33|Object|Object|ObjectObject|Object|ab|
> |5.0.8,
> 5.1.3|*String*|Object|ObjectObject|Object|*IncompatibleClassChangeError*|
> |6.0.0-beta-3,
> 6.0.0-RC-1|*String*|Object|ObjectObject|Object|*IncompatibleClassChangeError*|
> |6.0.0-RC-3, 6.0.0,
> 6.0.1-SNAPSHOT|*String*|Object|*StringString*|Object|*IncompatibleClassChangeError*|
> h3. Cause
> {{StaticTypeCheckingVisitor.visitObjectInitializerStatements}} type checks
> the initializer blocks under a placeholder {{ConstructorNode}} (GROOVY-5450)
> that has no declaring class.
> {{StaticCompilationVisitor.isStaticallyCompiled}} therefore returns {{false}}
> for it.
> * Since GROOVY-6925 (5.0.0),
> {{StaticCompilationVisitor.visitMethodCallExpression}} returns early when
> {{isStaticallyCompiled(getEnclosingDeclaration())}} is {{false}}. The calls
> in the block get no method target, and the "Target method for method call
> expression hasn't been set" check is skipped as well. The writer then emits
> dynamic calls ({{ScriptBytecodeAdapter.invokeMethodN}}, or
> {{invokeMethodOnCurrentN}} for an implicit-this call routed to a closure
> delegate, which assumes a {{GroovyObject}} receiver and gives the
> {{IncompatibleClassChangeError}}).
> * Since GROOVY-12363 (6.0.0-RC-2), an anonymous inner class takes its
> {{STATIC_COMPILE_NODE}} flag from {{isStaticallyCompiled(enclosingMethod)}}.
> For a class created in an initializer block that is the placeholder, so the
> whole anonymous class is compiled dynamically.
> * Constructor calls written directly in the block are not affected, because
> {{StaticInvocationWriter.writeInvokeConstructor}} reads the
> {{DIRECT_METHOD_CALL_TARGET}} metadata itself. Closures in the block are not
> affected either.
> h3. Proposed fix
> Give the placeholder its declaring class:
> {code:java}
> ConstructorNode init = new ConstructorNode(0, null, null, new
> BlockStatement(node.getObjectInitializerStatements(), null));
> init.setDeclaringClass(node);
> {code}
> With this change the initializer block makes the same choices as the
> constructor in all columns above, and {{D}} prints {{ab}}. The
> {{groovy.transform.stc}}, {{org.codehaus.groovy.classgen.asm.sc}}, {{bugs}}
> and {{groovy.bugs}} test packages pass on master (6,882 tests). Two tests are
> proposed in {{ClosuresSTCTest}} (also run statically compiled by
> {{ClosuresStaticCompileTest}}): the closure-delegate case from GROOVY-12581,
> and a check that the initializer block and the constructor select the same
> overloads. Both fail without the change.
> The placeholder code is the same on {{GROOVY_5_1_X}}, {{GROOVY_6_0_X}} and
> master, so the change applies to each.
> h3. Compatibility
> This restores 4.0 behaviour, but code in an initializer block of a
> {{@CompileStatic}} class that has relied on dynamic dispatch since 5.0 will
> change. It may select a different overload, and meta-class or category
> changes will no longer apply to those calls. It deserves a COMPATIBILITY note
> on any line it goes to.
> h3. Relationship to GROOVY-12581
> GROOVY-12581 reports the {{IncompatibleClassChangeError}} symptom. PR #3107
> fixes that symptom in {{StaticInvocationWriter}} by switching to the
> {{invokeMethod}} adapter once the receiver is replaced. That is not needed
> once the calls have targets again, and on its own it leaves the calls
> dynamic. Choice of fix for 5.1.x pending team review.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)