Stream: contributing

Topic: Roc tags internal representation


view this post on Zulip Sven van Caem (Sep 10 2024 at 12:37):

Hi! I have a particular problem and a more general question.

I'm looking at issue #6041 to help get a sense of the problems Rust glue is running into. After some prodding, it seems that in the sixteen-variant case, the Roc CLI describes IOError as an Enumeration tag union, whereas it describes the seventeen-variant case as a NonRecursive tag union. This could mean that either the Roc compiler is reporting the wrong shape, or that Roc is performing some kind of optimization in the representation for tag unions I'm not aware of.

More generally, I'd be curious if there's some documentation I could read on how tag unions get represented in memory?

view this post on Zulip Anton (Sep 10 2024 at 12:39):

@Brendan Hansknecht @Folkert de Vries

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:40):

16 is suspecious. are we off by one? in the bottom 3 bits we can store 8 variants, and 16 being one more power of two is a clue maybe?

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:43):

Given there's no payload in any of the variants, I'd expect one byte to be sufficient for a 256-tag union right?

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:43):

ok, what is happening I think is that [A, B, C] is represented just as an integer

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:43):

right, but the 17-variant one must have a payload somewhere then?

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:44):

Not according to the Roc code

IOError : [
    NotFound,
    PermissionDenied,
    ConnectionRefused,
    ConnectionReset,
    HostUnreachable,
    NetworkUnreachable,
    ConnectionAborted,
    NotConnected,
    AddrInUse,
    AddrNotAvailable,
    NetworkDown,
    BrokenPipe,
    AlreadyExists,
    WouldBlock,
    NotADirectory,
    IsADirectory,
    DirectoryNotEmpty, # comment out and glue generates correctly
]

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:44):

ok

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:45):

well, internally we only have NonRecursive, an Enumeration is not a thing in the IR, so that must be something that glue determines

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:46):

I see

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:46):

I'm inclined to think this is a bug in the glue part of the compiler then?

view this post on Zulip Brendan Hansknecht (Sep 10 2024 at 13:47):

I think enumeration is something Richard added for non recursive tags that have no payload

view this post on Zulip Brendan Hansknecht (Sep 10 2024 at 13:47):

Should just be a simple check, but could have a bug for sure.

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:47):

Is this documented anywhere? Or is there someone who has a complete picture on this?

view this post on Zulip Sven van Caem (Sep 10 2024 at 13:48):

"this" being, the in-memory representation of tag unions

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:48):

see fn add_int_enumeration( and its usages

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:49):

it does not perform any sophisticated transformation though, so the 16/17 values difference might be something deeper

view this post on Zulip Folkert de Vries (Sep 10 2024 at 13:50):

so this would require some (dbg!) debugging


Last updated: Jul 06 2025 at 12:14 UTC