Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk19 Public archive

Commit

Permalink
8288120: VerifyError with JEP 405 pattern match
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jun 24, 2022
1 parent b0db333 commit bdf9902
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
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;

/**
Expand Down Expand Up @@ -284,7 +285,7 @@ public void visitRecordPattern(JCRecordPattern tree) {
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),
Expand Down Expand Up @@ -344,12 +345,8 @@ public void visitRecordPattern(JCRecordPattern tree) {
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(),
Expand All @@ -359,7 +356,7 @@ private MethodSymbol getAccessor(RecordComponent component) {
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,
Expand Down
47 changes: 47 additions & 0 deletions test/langtools/tools/javac/patterns/ProxyMethodLookup.java
Original file line number Diff line number Diff line change
@@ -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 {}
}

1 comment on commit bdf9902

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.