Commit Graph
105 Commits
Author SHA1 Message Date
Jean Klingler 4a74923ebb Minor improvements to invalid struct key error (#15852) 2026-09-05 16:48:42 +09:00
Alexander Gubarev c929e885bb Fix struct key malformed error message (#15735) 2026-08-12 23:46:03 +02:00
José Valim 002e382794 Raise when loading struct info 2026-05-04 10:09:24 +02:00
José Valim 3120d4539b Type checked struct operations must not deadlock the compiler 2026-05-04 01:24:42 +02:00
José Valim facde14525 Inline struct loading 2026-05-04 01:24:42 +02:00
José Valim a0ef7f0c3e Move struct validation in patterns and updates to type checker
This means changing a struct definition only cases
recompilation when structs are created, no longer on
matches and updates.

Closes #15098.
2026-02-23 14:24:29 +01:00
José Valim e07f0117bd Improve error message when escaping default values with custom rules in structs, closes #14817 2025-10-07 14:26:02 +02:00
José Valim 36b0a69d3c Ensure escaping works within struct fields, closes #14817 2025-10-07 13:07:18 +02:00
José Valim 08d3865fad Transform the struct update syntax into a type assertion
This transforms the struct update into a type assertion,
requiring the type system to be sure the expression has
precisely the given struct type.

The struct update syntax may still be deprecated in the
future but this will provide a safer migration path and
allow us to engage in more conversations with the community.
2025-06-25 19:30:04 +02:00
Joe Yates 46b5fabc63 Fix use of prefer with '-ing' (#14568) 2025-06-10 11:17:04 +02:00
José Valim fb19dec15c Include lines in == Compilation error in file ... == slogans (#14538)
Closes #14535.
2025-05-27 10:31:17 +02:00
Jonatan Männchen e1d171e5c7 Add inline License / Copyright to Erlang files (#14255) 2025-02-05 13:50:00 +01:00
José Valim 9628bf83d9 Show non-expanded struct assocs in struct update deprecation warning 2025-01-22 17:22:01 +01:00
José Valim 83a70d799c Deprecate struct update syntax
The struct update syntax was added early in Elixir to help validate
at compile-time that the update keys were valid. However, for a couple
releases already, Elixir's static analysis can perform such validation
more reliably and find more error if you pattern match on the struct
when the variable is defined instead.

This deprecation simplifies the language and pushes developers to
better alternatives.

Closes #13974.
2025-01-18 10:08:31 +01:00
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