flix icon indicating copy to clipboard operation
flix copied to clipboard

Safety phase does not consider associated types when checking checked_cast

Open Bobini1 opened this issue 1 month ago • 7 comments

Am I doing something wrong or is this an error in the language?

import java.lang.Object
import java.lang.Integer


def main(): Unit =
    let t: Integer = Box.box(0x02000000);
    let _a: Object = checked_cast(t);
    ()
-- Safety Error -------------------------------------------------- T:\flix\test\src\Main.flix

>> Illegal checked cast: Attempt to cast a non-Java type to a Java type.

7 |     let _a: Object = checked_cast(t);
                         ^^^^^^^^^^^^^^^
                         illegal cast

From: Boxed[Int32]
To  : java.lang.Object

Bobini1 avatar Dec 01 '25 23:12 Bobini1

What are you trying to do? What's the bigger program / real context?

Ideally you should not be using checked/unchecked cast.

magnus-madsen avatar Dec 02 '25 07:12 magnus-madsen

~~Box[Int32] is not the same as Integer. You should use Integer.valueOf if you need to create a Java Integer for some reason.~~

mlutze avatar Dec 02 '25 08:12 mlutze

Box[Int32] is not the same as Integer. You should use Integer.valueOf if you need to create a Java Integer for some reason.

I am unsure actually. I think its an associated type which is actually Integer.

magnus-madsen avatar Dec 02 '25 08:12 magnus-madsen

Box[Int32] is not the same as Integer. You should use Integer.valueOf if you need to create a Java Integer for some reason.

I am unsure actually. I think its an associated type which is actually Integer.

:O You're right!

mlutze avatar Dec 02 '25 10:12 mlutze

@Bobini1 It seems there is a small bug in the compiler here. Nevertheless, casts should be rare, so it would be nice to know what the "real program" needs to do. Unless you are just experimenting?

magnus-madsen avatar Dec 02 '25 11:12 magnus-madsen

@magnus-madsen I'm trying to call this method via Java interop: https://java-native-access.github.io/jna/4.2.0/com/sun/jna/Function.html#invoke-java.lang.Class-java.lang.Object:A-

In particular, I would like to try to use it to call __stdcall NtQueryDirectoryFile from ntdll.dll and CreateFileW from Kernel32.dll.

Bobini1 avatar Dec 02 '25 12:12 Bobini1

OK, you can use an unchecked cast for now: https://doc.flix.dev/unchecked-casts.html

magnus-madsen avatar Dec 02 '25 12:12 magnus-madsen