flix icon indicating copy to clipboard operation
flix copied to clipboard

Error in xvar?

Open CasperDN opened this issue 6 months ago • 9 comments

This works and prints 2:

def test2(): Unit \ IO = {
    ematch xvar Edge(2) {
        case Edge(x) => println(x)
        case Edge(_) => ???
    }
}

This does not work. It types Edge((2, 3)) as XVar[#(Edge(Int32, Int32))].

def test1(): Unit \ IO = {
    ematch xvar Edge((2, 3)) {
        case Edge(x) => println(x)
        case Edge(_) => ???
    }
}

Error:

#
# An unexpected error has been detected by the Flix compiler:
#
#   Bad type on operand stack
Exception Details:
  Location:
    Def$test1.directApply(Ldev/flix/runtime/Unit$;)Ldev/flix/runtime/Result$; @23: putfield
  Reason:
    Type 'Tuple$Int32$Int32' (current frame, stack[2]) is not assignable to integer
  Current Frame:
    bci: @23
    flags: { }
    locals: { 'dev/flix/runtime/Unit$' }
    stack: { 'Tag$Int32$Int32', 'Tag$Int32$Int32', 'Tuple$Int32$Int32' }
  Bytecode:
    0000000: bb00 1659 b700 1759 1219 b500 1f59 bb00
    0000010: 2159 0506 b700 24b5 0028 c000 1b4c 2bc0
    0000020: 001b b400 1f12 19a5 0007 03a7 0004 049a
    0000030: 0024 2bc0 001b 4ebb 002a 5912 2cbb 002e
    0000040: 5912 3007 1019 0710 1cb7 0033 b700 36bf
    0000050: 0000 bf2b c000 16b4 0028 3dbb 0038 59b7
    0000060: 0039 592c b500 0eb0 0000 0000 0000 0000
    0000070: bf00 0000 0000 0000 0000 0000 0000 bf  
  Stackmap Table:
    append_frame(@46,Object[#27])
    same_locals_1_stack_item_frame(@47,Integer)
    full_frame(@80,{},{Object[#65]})
    append_frame(@83,Object[#16],Object[#27])
    full_frame(@104,{},{Object[#65]})
    same_locals_1_stack_item_frame(@113,Object[#65])

#
# This is a bug in the Flix compiler. Please report it here:
#
# https://github.com/flix/flix/issues
#
# -- Flix Compiler --
#
# Flix Version : 0.59.0
#   incremental: All
#
# -- Java Virtual Machine --
#
# JVM Version  : 23.0.1 (2024-10-15)
# JVM Vendor   : Oracle Corporation
# JAVA_HOME    : C:\Users\caspe\.jdks\openjdk-23.0.1
# System       : Windows 11 (10.0)
#
# -- Stack Trace --
java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    Def$test1.directApply(Ldev/flix/runtime/Unit$;)Ldev/flix/runtime/Result$; @23: putfield
  Reason:
    Type 'Tuple$Int32$Int32' (current frame, stack[2]) is not assignable to integer
  Current Frame:
    bci: @23
    flags: { }
    locals: { 'dev/flix/runtime/Unit$' }
    stack: { 'Tag$Int32$Int32', 'Tag$Int32$Int32', 'Tuple$Int32$Int32' }
  Bytecode:
    0000000: bb00 1659 b700 1759 1219 b500 1f59 bb00
    0000010: 2159 0506 b700 24b5 0028 c000 1b4c 2bc0
    0000020: 001b b400 1f12 19a5 0007 03a7 0004 049a
    0000030: 0024 2bc0 001b 4ebb 002a 5912 2cbb 002e
    0000040: 5912 3007 1019 0710 1cb7 0033 b700 36bf
    0000050: 0000 bf2b c000 16b4 0028 3dbb 0038 59b7
    0000060: 0039 592c b500 0eb0 0000 0000 0000 0000
    0000070: bf00 0000 0000 0000 0000 0000 0000 bf  
  Stackmap Table:
    append_frame(@46,Object[#27])
    same_locals_1_stack_item_frame(@47,Integer)
    full_frame(@80,{},{Object[#65]})
    append_frame(@83,Object[#16],Object[#27])
    full_frame(@104,{},{Object[#65]})
    same_locals_1_stack_item_frame(@113,Object[#65])

	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3650)
	at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3675)
	at java.base/java.lang.Class.getMethods(Class.java:2244)
	at ca.uwaterloo.flix.util.JvmUtils$.getMethods(JvmUtils.scala:92)
	at ca.uwaterloo.flix.util.JvmUtils$.getMethods(JvmUtils.scala:64)
	at ca.uwaterloo.flix.language.phase.jvm.JvmLoader$.methodsOf(JvmLoader.scala:150)
	at ca.uwaterloo.flix.language.phase.jvm.JvmLoader$.$anonfun$load$1(JvmLoader.scala:102)
	at scala.collection.IterableOnceOps.foldLeft(IterableOnce.scala:727)
	at scala.collection.IterableOnceOps.foldLeft$(IterableOnce.scala:721)
	at scala.collection.AbstractIterable.foldLeft(Iterable.scala:935)
	at ca.uwaterloo.flix.language.phase.jvm.JvmLoader$.load(JvmLoader.scala:101)
	at ca.uwaterloo.flix.language.phase.jvm.JvmLoader$.run(JvmLoader.scala:52)
	at ca.uwaterloo.flix.api.Flix.codeGen(Flix.scala:692)
	at ca.uwaterloo.flix.runtime.shell.Shell.compile(Shell.scala:374)
	at ca.uwaterloo.flix.runtime.shell.Shell.run(Shell.scala:402)
	at ca.uwaterloo.flix.runtime.shell.Shell.execEval(Shell.scala:320)
	at ca.uwaterloo.flix.runtime.shell.Shell.execReloadAndEval(Shell.scala:336)
	at ca.uwaterloo.flix.runtime.shell.Shell.execute(Shell.scala:167)
	at ca.uwaterloo.flix.runtime.shell.Shell.loop(Shell.scala:115)
	at ca.uwaterloo.flix.Main$.main(Main.scala:258)
	at ca.uwaterloo.flix.Main.main(Main.scala)

CasperDN avatar Jun 11 '25 13:06 CasperDN

Run with the newest newest master.

CasperDN avatar Jun 11 '25 13:06 CasperDN

--Your second program is incorrect and will be rejected by the type system. We are just not done with all the checks.--

My bad. The second program should be OK.

magnus-madsen avatar Jun 11 '25 13:06 magnus-madsen

@jaschdoc / @JonathanStarup is this simple a consequence of the frontend not being done yet?

I would have expected it to work for any single argument XVar-- even tuples.

magnus-madsen avatar Jun 11 '25 13:06 magnus-madsen

Or maybe its because #10532 is not in yet?

magnus-madsen avatar Jun 11 '25 13:06 magnus-madsen

Or maybe its because #10532 is not in yet?

Yes, this program is observing the unsoundness that arity will fix

Short explanation for @CasperDN, you can confuse the compiler when you try to use xvar with one term of a tuple type as you showed. So generally you should avoid that until the fix is merged in

JonathanStarup avatar Jun 11 '25 13:06 JonathanStarup

@CasperDN It seems this is expected behavior until our fix is in-- which can happen next week.

magnus-madsen avatar Jun 11 '25 13:06 magnus-madsen

I just wanted to check whether it was a known problem, coding around it is not hard. Should I just close the issue, or should it stay until the fix is in?

CasperDN avatar Jun 11 '25 13:06 CasperDN

Keep it open in case we get distracted by something and don't fix it :P

mlutze avatar Jun 12 '25 07:06 mlutze

We have now fixed the predicate arity thing. But we are still working on ext vars.

magnus-madsen avatar Jun 17 '25 15:06 magnus-madsen

@jaschdoc Is this still relevant?

magnus-madsen avatar Aug 11 '25 15:08 magnus-madsen

This works now.

def main(): Unit \ IO = {
    ematch xvar Edge((1, 2)) {
        case Edge(x) => println(x)
    }
}

jaschdoc avatar Aug 12 '25 09:08 jaschdoc