1 /*
 2  * Copyright (c) 2019, 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 
24 /**
25  * @test
26  * @bug 8217875
27  * @summary [lworld] Javac does not allow express casts between value types and their light weight box types
28  * @compile -XDenablePrimitiveClasses IntercastTest.java
29  * @run main/othervm -Xverify:none -XX:+EnableValhalla -XX:+EnablePrimitiveClasses IntercastTest
30  */
31 
32 public primitive class IntercastTest {
33 
34     int ARRAY[] = { 10, 20, 30 };
35 
36     static primitive class Tuple {
37         private final int index;
38         private final int element;
39 
40         private Tuple(int index, int element) {
41             this.index = index;
42             this.element = element;
43         }
44     }
45 
46     static primitive class Cursor {
47         private final int[] array;
48         private final int index;
49 
50         private Cursor(int[] array, int index) {
51             this.array = array;
52             this.index = index;
53         }
54 
55         Tuple current() {
56             return new Tuple(index, array[index]);
57         }
58 
59         Cursor.ref next() {
60             if (index + 1 == array.length) {
61                 return null;
62             }
63             return new Cursor(array, index + 1);
64         }
65     }
66 
67     private static Cursor.ref indexedElements(int[] array) {
68         if (array.length == 0) {
69             return null;
70         }
71         return new Cursor(array, 0);
72     }
73 
74     public int sum() {
75         int sum = 0;
76         for (Cursor.ref cursor = indexedElements(ARRAY); cursor != null; cursor = cursor.next()) {
77             Tuple tuple = cursor.current();
78             sum += tuple.index + tuple.element;
79         }
80         return sum;
81     }
82 
83     public static void main(String [] args) {
84         IntercastTest x = new IntercastTest();
85         if (x.sum() != 63 || x.ARRAY.length != 3) {
86             throw new AssertionError("Broken");
87         }
88         IntercastTest.ref xbox = (IntercastTest.ref) x;
89         if (xbox.sum() != 63 || xbox.ARRAY.length != 3) {
90             throw new AssertionError("Broken");
91         }
92         x = (IntercastTest) xbox;
93         if (x.sum() != 63 || x.ARRAY.length != 3) {
94             throw new AssertionError("Broken");
95         }
96     }
97 }