From 3a4b8a428f8a4b35de852abb72dd0cede3c399da Mon Sep 17 00:00:00 2001 From: Srikanth Sankaran <131454720+srikanth-sankaran@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:19:07 +0530 Subject: [PATCH] Apply the boxing conversion post instanceof where required (#2105) * Fixes https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2104 --- .../compiler/ast/InstanceOfExpression.java | 9 ++++-- .../regression/PatternMatching16Test.java | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java index 08f0a91435c..06972fc1fbc 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java @@ -128,9 +128,14 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean codeStream.iconst_0(); continueLabel.place(); - } else if (!valueRequired) { - codeStream.pop(); } + + if (valueRequired) { + codeStream.generateImplicitConversion(this.implicitConversion); + } else if (this.pattern == null) { + codeStream.pop(); // in the pattern case, if (!valueRequired), we have nothing on the stack to pop + } + codeStream.recordPositionsFrom(codeStream.position, this.sourceEnd); } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java index faf060c541b..9c9ad554257 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java @@ -4812,4 +4812,32 @@ public static void main(String [] args) { "for\n" + "!for"); } + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2104 + // [Patterns] Missing boxing conversion after instanceof leads to verify error + public void testBoxing() { + runConformTest( + new String[] { + "X.java", + """ + public class X { + + public static void foo(Boolean b) { + System.out.println(b); + } + + public static void main(String [] args) { + Object o = new Object(); + foo(o instanceof String); + foo("Hello" instanceof String); + foo(o instanceof String s); + foo("Hello" instanceof String s); + } + } + """, + }, + "false\n" + + "true\n" + + "false\n" + + "true"); + } }