changeset 52753:531a0335dc49 concise-method-declarations

fixing assertion during parsing
author vromero
date Mon, 01 Oct 2018 16:54:51 -0700
parents 5e0bd3dc812b
children fbed49487ec4
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.java test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.out
diffstat 8 files changed, 39 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Oct 01 16:54:51 2018 -0700
@@ -1112,6 +1112,9 @@
                 // Attribute method body.
                 if (tree.conciseMethodRef != null) {
                     localEnv.info.conciseMethod = tree;
+                    if (!tree.conciseMethodRef.hasTag(REFERENCE)) {
+                        log.error(tree.conciseMethodRef, Errors.OnlyMethodReferencesAllowed);
+                    }
                     attribTree(tree.conciseMethodRef, localEnv, statInfo);
                 } else {
                     attribStat(tree.body, localEnv);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Mon Oct 01 16:54:51 2018 -0700
@@ -2536,36 +2536,37 @@
                     JCFieldAccess qualifier = null;
                     JCMethodInvocation apply = null;
                     JCExpression expr = null;
-                    switch (tree.conciseMethodRef.kind) {
+                    JCMemberReference reference = (JCMemberReference)tree.conciseMethodRef;
+                    switch (reference.kind) {
                         case STATIC:
-                            qualifier = make.Select(make.QualIdent(tree.conciseMethodRef.expr.type.tsym), tree.conciseMethodRef.name);
+                            qualifier = make.Select(make.QualIdent(reference.expr.type.tsym), reference.name);
                             apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, make.Idents(tree.params));
                             break;
                         case UNBOUND:
-                            qualifier = make.Select(make.QualIdent(tree.params.head.sym), tree.conciseMethodRef.name);
+                            qualifier = make.Select(make.QualIdent(tree.params.head.sym), reference.name);
                             apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, List.nil());
                             break;
                         case BOUND: case SUPER:
-                            qualifier = make.Select(make.QualIdent(TreeInfo.symbol(tree.conciseMethodRef.expr)), tree.conciseMethodRef.name);
+                            qualifier = make.Select(make.QualIdent(TreeInfo.symbol(reference.expr)), reference.name);
                             apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, make.Idents(tree.params));
                             break;
                         case TOPLEVEL: case IMPLICIT_INNER:
-                            expr = makeNewClass(tree.conciseMethodRef.expr.type, make.Idents(tree.params));
+                            expr = makeNewClass(reference.expr.type, make.Idents(tree.params));
                             break;
                         case ARRAY_CTOR:
                             expr = make.NewArray(
-                                    make.Type(types.elemtype(tree.conciseMethodRef.expr.type)),
+                                    make.Type(types.elemtype(reference.expr.type)),
                                     List.of(make.Ident(tree.params.head.sym)), null)
-                                    .setType(new ArrayType(types.elemtype(tree.conciseMethodRef.expr.type), syms.arrayClass));
+                                    .setType(new ArrayType(types.elemtype(reference.expr.type), syms.arrayClass));
                             break;
                         default:
-                            qualifier = make.Select(make.QualIdent(tree.params.head.sym), tree.conciseMethodRef.name);
+                            qualifier = make.Select(make.QualIdent(tree.params.head.sym), reference.name);
                             apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, List.nil());
                     }
-                    switch (tree.conciseMethodRef.kind) {
+                    switch (reference.kind) {
                         case STATIC: case UNBOUND: case BOUND: case SUPER:
                             qualifier.type = tree.type;
-                            qualifier.sym = tree.conciseMethodRef.sym;
+                            qualifier.sym = reference.sym;
                             apply.setType(tree.type.asMethodType().restype);
                             expr = apply;
                             break;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Oct 01 16:54:51 2018 -0700
@@ -3933,7 +3933,7 @@
             }
             JCBlock body = null;
             JCExpression defaultValue = null;
-            JCMemberReference conciseMethodRef = null;
+            JCExpression conciseMethodRef = null;
             if (token.kind == LBRACE) {
                 body = block();
             } else if (token.kind == ARROW || token.kind == EQ) {
@@ -3951,8 +3951,7 @@
                         body = F.at(bodyPos).Block(0, List.of(exprStm));
                     }
                 } else {
-                    Assert.check(expr.hasTag(REFERENCE));
-                    conciseMethodRef = (JCMemberReference)expr;
+                    conciseMethodRef = expr;
                     body = F.at(bodyPos).Block(0, List.nil());
                 }
                 mods.flags |= Flags.CONCISE;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Oct 01 16:54:51 2018 -0700
