Stream: beginners

Topic: ✔ Strange bug when using dbg with functions


view this post on Zulip Fábio Beirão (Jun 07 2023 at 08:52):

I'm on the latest (nightly) version of roc (roc nightly pre-release, built from commit 107c6b0 on Di 06 Jun 2023 09:07:49 UTC)
I am scratching my head at that the heck is happening here. Hopefuly this is me doing something silly, but I can't figure it out

This code (I called the file bug.roc :sweat_smile:):

app "app"
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br",
    }
    imports [
        pf.Stdout,
    ]
    provides [main] to pf

findCommonItemTypes : { left : Str, right : Str } -> List Str
findCommonItemTypes = \{left, right} ->
    leftGraphemes = Str.graphemes left
    rightGraphemes = Str.graphemes right
    dbg leftGraphemes
    dbg rightGraphemes
    leftGraphemes |> List.keepIf (\grapheme -> rightGraphemes |> List.contains grapheme)

main =
    dbg ({left: "abc", right: "abc"} |> findCommonItemTypes)

    Stdout.line "Send help!"

Gives me this output:

[bug.roc 14:9] ["a", "b", "c"]
[bug.roc 15:9] ["a", "b", "c"]
[bug.roc 19:10] ["H", "b", "c"]
Send help!

:point_up::point_up: Where.. in the world.. did the "H" come from? :scream::scream: Help :sweat_smile:

view this post on Zulip Fábio Beirão (Jun 07 2023 at 08:55):

left: "abc", right: "a" -> also outputs ["H"]

view this post on Zulip Fábio Beirão (Jun 07 2023 at 08:57):

{left: "cba", right: "cba"} -> also outputs ["H", "b", "a"]

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:00):

On the rockin' repl it seems to work ok

  The rockin’ roc repl
────────────────────────

Enter an expression, or :help, or :q to quit.

» left = "abc" |> Str.graphemes

["a", "b", "c"] : List Str
                          # left

» right = "abc" |> Str.graphemes

["a", "b", "c"] : List Str
                         # right

» left |> List.keepIf (\grapheme -> right |> List.contains grapheme)

["a", "b", "c"] : List Str
                          # val1

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:02):

With the legacy linker something's even worse. Without changing the source code, I am getting different outputs. This is definitely reading memory that it shouldn't :(

image.png

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:06):

I think this might be worth opening an issue, I don't see where I am going wrong :sweat_smile: will open the issue, one moment..

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:14):

Ok, while fiddling around for creating the issue, I narrowed the issue down even further to dbg itself, which at least is a relief :sweat_smile:

view this post on Zulip Luke Boswell (Jun 07 2023 at 09:14):

Yeah, almost certainly a bug in dbg

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:15):

If I do
dbg ({left: "abc", right: "abc"} |> findCommonItemTypes) --> prints [bug.roc 17:10] ["H", "b", "c"]

But if I do

commonItemTypes = {left: "abc", right: "abc"} |> findCommonItemTypes
dbg commonItemTypes

Then it works correctly, printing [bug.roc 19:9] ["a", "b", "c"]

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:17):

Given that this is related to dbg, I wonder if it still worth creating a ticket?

view this post on Zulip Fábio Beirão (Jun 07 2023 at 09:23):

(added bonus, re-wrote my logic with sets, works so much better :grinning_face_with_smiling_eyes:)

findCommonItemTypes : { left : Str, right : Str } -> Set Str
findCommonItemTypes = \{ left, right } ->
    leftGraphemes = Str.graphemes left |> Set.fromList
    rightGraphemes = Str.graphemes right |> Set.fromList
    leftGraphemes |> Set.intersection rightGraphemes

view this post on Zulip Notification Bot (Jun 07 2023 at 09:23):

Fábio Beirão has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC