Skip to content

Commit

Permalink
Bogus error about return expression involving pattern matching (#1731)
Browse files Browse the repository at this point in the history
Fixes #1726
  • Loading branch information
srikanth-sankaran committed Dec 12, 2023
1 parent f8dddad commit d4e6217
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10953,12 +10953,36 @@ protected void consumeTypePattern() {
pushOnAstStack(aTypePattern);
}
protected void consumeRecordPattern() {
int length = this.astLengthPtr == -1 ? 0 : this.astLengthStack[this.astLengthPtr--];
this.astPtr -= length;

int length;
Annotation[] typeAnnotations = null;
if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
System.arraycopy(
this.expressionStack,
(this.expressionPtr -= length) + 1,
typeAnnotations = new Annotation[length],
0,
length);
}

TypeReference type = getTypeReference(0);

if (typeAnnotations != null) {
int levels = type.getAnnotatableLevels();
if (type.annotations == null)
type.annotations = new Annotation[levels][];
type.annotations[0] = typeAnnotations;
type.sourceStart = type.annotations[0][0].sourceStart;
type.bits |= ASTNode.HasTypeAnnotations;
}

int sourceEnd = this.intStack[this.intPtr--];
this.intPtr--;
RecordPattern recPattern = new RecordPattern(type, type.sourceStart, sourceEnd);

length = this.astLengthPtr == -1 ? 0 : this.astLengthStack[this.astLengthPtr--];
this.astPtr -= length;

if (length != 0) {
Pattern[] patterns = new Pattern[length];
System.arraycopy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4125,4 +4125,29 @@ public void testBug578628_4() {
"true",
compilerOptions);
}
public void testGH1726() {
if (this.complianceLevel < ClassFileConstants.JDK21)
return;
Map<String, String> compilerOptions = getCompilerOptions(true);
runConformTest(
new String[] {
"X.java",
"""
public class X {
record A(int x) {
}
public static int foo(Object a) {
return a instanceof A(int x) ? x : 1;
}
public static void main(String [] args) {
System.out.println("" + foo(new A(1234)) + foo(args));
}
}
""",
},
"12341",
compilerOptions);
}
}

0 comments on commit d4e6217

Please sign in to comment.