Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot define a function using the try operator ? in an expect statement #7081

Closed
ageron opened this issue Sep 15, 2024 · 2 comments · Fixed by #7115
Closed

Cannot define a function using the try operator ? in an expect statement #7081

ageron opened this issue Sep 15, 2024 · 2 comments · Fixed by #7115

Comments

@ageron
Copy link
Contributor

ageron commented Sep 15, 2024

The following code fails with roc nightly pre-release, built from commit 9a4d556 on Mon Sep 9 09:02:33 UTC 2024:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}

import pf.Stdout

inc = \i ->
    if i > 2 then
        Err MaxReached
    else
        Ok (i + 1)

expect
    run = \i ->
        newi =
            i
            |> inc?
            |> inc?
        Ok newi
    result = run 0
    result == Ok 2

main =
    Stdout.line! "Hello world"

The error is:

An internal compiler expectation was broken.
This is definitely a compiler bug.
Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>
a Expr::TrySuffix expression was not completely removed in desugar_value_def_suffixed
Location: crates/compiler/can/src/expr.rs:1125:40

The problem disappears if I define the run function outside the expect statement. However, I'd like to avoid polluting the global scope, and I'd like to keep the function tied to the expect statement.

@ageron
Copy link
Contributor Author

ageron commented Sep 21, 2024

Here's a shorter example, in a module:

# Inc.roc
module [inc]

inc = \i -> if i > 2 then Err TooBig else Ok (i + 1)

expect
    result = inc? 0
    result == 1

Just run roc test Inc.roc

@ageron
Copy link
Contributor Author

ageron commented Sep 27, 2024

🥳 thanks @lukewilliamboswell !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant