Stream: compiler development

Topic: Inline expect


view this post on Zulip Luke Boswell (Nov 22 2024 at 22:22):

@Oskar Hahn left some helpful comments on the zig template upgrade, one of which I found interesting and wanted to ask about.

Currently, libc is also needed for the inline-expect. But there is the plan to solve inline-expect the same way then dbg. If this is implemented, then a zig-roc-binary would not need libc and could be statically linked.

Do we have plans/any issues to track this work?

I assume the idea is that the platform provides a roc_expect effect that gets called when we hit an inline expectation.

view this post on Zulip Brendan Hansknecht (Nov 22 2024 at 23:30):

Yes

view this post on Zulip Brendan Hansknecht (Nov 22 2024 at 23:30):

I filed an issue for it a long while back

view this post on Zulip Brendan Hansknecht (Nov 22 2024 at 23:33):

https://github.com/roc-lang/roc/issues/6930

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:11):

Just looking at that change... it looks like we should at least put the effect into basic-cli and basic-webserver etc so it's ready for this change in future, even if it's not used currently.

Update all platforms to have roc_expect_failed with the api: fn roc_expect_failed(loc: *RocStr, src: *RocStr, variables: *RocList<{name: RocStr, value: RocStr}>).

Are we happy with this name still? I kind of like the idea of just roc_expect but don't mind the longer version.

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:13):

Original proposal https://roc.zulipchat.com/#narrow/channel/395097-compiler-development/topic/roc_expect_failed/near/418463775

view this post on Zulip Richard Feldman (Nov 23 2024 at 01:13):

I think it's clearer with the longer name - like this only gets called when an expectation fails

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:15):

I'm thinking I'll add it into the purity inference PR... unless you would prefer a separate PR for it?

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:15):

That will save merge conflicts when we land that

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:24):

https://github.com/roc-lang/basic-cli/pull/271

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:50):

Ad it to purity inference?

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:51):

^^^ this PR is based of the purity-inference branch in basic-cli and is a PR to merge back into that.

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:51):

As opposed to the main branch

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:52):

I'm a little lost, no one has done the work in the compiler to use expect this way

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:52):

I think that will be a medium sized lift. More work than debug was

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:52):

I wouldn't expect it to be ready for a while.

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:52):

Yeah, but adding into the host means that when we do it won't be a breaking change/easier to test.

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:53):

I'm starting to think the dbg, expect and passed in allocators is my highest priority over new syntax or build pipeline stuff

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:53):

Oh, just adding the effect to the host now ....sure, though wouldn't be surprised if the API changes while implementing, but could add it

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:53):

It just has a big impact on the platform development, and there are things I want to do with roc-ray and this stuff is really holding us back

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:53):

Got it

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 01:53):

Makes sense

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:54):

Yeah, I hope the API doesn't change too much... this may be too premature

view this post on Zulip Luke Boswell (Nov 23 2024 at 01:55):

I think that will be a medium sized lift. More work than debug was

Where in the compiler is all this work? Is it mostly backend/code gen related?

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 03:49):

Looking at the linked PRs would help

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 03:50):

Some of it is in ripping out a lot of test infra and runtime loading that deals with expect today

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 03:50):

Some is in the pipeline of how expect is lowered

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 03:51):

Code gen is where this bottoms out but should be relatively minimal

view this post on Zulip Oskar Hahn (Nov 23 2024 at 07:14):

When you are talking about the API of these functions:

The current API for dbg is:

fn roc_dbg(loc: *str.RocStr, msg: *str.RocStr, src: *str.RocStr) callconv(.C) void

When I remember correctly, then the third argument was added in December 2023. The idea was, that it was added as a third argument, so it is backward compatible. But the order should change after AoC2023.

So I think some API changes are very likely and therefore it is not helpful to add the functions to the platform at the moment.

view this post on Zulip Oskar Hahn (Nov 23 2024 at 11:41):

I tired to remove the stuff needed for the inline expect from my aoc-platform and I can confirm, that without it, libc is not needed and you get a statically linked binary:

$ roc build days/day01.roc
0 errors and 0 warnings found in 307 ms
 while successfully building:

    days/day01

$ ldd days/day01
        statically linked

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 16:05):

What is needed for inline expect today? Why does it need libc?

view this post on Zulip Oskar Hahn (Nov 23 2024 at 16:42):

As far as I know, it needs shm_open, mmap and getppid

view this post on Zulip Oskar Hahn (Nov 23 2024 at 16:45):

Yes, if I do not export roc_getppid, roc_mmap or roc_shm_open, inline expect does not work. And I don't know how to implement them manually.

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 17:15):

Ah

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 17:16):

Yeah I guess those need libc or direct os system calls....but libc is the easy way

view this post on Zulip Brendan Hansknecht (Nov 23 2024 at 17:16):

Forgot about those primitives. Yeah, they would get removed with these changes

view this post on Zulip Luke Boswell (Nov 28 2024 at 06:29):

Made a start on this change https://github.com/roc-lang/roc/pull/7256

I have no idea how big this change will be... but figured I'd give it a go.

So far I've got to this point... looks to be calling my low level (from llvm) and passing in some values I hardcoded in Can. I need to figure out how to thread the source and location in from Parse to Can, it looks a bit different than how Dbg does things.

Also, no idea why this "traps"... probably need to run on linux and/or use a debugger to figure that out.

$ cargo run -- test examples/platform-switching/rocLovesZig.roc
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
     Running `target/debug/roc test examples/platform-switching/rocLovesZig.roc`
[HOW DO WE GET SOURCE TO CAN?] HOW DO WE GET SOURCE TO CAN? = TODO IMPLEMENT ME!
Trace/BPT trap: 5

Last updated: Jul 06 2025 at 12:14 UTC