Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected result for Class.isInstance() given nullable array instance and non-nullable array class #20305

Closed
hzongaro opened this issue Oct 4, 2024 · 6 comments · Fixed by #20315
Assignees
Labels
comp:vm project:valhalla Used to track Project Valhalla related work

Comments

@hzongaro
Copy link
Member

hzongaro commented Oct 4, 2024

Given the class of a non-nullable array and a nullable array instance the Class.isInstance() method for the OpenJ9 value types prototype implementation yields a different result from that of the Reference Implementation. For the following test:

$ cat TestIsInstance.java
import jdk.internal.value.NullRestrictedCheckedType;
import jdk.internal.value.ValueClass;
import jdk.internal.vm.annotation.ImplicitlyConstructible;

public class TestIsInstance {
    @ImplicitlyConstructible
    public static value class Point {
        public final int i, j;

        public Point(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

    public static final void main(String[] args) throws ClassNotFoundException {
        Point[] nonNullableArray = (Point[]) ValueClass.newArrayInstance(NullRestrictedCheckedType.of(Point.class), 1);
        Point[] nullableArray = new Point[1];

        System.out.println("nonNullableArray.getClass().isInstance(nonNullableArray) == "+nonNullableArray.getClass().isInstance(nonNullableArray));
        System.out.println("nullableArray.getClass().isInstance(nullableArray) == "+nullableArray.getClass().isInstance(nullableArray));
        System.out.println("nonNullableArray.getClass().isInstance(nullableArray) == "+nonNullableArray.getClass().isInstance(nullableArray));
        System.out.println("nullableArray.getClass().isInstance(nonNullableArray) == "+nullableArray.getClass().isInstance(nonNullableArray));
    }
}

OpenJ9 yields:

nonNullableArray.getClass().isInstance(nonNullableArray) == true
nullableArray.getClass().isInstance(nullableArray) == true
nonNullableArray.getClass().isInstance(nullableArray) == true
nullableArray.getClass().isInstance(nonNullableArray) == true

while the Reference Implementation yields:

nonNullableArray.getClass().isInstance(nonNullableArray) == true
nullableArray.getClass().isInstance(nullableArray) == true
nonNullableArray.getClass().isInstance(nullableArray) == false
nullableArray.getClass().isInstance(nonNullableArray) == true

@theresa-m, @hangshao0, @a7ehuo - FYI

@hzongaro hzongaro added comp:vm project:valhalla Used to track Project Valhalla related work labels Oct 4, 2024
Copy link

github-actions bot commented Oct 4, 2024

Issue Number: 20305
Status: Open
Recommended Components: comp:vm, comp:test, comp:gc
Recommended Assignees: jasonfengj9, hangshao0, gacholio

@theresa-m
Copy link
Contributor

Thanks @hzongaro I opened a change to fix this.

I think it would be useful to have some checkcast tests for arrays as well since my change will impact that as well, I will write those later this week.

@hzongaro
Copy link
Member Author

hzongaro commented Oct 7, 2024

At the suggestion of @jdmpapin, I also tried a variation of this test using Class.isAssignableFrom(Class) instead. That exhibited a similar difference in behaviour between OpenJ9 and the Reference Implementation. I just thought I'd mention it in case that would entail any additional changes to your pull request.

@theresa-m
Copy link
Contributor

isAssignableFrom

Thanks! I believe my changes will fix Class.isAssignableFrom as well.

@theresa-m
Copy link
Contributor

I think it would be useful to have some checkcast tests for arrays as well since my change will impact that as well, I will write those later this week.

I think the only way to do this right now is with the CheckedType api which is not supported yet #19764

@github-project-automation github-project-automation bot moved this from TODO: VM to Done in Valhalla L-World Oct 9, 2024
Copy link

github-actions bot commented Oct 9, 2024

Issue Number: 20305
Status: Closed
Actual Components: comp:vm, project:valhalla
Actual Assignees: No one :(
PR Assignees: theresa-m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:vm project:valhalla Used to track Project Valhalla related work
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants