[ 
https://issues.apache.org/jira/browse/GROOVY-12845?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152527#comment-18152527
 ] 

ASF GitHub Bot commented on GROOVY-12845:
-----------------------------------------

codecov-commenter commented on PR #3377:
URL: https://github.com/apache/groovy/pull/3377#issuecomment-7252933086

   ## 
[Codecov](https://app.codecov.io/gh/apache/groovy/pull/3377?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 75.2892%. Comparing base 
([`494d161`](https://app.codecov.io/gh/apache/groovy/commit/494d161bb13d832e65136174312ea6609867d869?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache))
 to head 
([`3bfc7d1`](https://app.codecov.io/gh/apache/groovy/commit/3bfc7d17ed45660191a093d9bbe34ee934af4c48?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>
   
   
   
   [![Impacted file tree 
graph](https://app.codecov.io/gh/apache/groovy/pull/3377/graphs/tree.svg?width=650&height=150&src=pr&token=1r45138NfQ&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/groovy/pull/3377?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@                Coverage Diff                 @@
   ##               master      #3377        +/-   ##
   ==================================================
   + Coverage     75.2860%   75.2892%   +0.0032%     
   - Complexity      49967      49975         +8     
   ==================================================
     Files            1775       1775                
     Lines          163660     163661         +1     
     Branches        32301      32301                
   ==================================================
   + Hits           123213     123219         +6     
   - Misses          28947      28949         +2     
   + Partials        11500      11493         -7     
   ```
   
   | [Files with missing 
lines](https://app.codecov.io/gh/apache/groovy/pull/3377?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 | Coverage Δ | |
   |---|---|---|
   | 
[...java/org/codehaus/groovy/classgen/ReturnAdder.java](https://app.codecov.io/gh/apache/groovy/pull/3377?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2FReturnAdder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL1JldHVybkFkZGVyLmphdmE=)
 | `91.6667% <100.0000%> (+0.0700%)` | :arrow_up: |
   
   ... and [6 files with indirect coverage 
changes](https://app.codecov.io/gh/apache/groovy/pull/3377/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>




> Compiler crash: early yield in a return-position arrow switch with @Ensures 
> or a class @Invariant
> -------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12845
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12845
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-contracts
>    Affects Versions: 6.0.1
>            Reporter: Paul King
>            Priority: Major
>
> A {{yield}} that doesn't end its arm, in an arrow switch in return position, 
> crashes the compiler if the method has {{@Ensures}} or its class has 
> {{@Invariant}}.
> {code:groovy}
> import groovy.contracts.*
> class A {
>     @Ensures({ result > 0 })
>     int f(int x) {
>         switch (x) {
>             case 1 -> { if (x > 0) yield 1; yield 2 }
>             default -> 3
>         }
>     }
> }
> assert new A().f(1) == 1
> {code}
> {noformat}
> BUG! exception in phase 'class generation' in source unit 'Script1.groovy' 
> yield lowered for a switch in return position no longer ends with its value 
> and break
>     at 
> org.codehaus.groovy.classgen.ReturnAdder$1.visitBlockStatement(ReturnAdder.java:244)
> {noformat}
> It happens in dynamic and statically compiled code. With {{@CompileStatic}} 
> or a {{def}} return type, the phase is 'instruction selection'.
> ||Same method with||6.0.0||6.0.1 and master||
> |{{@Ensures}} or a class {{@Invariant}}|compiles, f(1) is 2 (wrong)|compiler 
> crash|
> |{{@Requires}}, {{@ThrowsIf}} or {{@Decreases}} only|compiles, f(1) is 2 
> (wrong)|1|
> |no contract|compiles, f(1) is 2 (wrong)|1|
> |{{@Ensures}}, written as {{return switch (x) ...}}|1|1|
> So GROOVY-12478 fixed the wrong value, but not for a method whose returns 
> groovy-contracts rewrites. Arms whose yields are all in tail position, such 
> as an {{if}}/{{else}} where both branches yield, are not affected.
> h3. Workaround
> Write {{return switch (x) ...}}.
> h3. Cause
> {{ReturnAdder}} visits the method twice. groovy-contracts runs it first, from 
> {{AssertStatementCreationUtility.getReturnStatements}}. That visit replaces 
> the value and {{break}} of the lowered yield with a return, but leaves the 
> {{YieldStatement}} marker on the block. The contract check is then woven 
> around that return. When the compiler runs {{ReturnAdder}} later, 
> {{addReturnsForEarlyYields}} finds the marked block again. It no longer ends 
> with a value and {{break}}, so the {{GroovyBugError}} is thrown.
> h3. Possible fix
> Remove the marker when the block is converted:
> {code:java}
> if (doAdd) {
>     statements.remove(n - 1);
>     statements.set(n - 2, returnStatement);
>     block.removeNodeMetaData(YieldStatement.class);
> }
> {code}
> With this change on master, all the crashing forms above compile and return 
> 1, and a postcondition violated by the early value is reported.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to