Skip to content

Commit 9f465da

Browse files
committed
Fix tuple atom formatting
1 parent 8d6df81 commit 9f465da

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- The minimum supported Gleam version has been increased to 1.9.0.
66
- The functions in the `bit_array` module now support unaligned bit arrays on
77
the JavaScript target.
8+
- Fixed a bug where tuples with atoms in the first position could be formatted
9+
incorrectly by `string.inspect`.
810

911
## v0.56.0 - 2025-03-09
1012

src/gleam_stdlib.erl

+16-9
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,7 @@ inspect(Data) when is_map(Data) ->
356356
],
357357
["dict.from_list([", lists:join(", ", Fields), "])"];
358358
inspect(Atom) when is_atom(Atom) ->
359-
Binary = erlang:atom_to_binary(Atom),
360-
case inspect_maybe_gleam_atom(Binary, none, <<>>) of
361-
{ok, Inspected} -> Inspected;
362-
{error, _} -> ["atom.create_from_string(\"", Binary, "\")"]
363-
end;
359+
erlang:element(2, inspect_atom(Atom));
364360
inspect(Any) when is_integer(Any) ->
365361
erlang:integer_to_list(Any);
366362
inspect(Any) when is_float(Any) ->
@@ -386,10 +382,15 @@ inspect(Any) when is_tuple(Any) % Record constructors
386382
andalso element(1, Any) =/= nil
387383
->
388384
[Atom | ArgsList] = erlang:tuple_to_list(Any),
389-
Args = lists:join(<<", ">>,
390-
lists:map(fun inspect/1, ArgsList)
391-
),
392-
[inspect(Atom), "(", Args, ")"];
385+
InspectedArgs = lists:map(fun inspect/1, ArgsList),
386+
case inspect_atom(Atom) of
387+
{gleam_atom, GleamAtom} ->
388+
Args = lists:join(<<", ">>, InspectedArgs),
389+
[GleamAtom, "(", Args, ")"];
390+
{erlang_atom, ErlangAtom} ->
391+
Args = lists:join(<<", ">>, [ErlangAtom | InspectedArgs]),
392+
["#(", Args, ")"]
393+
end;
393394
inspect(Tuple) when is_tuple(Tuple) ->
394395
Elements = lists:map(fun inspect/1, erlang:tuple_to_list(Tuple)),
395396
["#(", lists:join(", ", Elements), ")"];
@@ -403,6 +404,12 @@ inspect(Any) when is_function(Any) ->
403404
inspect(Any) ->
404405
["//erl(", io_lib:format("~p", [Any]), ")"].
405406

407+
inspect_atom(Atom) ->
408+
Binary = erlang:atom_to_binary(Atom),
409+
case inspect_maybe_gleam_atom(Binary, none, <<>>) of
410+
{ok, Inspected} -> {gleam_atom, Inspected};
411+
{error, _} -> {erlang_atom, ["atom.create_from_string(\"", Binary, "\")"]}
412+
end.
406413

407414
inspect_maybe_gleam_atom(<<>>, none, _) ->
408415
{error, nil};

test/gleam/string_test.gleam

+8-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ pub fn utf_codepoint_test() {
724724
string.utf_codepoint(65_535)
725725
|> should.be_ok
726726

727-
// One less than the lowest "High-surrogate code point"
727+
// One less than the lowest "High-surrogate code point"
728728
string.utf_codepoint(55_295)
729729
|> should.be_ok
730730

@@ -1385,6 +1385,13 @@ pub fn inspect_erlang_atom_with_uppercases_invalid_in_gleam_test() {
13851385
|> should.equal("atom.create_from_string(\"Upper\")")
13861386
}
13871387

1388+
@target(erlang)
1389+
pub fn inspect_erlang_atom_tag_tuple_test() {
1390+
#(string_to_erlang_atom("DOWN"), 1, 2)
1391+
|> string.inspect
1392+
|> should.equal("#(atom.create_from_string(\"DOWN\"), 1, 2)")
1393+
}
1394+
13881395
@target(erlang)
13891396
pub fn inspect_erlang_atom_with_leading_digit_invalid_in_gleam_test() {
13901397
string_to_erlang_atom("1_ok")

0 commit comments

Comments
 (0)