Skip to content

Commit a8385fe

Browse files
committed
8269354: javac crashes when processing parenthesized pattern in instanceof
Reviewed-by: vromero
1 parent c16d1fc commit a8385fe

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void visitTypeTest(JCInstanceOf tree) {
190190
syms.objectType
191191
: tree.expr.type;
192192
VarSymbol prevCurrentValue = currentValue;
193+
bindingContext = new BasicBindingContext();
193194
try {
194195
JCExpression translatedExpr = translate(tree.expr);
195196
Symbol exprSym = TreeInfo.symbol(translatedExpr);
@@ -206,16 +207,20 @@ public void visitTypeTest(JCInstanceOf tree) {
206207
}
207208

208209
Type principalType = principalType((JCPattern) tree.pattern);
209-
result = makeBinary(Tag.AND,
210-
makeTypeTest(make.Ident(currentValue), make.Type(principalType)),
211-
(JCExpression) this.<JCTree>translate(tree.pattern));
210+
JCExpression resultExpression=
211+
makeBinary(Tag.AND,
212+
makeTypeTest(make.Ident(currentValue), make.Type(principalType)),
213+
(JCExpression) this.<JCTree>translate(tree.pattern));
212214
if (currentValue != exprSym) {
213-
result = make.at(tree.pos).LetExpr(make.VarDef(currentValue, translatedExpr),
214-
(JCExpression)result).setType(syms.booleanType);
215-
((LetExpr) result).needsCond = true;
215+
resultExpression =
216+
make.at(tree.pos).LetExpr(make.VarDef(currentValue, translatedExpr),
217+
resultExpression).setType(syms.booleanType);
218+
((LetExpr) resultExpression).needsCond = true;
216219
}
220+
result = bindingContext.decorateExpression(resultExpression);
217221
} finally {
218222
currentValue = prevCurrentValue;
223+
bindingContext.pop();
219224
}
220225
} else {
221226
super.visitTypeTest(tree);

test/langtools/tools/javac/patterns/Parenthesized.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8262891
26+
* @bug 8262891 8269354
2727
* @summary Test parenthesized pattern
2828
* @compile --enable-preview -source ${jdk.version} Parenthesized.java
2929
* @run main/othervm --enable-preview Parenthesized
@@ -46,6 +46,8 @@ void run() {
4646
if (o instanceof (String s && s.isEmpty())) {
4747
System.err.println("OK: " + s);
4848
}
49+
boolean b1 = o instanceof (String s && s.isEmpty());
50+
boolean b2 = o instanceof String s && s.isEmpty();
4951
}
5052

5153
}

0 commit comments

Comments
 (0)