Stream: contributing

Topic: Broken glue


view this post on Zulip Agus Zubiaga (Mar 02 2023 at 23:28):

I started debugging glue on basic-cli. When I run it on main.roc, it's falling into this unreachable! because it finds a Result but type is Layout::Struct.

I believe it should be Layout::Union so that it matches this case.

view this post on Zulip Agus Zubiaga (Mar 02 2023 at 23:34):

I'm trying to figure out how to get the position in the source code

view this post on Zulip Ayaz Hafiz (Mar 02 2023 at 23:34):

Layout::Struct is generated for Results if either the ok or error type is void, for example Result Foo [] can only ever be Ok Foo, so we compile it to just the layout of Foo

view this post on Zulip Ayaz Hafiz (Mar 02 2023 at 23:35):

Unfortunately there is presently not enough debug information to trace you back to the source code position

view this post on Zulip Ayaz Hafiz (Mar 02 2023 at 23:36):

however, if you print out the types reached to that position (dbg!(subs.dbg(var))) it might give you enough hints to contextualize where the type was defined

view this post on Zulip Agus Zubiaga (Mar 02 2023 at 23:45):

Oh, I see. So we should be handling this case then.

view this post on Zulip Agus Zubiaga (Mar 02 2023 at 23:46):

Ayaz Hafiz said:

however, if you print out the types reached to that position (dbg!(subs.dbg(var))) it might give you enough hints to contextualize where the type was defined

Ok, it's a Result {} [] somewhere

view this post on Zulip Agus Zubiaga (Mar 02 2023 at 23:48):

Weirdly I can't seem to find one like that in basic-cli. There are a few Result {} {} but I believe that would be Layout::Union following your logic.

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 00:10):

Ah, Task holds a Result and main.roc has a Task {} []

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 00:14):

Ok, all is clear then. This is a real case and we have to handle Layout::Struct Result.

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 11:50):

I added that case and now it doesn't crash, but it also doesn't generate anything

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 11:53):

It's finding only what's under main.roc and not all the other modules such as Effect and its dependencies.

view this post on Zulip Folkert de Vries (Mar 03 2023 at 11:54):

it crawls from the type of mainForHost (or should, anyway)

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 11:55):

Right, I think it is. However, mainForHost references Task which references InternalTask which references Effect and it doesn't seem to find any of those.

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:00):

more specifically, no rust code is generated for those types?

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:01):

Correct. This is all it generates:

// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc glue` CLI command

#![allow(unused_unsafe)]
#![allow(dead_code)]
#![allow(unused_mut)]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(clippy::undocumented_unsafe_blocks)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::unused_unit)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::let_and_return)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::needless_borrow)]
#![allow(clippy::clone_on_copy)]

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:02):

Which makes sense because these are all the types it finds:

    types: [
        Unit,
        EmptyTagUnion,
        RocResult(
            TypeId(
                0,
            ),
            TypeId(
                1,
            ),
        ),
        Function {
            name: "TODO_roc_function_71",
            args: [
                TypeId(
                    0,
                ),
            ],
            ret: TypeId(
                2,
            ),
        },
    ],

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:03):

Just the stuff on main.roc if I'm reading correctly

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:04):

and all of those are skipped here: https://github.com/roc-lang/roc/blob/3b5f6ef8285fb4177f24c7094c807ca9fb6485ec/crates/glue/src/rust_glue.rs#L282-L299

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:06):

mainForHost returns a Task though, I think the debug output above says it returns a Result. I guess that's getting optimized away?

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:07):

I see, I think this makes sense.

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:08):

an effect is internally generated as a thunk (Effect a := {} -> a). I'm assuming you're looking at our main branch, and there we don't generate anything for functions yet

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:08):

we have an experimental branch that does handle functions

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:09):

but some other stuff is still broken on that branch

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:10):

Oh. Yes, I'm on main. Do you mean glue can generate functions in that experimental branch?

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:10):

it can generate glue for function types, yes

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:11):

our actual compilation scheme for functions is a bit involved

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:11):

I see. I wonder if the existing glue code in basic-cli was generated using glue on that branch.

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:12):

Maybe that's all I need to do

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:13):

(the branch with functions is glue-getters-rtfeldman, but it's in an unfinished state)

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:13):

well that glue file does not contain anything for Task or Effect

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:15):

Yeah, so that might not be the issue then. What I wanted to generate was error types like these: https://github.com/roc-lang/basic-cli/blob/main/src/InternalFile.roc

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:15):

Which were somehow generated here: https://github.com/roc-lang/basic-cli/blob/main/src/src/file_glue.rs

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:16):

right, but from what I know that type needs to be reachable from the type of mainForHost and it isn't because glue stops when it hits a function type and it always does for basic-cli

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:16):

so I'm not sure how that glue was generated

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:18):

Right, that's what I understood from you. Maybe at some point there was a version of glue that generated all the types that Taskand Effect reference but nothing for themselves?

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:19):

Sorry if I'm saying something stupid, this is my first endeavor into the compiler :D

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:20):

yeah no clearly at some point this glue was generated.

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:20):

Richard will know all of the details

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:28):

Tried running it on that branch and it hit the same unreachable!() for the Layout::Struct Result. I added that case and it generated some code for function as you said it would but still nothing for the types.

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:28):

I'll wait for Richard to give me some more context

view this post on Zulip Brian Carroll (Mar 03 2023 at 12:40):

