Skip to content

Commit 473df67

Browse files
committed
[WIP] Handle MatchCandidate properly
1 parent 12b901c commit 473df67

File tree

22 files changed

+229
-162
lines changed

22 files changed

+229
-162
lines changed

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,7 @@ public MethodHandle unreflectDeconstructor(Deconstructor<?> d) throws IllegalAcc
33623362
return unreflect(
33633363
ownerType.getDeclaredMethod(
33643364
SharedSecrets.getJavaLangReflectAccess().getMangledName(d),
3365+
ownerType,
33653366
ownerType
33663367
)
33673368
);

src/java.base/share/classes/java/lang/reflect/MemberPattern.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public Object[] invoke(Object matchCandidate)
401401
String underlyingName = getMangledName();
402402

403403
try {
404-
Method method = getDeclaringClass().getDeclaredMethod(underlyingName, matchCandidate.getClass());
404+
Method method = getDeclaringClass().getDeclaredMethod(underlyingName, matchCandidate.getClass(), matchCandidate.getClass());
405405
method.setAccessible(override);
406406
return (Object[])Carriers.boxedComponentValueArray(
407407
MethodType.methodType(
@@ -411,7 +411,7 @@ public Object[] invoke(Object matchCandidate)
411411
.toArray(Class[]::new)
412412
)
413413
).invoke(
414-
method.invoke(matchCandidate, matchCandidate)
414+
method.invoke(matchCandidate, matchCandidate, matchCandidate)
415415
);
416416
} catch (Throwable e) {
417417
throw new MatchException(e.getMessage(), e);

src/jdk.compiler/share/classes/com/sun/source/tree/MethodTree.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ public interface MethodTree extends Tree {
9797
*/
9898
VariableTree getReceiverParameter();
9999

100+
/**
101+
* Return an explicit match candidate parameter ("that" parameter),
102+
* or {@code null} if none.
103+
*
104+
* @return an explicit match candidate parameter ("that" parameter)
105+
* @since 25
106+
*/
107+
VariableTree getMatchCandidateParameter();
108+
100109
/**
101110
* Returns the exceptions listed as being thrown by this method.
102111
* @return the exceptions

src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public R visitMethod(MethodTree node, P p) {
220220
r = scanAndReduce(node.getParameters(), p, r);
221221
r = scanAndReduce(node.getBindings(), p, r);
222222
r = scanAndReduce(node.getReceiverParameter(), p, r);
223+
r = scanAndReduce(node.getMatchCandidateParameter(), p, r);
223224
r = scanAndReduce(node.getThrows(), p, r);
224225
r = scanAndReduce(node.getBody(), p, r);
225226
r = scanAndReduce(node.getDefaultValue(), p, r);

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public Type visitPatternType(PatternType t, S s) {
318318

319319
if (bindingtypes1 == bindingtypes) return t;
320320
else {
321-
PatternType patternType = new PatternType(bindingtypes1, erasedBindingTypes1, /*XXX*/t.restype, t.tsym) {
321+
PatternType patternType = new PatternType(bindingtypes1, erasedBindingTypes1, /*XXX*/t.restype, t.matchcandidatetype, t.tsym) {
322322
@Override
323323
protected boolean needsStripping() {
324324
return true;
@@ -618,6 +618,7 @@ public String argtypes(boolean varargs) {
618618
public List<Type> getBindingTypes() { return List.nil(); }
619619
public Type getReturnType() { return null; }
620620
public Type getReceiverType() { return null; }
621+
public Type getMatchCandidateType() { return null; }
621622
public List<Type> getThrownTypes() { return List.nil(); }
622623
public Type getUpperBound() { return null; }
623624
public Type getLowerBound() { return null; }
@@ -1863,6 +1864,7 @@ public DelegatedType(TypeTag tag, Type qtype,
18631864
public List<Type> getBindingTypes() { return qtype.getBindingTypes(); }
18641865
public Type getReturnType() { return qtype.getReturnType(); }
18651866
public Type getReceiverType() { return qtype.getReceiverType(); }
1867+
public Type getMatchCandidateType() { return qtype.getMatchCandidateType(); }
18661868
public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
18671869
public List<Type> allparams() { return qtype.allparams(); }
18681870
public Type getUpperBound() { return qtype.getUpperBound(); }
@@ -1940,14 +1942,17 @@ public static class PatternType extends Type implements ExecutableType {
19401942
public List<Type> erasedBindingTypes;
19411943

19421944
public Type restype;
1945+
public Type matchcandidatetype;
19431946

19441947
public PatternType(List<Type> bindingtypes,
19451948
List<Type> erasedBindingTypes,
19461949
Type restype, //TODO:
1950+
Type matchcandidatetype,
19471951
TypeSymbol methodClass) {
19481952
super(methodClass, List.nil());
19491953
this.bindingtypes = bindingtypes;
19501954
this.erasedBindingTypes = erasedBindingTypes;
1955+
this.matchcandidatetype = matchcandidatetype;
19511956
this.restype = restype;
19521957
}
19531958

@@ -1988,6 +1993,10 @@ public Type getReceiverType() {
19881993
return Type.noType;
19891994
}
19901995
@DefinedBy(Api.LANGUAGE_MODEL)
1996+
public Type getMatchCandidateType() {
1997+
return (matchcandidatetype == null) ? Type.noType : matchcandidatetype;
1998+
}
1999+
@DefinedBy(Api.LANGUAGE_MODEL)
19912000
public List<Type> getThrownTypes() { return List.nil(); }
19922001

19932002
@Override

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ public void visitMethodDef(JCMethodDecl tree) {
11251125
scan(tree.restype);
11261126
scan(tree.typarams);
11271127
scan(tree.recvparam);
1128+
scan(tree.matchcandparam);
11281129
scan(tree.params);
11291130
scan(tree.thrown);
11301131
scan(tree.defaultValue);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,10 @@ public void visitMethodDef(JCMethodDecl tree) {
10471047
tree.recvparam.pos(),
10481048
Errors.IntfAnnotationMembersCantHaveParams);
10491049

1050+
if (tree.sym.isPattern()) {
1051+
attribStat(tree.matchcandparam, localEnv);
1052+
}
1053+
10501054
// Attribute all value parameters.
10511055
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
10521056
attribStat(l.head, localEnv);
@@ -4452,7 +4456,10 @@ public void visitRecordPattern(JCRecordPattern tree) {
44524456
if (tree.patternDeclaration != null && tree.patternDeclaration.getParameters().size() == 1) { // TODO: improve error recovery
44534457
tree.type = tree.deconstructor.type = tree.patternDeclaration.getParameters().head.type;
44544458
} else {
4455-
tree.type = tree.deconstructor.type = uncapturedSite;
4459+
if (tree.deconstructor != null) {
4460+
tree.deconstructor.type = uncapturedSite;
4461+
}
4462+
tree.type = uncapturedSite;
44564463
}
44574464
chk.validate(tree.deconstructor, env, true);
44584465
result = tree.type;
@@ -4699,7 +4706,7 @@ private List<MethodSymbol> candidatesWithArity(Type site, Name deconstructorName
46994706
.stream()
47004707
.map(rc -> types.erasure(rc.type))
47014708
.collect(List.collector());
4702-
PatternType pt = new PatternType(recordComponents, erasedComponents, syms.voidType, syms.methodClass);
4709+
PatternType pt = new PatternType(recordComponents, erasedComponents, syms.voidType, site, syms.methodClass);
47034710

47044711
MethodSymbol synthesized = new MethodSymbol(PUBLIC | SYNTHETIC | PATTERN, ((ClassSymbol) site.tsym).name, pt, site.tsym);
47054712

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,14 +2648,18 @@ public void visitMethodDef(JCMethodDecl tree) {
26482648
implicitThisParam.mods.flags |= SYNTHETIC;
26492649
implicitThisParam.sym.flags_field |= SYNTHETIC;
26502650

2651+
JCVariableDecl implicitThatParam = tree.getMatchCandidateParameter();
2652+
implicitThisParam.mods.flags |= SYNTHETIC;
2653+
implicitThisParam.sym.flags_field |= SYNTHETIC;
2654+
26512655
MethodSymbol m = tree.sym;
2652-
tree.params = tree.params.prepend(implicitThisParam);
2656+
tree.params = tree.params.prepend(implicitThatParam).prepend(implicitThisParam);
26532657

2654-
m.params = m.params.prepend(implicitThisParam.sym);
2658+
m.params = m.params.prepend(implicitThatParam.sym).prepend(implicitThisParam.sym);
26552659
Type olderasure = m.erasure(types);
26562660
//create an external type for the pattern:
26572661
var mt = new MethodType(
2658-
olderasure.getParameterTypes().prepend(tree.sym.owner.type),
2662+
olderasure.getParameterTypes().prepend(tree.matchcandparam.type).prepend(tree.sym.owner.type),
26592663
olderasure.getReturnType(),
26602664
olderasure.getThrownTypes(),
26612665
syms.methodClass);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Type signature(MethodSymbol msym,
106106
List<JCVariableDecl> bindings,
107107
JCTree res,
108108
JCVariableDecl recvparam,
109+
JCVariableDecl matchcandidateparam,
109110
List<JCExpression> thrown,
110111
Env<AttrContext> env) {
111112

@@ -144,6 +145,15 @@ Type signature(MethodSymbol msym,
144145
recvtype = null;
145146
}
146147

148+
// Attribute match candidate type, if one is given.
149+
Type matchcandidatetype;
150+
if (matchcandidateparam!=null) {
151+
memberEnter(matchcandidateparam, env);
152+
matchcandidatetype = matchcandidateparam.vartype.type;
153+
} else {
154+
matchcandidatetype = null;
155+
}
156+
147157
// Attribute thrown exceptions.
148158
ListBuffer<Type> thrownbuf = new ListBuffer<>();
149159
for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
@@ -164,11 +174,13 @@ Type signature(MethodSymbol msym,
164174
.map(b -> types.erasure(b))
165175
.collect(List.collector());
166176

167-
PatternType patternType = new PatternType(bindingsbuf.toList(), erasedBindingTypes, restype, syms.methodClass);
177+
PatternType patternType = new PatternType(bindingsbuf.toList(), erasedBindingTypes, restype, matchcandidatetype, syms.methodClass);
168178

169179
return patternType;
170180
} else {
171181
Assert.check(bindings == null);
182+
Assert.check(matchcandidateparam == null);
183+
172184
MethodType mtype = new MethodType(argbuf.toList(),
173185
restype,
174186
thrownbuf.toList(),
@@ -226,7 +238,7 @@ public void visitMethodDef(JCMethodDecl tree) {
226238
try {
227239
// Compute the method type
228240
Type t = signature(m, tree.typarams, tree.params, tree.bindings,
229-
tree.restype, tree.recvparam,
241+
tree.restype, tree.recvparam, tree.matchcandparam,
230242
tree.thrown,
231243
localEnv);
232244
m.type = t;

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ public void visitMethodDef(JCMethodDecl tree) {
481481
tree.typarams = List.nil();
482482
tree.params = translateVarDefs(tree.params);
483483
tree.recvparam = translate(tree.recvparam, null);
484+
tree.matchcandparam = translate(tree.matchcandparam, null);
484485
tree.thrown = translate(tree.thrown, null);
485486
tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
486487
tree.type = erasure(tree.type);

0 commit comments

Comments
 (0)