Stream: compiler development

Topic: Is there a bug wehen compare a small string with big string


view this post on Zulip Oskar Hahn (Jan 14 2024 at 11:45):

I am looking at the zig implementation of RocStr and I am not sure, if there is a bug: https://github.com/roc-lang/roc/blob/be827982478bdbe74e90c6b3517fa3aecedf020c/crates/compiler/builtins/bitcode/src/str.zig#L186-L192

The code assumes, that when the len of two strings are different, then the strings are not equal. But I am not sure, if this is true.

If a platform returns a shot string like "hello world", but it does not return it as a short string. Instead, it returns it on the heap with {ptr: 0x123, len: 11, cap: 11} and this string gets compared in the roc code with the short string "hello world" (on the stack), then the len is (probably) unequal, but the strings should be the same.

view this post on Zulip Brian Carroll (Jan 14 2024 at 15:19):

Check how the len method works.

view this post on Zulip Brian Carroll (Jan 14 2024 at 15:19):

It's implemented differently for long and short strings.

view this post on Zulip Brian Carroll (Jan 14 2024 at 15:21):

You could write a Zig test for it if you want to confirm.

view this post on Zulip Brian Carroll (Jan 14 2024 at 15:50):

Both representations of the "hello world" string have the same length value. Otherwise one of them would have to be wrong.
For the short string, the length is contained in a single byte at the end. Maybe you are assuming the length of a short string is always 23 but that's not true.

view this post on Zulip Brian Carroll (Jan 14 2024 at 15:53):

Its capacity is 23 though

view this post on Zulip Oskar Hahn (Jan 15 2024 at 13:32):

You are right and I am sorry.

I mixed up self.len() with self.length.

I tried to write a test, but was not able to run it. I tried zig test str.zig, but it returned:

zig test str.zig
error: ld.lld: undefined symbol: getppid
    note: referenced by utils.zig:36 ([...]crates/compiler/builtins/bitcode/src/utils.zig:36)
    note:               [...]crates/compiler/builtins/bitcode/zig-cache/o/cb65059fe56dcfc1943415367f0a74a4/test.o:(roc_getppid)
error: ld.lld: undefined symbol: mmap
    note: referenced by utils.zig:47 ([...]crates/compiler/builtins/bitcode/src/utils.zig:47)
    note:               [...]crates/compiler/builtins/bitcode/zig-cache/o/cb65059fe56dcfc1943415367f0a74a4/test.o:(roc_mmap)
error: ld.lld: undefined symbol: shm_open
    note: referenced by utils.zig:44 ([...]crates/compiler/builtins/bitcode/src/utils.zig:44)
    note:               [...]crates/compiler/builtins/bitcode/zig-cache/o/cb65059fe56dcfc1943415367f0a74a4/test.o:(roc_shm_open)

How would you run it?

view this post on Zulip Anton (Jan 15 2024 at 13:47):

In CI we use zig build test like here.

view this post on Zulip Brian Carroll (Jan 15 2024 at 13:48):

Nothing to be sorry for, it's good to get stuck in and question things!
You have to run the tests through the build script. So don't pass the filename. Instead run all the tests. But then I think you can filter by test name with some option.


Last updated: Jul 06 2025 at 12:14 UTC