In case it helps, my workaround for the fact that glue doesn't handle functions, is to just define a constant of each type you care about! This works as long as the types don't have lambdas inside them!

view this post on Zulip Brian Carroll (Mar 03 2023 at 12:41):

At least that used to work a few months ago

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:44):

in the main.roc you mean?

view this post on Zulip Brian Carroll (Mar 03 2023 at 12:45):

yeah

view this post on Zulip Brian Carroll (Mar 03 2023 at 12:46):

whatever Roc file you pass to the glue generator

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 12:54):

Just tried that but it failed because of the unused constant:

thread 'main' panicked at 'not yet implemented: Gracefully report compilation problems during glue generation:

[UnusedDef(`16.IdentId(2)`, @233-240)], []', crates/glue/src/load.rs:125:9

view this post on Zulip Folkert de Vries (Mar 03 2023 at 12:59):

you might be able to get away with adding

main = 
    when someConstant is
        _ -> rest of main

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:03):

Ooh, that did work! Thanks

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:04):

It still doesn't work as it is on main because of the unreachable crash for Result, but it does if handle that case

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:06):

you can make a PR for handling that result case

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:06):

then we'll merge it in everywhere

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:07):

Yeah but I think that Roc programs would still crash becase the memory layout won't line up for things returning Result _ []

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:08):

I think roc_std is always using the union layout for Result

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:08):

ah, yes that is correct

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:08):

so that needs some special handling

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:08):

which is maybe why the file example is marked as "broken" in basic-cli

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:09):

right

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:12):

Should roc_std expose a NoErrorResult or something like that?

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:14):

hmm I'm not sure

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:14):

You could mess up and use it when you're supposed to use the normal Result. However, you can also mess up by returning any other invalid type.

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:15):

Will glue eventually expose a trait for the implementation of the roc_fx_ functions so that you get type errors in the platform?

view this post on Zulip Folkert de Vries (Mar 03 2023 at 13:19):

well the alternative is to generate "hidden" glue for the "noerrorresult" (which is just another roc value) and then expose it after wrapping in a normal RocResult

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 13:25):

Not sure I follow how that would work in practice. Would you generate a type per Effect function?

view this post on Zulip Brendan Hansknecht (Mar 03 2023 at 15:58):

roc_fx_ functions should be going away when glue gets more powerful

view this post on Zulip Brendan Hansknecht (Mar 03 2023 at 15:59):

They will be replaced with tag union, state machine based effects

view this post on Zulip Brendan Hansknecht (Mar 03 2023 at 15:59):

Richard has talked about the new approach in some previous meetups.

view this post on Zulip Brendan Hansknecht (Mar 03 2023 at 16:00):

Maybe roc_fx_ functions were not directly mentioned, but they should become redundant like the hosted module and I believe Effect in general

view this post on Zulip Brendan Hansknecht (Mar 03 2023 at 16:01):

Also, for the glue gen you want, can you make a dummy main file that just has a return type of the types you want generated. No tasks at all?

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 17:24):

Yeah, that’ll probably work as I’m not returning any Result _ []. Will give it a try tonight!

view this post on Zulip Agus Zubiaga (Mar 03 2023 at 17:24):

Thanks

view this post on Zulip Johan Lindskogen (Dec 30 2023 at 22:35):

Working on https://github.com/roc-lang/basic-cli/issues/145 I think I ran into a similar issue, basically; how do I regenerate the glue for basic-cli?

roc glue ../roc/crates/glue/src/RustGlue.roc platform/src/ platform/main.roc outputs an entirely different file structure than the existing one with file_glue.rs, dir_glue.rs, command_glue.rs

(@Luke Boswell refers to converting it to use RustGlue here: https://roc.zulipchat.com/#narrow/stream/316715-contributing/topic/basic-cli.20glue/near/397443629)

view this post on Zulip Luke Boswell (Dec 30 2023 at 23:36):

Glue for basic-cli has basically been manually put together rn. I have a WIP PR to make it so we can regenerate using a script, but I cant remember where I got to with that.

view this post on Zulip Luke Boswell (Dec 30 2023 at 23:37):

Are you trying to add new features? If so you can make a fake platform and then just include the types you want and glue can generate for those types you need.

view this post on Zulip Luke Boswell (Dec 30 2023 at 23:40):

This is the PR I'm refrring to https://github.com/roc-lang/basic-cli/pull/135

view this post on Zulip Johan Lindskogen (Jan 02 2024 at 08:05):

Luke Boswell said:

Are you trying to add new features? If so you can make a fake platform and then just include the types you want and glue can generate for those types you need.

Yes, I am adding Path.x methods in this issue https://github.com/roc-lang/basic-cli/issues/145

view this post on Zulip Luke Boswell (Jan 02 2024 at 09:57):

I've been updating that PR, it looks like it should be all OK to me, but for some reason it is getting stuck in CI creating a directory.

I did update examples/dir.roc to make it clearer, but I don't see how this change is affecting tests in CI.

view this post on Zulip Anton (Jan 02 2024 at 10:27):

I'll check it out

view this post on Zulip Johan Lindskogen (Jan 02 2024 at 21:38):

I submitted a PR if any of you would like to dissect further: :smiley: https://github.com/roc-lang/basic-cli/pull/150

view this post on Zulip Anton (Jan 03 2024 at 08:41):

This is caused by #5924, everybody seems to be hitting it now, I'm working on it today.


Last updated: Jul 06 2025 at 12:14 UTC