8288120: VerifyError with JEP 405 pattern match

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2022-06-24 08:13:28 +00:00
parent b0db33333a
commit bdf9902f75
2 changed files with 51 additions and 7 deletions

View File

@ -97,6 +97,7 @@ import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
import com.sun.tools.javac.tree.JCTree.LetExpr;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
/**
@ -284,7 +285,7 @@ public class TransPatterns extends TreeTranslator {
names.fromString(target.syntheticNameChar() + "c" + target.syntheticNameChar() + component.name),
component.erasure(types),
currentMethodSym);
Symbol accessor = getAccessor(component);
Symbol accessor = getAccessor(tree.pos(), component);
JCVariableDecl nestedTempVar =
make.VarDef(nestedTemp,
make.App(make.QualIdent(accessor),
@ -344,12 +345,8 @@ public class TransPatterns extends TreeTranslator {
result = test != null ? test : makeLit(syms.booleanType, 1);
}
private MethodSymbol getAccessor(RecordComponent component) {
private MethodSymbol getAccessor(DiagnosticPosition pos, RecordComponent component) {
return component2Proxy.computeIfAbsent(component, c -> {
MethodSymbol realAccessor = (MethodSymbol) component.owner
.members()
.findFirst(component.name, s -> s.kind == Kind.MTH &&
((MethodSymbol) s).params.isEmpty());
MethodType type = new MethodType(List.of(component.owner.erasure(types)),
types.erasure(component.type),
List.nil(),
@ -359,7 +356,7 @@ public class TransPatterns extends TreeTranslator {
type,
currentClass);
JCStatement accessorStatement =
make.Return(make.App(make.Select(make.Ident(proxy.params().head), realAccessor)));
make.Return(make.App(make.Select(make.Ident(proxy.params().head), c.accessor)));
VarSymbol ctch = new VarSymbol(Flags.SYNTHETIC,
names.fromString("catch" + currentClassTree.pos + target.syntheticNameChar()),
syms.throwableType,

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8288120
* @summary Verify an appropriate accessor method is looked up.
* @compile --enable-preview -source ${jdk.version} ProxyMethodLookup.java
* @run main/othervm --enable-preview ProxyMethodLookup
*/
public class ProxyMethodLookup {
public static void main(String[] args) {
Object val = new R(new Component());
boolean b = val instanceof R(var c);
}
interface ComponentBase {}
record Component() implements ComponentBase {}
sealed interface Base {
ComponentBase c();
}
record R(Component c) implements Base {}
}