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

John Engelman commented on GROOVY-10520:
----------------------------------------

Here's a complete example using {{javap}} to view the compiled code:
{code:java}
package sample

class SimpleClosureExample {

  void doThing(Closure c) {
    c.call()
  }

  void proceed() {
    doThing { //This is a comment (this is line 10)
      // And an empty block
    }
  }
}{code}
Groovy 2.5.4, javap -l output:
{noformat}
Compiled from "SimpleClosureExample.groovy"
public final class sample.SimpleClosureExample$_proceed_closure1 extends 
groovy.lang.Closure implements org.codehaus.groovy.runtime.GeneratedClosure {
  public static transient boolean __$stMC;  public 
sample.SimpleClosureExample$_proceed_closure1(java.lang.Object, 
java.lang.Object);
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      10     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;
          0      10     1 _outerInstance   Ljava/lang/Object;
          0      10     2 _thisObject   Ljava/lang/Object;  public 
java.lang.Object doCall(java.lang.Object);
    LineNumberTable:
      line 10: 4
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0       6     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;
          0       6     1    it   Ljava/lang/Object;  public java.lang.Object 
doCall();
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      10     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;  protected 
groovy.lang.MetaClass $getStaticMetaClass();
}{noformat}
Groovy 3.0.9, javap -l output:
{noformat}
Compiled from "SimpleClosureExample.groovy"
public final class sample.SimpleClosureExample$_proceed_closure1 extends 
groovy.lang.Closure implements org.codehaus.groovy.runtime.GeneratedClosure {
  public static transient boolean __$stMC;  public 
sample.SimpleClosureExample$_proceed_closure1(java.lang.Object, 
java.lang.Object);
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      11     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;
          0      11     1 _outerInstance   Ljava/lang/Object;
          0      11     2 _thisObject   Ljava/lang/Object;  public 
java.lang.Object doCall(java.lang.Object);
    LineNumberTable:
      line 12: 5
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0       7     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;
          0       7     1    it   Ljava/lang/Object;  public java.lang.Object 
doCall();
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      11     0  this   
Lsample/SimpleClosureExample$_proceed_closure1;  protected 
groovy.lang.MetaClass $getStaticMetaClass();
}{noformat}

> Incorrect line numbers for Closure classes in AstNode
> -----------------------------------------------------
>
>                 Key: GROOVY-10520
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10520
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.10
>            Reporter: John Engelman
>            Priority: Major
>         Attachments: image-2022-03-03-19-48-26-005.png
>
>
> The line number recorded in the compiled class for {{Closure}} is incorrect 
> in certain circumstances.
>  
> {noformat}
> 1. void doThing(Closure c) {
> 2.   c.class()
> 3. }
> 4.
> 5. void go() {
> 6.   doThing { // This is a comment
> 7.     // And there is no body
> 8.   }
> 9. }{noformat}
> When grabbing the {{lineNumber}} (using javassist and loading the {{doCall}} 
> method from the {{CtClass}} for the {{{}Closure{}}}), it is reported as line 
> 8, where in Groovy 2.5.x it was reported as line 6. Removing the comments but 
> leaving just a new line between the {{{}} and {{}}} (an empty block) has the 
> same result.
> The expected outcome here should be line 6 as that's the start of the closure.
> If you modify the closure like so:
>  
> {noformat}
> 1. void doThing(Closure c) {
> 2.   c.class()
> 3. }
> 4.
> 5. void go() {
> 6.   doThing { // This is a comment
> 7.     println "hi"
> 8.   }
> 9. }{noformat}
> The line is reported as line 7. This is also incorrect as that would be the 
> {{BlockStatement}} inside the {{Closure.}}
> Trying to debug the parser, I ended up in {{GroovyParse.blockStatementOpts}} 
> which appears to be setting the {{startLine}} value incorrectly (The below 
> screen shot is from compilation of a similar class where a closure contains 
> only comments). Notice that {{start}} is marked as a line number after 
> {{end}} which can't really be true.
> !image-2022-03-03-19-48-26-005.png!
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to