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

Incorrect Erlang code with block pattern during record update #4154

Closed
ghivert opened this issue Jan 7, 2025 · 3 comments · Fixed by #4155
Closed

Incorrect Erlang code with block pattern during record update #4154

ghivert opened this issue Jan 7, 2025 · 3 comments · Fixed by #4155
Labels
bug Something isn't working help wanted Contributions encouraged priority:high

Comments

@ghivert
Copy link

ghivert commented Jan 7, 2025

Hi !

I found a funny bug when experimenting with Gleam 1.7.0.
The following code:

pub type A {
  A(b: B)
}

pub type B {
  B(c: C)
}

pub type C {
  C(val: Int)
}

pub fn example(a: A) {
  A(..a, b: { 
    B(..a.b, c: { 
      C(..a.b.c, val: 0) 
    }) 
  })
}

Compiles incorrectly to:

example(A) ->
    _record = A,
    {a, (_record@1 = erlang:element(2, A),
        {b, (_record@2 = erlang:element(2, erlang:element(2, A)),
            {c, 0})})}.

Which is invalid, the (_record, { pattern is not working, but when removing the block pattern, like this:

pub fn example(a: A) {
  A(..a, b: 
    B(..a.b, c:  
      C(..a.b.c, val: 0) 
    ) 
  )
}

Compiles correctly to:

example(A) ->
    _record = A,
    {a,
        begin
            _record@1 = erlang:element(2, A),
            {b,
                begin
                    _record@2 = erlang:element(2, erlang:element(2, A)),
                    {c, 0}
                end}
        end}.

It triggers a hard to understand build error looking like (because of a , error)

project/src/module.gleam: spec for undefined function example/1
%   32|   A(..a, b: B(..a.b, c: { C(..a.b.c, val: 0) }))
%     |  ^

Edit: I made sure it's working with JS: everything gets compiled as IIFE, so there's no problem.

@ghivert ghivert added the bug Something isn't working label Jan 7, 2025
@lpil
Copy link
Member

lpil commented Jan 8, 2025

Oh yikes. Thank you

@lpil lpil added help wanted Contributions encouraged priority:high labels Jan 8, 2025
@joshi-monster
Copy link
Contributor

Oh no fixing this asap. sorry.

@lpil
Copy link
Member

lpil commented Jan 8, 2025

Thank you 💜

joshi-monster added a commit to joshi-monster/gleam that referenced this issue Jan 8, 2025
@lpil lpil closed this as completed in #4155 Jan 8, 2025
@lpil lpil closed this as completed in 72d54e6 Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contributions encouraged priority:high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants