From 9e75f922b17146ff78589555dfb20dd0783cffbd Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Thu, 8 Jul 2021 14:15:07 +0000 Subject: [PATCH] 8269738: AssertionError when combining pattern matching and function closure Reviewed-by: jlahoda, vromero --- .../sun/tools/javac/comp/TransPatterns.java | 2 +- .../LambdaCannotCapturePatternVariables.java | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java index a01dc81b60b..854ff1e543a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java @@ -768,7 +768,7 @@ public class TransPatterns extends TreeTranslator { VarSymbol bindingDeclared(BindingSymbol varSymbol) { VarSymbol res = parent.bindingDeclared(varSymbol); if (res == null) { - res = new VarSymbol(varSymbol.flags(), varSymbol.name, varSymbol.type, varSymbol.owner); + res = new VarSymbol(varSymbol.flags(), varSymbol.name, varSymbol.type, currentMethodSym); res.setTypeAttributes(varSymbol.getRawTypeAttributes()); hoistedVarMap.put(varSymbol, res); } diff --git a/test/langtools/tools/javac/patterns/LambdaCannotCapturePatternVariables.java b/test/langtools/tools/javac/patterns/LambdaCannotCapturePatternVariables.java index a8dadc59942..273ac999b20 100644 --- a/test/langtools/tools/javac/patterns/LambdaCannotCapturePatternVariables.java +++ b/test/langtools/tools/javac/patterns/LambdaCannotCapturePatternVariables.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8267610 + * @bug 8267610 8269738 * @summary LambdaToMethod cannot capture pattern variables. So the TransPatterns should * transform the pattern variables and symbols to normal variables and symbols. * @compile --enable-preview -source ${jdk.version} LambdaCannotCapturePatternVariables.java @@ -34,11 +34,24 @@ import java.util.function.Supplier; public class LambdaCannotCapturePatternVariables { + static Number num1 = 1; + static Number num2 = null; + static Number staticNum1 = (num1 instanceof Integer i) ? ((Supplier) () -> i).get() : null; + static Number staticNum2 = (num2 instanceof Integer i) ? ((Supplier) () -> i).get() : null; + Number instanceNum1 = (num1 instanceof Integer i) ? ((Supplier) () -> i).get() : null; + Number instanceNum2 = (num2 instanceof Integer i) ? ((Supplier) () -> i).get() : null; + public static void main(String[] args) { var testVar = new LambdaCannotCapturePatternVariables(); testVar.testInstanceOfPatternVariable(Integer.valueOf(1)); testVar.testSwitchPatternVariable(Integer.valueOf(1)); testVar.test(Integer.valueOf(1)); + assertTrue(staticNum1 != null, "staticNum1 is null unexpectedly"); + assertTrue(staticNum2 == null, "staticNum1 is not null unexpectedly"); + assertTrue(testVar.instanceNum1 != null, "instanceNum1 is null unexpectedly"); + assertTrue(testVar.instanceNum2 == null, "instanceNum2 is not null unexpectedly"); + assertTrue(staticNum1.intValue() == 1, "staticNum1.intValue() is not equal to 1"); + assertTrue(testVar.instanceNum1.intValue() == 1, "instanceNum1.intValue() is not equal to 1"); } public Integer testInstanceOfPatternVariable(Object x) { @@ -69,4 +82,9 @@ public class LambdaCannotCapturePatternVariables { return ((y instanceof Integer z) ? z : bar); })).get() : bar); } + + static void assertTrue(boolean cond, String info) { + if (!cond) + throw new AssertionError(info); + } }