You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by GitBox <gi...@apache.org> on 2019/07/08 21:08:58 UTC

[GitHub] [groovy] eric-milles commented on issue #963: GROOVY-9168: emit more specific error for use of uninitialized this

eric-milles commented on issue #963:  GROOVY-9168: emit more specific error for use of uninitialized this
URL: https://github.com/apache/groovy/pull/963#issuecomment-509392526
 
 
   There is a redundant check in `Verifier.visitConstructor` (included below).  Is this the better place to do checking for uninitiualized this/super references -- instead of in `StaticVerifier`?  I think `Verifier` runs later and could catch references added by the compiler.
   
   ```java
       @Override
       public void visitConstructor(ConstructorNode node) {
           GroovyCodeVisitor checkSuper = new CodeVisitorSupport() {
               boolean firstMethodCall = true;
               String type = null;
   
               @Override
               public void visitMethodCallExpression(MethodCallExpression call) {
                   if (!firstMethodCall) return;
                   firstMethodCall = false;
                   String name = call.getMethodAsString();
                   // the name might be null if the method name is a GString for example
                   if (name == null) return;
                   if (!name.equals("super") && !name.equals("this")) return;
                   type = name;
                   call.getArguments().visit(this);
                   type = null;
               }
   
               @Override
               public void visitConstructorCallExpression(ConstructorCallExpression call) {
                   if (!call.isSpecialCall()) return;
                   type = call.getText();
                   call.getArguments().visit(this);
                   type = null;
               }
   
               @Override
               public void visitVariableExpression(VariableExpression expression) {
                   if (type == null) return;
                   String name = expression.getName();
                   if (!name.equals("this") && !name.equals("super")) return;
                   throw new RuntimeParserException("cannot reference " + name + " inside of " + type + "(....) before supertype constructor has been called", expression);
               }
           };
           Statement s = node.getCode();
           if (s == null) {
               return;
           } else {
               s.visit(new VerifierCodeVisitor(this));
           }
           s.visit(checkSuper);
       }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services