Compare commits

...
613 Commits
Author SHA1 Message Date
José Valim 524528b973 Add branch docs publishing 2023-05-30 11:23:49 +02:00
jendis a64d42f5d3 Fix elixir test assertion for erlang >=OTP-24.1 (#11309) 2021-10-13 23:48:14 +02:00
Tobias Pfeiffer 7636046bd9 Add missing --warnings-as-errors on mix test to Changelog (#11303)
Sources:
* https://dashbit.co/blog/tests-with-warnings-as-errors
* https://hexdocs.pm/mix/master/Mix.Tasks.Test.html
2021-10-08 13:30:42 +02:00
Justin Morrow 552b5abed8 docs(code.ex): fix ensure_loaded typos in Code docs (#11253)
Fixes two documentation typos in the code.ex `@moduledoc`
`ensure_lodead!` => `ensure_loaded!`
`ensure_lodead?` => `ensure_loaded?`
2021-09-17 08:50:38 +02:00
José Valim 74bfab8ee2 Release v1.12.3 2021-09-05 10:02:59 +02:00
José Valim e00cbe16cd Revert content tracking instead of mtime. 2021-08-24 08:31:52 +02:00
Marc-André Lafortune 9df7a548ed Make sure .beam files still exist for restored files (#11187) 2021-08-18 07:51:50 +02:00
José Valim 9137fd1cb2 Do not return context tuples on optimized eval 2021-07-22 08:10:38 +02:00
José Valim 5189b292ac Purge v8 manifests properly 2021-07-16 21:37:57 +02:00
Marc-André Lafortune 74f9a299ea Rely on modification time and hash to determine modified sources (#11080) 2021-07-16 21:37:57 +02:00
José Valim 2ce9d25552 Do not error autocompletion with module attribute, closes #11120 2021-07-16 19:21:36 +02:00
José Valim b558142819 Rename exit_code to exit_status for consistency 2021-07-15 20:52:20 +02:00
Łukasz Samson d7f96d8332 Keep backwards compatibility on Range and Date.Range (#11117) 2021-07-14 13:32:03 +02:00
José Valim 010efd8ec6 Fix regex 2021-07-13 11:29:26 +02:00
José Valim c0174044bc Fix regex 2021-07-13 11:12:08 +02:00
José Valim 616e0d8ccc Do not crash when handling ambiguity errors, closes #11111 2021-07-13 10:36:14 +02:00
José Valim 13ba96be3c Keep backwards compatibility on range usage, closes #11110 2021-07-12 12:32:22 +02:00
José Valim edb5e7e052 Document backwards incompatible change, closes #11105 2021-07-06 15:01:41 +02:00
José Valim 58ca6524f8 Release v1.12.2 2021-07-01 18:15:34 +02:00
José Valim afcefb3e21 Run formatter 2021-06-26 09:33:24 +02:00
José Valim fb5aabf05a Remove warning and improve coverage on Enum.into/2 tests 2021-06-26 09:31:02 +02:00
José Valim d9a81101dd Optimize Enum.into and Map.new 2021-06-26 09:26:45 +02:00
José Valim 8fca53ad31 Ensure deprecated macros emit warnings 2021-06-23 22:15:43 +02:00
José Valim 76dfedb2e3 Compile most recently changed files first 2021-06-22 20:07:38 +02:00
Christopher Keele 8bec593f5d Ensure unconstrained rebar deps generate valid mix specifications (#11063) 2021-06-15 10:25:33 +02:00
TORIFUKUKaiou 03859fb92e Add @doc since: "1.12.0" and comments (#11046) 2021-06-08 09:07:35 +02:00
José Valim 53e49f33f1 Update CHANGELOG 2021-06-08 09:03:06 +02:00
Marc Riera c0bdd6101f Improve docs for sigil_r (#11051) 2021-06-08 08:59:24 +02:00
José Valim e7cb210712 Do not add compile time deps on args to Application.compile_env/2, closes #11052 2021-06-08 08:59:21 +02:00
TORIFUKUKaiou 4a4657e413 Add @doc since: "1.12.0" (#11039) 2021-06-05 22:21:21 +02:00
José Valim fcada17819 Align profiling times for better readability
Before we would write:

    lib/foo.ex compiled in 10ms (plus 100ms waiting)

However, because the file name is variable, it was
hard to see which file took the longest. Now we will
write:

        10ms compiling +    100ms waiting for lib/foo.ex

By right aligning all times, we can easily look in
the first row and see which file took the longest.
We can also provide a summary option in the future,
if necessary.
2021-06-05 22:21:15 +02:00
José Valim 6be8bddc02 Also annotate temp var for remote call in map.foo as generated
Closes #11030.
2021-06-01 15:04:23 +02:00
José Valim 9feda914dc Simplify reading of apps from deps 2021-06-01 11:53:12 +02:00
José Valim 62e1d71377 Avoid central bottleneck when loading apps if we know their source 2021-06-01 10:24:18 +02:00
José Valim d6ed6d18ef Remove duplication across branches 2021-06-01 09:37:47 +02:00
José Valim e024a12c50 Load apps concurrently 2021-06-01 09:36:25 +02:00
José Valim 42807d288e Release v1.12.1 2021-05-28 17:34:14 +02:00
José Valim 3b8d7fa15d Update CHANGELOG 2021-05-28 11:41:32 +02:00
José Valim d1d5e04319 Revert "Run all available tests if there are no pending --failed tests"
This reverts commit ae48325991.
2021-05-27 15:53:28 +02:00
José Valim ba1fd1abb8 Improve error message on invalid config 2021-05-26 19:14:28 +02:00
Wojtek Mach 8fd6c8f845 Ensure Mix.install/2 can be called after errors with deps (#11011) 2021-05-26 19:14:04 +02:00
Wojtek Mach 95dcbe9fe0 Mix.install/2: Expand paths for path deps (#11012) 2021-05-26 19:14:04 +02:00
José Valim 98a79dc839 Fix spec for which_children, closes #11005 2021-05-25 09:46:59 +02:00
José Valim 29444793b7 Fix formatter integration test 2021-05-22 21:24:58 +02:00
José Valim ff3674ddef Format multiline inside interpolation on first run, closes #10996 2021-05-22 20:46:05 +02:00
José Valim 0460017c5c Revert keeping of underscores between digits in camelize, closes #10995 2021-05-21 14:11:13 +02:00
José Valim f3b870857a Split on \r\n and \n accordingly during unicode compilation, closes #10991 2021-05-20 12:12:55 +02:00
José Valim faa2f35d8e Fix bootstrapping issue 2021-05-19 23:23:55 +02:00
José Valim 3339168330 Silence httpc warnings 2021-05-19 23:13:56 +02:00
José Valim 1c3ee4ed72 Remove unecessary copying of priv/include for Rebar 3 deps 2021-05-19 19:30:40 +02:00
José Valim 926455ca24 Add date to CHANGELOG 2021-05-19 14:36:49 +02:00
José Valim f73e318bce Release v1.12.0 2021-05-19 12:10:36 +02:00
Jason Axelson 1f3b022340 Improve ExUnit seed docs (#10962)
Make it more obvious what a seed of 0 does.
2021-05-19 12:08:12 +02:00
Preston Guillory f82f2c7b3e Optimize Enum.unzip/1 for lists (#10963)
The existing unzip implementation is generic for all enumerable types.
However, large optimizations are available if the input is a list.

First, instead of doing two reverse operations at the end, we can do
one at the beginning. This optimization is unavailable in the existing
generic implementation in order to avoid materializing an enumerable
that might be generated lazily.

Second, we can accumulate the two output lists in two separate parameters,
rather than in the reduce function's accumulator. This avoids generating a
tuple per input element that would just be matched away and discarded
by Enum.reduce/3.

Testing with Benchfella shows a roughly 2x to 3x performance improvement
depending on the size of the list.
2021-05-19 12:08:09 +02:00
José Valim 63e6f24457 Add IO.stream/0 and IO.binstream/0 as convenience functions 2021-05-19 12:07:51 +02:00
José Valim aa25920f75 Improve error message for expression after keyword
For maps and lists:

    $ elixir -e "%{foo: :bar, :baz => :bat}"
    ** (SyntaxError) nofile:1: unexpected expression after keyword list. Keyword lists must always come last in lists and maps. Therefore, this is not allowed:

        [some: :value, :another]
        %{some: :value, another => value}

    Instead, reorder it to be the last entry:

        [:another, some: :value]
        %{another => value, some: :value}

    Syntax error after: ','
        (elixir 1.13.0-dev) lib/code.ex:655: Code.eval_string_with_error_handling/3

For calls:

    $ elixir -e "call foo: :bar, :baz"
    ** (SyntaxError) nofile:1: unexpected expression after keyword list. Keyword lists must always come as the last argument. Therefore, this is not allowed:

        function_call(1, some: :option, 2)

    Instead, wrap the keyword in brackets:

        function_call(1, [some: :option], 2)

    Syntax error after: ','
        (elixir 1.13.0-dev) lib/code.ex:655: Code.eval_string_with_error_handling/3

Closes #10973.
2021-05-19 12:07:36 +02:00
José Valim 64dcd3f937 Improve unused type variable message, closes #10985 2021-05-19 12:07:33 +02:00
José Valim 4c6f393bd0 Update CHANGELOG.md 2021-05-16 20:17:40 +02:00
José Valim d11396204b Update CHANGELOG 2021-05-16 13:52:45 +02:00
José Valim 05ab231bc6 Remove REBAR_BARE_COMPILER_OUTPUT_DIR to fix issues with build_embedded
See #10978.
2021-05-16 13:51:19 +02:00
Eksperimental 9fcdf234dd Add OTP 24.0 to Linux CI (#10980) 2021-05-14 07:38:51 +02:00
Eksperimental 14afabb7c8 Fix typo (#10979) 2021-05-14 07:38:51 +02:00
José Valim 2d5141d69d Specify how we track OTP versions 2021-05-13 09:17:10 +02:00
José Valim 480c19cb5b Respect keywords in access in code formatter 2021-05-11 12:37:58 +02:00
felipe stival 1c9d7365db Fix signature expansion when no docs (#10971)
We were returning an error whenever there wasn't a list of docs. This
changes it so it returns an empty list and the expansion can proceed.
2021-05-11 08:47:55 +02:00
José Valim a5caf4bf56 Require Erlang/OTP 22+ 2021-05-06 19:20:14 +02:00
José Valim 767e3479df Require Erlang/OTP 22+ 2021-05-06 19:14:44 +02:00
Eksperimental 863c49899c Update Enum.{random, shuffle}/1 docs to OTP 22 (#10848) 2021-05-06 19:14:30 +02:00
José Valim 5467b4e335 Remove TODOs related to Erlang/OTP 22+ 2021-05-06 19:14:26 +02:00
Łukasz Samson 7aec544b22 Use :erlang.system_info(:ets_count) introduced in OTP 22 (#10841) 2021-05-06 19:14:22 +02:00
Eksperimental f71f37e43d Remove Integer.to_string/1 and Integer.to_charlist/1 (#8777)
They are not needed, since they are covered by their /2 version.
2021-05-06 19:14:18 +02:00
Wojtek Mach ff96fa2834 Add @doc since to System.shell/2 (#10967) 2021-05-06 09:08:07 +02:00
José Valim 49bc4fc0ea Add System.shell/2 (#10965) 2021-05-05 21:05:05 +02:00
Adam Rutkowski fe3c393c3f Disallow streaming with max_concurrency=0 (#10966) 2021-05-05 21:05:05 +02:00
Wojtek Mach d9d70158a6 Mix.install/2: Recover from deps.get failures (#10958) 2021-04-27 23:13:43 +02:00
José Valim a582bd931f Fix CI on Windows 2021-04-25 19:55:38 +02:00
Joe f358cd4977 Update CHANGELOG.md (#10953) 2021-04-25 19:45:18 +02:00
José Valim 47b4952d73 Merge CHANGELOG 2021-04-24 18:32:54 +02:00
José Valim ce321f8cf4 Release v1.12.0-rc.1 2021-04-24 16:49:35 +02:00
José Valim 77fa0c4f46 Handle new significant key in sasl reports 2021-04-23 17:16:10 +02:00
José Valim 3842dcce87 Update CHANGELOG 2021-04-23 11:55:16 +02:00
Eksperimental 22f2fa9ddc Add missing backtick in Code.cursor_context/2 (#10943) 2021-04-22 15:32:56 +02:00
Wojtek Mach 148cdcec14 Update Code.cursor_context/2 docs (#10931) 2021-04-20 12:37:46 +02:00
Wojtek Mach c13335ffd4 Add Module.reserved_attributes/0 (#10926) 2021-04-20 12:37:46 +02:00
José Valim d85d13fb28 Code.cursor_context/2 (#10915) 2021-04-20 12:37:46 +02:00
Eksperimental 0b4eb72609 Fix table in Operators page (#10930)
It was broken due to the changes in d9a23d0d38
2021-04-20 09:09:07 +02:00
José Valim a83fddb3c8 Do not crash on Path.absname(c:) (#10919) 2021-04-17 22:05:58 +02:00
José Valim 81d04d80fa Make sure we traverse all files when looking for definitions 2021-04-12 13:15:12 +02:00
José Valim ccb554614d Always pop the project stack 2021-04-12 12:36:11 +02:00
sabiwara 02d0567f05 Fix regression in Enum.dedup/1 (#10886) 2021-04-09 13:18:14 +02:00
José Valim 4582f87faf Update CHANGELOG 2021-04-09 13:13:16 +02:00
José Valim 274557b131 Fix bootstrap warning 2021-04-09 00:30:15 +02:00
José Valim d7946202bf Fix Mix suite with async_run and await_run 2021-04-09 00:11:45 +02:00
José Valim 9cfa855237 Support explicit ExUnit.run 2021-04-08 23:01:10 +02:00
José Valim 381991149c Explicitly mention relative and absolute URIs, closes #10884 2021-04-08 08:17:28 +02:00
Michał Łępicki bd38b6ed4c Fix grammar in Logger backends docs (#10883) 2021-04-08 08:17:28 +02:00
José Valim b571ddb3e0 Update Logger backend recommendations 2021-04-07 19:36:09 +02:00
José Valim 877b3d929b Only try to inspect git! failures if response is a binary 2021-04-07 19:15:25 +02:00
José Valim 7e2b22402c Do not ignore unimplemented function headers from used modules 2021-04-07 18:52:47 +02:00
José Valim 3df04b23f9 Temporarily defdelegate and defexception inside protocols 2021-04-05 10:17:16 +02:00
Wojtek Mach 33feac639a Mix.install/2: Allow calling with the same dependencies and don't generate .app file (#10875) 2021-04-04 20:19:09 +02:00
Wojtek Mach 8c4bed23e6 Use docs canonical version in all built-in apps (#10872)
Before:

https://hexdocs.pm/elixir/1.12.0-rc.0/changelog.html contains links like:

  - [`Mix.install/2`](https://hexdocs.pm/mix/Mix.html#install/2)

which currently 404.

After:

https://hexdocs.pm/elixir/1.12.0-rc.0/changelog.html would contain:

  - [`Mix.install/2`](https://hexdocs.pm/mix/1.12/Mix.html#install/2)

See demo: http://wojtekmach.pl/docs/elixir/1.12/elixir/changelog.html
2021-04-04 09:56:18 +02:00
Wojtek Mach b9c374e9bb Change docs canonical version (#10871)
We unfortunately had a discrepancy between what the docs said was the
canonical version and what was the actual URL:

    % curl --silent https://hexdocs.pm/elixir/1.12/Kernel.html | grep 'rel="canonical"'
          <link rel="canonical" href="https://hexdocs.pm/elixir/v1.12/Kernel.html" />
    % curl --silent https://hexdocs.pm/elixir/1.11/Kernel.html | grep 'rel="canonical"'
          <link rel="canonical" href="https://hexdocs.pm/elixir/v1.11/Kernel.html" />
    % curl --silent https://hexdocs.pm/elixir/1.10/Kernel.html | grep 'rel="canonical"'
          <link rel="canonical" href="https://hexdocs.pm/elixir/v1.10/Kernel.html" />

Notice `elixir/1.12` vs `elixir/v1.12`. The latter 404s!

This seems the easiest way to fix it.

Alternatively, we can handle it on hexdocs/bob side, move the docs to
the correct URL and add redirects. That would probably be more effort
but then again we wouldn't have to re-apply this same patch on all the
`v1.*` branches.
2021-04-04 08:34:06 +02:00
Eksperimental c045969850 Use first..start notation for ranges in CHANGELOG (#10870) 2021-04-03 23:28:20 +02:00
Enrico Rivarola 56e01f151b Sync unicode data to Unicode 13.0.0 (#10866) 2021-04-03 17:49:48 +02:00
Eksperimental 171caface8 Update Unicode upgrade instructions (#10862) 2021-04-03 17:49:48 +02:00
Eksperimental b2a04575c3 Update Unicode to version 13.0.0 (#10861) 2021-04-03 17:48:38 +02:00
José Valim 5ce4ede684 Add note about OTP 21 2021-04-03 16:39:45 +02:00
José Valim 31d2b99879 Release v1.12.0-rc.0 2021-04-03 15:32:28 +02:00
Michał Szajbe b49bac34d4 Pass xref's compile args to compile task (#10852) 2021-04-03 15:30:11 +02:00
José Valim 5b3ef7705e Update CHANGELOG 2021-04-03 15:27:09 +02:00
José Valim 64fca3cdb1 Clarifications to Calendar.ISO 2021-04-03 15:02:42 +02:00
José Valim 415d4647c4 Remove invalid escaping of sigils, closes #10864 2021-04-02 23:14:38 +02:00
Eksperimental 91344a24a8 Format source code with new rules 2021-04-01 09:48:05 +02:00
Eksperimental 5fbdee2714 Quote range operator atom
This commit has only the actual changes
Add tests for quoted range operator
2021-04-01 09:48:05 +02:00
Eksperimental 832fbce136 Fix grammar in Range docs (#10851)
- Use "with a step of X"
- Replace usage of "start" and "stop" with "first" and "last"
2021-03-30 18:51:06 +02:00
José Valim c45d9ab13d Do not discard blamed stacktrace from exception in exit in ExUnit formatter 2021-03-30 16:16:31 +02:00
Wojtek Mach 94096055ad Update compatibility-and-deprecations.md (#10846) 2021-03-30 15:02:27 +02:00
José Valim 02e9b898c4 More range copyedit 2021-03-30 14:44:50 +02:00
Eksperimental 1c01996c29 Copyedit Range specs and docs (#10845) 2021-03-30 14:44:50 +02:00
Eksperimental 103db3a2ef Simplify specs in Range by introducing t:limit/0 (#10844) 2021-03-30 14:26:40 +02:00
José Valim 6f95bf274f Use types were appropriate in Range 2021-03-30 14:14:10 +02:00
Kelvin Stinghen 7210ab2fbd Keeping underscore between digits on Macro.camelize/1 (#10843)
Closes #10839
2021-03-30 13:42:45 +02:00
Eksperimental 174b77150e Use one-based var names in Types (#10824)
In the same fashion as generated arguments 30373382ec
2021-03-30 13:42:41 +02:00
José Valim 1dd09cb0bc Document ranges structs are read-only 2021-03-30 12:02:32 +02:00
José Valim 203cb4bb6f Add Registry.values/3 2021-03-30 11:43:50 +02:00
Łukasz Jan Niemier 8ff5c76b9c Set correct value returned by get_fun/2 callback (#10842)
By looking at source of `Access.all/0` it seems that returned value by
this function is `container()` instead of tuple `{val, container()}`.
2021-03-30 11:08:44 +02:00
José Valim 51de0e09a7 Add line numbers of first expression to lists 2021-03-30 11:08:44 +02:00
José Valim 9b38e4ed07 Remove Range.empty?/1 2021-03-30 09:22:33 +02:00
Eksperimental 2d40ad09d1 Simplify Range's step error message (#10835) 2021-03-29 23:34:55 +02:00
Eksperimental 00d1bc6765 Update range definition (#10834)
Ranges are no longer exclusively a consecutive sequence of integers.
2021-03-29 23:34:51 +02:00
Eksperimental 544466a4d1 Fix t:Range.t/2 (#10833)
We need to be explicit and define the step field, otherwise IEx and ExDoc will spec it as "term()"

    (1)> t Range.t/2
    @type t(first, last) :: %Range{first: first, last: last, step: term()
2021-03-29 22:35:48 +02:00
José Valim 385d2cd54a Prepare v1.12 for release 2021-03-29 17:44:50 +02:00
José Valim de8e4f1bee Optimize Stream.zip and Stream.zip_with with only lists 2021-03-29 11:00:25 +02:00
José Valim cd18fc7afd Remove Enum.zip_reduce_while, as it is equivalent to Stream.zip+Enum.reduce_while 2021-03-29 10:45:26 +02:00
José Valim 6d39898b19 Update CHANGELOG 2021-03-29 10:28:33 +02:00
José Valim 2f49952d2f Clean up docs 2021-03-29 10:28:25 +02:00
José Valim b9474da3bf Bring zip optimizations for lists, add docs and specs 2021-03-29 09:56:47 +02:00
Adam Lancaster 7b4a57c35d Add zip_reduce and zip_reduce_while (#10828) 2021-03-29 09:33:36 +02:00
Wojtek Mach 1d7a7466f5 Add MIX_INSTALL_DIR and use it in Mix.install/2 (#10825) 2021-03-29 09:21:15 +02:00
AndreOrlov 077b711d14 Fix KeyError custom message (#10829) 2021-03-28 18:59:22 +02:00
José Valim 1e4222d127 More docs on ranges 2021-03-27 21:17:04 +01:00
Barna Kovacs 9732830d51 Fix typo in CHANGELOG.md (#10827) 2021-03-27 20:24:08 +01:00
Wojtek Mach f33c614f86 Make ExUnit tmp_dir absolute (#10826) 2021-03-27 20:03:06 +01:00
José Valim 5668ab2ff9 Update CHANGELOG 2021-03-27 17:45:07 +01:00
Eksperimental dad1b86a6a Streamline get_and_update specs for Access for Kernel (#10822)
In the same fashion of PRs #10228 and #10223

Replaces:
- `get_values` with `current_value`
- `update_value` with `new_value`
- `data` with `new_data` when suitable

Adds nil as an argument term in the function spec.
2021-03-27 14:18:50 +01:00
José Valim 251413a7b6 Adopt EEP 54
With EEP 54 (part of Erlang/OTP 24+), we can now get extra
information from Erlang errors. Before this patch:

    Interactive Elixir (1.12.0-dev)
    iex(1)> ets = :ets.new(:example, [])
    #Reference<0.3845811859.2669281281.223553>
    iex(2)> :ets.delete(ets)
    true
    iex(3)> :ets.insert(ets, :should_be_a_tuple)
    ** (ArgumentError) argument error
        (stdlib 3.15) :ets.insert(#Reference<0.3845811859.2669281281.223553>, :should_be_a_tuple)

After this patch:

    Interactive Elixir (1.12.0-dev)
    iex(1)> ets = :ets.new(:example, [])
    #Reference<0.105641012.1058144260.76455>
    iex(2)> :ets.delete(ets)
    true
    iex(3)> :ets.insert(ets, :should_be_a_tuple)
    ** (ArgumentError) errors were found at the given arguments:

      * 1st argument: the table identifier does not refer to an existing ETS table
      * 2nd argument: not a tuple

        (stdlib 3.15) :ets.insert(#Reference<0.105641012.1058144260.76455>, :should_be_a_tuple)

We choose to always track error info, instead of moving this
to blame, because it should be relatively cheap and it is
essential, especially when debugging live systems.
2021-03-27 11:52:21 +01:00
José Valim 3eb3a2e514 Better structure exception test 2021-03-27 11:31:23 +01:00
Fernando Tapia Rico fe6fed6fee Fix docs regarding empty ranges on negative steps (#10821) 2021-03-26 19:04:26 +01:00
José Valim bba29027f1 More docs around ranges 2021-03-26 15:55:15 +01:00
Wojtek Mach 09bf54014b Add step to Date.Range (#10816) 2021-03-26 15:03:24 +01:00
Wojtek Mach 9b44d9599f Fix Enumerable.Range.member?/2 with empty range (#10819) 2021-03-26 13:20:04 +01:00
José Valim 56ecdb172d Improve warnings 2021-03-26 13:19:04 +01:00
José Valim 7c8eb41a75 More docs and code comments for ensure_compiled! 2021-03-26 10:29:09 +01:00
Jason Axelson 3dab26248e Add "namespaces" guidance to library guidelines (#10818) 2021-03-26 10:16:56 +01:00
Eric Meadows-Jönsson dedee1d85a Fix OTP 24 test warnings (#10817) 2021-03-25 17:41:04 +01:00
Eric Meadows-Jönsson df56460f29 Fix mix tests on older git versions (#10815)
The configuration init.defaultBranch is a recent addition to git. When
it's not available the tests will fail because the wrong branch is
created.
2021-03-25 16:17:00 +01:00
Wojtek Mach 4f533cf284 Fix ^^^ warning (#10814)
Bitwise.xor/2 -> Bitwise.bxor/2
2021-03-25 12:30:58 +01:00
José Valim f546c846f3 Update CHANGELOG 2021-03-25 10:33:15 +01:00
José Valim 746ac57d81 Do not warn for configured runtime: false deps, closes #10624 2021-03-25 10:31:59 +01:00
José Valim 211892950d Update CHANGELOG 2021-03-25 10:07:52 +01:00
José Valim 895dc4f8e1 Properly underscore :rfc3986 2021-03-25 09:25:41 +01:00
Ted Metal c41ff4c20e Improve GenServer docs (#10812) 2021-03-25 09:22:22 +01:00
José Valim bc187f37dc Support stepped ranges (#10810) 2021-03-25 07:32:06 +01:00
Marco Milanesi 3fff5c4e43 Handle assert [%_{}] = [] (#10811) 2021-03-24 22:49:05 +01:00
Tom Crossland 7c62eb6193 Fix typo in CHANGELOG.md [no ci] (#10809) 2021-03-23 22:59:39 +01:00
Wojtek Mach 3533144e8e Update warnings for OTP master (#10807) 2021-03-23 17:40:04 +01:00
José Valim d9a23d0d38 Deprecate ^^^ 2021-03-23 10:23:58 +01:00
José Valim a362e11e20 Add a section on file-system watchers 2021-03-21 10:11:16 +01:00
José Valim 8df17a0891 Handle ambiguous unary op in keyword, closes #10806 2021-03-20 11:25:14 +01:00
José Valim 7d3a336980 Move parser tests to its own file and clean up private API 2021-03-20 11:20:37 +01:00
José Valim 601589e4be Update rebar3 docs 2021-03-17 10:21:40 +01:00
José Valim 59a3080d8b Also copy priv before compiling rebar3 deps 2021-03-17 10:19:54 +01:00
José Valim bc63b5fb80 Add ensure_compiled! to better handle deadlocks, closes #10802 2021-03-16 23:01:29 +01:00
José Valim 0a262ba86f Do not hide deadlocks between imports and structs, closes #10800 2021-03-16 17:47:53 +01:00
Andrew France 87faa45609 Clarify syntax for prepending an element to a list (#10801) 2021-03-16 17:46:35 +01:00
Wojtek Mach 014da5f595 Fix broken docs references (#10798) 2021-03-16 13:48:56 +01:00
Wojtek Mach eec6e5d0e3 Add :consolidate_protocols option to Mix.install/2 (#10797) 2021-03-16 12:58:53 +01:00
Łukasz Samson 07670cd36c Add timeout: :infinity and ordered: false in compile.erlang task (#10796) 2021-03-15 21:58:53 +01:00
Eric Meadows-Jönsson 1c60d52d33 Parallelize compile.erlang (#10789) 2021-03-15 14:27:31 +01:00
José Valim 6202282cec Only get consolidated files for --cover if in load path, closes #10790 2021-03-14 09:28:56 +01:00
Cohen Carlisle 25ded1f07b Update mix test --failed test comment (#10792) 2021-03-13 17:06:01 +01:00
José Valim 4852fc51c4 Support remote.vm.args to customize remote commands 2021-03-13 09:52:48 +01:00
José Valim ae48325991 Run all available tests if there are no pending --failed tests
Closes #10781
2021-03-13 09:37:38 +01:00
José Valim c8a52fde05 Recompile export dependencies when file is removed, closes #10784 2021-03-12 18:46:03 +01:00
Domingo Aguilera 99a58c5853 Update supervisor.ex (#10785) 2021-03-11 23:55:09 +01:00
José Valim c5373b965e All @dialyzer module attributes also work for functions 2021-03-11 21:42:19 +01:00
José Valim 228502e2a8 All dialyzer module attributes are also function attributes 2021-03-11 21:37:17 +01:00
José Valim 04b6bb7dec Update CHANGELOG 2021-03-11 19:43:29 +01:00
José Valim 6b2cc2332e Raise clearer error message on number followed by identifiers, closes #10755 2021-03-11 13:12:39 +01:00
José Valim d8d26e917a Warn on wrong dialyzer annotation to avoid breakage 2021-03-11 10:45:14 +01:00
doorgan f3ee6c51cc Improve improper lists typespecs readability (#10782) 2021-03-10 21:35:26 +01:00
doorgan dbc149d3f4 Clarify what type1 and type2 mean in maybe_improper_list typespec (#10780)
The typespecs for `maybe_improper_list` and `nonempty_improper_list` have little documentation, both in Elixir and Erlang docs. While the name is self-explanatory, the meaning of `type1` amd `type2` in the expression `maybe_improper_list(type1, type2)` is not clarified.

The [Erlang docs on typespecs](https://erlang.org/doc/reference_manual/typespec.html) have the following comments:
```erlang
| maybe_improper_list(Type1, Type2)    %% Type1=contents, Type2=termination
| nonempty_improper_list(Type1, Type2) %% Type1 and Type2 as above
```

This PR just mirrors these two comments in the Elixir docs.
2021-03-10 18:45:27 +01:00
José Valim 51d90f1932 Allow heredoc inside heredoc interpolation
Closes #10698
2021-03-10 16:03:24 +01:00
José Valim 8c29984ed1 Add :newline to unescape_map 2021-03-10 12:30:29 +01:00
José Valim e8e54d69d5 Add a section on formatter line length, closes #10677 2021-03-10 11:44:00 +01:00
José Valim c537c39c52 Validate dialyzer attributes, closes #10534 2021-03-10 11:13:41 +01:00
Eksperimental 5dbd1ab4ed Improve spec in Integer.parse/2 (#10776) 2021-03-09 15:23:17 +01:00
Timmo Verlaan abbb7983a0 Raise FunctionClauseError when passing invalid arguments to Date.compare/2 (#10774) 2021-03-08 19:09:41 +01:00
José Valim 7795cfc48d Fix bootstrap 2021-03-08 09:50:01 +01:00
José Valim a4ddf18871 Do not add compile time dependencies on @compile {:no_warn_undefined, _} 2021-03-08 09:39:32 +01:00
José Valim 01fdb4c7fb Clarify compile-time dependencies on module attributes 2021-03-07 20:00:08 +01:00
José Valim 5b3ae1b8c3 Rewrite not (foo in bar) to foo not in bar 2021-03-07 20:00:08 +01:00
Michał Łępicki 6ecab05bce Update Module.ParallelChecker.fetch_export/4 spec (#10772)
The return type changed in 96661c6963
2021-03-07 08:51:01 +01:00
José Valim c34fa0ca5c Fix warnings 2021-03-06 19:38:34 +01:00
José Valim 96661c6963 Show removal and deprecated tips from Erlang/OTP 2021-03-06 18:21:20 +01:00
José Valim eabce5c435 Ensure protocols within the current project are consolidated with --cover
Thanks to the detailed write up here for describing the bug:

https://securityboulevard.com/2021/03/how-i-sped-up-one-of-our-test-suites-by-17-5x-by-explicitly-defining-elixir-protocol-implementations/
2021-03-06 16:09:01 +01:00
José Valim bc0af62b21 Do not discard empty paths when merging, closes #10771 2021-03-06 09:30:30 +01:00
Matteo 95c2ddc307 Rectify usage of reader in release configuration (#10769) 2021-03-05 18:01:40 +01:00
José Valim 129dec4b4d Add more docs around config providers 2021-03-05 12:58:06 +01:00
José Valim 6a42cfe3e3 Further trim down generated doctest size 2021-03-05 10:32:44 +01:00
Wiwatta Mongkhonchit 3163aacdc4 Fix typo in mix test task (#10767)
The --export-coverage option specify the result files. This commit make
it clear that it means the name of the file you want to export the
result file to.
2021-03-05 07:55:25 +01:00
Eric Meadows-Jönsson 398302b88e Remove default requirement for rebar3 deps (#10766)
Ensures non-semver application versions do not break when no requirement
is specified in the rebar3 dependency.
2021-03-04 20:53:05 +01:00
José Valim 4d2be245c0 Avoid generating large ASTs on doctests
This makes the 633 doctests in Nx compile 4x faster.
2021-03-04 15:55:36 +01:00
Nathan Long 1f67ba6ca3 Note that dependency configs are not used (#10765) 2021-03-03 18:48:58 +01:00
Eksperimental 1666cf23bb Standardize use of "data types" and "data structure" (#10764) 2021-03-03 13:57:51 +01:00
John Bampton 87f65c1e1d chore: remove unneeded whitespace (#10762) 2021-02-28 09:39:35 +01:00
John Bampton c081dd1c50 chore: remove unneeded trailing whitespace (#10761) 2021-02-28 09:38:57 +01:00
Christopher Keele 74d49a6c33 Add internal link to mix task docs in mix alias docs (#10760) 2021-02-28 09:38:32 +01:00
Christopher Keele 83b87c3fc2 Fix small typo in mix alias docs (#10759) 2021-02-28 01:33:45 +01:00
José Valim 01422b1158 Run formatter 2021-02-28 01:33:01 +01:00
José Valim e04c325706 Pass env and target to Mix.Config readers 2021-02-27 21:53:05 +01:00
Benjamin Milde e3d88ae21c Copy upstream documentation change about regex ascii character class deficiency (#10757)
https://github.com/erlang/otp/pull/4551
2021-02-27 15:00:07 +01:00
Damir Vandic 3e453df186 Fix docs example in ExUnit.Case (#10754) 2021-02-26 10:00:27 +01:00
Mihai Târnovan 6db7b54c4c Use :os.env/0 in System.get_env if available (#10753) 2021-02-25 15:49:02 +01:00
Brett Beatty 58e36cdd0e Fix prefix check when consolidating protocols (#10751) 2021-02-24 20:50:56 +01:00
José Valim e30c8deff5 Do not pipe to previous value on dual ops 2021-02-24 16:13:37 +01:00
José Valim 4098349cc5 Use proper comparison of otp_release 2021-02-24 16:00:00 +01:00
José Valim f0b227d401 No longer auto-sort bindings 2021-02-24 15:39:33 +01:00
José Valim 37b6b03efa Use maps as bindings on Erlang/OTP 24 for faster evaluation 2021-02-24 15:16:10 +01:00
José Valim 73594085f2 Update erlang test helper 2021-02-24 09:50:48 +01:00
José Valim ad3dda75d6 Remove duplicated tests 2021-02-24 09:40:56 +01:00
METAL db47217c04 Add Link to erlang doc and update http to https (#10750) 2021-02-24 07:37:58 +01:00
Fernando Tapia Rico 079260247b CI: Upgrade FreeBSD to 12.2 (#10749) 2021-02-23 10:25:13 +01:00
José Valim 5ae4d96dc7 Fix undefined function warning 2021-02-23 10:06:20 +01:00
José Valim 84afbd4751 Fix filesystem race condition in Mix tests 2021-02-23 10:00:08 +01:00
José Valim 3cbf6fae26 Improve error message when erasing symlink fails 2021-02-23 10:00:08 +01:00
Adi Iyengar 7195724f17 Add documentation for Kernel.apply/x about clarity (#10748)
To better explain when to use apply, add a sentence similar
to [erlang docs](https://erlang.org/doc/man/erlang.html#apply-3)
2021-02-23 08:58:29 +01:00
Bessenyei Balázs Donát 024b4540bc Refine docstring for String.duplicate (#10747)
Duplicate `n` times might sound unusual to some. This commit changes the docstring for `String.duplicate` to use repeat.
2021-02-22 18:52:28 +01:00
sabiwara 2b8de98623 Optimize Enum.join/3 for lists (#10746) 2021-02-22 12:40:03 +01:00
sabiwara fa078c2fbb Optimize Enum.intersperse/2 (#10745) 2021-02-22 08:59:52 +01:00
sabiwara be26437a3e Optimize Enum.scan/2 and Enum.scan/3 (#10744) 2021-02-21 13:47:38 +01:00
Wojtek Mach ace56882b1 Handle :compile.file/2 returning :error on OTP 24 (#10742)
Before OTP 24 when given invalid args, :compile.file/2 would return:

    iex(1)> :compile.file('a.erl', [{:d, 'foo', 'bar'}, :report])
    {:error, :badarg}

On OTP 24:

    iex(1)> :compile.file('a.erl', [{:d, 'foo', 'bar'}, :report])

    *** Internal compiler error ***
    exception error: bad argument
      in function  io_lib:format/2
         called as io_lib:format("badly formed '~s'",[{"foo","bar"}])
      in call from sys_messages:list_errors/3 (sys_messages.erl, line 53)
      in call from lists:foreach/2 (lists.erl, line 1342)
      in call from compile:comp_ret_err/1 (compile.erl, line 546)
      in call from compile:'-internal_fun/2-anonymous-0-'/2 (compile.erl, line 229)
      in call from compile:'-do_compile/2-anonymous-0-'/1 (compile.erl, line 219)
    :error
2021-02-20 13:46:47 +01:00
Wojtek Mach b17d3a6cca 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
sabiwara be4cb59aea Optimize Enum.dedup/1 (#10741) 2021-02-20 08:01:13 +01:00
Nathan Long 4d9ba81972 Expand example and explanation of with clause (#10739) 2021-02-18 19:32:29 +01:00
Eksperimental c1bcb06e10 Update erlang.org links (#10737)
Consistently use https://erlang.org
See also ExDoc PR: https://github.com/elixir-lang/ex_doc/pull/1335
2021-02-18 13:18:43 +01:00
Eksperimental 61fed4a315 Migrate Erlang module URLs to Erlang module links (#10736) 2021-02-18 06:43:13 +01:00
Eksperimental 01c4ccff77 Revise use of link titles (#10735)
It removes the use of indiscriminate title casing.

Pages titles in Elixir pages are not title cased.
2021-02-17 21:30:43 +01:00
Eksperimental 545edebe44 Improve wording and typespecs in ambiguous datetimes (#10732) 2021-02-17 21:26:36 +01:00
Jonathan Arnett 74e38a3e74 Mark is_struct/2 code as generated (#10734) 2021-02-17 21:00:04 +01:00
Eksperimental 15522ff74d Add comma after First at the beginning of a sentence (#10733) 2021-02-17 18:00:45 +01:00
José Valim 11cc5c50f8 Add RELEASE_PROG to releases, closes #10728 (#10730) 2021-02-17 16:47:22 +01:00
Eksperimental ab368900bc Improve grammar around "then" (#10727) 2021-02-17 16:39:16 +01:00
dwiyanr 2bd687f331 Cleanup waiting map on Task.Supervised.stream/6 (#10729)
When running Task.Supervised.stream/6 with ordered options turned off,
the "waiting" map to store the response from task is not cleaned up.
This leads to the response piled up in the map causing memory leak.

This changes cleanup the map of the response that has been delivered to the reducer
function.
2021-02-17 16:07:02 +01:00
José Valim a34a977224 Simplify nested call 2021-02-17 15:59:21 +01:00
Eksperimental 61e73899c1 Add clarification in Atoms section in Unicode syntax page (#10725) 2021-02-17 13:01:37 +01:00
Eksperimental b15a5ee95f Fix warning message mentioning quoted atoms may end with ! or ? (#10724) 2021-02-17 12:08:53 +01:00
Eksperimental 38220526f0 Improve syntax docs (#10723) 2021-02-17 07:46:26 +01:00
Eksperimental 64224d8fe4 Use underscores in plural in warning messages (#10721) 2021-02-16 17:57:00 +01:00
Wojtek Mach ddd716a8f4 Fix remaining warning translations for OTP 24 (#10720)
Yecc error message changed so we need to loosen up our assertion:

OTP 23

    iex> iex
    Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

    Interactive Elixir (1.11.3) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> :yecc.file('f.yrl')
    f.yrl:1: syntax error before: '.'
    :error

OTP 24

    iex> iex
    Erlang/OTP 24 [DEVELOPMENT] [erts-11.1.7] [source-802d2c5083] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

    Interactive Elixir (1.12.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> :yecc.file('f.yrl')
    f.yrl:1:5: syntax error before: .

    :error
2021-02-16 15:09:09 +01:00
Wojtek Mach f33f180398 Fix translating erl compiler errors to diagnostics on OTP 24 (#10719)
The compiler previously emitted just `line` and now emits `{line,
column}`.

The diagnostic struct accepts as position either:

    nil
    line
    {start_line, start_col, end_line, end_col}

so we could have used the `column` to create the 4-tuple but we don't
have enough information to do that correctly, we don't know where the
line ends.
2021-02-16 13:15:04 +01:00
José Valim fc5b37f125 Deep merge and support argv on config/runtime.exs for escripts 2021-02-16 11:06:04 +01:00
José Valim c05c1cff54 Reload .app files for path deps in tracer, closes #10718 2021-02-15 23:47:54 +01:00
Barna Kovacs 80e180eb75 Fix typo in GenServer docs (#10717) 2021-02-15 18:20:51 +01:00
victor felder e95eea4bf2 Fix Macro.underscore for digit preceded by capitals, closes #10713 (#10715) 2021-02-14 22:30:19 +01:00
Barna Kovacs 3efecd80b8 Fix logic error in docs of GenServer's c:terminate/2 (#10714) 2021-02-14 19:14:42 +01:00
José Valim bc632da0fc Add a note about tasks running once, closes #10712 2021-02-14 17:22:55 +01:00
Christopher Keele 1b345edf15 Minor typo fix in ExUnit.DocTest moduledocs (#10711) 2021-02-14 08:16:51 +01:00
Frank Hunleth 08c3602a4e Preserve file mode when copying non-beam ebin files (#10709)
If a non-beam file in the ebin directory had execute permissions set,
those were lost when making a release. This switches the copy function
to `File.cp!/3` to preserve permissions.

Specifically, this affects the use of zigler with Nerves. Zigler
generates NIF shared libaries and stores them in the `ebin` directory.
The shared libraries had their execute mode bit set, but lost it on the
copy to the release. This affected the Nerves post-processing of all
firmware executables since Nerves uses the execute bit to identify
files that should be processed.

This also switches the copy call to the raising version like is done
when writing `.beam` files for consistency.
2021-02-13 21:04:26 +01:00
José Valim 7cc64a72af Improve Task docs to mention shutdown and different patterns 2021-02-13 09:56:35 +01:00
Eksperimental 1203f09174 Update to main branches instead of master on git usage (#10708) 2021-02-13 09:09:07 +01:00
Eksperimental d8d89b431e Fix Git warning (#10707)
It was partially solved in 74855f052d
2021-02-12 21:04:30 +01:00
Lee Bannard 4ad4ccaf96 erlsrv.exs typo in release docs (#10706) 2021-02-12 19:41:54 +01:00
José Valim 8ca10a59b2 Update CHANGELOG 2021-02-12 14:30:18 +01:00
José Valim 4f59441490 Revert "Do not support negative dates on the basic format"
This reverts commit ce27b8348c.
2021-02-12 14:21:15 +01:00
José Valim 5db83370ed Generalize operator completion on IEx' parser 2021-02-12 14:19:31 +01:00
Wojtek Mach 6437a1ad9f Update Erlang warnings translation (#10694)
Since https://github.com/erlang/otp/commit/8ecc648df22dbfe9dd68a3e2b968ba2b6a2ca7fe
instead of `{eval_failure, Reason}` we'd get `{eval_failure, Call, Reason}`
2021-02-12 12:23:58 +01:00
Łukasz Samson d8f1a5d6b6 Fix logic in get_in docs (#10704) 2021-02-12 10:48:16 +01:00
Eksperimental efd9a7852b Minor fix: remove/add new lines in Mix.Tasks.Test moduledoc (#10702) 2021-02-12 05:21:23 +01:00
José Valim 84ac335ef5 Talk about the most common case first 2021-02-11 18:58:29 +01:00
sabiwara 87b2b21baa Speedup Enum.min/1 and Enum.max/1 (#10701) 2021-02-11 14:27:33 +01:00
José Valim e5ba2261dc Add export dependencies on Macro.struct! 2021-02-11 10:58:18 +01:00
José Valim 1ae7fc0ee1 Add a section for working with structs on get_in 2021-02-11 10:58:18 +01:00
sabiwara 2ff6da21a6 Make Enum.any?/1 and Enum.all?/1 faster (#10700) 2021-02-11 10:53:09 +01:00
Jan Hruban cc2289806b Fix typo in documentation (#10697) 2021-02-09 13:47:50 +01:00
Jan Hruban 0037fa13e1 Document that File.cd!/2 affects the BEAM globally (#10695) (#10696) 2021-02-09 12:48:18 +01:00
Christopher Keele 7c75d24aa2 Support basic datetime format in Calendar.ISO parsing functions (#10693) 2021-02-08 22:58:32 +01:00
Christopher Keele 8fa1bb5195 Rework Calendar.ISO parsing function tests and examples (#10692)
* Add ISO format examples to the top-level moduledocs.

* Move unit tests out of parse_time/1 documentation and into suite.

* Move unit tests out of parse_naive_datetime/1 documentation and into suite.

* Move unit tests out of parse_utc_datetime/1 documentation and into suite.

* Remove duplicated parse_date/1 doctests, fix missing asserts, other
cleanup.
2021-02-06 09:12:31 +01:00
José Valim 018bf60793 Update references to the latest ISO 2021-02-06 08:32:47 +01:00
Christopher Keele 6af4565de2 Improve Calendar.ISO documentation (#10689) 2021-02-06 08:31:13 +01:00
Max Katz f7ac5e112c Fix spelling mistake in release docs (#10688) 2021-02-05 09:14:17 +01:00
José Valim ce27b8348c Do not support negative dates on the basic format 2021-02-05 07:55:22 +01:00
Devon Estes 2153b09bd4 Interpolate module attributes in match assertions (#10686)
This will interpolate the value of module attributes in match assertions
in ExUnit. Basically, the old diff would look like this:

```
  1) test assert match with module attribute (ExUnit.AssertionsTest)
     test/ex_unit/assertions_test.exs:186
     match (=) failed
     code:  assert {2, @test_mod_attribute} = Value.tuple()
     left:  {2, @test_mod_attribute}
     right: {2, 1}
     stacktrace:
       test/ex_unit/assertions_test.exs:187: (test)
```

And now it will look like this:

```
  1) test assert match with module attribute (ExUnit.AssertionsTest)
     test/ex_unit/assertions_test.exs:186
     match (=) failed
     code:  assert {2, @test_mod_attribute} = Value.tuple()
     left:  {2, 2}
     right: {2, 1}
     stacktrace:
       test/ex_unit/assertions_test.exs:187: (test)
```
2021-02-03 17:44:33 +01:00
Wojtek Mach 2c03fbfe32 Add basic docs for Kernel.ParallelCompiler.compile_to_path/3 (#10685) 2021-02-03 07:03:17 +01:00
Eksperimental 2e3011bbe3 Fix context in defdelagate/2 AST (#10682)
Fixes #10593
2021-02-02 11:11:58 +01:00
José Valim 9931cb8788 Fix URI deprecation warnings 2021-01-30 21:40:27 +01:00
Floris Huetink 0e81e83d16 Add functions for RFC 3986 compliant space encoding and decoding (#10680) 2021-01-30 21:36:46 +01:00
Milton Mazzarri b30a83e9d8 chore: fix callbacks amount in GenServer docs (#10681) 2021-01-30 20:25:42 +01:00
José Valim 3df3117c15 Run formatter 2021-01-30 00:10:59 +01:00
José Valim e8678f339b Blow up on bad inspect in doctest 2021-01-29 20:56:32 +01:00
José Valim d7af392d19 Improvements to xref docs 2021-01-28 16:59:11 +01:00
José Valim 34b3868d68 Print task times on MIX_DEBUG=1 2021-01-28 13:43:30 +01:00
André Albuquerque 9f346e256e Fix typo (s/InstalTest/InstallTest) (#10678) 2021-01-27 17:33:32 +01:00
Eksperimental 27c16dc063 Standardize use of key-value pair (#10676) 2021-01-26 15:25:47 +01:00
Wojtek Mach af787a48fe Add Mix.install/2 (#10674) 2021-01-26 15:24:59 +01:00
Wojtek Mach f4f444c2e8 Fix warning in Enum.all?/2 doctest (#10673) 2021-01-25 13:59:46 +01:00
Benjamin Milde ae5188d899 Add documentation about line break normalization for line based streaming (#10671) 2021-01-23 17:50:07 +01:00
José Valim 898d80e46c Do not set the limit for derived inspect, closes #10669 2021-01-22 17:35:26 +01:00
Xavier Noria b5b96b9bda Explain why Enum.all?/2 returns true for empty enumerables (#10667)
As per the discussion in elixir-core.
2021-01-22 10:51:28 +01:00
Eksperimental 6efaa00ce5 Improve clarity of Enum.reduce/3 spec (#10662) 2021-01-21 07:38:28 +01:00
José Valim aedd968e44 Fix bug with slice returning invalid value for negative first value 2021-01-20 18:05:21 +01:00
José Valim b662d32fc3 Fix typo, closes #10660 2021-01-15 18:09:15 +01:00
Wojtek Mach 78fa1763e4 Update context error test (#10657) 2021-01-15 14:42:34 +01:00
Eric Meadows-Jönsson 6beb687e2d Do not require RHS of and operator to be boolean (#10659)
Closes #10611.
2021-01-15 13:38:07 +01:00
sabiwara 9c5a21d8d5 Make tap/2 and then/2 macros (#10658) 2021-01-15 11:19:26 +01:00
felipe stival 8c26ddbc67 Improve error message on invalid pattern on test (#10649) 2021-01-15 09:17:57 +01:00
Eric Meadows-Jönsson 3763fd9664 Fix tuple subtype check (#10655)
Closes #10609.
2021-01-14 17:33:05 +01:00
Eksperimental 74855f052d Remove warnings in test in latest Git version (#10654)
The warning would be:
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
2021-01-14 09:49:06 +01:00
José Valim bb289fe6d6 Improve docs to EEx, closes #10653 2021-01-13 12:32:33 +01:00
Christian Taedcke 36417f5e62 Make mix.release script rand() work with busybox (#10645)
Combining dd, od and awk with the common arguments found in
busybox and GNU coreutils as well BSD the function rand() can
make correct id.

This is based on
https://github.com/erlware/relx/commit/200e12815ef665338cea1280244e9c4f9dfa06b9
and
https://github.com/erlware/relx/pull/828

Tested with busybox v1.29.3 with CONFIG_DESKTOP disabled.
2021-01-13 10:32:19 +01:00
José Valim e5a2f71e1b Add System.trap_signal/3 and System.untrap_signal/2 (#10650) 2021-01-13 09:30:39 +01:00
Eksperimental 16f5301c9b Remove redundant guard in Collectable.BitString (#10652) 2021-01-13 09:27:27 +01:00
Wojtek Mach e83e780aa1 Update CHANGELOG.md (#10646) 2021-01-12 20:32:57 +01:00
José Valim 6fc064a1af Consider sync time only when sync tests start 2021-01-12 13:10:36 +01:00
Tonći Galić 01477e6f1b Prevent using wrong test context (#10647)
setup blocks can return 3 shapes:
 - result tuple: {:ok, new_context}
 - (keyword) list: [conn: new_conn]
 - map: %{conn: conn}

the return values of setup blocks get merge into a map and passed as a map to
test context. users could however make a mistake by using (for example) a
keyword list as pattern for test context, which will return a function clause
error. this commit tries to return a more friendly error instead
2021-01-12 13:05:12 +01:00
Guillaume Cauchon 37b4ef56d2 Refactor replace/4 to remove guards on replacement parameter (#10644) 2021-01-12 09:29:51 +01:00
José Valim a7351454e6 Ensure changed files persist across mix compile.elixir cycles 2021-01-11 15:33:29 +01:00
José Valim bd301f672c Revert "Ensure that a manifest is generated even with no source code (#10591)"
This reverts commit c16cf72c2c.
2021-01-11 15:33:29 +01:00
Adam Millerchip df324981da Document head of line pipe treatment in IEx (#10641)
Updated to document the new behaviour after it was modified in
6a4b2ec4ee
2021-01-10 21:32:39 +01:00
Thiago Santos 2ab7a9246b Fix typos (#10639) 2021-01-09 23:09:41 +01:00
José Valim dbb9cff1b6 Use ERL_ROOTDIR 2021-01-09 17:24:59 +01:00
José Valim 7f55ccb249 Allow erl ROOTDIR to be set, closes #10638 2021-01-09 16:15:15 +01:00
José Valim 1c1654c88a Write a section on interpreting test results 2021-01-09 12:04:08 +01:00
José Valim 045e3b4855 Print how much time is spent on async/sync tests
~/OSS/ecto[master]$ mix test
    ...................................................
    ............. (...)

    Finished in 9.1 seconds (7.9s async, 1.2s sync)
    78 doctests, 1089 tests, 0 failures
2021-01-09 11:44:22 +01:00
Ben Wilson 463420518f Support compression on Registry (#10637) 2021-01-09 10:52:22 +01:00
José Valim ec3223f1d0 Revert "Update String.printable? character_limit typespec (#10635)" (#10636)
This reverts commit b62256c1a5.
2021-01-08 21:53:12 +01:00
Cẩm Huỳnh b62256c1a5 Update String.printable? character_limit typespec (#10635) 2021-01-08 20:41:26 +01:00
Cẩm Huỳnh ef16468175 Optimize String.codepoints (#10633) 2021-01-08 19:20:27 +01:00
Cẩm Huỳnh 123d01cd49 Optimize String.valid? (#10632) 2021-01-08 17:23:43 +01:00
José Valim 622597163e Remove rename_deprecated_at
We haven't added any new rules in a while and
we can assume that users relying on this option
have already rewritten (either manually or
automatically) the current deprecated calls.
2021-01-08 15:27:05 +01:00
Aleksei Matiushkin 6a4b2ec4ee Make copy-paste of piped multiline expressions to IEx work (#10626) 2021-01-05 22:33:38 +01:00
felipe stival ce5fc005a3 Fix typos (#10628)
Fixes some typos related to #10625
2021-01-05 21:43:11 +01:00
Todd Resudek c85774c461 Make day of month documentation more clear (#10627)
Show example while removing padding from day
2021-01-05 20:09:19 +01:00
Wojtek Mach f282013f4e Fix typos (#10625) 2021-01-05 15:37:57 +01:00
Eksperimental b889975c55 Improve example in Protocol.derive/3 (#10623) 2021-01-04 15:00:44 +01:00
Eksperimental 4b6698c3f3 Fix cryptic error when defimpl is defined without :for option (#10620) 2021-01-04 07:50:34 +01:00
Eksperimental a540166009 Update to a more descriptive typespec in Macro.Env (#10621) 2021-01-03 09:56:39 +01:00
Eksperimental e3092b7182 Document what exception impl_for!/1 raises (#10619)
Related: https://github.com/elixir-lang/elixir/pull/10617
2021-01-03 09:51:22 +01:00
José Valim 01f5196bf1 Properly handle keywords followed by ::, closes #10599 2021-01-02 17:41:16 +01:00
José Valim 409c79545b Update CHANGELOG 2021-01-02 17:28:31 +01:00
José Valim f106092130 Support all literals in guards, closes #10613 2021-01-02 17:06:42 +01:00
José Valim a0e62b48a7 Run the formatter 2021-01-02 17:05:25 +01:00
Julian Doherty 2a7946cd49 Improve Enum.chunk_while/4 docs to clarify after_fun semantics (#10595)
- add `{:halt, acc}` tuple return doc for `chunk_fun`
- explain `after_fun` semantics in more detail
- format return tuples for `chunk_fun` and `after_fun` as markdown lists
- added note that `acc` in `after_fun` return tuple is ignored
- note that `Enum.chunk_while/4` returns a list of *chunks*, not a list
  of lists (emitted chunks can be anything, not necessarily just lists)
2021-01-02 16:29:20 +01:00
José Valim 09d751772f Clarify writing documentation guide, closes #10610 2021-01-02 16:26:06 +01:00
José Valim d57ec28285 Do not break inside interpolation
Closes #10587
2021-01-02 16:20:39 +01:00
Eksperimental 61cf3246b6 Add Collectable.into/1 to Deprecation list & update Enum.into/2 doc (#10615) 2020-12-31 19:31:57 +01:00
Aleksei Matiushkin e8e008b1a8 Return :error on decomposing regular tuples as calls (#10612)
This makes two-elem and n-elem tuples consistent.
2020-12-31 10:12:58 +01:00
Guilherme 'nirev' de Maio 96f70936b7 Fix small typo in pipe operator doc (#10606) 2020-12-30 08:47:24 +01:00
Eksperimental b5ceaedaed Stremaline specs in Kernel.{tap, then}/2 (#10608)
x is not used anywhere in the specs. Use name of argument instead.
2020-12-30 08:46:06 +01:00
Eksperimental 66967cae26 Improve grammar in IO (#10607) 2020-12-30 08:45:40 +01:00
Eksperimental 54886279b8 Extend functions/macros not allowed inside Protocols (#10605) 2020-12-30 01:12:28 +01:00
felipe stival 44ac4958a7 Add Kernel.tap/2 (#10604) 2020-12-29 21:03:41 +01:00
Tomasz Marek Sulima 3fa9ef6f82 Add Kernel.then/2 (#10603) 2020-12-29 20:58:41 +01:00
Adam Rutkowski 952c9c1753 Fix Path.relative_to/2 when referencing self (#10601)
If `relative_to` argument points at `path`, the
result should be relative.

Currently it's:

    iex(1)> Path.relative_to('/foo/bar', '/foo/bar') |> Path.type()
    :absolute

This patch fixes it, so it's:

    iex(1)> Path.relative_to('/foo/bar', '/foo/bar')
    "."
    iex(2)> Path.type(v(1))
    :relative

Similar code in python:

    In [1]: os.path.relpath('/foo/bar', start='/foo/bar')
    Out[1]: '.'

And in ruby:

    > Pathname.new('/foo/bar').relative_path_from(Pathname.new('/foo/bar')).to_s
    => "."
2020-12-29 12:57:02 +01:00
Eksperimental e56d931f4f Add @deprecated attribute to macros in Behaviour (#10597) 2020-12-28 10:49:32 +01:00
José Valim a0799113fc Fix warnings on IEx 2020-12-26 20:24:23 +01:00
José Valim 44c2f912fe Simplify import handling in lexical tracker 2020-12-26 20:24:23 +01:00
Philip Ross c16cf72c2c Ensure that a manifest is generated even with no source code (#10591)
Closes #10590.
2020-12-23 10:28:54 +01:00
José Valim 86e2384634 Set evaluation to empty space to fix string replace on batch, closes #10495 (#10586) 2020-12-21 16:22:55 +01:00
José Valim 535699f611 Copy include directory before compiling ebin for rebar3
Closes #10517
2020-12-21 12:57:12 +01:00
José Valim be394b34cc Also warn for literal structs on min/max 2020-12-18 17:55:29 +01:00
José Valim b0bf30ebe1 Properly track removals on export deps, closes #10584 2020-12-18 10:49:23 +01:00
John Bampton 52141f2a3f Fix spelling (#10583) 2020-12-17 16:08:42 +01:00
José Valim d57d282adc Remove invalid entry from CHANGELOG 2020-12-17 13:21:18 +01:00
José Valim 592c5bba31 Update CHANGELOG 2020-12-17 13:16:50 +01:00
José Valim 53bbbc50b4 Do not warn for attributes used in after_compile
Closes #10579.
2020-12-17 13:05:02 +01:00
José Valim 0e159e3d85 Improve xref error message, closes #10582 2020-12-17 08:18:11 +01:00
Boris Kuznetsov 0dd0688f7f Add documentation for Keyword lists with default arguments (#10581) 2020-12-16 15:59:15 +01:00
Eksperimental a242f4310c Improve documentation for Protocol module (#10580) 2020-12-16 07:00:09 +01:00
Boris Kuznetsov 3e9bc1ef15 Documentation for multiple values in case (#10576) 2020-12-14 19:27:58 +01:00
Wojtek Mach a4fa6b7cf2 Update task.ex (#10574) 2020-12-14 07:28:45 +01:00
Kentaro Kuribayashi b1e4364e09 Fix test failures when MIX_TARGET is set with a non-empty value (#10573) 2020-12-14 07:28:14 +01:00
Aaron Ross bfa34ecf27 Generate error for unused "do" expression in EEx (#10566) 2020-12-14 07:27:24 +01:00
José Valim 3a41e167c6 Revert "Improve perf for Integer.gcd (#10571)" (#10572)
This reverts commit cdf53ca731.
2020-12-13 18:08:58 +01:00
Thomas Cioppettini 9a2a390638 Simplify Enum.each/2 (#10570)
:lists.foreach already returns an :ok atom,
so we don't need to add another one.
2020-12-13 17:18:38 +01:00
Thomas Cioppettini cdf53ca731 Improve perf for Integer.gcd (#10571)
If the two integers are equal to one another,
we can immediately exit.
2020-12-13 17:18:05 +01:00
John Bampton 54d29f90ff Remove trailing whitespace from ex file (#10569) 2020-12-13 13:35:21 +01:00
Eksperimental 00b8dd6e3f Make comment unambiguous (#10568)
The v2.0 could be interpretted as if it refering to the defstruct
version, which by the way it is already number 2.
2020-12-13 13:16:14 +01:00
Aleksei Magusev 53246a5a90 Fix double-newline and improve comment in the "new" gitignore 2020-12-13 11:03:17 +01:00
José Valim 8f1bd70fb5 Do not discard context from with, closes #10538 2020-12-13 09:19:12 +01:00
José Valim 3f86249556 Fix variable overriding in records, closes #10540 2020-12-13 09:05:03 +01:00
Eksperimental d0f084e9fd Fix function arity in Task.await/2 doc (#10564) 2020-12-12 17:27:02 +01:00
José Valim 919c637f6f Avoid computing powers if possible 2020-12-12 17:24:52 +01:00
José Valim 8bde6303a4 Add an example of handling tasks inside a GenServer 2020-12-12 12:14:07 +01:00
José Valim f1240de551 Add handle_text/3 to EEx.Engine 2020-12-12 08:37:27 +01:00
José Valim 23db39d847 Add line and column to EEx tokenizer 2020-12-11 22:38:14 +01:00
José Valim 66fac6f52d Deprecate use EEx.Engine in favor of explicit delegation 2020-12-11 21:52:27 +01:00
Jon Rowe 8731dd8192 Add offset to DateTime.to_iso8601/2 (making it to_iso8601/3) (#10315) 2020-12-11 19:28:23 +01:00
Hissssst e97476d21b Fix GenServer option typespec (#10563) 2020-12-11 13:30:52 +01:00
Eric Meadows-Jönsson 3a22dabc20 Fix bootstrap error 2020-12-10 15:42:06 +01:00
José Valim 438861be4d Do not use unicode chars if ANSI is disabled
See https://github.com/elixir-lang/elixir/issues/10481#issuecomment-742448132
2020-12-10 12:18:39 +01:00
José Valim 19bcb716b7 Improve error message on do-block in module attr, closes #10544 2020-12-10 12:12:14 +01:00
Eksperimental 5a9ae5c712 Add left out clarification in Enumerable count (#10560)
Mention that when we say that "you can count" is
without traversing the whole collection.
This was left out in 4a1e4481a3
2020-12-10 06:37:07 +01:00
felipe stival 5de91b18d3 Add Enum.product/1 (#10559) 2020-12-09 23:23:26 +01:00
Haelwenn Monnier 2f77ee9afc bin/elixir: Rename erl() function to erl_set() (#10557)
It conflicted with the erl executable for Korn-derived shells
where a function can be used in `exec command`.
2020-12-09 19:02:51 +01:00
James Duncombe 5632675802 Fix typo in Module documentation (#10558) 2020-12-09 18:34:12 +01:00
Vicente Merlo a637ea6e38 Add offset option to Regex.scan/3 and Regex.run/3 (#10555) 2020-12-09 18:15:41 +01:00
Fernando Tapia Rico 10ae66cda9 Doc Enum.with_index fun expects a zero-based index (#10556) 2020-12-09 17:25:26 +01:00
José Valim 3eefcb1cca Document compile flag 2020-12-09 17:15:13 +01:00
felipe stival 20f19cc89a Add support for functions on Enum.with_index/2 (#10552)
Makes Enum.with_index/2 supports both functions and offsets by setting
a default function (fn element, index -> {element, index} end) and
a default offset (0) and letting the user change one of them on the
second argument.
2020-12-09 15:35:19 +01:00
Wojtek Mach 8db8162aff Update Tuple.sum and Tuple.product docs (#10554) 2020-12-09 15:34:47 +01:00
Tomasz Marek Sulima 2114d752b0 Add Tuple.sum/1 (#10551) 2020-12-09 14:35:01 +01:00
Tomasz Marek Sulima 132e67186d Add Tuple.product/1 (#10550) 2020-12-09 14:33:19 +01:00
José Valim 5627de674f Do not match on error_info on OTP 24 2020-12-08 17:40:13 +01:00
Eksperimental 4a1e4481a3 Be clearer about Enumerable.count/1 implementations (#10542)
It should return {:ok, count} when we can count the number of elements
without traversing the enumerable.
2020-12-08 17:35:06 +01:00
Eksperimental 7950d1dd6a Hide Enumerable.List.slice/4 function (#10541) 2020-12-08 17:33:55 +01:00
Eksperimental ea1932af05 Improve t:Enumerable.reducer/2 (#10545) 2020-12-08 17:24:46 +01:00
Frank Hunleth adc9e5e0b5 Use defaults when the mix env or target are empty (#10537)
If the user sets `$MIX_ENV` or `$MIX_TARGET` to the empty string rather
that unsetting the environment variables, `:""` will be passed for the
environment and target respectively. This will likely fail. For the mix
target, this mistake turned out to be hard to debug for new Nerves users
since mix target is much less used in other contexts.

This commit catches the empty string being passed to `$MIX_ENV` and
`$MIX_TARGET` and sets the environment and target to the defaults just
like if those variables were unset.
2020-12-07 20:52:50 +01:00
José Valim 75413fe127 Take \r\n into account when computing heredoc indentation 2020-12-06 15:20:50 +01:00
Zach Daniel 157cc70f65 Add Enum.count_until/2 and Enum.count_until/3 (#10532) 2020-12-05 18:37:34 +01:00
José Valim 1b13706fef Verify arith error on bad inputs 2020-12-03 14:42:08 +01:00
José Valim 51f7c83868 Add Integer.pow/2 and Float.pow/2 (#10528) 2020-12-03 14:11:45 +01:00
Eksperimental e0ba78a760 Use one-based generated arguments everywhere (#10523)
It complemments changes introduced in 30373382e.
2020-12-02 18:02:34 +01:00
José Valim 3556642ac1 Clarify duplicate docs warnings 2020-12-02 09:20:28 +01:00
Paul Geraghty 2c6978c80b Fix typos in Enum documentation (#10522) 2020-12-02 08:15:43 +01:00
Austin Lanari 75403032be Add note about optional mix dependencies not being started by application (#10521) 2020-12-01 20:44:25 +01:00
José Valim 5adbaf3728 Consider macros and module attributes on the left side of ++ on ExUnit.Diff
Closes #10520
2020-12-01 07:43:37 +01:00
José Valim 30373382ec Use one-indexed args for clarity, closes #10518 2020-12-01 07:31:48 +01:00
José Valim a69bc93c51 Do not generate invalid code when formatting no args with when, closes #10508 2020-11-29 12:37:25 +01:00
José Valim 94b3539527 Document formatter newline behaviour, closes #10504 2020-11-29 11:28:55 +01:00
José Valim 0999441e77 Preserve CRLF for heredocs, closes #10504 2020-11-29 11:26:24 +01:00
José Valim af5494657c Add more examples to assert/2, closes #10503 2020-11-29 11:11:26 +01:00
José Valim 1abf082d46 Add since annotations 2020-11-29 11:06:48 +01:00
José Valim 1ead61658c Update CHANGELOG 2020-11-29 09:50:10 +01:00
José Valim 205d20f561 Add Module.get_definition/2 and Module.delete_definition/2 2020-11-29 09:37:00 +01:00
Papillon6814 660ac71d0e Add missing punctuation (#10515) 2020-11-27 07:13:33 +01:00
Eric Meadows-Jönsson 281d35e520 Fix docs tests on OTP 23 (#10510) 2020-11-23 22:43:15 +01:00
Andrea Leopardi c0c04ef143 Specify that Mix.raise/2 is available from 1.12+ (#10509) 2020-11-23 22:19:38 +01:00
José Valim 55c4f74f2b Improve error message on multiple doctest exceptions 2020-11-22 10:29:26 +01:00
Kensei Nakada b3e9c2a378 Add guard clause on functions in string module (#10506) 2020-11-22 08:17:35 +01:00
José Valim 83ffc52d28 Handle in memory modules in Xref.calls, closes #10502 2020-11-21 08:34:17 +01:00
José Valim e4137546a7 Do not raise when checking for ambiguity with static_atoms_encoder, closes #10501 2020-11-20 12:43:25 +01:00
Yiming Chen 8c62a2096d Speed up Regex.replace/4 when there is no match (#10500)
by only calling precompile_replacement/1 when there are matches
2020-11-20 10:55:15 +01:00
José Valim 571e05a38f Throughout reject aliases starting with numbers 2020-11-20 09:18:58 +01:00
José Valim 876e260e69 Properly expand option aliases followed by numbers 2020-11-20 09:00:51 +01:00
José Valim c47e48cdc4 Wrap around :file instead of calling internals 2020-11-20 07:14:35 +01:00
José Valim 11d089f5c3 Handle recursive vars in map values (#10498) 2020-11-18 12:40:05 +01:00
Andrea Leopardi 6628b83209 Clean up docs and support paths in EEx functions (#10499)
Before this commit, we were not supporting t:Path.t/0 paths but rather
t:String.t/0 paths in EEx file-related functions.

With this commit, we're not supporting t:Path.t/0. It's mostly calling
IO.chardata_to_string/0 on the given filename.

Another thing that was fixed is that now
:file and :line options in file-related EEx functions default to the
given file and line, but can be overridden. Before, they were forced.

I also cleaned up the docs here and there and added more examples.
2020-11-18 11:24:47 +01:00
José Valim 8344037762 Set @tag :unix to exit code tests 2020-11-18 10:41:42 +01:00
José Valim 7edb22322b mix format 2020-11-17 08:53:37 +01:00
Łukasz Jan Niemier 8722836d3a Allow setting custom exit code on Mix error (#10463) 2020-11-17 08:44:47 +01:00
José Valim cdbb1498c3 Fix test on Erlang master 2020-11-17 07:46:58 +01:00
Eksperimental 283d2207fc Update GitHub actions/checkout to v2 (#10496) 2020-11-17 07:39:28 +01:00
Eksperimental 444f7f8bfb Optimize Enumerable.List functions for empty lists (#10494) 2020-11-17 07:39:11 +01:00
José Valim 36d12b4bd4 Add a note about pattern matching to get_in 2020-11-14 15:27:26 +01:00
José Valim a6e9dd0dd6 Fix failure on Erlang/OTP master 2020-11-12 18:34:59 +01:00
Eric Meadows-Jönsson 166d6aa92e Fix intermittent test failure caused by archive warning (#10493) 2020-11-12 17:46:17 +01:00
José Valim 50ae98e4ba Update README.md 2020-11-12 17:42:35 +01:00
José Valim f928258f2e Improve docs around Map.equal? and MapSet.equal? 2020-11-12 09:51:38 +01:00
José Valim fb959e2db1 Improve coverage and add type docs 2020-11-12 09:51:38 +01:00
José Valim 3ee552b142 Small improvements to access docs 2020-11-12 09:51:38 +01:00
Dominic Letz fe03adbbde Add :compressed to stream_modes spec (#10490)
:compressed is already supported here so that gzipped files
can be streamed `File.stream!("log.gz", [:compressed])`.
2020-11-11 18:06:02 +01:00
Richard Michael 420db767c9 Explain REF format in mix.deps output (#10488) 2020-11-10 10:47:48 +01:00
Aleksei Matiushkin 86f0561d60 Dependency Handling section in Library Guidelines (#10487) 2020-11-06 15:58:43 +01:00
Matteo CostantiniandMatteo Costantini 3205a18142 Fix mix escript.build docs (#10486)
Co-authored-by: Matteo Costantini <matteo.costantini@dottori.it>
2020-11-06 14:35:25 +01:00
José Valim f20f4a3adc Pass the shell as argument when autocompleting
Closes #10480
2020-11-05 18:08:23 +01:00
Eksperimental 23d75ecdf5 Remove unnecessary @doc false in defimpl (#10483) 2020-11-05 11:39:06 +01:00
José Valim 6320ee7953 Fix unused variable warning 2020-11-03 20:43:05 +01:00
José Valim 39c15699fe Simplify merge and check unused vars 2020-11-03 20:42:53 +01:00
José Valim 648ee98794 Deprecate System.stacktrace/0 across the language 2020-11-03 20:28:52 +01:00
Jonatan Männchen fc5fca1dad Propose pin operator with unused overridden variable (#10479)
When rebinding a variable that is not later used, the warning
proposes to use the pin operator.

Closes ##10478.
2020-11-03 20:28:46 +01:00
José Valim 647edacd93 Check for stacktrace at compile-time and runtime 2020-11-03 20:19:01 +01:00
Thijs Klaver 72a46475e4 Fix typo in documentation (#10477) 2020-11-03 18:13:38 +01:00
José Valim fe9a18abaf Fix failing test 2020-11-03 15:45:30 +01:00
Thomas Citharel b96e766609 Fix small typo in Xref task docs (#10476) 2020-11-03 15:17:33 +01:00
Eric Meadows-Jönsson 6e48afb9dd Use directional unification for matches (#10473) 2020-11-02 16:20:14 +01:00
Gustavo Saiani b4de082432 Two minor doc fixes in ExUnit.Callback (#10472) 2020-11-02 13:07:40 +01:00
José Valim b7f65bb8ef Skip application boundary warnings for protocols
Otherwise we can get false positives when compiling
a protocol and there is an implementation defined
in another application that the protocol definition
does not depend on.

This commit also rewrites @protocol to @__protocol__
to prepare for future tracking changes.
2020-11-02 11:49:45 +01:00
José Valim abaf3505f9 Add TODOs 2020-11-01 18:49:56 +01:00
José Valim c36791091f Consider CRLF on Windows, closes #10470 2020-11-01 17:25:06 +01:00
Wojtek Mach 0530ef50c5 Update ansi.ex (#10469) 2020-11-01 16:01:49 +01:00
José Valim 31751f67da Docs: add more guidelines around parens 2020-10-31 10:46:41 +01:00
José Valim 03536e7c64 Prune tracers when fetching __ENV__ inside functions, closes #10466 2020-10-30 13:30:01 +01:00
José Valim 7ae5365d45 Remove unused function 2020-10-29 23:53:54 +01:00
José Valim 9b06694ca5 Remove allocator areas from runtime info (they are not allocators) 2020-10-29 23:44:00 +01:00
José Valim 7fcb057b35 Fix race condition when loading test helper, closes #10464 2020-10-29 19:28:37 +01:00
Nathan 0414a85fcf Mirror release permissions to the ones in OS package managers (#10458) 2020-10-29 18:04:23 +01:00
Eric Meadows-Jönsson f351ea72d8 Add expected type (#10459) 2020-10-28 20:21:26 +01:00
Wojtek Mach 29f14b92ee Update DynamicSupervisor option typedocs (#10462) 2020-10-28 19:30:34 +01:00
Fernando Tapia Rico 50f5f730c6 Minor change on 'mix release' docs (#10461) 2020-10-28 18:26:48 +01:00
José Valim e233c75a46 Fix dialyzer specs for Task.Supervisor, closes #10460 2020-10-28 08:51:05 +01:00
Eksperimental 6aeae2af5f Improve docs for Kernel.raise/1-2 (#10457) 2020-10-26 22:28:56 +01:00
jack 2b61999f9c Update function convert_time_unit doc (#10455) 2020-10-26 07:15:19 +01:00
José Valim 272303e558 Ensure manually set tracers are kept on compilation
Closes #10451.
2020-10-22 08:48:16 +02:00
José Valim 367b302663 Fix race condition deps.compile+deps.update
The test for this scenario is throuoghly
expressed in Hex' suite.
2020-10-21 20:53:41 +02:00
Tsutomu Kuroda 92df0bf9fd Fix docs for Kernel#and/2 and Kernel#or/2 (#10450) 2020-10-21 08:50:40 +02:00
José Valim b0e4b813a9 Clarify that imports override, closes #10448 2020-10-20 22:50:52 +02:00
José Valim 1000780a75 Handle aliases with code recursion
It addresses a regression introduced in 97e8b0242d.

Closes https://github.com/nerves-project/nerves/issues/557.
2020-10-20 20:34:09 +02:00
Chris Wögi e0a55f41c9 Improve Config docs (#10447)
Add missing config_env/0 & config_target/0
to the list of imports of `import Config`.
2020-10-20 17:53:01 +02:00
Łukasz Samson b7375e59ca Add more information when behaviour_info/1 is called locally (#10446) 2020-10-20 16:36:07 +02:00
José Valim 82a1834bbe Warn when using special functions locally, closes #10444 2020-10-20 10:51:43 +02:00
José Valim 4bec4fa762 Do not crash if a module is given instead of file on inner compiler error 2020-10-20 10:51:43 +02:00
Wojtek Mach f6b3be872e Add --warnings-as-errors option to mix test (#10435) 2020-10-20 08:13:13 +02:00
Garth Kidd 4b9e9a4cd4 Unset CDPATH while finding RELEASE_ROOT (#10445)
Solves #9835 without user intervention.
2020-10-20 08:12:36 +02:00
Saša Jurić 1dcce6309c Fix stop_supervised/1 for temporary children (#10442) 2020-10-18 21:37:12 +02:00
Wojtek Mach f3d40238d6 Minor docs updates (#10441)
Brought to you by:

    mix archive.install github wojtekmach/app_manifest
    asdf shell elixir 1.10.4-otp-23 ; mix app_manifest elixir eex ex_unit iex logger mix > manifest-1.10.4.txt
    asdf shell elixir 1.11.1-otp-23 ; mix app_manifest elixir eex ex_unit iex logger mix > manifest-1.11.1.txt
    git diff --no-index manifest-1.10.4.txt manifest-1.11.1.txt

:-D
2020-10-18 17:33:22 +02:00
José Valim 04e24965e6 Do not use private and removed ram_file:compress/1 (#10439) 2020-10-17 21:25:14 +02:00
José Valim f1c09df99e Fix test on earlier OTP versions 2020-10-17 20:29:10 +02:00
José Valim aa78b32291 Allow @on_load to be private, closes #10438 2020-10-17 20:08:06 +02:00
José Valim 65af7c254d Fix bootstrap issue in Code module 2020-10-17 20:07:48 +02:00
José Valim 330db011b6 Do not crash when getting docs for missing erts appdir
Closes #10437.
2020-10-17 19:45:54 +02:00
José Valim a042771ef2 Raise for is_record guards, closes #10436 2020-10-17 19:33:03 +02:00
José Valim 3415f904ab Update CHANGELOG 2020-10-16 09:27:01 +02:00
José Valim 5239954184 Add inet6 fallback to Mix usage of httpc, closes #10423 2020-10-16 09:20:55 +02:00
José Valim 87e19e353f Properly handle tags inside typespec in Erlang docs, closes #10430 2020-10-16 09:08:27 +02:00
Eksperimental 93a6b0d119 Improve error message in Stream.cycle/1 (#10427) 2020-10-14 19:42:38 +02:00
Eksperimental 3c3c0268e5 Streamline docs and function head for recently introduced zip_with functions (#10426) 2020-10-14 19:25:21 +02:00
José Valim dfd9d9aa57 Use Application.ensure_all_started/2
Our hand-rolled implementation won't behave
correctly if optional_applications are added
to Erlang/OTP, so let's depend directly on
them.
2020-10-13 19:00:38 +02:00
Michał Łępicki 2a5bde0fb7 Fix spec for Enum.zip_with/2 (#10424) 2020-10-13 12:31:19 +02:00
José Valim a47ef9e889 Bypass protocol dispatch on zip_with 2020-10-13 09:13:12 +02:00
Adam Lancaster 1705984fb4 Implements Enum.zip_with and Stream.zip_with (#10419) 2020-10-13 09:11:24 +02:00
José Valim 818af23bf1 Do not deadlock Logger if handler crashes on sync mode, closes #10420 2020-10-12 09:27:22 +02:00
José Valim b0bec91f31 Add :append to stream_mode, closes #10421 2020-10-12 09:14:03 +02:00
José Valim 15d791547c Ignore tracers if lexical tracker is dead or explicitly nil 2020-10-11 00:15:25 +02:00
Milton Mazzarri ba0dc877ef Do not show warning when super is in GenServer.child_spec/1 (#10418)
The original goal[1] was to deprecate the usage of `super` on every `GenServer`
callback, but `child_spec/1` is not a callback, it's a default implementation
that can be re-implemented.

This commit removes the _deprecation warning_ when someone use `super` in
`GenServer.child_spec/1`.

Fixes: #10415

[1] https://github.com/elixir-lang/elixir/commit/c024b0eeb2dc69a759fe5ceeb1c8216d58ae9386#diff-ed0344c13ed25389035f337d94902894R301-R316
2020-10-10 23:31:05 +02:00
Eksperimental 81928746ed Fix warning message in app requirement (#10417)
It was missing a closing square bracket.

Previously it was saying

  3. In case you don't want to add a requirement to :crypto, you may optionally skip this warning by adding [xref: [exclude: :crypto] to your "def project" in mix.exs

Now

  3. In case you don't want to add a requirement to :crypto, you may optionally skip this warning by adding [xref: [exclude: :crypto]] to your "def project" in mix.exs
2020-10-10 15:36:47 +02:00
Cẩm Huỳnh fc3104ca98 Rename a variable in Inspect documentation (#10416) 2020-10-10 15:28:23 +02:00
Brian Gomes Bascoy 4ba471a98d IEx help in place with tab key (#10339) 2020-10-08 16:57:03 +02:00
José Valim ab28c958a8 Do not crash when :reduce is set to nil in comprehensions, closes #10411 2020-10-08 15:37:48 +02:00
Xavier Noria d4b8dd37ea Improves the docs of config/runtime.exs and Config.config_env/0 (#10414) 2020-10-08 15:03:28 +02:00
José Valim faf1157020 Fix order dependent tests in logger suite 2020-10-08 09:43:45 +02:00
Michał Łępicki 86c1460863 Fix returning errors in Module.Types.Expr.of_expr (#10410) 2020-10-08 09:36:46 +02:00
José Valim 72c429206c Keep warnings when traversing clauses 2020-10-07 23:52:47 +02:00
Sergei Maximov 3681c387bc Fix a typo in Kernel.SpecialForms.&/1 docs (#10409) 2020-10-07 17:00:24 +02:00
Wojtek Mach 06f086a21e Fix IEx parser configuration (#10407) 2020-10-07 13:44:33 +02:00
José Valim 5ea99c5cd4 Do not add runtime dependencies to remotes in typespecs, closes #10406 2020-10-07 13:35:59 +02:00
José Valim e7cf3016bd Fix handle_call return type 2020-10-07 07:48:28 +02:00
José Valim b4386f7545 Update CHANGELOG 2020-10-06 19:57:44 +02:00
José Valim b7c9784f68 Remove TODO
The proposal for optional application in OTP is to
always start them if they are available and we don't
want to always start ExUnit or IEx before running
most projects, so we will always treat them as extra
apps.
2020-10-06 19:53:20 +02:00
José Valim 830bafe700 Consider targets when filtering apps, closes #10405 2020-10-06 19:30:47 +02:00
Eksperimental 7808f49c7b Add tests to Keyword.{replace, replace!}/3 (#10404) 2020-10-06 15:41:31 +02:00
Eksperimental 3b77056c72 Document since version in List.{first, last}/1-2 (#10403) 2020-10-06 15:24:01 +02:00
Eksperimental 87f132fbd5 Add tests to Kernel.struct/2, Map.{replace, replace!}/3 (#10402) 2020-10-06 15:22:39 +02:00
José Valim 285a6a6f84 Intercept sigquit on ExUnit
By using `Ctrl+\ ` to shutdown the VM, ExUnit will
intercept the sigquit signal and print the tests
currently running. This can be used to see which
test is stuck in a long running test suite:

    $ mix test
    .^\

    Aborting test suite, the following have not completed:

    * test greets the world [test/example_test.exs:5]
    * AnotherTest [test/another_test.exs]

    Showing results so far...

    1 doctest, 0 failures

    Randomized with seed 825614

As shown above, we also include a summary of the run
so far, alongside the seed and failure reports.
2020-10-06 12:20:59 +02:00
José Valim 04370f6ef2 Move parse to its own block and only set history/whereami when evaluating 2020-10-06 09:19:51 +02:00
James Russo cd04132761 Make IEx parser customizable (#10394) 2020-10-06 09:14:03 +02:00
José Valim efd0dd0e95 Consistently use :lists.reverse/1 2020-10-06 09:09:32 +02:00
Evadne Wu 1a263a6ec9 Updated Enumerable implementation for Stream (#10397)
`:inline_list_funs` → `:inline_list_funcs` as per https://erlang.org/doc/man/compile.html#inlining-of-list-functions.
2020-10-06 09:08:27 +02:00
Wojtek Mach 0e135ca0d1 Document Logger.Backends.Console (#10396) 2020-10-05 10:05:20 +02:00
José Valim e506ef460c Remove #=> before exception 2020-10-04 18:08:55 +02:00
Eksperimental 80b6ae36c4 Remove compilation warning in doctest in Kernel.binary_part/3 (#10393)
> warning: this expression will fail with ArgumentError
>   (for doctest at) lib/kernel.ex:310

It was introduced in  538a9a3 (#10370)
2020-10-04 18:08:37 +02:00
Eksperimental 98bab93b45 Raise if @enforce_keys keys are not found in defstruct
Closes #10353
2020-10-04 18:07:29 +02:00
Eksperimental 7d085ff93b Move struct tests in Kernel.ErrorsTest to their own describe 2020-10-04 18:07:29 +02:00
Blake Kostner e331294798 Add default value for List.first/1 and List.last/1 (#10391) 2020-10-04 08:57:43 +02:00
Aleksei Pakharev 6940ce6e8d Fix single quotes to backticks in string.ex (#10392) 2020-10-04 08:56:44 +02:00
José Valim a1c90e6a89 Only check the mode after matching on the codepoint
This guarantees the compiler will not emit the code
that is first always checking the mode.
2020-10-03 10:20:08 +02:00
Alt D. Soy 8ff465fa49 Add :turkic mode option to String case functions (#10376)
This PR adds support of Turkic languages casing of the letter i according
to Unicode SpecialCasing.txt.
2020-10-03 09:52:18 +02:00
Daniil Fedotov f1f2d46ec8 Restrict Logger.configure on config_change callback (#10388)
The `config_change` callback is called from `application_controller` process,
which is receiving env updates on releases. `Logger.configure` does a blocking
call to the `Logger` process, which calls `Application.put_env`

This causes a crash on logger config update.

The fix is to call `Logger.configure` only for the config that have not yet
been persisted.
2020-10-03 08:25:15 +02:00
José Valim 33e59c4b12 Fix race condition in tests 2020-10-02 23:10:45 +02:00
Lucas Lopes Paszinski 63d003fead Add "--warnings-as-errors" documentation to mix compile command line options. (#10387) 2020-10-02 23:02:29 +02:00
Waldemar Rachwał f029808903 Take --dot-iex into account after shell respawn (#10384) 2020-10-01 20:35:58 +02:00
Dave Lucia 68a4a31f1e Enum.max_by/3 and Enum.min_by/3 dialyzer fixes (#10383) 2020-10-01 13:23:06 +02:00
Michał Łępicki 50b410a97e Fix typo in types_test.exs (#10382)
existant -> existent
2020-10-01 10:58:08 +02:00
José Valim 76d245b608 Lift on errors (#10380) 2020-09-30 13:51:32 +02:00
José Valim 466bfc79fb Handle value vars in open maps without singleton keys (#10378) 2020-09-29 17:54:31 +02:00
José Valim e5746a2354 Handle var | left_type < right_type (#10377) 2020-09-29 17:54:11 +02:00
José Valim 5d212030bc Fix inference of open maps with non-singleton keys
Closes #10371.
2020-09-29 13:11:03 +02:00
Everett Griffiths f01d47ceb6 Clarify args passed to callback fun in File.cp/3 (#10374)
And improve general readability of this function's docs
2020-09-28 17:05:28 +02:00
Paulo Daniel Gonzalez fa8d38ecfe Use timer function instead of manually calculating milliseconds (#10367) 2020-09-27 16:41:25 +02:00
Hassan Shaikley 538a9a3f41 ArgumentError example for binary_part (#10370) 2020-09-27 08:44:07 +02:00
Tomasz Kowal 62aa2ee33c Fix copy paste mistake in String.capitalize docs (#10369) 2020-09-26 08:44:30 +02:00
José Valim 32f9b4c2f0 Update CHANGELOG 2020-09-25 13:48:16 +02:00
José Valim 78c05c2db7 Make config/runtime.exs deep merge, closes #10365 2020-09-25 13:30:52 +02:00
José Valim 2c354b9709 Fallback to regular attribute lookup 2020-09-25 12:58:58 +02:00
Gustavo Saiani 73fc537636 Remove duplicate assertion (#10366) 2020-09-24 14:51:41 +02:00
José Valim 9430e5c9dc Catch dialyzer bug 2020-09-23 16:14:57 +02:00
Gabriel Alves b2f561ef90 Raise instead of crashing on when trying to use list operators in guards (#10363) 2020-09-23 14:30:11 +02:00
José Valim 75726d5611 Keep invoking loadconfig on cli boot 2020-09-22 15:47:20 +02:00
José Valim 30f458fbb4 Add a note on async_stream+take 2020-09-22 11:11:52 +02:00
John-Goff 38feb59ba6 Update docs of Task.async_stream to talk more about side effects (#10359) 2020-09-21 16:06:45 +02:00
Gustavo Saiani 408233e3b5 Remove duplicated assert in quote test (#10358) 2020-09-21 14:49:17 +02:00
José Valim 202f0cdf97 Remove fresh context to optimize in-body guards (#10356) 2020-09-21 14:01:47 +02:00
Jason Goldberger d36ccf90ed Improved performance of List.last/1 (#10350) 2020-09-20 21:56:37 +02:00
Eksperimental 7dc2dd2e8e Rename struct attribute in defstruct (#10354)
Rename `@struct` to `@__struct__`.

Closes #10352
2020-09-20 20:26:25 +02:00
Eksperimental 6ad842a2e6 Add guard checks to Keyword.update/4 (#10348) 2020-09-18 20:13:24 +02:00
José Valim 25230b0932 Deprecate @foo() in favor of @foo, closes #10344 2020-09-18 16:36:28 +02:00
José Valim 3273dc7943 Make replace and replace! in Keyword consistent, closes #10345 2020-09-18 16:13:09 +02:00
José Valim b1d9f3da66 Return an empty map instead of none for public but undocumented functions 2020-09-18 11:59:44 +02:00
José Valim df04a2d53b Handle empty map in other places, closes #10343 2020-09-18 10:53:49 +02:00
José Valim 08f6bbd031 Align doc chunks return with EEP 48 2020-09-18 10:41:34 +02:00
José Valim 0e778ba754 Update iso.ex 2020-09-17 19:21:37 +02:00
José Valim 32890a3149 Append space on middle and start expr if necessary, closes #10340 2020-09-17 17:11:22 +02:00
Eksperimental 8b4bf64ed0 Include ExUnit examples in files to format (#10342) 2020-09-17 16:46:36 +02:00
Eksperimental bc701e719d Fix and improve specs and docs in Map.pop/3 and Map.pop!/2 (#10341) 2020-09-17 16:35:14 +02:00
José Valim 94f6549969 Be less aggressive on picking files to format
Otherwise it can pick up files in tmp directories
if it runs at the same time tests are running.
2020-09-17 14:26:09 +02:00
José Valim d6ccfa444e Speed up compilation with multiple describe 2020-09-17 14:24:49 +02:00
Fernando Tapia Rico cd0dbb58f4 Fix typo in ExUnit.Case 2020-09-17 11:02:19 +02:00
José Valim 216d6ecbdd Introduce register_test/6 to speed up test definition 2020-09-17 10:56:12 +02:00
José Valim c9dc22bab1 Reduce amount of injected code in case 2020-09-17 10:29:32 +02:00
José Valim a9059cd187 Update helpers.ex 2020-09-16 20:20:52 +02:00
Eksperimental d25b61db13 Update formatter inputs (#10335) 2020-09-16 07:59:36 +02:00
Eksperimental c97e1f49f1 Update map.ex (#10338)
Make output reflect how it is actually represented
2020-09-16 07:28:18 +02:00
José Valim ae97bb65fb Properly handle reduce in types, closes #10224 2020-09-14 23:01:58 +02:00
Eksperimental 9e5b148bc1 Format missing Collectable implementation (#10334) 2020-09-14 19:44:37 +02:00
José Valim bcbdd61bcc More typing improvements (#10333)
1. Move push_expr_stack to unify and keep helpers typing free

2. Add get_var! when accessing variables so it helps us find
   variables that have not been properly processed

3. Properly process all variables found missing on step 2
2020-09-14 19:30:35 +02:00
Eksperimental 609f67b0ad Improve docs and argument names for Collectable module (#10332)
It is not clear what original mean, if the element we extract data or the element we put into.
For clarity I chose to name it the same as the module.

this change make it clear that what needs to be returned is the initial accumulator and not the collectable we are
puting data into.

-          {original, collector_fun}
+          initial_acc = map_set
+
+          {initial_acc, collector_fun}

mention that the MapSet implementation is a simplified version, not the real one.

The implementations in the module have been reformated and some attribute names changed, so it is easier for developers who want to read the source code to understand how protocols are implemented.
2020-09-14 18:07:57 +02:00
Eksperimental 1d6779dfd4 Add v1.12 to Comptaibility page (#10330) 2020-09-14 15:23:38 +02:00
José Valim e6b861ca74 Remove boolean and number as builtin types (#10329) 2020-09-14 14:04:06 +02:00
José Valim 3d43d93d45 Infer -> Unify 2020-09-14 13:32:13 +02:00
José Valim d55c5cbe11 Remove duplicate tests 2020-09-14 11:40:36 +02:00
José Valim ae6b72794a Add pending deprecation, update CHANGELOG 2020-09-14 11:21:49 +02:00
José Valim 9bf7240419 Start v1.12+ 2020-09-14 09:45:34 +02:00
326 changed files with 17968 additions and 8277 deletions
+4 -3
View File
@@ -12,20 +12,21 @@ test_template: &DEFAULT_TEST_SETTINGS
test_freebsd_task:
<<: *DEFAULT_TEST_SETTINGS
name: FreeBSD 12.1
name: FreeBSD 13.0
alias: FreeBSD Stable
freebsd_instance:
image_family: freebsd-12-1
image_family: freebsd-13-0
cpu: 8
memory: 7424Mi
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
PATH: $PATH:/usr/local/lib/erlang22/bin
install_script:
- pkg install -y erlang git gmake
- pkg install -y erlang-runtime22 git gmake
- rm -rf .git
- gmake compile
+3 -2
View File
@@ -1,9 +1,10 @@
[
inputs: [
"lib/*/{lib,unicode,test}/**/*.{ex,exs}",
"lib/*/mix.exs"
"lib/*/*.exs",
"lib/ex_unit/examples/*.exs",
".formatter.exs"
],
locals_without_parens: [
# Formatter tests
assert_format: 2,
+87
View File
@@ -0,0 +1,87 @@
name: builds.hex.pm
on:
push:
branches:
- main
- v*.*
tags:
- v*
env:
ELIXIR_OPTS: "--warnings-as-errors"
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
concurrency: builds_txt
jobs:
release_pre_built:
strategy:
fail-fast: true
max-parallel: 1
matrix:
include:
- otp: 23
otp_version: '23.3'
build_docs: build_docs
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- name: Get tags
run: git fetch --tags origin
- uses: ./.github/workflows/release_pre_built
with:
otp_version: ${{ matrix.otp_version }}
otp: ${{ matrix.otp }}
build_docs: ${{ matrix.build_docs }}
- name: Utils.sh
run: |
cat << 'EOF' > utils.sh
function purge_key() {
curl \
-X POST \
-H "Fastly-Key: ${FASTLY_KEY}" \
-H "Accept: application/json" \
-H "Content-Length: 0" \
"https://api.fastly.com/service/$1/purge/$2"
}
function purge() {
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
sleep 2
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
sleep 2
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
}
EOF
chmod +x utils.sh
- name: Upload Docs to S3
if: ${{ matrix.build_docs }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
run: |
source utils.sh
version=$(echo ${{ github.ref_name }} | sed -e 's/^v//g')
for f in doc/*; do
if [ -d "$f" ]; then
app=`echo $f | sed s/"doc\/"//`
tarball="${app}-${version}.tar.gz"
surrogate_key="docs/${app}-${version}"
tar -czf "${tarball}" -C "doc/${app}" .
aws s3 cp "${tarball}" "s3://${{ env.AWS_S3_BUCKET }}/docs/${tarball}" \
--cache-control "public,max-age=3600" \
--metadata "{\"surrogate-key\":\"${surrogate_key}\",\"surrogate-control\":\"public,max-age=604800\"}"
purge "${surrogate_key}"
fi
done
+11 -8
View File
@@ -13,8 +13,9 @@ jobs:
name: Linux, ${{ matrix.otp_release }}, Ubuntu 16.04
continue-on-error: ${{ matrix.development }}
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-23.0', 'OTP-22.3', 'OTP-22.0', 'OTP-21.3.8', 'OTP-21.0']
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
development: [false]
include:
- otp_release: master
@@ -23,17 +24,17 @@ jobs:
development: true
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Erlang/OTP
run: |
cd $RUNNER_TEMP
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-14.04/${{ matrix.otp_release }}.tar.gz
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-16.04/${{ matrix.otp_release }}.tar.gz
mkdir -p otp
tar zxf otp.tar.gz -C otp --strip-components=1
otp/Install -minimal $(pwd)/otp
echo "::add-path::$(pwd)/otp/bin"
echo "$(pwd)/otp/bin" >> $GITHUB_PATH
- name: Compile Elixir
run: |
rm -rf .git
@@ -50,7 +51,7 @@ jobs:
run: make test_elixir
- name: Check reproducible builds
run: taskset 1 make check_reproducible
if: matrix.otp_release == 'OTP-23.0'
if: matrix.otp_release == 'OTP-24.0'
test_windows:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
@@ -61,7 +62,7 @@ jobs:
steps:
- name: Configure Git
run: git config --global core.autocrlf input
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Cache Erlang/OTP package
@@ -82,13 +83,15 @@ jobs:
- name: Erlang test suite
run: make --keep-going test_erlang
- name: Elixir test suite
run: make --keep-going test_elixir
run: |
del c:/Windows/System32/drivers/etc/hosts
make --keep-going test_elixir
check_posix_compliant:
name: Check POSIX-compliant
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Shellcheck
@@ -0,0 +1,51 @@
name: "Release pre built"
description: "Builds elixir release, ExDoc and generates docs"
inputs:
otp:
description: "The major OTP version"
otp_version:
description: "The exact OTP version (major.minor[.patch])"
build_docs:
description: "If docs have to be built or not"
runs:
using: "composite"
steps:
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ inputs.otp_version }}
version-type: strict
- name: Build Elixir Release
shell: bash
run: |
make Precompiled.zip
mv Precompiled.zip elixir-otp-${{ inputs.otp }}.zip
shasum -a 1 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha1sum
shasum -a 256 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha256sum
echo "$PWD/bin" >> $GITHUB_PATH
- name: Get latest stable ExDoc version
if: ${{ inputs.build_docs }}
shell: bash
run: |
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
echo "EX_DOC_LATEST_STABLE_VERSION=${EX_DOC_LATEST_STABLE_VERSION}" >> $GITHUB_ENV
- uses: actions/checkout@v3
if: ${{ inputs.build_docs }}
with:
repository: elixir-lang/ex_doc
ref: v${{ env.EX_DOC_LATEST_STABLE_VERSION }}
path: ex_doc
- name: Build ex_doc
if: ${{ inputs.build_docs }}
shell: bash
run: |
mv ex_doc ../ex_doc
cd ../ex_doc
../elixir/bin/mix do local.rebar --force, local.hex --force, deps.get, compile
cd ../elixir
- name: Build Docs
if: ${{ inputs.build_docs }}
shell: bash
run: |
make Docs.zip
shasum -a 1 Docs.zip > Docs.zip.sha1sum
shasum -a 256 Docs.zip > Docs.zip.sha256sum
+245 -342
View File
@@ -1,406 +1,309 @@
# Changelog for Elixir v1.11
# Changelog for Elixir v1.12
Over the last releases, the Elixir team has been focusing on the compiler, both in terms of catching more mistakes at compilation time and making it faster. Elixir v1.11 has made excellent progress on both fronts. This release also includes many other goodies, such as tighter Erlang integration, support for more guard expressions, built-in datetime formatting, and other calendar enhancements.
Elixir v1.12 is out with improvements to scripting, tighter Erlang/OTP 24 integration, stepped ranges, and dozen of new functions across the standard library. Overall this is a small release, which continues our tradition of bringing Elixir developers quality of life improvements every 6 months.
## Tighter Erlang integration
Elixir v1.12 requires Erlang/OTP 22+. We also recommend running `mix local.rebar` after installation to upgrade to the latest Rebar versions, which includes support for Erlang OTP/24+.
Following Elixir v1.10, we have further integrated with Erlang's new logger by adding four new log levels: `notice`, `critical`, `alert`, and `emergency`, matching all log levels found in the Syslog standard. The `Logger` module now supports structured logging by passing maps and keyword lists to its various functions. It is also possible to specify the log level per module, via the `Logger.put_module_level/2` function. Log levels per application will be added in future releases.
## Scripting improvements: `Mix.install/2` and `System.trap_signal/3`
IEx also has been improved to show the documentation for Erlang modules directly from your Elixir terminal. This works with Erlang/OTP 23+ and requires Erlang modules to have been compiled with documentation chunks.
Elixir v1.12 brings new conveniences for those using Elixir for scripting (via `.exs` files). Elixir has been capable of managing dependencies for a quite long time, but it could only be done within Mix projects. In particular, the Elixir team is wary of global dependencies as any scripts that rely on system packages are brittle and hard to reproduce whenever your system changes.
## Compiler checks: application boundaries
Elixir v1.11 builds on top of the recently added compilation tracers to track application boundaries. From this release, Elixir will warn if you invoke a function from an existing module but this module does not belong to any of your listed dependencies.
These two conditions may seem contradictory. After all, if a module is available, it must have come from a dependency. This is not true in two scenarios:
* Modules from Elixir and Erlang/OTP are always available - even if their applications are not explicitly listed as a dependency
* In an umbrella project, because all child applications are compiled within the same VM, you may have a module from a sibling project available, even if you don't depend on said sibling
This new compiler check makes sure that all modules that you invoke are listed as part of your dependencies, emitting a warning like below otherwise:
```text
:ssl.connect/2 defined in application :ssl is used by the current
application but the current application does not directly depend
on :ssl. To fix this, you must do one of:
1. If :ssl is part of Erlang/Elixir, you must include it under
:extra_applications inside "def application" in your mix.exs
2. If :ssl is a dependency, make sure it is listed under "def deps"
in your mix.exs
3. In case you don't want to add a requirement to :ssl, you may
optionally skip this warning by adding [xref: [exclude: :ssl]
to your "def project" in mix.exs
```
This comes with extra benefits in umbrella projects, as it requires child applications to explicitly list their dependencies, completely rejecting cyclic dependencies between siblings.
## Compiler checks: data constructors
In Elixir v1.11, the compiler also tracks structs and maps fields across a function body. For example, imagine you wanted to write this code:
def drive?(%User{age: age}), do: age >= 18
If there is either a typo on the `:age` field or the `:age` field was not yet defined, the compiler will fail accordingly. However, if you wrote this code:
def drive?(%User{} = user), do: user.age >= 18
The compiler would not catch the missing field and an error would only be raised at runtime. With v1.11, Elixir will track the usage of all maps and struct fields within the same function, emitting warnings for cases like above:
```text
warning: undefined field `age` in expression:
# example.exs:7
user.age
expected one of the following fields: name, address
where "user" was given the type %User{} in:
# example.exs:7
%User{} = user
Conflict found at
example.exs:7: Check.drive?/1
```
The compiler also checks binary constructors. Consider you have to send a string over the wire with length-based encoding, where the string is prefixed by its length, up to 4MBs. Your initial attempt may be this:
def run_length(string) when is_binary(string) do
<<byte_size(string)::32, string>>
end
However, the code above has a bug. Each segment given between `<<>>` must be an integer, unless specified otherwise. With Elixir v1.11, the compiler will let you know so:
```text
warning: incompatible types:
binary() !~ integer()
in expression:
<<byte_size(string)::integer()-size(32), string>>
where "string" was given the type integer() in:
# foo.exs:4
<<byte_size(string)::integer()-size(32), string>>
where "string" was given the type binary() in:
# foo.exs:3
is_binary(string)
HINT: all expressions given to binaries are assumed to be of type integer()
unless said otherwise. For example, <<expr>> assumes "expr" is an integer.
Pass a modifier, such as <<expr::float>> or <<expr::binary>>, to change the
default behaviour.
Conflict found at
foo.exs:4: Check.run_length/1
```
Which can be fixed by adding `::binary` to the second component:
def run_length(string) when is_binary(string) do
<<byte_size(string)::32, string::binary>>
end
While some of those warnings could be automatically fixed by the compiler, future versions will also perform those checks across functions and potentially across modules, where automatic fixes wouldn't be desired (nor possible).
## Compilation time improvements
Elixir v1.11 features many improvements to how the compiler tracks file dependencies, such that touching one file causes less files to be recompiled. In previous versions, Elixir tracked three types of dependencies:
* compile time dependencies - if A depends on B at compile time, such as by using a macro, whenever B changes, A is recompiled
* struct dependencies - if A depends on B's struct, whenever B's struct definition changed, A is recompiled
* runtime dependencies - if A depends on B at runtime, A is never recompiled
However, because dependencies are transitive, if A depends on B at compile time and B depends on C at runtime, A would depend on C at compile time. Therefore, it is very important to reduce the amount of compile time dependencies.
Elixir v1.11 replaces "struct dependencies" by "exports dependencies". In other words, if A depends on B, whenever B public's interface changes, A is recompiled. B's public interface is made by its struct definition and all of its public functions and macros.
This change allows us to mark `import`s and `require`s as "exports dependencies" instead of "compile time" dependencies. This simplifies the dependency graph considerably. For example, [in the Hex.pm project](https://github.com/hexpm/hexpm), changing the `user.ex` file in Elixir v1.10 would emit this:
```text
$ touch lib/hexpm/accounts/user.ex && mix compile
Compiling 90 files (.ex)
```
In Elixir v1.11, we now get:
```text
$ touch lib/hexpm/accounts/user.ex && mix compile
Compiling 16 files (.ex)
```
To make things even better, Elixir v1.11 also introduces a more granular file tracking for path dependencies. In previous versions, a module from a path dependency would always be treated as a compile time dependency. This often meant that if you have an umbrella project, changing an application would cause many modules in sibling applications to recompile. Fortunately, Elixir v1.11 will tag modules from dependencies as exports if appropriate, yielding dramatic improvements to those using path dependencies.
To round up the list of compiler enhancements, the `--profile=time` option added in Elixir v1.10 now also includes the time to compile each individual file. For example, in the Plug project, one can now get:
```text
[profile] lib/plug/conn.ex compiled in 935ms
[profile] lib/plug/ssl.ex compiled in 147ms (plus 744ms waiting)
[profile] lib/plug/static.ex compiled in 238ms (plus 654ms waiting)
[profile] lib/plug/csrf_protection.ex compiled in 237ms (plus 790ms waiting)
[profile] lib/plug/debugger.ex compiled in 719ms (plus 947ms waiting)
[profile] Finished compilation cycle of 60 modules in 1802ms
[profile] Finished group pass check of 60 modules in 75ms
```
While implementing those features, we have also made the `--long-compilation-threshold` flag more precise. In previous versions, `--long-compilation-threshold` would consider both the time a file spent to compile and the time spent waiting on other files. In Elixir v1.11, it considers only the compilation time. This means less false positives and you can now effectively get all files that take longer than 2s to compile by passing `--long-compilation-threshold 2`.
## `mix xref graph` improvements
To bring visibility to the compiler tracking improvements described in the previous section, we have also added new features to `mix xref`. `mix xref` is a task that describes cross-references between files in your projects. The `mix xref graph` subsection focuses on the dependency graph between them.
First we have made the existing `--label` flag to consider transitive dependencies. Using `--sink FILE` and `--label compile` can be a powerful combo to find out which files will change whenever the given `FILE` changes. For example, in the Hex.pm project, we get:
```text
$ mix xref graph --sink lib/hexpm/accounts/user.ex --label compile
lib/hexpm/billing/hexpm.ex
└── lib/hexpm/billing/billing.ex (compile)
lib/hexpm/billing/local.ex
└── lib/hexpm/billing/billing.ex (compile)
lib/hexpm/emails/bamboo.ex
├── lib/hexpm/accounts/email.ex (compile)
└── lib/hexpm/accounts/user.ex (compile)
lib/hexpm/emails/emails.ex
└── lib/hexpm_web/views/email_view.ex (compile)
lib/hexpm_web/controllers/api/docs_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/key_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/organization_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/organization_user_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/owner_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/package_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/release_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/repository_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/retirement_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/blog_controller.ex
└── lib/hexpm_web/views/blog_view.ex (compile)
lib/hexpm_web/endpoint.ex
├── lib/hexpm_web/plug_parser.ex (compile)
└── lib/hexpm_web/session.ex (compile)
```
All the files at the root will recompile if `lib/hexpm/accounts/user.ex` changes. Their children describe the *why*. For example, the `repository_controller.ex` file will recompile if user changes because it has a compile time dependency on `auth_helpers.ex`, which depends on `user.ex`. This indirect compile time dependency is often the source of recompilations and Elixir v1.11 now makes it trivial to spot them, so they can be eventually addressed.
Another improvement to `mix xref graph` is the addition of `--format cycles`, which will print all cycles in your compilation dependency graph. A `--min-cycle-size` flag can be used if you want to discard short cycles.
## `config/runtime.exs` and `mix app.config`
Elixir v1.9 introduced a new configuration file, specific to releases, called `config/releases.exs`. A release is a self-contained artifact with the Erlang VM, Elixir and your application, ready to run in production.
The addition of `config/releases.exs` has been a very useful one but, unfortunately, it applies only to releases. Developers not using releases must use the `config/config.exs` file, which often loaded too early at compilation time. For any dynamic configuration, developers had to resort to third-party tools or workarounds to achieve the desired results.
Elixir v1.11 addresses this issue by introducing a new configuration file, called `config/runtime.exs`. This new configuration file is loaded exactly before your application starts, when the code is already fully compiled. It is loaded in development, test, and production, regardless if you are using Mix or releases. Therefore it provides a unified API for runtime configuration in Elixir.
`config/runtime.exs` works the same as any other configuration file. However, given `config/runtime.exs` is meant to run with or without Mix, developers must not use `Mix.env()` or `Mix.target()` in `config/runtime.exs`. Instead, they must use the new `config_env()` and `config_target()`, which have been added to the `Config` module.
While `config/releases.exs` will continue to be supported, developers can migrate to `config/runtime.exs` without loss of functionality. For example, a `config/releases.exs` file such as this one
`Mix.install/2` is meant to be a sweetspot between single-file scripts and full-blown Mix projects. With `Mix.install/2`, you can list your dependencies on top of your scripts. When you execute the script for the first time, Elixir will download, compile, and cache your dependencies before running your script. Future invocations of the script will simply read the compiled artefacts from the cache:
```elixir
# config/releases.exs
import Config
config :foo, ...
config :bar, ...
Mix.install([:jason])
IO.puts Jason.encode!(%{hello: :world})
```
could run as is as `config/runtime.exs`. However, given `config/runtime.exs` runs in all environments, you may want to restrict part of your configuration to the `:prod` environment:
`Mix.install/2` also performs protocol consolidation, which gives script developers an option to execute their code in the most performant format possible.
```elixir
# config/runtime.exs
import Config
**Note:** `Mix.install/2` is currently experimental and it may change in future releases.
if config_env() == :prod do
config :foo, ...
config :bar, ...
end
Another improvement to scripting is the ability to trap exit signals via `System.trap_signal/3`. All you need is the signal name and a callback that will be invoked when the signal triggers. For example, ExUnit leverages this functionality to print all currently running tests when you abort the test suite via SIGQUIT (`Ctrl+\\ `):
```
$ mix test
.......................................................................
.....................^\
Aborting test suite, the following have not completed:
* test query building [test/ecto/query_test.exs:48]
* test placeholders in Repo.insert_all [test/ecto/repo_test.exs:502]
Showing results so far...
78 doctests, 1042 tests, 0 failures
```
If both files are available, releases will pick the now preferred `config/runtime.exs` instead of `config/releases.exs`.
This is particularly useful when your tests get stuck and you want to know which one is the culprit.
To wrap it all up, `Mix` also includes a new task called `mix app.config`. This task loads all applications and configures them, without starting them. Whenever you write your own Mix tasks, you will typically want to invoke either `mix app.start` or `mix app.config` before running your own code. Which one is better depends if you want your applications running or only configured.
**Important**: Trapping signals may have strong implications on how a system shuts down and behave in production and therefore it is extremely discouraged for libraries to set their own traps. Instead, they should redirect users to configure them themselves. The only cases where it is acceptable for libraries to set their own traps is when using Elixir in script mode, such as in `.exs` files and via Mix tasks.
## Other improvements
## Tighter Erlang/OTP 24 integration
Elixir v1.11 adds the `is_struct/2`, `is_exception/1`, and `is_exception/2` guards. It also adds support for the `map.field` syntax in guards.
Erlang/OTP 24 ships with JIT compilation support and Elixir developers don't have to do anything to reap its benefits. There are many other features in Erlang/OTP 24 to look forwards to and Elixir v1.12 provides integration with many of them: such as support for 16bit floats in bitstrings as well as performance improvements in the compiler and during code evaluation.
The Calendar module ships with a new `Calendar.strftime/3` function, which provides datetime formatting based on the `strftime` format. The `Date` module got new functions for working with weeks and months, such as `Date.beginning_of_month/1` and `Date.end_of_week/2`. Finally, all calendar types got conversion functions from and to gregorian timestamps, such as `Date.from_gregorian_days/2` and `NaiveDateTime.to_gregorian_seconds/1`.
Another excellent feature in Erlang/OTP 24 is the implementation of [EEP 54](http://erlang.org/eeps/eep-0054.html), which provides extended error information for many functions in Erlang's stdlib. Elixir v1.12 fully leverages this feature to improve reporting for errors coming from Erlang. For example, in earlier OTP versions, inserting an invalid argument into a ETS table that no longer exists would simply error with `ArgumentError`:
Mix also includes two new tasks: `mix app.config`, for application runtime configuration, and `mix test.coverage`, which generates aggregated coverage reports for umbrella projects and for test suites partitioned across processes.
```
Interactive Elixir (1.11.0)
iex(1)> ets = :ets.new(:example, [])
#Reference<0.3845811859.2669281281.223553>
iex(2)> :ets.delete(ets)
true
iex(3)> :ets.insert(ets, :should_be_a_tuple)
** (ArgumentError) argument error
(stdlib 3.15) :ets.insert(#Reference<0.3845811859.2669281281.223553>, :should_be_a_tuple)
```
## v1.11.0-dev
However, in Elixir v1.12 with Erlang/OTP 24:
```
Interactive Elixir (1.12.0)
iex(1)> ets = :ets.new(:example, [])
#Reference<0.105641012.1058144260.76455>
iex(2)> :ets.delete(ets)
true
iex(3)> :ets.insert(ets, :should_be_a_tuple)
** (ArgumentError) errors were found at the given arguments:
* 1st argument: the table identifier does not refer to an existing ETS table
* 2nd argument: not a tuple
(stdlib 3.15) :ets.insert(#Reference<0.105641012.1058144260.76455>, :should_be_a_tuple)
```
Finally, note Rebar v2 no longer works on Erlang/OTP 24+. Mix defaults to Rebar v3 since v1.4, so no changes should be necessary by the huge majority of developers. However, if you are explicitly setting `manager: :rebar` in your dependency, you want to move to Rebar v3 by removing the `:manager` option. Support for unsupported Rebar versions will be removed from Mix in the future.
## Stepped ranges
Elixir has support for ranges from before its v1.0 release. Ranges support only integers and are inclusive, using the mathematic notation `a..b`. Ranges in Elixir are either increasing `1..10` or decreasing `10..1` and the direction of the range was always inferred from the first and last positions. Ranges are always lazy as its values are emitted as they are enumerated rather than being computed upfront.
Unfortunately, due to this inference, it is not possible to have empty ranges. For example, if you want to create a list of `n` elements, you cannot express it with a range from `1..n`, as `1..0` (for `n=0`) is a decreasing range with two elements.
Elixir v1.12 supports stepped ranges via the `first..last//step` notation. For example: `1..10//2` will emit the numbers `1`, `3`, `5`, `7`, and `9`. You can consider the `//` operator as an equivalent to "range division", as it effectively divides the number of elements in the range by `step`, rounding up on inexact scenarios. Steps can be either positive (increasing ranges) or negative (decreasing ranges). Stepped ranges bring more expressive power to Elixir ranges and they elegantly solve the empty range problem, as they allow the direction of the steps to be explicitly declared instead of inferred.
As of Elixir v1.12, implicitly decreasing ranges are soft-deprecated and warnings will be emitted in future Elixir versions based on our [deprecation policy](https://hexdocs.pm/elixir/compatibility-and-deprecations.html#deprecations).
## Additional functions
Elixir v1.12 has the additional of many functions across the standard library. The `Enum` module received additions such as `Enum.count_until/2`, `Enum.product/1`, `Enum.zip_with/2`, and more. The `Integer` module now includes `Integer.pow/2` and `Integer.extended_gcd/2`. Finally, the `Kernel` module got two new functions, `Kernel.then/2` and `Kernel.tap/2`, which are specially useful in `|>` pipelines.
## v1.12.3 (2021-09-05)
### 1. Bug fixes
#### Elixir
* [Code] Make sure that bindings in the default context returned by `Code.eval_*` functions are not returned as tagged tuples
* [Kernel] Do not crash when handling ambiguity errors
* [Range] Still match on old range patterns throughout the stdlib
#### IEx
* [IEx.Autocomplete] Do not error autocompletion with module attribute
#### Mix
* [Mix] Rename inconsistent `:exit_code` option to `:exit_status` on `Mix.raise/2`
## v1.12.2 (2021-07-01)
### 1. Bug fixes
#### Elixir
* [Kernel] Ensure deprecated macros emit warnings
#### Mix
* [mix deps] Ensure unconstrained rebar deps generate valid mix specifications
### 2. Enhancements
#### Elixir
* [elixirc] Change the output of `--profile time` to make it easier to detect outliers
* [Application] Do not add compile time deps on args to `Application.compile_env/2` and `Application.compile_env!/2`
* [Enum] Optimize `Enum.into/3` and `Map.new/2`
#### Mix
* [mix compile] Compile most recently changed files first
* [mix compile, mix run, mix test] Speed up the time taken to load dependencies. This should make the usage of Mix inside projects quite more responsive
## v1.12.1 (2021-05-28)
### 1. Bug fixes
#### Elixir
* [Code] Make sure `Code.format_string!/2` formats multiline expression inside interpolation on the first run
* [Macro] Revert keeping of underscores between digits in camelize
#### Mix
* [Mix] Make sure `Mix.install/2` expand paths for deps
* [mix deps.get] Silence false positives on `httpc` warnings
* [mix test] Do not run the whole suite when there are no --failed tests as it won't behave as expected inside umbrellas
## v1.12.0 (2021-05-19)
### 1. Enhancements
#### EEx
* [EEx] Track column information in EEx templates when enabled in the compiler
* [EEx] Show column information in EEx error messages
* [EEx] Support `:indentation` option when compiling EEx templates for proper column tracking
* [EEx.Engine] Add `c:EEx.Engine.handle_text/3` callback that receives text metadata
* [EEx.Engine] Emit warnings for unused "do" expression in EEx
#### Elixir
* [Access] Add `Access.at!/1`
* [Calendar] Add `Calendar.strftime/3` for datetime formatting
* [Calendar] Add linear integer representations to Calendar modules: `Date.from_gregorian_days/2`, `Date.to_gregorian_days/1`, `NaiveDateTime.from_gregorian_seconds/3`, `NaiveDateTime.to_gregorian_seconds/1`, `Time.from_seconds_after_midnight/1`, and `Time.to_seconds_after_midnight/1`
* [Calendar] Add `new!` to Date/Time/NaiveDateTime/DateTime (`new` has also been added to `DateTime` for completeness)
* [Calendar] Support custom starting day of the week in `Date.day_of_week/2`
* [Calendar] Add `Date.beginning_of_month/1` and `Date.end_of_month/1`
* [Calendar] Add `Date.beginning_of_week/2` and `Date.end_of_week/2`
* [Code] Add `:column` to `Code.string_to_quoted*/2`
* [Code] Add `Code.can_await_module_compilation?/0` to check if the parallel compiler is enabled and it can await for other modules to be compiled
* [Config] Support `config_env/0` and `config_target/0` in `config` files
* [Config] Allow `import_config` to be disabled for some configuration files
* [Enum] Allow a sorting function on `Enum.min_max_by/3,4`, including the new `compare/2` conventions
* [Kernel] Add `is_struct/2` guard
* [Kernel] Add `is_exception/1` and `is_exception/2` guards
* [Kernel] Support `map.field` syntax in guards
* [Kernel] Add `+++` and `---` with right associativity to the list of custom operators
* [Kernel] Warn if a variable that looks like a compiler variable (such as `__MODULE__`) is unused
* [Kernel.ParallelCompiler] Report individual file compilation times when `profile: :time` is given
* [Kernel.ParallelCompiler] Improve precision of `:long_compilation_threshold` so it takes only compilation times into account (and not waiting times)
* [Registry] Add `Registry.delete_meta/2`
* [Task] Add `Task.await_many/2`
* [Code] Add `Code.cursor_context/2` to return the context of a code snippet
* [Code] Do not add newlines around interpolation on code formatting. Note this means formatted code that has interpolation after the line length on Elixir v1.12 won't be considered as formatted on earlier Elixir versions
* [Code] Do not add brackets when keywords is used in the access syntax
* [Calendar] Support basic datetime format in `Calendar.ISO` parsing functions
* [Code] Improve evaluation performance on systems running on Erlang/OTP 24+
* [Date] Support steps via `Date.range/3`
* [DateTime] Add `offset` to `DateTime.to_iso8601/2` (now `to_iso8601/3`)
* [Enum] Add `Enum.count_until/2` and `Enum.count_until/3`
* [Enum] Add `Enum.product/1`
* [Enum] Add `Enum.zip_with/2`, `Enum.zip_with/3`, `Enum.zip_reduce/3`, and `Enum.zip_reduce/4`
* [Enum] Add support for functions as the second argument of `Enum.with_index/2`
* [Exception] Show `error_info` data for exceptions coming from Erlang
* [Float] Add `Float.pow/2`
* [Integer] Add `Integer.pow/2` and `Integer.extended_gcd/2`
* [IO] Add `IO.stream/0` and `IO.binstream/0` which default to STDIO with line orientation
* [List] Add default value for `List.first/1` and `List.last/1`
* [Kernel] Add `first..last//step` as support for stepped ranges
* [Kernel] Also warn for literal structs on `min/2` and `max/2`
* [Kernel] Add `Kernel.tap/2` and `Kernel.then/2`
* [Kernel] Do not add runtime dependencies to remotes in typespecs
* [Kernel] When there is an unused variable warning and there is a variable with the same name previously defined, suggest the user may have wanted to use the pin operator
* [Kernel] Improve error messages on invalid character right after a number
* [Kernel] Show removal and deprecated tips from Erlang/OTP
* [Macro] Add export dependencies on `Macro.struct!/2`
* [Macro] Support `:newline` to customize newlines escaping in `Macro.unescape_string/2`
* [Module] Raise on invalid `@dialyzer` attributes
* [Module] Add `Module.get_definition/2` and `Module.delete_definition/2`
* [Module] Allow `@on_load` to be a private function
* [Module] Validate `@dialyzer` related module attributes
* [Module] Add `Module.reserved_attributes/0` to list all reserved attributes by the language
* [Range] Add `Range.new/3` and `Range.size/1`
* [Regex] Add offset option to `Regex.scan/3` and `Regex.run/3`
* [Registry] Support `:compression` on `Registry` tables
* [Registry] Support `Registry.values/3` for reading values under a given key-pid pair
* [Stream] Add `Stream.zip_with/2` and `Stream.zip_with/3`
* [String] Add `:turkic` mode option to String case functions
* [String] Update to Unicode 13.0
* [System] Add `System.trap_signal/3` and `System.untrap_signal/2`
* [System] Add `System.shell/2` to invoke a command that is interpreted by the shell
* [Tuple] Add `Tuple.sum/1` and `Tuple.product/1`
* [URI] Support RFC3986 compliant encoding and decoding of queries via the `:rfc3986` option
#### ExUnit
* [ExUnit] Add support for coloring on Windows 10 consoles/shells
* [ExUnit] Add `ExUnit.fetch_test_supervisor/0`
* [ExUnit] Add `@tag :tmp_dir` support to ExUnit. The temporary directory is automatically created and pruned before each test
* [ExUnit] Add file and line to ExUnit's `--trace`
* [ExUnit.Assertion] Allow receive timeouts to be computed at runtime
* [ExUnit.Doctest] Allow users to add tags to doctests
* [ExUnit] Intercept SIGQUIT (via Ctrl+\\) and show a list of all aborted tests as well as intermediate test results
* [ExUnit] Interpolate module attributes in match assertions diffs
* [ExUnit] Print how much time is spent on `async` vs `sync` tests
* [ExUnit] Improve error messages for doctests
* [ExUnit] Compile doctests faster (often by two times)
* [ExUnit] Add `ExUnit.async_run/0` and `ExUnit.await_run/1`
#### IEx
* [IEx] Add support for coloring on Windows 10 consoles/shells
* [IEx.Helpers] Show docs from Erlang modules that have been compiled with the docs chunk
#### Logger
* [Logger] Add `notice`, `critical`, `alert`, and `emergency` log levels
* [Logger] Support structured logging by logging maps or keyword lists
* [Logger] Allow level to be set per module with `Logger.put_module_level/2`
* [Logger] Include `erl_level` in Logger's metadata
* [IEx] Make IEx' parser configurable to allow special commands
* [IEx] Show function signature when pressing tab after the opening parens of a function
* [IEx] If an IEx expression starts with a binary operator, such as `|>`, automatically pipe in the result of the last expression
#### Mix
* [mix] Add `MIX_BUILD_ROOT` to config `_build` dir
* [mix] Introduce `MIX_XDG` as a simpler mechanism to opt-in to the XDG specification
* [mix] Allow requirements for a Mix task to be listed via the `@requirements` module attribute
* [mix] Allow optional dependencies to be defined in `:extra_applications` and `:applications`
* [mix app.config] Add new `mix app.config` task that compiles applications and loads runtime configuration
* [mix archive.install] Support `--repo` option on hex packages
* [mix compile] Support the `__mix_recompile__?/0` callback for custom behaviour on when Mix should recompile a given module
* [mix compile.elixir] Mark modules for path dependencies as "Export dependencies" if they changed but their public interface is the same
* [mix compile.elixir] Track application boundaries in the Elixir compiler. If you invoke code from Erlang or Elixir standard libraries and you don't depend on the proper applications, a warning will be emitted. A warning will also be emitted if you invoke code from an umbrella sibling that you don't depend on - effectively forbidding cyclic dependencies between apps
* [mix deps] Sort the dependencies alphabetically before printing
* [mix deps] Use `origin/HEAD` as the default git ref in dependencies
* [mix deps] Redact Git `username`/`password` in output log
* [mix deps] Support rebar3's `git_subdir` resource type
* [mix deps.compile] Allow local deps to be skipped on `mix deps.compile`
* [mix deps.unlock] Print which dependencies get unlocked when using the `--unused` flag
* [mix escript.install] Support `--repo` option on hex packages
* [mix new] Add `@impl` to application generated by `mix new --sup`
* [mix release] Enable overriding `sys.config` location via `RELEASE_SYS_CONFIG` env var
* [mix release] Boot a release under configuration in interactive mode and then swap to embedded mode (if running on Erlang/OTP 23+)
* [mix release] Add `rel_templates_path` to configure the source of template files such as "env.sh.eex", "vm.args.eex" and "overlays"
* [mix release] Allow some chunks to be kept in the `:strip_beams` config
* [mix test] Allow `:ignore_modules` inside `:test_coverage` option
* [mix test.coverage] Add `mix test.coverage` that aggregates coverage results from umbrellas and OS partitioning
* [mix xref] Make the `--label` option for `mix xref graph` transitive by default and add `--only-direct` for only direct dependencies
* [mix xref] Add `--format cycles` support for `mix xref graph`
* [mix xref] Add support to `mix xref graph` for using `--source` and `--sink` at the same time
* [Mix] Add `Mix.install/2` for dynamically installing a list of dependencies
* [Mix] Support `:exit_code` option in `Mix.raise/2`
* [Mix] Discard `MIX_ENV` and `MIX_TARGET` values if they are empty strings
* [Mix] Print the time taken to execute a task with on `MIX_DEBUG=1`
* [mix compile.erlang] Compile multiple files in parallel
* [mix escript.build] Deep merge configuration and ensure argv is set when executing `config/runtime.exs`
* [mix release] Add `RELEASE_PROG` to releases with the name of the executable starting the release
* [mix release] Support `remote.vm.args` to customize how the connecting VM boots
* [mix test] Run all available tests if there are no pending `--failed` tests. This provides a better workflow as you no longer need to toggle the `--failed` flag between runs
* [mix test] Support `mix test --warnings-as-errors`, treating warnings as errors and returning a non-zero exit status (only applies to test files, not compilation)
### 2. Bug fixes
#### EEx
* [EEx] Make trimming behaviour via the `:trim` option more consistent
#### Elixir
* [Application] Warn if non-atom keys are given to `put_env`, `get_env`, `fetch_env`, and `delete_env`
* [Code] Do not send language keyword through the `:static_atoms_encoder` in `Code.string_to_quoted`
* [Kernel] Validate values given to `:line` in quote to avoid emitting invalid ASTs
* [Kernel] Report the correct line number when raising inside a macro
* [Kernel] Fix an issue where `elixirc` would not accept paths with backslash (`\`) separators on Windows
* [Kernel] Properly parse `&//2` (i.e. the capture of the division operator)
* [Kernel] Raise `CompileError` when trying to define reserved types
* [Kernel] Improve compiler error message when using `|` in a `def` signature
* [Kernel.SpecialForms] Add `|/2` to the list of special forms to avoid inconsistent behaviour on overrides
* [Keyword] Enforce keys to be atoms in `Keyword.keys/1`
* [URI] `URI.decode_query/2` emits an empty string for parameters without values, according to https://url.spec.whatwg.org/#application/x-www-form-urlencoded
* [Version] Add defaults and enforce keys in `Version` struct
* [CLI] Ensure `-e ""` (with an empty string) parses correctly on Windows
* [Inspect] Do not override user supplied `:limit` option for derived implementations
* [Kernel] Allow heredoc inside a heredoc interpolation
* [Kernel] Preserve CRLF on heredocs
* [Kernel] Public functions without documentation now appear as an empty map on `Code.fetch_docs/1`, unless they start with underscore, where they remain as `:none`. This aligns Elixir's implementation with EEP48
* [Kernel] Do not crash when complex literals (binaries and maps) are used in guards
* [Kernel] Properly parse keywords (such as `end`) followed by the `::` operator
* [Kernel] Do not ignore unimplemented signatures from generated functions
* [Kernel] Improve error message when an expression follows a keyword list without brackets
* [Macro] `Macro.decompose_call/1` now also consider tuples with more than 2 elements to not be valid calls
* [Macro] Fix `Macro.to_string/1` double-escaping of escape characters in sigils
* [Macro] Fix `Macro.underscore/1` on digits preceded by capitals: "FOO10" now becomes "foo10" instead of "fo_o10"
* [Macro] Preserve underscores between digits on `Macro.underscore/1`
* [OptionParser] Properly parse when numbers follow-up aliases, for example, `-ab3` is now parsed as `-a -b 3`
* [Path] Fix `Path.relative_to/2` when referencing self
* [Path] Do not crash when a volume is given to `Path.absname/1`, such as "c:"
* [Task] Ensure `Task.async_stream/2` with `ordered: false` discard results as they are emitted, instead of needlessly accumulating inside the stream manager
* [Task] Raise if `:max_concurrency` is set to 0 on streaming operations
* [URI] Do not discard empty paths on `URI.merge/2`
#### ExUnit
* [ExUnit.CaptureIO] Fix race condition where a dead capture would still be considered as active
* [ExUnit.Diff] Do not crash when failing to eval/inspect struct
* [ExUnit.Diff] Properly diff numbers in respect to `==` and `===` operators
* [ExUnit.Case] Make `@tag tmp_dir` an absolute directory, avoiding inconsistencies if the test changes the current working directory
* [ExUnit.Diff] Fix cases where the diffing algorithm would fail to print a pattern correct
#### IEx
* [IEx] Fix tokenizer emitting repeated warnings in the REPL
* [IEx] Ensure `dot_iex_path` is preserved when restarting the evaluator
* [IEx.Pry] Ensure `IEx.pry` can be triggered more than twice when invoked from the same process
* [IEx] Fix auto-completion inside remote shells
#### Mix
* [mix cmd] Fix a bug where only the first --app option would be executed
* [mix compile] Fix an issue where new protocol implementations would not propagate when running `mix compile` from an umbrella root
* [mix deps.compile] Use `gmake` instead of `make` when compiling deps on NetBSD/DragonFlyBSD
* [mix release] Load `.app` from dependencies path when it is a project dependency
* [mix release] Always include "rel/overlays" in the list of overlays directories if available
* [mix release] Change `erts/bin/erl` binary mode to `0o755`
* [mix test] Compare to test coverage threshold inclusively
#### Logger
* [Logger] Print metadata for all types that implement String.Chars
* [mix app.config] Do not emit false positive warnings when configured dependencies that have `runtime: false` set
* [mix compile.elixir] Ensure that a manifest is generated even with no source code
* [mix compile.elixir] Make sure export dependencies trigger recompilation when the dependency is removed as well as when the whole file is removed
* [mix compile.elixir] Do not emit false positive warnings when a path dependency adds a module that is then used by the current application in the same `mix compile` cycle
* [mix test] Ensure protocols within the current project are consolidated when `--cover` is given
* [mix release] Improve compliance of release scripts with stripped down Linux installations
* [mix release] Preserve file mode when copying non-beam ebin files
* [mix xref] Ensure args are passed to the underlying `mix compile` call
### 3. Soft-deprecations (no warnings emitted)
### Elixir
#### Elixir
* [Exception] `Exception.exception?/1` is deprecated in favor of `Kernel.is_exception/1`
* [Regex] `Regex.regex?/1` is deprecated in favor of `Kernel.is_struct/2`
### Logger
* [Logger] `warn` log level is deprecated in favor of `warning`
### Mix
* [mix release] `config/releases.exs` is deprecated in favor of a more general purpose `config/runtime.exs`
* [Kernel] Using `first..last` to match on ranges is soft-deprecated and will warn on future Elixir versions. Use `first..last//step` instead
* [Kernel] Using `first..last` to create decreasing ranges is soft-deprecated and will warn on future versions. Use `first..last//-1` instead
### 4. Hard-deprecations
#### EEx
* [EEx.Engine] `use EEx.Engine` is deprecated in favor of explicit delegation
#### Elixir
* [Supervisor] Deprecate `Supervisor.start_child/2` and `Supervisor.terminate_child/2` in favor of `DynamicSupervisor`
* [Supervisor.Spec] Deprecate `Supervisor.Spec.worker/3` and `Supervisor.Spec.supervisor/3` in favor of the new typespecs
* [System] Deprecate `System.stracktrace/0` in favor of `__STACKTRACE__`
* [Kernel] The binary operator `^^^` is deprecated. If you are using `Bitwise.^^^/2`, use `Bitwise.bxor/2` instead
* [Kernel] Deprecate `@foo()` in favor of `@foo`
* [System] Deprecate `System.stacktrace/0` (it was already deprecated outside of catch/rescue and now it is deprecated everywhere)
#### Mix
* [Mix.Project] Deprecate `Mix.Project.compile/2` in favor of `Mix.Task.run("compile", args)`
* [mix compile] The `:xref` compiler is deprecated and it has no effect. Please remove it from your mix.exs file.
## v1.10
### 5. Backwards incompatible changes
The CHANGELOG for v1.10 releases can be found [in the v1.10 branch](https://github.com/elixir-lang/elixir/blob/v1.10/CHANGELOG.md).
A backward incompatible change was found on Elixir v1.12.0 after the release. This was not planned and it is documented below:
#### ExUnit
* [ExUnit.Formatter] `{:suite_finished, load_time, run_time}` message has been updated to `{:suite_finished, %{load: load_time, run: run_time, async: async_time}}`
## v1.11
The CHANGELOG for v1.11 releases can be found [in the v1.11 branch](https://github.com/elixir-lang/elixir/blob/v1.11/CHANGELOG.md).
+29 -21
View File
@@ -2,7 +2,7 @@ PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
#CANONICAL := vMAJOR.MINOR/
CANONICAL := 1.12/
CANONICAL ?= master/
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
@@ -28,9 +28,9 @@ SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
#==> Functions
define CHECK_ERLANG_RELEASE
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 21)])' -s erlang halt | grep -q '^true'; \
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 22)])' -s erlang halt | grep -q '^true'; \
if [ $$? != 0 ]; then \
echo "At least Erlang/OTP 21.0 is required to build Elixir"; \
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -83,7 +83,7 @@ $(PARSER): lib/elixir/src/elixir_parser.yrl
# Since Mix depends on EEx and EEx depends on Mix,
# we first compile EEx without the .app file,
# then Mix and then compile EEx fully
# then Mix, and then compile EEx fully
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
stdlib: $(KERNEL) VERSION
@@ -179,40 +179,48 @@ clean_residual_files:
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}")
DOCS_FORMAT = html
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
DOCS_COMPILE = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
DOCS_CONFIG = bin/elixir lib/elixir/scripts/docs_config.exs "$(1)"
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
docs_elixir: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (elixir)"
$(Q) rm -rf doc/elixir
$(call COMPILE_DOCS,Elixir,elixir,Kernel,--config "lib/elixir/docs.exs")
$(call DOCS_COMPILE,Elixir,elixir,Kernel,--config "lib/elixir/scripts/elixir_docs.exs")
$(call DOCS_CONFIG,elixir)
docs_eex: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (eex)"
$(Q) rm -rf doc/eex
$(call COMPILE_DOCS,EEx,eex,EEx)
$(call DOCS_COMPILE,EEx,eex,EEx,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,eex)
docs_mix: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (mix)"
$(Q) rm -rf doc/mix
$(call COMPILE_DOCS,Mix,mix,Mix)
$(call DOCS_COMPILE,Mix,mix,Mix,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,mix)
docs_iex: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (iex)"
$(Q) rm -rf doc/iex
$(call COMPILE_DOCS,IEx,iex,IEx)
$(call DOCS_COMPILE,IEx,iex,IEx,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,iex)
docs_ex_unit: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (ex_unit)"
$(Q) rm -rf doc/ex_unit
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit)
$(call DOCS_COMPILE,ExUnit,ex_unit,ExUnit,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,ex_unit)
docs_logger: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (logger)"
$(Q) rm -rf doc/logger
$(call COMPILE_DOCS,Logger,logger,Logger)
$(call DOCS_COMPILE,Logger,logger,Logger,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,logger)
../ex_doc/bin/ex_doc:
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
@@ -221,23 +229,23 @@ docs_logger: compile ../ex_doc/bin/ex_doc
#==> Zip tasks
Docs.zip: docs
rm -f Docs-v$(VERSION).zip
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
rm -f Docs.zip
zip -9 -r Docs.zip CHANGELOG.md doc NOTICE LICENSE README.md
@ echo "Docs file created $(CURDIR)/Docs.zip"
Precompiled.zip: build_man compile
rm -f Precompiled-v$(VERSION).zip
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
rm -f Precompiled.zip
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
@ echo "Precompiled file created $(CURDIR)/Precompiled.zip"
zips: Precompiled.zip Docs.zip
@ echo ""
@ echo "### Checksums"
@ echo ""
@ shasum -a 1 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
@ shasum -a 512 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
@ shasum -a 1 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
@ shasum -a 512 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
@ shasum -a 1 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
@ shasum -a 512 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
@ shasum -a 1 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
@ shasum -a 512 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
@ echo ""
#==> Test tasks
+7 -7
View File
@@ -1,4 +1,4 @@
# ![Elixir](https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png)
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=master)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=master)](https://cirrus-ci.com/github/elixir-lang/elixir)
@@ -29,7 +29,7 @@ For the many different ways to install Elixir,
[see our installation instructions on the website](https://elixir-lang.org/install.html).
To compile from source, you can follow the steps below.
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then clone this repository to your machine, compile and test it:
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). After that, clone this repository to your machine, compile and test it:
```sh
git clone https://github.com/elixir-lang/elixir.git
@@ -48,10 +48,10 @@ If Elixir fails to build (specifically when pulling in a new version via
If tests pass, you can use Interactive Elixir by running `bin/iex` in your terminal.
However, if tests fail, it is likely that you have an outdated Erlang/OTP version
(Elixir requires Erlang/OTP 21.0 or later). You can check your Erlang/OTP version
(Elixir requires Erlang/OTP 22.0 or later). You can check your Erlang/OTP version
by calling `erl` in the command line. You will see some information similar to:
Erlang/OTP 21 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
Erlang/OTP 22 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
If you have properly set up your dependencies and tests still fail,
you may want to open up a bug report, as explained next.
@@ -131,9 +131,9 @@ With tests running and passing, you are ready to contribute to Elixir and
We have saved some excellent pull requests we have received in the past in
case you are looking for some examples:
* [Implement Enum.member? - Pull Request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
* [Implement Enum.member? - Pull request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull request](https://github.com/elixir-lang/elixir/pull/1059)
### Reviewing changes
+3 -3
View File
@@ -6,12 +6,12 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
| Elixir version | Support
| -------------- | ------------------------------
| 1.11 | Development
| 1.10 | Bug fixes and security patches
| 1.12 | Bug fixes and security patches
| 1.11 | Security patches only
| 1.10 | Security patches only
| 1.9 | Security patches only
| 1.8 | Security patches only
| 1.7 | Security patches only
| 1.6 | Security patches only
## Announcements
+1 -1
View File
@@ -1 +1 @@
1.11.0-dev
1.12.3
+14 -14
View File
@@ -70,7 +70,7 @@ readlink_f () {
ERL=""
# Stores erl arguments preserving spaces/quotes (mimics an array)
erl () {
erl_set () {
eval "E${E}=\$1"
E=$((E + 1))
}
@@ -137,34 +137,34 @@ while [ $I -le $LENGTH ]; do
;;
--cookie)
S=2
erl "-setcookie"
erl "$2"
erl_set "-setcookie"
erl_set "$2"
;;
--sname|--name)
S=2
erl "$(echo "$1" | cut -c 2-)"
erl "$2"
erl_set "$(echo "$1" | cut -c 2-)"
erl_set "$2"
;;
--erl-config)
S=2
erl "-config"
erl "$2"
erl_set "-config"
erl_set "$2"
;;
--vm-args)
S=2
erl "-args_file"
erl "$2"
erl_set "-args_file"
erl_set "$2"
;;
--boot)
S=2
erl "-boot"
erl "$2"
erl_set "-boot"
erl_set "$2"
;;
--boot-var)
S=3
erl "-boot_var"
erl "$2"
erl "$3"
erl_set "-boot_var"
erl_set "$2"
erl_set "$3"
;;
--pipe-to)
S=3
+3
View File
@@ -99,18 +99,21 @@ if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixi
rem ******* EVAL PARAMETERS ************************
if ""==!par:-e=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--eval=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--rpc-eval=! (
set "VAR=%~2"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
shift
shift
+2 -1
View File
@@ -12,8 +12,9 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
--no-docs Does not attach documentation to compiled modules
--profile time Profile the time to compile modules
--verbose Prints compilation status
--warnings-as-errors Treats warnings as errors and return non-zero exit code
--warnings-as-errors Treats warnings as errors and return non-zero exit status
Options given after -- are passed down to the executed code.
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS.
+4 -3
View File
@@ -14,15 +14,16 @@ goto run
:documentation
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
echo.
echo -h, --help Prints this message and exits
echo -o The directory to output compiled files
echo -v, --version Prints Elixir version and exits
echo.
echo --help, -h Prints this message and exits
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
echo --no-debug-info Does not attach debug info to compiled modules
echo --no-docs Does not attach documentation to compiled modules
echo --profile time Profile the time to compile modules
echo --verbose Prints compilation status
echo --version, -v Prints Elixir version and exits
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit code
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
echo.
echo ** Options given after -- are passed down to the executed code
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
+72 -25
View File
@@ -17,11 +17,11 @@ defmodule EEx do
## API
This module provides 3 main APIs for you to use:
This module provides three main APIs for you to use:
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
directly. This is the simplest API to use but also the
slowest, since the code is evaluated and not compiled before.
slowest, since the code is evaluated at runtime and not precompiled.
2. Define a function from a string (`function_from_string/5`)
or a file (`function_from_file/5`). This allows you to embed
@@ -40,13 +40,14 @@ defmodule EEx do
They are:
* `:file` - the file to be used in the template. Defaults to the given
file the template is read from or to "nofile" when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to 1.
file the template is read from or to `"nofile"` when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to `1`.
* `:indentation` - (since v1.11.0) an integer added to the column after every
new line. Defaults to 0.
new line. Defaults to `0`.
* `:engine` - the EEx engine to be used for compilation.
* `:trim` - if true, trims whitespace left/right of quotation tags up until
newlines. At least one newline is retained. Defaults to false.
* `:trim` - if `true`, trims whitespace left and right of quotation as
long as at least one newline is present. All subsequent newlines and
spaces are removed but one newline is retained. Defaults to `false`.
## Engine
@@ -105,10 +106,13 @@ defmodule EEx do
"""
@doc """
Generates a function definition from the string.
Generates a function definition from the given string.
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`template` is the string containing the EEx template. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
## Examples
@@ -120,11 +124,11 @@ defmodule EEx do
"3"
"""
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge([file: __ENV__.file, line: __ENV__.line], options)
args = Enum.map(args, fn arg -> {arg, [line: info[:line]], nil} end)
compiled = EEx.compile_string(source, info)
compiled = EEx.compile_string(template, info)
case kind do
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
@@ -136,8 +140,11 @@ defmodule EEx do
@doc """
Generates a function definition from the file contents.
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`file` is the path to the EEx template file. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
This function is useful in case you have templates but
you want to precompile inside a module for speed.
@@ -160,7 +167,7 @@ defmodule EEx do
"""
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge(options, file: file, line: 1)
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
args = Enum.map(args, fn arg -> {arg, [line: 1], nil} end)
compiled = EEx.compile_file(file, info)
@@ -174,8 +181,25 @@ defmodule EEx do
end
@doc """
Gets a string `source` and generate a quoted expression
Gets a string `source` and generates a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
## Examples
iex> quoted = EEx.compile_string("<%= a + b %>")
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
iex> result
"3"
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
@@ -183,12 +207,34 @@ defmodule EEx do
end
@doc """
Gets a `filename` and generate a quoted expression
Gets a `filename` and generates a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
## Examples
# sample.eex
<%= a + b %>
# In code:
quoted = EEx.compile_file("sample.eex")
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
result
#=> "3"
"""
@spec compile_file(String.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
options = Keyword.merge(options, file: filename, line: 1)
@spec compile_file(Path.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.merge([file: filename, line: 1], options)
compile_string(File.read!(filename), options)
end
@@ -201,7 +247,7 @@ defmodule EEx do
"foo baz"
"""
@spec eval_string(String.t(), keyword, keyword) :: any
@spec eval_string(String.t(), keyword, keyword) :: String.t()
def eval_string(source, bindings \\ [], options \\ [])
when is_binary(source) and is_list(bindings) and is_list(options) do
compiled = compile_string(source, options)
@@ -216,15 +262,16 @@ defmodule EEx do
# sample.eex
foo <%= bar %>
# iex
# IEx
EEx.eval_file("sample.eex", bar: "baz")
#=> "foo baz"
"""
@spec eval_file(String.t(), keyword, keyword) :: any
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
def eval_file(filename, bindings \\ [], options \\ [])
when is_binary(filename) and is_list(bindings) and is_list(options) do
options = Keyword.put(options, :file, filename)
when is_list(bindings) and is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.put_new(options, :file, filename)
compiled = compile_file(filename, options)
do_eval(compiled, bindings, options)
end
+18 -3
View File
@@ -41,8 +41,16 @@ defmodule EEx.Compiler do
# Generates the buffers by handling each expression from the tokenizer.
# It returns Macro.t/0 or it raises.
defp generate_buffer([{:text, chars} | rest], buffer, scope, state) do
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
buffer =
if function_exported?(state.engine, :handle_text, 3) do
meta = [line: line, column: column]
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
else
# TODO: Remove this branch on Elixir v2.0
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
end
generate_buffer(rest, buffer, scope, state)
end
@@ -59,6 +67,13 @@ defmodule EEx.Compiler do
scope,
state
) do
if mark != '=' do
message =
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
:elixir_errors.erl_warn(start_line, state.file, message)
end
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
{contents, rest} =
@@ -179,7 +194,7 @@ defmodule EEx.Compiler do
# Look middle expressions that immediately follow a start_expr
defp look_ahead_middle(
[{:text, text}, {:middle_expr, line, _column, _, chars} | rest] = tokens,
[{:text, _, _, text}, {:middle_expr, line, _, _, chars} | rest] = tokens,
start,
contents
) do
+15 -13
View File
@@ -4,10 +4,8 @@ defmodule EEx.Engine do
An engine needs to implement all callbacks below.
An engine may also `use EEx.Engine` to get the default behaviour
but this is not advised. In such cases, if any of the callbacks
are overridden, they must call `super()` to delegate to the
underlying `EEx.Engine`.
This module also ships with a default engine implementation
you can delegate to. See `EEx.SmartEngine` as an example.
"""
@type state :: term
@@ -31,7 +29,8 @@ defmodule EEx.Engine do
It must return the updated state.
"""
@callback handle_text(state, text :: String.t()) :: state
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
state
@doc """
Called for the dynamic/code parts of a template.
@@ -69,6 +68,7 @@ defmodule EEx.Engine do
@callback handle_end(state) :: Macro.t()
@doc false
@deprecated "Use explicit delegation to EEx.Engine instead"
defmacro __using__(_) do
quote do
@behaviour EEx.Engine
@@ -90,7 +90,7 @@ defmodule EEx.Engine do
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, text)
EEx.Engine.handle_text(state, [], text)
end
def handle_expr(state, marker, expr) do
@@ -147,7 +147,7 @@ defmodule EEx.Engine do
end
end
@doc false
@doc "Default implementation for `c:init/1`."
def init(_opts) do
%{
binary: [],
@@ -156,18 +156,18 @@ defmodule EEx.Engine do
}
end
@doc false
@doc "Default implementation for `c:handle_begin/1`."
def handle_begin(state) do
check_state!(state)
%{state | binary: [], dynamic: []}
end
@doc false
@doc "Default implementation for `c:handle_end/1`."
def handle_end(quoted) do
handle_body(quoted)
end
@doc false
@doc "Default implementation for `c:handle_body/1`."
def handle_body(state) do
check_state!(state)
%{binary: binary, dynamic: dynamic} = state
@@ -176,14 +176,16 @@ defmodule EEx.Engine do
{:__block__, [], Enum.reverse(dynamic)}
end
@doc false
def handle_text(state, text) do
@doc "Default implementation for `c:handle_text/3`."
def handle_text(state, _meta, text) do
check_state!(state)
%{binary: binary} = state
%{state | binary: [text | binary]}
end
@doc false
@doc "Default implementation for `c:handle_expr/3`."
def handle_expr(state, "=", ast) do
check_state!(state)
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
+19 -3
View File
@@ -33,10 +33,26 @@ defmodule EEx.SmartEngine do
"""
use EEx.Engine
@behaviour EEx.Engine
def handle_expr(buffer, mark, expr) do
@impl true
defdelegate init(opts), to: EEx.Engine
@impl true
defdelegate handle_body(state), to: EEx.Engine
@impl true
defdelegate handle_begin(state), to: EEx.Engine
@impl true
defdelegate handle_end(state), to: EEx.Engine
@impl true
defdelegate handle_text(state, meta, text), to: EEx.Engine
@impl true
def handle_expr(state, marker, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
super(buffer, mark, expr)
EEx.Engine.handle_expr(state, marker, expr)
end
end
+64 -38
View File
@@ -6,7 +6,7 @@ defmodule EEx.Tokenizer do
@type column :: non_neg_integer
@type marker :: '=' | '/' | '|' | ''
@type token ::
{:text, content}
{:text, line, column, content}
| {:expr | :start_expr | :middle_expr | :end_expr, line, column, marker, content}
| {:eof, line, column}
@@ -17,7 +17,7 @@ defmodule EEx.Tokenizer do
It returns {:ok, list} with the following tokens:
* `{:text, content}`
* `{:text, line, column, content}`
* `{:expr, line, column, marker, content}`
* `{:start_expr, line, column, marker, content}`
* `{:middle_expr, line, column, marker, content}`
@@ -36,8 +36,11 @@ defmodule EEx.Tokenizer do
def tokenize(list, line, column, opts)
when is_list(list) and is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
column = opts.indentation + column
{list, line, column} = (opts.trim && trim_init(list, line, column)) || {list, line, column}
tokenize(list, line, column, opts, [], [])
{list, line, column} =
(opts.trim && trim_init(list, line, column, opts)) || {list, line, column}
tokenize(list, line, column, opts, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, opts, buffer, acc) do
@@ -53,7 +56,8 @@ defmodule EEx.Tokenizer do
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
tokenize(rest, new_line, new_column, opts, buffer, acc)
acc = tokenize_text(buffer, acc)
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], acc)
end
end
@@ -65,10 +69,10 @@ defmodule EEx.Tokenizer do
error
{:ok, expr, new_line, new_column, rest} ->
key =
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, tokens} -> token_key(tokens)
{:error, _, _, _} -> :expr
{:ok, tokens} -> token_key(tokens, expr)
{:error, _, _, _} -> {:expr, expr}
end
{rest, new_line, new_column, buffer} =
@@ -76,7 +80,7 @@ defmodule EEx.Tokenizer do
acc = tokenize_text(buffer, acc)
final = {key, line, column, marker, expr}
tokenize(rest, new_line, new_column, opts, [], [final | acc])
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], [final | acc])
end
end
@@ -122,87 +126,109 @@ defmodule EEx.Tokenizer do
end
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens) do
defp token_key(tokens, expr) do
case {tokens, Enum.reverse(tokens)} do
{[{:end, _} | _], [{:do, _} | _]} ->
:middle_expr
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
:start_expr
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
:middle_expr
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
:middle_expr
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
:start_expr
{:start_expr, expr}
else
:middle_expr
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> :end_expr
_ -> :expr
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
end
end
defp maybe_append_space([?\s]), do: [?\s]
defp maybe_append_space([h]), do: [h, ?\s]
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([], acc) do
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{:text, Enum.reverse(buffer)} | acc]
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, line, column, buffer} | acc]
end
defp trim_if_needed(rest, line, column, opts, buffer) do
if opts.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0)
{rest, line, column} = trim_right(rest, line, column, 0, opts)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column) when h in @spaces, do: trim_init(t, line, column + 1)
defp trim_init([?\r, ?\n | t], line, _column), do: trim_init(t, line + 1, 1)
defp trim_init([?\n | t], line, _column), do: trim_init(t, line + 1, 1)
defp trim_init([?<, ?% | _] = rest, line, column), do: {rest, line, column}
defp trim_init(_, _, _), do: false
defp trim_init([h | t], line, column, opts) when h in @spaces,
do: trim_init(t, line, column + 1, opts)
defp trim_init([?\r, ?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?<, ?% | _] = rest, line, column, _opts),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer) do
[?\n, ?\r | rest] -> trim_left(rest, count + 1)
[?\n | rest] -> trim_left(rest, count + 1)
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, count) do
case trim_whitespace(rest) do
[?\r, ?\n | rest] -> trim_right(rest, line + 1, 1, count + 1)
[?\n | rest] -> trim_right(rest, line + 1, 1, count + 1)
[] -> {[], line, column + length(rest)}
_ when count > 0 -> {[?\n | rest], line - 1, column}
_ -> {rest, line, column}
defp trim_right(rest, line, column, last_column, opts) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column + 1, opts)
{[?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column, opts)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t]) when h in @spaces, do: trim_whitespace(t)
defp trim_whitespace(list), do: list
defp trim_whitespace([h | t], column) when h in @spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
end
+9
View File
@@ -43,6 +43,15 @@ defmodule EEx.SmartEngineTest do
assert_received :found
end
test "error with unused \"do\" block without \"<%=\" modifier" do
stderr =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
end)
assert stderr =~ "the contents of this expression won't be output"
end
defp assert_eval(expected, actual, binding \\ []) do
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.SmartEngine)
assert result == expected
+40 -39
View File
@@ -7,36 +7,36 @@ defmodule EEx.TokenizerTest do
@opts %{indentation: 0, trim: false}
test "simple chars lists" do
assert T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 'foo'}, {:eof, 1, 4}]}
assert T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "simple strings" do
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 'foo'}, {:eof, 1, 4}]}
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "strings with embedded code" do
assert T.tokenize('foo <% bar %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
end
test "strings with embedded equals code" do
assert T.tokenize('foo <%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded slash code" do
assert T.tokenize('foo <%/ bar %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded pipe code" do
assert T.tokenize('foo <%| bar %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
end
test "strings with more than one line" do
assert T.tokenize('foo\n<%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
{:ok, [{:text, 1, 1, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
end
test "strings with more than one line and expression with more than one line" do
@@ -48,11 +48,11 @@ defmodule EEx.TokenizerTest do
'''
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:expr, 1, 5, '=', ' bar\n\nbaz '},
{:text, '\n'},
{:text, 3, 7, '\n'},
{:expr, 4, 1, '', ' foo '},
{:text, '\n'},
{:text, 4, 10, '\n'},
{:eof, 5, 1}
]
@@ -61,21 +61,21 @@ defmodule EEx.TokenizerTest do
test "quotation" do
assert T.tokenize('foo <%% true %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo <% true %>'}, {:eof, 1, 16}]}
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
end
test "quotation with do/end" do
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1, 1, @opts) ==
{:ok, [{:text, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
{:ok, [{:text, 1, 1, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
end
test "quotation with interpolation" do
exprs = [
{:text, 'a <% b '},
{:text, 1, 1, 'a <% b '},
{:expr, 1, 9, '=', ' c '},
{:text, ' '},
{:text, 1, 17, ' '},
{:expr, 1, 18, '=', ' d '},
{:text, ' e %> f'},
{:text, 1, 26, ' e %> f'},
{:eof, 1, 33}
]
@@ -84,7 +84,7 @@ defmodule EEx.TokenizerTest do
test "improperly formatted quotation with interpolation" do
exprs = [
{:text, '<%% a <%= b %> c %>'},
{:text, 1, 1, '<%% a <%= b %> c %>'},
{:eof, 1, 22}
]
@@ -93,7 +93,7 @@ defmodule EEx.TokenizerTest do
test "eex comments" do
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:eof, 1, 16}
]
@@ -102,7 +102,8 @@ defmodule EEx.TokenizerTest do
test "eex comments with do/end" do
exprs = [
{:text, 'foo bar'},
{:text, 1, 1, 'foo '},
{:text, 1, 19, 'bar'},
{:eof, 1, 32}
]
@@ -111,7 +112,7 @@ defmodule EEx.TokenizerTest do
test "elixir comments" do
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
{:eof, 1, 35}
]
@@ -122,7 +123,7 @@ defmodule EEx.TokenizerTest do
test "elixir comments with do/end" do
exprs = [
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 'text'},
{:text, 1, 27, 'text'},
{:end_expr, 1, 31, [], ' end # closeif '},
{:eof, 1, 50}
]
@@ -133,9 +134,9 @@ defmodule EEx.TokenizerTest do
test "strings with embedded do end" do
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 'bar'},
{:text, 1, 21, 'bar'},
{:end_expr, 1, 24, '', ' end '},
{:eof, 1, 33}
]
@@ -145,12 +146,12 @@ defmodule EEx.TokenizerTest do
test "strings with embedded -> end" do
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' cond do '},
{:middle_expr, 1, 18, '', ' false -> '},
{:text, 'bar'},
{:text, 1, 32, 'bar'},
{:middle_expr, 1, 35, '', ' true -> '},
{:text, 'baz'},
{:text, 1, 48, 'baz'},
{:end_expr, 1, 51, '', ' end '},
{:eof, 1, 60}
]
@@ -162,9 +163,9 @@ defmodule EEx.TokenizerTest do
test "strings with multiple callbacks" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 'foo'},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end, fn -> '},
{:text, 'bar'},
{:text, 1, 34, 'bar'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
@@ -176,9 +177,9 @@ defmodule EEx.TokenizerTest do
test "strings with callback followed by do block" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 'foo'},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end do '},
{:text, 'bar'},
{:text, 1, 30, 'bar'},
{:end_expr, 1, 33, '', ' end '},
{:eof, 1, 42}
]
@@ -188,11 +189,11 @@ defmodule EEx.TokenizerTest do
test "strings with embedded keywords blocks" do
exprs = [
{:text, 'foo '},
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 'bar'},
{:text, 1, 21, 'bar'},
{:middle_expr, 1, 24, '', ' else '},
{:text, 'baz'},
{:text, 1, 34, 'baz'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
@@ -206,9 +207,9 @@ defmodule EEx.TokenizerTest do
exprs = [
{:start_expr, 1, 2, '=', ' if true do '},
{:text, '\n TRUE \n'},
{:text, 1, 20, '\n TRUE \n'},
{:middle_expr, 3, 3, '', ' else '},
{:text, '\n FALSE \n'},
{:text, 3, 13, '\n FALSE \n'},
{:end_expr, 5, 3, '', ' end '},
{:eof, 7, 3}
]
@@ -218,7 +219,7 @@ defmodule EEx.TokenizerTest do
test "trim mode with comment" do
exprs = [
{:text, '\n123'},
{:text, 1, 19, '\n123'},
{:eof, 2, 4}
]
@@ -227,9 +228,9 @@ defmodule EEx.TokenizerTest do
test "trim mode with CRLF" do
exprs = [
{:text, '0\n'},
{:text, 1, 1, '0\n'},
{:expr, 2, 3, '=', ' 12 '},
{:text, '\n34'},
{:text, 2, 15, '\n34'},
{:eof, 3, 3}
]
@@ -238,9 +239,9 @@ defmodule EEx.TokenizerTest do
test "trim mode set to false" do
exprs = [
{:text, ' '},
{:text, 1, 1, ' '},
{:expr, 1, 2, '=', ' 12 '},
{:text, ' \n'},
{:text, 1, 11, ' \n'},
{:eof, 2, 1}
]
+45 -24
View File
@@ -87,7 +87,11 @@ defmodule EExTest do
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n <%= 456 %> \n <%= 789 %>"
string = "<%= 123 %> \n\n456\n\n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n \n456\n \n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
@@ -127,6 +131,17 @@ defmodule EExTest do
end
test "trim mode with no spaces" do
string = """
<%=if true do%>
this
<%else%>
that
<%end%>
"""
expected = "\n this\n"
assert_eval(expected, string, [], trim: true)
string = """
<%=cond do%>
<%false ->%>
@@ -245,7 +260,7 @@ defmodule EExTest do
assert_raise EEx.SyntaxError,
"nofile:1:18: unexpected middle of expression <% else %>",
fn ->
EEx.compile_string("<% if true %> foo<% else %>bar<% end %>")
EEx.compile_string("<%= if true %>foo<% else %>bar<% end %>")
end
end
@@ -256,16 +271,16 @@ defmodule EExTest do
end
test "when start expression is found without an end expression" do
msg = "nofile:2:17: unexpected end of string, expected a closing '<% end %>'"
msg = "nofile:2:18: unexpected end of string, expected a closing '<% end %>'"
assert_raise EEx.SyntaxError, msg, fn ->
EEx.compile_string("foo\n<% if true do %>")
EEx.compile_string("foo\n<%= if true do %>")
end
end
test "when nested end expression is found without a start expression" do
assert_raise EEx.SyntaxError, "nofile:1:30: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <% if true do %><% end %><% end %>")
assert_raise EEx.SyntaxError, "nofile:1:31: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <%= if true do %><% end %><% end %>")
end
end
@@ -513,7 +528,7 @@ defmodule EExTest do
<% "a" in y -> %>
Good
<% true -> %>
<% if true do %>true<% else %>false<% end %>
<%= if true do %>true<% else %>false<% end %>
Bad
<% end %>
"""
@@ -558,18 +573,6 @@ defmodule EExTest do
end
describe "buffers" do
test "unused buffers are kept out" do
string = """
<%= 123 %>
<% if true do %>
<%= 456 %>
<% end %>
<%= 789 %>
"""
assert_eval("123\n\n789\n", string)
end
test "inside comprehensions" do
string = """
<%= for _name <- packages || [] do %>
@@ -607,6 +610,24 @@ defmodule EExTest do
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
end
test "supports t:Path.t() paths" do
filename = to_charlist(Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex"))
result = EEx.eval_file(filename, bar: 1)
assert_normalized_newline_equal("foo 1\n", result)
end
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
EEx.compile_string("foo <%= bar", file: "my_file.eex")
end
test "supports overriding file and line through options" do
filename = Path.join(__DIR__, "fixtures/eex_template_with_syntax_error.eex")
assert_raise EEx.SyntaxError, "my_file.eex:11:1: missing token '%>'", fn ->
EEx.eval_file(filename, _bindings = [], file: "my_file.eex", line: 10)
end
end
end
describe "precompiled" do
@@ -652,8 +673,8 @@ defmodule EExTest do
buffer <> ":END"
end
def handle_text(buffer, text) do
buffer <> ":TEXT(#{String.trim(text)})"
def handle_text(buffer, meta, text) do
buffer <> ":TEXT-#{meta[:line]}-#{meta[:column]}(#{String.trim(text)})"
end
def handle_expr(buffer, "/", expr) do
@@ -671,16 +692,16 @@ defmodule EExTest do
describe "custom engines" do
test "text" do
assert_eval("BODY(INIT:TEXT(foo))", "foo", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT-1-1(foo))", "foo", [], engine: TestEngine)
end
test "custom marker" do
assert_eval("BODY(INIT:TEXT(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT-1-1(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
end
test "begin/end" do
assert_eval(
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(that):END"\nend))],
~s[BODY(INIT:TEXT-1-1(foo):EQUAL(if do\n "BEGIN:TEXT-1-17(this):END"\nelse\n "BEGIN:TEXT-1-31(that):END"\nend))],
"foo <%= if do %>this<% else %>that<% end %>",
[],
engine: TestEngine
@@ -0,0 +1 @@
foo <%= bar
+36 -35
View File
@@ -7,7 +7,7 @@ defmodule Access do
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
of the box. Keywords supports only atoms keys, keys for maps can
be of any type. Both returns `nil` if the key does not exist:
be of any type. Both return `nil` if the key does not exist:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
@@ -47,12 +47,11 @@ defmodule Access do
> `map[key]`, if your map is made of predefined atom keys,
> you should prefer to access those atom keys with `map.key`
> instead of `map[key]`, as `map.key` will raise if the key
> is missing. This is important because, if a map has a predefined
> set of keys and a key is missing, it is most likely a bug
> in your software or a typo on the key name. For this reason,
> because structs are predefined in nature, they only allow
> the `struct.key` syntax and they do not allow the `struct[key]`
> access syntax. See the `Map` module for more information.
> is missing (which is not supposed to happen if the keys are
> predefined). Similarly, since structs are maps and structs
> have predefined keys, they only allow the `struct.key`
> syntax and they do not allow the `struct[key]` access syntax.
> See the `Map` module for more information.
## Nested data structures
@@ -100,16 +99,15 @@ defmodule Access do
@type key :: any
@type value :: any
@type get_fun(data, get_value) ::
(:get, data, (term -> term) ->
{get_value, new_data :: container})
@type get_fun(data) ::
(:get, data, (term -> term) -> new_data :: container)
@type get_and_update_fun(data, get_value) ::
@type get_and_update_fun(data, current_value) ::
(:get_and_update, data, (term -> term) ->
{get_value, new_data :: container} | :pop)
{current_value, new_data :: container} | :pop)
@type access_fun(data, get_value) ::
get_fun(data, get_value) | get_and_update_fun(data, get_value)
@type access_fun(data, current_value) ::
get_fun(data) | get_and_update_fun(data, current_value)
@doc """
Invoked in order to access the value stored under `key` in the given term `term`.
@@ -135,16 +133,16 @@ defmodule Access do
The implementation of this callback should invoke `fun` with the value under
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
This function must return either `{get_value, update_value}` or `:pop`.
This function must return either `{current_value, new_value}` or `:pop`.
If the passed function returns `{get_value, update_value}`,
the return value of this callback should be `{get_value, new_data}`, where:
If the passed function returns `{current_value, new_value}`,
the return value of this callback should be `{current_value, new_data}`, where:
* `get_value` is the retrieved value (which can be operated on before being returned)
* `current_value` is the retrieved value (which can be operated on before being returned)
* `update_value` is the new value to be stored under `key`
* `new_value` is the new value to be stored under `key`
* `new_data` is `data` after updating the value of `key` with `update_value`.
* `new_data` is `data` after updating the value of `key` with `new_value`.
If the passed function returns `:pop`, the return value of this callback
must be `{value, new_data}` where `value` is the value under `key`
@@ -153,8 +151,9 @@ defmodule Access do
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
for more examples.
"""
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container | any_container
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: value, data: container | any_container
@doc """
Invoked to "pop" the value under `key` out of the given data structure.
@@ -321,9 +320,9 @@ defmodule Access do
a struct that implements the `Access` behaviour).
The `fun` argument receives the value of `key` (or `nil` if `key` is not
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
the "get" value `get_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`update_value`).
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
the "get" value `current_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`new_value`).
`fun` may also return `:pop`, which means the current value
should be removed from the container and returned.
@@ -338,8 +337,9 @@ defmodule Access do
{1, [a: 2]}
"""
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: var, data: container
def get_and_update(container, key, fun)
def get_and_update(%module{} = container, key, fun) do
@@ -422,7 +422,7 @@ defmodule Access do
The returned function uses the default value if the key does not exist.
This can be used to specify defaults and safely traverse missing keys:
iex> get_in(%{}, [Access.key(:user, %{name: "meg"}), Access.key(:name)])
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
"meg"
Such is also useful when using update functions, allowing us to introduce
@@ -452,7 +452,7 @@ defmodule Access do
** (BadMapError) expected a map, got: []
"""
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
def key(key, default \\ nil) do
fn
:get, data, next ->
@@ -496,7 +496,7 @@ defmodule Access do
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
"""
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
def key!(key) do
fn
:get, %{} = data, next ->
@@ -544,7 +544,7 @@ defmodule Access do
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
"""
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
def elem(index) when is_integer(index) and index >= 0 do
pos = index + 1
@@ -598,7 +598,7 @@ defmodule Access do
** (RuntimeError) Access.all/0 expected a list, got: %{}
"""
@spec all() :: access_fun(data :: list, get_value :: list)
@spec all() :: access_fun(data :: list, current_value :: list)
def all() do
&all/3
end
@@ -673,7 +673,7 @@ defmodule Access do
** (RuntimeError) Access.at/1 expected a list, got: %{}
"""
@spec at(integer) :: access_fun(data :: list, get_value :: term)
@spec at(integer) :: access_fun(data :: list, current_value :: term)
def at(index) when is_integer(index) do
fn op, data, next -> at(op, data, index, next) end
end
@@ -727,7 +727,8 @@ defmodule Access do
** (Enum.OutOfBoundsError) out of bounds error
"""
@spec at!(integer) :: access_fun(data :: list, get_value :: term)
@doc since: "1.11.0"
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
def at!(index) when is_integer(index) do
fn op, data, next -> at!(op, data, index, next) end
end
@@ -794,7 +795,7 @@ defmodule Access do
"""
@doc since: "1.6.0"
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
def filter(func) when is_function(func) do
fn op, data, next -> filter(op, data, func, next) end
end
+1 -1
View File
@@ -241,7 +241,7 @@ defmodule Agent do
and the start function will return `{:error, :timeout}`.
If the `:debug` option is present, the corresponding function in the
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
[`:sys` module](`:sys`) will be invoked.
If the `:spawn_opt` option is present, its value will be passed as options
to the underlying process as in `Process.spawn/4`.
+23 -26
View File
@@ -163,7 +163,7 @@ defmodule Application do
In the sections above, we have configured an application in the
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
this configuration to create an [*application resource
file*](http://erlang.org/doc/man/app.html), which is a file called
file*](https://erlang.org/doc/man/application.html), which is a file called
`APP_NAME.app`. For example, the application resource file of the OTP
application `ex_unit` is called `ex_unit.app`.
@@ -267,11 +267,10 @@ defmodule Application do
## Further information
For further details on applications please check the documentation of the
[`application`](http://www.erlang.org/doc/man/application.html) Erlang module,
and the
[Applications](http://www.erlang.org/doc/design_principles/applications.html)
[`:application` Erlang module](`:application`), and the
[Applications](https://erlang.org/doc/design_principles/applications.html)
section of the [OTP Design Principles User's
Guide](http://erlang.org/doc/design_principles/users_guide.html).
Guide](https://erlang.org/doc/design_principles/users_guide.html).
"""
@doc """
@@ -499,7 +498,8 @@ defmodule Application do
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at compile time instead of compile_env
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
# compile time instead of compile_env
@doc since: "1.10.0"
@spec compile_env(app, key | list, value) :: value
defmacro compile_env(app, key_or_path, default \\ nil) when is_atom(app) do
@@ -507,11 +507,22 @@ defmodule Application do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
end
end
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
defp expand_key_or_path(list, env) when is_list(list),
do: Enum.map(list, &expand_key_or_path(&1, env))
defp expand_key_or_path(other, _env),
do: other
@doc false
def __compile_env__(app, key_or_path, default, env) do
case fetch_compile_env(app, key_or_path, env) do
@@ -533,6 +544,8 @@ defmodule Application do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
end
@@ -694,9 +707,6 @@ defmodule Application do
:application.set_env(app, key, value, opts)
end
# TODO: Remove this once we support Erlang/OTP 22+ exclusively.
@compile {:no_warn_undefined, {:application, :set_env, 2}}
@doc """
Puts the environment for multiple apps at the same time.
@@ -705,28 +715,14 @@ defmodule Application do
* have the same application listed more than once
* have the same key inside the same application listed more than once
If those conditions are not met, the behaviour is undefined
(on Erlang/OTP 21 and earlier) or will raise (on Erlang/OTP 22
and later).
If those conditions are not met, it will raise.
It receives the same options as `put_env/4`. Returns `:ok`.
"""
@doc since: "1.9.0"
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
# TODO: Remove function exported? check when we require Erlang/OTP 22+
if function_exported?(:application, :set_env, 2) do
:application.set_env(config, opts)
else
for app_keyword <- config,
{app, keyword} = app_keyword,
key_value <- keyword,
{key, value} = key_value do
:application.set_env(app, key, value, opts)
end
:ok
end
:application.set_env(config, opts)
end
@doc """
@@ -743,6 +739,7 @@ defmodule Application do
defp maybe_warn_on_app_env_key(_app, key) when is_atom(key),
do: :ok
# TODO: Remove this deprecation warning on 2.0+ and allow list lookups as in compile_env.
defp maybe_warn_on_app_env_key(app, key) do
message = "passing non-atom as application env key is deprecated, got: #{inspect(key)}"
IO.warn_once({Application, :key, app, key}, message, _stacktrace_drop_levels = 2)
@@ -887,7 +884,7 @@ defmodule Application do
#=> "bar-123"
For more information on code paths, check the `Code` module in
Elixir and also Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html).
Elixir and also Erlang's [`:code` module](`:code`).
"""
@spec app_dir(app) :: String.t()
def app_dir(app) when is_atom(app) do
+5 -3
View File
@@ -4,7 +4,7 @@ defmodule Behaviour do
This module is deprecated. Instead of `defcallback/1` and
`defmacrocallback/1`, the `@callback` and `@macrocallback`
module attributes can be used (respectively). See the
module attributes can be used respectively. See the
documentation for `Module` for more information on these
attributes.
@@ -17,6 +17,7 @@ defmodule Behaviour do
@doc """
Defines a function callback according to the given type specification.
"""
@deprecated "Use the @callback module attribute instead"
defmacro defcallback(spec) do
do_defcallback(:def, split_spec(spec, quote(do: term)))
end
@@ -24,6 +25,7 @@ defmodule Behaviour do
@doc """
Defines a macro callback according to the given type specification.
"""
@deprecated "Use the @macrocallback module attribute instead"
defmacro defmacrocallback(spec) do
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
end
@@ -111,9 +113,9 @@ defmodule Behaviour do
end
end
defp __behaviour__doc_value(:none), do: nil
defp __behaviour__doc_value(:hidden), do: false
defp __behaviour__doc_value(%{"en" => doc}), do: doc
defp __behaviour__doc_value(:hidden), do: false
defp __behaviour__doc_value(_), do: nil
import unquote(__MODULE__)
end
+2 -16
View File
@@ -191,22 +191,8 @@ defmodule Bitwise do
:erlang.bxor(left, right)
end
@doc """
Bitwise XOR operator.
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 ^^^ 3
10
"""
@doc guard: true
@spec integer ^^^ integer :: integer
def left ^^^ right do
@doc false
def unquote(:^^^)(left, right) do
:erlang.bxor(left, right)
end
+4 -1
View File
@@ -453,7 +453,7 @@ defmodule Calendar do
b | Abbreviated month name | Jan
B | Full month name | January
c | Preferred date+time representation | 2018-10-17 12:34:56
d | Day of the month | 01, 12
d | Day of the month | 01, 31
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
H | Hour using a 24-hour clock | 00, 23
I | Hour using a 12-hour clock | 01, 12
@@ -485,6 +485,9 @@ defmodule Calendar do
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
"Mon, August 26 2019"
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
"April 2, 2020"
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
"2019-08-26 13:52:06"
+59 -16
View File
@@ -83,8 +83,8 @@ defmodule Date do
366
iex> Enum.member?(range, ~D[2001-02-01])
true
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
-366
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
"""
@doc since: "1.5.0"
@@ -92,19 +92,61 @@ defmodule Date do
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
%Date.Range{
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
first_in_iso_days: first_days,
last_in_iso_days: last_days
}
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.16
step = if first_days <= last_days, do: 1, else: -1
range(first, first_days, last, last_days, calendar, step)
end
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
raise ArgumentError, "both dates must have matching calendars"
end
@doc """
Returns a range of dates with a step.
## Examples
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> range
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
iex> Enum.count(range)
183
iex> Enum.member?(range, ~D[2001-01-03])
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
"""
@doc since: "1.12.0"
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) ::
Date.Range.t()
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step)
when is_integer(step) and step != 0 do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
range(first, first_days, last, last_days, calendar, step)
end
def range(
%{calendar: _, year: _, month: _, day: _} = first,
%{calendar: _, year: _, month: _, day: _} = last,
step
) do
raise ArgumentError,
"both dates must have matching calendar and the step must be a " <>
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}"
end
defp range(first, first_days, last, last_days, calendar, step) do
%Date.Range{
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
}
end
@doc """
Returns the current date in UTC.
@@ -273,7 +315,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
The year parsed by this function is limited to four digits.
@@ -298,7 +340,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -323,7 +365,7 @@ defmodule Date do
@doc """
Converts the given `date` to
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Date.to_iso8601/2` returns dates formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
@@ -500,8 +542,8 @@ defmodule Date do
end
end
def compare(date1, date2) do
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
def compare(%{calendar: calendar1} = date1, %{calendar: calendar2} = date2) do
if Calendar.compatible_calendars?(calendar1, calendar2) do
case {to_iso_days(date1), to_iso_days(date2)} do
{first, second} when first > second -> :gt
{first, second} when first < second -> :lt
@@ -659,11 +701,12 @@ defmodule Date do
end
end
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
@doc false
def to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
end
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
def to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
end
+157 -63
View File
@@ -2,12 +2,13 @@ defmodule Date.Range do
@moduledoc """
Returns an inclusive range between dates.
Ranges must be created with the `Date.range/2` function.
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
The following fields are public:
* `:first` - the initial date on the range
* `:last` - the last date on the range
* `:step` - (since v1.12.0) the step
The remaining fields are private and should not be accessed.
"""
@@ -16,98 +17,132 @@ defmodule Date.Range do
first: Date.t(),
last: Date.t(),
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days()
last_in_iso_days: iso_days(),
step: pos_integer | neg_integer
}
@typep iso_days() :: Calendar.iso_days()
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
defimpl Enumerable do
def member?(%{first: %{calendar: calendar}} = range, %Date{calendar: calendar} = date) do
%{
first: first,
last: last,
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days
} = range
def member?(
%Date.Range{
first: %{calendar: calendar},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
} = range,
%Date{calendar: calendar} = date
) do
{days, _} = Date.to_iso_days(date)
%{year: first_year, month: first_month, day: first_day} = first
%{year: last_year, month: last_month, day: last_day} = last
%{year: year, month: month, day: day} = date
first = {first_year, first_month, first_day}
last = {last_year, last_month, last_day}
date = {year, month, day}
cond do
empty?(range) ->
{:ok, false}
if first_in_iso_days <= last_in_iso_days do
{:ok, date >= first and date <= last}
else
{:ok, date >= last and date <= first}
first_days <= last_days ->
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
true ->
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
end
end
def member?(_, _) do
def member?(%Date.Range{step: _}, _) do
{:ok, false}
end
def count(%{first_in_iso_days: first, last_in_iso_days: last}) do
{:ok, abs(first - last) + 1}
# TODO: Remove me on v2.0
def member?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
date
) do
step = if first_days <= last_days, do: 1, else: -1
member?(Map.put(date_range, :step, step), date)
end
def slice(range) do
%{
first_in_iso_days: first,
last_in_iso_days: last,
first: %{calendar: calendar}
} = range
if first <= last do
{:ok, last - first + 1, &slice_asc(first + &1, &2, calendar)}
else
{:ok, first - last + 1, &slice_desc(first - &1, &2, calendar)}
end
def count(range) do
{:ok, size(range)}
end
defp slice_asc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_asc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_asc(current + 1, remaining - 1, calendar)]
def slice(
%Date.Range{
first_in_iso_days: first,
first: %{calendar: calendar},
step: step
} = range
) do
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
end
defp slice_desc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_desc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_desc(current - 1, remaining - 1, calendar)]
# TODO: Remove me on v2.0
def slice(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
slice(Map.put(date_range, :step, step))
end
def reduce(range, acc, fun) do
%{
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days,
first: %{calendar: calendar}
} = range
up? = first_in_iso_days <= last_in_iso_days
reduce(first_in_iso_days, last_in_iso_days, acc, fun, calendar, up?)
defp slice(current, _step, 1, calendar) do
[date_from_iso_days(current, calendar)]
end
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
defp slice(current, step, remaining, calendar) do
[
date_from_iso_days(current, calendar)
| slice(current + step, step, remaining - 1, calendar)
]
end
def reduce(
%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
first: %{calendar: calendar},
step: step
},
acc,
fun
) do
reduce(first_days, last_days, acc, fun, step, calendar)
end
# TODO: Remove me on v2.0
def reduce(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
acc,
fun
) do
step = if first_days <= last_days, do: 1, else: -1
reduce(Map.put(date_range, :step, step), acc, fun)
end
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
{:halted, acc}
end
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
end
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
reduce(x + 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
when step > 0 and first_days <= last_days
when step < 0 and first_days >= last_days do
reduce(
first_days + step,
last_days,
fun.(date_from_iso_days(first_days, calendar), acc),
fun,
step,
calendar
)
end
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
reduce(x - 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
end
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
{:done, acc}
end
@@ -122,11 +157,70 @@ defmodule Date.Range do
%Date{year: year, month: month, day: day, calendar: calendar}
end
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step > 0 and first_days > last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step < 0 and first_days < last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
do: abs(div(last_days - first_days, step)) + 1
# TODO: Remove me on v2.0
defp size(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
size(Map.put(date_range, :step, step))
end
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step > 0 and first_days > last_days,
do: true
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step < 0 and first_days < last_days,
do: true
defp empty?(%Date.Range{step: _}), do: false
# TODO: Remove me on v2.0
defp empty?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
empty?(Map.put(date_range, :step, step))
end
end
defimpl Inspect do
def inspect(%Date.Range{first: first, last: last}, _) do
import Kernel, except: [inspect: 2]
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
end
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
end
# TODO: Remove me on v2.0
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
step = if first <= last, do: 1, else: -1
inspect(Map.put(date_range, :step, step), opts)
end
end
end
+76 -23
View File
@@ -109,8 +109,9 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
iex> {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -139,7 +140,7 @@ defmodule DateTime do
@doc since: "1.11.0"
@spec new(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) ::
{:ok, t}
| {:ambiguous, t, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -240,9 +241,7 @@ defmodule DateTime do
{:error, reason} ->
raise ArgumentError,
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{
inspect(reason)
}"
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{inspect(reason)}"
end
end
@@ -366,8 +365,9 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -418,7 +418,7 @@ defmodule DateTime do
Calendar.time_zone_database()
) ::
{:ok, t}
| {:ambiguous, t, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -892,13 +892,14 @@ defmodule DateTime do
@doc """
Converts the given datetime to
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601) format.
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601) format.
By default, `DateTime.to_iso8601/2` returns datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
Only supports converting datetimes which are in the ISO calendar,
attempting to convert datetimes from other calendars will raise.
You can also optionally specify an offset for the formatted string.
WARNING: the ISO 8601 datetime format does not contain the time zone nor
its abbreviation, which means information is lost when converting to such
@@ -930,11 +931,31 @@ defmodule DateTime do
iex> DateTime.to_iso8601(dt, :basic)
"20000229T230007-0400"
"""
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
def to_iso8601(datetime, format \\ :extended)
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 3600)
"2000-03-01T04:00:07+01:00"
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07+00:00"
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07Z"
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
"""
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
when format in [:extended, :basic] do
%{
year: year,
@@ -949,21 +970,56 @@ defmodule DateTime do
std_offset: std_offset
} = datetime
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format) <>
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
Calendar.ISO.offset_to_string(utc_offset, std_offset, time_zone, format)
end
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
def to_iso8601(
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
format,
0
)
when format in [:extended, :basic] do
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
"Z"
end
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
when format in [:extended, :basic] do
{_, precision} = datetime.microsecond
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
Calendar.ISO.offset_to_string(offset, 0, nil, format)
end
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
datetime
|> convert!(Calendar.ISO)
|> to_iso8601(format)
|> to_iso8601(format, offset)
end
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
total_offset = datetime.utc_offset + datetime.std_offset
datetime
|> to_iso_days()
# Subtract total original offset in order to get UTC and add the new offset
|> Calendar.ISO.add_day_fraction_to_iso_days(offset - total_offset, 86400)
|> calendar.naive_datetime_from_iso_days()
end
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
end
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Since ISO 8601 does not include the proper time zone, the given
string will be converted to UTC and its offset in seconds will be
@@ -973,9 +1029,6 @@ defmodule DateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
+354 -137
View File
@@ -1,16 +1,127 @@
defmodule Calendar.ISO do
@moduledoc """
A calendar implementation that follows to ISO 8601.
The default calendar implementation, a Gregorian calendar following ISO 8601.
This calendar implements the proleptic Gregorian calendar and
This calendar implements a proleptic Gregorian calendar and
is therefore compatible with the calendar used in most countries
today. The proleptic means the Gregorian rules for leap years are
applied for all time, consequently the dates give different results
before the year 1583 from when the Gregorian calendar was adopted.
Note that while ISO 8601 allows times and datetimes to specify
24:00:00 as the zero hour of the next day, this notation is not
supported by Elixir.
## ISO 8601 compliance
The ISO 8601 specification is feature-rich, but allows applications
to selectively implement most parts of it. The choices Elixir makes
are catalogued below.
### Features
The standard library supports a minimal set of possible ISO 8601 features.
Specifically, the parser only supports calendar dates and does not support
ordinal and week formats.
By default Elixir only parses extended-formatted date/times. You can opt-in
to parse basic-formatted date/times.
`NaiveDateTime.to_iso8601/2` and `DateTime.to_iso8601/2` allow you to produce
either basic or extended formatted strings, and `Calendar.strftime/2` allows
you to format datetimes however else you desire.
Elixir does not support reduced accuracy formats (for example, a date without
the day component) nor decimal precisions in the lowest component (such as
`10:01:25,5`). No functions exist to parse ISO 8601 durations or time intervals.
#### Examples
Elixir expects the extended format by default when parsing:
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007")
{:error, :invalid_format}
Parsing can be restricted to basic if desired:
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :extended)
{:error, :invalid_format}
Only calendar dates are supported in parsing; ordinal and week dates are not.
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-105")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W16")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W016-3")
{:error, :invalid_format}
Years, months, days, hours, minutes, and seconds must be fully specified:
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-04")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23")
{:error, :invalid_format}
### Extensions
The parser and formatter adopt one ISO 8601 extension: extended year notation.
This allows dates to be prefixed with a `+` or `-` sign, extending the range of
expressible years from the default (`0000..9999`) to `-9999..9999`. Elixir still
restricts years in this format to four digits.
#### Examples
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("+2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_naive_datetime("-2015-01-23 23:50:07")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("+2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23 23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("+2015-01-23 23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
### Additions
ISO 8601 does not allow a whitespace instead of `T` as a separator
between date and times, both when parsing and formatting.
This is a common enough representation, Elixir allows it during parsing.
The formatting of dates in `NaiveDateTime.to_iso8601/1` and `DateTime.to_iso8601/1`
do produce specification-compliant string representations using the `T` separator.
#### Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
"""
@behaviour Calendar
@@ -72,7 +183,10 @@ defmodule Calendar.ISO do
@microseconds_per_second 1_000_000
@parts_per_day @seconds_per_day * @microseconds_per_second
@sep [?\s, ?T]
@datetime_seps [?\s, ?T]
@ext_date_sep ?-
@ext_time_sep ?:
@days_per_nonleap_year 365
@days_per_leap_year 366
@@ -81,10 +195,11 @@ defmodule Calendar.ISO do
# on ~D[0001-01-01] which is 366 days later.
@iso_epoch 366
[match_date, guard_date, read_date] =
[match_basic_date, match_ext_date, guard_date, read_date] =
quote do
[
<<y1, y2, y3, y4, ?-, m1, m2, ?-, d1, d2>>,
<<y1, y2, y3, y4, m1, m2, d1, d2>>,
<<y1, y2, y3, y4, @ext_date_sep, m1, m2, @ext_date_sep, d1, d2>>,
y1 >= ?0 and y1 <= ?9 and y2 >= ?0 and y2 <= ?9 and y3 >= ?0 and y3 <= ?9 and y4 >= ?0 and
y4 <= ?9 and m1 >= ?0 and m1 <= ?9 and m2 >= ?0 and m2 <= ?9 and d1 >= ?0 and d1 <= ?9 and
d2 >= ?0 and d2 <= ?9,
@@ -96,10 +211,11 @@ defmodule Calendar.ISO do
]
end
[match_time, guard_time, read_time] =
[match_basic_time, match_ext_time, guard_time, read_time] =
quote do
[
<<h1, h2, ?:, i1, i2, ?:, s1, s2>>,
<<h1, h2, i1, i2, s1, s2>>,
<<h1, h2, @ext_time_sep, i1, i2, @ext_time_sep, s1, s2>>,
h1 >= ?0 and h1 <= ?9 and h2 >= ?0 and h2 <= ?9 and i1 >= ?0 and i1 <= ?9 and i2 >= ?0 and
i2 <= ?9 and s1 >= ?0 and s1 <= ?9 and s2 >= ?0 and s2 <= ?9,
{
@@ -128,49 +244,69 @@ defmodule Calendar.ISO do
defguardp is_std_offset(offset) when is_integer(offset)
@doc """
Parses a time string.
Parses a time `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("T23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07,0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.123Z")
{:ok, {23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_time("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:61")
{:error, :invalid_time}
"""
@doc since: "1.10.0"
@impl true
def parse_time("T" <> string) when is_binary(string),
do: do_parse_time(string)
def parse_time(string) when is_binary(string),
do: do_parse_time(string)
do: parse_time(string, :extended)
defp do_parse_time(string) do
with <<unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
@doc """
Parses a time `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_time("235007", :basic)
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_time("T" <> string, format) when is_binary(string),
do: do_parse_time(string, format)
def parse_time(string, format) when is_binary(string),
do: do_parse_time(string, format)
defp do_parse_time(<<unquote(match_basic_time), rest::binary>>, :basic)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(<<unquote(match_ext_time), rest::binary>>, :extended)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(_, _) do
{:error, :invalid_format}
end
defp parse_formatted_time(hour, minute, second, rest) do
with {microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{hour, minute, second} = unquote(read_time)
if valid_time?(hour, minute, second, microsecond) do
{:ok, {hour, minute, second, microsecond}}
else
@@ -182,14 +318,16 @@ defmodule Calendar.ISO do
end
@doc """
Parses a date string.
Parses a date `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_date("2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("2015:01:23")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-01-32")
@@ -198,91 +336,142 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
def parse_date("-" <> string) when is_binary(string),
do: parse_date(string, -1)
def parse_date(string) when is_binary(string),
do: parse_date(string, 1)
do: parse_date(string, :extended)
defp parse_date(string, multiplier) do
with unquote(match_date) <- string, true <- unquote(guard_date) do
{year, month, day} = unquote(read_date)
year = multiplier * year
@doc """
Parses a date `string` according to a given `format`.
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
else
{:error, :invalid_date}
end
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_date("20150123", :basic)
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("20150123", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_date("-" <> string, format) when is_binary(string),
do: do_parse_date(string, -1, format)
def parse_date("+" <> string, format) when is_binary(string),
do: do_parse_date(string, 1, format)
def parse_date(string, format) when is_binary(string),
do: do_parse_date(string, 1, format)
defp do_parse_date(unquote(match_basic_date), multiplier, :basic) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(unquote(match_ext_date), multiplier, :extended) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_date(year, month, day, multiplier) do
year = multiplier * year
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
else
_ ->
{:error, :invalid_format}
{:error, :invalid_date}
end
end
@doc """
Parses a naive datetime string.
Parses a naive datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07Z")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07-02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 1}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07,0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123Z")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_naive_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+00:00")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:60")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-24:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
def parse_naive_datetime("-" <> string) when is_binary(string),
do: parse_naive_datetime(string, -1)
def parse_naive_datetime(string) when is_binary(string),
do: parse_naive_datetime(string, 1)
do: parse_naive_datetime(string, :extended)
defp parse_naive_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
@doc """
Parses a naive datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_naive_datetime("-" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, -1, format)
def parse_naive_datetime("+" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
def parse_naive_datetime(string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
defp do_parse_naive_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -299,54 +488,85 @@ defmodule Calendar.ISO do
end
@doc """
Parses a UTC datetime string.
Parses a UTC datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07Z")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {0, 0}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07,123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07,123+02:30")
{:ok, {-2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07")
{:error, :missing_offset}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_utc_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
def parse_utc_datetime("-" <> string) when is_binary(string),
do: parse_utc_datetime(string, -1)
def parse_utc_datetime(string) when is_binary(string),
do: parse_utc_datetime(string, 1)
do: parse_utc_datetime(string, :extended)
defp parse_utc_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
@doc """
Parses a UTC datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_utc_datetime("-" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, -1, format)
def parse_utc_datetime("+" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
def parse_utc_datetime(string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
defp do_parse_utc_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
{offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -376,8 +596,7 @@ defmodule Calendar.ISO do
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
end
else
_ ->
{:error, :invalid_format}
_ -> {:error, :invalid_format}
end
end
@@ -1069,9 +1288,7 @@ defmodule Calendar.ISO do
@doc """
Determines if the date given is valid according to the proleptic Gregorian calendar.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
Leap seconds are not supported by the built-in Calendar.ISO.
## Examples
+3 -6
View File
@@ -598,7 +598,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in naive date
@@ -607,9 +607,6 @@ defmodule NaiveDateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
@@ -676,7 +673,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -704,7 +701,7 @@ defmodule NaiveDateTime do
@doc """
Converts the given naive datetime to
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
By default, `NaiveDateTime.to_iso8601/2` returns naive datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
+3 -9
View File
@@ -211,7 +211,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in times.
@@ -219,12 +219,6 @@ defmodule Time do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
Time representations with reduced accuracy are not supported.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
## Examples
iex> Time.from_iso8601("23:50:07")
@@ -263,7 +257,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -290,7 +284,7 @@ defmodule Time do
@doc """
Converts the given time to
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
format, for human readability. It also supports the "basic" format through
+460 -105
View File
@@ -2,7 +2,7 @@ defmodule Code do
@moduledoc ~S"""
Utilities for managing code compilation, code evaluation, and code loading.
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
This module complements Erlang's [`:code` module](`:code`)
to add behaviour which is specific to Elixir. Almost all of the functions in this module
have global side effects on the behaviour of Elixir.
@@ -30,6 +30,56 @@ defmodule Code do
file, without tracking. `eval_file/2` should be used when you are interested in
the result of evaluating the file rather than the modules it defines.
## Code loading on the Erlang VM
Erlang has two modes to load code: interactive and embedded.
By default, the Erlang VM runs in interactive mode, where modules
are loaded as needed. In embedded mode the opposite happens, as all
modules need to be loaded upfront or explicitly.
You can use `ensure_loaded/1` (as well as `ensure_loaded?/1` and
`ensure_loaded!/1`) to check if a module is loaded before using it and
act.
## `ensure_compiled/1` and `ensure_compiled!/1`
Elixir also includes `ensure_compiled/1` and `ensure_compiled!/1`
functions that are a superset of `ensure_loaded/1`.
Since Elixir's compilation happens in parallel, in some situations
you may need to use a module that was not yet compiled, therefore
it can't even be loaded.
When invoked, `ensure_compiled/1` and `ensure_compiled!/1` halt the
compilation of the caller until the module becomes available. Note
the distinction between `ensure_compiled/1` and `ensure_compiled!/1`
is important: if you are using `ensure_compiled!/1`, you are
indicating to the compiler that you can only continue if said module
is available.
If you are using `Code.ensure_compiled/1`, you are implying you may
continue without the module and therefore Elixir may return
`{:error, :unavailable}` for cases where the module is not yet available
(but may be available later on).
For those reasons, developers must typically use `Code.ensure_compiled!/1`.
In particular, do not do this:
case Code.ensure_compiled(module) do
{:module, _} -> module
{:error, _} -> raise ...
end
Finally, note you only need `ensure_compiled!/1` to check for modules
being defined within the same project. It does not apply to modules from
dependencies as dependencies are always compiled upfront.
In most cases, `ensure_loaded/1` is enough. `ensure_compiled!/1`
must be used in rare cases, usually involving macros that need to
invoke a module for callback information. The use of `ensure_compiled/1`
is even less likely.
## Compilation tracers
Elixir supports compilation tracers, which allows modules to observe constructs
@@ -156,6 +206,259 @@ defmodule Code do
required_files()
end
@doc """
Receives a string and returns the cursor context.
This function receives a string with incomplete Elixir code,
representing a cursor position, and based on the string, it
provides contextual information about said position. The
return of this function can then be used to provide tips,
suggestions, and autocompletion functionality.
This function provides a best-effort detection and may not be
accurate under certain circumstances. See the "Limitations"
section below.
Consider adding a catch-all clause when handling the return
type of this function as new cursor information may be added
in future releases.
## Examples
iex> Code.cursor_context("")
:expr
iex> Code.cursor_context("hello_wor")
{:local_or_var, 'hello_wor'}
## Return values
* `{:alias, charlist}` - the context is an alias, potentially
a nested one, such as `Hello.Wor` or `HelloWor`
* `{:dot, inside_dot, charlist}` - the context is a dot
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, this may either be a remote call or a map
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, it must be a remote arity. Examples are
`Hello.world/`, `:hello.world/`, `hello.world/2`, and `@hello.world/2`
* `{:dot_call, inside_dot, charlist}` - the context is a dot
call. This means parentheses or space have been added after the expression.
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, it must be a remote call. Examples are
`Hello.world(`, `:hello.world(`, `Hello.world `, `hello.world(`, `hello.world `,
and `@hello.world(`
* `:expr` - may be any expression. Autocompletion may suggest an alias,
local or var
* `{:local_or_var, charlist}` - the context is a variable or a local
(import or local) call, such as `hello_wor`
* `{:local_arity, charlist}` - the context is a local (import or local)
call, such as `hello_world/`
* `{:local_call, charlist}` - the context is a local (import or local)
call, such as `hello_world(` and `hello_world `
* `{:module_attribute, charlist}` - the context is a module attribute, such
as `@hello_wor`
* `:none` - no context possible
* `:unquoted_atom` - the context is an unquoted atom. This can be either
previous atoms or all available `:erlang` modules
## Limitations
* There is no context for operators
* The current algorithm only considers the last line of the input
* Context does not yet track strings, sigils, etc.
* Arguments of functions calls are not currently recognized
"""
@doc since: "1.12.0"
@spec cursor_context(List.Chars.t(), keyword()) ::
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:dot_arity, inside_dot, charlist}
| {:dot_call, inside_dot, charlist}
| :expr
| {:local_or_var, charlist}
| {:local_arity, charlist}
| {:local_call, charlist}
| {:module_attribute, charlist}
| :none
| {:unquoted_atom, charlist}
when inside_dot:
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
def cursor_context(string, opts \\ [])
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
binary =
case :binary.matches(binary, "\n") do
[] ->
binary
matches ->
{position, _} = List.last(matches)
binary_part(binary, position + 1, byte_size(binary) - position - 1)
end
do_cursor_context(String.to_charlist(binary), opts)
end
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
chunked = Enum.chunk_by(charlist, &(&1 == ?\n))
case List.last(chunked, []) do
[?\n | _] -> do_cursor_context([], opts)
rest -> do_cursor_context(rest, opts)
end
end
def cursor_context(other, opts) do
cursor_context(to_charlist(other), opts)
end
@operators '\\<>+-*/:=|&~^@%'
@non_closing_punctuation '.,([{;'
@closing_punctuation ')]}'
@space '\t\s'
@closing_identifier '?!'
@operators_and_non_closing_puctuation @operators ++ @non_closing_punctuation
@non_identifier @closing_identifier ++
@operators ++ @non_closing_punctuation ++ @closing_punctuation ++ @space
defp do_cursor_context(list, _opts) do
reverse = Enum.reverse(list)
case strip_spaces(reverse, 0) do
# It is empty
{[], _} ->
:expr
{[?: | _], 0} ->
{:unquoted_atom, ''}
{[?@ | _], 0} ->
{:module_attribute, ''}
{[?. | rest], _} ->
dot(rest, '')
# It is a local or remote call with parens
{[?( | rest], _} ->
call_to_cursor_context(rest)
# A local arity definition
{[?/ | rest], _} ->
case identifier_to_cursor_context(rest) do
{:local_or_var, acc} -> {:local_arity, acc}
{:dot, base, acc} -> {:dot_arity, base, acc}
_ -> :none
end
# Starting a new expression
{[h | _], _} when h in @operators_and_non_closing_puctuation ->
:expr
# It is a local or remote call without parens
{rest, spaces} when spaces > 0 ->
call_to_cursor_context(rest)
# It is an identifier
_ ->
identifier_to_cursor_context(reverse)
end
end
defp strip_spaces([h | rest], count) when h in @space, do: strip_spaces(rest, count + 1)
defp strip_spaces(rest, count), do: {rest, count}
defp call_to_cursor_context(reverse) do
case identifier_to_cursor_context(reverse) do
{:local_or_var, acc} -> {:local_call, acc}
{:dot, base, acc} -> {:dot_call, base, acc}
_ -> :none
end
end
defp identifier_to_cursor_context(reverse) do
case identifier(reverse) do
# Parse :: first to avoid ambiguity with atoms
{:alias, false, '::' ++ _, _} -> :none
{kind, _, '::' ++ _, acc} -> alias_or_local_or_var(kind, acc)
# Now handle atoms, any other atom is unexpected
{_kind, _, ':' ++ _, acc} -> {:unquoted_atom, acc}
{:atom, _, _, _} -> :none
# Parse .. first to avoid ambiguity with dots
{:alias, false, _, _} -> :none
{kind, _, '..' ++ _, acc} -> alias_or_local_or_var(kind, acc)
# Module attributes
{:alias, _, '@' ++ _, _} -> :none
{:identifier, _, '@' ++ _, acc} -> {:module_attribute, acc}
# Everything else
{:alias, _, '.' ++ rest, acc} -> nested_alias(rest, acc)
{:identifier, _, '.' ++ rest, acc} -> dot(rest, acc)
{kind, _, _, acc} -> alias_or_local_or_var(kind, acc)
:none -> :none
end
end
defp nested_alias(rest, acc) do
case identifier_to_cursor_context(rest) do
{:alias, prev} -> {:alias, prev ++ '.' ++ acc}
_ -> :none
end
end
defp dot(rest, acc) do
case identifier_to_cursor_context(rest) do
{:local_or_var, prev} -> {:dot, {:var, prev}, acc}
{:unquoted_atom, _} = prev -> {:dot, prev, acc}
{:alias, _} = prev -> {:dot, prev, acc}
{:dot, _, _} = prev -> {:dot, prev, acc}
{:module_attribute, _} = prev -> {:dot, prev, acc}
_ -> :none
end
end
defp alias_or_local_or_var(:alias, acc), do: {:alias, acc}
defp alias_or_local_or_var(:identifier, acc), do: {:local_or_var, acc}
defp alias_or_local_or_var(_, _), do: :none
defp identifier([?? | rest]), do: check_identifier(rest, [??])
defp identifier([?! | rest]), do: check_identifier(rest, [?!])
defp identifier(rest), do: check_identifier(rest, [])
defp check_identifier([h | _], _acc) when h in @non_identifier, do: :none
defp check_identifier(rest, acc), do: rest_identifier(rest, acc)
defp rest_identifier([h | rest], acc) when h not in @non_identifier do
rest_identifier(rest, [h | acc])
end
defp rest_identifier(rest, acc) do
case String.Tokenizer.tokenize(acc) do
{kind, _, [], _, ascii_only?, _} -> {kind, ascii_only?, rest, acc}
_ -> :none
end
end
@doc """
Removes files from the required files list.
@@ -306,21 +609,33 @@ defmodule Code do
## Examples
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
{3, [a: 1, b: 2]}
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2, c: 3]}
iex> {result, binding} = Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2, c: 3]
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
{3, [a: 3, b: 2]}
iex> {result, binding} = Code.eval_string("a = a + b", [a: 1, b: 2])
iex> result
3
iex> Enum.sort(binding)
[a: 3, b: 2]
For convenience, you can pass `__ENV__/0` as the `opts` argument and
all imports, requires and aliases defined in the current environment
will be automatically carried over:
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2]}
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
"""
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@@ -358,9 +673,8 @@ defmodule Code do
* `:line_length` - the line length to aim for when formatting
the document. Defaults to 98. Note this value is used as
reference but it is not enforced by the formatter as sometimes
user intervention is required. See "Running the formatter"
section
guideline but there are situations where it is not enforced.
See the "Line length" section below for more information
* `:locals_without_parens` - a keyword list of name and arity
pairs that should be kept without parens whenever possible.
@@ -368,17 +682,12 @@ defmodule Code do
that name. The formatter already includes a list of functions
and this option augments this list.
* `:rename_deprecated_at` - rename all known deprecated functions
at the given version to their non-deprecated equivalent. It
expects a valid `Version` which is usually the minimum Elixir
version supported by the project.
* `:force_do_end_blocks` (since v1.9.0) - when `true`, converts all
inline usages of `do: ...`, `else: ...` and friends into `do/end`
blocks. Defaults to `false`. Note that this option is convergent:
once you set it to `true`, all keywords will be converted. If you
set it to `false` later on, `do/end` blocks won't be converted
back to keywords.
once you set it to `true`, **all keywords** will be converted.
If you set it to `false` later on, `do/end` blocks won't be
converted back to keywords.
## Design principles
@@ -386,8 +695,6 @@ defmodule Code do
First, the formatter never changes the semantics of the code by
default. This means the input AST and the output AST are equivalent.
Optional behaviour, such as `:rename_deprecated_at`, is allowed to
break this guarantee.
The second principle is to provide as little configuration as possible.
This eases the formatter adoption by removing contention points while
@@ -413,29 +720,8 @@ defmodule Code do
do not recommend to run the formatter blindly in an existing codebase.
Instead you should format and sanity check each formatted file.
Let's see some examples. The code below:
"this is a very long string ... #{inspect(some_value)}"
may be formatted as:
"this is a very long string ... #{
inspect(some_value)
}"
This happens because the only place the formatter can introduce a
new line without changing the code semantics is in the interpolation.
In those scenarios, we recommend developers to directly adjust the
code. Here we can use the binary concatenation operator `<>/2`:
"this is a very long string " <>
"... #{inspect(some_value)}"
The string concatenation makes the code fit on a single line and also
gives more options to the formatter.
A similar example is when the formatter breaks a function definition
over multiple clauses:
For example, the formatter may break a long function definition over
multiple clauses:
def my_function(
%User{name: name, age: age, ...},
@@ -500,6 +786,40 @@ defmodule Code do
we describe in the next sections the cases where the formatter keeps the
user encoding and how to control multiline expressions.
## Line length
Another point about the formatter is that the `:line_length` configuration
is a guideline. In many cases, it is not possible for the formatter to break
your code apart, which means it will go over the line length. For example,
if you have a long string:
"this is a very long string that will go over the line length"
The formatter doesn't know how to break it apart without changing the
code underlying syntax representation, so it is up to you to step in:
"this is a very long string " <>
"that will go over the line length"
The string concatenation makes the code fit on a single line and also
gives more options to the formatter.
This may also appear in do/end blocks, where the `do` keyword (or `->`)
may go over the line lenth because there is no opportunity for the
formatter to introduce a line break in a readable way. For example,
if you do:
case very_long_expression() do
And only the `do` keyword is above the line length, Elixir **will not**
emit this:
case very_long_expression()
do
So it prefers to not touch the line at all and leave `do` above the
line limit.
## Keeping user's formatting
The formatter respects the input format in some cases. Those are
@@ -646,6 +966,10 @@ defmodule Code do
are considered equivalent (the nesting is discarded alongside most of
user formatting). In such cases, the code formatter will always format to
the latter.
## Newlines
The formatter converts all newlines in code from `\r\n` to `\n`.
"""
@doc since: "1.6.0"
@spec format_string!(binary, keyword) :: iodata
@@ -682,15 +1006,21 @@ defmodule Code do
## Examples
iex> contents = quote(do: var!(a) + var!(b))
iex> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
{3, [a: 1, b: 2]}
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
For convenience, you can pass `__ENV__/0` as the `opts` argument and
all options will be automatically extracted from the current environment:
iex> contents = quote(do: var!(a) + var!(b))
iex> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2]}
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
"""
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@@ -1182,39 +1512,7 @@ defmodule Code do
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
## Code loading on the Erlang VM
Erlang has two modes to load code: interactive and embedded.
By default, the Erlang VM runs in interactive mode, where modules
are loaded as needed. In embedded mode the opposite happens, as all
modules need to be loaded upfront or explicitly.
Therefore, this function is used to check if a module is loaded
before using it and allows one to react accordingly. For example, the `URI`
module uses this function to check if a specific parser exists for a given
URI scheme.
## `ensure_compiled/1`
Elixir also contains an `ensure_compiled/1` function that is a
superset of `ensure_loaded/1`.
Since Elixir's compilation happens in parallel, in some situations
you may need to use a module that was not yet compiled, therefore
it can't even be loaded.
When invoked, `ensure_compiled/1` halts the compilation of the caller
until the module given to `ensure_compiled/1` becomes available or
all files for the current project have been compiled. If compilation
finishes and the module is not available, an error tuple is returned.
`ensure_compiled/1` does not apply to dependencies, as dependencies
must be compiled upfront.
In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
must be used in rare cases, usually involving macros that need to
invoke a module for callback information.
See the module documentation for more information on code loading.
## Examples
@@ -1249,14 +1547,62 @@ defmodule Code do
match?({:module, ^module}, ensure_loaded(module))
end
@doc """
Same as `ensure_loaded/1` but raises if the module cannot be loaded.
"""
@doc since: "1.12.0"
@spec ensure_loaded!(module) :: module
def ensure_loaded!(module) do
case ensure_loaded(module) do
{:module, module} ->
module
{:error, reason} ->
raise ArgumentError,
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
end
end
@doc """
Similar to `ensure_compiled!/1` but indicates you can continue without said module.
While `ensure_compiled!/1` indicates to the Elixir compiler you can
only continue when said module is available, this function indicates
you may continue compilation without said module.
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
If the module being checked is currently in a compiler deadlock,
this function returns `{:error, :unavailable}`. Unavailable doesn't
necessarily mean the module doesn't exist, just that it is not currently
available, but it (or may not) become available in the future.
Therefore, if you can only continue if the module is available, use
`ensure_compiled!/1` instead. In particular, do not do this:
case Code.ensure_compiled(module) do
{:module, _} -> module
{:error, _} -> raise ...
end
See the module documentation for more information on code loading.
"""
@spec ensure_compiled(module) ::
{:module, module}
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
def ensure_compiled(module) when is_atom(module) do
ensure_compiled(module, :soft)
end
@doc """
Ensures the given module is compiled and loaded.
If the module is already loaded, it works as no-op. If the module was
not compiled yet, `ensure_compiled/1` halts the compilation of the caller
until the module given to `ensure_compiled/1` becomes available or
not compiled yet, `ensure_compiled!/1` halts the compilation of the caller
until the module given to `ensure_compiled!/1` becomes available or
all files for the current project have been compiled. If compilation
finishes and the module is not available, an error tuple is returned.
finishes and the module is not available or is in a deadlock, an error
is raised.
Given this function halts compilation, use it carefully. In particular,
avoid using it to guess which modules are in the system. Overuse of this
@@ -1264,25 +1610,26 @@ defmodule Code do
if the other is compiled. This returns a specific unavailable error code,
where we cannot successfully verify a module is available or not.
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
If the module being checked is currently in a compiler deadlock,
this function returns `{:error, :unavailable}`. Unavailable doesn't
necessarily mean the module doesn't exist, just that it is not currently
available, but it (or may not) become available in the future.
Check `ensure_loaded/1` for more information on module loading
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
See the module documentation for more information on code loading.
"""
@spec ensure_compiled(module) ::
{:module, module}
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
def ensure_compiled(module) when is_atom(module) do
@doc since: "1.12.0"
@spec ensure_compiled!(module) :: module
def ensure_compiled!(module) do
case ensure_compiled(module, :hard) do
{:module, module} ->
module
{:error, reason} ->
raise ArgumentError,
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
end
end
defp ensure_compiled(module, mode) do
case :code.ensure_loaded(module) do
{:error, :nofile} = error ->
if can_await_module_compilation?() do
case Kernel.ErrorHandler.ensure_compiled(module, :module, :soft) do
case Kernel.ErrorHandler.ensure_compiled(module, :module, mode) do
:found -> {:module, module}
:deadlock -> {:error, :unavailable}
:not_found -> {:error, :nofile}
@@ -1326,7 +1673,7 @@ defmodule Code do
file.
It returns the term stored in the documentation chunk in the format defined by
[EEP 48](http://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
[EEP 48](https://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
the chunk is not available.
## Examples
@@ -1348,7 +1695,7 @@ defmodule Code do
| {:error, :module_not_found | :chunk_not_found | {:invalid_chunk, binary}}
when annotation: :erl_anno.anno(),
beam_language: :elixir | :erlang | atom(),
doc_content: %{required(binary) => binary} | :none | :hidden,
doc_content: %{optional(binary) => binary} | :none | :hidden,
doc_element:
{{kind :: atom, function_name :: atom, arity}, annotation, signature, doc_content,
metadata},
@@ -1373,8 +1720,16 @@ defmodule Code do
:error ->
case :code.which(module) do
:preloaded ->
path = Path.join([:code.lib_dir(:erts), "doc", "chunks", "#{module}.chunk"])
fetch_docs_from_chunk(path)
# The erts directory is not necessarily included in releases
# unless it is listed as an extra application.
case :code.lib_dir(:erts) do
path when is_list(path) ->
path = Path.join([path, "doc", "chunks", "#{module}.chunk"])
fetch_docs_from_chunk(path)
{:error, _} ->
{:error, :chunk_not_found}
end
_ ->
{:error, :module_not_found}
@@ -1421,7 +1776,7 @@ defmodule Code do
@doc ~S"""
Deprecated function to retrieve old documentation format.
Elixir v1.7 adopts [EEP 48](http://erlang.org/eep/eeps/eep-0048.html)
Elixir v1.7 adopts [EEP 48](https://erlang.org/eep/eeps/eep-0048.html)
which is a new documentation format meant to be shared across all
BEAM languages. The old format, used by `Code.get_docs/2`, is no
longer available, and therefore this function always returns `nil`.
+120 -113
View File
@@ -12,8 +12,11 @@ defmodule Code.Formatter do
@empty empty()
@ampersand_prec Code.Identifier.unary_op(:&) |> elem(1)
# Operators that are composed of multiple binary operators
@multi_binary_operators [:"..//"]
# Operators that do not have space between operands
@no_space_binary_operators [:..]
@no_space_binary_operators [:.., :"//"]
# Operators that do not have newline between operands (as well as => and keywords)
@no_newline_binary_operators [:\\, :in]
@@ -247,18 +250,6 @@ defmodule Code.Formatter do
defp state(comments, opts) do
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
rename_deprecated_at =
if version = opts[:rename_deprecated_at] do
case Version.parse(version) do
{:ok, parsed} ->
parsed
:error ->
raise ArgumentError,
"invalid version #{inspect(version)} given to :rename_deprecated_at"
end
end
locals_without_parens =
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
@@ -266,7 +257,7 @@ defmodule Code.Formatter do
force_do_end_blocks: force_do_end_blocks,
locals_without_parens: locals_without_parens,
operand_nesting: 2,
rename_deprecated_at: rename_deprecated_at,
skip_eol: false,
comments: comments
}
end
@@ -332,7 +323,7 @@ defmodule Code.Formatter do
{next_eol, comments, doc}
end
# Special AST nodes from compiler feedback.
# Special AST nodes from compiler feedback
defp quoted_to_algebra({{:special, :clause_args}, _meta, [args]}, _context, state) do
{doc, state} = clause_args_to_algebra(args, state)
@@ -403,10 +394,17 @@ defmodule Code.Formatter do
end
# foo[bar]
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target | args]}, _context, state) do
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target, arg]}, _context, state) do
{target_doc, state} = remote_target_to_algebra(target, state)
{call_doc, state} = list_to_algebra(meta, args, state)
{concat(target_doc, call_doc), state}
{access_doc, state} =
if keyword?(arg) do
list_to_algebra(meta, arg, state)
else
list_to_algebra(meta, [arg], state)
end
{concat(target_doc, access_doc), state}
end
# %Foo{}
@@ -521,19 +519,17 @@ defmodule Code.Formatter do
# not(left in right)
# left not in right
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
%{rename_deprecated_at: since} = state
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]}]}, context, state) do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
end
# TODO: Remove metadata and always rewrite to left not in right in Elixir v2.0.
if meta[:operator] == :"not in" || (since && Version.match?(since, "~> 1.5")) do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
else
unary_op_to_algebra(:not, meta, arg, context, state)
end
# 1..2//3
defp quoted_to_algebra({:"..//", meta, [left, middle, right]}, context, state) do
quoted_to_algebra({:"//", meta, [{:.., meta, [left, middle]}, right]}, context, state)
end
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta))
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta, state))
end
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
@@ -565,6 +561,10 @@ defmodule Code.Formatter do
if keyword_key?(left_arg) do
{left, state} =
case left_arg do
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
{:__block__, _, [:"..//"]} ->
{string(~S{"..//":}), state}
{:__block__, _, [atom]} when is_atom(atom) ->
key =
case Code.Identifier.classify(atom) do
@@ -711,7 +711,7 @@ defmodule Code.Formatter do
{operands, max_line} =
unwrap_right(right_arg, op, meta, right_context, [{{:root, left_context}, left_arg}])
operand_to_algebra = fn
fun = fn
{{:root, context}, arg}, _args, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{{doc, @empty, 1}, state}
@@ -723,14 +723,7 @@ defmodule Code.Formatter do
end
{doc, state} =
operand_to_algebra_with_comments(
operands,
meta,
min_line,
max_line,
state,
operand_to_algebra
)
operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun)
if keyword?(right_arg) and context in [:parens_arg, :no_parens_arg] do
{wrap_in_parens(doc), state}
@@ -749,7 +742,7 @@ defmodule Code.Formatter do
{pipes, min_line} =
unwrap_pipes(left_arg, meta, left_context, [{{op, right_context}, right_arg}])
operand_to_algebra = fn
fun = fn
{{:root, context}, arg}, _args, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{{doc, @empty, 1}, state}
@@ -761,7 +754,7 @@ defmodule Code.Formatter do
{{concat(op_string, doc), @empty, 1}, state}
end
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, state, operand_to_algebra)
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, context, state, fun)
end
defp binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, nesting) do
@@ -785,7 +778,7 @@ defmodule Code.Formatter do
concat(concat(group(left), op_string), group(right))
true ->
eol? = eol?(meta)
eol? = eol?(meta, state)
next_break_fits? =
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and not eol?
@@ -889,11 +882,45 @@ defmodule Code.Formatter do
{Enum.reverse(acc), line(meta)}
end
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, state, fun) do
{docs, comments?, state} =
quoted_to_algebra_with_comments(operands, [], min_line, max_line, state, fun)
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun) do
# If we are in a no_parens_one_arg expression, we actually cannot
# extract comments from the first operand, because it would rewrite:
#
# @spec function(x) ::
# # Comment
# any
# when x: any
#
# to:
#
# @spec # Comment
# function(x) ::
# any
# when x: any
#
# Instead we get:
#
# @spec function(x) ::
# any
# # Comment
# when x: any
#
# Which may look counter-intuitive but it actually makes sense,
# as the closest possible location for the comment is the when
# operator.
{operands, acc, state} =
if context == :no_parens_one_arg do
[operand | operands] = operands
{doc_triplet, state} = fun.(operand, :unused, state)
{operands, [doc_triplet], state}
else
{operands, [], state}
end
if comments? or eol?(meta) do
{docs, comments?, state} =
quoted_to_algebra_with_comments(operands, acc, min_line, max_line, state, fun)
if comments? or eol?(meta, state) do
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
else
{docs |> Enum.reduce(&glue(&2, &1)), state}
@@ -958,7 +985,7 @@ defmodule Code.Formatter do
)
when is_atom(fun) and is_integer(arity) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = remote_fun_to_algebra(target, fun, arity, state)
fun = Code.Identifier.inspect_as_function(fun)
{target_doc |> nest(1) |> concat(string(".#{fun}/#{arity}")), state}
end
@@ -1003,7 +1030,7 @@ defmodule Code.Formatter do
defp remote_to_algebra({{:., _, [target, fun]}, meta, args}, context, state)
when is_atom(fun) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = remote_fun_to_algebra(target, fun, length(args), state)
fun = Code.Identifier.inspect_as_function(fun)
remote_doc = target_doc |> concat(".") |> concat(string(fun))
if args == [] and not remote_target_is_a_module?(target) and not meta?(meta, :closing) do
@@ -1039,38 +1066,6 @@ defmodule Code.Formatter do
end
end
defp remote_fun_to_algebra(target, fun, arity, state) do
%{rename_deprecated_at: since} = state
atom_target =
case since && target do
{:__aliases__, _, [alias | _] = aliases} when is_atom(alias) ->
Module.concat(aliases)
{:__block__, _, [atom]} when is_atom(atom) ->
atom
_ ->
nil
end
with {fun, requirement} <- deprecated(atom_target, fun, arity),
true <- Version.match?(since, requirement) do
fun
else
_ -> Code.Identifier.inspect_as_function(fun)
end
end
# We can only rename functions in the same module because
# introducing a new module may be wrong due to aliases.
defp deprecated(Enum, :partition, 2), do: {"split_with", "~> 1.4"}
defp deprecated(Code, :unload_files, 2), do: {"unrequire_files", "~> 1.7"}
defp deprecated(Code, :loaded_files, 2), do: {"required_files", "~> 1.7"}
defp deprecated(Kernel.ParallelCompiler, :files, 2), do: {"compile", "~> 1.6"}
defp deprecated(Kernel.ParallelCompiler, :files_to_path, 2), do: {"compile_to_path", "~> 1.6"}
defp deprecated(_, _, _), do: :error
defp remote_target_to_algebra({:fn, _, [_ | _]} = quoted, state) do
# This change is not semantically required but for beautification.
{doc, state} = quoted_to_algebra(quoted, :no_parens_arg, state)
@@ -1161,7 +1156,7 @@ defmodule Code.Formatter do
end
args = if keyword?, do: left ++ right, else: left ++ [right]
many_eol? = match?([_, _ | _], args) and eol?(meta)
many_eol? = match?([_, _ | _], args) and eol?(meta, state)
no_generators? = no_generators?(args)
to_algebra_fun = &quoted_to_algebra(&1, context, &2)
@@ -1351,9 +1346,8 @@ defmodule Code.Formatter do
end
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{{:., _, [Kernel, :to_string]}, meta, [quoted]} = entry
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
doc = surround("\#{", doc, "}")
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
{doc, state} = interpolation_to_string(quoted, state)
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1368,9 +1362,8 @@ defmodule Code.Formatter do
end
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{:"::", _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
doc = surround("\#{", doc, "}")
{:"::", _, [{{:., _, [Kernel, :to_string]}, _meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = interpolation_to_string(quoted, state)
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1378,6 +1371,23 @@ defmodule Code.Formatter do
{concat(acc, last), state}
end
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
doc = interpolation_to_string(surround("\#{", doc, "}"))
{doc, %{state | skip_eol: skip_eol}}
end
defp interpolation_to_string(doc) do
[head | tail] =
doc
|> format_to_string()
|> String.split("\n")
Enum.reduce(tail, string(head), fn line, acc ->
concat([acc, line(), string(line)])
end)
end
## Sigils
defp maybe_sigil_to_algebra(fun, meta, args, state) do
@@ -1416,7 +1426,7 @@ defmodule Code.Formatter do
defp bitstring_to_algebra(meta, args, state) do
last = length(args) - 1
join = if eol?(meta), do: :line, else: :flex_break
join = if eol?(meta, state), do: :line, else: :flex_break
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
{args_doc, join, state} =
@@ -1484,7 +1494,7 @@ defmodule Code.Formatter do
## Literals
defp list_to_algebra(meta, args, state) do
join = if eol?(meta), do: :line, else: :break
join = if eol?(meta, state), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
@@ -1494,7 +1504,7 @@ defmodule Code.Formatter do
end
defp map_to_algebra(meta, name_doc, [{:|, _, [left, right]}], state) do
join = if eol?(meta), do: :line, else: :break
join = if eol?(meta, state), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{left_doc, state} = fun.(left, state)
@@ -1511,7 +1521,7 @@ defmodule Code.Formatter do
end
defp map_to_algebra(meta, name_doc, args, state) do
join = if eol?(meta), do: :line, else: :break
join = if eol?(meta, state), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
@@ -1522,7 +1532,7 @@ defmodule Code.Formatter do
end
defp tuple_to_algebra(meta, args, join, state) do
join = if eol?(meta), do: :line, else: join
join = if eol?(meta, state), do: :line, else: join
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, join, state} =
@@ -1539,6 +1549,11 @@ defmodule Code.Formatter do
Atom.to_string(atom)
end
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
defp atom_to_algebra(:"..//") do
string(":\"..//\"")
end
defp atom_to_algebra(atom) do
string = Atom.to_string(atom)
@@ -1699,7 +1714,7 @@ defmodule Code.Formatter do
|> glue(body_doc)
|> nest(2)
|> glue("end")
|> maybe_force_clauses(clauses)
|> maybe_force_clauses(clauses, state)
|> group()
{doc, state}
@@ -1733,7 +1748,7 @@ defmodule Code.Formatter do
|> glue(body_doc)
|> nest(2)
|> glue("end")
|> maybe_force_clauses(clauses)
|> maybe_force_clauses(clauses, state)
|> group()
{doc, state}
@@ -1761,7 +1776,7 @@ defmodule Code.Formatter do
"(() -> "
|> concat(nest(body_doc, :cursor))
|> concat(")")
|> maybe_force_clauses(clauses)
|> maybe_force_clauses(clauses, state)
|> group()
{doc, state}
@@ -1782,7 +1797,7 @@ defmodule Code.Formatter do
|> group()
|> concat(break() |> concat(body_doc) |> nest(2))
|> wrap_in_parens()
|> maybe_force_clauses(clauses)
|> maybe_force_clauses(clauses, state)
|> group()
{doc, state}
@@ -1801,8 +1816,8 @@ defmodule Code.Formatter do
## Clauses
defp maybe_force_clauses(doc, clauses) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta) end) do
defp maybe_force_clauses(doc, clauses, state) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
force_unfit(doc)
else
doc
@@ -1825,7 +1840,7 @@ defmodule Code.Formatter do
{doc_acc, state_acc}
end)
{clauses_doc |> maybe_force_clauses([clause | clauses]) |> group(), state}
{clauses_doc |> maybe_force_clauses([clause | clauses], state) |> group(), state}
end
defp clauses_to_algebra(other, min_line, max_line, state) do
@@ -2041,8 +2056,6 @@ defmodule Code.Formatter do
{wrap_in_parens_if_operator(doc, ast), state}
end
# TODO: We can remove this workaround once we remove
# ?rearrange_uop from the parser on v2.0.
defp wrap_in_parens_if_operator(doc, {:__block__, _, [expr]}) do
wrap_in_parens_if_operator(doc, expr)
end
@@ -2098,21 +2111,16 @@ defmodule Code.Formatter do
defp binary_operator?(quoted) do
case quoted do
{op, _, [_, _]} when is_atom(op) ->
Code.Identifier.binary_op(op) != :error
_ ->
false
{op, _, [_, _, _]} when op in @multi_binary_operators -> true
{op, _, [_, _]} when is_atom(op) -> Code.Identifier.binary_op(op) != :error
_ -> false
end
end
defp unary_operator?(quoted) do
case quoted do
{op, _, [_]} when is_atom(op) ->
Code.Identifier.unary_op(op) != :error
_ ->
false
{op, _, [_]} when is_atom(op) -> Code.Identifier.unary_op(op) != :error
_ -> false
end
end
@@ -2173,8 +2181,8 @@ defmodule Code.Formatter do
false
end
defp eol_or_comments?(meta, %{comments: comments}) do
eol?(meta) or
defp eol_or_comments?(meta, %{comments: comments} = state) do
eol?(meta, state) or
(
min_line = line(meta)
max_line = closing_line(meta)
@@ -2261,9 +2269,8 @@ defmodule Code.Formatter do
defp keyword_key?(_),
do: false
defp eol?(meta) do
Keyword.get(meta, :newlines, 0) > 0
end
defp eol?(_meta, %{skip_eol: true}), do: false
defp eol?(meta, _state), do: Keyword.get(meta, :newlines, 0) > 0
defp meta?(meta, key) do
is_list(meta[key])
+15 -13
View File
@@ -37,13 +37,14 @@ defmodule Code.Identifier do
op in [:"::"] -> {:right, 60}
op in [:|] -> {:right, 70}
op in [:=] -> {:right, 100}
op in [:||, :|||, :or] -> {:left, 130}
op in [:&&, :&&&, :and] -> {:left, 140}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
op in [:<, :<=, :>=, :>] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 170}
op in [:in] -> {:left, 180}
op in [:^^^] -> {:left, 190}
op in [:||, :|||, :or] -> {:left, 120}
op in [:&&, :&&&, :and] -> {:left, 130}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
op in [:<, :<=, :>=, :>] -> {:left, 150}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
op in [:in] -> {:left, 170}
op in [:^^^] -> {:left, 180}
op in [:"//"] -> {:right, 190}
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
@@ -68,10 +69,11 @@ defmodule Code.Identifier do
the ambiguity between the atom and the keyword identifier
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
syntax error); this category includes atoms like `:Foo`, since they are
valid identifiers but they need quotes to be used in function calls
(`Foo."Bar"`)
`.` operator. Those are typically AST nodes that are special forms (such as
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
`:..` and `:...`). This category also includes atoms like `:Foo`, since
they are valid identifiers but they need quotes to be used in function
calls (`Foo."Bar"`)
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
@@ -81,10 +83,10 @@ defmodule Code.Identifier do
charlist = Atom.to_charlist(atom)
cond do
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
:not_callable
atom in [:"::"] ->
atom in [:"::", :"//"] ->
:not_atomable
unary_op(atom) != :error or binary_op(atom) != :error ->
+52 -27
View File
@@ -27,8 +27,8 @@ defprotocol Collectable do
## Examples
To show how to manually use the `Collectable` protocol, let's play with its
implementation for `MapSet`.
To show how to manually use the `Collectable` protocol, let's play with a
simplified implementation for `MapSet`.
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
@@ -38,21 +38,33 @@ defprotocol Collectable do
#MapSet<[1, 2, 3]>
To show how the protocol can be implemented, we can again look at the
implementation for `MapSet`. In this implementation "collecting" elements
simplified implementation for `MapSet`. In this implementation "collecting" elements
simply means inserting them in the set through `MapSet.put/2`.
defimpl Collectable, for: MapSet do
def into(original) do
def into(map_set) do
collector_fun = fn
set, {:cont, elem} -> MapSet.put(set, elem)
set, :done -> set
_set, :halt -> :ok
map_set_acc, {:cont, elem} ->
MapSet.put(map_set_acc, elem)
map_set_acc, :done ->
map_set_acc
_map_set_acc, :halt ->
:ok
end
{original, collector_fun}
initial_acc = map_set
{initial_acc, collector_fun}
end
end
So now we can call `Enum.into/2`:
iex> Enum.into([1, 2, 3], MapSet.new())
#MapSet<[1, 2, 3]>
"""
@type command :: {:cont, term} | :done | :halt
@@ -60,8 +72,11 @@ defprotocol Collectable do
@doc """
Returns an initial accumulator and a "collector" function.
The returned function receives a term and a command and injects the term into
the collectable on every `{:cont, term}` command.
Receives a `collectable` which can be used as the initial accumulator that will
be passed to the function.
The collector function receives a term and a command and injects the term into
the collectable accumulator on every `{:cont, term}` command.
`:done` is passed as a command when no further values will be injected. This
is useful when there's a need to close resources or normalizing values. A
@@ -73,13 +88,13 @@ defprotocol Collectable do
For examples on how to use the `Collectable` protocol and `into/1` see the
module documentation.
"""
@spec into(t) :: {term, (term, command -> t | term)}
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
def into(collectable)
end
defimpl Collectable, for: List do
def into(original) do
if original != [] do
def into(list) do
if list != [] do
IO.warn(
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
@@ -90,9 +105,14 @@ defimpl Collectable, for: List do
end
fun = fn
list, {:cont, x} -> [x | list]
list, :done -> original ++ :lists.reverse(list)
_, :halt -> :ok
list_acc, {:cont, elem} ->
[elem | list_acc]
list_acc, :done ->
list ++ :lists.reverse(list_acc)
_list_acc, :halt ->
:ok
end
{[], fun}
@@ -100,7 +120,7 @@ defimpl Collectable, for: List do
end
defimpl Collectable, for: BitString do
def into(original) when is_binary(original) do
def into(binary) when is_binary(binary) do
fun = fn
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
[acc | x]
@@ -117,14 +137,14 @@ defimpl Collectable, for: BitString do
acc, :done ->
IO.iodata_to_binary(acc)
_, :halt ->
__acc, :halt ->
:ok
end
{[original], fun}
{[binary], fun}
end
def into(original) when is_bitstring(original) do
def into(bitstring) do
fun = fn
acc, {:cont, x} when is_bitstring(x) ->
<<acc::bitstring, x::bitstring>>
@@ -132,22 +152,27 @@ defimpl Collectable, for: BitString do
acc, :done ->
acc
_, :halt ->
_acc, :halt ->
:ok
end
{original, fun}
{bitstring, fun}
end
end
defimpl Collectable, for: Map do
def into(original) do
def into(map) do
fun = fn
map, {:cont, {k, v}} -> Map.put(map, k, v)
map, :done -> map
_, :halt -> :ok
map_acc, {:cont, {key, value}} ->
Map.put(map_acc, key, value)
map_acc, :done ->
map_acc
_map_acc, :halt ->
:ok
end
{original, fun}
{map, fun}
end
end
+12 -6
View File
@@ -16,7 +16,8 @@ defmodule Config do
import_config "#{config_env()}.exs"
`import Config` will import the functions `config/2`, `config/3`
and `import_config/1` to help you manage your configuration.
`config_env/0`, `config_target/0`, and `import_config/1`
to help you manage your configuration.
`config/2` and `config/3` are used to define key-value configuration
for a given application. Once Mix starts, it will automatically
@@ -35,8 +36,10 @@ defmodule Config do
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. For more information,
read our [library guidelines](library-guidelines.md).
application environment is effectively a global storage. Also note that
the `config/config.exs` of a library is not evaluated when the library is
used as a dependency, as configuration is always meant to configure the
current project. For more information, read our [library guidelines](library-guidelines.md).
## Migrating from `use Mix.Config`
@@ -64,8 +67,8 @@ defmodule Config do
## config/runtime.exs
For runtime configuration, you can use the `config/runtime.exs` file.
It is executed after your Mix project is compiled and also before a
release (assembled with `mix release`) starts.
It is executed right before applications start in both Mix and releases
(assembled with `mix release`).
"""
@opts_key {__MODULE__, :opts}
@@ -160,7 +163,10 @@ defmodule Config do
end
@doc """
Returns the environemnt this configuration file is executed on.
Returns the environment this configuration file is executed on.
In Mix projects this function returns the environment this configuration
file is executed on. In releases, the environment when `mix release` ran.
This is most often used to execute conditional code:
+43 -11
View File
@@ -11,11 +11,45 @@ defmodule Config.Provider do
the file system. For more information on runtime configuration,
see `mix release`.
## Sample config provider
## Multiple config files
For example, imagine you need to load some configuration from
a JSON file and load that into the system. Said configuration
provider would look like:
One common use of config providers is to specify multiple
configuration files in a release. Elixir ships with one provider,
called `Config.Reader`, which is capable of handling Elixir's
built-in config files.
For example, imagine you want to list some basic configuration
on Mix's built-in `config/runtime.exs` file, but you also want
some additional configuration files. To do so, you can do this
in your `mix.exs`:
releases: [
demo: [
config_providers: [
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
]
]
]
You can place this `extra_config.exs` file in your release in
multiple ways:
1. If it is available on the host when assembling the release,
you can place it on "rel/overlays/extra_config.exs" and it
will be automatically copied to the release root
2. If it is available on the target during deployment, you can
simply copy it to the release root as a step in your deployment
Now once the system boots, it will load both `config/runtime.exs`
and `extra_config.exs` early in the boot process.
## Custom config provider
You can also implement custom config providers, similar to how
`Config.Reader` works. For example, imagine you need to load
some configuration from a JSON file and load that into the system.
Said configuration provider would look like:
defmodule JSONConfigProvider do
@behaviour Config.Provider
@@ -39,19 +73,17 @@ defmodule Config.Provider do
end
end
Then when specifying your release, you can specify the provider in
Then, when specifying your release, you can specify the provider in
the release configuration:
releases: [
demo: [
# ...,
config_providers: [{JSONConfigProvider, "/etc/config.json"}]
config_providers: [
{JSONConfigProvider, "/etc/config.json"}
]
]
]
Now once the system boots, it will invoke the provider early in
the boot process, save the merged configuration to the disk, and
reboot the system with the new values in place.
"""
@type config :: keyword
@@ -93,7 +125,7 @@ defmodule Config.Provider do
Loads configuration (typically during system boot).
It receives the current `config` and the `state` returned by
`c:init/1`. Then you typically read the extra configuration
`c:init/1`. Then, you typically read the extra configuration
from an external source and merge it into the received `config`.
Merging should be done with `Config.Reader.merge/2`, as it
performs deep merge. It should return the updated config.
+1 -1
View File
@@ -16,7 +16,7 @@ defmodule Config.Reader do
Or if you want to read a custom path inside the release:
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
You can also pass a keyword list of options to the reader,
where the `:path` is a required key:
+3 -3
View File
@@ -147,10 +147,10 @@ defmodule DynamicSupervisor do
extra_arguments: [term()]
}
@typedoc "Option values used by the `start*` functions"
@typedoc "Options given to `start_link` functions"
@type option :: GenServer.option()
@typedoc "Options given to `start_link/1` and `init/1`"
@typedoc "Options given to `start_link` and `init/1` functions"
@type init_option ::
{:strategy, strategy()}
| {:max_restarts, non_neg_integer()}
@@ -419,7 +419,7 @@ defmodule DynamicSupervisor do
@doc since: "1.6.0"
@spec which_children(Supervisor.supervisor()) :: [
# module() | :dynamic here because :supervisor.modules() is not exported
{:undefined, pid | :restarting, :worker | :supervisor, module() | :dynamic}
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
]
def which_children(supervisor) do
call(supervisor, :which_children)
+688 -170
View File
File diff suppressed because it is too large Load Diff
+72 -18
View File
@@ -252,8 +252,8 @@ defmodule Exception do
defp rewrite_arg(arg) do
Macro.prewalk(arg, fn
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
{:.., meta, [first, last]}
{:%{}, meta, [__struct__: Range, first: first, last: last, step: step]} ->
{:"..//", meta, [first, last, step]}
other ->
other
@@ -709,7 +709,7 @@ defmodule ArgumentError do
@impl true
def blame(
%{message: "argument error"} = exception,
exception,
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
) do
message =
@@ -783,12 +783,7 @@ defmodule ArithmeticError do
end
defmodule SystemLimitError do
defexception []
@impl true
def message(_) do
"a system limit has been reached"
end
defexception message: "a system limit has been reached"
end
defmodule SyntaxError do
@@ -992,11 +987,23 @@ defmodule UndefinedFunctionError do
@doc false
def hint_for_loaded_module(module, function, arity, exports) do
if macro_exported?(module, function, arity) do
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
else
IO.iodata_to_binary(did_you_mean(module, function, exports))
cond do
macro_exported?(module, function, arity) ->
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
message = otp_obsolete(module, function, arity) ->
", #{message}"
true ->
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
defp otp_obsolete(module, function, arity) do
case :otp_internal.obsolete(module, function, arity) do
{:removed, [_ | _] = string} -> string
_ -> nil
end
end
@@ -1248,6 +1255,10 @@ defmodule KeyError do
end
@impl true
def blame(exception = %{message: message}, stacktrace) when is_binary(message) do
{exception, stacktrace}
end
def blame(exception = %{term: nil}, stacktrace) do
message = message(exception.key, exception.term)
{%{exception | message: message}, stacktrace}
@@ -1405,16 +1416,32 @@ defmodule ErlangError do
end
@doc false
def normalize(:badarg, _stacktrace) do
%ArgumentError{}
def normalize(:badarg, stacktrace) do
case error_info(:badarg, stacktrace) do
{:ok, args} ->
message = "errors were found at the given arguments:\n\n#{args}"
%ArgumentError{message: message}
:error ->
%ArgumentError{}
end
end
def normalize(:badarith, _stacktrace) do
%ArithmeticError{}
end
def normalize(:system_limit, _stacktrace) do
%SystemLimitError{}
def normalize(:system_limit, stacktrace) do
case error_info(:system_limit, stacktrace) do
{:ok, args} ->
message =
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
%SystemLimitError{message: message}
:error ->
%SystemLimitError{}
end
end
def normalize(:cond_clause, _stacktrace) do
@@ -1504,4 +1531,31 @@ defmodule ErlangError do
defp from_stacktrace(_) do
{nil, nil, nil}
end
@doc false
def error_info(erl_exception, stacktrace) do
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
%{} = error_info <- opts[:error_info] do
module = Map.get(error_info, :module, module)
function = Map.get(error_info, :function, :format_error)
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
extra = apply(module, function, [erl_exception, stacktrace])
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
if map_size(args_errors) > 0 do
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
else
:error
end
else
_ -> :error
end
end
defp arg_error({n, message}), do: " * #{nth(n)} argument: #{message}\n"
defp nth(1), do: "1st"
defp nth(2), do: "2nd"
defp nth(3), do: "3rd"
defp nth(n), do: "#{n}th"
end
+19 -8
View File
@@ -110,6 +110,8 @@ defmodule File do
@type stream_mode ::
encoding_mode()
| :append
| :compressed
| :trim_bom
| {:read_ahead, pos_integer | false}
| {:delayed_write, non_neg_integer, non_neg_integer}
@@ -762,15 +764,16 @@ defmodule File do
end
@doc """
Copies the contents in `source_file` to `destination_file` preserving its modes.
Copies the contents of `source_file` to `destination_file` preserving its modes.
`source_file` and `destination_file` must be a file or a symbolic link to one,
or in the case of destination, a path to a non-existent file. If either one of
them is a directory, `{:error, :eisdir}` will be returned.
`source_file` must be a file or a symbolic link to one. `destination_file` must
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
will be returned.
If a file already exists in the destination, it invokes a
callback which should return `true` if the existing file
should be overwritten, `false` otherwise. The callback defaults to return `true`.
The `callback` function is invoked if the `destination_file` already exists.
The function receives arguments for `source_file` and `destination_file`;
it should return `true` if the existing file should be overwritten, `false` if
otherwise. The default callback returns `true`.
The function returns `:ok` in case of success. Otherwise, it returns
`{:error, reason}`.
@@ -1539,6 +1542,12 @@ defmodule File do
executes the given function and then reverts back
to the previous path regardless of whether there is an exception.
The current working directory is temporarily set for the BEAM globally. This
can lead to race conditions if multiple processes are changing the current
working directory concurrently. To run an external command in a given
directory without changing the global current working directory, use the
`:cd` option of `System.cmd/3` and `Port.open/2`.
Raises an error if retrieving or changing the current
directory fails.
"""
@@ -1605,7 +1614,9 @@ defmodule File do
which means it can be used both for read and write.
The `line_or_bytes` argument configures how the file is read when
streaming, by `:line` (default) or by a given number of bytes.
streaming, by `:line` (default) or by a given number of bytes. When
using the `:line` option, CRLF line breaks (`"\r\n"`) are normalized
to LF (`"\n"`).
Operating the stream can fail on open for the same reasons as
`File.open!/2`. Note that the file is automatically opened each time streaming
+96 -40
View File
@@ -46,6 +46,46 @@ defmodule Float do
@precision_range 0..15
@type precision_range :: 0..15
@doc """
Computes `base` raised to power of `exponent`.
`base` must be a float and `exponent` can be any number.
However, if a negative base and a fractional exponent
are given, it raises `ArithmeticError`.
It always returns a float. See `Integer.pow/2` for
exponentiation that returns integers.
## Examples
iex> Float.pow(2.0, 0)
1.0
iex> Float.pow(2.0, 1)
2.0
iex> Float.pow(2.0, 10)
1024.0
iex> Float.pow(2.0, -1)
0.5
iex> Float.pow(2.0, -3)
0.125
iex> Float.pow(3.0, 1.5)
5.196152422706632
iex> Float.pow(-2.0, 3)
-8.0
iex> Float.pow(-2.0, 4)
16.0
iex> Float.pow(-1.0, 0.5)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(float, number) :: float
def pow(base, exponent) when is_float(base) and is_number(exponent),
do: :math.pow(base, exponent)
@doc """
Parses a binary into a float.
@@ -268,11 +308,11 @@ defmodule Float do
raise ArgumentError, invalid_precision_message(precision)
end
defp round(0.0, _precision, _rounding), do: 0.0
defp round(0.0 = num, _precision, _rounding), do: num
defp round(float, precision, rounding) do
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
{num, count, _} = decompose(significant, 1)
{num, count} = decompose(significant, 1)
count = count - exp + 1023
cond do
@@ -324,6 +364,22 @@ defmodule Float do
end
end
defp decompose(significant, initial) do
decompose(significant, 1, 0, initial)
end
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
end
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, last_count, acc)
end
defp decompose(<<>>, _count, last_count, acc) do
{acc, last_count}
end
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
@@ -403,55 +459,55 @@ defmodule Float do
def ratio(0.0), do: {0, 1}
def ratio(float) when is_float(float) do
case <<float::float>> do
<<sign::1, 0::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 0)
{sign(sign, num), shift_left(den, 1022)}
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
<<sign::1, exp::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 1)
num = sign(sign, num)
{num, den_exp} =
if exp != 0 do
# Floats are expressed like this:
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
#
# We compute the root factors of the mantissa so we have this:
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
{mantissa, count} = root_factors(mantissa, 0)
case exp - 1023 do
exp when exp > 0 ->
{den, exp} = shift_right(den, exp)
{shift_left(num, exp), den}
exp when exp < 0 ->
{num, shift_left(den, -exp)}
0 ->
{num, den}
# Now we can move the count around so we have this:
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
if mantissa == 0 do
{1, exp - 1023}
else
num = (1 <<< (52 - count)) + mantissa
den_exp = count - 52 + exp - 1023
{num, den_exp}
end
else
# Subnormals are expressed like this:
# (mantissa) * 2**(-52 + 1 - 1023)
#
# So we compute it to this:
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
#
# Which becomes:
# mantissa * 2**(count-1074)
root_factors(mantissa, -1074)
end
if den_exp > 0 do
{sign(sign, num <<< den_exp), 1}
else
{sign(sign, num), 1 <<< -den_exp}
end
end
defp decompose(significant, initial) do
decompose(significant, 1, 0, 2, 1, initial)
end
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
do: root_factors(mantissa >>> 1, count + 1)
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
end
defp root_factors(mantissa, count),
do: {mantissa, count}
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
end
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
{acc, last_count, last_power}
end
@compile {:inline, sign: 2, shift_left: 2}
@compile {:inline, sign: 2}
defp sign(0, num), do: num
defp sign(1, num), do: -num
defp shift_left(num, times), do: num <<< times
defp shift_right(num, 0), do: {num, 0}
defp shift_right(1, times), do: {1, times}
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
@doc """
Returns a charlist which corresponds to the text representation
of the given float.
+2 -2
View File
@@ -7,7 +7,7 @@ defmodule GenEvent do
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
integrate with an existing system, such as Elixir's Logger, please use
[`:gen_event`](https://erlang.org/doc/man/gen_event.html) instead.
[`:gen_event`](`:gen_event`) instead.
## Alternatives
@@ -38,7 +38,7 @@ defmodule GenEvent do
If your use case requires exactly what GenEvent provided, or you have to
integrate with an existing `:gen_event`-based system, you can still use the
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
[`:gen_event`](`:gen_event`) Erlang module.
"""
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
+20 -33
View File
@@ -61,7 +61,7 @@ defmodule GenServer do
Every time you do a `GenServer.call/3`, the client will send a message
that must be handled by the `c:handle_call/3` callback in the GenServer.
A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
A `cast/2` message must be handled by `c:handle_cast/2`. There are 8 possible
callbacks to be implemented when you use a `GenServer`. The only required
callback is `c:init/1`.
@@ -163,12 +163,12 @@ defmodule GenServer do
using `Process.register/2`.
* `{:global, term}` - the GenServer is registered globally with the given
term using the functions in the [`:global` module](http://www.erlang.org/doc/man/global.html).
term using the functions in the [`:global` module](`:global`).
* `{:via, module, term}` - the GenServer is registered with the given
mechanism and name. The `:via` option expects a module that exports
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
One such example is the [`:global` module](http://www.erlang.org/doc/man/global.html) which uses these functions
One such example is the [`:global` module](`:global`) which uses these functions
for keeping the list of names of processes and their associated PIDs
that are available globally for a network of Elixir nodes. Elixir also
ships with a local, decentralized and scalable registry called `Registry`
@@ -242,7 +242,8 @@ defmodule GenServer do
end
defp schedule_work do
# In 2 hours
# We schedule the work to happen in 2 hours (written in milliseconds).
# Alternatively, one might write :timer.hours(2)
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
end
end
@@ -311,14 +312,14 @@ defmodule GenServer do
## Debugging with the :sys module
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](http://www.erlang.org/doc/man/sys.html).
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](`:sys`).
Through various hooks, this module allows developers to introspect the state of
the process and trace system events that happen during its execution, such as
received messages, sent replies and state changes.
Let's explore the basic functions from the
[`:sys` module](http://www.erlang.org/doc/man/sys.html) used for debugging:
[`:sys` module](`:sys`) used for debugging:
* `:sys.get_state/2` - allows retrieval of the state of the process.
In the case of a GenServer process, it will be the callback module state,
@@ -400,8 +401,8 @@ defmodule GenServer do
in Erlang can also provide extra insight.
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
* [gen_server Behaviour - OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
* [`:gen_server` module documentation](`:gen_server`)
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.html)
* [Clients and Servers - Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
"""
@@ -424,7 +425,7 @@ defmodule GenServer do
`c:handle_call/3` for more information on hibernation.
Returning `{:ok, state, {:continue, continue}}` is similar to
`{:ok, state}` except that immediately after entering the loop
`{:ok, state}` except that immediately after entering the loop,
the `c:handle_continue/2` callback will be invoked with the value
`continue` as first argument.
@@ -471,8 +472,8 @@ defmodule GenServer do
Returning `{:reply, reply, new_state, :hibernate}` is similar to
`{:reply, reply, new_state}` except the process is hibernated and will
continue the loop once a message is in its message queue. If a message is
already in the message queue this will be immediately. Hibernating a
continue the loop once a message is in its message queue. However, if a message is
already in the message queue, the process will continue the loop immediately. Hibernating a
`GenServer` causes garbage collection and leaves a continuous heap that
minimises the memory used by the process.
@@ -506,7 +507,7 @@ defmodule GenServer do
occurs as with a `:reply` tuple.
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
is called with reason `reason` and state `new_state`. Then the `reply` is sent
is called with reason `reason` and state `new_state`. Then, the `reply` is sent
as the response to call and the process exits with reason `reason`.
Returning `{:stop, reason, new_state}` is similar to
@@ -584,8 +585,6 @@ defmodule GenServer do
This callback is optional. If one is not implemented, the server will fail
if a continue instruction is used.
This callback is only supported on Erlang/OTP 21+.
"""
@callback handle_continue(continue :: term, state :: term) ::
{:noreply, new_state}
@@ -599,15 +598,13 @@ defmodule GenServer do
`reason` is exit reason and `state` is the current state of the `GenServer`.
The return value is ignored.
`c:terminate/2` is called if a callback (except `c:init/1`) does one of the
following:
`c:terminate/2` is called if the `GenServer` traps exits (using `Process.flag/2`)
*and* the parent process sends an exit signal, or a callback (except `c:init/1`)
does one of the following:
* returns a `:stop` tuple
* raises
* calls `Kernel.exit/1`
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
* returns an invalid value
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
process sends an exit signal
If part of a supervision tree, a `GenServer` will receive an exit
signal when the tree is shutting down. The exit signal is based on
@@ -714,7 +711,7 @@ defmodule GenServer do
{:debug, debug}
| {:name, name}
| {:timeout, timeout}
| {:spawn_opt, Process.spawn_opt()}
| {:spawn_opt, [Process.spawn_opt()]}
| {:hibernate_after, timeout}
@typedoc "Debug options supported by the `start*` functions"
@@ -893,7 +890,7 @@ defmodule GenServer do
milliseconds initializing or it will be terminated and the start function
will return `{:error, :timeout}`
* `:debug` - if present, the corresponding function in the [`:sys` module](http://www.erlang.org/doc/man/sys.html) is invoked
* `:debug` - if present, the corresponding function in the [`:sys` module](`:sys`) is invoked
* `:spawn_opt` - if present, its value is passed as options to the
underlying process as in `Process.spawn/4`
@@ -1039,18 +1036,8 @@ defmodule GenServer do
is unknown whether the destination `server` successfully
handled the message.
`c:handle_cast/2` will be called on the server to handle
the request. In case the `server` is on a node which is
not yet connected to the caller one, the semantics differ
depending on the used Erlang/OTP version.
`server` can be any of the values described in the "Name registration"
section of the documentation for this module.
Before Erlang/OTP 21, the call is going to block until a
connection happens. This was done to guarantee ordering.
Starting with Erlang/OTP 21, both Erlang and Elixir do
not block the call.
"""
@spec cast(server, term) :: :ok
def cast(server, request)
+9 -8
View File
@@ -26,8 +26,8 @@ defprotocol Inspect do
defimpl Inspect, for: MapSet do
import Inspect.Algebra
def inspect(dict, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"])
def inspect(map_set, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
end
end
@@ -454,16 +454,17 @@ defimpl Inspect, for: Any do
end
def inspect(map, name, opts) do
# Use the :limit option and an extra element to force
# `container_doc/6` to append "...".
opts = %{opts | limit: min(opts.limit, map_size(map))}
map = Map.to_list(map) ++ ["..."]
map = Map.to_list(map) ++ [:...]
open = color("#" <> name <> "<", :map, opts)
sep = color(",", :map, opts)
close = color(">", :map, opts)
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
fun = fn
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
:..., _opts -> "..."
end
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
end
end
+2 -2
View File
@@ -894,12 +894,12 @@ defmodule Inspect.Algebra do
format(width, 0, [{0, :flat, doc}])
end
# Type representing the document mode to be rendered
# Type representing the document mode to be rendered:
#
# * flat - represents a document with breaks as flats (a break may fit, as it may break)
# * break - represents a document with breaks as breaks (a break always fits, since it breaks)
#
# The following modes are exclusive to fitting
# The following modes are exclusive to fitting:
#
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
+113 -42
View File
@@ -64,6 +64,55 @@ defmodule Integer do
"""
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
@doc """
Computes `base` raised to power of `exponent`.
Both `base` and `exponent` must be integers.
The exponent must be zero or positive.
See `Float.pow/2` for exponentiation of negative
exponents as well as floats.
## Examples
iex> Integer.pow(2, 0)
1
iex> Integer.pow(2, 1)
2
iex> Integer.pow(2, 10)
1024
iex> Integer.pow(2, 11)
2048
iex> Integer.pow(2, 64)
0x10000000000000000
iex> Integer.pow(3, 4)
81
iex> Integer.pow(4, 3)
64
iex> Integer.pow(-2, 3)
-8
iex> Integer.pow(-2, 4)
16
iex> Integer.pow(2, -2)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(integer, non_neg_integer) :: integer
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
guarded_pow(base, exponent)
end
# https://en.wikipedia.org/wiki/Exponentiation_by_squaring
defp guarded_pow(_, 0), do: 1
defp guarded_pow(b, 1), do: b
defp guarded_pow(b, e) when (e &&& 1) == 0, do: guarded_pow(b * b, e >>> 1)
defp guarded_pow(b, e), do: b * guarded_pow(b * b, e >>> 1)
@doc """
Computes the modulo remainder of an integer division.
@@ -226,7 +275,7 @@ defmodule Integer do
** (ArgumentError) invalid base 38
"""
@spec parse(binary, 2..36) :: {integer, binary} | :error
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
def parse(binary, base \\ 10)
def parse(_binary, base) when base not in 2..36 do
@@ -269,12 +318,12 @@ defmodule Integer do
defp count_digits_nosign(<<_::bits>>, _, count), do: count
# TODO: Remove Integer.to_string/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_string/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a binary which corresponds to the text representation
of `integer`.
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
Inlined by the compiler.
@@ -292,22 +341,6 @@ defmodule Integer do
iex> Integer.to_string(0123)
"123"
"""
@spec to_string(integer) :: String.t()
def to_string(integer) do
:erlang.integer_to_binary(integer)
end
@doc """
Returns a binary which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_string(100, 16)
"64"
@@ -319,15 +352,16 @@ defmodule Integer do
"""
@spec to_string(integer, 2..36) :: String.t()
def to_string(integer, base) do
def to_string(integer, base \\ 10) do
:erlang.integer_to_binary(integer, base)
end
# TODO: Remove Integer.to_charlist/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_charlist/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a charlist which corresponds to the text representation of the given `integer`.
Returns a charlist which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
Inlined by the compiler.
@@ -345,21 +379,6 @@ defmodule Integer do
iex> Integer.to_charlist(0123)
'123'
"""
@spec to_charlist(integer) :: charlist
def to_charlist(integer) do
:erlang.integer_to_list(integer)
end
@doc """
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_charlist(100, 16)
'64'
@@ -371,7 +390,7 @@ defmodule Integer do
"""
@spec to_charlist(integer, 2..36) :: charlist
def to_charlist(integer, base) do
def to_charlist(integer, base \\ 10) do
:erlang.integer_to_list(integer, base)
end
@@ -414,6 +433,58 @@ defmodule Integer do
defp gcd_positive(integer1, 0), do: integer1
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
@doc """
Returns the extended greatest common divisor of the two given integers.
It uses the Extended Euclidean algorithm to return a three-element tuple with the `gcd`
and the coefficients `m` and `n` of Bézout's identity such that:
gcd(a, b) = m*a + n*b
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
## Examples
iex> Integer.extended_gcd(240, 46)
{2, -9, 47}
iex> Integer.extended_gcd(46, 240)
{2, 47, -9}
iex> Integer.extended_gcd(-46, 240)
{2, -47, -9}
iex> Integer.extended_gcd(-46, -240)
{2, -47, 9}
iex> Integer.extended_gcd(14, 21)
{7, -1, 1}
iex> Integer.extended_gcd(10, 0)
{10, 1, 0}
iex> Integer.extended_gcd(0, 10)
{10, 0, 1}
iex> Integer.extended_gcd(0, 0)
{0, 0, 0}
"""
@doc since: "1.12.0"
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
def extended_gcd(0, 0), do: {0, 0, 0}
def extended_gcd(0, n), do: {n, 0, 1}
def extended_gcd(n, 0), do: {n, 1, 0}
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
extended_gcd(integer2, integer1, 0, 1, 1, 0)
end
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
div = div(r0, r1)
case r0 - div * r1 do
0 when r1 > 0 -> {r1, s1, t1}
0 when r1 < 0 -> {-r1, -s1, -t1}
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
end
end
@doc false
@deprecated "Use Integer.to_charlist/1 instead"
def to_char_list(integer), do: Integer.to_charlist(integer)
+35 -9
View File
@@ -34,7 +34,7 @@ defmodule IO do
IO data is a data type that can be used as a more efficient alternative to binaries
in certain situations.
A term of type **IO data** is a binary or a list containing bytes (integers in `0..255`)
A term of type **IO data** is a binary or a list containing bytes (integers within the `0..255` range)
or nested IO data. The type is recursive. Let's see an example of one of
the possible IO data representing the binary `"hello"`:
@@ -99,8 +99,8 @@ defmodule IO do
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
similar to IO data: the only difference is that integers in IO data represent
bytes while integers in chardata represent Unicode code points. Bytes
(`t:byte/0`) are integers in the `0..255` range, while Unicode code points
(`t:char/0`) are integers in the range `0..0x10FFFF`. The `IO` module provides
(`t:byte/0`) are integers within the `0..255` range, while Unicode code points
(`t:char/0`) are integers within the `0..0x10FFFF` range. The `IO` module provides
the `chardata_to_string/1` function for chardata as the "counter-part" of the
`iodata_to_binary/1` function for IO data.
@@ -108,8 +108,8 @@ defmodule IO do
argument error. For example, let's try to put a code point that is not
representable with one byte, like `?π`, inside IO data:
iex> IO.iodata_to_binary(["The symbol for pi is: ", ?π])
** (ArgumentError) argument error
IO.iodata_to_binary(["The symbol for pi is: ", ?π])
#=> ** (ArgumentError) argument error
If we use chardata instead, it will work as expected:
@@ -508,6 +508,17 @@ defmodule IO do
:io.get_line(map_dev(device), to_chardata(prompt))
end
@doc """
Returns a line-based `IO.Stream` on `:stdio`.
This is equivalent to:
IO.stream(:stdio, :line)
"""
@doc since: "1.12.0"
def stream, do: stream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`.
@@ -524,6 +535,9 @@ defmodule IO do
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
`stream/1` has been introduced in Elixir v1.12.0,
while `stream/2` has been available since v1.0.0.
## Examples
Here is an example on how we mimic an echo server
@@ -533,12 +547,23 @@ defmodule IO do
"""
@spec stream(device, :line | pos_integer) :: Enumerable.t()
def stream(device, line_or_codepoints)
def stream(device \\ :stdio, line_or_codepoints)
when line_or_codepoints == :line
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
end
@doc """
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
This is equivalent to:
IO.binstream(:stdio, :line)
"""
@doc since: "1.12.0"
def binstream, do: binstream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
@@ -547,8 +572,7 @@ defmodule IO do
and write.
The `device` is iterated by the given number of bytes or line by line if
`:line` is given.
This reads from the IO device as a raw binary.
`:line` is given. This reads from the IO device as a raw binary.
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
@@ -556,9 +580,11 @@ defmodule IO do
Finally, do not use this function on IO devices in Unicode
mode as it will return the wrong result.
`binstream/1` has been introduced in Elixir v1.12.0,
while `binstream/2` has been available since v1.0.0.
"""
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
def binstream(device, line_or_bytes)
def binstream(device \\ :stdio, line_or_bytes)
when line_or_bytes == :line
when is_integer(line_or_bytes) and line_or_bytes > 0 do
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
+1 -1
View File
@@ -242,7 +242,7 @@ defmodule IO.ANSI do
performed. If you don't want this behaviour, use `format_fragment/2`.
An optional boolean parameter can be passed to enable or disable
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
By default checks if ANSI is enabled using the `enabled?/0` function.
## Examples
+21 -6
View File
@@ -1,7 +1,8 @@
defmodule IO.ANSI.Docs do
@moduledoc false
@bullet_text "• "
@bullet_text_unicode "• "
@bullet_text_ascii "* "
@bullets [?*, ?-, ?+]
@spaces [" ", "\n", "\t"]
@@ -219,7 +220,10 @@ defmodule IO.ANSI.Docs do
end
defp traverse_erlang_html({:dt, _, entries}, indent, options) do
["#{indent} ", @bullet_text | handle_erlang_html_text(entries, indent <> " ", options)]
[
"#{indent} ",
bullet_text(options) | handle_erlang_html_text(entries, indent <> " ", options)
]
end
defp traverse_erlang_html({:dd, _, entries}, indent, options) do
@@ -231,12 +235,19 @@ defmodule IO.ANSI.Docs do
types =
for {:li, _, lines} <- entries,
line <- lines,
do: ["#{indent} ", line, ?\n]
do: ["#{indent} ", traverse_erlang_html(line, indent <> " ", options), ?\n]
["#{indent}Typespecs:\n\n", types, ?\n]
if types != [] do
["#{indent}Typespecs:\n\n", types, ?\n]
else
[]
end
else
for {:li, _, lines} <- entries do
["#{indent} ", @bullet_text | handle_erlang_html_text(lines, indent <> " ", options)]
[
"#{indent} ",
bullet_text(options) | handle_erlang_html_text(lines, indent <> " ", options)
]
end
end
end
@@ -420,7 +431,7 @@ defmodule IO.ANSI.Docs do
case stripped do
<<bullet, ?\s, item::binary>> when bullet in @bullets ->
write_text(text, indent, options)
process_list(@bullet_text, item, rest, count, indent, options)
process_list(bullet_text(options), item, rest, count, indent, options)
<<d1, ?., ?\s, item::binary>> when d1 in ?0..?9 ->
write_text(text, indent, options)
@@ -926,6 +937,10 @@ defmodule IO.ANSI.Docs do
end
end
defp bullet_text(options) do
if options[:enabled], do: @bullet_text_unicode, else: @bullet_text_ascii
end
defp color(style, colors) do
IO.ANSI.format_fragment(colors[style], colors[:enabled])
end
+527 -178
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -173,10 +173,11 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
directives =
state.directives
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|> Map.new()
|> Map.drop(to_remove)
|> add_directive(module, line, warn, :import)
directives =
+47 -20
View File
@@ -90,6 +90,11 @@ defmodule Kernel.ParallelCompiler do
spawn_workers(files, :compile, options)
end
@doc """
Compiles the given files and writes resulting BEAM files into path.
See `compile/2` for more information.
"""
@doc since: "1.6.0"
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
spawn_workers(files, {:compile, path}, options)
@@ -375,24 +380,31 @@ defmodule Kernel.ParallelCompiler do
# There is potentially a deadlock. We will release modules with
# the following order:
#
# 1. Code.ensure_compiled/1 checks (deadlock = soft)
# 2. Struct checks (deadlock = hard)
# 3. Modules without a known definition
# 4. Code invocation (deadlock = raise)
# 1. Code.ensure_compiled/1 checks without a known definition (deadlock = soft)
# 2. Code.ensure_compiled/1 checks with a known definition (deadlock = soft)
# 3. Struct/import/require/ensure_compiled! checks without a known definition (deadlock = hard)
# 4. Modules without a known definition
# 5. Code invocation (deadlock = raise)
#
# In theory there is no difference between hard and raise, the
# The reason for step 3 and 4 is to not treat typos as deadlocks and
# help developers handle those sooner. However, this can have false
# positives in case multiple modules are defined in the same file
# and the module we are waiting for is defined later on.
#
# Finally, note there is no difference between hard and raise, the
# difference is where the raise is happening, inside the compiler
# or in the caller.
cond do
deadlocked = deadlocked(waiting, :soft) || deadlocked(waiting, :hard) ->
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
without_definition = without_definition(waiting, files) ->
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
deadlocked =
deadlocked(waiting, :soft, false) ||
deadlocked(waiting, :soft, true) || deadlocked(waiting, :hard, false) ||
without_definition(waiting, files)
true ->
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
if deadlocked do
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
else
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
end
end
@@ -452,13 +464,21 @@ defmodule Kernel.ParallelCompiler do
nillify_empty(
for %{pid: pid} <- files,
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
not defining?(on, waiting),
do: {ref, :not_found}
)
end
defp deadlocked(waiting, type) do
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
defp deadlocked(waiting, type, defining?) do
nillify_empty(
for {_, _, ref, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
)
end
defp defining?(on, waiting) do
Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end)
end
defp nillify_empty([]), do: nil
@@ -603,13 +623,13 @@ defmodule Kernel.ParallelCompiler do
data = maybe_warn_long_compilation(data, state)
if state.profile != :none do
compiling = System.convert_time_unit(data.compiling, :native, :millisecond)
waiting = System.convert_time_unit(data.waiting, :native, :millisecond)
extra = if waiting > 0, do: " (plus #{waiting}ms waiting)", else: ""
compiling = to_padded_ms(data.compiling)
waiting = to_padded_ms(data.waiting)
relative = Path.relative_to_cwd(data.file)
IO.puts(
:stderr,
"[profile] #{Path.relative_to_cwd(data.file)} compiled in #{compiling}ms" <> extra
"[profile] #{compiling}ms compiling + #{waiting}ms waiting for #{relative}"
)
end
@@ -620,6 +640,13 @@ defmodule Kernel.ParallelCompiler do
end)
end
defp to_padded_ms(time) do
time
|> System.convert_time_unit(:native, :millisecond)
|> Integer.to_string()
|> String.pad_leading(6, " ")
end
defp discard_down(pid) do
receive do
{:DOWN, _, :process, ^pid, _} -> :ok
+46 -22
View File
@@ -201,7 +201,7 @@ defmodule Kernel.SpecialForms do
iex> <<"foo"::utf32>>
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
Otherwise we get an `ArgumentError` when construcing the binary:
Otherwise we get an `ArgumentError` when constructing the binary:
rest = "oo"
<<102, rest>>
@@ -233,9 +233,9 @@ defmodule Kernel.SpecialForms do
Sizes for types are a bit more nuanced. The default size for integers is 8.
For floats, it is 64. For floats, `size * unit` must result in 32 or 64,
For floats, it is 64. For floats, `size * unit` must result in 16, 32, or 64,
corresponding to [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point)
binary32 and binary64, respectively.
binary16, binary32, and binary64, respectively.
For binaries, the default is the size of the binary. Only the last binary in a
match can use the default size. All others must have their size specified
@@ -365,8 +365,8 @@ defmodule Kernel.SpecialForms do
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
To learn more about specific optimizations and performance considerations,
check out
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
check out the
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
"""
defmacro unquote(:<<>>)(args), do: error!([args])
@@ -617,9 +617,11 @@ defmodule Kernel.SpecialForms do
import List, only: [flatten: 1]
import String, except: [split: 2]
Note that calling `except` is always exclusive on a previously
declared `import/2`. If there is no previous import, then it applies
to all functions and macros in the module. For example:
Importing the same module again will erase the previous imports,
except when the `except` option is used, which is always exclusive
on a previously declared `import/2`. If there is no previous import,
then it applies to all functions and macros in the module. For
example:
import List, only: [flatten: 1, keyfind: 4]
import List, except: [flatten: 1]
@@ -1414,7 +1416,7 @@ defmodule Kernel.SpecialForms do
The `IO` module provides streams, that are both `Enumerable` and
`Collectable`, here is an upcase echo server using comprehensions:
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
for line <- IO.stream(), into: IO.stream() do
String.upcase(line)
end
@@ -1548,9 +1550,16 @@ defmodule Kernel.SpecialForms do
...> else
...> :error ->
...> {:error, :wrong_data}
...>
...> _other_error ->
...> :unexpected_error
...> end
{:error, :wrong_data}
The `else` block works like a `case` clause: it can have multiple clauses,
and the first match will be used. Variables bound inside `with` (such as
`width` in this example) are not available in the `else` block.
If an `else` block is used and there are no matching clauses, a `WithClauseError`
exception is raised.
"""
@@ -1598,7 +1607,7 @@ defmodule Kernel.SpecialForms do
defmacro unquote(:__block__)(args), do: error!([args])
@doc """
Caputure operator. Captures or creates an anonymous function.
Capture operator. Captures or creates an anonymous function.
## Capture
@@ -1721,19 +1730,21 @@ defmodule Kernel.SpecialForms do
## Examples
case thing do
{:selector, i, value} when is_integer(i) ->
value
value ->
value
case File.read(file) do
{:ok, contents} when is_binary(contents) ->
String.split(contents, "\n")
{:error, _reason} ->
Logger.warning "could not find #{file}, assuming empty..."
[]
end
In the example above, we match `thing` against each clause "head"
and execute the clause "body" corresponding to the first clause
that matches.
In the example above, we match the result of `File.read/1`
against each clause "head" and execute the clause "body"
corresponding to the first clause that matches.
If no clause matches, an error is raised.
For this reason, it may be necessary to add a final catch-all clause (like `_`)
If no clause matches, an error is raised. For this reason,
it may be necessary to add a final catch-all clause (like `_`)
which will always match.
x = 10
@@ -1741,6 +1752,7 @@ defmodule Kernel.SpecialForms do
case x do
0 ->
"This clause won't match"
_ ->
"This clause would match any value (x = #{x})"
end
@@ -1758,8 +1770,7 @@ defmodule Kernel.SpecialForms do
value
#=> unbound variable value
When binding variables with the same names as variables in the outer context,
the variables in the outer context are not affected.
Variables in the outer context cannot be overridden either:
value = 7
@@ -1786,6 +1797,19 @@ defmodule Kernel.SpecialForms do
end
#=> "Will match"
## Using guards to match against multiple values
While it is not possible to match against multiple patterns in a single
clause, it's possible to match against multiple values by using guards:
case data do
value when value in [:one, :two] ->
"#{value} has been matched"
:three ->
"three has been matched"
end
"""
defmacro case(condition, clauses), do: error!([condition, clauses])
+28 -11
View File
@@ -23,9 +23,8 @@ defmodule Kernel.Typespec do
for {{:type, name, arity}, _, _, doc, _} <- docs do
case doc do
%{"en" => doc_string} -> {{name, arity}, doc_string}
:none -> {{name, arity}, nil}
# Hidden or unknown format are ignored
_ -> {{name, arity}, false}
:hidden -> {{name, arity}, false}
_ -> {{name, arity}, nil}
end
end
@@ -644,10 +643,7 @@ defmodule Kernel.Typespec do
end
defp typespec({:__aliases__, _, _} = alias, vars, caller, state) do
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
typespec(atom, vars, caller, state)
typespec(expand_remote(alias, caller), vars, caller, state)
end
# Handle funs
@@ -735,9 +731,7 @@ defmodule Kernel.Typespec do
# Handle remote calls
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller, state) do
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
remote = expand_remote(remote, caller)
cond do
not is_atom(remote) ->
@@ -908,6 +902,25 @@ defmodule Kernel.Typespec do
## Helpers
# This is a backport of Macro.expand/2 because we want to expand
# aliases but we don't them to become compile-time references.
defp expand_remote({:__aliases__, _, _} = alias, env) do
case :elixir_aliases.expand(alias, env) do
receiver when is_atom(receiver) ->
receiver
aliases ->
aliases = :lists.map(&Macro.expand_once(&1, env), aliases)
case :lists.all(&is_atom/1, aliases) do
true -> :elixir_aliases.concat(aliases)
false -> alias
end
end
end
defp expand_remote(other, env), do: Macro.expand(other, env)
defp compile_error(caller, desc) do
raise CompileError, file: caller.file, line: caller.line, description: desc
end
@@ -1009,7 +1022,11 @@ defmodule Kernel.Typespec do
:elixir_errors.erl_warn(caller.line, caller.file, warning)
{_, :used_once} ->
compile_error(caller, "type variable #{name} is unused")
compile_error(
caller,
"type variable #{name} is used only once. Type variables in typespecs " <>
"must be referenced at least twice, otherwise it is equivalent to term()"
)
_ ->
:ok
+11 -2
View File
@@ -108,7 +108,16 @@ defmodule Kernel.Utils do
:lists.foreach(foreach, enforce_keys)
struct = :maps.put(:__struct__, module, :maps.from_list(fields))
{struct, enforce_keys, Module.get_attribute(module, :derive)}
case enforce_keys -- :maps.keys(struct) do
[] ->
{struct, enforce_keys, Module.get_attribute(module, :derive)}
error_keys ->
raise ArgumentError,
"@enforce_keys required keys (#{inspect(error_keys)}) that are not defined in defstruct: " <>
"#{inspect(fields)}"
end
end
defp warn_on_duplicate_struct_key([]) do
@@ -258,7 +267,7 @@ defmodule Kernel.Utils do
{new_var, acc}
%{} ->
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc)))
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
new_var = Macro.var(generated, Elixir)
{new_var, Map.put(acc, pair, {new_var, var})}
end
+36 -18
View File
@@ -287,8 +287,8 @@ defmodule Keyword do
{nil, [a: 1]}
"""
@spec get_and_update(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, value}
@spec get_and_update(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
when current_value: value
def get_and_update(keywords, key, fun)
when is_list(keywords) and is_atom(key),
@@ -351,8 +351,8 @@ defmodule Keyword do
{1, []}
"""
@spec get_and_update!(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, t}
@spec get_and_update!(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
when current_value: value
def get_and_update!(keywords, key, fun) do
get_and_update!(keywords, key, fun, [])
@@ -668,10 +668,19 @@ defmodule Keyword do
@doc since: "1.11.0"
@spec replace(t, key, value) :: t
def replace(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, _} -> [{key, value} | delete(keywords, key)]
false -> keywords
end
do_replace(keywords, key, value)
end
defp do_replace([{key, _} | keywords], key, value) do
[{key, value} | delete(keywords, key)]
end
defp do_replace([{_, _} = e | keywords], key, value) do
[e | do_replace(keywords, key, value)]
end
defp do_replace([], _key, _value) do
[]
end
@doc """
@@ -704,7 +713,7 @@ defmodule Keyword do
[e | replace!(keywords, key, value, original)]
end
defp replace!([], key, _value, original) when is_atom(key) do
defp replace!([], key, _value, original) do
raise(KeyError, key: key, term: original)
end
@@ -723,10 +732,16 @@ defmodule Keyword do
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
true
Comparison between values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Keyword.equal?([a: 1.0], [a: 1])
false
"""
@spec equal?(t, t) :: boolean
def equal?(left, right) when is_list(left) and is_list(right) do
:lists.sort(left) == :lists.sort(right)
:lists.sort(left) === :lists.sort(right)
end
@doc """
@@ -884,11 +899,11 @@ defmodule Keyword do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update!([{_, _} = e | keywords], key, fun, original) do
[e | update!(keywords, key, fun, original)]
defp update!([{_, _} = pair | keywords], key, fun, original) do
[pair | update!(keywords, key, fun, original)]
end
defp update!([], key, _fun, original) when is_atom(key) do
defp update!([], key, _fun, original) do
raise(KeyError, key: key, term: original)
end
@@ -914,18 +929,21 @@ defmodule Keyword do
[a: 1, b: 11]
"""
@spec update(t, key, default :: value, (existing_value :: value -> updated_value :: value)) :: t
@spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
def update(keywords, key, default, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
update_guarded(keywords, key, default, fun)
end
def update([{key, value} | keywords], key, _default, fun) do
defp update_guarded([{key, value} | keywords], key, _default, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
def update([{_, _} = e | keywords], key, default, fun) do
[e | update(keywords, key, default, fun)]
defp update_guarded([{_, _} = pair | keywords], key, default, fun) do
[pair | update_guarded(keywords, key, default, fun)]
end
def update([], key, default, _fun) when is_atom(key) do
defp update_guarded([], key, default, _fun) do
[{key, default}]
end
+33 -16
View File
@@ -15,6 +15,13 @@ defmodule List do
iex> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
An element can be prepended to a list using `|`:
iex> new = 0
iex> list = [1, 2, 3]
iex> [new | list]
[0, 1, 2, 3]
Lists in Elixir are effectively linked lists, which means
they are internally represented in pairs containing the
head and the tail of a list:
@@ -255,13 +262,18 @@ defmodule List do
end
@doc """
Returns the first element in `list` or `nil` if `list` is empty.
Returns the first element in `list` or `default` if `list` is empty.
`first/2` has been introduced in Elixir v1.12.0, while `first/1` has been available since v1.0.0.
## Examples
iex> List.first([])
nil
iex> List.first([], 1)
1
iex> List.first([1])
1
@@ -269,19 +281,25 @@ defmodule List do
1
"""
@spec first([]) :: nil
@spec first([elem, ...]) :: elem when elem: var
def first([]), do: nil
def first([head | _]), do: head
@spec first([], any) :: any
@spec first([elem, ...], any) :: elem when elem: var
def first(list, default \\ nil)
def first([], default), do: default
def first([head | _], _default), do: head
@doc """
Returns the last element in `list` or `nil` if `list` is empty.
Returns the last element in `list` or `default` if `list` is empty.
`last/2` has been introduced in Elixir v1.12.0, while `last/1` has been available since v1.0.0.
## Examples
iex> List.last([])
nil
iex> List.last([], 1)
1
iex> List.last([1])
1
@@ -289,11 +307,13 @@ defmodule List do
3
"""
@spec last([]) :: nil
@spec last([elem, ...]) :: elem when elem: var
def last([]), do: nil
def last([head]), do: head
def last([_ | tail]), do: last(tail)
@spec last([], any) :: any
@spec last([elem, ...], any) :: elem when elem: var
@compile {:inline, last: 2}
def last(list, default \\ nil)
def last([], default), do: default
def last([head], _default), do: head
def last([_ | tail], default), do: last(tail, default)
@doc """
Receives a list of tuples and returns the first tuple
@@ -811,9 +831,6 @@ defmodule List do
iex> List.to_existing_atom('🌢 Elixir')
:"🌢 Elixir"
iex> List.to_existing_atom('this_atom_will_never_exist')
** (ArgumentError) argument error
"""
@spec to_existing_atom(charlist) :: atom
def to_existing_atom(charlist) do
@@ -899,7 +916,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
the [`:binary` module](`:binary`).
## Examples
@@ -954,7 +971,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
the [`:binary` module](`:binary`).
## Examples
+102 -35
View File
@@ -270,7 +270,7 @@ defmodule Macro do
def pipe(expr, {:fn, _, _}, _integer) do
raise ArgumentError,
"cannot pipe #{to_string(expr)} into an anonymous function without" <>
" calling the function; use something like (fn ... end).() or" <>
" calling the function; use Kernel.then/2 instead or" <>
" define the anonymous function as a regular private function"
end
@@ -337,8 +337,13 @@ defmodule Macro do
end
@doc """
Generates AST nodes for a given number of required argument variables using
`Macro.var/2`.
Generates AST nodes for a given number of required argument
variables using `Macro.var/2`.
Note the arguments are not unique. If you later on want
to access the same variables, you can invoke this function
with the same inputs. Use `generate_unique_arguments/2` to
generate a unique arguments that can't be overridden.
## Examples
@@ -349,19 +354,47 @@ defmodule Macro do
@doc since: "1.5.0"
@spec generate_arguments(0, context :: atom) :: []
@spec generate_arguments(pos_integer, context) :: [{atom, [], context}, ...] when context: atom
def generate_arguments(amount, context)
def generate_arguments(amount, context), do: generate_arguments(amount, context, &var/2)
def generate_arguments(0, context) when is_atom(context), do: []
@doc """
Generates AST nodes for a given number of required argument
variables using `Macro.unique_var/2`.
def generate_arguments(amount, context)
when is_integer(amount) and amount > 0 and is_atom(context) do
for id <- 1..amount, do: var(String.to_atom("arg" <> Integer.to_string(id)), context)
## Examples
iex> [var1, var2] = Macro.generate_unique_arguments(2, __MODULE__)
iex> {:arg1, [counter: c1], __MODULE__} = var1
iex> {:arg2, [counter: c2], __MODULE__} = var2
iex> is_integer(c1) and is_integer(c2)
true
"""
@doc since: "1.11.3"
@spec generate_unique_arguments(0, context :: atom) :: []
@spec generate_unique_arguments(pos_integer, context) :: [
{atom, [counter: integer], context},
...
]
when context: atom
def generate_unique_arguments(amount, context),
do: generate_arguments(amount, context, &unique_var/2)
defp generate_arguments(0, context, _fun) when is_atom(context), do: []
defp generate_arguments(amount, context, fun)
when is_integer(amount) and amount > 0 and is_atom(context) do
for id <- 1..amount, do: fun.(String.to_atom("arg" <> Integer.to_string(id)), context)
end
@doc """
Generates an AST node representing the variable given
by the atoms `var` and `context`.
Note this variable is not unique. If you later on want
to access this same variable, you can invoke `var/2`
again with the same arguments. Use `unique_var/2` to
generate a unique variable that can't be overridden.
## Examples
In order to build a variable, a context is expected.
@@ -383,6 +416,24 @@ defmodule Macro do
{var, [], context}
end
@doc """
Generates an AST node representing a unique variable
given by the atoms `var` and `context`.
## Examples
iex> {:foo, [counter: c], __MODULE__} = Macro.unique_var(:foo, __MODULE__)
iex> is_integer(c)
true
"""
@doc since: "1.11.3"
@spec unique_var(var, context) :: {var, [counter: integer], context}
when var: atom, context: atom
def unique_var(var, context) when is_atom(var) and is_atom(context) do
{var, [counter: :elixir_module.next_counter(context)], context}
end
@doc """
Performs a depth-first traversal of quoted expressions
using an accumulator.
@@ -427,10 +478,14 @@ defmodule Macro do
end
defp do_traverse_args(args, acc, pre, post) when is_list(args) do
Enum.map_reduce(args, acc, fn x, acc ->
{x, acc} = pre.(x, acc)
do_traverse(x, acc, pre, post)
end)
:lists.mapfoldl(
fn x, acc ->
{x, acc} = pre.(x, acc)
do_traverse(x, acc, pre, post)
end,
acc,
args
)
end
@doc """
@@ -490,10 +545,15 @@ defmodule Macro do
iex> Macro.decompose_call(quote(do: 42))
:error
iex> Macro.decompose_call(quote(do: {:foo, [], []}))
:error
"""
@spec decompose_call(t()) :: {atom, [t()]} | {t(), atom, [t()]} | :error
def decompose_call(ast)
def decompose_call({:{}, _, args}) when is_list(args), do: :error
def decompose_call({{:., _, [remote, function]}, _, args})
when is_tuple(remote) or is_atom(remote),
do: {remote, function, args}
@@ -580,13 +640,15 @@ defmodule Macro do
expanding structs defined under the module being compiled.
It will raise `CompileError` if the struct is not available.
From Elixir v1.12, calling this function also adds an export
dependency on the given struct.
"""
@doc since: "1.8.0"
@spec struct!(module, Macro.Env.t()) :: %{__struct__: module} when module: module()
def struct!(module, env) when is_atom(module) do
if module == env.module do
Module.get_attribute(module, :struct)
end || :elixir_map.load_struct([line: env.line], module, [], env)
Module.get_attribute(module, :__struct__)
end || :elixir_map.load_struct([line: env.line], module, [], [], env)
end
@doc """
@@ -663,8 +725,8 @@ defmodule Macro do
and return a version with it unescaped.
"""
@spec unescape_string(String.t()) :: String.t()
def unescape_string(chars) do
:elixir_interpolation.unescape_chars(chars)
def unescape_string(string) do
:elixir_interpolation.unescape_string(string)
end
@doc ~S"""
@@ -679,8 +741,9 @@ defmodule Macro do
representing the code point of the character it wants to unescape.
Here is the default mapping function implemented by Elixir:
def unescape_map(unicode), do: true
def unescape_map(hex), do: true
def unescape_map(:newline), do: true
def unescape_map(:unicode), do: true
def unescape_map(:hex), do: true
def unescape_map(?0), do: ?0
def unescape_map(?a), do: ?\a
def unescape_map(?b), do: ?\b
@@ -697,9 +760,9 @@ defmodule Macro do
If the `unescape_map/1` function returns `false`, the char is
not escaped and the backslash is kept in the string.
Hexadecimals and Unicode code points will be escaped if the map
function returns `true` for `?x`. Unicode code points if the map
function returns `true` for `?u`.
Newlines, Unicode, and hexadecimals code points will be escaped if
the map returns `true` respectively for `:newline`, `:unicode`, and
`:hex`.
## Examples
@@ -709,25 +772,23 @@ defmodule Macro do
"""
@spec unescape_string(String.t(), (non_neg_integer -> non_neg_integer | false)) :: String.t()
def unescape_string(chars, map) do
:elixir_interpolation.unescape_chars(chars, map)
def unescape_string(string, map) do
:elixir_interpolation.unescape_string(string, map)
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens) do
case :elixir_interpolation.unescape_tokens(tokens) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
for token <- tokens do
if is_binary(token), do: unescape_string(token), else: token
end
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens, map) do
case :elixir_interpolation.unescape_tokens(tokens, map) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
for token <- tokens do
if is_binary(token), do: unescape_string(token, map), else: token
end
end
@@ -908,7 +969,7 @@ defmodule Macro do
def to_string({target, _, args} = ast, fun) when is_list(args) do
with :error <- unary_call(ast, fun),
:error <- binary_call(ast, fun),
:error <- op_call(ast, fun),
:error <- sigil_call(ast, fun) do
{list, last} = split_last(args)
@@ -1026,8 +1087,6 @@ defmodule Macro do
"\#{" <> to_string(arg, fun) <> "}"
binary when is_binary(binary) ->
binary = inspect_no_limit(binary)
binary = binary_part(binary, 1, byte_size(binary) - 2)
escape_sigil(binary, left)
end)
@@ -1082,7 +1141,14 @@ defmodule Macro do
:error
end
defp binary_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
defp op_call({:"..//", _, [left, middle, right]} = ast, fun) do
left = op_to_string(left, fun, :.., :left)
middle = op_to_string(middle, fun, :.., :right)
right = op_to_string(right, fun, :"//", :right)
{:ok, fun.(ast, left <> ".." <> middle <> "//" <> right)}
end
defp op_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
case Identifier.binary_op(op) do
{_, _} ->
left = op_to_string(left, fun, op, :left)
@@ -1095,7 +1161,7 @@ defmodule Macro do
end
end
defp binary_call(_, _) do
defp op_call(_, _) do
:error
end
@@ -1613,7 +1679,8 @@ defmodule Macro do
end
defp do_underscore(<<h, t, rest::binary>>, _)
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and t != ?. and t != ?_ do
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and not (t >= ?0 and t <= ?9) and
t != ?. and t != ?_ do
<<?_, to_lower_char(h), t>> <> do_underscore(rest, t)
end
+4 -1
View File
@@ -97,7 +97,7 @@ defmodule Macro.Env do
line: line,
macro_aliases: macro_aliases,
macros: macros,
module: atom,
module: module,
prematch_vars: prematch_vars,
unused_vars: unused_vars,
requires: requires,
@@ -105,7 +105,9 @@ defmodule Macro.Env do
vars: vars
}
# Define the __struct__ callbacks by hand for bootstrap reasons.
# TODO: Remove :vars field on v2.0
@doc false
def __struct__ do
%{
__struct__: __MODULE__,
@@ -130,6 +132,7 @@ defmodule Macro.Env do
}
end
@doc false
def __struct__(kv) do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
end
+34 -28
View File
@@ -19,8 +19,13 @@ defmodule Map do
in a map literal, the last one prevails.
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
can be used (as in many other special forms), provided key-value pairs are put at
the end:
can be used (as in many other special forms):
iex> %{a: 1, b: 2}
%{a: 1, b: 2}
If you want to mix the shorthand syntax with `=>`, the shorthand syntax must come
at the end:
iex> %{"hello" => "world", a: 1, b: 2}
%{:a => 1, :b => 2, "hello" => "world"}
@@ -45,11 +50,11 @@ defmodule Map do
map.foo
#=> "bar"
map.non_existing_key
#=> ** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
> Note: do not add parens when accessing fields, such as in `data.key()`.
> If parenthesis are used, Elixir will consider it to be a function call
> on `data`, which would be expected to be an atom.
> If parenthesis are used, Elixir will expect `data` to be an atom representing
> a module and attempt to call the *function* `key/0` in it.
The two syntaxes for accessing keys reveal the dual nature of maps. The `map[key]`
syntax is used for dynamically created maps that may have any key, of any type.
@@ -211,20 +216,10 @@ defmodule Map do
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform) when is_function(transform, 1) do
enumerable
|> Enum.to_list()
|> new_transform(transform, [])
end
defp new_transform([], _fun, acc) do
acc
|> :lists.reverse()
|> Enum.map(transform)
|> :maps.from_list()
end
defp new_transform([element | rest], fun, acc) do
new_transform(rest, fun, [fun.(element) | acc])
end
@doc """
Returns whether the given `key` exists in the given `map`.
@@ -614,7 +609,7 @@ defmodule Map do
%{a: 1, b: 11}
"""
@spec update(map, key, default :: value, (existing_value :: value -> updated_value :: value)) ::
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
map
def update(map, key, default, fun) when is_function(fun, 1) do
case map do
@@ -632,8 +627,8 @@ defmodule Map do
@doc """
Removes the value associated with `key` in `map` and returns the value and the updated map.
If `key` is present in `map`, it returns `{value, new_map}` where `value` is the value of
the key and `new_map` is the result of removing `key` from `map`. If `key`
If `key` is present in `map`, it returns `{value, updated_map}` where `value` is the value of
the key and `updated_map` is the result of removing `key` from `map`. If `key`
is not present in `map`, `{default, map}` is returned.
## Examples
@@ -646,7 +641,7 @@ defmodule Map do
{3, %{a: 1}}
"""
@spec pop(map, key, value) :: {value, new_map :: map}
@spec pop(map, key, default) :: {value, updated_map :: map} | {default, map} when default: value
def pop(map, key, default \\ nil) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -655,8 +650,8 @@ defmodule Map do
end
@doc """
Returns and removes the value associated with `key` in `map` or raises
if `key` is not present.
Removes the value associated with `key` in `map` and returns the value
and the updated map, or it raises if `key` is not present.
Behaves the same as `pop/3` but raises if `key` is not present in `map`.
@@ -671,7 +666,7 @@ defmodule Map do
"""
@doc since: "1.10.0"
@spec pop!(map, key) :: {value, map}
@spec pop!(map, key) :: {value, updated_map :: map}
def pop!(map, key) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -812,7 +807,7 @@ defmodule Map do
** (KeyError) key :b not found in: %{a: 1}
"""
@spec update!(map, key, (existing_value :: value -> updated_value :: value)) :: map
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
def update!(map, key, fun) when is_function(fun, 1) do
value = fetch!(map, key)
put(map, key, fun.(value))
@@ -841,7 +836,7 @@ defmodule Map do
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, %{b: "new value!", a: 1}}
{nil, %{a: 1, b: "new value!"}}
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
{1, %{}}
@@ -850,8 +845,8 @@ defmodule Map do
{nil, %{a: 1}}
"""
@spec get_and_update(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_map :: map}
when current_value: value
def get_and_update(map, key, fun) when is_function(fun, 1) do
current = get(map, key)
@@ -892,7 +887,7 @@ defmodule Map do
{1, %{}}
"""
@spec get_and_update!(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
when current_value: value
def get_and_update!(map, key, fun) when is_function(fun, 1) do
@@ -945,6 +940,11 @@ defmodule Map do
Two maps are considered to be equal if they contain
the same keys and those keys contain the same values.
Note this function exists for completeness so the `Map`
and `Keyword` modules provide similar APIs. In practice,
developers often compare maps using `==/2` or `===/2`
directly.
## Examples
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
@@ -952,6 +952,12 @@ defmodule Map do
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
false
Comparison between keys and values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Map.equal?(%{a: 1.0}, %{a: 1})
false
"""
@spec equal?(map, map) :: boolean
def equal?(map1, map2)
+7 -3
View File
@@ -57,7 +57,7 @@ defmodule MapSet do
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
@type t :: t(term)
# TODO: Remove version key on v2.0
# TODO: Remove version key on Elixir v2.0
defstruct map: %{}, version: 2
@doc """
@@ -223,7 +223,9 @@ defmodule MapSet do
@doc """
Checks if two sets are equal.
The comparison between elements must be done using `===/2`.
The comparison between elements is done using `===/2`,
which a set with `1` is not equivalent to a set with
`1.0`.
## Examples
@@ -231,11 +233,13 @@ defmodule MapSet do
true
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
false
iex> MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))
false
"""
@spec equal?(t, t) :: boolean
def equal?(%MapSet{map: map1, version: version}, %MapSet{map: map2, version: version}) do
Map.equal?(map1, map2)
map1 === map2
end
# Elixir v1.5 changed the map representation, so on
+213 -26
View File
@@ -235,7 +235,7 @@ defmodule Module do
end
For the list of supported warnings, see
[`:dialyzer` module](http://www.erlang.org/doc/man/dialyzer.html).
[`:dialyzer` module](`:dialyzer`).
Multiple uses of `@dialyzer` will accumulate instead of overriding
previous ones.
@@ -253,7 +253,7 @@ defmodule Module do
[`mix compile.elixir`](https://hexdocs.pm/mix/Mix.Tasks.Compile.Elixir.html).
If the external resource does not exist, the module still has
a dependency on it, causing the module be recompiled as soon
a dependency on it, causing the module to be recompiled as soon
as the file is added.
### `@file`
@@ -305,9 +305,9 @@ defmodule Module do
Accepts the function name (as an atom) of a function in the current module or
`{function_name, 0}` tuple where `function_name` is the name of a function in
the current module. The function must be public and have an arity of 0 (no
arguments). If the function does not return `:ok`, the loading of the module
will be aborted. For example:
the current module. The function must have an arity of 0 (no arguments). If
the function does not return `:ok`, the loading of the module will be aborted.
For example:
defmodule MyModule do
@on_load :load_check
@@ -335,6 +335,16 @@ defmodule Module do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `Kernel.defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
@@ -543,6 +553,103 @@ defmodule Module do
@callback __info__(:md5) :: binary()
@callback __info__(:module) :: module()
@doc """
Returns information about module attributes used by Elixir.
See the "Module attributes" section in the module documentation for more
information on each attribute.
## Examples
iex> map = Module.reserved_attributes()
iex> Map.has_key?(map, :moduledoc)
true
iex> Map.has_key?(map, :doc)
true
"""
@doc since: "1.12.0"
def reserved_attributes() do
%{
after_compile: %{
doc: "A hook that will be invoked right after the current module is compiled."
},
before_compile: %{
doc: "A hook that will be invoked before the module is compiled."
},
behaviour: %{
doc: "Specifies that the current module implements a given behaviour."
},
on_definition: %{
doc:
"A hook that will be invoked when each function or macro in the current module is defined."
},
impl: %{
doc: "Declares an implementation of a callback function or macro."
},
compile: %{
doc: "Defines options for module compilation."
},
deprecated: %{
doc: "Provides the deprecation reason for a function."
},
moduledoc: %{
doc: "Provides documentation for the current module."
},
doc: %{
doc: "Provides documentation for a function/macro/callback."
},
typedoc: %{
doc: "Provides documentation for a type."
},
dialyzer: %{
doc: "Defines Dialyzer warnings to request or suppress."
},
external_resource: %{
doc: "Specifies an external resource for the current module."
},
file: %{
doc:
"Changes the filename used in stacktraces for the function or macro that follows the attribute."
},
on_load: %{
doc: "A hook that will be invoked whenever the module is loaded."
},
vsn: %{
doc: "Specify the module version."
},
type: %{
doc: "Defines a type to be used in `@spec`."
},
typep: %{
doc: "Defines a private type to be used in `@spec`."
},
opaque: %{
doc: "Defines an opaque type to be used in `@spec`."
},
spec: %{
doc: "Provides a specification for a function."
},
callback: %{
doc: "Provides a specification for a behaviour callback."
},
macrocallback: %{
doc: "Provides a specification for a macro behaviour callback."
},
optional_callbacks: %{
doc: "Specifies which behaviour callbacks and macro behaviour callbacks are optional."
},
derive: %{
doc:
"Derives an implementation for the given protocol for the struct defined in the current module."
},
enforce_keys: %{
doc:
"Ensures the given keys are always set when building the struct defined in the current module."
}
}
end
@doc """
Checks if a module is open.
@@ -724,9 +831,6 @@ defmodule Module do
## Examples
iex> Module.safe_concat([Module, Unknown])
** (ArgumentError) argument error
iex> Module.safe_concat([List, Chars])
List.Chars
@@ -745,9 +849,6 @@ defmodule Module do
## Examples
iex> Module.safe_concat(Module, Unknown)
** (ArgumentError) argument error
iex> Module.safe_concat(List, Chars)
List.Chars
@@ -1061,6 +1162,62 @@ defmodule Module do
:ets.select(set, [{{{:def, :"$1"}, kind, :_, :_, :_, :_}, [], [:"$1"]}])
end
@doc """
Returns the definition for the given name-arity pair.
It returns a tuple with the `version`, the `kind`,
the definition `metadata`, and a list with each clause.
Each clause is a four-element tuple with metadata,
the arguments, the guards, and the clause AST.
The clauses are returned in the Elixir AST but a subset
that has already been expanded and normalized. This makes
it useful for analyzing code but it cannot be reinjected
into the module as it will have lost some of its original
context. Given this AST representation is mostly internal,
it is versioned and it may change at any time. Therefore,
**use this API with caution**.
"""
@spec get_definition(module, definition) ::
{:v1, def_kind, meta :: keyword,
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
@doc since: "1.12.0"
def get_definition(module, {name, arity})
when is_atom(module) and is_atom(name) and is_integer(arity) do
assert_not_compiled!(__ENV__.function, module, "")
{set, bag} = data_tables_for(module)
case :ets.lookup(set, {:def, {name, arity}}) do
[{_key, kind, meta, _, _, _}] ->
{:v1, kind, meta, bag_lookup_element(bag, {:clauses, {name, arity}}, 2)}
[] ->
nil
end
end
@doc """
Deletes a definition from a module.
It returns true if the definition exists and it was removed,
otherwise it returns false.
"""
@doc since: "1.12.0"
@spec delete_definition(module, definition) :: boolean()
def delete_definition(module, {name, arity})
when is_atom(module) and is_atom(name) and is_integer(arity) do
assert_not_readonly!(__ENV__.function, module)
case :elixir_def.take_definition(module, {name, arity}) do
false ->
false
_ ->
:elixir_locals.yank({name, arity}, module)
true
end
end
@doc """
Makes the given functions in `module` overridable.
@@ -1451,7 +1608,8 @@ defmodule Module do
redefining @doc attribute previously set at line #{current_line}.
Please remove the duplicate docs. If instead you want to override a \
previously defined @doc, attach the @doc attribute to a function head:
previously defined @doc, attach the @doc attribute to a function head \
(the function signature not followed by any do-block). For example:
@doc """
new docs
@@ -1973,20 +2131,13 @@ defmodule Module do
end
defp preprocess_attribute(:impl, value) do
case value do
_ when is_boolean(value) ->
value
module when is_atom(module) and module != nil ->
# Attempt to compile behaviour but ignore failure (will warn later)
_ = Code.ensure_compiled(module)
value
_ ->
raise ArgumentError,
"@impl is a built-in module attribute that marks the next definition " <>
"as a callback implementation. It should be a module or a boolean, " <>
"got: #{inspect(value)}"
if is_boolean(value) or (is_atom(value) and value != nil) do
value
else
raise ArgumentError,
"@impl is a built-in module attribute that marks the next definition " <>
"as a callback implementation. It should be a module or a boolean, " <>
"got: #{inspect(value)}"
end
end
@@ -2034,10 +2185,46 @@ defmodule Module do
end
end
defp preprocess_attribute(:dialyzer, value) do
# From https://github.com/erlang/otp/blob/master/lib/stdlib/src/erl_lint.erl
:lists.foreach(
fn attr ->
if not valid_dialyzer_attribute?(attr) do
raise ArgumentError, "invalid value for @dialyzer attribute: #{inspect(attr)}"
end
end,
List.wrap(value)
)
value
end
defp preprocess_attribute(_key, value) do
value
end
defp valid_dialyzer_attribute?({key, fun_arities}) when is_atom(key) do
(key == :nowarn_function or valid_dialyzer_attribute?(key)) and
:lists.all(
fn
{fun, arity} when is_atom(fun) and is_integer(arity) -> true
_ -> false
end,
List.wrap(fun_arities)
)
end
defp valid_dialyzer_attribute?(attr) do
:lists.member(
attr,
[:no_return, :no_unused, :no_improper_lists, :no_fun_app] ++
[:no_match, :no_opaque, :no_fail_call, :no_contracts] ++
[:no_behaviours, :no_undefined_callbacks, :unmatched_returns] ++
[:error_handling, :race_conditions, :no_missing_calls] ++
[:specdiffs, :overspecs, :underspecs, :unknown, :no_underspecs]
)
end
defp preprocess_doc_meta([], _module, _line, map), do: map
defp preprocess_doc_meta([{key, _} | tail], module, line, map)
+38 -42
View File
@@ -4,6 +4,7 @@ defmodule Module.ParallelChecker do
@type cache() :: {pid(), :ets.tid()}
@type warning() :: term()
@type kind() :: :def | :defmacro
@type mode() :: :elixir | :erlang
@doc """
Receives pairs of module maps and BEAM binaries. In parallel it verifies
@@ -67,17 +68,17 @@ defmodule Module.ParallelChecker do
or if the function does not exist return `{:error, :function}`.
"""
@spec fetch_export(cache(), module(), atom(), arity()) ::
{:ok, kind(), binary() | nil} | {:error, :function | :module}
{:ok, mode(), kind(), binary() | nil} | {:error, :function | :module}
def fetch_export({_server, ets}, module, fun, arity) do
case :ets.lookup(ets, {:cached, module}) do
[{_key, true}] ->
case :ets.lookup(ets, {:export, {module, fun, arity}}) do
[{_key, kind, reason}] -> {:ok, kind, reason}
[] -> {:error, :function}
end
[{_key, false}] ->
{:error, :module}
[{_key, mode}] ->
case :ets.lookup(ets, {:export, module, {fun, arity}}) do
[{_key, kind, reason}] -> {:ok, mode, kind, reason}
[] -> {:error, :function}
end
end
end
@@ -89,10 +90,9 @@ defmodule Module.ParallelChecker do
def all_exports({_server, ets}, module) do
# This is only called after we get a deprecation notice
# so we can assume it's a cached module
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
exports
|> Enum.map(fn {function, _kind} -> function end)
ets
|> :ets.match({{:export, module, :"$1"}, :_, :_})
|> Enum.flat_map(& &1)
|> Enum.sort()
end
@@ -336,28 +336,31 @@ defmodule Module.ParallelChecker do
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated)
cache_info(ets, map.module, exports, deprecated, :elixir)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
exports = info_exports(module)
{mode, exports} = info_exports(module)
deprecated = info_deprecated(module)
cache_info(ets, module, exports, deprecated)
cache_info(ets, module, exports, deprecated, mode)
else
:ets.insert(ets, {{:cached, module}, false})
end
end
defp info_exports(module) do
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
map =
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
{:elixir, map}
rescue
_ -> Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))
_ -> {:erlang, Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))}
end
defp info_deprecated(module) do
@@ -366,39 +369,32 @@ defmodule Module.ParallelChecker do
_ -> %{}
end
defp cache_info(ets, module, exports, deprecated) do
exports =
Enum.map(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
defp cache_info(ets, module, exports, deprecated, mode) do
Enum.each(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
:ets.insert(ets, {{:cached, module}, mode})
end
defp cache_chunk(ets, module, exports) do
exports =
Enum.map(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
Enum.each(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:export, {module, :__info__, 1}}, :def, nil})
exports = [{{:__info__, 1}, :def} | exports]
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
:ets.insert(ets, {{:export, module, {:__info__, 1}}, :def, nil})
:ets.insert(ets, {{:cached, module}, :elixir})
end
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
defp behaviour_exports(%{is_behaviour: false}), do: []
defp behaviour_exports(module) when is_atom(module) do
if {:behaviour_info, 1} in module.module_info(:functions) do
if function_exported?(module, :behaviour_info, 1) do
[{{:behaviour_info, 1}, :def}]
else
[]
+42 -115
View File
@@ -6,7 +6,7 @@ defmodule Module.Types do
end
import Module.Types.Helpers
alias Module.Types.{Expr, Pattern, Infer}
alias Module.Types.{Expr, Pattern, Unify}
@doc false
def warnings(module, file, defs, no_warn_undefined, cache) do
@@ -57,10 +57,10 @@ defmodule Module.Types do
end
defp warnings_from_clause(args, guards, body, def_expr, stack, context) do
head_stack = push_expr_stack(def_expr, stack)
head_stack = Unify.push_expr_stack(def_expr, stack)
with {:ok, _types, context} <- Pattern.of_head(args, guards, head_stack, context),
{:ok, _type, context} <- Expr.of_expr(body, stack, context) do
{:ok, _type, context} <- Expr.of_expr(body, :dynamic, stack, context) do
context.warnings
else
{:error, {type, error, context}} ->
@@ -92,7 +92,7 @@ defmodule Module.Types do
traces: %{},
# Counter to give type variables unique names
counter: 0,
# Track if a variable was infered from a type guard function such is_tuple/1
# Track if a variable was inferred from a type guard function such is_tuple/1
# or a guard function that fails such as elem/2, possible values are:
# `:guarded` when `is_tuple(x)`
# `:guarded` when `is_tuple and elem(x, 0)`
@@ -115,104 +115,21 @@ defmodule Module.Types do
# When false do not add a trace when a type variable is refined,
# useful when merging contexts where the variables already have traces
trace: true,
# Track if we are in a context where type guard functions should
# affect inference
type_guards_enabled?: true,
# There are two factors that control how we track guards.
#
# * consider_type_guards?: if type guards should be considered.
# This applies only at the root and root-based "and" and "or" nodes.
#
# * keep_guarded? - if a guarded clause should remain as guarded
# even on failure. Used on the right side of and.
#
type_guards: {_consider_type_guards? = true, _keep_guarded? = false},
# Context used to determine if unification is bi-directional, :expr
# is directional, :pattern is bi-directional
context: nil
}
end
## VARIABLE LIFTING
@doc """
Lifts type variables to their infered types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &do_lift_type/2)
types
end
@doc """
Lifts a single type to its infered type from the context.
"""
def lift_type(type, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{type, _context} = do_lift_type(type, context)
type
end
# Lift type variable to its infered (hopefully concrete) types from the context
defp do_lift_type({:var, var}, context) do
case Map.fetch(context.lifted_types, var) do
{:ok, lifted_var} ->
{{:var, lifted_var}, context}
:error ->
case Map.fetch(context.types, var) do
{:ok, :unbound} ->
new_lifted_var(var, context)
{:ok, type} ->
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = %{context | types: Map.delete(context.types, var)}
{type, context} = do_lift_type(type, context)
{type, %{context | types: types}}
:error ->
new_lifted_var(var, context)
end
end
end
defp do_lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
{{:tuple, n, types}, context}
end
defp do_lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = do_lift_type(key, context)
{value, context} = do_lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp do_lift_type({:list, type}, context) do
{type, context} = do_lift_type(type, context)
{{:list, type}, context}
end
defp do_lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
## ERROR TO WARNING
# Collect relevant information from context and traces to report error
@@ -222,8 +139,7 @@ defmodule Module.Types do
location = {context.file, line, {context.module, fun, arity}}
traces = type_traces(stack, context)
traces = tag_traces(traces, context)
{left, right, traces} = lift_all_types(left, right, traces, context)
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
{Module.Types, error, location}
end
@@ -234,14 +150,13 @@ defmodule Module.Types do
# in the stack since we get related variables anyway?
stack =
stack.unify_stack
|> Enum.uniq()
|> Enum.flat_map(&[&1 | related_variables(&1, context.types)])
|> Enum.uniq()
Enum.flat_map(stack, fn var_index ->
with %{^var_index => traces} <- context.traces,
%{^var_index => expr_var} <- context.types_to_vars do
Enum.map(traces, &{expr_var, &1})
Enum.map(traces, &tag_trace(expr_var, &1, context))
else
_other -> []
end
@@ -259,15 +174,26 @@ defmodule Module.Types do
end
# Tag if trace is for a concrete type or type variable
defp tag_traces(traces, context) do
Enum.flat_map(traces, fn {var, {type, expr, location}} ->
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
[{var, {:var, expr_var, expr, location}}]
else
_ -> [{var, {:type, type, expr, location}}]
end
end)
defp tag_trace(var, {type, expr, location}, context) do
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
{:var, var, expr_var, expr, location}
else
_ -> {:type, var, type, expr, location}
end
end
defp lift_all_types(left, right, traces, context) do
all_types = [left, right] ++ for({:type, _, type, _, _} <- traces, do: type)
[left, right | all_types] = Unify.lift_types(all_types, context)
{traces, []} =
Enum.map_reduce(traces, all_types, fn
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
other, acc -> {other, acc}
end)
{left, right, traces}
end
## FORMAT WARNINGS
@@ -300,9 +226,9 @@ defmodule Module.Types do
[
"incompatible types:\n\n ",
Infer.format_type(left, simplify_left?),
Unify.format_type(left, simplify_left?),
" !~ ",
Infer.format_type(right, simplify_right?),
Unify.format_type(right, simplify_right?),
"\n\n",
format_expr(expr, location),
traces,
@@ -343,16 +269,17 @@ defmodule Module.Types do
defp format_traces(traces, simplify?) do
traces
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map_reduce([], fn
{var, {:type, type, expr, location}}, hints ->
{:type, var, type, expr, location}, hints ->
{hint, hints} = format_type_hint(type, expr, hints)
trace = [
"where \"",
Macro.to_string(var),
"\" was given the type ",
Infer.format_type(type, simplify?),
Unify.format_type(type, simplify?),
hint,
" in:\n\n # ",
format_location(location),
@@ -363,7 +290,7 @@ defmodule Module.Types do
{trace, hints}
{var1, {:var, var2, expr, location}}, hints ->
{:var, var1, var2, expr, location}, hints ->
trace = [
"where \"",
Macro.to_string(var1),
@@ -518,8 +445,8 @@ defmodule Module.Types do
defp map_type?(_other), do: false
defp atom_type?(:atom), do: true
defp atom_type?(:boolean), do: true
defp atom_type?({:atom, _}), do: false
defp atom_type?({:union, union}), do: Enum.all?(union, &atom_type?/1)
defp atom_type?(_other), do: false
defp integer_type?(:integer), do: true
+165 -133
View File
@@ -2,60 +2,59 @@ defmodule Module.Types.Expr do
@moduledoc false
alias Module.Types.{Of, Pattern}
import Module.Types.{Helpers, Infer}
import Module.Types.{Helpers, Unify}
def of_expr(expr, %{context: stack_context} = stack, context) when stack_context != :expr do
of_expr(expr, %{stack | context: :expr}, context)
def of_expr(expr, expected, %{context: stack_context} = stack, context)
when stack_context != :expr do
of_expr(expr, expected, %{stack | context: :expr}, context)
end
# :atom
def of_expr(atom, _stack, context) when is_atom(atom) do
def of_expr(atom, _expected, _stack, context) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
def of_expr(literal, _stack, context) when is_integer(literal) do
def of_expr(literal, _expected, _stack, context) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
def of_expr(literal, _stack, context) when is_float(literal) do
def of_expr(literal, _expected, _stack, context) when is_float(literal) do
{:ok, :float, context}
end
# "..."
def of_expr(literal, _stack, context) when is_binary(literal) do
def of_expr(literal, _expected, _stack, context) when is_binary(literal) do
{:ok, :binary, context}
end
# #PID<...>
def of_expr(literal, _stack, context) when is_pid(literal) do
def of_expr(literal, _expected, _stack, context) when is_pid(literal) do
{:ok, :dynamic, context}
end
# <<...>>>
def of_expr({:<<>>, _meta, args}, stack, context) do
result = Of.binary(args, stack, context, &of_expr/3)
case result do
def of_expr({:<<>>, _meta, args}, _expected, stack, context) do
case Of.binary(args, stack, context, &of_expr/4) do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
def of_expr({:|, _meta, [left_expr, []]} = expr, stack, context) do
def of_expr({:|, _meta, [left_expr, []]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
of_expr(left_expr, stack, context)
of_expr(left_expr, :dynamic, stack, context)
end
# left | right
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case of_expr(left_expr, stack, context) do
case of_expr(left_expr, :dynamic, stack, context) do
{:ok, left, context} ->
case of_expr(right_expr, stack, context) do
case of_expr(right_expr, :dynamic, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
@@ -72,22 +71,23 @@ defmodule Module.Types.Expr do
end
# []
def of_expr([], _stack, context) do
def of_expr([], _expected, _stack, context) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
def of_expr(exprs, stack, context) when is_list(exprs) do
def of_expr(exprs, _expected, stack, context) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# __CALLER__
def of_expr({:__CALLER__, _meta, var_context}, _stack, context) when is_atom(var_context) do
def of_expr({:__CALLER__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
struct_pair = {:required, {:atom, :__struct__}, {:atom, Macro.Env}}
pairs =
@@ -99,7 +99,8 @@ defmodule Module.Types.Expr do
end
# __STACKTRACE__
def of_expr({:__STACKTRACE__, _meta, var_context}, _stack, context) when is_atom(var_context) do
def of_expr({:__STACKTRACE__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
file = {:tuple, 2, [{:atom, :file}, {:list, :integer}]}
line = {:tuple, 2, [{:atom, :line}, :integer]}
file_line = {:list, {:union, [file, line]}}
@@ -108,42 +109,45 @@ defmodule Module.Types.Expr do
end
# var
def of_expr(var, _stack, context) when is_var(var) do
{type, context} = new_var(var, context)
{:ok, type, context}
def of_expr(var, _expected, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
end
# {left, right}
def of_expr({left, right}, stack, context) do
of_expr({:{}, [], [left, right]}, stack, context)
def of_expr({left, right}, expected, stack, context) do
of_expr({:{}, [], [left, right]}, expected, stack, context)
end
# {...}
def of_expr({:{}, _meta, exprs} = expr, stack, context) do
def of_expr({:{}, _meta, exprs} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
{:error, reason} -> {:error, reason}
end
end
# left = right
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
# TODO: We might want to bring the expected type forward in case the type of this
# pattern is not useful. For example: 1 = _ = expr
stack = push_expr_stack(expr, stack)
with {:ok, left_type, context} <-
Pattern.of_pattern(left_expr, stack, context),
{:ok, right_type, context} <- of_expr(right_expr, stack, context),
do: unify(right_type, left_type, %{stack | context: :pattern}, context)
{:ok, right_type, context} <- of_expr(right_expr, left_type, stack, context),
do: unify(right_type, left_type, stack, context)
end
# %{map | ...}
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, stack, context) do
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, map_type, context} <- of_expr(map, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/3),
with {:ok, map_type, context} <- of_expr(map, map_type, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/4),
dynamic_value_pairs =
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
@@ -161,59 +165,72 @@ defmodule Module.Types.Expr do
end
# %Struct{map | ...}
def of_expr({:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr, stack, context) do
def of_expr(
{:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr,
_expected,
stack,
context
) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, struct, context} <- Of.struct(module, meta, context),
{:ok, update, context} <- of_expr(update, stack, context) do
{:ok, update, context} <- of_expr(update, map_type, stack, context) do
unify(update, struct, stack, context)
end
end
# %{...}
def of_expr({:%{}, _meta, args} = expr, stack, context) do
def of_expr({:%{}, _meta, args} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
Of.closed_map(args, stack, context, &of_expr/3)
Of.closed_map(args, stack, context, &of_expr/4)
end
# %Struct{...}
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context) do
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/3) do
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/4) do
unify(map, struct, stack, context)
end
end
# ()
def of_expr({:__block__, _meta, []}, _stack, context) do
def of_expr({:__block__, _meta, []}, _expected, _stack, context) do
{:ok, {:atom, nil}, context}
end
# (expr; expr)
def of_expr({:__block__, _meta, exprs}, stack, context) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
def of_expr({:__block__, _meta, exprs}, expected, stack, context) do
expected_types = List.duplicate(:dynamic, length(exprs) - 1) ++ [expected]
result =
map_reduce_ok(Enum.zip(exprs, expected_types), context, fn {expr, expected}, context ->
of_expr(expr, expected, stack, context)
end)
case result do
{:ok, expr_types, context} -> {:ok, Enum.at(expr_types, -1), context}
{:error, reason} -> {:error, reason}
end
end
# case expr do pat -> expr end
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, stack, context) do
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, _expr_type, context} <- of_expr(case_expr, stack, context),
:ok <- of_clauses(clauses, stack, context),
with {:ok, _expr_type, context} <- of_expr(case_expr, :dynamic, stack, context),
{:ok, context} <- of_clauses(clauses, stack, context),
do: {:ok, :dynamic, context}
end
# fn pat -> expr end
def of_expr({:fn, _meta, clauses} = expr, stack, context) do
def of_expr({:fn, _meta, clauses} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case of_clauses(clauses, stack, context) do
:ok -> {:ok, :dynamic, context}
{:ok, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
end
@@ -222,86 +239,104 @@ defmodule Module.Types.Expr do
@try_clause_blocks [:catch, :else, :after]
# try do expr end
def of_expr({:try, _meta, [blocks]} = expr, stack, context) do
def of_expr({:try, _meta, [blocks]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
result =
each_ok(blocks, fn
{:rescue, clauses} ->
each_ok(clauses, fn
{:->, _, [[{:in, _, [var, _exceptions]}], body]} ->
{result, context} =
reduce_ok(blocks, context, fn
{:rescue, clauses}, context ->
reduce_ok(clauses, context, fn
{:->, _, [[{:in, _, [var, _exceptions]}], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
of_expr_ok(body, stack, context)
{:->, _, [[var], body]} ->
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
{:->, _, [[var], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
of_expr_ok(body, stack, context)
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
end)
{block, body} when block in @try_blocks ->
of_expr_ok(body, stack, context)
{block, body}, context = acc when block in @try_blocks ->
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
{block, clauses} when block in @try_clause_blocks ->
{block, clauses}, context when block in @try_clause_blocks ->
of_clauses(clauses, stack, context)
end)
case result do
:ok -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
:error -> {:error, context}
end
end
# receive do pat -> expr end
def of_expr({:receive, _meta, [blocks]} = expr, stack, context) do
def of_expr({:receive, _meta, [blocks]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
result =
each_ok(blocks, fn
{:do, {:__block__, _, []}} ->
:ok
{result, context} =
reduce_ok(blocks, context, fn
{:do, {:__block__, _, []}}, context ->
{:ok, context}
{:do, clauses} ->
{:do, clauses}, context ->
of_clauses(clauses, stack, context)
{:after, [{:->, _meta, [head, body]}]} ->
with {:ok, _type, context} <- of_expr(head, stack, context),
{:ok, _type, _context} <- of_expr(body, stack, context),
do: :ok
{:after, [{:->, _meta, [head, body]}]}, context = acc ->
with {:ok, _type, context} <- of_expr(head, :dynamic, stack, context),
{:ok, _type, context} <- of_expr(body, :dynamic, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
case result do
:ok -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
:error -> {:error, context}
end
end
# for pat <- expr do expr end
def of_expr({:for, _meta, args} = expr, stack, context) do
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
case reduce_ok(args, context, &for_clause(&1, stack, &2)) do
{:ok, _context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
with {:ok, context} <- reduce_ok(clauses, context, &for_clause(&1, stack, &2)),
{:ok, context} <- reduce_ok(opts, context, &for_option(&1, stack, &2)) do
if Keyword.has_key?(opts, :reduce) do
with {:ok, context} <- of_clauses(block, stack, context) do
{:ok, :dynamic, context}
end
else
with {:ok, _type, context} <- of_expr(block, :dynamic, stack, context) do
{:ok, :dynamic, context}
end
end
end
end
# with pat <- expr do expr end
def of_expr({:with, _meta, clauses} = expr, stack, context) do
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
{:ok, _context} -> {:ok, :dynamic, context}
{:ok, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
end
# fun.(arg)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, stack, context) do
# fun.(args)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
stack = push_expr_stack(expr, stack)
case of_expr(fun, stack, context) do
case of_expr(fun, :dynamic, stack, context) do
{:ok, _fun_type, context} ->
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -312,12 +347,12 @@ defmodule Module.Types.Expr do
end
# expr.key_or_fun
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, stack, context)
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, _expected, stack, context)
when not is_atom(expr1) do
stack = push_expr_stack(expr2, stack)
if Keyword.get(meta2, :no_parens, false) do
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{value_var, context} = add_var(context),
pair_type = {:required, {:atom, key_or_fun}, value_var},
optional_type = {:optional, :dynamic, :dynamic},
@@ -325,20 +360,23 @@ defmodule Module.Types.Expr do
{:ok, _map_type, context} <- unify(map_field_type, expr_type, stack, context),
do: {:ok, value_var, context}
else
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
# TODO: Use expected type to infer intersection return type
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{:ok, _map_type, context} <- unify(expr_type, :atom, stack, context),
do: {:ok, :dynamic, context}
end
end
# expr.fun(arg)
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, stack, context) do
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
context = Of.remote(expr1, fun, length(args), meta1, context)
stack = push_expr_stack(expr2, stack)
with {:ok, _expr_type, context} <- of_expr(expr1, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, stack, context) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context) do
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -346,7 +384,12 @@ defmodule Module.Types.Expr do
end
# &Foo.bar/1
def of_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, _stack, context)
def of_expr(
{:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]},
_expected,
_stack,
context
)
when is_atom(module) and is_atom(fun) do
context = Of.remote(module, fun, arity, meta, context)
{:ok, :dynamic, context}
@@ -354,16 +397,19 @@ defmodule Module.Types.Expr do
# &foo/1
# & &1
def of_expr({:&, _meta, _arg}, _stack, context) do
def of_expr({:&, _meta, _arg}, _expected, _stack, context) do
# TODO: Function type
{:ok, :dynamic, context}
end
# fun(arg)
def of_expr({fun, _meta, args} = expr, stack, context) when is_atom(fun) and is_list(args) do
def of_expr({fun, _meta, args} = expr, _expected, stack, context)
when is_atom(fun) and is_list(args) do
# TODO: Use expected type to infer intersection return type
stack = push_expr_stack(expr, stack)
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -372,10 +418,15 @@ defmodule Module.Types.Expr do
defp for_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
do: {:ok, context}
end
defp for_clause({:<<>>, _, [{:<-, _, [pattern, expr]}]}, stack, context) do
# TODO: the compiler guarantees pattern is a binary but we need to check expr is a binary
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
do: {:ok, context}
end
@@ -384,39 +435,26 @@ defmodule Module.Types.Expr do
end
defp for_clause(expr, stack, context) do
of_expr_context(expr, stack, context)
of_expr_context(expr, :dynamic, stack, context)
end
defp for_option({:into, expr}, stack, context) do
of_expr_context(expr, stack, context)
of_expr_context(expr, :dynamic, stack, context)
end
defp for_option({:reduce, expr}, stack, context) do
of_expr_context(expr, stack, context)
of_expr_context(expr, :dynamic, stack, context)
end
defp for_option({:uniq, _}, _stack, context) do
{:ok, context}
end
defp for_option({:do, [{:->, _, [pattern, body]}]}, stack, context) do
case Pattern.of_pattern(pattern, stack, context) do
{:ok, _pattern_type, context} -> of_expr_context(body, stack, context)
{:error, reason} -> {:error, reason}
end
end
defp for_option({:do, body}, stack, context) do
of_expr_context(body, stack, context)
end
defp with_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
do: {:ok, context}
end
@@ -425,30 +463,31 @@ defmodule Module.Types.Expr do
end
defp with_clause(expr, stack, context) do
of_expr_context(expr, stack, context)
of_expr_context(expr, :dynamic, stack, context)
end
defp with_option({:do, body}, stack, context) do
of_expr_context(body, stack, context)
of_expr_context(body, :dynamic, stack, context)
end
defp with_option({:else, clauses}, stack, context) do
case of_clauses(clauses, stack, context) do
:ok -> {:ok, context}
{:error, reason} -> {:error, reason}
end
of_clauses(clauses, stack, context)
end
defp of_clauses(clauses, stack, context) do
each_ok(clauses, fn {:->, _meta, [head, body]} ->
reduce_ok(clauses, context, fn {:->, _meta, [head, body]}, context = acc ->
{patterns, guards} = extract_head(head)
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
{:ok, _expr_type, _context} <- of_expr(body, stack, context),
do: :ok
{:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
end
defp keep_warnings(context, %{warnings: warnings}) do
%{context | warnings: warnings}
end
defp extract_head([{:when, _meta, args}]) do
case Enum.split(args, -1) do
{patterns, [guards]} -> {patterns, flatten_when(guards)}
@@ -468,20 +507,13 @@ defmodule Module.Types.Expr do
[other]
end
defp of_expr_context(expr, stack, context) do
case of_expr(expr, stack, context) do
defp of_expr_context(expr, expected, stack, context) do
case of_expr(expr, expected, stack, context) do
{:ok, _type, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end
defp of_expr_ok(expr, stack, context) do
case of_expr(expr, stack, context) do
{:ok, _type, _context} -> :ok
{:error, reason} -> {:error, reason}
end
end
defp new_pattern_var({:_, _meta, var_context}, context) when is_atom(var_context) do
{:dynamic, context}
end
-23
View File
@@ -25,16 +25,6 @@ defmodule Module.Types.Helpers do
def get_meta({_, meta, _}), do: meta
def get_meta(_other), do: []
@doc """
Push expression to stack.
The expression stack is used to give the context where a type variable
was refined when show a type conflict error.
"""
def push_expr_stack(expr, stack) do
%{stack | last_expr: expr}
end
@doc """
Like `Enum.reduce/3` but only continues while `fun` returns `{:ok, acc}`
and stops on `{:error, reason}`.
@@ -91,19 +81,6 @@ defmodule Module.Types.Helpers do
defp do_map_ok([], acc, _fun), do: {:ok, Enum.reverse(acc)}
@doc """
Like `Enum.each/2` but only continues while `fun` returns `:ok`
and stops on `{:error, reason}`.
"""
def each_ok([head | tail], fun) do
case fun.(head) do
:ok -> each_ok(tail, fun)
{:error, reason} -> {:error, reason}
end
end
def each_ok([], _fun), do: :ok
@doc """
Like `Enum.map_reduce/3` but only continues while `fun` returns `{:ok, elem, acc}`
and stops on `{:error, reason}`.
+92 -41
View File
@@ -6,41 +6,81 @@ defmodule Module.Types.Of do
@prefix quote(do: ...)
@suffix quote(do: ...)
alias Module.Types.Infer
alias Module.ParallelChecker
import Module.Types.Helpers
import Module.Types.Unify
# There are important assumptions on how we work with maps.
#
# First, the keys in the map must be ordered by subtyping.
#
# Second, optional keys must be a superset of the required
# keys, i.e. %{required(atom) => integer, optional(:foo) => :bar}
# is forbidden.
#
# Third, in order to preserve co/contra-variance, a supertype
# must satisfy its subtypes. I.e. %{foo: :bar, atom() => :baz}
# is forbidden, it must be %{foo: :bar, atom() => :baz | :bar}.
#
# Once we support user declared maps, we need to validate these
# assumptions.
@doc """
Handles open maps (with dynamic => dynamic).
"""
def open_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
{:ok, {:map, pairs_to_unions(pairs, context) ++ [{:optional, :dynamic, :dynamic}]}, context}
def open_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
# If we match on a map such as %{"foo" => "bar"}, we cannot
# assert that %{binary() => binary()}, since we are matching
# only a single binary of infinite possible values. Therefore,
# the correct would be to match it to %{binary() => binary() | var}.
#
# We can skip this in two cases:
#
# 1. If the key is a singleton, then we know that it has no
# other value than the current one
#
# 2. If the value is a variable, then there is no benefit in
# creating another variable, so we can skip it
#
# For now, we skip generating the var itself and introduce
# :dynamic instead.
pairs =
for {key, value} <- pairs, not has_unbound_var?(key, context) do
if singleton?(key, context) or match?({:var, _}, value) do
{key, value}
else
{key, to_union([value, :dynamic], context)}
end
end
triplets = pairs_to_unions(pairs, [], context) ++ [{:optional, :dynamic, :dynamic}]
{:ok, {:map, triplets}, context}
end
end
@doc """
Handles closed maps (without dynamic => dynamic).
"""
def closed_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
{:ok, {:map, pairs_to_unions(pairs, context)}, context}
def closed_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
{:ok, {:map, closed_to_unions(pairs, context)}, context}
end
end
defp map_pairs(pairs, stack, context, fun) do
defp map_pairs(pairs, stack, context, of_fun) do
map_reduce_ok(pairs, context, fn {key, value}, context ->
with {:ok, key_type, context} <- fun.(key, stack, context),
{:ok, value_type, context} <- fun.(value, stack, context),
with {:ok, key_type, context} <- of_fun.(key, :dynamic, stack, context),
{:ok, value_type, context} <- of_fun.(value, :dynamic, stack, context),
do: {:ok, {key_type, value_type}, context}
end)
end
defp pairs_to_unions([{key, value}], _context), do: [{:required, key, value}]
defp closed_to_unions([{key, value}], _context), do: [{:required, key, value}]
defp pairs_to_unions(pairs, context) do
case Enum.split_with(pairs, fn {key, _value} -> Infer.has_unbound_var?(key, context) end) do
defp closed_to_unions(pairs, context) do
case Enum.split_with(pairs, fn {key, _value} -> has_unbound_var?(key, context) end) do
{[], pairs} -> pairs_to_unions(pairs, [], context)
{[_ | _], pairs} -> pairs_to_unions([{:dynamic, :dynamic} | pairs], [], context)
end
@@ -57,17 +97,17 @@ defmodule Module.Types.Of do
find_subtype_values(ahead, key, context) ++
find_subtype_values(behind, key, context)
pairs_to_unions(ahead, [{key, Infer.to_union(all_values, context)} | behind], context)
pairs_to_unions(ahead, [{key, to_union(all_values, context)} | behind], context)
end
defp pairs_to_unions([], acc, context) do
acc
|> Enum.sort(&Infer.subtype?(elem(&1, 0), elem(&2, 0), context))
|> Enum.sort(&subtype?(elem(&1, 0), elem(&2, 0), context))
|> Enum.map(fn {key, value} -> {:required, key, value} end)
end
defp find_subtype_values(pairs, key, context) do
for {pair_key, pair_value} <- pairs, Infer.subtype?(pair_key, key, context), do: pair_value
for {pair_key, pair_value} <- pairs, subtype?(pair_key, key, context), do: pair_value
end
defp find_matching_values([{key, value} | ahead], key, acc, values) do
@@ -103,39 +143,39 @@ defmodule Module.Types.Of do
In the stack, we add nodes such as <<expr>>, <<..., expr>>, etc,
based on the position of the expression within the binary.
"""
def binary([], _stack, context, _fun) do
def binary([], _stack, context, _of_fun) do
{:ok, context}
end
def binary([head], stack, context, fun) do
def binary([head], stack, context, of_fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head]}, stack)
binary_segment(head, head_stack, context, fun)
binary_segment(head, head_stack, context, of_fun)
end
def binary([head | tail], stack, context, fun) do
def binary([head | tail], stack, context, of_fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head, @suffix]}, stack)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_many([last], stack, context, fun) do
defp binary_many([last], stack, context, of_fun) do
last_stack = push_expr_stack({:<<>>, get_meta(last), [@prefix, last]}, stack)
binary_segment(last, last_stack, context, fun)
binary_segment(last, last_stack, context, of_fun)
end
defp binary_many([head | tail], stack, context, fun) do
defp binary_many([head | tail], stack, context, of_fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [@prefix, head, @suffix]}, stack)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, fun) do
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, of_fun) do
expected_type =
collect_binary_specifier(specifiers, &binary_type(stack.context, &1)) || :integer
@@ -152,17 +192,12 @@ defmodule Module.Types.Of do
{:ok, context}
true ->
with {:ok, type, context} <- fun.(expr, stack, context),
{:ok, _type, context} <- Infer.unify(type, expected_type, stack, context),
with {:ok, type, context} <- of_fun.(expr, expected_type, stack, context),
{:ok, _type, context} <- unify(type, expected_type, stack, context),
do: {:ok, context}
end
end
# TODO: Remove this clause once we properly handle comprehensions
defp binary_segment({:<-, _, _}, _stack, context, _fun) do
{:ok, context}
end
# Collect binary type specifiers,
# from `<<pattern::integer-size(10)>>` collect `integer`
defp collect_binary_specifier({:-, _meta, [left, right]}, fun) do
@@ -173,7 +208,7 @@ defmodule Module.Types.Of do
fun.(other)
end
defp binary_type(:expr, {:float, _, _}), do: :number
defp binary_type(:expr, {:float, _, _}), do: {:union, [:integer, :float]}
defp binary_type(:expr, {:utf8, _, _}), do: {:union, [:integer, :binary]}
defp binary_type(:expr, {:utf16, _, _}), do: {:union, [:integer, :binary]}
defp binary_type(:expr, {:utf32, _, _}), do: {:union, [:integer, :binary]}
@@ -214,12 +249,12 @@ defmodule Module.Types.Of do
defp check_export(module, fun, arity, meta, context) do
case ParallelChecker.fetch_export(context.cache, module, fun, arity) do
{:ok, :def, reason} ->
check_deprecated(module, fun, arity, reason, meta, context)
{:ok, mode, :def, reason} ->
check_deprecated(mode, module, fun, arity, reason, meta, context)
{:ok, :defmacro, reason} ->
{:ok, mode, :defmacro, reason} ->
context = warn(meta, context, {:unrequired_module, module, fun, arity})
check_deprecated(module, fun, arity, reason, meta, context)
check_deprecated(mode, module, fun, arity, reason, meta, context)
{:error, :module} ->
if warn_undefined?(module, fun, arity, context) do
@@ -238,7 +273,7 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(module, fun, arity, reason, meta, context) do
defp check_deprecated(:elixir, module, fun, arity, reason, meta, context) do
if reason do
warn(meta, context, {:deprecated, module, fun, arity, reason})
else
@@ -246,6 +281,22 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(:erlang, module, fun, arity, _reason, meta, context) do
case :otp_internal.obsolete(module, fun, arity) do
{:deprecated, string} when is_list(string) ->
reason = string |> List.to_string() |> String.capitalize()
warn(meta, context, {:deprecated, module, fun, arity, reason})
{:deprecated, string, removal} when is_list(string) and is_list(removal) ->
reason = string |> List.to_string() |> String.capitalize()
reason = "It will be removed in #{removal}. #{reason}"
warn(meta, context, {:deprecated, module, fun, arity, reason})
_ ->
context
end
end
# The protocol code dispatches to unknown modules, so we ignore them here.
#
# try do
+286 -320
View File
@@ -2,7 +2,7 @@ defmodule Module.Types.Pattern do
@moduledoc false
alias Module.Types.Of
import Module.Types.{Helpers, Infer}
import Module.Types.{Helpers, Unify}
@doc """
Handles patterns and guards at once.
@@ -24,113 +24,14 @@ defmodule Module.Types.Pattern do
of_pattern(pattern, %{stack | context: :pattern}, context)
end
# :atom
def of_pattern(atom, _stack, context) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
def of_pattern(literal, _stack, context) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
def of_pattern(literal, _stack, context) when is_float(literal) do
{:ok, :float, context}
end
# "..."
def of_pattern(literal, _stack, context) when is_binary(literal) do
{:ok, :binary, context}
end
# <<...>>>
def of_pattern({:<<>>, _meta, args}, stack, context) do
result = Of.binary(args, stack, context, &of_pattern/3)
case result do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
def of_pattern({:|, _meta, [left_expr, []]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
of_pattern(left_expr, stack, context)
end
# left | right
def of_pattern({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_pattern(left_expr, stack, context) do
{:ok, left, context} ->
case of_pattern(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
{:ok, right, context} ->
{:ok, to_union([left, right], context), context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# []
def of_pattern([], _stack, context) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
def of_pattern(exprs, stack, context) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# left ++ right
def of_pattern(
{{:., _meta1, [:erlang, :++]}, _meta2, [left_expr, right_expr]} = expr,
stack,
context
) do
stack = push_expr_stack(expr, stack)
case of_pattern(left_expr, stack, context) do
{:ok, {:list, left}, context} ->
case of_pattern(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:ok, right, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# _
def of_pattern({:_, _meta, atom}, _stack, context) when is_atom(atom) do
{:ok, :dynamic, context}
end
# ^var
def of_pattern({:^, _meta, [var]}, stack, context) do
of_pattern(var, stack, context)
def of_pattern({:^, _meta, [var]}, _stack, context) do
{:ok, get_var!(var, context), context}
end
# var
@@ -139,21 +40,6 @@ defmodule Module.Types.Pattern do
{:ok, type, context}
end
# {left, right}
def of_pattern({left, right}, stack, context) do
of_pattern({:{}, [], [left, right]}, stack, context)
end
# {...}
def of_pattern({:{}, _meta, exprs} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
{:error, reason} -> {:error, reason}
end
end
# left = right
def of_pattern({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
@@ -163,23 +49,6 @@ defmodule Module.Types.Pattern do
do: unify(left_type, right_type, stack, context)
end
# %{...}
def of_pattern({:%{}, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
Of.open_map(args, stack, context, &of_pattern/3)
end
# %Struct{...}
def of_pattern({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context)
when is_atom(module) do
stack = push_expr_stack(expr, stack)
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
unify(map, struct, stack, context)
end
end
# %_{...}
def of_pattern(
{:%, _meta1, [{:_, _meta2, var_context}, {:%{}, _meta3, args}]} = expr,
@@ -188,72 +57,72 @@ defmodule Module.Types.Pattern do
)
when is_atom(var_context) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, {:map, [{:required, {:atom, :__struct__}, :atom} | pairs]}, context}
end
end
# %^var{...}
def of_pattern({:%, meta1, [{:^, _meta2, [var]}, args]}, stack, context) do
of_pattern({:%, meta1, [var, args]}, stack, context)
end
# %var{...}
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context) do
# %var{...} and %^var{...}
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
when not is_atom(var) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {var_type, context} = new_var(var, context),
with {:ok, var_type, context} = of_pattern(var, stack, context),
{:ok, _, context} <- unify(var_type, :atom, stack, context),
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, {:map, [{:required, {:atom, :__struct__}, var_type} | pairs]}, context}
end
end
def unify_kinds(:required, _), do: :required
def unify_kinds(_, :required), do: :required
def unify_kinds(:optional, :optional), do: :optional
def of_pattern(expr, stack, context) do
of_shared(expr, stack, context, &of_pattern/3)
end
## GUARDS
# TODO: Some guards can be changed to intersection types or higher order types
@boolean {:union, [{:atom, true}, {:atom, false}]}
@number {:union, [:integer, :float]}
@guard_functions %{
{:is_atom, 1} => {[:atom], :boolean},
{:is_binary, 1} => {[:binary], :boolean},
{:is_bitstring, 1} => {[:binary], :boolean},
{:is_boolean, 1} => {[:boolean], :boolean},
{:is_float, 1} => {[:float], :boolean},
{:is_function, 1} => {[:fun], :boolean},
{:is_function, 2} => {[:fun, :integer], :boolean},
{:is_integer, 1} => {[:integer], :boolean},
{:is_list, 1} => {[{:list, :dynamic}], :boolean},
{:is_map, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], :boolean},
{:is_atom, 1} => {[:atom], @boolean},
{:is_binary, 1} => {[:binary], @boolean},
{:is_bitstring, 1} => {[:binary], @boolean},
{:is_boolean, 1} => {[@boolean], @boolean},
{:is_float, 1} => {[:float], @boolean},
{:is_function, 1} => {[:fun], @boolean},
{:is_function, 2} => {[:fun, :integer], @boolean},
{:is_integer, 1} => {[:integer], @boolean},
{:is_list, 1} => {[{:list, :dynamic}], @boolean},
{:is_map, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], @boolean},
{:is_map_key, 2} => {[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic},
{:is_number, 1} => {[:number], :boolean},
{:is_pid, 1} => {[:pid], :boolean},
{:is_port, 1} => {[:port], :boolean},
{:is_reference, 1} => {[:reference], :boolean},
{:is_tuple, 1} => {[:tuple], :boolean},
{:<, 2} => {[:dynamic, :dynamic], :boolean},
{:"=<", 2} => {[:dynamic, :dynamic], :boolean},
{:>, 2} => {[:dynamic, :dynamic], :boolean},
{:>=, 2} => {[:dynamic, :dynamic], :boolean},
{:"/=", 2} => {[:dynamic, :dynamic], :boolean},
{:"=/=", 2} => {[:dynamic, :dynamic], :boolean},
{:==, 2} => {[:dynamic, :dynamic], :boolean},
{:"=:=", 2} => {[:dynamic, :dynamic], :boolean},
{:*, 2} => {[:number, :number], :number},
{:+, 1} => {[:number], :number},
{:+, 2} => {[:number, :number], :number},
{:-, 1} => {[:number], :number},
{:-, 2} => {[:number, :number], :number},
{:/, 2} => {[:number, :number], :number},
{:abs, 1} => {[:number], :number},
{:ceil, 1} => {[:number], :integer},
{:floor, 1} => {[:number], :integer},
{:round, 1} => {[:number], :integer},
{:trunc, 1} => {[:number], :integer},
{:is_number, 1} => {[@number], @boolean},
{:is_pid, 1} => {[:pid], @boolean},
{:is_port, 1} => {[:port], @boolean},
{:is_reference, 1} => {[:reference], @boolean},
{:is_tuple, 1} => {[:tuple], @boolean},
{:<, 2} => {[:dynamic, :dynamic], @boolean},
{:"=<", 2} => {[:dynamic, :dynamic], @boolean},
{:>, 2} => {[:dynamic, :dynamic], @boolean},
{:>=, 2} => {[:dynamic, :dynamic], @boolean},
{:"/=", 2} => {[:dynamic, :dynamic], @boolean},
{:"=/=", 2} => {[:dynamic, :dynamic], @boolean},
{:==, 2} => {[:dynamic, :dynamic], @boolean},
{:"=:=", 2} => {[:dynamic, :dynamic], @boolean},
{:*, 2} => {[@number, @number], @number},
{:+, 1} => {[@number], @number},
{:+, 2} => {[@number, @number], @number},
{:-, 1} => {[@number], @number},
{:-, 2} => {[@number, @number], @number},
{:/, 2} => {[@number, @number], @number},
{:abs, 1} => {[@number], @number},
{:ceil, 1} => {[@number], :integer},
{:floor, 1} => {[@number], :integer},
{:round, 1} => {[@number], :integer},
{:trunc, 1} => {[@number], :integer},
{:element, 2} => {[:integer, :tuple], :dynamic},
{:hd, 1} => {[{:list, :dynamic}], :dynamic},
{:length, 1} => {[{:list, :dynamic}], :integer},
@@ -265,7 +134,7 @@ defmodule Module.Types.Pattern do
{:binary_part, 3} => {[:binary, :integer, :integer], :binary},
{:bit_size, 1} => {[:binary], :integer},
{:byte_size, 1} => {[:binary], :integer},
{:size, 1} => {[{:union, [:binary, :tuple]}], :boolean},
{:size, 1} => {[{:union, [:binary, :tuple]}], @boolean},
{:div, 2} => {[:integer, :integer], :integer},
{:rem, 2} => {[:integer, :integer], :integer},
{:node, 0} => {[], :atom},
@@ -276,15 +145,15 @@ defmodule Module.Types.Pattern do
{:bxor, 2} => {[:integer, :integer], :integer},
{:bsl, 2} => {[:integer, :integer], :integer},
{:bsr, 2} => {[:integer, :integer], :integer},
{:or, 2} => {[:boolean, :boolean], :boolean},
{:and, 2} => {[:boolean, :boolean], :boolean},
{:xor, 2} => {[:boolean, :boolean], :boolean},
{:not, 1} => {[:boolean], :boolean}
{:or, 2} => {[@boolean, @boolean], @boolean},
{:and, 2} => {[@boolean, @boolean], @boolean},
{:xor, 2} => {[@boolean, @boolean], @boolean},
{:not, 1} => {[@boolean], @boolean}
# Following guards are matched explicitly to handle
# type guard functions such as is_atom/1
# {:andalso, 2} => {[:boolean, :boolean], :boolean}
# {:orelse, 2} => {[:boolean, :boolean], :boolean}
# {:andalso, 2} => {[@boolean, @boolean], @boolean}
# {:orelse, 2} => {[@boolean, @boolean], @boolean}
}
@type_guards [
@@ -315,25 +184,31 @@ defmodule Module.Types.Pattern do
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_and(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
{:ok, _, context} <- unify(right_type, :boolean, stack, context),
do: {:ok, :boolean, context}
with {:ok, left_type, context} <- of_guard(left, stack, context),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
{:ok, right_type, context} <- of_guard(right, keep_guarded(stack), context),
do: {:ok, to_union([@boolean, right_type], context), context}
end
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
left_indexes = collect_var_indexes_from_expr(left, context)
right_indexes = collect_var_indexes_from_expr(right, context)
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, _right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_or(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
do: {:ok, :boolean, context}
with {:ok, left_type, left_context} <- of_guard(left, stack, context),
{:ok, _right_type, right_context} <- of_guard(right, stack, context),
context =
merge_context_or(
left_indexes,
right_indexes,
context,
stack,
left_context,
right_context
),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
do: {:ok, @boolean, context}
end
# The unary operators + and - are special cased to avoid common warnings until
@@ -355,12 +230,13 @@ defmodule Module.Types.Pattern do
stack = push_expr_stack(expr, stack)
{param_types, return_type} = guard_signature(guard, length(args))
type_guard? = type_guard?(guard)
{consider_type_guards?, keep_guarded?} = stack.type_guards
# Only check type guards in the context of and/or/not,
# a type guard in the context of is_tuple(x) > :foo
# should not affect the inference of x
if not type_guard? or stack.type_guards_enabled? do
arg_stack = %{stack | type_guards_enabled?: type_guard?}
if not type_guard? or consider_type_guards? do
arg_stack = %{stack | type_guards: {false, keep_guarded?}}
with {:ok, arg_types, context} <-
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
@@ -368,9 +244,7 @@ defmodule Module.Types.Pattern do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources =
Map.update(context.guard_sources, index, [:guarded], &[:guarded | &1])
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
{rest_arg_types, guard_sources}
_ ->
@@ -380,7 +254,7 @@ defmodule Module.Types.Pattern do
guard_sources =
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, [:fail], &[:fail | &1])
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
_, guard_sources ->
guard_sources
@@ -400,20 +274,26 @@ defmodule Module.Types.Pattern do
# var
def of_guard(var, _stack, context) when is_var(var) do
type = Map.fetch!(context.vars, var_name(var))
{:ok, type, context}
{:ok, get_var!(var, context), context}
end
# other literals
def of_guard(expr, stack, context) do
# Fall back to of_pattern/3 for literals
of_pattern(expr, stack, context)
of_shared(expr, stack, context, &of_guard/3)
end
defp fresh_context(context) do
types = Map.new(context.types, fn {var, _} -> {var, :unbound} end)
traces = Map.new(context.traces, fn {var, _} -> {var, []} end)
%{context | types: types, traces: traces}
defp collect_var_indexes_from_expr(expr, context) do
{_, vars} =
Macro.prewalk(expr, %{}, fn
var, acc when is_var(var) ->
var_name = var_name(var)
%{^var_name => type} = context.vars
{var, collect_var_indexes(type, context, acc)}
other, acc ->
{other, acc}
end)
Map.keys(vars)
end
defp unify_call(args, params, stack, context) do
@@ -425,126 +305,76 @@ defmodule Module.Types.Pattern do
end)
end
defp merge_context_and(context, stack, left, right) do
with {:ok, context} <- unify_new_types(context, stack, left),
{:ok, context} <- unify_new_types(context, stack, right) do
guard_sources = and_guard_sources(left.guard_sources, right.guard_sources)
guard_sources = merge_guard_sources([context.guard_sources, guard_sources])
{:ok, %{context | guard_sources: guard_sources}}
defp merge_context_or(left_indexes, right_indexes, context, stack, left, right) do
left_different = filter_different_indexes(left_indexes, left, right)
right_different = filter_different_indexes(right_indexes, left, right)
case {left_different, right_different} do
{[index], [index]} -> merge_context_or_equal(index, stack, left, right)
{_, _} -> merge_context_or_diff(left_different, context, left)
end
end
defp unify_new_types(context, stack, new_context) do
context = merge_traces(context, new_context)
defp filter_different_indexes(indexes, left, right) do
Enum.filter(indexes, fn index ->
%{^index => left_type} = left.types
%{^index => right_type} = right.types
left_type != right_type
end)
end
reduce_ok(Map.to_list(new_context.types), context, fn
{_index, :unbound}, context ->
{:ok, context}
defp merge_context_or_equal(index, stack, left, right) do
%{^index => left_type} = left.types
%{^index => right_type} = right.types
{index, new_type}, context ->
case unify({:var, index}, new_type, %{stack | trace: false}, context) do
{:ok, _, context} ->
{:ok, context}
cond do
left_type == :unbound ->
refine_var!(index, right_type, stack, left)
{:error, reason} ->
{:error, reason}
right_type == :unbound ->
left
true ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
if left.guard_sources[index] == :fail do
guard_sources = Map.put(left.guard_sources, index, :fail)
left = %{left | guard_sources: guard_sources}
refine_var!(index, left_type, stack, left)
else
guard_sources = merge_guard_sources([left.guard_sources, right.guard_sources])
left = %{left | guard_sources: guard_sources}
refine_var!(index, to_union([left_type, right_type], left), stack, left)
end
end
end
# If the variable failed, we can keep them from the left side as is.
# If they didn't fail, then we need to restore them to their original value.
defp merge_context_or_diff(indexes, old_context, new_context) do
Enum.reduce(indexes, new_context, fn index, context ->
if new_context.guard_sources[index] == :fail do
context
else
restore_var!(index, new_context, old_context)
end
end)
end
defp merge_guard_sources(sources) do
Enum.reduce(sources, fn left, right ->
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
Map.merge(left, right, fn
_index, :guarded, :guarded -> :guarded
_index, _, _ -> :fail
end)
end)
end
defp join_guard_source(left, right) do
sources = left ++ right
defp guarded_if_keep_guarded(:guarded, true), do: :guarded
defp guarded_if_keep_guarded(_, _), do: :fail
cond do
:fail in sources -> [:fail]
:guarded in sources -> [:guarded]
true -> []
end
end
defp and_guard_sources(left, right) do
Map.merge(left, right, fn _index, left, right ->
# When the failing guard function wont fail due to type check function before it,
# for example: is_list(x) and length(x)
if :guarded in left and :fail in right do
[:guarded]
else
join_guard_source(left, right)
end
end)
end
defp merge_traces(context, new_context) do
traces =
:maps.fold(
fn index, new_traces, traces ->
:maps.update_with(index, &(new_traces ++ &1), new_traces, traces)
end,
context.traces,
new_context.traces
)
%{context | traces: traces}
end
defp merge_context_or(context, stack, left, right) do
context =
case {Map.to_list(left.types), Map.to_list(right.types)} do
{[{index, :unbound}], [{index, type}]} ->
refine_var(index, type, stack, context)
{[{index, type}], [{index, :unbound}]} ->
refine_var(index, type, stack, context)
{[{index, left_type}], [{index, right_type}]} ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources = Map.put(context.guard_sources, index, [:fail])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, to_union([left_type, right_type], context), stack, context)
end
{left_types, _right_types} ->
Enum.reduce(left_types, context, fn {index, left_type}, context ->
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
context
end
end)
end
{:ok, context}
end
defp keep_guarded(%{type_guards: {consider?, _}} = stack),
do: %{stack | type_guards: {consider?, true}}
defp guard_signature(name, arity) do
Map.fetch!(@guard_functions, {name, arity})
@@ -553,4 +383,140 @@ defmodule Module.Types.Pattern do
defp type_guard?(name) do
name in @type_guards
end
## Shared
# :atom
defp of_shared(atom, _stack, context, _fun) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
defp of_shared(literal, _stack, context, _fun) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
defp of_shared(literal, _stack, context, _fun) when is_float(literal) do
{:ok, :float, context}
end
# "..."
defp of_shared(literal, _stack, context, _fun) when is_binary(literal) do
{:ok, :binary, context}
end
# <<...>>>
defp of_shared({:<<>>, _meta, args}, stack, context, fun) do
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
case Of.binary(args, stack, context, expected_fun) do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
defp of_shared({:|, _meta, [left_expr, []]} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
fun.(left_expr, stack, context)
end
# left | right
defp of_shared({:|, _meta, [left_expr, right_expr]} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
case fun.(left_expr, stack, context) do
{:ok, left, context} ->
case fun.(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
{:ok, right, context} ->
{:ok, to_union([left, right], context), context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# []
defp of_shared([], _stack, context, _fun) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
defp of_shared(exprs, stack, context, fun) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &fun.(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# left ++ right
defp of_shared(
{{:., _meta1, [:erlang, :++]}, _meta2, [left_expr, right_expr]} = expr,
stack,
context,
fun
) do
stack = push_expr_stack(expr, stack)
case fun.(left_expr, stack, context) do
{:ok, {:list, left}, context} ->
case fun.(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:ok, right, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# {left, right}
defp of_shared({left, right}, stack, context, fun) do
of_shared({:{}, [], [left, right]}, stack, context, fun)
end
# {...}
defp of_shared({:{}, _meta, exprs} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &fun.(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
{:error, reason} -> {:error, reason}
end
end
# %{...}
defp of_shared({:%{}, _meta, args} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
Of.open_map(args, stack, context, expected_fun)
end
# %Struct{...}
defp of_shared({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context, fun)
when is_atom(module) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, expected_fun) do
unify(map, struct, stack, context)
end
end
end
@@ -1,4 +1,4 @@
defmodule Module.Types.Infer do
defmodule Module.Types.Unify do
@moduledoc false
import Module.Types.Helpers
@@ -21,11 +21,6 @@ defmodule Module.Types.Infer do
# {:union, [type]}
# {:map, [{:required | :optional, key_type, value_type}]}
#
# TODO: Those types should be removed:
#
# :boolean
# :number
#
# Once new types are added, they should be considered in:
#
# * unify (all)
@@ -33,6 +28,8 @@ defmodule Module.Types.Infer do
# * subtype? (subtypes only)
# * has_unbound_var? (composite only)
# * recursive_type? (composite only)
# * collect_vars (composite only)
# * lift_types (composite only)
#
@doc """
@@ -64,23 +61,11 @@ defmodule Module.Types.Infer do
end
defp do_unify(type, {:var, var}, stack, context) do
case context.types do
%{^var => {:var, var_type}} ->
do_unify(type, {:var, var_type}, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = false)
end
unify_var(var, type, stack, context, _var_source = false)
end
defp do_unify({:var, var}, type, stack, context) do
case context.types do
%{^var => {:var, var_type}} ->
do_unify({:var, var_type}, type, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = true)
end
unify_var(var, type, stack, context, _var_source = true)
end
defp do_unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
@@ -114,14 +99,22 @@ defmodule Module.Types.Infer do
{:ok, target, context}
end
defp do_unify({:union, types}, target, stack, context) do
unify_result =
map_reduce_ok(types, context, fn type, context ->
unify(type, target, stack, context)
end)
case unify_result do
{:ok, types, context} -> {:ok, to_union(types, context), context}
{:error, context} -> {:error, context}
end
end
defp do_unify(source, target, stack, context) do
cond do
# This condition exists to handle unions with unbound vars.
# TODO: handle unions properly. Note we can easily unify
# "union < type" even if union has vars as the vars must be
# type
(match?({:union, _}, source) and has_unbound_var?(source, context)) or
(match?({:union, _}, target) and has_unbound_var?(target, context)) ->
# TODO: This condition exists to handle unions with unbound vars.
match?({:union, _}, target) and has_unbound_var?(target, context) ->
{:ok, source, context}
subtype?(source, target, context) ->
@@ -139,7 +132,7 @@ defmodule Module.Types.Infer do
defp unify_var(var, type, stack, context, var_source?) do
case context.types do
%{^var => :unbound} ->
context = refine_var(var, type, stack, context)
context = refine_var!(var, type, stack, context)
stack = push_unify_stack(var, stack)
if recursive_type?(type, [], context) do
@@ -152,6 +145,31 @@ defmodule Module.Types.Infer do
{:ok, {:var, var}, context}
end
%{^var => {:var, new_var} = var_type} ->
unify_result =
if var_source? do
unify(var_type, type, stack, context)
else
unify(type, var_type, stack, context)
end
case unify_result do
{:ok, type, context} ->
{:ok, type, context}
{:error, {type, reason, %{traces: error_traces} = error_context}} ->
old_var_traces = Map.get(context.traces, new_var, [])
new_var_traces = Map.get(error_traces, new_var, [])
add_var_traces = Enum.drop(new_var_traces, -length(old_var_traces))
error_traces =
error_traces
|> Map.update(var, add_var_traces, &(add_var_traces ++ &1))
|> Map.put(new_var, old_var_traces)
{:error, {type, reason, %{error_context | traces: error_traces}}}
end
%{^var => var_type} ->
# Only add trace if the variable wasn't already "expanded"
context =
@@ -171,8 +189,11 @@ defmodule Module.Types.Infer do
end
case unify_result do
{:ok, var_type, context} ->
context = refine_var(var, var_type, stack, context)
{:ok, {:var, ^var}, context} ->
{:ok, {:var, var}, context}
{:ok, res_type, context} ->
context = refine_var!(var, res_type, stack, context)
{:ok, {:var, var}, context}
{:error, reason} ->
@@ -306,6 +327,23 @@ defmodule Module.Types.Infer do
defp error(type, reason, context), do: {:error, {type, reason, context}}
@doc """
Push expression to stack.
The expression stack is used to give the context where a type variable
was refined when show a type conflict error.
"""
def push_expr_stack(expr, stack) do
%{stack | last_expr: expr}
end
@doc """
Gets a variable.
"""
def get_var!(var, context) do
Map.fetch!(context.vars, var_name(var))
end
@doc """
Adds a variable to the typing context and returns its type variable.
If the variable has already been added, return the existing type variable.
@@ -391,10 +429,21 @@ defmodule Module.Types.Infer do
%{stack | unify_stack: [var | stack.unify_stack]}
end
@doc """
Restores the variable information from the old context into new context.
"""
def restore_var!(var, new_context, old_context) do
%{^var => type} = old_context.types
%{^var => trace} = old_context.traces
types = Map.put(new_context.types, var, type)
traces = Map.put(new_context.traces, var, trace)
%{new_context | types: types, traces: traces}
end
@doc """
Set the type for a variable and add trace.
"""
def refine_var(var, type, stack, context) do
def refine_var!(var, type, stack, context) do
types = Map.put(context.types, var, type)
context = %{context | types: types}
trace_var(var, type, stack, context)
@@ -460,6 +509,41 @@ defmodule Module.Types.Infer do
false
end
@doc """
Collects all type vars recursively.
"""
def collect_var_indexes(type, context, acc \\ %{})
def collect_var_indexes({:var, var}, context, acc) do
case acc do
%{^var => _} ->
acc
%{} ->
case context.types do
%{^var => :unbound} -> Map.put(acc, var, true)
%{^var => type} -> collect_var_indexes(type, context, Map.put(acc, var, true))
end
end
end
def collect_var_indexes({:tuple, _, args}, context, acc),
do: Enum.reduce(args, acc, &collect_var_indexes(&1, context, &2))
def collect_var_indexes({:union, args}, context, acc),
do: Enum.reduce(args, acc, &collect_var_indexes(&1, context, &2))
def collect_var_indexes({:list, arg}, context, acc),
do: collect_var_indexes(arg, context, acc)
def collect_var_indexes({:map, pairs}, context, acc) do
Enum.reduce(pairs, acc, fn {_, key, value}, acc ->
collect_var_indexes(value, context, collect_var_indexes(key, context, acc))
end)
end
def collect_var_indexes(_type, _context, acc), do: acc
@doc """
Checks if the type has a type var.
"""
@@ -487,14 +571,33 @@ defmodule Module.Types.Infer do
def has_unbound_var?(_type, _context), do: false
@doc """
Returns true if it is a singleton type.
Only atoms are singleton types. Unbound vars are not
considered singleton types.
"""
def singleton?({:var, var}, context) do
case context.types do
%{^var => :unbound} -> false
%{^var => type} -> singleton?(type, context)
end
end
def singleton?({:atom, _}, _context), do: true
def singleton?(_type, _context), do: false
@doc """
Checks if the first argument is a subtype of the second argument.
This function assumes that:
* dynamic is not considered a subtype of all other types but the top type
* unbound variables are not subtype of anything
* dynamic is not considered a subtype of all other types but the top type.
This allows this function can be used for ordering, in other cases, you
may need to check for both sides
"""
def subtype?(type, type, _context), do: true
@@ -515,11 +618,6 @@ defmodule Module.Types.Infer do
def subtype?(_, :dynamic, _context), do: true
def subtype?({:atom, atom}, :atom, _context) when is_atom(atom), do: true
def subtype?({:atom, boolean}, :boolean, _context) when is_boolean(boolean), do: true
def subtype?(:boolean, :atom, _context), do: true
def subtype?(:float, :number, _context), do: true
def subtype?(:integer, :number, _context), do: true
# Composite
def subtype?({:tuple, _, _}, :tuple, _context), do: true
@@ -527,7 +625,7 @@ defmodule Module.Types.Infer do
def subtype?({:tuple, n, left_types}, {:tuple, n, right_types}, context) do
left_types
|> Enum.zip(right_types)
|> Enum.any?(fn {left, right} -> subtype?(left, right, context) end)
|> Enum.all?(fn {left, right} -> subtype?(left, right, context) end)
end
def subtype?({:map, left_pairs}, {:map, right_pairs}, context) do
@@ -606,6 +704,90 @@ defmodule Module.Types.Infer do
[]
end
## Type lifting
@doc """
Lifts type variables to their inferred types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &lift_type/2)
types
end
# Lift type variable to its inferred (hopefully concrete) types from the context
defp lift_type({:var, var}, context) do
case context.lifted_types do
%{^var => lifted_var} ->
{{:var, lifted_var}, context}
%{} ->
case context.types do
%{^var => :unbound} ->
new_lifted_var(var, context)
%{^var => type} ->
if recursive_type?(type, [], context) do
new_lifted_var(var, context)
else
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = put_in(context.types[var], :unbound)
{type, context} = lift_type(type, context)
{type, %{context | types: types}}
end
%{} ->
new_lifted_var(var, context)
end
end
end
defp lift_type({:union, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:union, types}, context}
end
defp lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:tuple, n, types}, context}
end
defp lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = lift_type(key, context)
{value, context} = lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp lift_type({:list, type}, context) do
{type, context} = lift_type(type, context)
{{:list, type}, context}
end
defp lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
@doc """
Formats types.
@@ -649,7 +831,7 @@ defmodule Module.Types.Infer do
end
def format_type({:var, index}, _simplify?) do
"var#{index}"
"var#{index + 1}"
end
def format_type(atom, _simplify?) when is_atom(atom) do
+23 -14
View File
@@ -428,34 +428,32 @@ defmodule OptionParser do
end
end
# Handles -a, -abc, -abc=something
# Handles -a, -abc, -abc=something, -n2
defp next_with_config(["-" <> option | rest] = argv, config) do
%{allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
{option, value} = split_option(option)
original = "-" <> option
letters = String.graphemes(option)
cond do
is_nil(value) and negative_number?(original) ->
is_nil(value) and starts_with_number?(option) ->
{:error, argv}
String.contains?(option, ["-", "_"]) ->
{:undefined, original, value, rest}
tl(letters) == [] ->
String.length(option) == 1 ->
# We have a regular one-letter alias here
tagged = tag_oneletter_alias(option, config)
next_tagged(tagged, value, original, rest, config)
true ->
key = get_option_key(option, allow_nonexistent_atoms?)
key = get_option_key(option, config.allow_nonexistent_atoms?)
option_key = config.aliases[key]
if key && option_key do
IO.warn("multi-letter aliases are deprecated, got: #{inspect(key)}")
next_tagged({:default, option_key}, value, original, rest, config)
else
next_with_config(expand_multiletter_alias(letters, value) ++ rest, config)
next_with_config(expand_multiletter_alias(option, value) ++ rest, config)
end
end
end
@@ -707,13 +705,25 @@ defmodule OptionParser do
end
end
defp expand_multiletter_alias(letters, value) do
defp expand_multiletter_alias(options, value) do
{options, maybe_integer} =
options
|> String.to_charlist()
|> Enum.split_while(&(&1 not in ?0..?9))
{last, expanded} =
letters
options
|> List.to_string()
|> String.graphemes()
|> Enum.map(&("-" <> &1))
|> List.pop_at(-1)
expanded ++ [last <> if(value, do: "=" <> value, else: "")]
expanded ++
[
last <>
if(maybe_integer != [], do: "=#{maybe_integer}", else: "") <>
if(value, do: "=#{value}", else: "")
]
end
defp normalize_tag(:negated, option, value, switches) do
@@ -754,7 +764,7 @@ defmodule OptionParser do
defp value_in_tail?(["-" | _]), do: true
defp value_in_tail?(["- " <> _ | _]), do: true
defp value_in_tail?(["-" <> arg | _]), do: negative_number?("-" <> arg)
defp value_in_tail?(["-" <> arg | _]), do: starts_with_number?(arg)
defp value_in_tail?([]), do: false
defp value_in_tail?(_), do: true
@@ -786,9 +796,8 @@ defmodule OptionParser do
end
end
defp negative_number?(arg) do
match?({_, ""}, Float.parse(arg))
end
defp starts_with_number?(<<char, _::binary>>) when char in ?0..?9, do: true
defp starts_with_number?(_), do: false
defp format_errors([_ | _] = errors, opts) do
types = opts[:switches] || opts[:strict]
+27 -32
View File
@@ -66,7 +66,7 @@ defmodule Path do
case type(path) do
:relative ->
absname_join(relative_to, path)
absname_join([relative_to, path])
:absolute ->
absname_join([path])
@@ -80,11 +80,11 @@ defmodule Path do
# Absolute path on current drive
defp absname_vr(["/" | rest], [volume | _], _relative), do: absname_join([volume | rest])
# Relative to current directory on current drive.
# Relative to current directory on current drive
defp absname_vr([<<x, ?:>> | rest], [<<x, _::binary>> | _], relative),
do: absname(absname_join(rest), relative)
# Relative to current directory on another drive.
# Relative to current directory on another drive
defp absname_vr([<<x, ?:>> | name], _, _relative) do
cwd =
case :file.get_cwd([x, ?:]) do
@@ -97,25 +97,25 @@ defmodule Path do
@slash [?/, ?\\]
# Joins a list
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
defp absname_join([]), do: ""
defp absname_join(list), do: absname_join(list, major_os_type())
defp absname_join([name]),
do: do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
defp absname_join([name1, name2 | rest], os_type) do
joined = do_absname_join(IO.chardata_to_string(name1), relative(name2), [], os_type)
absname_join([joined | rest], os_type)
end
# Joins two paths
defp absname_join(left, right),
do: do_absname_join(IO.chardata_to_string(left), relative(right), [], major_os_type())
defp absname_join([name], os_type) do
do_absname_join(IO.chardata_to_string(name), <<>>, [], os_type)
end
defp do_absname_join(<<uc_letter, ?:, rest::binary>>, relativename, [], :win32)
when uc_letter in ?A..?Z do
do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
end
when uc_letter in ?A..?Z,
do: do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
defp do_absname_join(<<c1, c2, rest::binary>>, relativename, [], :win32)
when c1 in @slash and c2 in @slash do
do_absname_join(rest, relativename, '//', :win32)
end
when c1 in @slash and c2 in @slash,
do: do_absname_join(rest, relativename, '//', :win32)
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32),
do: do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
@@ -313,6 +313,9 @@ defmodule Path do
iex> Path.relative_to("/usr/local/foo", "/etc")
"/usr/local/foo"
iex> Path.relative_to("/usr/local/foo", "/usr/local/foo")
"."
"""
@spec relative_to(t, t) :: binary
def relative_to(path, from) do
@@ -320,6 +323,10 @@ defmodule Path do
relative_to(split(path), split(from), path)
end
defp relative_to(path, path, _original) do
"."
end
defp relative_to([h | t1], [h | t2], original) do
relative_to(t1, t2, original)
end
@@ -573,27 +580,15 @@ defmodule Path do
@moduledoc false
def read_link_info(file) do
call({:read_link_info, file})
:file.read_link_info(file)
end
def list_dir(dir) do
case call({:list_dir, dir}) do
{:ok, files} ->
{:ok, for(file <- files, hd(file) != ?., do: file)}
other ->
other
case :file.list_dir(dir) do
{:ok, files} -> {:ok, for(file <- files, hd(file) != ?., do: file)}
other -> other
end
end
@compile {:inline, call: 1}
defp call(tuple) do
x = :erlang.dt_spread_tag(true)
y = :gen_server.call(:file_server_2, tuple)
:erlang.dt_restore_tag(x)
y
end
end
@doc """
+1 -1
View File
@@ -448,7 +448,7 @@ defmodule Process do
If the process is already dead when calling `Process.monitor/1`, a
`:DOWN` message is delivered immediately.
See [the need for monitoring](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
See ["The need for monitoring"](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
for an example. See `:erlang.monitor/2` for more information.
Inlined by the compiler.
+59 -54
View File
@@ -4,11 +4,11 @@ defmodule Protocol do
A protocol specifies an API that should be defined by its
implementations. A protocol is defined with `Kernel.defprotocol/2`
and its implementations with `Kernel.defimpl/2`.
and its implementations with `Kernel.defimpl/3`.
## Examples
## A real case
In Elixir, we have two verbs for checking how many items there
In Elixir, we have two nouns for checking how many items there
are in a data structure: `length` and `size`. `length` means the
information must be computed. For example, `length(list)` needs to
traverse the whole list to calculate its length. On the other hand,
@@ -53,7 +53,7 @@ defmodule Protocol do
It is possible to implement protocols for all Elixir types:
* Structs (see below)
* Structs (see the "Protocols and Structs" section below)
* `Tuple`
* `Atom`
* `List`
@@ -65,7 +65,7 @@ defmodule Protocol do
* `Map`
* `Port`
* `Reference`
* `Any` (see below)
* `Any` (see the "Fallback to `Any`" section below)
## Protocols and Structs
@@ -79,7 +79,7 @@ defmodule Protocol do
end
When implementing a protocol for a struct, the `:for` option can
be omitted if the `defimpl` call is inside the module that defines
be omitted if the `defimpl/3` call is inside the module that defines
the struct:
defmodule User do
@@ -134,13 +134,13 @@ defmodule Protocol do
def reverse(term), do: Enum.reverse(term)
end
Inside `defimpl/2`, you can use `@protocol` to access the protocol
Inside `defimpl/3`, you can use `@protocol` to access the protocol
being implemented and `@for` to access the module it is being
defined for.
## Types
Defining a protocol automatically defines a type named `t`, which
Defining a protocol automatically defines a zero-arity type named `t`, which
can be used as follows:
@spec print_size(Size.t()) :: :ok
@@ -177,7 +177,7 @@ defmodule Protocol do
* `impl_for/1` - returns the module that implements the protocol for the given argument,
`nil` otherwise
* `impl_for!/1` - same as above but raises an error if an implementation is
* `impl_for!/1` - same as above but raises `Protocol.UndefinedError` if an implementation is
not found
For example, for the `Enumerable` protocol we have:
@@ -191,15 +191,17 @@ defmodule Protocol do
iex> Enumerable.impl_for(42)
nil
In addition, every protocol implementation module contains the `__impl__/1` function. The
function takes one of the following atoms:
In addition, every protocol implementation module contains the `__impl__/1`
function. The function takes one of the following atoms:
* `:for` - returns the module responsible for the data structure of the protocol implementation
* `:for` - returns the module responsible for the data structure of the
protocol implementation
* `:protocol` - returns the protocol module for which this implementation is provided
* `:protocol` - returns the protocol module for which this implementation
is provided
For example, the module implementing the `Enumerable` protocol for lists is `Enumerable.List`.
Therefore, we can invoke `__impl__/1` on this module:
For example, the module implementing the `Enumerable` protocol for lists is
`Enumerable.List`. Therefore, we can invoke `__impl__/1` on this module:
iex(1)> Enumerable.List.__impl__(:for)
List
@@ -209,22 +211,17 @@ defmodule Protocol do
## Consolidation
In order to cope with code loading in development, protocols in
Elixir provide a slow implementation of protocol dispatching specific
to development.
In order to speed up dispatching in production environments, where
all implementations are known up-front, Elixir provides a feature
called *protocol consolidation*. Consolidation directly links protocols
to their implementations in a way that invoking a function from a
In order to speed up protocol dispatching, whenever all protocol implementations
are known up-front, typically after all Elixir code in a project is compiled,
Elixir provides a feature called *protocol consolidation*. Consolidation directly
links protocols to their implementations in a way that invoking a function from a
consolidated protocol is equivalent to invoking two remote functions.
Protocol consolidation is applied by default to all Mix projects during
compilation. This may be an issue during test. For instance, if you want
to implement a protocol during test, the implementation will have no
effect, as the protocol has already been consolidated. One possible
solution is to include compilation directories that are specific to your
test environment in your mix.exs:
Protocol consolidation is applied by default to all Mix projects during compilation.
This may be an issue during test. For instance, if you want to implement a protocol
during test, the implementation will have no effect, as the protocol has already been
consolidated. One possible solution is to include compilation directories that are
specific to your test environment in your mix.exs:
def project do
...
@@ -311,9 +308,11 @@ defmodule Protocol do
end
defp assert_protocol!(module, extra) do
case Code.ensure_compiled(module) do
{:module, ^module} -> :ok
_ -> raise ArgumentError, "#{inspect(module)} is not available" <> extra
try do
Code.ensure_compiled!(module)
rescue
e in ArgumentError ->
raise ArgumentError, e.message <> extra
end
try do
@@ -340,9 +339,11 @@ defmodule Protocol do
defp assert_impl!(protocol, base, extra) do
impl = Module.concat(protocol, base)
case Code.ensure_compiled(impl) do
{:module, ^impl} -> :ok
_ -> raise ArgumentError, "#{inspect(impl)} is not available" <> extra
try do
Code.ensure_compiled!(impl)
rescue
e in ArgumentError ->
raise ArgumentError, e.message <> extra
end
try do
@@ -397,17 +398,19 @@ defmodule Protocol do
end
Derivable.ok(%ImplStruct{})
{:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
#=> {:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
Explicit derivations can now be called via `__deriving__`:
Explicit derivations can now be called via `__deriving__/3`:
# Explicitly derived via `__deriving__`
# Explicitly derived via `__deriving__/3`
Derivable.ok(%ImplStruct{a: 1, b: 1})
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, []}
# Explicitly derived by API via `__deriving__`
# Explicitly derived by API via `__deriving__/3`
require Protocol
Protocol.derive(Derivable, ImplStruct, :oops)
Derivable.ok(%ImplStruct{a: 1, b: 1})
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, :oops}
"""
defmacro derive(protocol, module, options \\ []) do
@@ -439,7 +442,7 @@ defmodule Protocol do
@spec extract_protocols([charlist | String.t()]) :: [atom]
def extract_protocols(paths) do
extract_matching_by_attribute(paths, 'Elixir.', fn module, attributes ->
case attributes[:protocol] do
case attributes[:__protocol__] do
[fallback_to_any: _] -> module
_ -> nil
end
@@ -470,7 +473,7 @@ defmodule Protocol do
prefix = Atom.to_charlist(protocol) ++ '.'
extract_matching_by_attribute(paths, prefix, fn _mod, attributes ->
case attributes[:protocol_impl] do
case attributes[:__impl__] do
[protocol: ^protocol, for: for] -> for
_ -> nil
end
@@ -561,7 +564,7 @@ defmodule Protocol do
chunks = :lists.filter(fn {_name, value} -> value != :missing_chunk end, chunks)
chunks = :lists.map(fn {name, value} -> {List.to_string(name), value} end, chunks)
case attributes[:protocol] do
case attributes[:__protocol__] do
[fallback_to_any: any] ->
{:ok, {any, definitions}, specs, {info, chunks}}
@@ -681,14 +684,16 @@ defmodule Protocol do
# We don't allow function definition inside protocols
import Kernel,
except: [
defmacrop: 1,
defmacrop: 2,
defmacro: 1,
defmacro: 2,
def: 1,
def: 2,
defp: 1,
defp: 2,
def: 1,
def: 2
defguard: 1,
defguardp: 1,
defmacro: 1,
defmacro: 2,
defmacrop: 1,
defmacrop: 2
]
# Import the new dsl that holds the new def
@@ -795,8 +800,8 @@ defmodule Protocol do
# Store information as an attribute so it
# can be read without loading the module.
Module.register_attribute(__MODULE__, :protocol, persist: true)
@protocol [fallback_to_any: !!@fallback_to_any]
Module.register_attribute(__MODULE__, :__protocol__, persist: true)
@__protocol__ [fallback_to_any: !!@fallback_to_any]
@doc false
@spec __protocol__(:module) :: __MODULE__
@@ -855,8 +860,8 @@ defmodule Protocol do
unquote(block)
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
@protocol_impl [protocol: @protocol, for: @for]
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: @protocol, for: @for]
unquote(impl)
end
@@ -897,8 +902,8 @@ defmodule Protocol do
else
quoted =
quote do
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
@protocol_impl [protocol: unquote(protocol), for: unquote(for)]
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: unquote(protocol), for: unquote(for)]
@doc false
@spec __impl__(:target) :: unquote(impl)
+256 -59
View File
@@ -1,23 +1,75 @@
defmodule Range do
@moduledoc """
Ranges represent a sequence of one or many, ascending
or descending, consecutive integers.
Ranges represent a sequence of zero, one or many, ascending
or descending integers with a common difference called step.
Ranges can be either increasing (`first <= last`) or
decreasing (`first > last`). Ranges are also always
inclusive.
Ranges are always inclusive and they may have custom steps.
The most common form of creating and matching on ranges is
via the [`first..last`](`../2`) and [`first..last//step`](`..///3`)
notations, auto-imported from `Kernel`:
A range is represented internally as a struct. However,
the most common form of creating and matching on ranges
is via the `../2` macro, auto-imported from `Kernel`:
iex> Enum.to_list(1..3)
[1, 2, 3]
iex> Enum.to_list(1..3//2)
[1, 3]
iex> Enum.to_list(3..1//-1)
[3, 2, 1]
iex> range = 1..3
1..3
iex> first..last = range
Ranges may also have a single element:
iex> Enum.to_list(1..1)
[1]
iex> Enum.to_list(1..1//2)
[1]
Or even no elements at all:
iex> Enum.to_list(10..0//1)
[]
iex> Enum.to_list(0..10//-1)
[]
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `first >= last`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicitly decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
## Definition
An increasing range `first..last//step` is a range from
`first` to `last` increasing by `step` where all values
`v` must be `first <= v and v <= last`. Therefore, a range
`10..0//1` is an empty range because there is no value `v`
that is `10 <= v and v <= 0`.
Similarly, a decreasing range `first..last//-step` is a range
from `first` to `last` decreasing by `step` where all values
`v` must be `first >= v and v >= last`. Therefore, a range
`0..10//-1` is an empty range because there is no value `v`
that is `0 >= v and v >= 10`.
## Representation
Internally, ranges are represented as structs:
iex> range = 1..9//2
1..9//2
iex> first..last//step = range
iex> first
1
iex> last
3
9
iex> step
2
iex> range.step
2
You can access the range fields (`first`, `last`, and `step`)
directly but you should not modify nor create ranges by hand.
Instead use the proper operators or `new/2` and `new/3`.
A range implements the `Enumerable` protocol, which means
functions in the `Enum` module can be used to work with
@@ -40,23 +92,37 @@ defmodule Range do
not materialize the whole list of integers.
"""
defstruct first: nil, last: nil
@enforce_keys [:first, :last, :step]
defstruct first: nil, last: nil, step: nil
@type t :: %__MODULE__{first: integer, last: integer}
@type t(first, last) :: %__MODULE__{first: first, last: last}
@type limit :: integer
@type step :: pos_integer | neg_integer
@type t :: %__MODULE__{first: limit, last: limit, step: step}
@type t(first, last) :: %__MODULE__{first: first, last: last, step: step}
@doc """
Creates a new range.
If `first` is less than `last`, the range will be increasing from
`first` to `last`. If `first` is equal to `last`, the range will contain
one element, which is the number itself.
If `first` is greater than `last`, the range will be decreasing from `first`
to `last`, albeit this behaviour is deprecated. Therefore, it is advised to
explicitly list the step with `new/3`.
## Examples
iex> Range.new(-100, 100)
-100..100
"""
@spec new(integer, integer) :: t
@spec new(limit, limit) :: t
def new(first, last) when is_integer(first) and is_integer(last) do
%Range{first: first, last: last}
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.17
step = if first <= last, do: 1, else: -1
%Range{first: first, last: last, step: step}
end
def new(first, last) do
@@ -65,6 +131,66 @@ defmodule Range do
"got: #{inspect(first)}..#{inspect(last)}"
end
@doc """
Creates a new range with `step`.
## Examples
iex> Range.new(-100, 100, 2)
-100..100//2
"""
@doc since: "1.12.0"
@spec new(limit, limit, step) :: t
def new(first, last, step)
when is_integer(first) and is_integer(last) and is_integer(step) and step != 0 do
%Range{first: first, last: last, step: step}
end
def new(first, last, step) do
raise ArgumentError,
"ranges (first..last//step) expect both sides to be integers and the step to be a " <>
"non-zero integer, got: #{inspect(first)}..#{inspect(last)}//#{inspect(step)}"
end
@doc """
Returns the size of `range`.
## Examples
iex> Range.size(1..10)
10
iex> Range.size(1..10//2)
5
iex> Range.size(1..10//3)
4
iex> Range.size(1..10//-1)
0
iex> Range.size(10..1)
10
iex> Range.size(10..1//-1)
10
iex> Range.size(10..1//-2)
5
iex> Range.size(10..1//-3)
4
iex> Range.size(10..1//1)
0
"""
@doc since: "1.12.0"
def size(range)
def size(first..last//step) when step > 0 and first > last, do: 0
def size(first..last//step) when step < 0 and first < last, do: 0
def size(first..last//step), do: abs(div(last - first, step)) + 1
# TODO: Remove me on v2.0
def size(%{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
size(Map.put(range, :step, step))
end
@doc """
Checks if two ranges are disjoint.
@@ -79,90 +205,161 @@ defmodule Range do
iex> Range.disjoint?(1..5, 2..7)
false
Steps are also considered when computing the ranges to be disjoint:
iex> Range.disjoint?(1..10//2, 2..10//2)
true
# First element in common in all below is 29
iex> Range.disjoint?(2..100//3, 9..100//5)
false
iex> Range.disjoint?(101..2//-3, 99..9//-5)
false
iex> Range.disjoint?(1..100//14, 8..100//21)
false
iex> Range.disjoint?(57..-1//-14, 8..100//21)
false
iex> Range.disjoint?(1..100//14, 51..8//-21)
false
# If 29 is out of range
iex> Range.disjoint?(1..28//14, 8..28//21)
true
iex> Range.disjoint?(2..28//3, 9..28//5)
true
"""
@doc since: "1.8.0"
@spec disjoint?(t, t) :: boolean
def disjoint?(first1..last1 = _range1, first2..last2 = _range2) do
{first1, last1} = normalize(first1, last1)
{first2, last2} = normalize(first2, last2)
last2 < first1 or last1 < first2
def disjoint?(first1..last1//step1 = range1, first2..last2//step2 = range2) do
if size(range1) == 0 or size(range2) == 0 do
true
else
{first1, last1, step1} = normalize(first1, last1, step1)
{first2, last2, step2} = normalize(first2, last2, step2)
cond do
last2 < first1 or last1 < first2 ->
true
abs(step1) == 1 and abs(step2) == 1 ->
false
true ->
# We need to find the first intersection of two arithmetic
# progressions and see if they belong within the ranges
# https://math.stackexchange.com/questions/1656120/formula-to-find-the-first-intersection-of-two-arithmetic-progressions
{gcd, u, v} = Integer.extended_gcd(-step1, step2)
c = first1 - first2 + step2 - step1
t1 = -c / step1 * u
t2 = -c / step2 * v
t = max(floor(t1) + 1, floor(t2) + 1)
x = div(c * u + t * step2, gcd) - 1
y = div(c * v + t * step1, gcd) - 1
x < 0 or first1 + x * step1 > last1 or
y < 0 or first2 + y * step2 > last2
end
end
end
@compile inline: [normalize: 2]
defp normalize(first, last) when first > last, do: {last, first}
defp normalize(first, last), do: {first, last}
@compile inline: [normalize: 3]
defp normalize(first, last, step) when first > last, do: {last, first, -step}
defp normalize(first, last, step), do: {first, last, step}
@doc false
@deprecated "Pattern match on first..last instead"
@deprecated "Pattern match on first..last//step instead"
def range?(term)
def range?(first..last) when is_integer(first) and is_integer(last), do: true
def range?(_), do: false
end
defimpl Enumerable, for: Range do
def reduce(first..last, acc, fun) do
reduce(first, last, acc, fun, _up? = last >= first)
def reduce(first..last//step, acc, fun) do
reduce(first, last, acc, fun, step)
end
defp reduce(_first, _last, {:halt, acc}, _fun, _up?) do
# TODO: Remove me on v2.0
def reduce(%{__struct__: Range, first: first, last: last} = range, acc, fun) do
step = if first <= last, do: 1, else: -1
reduce(Map.put(range, :step, step), acc, fun)
end
defp reduce(_first, _last, {:halt, acc}, _fun, _step) do
{:halted, acc}
end
defp reduce(first, last, {:suspend, acc}, fun, up?) do
{:suspended, acc, &reduce(first, last, &1, fun, up?)}
defp reduce(first, last, {:suspend, acc}, fun, step) do
{:suspended, acc, &reduce(first, last, &1, fun, step)}
end
defp reduce(first, last, {:cont, acc}, fun, _up? = true) when first <= last do
reduce(first + 1, last, fun.(first, acc), fun, _up? = true)
end
defp reduce(first, last, {:cont, acc}, fun, _up? = false) when first >= last do
reduce(first - 1, last, fun.(first, acc), fun, _up? = false)
defp reduce(first, last, {:cont, acc}, fun, step)
when step > 0 and first <= last
when step < 0 and first >= last do
reduce(first + step, last, fun.(first, acc), fun, step)
end
defp reduce(_, _, {:cont, acc}, _fun, _up) do
{:done, acc}
end
def member?(first..last, value) when is_integer(value) do
if first <= last do
{:ok, first <= value and value <= last}
else
{:ok, last <= value and value <= first}
def member?(first..last//step = range, value) when is_integer(value) do
cond do
Range.size(range) == 0 ->
{:ok, false}
first <= last ->
{:ok, first <= value and value <= last and rem(value - first, step) == 0}
true ->
{:ok, last <= value and value <= first and rem(value - first, step) == 0}
end
end
def member?(_.._, _value) do
# TODO: Remove me on v2.0
def member?(%{__struct__: Range, first: first, last: last} = range, value)
when is_integer(value) do
step = if first <= last, do: 1, else: -1
member?(Map.put(range, :step, step), value)
end
def member?(_, _value) do
{:ok, false}
end
def count(first..last) do
if first <= last do
{:ok, last - first + 1}
else
{:ok, first - last + 1}
end
def count(range) do
{:ok, Range.size(range)}
end
def slice(first..last) do
if first <= last do
{:ok, last - first + 1, &slice_asc(first + &1, &2)}
else
{:ok, first - last + 1, &slice_desc(first - &1, &2)}
end
def slice(first.._//step = range) do
{:ok, Range.size(range), &slice(first + &1 * step, step, &2)}
end
defp slice_asc(current, 1), do: [current]
defp slice_asc(current, remaining), do: [current | slice_asc(current + 1, remaining - 1)]
# TODO: Remove me on v2.0
def slice(%{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
slice(Map.put(range, :step, step))
end
defp slice_desc(current, 1), do: [current]
defp slice_desc(current, remaining), do: [current | slice_desc(current - 1, remaining - 1)]
defp slice(current, _step, 1), do: [current]
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
end
defimpl Inspect, for: Range do
import Inspect.Algebra
import Kernel, except: [inspect: 2]
def inspect(first..last, opts) do
def inspect(first..last//1, opts) do
concat([to_doc(first, opts), "..", to_doc(last, opts)])
end
def inspect(first..last//step, opts) do
concat([to_doc(first, opts), "..", to_doc(last, opts), "//", to_doc(step, opts)])
end
# TODO: Remove me on v2.0
def inspect(%{__struct__: Range, first: first, last: last} = range, opts) do
step = if first <= last, do: 1, else: -1
inspect(Map.put(range, :step, step), opts)
end
end
+2 -2
View File
@@ -447,12 +447,12 @@ defmodule Record do
end
end
defp hoist_expressions(keyword, %{context: nil}) do
defp hoist_expressions(keyword, %{context: nil, module: module}) do
Enum.map_reduce(keyword, [], fn {key, expr}, acc ->
if simple_argument?(expr) do
{{key, expr}, acc}
else
var = Macro.var(key, __MODULE__)
var = Macro.unique_var(key, module)
{{key, var}, [{:=, [], [var, expr]} | acc]}
end
end)
+27 -21
View File
@@ -4,7 +4,7 @@ defmodule Regex do
Regex is based on PCRE (Perl Compatible Regular Expressions) and
built on top of Erlang's `:re` module. More information can be found
in the [`:re` module documentation](http://www.erlang.org/doc/man/re.html).
in the [`:re` module documentation](`:re`).
Regular expressions in Elixir can be created using the sigils
`~r` (see `Kernel.sigil_r/2`) or `~R` (see `Kernel.sigil_R/2`):
@@ -107,7 +107,6 @@ defmodule Regex do
* alnum - Letters and digits
* alpha - Letters
* ascii - Character codes 0-127
* blank - Space or tab only
* cntrl - Control characters
* digit - Decimal digits (same as \\d)
@@ -120,6 +119,11 @@ defmodule Regex do
* word - "Word" characters (same as \w)
* xdigit - Hexadecimal digits
There is another character class, `ascii`, that erroneously matches
Latin-1 characters instead of the 0-127 range specified by POSIX. This
cannot be fixed without altering the behaviour of other classes, so we
recommend matching the range with `[\\0-\x7f]` instead.
Note the behaviour of those classes may change according to the Unicode
and other modifiers:
@@ -280,7 +284,7 @@ defmodule Regex do
Returns `true` if the given `term` is a regex.
Otherwise returns `false`.
"""
# TODO: Remove this on Elixir v1.15
# TODO: deprecate permanently on Elixir v1.15
@doc deprecated: "Use Kernel.is_struct/2 or pattern match on %Regex{} instead"
def regex?(term)
def regex?(%Regex{}), do: true
@@ -296,6 +300,8 @@ defmodule Regex do
Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
Defaults to zero.
## Examples
@@ -315,8 +321,9 @@ defmodule Regex do
def run(%Regex{} = regex, string, options) when is_binary(string) do
return = Keyword.get(options, :return, :binary)
captures = Keyword.get(options, :capture, :all)
offset = Keyword.get(options, :offset, 0)
case safe_run(regex, string, [{:capture, captures, return}]) do
case safe_run(regex, string, [{:capture, captures, return}, {:offset, offset}]) do
:nomatch -> nil
:match -> []
{:match, results} -> results
@@ -425,6 +432,8 @@ defmodule Regex do
Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
Defaults to zero.
## Examples
@@ -450,7 +459,8 @@ defmodule Regex do
def scan(%Regex{} = regex, string, options) when is_binary(string) do
return = Keyword.get(options, :return, :binary)
captures = Keyword.get(options, :capture, :all)
options = [{:capture, captures, return}, :global]
offset = Keyword.get(options, :offset, 0)
options = [{:capture, captures, return}, :global, {:offset, offset}]
case safe_run(regex, string, options) do
:match -> []
@@ -643,20 +653,8 @@ defmodule Regex do
"""
@spec replace(t, String.t(), String.t() | (... -> String.t()), [term]) :: String.t()
def replace(regex, string, replacement, options \\ [])
def replace(regex, string, replacement, options)
when is_binary(string) and is_binary(replacement) and is_list(options) do
do_replace(regex, string, precompile_replacement(replacement), options)
end
def replace(regex, string, replacement, options)
when is_binary(string) and is_function(replacement) and is_list(options) do
{:arity, arity} = Function.info(replacement, :arity)
do_replace(regex, string, {replacement, arity}, options)
end
defp do_replace(%Regex{} = regex, string, replacement, options) do
def replace(%Regex{} = regex, string, replacement, options \\ [])
when is_binary(string) and is_list(options) do
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
opts = [{:capture, :all, :index} | opts]
@@ -665,13 +663,20 @@ defmodule Regex do
string
{:match, [mlist | t]} when is_list(mlist) ->
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary()
apply_list(string, precompile_replacement(replacement), [mlist | t])
|> IO.iodata_to_binary()
{:match, slist} ->
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary()
apply_list(string, precompile_replacement(replacement), [slist])
|> IO.iodata_to_binary()
end
end
defp precompile_replacement(replacement) when is_function(replacement) do
{:arity, arity} = Function.info(replacement, :arity)
{replacement, arity}
end
defp precompile_replacement(""), do: []
defp precompile_replacement(<<?\\, ?g, ?{, rest::binary>>) when byte_size(rest) > 0 do
@@ -823,6 +828,7 @@ defmodule Regex do
@doc false
# Unescape map function used by Macro.unescape_string.
def unescape_map(:newline), do: true
def unescape_map(?f), do: ?\f
def unescape_map(?n), do: ?\n
def unescape_map(?r), do: ?\r
+82 -16
View File
@@ -354,13 +354,20 @@ defmodule Registry do
"expected :listeners to be a list of named processes, got: #{inspect(listeners)}"
end
compressed = Keyword.get(options, :compressed, false)
unless is_boolean(compressed) do
raise ArgumentError,
"expected :compressed to be a boolean, got: #{inspect(compressed)}"
end
# The @info format must be kept in sync with Registry.Partition optimization.
entries = [
{@all_info, {keys, partitions, nil, nil, listeners}},
{@key_info, {keys, partitions, nil}} | meta
]
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries)
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries, compressed)
end
@doc false
@@ -724,6 +731,64 @@ defmodule Registry do
acc
end
@doc """
Reads the values for the given `key` for `pid` in `registry`.
For unique registries, it is either an empty list or a list
with a single element. For duplicate registries, it is a list
with zero, one, or multiple elements.
## Examples
In the example below we register the current process and look it up
both from itself and other processes:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueLookupTest)
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
[]
iex> {:ok, _} = Registry.register(Registry.UniqueLookupTest, "hello", :world)
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
[:world]
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", self()) end) |> Task.await()
[]
iex> parent = self()
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", parent) end) |> Task.await()
[:world]
The same applies to duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateLookupTest)
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
[]
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :world)
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
[:world]
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :another)
iex> Enum.sort(Registry.values(Registry.DuplicateLookupTest, "hello", self()))
[:another, :world]
"""
@doc since: "1.12.0"
@spec values(registry, key, pid) :: [value]
def values(registry, key, pid) when is_atom(registry) do
case key_info!(registry) do
{:unique, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, key, partitions)
case safe_lookup_second(key_ets, key) do
{^pid, value} ->
[value]
_ ->
[]
end
{:duplicate, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, pid, partitions)
for {^pid, value} <- safe_lookup_second(key_ets, key), do: value
end
end
@doc """
Unregisters all entries for the given `key` associated to the current
process in `registry`.
@@ -1339,12 +1404,12 @@ defmodule Registry.Supervisor do
@moduledoc false
use Supervisor
def start_link(kind, registry, partitions, listeners, entries) do
arg = {kind, registry, partitions, listeners, entries}
def start_link(kind, registry, partitions, listeners, entries, compressed) do
arg = {kind, registry, partitions, listeners, entries, compressed}
Supervisor.start_link(__MODULE__, arg, name: registry)
end
def init({kind, registry, partitions, listeners, entries}) do
def init({kind, registry, partitions, listeners, entries, compressed}) do
^registry = :ets.new(registry, [:set, :public, :named_table, read_concurrency: true])
true = :ets.insert(registry, entries)
@@ -1352,7 +1417,7 @@ defmodule Registry.Supervisor do
for i <- 0..(partitions - 1) do
key_partition = Registry.Partition.key_name(registry, i)
pid_partition = Registry.Partition.pid_name(registry, i)
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners}
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}
%{
id: pid_partition,
@@ -1412,9 +1477,9 @@ defmodule Registry.Partition do
## Callbacks
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners}) do
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}) do
Process.flag(:trap_exit, true)
key_ets = init_key_ets(kind, key_partition)
key_ets = init_key_ets(kind, key_partition, compressed)
pid_ets = init_pid_ets(kind, pid_partition)
# If we have only one partition, we do an optimization which
@@ -1435,17 +1500,18 @@ defmodule Registry.Partition do
# The key partition is a set for unique keys,
# duplicate bag for duplicate ones.
defp init_key_ets(:unique, key_partition) do
:ets.new(key_partition, [:set, :public, read_concurrency: true, write_concurrency: true])
defp init_key_ets(:unique, key_partition, compressed) do
opts = [:set, :public, read_concurrency: true, write_concurrency: true]
:ets.new(key_partition, compression_opt(opts, compressed))
end
defp init_key_ets(:duplicate, key_partition) do
:ets.new(key_partition, [
:duplicate_bag,
:public,
read_concurrency: true,
write_concurrency: true
])
defp init_key_ets(:duplicate, key_partition, compressed) do
opts = [:duplicate_bag, :public, read_concurrency: true, write_concurrency: true]
:ets.new(key_partition, compression_opt(opts, compressed))
end
defp compression_opt(opts, compressed) do
if compressed, do: [:compressed] ++ opts, else: opts
end
# A process can always have multiple keys, so the
+151 -41
View File
@@ -1105,9 +1105,9 @@ defmodule Stream do
end
@doc """
Zips two collections together, lazily.
Zips two enumerables together, lazily.
The zipping finishes as soon as any enumerable completes.
The zipping finishes as soon as either enumerable completes.
## Examples
@@ -1118,7 +1118,9 @@ defmodule Stream do
"""
@spec zip(Enumerable.t(), Enumerable.t()) :: Enumerable.t()
def zip(left, right), do: zip([left, right])
def zip(enumerable1, enumerable2) do
zip_with(enumerable1, enumerable2, fn left, right -> {left, right} end)
end
@doc """
Zips corresponding elements from a finite collection of enumerables
@@ -1137,69 +1139,175 @@ defmodule Stream do
@doc since: "1.4.0"
@spec zip(enumerables) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t()
def zip(enumerables) do
&prepare_zip(enumerables, &1, &2)
zip_with(enumerables, &List.to_tuple(&1))
end
defp prepare_zip(enumerables, acc, fun) do
step = &do_zip_step(&1, &2)
@doc """
Lazily zips corresponding elements from two enumerables into a new one, transforming them with
the `zip_fun` function as it goes.
The `zip_fun` will be called with the first element from `enumerable1` and the first
element from `enumerable2`, then with the second element from each, and so on until
either one of the enumerables completes.
## Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with(concat, concat, fn a, b -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]
"""
@doc since: "1.12.0"
@spec zip_with(Enumerable.t(), Enumerable.t(), (term, term -> term)) :: Enumerable.t()
def zip_with(enumerable1, enumerable2, zip_fun)
when is_list(enumerable1) and is_list(enumerable2) and is_function(zip_fun, 2) do
&zip_pair(enumerable1, enumerable2, &1, &2, zip_fun)
end
def zip_with(enumerable1, enumerable2, zip_fun) when is_function(zip_fun, 2) do
zip_with([enumerable1, enumerable2], fn [left, right] -> zip_fun.(left, right) end)
end
defp zip_pair(_list1, _list2, {:halt, acc}, _fun, _zip_fun) do
{:halted, acc}
end
defp zip_pair(list1, list2, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &zip_pair(list1, list2, &1, fun, zip_fun)}
end
defp zip_pair([], _list2, {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
defp zip_pair(_list1, [], {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
defp zip_pair([head1 | tail1], [head2 | tail2], {:cont, acc}, fun, zip_fun) do
zip_pair(tail1, tail2, fun.(zip_fun.(head1, head2), acc), fun, zip_fun)
end
@doc """
Lazily zips corresponding elements from a finite collection of enumerables into a new
enumerable, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put into a list which is then passed to
the 1-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
`zip_fun`, and so on until any one of the enums in `enumerables` completes.
Returns a new enumerable with the results of calling `zip_fun`.
## Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat], fn [a, b] -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat, 1..3], fn [a, b, c] -> a + b + c end) |> Enum.to_list()
[3, 6, 9]
"""
@doc since: "1.12.0"
@spec zip_with(enumerables, (Enumerable.t() -> term)) :: Enumerable.t()
when enumerables: [Enumerable.t()] | Enumerable.t()
def zip_with(enumerables, zip_fun) when is_function(zip_fun, 1) do
if is_list(enumerables) and :lists.all(&is_list/1, enumerables) do
&zip_list(enumerables, &1, &2, zip_fun)
else
&zip_enum(enumerables, &1, &2, zip_fun)
end
end
defp zip_list(_enumerables, {:halt, acc}, _fun, _zip_fun) do
{:halted, acc}
end
defp zip_list(enumerables, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &zip_list(enumerables, &1, fun, zip_fun)}
end
defp zip_list(enumerables, {:cont, acc}, fun, zip_fun) do
case zip_list_heads_tails(enumerables, [], []) do
{heads, tails} -> zip_list(tails, fun.(zip_fun.(heads), acc), fun, zip_fun)
:error -> {:done, acc}
end
end
defp zip_list_heads_tails([[head | tail] | rest], heads, tails) do
zip_list_heads_tails(rest, [head | heads], [tail | tails])
end
defp zip_list_heads_tails([[] | _rest], _heads, _tails) do
:error
end
defp zip_list_heads_tails([], heads, tails) do
{:lists.reverse(heads), :lists.reverse(tails)}
end
defp zip_enum(enumerables, acc, fun, zip_fun) do
step = fn x, acc ->
{:suspend, :lists.reverse([x | acc])}
end
enum_funs =
Enum.map(enumerables, fn enum ->
{&Enumerable.reduce(enum, &1, step), [], :cont}
end)
do_zip(enum_funs, acc, fun)
do_zip_enum(enum_funs, acc, fun, zip_fun)
end
# This implementation of do_zip/3 works for any number of
# streams to zip, even if right now zip/2 only zips two streams.
defp do_zip(zips, {:halt, acc}, _fun) do
# This implementation of do_zip_enum/4 works for any number of streams to zip
defp do_zip_enum(zips, {:halt, acc}, _fun, _zip_fun) do
do_zip_close(zips)
{:halted, acc}
end
defp do_zip(zips, {:suspend, acc}, fun) do
{:suspended, acc, &do_zip(zips, &1, fun)}
defp do_zip_enum(zips, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &do_zip_enum(zips, &1, fun, zip_fun)}
end
defp do_zip([], {:cont, acc}, _callback) do
defp do_zip_enum([], {:cont, acc}, _callback, _zip_fun) do
{:done, acc}
end
defp do_zip(zips, {:cont, acc}, callback) do
defp do_zip_enum(zips, {:cont, acc}, callback, zip_fun) do
try do
do_zip_next_tuple(zips, acc, callback, [], [])
do_zip_next(zips, acc, callback, [], [], zip_fun)
catch
kind, reason ->
do_zip_close(zips)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:next, buffer, acc} ->
do_zip(buffer, acc, callback)
do_zip_enum(buffer, acc, callback, zip_fun)
{:done, _acc} = other ->
other
end
end
# do_zip_next_tuple/5 computes the next tuple formed by
# do_zip_next/6 computes the next tuple formed by
# the next element of each zipped stream.
defp do_zip_next_tuple([{_, [], :halt} | zips], acc, _callback, _yielded_elems, buffer) do
defp do_zip_next(
[{_, [], :halt} | zips],
acc,
_callback,
_yielded_elems,
buffer,
_zip_fun
) do
do_zip_close(:lists.reverse(buffer, zips))
{:done, acc}
end
defp do_zip_next_tuple([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer) do
defp do_zip_next([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer, zip_fun) do
case fun.({:cont, []}) do
{:suspended, [elem | next_acc], fun} ->
next_buffer = [{fun, next_acc, :cont} | buffer]
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
{_, [elem | next_acc]} ->
next_buffer = [{fun, next_acc, :halt} | buffer]
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
{_, []} ->
# The current zipped stream terminated, so we close all the streams
@@ -1209,28 +1317,30 @@ defmodule Stream do
end
end
defp do_zip_next_tuple([{fun, zip_acc, zip_op} | zips], acc, callback, yielded_elems, buffer) do
defp do_zip_next(
[{fun, zip_acc, zip_op} | zips],
acc,
callback,
yielded_elems,
buffer,
zip_fun
) do
[elem | rest] = zip_acc
next_buffer = [{fun, rest, zip_op} | buffer]
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
end
defp do_zip_next_tuple([] = _zips, acc, callback, yielded_elems, buffer) do
defp do_zip_next([] = _zips, acc, callback, yielded_elems, buffer, zip_fun) do
# "yielded_elems" is a reversed list of results for the current iteration of
# zipping: it needs to be reversed and converted to a tuple to have the next
# tuple in the list resulting from zipping.
zipped = List.to_tuple(:lists.reverse(yielded_elems))
{:next, :lists.reverse(buffer), callback.(zipped, acc)}
# zipping. That is to say, the nth element from each of the enums being zipped.
# It needs to be reversed and passed to the zipping function so it can do it's thing.
{:next, :lists.reverse(buffer), callback.(zip_fun.(:lists.reverse(yielded_elems)), acc)}
end
defp do_zip_close(zips) do
:lists.foreach(fn {fun, _, _} -> fun.({:halt, []}) end, zips)
end
defp do_zip_step(x, acc) do
{:suspend, :lists.reverse([x | acc])}
end
## Sources
@doc """
@@ -1248,7 +1358,7 @@ defmodule Stream do
def cycle(enumerable)
def cycle([]) do
raise ArgumentError, "cannot cycle over empty enumerable"
raise ArgumentError, "cannot cycle over an empty enumerable"
end
def cycle(enumerable) when is_list(enumerable) do
@@ -1297,7 +1407,7 @@ defmodule Stream do
fn acc ->
case reduce.(acc) do
{state, []} when state in [:done, :halted] ->
raise ArgumentError, "cannot cycle over empty enumerable"
raise ArgumentError, "cannot cycle over an empty enumerable"
other ->
other
@@ -1333,9 +1443,9 @@ defmodule Stream do
## Examples
# Although not necessary, let's seed the random algorithm
iex> :rand.seed(:exrop, {1, 2, 3})
iex> :rand.seed(:exsss, {1, 2, 3})
iex> Stream.repeatedly(&:rand.uniform/0) |> Enum.take(3)
[0.7498295129076106, 0.06161655489244533, 0.7924073127680873]
[0.5455598952593053, 0.6039309974353404, 0.6684893034823949]
"""
@spec repeatedly((() -> element)) :: Enumerable.t()
@@ -1584,7 +1694,7 @@ defmodule Stream do
end
defimpl Enumerable, for: Stream do
@compile :inline_list_funs
@compile :inline_list_funcs
def count(_lazy), do: {:error, __MODULE__}
@@ -1621,7 +1731,7 @@ defimpl Enumerable, for: Stream do
defp do_done({reason, [acc | _]}, nil), do: {reason, acc}
defp do_done({reason, [acc | t]}, {done, fun}) do
[h | _] = Enum.reverse(t)
[h | _] = :lists.reverse(t)
case done.([acc, h], fun) do
{:cont, [acc | _]} -> {reason, acc}
@@ -1635,7 +1745,7 @@ defimpl Inspect, for: Stream do
import Inspect.Algebra
def inspect(%{enum: enum, funs: funs}, opts) do
inner = [enum: enum, funs: Enum.reverse(funs)]
inner = [enum: enum, funs: :lists.reverse(funs)]
concat(["#Stream<", to_doc(inner, opts), ">"])
end
end
+138 -93
View File
@@ -69,7 +69,7 @@ defmodule String do
## Code points and grapheme cluster
The functions in this module act according to the Unicode
Standard, version 12.1.0.
Standard, version 13.0.0.
As per the standard, a code point is a single Unicode Character,
which may be represented by one or more bytes.
@@ -133,7 +133,7 @@ defmodule String do
* `Kernel.bit_size/1` and `Kernel.byte_size/1` - size related functions
* `Kernel.is_bitstring/1` and `Kernel.is_binary/1` - type-check function
* Plus a number of functions for working with binaries (bytes)
in the [`:binary` module](http://www.erlang.org/doc/man/binary.html)
in the [`:binary` module](`:binary`)
There are many situations where using the `String` module can
be avoided in favor of binary functions or pattern matching.
@@ -286,7 +286,7 @@ defmodule String do
@typedoc "Pattern used in functions like `replace/4` and `split/3`"
@type pattern :: t | [t] | :binary.cp()
@conditional_mappings [:greek]
@conditional_mappings [:greek, :turkic]
@doc """
Checks if a string contains only printable characters up to `character_limit`.
@@ -470,11 +470,11 @@ defmodule String do
@spec split(t, pattern | Regex.t(), keyword) :: [t]
def split(string, pattern, options \\ [])
def split(string, %Regex{} = pattern, options) when is_binary(string) do
def split(string, %Regex{} = pattern, options) when is_binary(string) and is_list(options) do
Regex.split(pattern, string, options)
end
def split(string, "", options) when is_binary(string) do
def split(string, "", options) when is_binary(string) and is_list(options) do
parts = Keyword.get(options, :parts, :infinity)
index = parts_to_index(parts)
trim = Keyword.get(options, :trim, false)
@@ -486,7 +486,7 @@ defmodule String do
end
end
def split(string, pattern, options) when is_binary(string) do
def split(string, pattern, options) when is_binary(string) and is_list(options) do
parts = Keyword.get(options, :parts, :infinity)
trim = Keyword.get(options, :trim, false)
@@ -559,7 +559,7 @@ defmodule String do
@spec splitter(t, pattern, keyword) :: Enumerable.t()
def splitter(string, pattern, options \\ [])
def splitter(string, "", options) do
def splitter(string, "", options) when is_binary(string) and is_list(options) do
if Keyword.get(options, :trim, false) do
Stream.unfold(string, &next_grapheme/1)
else
@@ -567,7 +567,7 @@ defmodule String do
end
end
def splitter(string, pattern, options) do
def splitter(string, pattern, options) when is_binary(string) and is_list(options) do
pattern = maybe_compile_pattern(pattern)
trim = Keyword.get(options, :trim, false)
Stream.unfold(string, &do_splitter(&1, pattern, trim))
@@ -626,11 +626,13 @@ defmodule String do
@spec split_at(t, integer) :: {t, t}
def split_at(string, position)
def split_at(string, position) when is_integer(position) and position >= 0 do
def split_at(string, position)
when is_binary(string) and is_integer(position) and position >= 0 do
do_split_at(string, position)
end
def split_at(string, position) when is_integer(position) and position < 0 do
def split_at(string, position)
when is_binary(string) and is_integer(position) and position < 0 do
position = length(string) + position
case position >= 0 do
@@ -645,7 +647,7 @@ defmodule String do
end
@doc ~S"""
Returns `true` if `string1` is canonically equivalent to 'string2'.
Returns `true` if `string1` is canonically equivalent to `string2`.
It performs Normalization Form Canonical Decomposition (NFD) on the
strings before comparing them. This function is equivalent to:
@@ -672,7 +674,7 @@ defmodule String do
"""
@spec equivalent?(t, t) :: boolean
def equivalent?(string1, string2) do
def equivalent?(string1, string2) when is_binary(string1) and is_binary(string2) do
normalize(string1, :nfd) == normalize(string2, :nfd)
end
@@ -728,28 +730,28 @@ defmodule String do
"""
def normalize(string, form)
def normalize(string, :nfd) do
def normalize(string, :nfd) when is_binary(string) do
case :unicode.characters_to_nfd_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfd)
end
end
def normalize(string, :nfc) do
def normalize(string, :nfc) when is_binary(string) do
case :unicode.characters_to_nfc_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfc)
end
end
def normalize(string, :nfkd) do
def normalize(string, :nfkd) when is_binary(string) do
case :unicode.characters_to_nfkd_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkd)
end
end
def normalize(string, :nfkc) do
def normalize(string, :nfkc) when is_binary(string) do
case :unicode.characters_to_nfkc_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkc)
@@ -759,10 +761,10 @@ defmodule String do
@doc """
Converts all characters in the given string to uppercase according to `mode`.
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
uppercases only the letters a to z. `:greek` includes the context sensitive
mappings found in Greek.
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
## Examples
@@ -782,8 +784,16 @@ defmodule String do
iex> String.upcase("olá", :ascii)
"OLá"
And `:turkic` properly handles the letter i with the dotless variant:
iex> String.upcase("ıi")
"II"
iex> String.upcase("ıi", :turkic)
"Iİ"
"""
@spec upcase(t, :default | :ascii | :greek) :: t
@spec upcase(t, :default | :ascii | :greek | :turkic) :: t
def upcase(string, mode \\ :default)
def upcase("", _mode) do
@@ -798,7 +808,7 @@ defmodule String do
IO.iodata_to_binary(upcase_ascii(string))
end
def upcase(string, mode) when mode in @conditional_mappings do
def upcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
String.Casing.upcase(string, [], mode)
end
@@ -811,10 +821,10 @@ defmodule String do
@doc """
Converts all characters in the given string to lowercase according to `mode`.
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
lowercases only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek.
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
## Examples
@@ -834,7 +844,7 @@ defmodule String do
iex> String.downcase("OLÁ", :ascii)
"olÁ"
And `:greek` properly handles the context sensitive sigma in Greek:
The `:greek` mode properly handles the context sensitive sigma in Greek:
iex> String.downcase("ΣΣ")
"σσ"
@@ -842,8 +852,16 @@ defmodule String do
iex> String.downcase("ΣΣ", :greek)
"σς"
And `:turkic` properly handles the letter i with the dotless variant:
iex> String.downcase("Iİ")
"ii̇"
iex> String.downcase("Iİ", :turkic)
"ıi"
"""
@spec downcase(t, :default | :ascii | :greek) :: t
@spec downcase(t, :default | :ascii | :greek | :turkic) :: t
def downcase(string, mode \\ :default)
def downcase("", _mode) do
@@ -858,7 +876,7 @@ defmodule String do
IO.iodata_to_binary(downcase_ascii(string))
end
def downcase(string, mode) when mode in @conditional_mappings do
def downcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
String.Casing.downcase(string, [], mode)
end
@@ -872,10 +890,10 @@ defmodule String do
Converts the first character in the given string to
uppercase and the remainder to lowercase according to `mode`.
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
lowercases only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek.
capitalizes only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
## Examples
@@ -889,7 +907,7 @@ defmodule String do
"Olá"
"""
@spec capitalize(t, :default | :ascii | :greek) :: t
@spec capitalize(t, :default | :ascii | :greek | :turkic) :: t
def capitalize(string, mode \\ :default)
def capitalize(<<char, rest::binary>>, :ascii) do
@@ -1163,7 +1181,8 @@ defmodule String do
"""
@spec trim_leading(t, t) :: t
def trim_leading(string, to_trim) do
def trim_leading(string, to_trim)
when is_binary(string) and is_binary(to_trim) do
replace_leading(string, to_trim, "")
end
@@ -1193,7 +1212,8 @@ defmodule String do
"""
@spec trim_trailing(t, t) :: t
def trim_trailing(string, to_trim) do
def trim_trailing(string, to_trim)
when is_binary(string) and is_binary(to_trim) do
replace_trailing(string, to_trim, "")
end
@@ -1208,7 +1228,7 @@ defmodule String do
"""
@spec trim(t) :: t
def trim(string) do
def trim(string) when is_binary(string) do
string
|> trim_leading()
|> trim_trailing()
@@ -1225,7 +1245,7 @@ defmodule String do
"""
@spec trim(t, t) :: t
def trim(string, to_trim) do
def trim(string, to_trim) when is_binary(string) and is_binary(to_trim) do
string
|> trim_leading(to_trim)
|> trim_trailing(to_trim)
@@ -1562,7 +1582,7 @@ defmodule String do
one single grapheme.
"""
@spec reverse(t) :: t
def reverse(string) do
def reverse(string) when is_binary(string) do
do_reverse(next_grapheme(string), [])
end
@@ -1575,7 +1595,7 @@ defmodule String do
@compile {:inline, duplicate: 2}
@doc """
Returns a string `subject` duplicated `n` times.
Returns a string `subject` repeated `n` times.
Inlined by the compiler.
@@ -1592,7 +1612,7 @@ defmodule String do
"""
@spec duplicate(t, non_neg_integer) :: t
def duplicate(subject, n) do
def duplicate(subject, n) when is_binary(subject) and is_integer(n) and n >= 0 do
:binary.copy(subject, n)
end
@@ -1692,12 +1712,13 @@ defmodule String do
"""
@spec valid?(t) :: boolean
def valid?(string)
def valid?(<<_::utf8, t::binary>>), do: valid?(t)
def valid?(<<>>), do: true
def valid?(<<string::binary>>), do: valid_utf8?(string)
def valid?(_), do: false
defp valid_utf8?(<<_::utf8, rest::bits>>), do: valid_utf8?(rest)
defp valid_utf8?(<<>>), do: true
defp valid_utf8?(_), do: false
@doc false
@deprecated "Use String.valid?/1 instead"
def valid_character?(string) do
@@ -1741,7 +1762,7 @@ defmodule String do
def chunk("", _), do: []
def chunk(string, trait) when trait in [:valid, :printable] do
def chunk(string, trait) when is_binary(string) and trait in [:valid, :printable] do
{cp, _} = next_codepoint(string)
pred_fn = make_chunk_pred(trait)
do_chunk(string, pred_fn.(cp), pred_fn)
@@ -1809,7 +1830,7 @@ defmodule String do
"""
@spec next_grapheme(t) :: {grapheme, t} | nil
def next_grapheme(binary) do
def next_grapheme(binary) when is_binary(binary) do
case next_grapheme_size(binary) do
{size, rest} -> {binary_part(binary, 0, size), rest}
nil -> nil
@@ -1852,7 +1873,7 @@ defmodule String do
"""
@spec first(t) :: grapheme | nil
def first(string) do
def first(string) when is_binary(string) do
case next_grapheme(string) do
{char, _} -> char
nil -> nil
@@ -1873,7 +1894,7 @@ defmodule String do
"""
@spec last(t) :: grapheme | nil
def last(string) do
def last(string) when is_binary(string) do
do_last(next_grapheme(string), nil)
end
@@ -1922,11 +1943,11 @@ defmodule String do
"""
@spec at(t, integer) :: grapheme | nil
def at(string, position) when is_integer(position) and position >= 0 do
def at(string, position) when is_binary(string) and is_integer(position) and position >= 0 do
do_at(string, position)
end
def at(string, position) when is_integer(position) and position < 0 do
def at(string, position) when is_binary(string) and is_integer(position) and position < 0 do
position = length(string) + position
case position >= 0 do
@@ -1984,7 +2005,9 @@ defmodule String do
""
end
def slice(string, start, length) when start >= 0 and length >= 0 do
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start >= 0 and
length >= 0 do
case String.Unicode.split_at(string, start) do
{_, nil} ->
""
@@ -1995,7 +2018,9 @@ defmodule String do
end
end
def slice(string, start, length) when start < 0 and length >= 0 do
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start < 0 and
length >= 0 do
start = length(string) + start
case start >= 0 do
@@ -2027,19 +2052,24 @@ defmodule String do
iex> String.slice("elixir", 1..10)
"lixir"
iex> String.slice("elixir", 10..3)
""
iex> String.slice("elixir", -4..-1)
"ixir"
iex> String.slice("elixir", 2..-1)
"ixir"
iex> String.slice("elixir", -4..6)
"ixir"
iex> String.slice("elixir", -1..-4)
For ranges where `start > stop`, you need to explicit
mark them as increasing:
iex> String.slice("elixir", 2..-1//1)
"ixir"
iex> String.slice("elixir", 1..-2//1)
"lixi"
If values are out of bounds, it returns an empty string:
iex> String.slice("elixir", 10..3)
""
iex> String.slice("elixir", -10..-7)
@@ -2053,22 +2083,37 @@ defmodule String do
"""
@spec slice(t, Range.t()) :: t
def slice(string, range)
def slice("", _.._), do: ""
def slice(string, first..-1) when first >= 0 do
case String.Unicode.split_at(string, first) do
{_, nil} ->
""
{start_bytes, _} ->
binary_part(string, start_bytes, byte_size(string) - start_bytes)
def slice(string, first..last//step = range) when is_binary(string) do
# TODO: Deprecate negative steps on Elixir v1.16
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(string, first, last)
else
raise ArgumentError,
"String.slice/2 does not accept ranges with custom steps, got: #{inspect(range)}"
end
end
def slice(string, first..last) when first >= 0 and last >= 0 do
# TODO: Remove me on v2.0
def slice(string, %{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
slice(string, Map.put(range, :step, step))
end
defp slice_range("", _, _), do: ""
defp slice_range(string, first, -1) when first >= 0 do
case String.Unicode.split_at(string, first) do
{_, nil} -> ""
{start_bytes, _} -> binary_part(string, start_bytes, byte_size(string) - start_bytes)
end
end
defp slice_range(string, first, last) when first >= 0 and last >= 0 do
if last >= first do
slice(string, first, last - first + 1)
else
@@ -2076,9 +2121,8 @@ defmodule String do
end
end
def slice(string, first..last) do
{bytes, length} = do_acc_bytes(next_grapheme_size(string), [], 0)
defp slice_range(string, first, last) do
{bytes, length} = acc_bytes(next_grapheme_size(string), [], 0)
first = add_if_negative(first, length)
last = add_if_negative(last, length)
@@ -2088,21 +2132,25 @@ defmodule String do
last = min(last + 1, length)
bytes = Enum.drop(bytes, length - last)
first = last - first
{length_bytes, start_bytes} = Enum.split(bytes, first)
binary_part(string, Enum.sum(start_bytes), Enum.sum(length_bytes))
{length_bytes, start_bytes} = split_bytes(bytes, 0, first)
binary_part(string, start_bytes, length_bytes)
end
end
defp acc_bytes({size, rest}, bytes, length) do
acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
end
defp acc_bytes(nil, bytes, length) do
{bytes, length}
end
defp add_if_negative(value, to_add) when value < 0, do: value + to_add
defp add_if_negative(value, _to_add), do: value
defp do_acc_bytes({size, rest}, bytes, length) do
do_acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
end
defp do_acc_bytes(nil, bytes, length) do
{bytes, length}
end
defp split_bytes(rest, acc, 0), do: {acc, Enum.sum(rest)}
defp split_bytes([], acc, _), do: {acc, 0}
defp split_bytes([head | tail], acc, count), do: split_bytes(tail, head + acc, count - 1)
@doc """
Returns `true` if `string` starts with any of the prefixes given.
@@ -2214,7 +2262,7 @@ defmodule String do
"""
@spec match?(t, Regex.t()) :: boolean
def match?(string, regex) do
def match?(string, regex) when is_binary(string) do
Regex.match?(regex, string)
end
@@ -2281,7 +2329,7 @@ defmodule String do
strings.
In case you need to work with bytes, take a look at the
[`:binary` module](http://www.erlang.org/doc/man/binary.html).
[`:binary` module](`:binary`).
## Examples
@@ -2325,7 +2373,7 @@ defmodule String do
"""
@spec to_atom(String.t()) :: atom
def to_atom(string) do
def to_atom(string) when is_binary(string) do
:erlang.binary_to_atom(string, :utf8)
end
@@ -2342,12 +2390,9 @@ defmodule String do
iex> String.to_existing_atom("my_atom")
:my_atom
iex> String.to_existing_atom("this_atom_will_never_exist")
** (ArgumentError) argument error
"""
@spec to_existing_atom(String.t()) :: atom
def to_existing_atom(string) do
def to_existing_atom(string) when is_binary(string) do
:erlang.binary_to_existing_atom(string, :utf8)
end
@@ -2373,7 +2418,7 @@ defmodule String do
"""
@spec to_integer(String.t()) :: integer
def to_integer(string) do
def to_integer(string) when is_binary(string) do
:erlang.binary_to_integer(string)
end
@@ -2389,7 +2434,7 @@ defmodule String do
"""
@spec to_integer(String.t(), 2..36) :: integer
def to_integer(string, base) do
def to_integer(string, base) when is_binary(string) and is_integer(base) do
:erlang.binary_to_integer(string, base)
end
@@ -2415,7 +2460,7 @@ defmodule String do
"""
@spec to_float(String.t()) :: float
def to_float(string) do
def to_float(string) when is_binary(string) do
:erlang.binary_to_float(string)
end
@@ -2452,7 +2497,7 @@ defmodule String do
def bag_distance(_string, ""), do: 0.0
def bag_distance("", _string), do: 0.0
def bag_distance(string1, string2) do
def bag_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
{bag1, length1} = string_to_bag(string1, %{}, 0)
{bag2, length2} = string_to_bag(string2, %{}, 0)
@@ -2518,7 +2563,7 @@ defmodule String do
def jaro_distance(_string, ""), do: 0.0
def jaro_distance("", _string), do: 0.0
def jaro_distance(string1, string2) do
def jaro_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
{chars1, len1} = chars_and_length(string1)
{chars2, len2} = chars_and_length(string2)
@@ -2605,7 +2650,7 @@ defmodule String do
"""
@doc since: "1.3.0"
@spec myers_difference(t, t) :: [{:eq | :ins | :del, t}]
def myers_difference(string1, string2) do
def myers_difference(string1, string2) when is_binary(string1) and is_binary(string2) do
graphemes(string1)
|> List.myers_difference(graphemes(string2))
|> Enum.map(fn {kind, chars} -> {kind, IO.iodata_to_binary(chars)} end)
+1 -1
View File
@@ -415,7 +415,7 @@ defmodule StringIO do
defp list_to_binary(data, :unicode) when is_list(data), do: List.to_string(data)
defp list_to_binary(data, :latin1) when is_list(data), do: :erlang.list_to_binary(data)
# From http://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# From https://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# Erlang term, but if it is a list(), the I/O server can convert it to a binary().
defp get_until_result(data, encoding) when is_list(data), do: list_to_binary(data, encoding)
defp get_until_result(data, _), do: data
+1 -1
View File
@@ -930,7 +930,7 @@ defmodule Supervisor do
"""
@spec which_children(supervisor) :: [
# inlining module() | :dynamic here because :supervisor.modules() is not exported
{term() | :undefined, child | :restarting, :worker | :supervisor, module() | :dynamic}
{term() | :undefined, child | :restarting, :worker | :supervisor, [module()] | :dynamic}
]
def which_children(supervisor) do
call(supervisor, :which_children)
+267 -34
View File
@@ -43,7 +43,7 @@ defmodule System do
system time may not match in case of time warps although the VM works towards
aligning them. This time is not monotonic (i.e., it may decrease)
as its behaviour is configured [by the VM time warp
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
mode](https://erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
* `monotonic_time/0` - a monotonically increasing time provided
by the Erlang VM.
@@ -58,7 +58,7 @@ defmodule System do
For a more complete rundown on the VM support for different
times, see the [chapter on time and time
correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
correction](https://erlang.org/doc/apps/erts/time_correction.html)
in the Erlang docs.
"""
@@ -81,6 +81,22 @@ defmodule System do
| :nanosecond
| pos_integer
@type signal ::
:sigabrt
| :sigalrm
| :sigchld
| :sighup
| :sigquit
| :sigstop
| :sigterm
| :sigtstp
| :sigusr1
| :sigusr2
@vm_signals [:sigquit, :sigterm, :sigusr1]
@os_signals [:sighup, :sigabrt, :sigalrm, :sigusr2, :sigchld, :sigstop, :sigtstp]
@signals @vm_signals ++ @os_signals
@base_dir :filename.join(__DIR__, "../../..")
@version_file :filename.join(@base_dir, "VERSION")
@@ -409,8 +425,8 @@ defmodule System do
The function must receive the exit status code as an argument.
If the VM terminates programmatically, via `System.stop/1` or `System.halt/1`,
the `at_exit/1` callbacks are not executed.
If the VM terminates programmatically, via `System.stop/1`, `System.halt/1`,
or exit signals, the `at_exit/1` callbacks are not executed.
"""
@spec at_exit((non_neg_integer -> any)) :: :ok
def at_exit(fun) when is_function(fun, 1) do
@@ -418,6 +434,150 @@ defmodule System do
:ok
end
defmodule SignalHandler do
@moduledoc false
@behaviour :gen_event
@impl true
def init({event, fun}) do
{:ok, {event, fun}}
end
@impl true
def handle_call(_message, state) do
{:ok, :ok, state}
end
@impl true
def handle_event(signal, {event, fun}) do
if signal == event, do: :ok = fun.()
{:ok, {event, fun}}
end
@impl true
def handle_info(_, {event, fun}) do
{:ok, {event, fun}}
end
end
@doc """
Traps the given `signal` to execute the `fun`.
**Important**: Trapping signals may have strong implications
on how a system shuts down and behave in production and
therefore it is extremely discouraged for libraries to
set their own traps. Instead, they should redirect users
to configure them themselves. The only cases where it is
acceptable for libraries to set their own traps is when
using Elixir in script mode, such as in `.exs` files and
via Mix tasks.
An optional `id` that uniquely identifies the function
can be given, otherwise a unique one is automatically
generated. If a previously registered `id` is given,
this function returns an error tuple. The `id` can be
used to remove a registered signal by calling
`untrap_signal/2`.
The given `fun` receives no arguments and it must return
`:ok`.
It returns `{:ok, id}` in case of success,
`{:error, :already_registered}` in case the id has already
been registered for the given signal, or `{:error, :not_sup}`
in case trapping exists is not supported by the current OS.
The first time a signal is trapped, it will override the
default behaviour from the operating system. If the same
signal is trapped multiple times, subsequent functions
given to `trap_signal` will execute *first*. In other
words, you can consider each function is prepended to
the signal handler.
By default, the Erlang VM register traps to the three
signals:
* `:sigstop` - gracefully shuts down the VM with `stop/0`
* `:sigquit` - halts the VM via `halt/0`
* `:sigusr1` - halts the VM via status code of 1
Therefore, if you add traps to the signals above, the
default behaviour above will be executed after all user
signals.
## Implementation notes
All signals run from a single process. Therefore, blocking the
`fun` will block subsequent traps. It is also not possible to add
or remove traps from within a trap itself.
Internally, this functionality is built on top of `:os.set_signal/2`.
When you register a trap, Elixir automatically sets it to `:handle`
and it reverts it back to `:default` once all traps are removed
(except for `:sigquit`, `:sigterm`, and `:sigusr1` which are always
handled). If you or a library call `:os.set_signal/2` directly,
it may disable Elixir traps (or Elixir may override your configuration).
"""
@doc since: "1.12.0"
@spec trap_signal(signal, (() -> :ok)) :: {:ok, reference()} | {:error, :not_sup}
@spec trap_signal(signal, id, (() -> :ok)) ::
{:ok, id} | {:error, :already_registered} | {:error, :not_sup}
when id: term()
def trap_signal(signal, id \\ make_ref(), fun)
when signal in @signals and is_function(fun, 0) do
:elixir_config.serial(fn ->
gen_id = {signal, id}
if {SignalHandler, gen_id} in signal_handlers() do
{:error, :already_registered}
else
try do
:os.set_signal(signal, :handle)
rescue
_ -> {:error, :not_sup}
else
:ok ->
:ok =
:gen_event.add_handler(:erl_signal_server, {SignalHandler, gen_id}, {signal, fun})
{:ok, id}
end
end
end)
end
@doc """
Removes a previously registered `signal` with `id`.
"""
@doc since: "1.12.0"
@spec untrap_signal(signal, id) :: :ok | {:error, :not_found} when id: term
def untrap_signal(signal, id) when signal in @signals do
:elixir_config.serial(fn ->
gen_id = {signal, id}
case :gen_event.delete_handler(:erl_signal_server, {SignalHandler, gen_id}, :delete) do
:ok ->
if not trapping?(signal) do
:os.set_signal(signal, :default)
end
:ok
{:error, :module_not_found} ->
{:error, :not_found}
end
end)
end
defp trapping?(signal) do
signal in @vm_signals or
Enum.any?(signal_handlers(), &match?({_, {^signal, _}}, &1))
end
defp signal_handlers do
:gen_event.which_handlers(:erl_signal_server)
end
@doc """
Locates an executable on the system.
@@ -437,19 +597,29 @@ defmodule System do
end
end
# TODO: Remove this once we require Erlang/OTP 24+
@compile {:no_warn_undefined, {:os, :env, 0}}
@doc """
Returns all system environment variables.
The returned value is a map containing name-value pairs.
Variable names and their values are strings.
"""
# TODO: Remove this once we require Erlang/OTP 24+
@spec get_env() :: %{optional(String.t()) => String.t()}
def get_env do
Enum.into(:os.getenv(), %{}, fn var ->
var = IO.chardata_to_string(var)
[k, v] = String.split(var, "=", parts: 2)
{k, v}
end)
if function_exported?(:os, :env, 0) do
Map.new(:os.env(), fn {k, v} ->
{IO.chardata_to_string(k), IO.chardata_to_string(v)}
end)
else
Enum.into(:os.getenv(), %{}, fn var ->
var = IO.chardata_to_string(var)
[k, v] = String.split(var, "=", parts: 2)
{k, v}
end)
end
end
@doc """
@@ -586,23 +756,21 @@ defmodule System do
@doc """
Deprecated mechanism to retrieve the last exception stacktrace.
Accessing the stacktrace outside of a rescue/catch is deprecated.
If you want to support only Elixir v1.7+, you must access
`__STACKTRACE__/0` inside a rescue/catch. If you want to support
earlier Elixir versions, move `System.stacktrace/0` inside a rescue/catch.
Starting from Erlang/OTP 23, this function will always return an empty list.
Note that the Erlang VM (and therefore this function) does not
return the current stacktrace but rather the stacktrace of the
latest exception. To retrieve the stacktrace of the current process,
use `Process.info(self(), :current_stacktrace)` instead.
Starting from Erlang/OTP 23, this function will always return an
empty list.
"""
# TODO: Once Erlang/OTP 23 is required, remove conditional, and update @doc accordingly.
# The warning is emitted by the compiler - so a @doc annotation is enough
@doc deprecated: "Use __STACKTRACE__ instead"
# TODO: Remove conditional on Erlang/OTP 23+.
# Note Elixir may be compiled in an earlier Erlang version but runs on a
# newer one, so we need the check at compilation time and runtime.
@deprecated "Use __STACKTRACE__ instead"
if function_exported?(:erlang, :get_stacktrace, 0) do
def stacktrace, do: apply(:erlang, :get_stacktrace, [])
def stacktrace do
if function_exported?(:erlang, :get_stacktrace, 0) do
apply(:erlang, :get_stacktrace, [])
else
[]
end
end
else
def stacktrace, do: []
end
@@ -713,6 +881,62 @@ defmodule System do
:init.stop(String.to_charlist(status))
end
@doc ~S"""
Executes the given `command` in the OS shell.
It uses `sh` for Unix-like systems and `cmd` for Windows.
**Important**: Use this function with care. In particular, **never
pass untrusted user input to this function**, as the user would be
able to perform "command injection attacks" by executing any code
directly on the machine. Generally speaking, prefer to use `cmd/3`
over this function.
## Examples
iex> System.shell("echo hello")
{"hello\n", 0}
If you want to stream the devices to IO as they come:
iex> System.shell("echo hello", into: IO.stream())
hello
{%IO.Stream{}, 0}
## Options
It accepts the same options as `cmd/3`, except for `arg0`.
"""
@doc since: "1.12.0"
@spec shell(binary, keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
def shell(command, opts \\ []) when is_binary(command) do
assert_no_null_byte!(command, "System.shell/2")
# Finding shell command logic from :os.cmd in OTP
# https://github.com/erlang/otp/blob/8deb96fb1d017307e22d2ab88968b9ef9f1b71d0/lib/kernel/src/os.erl#L184
command =
case :os.type() do
{:unix, _} ->
command =
command
|> String.replace("\"", "\\\"")
|> String.to_charlist()
'sh -c "' ++ command ++ '"'
{:win32, osname} ->
command = String.to_charlist(command)
case {System.get_env("COMSPEC"), osname} do
{nil, :windows} -> 'command.com /s /c ' ++ command
{nil, _} -> 'cmd /s /c ' ++ command
{cmd, _} -> '#{cmd} /s /c ' ++ command
end
end
do_cmd({:spawn, command}, [], opts)
end
@doc ~S"""
Executes the given `command` with `args`.
@@ -745,7 +969,9 @@ defmodule System do
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
{"hello\n", 0}
iex> System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
If you want to stream the devices to IO as they come:
iex> System.cmd("echo", ["hello"], into: IO.stream())
hello
{%IO.Stream{}, 0}
@@ -796,7 +1022,7 @@ defmodule System do
## Shell commands
If you desire to execute a trusted command inside a shell, with pipes,
redirecting and so on, please check `:os.cmd/1`.
redirecting and so on, please check `shell/2`.
"""
@spec cmd(binary, [binary], keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
@@ -815,11 +1041,15 @@ defmodule System do
:os.find_executable(cmd) || :erlang.error(:enoent, [command, args, opts])
end
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
do_cmd({:spawn_executable, cmd}, [args: args], opts)
end
defp do_cmd(port_init, base_opts, opts) do
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide] ++ base_opts, "")
{initial, fun} = Collectable.into(into)
try do
do_cmd(Port.open({:spawn_executable, cmd}, opts), initial, fun)
do_port(Port.open(port_init, opts), initial, fun)
catch
kind, reason ->
fun.(initial, :halt)
@@ -829,17 +1059,18 @@ defmodule System do
end
end
defp do_cmd(port, acc, fun) do
defp do_port(port, acc, fun) do
receive do
{^port, {:data, data}} ->
do_cmd(port, fun.(acc, {:cont, data}), fun)
do_port(port, fun.(acc, {:cont, data}), fun)
{^port, {:exit_status, status}} ->
{acc, status}
end
end
defp cmd_opts([{:into, any} | t], opts, _into), do: cmd_opts(t, opts, any)
defp cmd_opts([{:into, any} | t], opts, _into),
do: cmd_opts(t, opts, any)
defp cmd_opts([{:cd, bin} | t], opts, into) when is_binary(bin),
do: cmd_opts(t, [{:cd, bin} | opts], into)
@@ -850,7 +1081,8 @@ defmodule System do
defp cmd_opts([{:stderr_to_stdout, true} | t], opts, into),
do: cmd_opts(t, [:stderr_to_stdout | opts], into)
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into), do: cmd_opts(t, opts, into)
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into),
do: cmd_opts(t, opts, into)
defp cmd_opts([{:parallelism, bool} | t], opts, into) when is_boolean(bool),
do: cmd_opts(t, [{:parallelism, bool} | opts], into)
@@ -861,7 +1093,8 @@ defmodule System do
defp cmd_opts([{key, val} | _], _opts, _into),
do: raise(ArgumentError, "invalid option #{inspect(key)} with value #{inspect(val)}")
defp cmd_opts([], opts, into), do: {into, opts}
defp cmd_opts([], opts, into),
do: {into, opts}
defp validate_env(enum) do
Enum.map(enum, fn
@@ -940,7 +1173,7 @@ defmodule System do
unit before you display them to humans.
To determine how many seconds the `:native` unit represents in your current
runtime, you can can call this function to convert 1 second to the `:native`
runtime, you can call this function to convert 1 second to the `:native`
time unit: `System.convert_time_unit(1, :second, :native)`.
"""
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
+254 -139
View File
@@ -19,7 +19,7 @@ defmodule Task do
Besides `async/1` and `await/2`, tasks can also be
started as part of a supervision tree and dynamically spawned
on remote nodes. We will explore all three scenarios next.
on remote nodes. We will explore these scenarios next.
## async and await
@@ -43,78 +43,8 @@ defmodule Task do
meant to receive the result no longer exists, there is
no purpose in completing the computation.
If this is not desired, use `Task.start/1` or consider starting
the task under a `Task.Supervisor` using `async_nolink` or
`start_child`.
`Task.yield/2` is an alternative to `await/2` where the caller will
temporarily block, waiting until the task replies or crashes. If the
result does not arrive within the timeout, it can be called again at a
later moment. This allows checking for the result of a task multiple
times. If a reply does not arrive within the desired time,
`Task.shutdown/2` can be used to stop the task.
## Supervised tasks
It is also possible to spawn a task under a supervisor. The `Task`
module implements the `child_spec/1` function, which allows it to
be started directly under a supervisor by passing a tuple with
a function to run:
Supervisor.start_link([
{Task, fn -> :some_work end}
], strategy: :one_for_one)
However, if you want to invoke a specific module, function and
arguments, or give the task process a name, you need to define
the task in its own module:
defmodule MyTask do
use Task
def start_link(arg) do
Task.start_link(__MODULE__, :run, [arg])
end
def run(arg) do
# ...
end
end
And then passing it to the supervisor:
Supervisor.start_link([
{MyTask, arg}
], strategy: :one_for_one)
Since these tasks are supervised and not directly linked to
the caller, they cannot be awaited on. `start_link/1`, unlike
`async/1`, returns `{:ok, pid}` (which is the result expected
by supervisors).
`use Task` defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
`child_spec/1` can be customized with the following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the child should be restarted, defaults to `:temporary`
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
Opposite to `GenServer`, `Agent` and `Supervisor`, a Task has
a default `:restart` of `:temporary`. This means the task will
not be restarted even if it crashes. If you desire the task to
be restarted for non-successful exits, do:
use Task, restart: :transient
If you want the task to always be restarted:
use Task, restart: :permanent
See the "Child specification" section in the `Supervisor` module
for more detailed information. The `@doc` annotation immediately
preceding `use Task` will be attached to the generated `child_spec/1`
function.
If this is not desired, you will want to use supervised
tasks, described next.
## Dynamically supervised tasks
@@ -139,22 +69,36 @@ defmodule Task do
{Task.Supervisor, name: MyApp.TaskSupervisor}
], strategy: :one_for_one)
Now you can dynamically start supervised tasks:
Task.Supervisor.start_child(MyApp.TaskSupervisor, fn ->
# Do something
end)
Or even use the async/await pattern:
And now you can use async/await once again passig the name of
the supervisor isntead of the pid:
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
# Do something
end)
|> Task.await()
Finally, check `Task.Supervisor` for other supported operations.
We encourage developers to rely on supervised tasks as much as
possible. Supervised tasks enable a huge variety of patterns
which allows you explicit control on how to handle the results,
errors, and timeouts. Here is a summary:
## Distributed tasks
* Use `Task.Supervisor.start_child/2` to start a fire-and-forget
task and you don't care about its results nor about if it completes
successfully
* Use `Task.Supervisor.async/2` + `Task.await/2` allows you to execute
tasks concurrently and retrieve its result. If the task fails,
the caller will also fail
* Use `Task.Supervisor.async_nolink/2` + `Task.yield/2` + `Task.shutdown/2`
allows you to execute tasks concurrently and retrieve their results
or the reason they failed within a given time frame. If the task fails,
the caller won't fail: you will receive the error reason either on
`yield` or `shutdown`
See the `Task.Supervisor` module for details on the supported operations.
### Distributed tasks
Since Elixir provides a `Task.Supervisor`, it is easy to use one
to dynamically start tasks across nodes:
@@ -166,12 +110,79 @@ defmodule Task do
supervisor = {MyApp.DistSupervisor, :remote@local}
Task.Supervisor.async(supervisor, MyMod, :my_fun, [arg1, arg2, arg3])
Note that, when working with distributed tasks, one should use the `Task.Supervisor.async/4` function
that expects explicit module, function and arguments, instead of `Task.Supervisor.async/2` that
works with anonymous functions. That's because anonymous functions expect
the same module version to exist on all involved nodes. Check the `Agent` module
documentation for more information on distributed processes as the limitations
described there apply to the whole ecosystem.
Note that, when working with distributed tasks, one should use the
`Task.Supervisor.async/4` function that expects explicit module, function,
and arguments, instead of `Task.Supervisor.async/2` that works with anonymous
functions. That's because anonymous functions expect the same module version
to exist on all involved nodes. Check the `Agent` module documentation for
more information on distributed processes as the limitations described there
apply to the whole ecosystem.
## Statically supervised tasks
The `Task` module implements the `child_spec/1` function, which
allows it to be started directly under a regular `Supervisor` -
instead of a `Task.Supervisor` - by passing a tuple with a function
to run:
Supervisor.start_link([
{Task, fn -> :some_work end}
], strategy: :one_for_one)
This is often useful when you need to execute some steps while
setting up your supervision tree. For example: to warm up caches,
log the initialization status, etc.
If you don't want to put the Task code directly under the `Supervisor`,
you can wrap the `Task` in its own module, similar to how you would
do with a `GenServer` or an `Agent`:
defmodule MyTask do
use Task
def start_link(arg) do
Task.start_link(__MODULE__, :run, [arg])
end
def run(arg) do
# ...
end
end
And then passing it to the supervisor:
Supervisor.start_link([
{MyTask, arg}
], strategy: :one_for_one)
Since these tasks are supervised and not directly linked to the caller,
they cannot be awaited on. By default, the functions `Task.start`
and `Task.start_link` are for fire-and-forget tasks, where you don't
care about the results or if it completes successfully or not.
`use Task` defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
`child_spec/1` can be customized with the following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the child should be restarted, defaults to `:temporary`
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
Opposite to `GenServer`, `Agent` and `Supervisor`, a Task has
a default `:restart` of `:temporary`. This means the task will
not be restarted even if it crashes. If you desire the task to
be restarted for non-successful exits, do:
use Task, restart: :transient
If you want the task to always be restarted:
use Task, restart: :permanent
See the "Child specification" section in the `Supervisor` module
for more detailed information. The `@doc` annotation immediately
preceding `use Task` will be attached to the generated `child_spec/1`
function.
## Ancestor and Caller Tracking
@@ -287,11 +298,11 @@ defmodule Task do
end
@doc """
Starts a process linked to the current process.
Starts a task as part of a supervision tree with the given `fun`.
`fun` must be a zero-arity anonymous function.
This is often used to start the process as part of a supervision tree.
This is used to start a statically supervised task under a supervision tree.
"""
@spec start_link((() -> any)) :: {:ok, pid}
def start_link(fun) when is_function(fun, 0) do
@@ -299,12 +310,15 @@ defmodule Task do
end
@doc """
Starts a task as part of a supervision tree.
Starts a task as part of a supervision tree with the given
`module`, `function`, and `args`.
This is used to start a statically supervised task under a supervision tree.
"""
@spec start_link(module, atom, [term]) :: {:ok, pid}
def start_link(module, function_name, args)
when is_atom(module) and is_atom(function_name) and is_list(args) do
mfa = {module, function_name, args}
def start_link(module, function, args)
when is_atom(module) and is_atom(function) and is_list(args) do
mfa = {module, function, args}
Task.Supervised.start_link(get_owner(self()), get_callers(self()), mfa)
end
@@ -313,9 +327,14 @@ defmodule Task do
`fun` must be a zero-arity anonymous function.
This is only used when the task is used for side-effects
(i.e. no interest in the returned result) and it should not
be linked to the current process.
This should only used when the task is used for side-effects
(like I/O) and you have no interest on its results nor if it
completes successfully.
If the current node is shutdown, the node will terminate even
if the task was not completed. For this reason, we recommend
to use `Task.Supervisor.start_child/2` instead, which allows
you to control the shutdown time via the `:shutdown` option.
"""
@spec start((() -> any)) :: {:ok, pid}
def start(fun) when is_function(fun, 0) do
@@ -325,9 +344,14 @@ defmodule Task do
@doc """
Starts a task.
This is only used when the task is used for side-effects
(i.e. no interest in the returned result) and it should not
be linked to the current process.
This should only used when the task is used for side-effects
(like I/O) and you have no interest on its results nor if it
completes successfully.
If the current node is shutdown, the node will terminate even
if the task was not completed. For this reason, we recommend
to use `Task.Supervisor.start_child/2` instead, which allows
you to control the shutdown time via the `:shutdown` option.
"""
@spec start(module, atom, [term]) :: {:ok, pid}
def start(module, function_name, args)
@@ -339,30 +363,14 @@ defmodule Task do
@doc """
Starts a task that must be awaited on.
`fun` must be a zero-arity anonymous function.
This function spawns a process that is linked to and monitored
by the caller process. A `Task` struct is returned containing
the relevant information.
Read the `Task` module documentation for more information about the
general usage of `async/1` and `async/3`.
See also `async/3`.
"""
@spec async((() -> any)) :: t
def async(fun) when is_function(fun, 0) do
async(:erlang, :apply, [fun, []])
end
@doc """
Starts a task that must be awaited on.
A `Task` struct is returned containing the relevant information.
Developers must eventually call `Task.await/2` or `Task.yield/2`
followed by `Task.shutdown/2` on the returned task.
`fun` must be a zero-arity anonymous function. This function
spawns a process that is linked to and monitored by the caller
process. A `Task` struct is returned containing the relevant
information. Developers must eventually call `Task.await/2` or
`Task.yield/2` followed by `Task.shutdown/2` on the returned task.
Read the `Task` module documentation for more information about
the general usage of `async/1` and `async/3`.
the general usage of async tasks.
## Linking
@@ -411,11 +419,17 @@ defmodule Task do
to any supervisor, you may leave dangling tasks in case
the parent dies.
## Message format
"""
@spec async((() -> any)) :: t
def async(fun) when is_function(fun, 0) do
async(:erlang, :apply, [fun, []])
end
The reply sent by the task will be in the format `{ref, result}`,
where `ref` is the monitor reference held by the task struct
and `result` is the return value of the task function.
@doc """
Starts a task that must be awaited on.
Similar to `async/1` except the function to be started is
specified by the given `module`, `function_name`, and `args`.
"""
@spec async(module, atom, [term]) :: t
def async(module, function_name, args)
@@ -456,10 +470,13 @@ defmodule Task do
at the same time. Defaults to `System.schedulers_online/0`.
* `:ordered` - whether the results should be returned in the same order
as the input stream. This option is useful when you have large
streams and don't want to buffer results before they are delivered.
This is also useful when you're using the tasks for side effects.
Defaults to `true`.
as the input stream. When the output is ordered, Elixir may need to
buffer results to emit them in the original order. Setting this option
to false disables the need to buffer at the cost of removing ordering.
This is also useful when you're using the tasks only for the side effects.
Note that regardless of what `:ordered` is set to, the tasks will
process asynchronously. If you need to process elements in order,
consider using `Enum.map/2` or `Enum.each/2` instead. Defaults to `true`.
* `:timeout` - the maximum amount of time (in milliseconds or `:infinity`)
each task is allowed to execute for. Defaults to `5000`.
@@ -491,6 +508,38 @@ defmodule Task do
stream = Task.async_stream(collection, Mod, :expensive_fun, [], ordered: false)
Stream.run(stream)
## Attention: async + take
Given items in an async stream are processed concurrently, doing
`async_stream` followed by `Enum.take/2` may cause more items than
requested to be processed. Let's see an example:
1..100
|> Task.async_stream(fn i ->
Process.sleep(100)
IO.puts(to_string(i))
end)
|> Enum.take(10)
For a machine with 8 cores, the above will process 16 items instead
of 10. The reason is that `async_stream/5` always have 8 elements
processing at once. So by the time `Enum` says it got all elements
it needed, there are still 6 elements left to be processed.
The solution here is to use `Stream.take/2` instead of `Enum.take/2`
to filter elements before-hand:
1..100
|> Stream.take(10)
|> Task.async_stream(fn i ->
Process.sleep(100)
IO.puts(to_string(i))
end)
|> Enum.to_list()
If for some reason you cannot take the elements before hand,
you can use `:max_concurrency` to limit how many elements
may be over processed at the cost of reducing concurrency.
"""
@doc since: "1.4.0"
@spec async_stream(Enumerable.t(), module, atom, [term], keyword) :: Enumerable.t()
@@ -555,7 +604,7 @@ defmodule Task do
end
end
@doc """
@doc ~S"""
Awaits a task reply and returns it.
In case the task process dies, the current process will exit with the same
@@ -577,20 +626,86 @@ defmodule Task do
to be able to check multiple times if a long-running task has finished
its computation, use `yield/2` instead.
## Compatibility with OTP behaviours
It is not recommended to `await` a long-running task inside an OTP
behaviour such as `GenServer`. Instead, you should match on the message
coming from a task inside your `c:GenServer.handle_info/2` callback. For
more information on the format of the message, see the documentation for
`async/1`.
## Examples
iex> task = Task.async(fn -> 1 + 1 end)
iex> Task.await(task)
2
## Compatibility with OTP behaviours
It is not recommended to `await` a long-running task inside an OTP
behaviour such as `GenServer`. Instead, you should match on the message
coming from a task inside your `c:GenServer.handle_info/2` callback.
A GenServer will receive two messages on `handle_info/2`:
* `{ref, result}` - the reply message where `ref` is the monitor
reference returned by the `task.ref` and `result` is the task
result
* `{:DOWN, ref, :process, pid, reason}` - since all tasks are also
monitored, you will also receive the `:DOWN` message delivered by
`Process.monitor/1`. If you receive the `:DOWN` message without a
a reply, it means the task crashed
Another consideration to have in mind is that tasks started by `Task.async/1`
are always linked to their callers and you may not want the GenServer to
crash if the task crashes. Therefore, it is preferable to instead use
`Task.Supervisor.async_nolink/3` inside OTP behaviours. For completeness, here
is an example of a GenServer that start tasks and handles their results:
defmodule GenServerTaskExample do
use GenServer
def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def init(_opts) do
# We will keep all running tasks in a map
{:ok, %{tasks: %{}}}
end
# Imagine we invoke a task from the GenServer to access a URL...
def handle_call(:some_message, _from, state) do
url = ...
task = Task.Supervisor.async_nolink(MyApp.TaskSupervisor, fn -> fetch_url(url) end)
# After we start the task, we store its reference and the url it is fetching
state = put_in(state.tasks[task.ref], url)
{:reply, :ok, state}
end
# If the task succeeds...
def handle_info({ref, result}, state) do
# The task succeed so we can cancel the monitoring and discard the DOWN message
Process.demonitor(ref, [:flush])
{url, state} = pop_in(state.tasks[ref])
IO.puts "Got #{inspect(result)} for URL #{inspect url}"
{:noreply, state}
end
# If the task fails...
def handle_info({:DOWN, ref, _, _, reason}, state) do
{url, state} = pop_in(state.tasks[ref])
IO.puts "URL #{inspect url} failed with reason #{inspect(reason)}"
{:noreply, state}
end
end
With the server defined, you will want to start the task supervisor
above and the GenServer in your supervision tree:
children = [
{Task.Supervisor, name: MyApp.TaskSupervisor},
{GenServerTaskExample, name: MyApp.GenServerTaskExample}
]
Supervisor.start_link(children, strategy: :one_for_one)
"""
@spec await(t, timeout) :: term
def await(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
+8 -9
View File
@@ -177,6 +177,11 @@ defmodule Task.Supervised do
def stream(enumerable, acc, reducer, mfa, options, spawn) do
next = &Enumerable.reduce(enumerable, &1, fn x, acc -> {:suspend, [x | acc]} end)
max_concurrency = Keyword.get(options, :max_concurrency, System.schedulers_online())
unless is_integer(max_concurrency) and max_concurrency > 0 do
raise ArgumentError, ":max_concurrency must be an integer greater than zero"
end
ordered? = Keyword.get(options, :ordered, true)
timeout = Keyword.get(options, :timeout, 5000)
on_timeout = Keyword.get(options, :on_timeout, :exit)
@@ -312,6 +317,7 @@ defmodule Task.Supervised do
stream_deliver({:cont, acc}, max + 1, spawned, delivered, waiting, next, config)
else
pair = deliver_now(result, acc, next, config)
waiting = Map.delete(waiting, position)
stream_reduce(pair, max + 1, spawned, delivered + 1, waiting, next, config)
end
@@ -392,15 +398,8 @@ defmodule Task.Supervised do
:erlang.raise(kind, reason, __STACKTRACE__)
else
pair ->
stream_deliver(
pair,
max,
spawned,
delivered + 1,
Map.delete(waiting, delivered),
next,
config
)
waiting = Map.delete(waiting, delivered)
stream_deliver(pair, max, spawned, delivered + 1, waiting, next, config)
end
%{} ->
+8 -7
View File
@@ -27,10 +27,7 @@ defmodule Task.Supervisor do
@typedoc "Option values used by `start_link`"
@type option ::
DynamicSupervisor.option()
# :permanent | :transient | :temporary here because :supervisor.restart() is not exported
| {:restart, :permanent | :transient | :temporary}
# :brutal_kill | timeout() here because :supervisor.shutdown() is not exported
| {:shutdown, :brutal_kill | timeout()}
| DynamicSupervisor.init_option()
@doc false
def child_spec(opts) when is_list(opts) do
@@ -72,7 +69,7 @@ defmodule Task.Supervisor do
described under the `Name Registration` section in the `GenServer` module
docs;
* `:max_restarts`, `:max_seconds` and `:max_children` - as specified in
* `:max_restarts`, `:max_seconds`, and `:max_children` - as specified in
`DynamicSupervisor`;
This function could also receive `:restart` and `:shutdown` as options
@@ -382,10 +379,14 @@ defmodule Task.Supervisor do
@doc """
Starts a task as a child of the given `supervisor`.
Task.Supervisor.start_child(MyTaskSupervisor, fn ->
IO.puts "I am running in a task"
end)
Note that the spawned process is not linked to the caller, but
only to the supervisor. This command is useful in case the
task needs to perform side-effects (like I/O) and does not need
to report back to the caller.
task needs to perform side-effects (like I/O) and you have no
interest on its results nor if it completes successfully.
## Options
+38
View File
@@ -128,6 +128,44 @@ defmodule Tuple do
:erlang.delete_element(index + 1, tuple)
end
@doc """
Computes a sum of tuple elements.
## Examples
iex> Tuple.sum({255, 255})
510
iex> Tuple.sum({255, 0.0})
255.0
iex> Tuple.sum({})
0
"""
@doc since: "1.12.0"
@spec sum(tuple) :: number()
def sum(tuple), do: sum(tuple, tuple_size(tuple))
defp sum(_tuple, 0), do: 0
defp sum(tuple, index), do: :erlang.element(index, tuple) + sum(tuple, index - 1)
@doc """
Computes a product of tuple elements.
## Examples
iex> Tuple.product({255, 255})
65025
iex> Tuple.product({255, 1.0})
255.0
iex> Tuple.product({})
1
"""
@doc since: "1.12.0"
@spec product(tuple) :: number()
def product(tuple), do: product(tuple, tuple_size(tuple))
defp product(_tuple, 0), do: 1
defp product(tuple, index), do: :erlang.element(index, tuple) * product(tuple, index - 1)
@doc """
Converts a tuple to a list.
+126 -56
View File
@@ -71,55 +71,89 @@ defmodule URI do
end
@doc """
Encodes an enumerable into a query string.
Encodes `enumerable` into a query string using `encoding`.
Takes an enumerable that enumerates as a list of two-element
tuples (for instance, a map or a keyword list) and returns a string
in the form of `key1=value1&key2=value2...` where keys and
values are URL encoded as per `encode_www_form/1`.
in the form of `key1=value1&key2=value2...`.
Keys and values can be any term that implements the `String.Chars`
protocol with the exception of lists, which are explicitly forbidden.
You can specify one of the following `encoding` strategies:
* `:www_form` - (default, since v1.12.0) keys and values are URL encoded as
per `encode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It encodes " " as "+".
* `:rfc3986` - (since v1.12.0) the same as `:www_form` except it encodes
" " as "%20" according [RFC 3986](https://tools.ietf.org/html/rfc3986).
This is the best option if you are encoding in a non-browser situation,
since encoding spaces as "+" can be ambiguous to URI parsers. This can
inadvertently lead to spaces being interpreted as literal plus signs.
Encoding defaults to `:www_form` for backward compatibility.
## Examples
iex> hd = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(hd)
iex> query = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(query)
"bar=2&foo=1"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query)
"key=value+with+spaces"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query, :rfc3986)
"key=value%20with%20spaces"
iex> URI.encode_query(%{key: [:a, :list]})
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
** (ArgumentError) encode_query/2 values cannot be lists, got: [:a, :list]
"""
@spec encode_query(Enum.t()) :: binary
def encode_query(enumerable) do
Enum.map_join(enumerable, "&", &encode_kv_pair/1)
@spec encode_query(Enum.t(), :rfc3986 | :www_form) :: binary
def encode_query(enumerable, encoding \\ :www_form) do
Enum.map_join(enumerable, "&", &encode_kv_pair(&1, encoding))
end
defp encode_kv_pair({key, _}) when is_list(key) do
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect(key)}"
defp encode_kv_pair({key, _}, _encoding) when is_list(key) do
raise ArgumentError, "encode_query/2 keys cannot be lists, got: #{inspect(key)}"
end
defp encode_kv_pair({_, value}) when is_list(value) do
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect(value)}"
defp encode_kv_pair({_, value}, _encoding) when is_list(value) do
raise ArgumentError, "encode_query/2 values cannot be lists, got: #{inspect(value)}"
end
defp encode_kv_pair({key, value}) do
defp encode_kv_pair({key, value}, :rfc3986) do
encode(Kernel.to_string(key), &char_unreserved?/1) <>
"=" <> encode(Kernel.to_string(value), &char_unreserved?/1)
end
defp encode_kv_pair({key, value}, :www_form) do
encode_www_form(Kernel.to_string(key)) <> "=" <> encode_www_form(Kernel.to_string(value))
end
@doc """
Decodes a query string into a map.
Decodes `query` into a map.
Given a query string in the form of `key1=value1&key2=value2...`, this
function inserts each key-value pair in the query string as one entry in the
given `map`. Keys and values in the resulting map will be binaries. Keys and
values will be percent-unescaped.
You can specify one of the following `encoding` options:
* `:www_form` - (default, since v1.12.0) keys and values are decoded as per
`decode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It decodes "+" as " ".
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
`decode/1`. The result is the same as `:www_form` except for leaving "+"
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
Encoding defaults to `:www_form` for backward compatibility.
Use `query_decoder/1` if you want to iterate over each value manually.
## Examples
@@ -130,43 +164,54 @@ defmodule URI do
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
%{"percent" => "oh yes!", "starting" => "map"}
iex> URI.decode_query("percent=oh+yes%21", %{}, :rfc3986)
%{"percent" => "oh+yes!"}
"""
@spec decode_query(binary, %{optional(binary) => binary}) :: %{optional(binary) => binary}
def decode_query(query, map \\ %{})
@spec decode_query(binary, %{optional(binary) => binary}, :rfc3986 | :www_form) :: %{
optional(binary) => binary
}
def decode_query(query, map \\ %{}, encoding \\ :www_form)
def decode_query(query, %_{} = dict) when is_binary(query) do
IO.warn("URI.decode_query/2 is deprecated, please use URI.decode_query/1")
decode_query_into_dict(query, dict)
def decode_query(query, %_{} = dict, encoding) when is_binary(query) do
IO.warn(
"URI.decode_query/3 expects the second argument to be a map, other usage is deprecated"
)
decode_query_into_dict(query, dict, encoding)
end
def decode_query(query, map) when is_binary(query) and is_map(map) do
decode_query_into_map(query, map)
def decode_query(query, map, encoding) when is_binary(query) and is_map(map) do
decode_query_into_map(query, map, encoding)
end
def decode_query(query, dict) when is_binary(query) do
IO.warn("URI.decode_query/2 is deprecated, please use URI.decode_query/1")
decode_query_into_dict(query, dict)
def decode_query(query, dict, encoding) when is_binary(query) do
IO.warn(
"URI.decode_query/3 expects the second argument to be a map, other usage is deprecated"
)
decode_query_into_dict(query, dict, encoding)
end
defp decode_query_into_map(query, map) do
case decode_next_query_pair(query) do
defp decode_query_into_map(query, map, encoding) do
case decode_next_query_pair(query, encoding) do
nil ->
map
{{key, value}, rest} ->
decode_query_into_map(rest, Map.put(map, key, value))
decode_query_into_map(rest, Map.put(map, key, value), encoding)
end
end
defp decode_query_into_dict(query, dict) do
case decode_next_query_pair(query) do
defp decode_query_into_dict(query, dict, encoding) do
case decode_next_query_pair(query, encoding) do
nil ->
dict
{{key, value}, rest} ->
# Avoid warnings about Dict being deprecated
dict_module = Dict
decode_query_into_dict(rest, dict_module.put(dict, key, value))
decode_query_into_dict(rest, dict_module.put(dict, key, value), encoding)
end
end
@@ -176,25 +221,40 @@ defmodule URI do
Key and value in each tuple will be binaries and will be percent-unescaped.
You can specify one of the following `encoding` options:
* `:www_form` - (default, since v1.12.0) keys and values are decoded as per
`decode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It decodes "+" as " ".
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
`decode/1`. The result is the same as `:www_form` except for leaving "+"
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
Encoding defaults to `:www_form` for backward compatibility.
## Examples
iex> URI.query_decoder("foo=1&bar=2") |> Enum.to_list()
[{"foo", "1"}, {"bar", "2"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water") |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please") |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water please"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please", :rfc3986) |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water+please"}]
"""
@spec query_decoder(binary) :: Enumerable.t()
def query_decoder(query) when is_binary(query) do
Stream.unfold(query, &decode_next_query_pair/1)
@spec query_decoder(binary, :rfc3986 | :www_form) :: Enumerable.t()
def query_decoder(query, encoding \\ :www_form) when is_binary(query) do
Stream.unfold(query, &decode_next_query_pair(&1, encoding))
end
defp decode_next_query_pair("") do
defp decode_next_query_pair("", _encoding) do
nil
end
defp decode_next_query_pair(query) do
defp decode_next_query_pair(query, encoding) do
{undecoded_next_pair, rest} =
case :binary.split(query, "&") do
[next_pair, rest] -> {next_pair, rest}
@@ -203,13 +263,24 @@ defmodule URI do
next_pair =
case :binary.split(undecoded_next_pair, "=") do
[key, value] -> {decode_www_form(key), decode_www_form(value)}
[key] -> {decode_www_form(key), ""}
[key, value] ->
{decode_with_encoding(key, encoding), decode_with_encoding(value, encoding)}
[key] ->
{decode_with_encoding(key, encoding), ""}
end
{next_pair, rest}
end
defp decode_with_encoding(string, :www_form) do
decode_www_form(string)
end
defp decode_with_encoding(string, :rfc3986) do
decode(string)
end
@doc ~s"""
Checks if `character` is a reserved one in a URI.
@@ -300,6 +371,10 @@ defmodule URI do
@doc """
Encodes `string` as "x-www-form-urlencoded".
Note "x-www-form-urlencoded" is not specified as part of
RFC 3986. However, it is a commonly used format to encode
query strings and form data by browsers.
## Example
iex> URI.encode_www_form("put: it+й")
@@ -347,6 +422,10 @@ defmodule URI do
@doc """
Decodes `string` as "x-www-form-urlencoded".
Note "x-www-form-urlencoded" is not specified as part of
RFC 3986. However, it is a commonly used format to encode
query strings and form data by browsers.
## Examples
iex> URI.decode_www_form("%3Call+in%2F")
@@ -383,14 +462,13 @@ defmodule URI do
defp hex_to_dec(_n), do: throw(:malformed_uri)
@doc """
Parses a well-formed URI reference into its components.
Parses a well-formed URI into its components.
Note this function expects a well-formed URI and does not perform
any validation. See the "Examples" section below for examples of how
`URI.parse/1` can be used to parse a wide range of URIs.
This function uses the parsing regular expression as defined
in [RFC 3986, Appendix B](https://tools.ietf.org/html/rfc3986#appendix-B).
This function can parse both absolute and relative URLs. You can check
if a URI is absolute or relative by checking if the `scheme` field is
nil or not. Furthermore, this function expects both absolute and
relative URIs to be well-formed and does not perform any validation.
See the "Examples" section below.
When a URI is given without a port, the value returned by
`URI.default_port/1` for the URI's scheme is used for the `:port` field.
@@ -643,16 +721,8 @@ defmodule URI do
defp remove_dot_segments([head | tail], acc), do: remove_dot_segments(tail, [head | acc])
defp path_to_segments(path) do
[head | tail] = String.split(path, "/")
reverse_and_discard_empty(tail, [head])
path |> String.split("/") |> Enum.reverse()
end
defp reverse_and_discard_empty([], acc), do: acc
defp reverse_and_discard_empty([head], acc), do: [head | acc]
defp reverse_and_discard_empty(["" | tail], acc), do: reverse_and_discard_empty(tail, acc)
defp reverse_and_discard_empty([head | tail], acc),
do: reverse_and_discard_empty(tail, [head | acc])
end
defimpl String.Chars, for: URI do
@@ -8,12 +8,12 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
Elixir version | Support
:------------- | :-----------------------------
1.11 | Development
1.10 | Bug fixes and security patches
1.12 | Bug fixes and security patches
1.11 | Security patches only
1.10 | Security patches only
1.9 | Security patches only
1.8 | Security patches only
1.7 | Security patches only
1.6 | Security patches only
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
@@ -39,24 +39,25 @@ The only exception to the compatibility guarantees above are experimental featur
## Compatibility between Elixir and Erlang/OTP
Erlang/OTP versioning is independent from the versioning of Elixir. Each version of Elixir supports a specific range of Erlang/OTP versions. The compatibility table is shown below.
Erlang/OTP versioning is independent from the versioning of Elixir. Erlang releases a new major version yearly. Our goal is to support the last three Erlang major versions by the time Elixir is released. The compatibility table is shown below.
Elixir version | Supported Erlang/OTP versions
:------------- | :-------------------------------
1.0 | 17 - 17 (and Erlang/OTP 18 from v1.0.5)
1.1 | 17 - 18
1.2 | 18 - 18 (and Erlang/OTP 19 from v1.2.6)
1.3 | 18 - 19
1.4 | 18 - 19 (and Erlang/OTP 20 from v1.4.5)
1.5 | 18 - 20
1.6 | 19 - 20 (and Erlang/OTP 21 from v1.6.6)
1.7 | 19 - 22
1.8 | 20 - 22
1.9 | 20 - 22
1.12 | 22 - 24
1.11 | 21 - 23 (and Erlang/OTP 24 from v1.11.4)
1.10 | 21 - 22 (and Erlang/OTP 23 from v1.10.3)
1.11 | 21 - 23
1.9 | 20 - 22
1.8 | 20 - 22
1.7 | 19 - 22
1.6 | 19 - 20 (and Erlang/OTP 21 from v1.6.6)
1.5 | 18 - 20
1.4 | 18 - 19 (and Erlang/OTP 20 from v1.4.5)
1.3 | 18 - 19
1.2 | 18 - 18 (and Erlang/OTP 19 from v1.2.6)
1.1 | 17 - 18
1.0 | 17 - 17 (and Erlang/OTP 18 from v1.0.5)
While Elixir often adds compatibility to new Erlang/OTP versions on released branches, such as support for Erlang/OTP 20 in v1.4.5, those releases usually contain the minimum changes for Elixir to run without errors. Only the next minor release, in this case v1.5.0, does effectively leverage the new features provided by the latest Erlang/OTP release.
Note Elixir may add compatibility to new Erlang/OTP versions on patch releases, such as support for Erlang/OTP 20 in v1.4.5. Those releases are made for convenience and typically contain the minimum changes for Elixir to run without errors, if any changes are necessary. Only the next minor release, in this case v1.5.0, effectively leverages the new features provided by the latest Erlang/OTP release.
## Deprecations
@@ -76,6 +77,10 @@ The first column is the version the feature was hard deprecated. The second colu
Version | Deprecated feature | Replaced by (available since)
:-------| :-------------------------------------------------- | :---------------------------------------------------------------
[v1.12] | `^^^/2` | Use `bxor/2` instead (v1.0)
[v1.12] | `@foo()` to read module attributes | Remove the parenthesis (v1.0)
[v1.12] | `use EEx.Engine` | Explicitly delegate to EEx.Engine instead (v1.0)
[v1.12] | `:xref` compiler in Mix | Nothing (it always runs as part of the compiler now)
[v1.11] | `Mix.Project.compile/2` | `Mix.Task.run("compile", args)` (v1.0)
[v1.11] | `Supervisor.Spec.worker/3` and `Supervisor.Spec.supervisor/3` | The new child specs outlined in `Supervisor` (v1.5)
[v1.11] | `Supervisor.start_child/2` and `Supervisor.terminate_child/2` | `DynamicSupervisor` (v1.6)
@@ -92,6 +97,7 @@ Version | Deprecated feature | Replaced by (ava
[v1.9] | Enumerable keys in `Map.drop/2`, `Map.split/2`, and `Map.take/2` | Call `Enum.to_list/1` on the second argument before hand (v1.0)
[v1.9] | `Mix.Project.load_paths/1` | `Mix.Project.compile_path/1` (v1.0)
[v1.9] | Passing `:insert_replaced` to `String.replace/4` | Use `:binary.replace/4` (v1.0)
[v1.8] | Passing a non-empty list to `Collectable.into/1` | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Passing a non-empty list to `:into` in [`for`](`Kernel.SpecialForms.for/1`) | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Passing a non-empty list to `Enum.into/2` | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Time units in its plural form, such as: `:seconds`, `:milliseconds`, and the like | Use the singular form, such as: `:second`, `:millisecond`, and so on (v1.4)
@@ -105,7 +111,7 @@ Version | Deprecated feature | Replaced by (ava
[v1.8] | `System.cwd/0` and `System.cwd!/0` | `File.cwd/0` and `File.cwd!/0` (v1.0)
[v1.7] | `Code.get_docs/2` | `Code.fetch_docs/1` (v1.7)
[v1.7] | `Enum.chunk/2,3,4` | `Enum.chunk_every/2` and [`Enum.chunk_every/3,4`](`Enum.chunk_every/4`) (v1.5)
[v1.7] | Calling `super/1` in`GenServer` callbacks | Implenting the behaviour explicitly without calling `super/1` (v1.0)
[v1.7] | Calling `super/1` in`GenServer` callbacks | Implementing the behaviour explicitly without calling `super/1` (v1.0)
[v1.7] | [`not left in right`](`Kernel.in/2`) | [`left not in right`](`Kernel.in/2`) (v1.5)
[v1.7] | `Registry.start_link/3` | `Registry.start_link/1` (v1.5)
[v1.7] | `Stream.chunk/2,3,4` | `Stream.chunk_every/2` and [`Stream.chunk_every/3,4`](`Stream.chunk_every/4`) (v1.5)
@@ -118,7 +124,7 @@ Version | Deprecated feature | Replaced by (ava
[v1.5] | `Atom.to_char_list/1` | `Atom.to_charlist/1` (v1.3)
[v1.5] | `Enum.filter_map/3` | `Enum.filter/2` + `Enum.map/2` or [`for`](`Kernel.SpecialForms.for/1`) comprehensions (v1.0)
[v1.5] | `Float.to_char_list/1` | `Float.to_charlist/1` (v1.3)
[v1.5] | `GenEvent` module | `Supervisor` and `GenServer` (v1.0);<br/>[`GenStage`](https://hex.pm/packages/gen_stage) (v1.3);<br/>[`:gen_event`](http://www.erlang.org/doc/man/gen_event.html) (Erlang/OTP 17)
[v1.5] | `GenEvent` module | `Supervisor` and `GenServer` (v1.0);<br/>[`GenStage`](https://hex.pm/packages/gen_stage) (v1.3);<br/>[`:gen_event`](`:gen_event`) (Erlang/OTP 17)
[v1.5] | `<%=` in middle and end expressions in `EEx` | Use `<%` (`<%=` is allowed only in start expressions) (v1.0)
[v1.5] | `:as_char_lists` value in `t:Inspect.Opts.t/0` type | `:as_charlists` value (v1.3)
[v1.5] | `:char_lists` key in `t:Inspect.Opts.t/0` type | `:charlists` key (v1.3)
@@ -174,4 +180,5 @@ Version | Deprecated feature | Replaced by (ava
[v1.8]: https://github.com/elixir-lang/elixir/blob/v1.8/CHANGELOG.md#4-hard-deprecations
[v1.9]: https://github.com/elixir-lang/elixir/blob/v1.9/CHANGELOG.md#4-hard-deprecations
[v1.10]: https://github.com/elixir-lang/elixir/blob/v1.10/CHANGELOG.md#4-hard-deprecations
[v1.11]: https://github.com/elixir-lang/elixir/blob/master/CHANGELOG.md#4-hard-deprecations
[v1.11]: https://github.com/elixir-lang/elixir/blob/v1.11/CHANGELOG.md#4-hard-deprecations
[v1.12]: https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md#4-hard-deprecations
+18 -2
View File
@@ -34,6 +34,14 @@ Writing code is only the first of many steps to publish a package. We strongly r
Projects are often made available to other developers [by publishing a Hex package](https://hex.pm/docs/publish). Hex also [supports private packages for organizations](https://hex.pm/pricing). If ExDoc is configured for the Mix project, publishing a package on Hex will also automatically publish the generated documentation to [HexDocs](https://hexdocs.pm).
## Dependency handling
When your library is published and used as a dependency, its [lockfile](https://hexdocs.pm/mix/Mix.Project.html#module-configuration) (usually named `mix.lock`) is _ignored by the host project_. Running `mix deps.get` in the host project attempts to get the latest possible versions of your library’s dependencies, as specified by the requirements in the `deps` section of your `mix.exs`. These versions might be greater than those stored in your `mix.lock` (and hence used in your tests / CI).
On the other hand, contributors of your library, need a deterministic build, which implies the presence of `mix.lock` in your Version Control System (VCS).
The best practice of handling `mix.lock` file therefore would be to keep it in VCS, and run two different Continuous Integration (CI) workflows: the usual deterministic one, and another one, that starts with `mix deps.unlock --all` and always compiles your library and runs tests against latest versions of dependencies. The latter one might be even run nightly or otherwise recurrently to stay notified about any possible issue in regard to dependencies updates.
## Anti-patterns
In this section we document common anti-patterns to avoid when writing libraries.
@@ -170,7 +178,15 @@ end
That's because by reading the application in the module body and storing it in a module attribute, we are effectively reading the configuration at compile-time, which may become an issue when configuring the system later.
If, for some reason, you must read the application environment at compile time, use `Application.compile_env/2`. Read [the "Compile-time environment" section of the Application docs](Application.html#module-compile-time-environment) for more information.
If, for some reason, you must read the application environment at compile time, use `Application.compile_env/2`. Read [the "Compile-time environment" section of the `Application` module documentation](Application.html#module-compile-time-environment) for more information.
### Avoid defining modules that are not in your "namespace"
Even though Elixir does not formally have the concept of namespaces, a library should use its name as a "prefix" for all of its modules (except for special cases like mix tasks). For example if the library's OTP application name is `:my_lib`, then all of its modules should start with the `MyLib` prefix, for example `MyLib.User`, `MyLib.SubModule`, and `MyLib.Application`.
This is important because the Erlang VM can only load one instance of a module at a time. So if there are multiple libraries that define the same module, then they are incompatible with each other due to this limitation. By always using the library name as a prefix, it avoids module name clashes due to the unique prefix.
Furthermore, when writing a library that is an extension of another library, you should avoid defining modules inside the parent's library namespace. For example, if you are writing a package that adds authentication to [`Plug`](https://github.com/elixir-plug/plug) called `plug_auth`, its modules should be namespaced under `PlugAuth` instead of `Plug.Auth`, so it avoid conflicts with `Plug` if it were to ever define its own authentication functionality.
### Avoid `use` when an `import` is enough
@@ -218,7 +234,7 @@ While there are situations where `use SomeModule` is necessary, `use` should be
Although the previous section could be summarized as "avoid macros", both topics are important enough to deserve their own sections.
To quote [the official guide on Macros](https://elixir-lang.org/getting-started/meta/macros.html):
To quote [the official guide on macros](https://elixir-lang.org/getting-started/meta/macros.html):
> Even though Elixir attempts its best to provide a safe environment for macros, the major responsibility of writing clean code with macros falls on developers. Macros are harder to write than ordinary Elixir functions and it's considered to be bad style to use them when they're not necessary. So write macros responsibly.
>
+24 -54
View File
@@ -6,28 +6,27 @@ This document covers operators in Elixir, how they are parsed, how they can be d
The following is a list of all operators that Elixir is capable of parsing, ordered from higher to lower precedence, alongside their associativity:
Operator | Associativity
---------------------------------------------------------------------------------------- | -------------
`@` | Unary
`.` | Left to right
`+` `-` `!` `^` `not` `~~~` | Unary
`*` `/` | Left to right
`+` `-` | Left to right
`++` `--` `..` `<>` `+++` `---` | Right to left
`^^^` | Left to right
`in` `not in` | Left to right
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left to right
`<` `>` `<=` `>=` | Left to right
`==` `!=` `=~` `===` `!==` | Left to right
`&&` `&&&` `and` | Left to right
`\|\|` `\|\|\|` `or` | Left to right
`=` | Right to left
`&` | Unary
`=>` (valid syntax only inside `%{}`) | Right to left
`\|` | Right to left
`::` | Right to left
`when` | Right to left
`<-` `\\` | Left to right
Operator | Associativity
----------------------------------------------------- | -------------
`@` | Unary
`.` | Left
`+` `-` `!` `^` `not` `~~~` | Unary
`*` `/` | Left
`+` `-` | Left
`++` `--` `+++` `---` `..` `<>` | Right
`in` `not in` | Left
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left
`<` `>` `<=` `>=` | Left
`==` `!=` `=~` `===` `!==` | Left
`&&` `&&&` `and` | Left
`\|\|` `\|\|\|` `or` | Left
`=` | Right
`&` | Unary
`=>` (valid only inside `%{}`) | Right
`\|` | Right
`::` | Right
`when` | Right
`<-` `\\` | Left
## General operators
@@ -155,41 +154,12 @@ The following is a table of all the operators that Elixir is capable of parsing,
* `~>`
* `<~>`
* `<|>`
* `^^^`
* `+++`
* `---`
* `~~~`
The following operators are used by the `Bitwise` module when imported: [`&&&`](`Bitwise.&&&/2`), [`^^^`](`Bitwise.^^^/2`), [`<<<`](`Bitwise.<<</2`), [`>>>`](`Bitwise.>>>/2`), [`|||`](`Bitwise.|||/2`), [`~~~`](`Bitwise.~~~/1`). See the documentation for `Bitwise` for more information.
The following operators are used by the `Bitwise` module when imported: [`&&&`](`Bitwise.&&&/2`), [`<<<`](`Bitwise.<<</2`), [`>>>`](`Bitwise.>>>/2`), [`|||`](`Bitwise.|||/2`), [`~~~`](`Bitwise.~~~/1`). See the documentation for `Bitwise` for more information.
### Redefining existing operators
Note the Elixir community generally discourages custom operators. They can be hard to read and even more to understand, as they don't have a descriptive name like functions do. That said, some specific cases or custom domain specific languages (DSLs) may justify these practices.
The operators that Elixir uses (for example, `+`) can be defined by any module and used in place of the ones defined by Elixir, provided they're specifically not imported from `Kernel` (which is imported everywhere by default). For example:
```elixir
defmodule WrongMath do
# Let's make math wrong by changing the meaning of +:
def a + b, do: a - b
end
```
Now, we will get an error if we try to use this operator "out of the box":
```elixir
iex> import WrongMath
iex> 1 + 2
** (CompileError) iex:11: function +/2 imported from both WrongMath and Kernel, call is ambiguous
```
So, as mentioned above, we need to explicitly *not* import `+/2` from `Kernel`:
```elixir
iex> import WrongMath
iex> import Kernel, except: [+: 2]
iex> 1 + 2
-1
```
### Final note
While it's possible to define unused operators (such as `<~>`) and to "override" predefined operators (such as `+`), the Elixir community generally discourages this. Custom-defined operators can be really hard to read and even more to understand, as they don't have a descriptive name like functions do. That said, some specific cases or custom domain specific languages (DSLs) may justify these practices.
It is also possible replace predefined operators, such as `+`, but doing so is extremely discouraged.
+3 -3
View File
@@ -1,12 +1,12 @@
# Patterns and Guards
Elixir provides pattern matching, which allows us to assert on the shape or extract values from data-structures. Patterns are often augmented with guards, which give developers the ability to perform more complex checks, albeit limited.
Elixir provides pattern matching, which allows us to assert on the shape or extract values from data structures. Patterns are often augmented with guards, which give developers the ability to perform more complex checks, albeit limited.
This page describes the semantics of patterns and guards, where they are all allowed, and how to extend them.
## Patterns
Patterns in Elixir are made of variables, literals, and data-structure specific syntax. One of the most used constructs to perform pattern matching is the match operator ([`=`](`=/2`)):
Patterns in Elixir are made of variables, literals, and data structure specific syntax. One of the most used constructs to perform pattern matching is the match operator ([`=`](`=/2`)):
```iex
iex> x = 1
@@ -33,7 +33,7 @@ iex> 1 = y
In other words, patterns are allowed only on the left side of `=`. The right side of `=` follows the regular evaluation semantics of the language.
Now let's cover the pattern matching rules for each construct and then for each relevant data-types.
Now let's cover the pattern matching rules for each construct and then for each relevant data types.
### Variables
+13 -5
View File
@@ -21,7 +21,7 @@ Integers (`1234`) and floats (`123.4`) in Elixir are represented as a sequence o
### Atoms
Unquoted atoms start with a colon (`:`) which must be immediately followed by an underscore or a Unicode letter. The atom may continue using a sequence of Unicode letters, numbers, underscores, and `@`. Atoms may end in `!` or `?`. See [Unicode Syntax](unicode-syntax.md) for a formal specification. Valid unquoted atoms are: `:ok`, `:ISO8601`, and `:integer?`.
Unquoted atoms start with a colon (`:`) which must be immediately followed by a Unicode letter or an underscore. The atom may continue using a sequence of Unicode letters, numbers, underscores, and `@`. Atoms may end in `!` or `?`. See [Unicode syntax](unicode-syntax.md) for a formal specification. Valid unquoted atoms are: `:ok`, `:ISO8601`, and `:integer?`.
If the colon is immediately followed by a pair of double- or single-quotes surrounding the atom name, the atom is considered quoted. In contrast with an unquoted atom, this one can be made of any Unicode character (not only letters), such as `:'🌢 Elixir'`, `:"++olá++"`, and `:"123"`.
@@ -84,13 +84,13 @@ Structs built on the map syntax by passing the struct name between `%` and `{`.
### Variables
Variables in Elixir must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode letters, numbers, and underscores. Variables may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
Variables in Elixir must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode letters, numbers, and underscores. Variables may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
[Elixir's naming conventions](naming-conventions.md) recommend variables to be in `snake_case` format.
### Non-qualified calls (local calls)
Non-qualified calls, such as `add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscore. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
Non-qualified calls, such as `add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscore. Calls may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
Parentheses for non-qualified calls are optional, except for zero-arity calls, which would then be ambiguous with variables. If parentheses are used, they must immediately follow the function name *without spaces*. For example, `add (1, 2)` is a syntax error, since `(1, 2)` is treated as an invalid block which is attempted to be given as a single argument to `add`.
@@ -102,7 +102,7 @@ As many programming languages, Elixir also support operators as non-qualified ca
### Qualified calls (remote calls)
Qualified calls, such as `Math.add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscores. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
Qualified calls, such as `Math.add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscores. Calls may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
[Elixir's naming conventions](naming-conventions.md) recommend calls to be in `snake_case` format.
@@ -425,7 +425,15 @@ end
The above is treated the same as `sum(1, 2, 3)` by the parser.
The same applies to qualified calls such as `Foo.bar(1, 2, 3)`, which is the same as `Foo.bar 1, 2, 3`. However, remember parentheses are not optional for non-qualified calls with no arguments, such as `sum()`. Removing the parentheses for `sum` causes it to be represented as the variable `sum`, which means they would be no longer equivalent.
The same applies to qualified calls such as `Foo.bar(1, 2, 3)`, which is equivalent to `Foo.bar 1, 2, 3`. There are, however, some situations where parentheses are required:
* when calling anonymous functions, such as `f.(1, 2)`;
* for non-qualified calls with no arguments, such as `sum()`. Removing the parentheses for `sum` causes it to be represented as the variable `sum`;
* for dynamic qualified calls with no arguments. `data.key` means accessing a field named `key` in the map given by `data`. `mod.fun()`, with parens, means calling a function named `fun` in the module `mod`;
In practice, developers prefer to add parentheses to most of their calls. They are skipped mainly in Elixir's control-flow constructs, such as `defmodule`, `if`, `case`, etc, and in certain DSLs.
### Keywords
+9 -9
View File
@@ -2,8 +2,8 @@
Elixir comes with a notation for declaring types and specifications. Elixir is a dynamically typed language, and as such, type specifications are never used by the compiler to optimize or modify code. Still, using type specifications is useful because:
* they provide documentation (for example, tools such as [ExDoc](https://github.com/elixir-lang/ex_doc) show type specifications in the documentation)
* they're used by tools such as [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), that can analyze code with typespec to find type inconsistencies and possible bugs
* they provide documentation (for example, tools such as [`ExDoc`](https://hexdocs.pm/ex_doc/) show type specifications in the documentation)
* they're used by tools such as [Dialyzer](`:dialyzer`), that can analyze code with typespec to find type inconsistencies and possible bugs
Type specifications (sometimes referred to as *typespecs*) are defined in different contexts using the following attributes:
@@ -36,7 +36,7 @@ In the example above, this happens:
## Types and their syntax
The syntax Elixir provides for type specifications is similar to [the one in Erlang](http://www.erlang.org/doc/reference_manual/typespec.html). Most of the built-in types provided in Erlang (for example, `pid()`) are expressed in the same way: `pid()` (or simply `pid`). Parameterized types (such as `list(integer)`) are supported as well and so are remote types (such as `Enum.t`). Integers and atom literals are allowed as types (for example, `1`, `:atom`, or `false`). All other types are built out of unions of predefined types. Some shorthands are allowed, such as `[...]`, `<<>>`, and `{...}`.
The syntax Elixir provides for type specifications is similar to [the one in Erlang](https://erlang.org/doc/reference_manual/typespec.html). Most of the built-in types provided in Erlang (for example, `pid()`) are expressed in the same way: `pid()` (or simply `pid`). Parameterized types (such as `list(integer)`) are supported as well and so are remote types (such as `Enum.t`). Integers and atom literals are allowed as types (for example, `1`, `:atom`, or `false`). All other types are built out of unions of predefined types. Some shorthands are allowed, such as `[...]`, `<<>>`, and `{...}`.
The notation to represent the union of types is the pipe `|`. For example, the typespec `type :: atom() | pid() | tuple()` creates a type `type` that can be either an `atom`, a `pid`, or a `tuple`. This is usually called a [sum type](https://en.wikipedia.org/wiki/Tagged_union) in other languages
@@ -59,12 +59,12 @@ The notation to represent the union of types is the pipe `|`. For example, the t
| non_neg_integer() # 0, 1, 2, 3, ...
| pos_integer() # 1, 2, 3, ...
## Lists
| list(type) # proper list ([]-terminated)
| nonempty_list(type) # non-empty proper list
| maybe_improper_list(type1, type2) # proper or improper list
| nonempty_improper_list(type1, type2) # improper list
| nonempty_maybe_improper_list(type1, type2) # non-empty proper or improper list
## Lists
| list(type) # proper list ([]-terminated)
| nonempty_list(type) # non-empty proper list
| maybe_improper_list(content_type, termination_type) # proper or improper list
| nonempty_improper_list(content_type, termination_type) # improper list
| nonempty_maybe_improper_list(content_type, termination_type) # non-empty proper or improper list
| Literals # Described in section "Literals"
| BuiltIn # Described in section "Built-in types"
+9 -7
View File
@@ -18,13 +18,13 @@ where `<Start>` uses the same categories as the spec but restricts them to the N
> characters derived from the Unicode General Category of uppercase letters, lowercase letters, titlecase letters, modifier letters, other letters, letter numbers, plus `Other_ID_Start`, minus `Pattern_Syntax` and `Pattern_White_Space` code points
>
> In set notation: `[\p{L}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`
> In set notation: `[\p{L}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
and `<Continue>` uses the same categories as the spec but restricts them to the NFC form (see R6):
> ID_Start characters, plus characters having the Unicode General Category of nonspacing marks, spacing combining marks, decimal number, connector punctuation, plus `Other_ID_Continue`, minus `Pattern_Syntax` and `Pattern_White_Space` code points.
>
> In set notation: `[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`
> In set notation: `[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
`<Ending>` is an addition specific to Elixir that includes only the code points `?` (003F) and `!` (0021).
@@ -36,17 +36,19 @@ Elixir does not allow the use of ZWJ or ZWNJ in identifiers and therefore does n
Unicode atoms in Elixir follow the identifier rule above with the following modifications:
* `<Start>` includes the code point `_` (005F)
* `<Continue>` includes the code point `@` (0040)
* `<Start>` additionally includes the code point `_` (005F)
* `<Continue>` additionally includes the code point `@` (0040)
> Note that all Elixir operators are also valid atoms. Therefore `:+`, `:@`, `:|>`, and others are all valid atoms. The full description of valid atoms is available in the Syntax Reference, this document covers only the rules for identifier-based atoms.
Note atoms can also be quoted, which allows any characters, such as `:"hello elixir"`. All Elixir operators are also valid atoms (`:+`, `:@`, `:|>`, etc.). The full description of valid atoms is available in the ["Atoms" section in the syntax reference](syntax-reference.html#atoms).
### Variables
Variables in Elixir follow the identifier rule above with the following modifications:
* `<Start>` includes the code point `_` (005F)
* `<Start>` must not include Lu (letter uppercase) and Lt (letter titlecase) characters
* `<Start>` additionally includes the code point `_` (005F)
* `<Start>` additionally excludes Lu (letter uppercase) and Lt (letter titlecase) characters
In set notation: `[\u{005F}\p{Ll}\p{Lm}\p{Lo}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
## R3. Pattern_White_Space and Pattern_Syntax Characters

Some files were not shown because too many files have changed in this diff Show More