Skip to content

Bad compilation when using variable name result #4998

@keroami

Description

@keroami

For some reason, my code

fn do_get(event_log, id, seq_id, state, apply) -> snag.Result(#(state, Int)) {
  let result = consume_since(event_log, id, seq_id)
  use #(new_state, last_id) <- result.try(reconstruct(
    result.entries,
    0,
    state,
    apply,
  ))
  case result {
    ThereIsMore(_) -> do_get(event_log, id, last_id, new_state, apply)
    ThisIsAll(_) -> Ok(#(new_state, last_id))
  }
}

is compiled to

-file("src/lite_events/event_log.gleam", 72).
-spec do_get(
    event_log(any(), AJG),
    binary(),
    integer(),
    AEK,
    fun((AEK, AJG) -> AEK)
) -> {ok, {AEK, integer()}} | {error, snag:snag()}.
do_get(Event_log, Id, Seq_id, State, Apply) ->
    Result = consume_since(Event_log, Id, Seq_id),
    begin
        Result@1 = reconstruct(erlang:element(2, Result), 0, State, Apply),
        case Result@1 of
            {ok, X} ->
                {New_state, Last_id} = X,
                case Result@1 of
                    {there_is_more, _} ->
                        do_get(Event_log, Id, Last_id, New_state, Apply);

                    {this_is_all, _} ->
                        {ok, {New_state, Last_id}}
                end;

            {error, _inline_e_0} ->
                {error, _inline_e_0}
        end
    end.

which applies the nested case to Result@1 which is wrong (should be Result).
In this particular case, the program crashes, because the replacement causes a type mismatch.

When I try to make a simple piece of code to trigger this, i,e, when I write a program with just a method very similar to this one), there is no begin after the assignment to Result, and no assignment of the resonctruct to an erlang variable. So the program runs just fine. I don't know why there is this difference. Happy to answer questions about this.

Changing the name in gleam from result to anything else solves the conflict, so I can move on.

gleam git main, but the tagged release v1.12.0 works.
Erlang 27

Kero aka McNimble.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions