diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java index 6e15f5acf5b..c6f604b4524 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java @@ -2998,7 +2998,7 @@ public class Lower extends TreeTranslator { boxIfNeeded(make.Ident(dollar_s), types.unboxedType(tree.expr.type))); } else { exactnessCheck = make.at(tree.pos()) - .TypeTest(tree.expr, make.Type(types.boxedClass(tree.pattern.type).type)) + .TypeTest(make.Ident(dollar_s), make.Type(types.boxedClass(tree.pattern.type).type)) .setType(syms.booleanType); } diff --git a/test/langtools/jdk/jshell/PrimitiveInstanceOfTest.java b/test/langtools/jdk/jshell/PrimitiveInstanceOfTest.java index 6a12392ffd9..f967b14d280 100644 --- a/test/langtools/jdk/jshell/PrimitiveInstanceOfTest.java +++ b/test/langtools/jdk/jshell/PrimitiveInstanceOfTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, 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 @@ -22,7 +22,7 @@ */ /* * @test - * @bug 8304487 + * @bug 8304487 8325257 * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) * @build KullaTesting TestingInputStream * @run testng PrimitiveInstanceOfTest @@ -49,6 +49,17 @@ public class PrimitiveInstanceOfTest extends KullaTesting { assertEval("i instanceof Number"); } + public void testInstanceOfObjectToPrimitive() { + assertEval("Object o = 1L;"); + assertEval("o instanceof long"); + assertEval("o instanceof Long"); + } + + public void testInstanceOfPrimitiveToPrimitiveInvokingExactnessMethod() { + assertEval("int b = 1024;"); + assertEval("b instanceof byte"); + } + @org.testng.annotations.BeforeMethod public void setUp() { super.setUp(bc -> bc.compilerOptions("--source", System.getProperty("java.specification.version"), "--enable-preview").remoteVMOptions("--enable-preview")); diff --git a/test/langtools/tools/javac/patterns/PrimitiveInstanceOfTypeComparisonOp.java b/test/langtools/tools/javac/patterns/PrimitiveInstanceOfTypeComparisonOp.java index 352d2a8a851..56f8ee30770 100644 --- a/test/langtools/tools/javac/patterns/PrimitiveInstanceOfTypeComparisonOp.java +++ b/test/langtools/tools/javac/patterns/PrimitiveInstanceOfTypeComparisonOp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, 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 @@ -23,15 +23,17 @@ /* * @test - * @bug 8304487 + * @bug 8304487 8325257 * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) * @enablePreview * @compile PrimitiveInstanceOfTypeComparisonOp.java * @run main/othervm PrimitiveInstanceOfTypeComparisonOp */ public class PrimitiveInstanceOfTypeComparisonOp { + public static final int qualI = 42; public static void main(String[] args) { + assertEquals(true, qualifiedExprConversion()); assertEquals(true, identityPrimitiveConversion()); assertEquals(true, wideningPrimitiveConversion()); assertEquals(true, narrowingPrimitiveConversion()); @@ -51,6 +53,10 @@ public class PrimitiveInstanceOfTypeComparisonOp { assertEquals(true, exprStaticallyQualified()); } + public static boolean qualifiedExprConversion() { + return PrimitiveInstanceOfTypeComparisonOp.qualI instanceof int; + } + public static boolean identityPrimitiveConversion() { int i = 42; return i instanceof int;