1 /* 2 * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package runtime.valhalla.inlinetypes; 24 25 26 import jdk.test.lib.Asserts; 27 import jdk.internal.vm.annotation.LooselyConsistentValue; 28 import jdk.internal.vm.annotation.ImplicitlyConstructible; 29 import jdk.internal.vm.annotation.NullRestricted; 30 31 /* 32 * @test 33 * @summary Flattenable field semantic test 34 * @library /test/lib 35 * @modules java.base/jdk.internal.vm.annotation 36 * @enablePreview 37 * @compile FlattenableSemanticTest.java 38 * @run main/othervm -XX:InlineFieldMaxFlatSize=64 runtime.valhalla.inlinetypes.FlattenableSemanticTest 39 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:ForceNonTearable=* runtime.valhalla.inlinetypes.FlattenableSemanticTest 40 */ 41 public class FlattenableSemanticTest { 42 43 @ImplicitlyConstructible 44 @LooselyConsistentValue 45 static value class Point { 46 final int x = 0; 47 final int y = 0; 48 } 49 50 @ImplicitlyConstructible 51 @LooselyConsistentValue 52 public value class JumboInline { 53 final long l0 = 0; 54 final long l1 = 1; 55 final long l2 = 2; 56 final long l3 = 3; 57 final long l4 = 4; 58 final long l5 = 5; 59 final long l6 = 6; 60 final long l7 = 7; 61 final long l8 = 8; 62 final long l9 = 9; 63 final long l10 = 10; 64 final long l11 = 11; 65 final long l12 = 12; 66 final long l13 = 13; 67 final long l14 = 14; 68 final long l15 = 15; 69 final long l16 = 16; 70 final long l17 = 17; 71 final long l18 = 18; 72 final long l19 = 19; 73 } 74 75 static Point nfsp; 76 @NullRestricted 77 static Point fsp; 78 79 Point nfip; 80 @NullRestricted 81 Point fip; 82 83 static JumboInline nfsj; 84 @NullRestricted 85 static JumboInline fsj; 86 87 JumboInline nfij; 88 @NullRestricted 89 JumboInline fij; 90 91 static Object getNull() { 92 return null; 93 } 94 95 FlattenableSemanticTest() { } 96 97 public static void main(String[] args) { 98 FlattenableSemanticTest test = new FlattenableSemanticTest(); 99 100 // Uninitialized inline fields must be null for non flattenable fields 101 Asserts.assertNull(nfsp, "Invalid non null value for unitialized non flattenable field"); 102 Asserts.assertNull(nfsj, "Invalid non null value for unitialized non flattenable field"); 103 Asserts.assertNull(test.nfip, "Invalid non null value for unitialized non flattenable field"); 104 Asserts.assertNull(test.nfij, "Invalid non null value for unitialized non flattenable field"); 105 106 // fsp.equals(null); 107 108 // Uninitialized inline fields must be non null for flattenable fields 109 Asserts.assertNotNull(fsp, "Invalid null value for unitialized flattenable field"); 110 Asserts.assertNotNull(fsj, "Invalid null value for unitialized flattenable field"); 111 Asserts.assertNotNull(test.fip, "Invalid null value for unitialized flattenable field"); 112 Asserts.assertNotNull(test.fij, "Invalid null value for unitialized flattenable field"); 113 114 // Assigning null must be allowed for non flattenable inline fields 115 boolean exception = true; 116 try { 117 nfsp = (Point)getNull(); 118 nfsp = null; 119 exception = false; 120 } catch (NullPointerException e) { 121 exception = true; 122 } 123 Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field"); 124 125 try { 126 nfsj = (JumboInline)getNull(); 127 nfsj = null; 128 exception = false; 129 } catch (NullPointerException e) { 130 exception = true; 131 } 132 Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field"); 133 134 try { 135 test.nfip = (Point)getNull(); 136 test.nfip = null; 137 exception = false; 138 } catch (NullPointerException e) { 139 exception = true; 140 } 141 Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field"); 142 143 try { 144 test.nfij = (JumboInline)getNull(); 145 test.nfij = null; 146 exception = false; 147 } catch (NullPointerException e) { 148 exception = true; 149 } 150 Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field"); 151 152 // Assigning null to a flattenable inline field must trigger a NPE 153 exception = false; 154 try { 155 fsp = (Point)getNull(); 156 } catch(NullPointerException e) { 157 exception = true; 158 } 159 Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field"); 160 exception = false; 161 try { 162 fsj = (JumboInline)getNull(); 163 } catch(NullPointerException e) { 164 exception = true; 165 } 166 Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field"); 167 exception = false; 168 try { 169 test.fip = (Point)getNull(); 170 } catch(NullPointerException e) { 171 exception = true; 172 } 173 Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field"); 174 exception = false; 175 try { 176 test.fij = (JumboInline)getNull(); 177 } catch(NullPointerException e) { 178 exception = true; 179 } 180 Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field"); 181 exception = false; 182 } 183 184 }