Issue #270849 - NB shows false error for lambda passed in argument - fixed.
authorDusan Balek <dbalek@netbeans.org>
Mon, 26 Jun 2017 10:38:22 +0200
changeset 5954f453b3a76796
parent 5953 1dba5382f105
child 5955 f54cccaf6e6c
Issue #270849 - NB shows false error for lambda passed in argument - fixed.
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
test/tools/javac/lambda/LambdaConv28.java
test/tools/javac/lambda/LambdaConv28.out
     1.1 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Fri Jun 16 13:45:50 2017 +0200
     1.2 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Mon Jun 26 10:38:22 2017 +0200
     1.3 @@ -348,12 +348,28 @@
     1.4      };
     1.5  
     1.6      /**
     1.7 +     * Create a copy of Env if needed.
     1.8 +     */
     1.9 +    Env<AttrContext> copyEnvIfNeeded(JCTree tree, Env<AttrContext> env) {
    1.10 +        if (!analyzerModes.isEmpty() &&
    1.11 +                !env.info.isSpeculative &&
    1.12 +                TreeInfo.isStatement(tree)) {
    1.13 +            Env<AttrContext> analyzeEnv =
    1.14 +                    env.dup(env.tree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
    1.15 +            analyzeEnv.info.returnResult = analyzeEnv.info.returnResult != null ?
    1.16 +                    attr.new ResultInfo(analyzeEnv.info.returnResult.pkind,
    1.17 +                                        analyzeEnv.info.returnResult.pt) : null;
    1.18 +            return analyzeEnv;
    1.19 +        } else {
    1.20 +            return null;
    1.21 +        }
    1.22 +    }
    1.23 +
    1.24 +    /**
    1.25       * Analyze an AST node if needed.
    1.26       */
    1.27      void analyzeIfNeeded(JCTree tree, Env<AttrContext> env) {
    1.28 -        if (!analyzerModes.isEmpty() &&
    1.29 -                !env.info.isSpeculative &&
    1.30 -                TreeInfo.isStatement(tree)) {
    1.31 +        if (env != null) {
    1.32              JCStatement stmt = (JCStatement)tree;
    1.33              analyze(stmt, env);
    1.34          }
     2.1 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jun 16 13:45:50 2017 +0200
     2.2 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jun 26 10:38:22 2017 +0200
     2.3 @@ -728,8 +728,7 @@
     2.4      /** Derived visitor method: attribute a statement or definition tree.
     2.5       */
     2.6      public Type attribStat(JCTree tree, Env<AttrContext> env) {
     2.7 -        Env<AttrContext> analyzeEnv =
     2.8 -                env.dup(tree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
     2.9 +        Env<AttrContext> analyzeEnv = analyzer.copyEnvIfNeeded(tree, env);
    2.10          boolean baCatched = false;
    2.11          try {
    2.12              return attribTree(tree, env, statInfo);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/lambda/LambdaConv28.java	Mon Jun 26 10:38:22 2017 +0200
     3.3 @@ -0,0 +1,32 @@
     3.4 +/*
     3.5 + * @test /nodynamiccopyright/
     3.6 + * @bug 8181911
     3.7 + * @summary Verify that the analyzer does not affect ordinary compilation.
     3.8 + * @compile/ref=LambdaConv28.out -XDrawDiagnostics -XDfind=lambda LambdaConv28.java
     3.9 + */
    3.10 +
    3.11 +class LambdaConv28 {
    3.12 +
    3.13 +    public void test(A a) {
    3.14 +        test(()-> {
    3.15 +            return new I() {
    3.16 +                public <T> void t() {
    3.17 +                }
    3.18 +            };
    3.19 +        });
    3.20 +        test(new A() {
    3.21 +            public I get() {
    3.22 +                return null;
    3.23 +            }
    3.24 +        });
    3.25 +    }
    3.26 +
    3.27 +    public interface I {
    3.28 +        public <T> void t();
    3.29 +    }
    3.30 +
    3.31 +    public interface A {
    3.32 +        public I get();
    3.33 +    }
    3.34 +
    3.35 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/lambda/LambdaConv28.out	Mon Jun 26 10:38:22 2017 +0200
     4.3 @@ -0,0 +1,2 @@
     4.4 +LambdaConv28.java:17:22: compiler.warn.potential.lambda.found
     4.5 +1 warning