@@ -2023,14 +2023,6 @@
 compiler.warn.potential.lambda.found=\
     This anonymous inner class creation can be turned into a lambda expression.
 
-# 0: string
-compiler.note.potential.concise.method.found=\
-    This method''s body: {0}\n\
-    can be turned into a concise method
-
-compiler.warn.potential.concise.method.found=\
-    This method''s body can be turned into a concise method
-
 compiler.warn.method.redundant.typeargs=\
     Redundant type arguments in method call.
 
@@ -3468,3 +3460,16 @@
 
 compiler.err.preview.without.source.or.release=\
     --enable-preview must be used with either -source or --release
+
+# concise methods
+
+# 0: string
+compiler.note.potential.concise.method.found=\
+    This method''s body: {0}\n\
+    can be turned into a concise method
+
+compiler.warn.potential.concise.method.found=\
+    This method''s body can be turned into a concise method
+
+compiler.err.only.method.references.allowed=\
+    the expression after the '=' in a concise method must be a method reference
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Oct 01 16:54:51 2018 -0700
@@ -847,7 +847,7 @@
         /** is this a concise method? reserved for those concise methods which are just wrappers
          *  around a method reference
          */
-        public JCMemberReference conciseMethodRef;
+        public JCExpression conciseMethodRef;
 
         protected JCMethodDecl(JCModifiers mods,
                             Name name,
@@ -859,7 +859,7 @@
                             JCBlock body,
                             JCExpression defaultValue,
                             MethodSymbol sym,
-                            JCMemberReference conciseMethodRef)
+                            JCExpression conciseMethodRef)
         {
             this.mods = mods;
             this.name = name;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Mon Oct 01 16:54:51 2018 -0700
@@ -194,7 +194,7 @@
                                List<JCExpression> thrown,
                                JCBlock body,
                                JCExpression defaultValue,
-                               JCMemberReference conciseMethodRef) {
+                               JCExpression conciseMethodRef) {
         return MethodDef(
                 mods, name, restype, typarams, null, params,
                 thrown, body, defaultValue, conciseMethodRef);
@@ -234,7 +234,7 @@
                                List<JCExpression> thrown,
                                JCBlock body,
                                JCExpression defaultValue,
-                               JCMemberReference conciseMethodRef)
+                               JCExpression conciseMethodRef)
     {
         JCMethodDecl tree = new JCMethodDecl(mods,
                                        name,
--- a/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.java	Fri Sep 28 10:24:36 2018 -0700
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.java	Mon Oct 01 16:54:51 2018 -0700
@@ -16,4 +16,8 @@
     }
 
     ConciseMethodsNegTest01() = ConciseMethodsNegTest01::make;
+
+    private static void all() {}
+
+    public static void foo() = ConciseMethodsNegTest01.all();
 }
--- a/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.out	Fri Sep 28 10:24:36 2018 -0700
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.out	Mon Oct 01 16:54:51 2018 -0700
@@ -3,4 +3,5 @@
 ConciseMethodsNegTest01.java:10:18: compiler.err.abstract.meth.cant.have.body
 ConciseMethodsNegTest01.java:12:16: compiler.err.native.meth.cant.have.body
 ConciseMethodsNegTest01.java:18:5: compiler.err.constructors.cant.have.concise.body
-5 errors
+ConciseMethodsNegTest01.java:22:59: compiler.err.only.method.references.allowed
+6 errors