From 0f5c4c923208b63ab645341883637318775d81d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 14 Sep 2017 07:05:29 +0200 Subject: Document that the compiler may optimize away atoms The following code could fail in the call to list_existing_atom/1: String = atom_to_list(some_atom), Atom = list_to_existing_atom(String) because the compiler will rewrite the code to: String = "some_atom", Atom = list_to_existing_atom(String) If some_atom is not used in another place, it will not exist. The compiler could be updated to preserve the atom in this simple case, but it would be hard to make sure that the compiler never loses atoms that exist in the source text. It also difficult to imagine a real world use case where this would be a problem. If an atom is mentioned only in a way that the compiler can optimize away, is there really any need to create the atom at all in list_to_existing_atom/1? Therefore, it is better to place the responsibility that the atom exists on the user of list_to_existing_atom/1. Update the documentation to mention that the compiler may optimize away atoms. https://bugs.erlang.org/browse/ERL-453 --- erts/doc/src/erlang.xml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'erts/doc/src') diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index 5afac46d21..2465f49581 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -432,6 +432,16 @@ Z = erlang:adler32_combine(X,Y,iolist_size(Data2)). binary_to_atom/2, but the atom must exist.

Failure: badarg if the atom does not exist.

+ +

Note that the compiler may optimize away atoms. For + example, the compiler will rewrite + atom_to_list(some_atom) to "some_atom". If + that expression is the only mention of the atom + some_atom in the containing module, the atom will not + be created when the module is loaded, and a subsequent call + to binary_to_existing_atom(<<"some_atom">>, utf8) + will fail.

+
@@ -2478,6 +2488,15 @@ os_prompt% but only if there already exists such atom.

Failure: badarg if there does not already exist an atom whose text representation is String.

+ +

Note that the compiler may optimize away atoms. For + example, the compiler will rewrite + atom_to_list(some_atom) to "some_atom". If + that expression is the only mention of the atom + some_atom in the containing module, the atom will not + be created when the module is loaded, and a subsequent call + to list_to_existing_atom("some_atom") will fail.

+
-- cgit v1.2.3