Hi! I'm working with Luke on his roc-ray platform, and ran into either a bug or (more likely) a thing I don't get about roc glue.
If I run this command:
roc glue ~/path/to/roc/crates/glue/src/RustGlue.roc glue/out glue/glue.roc
where glue/glue.roc
has this in it:
ThingToCheck : {
timestampMillis : U64,
frameCount : U64,
keys : List U64,
mouseButtons : List U64,
mousePosX : F32,
mousePosY : F32,
}
in the output, there's different versions of ThingToCheck as a rust struct for x86, x86_64, wasm32, etc. These different structs have different orders to their fields, which I understand matters because they're repr(C)
. For example:
#[derive(Clone, Default, Debug, PartialEq, PartialOrd, )]
#[repr(C)]
pub struct ThingToCheck {
pub frameCount: u64,
pub keys: roc_std::RocList<u64>,
pub mouseButtons: roc_std::RocList<u64>,
pub mousePosX: f32,
pub mousePosY: f32,
pub timestampMillis: u64,
}
// x86_64
#[derive(Clone, Default, Debug, PartialEq, PartialOrd, )]
#[repr(C)]
pub struct ThingToCheck {
pub frameCount: u64,
pub keys: roc_std::RocList<u64>,
pub mouseButtons: roc_std::RocList<u64>,
pub timestampMillis: u64,
pub mousePosX: f32,
pub mousePosY: f32,
}
Is that expected, and should our (currently handwritten not generated) host be using cfg(target_arch = "whatever")
to handle this kind of thing?
The first one is wasm?
Or I guess x86_32.
Anyway, I think it is correct
Roc sorts by alignment first then by name
On x86_32 and wasm32, u64
has an alignment of 32
bits
So on those targets all fields have the same alignment. Thus just alphabetical order
On x86_64, they have u64
has 64
bit alignment. So does List
. So they go before the f32
s
I'm not familiar with having to think about alignment at all. Am I right to think that this means using a single handwritten version (without cfg(target_arch)) is probably going to end up broken across different compilation targets? Like the data roc gets out of the bytes passed from rust would just be arbitrarily incorrect?
For example, given enough variety in fields, if it was sorted correctly for aarch64 it would necessarily be sorted incorrectly for an x86_64 machine.
using a single handwritten version (without cfg(target_arch)) is probably going to end up broken across different compilation targets?
Yes
aarch64 it would necessarily be sorted incorrectly for an x86_64 machine.
I think these are the same though, at least that's why i just hand rolled it
I think at some point we talked about changing roc to just sort by alphabetical. Is there a reason we wouldn't do this?
ah, gotcha; it only really matters for 32 vs 64, which in practice is likely to mean wasm?
That's my poor mans understanding
Yeah, that is a good poor mans understanding. Sometimes there can be weird exceptions, but that almost always is the difference.
thanks! that's reassuring.
Last updated: Jul 06 2025 at 12:14 UTC