1 /*
 2  * Copyright (c) 2023, 2024, Arm Limited. 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 8309650
27  * @summary Fix mismatch inline type issue during method calls
28  * @library /test/lib
29  * @enablePreview
30  * @run main/othervm -XX:-TieredCompilation -Xcomp
31  *                   compiler.valhalla.inlinetypes.TestCastMismatch
32  */
33 
34 package compiler.valhalla.inlinetypes;
35 
36 import java.util.Random;
37 import jdk.test.lib.Utils;
38 
39 public class TestCastMismatch {
40     private static int LOOP_COUNT = 50000;
41 
42     private static final Random RD = Utils.getRandomInstance();
43 
44     public static MultiValues add(MultiValues v1, MultiValues v2) {
45         return v1.factory(v1.value1() + v2.value1(), v1.value2() + v2.value2());
46     }
47 
48     public static void main(String[] args) {
49         Point p1 = new Point(RD.nextInt(), RD.nextInt());
50         Point p2 = new Point(RD.nextInt(), RD.nextInt());
51         for (int i = 0; i < LOOP_COUNT; i++) {
52             p1 = (Point) add(p1, p2);
53         }
54 
55         System.out.println("PASS");
56     }
57 
58     static abstract value class MultiValues {
59         public abstract int value1();
60         public abstract int value2();
61         public abstract MultiValues factory(int value1, int value2);
62     }
63 
64     static value class Point extends MultiValues {
65         private int x;
66         private int y;
67 
68         private Point(int x, int y) {
69             this.x = x;
70             this.y = y;
71         }
72 
73         @Override
74         public int value1() {
75             return x;
76         }
77 
78         @Override
79         public int value2() {
80             return y;
81         }
82 
83         @Override
84         public Point factory(int value1, int value2) {
85             return new Point(value1, value2);
86         }
87     }
88 }
89