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

Inconsistent Handling of ! Operator #7103

Closed
sajjaduc opened this issue Sep 19, 2024 · 3 comments · Fixed by #7128
Closed

Inconsistent Handling of ! Operator #7103

sajjaduc opened this issue Sep 19, 2024 · 3 comments · Fixed by #7128
Labels
bug Something isn't working can Canonicalization

Comments

@sajjaduc
Copy link

I've encountered an inconsistency in how the Roc language handles the ! operator when used within functions. Specifically, the behavior differs depending on whether the ! operator is used as a standalone statement or as the last line of a function.

The following code works as expected:

run : Task {} _
run =
    Stdout.line! "Say something:"
    input = Stdin.line!
    Stdout.line! "You said: $(input)"

However, if I remove the last two lines, it fails:

run : Task {} _
run =
    Stdout.line! "Say something:"

I am calling the run function simply as follows:

main =
    run

Note: Removing the annotation solves the problem, and so does removing the ! operator from the last line.

Tested w/ basic-cli (0.15.0).

@lukewilliamboswell
Copy link
Collaborator

Some notes investigating this bug...this is about as far as my brain can handle for today. The issue looks to be related to interaction between the top level type annotation and having a suffixed statement in that final position as this gets us to this position in desugar.rs.

In desugar_value_def_suffixed we pass in a TrySuffix as body expression here.

That will immediately unwrap giving us an

Err(EUnwrapped::UnwrappedSubExpr {
    sub_arg,
    sub_pat,
    sub_new,
    target,
})

With the following values...

[crates/compiler/can/src/desugar.rs:270:21] &sub_arg = @26-55 Apply(
    @26-55 Var {
        module_name: "Stdout",
        ident: "line",
    },
    [
        @39-55 Str(
            PlainLine(
                "Say something:",
            ),
        ),
    ],
    Space,
)
[crates/compiler/can/src/desugar.rs:270:21] &sub_pat = @26-55 Identifier {
    ident: "#!0_arg",
}
[crates/compiler/can/src/desugar.rs:270:21] &sub_new = @26-55 Var {
    module_name: "",
    ident: "#!0_arg",
}

We pass these into the apply_try_function

AnnotatedBody {
    ann_pattern,
    ann_type,
    lines_between,
    body_pattern,
    body_expr: apply_try_function(
        arena,
        body_expr.region,
        sub_arg,
        sub_pat,
        sub_new,
        Some((ann_pattern, ann_type)),
        target,
    ),
}

Which now causes issues because the sub_pat is an #!0_arg ident while the ann_pattern is run ... these don't match and so we get a false in this if branch

ident: if loc_ann_pat.value.equivalent(&loc_pat.value) {
    new_ident
} else {
    // create another pattern to preserve inconsistency
    arena.alloc(next_unique_suffixed_ident())
},

And the "create another pattern to preserve inconsistency" then causes us issues. I'm wondering if this should blow up at this point instead of creating another pattern which seems to be failing silently.

@lukewilliamboswell lukewilliamboswell added bug Something isn't working can Canonicalization labels Sep 20, 2024
@sajjaduc
Copy link
Author

Thanks for taking a look @lukewilliamboswell.

I wish I can contribute here but unfortunately I don't know Rust at all. And I could be totally out of line here but logically what makes sense to me is that it's related to the simple fact that Roc allows you to use ! operator on the last line, even though there is no 2nd task added!?! Wouldn't it be more logically correct to not allow that at all?

I guess it's time for me to go learn Rust first because I genuinely want to contribute to Roc in any way shape or form.

@Anton-4
Copy link
Collaborator

Anton-4 commented Sep 21, 2024

Wouldn't it be more logically correct to not allow that at all?

We decided to allow that for consistency but the ! syntax will also undergo significant changes in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working can Canonicalization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants