This commit also simplifies macro expansion for both imports and aliases
to always give higher preference to the macro environment rather than
the caller one, if such is available. The deal is to simplify both the
code and understanding of how macros work.
Prior to this commit, the following code would work:
defmodule Hello do
defmacro write do
quote do
a = 1
end
end
defmacro read do
quote do
a
end
end
end
require Hello
Hello.write
Hello.read
Although this is convenient, it has many issues:
1. It is not clear at any point the intent of sharing the variables
in between the macros
2. Whenever macros from the same module are nested, there is a chance
a variable used before and after the nesting to be overriden
We address this issue by only allowing variables explicitly marked
with var! to be shared. Here is how the example above should be updated:
defmodule Hello do
defmacro write do
quote do
var!(a, Hello) = 1
end
end
defmacro read do
quote do
var!(a, Hello)
end
end
end
require Hello
Hello.write
Hello.read
This refactoring helps us clean up the interpolation code and
guarantee it works as expected since it goes under the same
tokenization rules. It also helps provide better sigil handling
(going to be improved in upcoming commits).
This provides a more robust way of checking from where
the original expressions are coming from compared to the
lexical check_clauses field we had in elixir_scope.
A direct consequence of this commit is that, the following
code:
defrecord Sample, [:hello] do
import List
end
Now raises a warning if the List import is not used,
where it didn't previously.
This means the second item in the AST is no longer an integer
(representing the line), but a keywords list. Code that relies
on the line information from AST or that manually generate AST
nodes need to be properly updated.
the same syntax and be case insensitive. Also relocated the character type macros
into lib/elixir/include/elixir.hrl so that they didn't need to be defined in both
lib/elixir/src/elixir_interpolation.erl and lib/elixir/src/elixir_tokenizer.erl.
Added to the erlang and elixir unit tests to test the new syntax.
The following code:
counter = 1
counter = 2
Is now rewritten internally to:
counter = 1
counter@1 = 2
Which gives us a couple things:
1. Better debugging
2. We can now show warnings for rebound variables not used
Elixir also keeps `_@` prefix to store internal variables
(for example, `_@1`, `_@MODULE`, etc).
Closes#607.