Commit Graph
91 Commits
Author SHA1 Message Date
José Valim 485ca72ae2 Infer types and use them across remote calls (#13981)
This also unifies handling of deterministic builds with Erlang/OTP
as well as warnings_as_errors.
2024-11-09 18:00:27 +01:00
José Valim c7fa5f95fc Add Macro.struct_info! 2024-10-26 09:21:04 +02:00
José Valim 514355f3d9 Type inference for structs and type checking for dot/remote (#13518)
We now warn:

* `expr.field` when `expr` may not be a map
* `expr.call()` when `expr` may not be an atom
* `expr.call(...)` when `expr` may not be an atom
* `&expr.foo/1` when `expr` may not be an atom

Furthermore, we lay down the ground work for checking undefined
and deprecation warnings across unions. For example, if you write
this code:

    mod = if something?, do: Foo, else: Bar
    mod.some_function()

In the future, it will warn if any of Foo OR Bar do not define the relevant function.
Finally, we improve pretty printing of maps and aliases in types.
2024-04-30 14:15:32 +02:00
José Valim 90c8327888 Support bitstring specifies as map keys in pattern, closes #12586 2023-05-22 23:10:53 +02:00
sabiwara e2d2a88495 Deterministic struct expansion (#12418) 2023-02-19 11:36:15 +01:00
José Valim b24869687b Introduce mechanism to collect several errors in a module (#12275) 2022-11-28 15:40:30 +01:00
José Valim 904072081a Raise on missing struct for module conflict, closes #12113 2022-09-04 16:01:37 +02:00
José Valim 2a7d58c3f7 Register functions used in local macros as exports
Ideally we want to list them as requires but we don't have
the infrastructure to do so. So meanwhile, we list them
as exports, which is the same level used by require.
2022-07-11 09:09:54 +02:00
José Valim 41a698a1f3 Wrap more of the struct expansion stacktrace 2022-06-15 20:56:00 +02:00
felipe stival 27c623cd1e EEP 52: Allow pins inside map keys in matches (#11544) 2022-01-06 00:48:04 +01:00
José Valim 5eb2efe543 Break Macro.Env apart (#11164)
Previously, the internal Elixir expansion pass worked directly on
Macro.Env. However, this poised an issue, if we want to track
more information in the pass, we ended up exposing it on
Macro.Env, making it larger, and potentially slowing down
operations such as __ENV__ serialization.

This commit refactors the expansion pass to work with two
structures, the Macro.Env struct and a #elixir_ex{} record.
2021-08-03 13:35:15 +02:00
José Valim a355450af8 Ensure waiting is tracked across typespecs
The Elixir compiler spawns a separate process per file.
When a file has to wait on another module, Elixir tracks
in the compiler that the file is waiting.

However, every time a module is defined, the Elixir compiler
spawns a separate process to compile to .beam, and this
process may expand structs in the typespec. Since this
new process is no longer the original file process, Elixir
was not able to track its waiting time.

This PR address this issue by passing the original file_pid
to the .beam compiler process. Note though that, if we
change typespecs to be compiled in the original file process,
this change is no longer required, but at the moment there are
no plans to make such change.

Closes #11036.
2021-06-05 19:21:12 +02:00
José Valim b07e17679c Add export dependencies on Macro.struct! 2021-02-11 10:58:18 +01:00
Eric Meadows-Jönsson bcab846549 Only error for duplicate map keys in matches (#9748)
In matches we will error:

    %{a: :b, a: :c} = %{a: :b}

In normal expressions we will only warn:

    %{a: :b, a: :c}
2020-01-21 18:15:35 +01:00
Eksperimental 8eef0ae581 Replace usage of "etc." (#9569) 2019-11-29 08:22:18 +01:00
José Valim 338ba06b59 Add struct keys to tracing on struct expansion 2019-09-01 09:52:18 -06:00
Eric Meadows-Jönsson 475076adfd Support struct patterns (#9306) 2019-08-22 13:28:05 -07:00
José Valim 9790329a08 Add compiler tracing (#9247)
This feature allows IDEs and other tools wanting to
perform source code analysis to do so reliably without
a need to reimplement Elixir's compiler expansion and
without relying on Elixir's private APIs.

This commit also adds :parser_options to compiler
options, which allows developers to combine both options
to retrieve more accurate information, such as columns.
2019-08-02 13:35:12 +02:00
Eric Meadows-Jönsson 772c078651 Remove dispatch tracking in LexicalTracker (#9228) 2019-07-18 13:48:02 +02:00
José Valim cd9dbd3daa Improve undefined struct messages 2019-06-10 11:19:00 +02:00
José Valim e80b55b21c Unify error handling and compiler deadlock checking (#9009) 2019-05-02 16:27:13 +02:00
José Valim 7df4932aaa Allow Code.ensure_compiled/2 to not raise on deadlocks
This is important when you may want to check if a module
is available but if that module is running a similar check
on you, you don't want the compiler to fail.
2019-04-01 13:04:00 +02:00
Fernando Tapia Rico 828f6e6b4d Ensure the struct name matches the given module
This change fixes inconsistencies when building structs: if the
struct was built using the map syntax the `:__struct__` key
was overridden by the given module name; however, if the struct
was built using `struct/1,2` the `:__struct__` key was kept.

For example:

    defmodule X do
      defstruct [:x]
    end

    defmodule Y do
      defdelegate __struct__, to: X
      defdelegate __struct__(args), to: X
    end

    iex> %Y{}
    %Y{x: nil}

    iex> struct(Y)
    %X{x: nil}

Closes #8800
2019-03-31 09:52:58 +02:00
Fernando Tapia Rico 21789c68a1 Use inspect/1 with values returned by __struct__/* (#8873)
__struct__/0,1 is not meant to return an AST. Using
Macro.to_string/1 might lead to incorrect error messages:

    defmodule MyStruct do
      def __struct__, do: {:ok, :one, :two}
      def __struct__(_), do: {:ok, :one, :two}
    end

    iex> %MyStruct{}
    ** (CompileError) iex:2: expected MyStruct.__struct__/1 to
       return a map with a :__struct__ key that holds the name
       of the struct (atom), got: ok
2019-03-08 23:24:45 +01:00
Fernando Tapia Rico ea4e367a78 Use :elixir_aliases.inspect/1 with module aliases (#8869)
The `Module` variable in these cases is guaranteed to be a module
alias.
2019-03-08 22:49:55 +01:00
Fernando Tapia Rico 8a77ef170a Validate __struct__ key in map returned by __struct__/0,1 (#8864) 2019-03-07 12:12:30 +01:00
José Valim 3418f06ee8 Resolve circular struct dependencies in typespecs (#8377)
Prior to this patch, if a module depends on the struct of
another module and that other module depends on the struct
of the first module inside typespecs, it would lead to a
compiler deadlock, which this deadlock would not happen
in "regular code".

This commit also adds a test to ensure @enforce_keys are
not enforced when expanding typespecs.

For now, we are using a compiler private API, but we should
provide a public version for it in a future commit.
2018-11-13 17:47:24 +01:00
Eksperimental 477c821ef0 Use "etc." and not "etc" (#8379)
Command used to detect these typos:
$ ag -s "\betc[^.]"
2018-11-10 14:04:49 +01:00
Fernando Tapia Rico 633f8ba817 Avoid errors with more than one duplicated key (#8346)
Closes #8345
2018-10-26 17:44:09 +02:00
Glauber Campinho 2d9dece6cb Optimizes multiple :maps put and merge operations (#8223)
Optimizes :maps.put/3 to a VM operation instead of a remote call, including nested calls to it.
It does the same for :maps.merge/2 with a simple map on the right side.

Closes #7353
2018-09-28 08:40:27 +02:00
Eksperimental a00394429e "Fall back" the verb is 2 separate words (#8131) 2018-08-27 12:10:22 +02:00
Fernando Tapia Rico 79b63d08fa Use ETS instead of ets in documentation (#8099) 2018-08-17 00:35:49 +02:00
Michał Muskała 25aede7984 Fix all compiler warnings (#7992) 2018-07-27 16:42:24 +02:00
Michał Muskała 6ebeabf0b0 Separate testing for unquotes from escaping 2018-07-25 00:10:02 +02:00
José Valim 94c3149ca9 Allow to prune metadata while escaping (#7949)
We use this option in ExUnit as we are escaping
the code to eventually convert it to a string
representation and therefore the metadata is not
relevant.
2018-07-21 12:56:07 +02:00
José Valim 62872cfcca Improve error messages for bad map key patterns 2018-06-25 16:56:57 +02:00
Derek Kraan c955b2cc4e Include caller information on struct expansion errors
Closes #7752
2018-06-21 18:48:55 +02:00
José Valim 8dc7f0a96b Raise on invalid pin in maps, closes #7543 2018-04-10 20:49:53 +02:00
José Valim ed5ec1eaf9 Simplify key check in map match 2018-02-10 21:13:52 +01:00
Michał Muskała 9e079d30b7 Separate tracking structs from compile-time dependencies
This is a first step to limiting recompilations in case a struct is used in a
module, the "parent module" of the struct changes, but struct definition itself
does not change.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2017-10-31 14:24:48 +01:00
Ivan Kozik 53acbc7ac2 Fix more typos and spelling errors (#6530) 2017-09-01 10:20:53 +02:00
José Valim ad4880ab98 Always use in_context definition for struct expansion, closes #6409 2017-07-31 06:39:26 +02:00
José Valim 710dd2b012 Do not remove structs syntax in Elixir pass 2017-05-20 20:50:54 +02:00
Michał Muskała d8d412cbf2 Make sure System.stracktrace is first thing done in the handler (#6119)
If any of the functions catches an error, we'd get a wrong trace
2017-05-18 12:27:17 +02:00
Andrea Leopardi 159f9d3328 Warn when a :__struct__ key is used when building/updating structs (#6113)
Until now, "%x{__struct__: y}" would ignore the ":__struct__" part and only
use/match on "x", without warnings. With this commit, a warning is emitted in
this case and in the respective case when updating ("%SomeStruct{x | __struct__:
y}").
2017-05-15 20:56:02 +02:00
José Valim 7f8214ebfd Remove warnings on OTP 20 2017-05-07 10:28:08 +02:00
José Valim 5cef69c7e6 Clarify docs 2017-03-15 16:04:36 +01:00
José Valim 922139da50 Solve race conditions on struct dispatch 2017-03-15 15:46:52 +01:00
José Valim f37c4e001f Add prematch_vars 2017-02-23 10:43:09 +01:00
Andrea Leopardi fbbc38584a Replace elixir_errors:compile_error/4 with form_error/4 in elixir_map 2017-02-20 13:24:24 +01:00