Commit Graph
89 Commits
Author SHA1 Message Date
Łukasz Samson 5e44c78fff Fix compiler crash on invalid nested bitstring (#15280) 2026-04-16 15:32:48 +02:00
Lukasz Samson 5d794ab17c Pass correct bitstring meta during expansion
Closes #15244
2026-04-08 18:42:12 +02:00
José Valim bfa154e70b Consider subtyping on bitstring segments 2026-01-10 19:12:19 +01:00
Jean Klingler b87a9fa35b Do not consider variables from pattern in bitstring modifier (#14738) 2025-08-31 19:20:26 +09:00
Jonatan Männchen e1d171e5c7 Add inline License / Copyright to Erlang files (#14255) 2025-02-05 13:50:00 +01:00
José Valim e4f7ee448c Perform type inference using reverse arrows on all non-branching constructs (#14145) 2025-01-15 21:15:22 +01:00
José Valim 860f485bdf Inference of patterns (#13909)
This pull request adds inference of patterns, being able to
refine types as new information is added. The next step is
to use this inference in the compiler itself.

Future work will add module-local inference of return types
(most likely still in v1.18) and inference of guards (most
likely in v1.19).

In order to support inference of patterns, Elixir will raise
if it finds recursive variable definitions. This means
patterns that never match, such as this one, will no longer
compile:

    def foo(x = {:ok, y}, x = y)

However, recursion of root variables (where variables directly
point to each other), will also fail to compile:

    def foo(x = y, y = z, z = x)

While the definition above could succeed (as long as all three
arguments are equal), there is a much cleaner version of writing
the same code, that does not require solving cycles in our head:

    def foo(x, x, x)
2024-10-18 11:01:23 +02:00
Jean Klingler 4126cdd8ab Add --migrate flag to mix format (#13846) 2024-09-21 21:47:16 +09:00
Jean Klingler 3da9af2150 Only infer size in pinned variable when needed (#13423) 2024-03-20 18:59:21 +09:00
Jean Klingler 856da799a3 Auto infer size of matched variable in bitstrings (#13106) 2023-11-15 20:50:10 +09:00
José Valim a7adda21fd Remove always true otp_release checks 2023-10-08 13:40:37 +02:00
José Valim 7454333fd0 Require pin variable when accessing variable inside binary size in match, closes #12588 2023-05-26 16:35:36 +02:00
José Valim da637b7523 Collect function errors where possible in elixir_bitstring and elixir_expand 2022-11-28 16:15:34 +01:00
José Valim b24869687b Introduce mechanism to collect several errors in a module (#12275) 2022-11-28 15:40:30 +01:00
sabiwara e58f70a65c Refactor: use :elixir_bitstring to avoid duplication (#12271) 2022-11-25 16:00:37 +01:00
Michael Adkins 4dd730267a Add commas to bare usage of "however" where appropriate (#12119) 2022-09-06 20:29:01 +02:00
Andreas Lappe 487c24d518 Fix typo (#12068) 2022-08-12 12:49:10 +02:00
sabiwara 7dc718b292 Replace inner AST for bitstring modifiers (#12055) 2022-08-06 09:28:07 +02:00
sabiwara c64606b765 Warn on missing parentheses in bitstring modifiers (#11862) 2022-07-27 13:28:24 +02:00
José Valim 239f8f9e3b Require Erlang/OTP 24 2022-07-27 13:20:42 +02:00
José Valim 07cc16717b Allow any expression in bitstring size outside of matches/guards 2022-01-12 01:47:11 +01:00
Jorge Bejar 4c40630c08 EEP 52: Support for size expression in bitstring matching (#11558) 2022-01-11 21:42:28 +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
Eksperimental 784b881b76 Standardize use of Erlang/OTP VERSION (#10867) 2021-04-03 17:11:57 +02:00
Wojtek Mach 68661dfa91 Support 16bit floats in bitstrings (#10740)
On OTP 24:

    iex> <<x::float-16>> = <<60, 0>>
    iex> x
    1.0
    iex> <<x::float-16>>
    <<60, 0>>

Before OTP 24 we'd get errors or wouldn't match:

    iex> <<1.0::float-16>>
    ** (ArgumentError) argument error while evaluating iex at line 1

    <<x::float-16>> = <<60, 0>>
    ** (MatchError) no match of right hand side value: <<60, 0>>

    iex> (fn <<x::float-16>> -> x; _ -> :nomatch end).(<<60, 0>>)
    :nomatch
2021-02-20 11:24:58 +01:00
José Valim 63837a720b Refactor binary handling 2020-04-05 20:15:50 +02:00
José Valim ba5a9030fe Improve error message for nested binaries in match without specifiers, closes #9908 2020-04-04 12:35:46 +02:00
José Valim 05eb5b2375 Do not show binary types if they are inferred 2020-02-23 10:26:11 +01:00
Eric Meadows-Jönsson e98284fbb0 Add checking of patterns in body (#9828) 2020-02-20 16:46:06 +01:00
José Valim d3b011947f Do not expand interpolation twice, closes #9709 2020-01-12 17:19:50 +01:00
José Valim dbf8a7a9fe Use version metadata in Erlang pass 2019-11-16 02:45:55 +01:00
José Valim c74bfa4a52 Only transform environment in match if necessary 2019-10-29 17:17:53 +01:00
José Valim 1c9b55ae6d Further optimize variable merging 2019-10-29 17:17:53 +01:00
José Valim acc9b49208 Move unused_vars to its own variable 2019-10-29 17:17:53 +01:00
José Valim 4992622be1 Remove a need to merge unused vars 2019-10-29 17:17:53 +01:00
José Valim 31e4a93bc6 Clean up Macro.Env and hide private fields 2019-07-25 22:07:51 +02:00
José Valim e80b55b21c Unify error handling and compiler deadlock checking (#9009) 2019-05-02 16:27:13 +02:00
Fernando Tapia Rico 94c0dd23d2 Ensure guard checks when expanding string interpolations (#8905)
During the bitstring expansion, guard checks were avoided due to
a special case for inlining binaries during interpolation. These
changes ensure guards are checked.
2019-03-22 11:26:02 +01:00
José Valim 4c87c902c9 Ensure we support counters in bitstring size vars 2019-01-07 22:31:31 +01:00
Fernando Tapia Rico 7104ca20fd Error on undefined variables in bitstring segments (#8598)
Binary/bitstring matching allows to dynamically define the
`size` of the binary in certain conditions:

  * if the `size` variable is defined prior to the pattern
    match:

        iex> size = 8
        iex> <<a::size(size), rest::binary>> = "hello"
        iex> a
        104

  * if the `size` variable is matched within the same
    binary/bitstring match, prior to its use:

        iex> <<name_size::size(8), name::binary-size(name_size), _rest::binary>> = <<5, "Frank the Walrus">>
        iex> name
        "Frank"

Other cases are considered illegal patterns, for example:

    {name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, "Frank the Walrus"}

This commit raises a `CompileError: undefined variable ...` for such cases.
2019-01-05 17:46:33 +01:00
Wojtek Mach c0317dcd64 Expand operands in size*unit bitstring modifier (#8375) 2018-11-08 19:42:55 +01:00
Glauber Campinho 6af3110b6a Small optimization on elixir_bitstring:expand/4 (#8074)
`elixir_env:mergea(EV, EC)` is always equivalent to `EV` so we can avoid the operation.
Similar to #8069
2018-08-12 10:05:59 +02:00
José Valim 2af6e62626 Ensure Macro.expand/2 does not change the meaning of interpolations 2018-08-11 20:42:24 +02:00
Tan Jay Jun d555a7fdbc Fix error typo in elixir_bitstring.erl (#8030)
[ci skip]
2018-08-02 15:02:17 +02:00
Aleksei Magusev d1156f4161 Handle nested bitstring pattern matching failures in a better way (#7370) 2018-02-20 23:43:14 +01:00
José Valim 90cd4e7050 Merge binaries and bitstrings when possible 2018-01-25 15:40:44 -02:00
José Valim d2fb3279f1 Perform merging of bitstring subinaries 2018-01-25 09:25:09 -02:00
José Valim 9d40e110d9 Move unused_vars check to Elixir land
This gives us better control over when and how unused variables are printed.

As a result, we are able to emit unused variable warnings in situations
we could not before. This also opens up the way for us to remove a
dependency on erl_lint and track types information, which allows us to
speed up compilation times about 5% and allow us to emit more performant
code in some situations.
2018-01-20 19:45:48 +01:00
José Valim 85becb02e8 Merge match_vars and prematch_vars 2018-01-20 19:42:08 +01:00
José Valim fd4dc4474a Process variable versioning on Elixir pass 2018-01-20 19:42:08 +01:00