Stream: beginners

Topic: Platform: invalid content in tag union variable


view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:33):

Hi! Trying my hand at writing a platform, based on the existing examples. Even with the simplest example, I run into a compiler panic and can't figure out what's wrong.

The setup is simple: export a sayHello function from Rust (well, roc_fx_sayHello) that returns a RocStr, and wrap it in a nice File.sayHello interface for platform users. Then, simply call it from main.

The error I get:
thread '<unnamed>' panicked at 'invalid content in tag union variable: (113, Structure(Apply(`3.IdentId(0)`, SubsSlice { start: 50, length: 0 })))', compiler/mono/src/layout.rs:1776:23

It seems related to the use of Effect.sayHello, since the error goes away if I just use a regular Roc function in File.sayHello

Here's a repo with my code: https://github.com/erwinkn/advent-of-code-2021
(yes I'm catching up on AoC and yes I got sidetracked into writing a platform :joy: )

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:40):

in the package config sayHello : {} -> Str must be sayHello : {} -> Effect Str

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:40):

functions from the platform must always return an Effect

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:40):

(to roc)

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:41):

no changes are needed on the rust side

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:48):

OK, got it! I'll have to propagate the Effect to main to File.sayHello and main then I guess

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:49):

exactly. That's required for purity: your rust code could do whatever sideeffects, and we have to manage that on the roc side somehow

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:50):

On that note, is there any documentation somewhere regarding the difference in the Rust functions exposed in the hello-X examples (where only roc_main is exposed in the "linked" block) vs other examples like the CLI platform, where you also have roc_main_size, call_Fx, size_Fx and size_Fx_Result?

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:53):

no, that is still very unstable. Right now host-exposed functions get a _size constant, and aliases defined in its signature also get a bunch of size constants and if it's a closure type it also gets a caller

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:53):

but, the best place to learn about that right now is looking at that working code. We will be changing things there in the future

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:54):

So, I should probably copy / paste the code from the CLI platform, to make sure the size constants and callers are handled correctly?

view this post on Zulip Folkert de Vries (Dec 09 2021 at 09:55):

that's a good starting point, yes

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:58):

Alright, thanks!

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 09:59):

Last thing: would there be an interest in a documented platform example, kinda like a "platform author starter pack"? Basically one of the existing examples, with comments to explain what's happening, what is compiler boilerplate and where your code should go
Or is it too early for this?

view this post on Zulip Folkert de Vries (Dec 09 2021 at 10:02):

it is early, and it will change. But it could be helpful right now, and show us where the current approach is lacking

view this post on Zulip Erwin Kuhn (Dec 09 2021 at 10:03):

I'd be interested in writing this, to learn myself! I'll crosspost this in #ideas


Last updated: Jul 06 2025 at 12:14 UTC