Skip to content

Commit

Permalink
Fix echo tuple format
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Mar 10, 2025
1 parent 2bbe1dc commit ab3b38a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

### Bug fixes

- Fixed a bug where tuples with atoms in the first position could be
incorrectly formatted by `echo`.

## v1.9.1 - 2025-03-10

### Formatter
Expand Down
20 changes: 15 additions & 5 deletions compiler-core/templates/echo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,24 @@ inspect@list(List) ->
inspect@map(Map) ->
Fields = [
[<<"#(">>, echo@inspect(Key), <<", ">>, echo@inspect(Value), <<")">>]
|| {Key, Value} <- maps:to_list(Map)
|| {Key, Value} <- maps:to_list(Map)
],
["dict.from_list([", lists:join(", ", Fields), "])"].

inspect@record(Record) ->
[Atom | ArgsList] = erlang:tuple_to_list(Record),
Args = lists:join(", ", lists:map(fun echo@inspect/1, ArgsList)),
[echo@inspect(Atom), "(", Args, ")"].
[Atom | ArgsList] = Tuple = erlang:tuple_to_list(Record),
case inspect@maybe_gleam_atom(Atom, none, <<>>) of
{ok, Tag} ->
Args = lists:join(", ", lists:map(fun echo@inspect/1, ArgsList)),
[Tag, "(", Args, ")"];
_ ->
inspect@tuple(Tuple)
end.

inspect@tuple(Tuple) when erlang:is_tuple(Tuple) ->
inspect@tuple(erlang:tuple_to_list(Tuple));
inspect@tuple(Tuple) ->
Elements = lists:map(fun echo@inspect/1, erlang:tuple_to_list(Tuple)),
Elements = lists:map(fun echo@inspect/1, Tuple),
["#(", lists:join(", ", Elements), ")"].

inspect@function(Function) ->
Expand Down Expand Up @@ -147,6 +154,9 @@ inspect@proper_or_improper_list(List) ->
{improper, [echo@inspect(First), " | ", echo@inspect(ImproperRest)]}
end.

inspect@maybe_gleam_atom(Atom, PrevChar, Acc) when erlang:is_atom(Atom) ->
Binary = erlang:atom_to_binary(Atom),
inspect@maybe_gleam_atom(Binary, PrevChar, Acc);
inspect@maybe_gleam_atom(Atom, PrevChar, Acc) ->
case {Atom, PrevChar} of
{<<>>, none} ->
Expand Down
2 changes: 2 additions & 0 deletions test-output/cases/echo_non_record_atom_tag/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "echo_non_record_atom_tag"
version = "1.0.0"
10 changes: 10 additions & 0 deletions test-output/cases/echo_non_record_atom_tag/src/main.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn main() {
echo #(to_atom("UP"), 1, 2)
echo #(to_atom("down"), 12.34)
echo #(to_atom("Both"), "ok")
}

pub type Atom

@external(erlang, "erlang", "binary_to_atom")
pub fn to_atom(string: String) -> Atom
5 changes: 5 additions & 0 deletions test-output/src/tests/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,8 @@ fn echo_string() {
fn echo_tuple() {
assert_echo!("echo_tuple");
}

#[test]
fn echo_non_record_atom_tag() {
assert_echo!(Target::Erlang, "echo_non_record_atom_tag");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: test-output/src/tests/echo.rs
assertion_line: 191
expression: output
snapshot_kind: text
---
--- main.gleam ----------------------
pub fn main() {
echo #(to_atom("UP"), 1, 2)
echo #(to_atom("down"), 12.34)
echo #(to_atom("Both"), "ok")
}

pub type Atom

@external(erlang, "erlang", "binary_to_atom")
pub fn to_atom(string: String) -> Atom


--- gleam run output ----------------
src/main.gleam:2
#(atom.create_from_string("UP"), 1, 2)
src/main.gleam:3
Down(12.34)
src/main.gleam:4
#(atom.create_from_string("Both"), "ok")

0 comments on commit ab3b38a

Please sign in to comment.