Compare commits

..
133 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
345 changed files with 10961 additions and 21000 deletions
+1 -2
View File
@@ -1,9 +1,9 @@
### Precheck
* Do not use the issue tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
* For bugs, do a quick search and make sure the bug has not yet been reported
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
* Do not use the issues tracker for guidance, questions or support (try Elixir Forum, Stack Overflow, Slack, etc. instead)
* Finally, be nice and have fun!
### Environment
@@ -14,7 +14,6 @@
### Current behavior
Include code samples, errors and stacktraces if appropriate.
If reporting a bug, please include the reproducing steps.
### Expected behavior
+7 -8
View File
@@ -10,18 +10,19 @@ env:
jobs:
test_linux:
name: Linux, ${{ matrix.otp_release }}, Ubuntu 18.04
name: Linux, ${{ matrix.otp_release }}, Ubuntu 16.04
continue-on-error: ${{ matrix.development }}
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-24.3', 'OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
development: [false]
include:
- otp_release: master
development: true
- otp_release: maint
development: true
runs-on: ubuntu-18.04
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v2
with:
@@ -29,7 +30,7 @@ jobs:
- name: Install Erlang/OTP
run: |
cd $RUNNER_TEMP
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-18.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
@@ -46,10 +47,8 @@ jobs:
run: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
- name: Erlang test suite
run: make test_erlang
continue-on-error: ${{ matrix.development }}
- name: Elixir test suite
run: make test_elixir
continue-on-error: ${{ matrix.development }}
- name: Check reproducible builds
run: taskset 1 make check_reproducible
if: matrix.otp_release == 'OTP-24.0'
@@ -58,7 +57,7 @@ jobs:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
strategy:
matrix:
otp_release: ['22.3']
otp_release: ['22.0']
runs-on: windows-2019
steps:
- name: Configure Git
@@ -90,7 +89,7 @@ jobs:
check_posix_compliant:
name: Check POSIX-compliant
runs-on: ubuntu-18.04
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v2
with:
+225 -324
View File
@@ -1,408 +1,309 @@
# Changelog for Elixir v1.13
# Changelog for Elixir v1.12
The focus behind Elixir v1.13 has been on tooling, mainly tooling related to code formatting, code fragments, code reflection, and code recompilation. A lot of this functionality will directly impact developers working on large codebases and provide meaningful quality of life improvements for those working on Elixir tooling and environments, such as IDEs, notebooks, etc.
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.
## Semantic recompilation
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+.
Elixir v1.13 comes with many improvements to the compiler, so it recompiles your files less frequently. In particular:
## Scripting improvements: `Mix.install/2` and `System.trap_signal/3`
* The digest of the files are considered in addition to their size. This avoids recompiling many files when switching or rebasing branches.
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.
* Changing your `mix.exs` will no longer trigger a full recompilation, unless you specifically change the configurations used by the Elixir compiler (`:elixirc_paths` and `:elixirc_options`).
* Changing compile-time configuration files (`config/config.exs` and any other file imported from it) now only recompiles the project files that depend on the reconfigured applications, instead of a full recompilation. However, if you change the configuration of your application itself, the whole project is still recompiled.
* Adding, updating or removing a dependency now only recompiles the project files that depend on the modified a dependency.
* If your project has both Erlang and Elixir files, changing an Erlang file will now recompile only the Elixir files that depend on it.
In a nutshell, Elixir went from triggering full recompilations whenever any of `mix.exs`, `config/config.exs`, `src/*`, and `mix.lock` changed on disk to semantic recompilations. Now it only fully recompiles when:
* you change the compilation options in `mix.exs`
* you change the configuration for the current project in `config/config.exs`
## mix xref
`mix xref` is a tool that analyzes relationships between files. By analyzing the compile-time and runtime dependencies between files, it allows developers to understand what files have to be recompiled whenever a file changes.
Elixir v1.13 comes with many improvements to `mix xref`, such as:
* `mix xref graph` now supports `--label` to be set to "compile-connected", which returns all compile-time dependencies that lead to additional transitive dependencies.
* A new `mix xref trace FILE` subcommand receives a file and returns all dependencies in said file, including the line and what caused said dependency (a function/macro call, an alias, a struct, etc).
* All `mix xref` subcommands support the `--fail-above` flag, which allows you to enforce your project has at most a certain number of compile-time cycles, transitive compile-time dependencies, etc.
* `mix xref graph` now supports multiple `--sink` and `--source` to be given.
With these improvements, it has become simpler to understand the impact code recompilation has in our codebases and how to limit it.
## Code fragments
The `Code` module got a companion module called `Code.Fragment`, which hosts functions that work on incomplete code, as is often the scenario in editors, interactive shells, etc. The module contains different heuristics to analyze the source code and return context informational.
Thanks to these improvements, `IEx`' autocomplete got several quality of life improvements, such as the autocompletion of sigils, structs, and paths. For example, typing `~<TAB>` now shows:
```iex
iex(1)> ~
~C (sigil_C) ~D (sigil_D) ~N (sigil_N) ~R (sigil_R)
~S (sigil_S) ~T (sigil_T) ~U (sigil_U) ~W (sigil_W)
~c (sigil_c) ~r (sigil_r) ~s (sigil_s) ~w (sigil_w)
```
Adding the sigil letter and pressing tab then shows the available delimiters:
```iex
iex(1)> ~r
" """ ' ''' ( / < [ { |
```
Similarly, `%<TAB>` now shows only the available structs (exceptions excluded), instead of all modules:
`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
iex(1)> %File.St
File.Stat File.Stream
Mix.install([:jason])
IO.puts Jason.encode!(%{hello: :world})
```
Once you define the struct, you can hit `tab` to show all struct fields available:
`Mix.install/2` also performs protocol consolidation, which gives script developers an option to execute their code in the most performant format possible.
```elixir
iex(1)> %URI{
authority: fragment: host: path: port:
query: scheme: userinfo:
**Note:** `Mix.install/2` is currently experimental and it may change in future releases.
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
```
As you fill a field in, the already filled fields no longer show up:
This is particularly useful when your tests get stuck and you want to know which one is the culprit.
```elixir
iex(1)> %URI{path: "/example",
authority: fragment: host: port: query:
scheme: userinfo:
**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.
## Tighter Erlang/OTP 24 integration
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.
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`:
```
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)
```
Along the same lines, `SyntaxError` and `TokenMissingError` were improved to show a code snippet whenever possible:
However, in Elixir v1.12 with Erlang/OTP 24:
```elixir
$ elixir -e "hello + * world"
** (SyntaxError) nofile:1:9: syntax error before: '*'
|
1 | hello + * world
| ^
```
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, new compilation tracers have been added, alongside a handful of functions in `Module` to retrieve module metadata, which can be used to enrich suggestions in programming environments.
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.
## Extended code formatting
## Stepped ranges
The `mix format` task has been augmented with the notion of plugins. Plugins can teach the formatter how to format new files and how to format sigils, via the `Mix.Tasks.Format` behaviour.
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.
For example, imagine that your project uses Markdown in two distinct ways: via a custom `~M` sigil and via files with the `.md` and `.markdown` extensions. A custom plugin would look like this:
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
defmodule MixMarkdownFormatter do
@behaviour Mix.Tasks.Format
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.
def features(_opts) do
[sigils: [:M], extensions: [".md", ".markdown"]]
end
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).
def format(contents, opts) do
# logic that formats markdown
end
end
```
## Additional functions
Now any application can use your formatter as follows:
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.
```elixir
# .formatter.exs
[
# Define the desired plugins
plugins: [MixMarkdownFormatter],
# Remember to update the inputs list to include the new extensions
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "posts/*.{md,markdown}"]
]
```
Finally, the `Code` module has also been augmented with two functions: `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`. Those functions allow someone to retrieve the Elixir AST with their original source code comments, and then convert this AST to formatted code. In other words, those functions provide a wrapper around the Elixir Code Formatter, supporting developers who wish to create tools that directly manipulate and custom format Elixir source code.
## v1.13.4 (2022-04-07)
This release has been verified to work with Erlang/OTP 25 RC2.
### 1. Enhancements
#### Elixir
* [Code] Allow iodata to be returned in sigil formatting functions
* [Code] Pass opening delimiter information to sigil formatting functions
### 2. Bug fixes
#### Elixir
* [Kernel] Tweak type unification to fix infinite loop with recursive vars
* [Kernel] Add compile-time dependencies on `require`
* [Registry] Make `Registry` send work with named triplets
### 3. Deprecations
#### Mix
* [mix rebar] Deprecate Rebar 2 as it no longer works on Erlang/OTP 25
## v1.13.3 (2022-02-09)
### 1. Enhancements
#### Mix
* [mix format] Supply file and line to formatter plugins
* [mix format] Support embedded Elixir expressions inside formatter plugins
### 2. Bug fixes
#### Elixir
* [Code] Fix duplicate bindings causing errors during evaluation
* [Kernel] Make sure signatures stored in the Documentation chunk does not contain newlines
* [Kernel] Fix infinite loop when compiling guards with recursive map access
* [Macro] Fix error on `Macro.to_string/1` when the plain alias `Elixir` is given
* [String] Fix error for certain codepoint combinations in `String.split_at/2`
#### Mix
* [mix compile] Recompile project files when exports from dependencies change
* [mix test] Fix total coverage always showing in red even when above the threshold
## v1.13.2 (2022-01-13)
### 1. Enhancements
#### Mix
* [mix format] Allow plugins to also format `.ex` and `.exs` files
* [mix release] Allow bypassing application mode validation in release spec
* [mix test] Print a message when the suite fails due to the coverage threshold
### 2. Bug fixes
#### Elixir
* [Code] Do not emit warnings on `Code.Fragment.container_cursor_to_quoted/2`
* [Kernel] Fix a crash when a for-comprehension with `:uniq` was used inside another comprehension with `:uniq`
* [Kernel] Ensure `env.context_modules` is properly set inside optimized `defmodule`
* [Keyword] Deprecate the recently added `Keyword.map/2` as it is equivalent to `Keyword.new/2`
* [Map] Deprecate the recently added `Map.map/2` as it is equivalent to `Map.new/2`
* [Protocol] Warn on zero arity callbacks inside protocols
## v1.13.1 (2021-12-14)
## v1.12.3 (2021-09-05)
### 1. Bug fixes
#### Elixir
* [Code] Do not show code snippets in `SyntaxError` and `TokenMissingError` if line is empty
* [Exception] Do not fail blaming `ArgumentError` for improper lists on `apply/3`
* [Macro] Set a max `line_length` for `Macro.to_string/1`
* [Macro] Fix formatting of lists on module attributes for `Macro.to_string/1`
* [String] Fix incorrect codepoint byte counting in `slice` with negative positions in ranges
* [Task] Ensure async streams can be consumed from another process than the one that creates them
* [URI] Undeprecate `URI.parse/1` as `URI.new/1` is too strict in many common cases
* [URI] Make sure `URI.new/1` returns nil for empty paths
* [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] Make sure the `--version` flag halts IEx
* [IEx.Autocomplete] Do not error autocompletion with module attribute
#### Mix
* [Mix] Make protocol consolidation part of the `Mix.install/2` cache
* [Mix] Rename inconsistent `:exit_code` option to `:exit_status` on `Mix.raise/2`
## v1.13.0 (2021-12-03)
## 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] Add `:parser_options` to EEx functions
* [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
* [Calendar] Add `c:Calendar.year_of_era/3` to support calendars where the beginning of a new era does not align with the beginning of a new year
* [CLI] Support `--short-version` on the CLI that does not boot the VM
* [Code] Add `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`
* [Code] Add more `:token_metadata` to aliases and remote calls when parsing strings
* [Code] Add `Code.Fragment` module to provide best-effort information from code fragments. The module currently provides an updated `Code.Fragment.cursor_context/2` with operator support and `Code.Fragment.surround_context/2` which looks at a given position in a fragment and find its surrounding delimiters
* [Code] Allow custom sigil formatting on `Code.format_string!/2`
* [Code] Add `{:on_module, bytecode, :none}` trace to compilation tracers
* [Enum] Optimize `Enum.concat/1` for lists of lists
* [Enum] Add `Enum.slide/3`
* [Exception] Better format Elixir exceptions in Erlang
* [Inspect] Allow default inspect fun to be set globally with `Inspect.Opts.default_inspect_fun/1`
* [IO] Allow `:eof` to be given as limit to `IO.getn/2`
* [Kernel] Support the `:sigils` option in `import Mod, only: :sigils` and allow the sigil modifiers to be also digits
* [Kernel] Make `get_in` consistently abort and return `nil` when `nil` values are found (previously Elixir would raise an error in this case). This allows a user to use `get_in` as a safe navigation operator.
* [Kernel] Improve compilation times by reducing the amount of copies of the AST across compiler processes
* [Kernel] Raise if trying to define a module with a slash in its name
* [Kernel] Warn when `?\` is used and there is no need for a escape character
* [Kernel] Track structs in typespecs as export deps instead of compile-time deps
* [Kernel] Add power operator (`**/2`)
* [Keyword] Add `Keyword.validate/2`
* [Keyword] Implement `Keyword.filter/2` and `Keyword.map/2`
* [List] Add `List.keyfind!/3`
* [Macro] Add `Macro.prewalker/1` and `Macro.postwalker/1`
* [Macro.Env] Add the following reflection functions: `required?/2`, `lookup_import/2`, `fetch_alias/2`, and `fetch_macro_alias/2`
* [Map] Implement `Map.filter/2` and `Map.map/2`
* [Module] Support `:nillify_clauses` in `Module.get_definition/3`
* [Module] Add `Module.attributes_in/1` and `Module.overridables_in/1`
* [OptionParser] Add "did you mean?" suggestions to `OptionParser.ParseError` messages
* [Record] Add record reflection via `@__records__`
* [Task] Add `Task.completed/1`
* [Task] Add `Task.ignore/1` to keep a task running but ignoring all of its results
* [Task] Reduce the amount of copying `Task.async*` functions
* [URI] Add `URI.new/1` and `URI.new!/1`
* [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] Show hint if comparing different but equivalent strings
* [ExUnit.CaptureIO] Add `with_io/3` to return result with captured io
* [ExUnit.CaptureLog] Add `with_log/2` to return result with captured logs
* [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.Autocomplete] Add path autocompletion whenever when the cursor follows `"./` or `"/` or `"DRIVER:` where `DRIVER` is a single letter
* [IEx.Autocomplete] Add autocompletion for sigils, struct names, and struct fields
* [IEx.Helpers] Allow multiple modules to be given to `r/1`
#### Logger
* [Logger] Add `Logger.put_application_level/2`
* [Logger] Print all log levels in accordance to Erlang/OTP. This also means `[warn]` is now shown as `[warning]`
* [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_INSTALL_FORCE` environment variable support
* [Mix] Support `:config` and `:system_env` in `Mix.install/2`
* [Mix] Add `Mix.installed?/0`
* [Mix.Shell] Add `:default` option to `Mix.Shell.yes?`
* [mix archive.install] Run `loadconfig` before building archive
* [mix compile] Move Elixir version check to before deps are compiled, in order to give feedback earlier
* [mix compile.elixir] Do not recompile files if their modification time change but their contents are still the same and the .beam files are still on disk
* [mix compile.elixir] Do not recompile all Elixir sources when Erlang modules change, only dependent ones
* [mix compile.elixir] Do not recompile Elixir files if `mix.exs` changes, instead recompile only files using `Mix.Project` or trigger a recompilation if a compiler option changes
* [mix compile.elixir] Only recompile needed files when a dependency is added, updated or removed
* [mix compile.elixir] Only recompile needed files when a dependency is configured
* [mix deps] Add `:subdir` option to git deps
* [mix escript.install] Run `loadconfig` before building escript
* [mix format] Support `:plugins` in `mix format` that can hook into custom extensions and sigils
* [mix format] Add `Mix.Tasks.Format.formatter_for_file/2`
* [mix local.rebar] No longer support `sub_dirs` in Rebar 2 to help migration towards Rebar 3
* [mix local.rebar] Support `--if-missing` option when installing Rebar
* [mix local.rebar] Set `REBAR_PROFILE=prod` when compiling Rebar dependencies
* [mix test] Support `--profile-require=time` to profile the time loading test files themselves
* [mix test] Allow filtering modules from coverage using regex
* [mix test] Allow the exit status of ExUnit to be configured and set the default to 2
* [mix test] Exit with a status of 3 when coverage falls below threshold
* [mix test] Write failed manifest when suite fails due to --warnings-as-errors
* [mix test] Ignore `MIX_TEST_PARTITION` when partitions set to 1
* [mix xref] Support multiple sinks and sources in `mix xref graph`
* [mix xref] Add `trace` subcommand to print compilation dependencies between files
* [mix xref] Add `--fail-above` option to `mix xref`
* [mix xref] Add `--label compile-connected` to `mix xref`
* [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] Accept comments in EEx between do and the first clause
* [EEx] Accept EEx expressions where `->` is followed by newline
#### Elixir
* [Application] Allow any expression as first argument of `compile_env`
* [Application] Warn if `Application.compile_env` or `Application.compile_env!` are called without a require
* [Code] Make sure `:static_atoms_encoder` in `Code.string_to_quoted/2` also applies to quoted keyword keys
* [Code] Ensure bindings with no context are returned as atoms instead of `{binding, nil}` in eval operations
* [Inspect] Fix a bug when inspecting a non-binary bitstring with colors
* [Kernel] Reject bidirectional formatting characters in strings and comments
* [Kernel] Support escaping of terminators in uppercase sigils heredocs for consistency
* [Kernel] Raise if `__CALLER__` or `__ENV__` or `__STACKTRACE__` are used in match
* [Kernel] Improve error message on invalid argument for `byte_size` from binary concat
* [Kernel] Raise when aliasing non-Elixir modules without `:as`
* [Kernel] Allow `unquote_splicing` inside `%{...}` without parens
* [Kernel] Ensure that waiting on a struct expansion inside a typespec is correctly tracked as waiting time in the compiler
* [Kernel] Correctly parse the atom `.` as a keyword list key
* [Kernel] Do not leak variables from the first generator in `with` and `for` special forms
* [Kernel] Fix column number on strings with NFD characters
* [Kernel] Fix a bug where a combination of dynamic line in `quote` with `unquote` of remote calls would emit invalid AST metadata
* [OptionParser] Validate switch types/modifiers early on to give more precise feedback
* [Protocol] Add `defdelegate` to the list of unallowed macros inside protocols as protocols do not allow function definitions
* [Protocol] Warn if `@callback`, `@macrocallback` and `@optional_callbacks` are defined inside protocol
* [Protocol] Ensure protocol metadata is deterministic on consolidation
* [Range] Always show step when range is descending
* [String] Update Unicode database to version 14.0
* [URI] Only percent decode if followed by hex digits (according to https://url.spec.whatwg.org/#percent-decode)
* [Version] Ensure proper precedence of `and`/`or` in version requirements
* [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] Fix formatter and counters from `ExUnit.run/0` to consider all tests in a module whenever if a module's `setup_all` fails
* [ExUnit] Allow doctests newlines to be terminated by CRLF
* [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 the loss of `.iex.exs` context after a pry session
* [IEx] Stop evaluator before exiting IEx server to avoid evaluators leaking
#### Logger
* [Logger] Raise clear error message for invalid `:compile_time_purge_matching` configuration
* [Logger] Fix a bug where Logger would not reset its discard counter under some scenarios
* [IEx] Fix auto-completion inside remote shells
#### Mix
* [mix compile.elixir] Track transitive runtime dependencies coming from local/path dependencies
* [mix compile.elixir] Recompile file if `@external_resource` is deleted
* [mix compile.elixir] Print number of compiling files on all compiler cycles. This will make the `Compiling N files (.ex)` show up multiple times if necessary
* [mix deps] Raise if local dep is unavailable while compiling
* [mix deps.unlock] Fix blank output when unlocking a dependency that is not locked
* [mix local.install] Do not respect `MIX_DEPS_PATH` for install commands
* [mix release] Improve release scripts by making sure shell errors cascade (this is done by avoiding exporting and defining variables in a single step)
* [mix release] Do not boot release if `RELEASE_COOKIE` is empty
* [mix release] Allow releases running as a daemon to be restarted
* [mix release] Raise proper error message when non-serializable values are in configs
* [mix test] Fix coverage engine to also tag `case`, `cond`, and `receive` branches where the right side is a literal
* [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
* [Code] Environment options in `Code.eval_quoted/3` and `Code.eval_string/3`, such as `:aliases` and `:tracers`, have been deprecated in favor of passing an environment
* [IO] `:all` on `IO.getn` is deprecated in favor of `:eof`
* [URI] `URI.parse/1` is deprecated in favor of `URI.new/1` and `URI.new!/1`
#### Mix
* [mix format] `Mix.Tasks.Format.formatter_opts_for_file/2` is deprecated in favor of `Mix.Tasks.Format.formatter_for_file/2`
* [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
* [Code] `Code.cursor_context/2` is deprecated, use `Code.Fragment.cursor_context/2` instead
* [Macro] `Macro.to_string/2` is deprecated, use `Macro.to_string/1` instead
* [System] `System.get_pid/0` is deprecated, use `System.pid/0` instead
* [Version] Using `!` or `!=` in version requirements is deprecated, use `~>` or `>=` instead
* [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 escript.build] `:strip_beam` option is deprecated in favor of `:strip_beams`
* [Mix] `:exit_code` in `Mix.raise/2` has been deprecated in favor of `:exit_status`
* [Mix.Config] `Mix.Config` is deprecated in favor of `Config` module
* [mix compile] The `:xref` compiler is deprecated and it has no effect. Please remove it from your mix.exs file.
## v1.12
### 5. Backwards incompatible changes
The CHANGELOG for v1.12 releases can be found [in the v1.12 branch](https://github.com/elixir-lang/elixir/blob/v1.12/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).
+10 -16
View File
@@ -13,23 +13,17 @@ The goal of the Code of Conduct is to specify a baseline standard of behavior so
These are the values Elixir developers should aspire to:
* Be friendly and welcoming
* Be kind
* Be patient
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
* Interpret the arguments of others in good faith, do not seek to disagree.
* When we do disagree, try to understand why.
* Be thoughtful
* Productive communication requires effort. Think about how your words will be interpreted.
* Remember that sometimes it is best to refrain entirely from commenting.
* Be respectful
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
* Be constructive
* Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation.
* Avoid unconstructive criticism: don't merely decry the current state of affairs; offer — or at least solicit — suggestions as to how things may be improved.
* Avoid harsh words and stern tone: we are all aligned towards the well-being of the community and the progress of the ecosystem. Harsh words exclude, demotivate, and lead to unnecessary conflict.
* Avoid snarking (pithy, unproductive, sniping comments).
* Avoid microaggressions (brief and commonplace verbal, behavioral and environmental indignities that communicate hostile, derogatory or negative slights and insults towards a project, person or group).
* Be responsible
* What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
* Avoid destructive behavior
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
* Snarking (pithy, unproductive, sniping comments).
The following actions are explicitly forbidden:
@@ -47,11 +41,11 @@ Explicit enforcement of the Code of Conduct applies to the official mediums oper
* The [official GitHub projects][1] and code reviews.
* The official elixir-lang mailing lists.
* The **[#elixir][2]** IRC channel on [Libera.Chat][3].
* The **[#elixir-lang][2]** IRC channel on [Freenode][3].
Other Elixir activities (such as conferences, meetups, and unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
Project maintainers may block, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
@@ -59,8 +53,8 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be repor
## Acknowledgements
This document was based on the Code of Conduct from the Go project (dated Sep/2021) and the Contributor Covenant (v1.4).
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
[1]: https://github.com/elixir-lang/
[2]: https://web.libera.chat/#elixir
[3]: https://libera.chat/
[2]: https://webchat.freenode.net/?channels=#elixir-lang
[3]: https://www.freenode.net
+3 -3
View File
@@ -2,7 +2,7 @@ PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := 1.13/
CANONICAL := 1.12/
CANONICAL ?= master/
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
@@ -105,6 +105,7 @@ unicode: $(UNICODE)
$(UNICODE): lib/elixir/unicode/*
@ echo "==> unicode (compile)";
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/properties.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
@@ -171,7 +172,6 @@ clean_residual_files:
rm -rf lib/mix/test/fixtures/git_rebar/
rm -rf lib/mix/test/fixtures/git_repo/
rm -rf lib/mix/test/fixtures/git_sparse_repo/
rm -rf lib/mix/test/fixtures/archive/ebin/
rm -f erl_crash.dump
$(Q) $(MAKE) clean_man
@@ -286,7 +286,7 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
$(Q) mkdir -p $(TEST_EBIN)
$(Q) $(ERLC) -o $(TEST_EBIN) $<
test_elixir: test_stdlib test_ex_unit test_logger test_eex test_iex test_mix
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
test_stdlib: compile
@ echo "==> elixir (ex_unit)"
-1
View File
@@ -22,7 +22,6 @@ limitations under the License.
== All other files
Copyright 2012 Plataformatec
Copyright 2021 The Elixir Team
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
+37 -59
View File
@@ -11,14 +11,11 @@ For more about Elixir, installation and documentation,
## Policies
New releases are announced in the [announcement mailing list][8].
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com
and replying to the confirmation email.
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
All security releases [will be tagged with `[security]`][10]. For more
information, please read our [Security Policy][9].
All security releases [will be tagged with `[security]`][10]. For more information, please read our [Security Policy][9].
All interactions in our official communication channels follow our
[Code of Conduct][1].
All interactions in our official communication channels follow our [Code of Conduct][1].
## Bug reports
@@ -26,56 +23,13 @@ For reporting bugs, [visit our issue tracker][2] and follow the steps
for reporting a new issue. **Please disclose security vulnerabilities
privately at elixir-security@googlegroups.com**.
## Issues tracker management
All currently open bugs related to the Elixir repository are listed
in the issues tracker. The Elixir team uses the issues tracker to focus
on *actionable items*, including planned enhancements in the short- and
medium-term. We also do our best to label entries for clarity and to ease
collaboration.
Our *actionable item policy* has some important consequences, such as:
* Proposing new features as well as request for support, help, and
guidance must be done in their own spaces, detailed next.
* Issues where we have identified to be outside of Elixir scope,
such as a bug upstream, will be closed (and requested to be moved
elsewhere if appropriate).
* We actively close unrelated and non-actionable issues to keep the
issues tracker tidy. However, we may get things wrong from time to
time, so we are glad to revisit issues and reopen if necessary.
Keep the tone positive and be kind! For more information, see the
[Code of Conduct][1].
### Proposing new features
For proposing new features, please start a discussion in the
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
to argue and explain why a feature is useful and how it will impact the
codebase and the community.
Once a proposal is accepted, it will be added to [the issue tracker][2].
Features and bug fixes that have already been merged and will be included
in the next release are then "closed" and added to the [changelog][7].
### Discussions, support, and help
For general discussions, support, and help, please use many of the community
spaces [listed on the sidebar of the Elixir website](https://elixir-lang.org/),
such as forums, chat platforms, etc, where the wider community will be available
to help you.
## Compiling from source
For the many different ways to install Elixir,
[see our installation instructions on the website](https://elixir-lang.org/install.html).
However, if you want to contribute to Elixir, you will need to compile from source.
To compile from source, you can follow the steps below.
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang).
After that, 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
@@ -87,13 +41,37 @@ make clean test
[this article includes important notes for compiling Elixir from source
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
In case you want to use this Elixir version as your system version,
you need to add the `bin` directory to [your PATH environment variable](https://elixir-lang.org/install.html#setting-path-environment-variable).
If Elixir fails to build (specifically when pulling in a new version via
`git`), be sure to remove any previous build artifacts by running
`make clean`, then `make test`.
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 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 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.
## Proposing new features
For proposing new features, please start a discussion in the
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
to argue and explain why a feature is useful and how it will impact the
codebase and the community.
Once a proposal is accepted, it will be added to [the issue tracker][2].
The issue tracker focuses on *actionable items* and it holds a list of
upcoming enhancements and pending bugs. All entries in the tracker are
tagged for clarity and to ease collaboration.
Features and bug fixes that have already been merged and will be included
in the next release are marked as "closed" in the issue tracker and are
added to the [changelog][7].
## Contributing
We welcome everyone to contribute to Elixir. To do so, there are a few
@@ -145,7 +123,7 @@ make clean_elixir compile
Similarly, if you can't get Elixir to compile or the tests to pass after
updating an existing checkout, run `make clean compile`. You can check
[the official build status](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml).
[the official build status on Cirrus CI](https://cirrus-ci.com/github/elixir-lang/elixir).
More tasks can be found by reading the [Makefile](Makefile).
With tests running and passing, you are ready to contribute to Elixir and
@@ -212,13 +190,13 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
* [Issue tracker][2]
* [Changelog][7]
* [Security Policy][9]
* **[#elixir][4]** on [Libera.Chat][5] IRC
* **[#elixir-lang][4]** on [Freenode][5] IRC
[1]: CODE_OF_CONDUCT.md
[2]: https://github.com/elixir-lang/elixir/issues
[3]: https://groups.google.com/group/elixir-lang-core
[4]: https://web.libera.chat/#elixir
[5]: https://libera.chat
[4]: https://webchat.freenode.net/?channels=#elixir-lang
[5]: https://www.freenode.net
[6]: https://elixir-lang.org/docs.html
[7]: CHANGELOG.md
[8]: https://groups.google.com/group/elixir-lang-ann
@@ -227,7 +205,7 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
## License
"Elixir" and the Elixir logo are registered trademarks of The Elixir Team.
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
Elixir source code is released under Apache License 2.0.
+3 -3
View File
@@ -4,7 +4,7 @@
1. Ensure you are running on the oldest supported Erlang version
2. Update version in /VERSION, bin/elixir and bin/elixir.bat
2. Update version in /VERSION
3. Ensure /CHANGELOG.md is updated, versioned and add the current date
@@ -32,11 +32,11 @@
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
3. Commit "Branch out vMAJOR.MINOR"
3. Commit "Prepare vMAJOR.MINOR for release"
### Back in master
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
1. Bump /VERSION file
2. Start new /CHANGELOG.md
+8 -7
View File
@@ -4,13 +4,14 @@
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
Elixir version | Support
:------------- | :-----------------------------
1.13 | Bug fixes and security patches
1.12 | Security patches only
1.11 | Security patches only
1.10 | Security patches only
1.9 | Security patches only
| Elixir version | Support
| -------------- | ------------------------------
| 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
## Announcements
+1 -1
View File
@@ -1 +1 @@
1.13.4
1.12.3
+4 -17
View File
@@ -1,22 +1,20 @@
#!/bin/sh
set -e
ELIXIR_VERSION=1.13.4
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
cat <<USAGE >&2
Usage: $(basename "$0") [options] [.exs file] [data]
## General options
-e "COMMAND" Evaluates the given command (*)
-h, --help Prints this message (standalone)
-h, --help Prints this message and exits
-r "FILE" Requires the given files/patterns (*)
-S SCRIPT Finds and executes the given script in \$PATH
-S SCRIPT   Finds and executes the given script in \$PATH
-pr "FILE" Requires the given files/patterns in parallel (*)
-pa "PATH" Prepends the given path to Erlang code path (*)
-pz "PATH" Appends the given path to Erlang code path (*)
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
-v, --version Prints Elixir version and exits
--app APP Starts the given app and its dependencies (*)
--erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -24,7 +22,6 @@ Usage: $(basename "$0") [options] [.exs file] [data]
--logger-otp-reports BOOL Enables or disables OTP reporting
--logger-sasl-reports BOOL Enables or disables SASL reporting
--no-halt Does not halt the Erlang VM after execution
--short-version Prints Elixir version (standalone)
--werl Uses Erlang's Windows shell GUI (Windows only)
Options given after the .exs file or -- are passed down to the executed code.
@@ -55,7 +52,6 @@ It will attempt to create PIPEDIR and LOGDIR if they don't exist.
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
** Options marked with (*) can be given more than once.
** Standalone options can't be combined with other options.
USAGE
exit 1
fi
@@ -70,11 +66,6 @@ readlink_f () {
fi
}
if [ $# -eq 1 ] && [ "$1" = "--short-version" ]; then
echo "$ELIXIR_VERSION"
exit 0
fi
# Stores static Erlang arguments and --erl (which is passed as is)
ERL=""
@@ -219,11 +210,7 @@ if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
fi
# One MAY change ERTS_BIN= but you MUST NOT change
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
ERTS_BIN=
ERTS_BIN="$ERTS_BIN"
set -- "$ERTS_BIN$ERL_EXEC" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
if [ -n "$RUN_ERL_PIPE" ]; then
+8 -20
View File
@@ -1,14 +1,10 @@
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
set ELIXIR_VERSION=1.13.4
setlocal enabledelayedexpansion
if ""%1""=="""" if ""%2""=="""" goto documentation
if /I ""%1""==""--help"" if ""%2""=="""" goto documentation
if /I ""%1""==""-h"" if ""%2""=="""" goto documentation
if /I ""%1""==""/h"" if ""%2""=="""" goto documentation
if ""%1""==""/?"" if ""%2""=="""" goto documentation
if /I ""%1""==""--short-version"" if ""%2""=="""" goto shortversion
if ""%1""=="""" goto documentation
if /I ""%1""==""--help"" goto documentation
if /I ""%1""==""-h"" goto documentation
if /I ""%1""==""/h"" goto documentation
if ""%1""==""/?"" goto documentation
goto parseopts
:documentation
@@ -17,13 +13,13 @@ echo.
echo ## General options
echo.
echo -e "COMMAND" Evaluates the given command (*)
echo -h, --help Prints this message (standalone)
echo -h, --help Prints this message and exits
echo -r "FILE" Requires the given files/patterns (*)
echo -S SCRIPT Finds and executes the given script in $PATH
echo -pr "FILE" Requires the given files/patterns in parallel (*)
echo -pa "PATH" Prepends the given path to Erlang code path (*)
echo -pz "PATH" Appends the given path to Erlang code path (*)
echo -v, --version Prints Erlang/OTP and Elixir versions (standalone)
echo -v, --version Prints Elixir version and exits
echo.
echo --app APP Starts the given app and its dependencies (*)
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -31,7 +27,6 @@ echo --eval "COMMAND" Evaluates the given command, same as -e (*)
echo --logger-otp-reports BOOL Enables or disables OTP reporting
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
echo --no-halt Does not halt the Erlang VM after execution
echo --short-version Prints Elixir version (standalone)
echo --werl Uses Erlang's Windows shell GUI (Windows only)
echo.
echo Options given after the .exs file or -- are passed down to the executed code.
@@ -59,11 +54,6 @@ echo.
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
echo.
echo ** Options marked with (*) can be given more than once.
echo ** Standalone options can't be combined with other options.
goto end
:shortversion
echo !ELIXIR_VERSION!
goto end
:parseopts
@@ -88,7 +78,6 @@ set SCRIPT_PATH=%~dp0
rem Designates the path to the ERTS system
set ERTS_BIN=
set ERTS_BIN=!ERTS_BIN!
rem Recursive loop called for each parameter that parses the cmd line parameters
:startloop
@@ -136,7 +125,6 @@ if ""==!par:-pr=! (set "parsElixir=!parsElixir! -pr %1" && shift && goto
if ""==!par:-pa=! (set "parsElixir=!parsElixir! -pa %1" && shift && goto startloop)
if ""==!par:-pz=! (set "parsElixir=!parsElixir! -pz %1" && shift && goto startloop)
if ""==!par:-v=! (set "parsElixir=!parsElixir! -v" && goto startloop)
if ""==!par:--version=! (set "parsElixir=!parsElixir! --version" && goto startloop)
if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && goto startloop)
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
@@ -180,4 +168,4 @@ if defined useWerl (
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
:end
endlocal
endlocal
+1 -1
View File
@@ -7,7 +7,7 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
-h, --help Prints this message and exits
-o The directory to output compiled files
-v, --version Prints Elixir version and exits (standalone)
-v, --version Prints Elixir version and exits
--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
+1 -1
View File
@@ -16,7 +16,7 @@ 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 (standalone)
echo -v, --version Prints Elixir version and exits
echo.
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
-4
View File
@@ -48,10 +48,6 @@ defmodule EEx do
* `: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`.
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code that is generated.
See `Code.string_to_quoted/2` for available options. Note that the options
`:file`, `:line` and `:column` are ignored if passed in.
Defaults to `Code.get_compiler_option(:parser_options)` (which defaults to `[]` if not set).
## Engine
+15 -13
View File
@@ -16,7 +16,6 @@ defmodule EEx.Compiler do
column = 1
indentation = opts[:indentation] || 0
trim = opts[:trim] || false
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
tokenizer_options = %{trim: trim, indentation: indentation}
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
@@ -28,7 +27,7 @@ defmodule EEx.Compiler do
quoted: [],
start_line: nil,
start_column: nil,
parser_options: parser_options
parser_options: Code.get_compiler_option(:parser_options)
}
init = state.engine.init(opts)
@@ -72,11 +71,10 @@ defmodule EEx.Compiler do
message =
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
:elixir_errors.erl_warn({start_line, start_column}, state.file, message)
:elixir_errors.erl_warn(start_line, state.file, message)
end
{rest, line, contents} =
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
{contents, rest} =
generate_buffer(
@@ -117,7 +115,7 @@ defmodule EEx.Compiler do
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
"please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn({line, column}, state.file, message)
:elixir_errors.erl_warn(line, state.file, message)
generate_buffer([{:middle_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line
@@ -155,7 +153,7 @@ defmodule EEx.Compiler do
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn({line, column}, state.file, message)
:elixir_errors.erl_warn(line, state.file, message)
generate_buffer([{:end_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line, column: column
@@ -195,20 +193,24 @@ defmodule EEx.Compiler do
# Look middle expressions that immediately follow a start_expr
defp look_ahead_middle([{:text, _, _, text} | rest], start, contents) do
defp look_ahead_middle(
[{:text, _, _, text}, {:middle_expr, line, _, _, chars} | rest] = tokens,
start,
contents
) do
if only_spaces?(text) do
look_ahead_middle(rest, start, contents ++ text)
{contents ++ text ++ chars, line, rest}
else
nil
{contents, start, tokens}
end
end
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
{rest, line, contents ++ chars}
{contents ++ chars, line, rest}
end
defp look_ahead_middle(_tokens, _start, _contents) do
nil
defp look_ahead_middle(tokens, start, contents) do
{contents, start, tokens}
end
defp only_spaces?(chars) do
+3 -13
View File
@@ -71,15 +71,8 @@ defmodule EEx.Tokenizer do
{:ok, expr, new_line, new_column, rest} ->
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, _line, _column, warnings, tokens} ->
Enum.each(Enum.reverse(warnings), fn {location, file, msg} ->
:elixir_errors.erl_warn(location, file, msg)
end)
token_key(tokens, expr)
{:error, _, _, _, _} ->
{:expr, expr}
{:ok, tokens} -> token_key(tokens, expr)
{:error, _, _, _} -> {:expr, expr}
end
{rest, new_line, new_column, buffer} =
@@ -134,7 +127,7 @@ defmodule EEx.Tokenizer do
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens, expr) do
case {tokens, tokens |> Enum.reverse() |> drop_eol()} do
case {tokens, Enum.reverse(tokens)} do
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
@@ -165,9 +158,6 @@ defmodule EEx.Tokenizer do
end
end
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
defp drop_eol(rest), do: rest
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)]
+7 -41
View File
@@ -64,7 +64,7 @@ defmodule EEx.TokenizerTest do
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
end
test "quotation with do-end" do
test "quotation with do/end" do
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
end
@@ -91,7 +91,7 @@ defmodule EEx.TokenizerTest do
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments" do
test "eex comments" do
exprs = [
{:text, 1, 1, 'foo '},
{:eof, 1, 16}
@@ -100,7 +100,7 @@ defmodule EEx.TokenizerTest do
assert T.tokenize('foo <%# true %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments with do-end" do
test "eex comments with do/end" do
exprs = [
{:text, 1, 1, 'foo '},
{:text, 1, 19, 'bar'},
@@ -110,29 +110,7 @@ defmodule EEx.TokenizerTest do
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments inside do-end" do
exprs = [
{:start_expr, 1, 1, '', ' if true do '},
{:text, 1, 31, 'bar'},
{:end_expr, 1, 34, [], ' end '},
{:eof, 1, 43}
]
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
exprs = [
{:start_expr, 1, 1, [], ' case true do '},
{:middle_expr, 1, 33, '', ' true -> '},
{:text, 1, 46, 'bar'},
{:end_expr, 1, 49, [], ' end '},
{:eof, 1, 58}
]
assert T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "Elixir comments" do
test "elixir comments" do
exprs = [
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
@@ -142,7 +120,7 @@ defmodule EEx.TokenizerTest do
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
end
test "Elixir comments with do-end" do
test "elixir comments with do/end" do
exprs = [
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 1, 27, 'text'},
@@ -182,19 +160,7 @@ defmodule EEx.TokenizerTest do
{:ok, exprs}
end
test "strings with fn-end with newline" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn ->\n'},
{:text, 2, 3, 'foo'},
{:end_expr, 2, 6, [], ' end '},
{:eof, 2, 15}
]
assert T.tokenize('<%= a fn ->\n%>foo<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with multiple fn-end" do
test "strings with multiple callbacks" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
@@ -208,7 +174,7 @@ defmodule EEx.TokenizerTest do
{:ok, exprs}
end
test "strings with fn-end followed by do block" do
test "strings with callback followed by do block" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
-24
View File
@@ -198,15 +198,6 @@ defmodule EExTest do
assert_eval("foo baz", "foo <%= if false do %>bar<% else %>baz<% end %>")
end
test "embedded code with comments in do end" do
assert_eval("foo bar", "foo <%= case true do %><%# comment %><% true -> %>bar<% end %>")
assert_eval(
"foo\n\nbar\n",
"foo\n<%= case true do %>\n<%# comment %>\n<% true -> %>\nbar\n<% end %>"
)
end
test "embedded code with nested do end" do
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
end
@@ -224,11 +215,6 @@ defmodule EExTest do
"<%= Enum.map([1, 2, 3], fn x -> %> <%= 100 + x %> <% end) %>"
)
assert_eval(
" 101 102 103 ",
"<%= Enum.map([1, 2, 3], fn x ->\n%> <%= 100 + x %> <% end) %>"
)
assert_eval(
" 101 102 103 ",
"<%= apply Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end] %>"
@@ -732,16 +718,6 @@ defmodule EExTest do
end
end
describe "parser options" do
test "customizes parsed code" do
atoms_encoder = fn "not_jose", _ -> {:ok, :jose} end
assert_eval("valid", "<%= not_jose %>", [jose: "valid"],
parser_options: [static_atoms_encoder: atoms_encoder]
)
end
end
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
opts = Keyword.merge([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
result = EEx.eval_string(actual, binding, opts)
+3
View File
@@ -445,6 +445,9 @@ defmodule Access do
An error is raised if the accessed structure is not a map or a struct:
iex> get_in(nil, [Access.key(:foo)])
** (BadMapError) expected a map, got: nil
iex> get_in([], [Access.key(:foo)])
** (BadMapError) expected a map, got: []
+8 -22
View File
@@ -42,16 +42,12 @@ defmodule Application do
end
In Mix projects, the environment of the application and its dependencies can
be overridden via the `config/config.exs` and `config/runtime.exs` files. The
former is loaded at build-time, before your code compiles, and the latter at
runtime, just before your app starts. For example, someone using your application
can override its `:db_host` environment variable as follows:
be overridden via the `config/config.exs` file. For example, someone using
your application can override its `:db_host` environment variable as follows:
import Config
config :my_app, :db_host, "db.local"
See the "Configuration" section in the `Mix` module for more information.
You can also change the application environment dynamically by using functions
such as `put_env/3` and `delete_env/2`. However, as a rule of thumb, each application
is responsible for its own environment. Please do not use the functions in this
@@ -100,7 +96,6 @@ defmodule Application do
first choice. However, if you really have to read the application environment
during compilation, we recommend you to use `compile_env/3` instead:
require Application
@db_host Application.compile_env(:my_app, :db_host, "db.local")
By using `compile_env/3`, tools like Mix will store the values used during
@@ -390,7 +385,6 @@ defmodule Application do
:maxT,
:registered,
:included_applications,
:optional_applications,
:applications,
:mod,
:start_phases
@@ -508,7 +502,7 @@ defmodule Application do
# 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) do
defmacro compile_env(app, key_or_path, default \\ nil) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
@@ -545,7 +539,7 @@ defmodule Application do
"""
@doc since: "1.10.0"
@spec compile_env!(app, key | list) :: value
defmacro compile_env!(app, key_or_path) do
defmacro compile_env!(app, key_or_path) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
@@ -630,18 +624,10 @@ defmodule Application do
Our database engine used by `:my_app` needs to know what databases exist, and
what the database configurations are. The database engine can make a call to
`Application.get_env(:my_app, :my_app_databases, [])` to retrieve the list of
databases (specified by module names).
The engine can then traverse each repository in the list and call
`Application.get_env(:my_app, Databases.RepoOne)` and so forth to retrieve the
configuration of each one. In this case, each configuration will be a keyword
list, so you can use the functions in the `Keyword` module or even the `Access`
module to traverse it, for example:
config = Application.get_env(:my_app, Databases.RepoOne)
config[:ip]
`get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
by module names). Our database engine can then traverse each repository in the
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
the configuration of each one.
"""
@spec get_env(app, key, value) :: value
def get_env(app, key, default \\ nil) when is_atom(app) do
+15 -18
View File
@@ -92,9 +92,6 @@ defmodule Base do
"""
@type encode_case :: :upper | :lower
@type decode_case :: :upper | :lower | :mixed
b16_alphabet = '0123456789ABCDEF'
b64_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
b64url_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
@@ -270,7 +267,7 @@ defmodule Base do
"666f6f626172"
"""
@spec encode16(binary, case: encode_case) :: binary
@spec encode16(binary, keyword) :: binary
def encode16(data, opts \\ []) when is_binary(data) do
case = Keyword.get(opts, :case, :upper)
do_encode16(case, data)
@@ -303,7 +300,7 @@ defmodule Base do
{:ok, "foobar"}
"""
@spec decode16(binary, case: decode_case) :: {:ok, binary} | :error
@spec decode16(binary, keyword) :: {:ok, binary} | :error
def decode16(string, opts \\ []) do
{:ok, decode16!(string, opts)}
rescue
@@ -340,7 +337,7 @@ defmodule Base do
"foobar"
"""
@spec decode16!(binary, case: decode_case) :: binary
@spec decode16!(binary, keyword) :: binary
def decode16!(string, opts \\ [])
def decode16!(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
@@ -370,7 +367,7 @@ defmodule Base do
"Zm9vYg"
"""
@spec encode64(binary, padding: boolean) :: binary
@spec encode64(binary, keyword) :: binary
def encode64(data, opts \\ []) when is_binary(data) do
pad? = Keyword.get(opts, :padding, true)
do_encode64(data, pad?)
@@ -400,7 +397,7 @@ defmodule Base do
{:ok, "foob"}
"""
@spec decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
@spec decode64(binary, keyword) :: {:ok, binary} | :error
def decode64(string, opts \\ []) when is_binary(string) do
{:ok, decode64!(string, opts)}
rescue
@@ -434,7 +431,7 @@ defmodule Base do
"foob"
"""
@spec decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
@spec decode64!(binary, keyword) :: binary
def decode64!(string, opts \\ []) when is_binary(string) do
pad? = Keyword.get(opts, :padding, true)
string |> remove_ignored(opts[:ignore]) |> do_decode64(pad?)
@@ -456,7 +453,7 @@ defmodule Base do
"_3_-_A"
"""
@spec url_encode64(binary, padding: boolean) :: binary
@spec url_encode64(binary, keyword) :: binary
def url_encode64(data, opts \\ []) when is_binary(data) do
pad? = Keyword.get(opts, :padding, true)
do_encode64url(data, pad?)
@@ -484,7 +481,7 @@ defmodule Base do
{:ok, <<255, 127, 254, 252>>}
"""
@spec url_decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
@spec url_decode64(binary, keyword) :: {:ok, binary} | :error
def url_decode64(string, opts \\ []) when is_binary(string) do
{:ok, url_decode64!(string, opts)}
rescue
@@ -516,7 +513,7 @@ defmodule Base do
<<255, 127, 254, 252>>
"""
@spec url_decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
@spec url_decode64!(binary, keyword) :: binary
def url_decode64!(string, opts \\ []) when is_binary(string) do
pad? = Keyword.get(opts, :padding, true)
string |> remove_ignored(opts[:ignore]) |> do_decode64url(pad?)
@@ -554,7 +551,7 @@ defmodule Base do
"MZXW6YTBOI"
"""
@spec encode32(binary, case: encode_case, padding: boolean) :: binary
@spec encode32(binary, keyword) :: binary
def encode32(data, opts \\ []) when is_binary(data) do
case = Keyword.get(opts, :case, :upper)
pad? = Keyword.get(opts, :padding, true)
@@ -597,7 +594,7 @@ defmodule Base do
{:ok, "foobar"}
"""
@spec decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
@spec decode32(binary, keyword) :: {:ok, binary} | :error
def decode32(string, opts \\ []) do
{:ok, decode32!(string, opts)}
rescue
@@ -643,7 +640,7 @@ defmodule Base do
"foobar"
"""
@spec decode32!(binary, case: decode_case, padding: boolean) :: binary
@spec decode32!(binary, keyword) :: binary
def decode32!(string, opts \\ []) when is_binary(string) do
case = Keyword.get(opts, :case, :upper)
pad? = Keyword.get(opts, :padding, true)
@@ -683,7 +680,7 @@ defmodule Base do
"CPNMUOJ1E8"
"""
@spec hex_encode32(binary, case: encode_case, padding: boolean) :: binary
@spec hex_encode32(binary, keyword) :: binary
def hex_encode32(data, opts \\ []) when is_binary(data) do
case = Keyword.get(opts, :case, :upper)
pad? = Keyword.get(opts, :padding, true)
@@ -727,7 +724,7 @@ defmodule Base do
{:ok, "foobar"}
"""
@spec hex_decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
@spec hex_decode32(binary, keyword) :: {:ok, binary} | :error
def hex_decode32(string, opts \\ []) do
{:ok, hex_decode32!(string, opts)}
rescue
@@ -774,7 +771,7 @@ defmodule Base do
"foobar"
"""
@spec hex_decode32!(binary, case: decode_case, padding: boolean) :: binary
@spec hex_decode32!(binary, keyword) :: binary
def hex_decode32!(string, opts \\ []) when is_binary(string) do
case = Keyword.get(opts, :case, :upper)
pad? = Keyword.get(opts, :padding, true)
+1 -1
View File
@@ -189,7 +189,7 @@ defmodule Calendar do
@doc """
Calculates the year and era from the given `year`.
"""
@callback year_of_era(year, month, day) :: {year, era}
@callback year_of_era(year) :: {year, era}
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
+2 -8
View File
@@ -943,14 +943,8 @@ defmodule Date do
@spec year_of_era(Calendar.date()) :: {Calendar.year(), non_neg_integer()}
def year_of_era(date)
def year_of_era(%{calendar: calendar, year: year, month: month, day: day}) do
# TODO: Remove me on 1.17
# The behaviour implementation already warns on missing callback.
if function_exported?(calendar, :year_of_era, 3) do
calendar.year_of_era(year, month, day)
else
calendar.year_of_era(year)
end
def year_of_era(%{calendar: calendar, year: year}) do
calendar.year_of_era(year)
end
@doc """
-1
View File
@@ -23,7 +23,6 @@ defmodule Date.Range do
@typep iso_days() :: Calendar.iso_days()
@enforce_keys [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
defimpl Enumerable do
+6 -59
View File
@@ -2,14 +2,11 @@ defmodule DateTime do
@moduledoc """
A datetime implementation with a time zone.
This datetime can be seen as a snapshot of a date and time
at a given time zone. For such purposes, it also includes both
UTC and Standard offsets, as well as the zone abbreviation
field used exclusively for formatting purposes. Note future
datetimes are not necessarily guaranteed to exist, as time
zones may change any time in the future due to geopolitical
reasons. See the "Datetimes as snapshots" section for more
information.
This datetime can be seen as an ephemeral snapshot
of a datetime at a given time zone. For such purposes,
it also includes both UTC and Standard offsets, as
well as the zone abbreviation field used exclusively
for formatting purposes.
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
are structural and based on the DateTime struct fields. For proper
@@ -44,56 +41,6 @@ defmodule DateTime do
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
See the proper names in the library installation instructions.
## Datetimes as snapshots
In the first section, we described datetimes as a "snapshot of
a date and time at a given time zone". To understand precisely
what we mean, let's see an example.
Imagine someone in Poland wants to schedule a meeting with someone
in Brazil in the next year. The meeting will happen at 2:30 AM
in the Polish time zone. At what time will the meeting happen in
Brazil?
You can consult the time zone database today, one year before,
using the API in this module and it will give you an answer that
is valid right now. However, this answer may not be valid in the
future. Why? Because both Brazil and Poland may change their timezone
rules, ultimately affecting the result. For example, a country may
choose to enter or abandon "Daylight Saving Time", which is a
process where we adjust the clock one hour forward or one hour
back once per year. Whenener the rules change, the exact instant
that 2:30 AM in Polish time will be in Brazil may change.
In other words, whenever working with future DateTimes, there is
no guarantee the results you get will always be correct, until
the event actually happens. Therefore, when you ask for a future
time, the answers you get are a snapshot that reflects the current
state of the time zone rules. For datetimes in the past, this is
not a problem, because time zone rules do not change for past
events.
To make matters worse, it may be that the 2:30 AM in Polish time
does not actually even exist or it is ambiguous. If a certain
time zone observes "Daylight Saving Time", they will move their
clock forward once a year. When this happens, there is a whole
hour that does not exist. Then, when they move the clock back,
there is a certain hour that will happen twice. So if you want
to schedule a meeting when this shift back happens, you would
need to explicitly say which of the 2:30 AM you precisely mean.
Applications that are date and time sensitive, need to take
these scenarios into account and correctly communicate them to
users.
The good news is: Elixir contains all of the building blocks
necessary to tackle those problems. The default timezone database
used by Elixir, `Calendar.UTCOnlyTimeZoneDatabase`, only works
with UTC, which does not observe those issues. Once you bring
a proper time zone database, the functions in this module will
query the database and return the relevant information. For
example, look at how `DateTime.new/4` returns different results
based on the scenarios described in this section.
"""
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
@@ -503,7 +450,7 @@ defmodule DateTime do
# we get the last microsecond just before.
before_naive =
first_period_until_wall
|> Map.replace!(:microsecond, {999_999, 6})
|> Map.put(:microsecond, {999_999, 6})
|> NaiveDateTime.add(-1)
after_naive = second_period_from_wall
+21 -78
View File
@@ -131,8 +131,6 @@ defmodule Calendar.ISO do
unix_end = 315_569_519_999_999_999 - @unix_epoch * 1_000_000
@unix_range_microseconds unix_start..unix_end
defguardp is_format(term) when term in [:basic, :extended]
@typedoc """
"Before the Current Era" or "Before the Common Era" (BCE), for those years less than `1`.
"""
@@ -158,8 +156,6 @@ defmodule Calendar.ISO do
@type minute :: 0..59
@type second :: 0..59
@type weekday :: :monday | :tuesday | :wednesday | :thursday | :friday | :saturday | :sunday
@type utc_offset :: integer
@type format :: :basic | :extended
@typedoc """
Microseconds with stored precision.
@@ -266,9 +262,6 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
@spec parse_time(String.t()) ::
{:ok, {hour, minute, second, microsecond}}
| {:error, atom}
def parse_time(string) when is_binary(string),
do: parse_time(string, :extended)
@@ -289,15 +282,11 @@ defmodule Calendar.ISO do
"""
@doc since: "1.12.0"
@spec parse_time(String.t(), format) ::
{:ok, {hour, minute, second, microsecond}}
| {:error, atom}
def parse_time(string, format) when is_binary(string) and is_format(format) do
case string do
"T" <> rest -> do_parse_time(rest, format)
_ -> do_parse_time(string, format)
end
end
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
@@ -347,9 +336,6 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
@spec parse_date(String.t()) ::
{:ok, {year, month, day}}
| {:error, atom}
def parse_date(string) when is_binary(string),
do: parse_date(string, :extended)
@@ -370,19 +356,13 @@ defmodule Calendar.ISO do
"""
@doc since: "1.12.0"
@spec parse_date(String.t(), format) ::
{:ok, {year, month, day}}
| {:error, atom}
def parse_date(string, format) when is_binary(string) and is_format(format),
do: parse_date_guarded(string, format)
defp parse_date_guarded("-" <> string, format),
def parse_date("-" <> string, format) when is_binary(string),
do: do_parse_date(string, -1, format)
defp parse_date_guarded("+" <> string, format),
def parse_date("+" <> string, format) when is_binary(string),
do: do_parse_date(string, 1, format)
defp parse_date_guarded(string, 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
@@ -432,9 +412,6 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
@spec parse_naive_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}}
| {:error, atom}
def parse_naive_datetime(string) when is_binary(string),
do: parse_naive_datetime(string, :extended)
@@ -455,19 +432,13 @@ defmodule Calendar.ISO do
"""
@doc since: "1.12.0"
@spec parse_naive_datetime(String.t(), format) ::
{:ok, {year, month, day, hour, minute, second, microsecond}}
| {:error, atom}
def parse_naive_datetime(string, format) when is_binary(string) and is_format(format),
do: parse_naive_datetime_guarded(string, format)
defp parse_naive_datetime_guarded("-" <> string, format),
def parse_naive_datetime("-" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, -1, format)
defp parse_naive_datetime_guarded("+" <> string, format),
def parse_naive_datetime("+" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
defp parse_naive_datetime_guarded(string, format),
def parse_naive_datetime(string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
defp do_parse_naive_datetime(
@@ -536,9 +507,6 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
@spec parse_utc_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
| {:error, atom}
def parse_utc_datetime(string) when is_binary(string),
do: parse_utc_datetime(string, :extended)
@@ -559,19 +527,13 @@ defmodule Calendar.ISO do
"""
@doc since: "1.12.0"
@spec parse_utc_datetime(String.t(), format) ::
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
| {:error, atom}
def parse_utc_datetime(string, format) when is_binary(string) and is_format(format),
do: parse_utc_datetime_guarded(string, format)
defp parse_utc_datetime_guarded("-" <> string, format),
def parse_utc_datetime("-" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, -1, format)
defp parse_utc_datetime_guarded("+" <> string, format),
def parse_utc_datetime("+" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
defp parse_utc_datetime_guarded(string, format),
def parse_utc_datetime(string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
defp do_parse_utc_datetime(
@@ -1023,33 +985,14 @@ defmodule Calendar.ISO do
"""
@doc since: "1.8.0"
@spec year_of_era(year) :: {1..10000, era}
def year_of_era(year) when is_year_CE(year), do: {year, 1}
def year_of_era(year) when is_year_BCE(year), do: {abs(year) + 1, 0}
@doc """
Calendar callback to compute the year and era from the
given `year`, `month` and `day`.
In the ISO calendar, the new year coincides with the new era,
so the `month` and `day` arguments are discarded. If you only
have the year available, you can `year_of_era/1` instead.
## Examples
iex> Calendar.ISO.year_of_era(1, 1, 1)
{1, 1}
iex> Calendar.ISO.year_of_era(2018, 12, 1)
{2018, 1}
iex> Calendar.ISO.year_of_era(0, 1, 1)
{1, 0}
iex> Calendar.ISO.year_of_era(-1, 12, 1)
{2, 0}
"""
@doc since: "1.13.0"
@impl true
@spec year_of_era(year, month, day) :: {1..10000, era}
def year_of_era(year, _month, _day), do: year_of_era(year)
def year_of_era(year) when is_year_CE(year) do
{year, 1}
end
def year_of_era(year) when is_year_BCE(year) do
{abs(year) + 1, 0}
end
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
+353 -273
View File
@@ -30,11 +30,6 @@ 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.
The functions above work with Elixir source. If you want to work
with modules compiled to bytecode, which have the `.beam` extension
and are typically found below the _build directory of a Mix project,
see the functions in Erlang's [`:code`](`:code`) module.
## Code loading on the Erlang VM
Erlang has two modes to load code: interactive and embedded.
@@ -149,12 +144,6 @@ defmodule Code do
of keys to traverse in the application environment and `return` is either
`{:ok, value}` or `:error`.
* `{:on_module, bytecode, :none}` - (since v1.11.0) traced whenever a module
is defined. This is equivalent to the `@after_compile` callback and invoked
after any `@after_compile` in the given module. The third element is currently
`:none` but it may provide more metadata in the future. It is best to ignore
it at the moment.
The `:tracers` compiler option can be combined with the `:parser_options`
compiler option to enrich the metadata of the traced events above.
@@ -217,10 +206,257 @@ defmodule Code do
required_files()
end
@doc false
@deprecated "Use Code.Fragment.cursor_context/2 instead"
def cursor_context(code, options \\ []) do
Code.Fragment.cursor_context(code, options)
@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 """
@@ -343,18 +579,33 @@ defmodule Code do
* `:line` - the line on which the script starts
Additionally, you may also pass an environment as second argument,
so the evaluation happens within that environment. However, if the evaluated
code requires or compiles another file, the environment given to this function
will not apply to said files.
Additionally, the following scope values can be configured:
Returns a tuple of the form `{value, binding}`, where `value` is the value
returned from evaluating `string`. If an error occurs while evaluating
`string` an exception will be raised.
* `:aliases` - a list of tuples with the alias and its target
`binding` is a list with all variable bindings after evaluating `string`.
The binding keys are usually atoms, but they may be a tuple for variables
defined in a different context.
* `:requires` - a list of modules required
* `:functions` - a list of tuples where the first element is a module
and the second a list of imported function names and arity; the list
of function names and arity must be sorted
* `:macros` - a list of tuples where the first element is a module
and the second a list of imported macro names and arity; the list
of function names and arity must be sorted
Note that setting any of the values above overrides Elixir's default
values. For example, setting `:requires` to `[]` will no longer
automatically require the `Kernel` module. In the same way setting
`:macros` will no longer auto-import `Kernel` macros like `Kernel.if/2`,
`Kernel.SpecialForms.case/2`, and so on.
Returns a tuple of the form `{value, binding}`,
where `value` is the value returned from evaluating `string`.
If an error occurs while evaluating `string` an exception will be raised.
`binding` is a list with all variable bindings
after evaluating `string`. The binding keys are usually atoms, but they
may be a tuple for variables defined in a different context.
## Examples
@@ -391,15 +642,16 @@ defmodule Code do
def eval_string(string, binding \\ [], opts \\ [])
def eval_string(string, binding, %Macro.Env{} = env) do
validated_eval_string(string, binding, env)
eval_string_with_error_handling(string, binding, Map.to_list(env))
end
def eval_string(string, binding, opts) when is_list(opts) do
validated_eval_string(string, binding, opts)
validate_eval_opts(opts)
eval_string_with_error_handling(string, binding, opts)
end
defp validated_eval_string(string, binding, opts_or_env) do
%{line: line, file: file} = env = :elixir.env_for_eval(opts_or_env)
defp eval_string_with_error_handling(string, binding, opts) do
%{line: line, file: file} = env = :elixir.env_for_eval(opts)
forms = :elixir.string_to_quoted!(to_charlist(string), line, 1, file, [])
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
{value, binding}
@@ -431,10 +683,10 @@ defmodule Code do
and this option augments this list.
* `:force_do_end_blocks` (since v1.9.0) - when `true`, converts all
inline usages of `do: ...`, `else: ...` and friends into `do`-`end`
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
If you set it to `false` later on, `do/end` blocks won't be
converted back to keywords.
## Design principles
@@ -553,19 +805,17 @@ defmodule Code do
gives more options to the formatter.
This may also appear in do/end blocks, where the `do` keyword (or `->`)
may go over the line length because there is no opportunity for the
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
end
And only the `do` keyword is above the line length, Elixir **will not**
emit this:
case very_long_expression()
do
end
So it prefers to not touch the line at all and leave `do` above the
line limit.
@@ -588,7 +838,7 @@ defmodule Code do
line before and after and 2) empty lines are always squeezed
together into a single empty line
* The choice between `:do` keyword and `do`-`end` blocks is left
* The choice between `:do` keyword and `do/end` blocks is left
to the user
* Lists, tuples, bitstrings, maps, structs and function calls will be
@@ -637,7 +887,7 @@ defmodule Code do
Elixir has two syntaxes for function calls. With parens and no parens.
By default, Elixir will add parens to all calls except for:
1. calls that have `do`-`end` blocks
1. calls that have do/end blocks
2. local calls without parens where the name and arity of the local
call is also listed under `:locals_without_parens` (except for
calls with arity 0, where the compiler always require parens)
@@ -725,25 +975,8 @@ defmodule Code do
@spec format_string!(binary, keyword) :: iodata
def format_string!(string, opts \\ []) when is_binary(string) and is_list(opts) do
line_length = Keyword.get(opts, :line_length, 98)
to_quoted_opts =
[
unescape: false,
warn_on_unnecessary_quotes: false,
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
token_metadata: true
] ++ opts
{forms, comments} = string_to_quoted_with_comments!(string, to_quoted_opts)
to_algebra_opts =
[
comments: comments
] ++ opts
doc = Code.Formatter.to_algebra(forms, to_algebra_opts)
Inspect.Algebra.format(doc, line_length)
algebra = Code.Formatter.to_algebra!(string, opts)
Inspect.Algebra.format(algebra, line_length)
end
@doc """
@@ -768,7 +1001,7 @@ defmodule Code do
Macro arguments are typically transformed by unquoting them into the
returned quoted expressions (instead of evaluated).
See `eval_string/3` for a description of `binding` and `opts`.
See `eval_string/3` for a description of `binding` and options.
## Examples
@@ -794,25 +1027,64 @@ defmodule Code do
def eval_quoted(quoted, binding \\ [], opts \\ [])
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(env))
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
{value, binding}
end
def eval_quoted(quoted, binding, opts) when is_list(opts) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(opts))
validate_eval_opts(opts)
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, opts)
{value, binding}
end
defp validate_eval_opts(opts) do
if f = opts[:functions], do: validate_imports(:functions, f)
if m = opts[:macros], do: validate_imports(:macros, m)
if a = opts[:aliases], do: validate_aliases(:aliases, a)
if r = opts[:requires], do: validate_requires(:requires, r)
end
defp validate_requires(kind, requires) do
valid = is_list(requires) and Enum.all?(requires, &is_atom(&1))
unless valid do
raise ArgumentError, "expected :#{kind} option given to eval in the format: [module]"
end
end
defp validate_aliases(kind, aliases) do
valid = is_list(aliases) and Enum.all?(aliases, fn {k, v} -> is_atom(k) and is_atom(v) end)
unless valid do
raise ArgumentError,
"expected :#{kind} option given to eval in the format: [{module, module}]"
end
end
defp validate_imports(kind, imports) do
valid =
is_list(imports) and
Enum.all?(imports, fn {k, v} ->
is_atom(k) and is_list(v) and
Enum.all?(v, fn {name, arity} -> is_atom(name) and is_integer(arity) end)
end)
unless valid do
raise ArgumentError,
"expected :#{kind} option given to eval in the format: [{module, [{name, arity}]}]"
end
end
@doc ~S"""
Converts the given string to its quoted form.
Returns `{:ok, quoted_form}` if it succeeds,
`{:error, {meta, message_info, token}}` otherwise.
`{:error, {line, error, token}}` otherwise.
## Options
* `:file` - the filename to be reported in case of parsing errors.
Defaults to `"nofile"`.
Defaults to "nofile".
* `:line` - the starting line of the string being parsed.
Defaults to 1.
@@ -823,13 +1095,6 @@ defmodule Code do
* `:columns` - when `true`, attach a `:column` key to the quoted
metadata. Defaults to `false`.
* `:unescape` (since v1.10.0) - when `false`, preserves escaped sequences.
For example, `"null byte\\t\\x00"` will be kept as is instead of being
converted to a bitstring literal. Note if you set this option to false, the
resulting AST is no longer valid, but it can be useful to analyze/transform
source code, typically in in combination with `quoted_to_algebra/2`.
Defaults to `true`.
* `:existing_atoms_only` - when `true`, raises an error
when non-existing atoms are found by the tokenizer.
Defaults to `false`.
@@ -894,7 +1159,7 @@ defmodule Code do
"""
@spec string_to_quoted(List.Chars.t(), keyword) ::
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
{:ok, Macro.t()} | {:error, {location :: keyword, term, term}}
def string_to_quoted(string, opts \\ []) when is_list(opts) do
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
@@ -912,7 +1177,7 @@ defmodule Code do
@doc """
Converts the given string to its quoted form.
It returns the AST if it succeeds,
It returns the ast if it succeeds,
raises an exception otherwise. The exception is a `TokenMissingError`
in case a token is missing (usually because the expression is incomplete),
`SyntaxError` otherwise.
@@ -928,188 +1193,7 @@ defmodule Code do
end
@doc """
Converts the given string to its quoted form and a list of comments.
This function is useful when performing textual changes to the source code,
while preserving information like comments and literals position.
Returns `{:ok, quoted_form, comments}` if it succeeds,
`{:error, {line, error, token}}` otherwise.
Comments are maps with the following fields:
* `:line` - The line number the source code
* `:text` - The full text of the comment, including the leading `#`
* `:previous_eol_count` - How many end of lines there are between the comment and the previous AST node or comment
* `:next_eol_count` - How many end of lines there are between the comment and the next AST node or comment
Check `string_to_quoted/2` for options information.
## Examples
iex> Code.string_to_quoted_with_comments("\""
...> :foo
...>
...> # Hello, world!
...>
...>
...> # Some more comments!
...> "\"")
{:ok, :foo, [
%{line: 3, column: 1, previous_eol_count: 2, next_eol_count: 3, text: "\# Hello, world!"},
%{line: 6, column: 1, previous_eol_count: 3, next_eol_count: 1, text: "\# Some more comments!"},
]}
iex> Code.string_to_quoted_with_comments(":foo # :bar")
{:ok, :foo, [
%{line: 1, column: 6, previous_eol_count: 0, next_eol_count: 0, text: "\# :bar"}
]}
"""
@doc since: "1.13.0"
@spec string_to_quoted_with_comments(List.Chars.t(), keyword) ::
{:ok, Macro.t(), list(map())} | {:error, {location :: keyword, term, term}}
def string_to_quoted_with_comments(string, opts \\ []) when is_list(opts) do
charlist = to_charlist(string)
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
column = Keyword.get(opts, :column, 1)
Process.put(:code_formatter_comments, [])
opts = [preserve_comments: &preserve_comments/5] ++ opts
with {:ok, tokens} <- :elixir.string_to_tokens(charlist, line, column, file, opts),
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, opts) do
comments = Enum.reverse(Process.get(:code_formatter_comments))
{:ok, forms, comments}
end
after
Process.delete(:code_formatter_comments)
end
@doc """
Converts the given string to its quoted form and a list of comments.
Returns the AST and a list of comments if it succeeds, raises an exception
otherwise. The exception is a `TokenMissingError` in case a token is missing
(usually because the expression is incomplete), `SyntaxError` otherwise.
Check `string_to_quoted/2` for options information.
"""
@doc since: "1.13.0"
@spec string_to_quoted_with_comments!(List.Chars.t(), keyword) :: {Macro.t(), list(map())}
def string_to_quoted_with_comments!(string, opts \\ []) do
charlist = to_charlist(string)
case string_to_quoted_with_comments(charlist, opts) do
{:ok, forms, comments} ->
{forms, comments}
{:error, {location, error, token}} ->
:elixir_errors.parse_error(
location,
Keyword.get(opts, :file, "nofile"),
error,
token,
{charlist, Keyword.get(opts, :line, 1), Keyword.get(opts, :column, 1)}
)
end
end
defp preserve_comments(line, column, tokens, comment, rest) do
comments = Process.get(:code_formatter_comments)
comment = %{
line: line,
column: column,
previous_eol_count: previous_eol_count(tokens),
next_eol_count: next_eol_count(rest, 0),
text: List.to_string(comment)
}
Process.put(:code_formatter_comments, [comment | comments])
end
defp next_eol_count('\s' ++ rest, count), do: next_eol_count(rest, count)
defp next_eol_count('\t' ++ rest, count), do: next_eol_count(rest, count)
defp next_eol_count('\n' ++ rest, count), do: next_eol_count(rest, count + 1)
defp next_eol_count('\r\n' ++ rest, count), do: next_eol_count(rest, count + 1)
defp next_eol_count(_, count), do: count
defp previous_eol_count([{token, {_, _, count}} | _])
when token in [:eol, :",", :";"] and count > 0 do
count
end
defp previous_eol_count([]), do: 1
defp previous_eol_count(_), do: 0
@doc ~S"""
Converts a quoted expression to an algebra document using Elixir's formatter rules.
The algebra document can be converted into a string by calling:
doc
|> Inspect.Algebra.format(:infinity)
|> IO.iodata_to_binary()
For a high-level function that does the same, see `Macro.to_string/1`.
## Formatting considerations
The Elixir AST does not contain metadata for literals like strings, lists, or
tuples with two elements, which means that the produced algebra document will
not respect all of the user preferences and comments may be misplaced.
To get better results, you can use the `:token_metadata`, `:unescape` and
`:literal_encoder` options to `string_to_quoted/2` to provide additional
information to the formatter:
[
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
token_metadata: true,
unescape: false
]
This will produce an AST that contains information such as `do` blocks start
and end lines or sigil delimiters, and by wrapping literals in blocks they can
now hold metadata like line number, string delimiter and escaped sequences, or
integer formatting (such as `0x2a` instead of `47`). However, **note this AST is
not valid**. If you evaluate it, it won't have the same semantics as the regular
Elixir AST due to the `:unescape` and `:literal_encoder` options. However,
those options are useful if you're doing source code manipulation, where it's
important to preserve user choices and comments placing.
## Options
* `:comments` - the list of comments associated with the quoted expression.
Defaults to `[]`. It is recommended that both `:token_metadata` and
`:literal_encoder` options are given to `string_to_quoted_with_comments/2`
in order to get proper placement for comments
* `:escape` - when `true`, escaped sequences like `\n` will be escaped into
`\\n`. If the `:unescape` option was set to `false` when using
`string_to_quoted/2`, setting this option to `false` will prevent it from
escaping the sequences twice. Defaults to `true`.
* `:locals_without_parens` - a keyword list of name and arity
pairs that should be kept without parens whenever possible.
The arity may be the atom `:*`, which implies all arities of
that name. The formatter already includes a list of functions
and this option augments this list.
"""
@doc since: "1.13.0"
@spec quoted_to_algebra(Macro.t(), keyword) :: Inspect.Algebra.t()
def quoted_to_algebra(quoted, opts \\ []) do
quoted
|> Code.Normalizer.normalize(opts)
|> Code.Formatter.to_algebra(opts)
end
@doc """
Evaluates the given file.
Evals the given file.
Accepts `relative_to` as an argument to tell where the file is located.
@@ -1128,14 +1212,9 @@ defmodule Code do
def load_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
:elixir_code_server.call({:acquire, file})
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(file, fn _, _ -> :ok end)
end)
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
:elixir_code_server.cast({:required, file})
loaded
verify_loaded(loaded)
end
@doc """
@@ -1179,13 +1258,9 @@ defmodule Code do
nil
:proceed ->
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(file, fn _, _ -> :ok end)
end)
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
:elixir_code_server.cast({:required, file})
loaded
verify_loaded(loaded)
end
end
@@ -1211,8 +1286,8 @@ defmodule Code do
@doc """
Stores all given compilation options.
To store individual options and for a description of all
options, see `put_compiler_option/2`.
To store individual options, see `put_compiler_option/2`.
For a description of all options, see `put_compiler_option/2`.
## Examples
@@ -1390,7 +1465,7 @@ defmodule Code do
@spec compile_string(List.Chars.t(), binary) :: [{module, binary}]
def compile_string(string, file \\ "nofile") when is_binary(file) do
loaded = :elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
end
@doc """
@@ -1404,7 +1479,7 @@ defmodule Code do
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
loaded = :elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
end
@doc """
@@ -1424,9 +1499,8 @@ defmodule Code do
@doc since: "1.7.0"
@spec compile_file(binary, nil | binary) :: [{module, binary}]
def compile_file(file, relative_to \\ nil) when is_binary(file) do
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
end)
loaded = :elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
verify_loaded(loaded)
end
@doc """
@@ -1599,7 +1673,7 @@ defmodule Code do
file.
It returns the term stored in the documentation chunk in the format defined by
[EEP 48](https://www.erlang.org/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
@@ -1702,7 +1776,7 @@ defmodule Code do
@doc ~S"""
Deprecated function to retrieve old documentation format.
Elixir v1.7 adopts [EEP 48](https://www.erlang.org/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`.
@@ -1733,4 +1807,10 @@ defmodule Code do
raise Code.LoadError, file: file
end
end
defp verify_loaded(loaded) do
maps_binaries = Enum.map(loaded, fn {_module, map, binary} -> {map, binary} end)
Module.ParallelChecker.verify(maps_binaries, [])
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
end
end
+183 -139
View File
@@ -142,92 +142,171 @@ defmodule Code.Formatter do
@do_end_keywords [:rescue, :catch, :else, :after]
@doc """
Converts the quoted expression into an algebra document.
Checks if two strings are equivalent.
"""
def to_algebra(quoted, opts \\ []) do
comments = Keyword.get(opts, :comments, [])
def equivalent(string1, string2) when is_binary(string1) and is_binary(string2) do
quoted1 = :elixir.string_to_quoted!(to_charlist(string1), 1, 1, "nofile", [])
quoted2 = :elixir.string_to_quoted!(to_charlist(string2), 1, 1, "nofile", [])
state =
comments
|> Enum.map(&format_comment/1)
|> gather_comments()
|> state(opts)
case not_equivalent(quoted1, quoted2) do
{left, right} -> {:error, left, right}
nil -> :ok
end
end
{doc, _} = block_to_algebra(quoted, @min_line, @max_line, state)
doc
defp not_equivalent({:__block__, _, [left]}, right) do
not_equivalent(left, right)
end
defp not_equivalent(left, {:__block__, _, [right]}) do
not_equivalent(left, right)
end
defp not_equivalent({:__block__, _, []}, nil) do
nil
end
defp not_equivalent(nil, {:__block__, _, []}) do
nil
end
defp not_equivalent([left | lefties], [right | righties]) do
not_equivalent(left, right) || not_equivalent(lefties, righties)
end
defp not_equivalent({left_name, _, left_args}, {right_name, _, right_args}) do
not_equivalent(left_name, right_name) || not_equivalent(left_args, right_args)
end
defp not_equivalent({left1, left2}, {right1, right2}) do
not_equivalent(left1, right1) || not_equivalent(left2, right2)
end
defp not_equivalent(side, side) do
nil
end
defp not_equivalent(left, right) do
{left, right}
end
@doc """
Lists all default locals without parens.
Converts `string` to an algebra document.
Returns `{:ok, doc}` or `{:error, parser_error}`.
See `Code.format_string!/2` for the list of options.
"""
def locals_without_parens do
@locals_without_parens
def to_algebra(string, opts \\ []) when is_binary(string) and is_list(opts) do
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
charlist = String.to_charlist(string)
Process.put(:code_formatter_comments, [])
tokenizer_options = [
unescape: false,
preserve_comments: &preserve_comments/5,
warn_on_unnecessary_quotes: false
]
parser_options = [
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
token_metadata: true
]
with {:ok, tokens} <- :elixir.string_to_tokens(charlist, line, 1, file, tokenizer_options),
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, parser_options) do
state =
Process.get(:code_formatter_comments)
|> Enum.reverse()
|> gather_comments()
|> state(opts)
{doc, _} = block_to_algebra(forms, @min_line, @max_line, state)
{:ok, doc}
end
after
Process.delete(:code_formatter_comments)
end
@doc """
Checks if a function is a local without parens.
Converts `string` to an algebra document.
Raises if the `string` cannot be parsed.
See `Code.format_string!/2` for the list of options.
"""
def local_without_parens?(fun, arity, locals_without_parens) do
arity > 0 and
Enum.any?(locals_without_parens, fn {key, val} ->
key == fun and (val == :* or val == arity)
end)
def to_algebra!(string, opts \\ []) do
case to_algebra(string, opts) do
{:ok, doc} ->
doc
{:error, {location, error, token}} ->
:elixir_errors.parse_error(location, Keyword.get(opts, :file, "nofile"), error, token)
end
end
defp state(comments, opts) do
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
file = Keyword.get(opts, :file, nil)
sigils = Keyword.get(opts, :sigils, [])
sigils =
Map.new(sigils, fn {key, value} ->
with true <- is_atom(key) and is_function(value, 2),
[char] <- Atom.to_charlist(key),
true <- char in ?A..?Z do
{char, value}
else
_ ->
raise ArgumentError,
":sigils must be a keyword list with a single uppercased letter as key and an " <>
"anonymous function expecting two arguments as value, got: #{inspect(sigils)}"
end
end)
locals_without_parens =
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
%{
force_do_end_blocks: force_do_end_blocks,
locals_without_parens: locals_without_parens ++ locals_without_parens(),
locals_without_parens: locals_without_parens,
operand_nesting: 2,
skip_eol: false,
comments: comments,
sigils: sigils,
file: file
comments: comments
}
end
defp format_comment(%{text: text} = comment) do
%{comment | text: format_comment_text(text)}
# Code comment handling
defp preserve_comments(line, _column, tokens, comment, rest) do
comments = Process.get(:code_formatter_comments)
comment = {line, {previous_eol(tokens), next_eol(rest, 0)}, format_comment(comment, [])}
Process.put(:code_formatter_comments, [comment | comments])
end
defp format_comment_text("#"), do: "#"
defp format_comment_text("#!" <> rest), do: "#!" <> rest
defp format_comment_text("##" <> rest), do: "#" <> format_comment_text("#" <> rest)
defp format_comment_text("# " <> rest), do: "# " <> rest
defp format_comment_text("#" <> rest), do: "# " <> rest
defp next_eol('\s' ++ rest, count), do: next_eol(rest, count)
defp next_eol('\t' ++ rest, count), do: next_eol(rest, count)
defp next_eol('\n' ++ rest, count), do: next_eol(rest, count + 1)
defp next_eol('\r\n' ++ rest, count), do: next_eol(rest, count + 1)
defp next_eol(_, count), do: count
defp previous_eol([{token, {_, _, count}} | _])
when token in [:eol, :",", :";"] and count > 0 do
count
end
defp previous_eol([]), do: 1
defp previous_eol(_), do: nil
defp format_comment('##' ++ rest, acc), do: format_comment([?# | rest], [?# | acc])
defp format_comment('#!', acc), do: reverse_to_string(acc, '#!')
defp format_comment('#! ' ++ _ = rest, acc), do: reverse_to_string(acc, rest)
defp format_comment('#!' ++ rest, acc), do: reverse_to_string(acc, [?#, ?!, ?\s, rest])
defp format_comment('#', acc), do: reverse_to_string(acc, '#')
defp format_comment('# ' ++ _ = rest, acc), do: reverse_to_string(acc, rest)
defp format_comment('#' ++ rest, acc), do: reverse_to_string(acc, [?#, ?\s, rest])
defp reverse_to_string(acc, prefix) do
acc |> Enum.reverse(prefix) |> List.to_string()
end
# If there is a no new line before, we can't gather all followup comments.
defp gather_comments([%{previous_eol_count: 0} = comment | comments]) do
comment = %{comment | previous_eol_count: @newlines}
defp gather_comments([{line, {nil, next_eol}, doc} | comments]) do
comment = {line, {@newlines, next_eol}, doc}
[comment | gather_comments(comments)]
end
defp gather_comments([comment | comments]) do
%{line: line, next_eol_count: next_eol_count, text: doc} = comment
{next_eol_count, comments, doc} =
gather_followup_comments(line + 1, next_eol_count, comments, doc)
comment = %{comment | next_eol_count: next_eol_count, text: doc}
defp gather_comments([{line, {previous_eol, next_eol}, doc} | comments]) do
{next_eol, comments, doc} = gather_followup_comments(line + 1, next_eol, comments, doc)
comment = {line, {previous_eol, next_eol}, doc}
[comment | gather_comments(comments)]
end
@@ -235,14 +314,13 @@ defmodule Code.Formatter do
[]
end
defp gather_followup_comments(line, _, [%{line: line} = comment | comments], doc)
when comment.previous_eol_count != 0 do
%{next_eol_count: next_eol_count, text: text} = comment
gather_followup_comments(line + 1, next_eol_count, comments, line(doc, text))
defp gather_followup_comments(line, _, [{line, {previous_eol, next_eol}, text} | comments], doc)
when previous_eol != nil do
gather_followup_comments(line + 1, next_eol, comments, line(doc, text))
end
defp gather_followup_comments(_line, next_eol_count, comments, doc) do
{next_eol_count, comments, doc}
defp gather_followup_comments(_line, next_eol, comments, doc) do
{next_eol, comments, doc}
end
# Special AST nodes from compiler feedback
@@ -272,7 +350,7 @@ defmodule Code.Formatter do
{doc, state} =
entries
|> prepend_heredoc_line()
|> interpolation_to_algebra(~s["""], state, @double_heredoc, @double_heredoc)
|> interpolation_to_algebra(:heredoc, state, @double_heredoc, @double_heredoc)
{force_unfit(doc), state}
@@ -294,7 +372,7 @@ defmodule Code.Formatter do
{doc, state} =
entries
|> prepend_heredoc_line()
|> list_interpolation_to_algebra(~s['''], state, @single_heredoc, @single_heredoc)
|> list_interpolation_to_algebra(:heredoc, state, @single_heredoc, @single_heredoc)
{force_unfit(doc), state}
@@ -357,7 +435,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra({:__block__, meta, [list]}, _context, state) when is_list(list) do
case meta[:delimiter] do
~s['''] ->
string = list |> List.to_string() |> escape_heredoc(~s['''])
string = list |> List.to_string() |> escape_heredoc()
{@single_heredoc |> concat(string) |> concat(@single_heredoc) |> force_unfit(), state}
~s['] ->
@@ -371,7 +449,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra({:__block__, meta, [string]}, _context, state) when is_binary(string) do
if meta[:delimiter] == ~s["""] do
string = escape_heredoc(string, ~s["""])
string = escape_heredoc(string)
{@double_heredoc |> concat(string) |> concat(@double_heredoc) |> force_unfit(), state}
else
string = escape_string(string, @double_quote)
@@ -520,11 +598,6 @@ defmodule Code.Formatter do
{doc, state}
end
# #PID's and #Ref's may appear on regular AST
defp quoted_to_algebra(unknown, _context, state) do
{inspect(unknown), state}
end
## Blocks
defp block_to_algebra([{:->, _, _} | _] = type_fun, min_line, max_line, state) do
@@ -1007,14 +1080,9 @@ defmodule Code.Formatter do
defp local_to_algebra(fun, meta, args, context, state) when is_atom(fun) do
skip_parens =
cond do
meta?(meta, :closing) ->
:skip_if_only_do_end
local_without_parens?(fun, length(args), state.locals_without_parens) ->
:skip_unless_many_args
true ->
:skip_if_do_end
meta?(meta, :closing) -> :skip_if_only_do_end
local_without_parens?(fun, args, state) -> :skip_unless_many_args
true -> :skip_if_do_end
end
{{call_doc, state}, wrap_in_parens?} =
@@ -1193,6 +1261,15 @@ defmodule Code.Formatter do
end
end
defp local_without_parens?(fun, args, %{locals_without_parens: locals_without_parens}) do
length = length(args)
length > 0 and
Enum.any?(locals_without_parens, fn {key, val} ->
key == fun and (val == :* or val == length)
end)
end
defp no_generators?(args) do
not Enum.any?(args, &match?({:<-, _, [_, _]}, &1))
end
@@ -1319,37 +1396,13 @@ defmodule Code.Formatter do
opening_delimiter when not is_nil(opening_delimiter) <- meta[:delimiter] do
doc = <<?~, name, opening_delimiter::binary>>
entries =
case state.sigils do
%{^name => callback} ->
metadata = [
file: state.file,
line: meta[:line],
sigil: List.to_atom([name]),
modifiers: modifiers,
opening_delimiter: opening_delimiter
]
case callback.(hd(entries), metadata) do
iodata when is_binary(iodata) or is_list(iodata) ->
[IO.iodata_to_binary(iodata)]
other ->
raise ArgumentError,
"expected sigil callback to return iodata, got: #{inspect(other)}"
end
%{} ->
entries
end
if opening_delimiter in [@double_heredoc, @single_heredoc] do
closing_delimiter = concat(opening_delimiter, List.to_string(modifiers))
{doc, state} =
entries
|> prepend_heredoc_line()
|> interpolation_to_algebra(opening_delimiter, state, doc, closing_delimiter)
|> interpolation_to_algebra(:heredoc, state, doc, closing_delimiter)
{force_unfit(doc), state}
else
@@ -1558,13 +1611,11 @@ defmodule Code.Formatter do
end
end
defp escape_heredoc(string, escape) do
string = String.replace(string, escape, "\\" <> escape)
defp escape_heredoc(string) do
heredoc_to_algebra(["" | String.split(string, "\n")])
end
defp escape_string(string, <<_, _, _>> = escape) do
string = String.replace(string, escape, "\\" <> escape)
defp escape_string(string, :heredoc) do
heredoc_to_algebra(String.split(string, "\n"))
end
@@ -1869,7 +1920,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra_with_comments(args, acc, min_line, max_line, state, fun) do
{pre_comments, state} =
get_and_update_in(state.comments, fn comments ->
Enum.split_while(comments, fn %{line: line} -> line <= min_line end)
Enum.split_while(comments, fn {line, _, _} -> line <= min_line end)
end)
{docs, comments?, state} =
@@ -1885,29 +1936,23 @@ defmodule Code.Formatter do
end
defp each_quoted_to_algebra_with_comments([arg | args], acc, max_line, state, comments?, fun) do
case traverse_line(arg, {@max_line, @min_line}) do
{@max_line, @min_line} ->
{doc_triplet, state} = fun.(arg, args, state)
acc = [doc_triplet | acc]
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
{doc_start, doc_end} = traverse_line(arg, {@max_line, @min_line})
{doc_start, doc_end} ->
{acc, comments, comments?} =
extract_comments_before(doc_start, acc, state.comments, comments?)
{acc, comments, comments?} =
extract_comments_before(doc_start, acc, state.comments, comments?)
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
{acc, comments, comments?} =
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
{acc, comments, comments?} =
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
state = %{state | comments: comments}
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
end
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
state = %{state | comments: comments}
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
end
defp extract_comments_before(max, acc, [%{line: line} = comment | rest], _) when line < max do
%{previous_eol_count: previous, next_eol_count: next, text: doc} = comment
defp extract_comments_before(max, acc, [{line, _, _} = comment | rest], _) when line < max do
{_, {previous, next}, doc} = comment
acc = [{doc, @empty, next} | add_previous_to_acc(acc, previous)]
extract_comments_before(max, acc, rest, true)
end
@@ -1922,7 +1967,7 @@ defmodule Code.Formatter do
defp add_previous_to_acc(acc, _previous),
do: acc
defp extract_comments_trailing(min, max, acc, [%{line: line, text: doc_comment} | rest], _)
defp extract_comments_trailing(min, max, acc, [{line, _, doc_comment} | rest], _)
when line >= min and line <= max do
acc = [{doc_comment, @empty, 1} | acc]
extract_comments_trailing(min, max, acc, rest, true)
@@ -1934,7 +1979,7 @@ defmodule Code.Formatter do
# If the document is immediately followed by comment which is followed by newlines,
# its newlines wouldn't have considered the comment, so we need to adjust it.
defp adjust_trailing_newlines({doc, next_line, newlines}, doc_end, [%{line: line} | _])
defp adjust_trailing_newlines({doc, next_line, newlines}, doc_end, [{line, _, _} | _])
when newlines > 1 and line == doc_end + 1 do
{doc, next_line, 1}
end
@@ -2141,7 +2186,7 @@ defmodule Code.Formatter do
(
min_line = line(meta)
max_line = closing_line(meta)
Enum.any?(comments, fn %{line: line} -> line > min_line and line < max_line end)
Enum.any?(comments, fn {line, _, _} -> line > min_line and line < max_line end)
)
end
@@ -2166,8 +2211,8 @@ defmodule Code.Formatter do
{{_, arg_meta, _}, _} = hd(arg)
first_line = line(arg_meta)
case Enum.drop_while(comments, fn %{line: line} -> line <= block_line end) do
[%{line: line} | _] when line <= first_line ->
case Enum.drop_while(comments, fn {line, _, _} -> line <= block_line end) do
[{line, _, _} | _] when line <= first_line ->
{false, block}
_ ->
@@ -2185,7 +2230,7 @@ defmodule Code.Formatter do
end
defp force_args?(args) do
match?([_ | _], args) and force_args?(args, %{})
match?([_, _ | _], args) and force_args?(args, MapSet.new())
end
defp force_args?([[arg | _] | args], lines) do
@@ -2195,19 +2240,18 @@ defmodule Code.Formatter do
defp force_args?([arg | args], lines) do
line =
case arg do
{{_, meta, _}, _} -> meta[:line]
{_, meta, _} -> meta[:line]
{{_, meta, _}, _} -> line(meta)
{_, meta, _} -> line(meta)
end
cond do
# Line may be missing from non-formatter AST
is_nil(line) -> force_args?(args, lines)
Map.has_key?(lines, line) -> false
true -> force_args?(args, Map.put(lines, line, true))
if MapSet.member?(lines, line) do
false
else
force_args?(args, MapSet.put(lines, line))
end
end
defp force_args?([], lines), do: map_size(lines) >= 2
defp force_args?([], _lines), do: true
defp force_keyword(doc, arg) do
if force_args?(arg), do: force_unfit(doc), else: doc
-841
View File
@@ -1,841 +0,0 @@
defmodule Code.Fragment do
@moduledoc """
This module provides conveniences for analyzing fragments of
textual code and extract available information whenever possible.
Most of the functions in this module provide a best-effort
and may not be accurate under all circumstances. Read each
documentation for more information.
This module should be considered experimental.
"""
@type position :: {line :: pos_integer(), column :: pos_integer()}
@doc """
Receives a string and returns the cursor context.
This function receives a string with an Elixir code fragment,
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 all 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.Fragment.cursor_context("")
:expr
iex> Code.Fragment.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)
arity, 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`
* `{:operator, charlist}` - the context is an operator, such as `+` or
`==`. Note textual operators, such as `when` do not appear as operators
but rather as `:local_or_var`. `@` is never an `:operator` and always a
`:module_attribute`
* `{:operator_arity, charlist}` - the context is an operator arity, which
is an operator followed by /, such as `+/`, `not/` or `when/`
* `{:operator_call, charlist}` - the context is an operator call, which is
an operator followed by space, such as `left + `, `not ` or `x when `
* `:none` - no context possible
* `{:sigil, charlist}` - the context is a sigil. It may be either the beginning
of a sigil, such as `~` or `~s`, or an operator starting with `~`, such as
`~>` and `~>>`
* `{:struct, charlist}` - the context is a struct, such as `%`, `%UR` or `%URI`
* `{:unquoted_atom, charlist}` - the context is an unquoted atom. This
can be any atom or an atom representing a module
## Limitations
The current algorithm only considers the last line of the input. This means
it will also show suggestions inside strings, heredocs, etc, which is
intentional as it helps with doctests, references, and more.
"""
@doc since: "1.13.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}
| {:operator, charlist}
| {:operator_arity, charlist}
| {:operator_call, charlist}
| :none
| {:sigil, charlist}
| {:struct, charlist}
| {:unquoted_atom, charlist}
when inside_dot:
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
def cursor_context(fragment, 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
binary
|> String.to_charlist()
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
end
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
charlist =
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
[?\n | _] -> []
rest -> rest
end
charlist
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
end
def cursor_context(other, opts) when is_list(opts) do
cursor_context(to_charlist(other), opts)
end
@operators '\\<>+-*/:=|&~^%!'
@starter_punctuation ',([{;'
@non_starter_punctuation ')]}"\'.$'
@space '\t\s'
@trailing_identifier '?!'
@tilde_op_prefix '<=~'
@non_identifier @trailing_identifier ++
@operators ++ @starter_punctuation ++ @non_starter_punctuation ++ @space
@textual_operators ~w(when not and or in)c
@incomplete_operators ~w(^^ ~~ ~)c
defp codepoint_cursor_context(reverse, _opts) do
{stripped, spaces} = strip_spaces(reverse, 0)
case stripped do
# It is empty
[] -> {:expr, 0}
# Structs
[?%, ?:, ?: | _] -> {{:struct, ''}, 1}
[?%, ?: | _] -> {{:unquoted_atom, '%'}, 2}
[?% | _] -> {{:struct, ''}, 1}
# Token/AST only operators
[?>, ?= | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
[?>, ?- | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
# Two-digit containers
[?<, ?< | rest] when rest == [] or hd(rest) != ?< -> {:expr, 0}
# Ambiguity around :
[?: | rest] when rest == [] or hd(rest) != ?: -> unquoted_atom_or_expr(spaces)
# Dots
[?.] -> {:none, 0}
[?. | rest] when hd(rest) not in '.:' -> dot(rest, spaces + 1, '')
# It is a local or remote call with parens
[?( | rest] -> call_to_cursor_context(strip_spaces(rest, spaces + 1))
# A local arity definition
[?/ | rest] -> arity_to_cursor_context(strip_spaces(rest, spaces + 1))
# Starting a new expression
[h | _] when h in @starter_punctuation -> {:expr, 0}
# It is a local or remote call without parens
rest when spaces > 0 -> call_to_cursor_context({rest, spaces})
# It is an identifier
_ -> identifier_to_cursor_context(reverse, 0, false)
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 unquoted_atom_or_expr(0), do: {{:unquoted_atom, ''}, 1}
defp unquoted_atom_or_expr(_), do: {:expr, 0}
defp arity_to_cursor_context({reverse, spaces}) do
case identifier_to_cursor_context(reverse, spaces, true) do
{{:local_or_var, acc}, count} -> {{:local_arity, acc}, count}
{{:dot, base, acc}, count} -> {{:dot_arity, base, acc}, count}
{{:operator, acc}, count} -> {{:operator_arity, acc}, count}
{_, _} -> {:none, 0}
end
end
defp call_to_cursor_context({reverse, spaces}) do
case identifier_to_cursor_context(reverse, spaces, true) do
{{:local_or_var, acc}, count} -> {{:local_call, acc}, count}
{{:dot, base, acc}, count} -> {{:dot_call, base, acc}, count}
{{:operator, acc}, count} -> {{:operator_call, acc}, count}
{_, _} -> {:none, 0}
end
end
defp identifier_to_cursor_context([?., ?., ?: | _], n, _), do: {{:unquoted_atom, '..'}, n + 3}
defp identifier_to_cursor_context([?., ?., ?. | _], n, _), do: {{:local_or_var, '...'}, n + 3}
defp identifier_to_cursor_context([?., ?: | _], n, _), do: {{:unquoted_atom, '.'}, n + 2}
defp identifier_to_cursor_context([?., ?. | _], n, _), do: {{:operator, '..'}, n + 2}
defp identifier_to_cursor_context(reverse, count, call_op?) do
case identifier(reverse, count) do
:none ->
{:none, 0}
:operator ->
operator(reverse, count, [], call_op?)
{:module_attribute, acc, count} ->
{{:module_attribute, acc}, count}
{:sigil, acc, count} ->
{{:sigil, acc}, count}
{:unquoted_atom, acc, count} ->
{{:unquoted_atom, acc}, count}
{:alias, rest, acc, count} ->
case strip_spaces(rest, count) do
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
nested_alias(rest, count + 1, acc)
{'%' ++ _, count} ->
{{:struct, acc}, count + 1}
_ ->
{{:alias, acc}, count}
end
{:identifier, _, acc, count} when call_op? and acc in @textual_operators ->
{{:operator, acc}, count}
{:identifier, rest, acc, count} ->
case strip_spaces(rest, count) do
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
dot(rest, count + 1, acc)
_ ->
{{:local_or_var, acc}, count}
end
end
end
defp identifier([?? | rest], count), do: check_identifier(rest, count + 1, [??])
defp identifier([?! | rest], count), do: check_identifier(rest, count + 1, [?!])
defp identifier(rest, count), do: check_identifier(rest, count, [])
defp check_identifier([h | t], count, acc) when h not in @non_identifier,
do: rest_identifier(t, count + 1, [h | acc])
defp check_identifier(_, _, _), do: :operator
defp rest_identifier([h | rest], count, acc) when h not in @non_identifier do
rest_identifier(rest, count + 1, [h | acc])
end
defp rest_identifier(rest, count, [?@ | acc]) do
case tokenize_identifier(rest, count, acc) do
{:identifier, _rest, acc, count} -> {:module_attribute, acc, count}
:none when acc == [] -> {:module_attribute, '', count}
_ -> :none
end
end
defp rest_identifier([?~ | rest], count, [letter])
when (letter in ?A..?Z or letter in ?a..?z) and
(rest == [] or hd(rest) not in @tilde_op_prefix) do
{:sigil, [letter], count + 1}
end
defp rest_identifier([?: | rest], count, acc) when rest == [] or hd(rest) != ?: do
case String.Tokenizer.tokenize(acc) do
{_, _, [], _, _, _} -> {:unquoted_atom, acc, count + 1}
_ -> :none
end
end
defp rest_identifier([?? | _], _count, _acc) do
:none
end
defp rest_identifier(rest, count, acc) do
tokenize_identifier(rest, count, acc)
end
defp tokenize_identifier(rest, count, acc) do
case String.Tokenizer.tokenize(acc) do
# Not actually an atom cause rest is not a :
{:atom, _, _, _, _, _} ->
:none
# Aliases must be ascii only
{:alias, _, _, _, false, _} ->
:none
{kind, _, [], _, _, extra} ->
if ?@ in extra do
:none
else
{kind, rest, acc, count}
end
_ ->
:none
end
end
defp nested_alias(rest, count, acc) do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:struct, prev}, count} -> {{:struct, prev ++ '.' ++ acc}, count}
{{:alias, prev}, count} -> {{:alias, prev ++ '.' ++ acc}, count}
_ -> {:none, 0}
end
end
defp dot(rest, count, acc) do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:local_or_var, var}, count} -> {{:dot, {:var, var}, acc}, count}
{{:unquoted_atom, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:alias, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:dot, _, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:module_attribute, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:struct, acc}, count} -> {{:struct, acc ++ '.'}, count}
{_, _} -> {:none, 0}
end
end
defp operator([h | rest], count, acc, call_op?) when h in @operators do
operator(rest, count + 1, [h | acc], call_op?)
end
defp operator(rest, count, acc, call_op?) when acc in @incomplete_operators do
{rest, dot_count} = strip_spaces(rest, count)
cond do
call_op? ->
{:none, 0}
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
dot(tl(rest), dot_count + 1, acc)
acc == '~' ->
{{:sigil, ''}, count}
true ->
{{:operator, acc}, count}
end
end
# If we are opening a sigil, ignore the operator.
defp operator([letter, ?~ | rest], _count, [op], _call_op?)
when op in '<|/' and (letter in ?A..?Z or letter in ?a..?z) and
(rest == [] or hd(rest) not in @tilde_op_prefix) do
{:none, 0}
end
defp operator(rest, count, acc, _call_op?) do
case :elixir_tokenizer.tokenize(acc, 1, 1, []) do
{:ok, _, _, _, [{:atom, _, _}]} ->
{{:unquoted_atom, tl(acc)}, count}
{:ok, _, _, _, [{_, _, op}]} ->
{rest, dot_count} = strip_spaces(rest, count)
cond do
Code.Identifier.unary_op(op) == :error and Code.Identifier.binary_op(op) == :error ->
:none
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
dot(tl(rest), dot_count + 1, acc)
true ->
{{:operator, acc}, count}
end
_ ->
{:none, 0}
end
end
@doc """
Receives a string and returns the surround context.
This function receives a string with an Elixir code fragment
and a `position`. It returns a map containing the beginning
and ending of the identifier alongside its context, or `:none`
if there is nothing with a known context.
The difference between `cursor_context/2` and `surround_context/3`
is that the former assumes the expression in the code fragment
is incomplete. For example, `do` in `cursor_context/2` may be
a keyword or a variable or a local call, while `surround_context/3`
assumes the expression in the code fragment is complete, therefore
`do` would always be a keyword.
The `position` contains both the `line` and `column`, both starting
with the index of 1. The column must precede the surrounding expression.
For example, the expression `foo`, will return something for the columns
1, 2, and 3, but not 4:
foo
^ column 1
foo
^ column 2
foo
^ column 3
foo
^ column 4
The returned map contains the column the expression starts and the
first column after the expression ends.
Similar to `cursor_context/2`, this function also provides a best-effort
detection and may not be accurate under all circumstances. See the
"Return values" and "Limitations" section under `cursor_context/2` for
more information.
## Examples
iex> Code.Fragment.surround_context("foo", {1, 1})
%{begin: {1, 1}, context: {:local_or_var, 'foo'}, end: {1, 4}}
## Differences to `cursor_context/2`
Because `surround_context/3` deals with complete code, it has some
difference to `cursor_context/2`:
* `dot_call`/`dot_arity` and `operator_call`/`operator_arity`
are collapsed into `dot` and `operator` contexts respectively
as there aren't any meaningful distinctions between them
* On the other hand, this function still makes a distinction between
`local_call`/`local_arity` and `local_or_var`, since the latter can
be a local or variable
* `@` when not followed by any identifier is returned as `{:operator, '@'}`
(in contrast to `{:module_attribute, ''}` in `cursor_context/2`
* This function never returns empty sigils `{:sigil, ''}` or empty structs
`{:struct, ''}` as context
"""
@doc since: "1.13.0"
@spec surround_context(List.Chars.t(), position(), keyword()) ::
%{begin: position, end: position, context: context} | :none
when context:
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:local_or_var, charlist}
| {:local_arity, charlist}
| {:local_call, charlist}
| {:module_attribute, charlist}
| {:operator, charlist}
| {:unquoted_atom, charlist},
inside_dot:
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
def surround_context(fragment, position, options \\ [])
def surround_context(binary, {line, column}, opts) when is_binary(binary) do
binary
|> String.split("\n")
|> Enum.at(line - 1, '')
|> String.to_charlist()
|> position_surround_context(line, column, opts)
end
def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
charlist
|> :string.split('\n', :all)
|> Enum.at(line - 1, '')
|> position_surround_context(line, column, opts)
end
def surround_context(other, {_, _} = position, opts) do
surround_context(to_charlist(other), position, opts)
end
defp position_surround_context(charlist, line, column, opts)
when is_integer(line) and line >= 1 and is_integer(column) and column >= 1 do
{reversed_pre, post} = string_reverse_at(charlist, column - 1, [])
{reversed_pre, post} = adjust_position(reversed_pre, post)
case take_identifier(post, []) do
{_, [], _} ->
maybe_operator(reversed_pre, post, line, opts)
{:identifier, reversed_post, rest} ->
{rest, _} = strip_spaces(rest, 0)
reversed = reversed_post ++ reversed_pre
case codepoint_cursor_context(reversed, opts) do
{{:struct, acc}, offset} ->
build_surround({:struct, acc}, reversed, line, offset)
{{:alias, acc}, offset} ->
build_surround({:alias, acc}, reversed, line, offset)
{{:dot, _, [_ | _]} = dot, offset} ->
build_surround(dot, reversed, line, offset)
{{:local_or_var, acc}, offset} when hd(rest) == ?( ->
build_surround({:local_call, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} when hd(rest) == ?/ ->
build_surround({:local_arity, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} when acc in @textual_operators ->
build_surround({:operator, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} when acc not in ~w(do end after else catch rescue)c ->
build_surround({:local_or_var, acc}, reversed, line, offset)
{{:module_attribute, ''}, offset} ->
build_surround({:operator, '@'}, reversed, line, offset)
{{:module_attribute, acc}, offset} ->
build_surround({:module_attribute, acc}, reversed, line, offset)
{{:sigil, acc}, offset} ->
build_surround({:sigil, acc}, reversed, line, offset)
{{:unquoted_atom, acc}, offset} ->
build_surround({:unquoted_atom, acc}, reversed, line, offset)
_ ->
maybe_operator(reversed_pre, post, line, opts)
end
{:alias, reversed_post, _rest} ->
reversed = reversed_post ++ reversed_pre
case codepoint_cursor_context(reversed, opts) do
{{:alias, acc}, offset} ->
build_surround({:alias, acc}, reversed, line, offset)
{{:struct, acc}, offset} ->
build_surround({:struct, acc}, reversed, line, offset)
_ ->
:none
end
end
end
defp maybe_operator(reversed_pre, post, line, opts) do
case take_operator(post, []) do
{[], _rest} ->
:none
{reversed_post, rest} ->
reversed = reversed_post ++ reversed_pre
case codepoint_cursor_context(reversed, opts) do
{{:operator, acc}, offset} when acc not in @incomplete_operators ->
build_surround({:operator, acc}, reversed, line, offset)
{{:sigil, ''}, offset} when hd(rest) in ?A..?Z or hd(rest) in ?a..?z ->
build_surround({:sigil, [hd(rest)]}, [hd(rest) | reversed], line, offset + 1)
{{:dot, _, [_ | _]} = dot, offset} ->
build_surround(dot, reversed, line, offset)
_ ->
:none
end
end
end
defp build_surround(context, reversed, line, offset) do
{post, reversed_pre} = enum_reverse_at(reversed, offset, [])
pre = :lists.reverse(reversed_pre)
pre_length = :string.length(pre) + 1
%{
context: context,
begin: {line, pre_length},
end: {line, pre_length + :string.length(post)}
}
end
defp take_identifier([h | t], acc) when h in @trailing_identifier,
do: {:identifier, [h | acc], t}
defp take_identifier([h | t], acc) when h not in @non_identifier,
do: take_identifier(t, [h | acc])
defp take_identifier(rest, acc) do
with {[?. | t], _} <- strip_spaces(rest, 0),
{[h | _], _} when h in ?A..?Z <- strip_spaces(t, 0) do
take_alias(rest, acc)
else
_ -> {:identifier, acc, rest}
end
end
defp take_alias([h | t], acc) when h not in @non_identifier,
do: take_alias(t, [h | acc])
defp take_alias(rest, acc) do
with {[?. | t], acc} <- move_spaces(rest, acc),
{[h | t], acc} when h in ?A..?Z <- move_spaces(t, [?. | acc]) do
take_alias(t, [h | acc])
else
_ -> {:alias, acc, rest}
end
end
defp take_operator([h | t], acc) when h in @operators, do: take_operator(t, [h | acc])
defp take_operator([h | t], acc) when h == ?., do: take_operator(t, [h | acc])
defp take_operator(rest, acc), do: {acc, rest}
# Unquoted atom handling
defp adjust_position(reversed_pre, [?: | post])
when hd(post) != ?: and (reversed_pre == [] or hd(reversed_pre) != ?:) do
{[?: | reversed_pre], post}
end
defp adjust_position(reversed_pre, [?% | post]) do
adjust_position([?% | reversed_pre], post)
end
# Dot/struct handling
defp adjust_position(reversed_pre, post) do
case move_spaces(post, reversed_pre) do
# If we are between spaces and a dot, move past the dot
{[?. | post], reversed_pre} when hd(post) != ?. and hd(reversed_pre) != ?. ->
{post, reversed_pre} = move_spaces(post, [?. | reversed_pre])
{reversed_pre, post}
_ ->
case strip_spaces(reversed_pre, 0) do
# If there is a dot to our left, make sure to move to the first character
{[?. | rest], _} when rest == [] or hd(rest) not in '.:' ->
{post, reversed_pre} = move_spaces(post, reversed_pre)
{reversed_pre, post}
# If there is a % to our left, make sure to move to the first character
{[?% | _], _} ->
case move_spaces(post, reversed_pre) do
{[h | _] = post, reversed_pre} when h in ?A..?Z ->
{reversed_pre, post}
_ ->
{reversed_pre, post}
end
_ ->
{reversed_pre, post}
end
end
end
defp move_spaces([h | t], acc) when h in @space, do: move_spaces(t, [h | acc])
defp move_spaces(t, acc), do: {t, acc}
defp string_reverse_at(charlist, 0, acc), do: {acc, charlist}
defp string_reverse_at(charlist, n, acc) do
case :unicode_util.gc(charlist) do
[gc | cont] when is_integer(gc) -> string_reverse_at(cont, n - 1, [gc | acc])
[gc | cont] when is_list(gc) -> string_reverse_at(cont, n - 1, :lists.reverse(gc, acc))
[] -> {acc, []}
end
end
defp enum_reverse_at([h | t], n, acc) when n > 0, do: enum_reverse_at(t, n - 1, [h | acc])
defp enum_reverse_at(rest, _, acc), do: {acc, rest}
@doc """
Receives a code fragment and returns a quoted expression
with a cursor at the nearest argument position.
A container is any Elixir expression starting with `(`,
`{`, and `[`. This includes function calls, tuples, lists,
maps, and so on. For example, take this code, which would
be given as input:
max(some_value,
This function will return the AST equivalent to:
max(some_value, __cursor__())
In other words, this function is capable of closing any open
brackets and insert the cursor position. Any content at the
cursor position that is after a comma or an opening bracket
is discarded. For example, if this is given as input:
max(some_value, another_val
It will return the same AST:
max(some_value, __cursor__())
Similarly, if only this is given:
max(some_va
Then it returns:
max(__cursor__())
Calls without parenthesis are also supported, as we assume the
brackets are implicit.
Operators and anonymous functions are not containers, and therefore
will be discarded. The following will all return the same AST:
max(some_value,
max(some_value, fn x -> x end
max(some_value, 1 + another_val
max(some_value, 1 |> some_fun() |> another_fun
On the other hand, tuples, lists, maps, etc all retain the
cursor position:
max(some_value, [1, 2,
Returns the following AST:
max(some_value, [1, 2, __cursor__()])
Keyword lists (and do-end blocks) are also retained. The following:
if(some_value, do:
if(some_value, do: :token
if(some_value, do: 1 + val
all return:
if(some_value, do: __cursor__())
The AST returned by this function is not safe to evaluate but
it can be analyzed and expanded.
## Examples
iex> Code.Fragment.container_cursor_to_quoted("max(some_value, ")
{:ok, {:max, [line: 1], [{:some_value, [line: 1], nil}, {:__cursor__, [line: 1], []}]}}
## Options
* `:file` - the filename to be reported in case of parsing errors.
Defaults to `"nofile"`.
* `:line` - the starting line of the string being parsed.
Defaults to 1.
* `:column` - the starting column of the string being parsed.
Defaults to 1.
* `:columns` - when `true`, attach a `:column` key to the quoted
metadata. Defaults to `false`.
* `:token_metadata` - when `true`, includes token-related
metadata in the expression AST, such as metadata for `do` and `end`
tokens, for closing tokens, end of expressions, as well as delimiters
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
"""
@doc since: "1.13.0"
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
def container_cursor_to_quoted(fragment, opts \\ []) do
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
column = Keyword.get(opts, :column, 1)
columns = Keyword.get(opts, :columns, false)
token_metadata = Keyword.get(opts, :token_metadata, false)
fragment = to_charlist(fragment)
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
{:ok, _, _, _warnings, tokens} ->
:elixir.tokens_to_quoted(tokens, nil, columns: columns, token_metadata: token_metadata)
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, {to_string(prefix), to_string(suffix)}, to_string(token)}}
{:error, {line, column, error, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, to_string(error), to_string(token)}}
end
end
end
+1 -2
View File
@@ -48,7 +48,6 @@ defmodule Code.Identifier do
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
op in [:**] -> {:left, 230}
op in [:.] -> {:left, 310}
true -> :error
end
@@ -97,7 +96,7 @@ defmodule Code.Identifier do
:alias
true ->
case :elixir_config.identifier_tokenizer().tokenize(charlist) do
case :elixir_config.get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
{kind, _acc, [], _, _, special} ->
if kind == :identifier and not :lists.member(?@, special) do
:callable_local
-585
View File
@@ -1,585 +0,0 @@
defmodule Code.Normalizer do
@moduledoc false
defguard is_literal(x)
when is_integer(x) or
is_float(x) or
is_binary(x) or
is_atom(x)
@doc """
Wraps literals in the quoted expression to conform to the AST format expected
by the formatter.
"""
def normalize(quoted, opts \\ []) do
line = Keyword.get(opts, :line, nil)
escape = Keyword.get(opts, :escape, true)
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
state = %{
escape: escape,
parent_meta: [line: line],
locals_without_parens: locals_without_parens ++ Code.Formatter.locals_without_parens()
}
do_normalize(quoted, state)
end
# Wrapped literals should receive the block meta
defp do_normalize({:__block__, meta, [literal]}, state)
when not is_tuple(literal) or tuple_size(literal) == 2 do
normalize_literal(literal, meta, state)
end
# Only normalize the first argument of an alias if it's not an atom
defp do_normalize({:__aliases__, meta, [first | rest]}, state) when not is_atom(first) do
meta = patch_meta_line(meta, state.parent_meta)
first = do_normalize(first, %{state | parent_meta: meta})
{:__aliases__, meta, [first | rest]}
end
defp do_normalize({:__aliases__, _, _} = quoted, _state) do
quoted
end
# Skip captured arguments like &1
defp do_normalize({:&, meta, [term]}, state) when is_integer(term) do
meta = patch_meta_line(meta, state.parent_meta)
{:&, meta, [term]}
end
# Ranges
defp do_normalize(left..right//step, state) do
left = do_normalize(left, state)
right = do_normalize(right, state)
meta = meta_line(state)
if step == 1 do
{:.., meta, [left, right]}
else
step = do_normalize(step, state)
{:"..//", meta, [left, right, step]}
end
end
# Bit containers
defp do_normalize({:<<>>, _, args} = quoted, state) when is_list(args) do
normalize_bitstring(quoted, state)
end
# Atoms with interpolations
defp do_normalize(
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
[{:<<>>, _, args} = string, :utf8]},
state
)
when is_list(args) do
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
call_meta = patch_meta_line(call_meta, dot_meta)
string =
if state.escape do
normalize_bitstring(string, state, true)
else
normalize_bitstring(string, state)
end
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, :utf8]}
end
# Charlists with interpolations
defp do_normalize({{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}, state) do
parts =
Enum.map(parts, fn
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args} ->
args = normalize_args(args, state)
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args}
part ->
if state.escape do
maybe_escape_literal(part, state)
else
part
end
end)
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
end
# Don't normalize the `Access` atom in access syntax
defp do_normalize({:., meta, [Access, :get]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
{:., meta, [Access, :get]}
end
# Only normalize the left side of the dot operator
# The right hand side is an atom in the AST but it's not an atom literal, so
# it should not be wrapped
defp do_normalize({:., meta, [left, right]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
left = do_normalize(left, %{state | parent_meta: meta})
{:., meta, [left, right]}
end
# A list of left to right arrows is not considered as a list literal, so it's not wrapped
defp do_normalize([{:->, _, [_ | _]} | _] = quoted, state) do
normalize_args(quoted, state)
end
# left -> right
defp do_normalize({:->, meta, [left, right]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
left = normalize_args(left, %{state | parent_meta: meta})
right = do_normalize(right, %{state | parent_meta: meta})
{:->, meta, [left, right]}
end
# Maps
defp do_normalize({:%{}, meta, args}, state) when is_list(args) do
meta =
if meta == [] do
line = state.parent_meta[:line]
[line: line, closing: [line: line]]
else
meta
end
state = %{state | parent_meta: meta}
args =
case args do
[{:|, pipe_meta, [left, right]}] ->
left = do_normalize(left, state)
right = normalize_map_args(right, state)
[{:|, pipe_meta, [left, right]}]
[{_, _, _} = call] ->
[do_normalize(call, state)]
args ->
normalize_map_args(args, state)
end
{:%{}, meta, args}
end
# Sigils
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
when is_list(args) and is_atom(sigil) do
case Atom.to_string(sigil) do
<<"sigil_", _name>> ->
meta =
meta
|> patch_meta_line(state.parent_meta)
|> Keyword.put_new(:delimiter, "\"")
{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
_ ->
normalize_call(quoted, state)
end
end
# Tuples
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
{last_arg, args} = List.pop_at(args, -1)
if args != [] and match?([_ | _], last_arg) and keyword?(last_arg) do
args = normalize_args(args, state)
kw_list = normalize_kw_args(last_arg, state, true)
{:{}, meta, args ++ kw_list}
else
normalize_call(quoted, state)
end
end
# Module attributes
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
value =
cond do
keyword?(value) ->
normalize_kw_args(value, state, true)
is_list(value) ->
normalize_literal(value, meta, state)
true ->
do_normalize(value, state)
end
{:@, meta, [{name, name_meta, [value]}]}
end
# Regular blocks
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
{:__block__, meta, normalize_args(args, state)}
end
# Calls
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
normalize_call(quoted, state)
end
# Vars
defp do_normalize({_, _, context} = quoted, _state) when is_atom(context) do
quoted
end
# Literals
defp do_normalize(quoted, state) do
normalize_literal(quoted, [], state)
end
# Numbers
defp normalize_literal(number, meta, state) when is_number(number) do
meta =
meta
|> Keyword.put_new(:token, inspect(number))
|> patch_meta_line(state.parent_meta)
{:__block__, meta, [number]}
end
# Atom, Strings
defp normalize_literal(literal, meta, state) when is_atom(literal) or is_binary(literal) do
meta = patch_meta_line(meta, state.parent_meta)
literal = maybe_escape_literal(literal, state)
if is_atom(literal) and Code.Identifier.classify(literal) == :alias and
is_nil(meta[:delimiter]) do
segments =
case Atom.to_string(literal) do
"Elixir" ->
[:"Elixir"]
"Elixir." <> segments ->
segments
|> String.split(".")
|> Enum.map(&String.to_atom/1)
end
{:__aliases__, meta, segments}
else
{:__block__, meta, [literal]}
end
end
# 2-tuples
defp normalize_literal({left, right}, meta, state) do
meta = patch_meta_line(meta, state.parent_meta)
state = %{state | parent_meta: meta}
if match?([_ | _], right) and keyword?(right) do
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state, true)}]}
else
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
end
end
# Lists
defp normalize_literal(list, meta, state) when is_list(list) do
if list != [] and List.ascii_printable?(list) do
# It's a charlist
list =
if state.escape do
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), -1)
IO.iodata_to_binary(string) |> to_charlist()
else
list
end
meta =
meta
|> Keyword.put_new(:delimiter, "'")
|> patch_meta_line(state.parent_meta)
{:__block__, meta, [list]}
else
meta =
if line = state.parent_meta[:line] do
meta
|> Keyword.put_new(:closing, line: line)
|> patch_meta_line(state.parent_meta)
else
meta
end
{:__block__, meta, [normalize_kw_args(list, state, false)]}
end
end
# Probably an invalid value, wrap it and send it upstream
defp normalize_literal(quoted, meta, _state) do
{:__block__, meta, [quoted]}
end
defp normalize_call({form, meta, args}, state) do
meta = patch_meta_line(meta, state.parent_meta)
arity = length(args)
# Only normalize the form if it's a qualified call
form =
if is_atom(form) do
form
else
do_normalize(form, %{state | parent_meta: meta})
end
meta =
if is_nil(meta[:no_parens]) and is_nil(meta[:closing]) and is_nil(meta[:do]) and
not Code.Formatter.local_without_parens?(form, arity, state.locals_without_parens) do
[closing: [line: meta[:line]]] ++ meta
else
meta
end
cond do
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], List.last(args)) ->
# def foo do :ok end
# def foo, do: :ok
normalize_kw_blocks(form, meta, args, state)
match?([{:do, _} | _], List.last(args)) ->
# Non normalized kw blocks
line = state.parent_meta[:line]
meta = meta ++ [do: [line: line], end: [line: line]]
normalize_kw_blocks(form, meta, args, state)
allow_keyword?(form, arity) ->
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
{last_arg, leading_args} = List.pop_at(args, -1, [])
last_args =
case last_arg do
{:__block__, _, [[{{:__block__, key_meta, _}, _} | _]] = last_args} ->
if key_meta[:format] == :keyword do
last_args
else
[last_arg]
end
[] ->
[]
_ ->
[last_arg]
end
{form, meta, leading_args ++ last_args}
true ->
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
{form, meta, args}
end
end
defp allow_keyword?(:when, 2), do: true
defp allow_keyword?(:{}, _), do: false
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
defp normalize_bitstring({:<<>>, meta, parts} = quoted, state, escape_interpolation \\ false) do
meta = patch_meta_line(meta, state.parent_meta)
parts =
if interpolated?(quoted) do
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
else
state = %{state | parent_meta: meta}
Enum.map(parts, fn part ->
with {:"::", meta, [left, _]} <- part,
true <- meta[:inferred_bitstring_spec] do
do_normalize(left, state)
else
_ -> do_normalize(part, state)
end
end)
end
{:<<>>, meta, parts}
end
defp normalize_interpolation_parts(parts, state, escape_interpolation) do
Enum.map(parts, fn
{:"::", interpolation_meta,
[
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
{:binary, binary_meta, context}
]} ->
middle = do_normalize(middle, %{state | parent_meta: dot_meta})
{:"::", interpolation_meta,
[
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
{:binary, binary_meta, context}
]}
part ->
if escape_interpolation do
maybe_escape_literal(part, state)
else
part
end
end)
end
defp normalize_map_args(args, state) do
Enum.map(normalize_kw_args(args, state, false), fn
{:__block__, _, [{_, _} = pair]} -> pair
pair -> pair
end)
end
defp normalize_kw_blocks(form, meta, args, state) do
{kw_blocks, leading_args} = List.pop_at(args, -1)
kw_blocks =
Enum.map(kw_blocks, fn {tag, block} ->
block = do_normalize(block, %{state | parent_meta: meta})
block =
case block do
{_, _, [[{:->, _, _} | _] = block]} -> block
block -> block
end
# Only wrap the tag if it isn't already wrapped
tag =
case tag do
{:__block__, _, _} -> tag
_ -> {:__block__, [line: meta[:line]], [tag]}
end
{tag, block}
end)
leading_args = normalize_args(leading_args, %{state | parent_meta: meta})
{form, meta, leading_args ++ [kw_blocks]}
end
defp normalize_kw_args(elems, state, keyword?)
defp normalize_kw_args(
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
state,
keyword?
)
when is_atom(key) do
keyword? = keyword? or keyword?(current)
first =
if key_meta[:format] == :keyword and not keyword? do
key_meta = Keyword.delete(key_meta, :format)
line = key_meta[:line] || meta_line(state)
{:__block__, [line: line], [{{:__block__, key_meta, [key]}, value}]}
else
first
end
[first | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([{left, right} | rest] = current, state, keyword?) do
keyword? = keyword? or keyword?(current)
left =
if keyword? do
meta = [format: :keyword] ++ meta_line(state)
{:__block__, meta, [maybe_escape_literal(left, state)]}
else
do_normalize(left, state)
end
right = do_normalize(right, state)
pair =
with {:__block__, meta, _} <- left,
:keyword <- meta[:format] do
{left, right}
else
_ -> {:__block__, meta_line(state), [{left, right}]}
end
[pair | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([first | rest], state, keyword?) do
[do_normalize(first, state) | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([], _state, _keyword?) do
[]
end
defp normalize_args(args, state) do
Enum.map(args, &do_normalize(&1, state))
end
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
{string, _} = Code.Identifier.escape(string, -1)
IO.iodata_to_binary(string)
end
defp maybe_escape_literal(atom, %{escape: true} = state) when is_atom(atom) do
atom
|> Atom.to_string()
|> maybe_escape_literal(state)
|> String.to_atom()
end
defp maybe_escape_literal(term, _) do
term
end
# Check if we have an interpolated string.
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
Enum.all?(parts, fn
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
binary when is_binary(binary) -> true
_ -> false
end)
end
defp interpolated?(_) do
false
end
defp patch_meta_line(meta, parent_meta) do
with nil <- meta[:line],
line when is_integer(line) <- parent_meta[:line] do
[line: line] ++ meta
else
_ -> meta
end
end
defp meta_line(state) do
if line = state.parent_meta[:line] do
[line: line]
else
[]
end
end
defp keyword?([{{:__block__, key_meta, [key]}, _} | rest]) when is_atom(key) do
if key_meta[:format] == :keyword do
keyword?(rest)
else
false
end
end
defp keyword?([{key, _value} | rest]) when is_atom(key) do
case Atom.to_charlist(key) do
'Elixir.' ++ _ -> false
_ -> keyword?(rest)
end
end
defp keyword?([]), do: true
defp keyword?(_other), do: false
end
+71 -75
View File
@@ -7,9 +7,9 @@ defmodule Code.Typespec do
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
def spec_to_quoted(name, spec)
def spec_to_quoted(name, {:type, anno, :fun, [{:type, _, :product, args}, result]})
def spec_to_quoted(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
when is_atom(name) do
meta = meta(anno)
meta = [line: line]
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
vars =
@@ -27,13 +27,11 @@ defmodule Code.Typespec do
end
end
def spec_to_quoted(name, {:type, anno, :fun, []}) when is_atom(name) do
meta = meta(anno)
{:"::", meta, [{name, meta, []}, quote(do: term)]}
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
{:"::", [line: line], [{name, [line: line], []}, quote(do: term)]}
end
def spec_to_quoted(name, {:type, anno, :bounded_fun, [type, constrs]}) when is_atom(name) do
meta = meta(anno)
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
guards =
@@ -41,6 +39,7 @@ defmodule Code.Typespec do
{erl_to_ex_var(var), typespec_to_quoted(type)}
end
meta = [line: line]
ignore_vars = Keyword.keys(guards)
vars =
@@ -53,7 +52,7 @@ defmodule Code.Typespec do
args = for arg <- args, do: typespec_to_quoted(arg)
when_args = [
{:"::", meta, [{name, meta, args}, typespec_to_quoted(result)]},
{:"::", meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
guards ++ vars
]
@@ -189,27 +188,27 @@ defmodule Code.Typespec do
## To AST conversion
defp collect_vars({:ann_type, _anno, args}) when is_list(args) do
defp collect_vars({:ann_type, _line, args}) when is_list(args) do
[]
end
defp collect_vars({:type, _anno, _kind, args}) when is_list(args) do
defp collect_vars({:type, _line, _kind, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:remote_type, _anno, args}) when is_list(args) do
defp collect_vars({:remote_type, _line, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:typed_record_field, _anno, type}) do
defp collect_vars({:typed_record_field, _line, type}) do
collect_vars(type)
end
defp collect_vars({:paren_type, _anno, [type]}) do
defp collect_vars({:paren_type, _line, [type]}) do
collect_vars(type)
end
defp collect_vars({:var, _anno, var}) do
defp collect_vars({:var, _line, var}) do
[erl_to_ex_var(var)]
end
@@ -217,48 +216,48 @@ defmodule Code.Typespec do
[]
end
defp typespec_to_quoted({:user_type, anno, name, args}) do
defp typespec_to_quoted({:user_type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, meta(anno), args}
{name, [line: line], args}
end
defp typespec_to_quoted({:type, anno, :tuple, :any}) do
{:tuple, meta(anno), []}
defp typespec_to_quoted({:type, line, :tuple, :any}) do
{:tuple, [line: line], []}
end
defp typespec_to_quoted({:type, anno, :tuple, args}) do
defp typespec_to_quoted({:type, line, :tuple, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{:{}, meta(anno), args}
{:{}, [line: line], args}
end
defp typespec_to_quoted({:type, _anno, :list, [{:type, _, :union, unions} = arg]}) do
defp typespec_to_quoted({:type, _line, :list, [{:type, _, :union, unions} = arg]}) do
case unpack_typespec_kw(unions, []) do
{:ok, ast} -> ast
:error -> [typespec_to_quoted(arg)]
end
end
defp typespec_to_quoted({:type, anno, :list, []}) do
{:list, meta(anno), []}
defp typespec_to_quoted({:type, line, :list, []}) do
{:list, [line: line], []}
end
defp typespec_to_quoted({:type, _anno, :list, [arg]}) do
defp typespec_to_quoted({:type, _line, :list, [arg]}) do
[typespec_to_quoted(arg)]
end
defp typespec_to_quoted({:type, anno, :nonempty_list, []}) do
[{:..., meta(anno), nil}]
defp typespec_to_quoted({:type, line, :nonempty_list, []}) do
[{:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, anno, :nonempty_list, [arg]}) do
[typespec_to_quoted(arg), {:..., meta(anno), nil}]
defp typespec_to_quoted({:type, line, :nonempty_list, [arg]}) do
[typespec_to_quoted(arg), {:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, anno, :map, :any}) do
{:map, meta(anno), []}
defp typespec_to_quoted({:type, line, :map, :any}) do
{:map, [line: line], []}
end
defp typespec_to_quoted({:type, anno, :map, fields}) do
defp typespec_to_quoted({:type, line, :map, fields}) do
fields =
Enum.map(fields, fn
{:type, _, :map_field_assoc, :any} ->
@@ -276,17 +275,16 @@ defmodule Code.Typespec do
case List.keytake(fields, :__struct__, 0) do
{{:__struct__, struct}, fields_pruned} when is_atom(struct) and struct != nil ->
map_pruned = {:%{}, meta(anno), fields_pruned}
{:%, meta(anno), [struct, map_pruned]}
map_pruned = {:%{}, [line: line], fields_pruned}
{:%, [line: line], [struct, map_pruned]}
_ ->
{:%{}, meta(anno), fields}
{:%{}, [line: line], fields}
end
end
defp typespec_to_quoted({:type, anno, :binary, [arg1, arg2]}) do
defp typespec_to_quoted({:type, line, :binary, [arg1, arg2]}) do
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
line = meta(anno)[:line]
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
{arg1, 0} ->
@@ -300,57 +298,57 @@ defmodule Code.Typespec do
end
end
defp typespec_to_quoted({:type, anno, :union, args}) do
defp typespec_to_quoted({:type, line, :union, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, meta(anno), [arg, expr]} end)
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, [line: line], [arg, expr]} end)
end
defp typespec_to_quoted({:type, anno, :fun, [{:type, _, :product, args}, result]}) do
defp typespec_to_quoted({:type, line, :fun, [{:type, _, :product, args}, result]}) do
args = for arg <- args, do: typespec_to_quoted(arg)
[{:->, meta(anno), [args, typespec_to_quoted(result)]}]
[{:->, [line: line], [args, typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, anno, :fun, [args, result]}) do
[{:->, meta(anno), [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
defp typespec_to_quoted({:type, line, :fun, [args, result]}) do
[{:->, [line: line], [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, anno, :fun, []}) do
typespec_to_quoted({:type, anno, :fun, [{:type, anno, :any}, {:type, anno, :any, []}]})
defp typespec_to_quoted({:type, line, :fun, []}) do
typespec_to_quoted({:type, line, :fun, [{:type, line, :any}, {:type, line, :any, []}]})
end
defp typespec_to_quoted({:type, anno, :range, [left, right]}) do
{:.., meta(anno), [typespec_to_quoted(left), typespec_to_quoted(right)]}
defp typespec_to_quoted({:type, line, :range, [left, right]}) do
{:.., [line: line], [typespec_to_quoted(left), typespec_to_quoted(right)]}
end
defp typespec_to_quoted({:type, _anno, nil, []}) do
defp typespec_to_quoted({:type, _line, nil, []}) do
[]
end
defp typespec_to_quoted({:type, anno, name, args}) do
defp typespec_to_quoted({:type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, meta(anno), args}
{name, [line: line], args}
end
defp typespec_to_quoted({:var, anno, var}) do
{erl_to_ex_var(var), meta(anno), nil}
defp typespec_to_quoted({:var, line, var}) do
{erl_to_ex_var(var), [line: line], nil}
end
defp typespec_to_quoted({:op, anno, op, arg}) do
{op, meta(anno), [typespec_to_quoted(arg)]}
defp typespec_to_quoted({:op, line, op, arg}) do
{op, [line: line], [typespec_to_quoted(arg)]}
end
defp typespec_to_quoted({:remote_type, anno, [mod, name, args]}) do
remote_type(anno, mod, name, args)
defp typespec_to_quoted({:remote_type, line, [mod, name, args]}) do
remote_type(line, mod, name, args)
end
defp typespec_to_quoted({:ann_type, anno, [var, type]}) do
{:"::", meta(anno), [typespec_to_quoted(var), typespec_to_quoted(type)]}
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
{:"::", [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
end
defp typespec_to_quoted(
{:typed_record_field, {:record_field, anno1, {:atom, anno2, name}}, type}
{:typed_record_field, {:record_field, line, {:atom, line1, name}}, type}
) do
typespec_to_quoted({:ann_type, anno1, [{:var, anno2, name}, type]})
typespec_to_quoted({:ann_type, line, [{:var, line1, name}, type]})
end
defp typespec_to_quoted({:type, _, :any}) do
@@ -361,7 +359,7 @@ defmodule Code.Typespec do
typespec_to_quoted(type)
end
defp typespec_to_quoted({type, _anno, atom}) when is_atom(type) do
defp typespec_to_quoted({type, _line, atom}) when is_atom(type) do
atom
end
@@ -369,30 +367,30 @@ defmodule Code.Typespec do
## Helpers
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
typespec_to_quoted({:type, anno, :charlist, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
typespec_to_quoted({:type, line, :charlist, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
typespec_to_quoted({:type, anno, :nonempty_charlist, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
typespec_to_quoted({:type, line, :nonempty_charlist, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
typespec_to_quoted({:type, anno, :struct, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
typespec_to_quoted({:type, line, :struct, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
typespec_to_quoted({:type, anno, :as_boolean, [arg]})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
typespec_to_quoted({:type, line, :as_boolean, [arg]})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
typespec_to_quoted({:type, anno, :keyword, args})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
typespec_to_quoted({:type, line, :keyword, args})
end
defp remote_type(anno, mod, name, args) do
defp remote_type(line, mod, name, args) do
args = for arg <- args, do: typespec_to_quoted(arg)
dot = {:., meta(anno), [typespec_to_quoted(mod), typespec_to_quoted(name)]}
{dot, meta(anno), args}
dot = {:., [line: line], [typespec_to_quoted(mod), typespec_to_quoted(name)]}
{dot, [line: line], args}
end
defp erl_to_ex_var(var) do
@@ -416,6 +414,4 @@ defmodule Code.Typespec do
defp unpack_typespec_kw(_, _acc) do
:error
end
defp meta(anno), do: [line: :erl_anno.line(anno)]
end
+3 -4
View File
@@ -17,8 +17,8 @@ defprotocol Collectable do
This design is intentional. `Enumerable` was designed to support infinite
collections, resources and other structures with fixed shape. For example,
it doesn't make sense to insert values into a `Range`, as it has a
fixed shape where only the range limits and step are stored.
it doesn't make sense to insert values into a range, as it has a fixed
shape where just the range limits are stored.
The `Collectable` module was designed to fill the gap left by the
`Enumerable` protocol. `Collectable.into/1` can be seen as the opposite of
@@ -94,11 +94,10 @@ end
defimpl Collectable, for: List do
def into(list) do
# TODO: Change the behaviour so the into always comes last on Elixir v2.0
if list != [] do
IO.warn(
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
"Enum.into/2 and \"for\" comprehensions with an :into option is incorrect " <>
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
"list, consider concatenating the two lists with the ++ operator."
+5 -7
View File
@@ -75,7 +75,7 @@ defmodule Config do
@config_key {__MODULE__, :config}
@imports_key {__MODULE__, :imports}
defp get_opts!(), do: Process.get(@opts_key) || raise_improper_use!()
defp get_opts!(), do: Process.get(@opts_key)
defp put_opts(value), do: Process.put(@opts_key, value)
defp delete_opts(), do: Process.delete(@opts_key)
@@ -102,9 +102,8 @@ defmodule Config do
The given `opts` are merged into the existing configuration
for the given `root_key`. Conflicting keys are overridden by the
ones specified in `opts`, unless they are keywords, which are
deep merged recursively. For example, the application configuration
below
ones specified in `opts`. For example, the application
configuration below
config :logger,
level: :warn,
@@ -139,9 +138,8 @@ defmodule Config do
The given `opts` are merged into the existing values for `key`
in the given `root_key`. Conflicting keys are overridden by the
ones specified in `opts`, unless they are keywords, which are
deep merged recursively. For example, the application configuration
below
ones specified in `opts`. For example, the application
configuration below
config :ecto, Repo,
log_level: :warn,
+12 -6
View File
@@ -20,8 +20,8 @@ defmodule Config.Provider do
For example, imagine you want to list some basic configuration
on Mix's built-in `config/runtime.exs` file, but you also want
to support additional configuration files. To do so, you can add
this inside the `def project` portion of your `mix.exs`:
some additional configuration files. To do so, you can do this
in your `mix.exs`:
releases: [
demo: [
@@ -42,8 +42,7 @@ defmodule Config.Provider do
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. You can learn
more options on `Config.Reader`.
and `extra_config.exs` early in the boot process.
## Custom config provider
@@ -56,10 +55,8 @@ defmodule Config.Provider do
@behaviour Config.Provider
# Let's pass the path to the JSON file as config
@impl true
def init(path) when is_binary(path), do: path
@impl true
def load(config, path) do
# We need to start any app we may depend on.
{:ok, _} = Application.ensure_all_started(:jason)
@@ -230,6 +227,7 @@ defmodule Config.Provider do
end
defp boot_providers(path, provider, reboot_config, reboot_fun) do
validate_no_cyclic_boot!(path)
original_config = read_config!(path)
config =
@@ -354,6 +352,14 @@ defmodule Config.Provider do
defp booted_value(%{prune_runtime_sys_config_after_boot: true}, path), do: {:booted, path}
defp booted_value(%{prune_runtime_sys_config_after_boot: false}, _path), do: {:booted, nil}
defp validate_no_cyclic_boot!(path) do
if System.get_env("ELIXIR_CONFIG_PROVIDER_BOOTED") do
bad_path_abort("Got infinite loop when running Config.Provider", path)
else
System.put_env("ELIXIR_CONFIG_PROVIDER_BOOTED", "1")
end
end
defp read_config!(path) do
case :file.consult(path) do
{:ok, [inner]} ->
+10 -16
View File
@@ -4,23 +4,15 @@ defmodule Config.Reader do
## As a provider
`Config.Reader` can also be used as a `Config.Provider`. A config
provider is used during releases to customize how applications are
configured. When used as a provider, it expects a single argument:
the configuration path (as outlined in `t:Config.Provider.config_path/0`)
for the file to be read and loaded during the system boot.
`Config.Reader` can also be used as a `Config.Provider`. When used
as a provider, it expects a single argument: the configuration path
(as outlined in `t:Config.Provider.config_path/0`) for the file to
be read and loaded during the system boot.
For example, if you expect the target system to have a config file
in an absolute path, you can add this inside the `def project` portion
of your `mix.exs`:
in an absolute path, you can configure your `mix release` as:
releases: [
demo: [
config_providers: [
{Config.Reader, "/etc/config.exs"}
]
]
]
config_providers: [{Config.Reader, "/etc/config.exs"}]
Or if you want to read a custom path inside the release:
@@ -36,8 +28,10 @@ defmodule Config.Reader do
imports: :disabled}
]
Remember Mix already loads `config/runtime.exs` by default.
For more examples and scenarios, see the `Config.Providers` module.
Note by default Mix releases supports runtime configuration via
a `config/runtime.exs`. If a `config/runtime.exs` exists in your
application, it is automatically copied inside the release and
automatically set as a config provider.
"""
@behaviour Config.Provider
+37 -226
View File
@@ -4,7 +4,10 @@ defprotocol Enumerable do
When you invoke a function in the `Enum` module, the first argument
is usually a collection that must implement this protocol.
For example, the expression `Enum.map([1, 2, 3], &(&1 * 2))`
For example, the expression:
Enum.map([1, 2, 3], &(&1 * 2))
invokes `Enumerable.reduce/3` to perform the reducing operation that
builds a mapped list by calling the mapping function `&(&1 * 2)` on
every element in the collection and consuming the element with an
@@ -70,20 +73,15 @@ defprotocol Enumerable do
It may be *done* when the enumeration is finished by reaching
its end, or *halted*/*suspended* when the enumeration was halted
or suspended by the tagged accumulator.
or suspended by the `t:reducer/0` function.
In case the tagged `:halt` accumulator is given, the `:halted` tuple
with the accumulator must be returned. Functions like `Enum.take_while/2`
use `:halt` underneath and can be used to test halting enumerables.
In case a `t:reducer/0` function returns the `:suspend` accumulator, the
`:suspended` tuple must be explicitly handled by the caller and
never leak. In practice, this means regular enumeration functions
just need to be concerned about `:done` and `:halted` results.
In case the tagged `:suspend` accumulator is given, the caller must
return the `:suspended` tuple with the accumulator and a continuation.
The caller is then responsible of managing the continuation and the
caller must always call the continuation, eventually halting or continuing
until the end. `Enum.zip/2` uses suspension, so it can be used to test
whether your implementation handles suspension correctly. You can also use
`Stream.zip/2` with `Enum.take_while/2` to test the combination of
`:suspend` with `:halt`.
Furthermore, a `:suspend` call must always be followed by another call,
eventually halting or continuing until the end.
"""
@type result ::
{:done, term}
@@ -282,7 +280,7 @@ defmodule Enum do
end
@doc """
Returns `true` if all elements in `enumerable` are truthy.
Returns `true` if all elements in `enumerable` are truthy.
When an element has a falsy value (`false` or `nil`) iteration stops immediately
and `false` is returned. In all other cases `true` is returned.
@@ -603,14 +601,9 @@ defmodule Enum do
"""
@spec concat(t) :: t
def concat(enumerables)
def concat(list) when is_list(list) do
concat_list(list)
end
def concat(enums) do
concat_enum(enums)
def concat(enumerables) do
fun = &[&1 | &2]
enumerables |> reduce([], &reduce(&1, &2, fun)) |> :lists.reverse()
end
@doc """
@@ -635,7 +628,7 @@ defmodule Enum do
end
def concat(left, right) do
concat_enum([left, right])
concat([left, right])
end
@doc """
@@ -1211,16 +1204,13 @@ defmodule Enum do
def flat_map(enumerable, fun) do
reduce(enumerable, [], fn entry, acc ->
case fun.(entry) do
list when is_list(list) -> [list | acc]
other -> [to_list(other) | acc]
list when is_list(list) -> :lists.reverse(list, acc)
other -> reduce(other, acc, &[&1 | &2])
end
end)
|> flat_reverse([])
|> :lists.reverse()
end
defp flat_reverse([h | t], acc), do: flat_reverse(t, h ++ acc)
defp flat_reverse([], acc), do: acc
@doc """
Maps and reduces an `enumerable`, flattening the given results (only one level deep).
@@ -1338,7 +1328,7 @@ defmodule Enum do
value = value_fun.(entry)
case acc do
%{^key => existing} -> %{acc | key => [value | existing]}
%{^key => existing} -> Map.put(acc, key, [value | existing])
%{} -> Map.put(acc, key, [value])
end
end)
@@ -1397,10 +1387,9 @@ defmodule Enum do
@doc """
Inserts the given `enumerable` into a `collectable`.
Note that passing a non-empty list as the `collectable` is deprecated.
If you're collecting into a non-empty keyword list, consider using
`Keyword.merge(collectable, Enum.to_list(enumerable))`. If you're collecting
into a non-empty list, consider something like `Enum.to_list(enumerable) ++ collectable`.
Note that passing a non-empty list as the `collectable` is deprecated. If you're collecting
into a non-empty keyword list, consider using `Keyword.merge(collectable, Enum.to_list(enumerable))`.
If you're collecting into a non-empty list, consider something like `Enum.to_list(enumerable) ++ collectable`.
## Examples
@@ -1471,7 +1460,7 @@ defmodule Enum do
## Examples
iex> Enum.into([1, 2, 3], [], fn x -> x * 3 end)
iex> Enum.into([2, 3], [3], fn x -> x * 3 end)
[3, 6, 9]
iex> Enum.into(%{a: 1, b: 2}, %{c: 3}, fn {k, v} -> {k, v * 2} end)
@@ -1479,8 +1468,8 @@ defmodule Enum do
"""
@spec into(Enumerable.t(), Collectable.t(), (term -> term)) :: Collectable.t()
def into(enumerable, [], transform) do
Enum.map(enumerable, transform)
def into(enumerable, collectable, transform) when is_list(collectable) do
collectable ++ map(enumerable, transform)
end
def into(%_{} = enumerable, collectable, transform) do
@@ -2532,169 +2521,6 @@ defmodule Enum do
end
end
@doc """
Slides a single or multiple elements given by `range_or_single_index` from `enumerable`
to `insertion_index`.
The semantics of the range to be moved match the semantics of `Enum.slice/2`.
Specifically, that means:
* Indices are normalized, meaning that negative indexes will be counted from the end
(for example, -1 means the last element of the enumerable). This will result in *two*
traversals of your enumerable on types like lists that don't provide a constant-time count.
* If the normalized index range's `last` is out of bounds, the range is truncated to the last element.
* If the normalized index range's `first` is out of bounds, the selected range for sliding
will be empty, so you'll get back your input list.
* Decreasing ranges (such as `5..0//1`) also select an empty range to be moved,
so you'll get back your input list.
* Ranges with any step but 1 will raise an error.
## Examples
# Slide a single element
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 5, 1)
[:a, :f, :b, :c, :d, :e, :g]
# Slide a range of elements backward
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 3..5, 1)
[:a, :d, :e, :f, :b, :c, :g]
# Slide a range of elements forward
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 1..3, 5)
[:a, :e, :f, :b, :c, :d, :g]
# Slide with negative indices (counting from the end)
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 3..-1//1, 2)
[:a, :b, :d, :e, :f, :g, :c]
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], -4..-2, 1)
[:a, :d, :e, :f, :b, :c, :g]
# Insert at negative indices (counting from the end)
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 3, -1)
[:a, :b, :c, :e, :f, :g, :d]
"""
@doc since: "1.13.0"
def slide(enumerable, range_or_single_index, insertion_index)
def slide(enumerable, single_index, insertion_index) when is_integer(single_index) do
slide(enumerable, single_index..single_index, insertion_index)
end
# This matches the behavior of Enum.slice/2
def slide(_, _.._//step = index_range, _insertion_index) when step != 1 do
raise ArgumentError,
"Enum.slide/3 does not accept ranges with custom steps, got: #{inspect(index_range)}"
end
# Normalize negative input ranges like Enum.slice/2
def slide(enumerable, first..last, insertion_index)
when first < 0 or last < 0 or insertion_index < 0 do
count = Enum.count(enumerable)
normalized_first = if first >= 0, do: first, else: first + count
normalized_last = if last >= 0, do: last, else: last + count
normalized_insertion_index =
if insertion_index >= 0, do: insertion_index, else: insertion_index + count
if normalized_first >= 0 and normalized_first < count and
normalized_first != normalized_insertion_index do
normalized_range = normalized_first..normalized_last//1
slide(enumerable, normalized_range, normalized_insertion_index)
else
Enum.to_list(enumerable)
end
end
def slide(enumerable, insertion_index.._, insertion_index) do
Enum.to_list(enumerable)
end
def slide(_, first..last, insertion_index)
when insertion_index > first and insertion_index <= last do
raise "Insertion index for slide must be outside the range being moved " <>
"(tried to insert #{first}..#{last} at #{insertion_index})"
end
# Guarantees at this point: step size == 1 and first <= last and (insertion_index < first or insertion_index > last)
def slide(enumerable, first..last, insertion_index) do
impl = if is_list(enumerable), do: &slide_list_start/4, else: &slide_any/4
cond do
insertion_index <= first -> impl.(enumerable, insertion_index, first, last)
insertion_index > last -> impl.(enumerable, first, last + 1, insertion_index)
end
end
# Takes the range from middle..last and moves it to be in front of index start
defp slide_any(enumerable, start, middle, last) do
# We're going to deal with 4 "chunks" of the enumerable:
# 0. "Head," before the start index
# 1. "Slide back," between start (inclusive) and middle (exclusive)
# 2. "Slide front," between middle (inclusive) and last (inclusive)
# 3. "Tail," after last
#
# But, we're going to accumulate these into only two lists: pre and post.
# We'll reverse-accumulate the head into our pre list, then "slide back" into post,
# then "slide front" into pre, then "tail" into post.
#
# Then at the end, we're going to reassemble and reverse them, and end up with the
# chunks in the correct order.
{_size, pre, post} =
Enum.reduce(enumerable, {0, [], []}, fn item, {index, pre, post} ->
{pre, post} =
cond do
index < start -> {[item | pre], post}
index >= start and index < middle -> {pre, [item | post]}
index >= middle and index <= last -> {[item | pre], post}
true -> {pre, [item | post]}
end
{index + 1, pre, post}
end)
:lists.reverse(pre, :lists.reverse(post))
end
# Like slide_any/4 above, this optimized implementation of slide for lists depends
# on the indices being sorted such that we're moving middle..last to be in front of start.
defp slide_list_start([h | t], start, middle, last)
when start > 0 and start <= middle and middle <= last do
[h | slide_list_start(t, start - 1, middle - 1, last - 1)]
end
defp slide_list_start(list, 0, middle, last), do: slide_list_middle(list, middle, last, [])
defp slide_list_middle([h | t], middle, last, acc) when middle > 0 do
slide_list_middle(t, middle - 1, last - 1, [h | acc])
end
defp slide_list_middle(list, 0, last, start_to_middle) do
{slid_range, tail} = slide_list_last(list, last + 1, [])
slid_range ++ :lists.reverse(start_to_middle, tail)
end
# You asked for a middle index off the end of the list... you get what we've got
defp slide_list_middle([], _, _, acc) do
:lists.reverse(acc)
end
defp slide_list_last([h | t], last, acc) when last > 0 do
slide_list_last(t, last - 1, [h | acc])
end
defp slide_list_last(rest, 0, acc) do
{:lists.reverse(acc), rest}
end
defp slide_list_last([], _, acc) do
{:lists.reverse(acc), []}
end
@doc """
Applies the given function to each element in the `enumerable`,
storing the result in a list and passing it as the accumulator
@@ -3644,7 +3470,7 @@ defmodule Enum do
"""
@spec zip(t, t) :: [{any, any}]
def zip(enumerable1, enumerable2) when is_list(enumerable1) and is_list(enumerable2) do
zip_list(enumerable1, enumerable2, [])
zip_list(enumerable1, enumerable2)
end
def zip(enumerable1, enumerable2) do
@@ -3717,7 +3543,7 @@ defmodule Enum do
@spec zip_with(t, t, (enum1_elem :: term, enum2_elem :: term -> term)) :: [term]
def zip_with(enumerable1, enumerable2, zip_fun)
when is_list(enumerable1) and is_list(enumerable2) and is_function(zip_fun, 2) do
zip_with_list(enumerable1, enumerable2, zip_fun)
zip_list(enumerable1, enumerable2, zip_fun)
end
def zip_with(enumerable1, enumerable2, zip_fun) when is_function(zip_fun, 2) do
@@ -3786,17 +3612,16 @@ defmodule Enum do
end
@doc """
Reduces over all of the given enumerables, halting as soon as any enumerable is
empty.
Reduces a over all of the given enums, halting as soon as any enumerable is empty.
The reducer will receive 2 args: a list of elements (one from each enum) and the
The reducer will receive 2 args, a list of elements (one from each enum) and the
accumulator.
In practice, the behaviour provided by this function can be achieved with:
Enum.reduce(Stream.zip(enums), acc, reducer)
But `zip_reduce/3` exists for convenience purposes.
But `zip_reduce/4` exists for convenience purposes.
## Examples
@@ -3981,17 +3806,6 @@ defmodule Enum do
false
end
## concat
defp concat_list([h | t]) when is_list(h), do: h ++ concat_list(t)
defp concat_list([h | t]), do: concat_enum([h | t])
defp concat_list([]), do: []
defp concat_enum(enum) do
fun = &[&1 | &2]
enum |> reduce([], &reduce(&1, &2, fun)) |> :lists.reverse()
end
# dedup
defp dedup_list([value | tail], acc) do
@@ -4437,19 +4251,16 @@ defmodule Enum do
## zip
defp zip_list([head1 | next1], [head2 | next2], acc) do
zip_list(next1, next2, [{head1, head2} | acc])
defp zip_list(enumerable1, enumerable2) do
zip_list(enumerable1, enumerable2, fn x, y -> {x, y} end)
end
defp zip_list([], _, acc), do: :lists.reverse(acc)
defp zip_list(_, [], acc), do: :lists.reverse(acc)
defp zip_with_list([head1 | next1], [head2 | next2], fun) do
[fun.(head1, head2) | zip_with_list(next1, next2, fun)]
defp zip_list([head1 | next1], [head2 | next2], fun) do
[fun.(head1, head2) | zip_list(next1, next2, fun)]
end
defp zip_with_list(_, [], _fun), do: []
defp zip_with_list([], _, _fun), do: []
defp zip_list(_, [], _fun), do: []
defp zip_list([], _, _fun), do: []
defp zip_reduce_list([head1 | next1], [head2 | next2], acc, fun) do
zip_reduce_list(next1, next2, fun.(head1, head2, acc), fun)
+33 -108
View File
@@ -43,16 +43,10 @@ defmodule Exception do
@callback blame(t, stacktrace) :: {t, stacktrace}
@optional_callbacks [blame: 2]
@doc false
# Callback for formatting Erlang exceptions
def format_error(%struct{} = exception, _stacktrace) do
%{general: message(exception), reason: "#" <> Atom.to_string(struct)}
end
@doc """
Returns `true` if the given `term` is an exception.
"""
# TODO: Deprecate this on Elixir v1.15
# TODO: Remove this on Elixir v1.15
@doc deprecated: "Use Kernel.is_exception/1 instead"
def exception?(term)
def exception?(%_{__exception__: true}), do: true
@@ -68,7 +62,7 @@ defmodule Exception do
caught_exception ->
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(message(caught_exception))} while retrieving Exception.message/1 " <>
"for #{inspect(exception)}. Stacktrace:\n#{format_stacktrace(__STACKTRACE__)}"
"for #{inspect(exception)}"
else
result when is_binary(result) ->
result
@@ -217,17 +211,16 @@ defmodule Exception do
clauses =
for {meta, ex_args, guards, _block} <- clauses do
scope = :elixir_erl.scope(meta, true)
ann = :elixir_erl.get_ann(meta)
{erl_args, scope} =
:elixir_erl_clauses.match(ann, &:elixir_erl_pass.translate_args/3, ex_args, scope)
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
{args, binding} =
[call_args, ex_args, erl_args]
|> Enum.zip()
|> Enum.map_reduce([], &blame_arg/2)
guards = Enum.map(guards, &blame_guard(&1, ann, scope, binding))
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
{args, guards}
end
@@ -267,11 +260,11 @@ defmodule Exception do
end)
end
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, ann, scope, binding)
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, scope, binding)
when op == :andalso or op == :orelse do
guards = [
blame_guard(left, ann, scope, binding),
blame_guard(right, ann, scope, binding)
blame_guard(left, scope, binding),
blame_guard(right, scope, binding)
]
kernel_op =
@@ -283,8 +276,8 @@ defmodule Exception do
{kernel_op, meta, guards}
end
defp blame_guard(ex_guard, ann, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
defp blame_guard(ex_guard, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
match? =
try do
@@ -697,17 +690,6 @@ defmodule Exception do
end
end
@doc false
def format_snippet(snippet, error_line) do
line_digits = error_line |> Integer.to_string() |> byte_size()
placeholder = String.duplicate(" ", max(line_digits, 2))
padding = if line_digits < 2, do: " "
" #{placeholder} |\n" <>
" #{padding}#{error_line} | #{snippet.content}\n" <>
" #{placeholder} | #{String.duplicate(" ", snippet.offset)}^"
end
defp format_location(opts) when is_list(opts) do
format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line), " ")
end
@@ -732,14 +714,10 @@ defmodule ArgumentError do
) do
message =
cond do
not proper_list?(args) ->
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
# Note that args may be an empty list even if they were supplied
not is_atom(module) and is_atom(function) and args == [] ->
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
"If you are using Kernel.apply/3, make sure the module is an atom. " <>
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
"If you are using apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as map.field or module.function(), " <>
"make sure the left side of the dot is an atom or a map"
@@ -748,9 +726,12 @@ defmodule ArgumentError do
"Modules (the first argument of apply) must always be an atom"
not is_atom(function) ->
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)}. " <>
"However #{inspect(function)} is not a valid function name. Function names (the second argument " <>
"of apply) must always be an atom"
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
"Functions (the second argument of apply) must always be an atom"
not is_list(args) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
end
{%{exception | message: message}, stacktrace}
@@ -759,9 +740,6 @@ defmodule ArgumentError do
def blame(exception, stacktrace) do
{exception, stacktrace}
end
defp proper_list?(list) when length(list) >= 0, do: true
defp proper_list?(_), do: false
end
defmodule ArithmeticError do
@@ -809,56 +787,20 @@ defmodule SystemLimitError do
end
defmodule SyntaxError do
defexception [:file, :line, :column, :snippet, description: "syntax error"]
defexception [:file, :line, :column, description: "syntax error"]
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description,
snippet: snippet
})
when not is_nil(snippet) and not is_nil(column) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
end
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description
}) do
def message(%{file: file, line: line, column: column, description: description}) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description
end
end
defmodule TokenMissingError do
defexception [:file, :line, :snippet, :column, description: "expression is incomplete"]
defexception [:file, :line, :column, description: "expression is incomplete"]
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description,
snippet: snippet
})
when not is_nil(snippet) and not is_nil(column) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
end
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description
}) do
def message(%{file: file, line: line, column: column, description: description}) do
Exception.format_file_line_column(file && Path.relative_to_cwd(file), line, column) <>
" " <> description
end
@@ -1091,7 +1033,7 @@ defmodule UndefinedFunctionError do
case result do
[] -> []
suggestions -> [". Did you mean:\n\n" | Enum.map(suggestions, &format_fa/1)]
suggestions -> [". Did you mean one of:\n\n" | Enum.map(suggestions, &format_fa/1)]
end
end
@@ -1122,9 +1064,7 @@ defmodule UndefinedFunctionError do
end
defp expects_callback?(behaviour, function, arity) do
callbacks =
behaviour.behaviour_info(:callbacks) -- behaviour.behaviour_info(:optional_callbacks)
callbacks = behaviour.behaviour_info(:callbacks)
Enum.member?(callbacks, {function, arity})
end
@@ -1166,7 +1106,7 @@ defmodule FunctionClauseError do
%{module: module, function: function, arity: arity} ->
formatted = Exception.format_mfa(module, function, arity)
blamed = blame(exception, &inspect/1, &blame_match/1)
blamed = blame(exception, &inspect/1, &blame_match/2)
"no function clause matching in #{formatted}" <> blamed
end
end
@@ -1188,24 +1128,24 @@ defmodule FunctionClauseError do
end
end
defp blame_match(%{match?: true, node: node}), do: Macro.to_string(node)
defp blame_match(%{match?: false, node: node}), do: "-" <> Macro.to_string(node) <> "-"
defp blame_match(%{match?: true, node: node}, _), do: Macro.to_string(node)
defp blame_match(%{match?: false, node: node}, _), do: "-" <> Macro.to_string(node) <> "-"
defp blame_match(_, string), do: string
@doc false
def blame(%{args: nil}, _, _) do
""
end
def blame(exception, inspect_fun, fun) do
def blame(exception, inspect_fun, ast_fun) do
%{module: module, function: function, arity: arity, kind: kind, args: args, clauses: clauses} =
exception
mfa = Exception.format_mfa(module, function, arity)
format_clause_fun = fn {args, guards} ->
args = Enum.map_join(args, ", ", fun)
base = " #{kind} #{function}(#{args})"
Enum.reduce(guards, base, &"#{&2} when #{clause_to_string(&1, fun)}") <> "\n"
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
end
"\n\nThe following arguments were given to #{mfa}:\n" <>
@@ -1213,11 +1153,6 @@ defmodule FunctionClauseError do
"#{format_clauses(clauses, format_clause_fun, @clause_limit)}"
end
defp clause_to_string({op, _, [left, right]}, fun),
do: clause_to_string(left, fun) <> " #{op} " <> clause_to_string(right, fun)
defp clause_to_string(node, fun), do: fun.(node)
defp format_args(args, inspect_fun) do
args
|> Enum.with_index(1)
@@ -1362,7 +1297,7 @@ defmodule KeyError do
case suggestions do
[] -> []
suggestions -> [". Did you mean:\n\n" | format_suggestions(suggestions)]
suggestions -> [". Did you mean one of:\n\n" | format_suggestions(suggestions)]
end
end
@@ -1597,18 +1532,8 @@ defmodule ErlangError do
{nil, nil, nil}
end
defp error_info(:badarg, [{:erlang, fun, _, _} | _]) when fun in [:byte_size, :bit_size] do
{:ok,
"""
* 1st argument: not a bitstring
This typically happens when calling Kernel.#{fun}/1 with an invalid argument \
or when performing binary construction or binary concatenation with <> and \
one of the arguments is not a binary\
"""}
end
defp error_info(erl_exception, stacktrace) do
@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)
+10 -16
View File
@@ -159,8 +159,8 @@ defmodule GenServer do
a name on start via the `:name` option. Registered names are also
automatically cleaned up on termination. The supported values are:
* an atom - the GenServer is registered locally (to the current node)
with the given name using `Process.register/2`.
* an atom - the GenServer is registered locally with the given name
using `Process.register/2`.
* `{:global, term}` - the GenServer is registered globally with the given
term using the functions in the [`:global` module](`:global`).
@@ -254,20 +254,15 @@ defmodule GenServer do
a timeout value in milliseconds; if not, `:infinity` is assumed.
The timeout can be used to detect a lull in incoming messages.
The `timeout()` value is used as follows:
If the process has no messages waiting when the timeout is set and the
number of given milliseconds pass without any message arriving,
then `handle_info/2` will be called with `:timeout` as the first argument.
The timeout is cleared if any message is waiting or arrives before the
given timeout.
* If the process has any message already waiting when the `timeout()` value
is returned, the timeout is ignored and the waiting message is handled as
usual. This means that even a timeout of `0` milliseconds is not guaranteed
to execute (if you want to take another action immediately and unconditionally,
use a `:continue` instruction instead).
* If any message arrives before the specified number of milliseconds
elapse, the timeout is cleared and that message is handled as usual.
* Otherwise, when the specified number of milliseconds have elapsed with no
message arriving, `handle_info/2` is called with `:timeout` as the first
argument.
Because a message may arrive before the timeout is set, even a timeout of `0`
milliseconds is not guaranteed to execute. To take another action immediately
and unconditionally, use a `:continue` instruction.
## When (not) to use a GenServer
@@ -1018,7 +1013,6 @@ defmodule GenServer do
nil ->
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
# TODO: remove this clause when we require Erlang/OTP 25+
pid when pid == self() ->
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
+1 -1
View File
@@ -157,7 +157,7 @@ defimpl Inspect, for: BitString do
defp each_bit(bitstring, _counter, opts) do
size = bit_size(bitstring)
<<h::size(size)>> = bitstring
concat(Inspect.Integer.inspect(h, opts), "::size(" <> Integer.to_string(size) <> ")")
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
end
@compile {:inline, decrement: 1}
+4 -69
View File
@@ -32,7 +32,7 @@ defmodule Inspect.Opts do
to pass the custom options through.
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents.
Defaults to `Inspect.Opts.default_inspect_fun/0`.
Defaults to `Inspect.inspect/2`.
* `:limit` - limits the number of items that are inspected for tuples,
bitstrings, maps, lists and any other collection of items, with the exception of
@@ -105,62 +105,6 @@ defmodule Inspect.Opts do
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
width: non_neg_integer | :infinity
}
@doc """
Builds an `Inspect.Opts` struct.
"""
@doc since: "1.13.0"
@spec new(keyword()) :: t
def new(opts) do
struct(%Inspect.Opts{inspect_fun: default_inspect_fun()}, opts)
end
@doc """
Returns the default inspect function.
"""
@doc since: "1.13.0"
@spec default_inspect_fun() :: (term, t -> Inspect.Algebra.t())
def default_inspect_fun do
:persistent_term.get({__MODULE__, :inspect_fun}, &Inspect.inspect/2)
end
@doc """
Sets the default inspect function.
Set this option with care as it will change how all values
in the system are inspected. The main use of this functionality
is to provide an entry point to filter inspected values,
in order for entities to comply with rules and legislations
on data security and data privacy.
It is **extremely discouraged** for libraries to set their own
function as this must be controlled by applications. Libraries
should instead define their own structs with custom inspect
implementations. If a library must change the default inspect
function, then it is best to define to ask users of your library
to explicitly call `default_inspect_fun/1` with your function of
choice.
The default is `Inspect.inspect/2`.
## Examples
previous_fun = Inspect.Opts.default_inspect_fun()
Inspect.Opts.default_inspect_fun(fn
%{address: _} = map, opts ->
previous_fun.(%{map | address: "[REDACTED]"}, opts)
value, opts ->
previous_fun.(value, opts)
end)
"""
@doc since: "1.13.0"
@spec default_inspect_fun((term, t -> Inspect.Algebra.t())) :: :ok
def default_inspect_fun(fun) when is_function(fun, 2) do
:persistent_term.put({__MODULE__, :inspect_fun}, fun)
end
end
defmodule Inspect.Error do
@@ -354,13 +298,7 @@ defmodule Inspect.Algebra do
try do
Process.put(:inspect_trap, true)
res =
Inspect.Map.inspect(struct, %{
opts
| syntax_colors: [],
inspect_fun: Inspect.Opts.default_inspect_fun()
})
res = Inspect.Map.inspect(struct, %{opts | syntax_colors: []})
res = IO.iodata_to_binary(format(res, :infinity))
message =
@@ -370,10 +308,7 @@ defmodule Inspect.Algebra do
exception = Inspect.Error.exception(message: message)
if opts.safe do
Inspect.inspect(exception, %{
opts
| inspect_fun: Inspect.Opts.default_inspect_fun()
})
Inspect.inspect(exception, opts)
else
reraise(exception, __STACKTRACE__)
end
@@ -601,7 +536,7 @@ defmodule Inspect.Algebra do
Colors a document if the `color_key` has a color in the options.
"""
@doc since: "1.4.0"
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: t
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
if precolor = Keyword.get(syntax_colors, color_key) do
postcolor = Keyword.get(syntax_colors, :reset, :reset)
+13 -7
View File
@@ -104,13 +104,19 @@ defmodule Integer do
@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])
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.
This function performs a [floored division](`floor_div/2`), which means that
`Integer.mod/2` uses floored division, which means that
the result will always have the sign of the `divisor`.
Raises an `ArithmeticError` exception if one of the arguments is not an
@@ -142,8 +148,8 @@ defmodule Integer do
Raises an `ArithmeticError` exception if one of the arguments is not an
integer, or when the `divisor` is `0`.
This function performs a *floored* integer division, which means that
the result will always be rounded towards negative infinity.
`Integer.floor_div/2` performs *floored* integer division. This means that
the result is always rounded towards negative infinity.
If you want to perform truncated integer division (rounding towards zero),
use `Kernel.div/2` instead.
@@ -430,7 +436,7 @@ defmodule Integer do
@doc """
Returns the extended greatest common divisor of the two given integers.
This function uses the extended Euclidean algorithm to return a three-element tuple with the `gcd`
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
@@ -462,8 +468,8 @@ defmodule Integer do
@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, b), do: {b, 0, 1}
def extended_gcd(a, 0), do: {a, 1, 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)
+49 -61
View File
@@ -122,14 +122,14 @@ defmodule IO do
@type nodata :: {:error, term} | :eof
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
defguardp is_device(term) when is_atom(term) or is_pid(term)
defguardp is_iodata(data) when is_list(data) or is_binary(data)
@doc """
Reads from the IO `device`.
The `device` is iterated by the given number of characters, line by line if
`:line` is given, or until `:eof`.
The `device` is iterated by the given number of characters or line by line if
`:line` is given.
Alternatively, if `:all` is given, then whole `device` is returned.
It returns:
@@ -141,24 +141,14 @@ defmodule IO do
for instance, `{:error, :estale}` if reading from an
NFS volume
If `:all` is given, `:eof` is never returned, but an
empty string in case the device has reached EOF.
"""
@spec read(device, :eof | :line | non_neg_integer) :: chardata | nodata
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
def read(device \\ :stdio, line_or_chars)
# TODO: Deprecate me on v1.17
def read(device, :all) do
with :eof <- read(device, :eof) do
with [_ | _] = opts <- :io.getopts(device),
false <- Keyword.get(opts, :binary, true) do
''
else
_ -> ""
end
end
end
def read(device, :eof) do
getn(device, '', :eof)
do_read_all(map_dev(device), :empty)
end
def read(device, :line) do
@@ -169,11 +159,35 @@ defmodule IO do
:io.get_chars(map_dev(device), '', count)
end
defp do_read_all(mapped_dev, acc) do
case :io.get_line(mapped_dev, "") do
line when is_binary(line) or is_list(line) -> do_read_all(mapped_dev, concat(acc, line))
:eof -> read_eof(mapped_dev, acc)
other -> other
end
end
defp concat(:empty, line), do: line
defp concat(acc, line) when is_binary(acc), do: acc <> line
defp concat(acc, line) when is_list(acc), do: acc ++ line
defp read_eof(device, :empty) do
with [_ | _] = opts <- :io.getopts(device),
false <- Keyword.get(opts, :binary, true) do
''
else
_ -> ""
end
end
defp read_eof(_device, acc), do: acc
@doc """
Reads from the IO `device`. The operation is Unicode unsafe.
The `device` is iterated by the given number of bytes, line by line if
`:line` is given, or until `:eof`.
The `device` is iterated by the given number of bytes or line by line if
`:line` is given.
Alternatively, if `:all` is given, then whole `device` is returned.
It returns:
@@ -185,19 +199,17 @@ defmodule IO do
for instance, `{:error, :estale}` if reading from an
NFS volume
If `:all` is given, `:eof` is never returned, but an
empty string in case the device has reached EOF.
Note: do not use this function on IO devices in Unicode mode
as it will return the wrong result.
"""
@spec binread(device, :eof | :line | non_neg_integer) :: iodata | nodata
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
def binread(device \\ :stdio, line_or_chars)
# TODO: Deprecate me on v1.17
def binread(device, :all) do
with :eof <- binread(device, :eof), do: ""
end
def binread(device, :eof) do
binread_eof(map_dev(device), "")
do_binread_all(map_dev(device), "")
end
def binread(device, :line) do
@@ -215,10 +227,10 @@ defmodule IO do
end
@read_all_size 4096
defp binread_eof(mapped_dev, acc) do
defp do_binread_all(mapped_dev, acc) do
case :file.read(mapped_dev, @read_all_size) do
{:ok, data} -> binread_eof(mapped_dev, acc <> data)
:eof -> if acc == "", do: :eof, else: acc
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
:eof -> acc
other -> other
end
end
@@ -281,7 +293,7 @@ defmodule IO do
"""
@spec puts(device, chardata | String.Chars.t()) :: :ok
def puts(device \\ :stdio, item) when is_device(device) do
def puts(device \\ :stdio, item) do
:io.put_chars(map_dev(device), [to_chardata(item), ?\n])
end
@@ -305,7 +317,7 @@ defmodule IO do
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
def warn(message, []) do
message = [to_chardata(message), ?\n]
:elixir_errors.log_and_print_warning(0, nil, message, message)
:elixir_errors.io_warn(0, nil, message, message)
end
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
@@ -314,7 +326,7 @@ defmodule IO do
line = opts[:line]
file = opts[:file]
:elixir_errors.log_and_print_warning(
:elixir_errors.io_warn(
line || 0,
file && List.to_string(file),
message,
@@ -416,9 +428,9 @@ defmodule IO do
See `inspect/2` for a full list of options.
"""
@spec inspect(device, item, keyword) :: item when item: var
def inspect(device, item, opts) when is_device(device) and is_list(opts) do
def inspect(device, item, opts) when is_list(opts) do
label = if label = opts[:label], do: [to_chardata(label), ": "], else: []
opts = Inspect.Opts.new(opts)
opts = struct(Inspect.Opts, opts)
doc = Inspect.Algebra.group(Inspect.Algebra.to_doc(item, opts))
chardata = Inspect.Algebra.format(doc, opts.width)
puts(device, [label, chardata])
@@ -433,18 +445,12 @@ defmodule IO do
Otherwise, `count` is the number of raw bytes to be retrieved.
See `IO.getn/3` for a description of return values.
"""
@spec getn(
device | chardata | String.Chars.t(),
pos_integer | :eof | chardata | String.Chars.t()
) ::
@spec getn(device | chardata | String.Chars.t(), pos_integer | chardata | String.Chars.t()) ::
chardata | nodata
def getn(prompt, count \\ 1)
def getn(prompt, :eof) do
getn(:stdio, prompt, :eof)
end
def getn(prompt, count) when is_integer(count) and count > 0 do
getn(:stdio, prompt, count)
end
@@ -471,27 +477,11 @@ defmodule IO do
NFS volume
"""
@spec getn(device, chardata | String.Chars.t(), pos_integer | :eof) :: chardata | nodata
def getn(device, prompt, :eof) do
getn_eof(map_dev(device), to_chardata(prompt), [])
end
@spec getn(device, chardata | String.Chars.t(), pos_integer) :: chardata | nodata
def getn(device, prompt, count) when is_integer(count) and count > 0 do
:io.get_chars(map_dev(device), to_chardata(prompt), count)
end
defp getn_eof(device, prompt, acc) do
case :io.get_line(device, prompt) do
line when is_binary(line) or is_list(line) -> getn_eof(device, '', [line | acc])
:eof -> wrap_eof(:lists.reverse(acc))
other -> other
end
end
defp wrap_eof([h | _] = acc) when is_binary(h), do: IO.iodata_to_binary(acc)
defp wrap_eof([h | _] = acc) when is_list(h), do: :lists.flatten(acc)
defp wrap_eof([]), do: :eof
@doc ~S"""
Reads a line from the IO `device`.
@@ -622,8 +612,6 @@ defmodule IO do
"""
@spec chardata_to_string(chardata) :: String.t()
def chardata_to_string(chardata)
def chardata_to_string(string) when is_binary(string) do
string
end
+21 -28
View File
@@ -194,10 +194,6 @@ defmodule IO.ANSI.Docs do
inline_text("*", traverse_erlang_html(entries, indent, options), options)
end
defp traverse_erlang_html({tag, _, entries}, indent, options) when tag in [:strong, :b] do
inline_text("**", traverse_erlang_html(entries, indent, options), options)
end
defp traverse_erlang_html({:code, _, entries}, indent, options) do
inline_text("`", traverse_erlang_html(entries, indent, options), options)
end
@@ -299,7 +295,7 @@ defmodule IO.ANSI.Docs do
end
defp inline_html?(binary) when is_binary(binary), do: true
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :strong, :b, :br], do: true
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :br], do: true
defp inline_html?(_), do: false
## Markdown
@@ -802,14 +798,14 @@ defmodule IO.ANSI.Docs do
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
end
# We have four entries: **, __, *, _ and `.
# We have four entries: **, *, _ and `.
#
# The first four behave the same while the last one is simpler
# The first three behave the same while the last one is simpler
# when it comes to delimiters as it ignores spaces and escape
# characters. But, since the first two has two characters,
# we need to handle 3 cases:
# characters. But, since the first has two characters, we need to
# handle 3 cases:
#
# 1. __ and **
# 1. **
# 2. _ and *
# 3. `
#
@@ -823,8 +819,8 @@ defmodule IO.ANSI.Docs do
### Inline start
defp handle_inline(<<mark, mark, rest::binary>>, options) when mark in @single do
handle_inline(rest, [mark | mark], [<<mark, mark>>], [], options)
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
handle_inline(rest, ?d, ["**"], [], options)
end
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
@@ -837,10 +833,9 @@ defmodule IO.ANSI.Docs do
### Inline delimiters
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
when rest != "" and delimiter in @delimiters and mark in @single do
acc = [delimiter, Enum.reverse(buffer) | acc]
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
when rest != "" and delimiter in @delimiters do
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer) | acc], options)
end
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
@@ -855,10 +850,9 @@ defmodule IO.ANSI.Docs do
### Clauses for handling escape
defp handle_inline(<<?\\, ?\\, mark, mark, rest::binary>>, nil, buffer, acc, options)
when rest != "" and mark in @single do
acc = [?\\, Enum.reverse(buffer) | acc]
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
when rest != "" do
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer) | acc], options)
end
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
@@ -877,8 +871,8 @@ defmodule IO.ANSI.Docs do
### Inline end
defp handle_inline(<<mark, mark, delimiter, rest::binary>>, [mark | mark], buffer, acc, options)
when delimiter in @delimiters and mark in @single do
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
when delimiter in @delimiters do
inline_buffer = inline_buffer(buffer, options)
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
end
@@ -889,8 +883,8 @@ defmodule IO.ANSI.Docs do
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
end
defp handle_inline(<<mark, mark, rest::binary>>, [mark | mark], buffer, acc, options)
when rest == "" and mark in @single do
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
when rest == "" do
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
end
@@ -936,11 +930,10 @@ defmodule IO.ANSI.Docs do
defp color_for(mark, colors) do
case mark do
"__" -> color(:doc_bold, colors)
"**" -> color(:doc_bold, colors)
"_" -> color(:doc_underline, colors)
"*" -> color(:doc_underline, colors)
"`" -> color(:doc_inline_code, colors)
"_" -> color(:doc_underline, colors)
"*" -> color(:doc_bold, colors)
"**" -> color(:doc_bold, colors)
end
end
+151 -299
View File
@@ -305,8 +305,6 @@ defmodule Kernel do
prefer `module.function(arg_1, arg_2, ..., arg_n)` as it is clearer than
`apply(module, :function, [arg_1, arg_2, ..., arg_n])`.
`apply/3` cannot be used to call private functions.
Inlined by the compiler.
## Examples
@@ -542,7 +540,7 @@ defmodule Kernel do
Giving it an empty list raises:
hd([])
tl([])
#=> ** (ArgumentError) argument error
"""
@@ -556,21 +554,6 @@ defmodule Kernel do
Returns `true` if `term` is an atom; otherwise returns `false`.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> is_atom(false)
true
iex> is_atom(:name)
true
iex> is_atom(AnAtom)
true
iex> is_atom("true")
false
"""
@doc guard: true
@spec is_atom(term) :: boolean
@@ -623,18 +606,6 @@ defmodule Kernel do
a boolean); otherwise returns `false`.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> is_boolean(false)
true
iex> is_boolean(true)
true
iex> is_boolean(:test)
false
"""
@doc guard: true
@spec is_boolean(term) :: boolean
@@ -657,15 +628,6 @@ defmodule Kernel do
Returns `true` if `term` is a function; otherwise returns `false`.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> is_function(fn x -> x + x end)
true
iex> is_function("not a function")
false
"""
@doc guard: true
@spec is_function(term) :: boolean
@@ -1197,7 +1159,7 @@ defmodule Kernel do
@doc since: "1.12.0"
defmacro tap(value, fun) do
quote bind_quoted: [fun: fun, value: value] do
_ = fun.(value)
fun.(value)
value
end
end
@@ -1458,11 +1420,18 @@ defmodule Kernel do
end
@doc """
List subtraction operator. Removes the first occurrence of an element
on the left list for each element on the right.
List subtraction operator. Removes the first occurrence of an element on the left list
for each element on the right.
This function is optimized so the complexity of `a -- b` is proportional
to `length(a) * log(length(b))`. See also the [Erlang efficiency
Before Erlang/OTP 22, the complexity of `a -- b` was proportional to
`length(a) * length(b)`, meaning that it would be very slow if
both `a` and `b` were long lists. In such cases, consider
converting each list to a `MapSet` and using `MapSet.difference/2`.
As of Erlang/OTP 22, this operation is significantly faster even if both
lists are very long, and using `--/2` is usually faster and uses less
memory than using the `MapSet`-based alternative mentioned above.
See also the [Erlang efficiency
guide](https://erlang.org/doc/efficiency_guide/retired_myths.html).
Inlined by the compiler.
@@ -1885,8 +1854,6 @@ defmodule Kernel do
@doc """
Binary concatenation operator. Concatenates two binaries.
Raises an `ArgumentError` if one of the sides aren't binaries.
## Examples
iex> "foo" <> "bar"
@@ -1899,7 +1866,7 @@ defmodule Kernel do
iex> x
"bar"
`x <> "bar" = "foobar"` would result in an `ArgumentError` exception.
`x <> "bar" = "foobar"` would have resulted in a `CompileError` exception.
"""
defmacro left <> right do
@@ -2003,35 +1970,26 @@ defmodule Kernel do
false -> message
end
erlang_error =
case :erlang.system_info(:otp_release) >= '24' do
true ->
fn x ->
quote do
:erlang.error(unquote(x), :none, error_info: %{module: Exception})
end
end
false ->
fn x ->
quote do
:erlang.error(unquote(x))
end
end
end
case message do
message when is_binary(message) ->
erlang_error.(quote do: RuntimeError.exception(unquote(message)))
quote do
:erlang.error(RuntimeError.exception(unquote(message)))
end
{:<<>>, _, _} = message ->
erlang_error.(quote do: RuntimeError.exception(unquote(message)))
quote do
:erlang.error(RuntimeError.exception(unquote(message)))
end
alias when is_atom(alias) ->
erlang_error.(quote do: unquote(alias).exception([]))
quote do
:erlang.error(unquote(alias).exception([]))
end
_ ->
erlang_error.(quote do: Kernel.Utils.raise(unquote(message)))
quote do
:erlang.error(Kernel.Utils.raise(unquote(message)))
end
end
end
@@ -2165,7 +2123,6 @@ defmodule Kernel do
iex> "abcd" =~ ""
true
For more information about regular expressions, please check the `Regex` module.
"""
@spec String.t() =~ (String.t() | Regex.t()) :: boolean
def left =~ "" when is_binary(left), do: true
@@ -2235,7 +2192,7 @@ defmodule Kernel do
"""
@spec inspect(Inspect.t(), keyword) :: String.t()
def inspect(term, opts \\ []) when is_list(opts) do
opts = Inspect.Opts.new(opts)
opts = struct(Inspect.Opts, opts)
limit =
case opts.pretty do
@@ -2549,9 +2506,6 @@ defmodule Kernel do
iex> 1 |> then(fn x -> x * 2 end)
2
iex> 1 |> then(fn x -> Enum.drop(["a", "b", "c"], x) end)
["b", "c"]
"""
@doc since: "1.12.0"
defmacro then(value, fun) do
@@ -2573,66 +2527,47 @@ defmodule Kernel do
iex> get_in(users, ["john", :age])
27
`get_in/2` can also use the accessors in the `Access` module
to traverse more complex data structures. For example, here we
use `Access.all/0` to traverse a list:
iex> users = [%{name: "john", age: 27}, %{name: "meg", age: 23}]
iex> get_in(users, [Access.all(), :age])
[27, 23]
In case any of the components returns `nil`, `nil` will be returned
and `get_in/2` won't traverse any futher:
In case any of the keys returns `nil`, `nil` will be returned:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> get_in(users, ["unknown", :age])
nil
iex> users = nil
iex> get_in(users, [Access.all(), :age])
nil
The main feature of `get_in/2` is precisely that it aborts traversal
when a `nil` value is found. Unless you need nil-safety, you are likely
better off by writing "regular" Elixir code:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> users["john"][:age]
27
iex> users = [%{name: "john", age: 27}, %{name: "meg", age: 23}]
iex> Enum.map(users, fn user -> user[:age] end)
[27, 23]
Alternatively, if you need to access complex data-structures, you can
use pattern matching:
Note that `get_in` exists mostly for convenience and parity with
functionality found in `put_in` and `update_in`. Given Elixir
provides pattern matching, it can often be more expressive for
deep data traversal, for example:
case users do
%{"john" => %{age: age}} -> age
%{"unknown" => %{age: age}} -> age
_ -> default_value
end
## Functions as keys
If a key given to `get_in/2` is a function, the function will be invoked
passing three arguments:
If a key is a function, the function will be invoked passing three
arguments:
* the operation (`:get`)
* the data to be accessed
* a function to be invoked next
This means `get_in/2` can be extended to provide custom lookups.
That's precisely how the `Access.all/0` key in the previous section
behaves. For example, we can manually implement such traversal as
follows:
In the example below, we use a function to get all the maps inside
a list:
iex> users = [%{name: "john", age: 27}, %{name: "meg", age: 23}]
iex> all = fn :get, data, next -> Enum.map(data, next) end
iex> get_in(users, [all, :age])
[27, 23]
The `Access` module ships with many convenience accessor functions.
See `Access.all/0`, `Access.key/2`, and others as examples.
If the previous value before invoking the function is `nil`,
the function *will* receive `nil` as a value and must handle it
accordingly.
The `Access` module ships with many convenience accessor functions,
like the `all` anonymous function defined above. See `Access.all/0`,
`Access.key/2`, and others as examples.
## Working with structs
@@ -2658,11 +2593,12 @@ defmodule Kernel do
@spec get_in(Access.t(), nonempty_list(term)) :: term
def get_in(data, keys)
def get_in(nil, [_ | _]), do: nil
def get_in(data, [h]) when is_function(h), do: h.(:get, data, & &1)
def get_in(data, [h | t]) when is_function(h), do: h.(:get, data, &get_in(&1, t))
def get_in(nil, [_]), do: nil
def get_in(nil, [_ | t]), do: get_in(nil, t)
def get_in(data, [h]), do: Access.get(data, h)
def get_in(data, [h | t]), do: get_in(Access.get(data, h), t)
@@ -2680,12 +2616,8 @@ defmodule Kernel do
iex> put_in(users, ["john", :age], 28)
%{"john" => %{age: 28}, "meg" => %{age: 23}}
If any of the intermediate values are nil, it will raise:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> put_in(users, ["jane", :age], "oops")
** (ArgumentError) could not put/update key :age on a nil value
In case any of the entries in the middle returns `nil`,
an error will be raised when trying to access it next.
"""
@spec put_in(Access.t(), nonempty_list(term), term) :: Access.t()
def put_in(data, [_ | _] = keys, value) do
@@ -2712,13 +2644,8 @@ defmodule Kernel do
iex> update_in(users, ["john", :age], &(&1 + 1))
%{"john" => %{age: 28}, "meg" => %{age: 23}}
Note the current value given to the anonymous function may be `nil`.
If any of the intermediate values are nil, it will raise:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> update_in(users, ["jane", :age], & &1 + 1)
** (ArgumentError) could not put/update key :age on a nil value
In case any of the entries in the middle returns `nil`,
an error will be raised when trying to access it next.
"""
@spec update_in(Access.t(), nonempty_list(term), (term -> term)) :: Access.t()
def update_in(data, [_ | _] = keys, fun) when is_function(fun) do
@@ -2756,13 +2683,6 @@ defmodule Kernel do
iex> get_and_update_in(users, ["john", :age], &{&1, &1 + 1})
{27, %{"john" => %{age: 28}, "meg" => %{age: 23}}}
Note the current value given to the anonymous function may be `nil`.
If any of the intermediate values are nil, it will raise:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> get_and_update_in(users, ["jane", :age], &{&1, &1 + 1})
** (ArgumentError) could not put/update key :age on a nil value
## Functions as keys
If a key is a function, the function will be invoked passing three
@@ -3248,24 +3168,6 @@ defmodule Kernel do
iex> binding()
[]
Furthermore, remember the pin operator matches _values_, not _patterns_:
match?(%{x: 1}, %{x: 1, y: 2})
#=> true
attrs = %{x: 1}
match?(^attrs, %{x: 1, y: 2})
#=> false
The pin operator will check if the values are equal, using `===/2`, while
patterns have their own rules when matching maps, lists, and so forth.
Such behaviour is not specific to `match?/2`. The following code also
throws an exception:
attrs = %{x: 1}
^attrs = %{x: 1, y: 2}
#=> (MatchError) no match of right hand side value: %{x: 1, y: 2}
"""
defmacro match?(pattern, expr) do
success =
@@ -3308,10 +3210,6 @@ defmodule Kernel do
will be available at compile-time. Custom attributes may be configured to
behave closer to Erlang by using `Module.register_attribute/3`.
> **Important:** Libraries and frameworks should consider prefixing any
> module attributes that are private by underscore, such as `@_my_data`
> so code completion tools do not show them on suggestions and prompts.
Finally, note that attributes can also be read inside functions:
defmodule MyServer do
@@ -3332,39 +3230,11 @@ defmodule Kernel do
time and not at runtime. Check the `Module` module for other functions
to manipulate module attributes.
## Attention! Multiple references of the same attribute
## Compile-time considerations
As mentioned above, every time you read a module attribute, a snapshot
of its current value is taken. Therefore, if you are storing large
values inside module attributes (for example, embedding external files
in module attributes), you should avoid referencing the same attribute
multiple times. For example, don't do this:
@files %{
example1: File.read!("lib/example1.data"),
example2: File.read!("lib/example2.data")
}
def example1, do: @files[:example1]
def example2, do: @files[:example2]
In the above, each reference to `@files` may end-up with a complete
and individual copy of the whole `@files` module attribute. Instead,
reference the module attribute once in a private function:
@files %{
example1: File.read!("lib/example1.data"),
example2: File.read!("lib/example2.data")
}
defp files(), do: @files
def example1, do: files()[:example1]
def example2, do: files()[:example2]
## Attention! Compile-time dependencies
Keep in mind references to other modules, even in module attributes,
generate compile-time dependencies to said modules.
One thing to keep in mind is that references to other modules, even
in module attributes, generate compile-time dependencies to said
modules.
For example, take this common pattern:
@@ -3503,7 +3373,7 @@ defmodule Kernel do
end
try do
:elixir_quote.escape(value, :none, false)
:elixir_quote.escape(value, :default, false)
rescue
ex in [ArgumentError] ->
raise ArgumentError,
@@ -3647,7 +3517,7 @@ defmodule Kernel do
bar
end
Note that `do`-`end` become delimiters. The second example would
Note that `do/end` become delimiters. The second example would
translate to:
if foo do
@@ -4105,51 +3975,6 @@ defmodule Kernel do
:lists.member({macro, arity}, module.__info__(:macros))
end
@doc """
Power operator.
It expects two numbers are input. If the left-hand side is an integer
and the right-hand side is more than or equal to 0, then the result is
integer. Otherwise it returns a float.
## Examples
iex> 2 ** 2
4
iex> 2 ** -4
0.0625
iex> 2.0 ** 2
4.0
iex> 2 ** 2.0
4.0
"""
@doc since: "1.13.0"
@spec integer ** non_neg_integer :: integer
@spec integer ** neg_integer :: float
@spec float ** float :: float
def base ** exponent when is_integer(base) and is_integer(exponent) and exponent >= 0 do
integer_pow(base, 1, exponent)
end
def base ** exponent when is_number(base) and is_number(exponent) do
:math.pow(base, exponent)
end
# https://en.wikipedia.org/wiki/Exponentiation_by_squaring
defp integer_pow(_, _, 0),
do: 1
defp integer_pow(b, a, 1),
do: b * a
defp integer_pow(b, a, e) when :erlang.band(e, 1) == 0,
do: integer_pow(b * b, a, :erlang.bsr(e, 1))
defp integer_pow(b, a, e),
do: integer_pow(b * b, a * b, :erlang.bsr(e, 1))
@doc """
Membership operator. Checks if the element on the left-hand side is a member of the
collection on the right-hand side.
@@ -4184,9 +4009,15 @@ defmodule Kernel do
when x === 1 or x === 2 or x === 3
However, this construct will be inneficient for large lists. In such cases, it
is best to stop using guards and use a more appropriate data structure, such
as `MapSet`.
When using ranges:
when x in 1..3
translates to:
when is_integer(x) and x >= 1 and x <= 3
Note that only integers can be considered inside a range by `in`.
### AST considerations
@@ -4219,12 +4050,22 @@ defmodule Kernel do
false
end
[head | tail] = list ->
# We only expand lists in the body if they are relatively
# short and it is made only of literal expressions.
case not in_body? or small_literal_list?(right) do
true -> in_var(in_body?, left, &in_list(&1, head, tail, expand, list, in_body?))
false -> quote(do: :lists.member(unquote(left), unquote(right)))
[head | tail] = list when not in_body? ->
in_list(left, head, tail, expand, list, in_body?)
[_ | _] = list when in_body? ->
case ensure_evaled(list, {0, []}, expand) do
{[head | tail], {_, []}} ->
in_var(in_body?, left, &in_list(&1, head, tail, expand, list, in_body?))
{[head | tail], {_, vars_values}} ->
{vars, values} = :lists.unzip(:lists.reverse(vars_values))
is_in_list = &in_list(&1, head, tail, expand, list, in_body?)
quote do
{unquote_splicing(vars)} = {unquote_splicing(values)}
unquote(in_var(in_body?, left, is_in_list))
end
end
{:%{}, _meta, [__struct__: Elixir.Range, first: first, last: last, step: step]} ->
@@ -4261,11 +4102,42 @@ defmodule Kernel do
end
end
defp small_literal_list?(list) when is_list(list) and length(list) <= 32 do
:lists.all(fn x -> is_binary(x) or is_atom(x) or is_number(x) end, list)
# Called as ensure_evaled(list, {0, []}). Note acc is reversed.
defp ensure_evaled(list, acc, expand) do
fun = fn
{:|, meta, [head, tail]}, acc ->
{head, acc} = ensure_evaled_element(head, acc)
{tail, acc} = ensure_evaled_tail(expand.(tail), acc, expand)
{{:|, meta, [head, tail]}, acc}
elem, acc ->
ensure_evaled_element(elem, acc)
end
:lists.mapfoldl(fun, acc, list)
end
defp small_literal_list?(_list), do: false
defp ensure_evaled_element(elem, acc)
when is_number(elem) or is_atom(elem) or is_binary(elem) do
{elem, acc}
end
defp ensure_evaled_element(elem, acc) do
ensure_evaled_var(elem, acc)
end
defp ensure_evaled_tail(elem, acc, expand) when is_list(elem) do
ensure_evaled(elem, acc, expand)
end
defp ensure_evaled_tail(elem, acc, _expand) do
ensure_evaled_var(elem, acc)
end
defp ensure_evaled_var(elem, {index, ast}) do
var = {String.to_atom("arg" <> Integer.to_string(index + 1)), [], __MODULE__}
{var, {index + 1, [{var, elem} | ast]}}
end
defp in_range(left, first, last, nil) do
# TODO: nil steps are only supported due to x..y in guards. Remove me on Elixir 2.0.
@@ -4412,7 +4284,7 @@ defmodule Kernel do
defmacro var!({name, meta, atom}, context) when is_atom(name) and is_atom(atom) do
# Remove counter and force them to be vars
meta = :lists.keydelete(:counter, 1, meta)
meta = :lists.keystore(:if_undefined, 1, meta, {:if_undefined, :raise})
meta = :lists.keystore(:var, 1, meta, {:var, true})
case Macro.expand(context, __CALLER__) do
context when is_atom(context) ->
@@ -4536,13 +4408,19 @@ defmodule Kernel do
defmacro defmodule(alias, do: block) do
env = __CALLER__
expanded = expand_module_alias(alias, env)
boot? = bootstrapped?(Macro)
expanded =
case boot? do
true -> Macro.expand(alias, env)
false -> alias
end
{expanded, with_alias} =
case is_atom(expanded) do
case boot? and is_atom(expanded) do
true ->
# Expand the module considering the current environment/nesting
{full, old, new} = alias_defmodule(alias, expanded, env)
{full, old, new} = expand_module(alias, expanded, env)
meta = [defined: full, context: env.module] ++ alias_meta(alias)
{full, {:alias, meta, [old, [as: new, warn: false]]}}
@@ -4568,10 +4446,10 @@ defmodule Kernel do
quote(do: Kernel.LexicalTracker.read_cache(unquote(pid), unquote(integer)))
%{} ->
:elixir_quote.escape(block, :none, false)
:elixir_quote.escape(block, :default, false)
end
module_vars = :lists.map(&module_var/1, :maps.keys(env.versioned_vars))
module_vars = :lists.map(&module_var/1, :maps.keys(elem(env.current_vars, 0)))
quote do
unquote(with_alias)
@@ -4582,34 +4460,16 @@ defmodule Kernel do
defp alias_meta({:__aliases__, meta, _}), do: meta
defp alias_meta(_), do: []
# We don't want to trace :alias_reference since we are defining the alias
defp expand_module_alias({:__aliases__, _, _} = original, env) do
case :elixir_aliases.expand_or_concat(original, env) do
receiver when is_atom(receiver) ->
receiver
aliases ->
aliases = :lists.map(&Macro.expand(&1, env), aliases)
case :lists.all(&is_atom/1, aliases) do
true -> :elixir_aliases.concat(aliases)
false -> original
end
end
end
defp expand_module_alias(other, env), do: Macro.expand(other, env)
# defmodule Elixir.Alias
defp alias_defmodule({:__aliases__, _, [:"Elixir", _ | _]}, module, _env),
defp expand_module({:__aliases__, _, [:"Elixir", _ | _]}, module, _env),
do: {module, module, nil}
# defmodule Alias in root
defp alias_defmodule({:__aliases__, _, _}, module, %{module: nil}),
defp expand_module({:__aliases__, _, _}, module, %{module: nil}),
do: {module, module, nil}
# defmodule Alias nested
defp alias_defmodule({:__aliases__, _, [h | t]}, _module, env) when is_atom(h) do
defp expand_module({:__aliases__, _, [h | t]}, _module, env) when is_atom(h) do
module = :elixir_aliases.concat([env.module, h])
alias = String.to_atom("Elixir." <> Atom.to_string(h))
@@ -4620,7 +4480,7 @@ defmodule Kernel do
end
# defmodule _
defp alias_defmodule(_raw, module, _env) do
defp expand_module(_raw, module, _env) do
{module, module, nil}
end
@@ -4695,15 +4555,15 @@ defmodule Kernel do
Functions containing many arguments can benefit from using `Keyword`
lists to group and pass attributes as a single value.
defmodule MyConfiguration do
@default_opts [storage: "local"]
defmodule MyConfiguration do
@default_opts [storage: "local"]
def configure(resource, opts \\ []) do
opts = Keyword.merge(@default_opts, opts)
storage = opts[:storage]
# ...
end
end
def configure(resource, opts \\ []) do
opts = Keyword.merge(@default_opts, opts)
storage = opts[:storage]
# ...
end
end
The difference between using `Map` and `Keyword` to store many
arguments is `Keyword`'s keys:
@@ -4841,12 +4701,12 @@ defmodule Kernel do
unquoted_call = :elixir_quote.has_unquotes(call)
unquoted_expr = :elixir_quote.has_unquotes(expr)
escaped_call = :elixir_quote.escape(call, :none, true)
escaped_call = :elixir_quote.escape(call, :default, true)
escaped_expr =
case unquoted_expr do
true ->
:elixir_quote.escape(expr, :none, true)
:elixir_quote.escape(expr, :default, true)
false ->
key = :erlang.unique_integer()
@@ -4874,13 +4734,13 @@ defmodule Kernel do
A struct is a tagged map that allows developers to provide
default values for keys, tags to be used in polymorphic
dispatches and compile time assertions. For more information
about structs, please check `Kernel.SpecialForms.%/2`.
dispatches and compile time assertions.
It is only possible to define a struct per module, as the
struct it tied to the module itself. Calling `defstruct/1`
also defines a `__struct__/0` function that returns the
struct itself.
To define a struct, a developer must define both `__struct__/0` and
`__struct__/1` functions. `defstruct/1` is a convenience macro which
defines such functions with some conveniences.
For more information about structs, please check `Kernel.SpecialForms.%/2`.
## Examples
@@ -5183,15 +5043,10 @@ defmodule Kernel do
end
@doc """
Makes the given definitions in the current module overridable.
Makes the given functions in the current module overridable.
If the user defines a new function or macro with the same name
and arity, then the overridable ones are discarded. Otherwise, the
original definitions are used.
It is possible for the overridden definition to have a different visibility
than the original: a public function can be overridden by a private
function and vice-versa.
An overridable function is lazily defined, allowing a developer to override
it.
Macros cannot be overridden as functions and vice-versa.
@@ -6020,9 +5875,6 @@ defmodule Kernel do
iex> ~w(foo bar baz)a
[:foo, :bar, :baz]
iex> ~w(foo bar baz)c
['foo', 'bar', 'baz']
"""
defmacro sigil_w(term, modifiers)
+7 -19
View File
@@ -16,8 +16,6 @@ defmodule Kernel.CLI do
profile: nil
}
@standalone_opts ["-h", "--help", "--short-version"]
@doc """
This is the API invoked by Elixir boot process.
"""
@@ -90,7 +88,7 @@ defmodule Kernel.CLI do
case blamed do
%FunctionClauseError{} ->
formatted = Exception.format_banner(kind, reason, stacktrace)
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/2))
[formatted, padded_blame]
_ ->
@@ -178,8 +176,9 @@ defmodule Kernel.CLI do
IO.write(:stderr, format_error(kind, reason, stacktrace))
end
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
defp blame_match(%{match?: true, node: node}, _), do: blame_ansi(:normal, "+", node)
defp blame_match(%{match?: false, node: node}, _), do: blame_ansi(:red, "-", node)
defp blame_match(_, string), do: string
defp blame_ansi(color, no_ansi, node) do
if IO.ANSI.enabled?() do
@@ -196,9 +195,8 @@ defmodule Kernel.CLI do
end
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
[Kernel.ErrorHandler, Module.ParallelChecker]
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_pass, Kernel.ErrorHandler]
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
prune_stacktrace(t)
@@ -218,16 +216,7 @@ defmodule Kernel.CLI do
# Parse shared options
defp warn_standalone(opt) do
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
end
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
warn_standalone(opt)
System.halt(1)
end
defp parse_shared([opt | t], _config) when opt in ["-v", "--version"] do
defp parse_shared([opt | _t], _config) when opt in ["-v", "--version"] do
if function_exported?(IEx, :started?, 0) and IEx.started?() do
IO.puts("IEx " <> System.build_info()[:build])
else
@@ -235,7 +224,6 @@ defmodule Kernel.CLI do
IO.puts("Elixir " <> System.build_info()[:build])
end
t != [] && warn_standalone(opt)
System.halt(0)
end
+2 -2
View File
@@ -30,10 +30,10 @@ defmodule Kernel.ErrorHandler do
end
def ensure_compiled(module, kind, deadlock) do
{compiler_pid, file_pid} = :erlang.get(:elixir_compiler_info)
parent = :erlang.get(:elixir_compiler_pid)
ref = :erlang.make_ref()
modules = :elixir_module.compiler_modules()
send(compiler_pid, {:waiting, kind, self(), ref, file_pid, module, modules, deadlock})
send(parent, {:waiting, kind, self(), ref, module, modules, deadlock})
:erlang.garbage_collect(self())
receive do
+9 -10
View File
@@ -30,8 +30,8 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def add_export(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_export, module})
def add_require(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_require, module})
end
@doc false
@@ -168,16 +168,15 @@ defmodule Kernel.LexicalTracker do
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
end
def handle_cast({:add_export, module}, state) do
def handle_cast({:add_require, module}, state) do
{:noreply, put_in(state.exports[module], true)}
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
%{directives: directives, exports: exports} = state
to_remove = for {{:import, {^module, _, _}} = key, _} <- directives, do: key
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
directives =
directives
state.directives
|> Map.drop(to_remove)
|> add_directive(module, line, warn, :import)
@@ -186,7 +185,7 @@ defmodule Kernel.LexicalTracker do
add_directive(directives, {module, function, arity}, line, warn, :import)
end)
{:noreply, %{state | directives: directives, exports: Map.put(exports, module, true)}}
{:noreply, %{state | directives: directives}}
end
def handle_cast({:add_alias, module, line, warn}, state) do
@@ -222,9 +221,9 @@ defmodule Kernel.LexicalTracker do
do: Map.put(references, module, :compile)
defp add_reference(references, module, :runtime) when is_atom(module) do
case references do
%{^module => _} -> references
_ -> Map.put(references, module, :runtime)
case Map.fetch(references, module) do
{:ok, _} -> references
:error -> Map.put(references, module, :runtime)
end
end
+78 -105
View File
@@ -3,12 +3,6 @@ defmodule Kernel.ParallelCompiler do
A module responsible for compiling and requiring files in parallel.
"""
@typedoc "The line. 0 indicates no line."
@type line() :: non_neg_integer()
@type location() :: line() | {line(), column :: non_neg_integer}
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
@type error() :: {file :: Path.t(), line(), message :: String.t()}
@doc """
Starts a task for parallel compilation.
@@ -22,28 +16,23 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
def async(fun) when is_function(fun, 0) do
case :erlang.get(:elixir_compiler_info) do
{compiler, _} ->
file = :erlang.get(:elixir_compiler_file)
dest = :erlang.get(:elixir_compiler_dest)
if parent = :erlang.get(:elixir_compiler_pid) do
file = :erlang.get(:elixir_compiler_file)
dest = :erlang.get(:elixir_compiler_dest)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
checker = Module.ParallelChecker.get()
Task.async(fn ->
send(compiler, {:async, self()})
Module.ParallelChecker.put(compiler, checker)
:erlang.put(:elixir_compiler_info, {compiler, self()})
:erlang.put(:elixir_compiler_file, file)
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
:erlang.process_flag(:error_handler, error_handler)
fun.()
end)
:undefined ->
raise ArgumentError,
"cannot spawn parallel compiler task because " <>
"the current file is not being compiled/required"
Task.async(fn ->
send(parent, {:async, self()})
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_compiler_file, file)
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
:erlang.process_flag(:error_handler, error_handler)
fun.()
end)
else
raise ArgumentError,
"cannot spawn parallel compiler task because " <>
"the current file is not being compiled/required"
end
end
@@ -97,7 +86,6 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
@spec compile([Path.t()], keyword()) :: {:ok, [atom], [warning]} | {:error, [error], [warning]}
def compile(files, options \\ []) when is_list(options) do
spawn_workers(files, :compile, options)
end
@@ -108,8 +96,6 @@ defmodule Kernel.ParallelCompiler do
See `compile/2` for more information.
"""
@doc since: "1.6.0"
@spec compile_to_path([Path.t()], Path.t(), keyword()) ::
{:ok, [atom], [warning]} | {:error, [error], [warning]}
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
spawn_workers(files, {:compile, path}, options)
end
@@ -135,21 +121,10 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
@spec require([Path.t()], keyword()) ::
{:ok, [atom], [warning]} | {:error, [error], [warning]}
def require(files, options \\ []) when is_list(options) do
spawn_workers(files, :require, options)
end
@doc """
Prints a warning returned by the compiler.
"""
@doc since: "1.13.0"
@spec print_warning(warning) :: :ok
def print_warning({file, location, warning}) do
:elixir_errors.print_warning(location, file, warning)
end
@doc false
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
def files(files, options \\ []) when is_list(options) do
@@ -171,32 +146,7 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers(files, output, options) do
{:module, _} = :code.ensure_loaded(Kernel.ErrorHandler)
schedulers = max(:erlang.system_info(:schedulers_online), 2)
{:ok, checker} = Module.ParallelChecker.start_link(schedulers)
try do
outcome = spawn_workers(schedulers, checker, files, output, options)
{outcome, Code.get_compiler_option(:warnings_as_errors)}
else
{{:ok, _, [_ | _] = warnings}, true} ->
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
{:error, warnings, []}
{{:ok, outcome, warnings}, _} ->
beam_timestamp = Keyword.get(options, :beam_timestamp)
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
{{:error, errors, warnings}, true} ->
{:error, errors ++ warnings, []}
{{:error, errors, warnings}, _} ->
{:error, errors, warnings}
after
Module.ParallelChecker.stop(checker)
end
end
defp spawn_workers(schedulers, checker, files, output, options) do
beam_timestamp = Keyword.get(options, :beam_timestamp)
threshold = Keyword.get(options, :long_compilation_threshold, 10) * 1000
timer_ref = Process.send_after(self(), :threshold_check, threshold)
@@ -211,8 +161,7 @@ defmodule Kernel.ParallelCompiler do
output: output,
timer_ref: timer_ref,
long_compilation_threshold: threshold,
schedulers: schedulers,
checker: checker
schedulers: schedulers
})
Process.cancel_timer(state.timer_ref)
@@ -223,7 +172,21 @@ defmodule Kernel.ParallelCompiler do
0 -> :ok
end
outcome
case {outcome, Code.get_compiler_option(:warnings_as_errors)} do
{{:ok, _, [_ | _] = warnings}, true} ->
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
{:error, warnings, []}
{{:ok, outcome, warnings}, _} ->
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
{{:error, errors, warnings}, true} ->
{:error, errors ++ warnings, []}
{{:error, errors, warnings}, _} ->
{:error, errors, warnings}
end
end
defp each_file(fun) when is_function(fun, 1), do: fn file, _ -> fun.(file) end
@@ -265,21 +228,32 @@ defmodule Kernel.ParallelCompiler do
end
defp maybe_check_modules(result, runtime_modules, state) do
%{profile: profile, checker: checker} = state
%{schedulers: schedulers, profile: profile} = state
compiled_modules =
for {{:module, _module}, {_binary, info}} <- result,
do: info
if :elixir_config.get(:bootstrap) do
[]
else
compiled_modules = checker_compiled_modules(result)
runtime_modules = checker_runtime_modules(runtime_modules)
runtime_modules =
for module <- runtime_modules,
path = :code.which(module),
is_list(path) and path != [],
do: {module, path}
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(compiled_modules, runtime_modules, schedulers)
end)
end
end
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(checker, compiled_modules, runtime_modules)
end)
defp checker_compiled_modules(result) do
for {{:module, _module}, {binary, module_map}} <- result do
{module_map, binary}
end
end
defp checker_runtime_modules(modules) do
for module <- modules,
path = :code.which(module),
is_list(path) and path != [] do
{module, File.read!(path)}
end
end
defp profile_init(:time), do: {:time, System.monotonic_time(), 0}
@@ -317,9 +291,9 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers([{ref, found} | t], spawned, waiting, files, result, warnings, state) do
{files, waiting} =
case List.keytake(waiting, ref, 2) do
{{_kind, pid, ^ref, file_pid, _on, _defining, _deadlock}, waiting} ->
{{_kind, pid, ^ref, _on, _defining, _deadlock}, waiting} ->
send(pid, {ref, found})
{update_timing(files, file_pid, :waiting), waiting}
{update_timing(files, pid, :waiting), waiting}
nil ->
# In case the waiting process died (for example, it was an async process),
@@ -331,14 +305,13 @@ defmodule Kernel.ParallelCompiler do
end
defp spawn_workers([file | queue], spawned, waiting, files, result, warnings, state) do
%{output: output, dest: dest, checker: checker} = state
%{output: output, dest: dest} = state
parent = self()
file = Path.expand(file)
{pid, ref} =
:erlang.spawn_monitor(fn ->
Module.ParallelChecker.put(parent, checker)
:erlang.put(:elixir_compiler_info, {parent, self()})
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_compiler_file, file)
try do
@@ -392,7 +365,7 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers(
[],
1,
[{_, pid, ref, _, _, _, _}] = waiting,
[{_, pid, ref, _, _, _}] = waiting,
[%{pid: pid}] = files,
result,
warnings,
@@ -486,11 +459,11 @@ defmodule Kernel.ParallelCompiler do
# The goal of this function is to find leaves in the dependency graph,
# i.e. to find code that depends on code that we know is not being defined.
# Note that not all files have been compiled yet, so they may not be in waiting.
# Note that not all files have been compile yet, so they may not be in waiting.
defp without_definition(waiting, files) do
nillify_empty(
for %{pid: pid} <- files,
{_, _, ref, ^pid, on, _, _} <- waiting,
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
not defining?(on, waiting),
do: {ref, :not_found}
)
@@ -498,14 +471,14 @@ defmodule Kernel.ParallelCompiler do
defp deadlocked(waiting, type, defining?) do
nillify_empty(
for {_, _, ref, _, on, _, ^type} <- waiting,
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)
Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end)
end
defp nillify_empty([]), do: nil
@@ -522,41 +495,41 @@ defmodule Kernel.ParallelCompiler do
{:available, kind, module} ->
available =
for {^kind, _, ref, _, ^module, _defining, _deadlock} <- waiting,
for {^kind, _, ref, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {kind, module}, true)
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
{:module_available, child, ref, file, module, binary, checker_info} ->
{:module_available, child, ref, file, module, binary, module_map} ->
state.each_module.(file, module, binary)
# Release the module loader which is waiting for an ack
send(child, {ref, :ack})
available =
for {:module, _, ref, _, ^module, _defining, _deadlock} <- waiting,
for {:module, _, ref, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {:module, module}, {binary, checker_info})
result = Map.put(result, {:module, module}, {binary, module_map})
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
# If we are simply requiring files, we do not add to waiting.
{:waiting, _kind, child, ref, _file_pid, _on, _defining, _deadlock} when output == :require ->
{:waiting, _kind, child, ref, _on, _defining, _deadlock} when output == :require ->
send(child, {ref, :not_found})
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
{:waiting, kind, child_pid, ref, file_pid, on, defining, deadlock?} ->
{:waiting, kind, child, ref, on, defining, deadlock?} ->
# If we already got what we were waiting for, do not put it on waiting.
# If we're waiting on ourselves, send :found so that we can crash with
# a better error.
{files, waiting} =
if Map.has_key?(result, {kind, on}) or on in defining do
send(child_pid, {ref, :found})
send(child, {ref, :found})
{files, waiting}
else
files = update_timing(files, file_pid, :compiling)
{files, [{kind, child_pid, ref, file_pid, on, defining, deadlock?} | waiting]}
files = update_timing(files, child, :compiling)
{files, [{kind, child, ref, on, defining, deadlock?} | waiting]}
end
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
@@ -577,10 +550,10 @@ defmodule Kernel.ParallelCompiler do
state = %{state | timer_ref: timer_ref}
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
{:warning, file, location, message} ->
{:warning, file, line, message} ->
file = file && Path.absname(file)
message = :unicode.characters_to_binary(message)
warning = {file, location, message}
warning = {file, line, message}
wait_for_messages(queue, spawned, waiting, files, result, [warning | warnings], state)
{:file_ok, child_pid, ref, file, lexical} ->
@@ -702,7 +675,7 @@ defmodule Kernel.ParallelCompiler do
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
Process.exit(pid, :kill)
{kind, ^pid, _, _, on, _, _} = List.keyfind(waiting, pid, 1)
{kind, ^pid, _, on, _, _} = List.keyfind(waiting, pid, 1)
description = "deadlocked waiting on #{kind} #{inspect(on)}"
error = CompileError.exception(description: description, file: nil, line: nil)
print_error(file, :error, error, stacktrace)
@@ -749,7 +722,7 @@ defmodule Kernel.ParallelCompiler do
line = get_line(file, reason, stack)
file = Path.absname(file)
message = :unicode.characters_to_binary(Kernel.CLI.format_error(kind, reason, stack))
{file, line || 0, message}
{file, line, message}
end
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
+6 -57
View File
@@ -554,9 +554,7 @@ defmodule Kernel.SpecialForms do
defmacro alias(module, opts), do: error!([module, opts])
@doc """
Requires a module as a compile-time dependency.
Requiring a module is necessary in order to use its macros.
Requires a module in order to use its macros.
## Examples
@@ -578,6 +576,7 @@ defmodule Kernel.SpecialForms do
`require/2` also accepts `:as` as an option so it automatically sets
up an alias. Please check `alias/2` for more information.
"""
defmacro require(module, opts), do: error!([module, opts])
@@ -1212,12 +1211,6 @@ defmodule Kernel.SpecialForms do
reported to where `defadd` was invoked. `location: :keep` affects
only definitions inside the quote.
> **Important:** do not use location: :keep if the function definition
> also `unquote`s some of the macro arguments. If you do so, Elixir
> will store the file definition of the current location but the
> unquoted arguments may contain line information of the macro caller,
> leading to erroneous stacktraces.
## Binding and unquote fragments
Elixir quote/unquote mechanisms provide a functionality called
@@ -1526,15 +1519,15 @@ defmodule Kernel.SpecialForms do
iex> width
nil
The behaviour of any expression in a clause is the same as if it was
written outside of `with`. For example, `=` will raise a `MatchError`
instead of returning the non-matched value:
The behaviour of any expression in a clause is the same as outside.
For example, `=` will raise a `MatchError` instead of returning the
non-matched value:
with :foo = :bar, do: :ok
** (MatchError) no match of right hand side value: :bar
As with any other function or macro call in Elixir, explicit parens can
also be used around the arguments before the `do`-`end` block:
also be used around the arguments before the `do`/`end` block:
iex> opts = %{width: 10, height: 15}
iex> with(
@@ -1547,8 +1540,6 @@ defmodule Kernel.SpecialForms do
The choice between parens and no parens is a matter of preference.
## Else clauses
An `else` option can be given to modify what is being returned from
`with` in the case of a failed match:
@@ -1571,48 +1562,6 @@ defmodule Kernel.SpecialForms do
If an `else` block is used and there are no matching clauses, a `WithClauseError`
exception is raised.
### Beware!
Keep in mind that, one of potential drawback of `with` is that all
failure clauses are flattened into a single `else` block. For example,
take this code that checks if a given path points to an Elixir file
and that it exists before creating a backup copy:
with ".ex" <- Path.extname(path),
true <- File.exists?(path) do
backup_path = path <> ".backup"
File.cp!(path, backup_path)
{:ok, backup_path}
else
binary when is_binary(binary) ->
{:error, :invalid_extension}
false ->
{:error, :missing_file}
end
Note how we are having to reconstruct the result types of `Path.extname/1`
and `File.exists?/1` to build error messages. In this case, it is better
to change the with clauses to already return the desired format, like this:
with :ok <- validate_extension(path),
:ok <- validate_exists(path) do
backup_path = path <> ".backup"
File.cp!(path, backup_path)
{:ok, backup_path}
end
defp validate_extname(path) do
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
end
defp validate_exists(path) do
if File.exists?(path), do: :ok, else: {:error, :missing_file}
end
Note how the code above is better organized and clearer once we
make sure each clause in `with` returns a normalized format.
"""
defmacro with(args), do: error!([args])
+4 -2
View File
@@ -553,7 +553,9 @@ defmodule Kernel.Typespec do
end
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller, state) do
module = Macro.expand(name, %{caller | function: {:__info__, 1}})
# We cannot set a function name to avoid tracking
# as a compile time dependency, because for structs it actually is one.
module = Macro.expand(name, caller)
struct =
module
@@ -575,7 +577,7 @@ defmodule Kernel.Typespec do
unless Keyword.has_key?(struct, field) do
compile_error(
caller,
"undefined field #{inspect(field)} on struct #{inspect(module)}"
"undefined field #{inspect(field)} on struct #{Macro.to_string(name)}"
)
end
end
+6 -13
View File
@@ -29,16 +29,9 @@ defmodule Kernel.Utils do
append_first? = Keyword.get(opts, :append_first, false)
{name, args} =
case fun do
{:when, _, [_left, right]} ->
raise ArgumentError,
"guards are not allowed in defdelegate/2, got: when #{Macro.to_string(right)}"
_ ->
case Macro.decompose_call(fun) do
{_, _} = pair -> pair
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
end
case Macro.decompose_call(fun) do
{_, _} = pair -> pair
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
end
as = Keyword.get(opts, :as, name)
@@ -144,9 +137,9 @@ defmodule Kernel.Utils do
Announcing callback for defstruct.
"""
def announce_struct(module) do
case :erlang.get(:elixir_compiler_info) do
case :erlang.get(:elixir_compiler_pid) do
:undefined -> :ok
{pid, _} -> send(pid, {:available, :struct, module})
pid -> send(pid, {:available, :struct, module})
end
end
@@ -226,7 +219,7 @@ defmodule Kernel.Utils do
def defguard(args, expr, env) do
{^args, vars} = extract_refs_from_args(args)
env = :elixir_env.with_vars(%{env | context: :guard}, vars)
{expr, _, _} = :elixir_expand.expand(expr, :elixir_env.env_to_ex(env), env)
{expr, _scope} = :elixir_expand.expand(expr, env)
quote do
case Macro.Env.in_guard?(__CALLER__) do
+126 -318
View File
@@ -13,43 +13,45 @@ defmodule Keyword do
[{:exit_on_close, true}, {:active, :once}, {:packet_size, 1024}]
Elixir provides a special and more concise syntax for keyword lists:
Elixir provides a special and more concise syntax for keyword lists
that looks like this:
[exit_on_close: true, active: :once, packet_size: 1024]
The two syntaxes return the exact same value.
A *key* can be any atom, consisting of Unicode letters, numbers,
an underscore or the `@` sign. If the *key* should have any other
characters, such as spaces, you can wrap it in quotes:
The two syntaxes are completely equivalent. Like atoms, keyword
lists keys must be composed of Unicode characters such as letters,
numbers, underscore, and `@`. If the keyword has a character that
does not belong to the category above, such as spaces, you can wrap
it in quotes:
iex> ["exit on close": true]
["exit on close": true]
Wrapping an atom in quotes does not make it a string. Keyword list
*keys* are always atoms. Quotes should only be used when necessary
or Elixir will issue a warning.
Wrapping a keyword in quotes does not make it a string. Keyword lists
keys are always atoms. If you use quotes around the key when quoting
is not necessary, Elixir will warn.
## Duplicate keys and ordering
A keyword may have duplicate keys so it is not strictly a key-value
data type. However most of the functions in this module work on a
key-value structure and behave similar to the functions you would
find in the `Map` module. For example, `Keyword.get/3` will get the first
entry matching the given key, regardless if duplicate entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicate
A keyword may have duplicated keys so it is not strictly a key-value
data type. However most of the functions in this module behave exactly
as a key-value so they work similarly to the functions you would find
in the `Map` module. For example, `Keyword.get/3` will get the first
entry matching the given key, regardless if duplicated entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicated
entries for a given key are removed when invoked. Note, however, that
keyword list operations need to traverse the whole list in order to find
keys, so these operations are slower than their map counterparts.
A handful of functions exist to handle duplicate keys, for example,
A handful of functions exist to handle duplicated keys, for example,
`get_values/2` returns all values for a given key and `delete_first/2`
deletes just the first entry of the existing ones.
deletes just one of the existing entries.
Even though lists preserve the existing order, the functions in
Even though lists preserve the user ordering, the functions in
`Keyword` do not guarantee any ordering. For example, if you invoke
`Keyword.put(opts, new_key, new_value)`, there is no guarantee for
where `new_key` will be added to (the front, the end or anywhere else).
`Keyword.put(opts, new_key, new_value)`, there is no guarantee to
where `new_key` will be added (to the front, to the end, or
anywhere else).
Given ordering is not guaranteed, it is not recommended to pattern
match on keyword lists either. For example, a function such as:
@@ -65,7 +67,7 @@ defmodule Keyword do
my_function([another_key: :bar, some_key: :foo])
Most of the functions in this module work in linear time. This means
that the time it takes to perform an operation grows at the same
that, the time it takes to perform an operation grows at the same
rate as the length of the list.
## Call syntax
@@ -81,8 +83,8 @@ defmodule Keyword do
String.split("1-0", "-", trim: true, parts: 2)
Since tuples, lists and maps are treated similarly to function
arguments in Elixir syntax, this property is also available to them:
Since tuples, lists, maps, and others are treated the same as function
calls in Elixir syntax, this property is also available to them:
iex> {1, 2, foo: :bar}
{1, 2, [{:foo, :bar}]}
@@ -103,9 +105,7 @@ defmodule Keyword do
@type t(value) :: [{key, value}]
@doc """
Returns `true` if `term` is a keyword list, otherwise `false`.
When `term` is a list it is traversed to the end.
Returns `true` if `term` is a keyword list; otherwise returns `false`.
## Examples
@@ -145,7 +145,7 @@ defmodule Keyword do
@doc """
Creates a keyword list from an enumerable.
Removes duplicate entries and the last one prevails.
Duplicated entries are removed, the latest one prevails.
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
guarantees the keys are unique.
@@ -166,7 +166,7 @@ defmodule Keyword do
@doc """
Creates a keyword list from an enumerable via the transformation function.
Removes duplicate entries and the last one prevails.
Duplicated entries are removed, the latest one prevails.
Unlike `Enum.into(enumerable, [], fun)`,
`Keyword.new(enumerable, fun)` guarantees the keys are unique.
@@ -187,146 +187,12 @@ defmodule Keyword do
end
@doc """
Ensures the first argument is a `keyword` with the given
keys and default values.
Gets the value for a specific `key`.
The second argument must be a list of atoms, specifying
a given key, or tuples specifying a key and a default value.
If `key` does not exist, return the default value
(`nil` if no default value).
If the keyword list has only the given keys, it returns
`{:ok, keyword}` with default values applied. Otherwise it
returns `{:error, invalid_keys}` with invalid keys.
See also: `validate!/2`.
## Examples
iex> {:ok, result} = Keyword.validate([], [one: 1, two: 2])
iex> Enum.sort(result)
[one: 1, two: 2]
iex> {:ok, result} = Keyword.validate([two: 3], [one: 1, two: 2])
iex> Enum.sort(result)
[one: 1, two: 3]
If atoms are given, they are supported as keys but do not
provide a default value:
iex> {:ok, result} = Keyword.validate([], [:one, two: 2])
iex> Enum.sort(result)
[two: 2]
iex> {:ok, result} = Keyword.validate([one: 1], [:one, two: 2])
iex> Enum.sort(result)
[one: 1, two: 2]
Passing unknown keys returns an error:
iex> Keyword.validate([three: 3, four: 4], [one: 1, two: 2])
{:error, [:four, :three]}
"""
@doc since: "1.13.0"
@spec validate(keyword(), values :: [atom() | {atom(), term()}]) ::
{:ok, keyword()} | {:error, [atom]}
def validate(keyword, values) when is_list(keyword) and is_list(values) do
validate(keyword, values, [], [], [])
end
defp validate([{key, _} = pair | keyword], values1, values2, acc, bad_keys) when is_atom(key) do
case find_key!(key, values1, values2) do
{values1, values2} ->
validate(keyword, values1, values2, [pair | acc], bad_keys)
:error ->
case find_key!(key, values2, values1) do
{values1, values2} ->
validate(keyword, values1, values2, [pair | acc], bad_keys)
:error ->
validate(keyword, values1, values2, acc, [key | bad_keys])
end
end
end
defp validate([], values1, values2, acc, []) do
{:ok, move_pairs!(values1, move_pairs!(values2, acc))}
end
defp validate([], _values1, _values2, _acc, bad_keys) do
{:error, bad_keys}
end
defp validate([pair | _], _values1, _values2, _acc, []) do
raise ArgumentError,
"expected a keyword list as first argument, got invalid entry: #{inspect(pair)}"
end
defp find_key!(key, [key | rest], acc), do: {rest, acc}
defp find_key!(key, [{key, _} | rest], acc), do: {rest, acc}
defp find_key!(key, [head | tail], acc), do: find_key!(key, tail, [head | acc])
defp find_key!(_key, [], _acc), do: :error
defp move_pairs!([key | rest], acc) when is_atom(key),
do: move_pairs!(rest, acc)
defp move_pairs!([{key, _} = pair | rest], acc) when is_atom(key),
do: move_pairs!(rest, [pair | acc])
defp move_pairs!([], acc),
do: acc
defp move_pairs!([other | _], _) do
raise ArgumentError,
"expected the second argument to be a list of atoms or tuples, got: #{inspect(other)}"
end
@doc """
Similar to `validate/2` but returns the keyword or raises an error.
## Examples
iex> Keyword.validate!([], [one: 1, two: 2]) |> Enum.sort()
[one: 1, two: 2]
iex> Keyword.validate!([two: 3], [one: 1, two: 2]) |> Enum.sort()
[one: 1, two: 3]
If atoms are given, they are supported as keys but do not
provide a default value:
iex> Keyword.validate!([], [:one, two: 2]) |> Enum.sort()
[two: 2]
iex> Keyword.validate!([one: 1], [:one, two: 2]) |> Enum.sort()
[one: 1, two: 2]
Passing unknown keys raises an error:
iex> Keyword.validate!([three: 3], [one: 1, two: 2])
** (ArgumentError) unknown keys [:three] in [three: 3], the allowed keys are: [:one, :two]
"""
@doc since: "1.13.0"
@spec validate!(keyword(), values :: [atom() | {atom(), term()}]) :: keyword()
def validate!(keyword, values) do
case validate(keyword, values) do
{:ok, kw} ->
kw
{:error, invalid_keys} ->
keys =
for value <- values,
do: if(is_atom(value), do: value, else: elem(value, 0))
raise ArgumentError,
"unknown keys #{inspect(invalid_keys)} in #{inspect(keyword)}, the allowed keys are: #{inspect(keys)}"
end
end
@doc """
Gets the value under the given `key`.
Returns the default value if `key` does not exist
(`nil` if no default value is provided).
If duplicate entries exist, it returns the first one.
If duplicated entries exist, the first one is returned.
Use `get_values/2` to retrieve all entries.
## Examples
@@ -340,7 +206,7 @@ defmodule Keyword do
iex> Keyword.get([a: 1], :b, 3)
3
With duplicate keys:
With duplicated keys:
iex> Keyword.get([a: 1, a: 2], :a, 3)
1
@@ -357,14 +223,14 @@ defmodule Keyword do
end
@doc """
Gets the value under the given `key`.
Gets the value for a specific `key`.
If `key` does not exist, lazily evaluates `fun` and returns its result.
This is useful if the default value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
If duplicate entries exist, it returns the first one.
If duplicated entries exist, the first one is returned.
Use `get_values/2` to retrieve all entries.
## Examples
@@ -392,14 +258,14 @@ defmodule Keyword do
@doc """
Gets the value from `key` and updates it, all in one pass.
The `fun` argument receives the value of `key` (or `nil` if `key`
This `fun` argument receives the value of `key` (or `nil` if `key`
is not present) and must return a two-element tuple: the current value
(the retrieved value, which can be operated on before being returned)
and the new value to be stored under `key`. The `fun` may also
return `:pop`, implying the current value shall be removed from the
keyword list and returned.
Returns a tuple that contains the current value returned by
The returned value is a tuple with the current value returned by
`fun` and a new keyword list with the updated value under `key`.
## Examples
@@ -414,11 +280,6 @@ defmodule Keyword do
...> end)
{nil, [b: "new value!", a: 1]}
iex> Keyword.get_and_update([a: 2], :a, fn number ->
...> {2 * number, 3 * number}
...> end)
{4, [a: 6]}
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
{1, []}
@@ -462,15 +323,15 @@ defmodule Keyword do
end
@doc """
Gets the value under `key` and updates it. Raises if there is no `key`.
Gets the value from `key` and updates it. Raises if there is no `key`.
The `fun` argument receives the value under `key` and must return a
This `fun` argument receives the value of `key` and must return a
two-element tuple: the current value (the retrieved value, which can be
operated on before being returned) and the new value to be stored under
`key`.
Returns a tuple that contains the current value returned by
`fun` and a new keyword list with the updated value under `key`.
The returned value is a tuple with the current value returned by `fun` and a new
keyword list with the updated value under `key`.
## Examples
@@ -515,13 +376,13 @@ defmodule Keyword do
end
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
raise KeyError, key: key, term: acc
raise(KeyError, key: key, term: acc)
end
@doc """
Fetches the value for a specific `key` and returns it in a tuple.
If the `key` does not exist, it returns `:error`.
If the `key` does not exist, returns `:error`.
## Examples
@@ -542,7 +403,7 @@ defmodule Keyword do
@doc """
Fetches the value for specific `key`.
If the `key` does not exist, it raises a `KeyError`.
If `key` does not exist, a `KeyError` is raised.
## Examples
@@ -556,12 +417,12 @@ defmodule Keyword do
def fetch!(keywords, key) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, value} -> value
false -> raise KeyError, key: key, term: keywords
false -> raise(KeyError, key: key, term: keywords)
end
end
@doc """
Gets all values under a specific `key`.
Gets all values for a specific `key`.
## Examples
@@ -585,7 +446,7 @@ defmodule Keyword do
@doc """
Returns all keys from the keyword list.
Keeps duplicate keys in the resulting list of keys.
Duplicated keys appear duplicated in the final list of keys.
## Examples
@@ -601,24 +462,26 @@ defmodule Keyword do
"""
@spec keys(t) :: [key]
def keys(keywords) when is_list(keywords) do
:lists.map(
fn
{key, _} when is_atom(key) -> key
element -> throw(element)
end,
keywords
)
catch
element ->
raise ArgumentError,
"expected a keyword list, but an entry in the list is not a two-element tuple " <>
"with an atom as its first element, got: #{inspect(element)}"
try do
:lists.map(
fn
{key, _} when is_atom(key) -> key
element -> throw(element)
end,
keywords
)
catch
element ->
raise ArgumentError,
"expected a keyword list, but an entry in the list is not a two-element tuple with an atom as its first element, " <>
"got: #{inspect(element)}"
end
end
@doc """
Returns all values from the keyword list.
Keeps values from duplicate keys in the resulting list of values.
Values from duplicated keys will be kept in the final list of values.
## Examples
@@ -655,11 +518,11 @@ defmodule Keyword do
end
@doc """
Deletes the entries in the keyword list under a specific `key`.
Deletes the entries in the keyword list for a specific `key`.
If the `key` does not exist, it returns the keyword list unchanged.
If the `key` does not exist, returns the keyword list unchanged.
Use `delete_first/2` to delete just the first entry in case of
duplicate keys.
duplicated keys.
## Examples
@@ -685,9 +548,9 @@ defmodule Keyword do
defp delete_key([], _key), do: []
@doc """
Deletes the first entry in the keyword list under a specific `key`.
Deletes the first entry in the keyword list for a specific `key`.
If the `key` does not exist, it returns the keyword list unchanged.
If the `key` does not exist, returns the keyword list unchanged.
## Examples
@@ -718,10 +581,10 @@ defmodule Keyword do
end
@doc """
Puts the given `value` under the specified `key`.
Puts the given `value` under `key`.
If a value under `key` already exists, it overrides the value
and removes all duplicate entries.
If a previous value is already stored, all entries are
removed and the value is overridden.
## Examples
@@ -743,19 +606,19 @@ defmodule Keyword do
in keyword list unless `key` is already present.
This is useful if the value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
## Examples
iex> keyword = [a: 1]
iex> fun = fn ->
...> # some expensive operation here
...> 13
...> 3
...> end
iex> Keyword.put_new_lazy(keyword, :a, fun)
[a: 1]
iex> Keyword.put_new_lazy(keyword, :b, fun)
[b: 13, a: 1]
[b: 3, a: 1]
"""
@spec put_new_lazy(t, key, (() -> value)) :: t
@@ -768,7 +631,8 @@ defmodule Keyword do
end
@doc """
Puts the given `value` under `key`, unless the entry `key` already exists.
Puts the given `value` under `key` unless the entry `key`
already exists.
## Examples
@@ -789,8 +653,8 @@ defmodule Keyword do
@doc """
Puts a value under `key` only if the `key` already exists in `keywords`.
In case a key exists multiple times in the keyword list,
it removes later occurrences.
In the case a value is stored multiple times in the keyword list,
later occurrences are removed.
## Examples
@@ -822,7 +686,7 @@ defmodule Keyword do
@doc """
Puts a value under `key` only if the `key` already exists in `keywords`.
If `key` is not present in `keywords`, it raises a `KeyError`.
If `key` is not present in `keywords`, a `KeyError` exception is raised.
## Examples
@@ -850,13 +714,13 @@ defmodule Keyword do
end
defp replace!([], key, _value, original) do
raise KeyError, key: key, term: original
raise(KeyError, key: key, term: original)
end
@doc """
Checks if two keywords are equal.
Considers two keywords to be equal if they contain
Two keywords are considered to be equal if they contain
the same keys and those keys contain the same values.
## Examples
@@ -883,10 +747,10 @@ defmodule Keyword do
@doc """
Merges two keyword lists into one.
Adds all keys, including duplicate keys, given in `keywords2`
to `keywords1`, overriding any existing ones.
All keys, including duplicated keys, given in `keywords2` will be added
to `keywords1`, overriding any existing one.
There are no guarantees about the order of the keys in the returned keyword.
There are no guarantees about the order of keys in the returned keyword.
## Examples
@@ -927,13 +791,13 @@ defmodule Keyword do
@doc """
Merges two keyword lists into one.
Adds all keys, including duplicate keys, given in `keywords2`
to `keywords1`. Invokes the given function to solve conflicts.
All keys, including duplicated keys, given in `keywords2` will be added
to `keywords1`. The given function will be invoked to solve conflicts.
If `keywords2` has duplicate keys, it invokes the given function
If `keywords2` has duplicate keys, the given function will be invoked
for each matching pair in `keywords1`.
There are no guarantees about the order of the keys in the returned keyword.
There are no guarantees about the order of keys in the returned keyword.
## Examples
@@ -1007,11 +871,12 @@ defmodule Keyword do
end
@doc """
Updates the value under `key` using the given function.
Updates the `key` with the given function.
Raises `KeyError` if the `key` does not exist.
If the `key` does not exist, raises `KeyError`.
Removes all duplicate keys and only updates the first one.
If there are duplicated keys, they are all removed and only the first one
is updated.
## Examples
@@ -1039,16 +904,18 @@ defmodule Keyword do
end
defp update!([], key, _fun, original) do
raise KeyError, key: key, term: original
raise(KeyError, key: key, term: original)
end
@doc """
Updates the value under `key` in `keywords` using the given function.
Updates the `key` in `keywords` with the given function.
If the `key` does not exist, it inserts the given `default` value.
Does not pass the `default` value through the update function.
Removes all duplicate keys and only updates the first one.
If there are duplicated keys, they are all removed and only the first one
is updated.
The default value will not be passed through the update function.
## Examples
@@ -1081,14 +948,14 @@ defmodule Keyword do
end
@doc """
Takes all entries corresponding to the given `keys` and extracts them into a
Takes all entries corresponding to the given keys and extracts them into a
separate keyword list.
Returns a tuple with the new list and the old list with removed keys.
Ignores keys for which there are no entries in the keyword list.
Keys for which there are no entries in the keyword list are ignored.
Entries with duplicate keys end up in the same keyword list.
Entries with duplicated keys end up in the same keyword list.
## Examples
@@ -1113,10 +980,10 @@ defmodule Keyword do
end
@doc """
Takes all entries corresponding to the given `keys` and returns them as a new
Takes all entries corresponding to the given keys and returns them in a new
keyword list.
Preserves duplicate keys in the new keyword list.
Duplicated keys are preserved in the new keyword list.
## Examples
@@ -1132,14 +999,12 @@ defmodule Keyword do
end
@doc """
Drops the given `keys` from the keyword list.
Drops the given keys from the keyword list.
Removes duplicate keys from the new keyword list.
Duplicated keys are preserved in the new keyword list.
## Examples
iex> Keyword.drop([a: 1, a: 2], [:a])
[]
iex> Keyword.drop([a: 1, b: 2, c: 3], [:b, :d])
[a: 1, c: 3]
iex> Keyword.drop([a: 1, b: 2, b: 3, c: 3, a: 5], [:b, :d])
@@ -1148,7 +1013,7 @@ defmodule Keyword do
"""
@spec drop(t, [key]) :: t
def drop(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> k not in keys end, keywords)
:lists.filter(fn {key, _} -> key not in keys end, keywords)
end
@doc """
@@ -1156,10 +1021,11 @@ defmodule Keyword do
It returns a tuple where the first element is the first value for `key` and the
second element is a keyword list with all entries associated with `key` removed.
If the `key` is not present in the keyword list, it returns `{default, keyword_list}`.
If the `key` is not present in the keyword list, `{default, keyword_list}` is
returned.
If you don't want to remove all the entries associated with `key` use `pop_first/3`
instead, which will remove only the first entry.
instead, that function will remove only the first entry.
## Examples
@@ -1185,7 +1051,7 @@ defmodule Keyword do
Returns the first value for `key` and removes all associated entries in the keyword list,
raising if `key` is not present.
This function behaves like `pop/3`, but raises in case the `key` is not present in the
This function behaves like `pop/3`, but raises in cases the `key` is not present in the
given `keywords`.
## Examples
@@ -1212,10 +1078,11 @@ defmodule Keyword do
It returns a tuple where the first element is a list of values for `key` and the
second element is a keyword list with all entries associated with `key` removed.
If the `key` is not present in the keyword list, it returns `{[], keyword_list}`.
If the `key` is not present in the keyword list, `{[], keyword_list}` is
returned.
If you don't want to remove all the entries associated with `key` use `pop_first/3`
instead, which will remove only the first entry.
instead, that function will remove only the first entry.
## Examples
@@ -1246,9 +1113,10 @@ defmodule Keyword do
Lazily returns and removes all values associated with `key` in the keyword list.
This is useful if the default value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
Removes all duplicate keys. See `pop_first/3` for removing only the first entry.
All duplicated keys are removed. See `pop_first/3` for
removing only the first entry.
## Examples
@@ -1267,15 +1135,18 @@ defmodule Keyword do
def pop_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
case fetch(keywords, key) do
{:ok, value} -> {value, delete(keywords, key)}
:error -> {fun.(), keywords}
{:ok, value} ->
{value, delete(keywords, key)}
:error ->
{fun.(), keywords}
end
end
@doc """
Returns and removes the first value associated with `key` in the keyword list.
Keeps duplicate keys in the resulting keyword list.
Duplicated keys are not removed.
## Examples
@@ -1307,76 +1178,13 @@ defmodule Keyword do
"""
@spec to_list(t) :: t
def to_list(keywords) when is_list(keywords) do
keywords
def to_list(keyword) when is_list(keyword) do
keyword
end
@doc false
@deprecated "Use Kernel.length/1 instead"
def size(keywords) do
length(keywords)
end
@doc """
Returns a keyword list containing only the entries from `keywords`
for which the function `fun` returns a truthy value.
See also `reject/2` which discards all entries where the function
returns a truthy value.
## Examples
iex> Keyword.filter([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
[one: 1, three: 3]
"""
@doc since: "1.13.0"
@spec filter(t, ({key, value} -> as_boolean(term))) :: t
def filter(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
do_filter(keywords, fun)
end
defp do_filter([], _fun), do: []
defp do_filter([{_, _} = entry | entries], fun) do
if fun.(entry) do
[entry | do_filter(entries, fun)]
else
do_filter(entries, fun)
end
end
@doc """
Returns a keyword list excluding the entries from `keywords`
for which the function `fun` returns a truthy value.
See also `filter/2`.
## Examples
iex> Keyword.reject([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
[two: 2]
"""
@doc since: "1.13.0"
@spec reject(t, ({key, value} -> as_boolean(term))) :: t
def reject(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
do_reject(keywords, fun)
end
defp do_reject([], _fun), do: []
defp do_reject([{_, _} = entry | entries], fun) do
if fun.(entry) do
do_reject(entries, fun)
else
[entry | do_reject(entries, fun)]
end
end
@doc false
@deprecated "Use Keyword.new/2 instead"
def map(keywords, fun) when is_list(keywords) do
Enum.map(keywords, fn {k, v} -> {k, fun.({k, v})} end)
def size(keyword) do
length(keyword)
end
end
+9 -45
View File
@@ -230,7 +230,7 @@ defmodule List do
@doc """
Folds (reduces) the given list from the left with
a function. Requires an accumulator, which can be any value.
a function. Requires an accumulator.
## Examples
@@ -239,9 +239,6 @@ defmodule List do
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
2
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
{6, -6}
"""
@spec foldl([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
@@ -251,15 +248,12 @@ defmodule List do
@doc """
Folds (reduces) the given list from the right with
a function. Requires an accumulator, which can be any value.
a function. Requires an accumulator.
## Examples
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
-2
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
%{product: 24, sum: 10}
"""
@spec foldr([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
@@ -341,40 +335,10 @@ defmodule List do
"""
@spec keyfind([tuple], any, non_neg_integer, any) :: any
def keyfind(list, key, position, default \\ nil) when is_integer(position) do
def keyfind(list, key, position, default \\ nil) do
:lists.keyfind(key, position + 1, list) || default
end
@doc """
Receives a list of tuples and returns the first tuple
where the element at `position` in the tuple matches the
given `key`.
If no matching tuple is found, an error is raised.
## Examples
iex> List.keyfind!([a: 1, b: 2], :a, 0)
{:a, 1}
iex> List.keyfind!([a: 1, b: 2], 2, 1)
{:b, 2}
iex> List.keyfind!([a: 1, b: 2], :c, 0)
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
"""
@doc since: "1.13.0"
@spec keyfind!([tuple], any, non_neg_integer) :: any
def keyfind!(list, key, position) when is_integer(position) do
:lists.keyfind(key, position + 1, list) ||
raise KeyError,
key: key,
term: list,
message:
"key #{inspect(key)} at position #{inspect(position)} not found in: #{inspect(list)}"
end
@doc """
Receives a list of tuples and returns `true` if there is
a tuple where the element at `position` in the tuple matches
@@ -393,7 +357,7 @@ defmodule List do
"""
@spec keymember?([tuple], any, non_neg_integer) :: boolean
def keymember?(list, key, position) when is_integer(position) do
def keymember?(list, key, position) do
:lists.keymember(key, position + 1, list)
end
@@ -411,7 +375,7 @@ defmodule List do
"""
@spec keyreplace([tuple], any, non_neg_integer, tuple) :: [tuple]
def keyreplace(list, key, position, new_tuple) when is_integer(position) do
def keyreplace(list, key, position, new_tuple) do
:lists.keyreplace(key, position + 1, list, new_tuple)
end
@@ -429,7 +393,7 @@ defmodule List do
"""
@spec keysort([tuple], non_neg_integer) :: [tuple]
def keysort(list, position) when is_integer(position) do
def keysort(list, position) do
:lists.keysort(position + 1, list)
end
@@ -449,7 +413,7 @@ defmodule List do
"""
@spec keystore([tuple], any, non_neg_integer, tuple) :: [tuple, ...]
def keystore(list, key, position, new_tuple) when is_integer(position) do
def keystore(list, key, position, new_tuple) do
:lists.keystore(key, position + 1, list, new_tuple)
end
@@ -471,7 +435,7 @@ defmodule List do
"""
@spec keydelete([tuple], any, non_neg_integer) :: [tuple]
def keydelete(list, key, position) when is_integer(position) do
def keydelete(list, key, position) do
:lists.keydelete(key, position + 1, list)
end
@@ -495,7 +459,7 @@ defmodule List do
"""
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
def keytake(list, key, position) when is_integer(position) do
def keytake(list, key, position) do
case :lists.keytake(key, position + 1, list) do
{:value, element, list} -> {element, list}
false -> nil
+25 -196
View File
@@ -53,7 +53,7 @@ defmodule Macro do
## Custom Sigils
Macros are also commonly used to implement custom sigils. To create a custom
sigil, define a macro with the name `sigil_{identifier}` that takes two
sigil, define a function with the name `sigil_{identifier}` that takes two
arguments. The first argument will be the string, the second will be a charlist
containing any modifiers. If the sigil is lower case (such as `sigil_x`) then
the string argument will allow interpolation. If the sigil is upper case
@@ -167,9 +167,9 @@ defmodule Macro do
and charlists as a string (such as `"{"`, `"/"`, `"'"`, and the like)
* `:format` - set to `:keyword` when an atom is defined as a keyword
* `:do` - contains metadata about the `do` location in a function call with
`do`-`end` blocks (when `:token_metadata` is true)
`do/end` blocks (when `:token_metadata` is true)
* `:end` - contains metadata about the `end` location in a function call with
`do`-`end` blocks (when `:token_metadata` is true)
`do/end` blocks (when `:token_metadata` is true)
* `:end_of_expression` - denotes when the end of expression effectively
happens. Available for all expressions except the last one inside a
`__block__` (when `:token_metadata` is true)
@@ -490,23 +490,6 @@ defmodule Macro do
@doc """
Performs a depth-first, pre-order traversal of quoted expressions.
Returns a new ast where each node is the result of invoking `fun` on each
corresponding node of `ast`.
## Examples
iex> ast = quote do: 5 + 3 * 7
iex> new_ast = Macro.prewalk(ast, fn
...> {:+, meta, children} -> {:*, meta, children}
...> {:*, meta, children} -> {:+, meta, children}
...> other -> other
...> end)
iex> Code.eval_quoted(ast)
{26, []}
iex> Code.eval_quoted(new_ast)
{50, []}
"""
@spec prewalk(t, (t -> t)) :: t
def prewalk(ast, fun) when is_function(fun, 1) do
@@ -604,7 +587,8 @@ defmodule Macro do
* `:prune_metadata` - when true, removes metadata from escaped AST
nodes. Note this option changes the semantics of escaped code and
it should only be used when escaping ASTs. Defaults to false.
it should only be used when escaping ASTs, never values. Defaults
to false.
As an example, `ExUnit` stores the AST of every assertion, so when
an assertion fails we can show code snippets to users. Without this
@@ -612,8 +596,7 @@ defmodule Macro do
MD5 of the module bytecode, because the AST contains metadata,
such as counters, specific to the compilation environment. By pruning
the metadata, we ensure that the module is deterministic and reduce
the amount of data `ExUnit` needs to keep around. Only the minimal
amount of metadata is kept, such as `:line` and `:no_parens`.
the amount of data `ExUnit` needs to keep around.
## Comparison to `Kernel.SpecialForms.quote/2`
@@ -644,7 +627,7 @@ defmodule Macro do
@spec escape(term, keyword) :: t()
def escape(expr, opts \\ []) do
unquote = Keyword.get(opts, :unquote, false)
kind = if Keyword.get(opts, :prune_metadata, false), do: :prune_metadata, else: :none
kind = if Keyword.get(opts, :prune_metadata, false), do: :prune_metadata, else: :default
:elixir_quote.escape(expr, kind, unquote)
end
@@ -661,9 +644,7 @@ defmodule Macro do
dependency on the given struct.
"""
@doc since: "1.8.0"
@spec struct!(module, Macro.Env.t()) ::
%{required(:__struct__) => module, optional(atom) => any}
when module: module()
@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__)
@@ -719,125 +700,6 @@ defmodule Macro do
defp find_invalid(other), do: {:error, other}
@doc """
Returns an enumerable that traverses the `ast` in depth-first,
pre-order traversal.
## Examples
iex> ast = quote do: foo(1, "abc")
iex> Enum.map(Macro.prewalker(ast), & &1)
[{:foo, [], [1, "abc"]}, 1, "abc"]
"""
@doc since: "1.13.0"
def prewalker(ast) do
&prewalker([ast], &1, &2)
end
defp prewalker(_buffer, {:halt, acc}, _fun) do
{:halted, acc}
end
defp prewalker(buffer, {:suspend, acc}, fun) do
{:suspended, acc, &prewalker(buffer, &1, fun)}
end
defp prewalker([], {:cont, acc}, _fun) do
{:done, acc}
end
defp prewalker([{left, right} = node | tail], {:cont, acc}, fun) do
prewalker([left, right | tail], fun.(node, acc), fun)
end
defp prewalker([{left, meta, right} = node | tail], {:cont, acc}, fun)
when is_atom(left) and is_list(meta) do
if is_atom(right) do
prewalker(tail, fun.(node, acc), fun)
else
prewalker(right ++ tail, fun.(node, acc), fun)
end
end
defp prewalker([{left, meta, right} = node | tail], {:cont, acc}, fun) when is_list(meta) do
if is_atom(right) do
prewalker([left | tail], fun.(node, acc), fun)
else
prewalker([left | right] ++ tail, fun.(node, acc), fun)
end
end
defp prewalker([list | tail], {:cont, acc}, fun) when is_list(list) do
prewalker(list ++ tail, fun.(list, acc), fun)
end
defp prewalker([head | tail], {:cont, acc}, fun) do
prewalker(tail, fun.(head, acc), fun)
end
@doc """
Returns an enumerable that traverses the `ast` in depth-first,
post-order traversal.
## Examples
iex> ast = quote do: foo(1, "abc")
iex> Enum.map(Macro.postwalker(ast), & &1)
[1, "abc", {:foo, [], [1, "abc"]}]
"""
@doc since: "1.13.0"
def postwalker(ast) do
&postwalker([ast], make_ref(), &1, &2)
end
defp postwalker(_buffer, _ref, {:halt, acc}, _fun) do
{:halted, acc}
end
defp postwalker(buffer, ref, {:suspend, acc}, fun) do
{:suspended, acc, &postwalker(buffer, ref, &1, fun)}
end
defp postwalker([], _ref, {:cont, acc}, _fun) do
{:done, acc}
end
defp postwalker([{ref, head} | tail], ref, {:cont, acc}, fun) do
postwalker(tail, ref, fun.(head, acc), fun)
end
defp postwalker([{left, right} = node | tail], ref, {:cont, acc}, fun) do
postwalker([right, {ref, node} | tail], ref, fun.(left, acc), fun)
end
defp postwalker([{left, meta, right} = node | tail], ref, {:cont, acc}, fun)
when is_atom(left) and is_list(meta) do
if is_atom(right) do
postwalker(tail, ref, fun.(node, acc), fun)
else
postwalker(right ++ [{ref, node} | tail], ref, {:cont, acc}, fun)
end
end
defp postwalker([{left, meta, right} = node | tail], ref, cont_acc, fun)
when is_list(meta) do
if is_atom(right) do
postwalker([left, {ref, node} | tail], ref, cont_acc, fun)
else
postwalker([left | right] ++ [{ref, node} | tail], ref, cont_acc, fun)
end
end
defp postwalker([list | tail], ref, cont_acc, fun) when is_list(list) do
postwalker(list ++ [{ref, list} | tail], ref, cont_acc, fun)
end
defp postwalker([head | tail], ref, {:cont, acc}, fun) do
postwalker(tail, ref, fun.(head, acc), fun)
end
@doc ~S"""
Unescapes the given chars.
@@ -933,25 +795,6 @@ defmodule Macro do
@doc """
Converts the given expression AST to a string.
This function discards all formatting of the original code.
See `Code.quoted_to_algebra/2` as a lower level function
with more control around formatting.
## Examples
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
"foo.bar(1, 2, 3)"
"""
@spec to_string(t()) :: String.t()
def to_string(tree) do
doc = Inspect.Algebra.format(Code.quoted_to_algebra(tree), 98)
IO.iodata_to_binary(doc)
end
@doc """
Converts the given expression AST to a string.
The given `fun` is called for every node in the AST with two arguments: the
AST of the node being printed and the string representation of that same
node. The return value of this function is used as the final string
@@ -961,17 +804,19 @@ defmodule Macro do
## Examples
Macro.to_string(quote(do: 1 + 2), fn
1, _string -> "one"
2, _string -> "two"
_ast, string -> string
end)
#=> "one + two"
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
"foo.bar(1, 2, 3)"
iex> Macro.to_string(quote(do: 1 + 2), fn
...> 1, _string -> "one"
...> 2, _string -> "two"
...> _ast, string -> string
...> end)
"one + two"
"""
@deprecated "Use Macro.to_string/1 instead"
@spec to_string(t(), (t(), String.t() -> String.t())) :: String.t()
def to_string(tree, fun)
def to_string(tree, fun \\ fn _ast, string -> string end)
# Variables
def to_string({var, _, context} = ast, fun) when is_atom(var) and is_atom(context) do
@@ -1596,7 +1441,7 @@ defmodule Macro do
end
defp do_expand_once({:__aliases__, meta, _} = original, env) do
case :elixir_aliases.expand_or_concat(original, env) do
case :elixir_aliases.expand(original, env) do
receiver when is_atom(receiver) ->
:elixir_env.trace({:alias_reference, meta, receiver}, env)
{receiver, true}
@@ -1655,10 +1500,7 @@ defmodule Macro do
[]
end
s = :elixir_env.env_to_ex(env)
expand =
:elixir_dispatch.expand_import(meta, {atom, length(args)}, args, s, env, extra, true)
expand = :elixir_dispatch.expand_import(meta, {atom, length(args)}, args, env, extra, true)
case expand do
{:ok, receiver, quoted} ->
@@ -1689,9 +1531,7 @@ defmodule Macro do
{original, false}
true ->
s = :elixir_env.env_to_ex(env)
name_arity = {right, length(args)}
expand = :elixir_dispatch.expand_require(meta, receiver, name_arity, args, s, env)
expand = :elixir_dispatch.expand_require(meta, receiver, {right, length(args)}, args, env)
case expand do
{:ok, receiver, quoted} ->
@@ -1718,22 +1558,9 @@ defmodule Macro do
@doc """
Returns `true` if the given name and arity is an operator.
## Examples
iex> Macro.operator?(:not_an_operator, 3)
false
iex> Macro.operator?(:+, 1)
true
iex> Macro.operator?(:++, 2)
true
iex> Macro.operator?(:..//, 3)
true
"""
@doc since: "1.7.0"
@spec operator?(name :: atom(), arity()) :: boolean()
def operator?(:"..//", 3), do: true
def operator?(name, 2) when is_atom(name), do: Identifier.binary_op(name) != :error
def operator?(name, 1) when is_atom(name), do: Identifier.unary_op(name) != :error
def operator?(name, arity) when is_atom(name) and is_integer(arity), do: false
@@ -1741,7 +1568,7 @@ defmodule Macro do
@doc """
Returns `true` if the given quoted expression represents a quoted literal.
Atoms and numbers are always literals. Binaries, lists, tuples,
Atoms, numbers, and functions are always literals. Binaries, lists, tuples,
maps, and structs are only literals if all of their terms are also literals.
## Examples
@@ -1776,7 +1603,9 @@ defmodule Macro do
def quoted_literal?({:{}, _, args}), do: quoted_literal?(args)
def quoted_literal?({left, right}), do: quoted_literal?(left) and quoted_literal?(right)
def quoted_literal?(list) when is_list(list), do: Enum.all?(list, &quoted_literal?/1)
def quoted_literal?(term), do: is_atom(term) or is_number(term) or is_binary(term)
def quoted_literal?(term),
do: is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
@doc """
Receives an AST node and expands it until it can no longer
+56 -137
View File
@@ -21,6 +21,8 @@ defmodule Macro.Env do
It contains the following fields:
* `aliases` - a list of two-element tuples, where the first
element is the aliased name and the second one the actual name
* `context` - the context of the environment; it can be `nil`
(default context), `:guard` (inside a guard) or `:match` (inside a match)
* `context_modules` - a list of modules defined in the current context
@@ -28,44 +30,66 @@ defmodule Macro.Env do
* `function` - a tuple as `{atom, integer}`, where the first
element is the function name and the second its arity; returns
`nil` if not inside a function
* `functions` - a list of functions imported from each module
* `line` - the current line as an integer
* `macro_aliases` - a list of aliases defined inside the current macro
* `macros` - a list of macros imported from each module
* `module` - the current module name
* `requires` - the list of required modules
The following fields are private to Elixir's macro expansion mechanism and
must not be accessed directly:
* `aliases`
* `functions`
* `macro_aliases`
* `macros`
* `contextual_vars`
* `current_vars`
* `lexical_tracker`
* `requires`
* `prematch_vars`
* `tracers`
* `versioned_vars`
* `unused_vars`
The following fields are deprecated and must not be accessed or relied on:
* `vars` - a list keeping all defined variables as `{var, context}`
"""
@type aliases :: [{module, module}]
@type context :: :match | :guard | nil
@type context_modules :: [module]
@type file :: binary
@type functions :: [{module, [name_arity]}]
@type lexical_tracker :: pid | nil
@type line :: non_neg_integer
@type macro_aliases :: [{module, {term, module}}]
@type macros :: [{module, [name_arity]}]
@type name_arity :: {atom, arity}
@type requires :: [module]
@type variable :: {atom, atom | term}
@typep aliases :: [{module, module}]
@typep functions :: [{module, [name_arity]}]
@typep lexical_tracker :: pid | nil
@typep macro_aliases :: [{module, {term, module}}]
@typep macros :: [{module, [name_arity]}]
@typep requires :: [module]
@typep contextual_vars :: [atom]
@typep current_vars ::
{%{optional(variable) => {var_version, var_type}},
%{optional(variable) => {var_version, var_type}} | false}
@typep unused_vars ::
{%{optional({atom, var_version}) => non_neg_integer | false}, non_neg_integer}
@typep prematch_vars ::
{%{optional(variable) => {var_version, var_type}}, non_neg_integer}
| :warn
| :raise
| :pin
| :apply
@typep tracers :: [module]
@typep versioned_vars :: %{optional(variable) => var_version :: non_neg_integer}
@typep var_type :: :term
@typep var_version :: non_neg_integer
@typep vars :: [variable]
@type t :: %{
__struct__: __MODULE__,
aliases: aliases,
context: context,
context_modules: context_modules,
contextual_vars: contextual_vars,
current_vars: current_vars,
file: file,
function: name_arity | nil,
functions: functions,
@@ -74,12 +98,15 @@ defmodule Macro.Env do
macro_aliases: macro_aliases,
macros: macros,
module: module,
prematch_vars: prematch_vars,
unused_vars: unused_vars,
requires: requires,
tracers: tracers,
versioned_vars: versioned_vars
vars: vars
}
# Define the __struct__ callbacks by hand for bootstrap reasons.
# TODO: Remove :vars field on v2.0
@doc false
def __struct__ do
%{
@@ -87,6 +114,8 @@ defmodule Macro.Env do
aliases: [],
context: nil,
context_modules: [],
contextual_vars: [],
current_vars: {%{}, %{}},
file: "nofile",
function: nil,
functions: [],
@@ -95,9 +124,11 @@ defmodule Macro.Env do
macro_aliases: [],
macros: [],
module: nil,
prematch_vars: :warn,
requires: [],
tracers: [],
versioned_vars: %{}
unused_vars: {%{}, 0},
vars: []
}
end
@@ -118,30 +149,19 @@ defmodule Macro.Env do
@spec vars(t) :: [variable]
def vars(env)
def vars(%{__struct__: Macro.Env, versioned_vars: vars}) do
Map.keys(vars)
def vars(%{__struct__: Macro.Env, current_vars: {read, _}}) do
Map.keys(read)
end
@doc """
Checks if a variable belongs to the environment.
## Examples
iex> x = 13
iex> x
13
iex> Macro.Env.has_var?(__ENV__, {:x, nil})
true
iex> Macro.Env.has_var?(__ENV__, {:unknown, nil})
false
"""
@doc since: "1.7.0"
@spec has_var?(t, variable) :: boolean()
def has_var?(env, var)
def has_var?(%{__struct__: Macro.Env, versioned_vars: vars}, var) do
Map.has_key?(vars, var)
def has_var?(%{__struct__: Macro.Env, current_vars: {read, _}}, var) do
Map.has_key?(read, var)
end
@doc """
@@ -155,117 +175,16 @@ defmodule Macro.Env do
[file: file, line: line]
end
@doc """
Fetches the alias for the given atom.
Returns `{:ok, alias}` if the alias exists, `:error`
otherwise.
## Examples
iex> alias Foo.Bar, as: Baz
iex> Baz
Foo.Bar
iex> Macro.Env.fetch_alias(__ENV__, :Baz)
{:ok, Foo.Bar}
iex> Macro.Env.fetch_alias(__ENV__, :Unknown)
:error
"""
@doc since: "1.13.0"
@spec fetch_alias(t, atom) :: {:ok, atom} | :error
def fetch_alias(%{__struct__: Macro.Env, aliases: aliases}, atom) when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
@doc """
Fetches the macro alias for the given atom.
Returns `{:ok, macro_alias}` if the alias exists, `:error`
otherwise.
A macro alias is only used inside quoted expansion. See
`fetch_alias/2` for a more general example.
"""
@doc since: "1.13.0"
@spec fetch_macro_alias(t, atom) :: {:ok, atom} | :error
def fetch_macro_alias(%{__struct__: Macro.Env, macro_aliases: aliases}, atom)
when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
@doc """
Returns the modules from which the given `{name, arity}` was
imported.
It returns a list of two element tuples in the shape of
`{:function | :macro, module}`. The elements in the list
are in no particular order and the order is not guaranteed.
## Examples
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[]
iex> import Tuple, only: [duplicate: 2], warn: false
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[{:function, Tuple}]
iex> import List, only: [duplicate: 2], warn: false
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[{:function, List}, {:function, Tuple}]
iex> Macro.Env.lookup_import(__ENV__, {:def, 1})
[{:macro, Kernel}]
"""
@doc since: "1.13.0"
@spec lookup_import(t, name_arity) :: [{:function | :macro, module}]
def lookup_import(
%{__struct__: Macro.Env, functions: functions, macros: macros},
{name, arity} = pair
)
when is_atom(name) and is_integer(arity) do
f = for {mod, pairs} <- functions, :ordsets.is_element(pair, pairs), do: {:function, mod}
m = for {mod, pairs} <- macros, :ordsets.is_element(pair, pairs), do: {:macro, mod}
f ++ m
end
@doc """
Returns true if the given module has been required.
## Examples
iex> Macro.Env.required?(__ENV__, Integer)
false
iex> require Integer
iex> Macro.Env.required?(__ENV__, Integer)
true
iex> Macro.Env.required?(__ENV__, Kernel)
true
"""
@doc since: "1.13.0"
@spec required?(t, module) :: boolean
def required?(%{__struct__: Macro.Env, requires: requires}, mod) when is_atom(mod),
do: mod in requires
@doc """
Prepend a tracer to the list of tracers in the environment.
## Examples
Macro.Env.prepend_tracer(__ENV__, MyCustomTracer)
"""
@doc since: "1.13.0"
@spec prepend_tracer(t, module) :: t
def prepend_tracer(%{__struct__: Macro.Env, tracers: tracers} = env, tracer) do
%{env | tracers: [tracer | tracers]}
end
@doc """
Returns a `Macro.Env` in the match context.
"""
@spec to_match(t) :: t
def to_match(%{__struct__: Macro.Env} = env) do
%{env | context: :match}
def to_match(%{__struct__: Macro.Env, context: :match} = env) do
env
end
def to_match(%{__struct__: Macro.Env, current_vars: {read, _}, unused_vars: {_, counter}} = env) do
%{env | context: :match, prematch_vars: {read, counter}}
end
@doc """
+5 -97
View File
@@ -214,24 +214,7 @@ defmodule Map do
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform)
def new(%_{} = enumerable, transform), do: new_from_enum(enumerable, transform)
def new(%{} = map, transform), do: new_from_map(map, transform)
def new(enumerable, transform), do: new_from_enum(enumerable, transform)
defp new_from_map(map, transform) when is_function(transform, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_map(next, transform))
end
defp do_map(:none, _fun), do: []
defp do_map({key, value, iter}, transform) do
[transform.({key, value}) | do_map(:maps.next(iter), transform)]
end
defp new_from_enum(enumerable, transform) when is_function(transform, 1) do
def new(enumerable, transform) when is_function(transform, 1) do
enumerable
|> Enum.map(transform)
|> :maps.from_list()
@@ -335,7 +318,7 @@ defmodule Map do
def replace(map, key, value) do
case map do
%{^key => _value} ->
%{map | key => value}
put(map, key, value)
%{} ->
map
@@ -631,7 +614,7 @@ defmodule Map do
def update(map, key, default, fun) when is_function(fun, 1) do
case map do
%{^key => value} ->
%{map | key => fun.(value)}
put(map, key, fun.(value))
%{} ->
put(map, key, default)
@@ -827,7 +810,7 @@ defmodule Map do
@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)
%{map | key => fun.(value)}
put(map, key, fun.(value))
end
@doc """
@@ -912,7 +895,7 @@ defmodule Map do
case fun.(value) do
{get, update} ->
{get, %{map | key => update}}
{get, put(map, key, update)}
:pop ->
{value, delete(map, key)}
@@ -988,79 +971,4 @@ defmodule Map do
def size(map) do
map_size(map)
end
@doc """
Returns a map containing only those pairs from `map`
for which `fun` returns a truthy value.
`fun` receives the key and value of each of the
elements in the map as a key-value pair.
See also `reject/2` which discards all elements where the
function returns a truthy value.
> Note: if you find yourself doing multiple calls to `Map.filter/2`
> and `Map.reject/2` in a pipeline, it is likely more efficient
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
> a map at the end using `Map.new/1`.
## Examples
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
%{one: 1, three: 3}
"""
@doc since: "1.13.0"
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
def filter(map, fun) when is_map(map) and is_function(fun, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_filter(next, fun))
end
defp do_filter(:none, _fun), do: []
defp do_filter({key, value, iter}, fun) do
if fun.({key, value}) do
[{key, value} | do_filter(:maps.next(iter), fun)]
else
do_filter(:maps.next(iter), fun)
end
end
@doc """
Returns map excluding the pairs from `map` for which `fun` returns
a truthy value.
See also `filter/2`.
## Examples
iex> Map.reject(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
%{two: 2}
"""
@doc since: "1.13.0"
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
def reject(map, fun) when is_map(map) and is_function(fun, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_reject(next, fun))
end
defp do_reject(:none, _fun), do: []
defp do_reject({key, value, iter}, fun) do
if fun.({key, value}) do
do_reject(:maps.next(iter), fun)
else
[{key, value} | do_reject(:maps.next(iter), fun)]
end
end
@doc false
@deprecated "Use Map.new/2 instead"
def map(map, fun) when is_map(map) do
new(map, fn {k, v} -> {k, fun.({k, v})} end)
end
end
-1
View File
@@ -402,7 +402,6 @@ defmodule MapSet do
end
defimpl Collectable do
# TODO: Optimize into an empty mapset by using :maps.from_keys/2 on Erlang/OTP 24+
def into(map_set) do
fun = fn
list, {:cont, x} -> [{x, []} | list]
+28 -109
View File
@@ -709,24 +709,22 @@ defmodule Module do
def eval_quoted(%Macro.Env{} = env, quoted, binding, opts)
when is_list(binding) and is_list(opts) do
validated_eval_quoted(env.module, quoted, binding, struct!(env, opts))
eval_quoted(env.module, quoted, binding, Keyword.merge(Map.to_list(env), opts))
end
def eval_quoted(module, quoted, binding, %Macro.Env{} = env)
when is_atom(module) and is_list(binding) do
validated_eval_quoted(module, quoted, binding, env)
eval_quoted(module, quoted, binding, Map.to_list(env))
end
def eval_quoted(module, quoted, binding, opts)
when is_atom(module) and is_list(binding) and is_list(opts) do
validated_eval_quoted(module, quoted, binding, opts)
end
defp validated_eval_quoted(module, quoted, binding, env_or_opts) do
assert_not_compiled!({:eval_quoted, 4}, module)
assert_not_compiled!(__ENV__.function, module)
:elixir_def.reset_last(module)
env = :elixir.env_for_eval(env_or_opts)
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, %{env | module: module})
{value, binding, _env} =
:elixir.eval_quoted(quoted, binding, Keyword.put(opts, :module, module))
{value, binding}
end
@@ -792,8 +790,6 @@ defmodule Module do
@doc """
Concatenates a list of aliases and returns a new alias.
It handles binaries and atoms.
## Examples
iex> Module.concat([Foo, Bar])
@@ -811,8 +807,6 @@ defmodule Module do
@doc """
Concatenates two aliases and returns a new alias.
It handles binaries and atoms.
## Examples
iex> Module.concat(Foo, Bar)
@@ -833,7 +827,7 @@ defmodule Module do
was already referenced.
If the alias was not referenced yet, fails with `ArgumentError`.
It handles binaries and atoms.
It handles charlists, binaries and atoms.
## Examples
@@ -851,7 +845,7 @@ defmodule Module do
already referenced.
If the alias was not referenced yet, fails with `ArgumentError`.
It handles binaries and atoms.
It handles charlists, binaries and atoms.
## Examples
@@ -967,8 +961,8 @@ defmodule Module do
defp autogenerated_key(counters, key) do
case counters do
%{^key => :once} -> {key, %{counters | key => 2}}
%{^key => value} -> {key, %{counters | key => value + 1}}
%{^key => :once} -> {key, Map.put(counters, key, 2)}
%{^key => value} -> {key, Map.put(counters, key, value + 1)}
%{} -> {key, Map.put(counters, key, :once)}
end
end
@@ -1117,61 +1111,6 @@ defmodule Module do
Kernel.Typespec.spec_to_callback(module, definition)
end
@doc """
Returns all module attributes names defined in `module`.
This function can only be used on modules that have not yet been compiled.
## Examples
defmodule Example do
@foo 1
Module.register_attribute(__MODULE__, :bar, accumulate: true)
:foo in Module.attributes_in(__MODULE__)
#=> true
:bar in Module.attributes_in(__MODULE__)
#=> true
end
"""
@doc since: "1.13.0"
@spec attributes_in(module) :: [atom]
def attributes_in(module) when is_atom(module) do
assert_not_compiled!(__ENV__.function, module)
{set, _} = data_tables_for(module)
:ets.select(set, [{{:"$1", :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
end
@doc """
Returns all overridable definitions in `module`.
Note a definition is included even if it was was already overridden.
You can use `defines?/2` to see if a definition exists or one is pending.
This function can only be used on modules that have not yet been compiled.
## Examples
defmodule Example do
def foo, do: 1
def bar, do: 2
defoverridable foo: 1, bar: 1
def foo, do: 3
[:bar, :foo] = Module.overridables_in(__MODULE__) |> Enum.sort()
end
"""
@doc since: "1.13.0"
@spec overridables_in(module) :: [atom]
def overridables_in(module) when is_atom(module) do
assert_not_compiled!(__ENV__.function, module)
:elixir_overridable.overridables_for(module)
end
@doc """
Returns all functions and macros defined in `module`.
@@ -1238,31 +1177,19 @@ defmodule Module do
context. Given this AST representation is mostly internal,
it is versioned and it may change at any time. Therefore,
**use this API with caution**.
## Options
* `:nillify_clauses` (since v1.13.0) - returns `nil` instead
of returning the clauses. This is useful when there is
only an interest in fetching the kind and metadata
"""
@spec get_definition(module, definition, keyword) ::
@spec get_definition(module, definition) ::
{:v1, def_kind, meta :: keyword,
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}] | nil}
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
@doc since: "1.12.0"
def get_definition(module, {name, arity}, options \\ [])
when is_atom(module) and is_atom(name) and is_integer(arity) and is_list(options) do
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, _, _, _}] ->
clauses =
if options[:nillify_clauses],
do: nil,
else: bag_lookup_element(bag, {:clauses, {name, arity}}, 2)
{:v1, kind, meta, clauses}
{:v1, kind, meta, bag_lookup_element(bag, {:clauses, {name, arity}}, 2)}
[] ->
nil
@@ -1361,7 +1288,7 @@ defmodule Module do
behaviour_definitions = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
cond do
not Code.ensure_loaded?(behaviour) ->
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
{:error, "it was not defined"}
not function_exported?(behaviour, :behaviour_info, 1) ->
@@ -1398,12 +1325,7 @@ defmodule Module do
end
@doc """
Returns `true` if `tuple` in `module` was marked as overridable
at some point.
Note `overridable?/2` returns true even if the definition was
already overridden. You can use `defines?/2` to see if a definition
exists or one is pending.
Returns `true` if `tuple` in `module` is marked as overridable.
"""
@spec overridable?(module, definition) :: boolean
def overridable?(module, {function_name, arity} = tuple)
@@ -1796,7 +1718,14 @@ defmodule Module do
defp check_behaviours(env, behaviours) do
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
cond do
not Code.ensure_loaded?(behaviour) ->
not is_atom(behaviour) ->
message =
"@behaviour #{inspect(behaviour)} must be an atom (in module #{inspect(env.module)})"
IO.warn(message, Macro.Env.stacktrace(env))
acc
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
message =
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
@@ -1811,8 +1740,7 @@ defmodule Module do
acc
true ->
event = {:remote_function, [], behaviour, :behaviour_info, 1}
:elixir_env.trace(event, %{env | function: {:__info__, 1}})
:elixir_env.trace({:require, [], behaviour, []}, env)
optional_callbacks = behaviour_info(behaviour, :optional_callbacks)
callbacks = behaviour_info(behaviour, :callbacks)
Enum.reduce(callbacks, acc, &add_callback(&1, behaviour, env, optional_callbacks, &2))
@@ -2186,15 +2114,6 @@ defmodule Module do
end
end
defp preprocess_attribute(:behaviour, value) do
if is_atom(value) do
Code.ensure_compiled(value)
value
else
raise ArgumentError, "@behaviour expects a module, got: #{inspect(value)}"
end
end
defp preprocess_attribute(:on_load, value) do
case value do
_ when is_atom(value) ->
@@ -2241,7 +2160,7 @@ defmodule Module do
defp preprocess_attribute(:external_resource, value) when not is_binary(value) do
raise ArgumentError,
"@external_resource is a built-in module attribute used for specifying file " <>
"dependencies. It should be a string path to a file, got: #{inspect(value)}"
"dependencies. It should be a string the path to a file, got: #{inspect(value)}"
end
defp preprocess_attribute(:deprecated, value) when not is_binary(value) do
+2 -3
View File
@@ -71,9 +71,8 @@ defmodule Module.LocalsTracker do
:ok
end
@doc """
Collect all conflicting imports with the given functions
"""
# Collecting all conflicting imports with the given functions
@doc false
def collect_imports_conflicts({set, _bag}, all_defined) do
for {pair, _, meta, _} <- all_defined, n = out_neighbour(set, {:import, pair}) do
{meta, {n, pair}}
+127 -209
View File
@@ -1,141 +1,44 @@
defmodule Module.ParallelChecker do
@moduledoc false
import Kernel, except: [spawn: 3]
@type cache() :: {pid(), :ets.tid()}
@type warning() :: term()
@type kind() :: :def | :defmacro
@type mode() :: :elixir | :erlang
@doc """
Initializes the parallel checker process.
Receives pairs of module maps and BEAM binaries. In parallel it verifies
the modules and adds the ExCk chunk to the binaries. Returns the updated
binaries and a list of warnings from the verification.
"""
def start_link(schedulers \\ nil) do
:gen_server.start_link(__MODULE__, schedulers, [])
end
@spec verify([{map(), binary()}], [{module(), binary()}], pos_integer() | nil) :: [warning()]
def verify(compiled_modules, runtime_binaries, schedulers \\ nil) do
compiled_maps = Enum.map(compiled_modules, fn {map, _binary} -> {map.module, map} end)
@doc """
Stops the parallel checker process.
"""
def stop(checker) do
send(checker, {__MODULE__, :stop})
:ok
end
case compiled_maps ++ runtime_binaries do
[] ->
[]
@doc """
Gets the parallel checker data from pdict.
"""
def get do
{_, checker} = :erlang.get(:elixir_checker_info)
checker
end
check_modules ->
schedulers = schedulers || max(:erlang.system_info(:schedulers_online), 2)
@doc """
Stores the parallel checker information.
"""
def put(pid, checker) do
:erlang.put(:elixir_checker_info, {pid, checker})
end
{:ok, server} =
:gen_server.start_link(__MODULE__, [check_modules, self(), schedulers], [])
@doc """
Spawns a process that runs the parallel checker.
"""
def spawn({pid, checker}, module, info) do
ref = make_ref()
spawned =
spawn(fn ->
Process.link(pid)
mon_ref = Process.monitor(pid)
receive do
{^ref, :cache, ets} ->
loaded_info =
if is_map(info) do
cache_from_module_map(ets, info)
info
else
info = File.read!(info)
cache_from_chunk(ets, module, info)
info
end
send(checker, {ref, :cached})
receive do
{^ref, :check} ->
warnings = check_module(module, loaded_info, {checker, ets})
send(pid, {__MODULE__, module, warnings})
send(checker, {__MODULE__, :done})
end
{:DOWN, ^mon_ref, _, _, _} ->
:ok
end
end)
{spawned, ref}
end
@doc """
Verifies the given compilation function
by starting a checker if one does not exist.
See `verify/3`.
"""
def verify(fun) do
case :erlang.get(:elixir_compiler_info) do
:undefined ->
previous = :erlang.get(:elixir_checker_info)
{:ok, checker} = start_link()
put(self(), checker)
try do
{result, compile_info} = Enum.unzip(fun.())
_ = verify(checker, compile_info, [])
result
after
if previous != :undefined do
:erlang.put(:elixir_checker_info, previous)
else
:erlang.erase(:elixir_checker_info)
end
stop(checker)
end
_ ->
# If we are during compilation, then they will be
# reported to the compiler, which will validate them.
Enum.map(fun.(), &elem(&1, 0))
preload_cache(get_ets(server), check_modules)
start(server)
collect_results(length(check_modules), [])
end
end
@doc """
Receives pairs of module maps and BEAM binaries. In parallel it verifies
the modules and adds the ExCk chunk to the binaries. Returns the updated
list of warnings from the verification.
"""
@spec verify(pid(), [{pid(), reference()}], [{module(), binary()}]) :: [warning()]
def verify(checker, compiled_info, runtime_files) do
runtime_info =
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
modules = compiled_info ++ runtime_info
:gen_server.cast(checker, {:start, modules})
collect_results(modules, [])
end
defp collect_results([], warnings) do
defp collect_results(0, warnings) do
warnings
end
defp collect_results([_ | modules], warnings) do
defp collect_results(count, warnings) do
receive do
{__MODULE__, _module, new_warnings} ->
collect_results(modules, new_warnings ++ warnings)
collect_results(count - 1, new_warnings ++ warnings)
end
end
@@ -143,8 +46,8 @@ defmodule Module.ParallelChecker do
Test cache.
"""
def test_cache do
{:ok, checker} = start_link()
{checker, :gen_server.call(checker, :ets)}
{:ok, pid} = :gen_server.start_link(__MODULE__, [[], self(), 1], [])
{pid, get_ets(pid)}
end
@doc """
@@ -195,8 +98,8 @@ defmodule Module.ParallelChecker do
## Module checking
defp check_module(module, info, cache) do
case extract_definitions(module, info) do
defp check_module(module, cache) do
case extract_definitions(module) do
{:ok, module, file, definitions, no_warn_undefined} ->
Module.Types.warnings(module, file, definitions, no_warn_undefined, cache)
|> group_warnings()
@@ -207,7 +110,7 @@ defmodule Module.ParallelChecker do
end
end
defp extract_definitions(module, module_map) when is_map(module_map) do
defp extract_definitions({module, module_map}) when is_map(module_map) do
no_warn_undefined =
module_map.compile_opts
|> extract_no_warn_undefined()
@@ -216,11 +119,11 @@ defmodule Module.ParallelChecker do
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
end
defp extract_definitions(module, binary) when is_binary(binary) do
defp extract_definitions({module, binary}) when is_binary(binary) do
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
{:debug_info_v1, backend, data} <- chunk,
{:ok, module_map} <- backend.debug_info(:elixir_v1, module, data, []) do
extract_definitions(module, module_map)
extract_definitions({module, module_map})
else
_ -> :error
end
@@ -273,7 +176,7 @@ defmodule Module.ParallelChecker do
defp format_locations(locations) do
[
"Invalid call found at #{length(locations)} locations:\n",
"Found at #{length(locations)} locations:\n",
Enum.map(locations, &format_location/1)
]
end
@@ -301,7 +204,106 @@ defmodule Module.ParallelChecker do
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
end
## Cache
## Server callbacks
def init([modules, send_results, schedulers]) do
ets = :ets.new(:checker_cache, [:set, :public, {:read_concurrency, true}])
state = %{
ets: ets,
waiting: %{},
send_results: send_results,
modules: modules,
spawned: 0,
schedulers: schedulers
}
{:ok, state}
end
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
case waiting do
%{^module => froms} ->
waiting = Map.put(state.waiting, module, [from | froms])
{:noreply, %{state | waiting: waiting}}
%{} ->
waiting = Map.put(state.waiting, module, [])
{:reply, true, %{state | waiting: waiting}}
end
end
def handle_call({:unlock, module}, _from, %{waiting: waiting} = state) do
froms = Map.fetch!(waiting, module)
Enum.each(froms, &:gen_server.reply(&1, false))
waiting = Map.delete(waiting, module)
{:reply, :ok, %{state | waiting: waiting}}
end
def handle_call(:get_ets, _from, %{ets: ets} = state) do
{:reply, ets, state}
end
def handle_cast(:start, state) do
{:noreply, spawn_checkers(state)}
end
def handle_info({__MODULE__, :done}, state) do
state = %{state | spawned: state.spawned - 1}
if state.spawned == 0 and state.modules == [] do
{:stop, :normal, state}
else
state = spawn_checkers(state)
{:noreply, state}
end
end
defp lock(server, module) do
:gen_server.call(server, {:lock, module}, :infinity)
end
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module})
end
defp get_ets(server) do
:gen_server.call(server, :get_ets)
end
defp start(server) do
:gen_server.cast(server, :start)
end
defp preload_cache(ets, modules) do
Enum.each(modules, fn
{_module, map} when is_map(map) -> cache_from_module_map(ets, map)
{module, binary} when is_binary(binary) -> cache_from_chunk(ets, module, binary)
end)
end
defp spawn_checkers(%{modules: []} = state) do
state
end
defp spawn_checkers(%{spawned: spawned, schedulers: schedulers} = state)
when spawned >= schedulers do
state
end
defp spawn_checkers(%{modules: [{module, _} = verify | modules]} = state) do
parent = self()
ets = state.ets
send_results_pid = state.send_results
spawn_link(fn ->
warnings = check_module(verify, {parent, ets})
send(send_results_pid, {__MODULE__, module, warnings})
send(parent, {__MODULE__, :done})
end)
spawn_checkers(%{state | modules: modules, spawned: state.spawned + 1})
end
defp cache_module({server, ets}, module) do
if lock(server, module) do
@@ -408,88 +410,4 @@ defmodule Module.ParallelChecker do
end
end)
end
defp lock(server, module) do
:gen_server.call(server, {:lock, module}, :infinity)
end
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module})
end
## Server callbacks
def init(schedulers) do
ets = :ets.new(__MODULE__, [:set, :public, {:read_concurrency, true}])
state = %{
ets: ets,
waiting: %{},
modules: [],
spawned: 0,
schedulers: schedulers || max(:erlang.system_info(:schedulers_online), 2)
}
{:ok, state}
end
def handle_call(:ets, _from, state) do
{:reply, state.ets, state}
end
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
case waiting do
%{^module => froms} ->
waiting = Map.put(state.waiting, module, [from | froms])
{:noreply, %{state | waiting: waiting}}
%{} ->
waiting = Map.put(state.waiting, module, [])
{:reply, true, %{state | waiting: waiting}}
end
end
def handle_call({:unlock, module}, _from, %{waiting: waiting} = state) do
froms = Map.fetch!(waiting, module)
Enum.each(froms, &:gen_server.reply(&1, false))
waiting = Map.delete(waiting, module)
{:reply, :ok, %{state | waiting: waiting}}
end
def handle_info({__MODULE__, :done}, state) do
state = %{state | spawned: state.spawned - 1}
{:noreply, run_checkers(state)}
end
def handle_info({__MODULE__, :stop}, state) do
{:stop, :normal, state}
end
def handle_cast({:start, modules}, %{ets: ets} = state) do
for {pid, ref} <- modules do
send(pid, {ref, :cache, ets})
end
for {_pid, ref} <- modules do
receive do
{^ref, :cached} -> :ok
end
end
{:noreply, run_checkers(%{state | modules: modules})}
end
defp run_checkers(%{modules: []} = state) do
state
end
defp run_checkers(%{spawned: spawned, schedulers: schedulers} = state)
when spawned >= schedulers do
state
end
defp run_checkers(%{modules: [{pid, ref} | modules]} = state) do
send(pid, {ref, :check})
run_checkers(%{state | modules: modules, spawned: state.spawned + 1})
end
end
+49 -117
View File
@@ -133,24 +133,13 @@ defmodule Module.Types do
## ERROR TO WARNING
# Collect relevant information from context and traces to report error
def error_to_warning(:unable_apply, {mfa, args, expected, signature, stack}, context) do
{fun, arity} = context.function
line = get_meta(stack.last_expr)[:line]
location = {context.file, line, {context.module, fun, arity}}
traces = type_traces(stack, context)
{[signature | args], traces} = lift_all_types([signature | args], traces, context)
error = {:unable_apply, mfa, args, expected, signature, {location, stack.last_expr, traces}}
{Module.Types, error, location}
end
def error_to_warning(:unable_unify, {left, right, stack}, context) do
{fun, arity} = context.function
line = get_meta(stack.last_expr)[:line]
location = {context.file, line, {context.module, fun, arity}}
traces = type_traces(stack, context)
{[left, right], traces} = lift_all_types([left, right], 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
@@ -194,83 +183,58 @@ defmodule Module.Types do
end
end
defp lift_all_types(types, traces, context) do
trace_types = for({:type, _, type, _, _} <- traces, do: type)
{types, lift_context} = Unify.lift_types(types, context)
{trace_types, _lift_context} = Unify.lift_types(trace_types, lift_context)
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, trace_types, fn
Enum.map_reduce(traces, all_types, fn
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
other, acc -> {other, acc}
end)
{types, traces}
{left, right, traces}
end
## FORMAT WARNINGS
def format_warning({:unable_apply, mfa, args, expected, signature, {location, expr, traces}}) do
{module, function, arity} = mfa
mfa_args = Macro.generate_arguments(arity, __MODULE__)
{module, function, ^arity} = call_to_mfa(erl_to_ex(module, function, mfa_args, []))
format_mfa = Exception.format_mfa(module, function, arity)
{traces, [] = _hints} = format_traces(traces, [], false)
clauses =
Enum.map(
signature,
&String.slice(IO.iodata_to_binary(Unify.format_type({:fun, [&1]}, false)), 1..-2)
)
[
"expected #{format_mfa} to have signature:\n\n ",
Enum.map_join(args, ", ", &Unify.format_type(&1, false)),
" -> #{Unify.format_type(expected, false)}",
"\n\nbut it has signature:\n\n ",
indent(Enum.join(clauses, "\n")),
"\n\n",
format_expr(expr, location),
traces,
"Conflict found at"
]
end
def format_warning({:unable_unify, left, right, {location, expr, traces}}) do
if map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) do
{:ok, atom, known_atoms} = missing_field(left, right)
cond do
map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) ->
{:ok, atom, known_atoms} = missing_field(left, right)
# Drop the last trace which is the expression map.foo
traces = Enum.drop(traces, 1)
{traces, hints} = format_traces(traces, [left, right], true)
# Drop the last trace which is the expression map.foo
traces = Enum.drop(traces, 1)
{traces, hints} = format_traces(traces, true)
[
"undefined field \"#{atom}\" ",
format_expr(expr, location),
"expected one of the following fields: ",
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
"\n\n",
traces,
format_message_hints(hints),
"Conflict found at"
]
else
simplify_left? = simplify_type?(left, right)
simplify_right? = simplify_type?(right, left)
[
"undefined field \"#{atom}\" ",
format_expr(expr, location),
"expected one of the following fields: ",
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
"\n\n",
traces,
format_message_hints(hints),
"Conflict found at"
]
{traces, hints} = format_traces(traces, [left, right], simplify_left? or simplify_right?)
true ->
simplify_left? = simplify_type?(left, right)
simplify_right? = simplify_type?(right, left)
[
"incompatible types:\n\n ",
Unify.format_type(left, simplify_left?),
" !~ ",
Unify.format_type(right, simplify_right?),
"\n\n",
format_expr(expr, location),
traces,
format_message_hints(hints),
"Conflict found at"
]
{traces, hints} = format_traces(traces, simplify_left? or simplify_right?)
[
"incompatible types:\n\n ",
Unify.format_type(left, simplify_left?),
" !~ ",
Unify.format_type(right, simplify_right?),
"\n\n",
format_expr(expr, location),
traces,
format_message_hints(hints),
"Conflict found at"
]
end
end
@@ -299,17 +263,17 @@ defmodule Module.Types do
end
end
defp format_traces([], _types, _simplify?) do
defp format_traces([], _simplify?) do
{[], []}
end
defp format_traces(traces, types, simplify?) do
defp format_traces(traces, simplify?) do
traces
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map_reduce([], fn
{:type, var, type, expr, location}, hints ->
{hint, hints} = format_type_hint(type, types, expr, hints)
{hint, hints} = format_type_hint(type, expr, hints)
trace = [
"where \"",
@@ -354,7 +318,7 @@ defmodule Module.Types do
end
defp simplify_type?(type, other) do
map_like_type?(type) and not map_like_type?(other)
map_type?(type) and not map_type?(other)
end
## EXPRESSION FORMATTING
@@ -397,16 +361,14 @@ defmodule Module.Types do
## Hints
defp format_message_hints(hints) do
hints
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map(&[format_message_hint(&1), "\n"])
hints |> Enum.uniq() |> Enum.reverse() |> Enum.map(&format_message_hint/1)
end
defp format_message_hint(:inferred_dot) do
"""
HINT: "var.field" (without parentheses) implies "var" is a map() while \
"var.fun()" (with parentheses) implies "var" is an atom()
"""
end
@@ -416,24 +378,18 @@ defmodule Module.Types do
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.
"""
end
defp format_message_hint({:sized_and_unsize_tuples, {size, var}}) do
"""
HINT: use pattern matching or "is_tuple(#{Macro.to_string(var)}) and \
tuple_size(#{Macro.to_string(var)}) == #{size}" to guard a sized tuple.
"""
end
defp format_type_hint(type, types, expr, hints) do
case format_type_hint(type, types, expr) do
defp format_type_hint(type, expr, hints) do
case format_type_hint(type, expr) do
{message, hint} -> {message, [hint | hints]}
:error -> {[], hints}
end
end
defp format_type_hint(type, types, expr) do
defp format_type_hint(type, expr) do
cond do
dynamic_map_dot?(type, expr) ->
{" (due to calling var.field)", :inferred_dot}
@@ -444,9 +400,6 @@ defmodule Module.Types do
inferred_bitstring_spec?(type, expr) ->
{[], :inferred_bitstring_spec}
message = sized_and_unsize_tuples(expr, types) ->
{[], {:sized_and_unsize_tuples, message}}
true ->
:error
end
@@ -482,33 +435,15 @@ defmodule Module.Types do
end
end
defp sized_and_unsize_tuples({{:., _, [:erlang, :is_tuple]}, _, [var]}, types) do
case Enum.find(types, &match?({:tuple, _, _}, &1)) do
{:tuple, size, _} ->
{size, var}
nil ->
nil
end
end
defp sized_and_unsize_tuples(_expr, _types) do
nil
end
## Formatting helpers
defp indent(string) do
String.replace(string, "\n", "\n ")
String.replace(string, "\n", " \n")
end
defp map_type?({:map, _}), do: true
defp map_type?(_other), do: false
defp map_like_type?({:map, _}), do: true
defp map_like_type?({:union, union}), do: Enum.any?(union, &map_like_type?/1)
defp map_like_type?(_other), do: false
defp atom_type?(:atom), do: true
defp atom_type?({:atom, _}), do: false
defp atom_type?({:union, union}), do: Enum.all?(union, &atom_type?/1)
@@ -516,7 +451,4 @@ defmodule Module.Types do
defp integer_type?(:integer), do: true
defp integer_type?(_other), do: false
defp call_to_mfa({{:., _, [mod, fun]}, _, args}), do: {mod, fun, length(args)}
defp call_to_mfa({fun, _, args}) when is_atom(fun), do: {Kernel, fun, length(args)}
end
+18 -19
View File
@@ -334,10 +334,15 @@ defmodule Module.Types.Expr do
# TODO: Use expected type to infer intersection return type
stack = push_expr_stack(expr, stack)
with {:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
{:ok, _arg_types, context} <-
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, :dynamic, context}
case of_expr(fun, :dynamic, stack, context) do
{:ok, _fun_type, context} ->
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
{:error, reason} ->
{:error, reason}
end
end
@@ -370,10 +375,11 @@ defmodule Module.Types.Expr do
stack = push_expr_stack(expr2, stack)
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
{:ok, _arg_types, context} <-
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, :dynamic, 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
end
end
@@ -469,19 +475,12 @@ defmodule Module.Types.Expr do
end
defp of_clauses(clauses, stack, context) do
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
reduce_ok(clauses, context, fn {:->, _meta, [head, body]}, context = acc ->
{patterns, guards} = extract_head(head)
case Pattern.of_head(patterns, guards, stack, context) do
{:ok, _, context} ->
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
error ->
# Skip the clause if it the head has an error
if meta[:generated], do: {:ok, acc}, else: error
end
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
{:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
end
-54
View File
@@ -101,30 +101,6 @@ defmodule Module.Types.Helpers do
defp do_map_reduce_ok([], {list, acc}, _fun), do: {:ok, Enum.reverse(list), acc}
@doc """
Like `Enum.flat_map/2` but only continues while `fun` returns `{:ok, list}`
and stops on `{:error, reason}`.
"""
def flat_map_ok(list, fun) do
do_flat_map_ok(list, [], fun)
end
defp do_flat_map_ok([head | tail], acc, fun) do
case fun.(head) do
{:ok, elem} ->
do_flat_map_ok(tail, [elem | acc], fun)
{:error, reason} ->
{:error, reason}
end
end
defp do_flat_map_ok([], acc, _fun), do: {:ok, Enum.reverse(Enum.concat(acc))}
@doc """
Like `Enum.flat_map_reduce/3` but only continues while `fun` returns `{:ok, list, acc}`
and stops on `{:error, reason}`.
"""
def flat_map_reduce_ok(list, acc, fun) do
do_flat_map_reduce_ok(list, {[], acc}, fun)
end
@@ -154,10 +130,6 @@ defmodule Module.Types.Helpers do
end
end
@doc """
Combines a list of guard expressions `when x when y when z` to an expression
combined with `or`, `x or y or z`.
"""
# TODO: Remove this and let multiple when be treated as multiple clauses,
# meaning they will be intersection types
def guards_to_or([]) do
@@ -167,30 +139,4 @@ defmodule Module.Types.Helpers do
def guards_to_or(guards) do
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} end)
end
@doc """
Like `Enum.zip/1` but will zip multiple lists together instead of only two.
"""
def zip_many(lists) do
zip_many(lists, [], [[]])
end
defp zip_many([], [], [[] | acc]) do
map_reverse(acc, [], &Enum.reverse/1)
end
defp zip_many([], remain, [last | acc]) do
zip_many(Enum.reverse(remain), [], [[] | [last | acc]])
end
defp zip_many([[] | _], remain, [last | acc]) do
zip_many(Enum.reverse(remain), [], [last | acc])
end
defp zip_many([[elem | list1] | list2], remain, [last | acc]) do
zip_many(list2, [list1 | remain], [[elem | last] | acc])
end
defp map_reverse([], acc, _fun), do: acc
defp map_reverse([head | tail], acc, fun), do: map_reverse(tail, [fun.(head) | acc], fun)
end
+1 -1
View File
@@ -327,7 +327,7 @@ defmodule Module.Types.Of do
%{context | warnings: [{__MODULE__, warning, location} | context.warnings]}
end
## Warning formatting
## Warning formating
def format_warning({:undefined_module, module, fun, arity}) do
[
+115 -352
View File
@@ -10,8 +10,8 @@ defmodule Module.Types.Pattern do
def of_head(patterns, guards, stack, context) do
with {:ok, types, context} <-
map_reduce_ok(patterns, context, &of_pattern(&1, stack, &2)),
# TODO: Check that of_guard/4 returns boolean() | :fail
{:ok, _, context} <- of_guard(guards_to_or(guards), :dynamic, stack, context),
# TODO: Check that of_guard/3 returns boolean() | :fail
{:ok, _, context} <- of_guard(guards_to_or(guards), stack, context),
do: {:ok, types, context}
end
@@ -86,75 +86,69 @@ defmodule Module.Types.Pattern do
# TODO: Some guards can be changed to intersection types or higher order types
@boolean {:union, [{:atom, true}, {:atom, false}]}
@number {:union, [:integer, :float]}
@unary_number_fun [{[:integer], :integer}, {[@number], :float}]
@binary_number_fun [
{[:integer, :integer], :integer},
{[:float, @number], :float},
{[@number, :float], :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_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} => @binary_number_fun,
{:+, 1} => @unary_number_fun,
{:+, 2} => @binary_number_fun,
{:-, 1} => @unary_number_fun,
{:-, 2} => @binary_number_fun,
{:/, 2} => @binary_number_fun,
{:abs, 1} => @unary_number_fun,
{: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}],
{:map_get, 2} => [{[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic}],
{:map_size, 1} => [{[{:map, [{:optional, :dynamic, :dynamic}]}], :integer}],
{:tl, 1} => [{[{:list, :dynamic}], :dynamic}],
{:tuple_size, 1} => [{[:tuple], :integer}],
{:node, 1} => [{[{:union, [:pid, :reference, :port]}], :atom}],
{:binary_part, 3} => [{[:binary, :integer, :integer], :binary}],
{:bit_size, 1} => [{[:binary], :integer}],
{:byte_size, 1} => [{[:binary], :integer}],
{:size, 1} => [{[{:union, [:binary, :tuple]}], @boolean}],
{:div, 2} => [{[:integer, :integer], :integer}],
{:rem, 2} => [{[:integer, :integer], :integer}],
{:node, 0} => [{[], :atom}],
{:self, 0} => [{[], :pid}],
{:bnot, 1} => [{[:integer], :integer}],
{:band, 2} => [{[:integer, :integer], :integer}],
{:bor, 2} => [{[:integer, :integer], :integer}],
{: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}]
{: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},
{:element, 2} => {[:integer, :tuple], :dynamic},
{:hd, 1} => {[{:list, :dynamic}], :dynamic},
{:length, 1} => {[{:list, :dynamic}], :integer},
{:map_get, 2} => {[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic},
{:map_size, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], :integer},
{:tl, 1} => {[{:list, :dynamic}], :dynamic},
{:tuple_size, 1} => {[:tuple], :integer},
{:node, 1} => {[{:union, [:pid, :reference, :port]}], :atom},
{:binary_part, 3} => {[:binary, :integer, :integer], :binary},
{:bit_size, 1} => {[:binary], :integer},
{:byte_size, 1} => {[:binary], :integer},
{:size, 1} => {[{:union, [:binary, :tuple]}], @boolean},
{:div, 2} => {[:integer, :integer], :integer},
{:rem, 2} => {[:integer, :integer], :integer},
{:node, 0} => {[], :atom},
{:self, 0} => {[], :pid},
{:bnot, 1} => {[:integer], :integer},
{:band, 2} => {[:integer, :integer], :integer},
{:bor, 2} => {[:integer, :integer], :integer},
{: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}
# Following guards are matched explicitly to handle
# type guard functions such as is_atom/1
@@ -169,6 +163,7 @@ defmodule Module.Types.Pattern do
:is_boolean,
:is_float,
:is_function,
:is_function,
:is_integer,
:is_list,
:is_map,
@@ -183,27 +178,26 @@ defmodule Module.Types.Pattern do
Refines the type variables in the typing context using type check guards
such as `is_integer/1`.
"""
def of_guard(expr, expected, %{context: stack_context} = stack, context)
when stack_context != :pattern do
of_guard(expr, expected, %{stack | context: :pattern}, context)
def of_guard(expr, %{context: stack_context} = stack, context) when stack_context != :pattern do
of_guard(expr, %{stack | context: :pattern}, context)
end
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, _expected, stack, context) do
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, left_type, context} <- of_guard(left, @boolean, stack, 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, :dynamic, keep_guarded(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, _expected, stack, context) do
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
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, @boolean, stack, context),
{:ok, _right_type, right_context} <- of_guard(right, :dynamic, stack, 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,
@@ -220,147 +214,71 @@ defmodule Module.Types.Pattern do
# The unary operators + and - are special cased to avoid common warnings until
# we add support for intersection types for the guard functions
# -integer / +integer
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _expected, _stack, context)
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _stack, context)
when guard in [:+, :-] and is_integer(integer) do
{:ok, :integer, context}
end
# -float / +float
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _expected, _stack, context)
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _stack, context)
when guard in [:+, :-] and is_float(float) do
{:ok, :float, context}
end
# tuple_size(arg) == integer
def of_guard(
{{:., _, [:erlang, :==]}, _, [{{:., _, [:erlang, :tuple_size]}, _, [var]}, size]} = expr,
expected,
stack,
context
)
when is_var(var) and is_integer(size) do
of_tuple_size(var, size, expr, expected, stack, context)
end
# integer == tuple_size(arg)
def of_guard(
{{:., _, [:erlang, :==]}, _, [size, {{:., _, [:erlang, :tuple_size]}, _, [var]}]} = expr,
expected,
stack,
context
)
when is_var(var) and is_integer(size) do
of_tuple_size(var, size, expr, expected, stack, context)
end
# fun(args)
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, expected, stack, context) do
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, stack, context) 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
signature = guard_signature(guard, length(args))
# 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 consider_type_guards? do
stack = push_expr_stack(expr, stack)
expected_clauses = filter_clauses(signature, expected, stack, context)
param_unions = signature_to_param_unions(expected_clauses, context)
arg_stack = %{stack | type_guards: {false, keep_guarded?}}
mfa = {:erlang, guard, length(args)}
with {:ok, arg_types, context} <-
map_reduce_ok(Enum.zip(args, param_unions), context, fn {arg, param}, context ->
of_guard(arg, param, arg_stack, context)
end),
{:ok, return_type, context} <-
unify_call(
arg_types,
expected_clauses,
expected,
mfa,
signature,
stack,
context,
type_guard?
) do
guard_sources = guard_sources(arg_types, type_guard?, keep_guarded?, context)
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
{:ok, context} <- unify_call(arg_types, param_types, stack, context) do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
{rest_arg_types, guard_sources}
_ ->
{arg_types, context.guard_sources}
end
guard_sources =
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
_, guard_sources ->
guard_sources
end)
{:ok, return_type, %{context | guard_sources: guard_sources}}
end
else
# Assume that type guards always return boolean
boolean = {:union, [atom: true, atom: false]}
[{_params, ^boolean}] = signature
{:ok, boolean, context}
{:ok, return_type, context}
end
end
# map.field
def of_guard({{:., meta1, [map, field]}, meta2, []}, expected, stack, context) do
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, expected, stack, context)
def of_guard({{:., meta1, [map, field]}, meta2, []}, stack, context) do
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, stack, context)
end
# var
def of_guard(var, _expected, _stack, context) when is_var(var) do
def of_guard(var, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
end
def of_guard(expr, _expected, stack, context) do
of_shared(expr, stack, context, &of_guard(&1, :dynamic, &2, &3))
end
defp of_tuple_size(var, size, expr, _expected, stack, context) do
{consider_type_guards?, _keep_guarded?} = stack.type_guards
result =
if consider_type_guards? do
stack = push_expr_stack(expr, stack)
tuple_elems = Enum.map(1..size//1, fn _ -> :dynamic end)
with {:ok, type, context} <- of_guard(var, :dynamic, stack, context),
{:ok, _type, context} <- unify({:tuple, size, tuple_elems}, type, stack, context),
do: {:ok, context}
else
{:ok, context}
end
case result do
{:ok, context} ->
boolean = {:union, [atom: true, atom: false]}
{:ok, boolean, context}
{:error, reason} ->
{:error, reason}
end
end
defp signature_to_param_unions(signature, context) do
signature
|> Enum.map(fn {params, _return} -> params end)
|> zip_many()
|> Enum.map(&to_union(&1, context))
end
# Collect guard sources from argument types, see type context documentation
# for more information
defp guard_sources(arg_types, type_guard?, keep_guarded?, context) do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
{rest_arg_types, guard_sources}
_ ->
{arg_types, context.guard_sources}
end
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
_, guard_sources ->
guard_sources
end)
def of_guard(expr, stack, context) do
of_shared(expr, stack, context, &of_guard/3)
end
defp collect_var_indexes_from_expr(expr, context) do
@@ -378,162 +296,15 @@ defmodule Module.Types.Pattern do
Map.keys(vars)
end
defp unify_call(args, clauses, _expected, _mfa, _signature, stack, context, true = _type_guard?) do
unify_type_guard_call(args, clauses, stack, context)
end
defp unify_call(args, clauses, expected, mfa, signature, stack, context, false = _type_guard?) do
unify_call(args, clauses, expected, mfa, signature, stack, context)
end
defp unify_call([], [{[], return}], _expected, _mfa, _signature, _stack, context) do
{:ok, return, context}
end
defp unify_call(args, clauses, expected, mfa, signature, stack, context) do
# Given the arguments:
# foo | bar, {:ok, baz | bat}
# Expand unions in arguments:
# foo | bar, {:ok, baz} | {:ok, bat}
# Permute arguments:
# foo, {:ok, baz}
# foo, {:ok, bat}
# bar, {:ok, baz}
# bar, {:ok, bat}
flatten_args = Enum.map(args, &flatten_union(&1, context))
cartesian_args = cartesian_product(flatten_args)
# Remove clauses that do not match the expected type
# Ignore type variables in parameters by changing them to dynamic
clauses =
clauses
|> filter_clauses(expected, stack, context)
|> Enum.map(fn {params, return} ->
{Enum.map(params, &var_to_dynamic/1), return}
end)
# For each permuted argument find the clauses they match
# All arguments must match at least one clause, but all clauses
# do not need to match
# Collect the return values from clauses that matched and collect
# the type contexts from unifying argument and parameter to
# infer type variables in arguments
result =
flat_map_ok(cartesian_args, fn cartesian_args ->
result =
Enum.flat_map(clauses, fn {params, return} ->
result =
map_ok(Enum.zip(cartesian_args, params), fn {arg, param} ->
case unify(arg, param, stack, context) do
{:ok, _type, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
case result do
{:ok, contexts} -> [{return, contexts}]
{:error, _reason} -> []
end
end)
if result != [] do
{:ok, result}
else
{:error, args}
end
end)
case result do
{:ok, returns_contexts} ->
{success_returns, contexts} = Enum.unzip(returns_contexts)
contexts = Enum.concat(contexts)
indexes =
for types <- flatten_args,
type <- types,
index <- collect_var_indexes_from_type(type),
do: index,
uniq: true
# Build unions from collected type contexts to unify with
# type variables from arguments
result =
map_reduce_ok(indexes, context, fn index, context ->
union =
contexts
|> Enum.map(&Map.fetch!(&1.types, index))
|> Enum.reject(&(&1 == :unbound))
if union == [] do
{:ok, {:var, index}, context}
else
unify({:var, index}, to_union(union, context), stack, context)
end
end)
case result do
{:ok, _types, context} -> {:ok, to_union(success_returns, context), context}
{:error, reason} -> {:error, reason}
end
{:error, args} ->
error(:unable_apply, {mfa, args, expected, signature, stack}, context)
end
end
defp unify_type_guard_call(args, [{params, return}], stack, context) do
result =
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
case unify(arg, param, stack, context) do
{:ok, _, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
case result do
{:ok, context} -> {:ok, return, context}
{:error, reason} -> {:error, reason}
end
end
defp cartesian_product(lists) do
List.foldr(lists, [[]], fn list, acc ->
for elem_list <- list,
list_acc <- acc,
do: [elem_list | list_acc]
defp unify_call(args, params, stack, context) do
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
case unify(arg, param, stack, context) do
{:ok, _, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
end
defp var_to_dynamic(type) do
{type, _acc} =
walk(type, :ok, fn
{:var, _index}, :ok ->
{:dynamic, :ok}
other, :ok ->
{other, :ok}
end)
type
end
defp collect_var_indexes_from_type(type) do
{_type, indexes} =
walk(type, [], fn
{:var, index}, indexes ->
{{:var, index}, [index | indexes]}
other, indexes ->
{other, indexes}
end)
indexes
end
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)
@@ -605,21 +376,13 @@ defmodule Module.Types.Pattern do
defp keep_guarded(%{type_guards: {consider?, _}} = stack),
do: %{stack | type_guards: {consider?, true}}
defp filter_clauses(signature, expected, stack, context) do
Enum.filter(signature, fn {_params, return} ->
match?({:ok, _type, _context}, unify(return, expected, stack, context))
end)
defp guard_signature(name, arity) do
Map.fetch!(@guard_functions, {name, arity})
end
Enum.each(@guard_functions, fn {{name, arity}, signature} ->
defp guard_signature(unquote(name), unquote(arity)), do: unquote(Macro.escape(signature))
end)
Enum.each(@type_guards, fn name ->
defp type_guard?(unquote(name)), do: true
end)
defp type_guard?(name) when is_atom(name), do: false
defp type_guard?(name) do
name in @type_guards
end
## Shared
+112 -268
View File
@@ -10,7 +10,6 @@ defmodule Module.Types.Unify do
# {:atom, atom} < :atom
# :integer
# :float
# :binary
# :pid
# :port
# :reference
@@ -21,37 +20,55 @@ defmodule Module.Types.Unify do
# {:tuple, size, [type]} < :tuple
# {:union, [type]}
# {:map, [{:required | :optional, key_type, value_type}]}
# {:fun, [{params, return}]}
#
# Once new types are added, they should be considered in:
#
# * unify (all)
# * format_type (all)
# * subtype? (subtypes only)
# * has_unbound_var? (composite only)
# * recursive_type? (composite only)
# * collect_var_indexes (composite only)
# * collect_vars (composite only)
# * lift_types (composite only)
# * flatten_union (composite only)
# * walk (composite only)
#
@doc """
Unifies two types and returns the unified type and an updated typing context
or an error in case of a typing conflict.
"""
def unify(same, same, _stack, context) do
def unify(source, target, stack, context) do
case do_unify(source, target, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, reason} ->
if stack.context == :pattern do
case do_unify(target, source, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, _} ->
{:error, reason}
end
else
{:error, reason}
end
end
end
defp do_unify(same, same, _stack, context) do
{:ok, same, context}
end
def unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
end
def unify(type, {:var, var}, stack, context) do
defp do_unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
end
def unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
defp do_unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
end
defp do_unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
result =
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
unify(source, target, stack, context)
@@ -63,26 +80,26 @@ defmodule Module.Types.Unify do
end
end
def unify({:list, source}, {:list, target}, stack, context) do
defp do_unify({:list, source}, {:list, target}, stack, context) do
case unify(source, target, stack, context) do
{:ok, type, context} -> {:ok, {:list, type}, context}
{:error, reason} -> {:error, reason}
end
end
def unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
defp do_unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
unify_maps(source_pairs, target_pairs, stack, context)
end
def unify(source, :dynamic, _stack, context) do
defp do_unify(source, :dynamic, _stack, context) do
{:ok, source, context}
end
def unify(:dynamic, target, _stack, context) do
defp do_unify(:dynamic, target, _stack, context) do
{:ok, target, context}
end
def unify({:union, types}, target, stack, context) do
defp do_unify({:union, types}, target, stack, context) do
unify_result =
map_reduce_ok(types, context, fn type, context ->
unify(type, target, stack, context)
@@ -94,7 +111,7 @@ defmodule Module.Types.Unify do
end
end
def unify(source, target, stack, context) do
defp do_unify(source, target, stack, context) do
cond do
# TODO: This condition exists to handle unions with unbound vars.
match?({:union, _}, target) and has_unbound_var?(target, context) ->
@@ -108,11 +125,11 @@ defmodule Module.Types.Unify do
end
end
def unify_var(var, :dynamic, _stack, context, _var_source?) do
defp unify_var(var, :dynamic, _stack, context, _var_source?) do
{:ok, {:var, var}, context}
end
def unify_var(var, type, stack, context, var_source?) do
defp unify_var(var, type, stack, context, var_source?) do
case context.types do
%{^var => :unbound} ->
context = refine_var!(var, type, stack, context)
@@ -188,7 +205,7 @@ defmodule Module.Types.Unify do
# * All required keys on each side need to match to the other side.
# * All optional keys on each side that do not match must be discarded.
def unify_maps(source_pairs, target_pairs, stack, context) do
defp unify_maps(source_pairs, target_pairs, stack, context) do
{source_required, source_optional} = split_pairs(source_pairs)
{target_required, target_optional} = split_pairs(target_pairs)
@@ -219,7 +236,7 @@ defmodule Module.Types.Unify do
end
end
def unify_source_required(source_required, target_pairs, stack, context) do
defp unify_source_required(source_required, target_pairs, stack, context) do
map_reduce_ok(source_required, context, fn {source_key, source_value}, context ->
Enum.find_value(target_pairs, fn {target_kind, target_key, target_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -239,7 +256,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_target_required(target_required, source_pairs, stack, context) do
defp unify_target_required(target_required, source_pairs, stack, context) do
map_reduce_ok(target_required, context, fn {target_key, target_value}, context ->
Enum.find_value(source_pairs, fn {source_kind, source_key, source_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -259,7 +276,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_source_optional(source_optional, target_optional, stack, context) do
defp unify_source_optional(source_optional, target_optional, stack, context) do
flat_map_reduce_ok(source_optional, context, fn {source_key, source_value}, context ->
Enum.find_value(target_optional, fn {target_key, target_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -279,7 +296,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_target_optional(target_optional, source_optional, stack, context) do
defp unify_target_optional(target_optional, source_optional, stack, context) do
flat_map_reduce_ok(target_optional, context, fn {target_key, target_value}, context ->
Enum.find_value(source_optional, fn {source_key, source_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -308,7 +325,7 @@ defmodule Module.Types.Unify do
{required, optional}
end
def error(type, reason, context), do: {:error, {type, reason, context}}
defp error(type, reason, context), do: {:error, {type, reason, context}}
@doc """
Push expression to stack.
@@ -488,12 +505,6 @@ defmodule Module.Types.Unify do
end)
end
defp recursive_type?({:fun, clauses}, parents, context) do
Enum.any?(clauses, fn {args, return} ->
Enum.any?([return | args], &recursive_type?(&1, [clauses | parents], context))
end)
end
defp recursive_type?(_other, _parents, _context) do
false
end
@@ -501,55 +512,65 @@ defmodule Module.Types.Unify do
@doc """
Collects all type vars recursively.
"""
def collect_var_indexes(type, context, acc \\ %{}) do
{_type, indexes} =
walk(type, acc, fn
{:var, var}, acc ->
case acc do
%{^var => _} ->
{{:var, var}, acc}
def collect_var_indexes(type, context, acc \\ %{})
%{} ->
case context.types do
%{^var => :unbound} ->
{{:var, var}, Map.put(acc, var, true)}
def collect_var_indexes({:var, var}, context, acc) do
case acc do
%{^var => _} ->
acc
%{^var => type} ->
{{:var, var}, collect_var_indexes(type, context, Map.put(acc, var, true))}
end
end
other, acc ->
{other, acc}
end)
indexes
%{} ->
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.
"""
def has_unbound_var?(type, context) do
walk(type, :ok, fn
{:var, var}, acc ->
case context.types do
%{^var => :unbound} ->
throw(:has_unbound_var?)
%{^var => type} ->
has_unbound_var?(type, context)
{{:var, var}, acc}
end
other, acc ->
{other, acc}
end)
false
catch
:throw, :has_unbound_var? -> true
def has_unbound_var?({:var, var}, context) do
case context.types do
%{^var => :unbound} -> true
%{^var => type} -> has_unbound_var?(type, context)
end
end
def has_unbound_var?({:tuple, _, args}, context),
do: Enum.any?(args, &has_unbound_var?(&1, context))
def has_unbound_var?({:union, args}, context),
do: Enum.any?(args, &has_unbound_var?(&1, context))
def has_unbound_var?({:list, arg}, context),
do: has_unbound_var?(arg, context)
def has_unbound_var?({:map, pairs}, context) do
Enum.any?(pairs, fn {_, key, value} ->
has_unbound_var?(key, context) or has_unbound_var?(value, context)
end)
end
def has_unbound_var?(_type, _context), do: false
@doc """
Returns true if it is a singleton type.
@@ -648,15 +669,17 @@ defmodule Module.Types.Unify do
def to_union([type], _context), do: type
def to_union(types, context) when types != [] do
case unique_super_types(unnest_unions(types), context) do
flat_types = flatten_union(types)
case unique_super_types(flat_types, context) do
[type] -> type
types -> {:union, types}
end
end
defp unnest_unions(types) do
defp flatten_union(types) do
Enum.flat_map(types, fn
{:union, types} -> unnest_unions(types)
{:union, types} -> flatten_union(types)
type -> [type]
end)
end
@@ -686,10 +709,6 @@ defmodule Module.Types.Unify do
@doc """
Lifts type variables to their inferred types from the context.
"""
def lift_types(types, %{lifted_types: _} = context) do
Enum.map_reduce(types, context, &lift_type/2)
end
def lift_types(types, context) do
context = %{
types: context.types,
@@ -697,7 +716,8 @@ defmodule Module.Types.Unify do
lifted_counter: 0
}
Enum.map_reduce(types, context, &lift_type/2)
{types, _context} = Enum.map_reduce(types, context, &lift_type/2)
types
end
# Lift type variable to its inferred (hopefully concrete) types from the context
@@ -755,16 +775,6 @@ defmodule Module.Types.Unify do
{{:list, type}, context}
end
defp lift_type({:fun, clauses}, context) do
clauses =
Enum.map_reduce(clauses, context, fn {args, return}, context ->
{[return | args], context} = Enum.map_reduce([return | args], context, &lift_type/2)
{{args, return}, context}
end)
{{:fun, clauses}, context}
end
defp lift_type(other, context) do
{other, context}
end
@@ -778,107 +788,6 @@ defmodule Module.Types.Unify do
{type, context}
end
# TODO: Figure out function expansion
@doc """
Expand unions so that all unions are at the top level.
{integer() | float()} => {integer()} | {float()}
"""
def flatten_union({:union, types}, context) do
Enum.flat_map(types, &flatten_union(&1, context))
end
def flatten_union(type, context) do
List.wrap(do_flatten_union(type, context))
end
def do_flatten_union({:tuple, num, types}, context) do
flatten_union_tuple(types, num, context, [])
end
def do_flatten_union({:list, type}, context) do
case do_flatten_union(type, context) do
{:union, union_types} -> Enum.map(union_types, &{:list, &1})
_type -> [{:list, type}]
end
end
def do_flatten_union({:map, pairs}, context) do
flatten_union_map(pairs, context, [])
end
def do_flatten_union({:var, var}, context) do
if looping_var?(var, context, []) do
{:var, var}
else
case context.types do
%{^var => :unbound} -> {:var, var}
%{^var => {:union, types}} -> Enum.map(types, &do_flatten_union(&1, context))
%{^var => type} -> do_flatten_union(type, context)
end
end
end
def do_flatten_union(type, _context) do
type
end
defp flatten_union_tuple([type | types], num, context, acc) do
case do_flatten_union(type, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_tuple(types, num, context, [&1 | acc]))
type ->
flatten_union_tuple(types, num, context, [type | acc])
end
end
defp flatten_union_tuple([], num, _context, acc) do
[{:tuple, num, Enum.reverse(acc)}]
end
defp flatten_union_map([{kind, key, value} | pairs], context, acc) do
case do_flatten_union(key, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_map_value(kind, &1, value, pairs, context, acc))
type ->
flatten_union_map_value(kind, type, value, pairs, context, acc)
end
end
defp flatten_union_map([], _context, acc) do
[{:map, Enum.reverse(acc)}]
end
defp flatten_union_map_value(kind, key, value, pairs, context, acc) do
case do_flatten_union(value, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_map(pairs, context, [{kind, key, &1} | acc]))
value ->
flatten_union_map(pairs, context, [{kind, key, value} | acc])
end
end
defp looping_var?(var, context, parents) do
case context.types do
%{^var => :unbound} ->
false
%{^var => {:var, type}} ->
if var in parents do
true
else
looping_var?(type, context, [var | parents])
end
%{^var => _type} ->
false
end
end
@doc """
Formats types.
@@ -888,7 +797,7 @@ defmodule Module.Types.Unify do
def format_type({:map, pairs}, true) do
case List.keyfind(pairs, {:atom, :__struct__}, 1) do
{:required, {:atom, :__struct__}, {:atom, struct}} ->
["%", inspect(struct), "{}"]
"%#{inspect(struct)}{}"
_ ->
"map()"
@@ -896,31 +805,24 @@ defmodule Module.Types.Unify do
end
def format_type({:union, types}, simplify?) do
types
|> Enum.map(&format_type(&1, simplify?))
|> Enum.intersperse(" | ")
"#{Enum.map_join(types, " | ", &format_type(&1, simplify?))}"
end
def format_type({:tuple, _, types}, simplify?) do
format =
types
|> Enum.map(&format_type(&1, simplify?))
|> Enum.intersperse(", ")
["{", format, "}"]
"{#{Enum.map_join(types, ", ", &format_type(&1, simplify?))}}"
end
def format_type({:list, type}, simplify?) do
["[", format_type(type, simplify?), "]"]
"[#{format_type(type, simplify?)}]"
end
def format_type({:map, pairs}, false) do
case List.keytake(pairs, {:atom, :__struct__}, 1) do
{{:required, {:atom, :__struct__}, {:atom, struct}}, pairs} ->
["%", inspect(struct), "{", format_map_pairs(pairs), "}"]
"%#{inspect(struct)}{#{format_map_pairs(pairs)}}"
_ ->
["%{", format_map_pairs(pairs), "}"]
"%{#{format_map_pairs(pairs)}}"
end
end
@@ -929,84 +831,26 @@ defmodule Module.Types.Unify do
end
def format_type({:var, index}, _simplify?) do
["var", Integer.to_string(index + 1)]
end
def format_type({:fun, clauses}, simplify?) do
format =
Enum.map(clauses, fn {params, return} ->
params = Enum.intersperse(Enum.map(params, &format_type(&1, simplify?)), ", ")
params = if params == [], do: params, else: [params, " "]
return = format_type(return, simplify?)
[params, "-> ", return]
end)
["(", Enum.intersperse(format, "; "), ")"]
"var#{index + 1}"
end
def format_type(atom, _simplify?) when is_atom(atom) do
[Atom.to_string(atom), "()"]
"#{atom}()"
end
defp format_map_pairs(pairs) do
{atoms, others} = Enum.split_with(pairs, &match?({:required, {:atom, _}, _}, &1))
{required, optional} = Enum.split_with(others, &match?({:required, _, _}, &1))
(atoms ++ required ++ optional)
|> Enum.map(fn
Enum.map_join(atoms ++ required ++ optional, ", ", fn
{:required, {:atom, atom}, right} ->
[Atom.to_string(atom), ": ", format_type(right, false)]
"#{atom}: #{format_type(right, false)}"
{:required, left, right} ->
[format_type(left, false), " => ", format_type(right, false)]
"#{format_type(left, false)} => #{format_type(right, false)}"
{:optional, left, right} ->
["optional(", format_type(left, false), ") => ", format_type(right, false)]
"optional(#{format_type(left, false)}) => #{format_type(right, false)}"
end)
|> Enum.intersperse(", ")
end
@doc """
Performs a depth-first, pre-order traversal of the type tree using an accumulator.
"""
def walk({:map, pairs}, acc, fun) do
{pairs, acc} =
Enum.map_reduce(pairs, acc, fn {kind, key, value}, acc ->
{key, acc} = walk(key, acc, fun)
{value, acc} = walk(value, acc, fun)
{{kind, key, value}, acc}
end)
fun.({:map, pairs}, acc)
end
def walk({:union, types}, acc, fun) do
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
fun.({:union, types}, acc)
end
def walk({:tuple, num, types}, acc, fun) do
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
fun.({:tuple, num, types}, acc)
end
def walk({:list, type}, acc, fun) do
{type, acc} = walk(type, acc, fun)
fun.({:list, type}, acc)
end
def walk({:fun, clauses}, acc, fun) do
{clauses, acc} =
Enum.map_reduce(clauses, acc, fn {params, return}, acc ->
{params, acc} = Enum.map_reduce(params, acc, &walk(&1, &2, fun))
{return, acc} = walk(return, acc, fun)
{{params, return}, acc}
end)
fun.({:fun, clauses}, acc)
end
def walk(type, acc, fun) do
fun.(type, acc)
end
end
+1 -38
View File
@@ -596,11 +596,9 @@ defmodule OptionParser do
raise ArgumentError, ":switches and :strict cannot be given together"
switches = opts[:switches] ->
validate_switches(switches)
{switches, false}
strict = opts[:strict] ->
validate_switches(strict)
{strict, true}
true ->
@@ -616,20 +614,6 @@ defmodule OptionParser do
}
end
defp validate_switches(switches) do
Enum.map(switches, &validate_switch/1)
end
defp validate_switch({_name, type_or_type_and_modifiers}) do
valid = [:boolean, :count, :integer, :float, :string, :keep]
invalid = List.wrap(type_or_type_and_modifiers) -- valid
if invalid != [] do
raise ArgumentError,
"invalid switch types/modifiers: " <> Enum.map_join(invalid, ", ", &inspect/1)
end
end
defp validate_option(value, kinds) do
{invalid?, value} =
cond do
@@ -828,15 +812,7 @@ defmodule OptionParser do
if type = get_type(option, opts, types) do
"#{option} : Missing argument of type #{type}"
else
msg = "#{option} : Unknown option"
case did_you_mean(option, types) do
{similar, score} when score > 0.8 ->
msg <> ". Did you mean --#{similar}?"
_ ->
msg
end
"#{option} : Unknown option"
end
end
@@ -855,17 +831,4 @@ defmodule OptionParser do
types[key]
end
end
defp did_you_mean(option, types) do
key = option |> String.trim_leading("-") |> String.replace("-", "_")
Enum.reduce(types, {nil, 0}, &max_similar(&1, key, &2))
end
defp max_similar({source, _}, target, {_, current} = best) do
source = Atom.to_string(source)
score = String.jaro_distance(source, target)
option = String.replace(source, "_", "-")
if score < current, do: best, else: {option, score}
end
end
+1 -12
View File
@@ -5,8 +5,7 @@ defmodule Path do
The functions in this module may receive a chardata as
argument (i.e. a string or a list of characters / string)
and will always return a string (encoded in UTF-8). If a binary
is given, in whatever encoding, its encoding will be kept.
and will always return a string (encoded in UTF-8).
The majority of the functions in this module do not
interact with the file system, except for a few functions
@@ -426,16 +425,6 @@ defmodule Path do
@doc """
Returns the extension of the last component of `path`.
The behaviour of this function changed in Erlang/OTP 24 for filenames
starting with a dot and without an extension. For example, for a file
named ".gitignore", `extname/1` now returns an empty string, while it
would return ".gitignore" in previous Erlang/OTP versions. This was
done to match the behaviour of `rootname/1`, which would return
".gitignore" as its name (and therefore it cannot also be an extension).
See `basename/1` and `rootname/1` for related functions to extract
information from paths.
## Examples
iex> Path.extname("foo.erl")
+1 -1
View File
@@ -222,7 +222,7 @@ defmodule Port do
@doc """
Sends `data` to the port driver `port`.
For more information, see `:erlang.port_command/3`.
For more information, see `:erlang.port_command/2`.
Inlined by the compiler.
"""
-7
View File
@@ -383,19 +383,12 @@ defmodule Process do
@type spawn_opt ::
:link
| :monitor
| {:monitor, monitor_option()}
| {:priority, :low | :normal | :high}
| {:fullsweep_after, non_neg_integer}
| {:min_heap_size, non_neg_integer}
| {:min_bin_vheap_size, non_neg_integer}
| {:max_heap_size, heap_size}
| {:message_queue_data, :off_heap | :on_heap}
@type spawn_opts :: [spawn_opt]
# TODO: Use :erlang.monitor_option() on Erlang/OTP 24+
@typep monitor_option ::
[alias: :explicit_unalias | :demonitor | :reply_demonitor, tag: term()]
@doc """
Spawns the given function according to the given options.
+4 -89
View File
@@ -275,7 +275,7 @@ defmodule Protocol do
name = unquote(name)
arity = unquote(arity)
@__functions__ [{name, arity} | @__functions__]
@functions [{name, arity} | @functions]
# Generate a fake definition with the user
# signature that will be used by docs
@@ -548,9 +548,6 @@ defmodule Protocol do
| {:error, :not_a_protocol}
| {:error, :no_beam_info}
def consolidate(protocol, types) when is_atom(protocol) do
# Ensure the types are sorted so the compiled beam is deterministic
types = Enum.sort(types)
with {:ok, ast_info, specs, compile_info} <- beam_protocol(protocol),
{:ok, definitions} <- change_debug_info(protocol, ast_info, types),
do: compile(definitions, specs, compile_info)
@@ -684,8 +681,6 @@ defmodule Protocol do
def __protocol__(name, do: block) do
quote do
defmodule unquote(name) do
@before_compile Protocol
# We don't allow function definition inside protocols
import Kernel,
except: [
@@ -693,7 +688,6 @@ defmodule Protocol do
def: 2,
defp: 1,
defp: 2,
defdelegate: 2,
defguard: 1,
defguardp: 1,
defmacro: 1,
@@ -709,7 +703,7 @@ defmodule Protocol do
@compile :debug_info
# Set up a clear slate to store defined functions
@__functions__ []
@functions []
@fallback_to_any false
# Invoke the user given block
@@ -721,85 +715,6 @@ defmodule Protocol do
end
end
defp callback_ast_to_fa({kind, {:"::", meta, [{name, _, args}, _return]}, _pos})
when kind in [:callback, :macrocallback] do
[{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa(
{kind, {:when, _, [{:"::", meta, [{name, _, args}, _return]}, _vars]}, _pos}
)
when kind in [:callback, :macrocallback] do
[{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa({kind, _, _pos}) when kind in [:callback, :macrocallback] do
[]
end
defp callback_metas(module, kind)
when kind in [:callback, :macrocallback] do
:lists.flatmap(&callback_ast_to_fa/1, Module.get_attribute(module, kind))
|> :maps.from_list()
end
defp get_callback_line(fa, metas),
do: :maps.get(fa, metas, [])[:line]
defp warn(message, env, nil) do
IO.warn(message, Macro.Env.stacktrace(env))
end
defp warn(message, env, line) when is_integer(line) do
stacktrace = :maps.update(:line, line, env) |> Macro.Env.stacktrace()
IO.warn(message, stacktrace)
end
# TODO: Convert the following warnings into errors future Elixir versions
def __before_compile__(env) do
# Callbacks
callback_metas = callback_metas(env.module, :callback)
callbacks = :maps.keys(callback_metas)
functions = Module.get_attribute(env.module, :__functions__)
:lists.map(
fn {name, arity} = fa ->
warn(
"cannot define @callback #{name}/#{arity} inside protocol, use def/1 to outline your protocol definition",
env,
get_callback_line(fa, callback_metas)
)
end,
callbacks -- functions
)
# Macro Callbacks
macrocallback_metas = callback_metas(env.module, :macrocallback)
macrocallbacks = :maps.keys(macrocallback_metas)
:lists.map(
fn {name, arity} = fa ->
warn(
"cannot define @macrocallback #{name}/#{arity} inside protocol, use def/1 to outline your protocol definition",
env,
get_callback_line(fa, macrocallback_metas)
)
end,
macrocallbacks
)
# Optional Callbacks
optional_callbacks = Module.get_attribute(env.module, :optional_callbacks)
if length(optional_callbacks) > 0 do
warn(
"cannot define @optional_callbacks inside protocol, all of the protocol definitions are required",
env,
nil
)
end
end
defp after_defprotocol do
quote bind_quoted: [built_in: __built_in__()] do
any_impl_for =
@@ -890,11 +805,11 @@ defmodule Protocol do
@doc false
@spec __protocol__(:module) :: __MODULE__
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@__functions__))
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@functions))
@spec __protocol__(:consolidated?) :: boolean
@spec __protocol__(:impls) :: :not_consolidated | {:consolidated, [module]}
Kernel.def(__protocol__(:module), do: __MODULE__)
Kernel.def(__protocol__(:functions), do: unquote(:lists.sort(@__functions__)))
Kernel.def(__protocol__(:functions), do: unquote(:lists.sort(@functions)))
Kernel.def(__protocol__(:consolidated?), do: false)
Kernel.def(__protocol__(:impls), do: :not_consolidated)
end
+6 -6
View File
@@ -40,14 +40,14 @@ defmodule Range do
## Definition
An increasing range `first..last//step` is a range from
`first` to `last` increasing by `step` where `step` must be a positive
integer and all values `v` must be `first <= v and v <= last`. Therefore, a range
`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 `step` must be a negative
integer and values `v` must be `first >= v and v >= last`. Therefore, a range
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`.
@@ -349,7 +349,7 @@ defimpl Inspect, for: Range do
import Inspect.Algebra
import Kernel, except: [inspect: 2]
def inspect(first..last//1, opts) when last >= first do
def inspect(first..last//1, opts) do
concat([to_doc(first, opts), "..", to_doc(last, opts)])
end
+32 -35
View File
@@ -34,12 +34,6 @@ defmodule Record do
# expands to: "@type user :: {:user, String.t(), integer}"
end
## Reflection
A list of all records in a module, if any, can be retrieved by reading the
`@__records__` module attribute. It returns a list of maps with the record
kind, name, tag, and fields. The attribute is only available inside the
module definition.
"""
@doc """
@@ -53,8 +47,8 @@ defmodule Record do
## Options
This function requires one of the following options, which are exclusive to each
other (i.e., only one of them can be used in the same call):
This function accepts the following options, which are exclusive to each other
(i.e., only one of them can be used in the same call):
* `:from` - (binary representing a path to a file) path to the Erlang file
that contains the record definition to extract; with this option, this
@@ -66,8 +60,6 @@ defmodule Record do
this function uses the same path lookup used by the `-include_lib`
attribute used in Erlang modules.
It additionally accepts the following optional, non-exclusive options:
* `:includes` - (a list of directories as binaries) if the record being
extracted depends on relative includes, this option allows developers
to specify the directory where those relative includes exist.
@@ -113,8 +105,21 @@ defmodule Record do
## Options
Accepts the same options as listed for `Record.extract/2`.
This function accepts the following options, which are exclusive to each other
(i.e., only one of them can be used in the same call):
* `:from` - (binary representing a path to a file) path to the Erlang file
that contains the record definitions to extract; with this option, this
function uses the same path lookup used by the `-include` attribute used in
Erlang modules.
* `:from_lib` - (binary representing a path to a file) path to the Erlang
file that contains the record definitions to extract; with this option,
this function uses the same path lookup used by the `-include_lib`
attribute used in Erlang modules.
These options are expected to be literals (including the binary values) at
compile time.
"""
@spec extract_all(keyword) :: [{name :: atom, keyword}]
def extract_all(opts) when is_list(opts) do
@@ -250,8 +255,10 @@ defmodule Record do
"""
defmacro defrecord(name, tag \\ nil, kv) do
quote bind_quoted: [name: name, tag: tag, kv: kv] do
fields = Record.__fields__(:defrecord, kv)
Record.__validate__(__MODULE__, name, fields)
tag = tag || name
fields = Record.__record__(__MODULE__, :defrecord, name, tag, kv)
defmacro unquote(name)(args \\ []) do
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
@@ -268,8 +275,10 @@ defmodule Record do
"""
defmacro defrecordp(name, tag \\ nil, kv) do
quote bind_quoted: [name: name, tag: tag, kv: kv] do
fields = Record.__fields__(:defrecordp, kv)
Record.__validate__(__MODULE__, name, fields)
tag = tag || name
fields = Record.__record__(__MODULE__, :defrecordp, name, tag, kv)
defmacrop unquote(name)(args \\ []) do
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
@@ -281,6 +290,13 @@ defmodule Record do
end
end
@doc false
def __validate__(module, name, fields) do
error_on_duplicate_record(module, name)
# TODO: Make it raise on v2.0
warn_on_duplicate_key(:lists.keysort(1, fields))
end
defp error_on_duplicate_record(module, name) do
defined_arity =
Enum.find(0..2, fn arity ->
@@ -306,28 +322,9 @@ defmodule Record do
warn_on_duplicate_key(rest)
end
# Callback invoked from the record/2 macro.
@doc false
def __record__(module, kind, name, tag, kv) do
error_on_duplicate_record(module, name)
fields = fields(kind, kv)
Module.register_attribute(module, :__records__, accumulate: true)
Module.put_attribute(module, :__records__, %{
kind: kind,
name: name,
tag: tag,
fields: :lists.map(&elem(&1, 0), fields)
})
# TODO: Make it raise on v2.0
warn_on_duplicate_key(:lists.keysort(1, fields))
fields
end
# Normalizes of record fields to have default values.
defp fields(kind, fields) do
@doc false
def __fields__(type, fields) do
normalizer_fun = fn
{key, value} when is_atom(key) ->
try do
@@ -343,7 +340,7 @@ defmodule Record do
{key, nil}
other ->
raise ArgumentError, "#{kind} fields must be atoms, got: #{inspect(other)}"
raise ArgumentError, "#{type} fields must be atoms, got: #{inspect(other)}"
end
:lists.map(normalizer_fun, fields)
+3 -6
View File
@@ -274,9 +274,6 @@ defmodule Regex do
iex> Regex.match?(~r/foo/, "bar")
false
Elixir also provides `Kernel.=~/2` and `String.match?/2` as
an alternative to test strings against regular expressions and
strings.
"""
@spec match?(t, String.t()) :: boolean
def match?(%Regex{} = regex, string) when is_binary(string) do
@@ -374,7 +371,7 @@ defmodule Regex do
## Examples
iex> Regex.source(~r/foo/)
iex> Regex.source(~r(foo))
"foo"
"""
@@ -388,7 +385,7 @@ defmodule Regex do
## Examples
iex> Regex.opts(~r/foo/m)
iex> Regex.opts(~r(foo)m)
"m"
"""
@@ -456,7 +453,7 @@ defmodule Regex do
[[{0, 1}], [{9, 3}]]
"""
@spec scan(t(), String.t(), [term()]) :: [[String.t()]] | [[{integer(), integer()}]]
@spec scan(t, String.t(), [term]) :: [[String.t()]]
def scan(regex, string, options \\ [])
def scan(%Regex{} = regex, string, options) when is_binary(string) do
+1 -7
View File
@@ -260,10 +260,6 @@ defmodule Registry do
end
end
def send({registry, key, _value}, msg) do
Registry.send({registry, key}, msg)
end
@doc false
def unregister_name({registry, key}), do: unregister(registry, key)
def unregister_name({registry, key, _value}), do: unregister(registry, key)
@@ -1065,9 +1061,7 @@ defmodule Registry do
:ets.lookup(registry, key)
catch
:error, :badarg ->
raise ArgumentError,
"unknown registry: #{inspect(registry)}. Either the registry name is invalid or " <>
"the registry is not running, possibly because its application isn't started"
raise ArgumentError, "unknown registry: #{inspect(registry)}"
else
[{^key, value}] -> {:ok, value}
_ -> :error
+111 -226
View File
@@ -68,8 +68,8 @@ defmodule String do
## Code points and grapheme cluster
The functions in this module act according to
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
The functions in this module act according to the Unicode
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.
@@ -494,12 +494,6 @@ defmodule String do
{:infinity, false} ->
:binary.split(string, pattern, [:global])
{:infinity, true} ->
:binary.split(string, pattern, [:global, :trim_all])
{2, false} ->
:binary.split(string, pattern)
_ ->
pattern = maybe_compile_pattern(pattern)
split_each(string, pattern, trim, parts_to_index(parts))
@@ -513,11 +507,9 @@ defmodule String do
defp split_empty(string, _, 1), do: [string]
defp split_empty(string, trim, count) do
case :unicode_util.gc(string) do
[gc] -> [grapheme_to_binary(gc) | split_empty(<<>>, trim, count - 1)]
[gc | rest] -> [grapheme_to_binary(gc) | split_empty(rest, trim, count - 1)]
[] -> split_empty("", trim, 1)
{:error, <<byte, rest::bits>>} -> [<<byte>> | split_empty(rest, trim, count - 1)]
case next_grapheme(string) do
{h, t} -> [h | split_empty(t, trim, count - 1)]
nil -> split_empty("", trim, 1)
end
end
@@ -650,10 +642,8 @@ defmodule String do
end
defp do_split_at(string, position) do
remaining = byte_size_remaining_at(string, position)
start = byte_size(string) - remaining
<<left::size(start)-binary, right::size(remaining)-binary>> = string
{left, right}
{byte_size, rest} = String.Unicode.split_at(string, position)
{binary_part(string, 0, byte_size), rest || ""}
end
@doc ~S"""
@@ -811,7 +801,7 @@ defmodule String do
end
def upcase(string, :default) when is_binary(string) do
String.Unicode.upcase(string, [], :default)
String.Casing.upcase(string, [], :default)
end
def upcase(string, :ascii) when is_binary(string) do
@@ -819,7 +809,7 @@ defmodule String do
end
def upcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
String.Unicode.upcase(string, [], mode)
String.Casing.upcase(string, [], mode)
end
defp upcase_ascii(<<char, rest::bits>>) when char >= ?a and char <= ?z,
@@ -879,7 +869,7 @@ defmodule String do
end
def downcase(string, :default) when is_binary(string) do
String.Unicode.downcase(string, [], :default)
String.Casing.downcase(string, [], :default)
end
def downcase(string, :ascii) when is_binary(string) do
@@ -887,7 +877,7 @@ defmodule String do
end
def downcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
String.Unicode.downcase(string, [], mode)
String.Casing.downcase(string, [], mode)
end
defp downcase_ascii(<<char, rest::bits>>) when char >= ?A and char <= ?Z,
@@ -926,7 +916,7 @@ defmodule String do
end
def capitalize(string, mode) when is_binary(string) do
{char, rest} = String.Unicode.titlecase_once(string, mode)
{char, rest} = String.Casing.titlecase_once(string, mode)
char <> downcase(rest, mode)
end
@@ -1492,7 +1482,7 @@ defmodule String do
defp replace_guarded(subject, "", replacement_binary, options)
when is_binary(replacement_binary) do
if Keyword.get(options, :global, true) do
intersperse_bin(subject, replacement_binary, [replacement_binary])
IO.iodata_to_binary([replacement_binary | intersperse_bin(subject, replacement_binary)])
else
replacement_binary <> subject
end
@@ -1500,7 +1490,7 @@ defmodule String do
defp replace_guarded(subject, "", replacement_fun, options) do
if Keyword.get(options, :global, true) do
intersperse_fun(subject, replacement_fun, [replacement_fun.("")])
IO.iodata_to_binary([replacement_fun.("") | intersperse_fun(subject, replacement_fun)])
else
IO.iodata_to_binary([replacement_fun.("") | subject])
end
@@ -1530,31 +1520,17 @@ defmodule String do
end
end
defp intersperse_bin(subject, replacement, acc) do
case :unicode_util.gc(subject) do
[current | rest] ->
intersperse_bin(rest, replacement, [replacement, current | acc])
[] ->
reverse_characters_to_binary(acc)
{:error, <<byte, rest::bits>>} ->
reverse_characters_to_binary(acc) <>
<<byte>> <> intersperse_bin(rest, replacement, [replacement])
defp intersperse_bin(subject, replacement) do
case next_grapheme(subject) do
{current, rest} -> [current, replacement | intersperse_bin(rest, replacement)]
nil -> []
end
end
defp intersperse_fun(subject, replacement, acc) do
case :unicode_util.gc(subject) do
[current | rest] ->
intersperse_fun(rest, replacement, [replacement.(""), current | acc])
[] ->
reverse_characters_to_binary(acc)
{:error, <<byte, rest::bits>>} ->
reverse_characters_to_binary(acc) <>
<<byte>> <> intersperse_fun(rest, replacement, [replacement.("")])
defp intersperse_fun(subject, replacement) do
case next_grapheme(subject) do
{current, rest} -> [current, replacement.("") | intersperse_fun(rest, replacement)]
nil -> []
end
end
@@ -1607,17 +1583,16 @@ defmodule String do
"""
@spec reverse(t) :: t
def reverse(string) when is_binary(string) do
do_reverse(:unicode_util.gc(string), [])
do_reverse(next_grapheme(string), [])
end
defp do_reverse([grapheme | rest], acc),
do: do_reverse(:unicode_util.gc(rest), [grapheme | acc])
defp do_reverse({grapheme, rest}, acc) do
do_reverse(next_grapheme(rest), [grapheme | acc])
end
defp do_reverse([], acc),
do: :unicode.characters_to_binary(acc)
defp do_reverse(nil, acc), do: IO.iodata_to_binary(acc)
defp do_reverse({:error, <<byte, rest::bits>>}, acc),
do: :unicode.characters_to_binary(acc) <> <<byte>> <> do_reverse(:unicode_util.gc(rest), [])
@compile {:inline, duplicate: 2}
@doc """
Returns a string `subject` repeated `n` times.
@@ -1636,7 +1611,6 @@ defmodule String do
"abcabc"
"""
@compile {:inline, duplicate: 2}
@spec duplicate(t, non_neg_integer) :: t
def duplicate(subject, n) when is_binary(subject) and is_integer(n) and n >= 0 do
:binary.copy(subject, n)
@@ -1669,19 +1643,7 @@ defmodule String do
"""
@spec codepoints(t) :: [codepoint]
def codepoints(string) when is_binary(string) do
do_codepoints(string)
end
defp do_codepoints(<<codepoint::utf8, rest::bits>>) do
[<<codepoint::utf8>> | do_codepoints(rest)]
end
defp do_codepoints(<<byte, rest::bits>>) do
[<<byte>> | do_codepoints(rest)]
end
defp do_codepoints(<<>>), do: []
defdelegate codepoints(string), to: String.Unicode
@doc ~S"""
Returns the next code point in a string.
@@ -1724,10 +1686,9 @@ defmodule String do
Binary pattern matching, however, is simpler and more efficient,
so pick the option that better suits your use case.
"""
@compile {:inline, next_codepoint: 1}
@spec next_codepoint(t) :: {codepoint, t} | nil
def next_codepoint(<<cp::utf8, rest::binary>>), do: {<<cp::utf8>>, rest}
def next_codepoint(<<byte, rest::binary>>), do: {<<byte>>, rest}
def next_codepoint(<<>>), do: nil
defdelegate next_codepoint(string), to: String.Unicode
@doc ~S"""
Checks whether `string` contains only valid characters.
@@ -1751,8 +1712,6 @@ defmodule String do
"""
@spec valid?(t) :: boolean
def valid?(string)
def valid?(<<string::binary>>), do: valid_utf8?(string)
def valid?(_), do: false
@@ -1849,17 +1808,10 @@ defmodule String do
["é"]
"""
@compile {:inline, graphemes: 1}
@spec graphemes(t) :: [grapheme]
def graphemes(string) when is_binary(string), do: do_graphemes(string)
defdelegate graphemes(string), to: String.Unicode
defp do_graphemes(gcs) do
case :unicode_util.gc(gcs) do
[gc | rest] -> [grapheme_to_binary(gc) | do_graphemes(rest)]
[] -> []
{:error, <<byte, rest::bits>>} -> [<<byte>> | do_graphemes(rest)]
end
end
@compile {:inline, next_grapheme: 1, next_grapheme_size: 1}
@doc """
Returns the next grapheme in a string.
@@ -1877,28 +1829,32 @@ defmodule String do
nil
"""
@compile {:inline, next_grapheme: 1}
@spec next_grapheme(t) :: {grapheme, t} | nil
def next_grapheme(string) when is_binary(string) do
case :unicode_util.gc(string) do
[gc] -> {grapheme_to_binary(gc), <<>>}
[gc | rest] -> {grapheme_to_binary(gc), rest}
[] -> nil
{:error, <<byte, rest::bits>>} -> {<<byte>>, rest}
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
end
end
@doc false
@deprecated "Use String.next_grapheme/1 instead"
@doc """
Returns the size (in bytes) of the next grapheme.
The result is a tuple with the next grapheme size in bytes and
the remainder of the string or `nil` in case the string
reached its end.
## Examples
iex> String.next_grapheme_size("olá")
{1, "lá"}
iex> String.next_grapheme_size("")
nil
"""
@spec next_grapheme_size(t) :: {pos_integer, t} | nil
def next_grapheme_size(string) when is_binary(string) do
case :unicode_util.gc(string) do
[gc] -> {grapheme_byte_size(gc), <<>>}
[gc | rest] -> {grapheme_byte_size(gc), rest}
[] -> nil
{:error, <<_, rest::bits>>} -> {1, rest}
end
end
defdelegate next_grapheme_size(string), to: String.Unicode
@doc """
Returns the first grapheme from a UTF-8 string,
@@ -1918,10 +1874,9 @@ defmodule String do
"""
@spec first(t) :: grapheme | nil
def first(string) when is_binary(string) do
case :unicode_util.gc(string) do
[gc | _] -> grapheme_to_binary(gc)
[] -> nil
{:error, <<byte, _::bits>>} -> <<byte>>
case next_grapheme(string) do
{char, _} -> char
nil -> nil
end
end
@@ -1929,13 +1884,8 @@ defmodule String do
Returns the last grapheme from a UTF-8 string,
`nil` if the string is empty.
It traverses the whole string to find its last grapheme.
## Examples
iex> String.last("")
nil
iex> String.last("elixir")
"r"
@@ -1944,13 +1894,15 @@ defmodule String do
"""
@spec last(t) :: grapheme | nil
def last(""), do: nil
def last(string) when is_binary(string), do: do_last(:unicode_util.gc(string), nil)
def last(string) when is_binary(string) do
do_last(next_grapheme(string), nil)
end
defp do_last([gc | rest], _), do: do_last(:unicode_util.gc(rest), gc)
defp do_last([], acc) when is_binary(acc), do: acc
defp do_last([], acc), do: :unicode.characters_to_binary([acc])
defp do_last({:error, <<byte, rest::bits>>}, _), do: do_last(:unicode_util.gc(rest), <<byte>>)
defp do_last({char, rest}, _) do
do_last(next_grapheme(rest), char)
end
defp do_last(nil, last_char), do: last_char
@doc """
Returns the number of Unicode graphemes in a UTF-8 string.
@@ -1965,15 +1917,7 @@ defmodule String do
"""
@spec length(t) :: non_neg_integer
def length(string) when is_binary(string), do: length(string, 0)
defp length(gcs, acc) do
case :unicode_util.gc(gcs) do
[_ | rest] -> length(rest, acc + 1)
[] -> acc
{:error, <<_, rest::bits>>} -> length(rest, acc + 1)
end
end
defdelegate length(string), to: String.Unicode
@doc """
Returns the grapheme at the `position` of the given UTF-8 `string`.
@@ -2013,11 +1957,10 @@ defmodule String do
end
defp do_at(string, position) do
left = byte_size_remaining_at(string, position)
string
|> binary_part(byte_size(string) - left, left)
|> first()
case String.Unicode.split_at(string, position) do
{_, nil} -> nil
{_, rest} -> first(rest)
end
end
@doc """
@@ -2065,7 +2008,14 @@ defmodule String 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
do_slice(string, start, length)
case String.Unicode.split_at(string, start) do
{_, nil} ->
""
{start_bytes, rest} ->
{len_bytes, _} = String.Unicode.split_at(rest, length)
binary_part(string, start_bytes, len_bytes)
end
end
def slice(string, start, length)
@@ -2074,19 +2024,11 @@ defmodule String do
start = length(string) + start
case start >= 0 do
true -> do_slice(string, start, length)
true -> slice(string, start, length)
false -> ""
end
end
defp do_slice(string, start, length) do
from_start = byte_size_remaining_at(string, start)
rest = binary_part(string, byte_size(string) - from_start, from_start)
from_end = byte_size_remaining_at(rest, length)
binary_part(rest, 0, from_start - from_end)
end
@doc """
Returns a substring from the offset given by the start of the
range to the offset given by the end of the range.
@@ -2116,7 +2058,7 @@ defmodule String do
iex> String.slice("elixir", -4..6)
"ixir"
For ranges where `start > stop`, you need to explicitly
For ranges where `start > stop`, you need to explicit
mark them as increasing:
iex> String.slice("elixir", 2..-1//1)
@@ -2157,8 +2099,7 @@ defmodule String do
end
# TODO: Remove me on v2.0
def slice(string, %{__struct__: Range, first: first, last: last} = range)
when is_binary(string) do
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
@@ -2166,8 +2107,10 @@ defmodule String do
defp slice_range("", _, _), do: ""
defp slice_range(string, first, -1) when first >= 0 do
left = byte_size_remaining_at(string, first)
binary_part(string, byte_size(string) - left, left)
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
@@ -2179,7 +2122,7 @@ defmodule String do
end
defp slice_range(string, first, last) do
{bytes, length} = acc_bytes(:unicode_util.gc(string), [], 0)
{bytes, length} = acc_bytes(next_grapheme_size(string), [], 0)
first = add_if_negative(first, length)
last = add_if_negative(last, length)
@@ -2194,14 +2137,13 @@ defmodule String do
end
end
defp acc_bytes([gc | rest], bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [grapheme_byte_size(gc) | bytes], length + 1)
defp acc_bytes({size, rest}, bytes, length) do
acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
end
defp acc_bytes([], bytes, length),
do: {bytes, length}
defp acc_bytes({:error, <<_, rest::bits>>}, bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [1 | bytes], length + 1)
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
@@ -2318,8 +2260,6 @@ defmodule String do
iex> String.match?("bar", ~r/foo/)
false
Elixir also provides `Kernel.=~/2` and `Regex.match?/2` as
alternatives to test strings against regular expressions.
"""
@spec match?(t, Regex.t()) :: boolean
def match?(string, regex) when is_binary(string) do
@@ -2568,17 +2508,18 @@ defmodule String do
end
defp string_to_bag(string, bag, length) do
case :unicode_util.gc(string) do
[gc | rest] -> string_to_bag(rest, bag_store(bag, gc), length + 1)
[] -> {bag, length}
{:error, <<byte, rest::bits>>} -> string_to_bag(rest, bag_store(bag, <<byte>>), length + 1)
end
end
case next_grapheme(string) do
{char, rest} ->
bag =
case bag do
%{^char => current} -> %{bag | char => current + 1}
%{} -> Map.put(bag, char, 1)
end
defp bag_store(bag, gc) do
case bag do
%{^gc => current} -> %{bag | gc => current + 1}
%{} -> Map.put(bag, gc, 1)
string_to_bag(rest, bag, length + 1)
nil ->
{bag, length}
end
end
@@ -2623,8 +2564,8 @@ defmodule String do
def jaro_distance("", _string), do: 0.0
def jaro_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
{chars1, len1} = graphemes_and_length(string1)
{chars2, len2} = graphemes_and_length(string2)
{chars1, len1} = chars_and_length(string1)
{chars2, len2} = chars_and_length(string2)
case match(chars1, len1, chars2, len2) do
{0, _trans} ->
@@ -2635,6 +2576,12 @@ defmodule String do
end
end
@compile {:inline, chars_and_length: 1}
defp chars_and_length(string) do
chars = graphemes(string)
{chars, Kernel.length(chars)}
end
defp match(chars1, len1, chars2, len2) do
if len1 < len2 do
match(chars1, chars2, div(len2, 2) - 1)
@@ -2713,66 +2660,4 @@ defmodule String do
@deprecated "Use String.to_charlist/1 instead"
@spec to_char_list(t) :: charlist
def to_char_list(string), do: String.to_charlist(string)
## Helpers
@compile {:inline,
codepoint_byte_size: 1,
grapheme_byte_size: 1,
grapheme_to_binary: 1,
graphemes_and_length: 1,
reverse_characters_to_binary: 1}
defp byte_size_unicode(binary) when is_binary(binary), do: byte_size(binary)
defp byte_size_unicode([head]), do: byte_size_unicode(head)
defp byte_size_unicode([head | tail]), do: byte_size_unicode(head) + byte_size_unicode(tail)
defp byte_size_remaining_at(unicode, 0) do
byte_size_unicode(unicode)
end
defp byte_size_remaining_at(unicode, n) do
case :unicode_util.gc(unicode) do
[_] -> 0
[_ | rest] -> byte_size_remaining_at(rest, n - 1)
[] -> 0
{:error, <<_, bin::bits>>} -> byte_size_remaining_at(bin, n - 1)
end
end
defp codepoint_byte_size(cp) when cp <= 0x007F, do: 1
defp codepoint_byte_size(cp) when cp <= 0x07FF, do: 2
defp codepoint_byte_size(cp) when cp <= 0xFFFF, do: 3
defp codepoint_byte_size(_), do: 4
defp grapheme_to_binary(cp) when is_integer(cp), do: <<cp::utf8>>
defp grapheme_to_binary(gc), do: :unicode.characters_to_binary(gc)
defp grapheme_byte_size(cp) when is_integer(cp), do: codepoint_byte_size(cp)
defp grapheme_byte_size(cps), do: grapheme_byte_size(cps, 0)
defp grapheme_byte_size([cp | cps], acc),
do: grapheme_byte_size(cps, acc + codepoint_byte_size(cp))
defp grapheme_byte_size([], acc),
do: acc
defp graphemes_and_length(string),
do: graphemes_and_length(string, [], 0)
defp graphemes_and_length(string, acc, length) do
case :unicode_util.gc(string) do
[gc | rest] ->
graphemes_and_length(rest, [gc | acc], length + 1)
[] ->
{:lists.reverse(acc), length}
{:error, <<byte, rest::bits>>} ->
graphemes_and_length(rest, [<<byte>> | acc], length + 1)
end
end
defp reverse_characters_to_binary(acc),
do: acc |> :lists.reverse() |> :unicode.characters_to_binary()
end
+1 -1
View File
@@ -284,7 +284,7 @@ defmodule Supervisor do
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of children that are automatically initialized, we manually
initialize the children by calling `Supervisor.init/2` inside its
initialized the children by calling `Supervisor.init/2` inside its
`c:init/1` callback.
`use Supervisor` also defines a `child_spec/1` function which allows
+7 -10
View File
@@ -606,6 +606,7 @@ defmodule System do
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
if function_exported?(:os, :env, 0) do
@@ -707,7 +708,8 @@ defmodule System do
For more information, see `:os.getpid/0`.
"""
@deprecated "Use System.pid/0 instead"
# TODO: deprecate permanently on v1.13
@doc deprecated: "Use System.pid/0 instead"
@spec get_pid() :: binary
def get_pid, do: IO.iodata_to_binary(:os.getpid())
@@ -855,13 +857,8 @@ defmodule System do
All applications are taken down smoothly, all code is unloaded, and all ports
are closed before the system terminates by calling `halt/1`.
`status` must be a non-negative integer or a binary.
* If an integer, the runtime system exits with the integer value which is
returned to the operating system.
* If a binary, an Erlang crash dump is produced with status as slogan, and
then the runtime system exits with status code 1.
`status` must be a non-negative integer value which is returned by the
runtime system to the operating system.
Note that on many platforms, only the status codes 0-255 are supported
by the operating system.
@@ -900,7 +897,7 @@ defmodule System do
iex> System.shell("echo hello")
{"hello\n", 0}
If you want to stream the output to Standard IO as it arrives:
If you want to stream the devices to IO as they come:
iex> System.shell("echo hello", into: IO.stream())
hello
@@ -972,7 +969,7 @@ defmodule System do
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
{"hello\n", 0}
If you want to stream the output to Standard IO as it arrives:
If you want to stream the devices to IO as they come:
iex> System.cmd("echo", ["hello"], into: IO.stream())
hello
+70 -212
View File
@@ -35,7 +35,7 @@ defmodule Task do
1. If you are using async tasks, you **must await** a reply
as they are *always* sent. If you are not expecting a reply,
consider using `Task.start_link/1` as detailed below.
consider using `Task.start_link/1` detailed below.
2. async tasks link the caller and the spawned process. This
means that, if the caller crashes, the task will crash
@@ -69,8 +69,8 @@ defmodule Task do
{Task.Supervisor, name: MyApp.TaskSupervisor}
], strategy: :one_for_one)
And now you can use async/await by passing the name of
the supervisor instead of the pid:
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
@@ -79,21 +79,22 @@ defmodule Task do
We encourage developers to rely on supervised tasks as much as
possible. Supervised tasks enable a huge variety of patterns
that allow you explicit control on how to handle the results,
which allows you explicit control on how to handle the results,
errors, and timeouts. Here is a summary:
* Using `Task.Supervisor.start_child/2` allows you to start a fire-and-forget
task that you don't care about its results or if it completes successfully or not.
* 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
* Using `Task.Supervisor.async/2` + `Task.await/2` allows you to execute
* 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.
the caller will also fail
* Using `Task.Supervisor.async_nolink/2` + `Task.yield/2` + `Task.shutdown/2`
* 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`.
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.
@@ -110,8 +111,8 @@ defmodule Task do
Task.Supervisor.async(supervisor, MyMod, :my_fun, [arg1, arg2, arg3])
Note that, when working with distributed tasks, one should use the
`Task.Supervisor.async/5` function that expects explicit module, function,
and arguments, instead of `Task.Supervisor.async/3` that works with anonymous
`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
@@ -155,8 +156,8 @@ defmodule Task do
], 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/1`
and `Task.start_link/1` are for fire-and-forget tasks, where you don't
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
@@ -192,7 +193,7 @@ defmodule Task do
For example, we recommend developers to always start tasks under a supervisor.
This provides more visibility and allows you to control how those tasks are
terminated when a node shuts down. That might look something like
`Task.Supervisor.start_child(MySupervisor, task_function)`. This means
`Task.Supervisor.start_child(MySupervisor, task_specification)`. This means
that, although your code is the one who invokes the task, the actual ancestor of
the task is the supervisor, as the supervisor is the one effectively starting it.
@@ -241,8 +242,8 @@ defmodule Task do
"""
@type t :: %__MODULE__{
pid: pid() | nil,
ref: reference(),
owner: pid()
ref: reference() | nil,
owner: pid() | nil
}
defguardp is_timeout(timeout)
@@ -390,13 +391,13 @@ defmodule Task do
Task.await(x) + y
As before, if `heavy_fun/0` fails, the whole computation will
fail, including the caller process. If you don't want the task
fail, including the parent process. If you don't want the task
to fail then you must change the `heavy_fun/0` code in the
same way you would achieve it if you didn't have the async call.
For example, to either return `{:ok, val} | :error` results or,
in more extreme cases, by using `try/rescue`. In other words,
an asynchronous task should be thought of as an extension of the
caller process rather than a mechanism to isolate it from all errors.
an asynchronous task should be thought of as an extension of a
process rather than a mechanism to isolate it from all errors.
If you don't want to link the caller to the task, then you
must use a supervised task with `Task.Supervisor` and call
@@ -416,7 +417,7 @@ defmodule Task do
* Unlinking the task process started with `async`/`await`.
If you unlink the processes and the task does not belong
to any supervisor, you may leave dangling tasks in case
the caller process dies.
the parent dies.
"""
@spec async((() -> any)) :: t
@@ -424,9 +425,6 @@ defmodule Task do
async(:erlang, :apply, [fun, []])
end
# TODO: Remove conditional on Erlang/OTP 24
@compile {:no_warn_undefined, {:erlang, :monitor, 3}}
@doc """
Starts a task that must be awaited on.
@@ -438,72 +436,21 @@ defmodule Task do
when is_atom(module) and is_atom(function_name) and is_list(args) do
mfa = {module, function_name, args}
owner = self()
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), :nomonitor)
{reply_to, ref} =
if function_exported?(:erlang, :monitor, 3) do
ref = :erlang.monitor(:process, pid, alias: :demonitor)
{ref, ref}
else
{owner, Process.monitor(pid)}
end
send(pid, {owner, ref, reply_to, get_callers(owner), mfa})
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), get_callers(owner), :nomonitor, mfa)
ref = Process.monitor(pid)
send(pid, {owner, ref})
%Task{pid: pid, ref: ref, owner: owner}
end
@doc """
Starts a task that immediately completes with the given `result`.
Unlike `async/1`, this task does not spawn a linked process. It can
be awaited or yielded like any other task.
## Usage
In some cases, it is useful to create a "completed" task that represents
a task that has already run and generated a result. For example, when
processing data you may be able to determine that certain inputs are
invalid before dispatching them for further processing:
def process(data) do
tasks =
for entry <- data do
if invalid_input?(entry) do
Task.completed({:error, :invalid_input})
else
Task.async(fn -> further_process(entry) end)
end
end
Task.await_many(tasks)
end
In many cases, `Task.completed/1` may be avoided in favor of returning the
result directly. You should generally only require this variant when working
with mixed asynchrony, when a group of inputs will be handled partially
synchronously and partially asynchronously.
"""
@doc since: "1.13.0"
@spec completed(any) :: t
def completed(result) do
ref = make_ref()
owner = self()
# "complete" the task immediately
send(owner, {ref, result})
%Task{pid: nil, ref: ref, owner: owner}
end
@doc """
Returns a stream where the given function (`module` and `function_name`)
is mapped concurrently on each element in `enumerable`.
Each element of `enumerable` will be prepended to the given `args` and
processed by its own task. Those tasks will be linked to an intermediate
process that is then linked to the caller process. This means a failure
in a task terminates the caller process and a failure in the caller
process terminates all tasks.
processed by its own task. The tasks will be linked to an intermediate
process that is then linked to the current process. This means a failure
in a task terminates the current process and a failure in the current process
terminates all tasks.
When streamed, each task will emit `{:ok, value}` upon successful
completion or `{:exit, reason}` if the caller is trapping exits.
@@ -513,10 +460,9 @@ defmodule Task do
be controlled via options (see the "Options" section below).
Consider using `Task.Supervisor.async_stream/6` to start tasks
under a supervisor. If you find yourself trapping exits to ensure
errors in the tasks do not terminate the caller process, consider
using `Task.Supervisor.async_stream_nolink/6` to start tasks that
are not linked to the caller process.
under a supervisor. If you find yourself trapping exits to handle exits
inside the async stream, consider using `Task.Supervisor.async_stream_nolink/6`
to start tasks that are not linked to the calling process.
## Options
@@ -537,7 +483,7 @@ defmodule Task do
* `:on_timeout` - what to do when a task times out. The possible
values are:
* `:exit` (default) - the caller (the process that spawned the tasks) exits.
* `:exit` (default) - the process that spawned the tasks exits.
* `:kill_task` - the task that timed out is killed. The value
emitted for that task is `{:exit, :timeout}`.
@@ -562,32 +508,11 @@ defmodule Task do
stream = Task.async_stream(collection, Mod, :expensive_fun, [], ordered: false)
Stream.run(stream)
## First async tasks to complete
## Attention: async + take
You can also use `async_stream/3` to execute M tasks and find the N tasks
to complete. For example:
[
&heavy_call_1/0,
&heavy_call_2/0,
&heavy_call_3/0
]
|> Task.async_stream(fn fun -> fun.() end, ordered: false, max_concurrency: 3)
|> Stream.filter(&match?({:ok, _}, &1))
|> Enum.take(2)
In the example above, we are executing three tasks and waiting for the
first 2 to complete. We use `Stream.filter/2` to restrict ourselves only
to successfully completed tasks, and then use `Enum.take/2` to retrieve
N items. Note it is important to set both `ordered: false` and
`max_concurrency: M`, where M is the number of tasks, to make sure all
calls execute concurrently.
### Attention: unbound async + take
If you want to potentially process a high number of items and keep only
part of the results, you may end-up processing more items than desired.
Let's see an example:
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 ->
@@ -596,15 +521,13 @@ defmodule Task do
end)
|> Enum.take(10)
Running the example above in a machine with 8 cores will process 16 items,
even though you want only 10 elements, since `async_stream/3` process items
concurrently. That's because it will process 8 elements at once. Then all 8
elements complete at roughly the same time, causing 8 elements to be kicked
off for processing. Out of these extra 8, only 2 will be used, and the rest
will be terminated.
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.
Depending on the problem, you can filter or limit the number of elements
upfront:
The solution here is to use `Stream.take/2` instead of `Enum.take/2`
to filter elements before-hand:
1..100
|> Stream.take(10)
@@ -614,11 +537,9 @@ defmodule Task do
end)
|> Enum.to_list()
In other cases, you likely want to tweak `:max_concurrency` to limit how
many elements may be over processed at the cost of reducing concurrency.
You can also set the number of elements to take to be a multiple of
`:max_concurrency`. For instance, setting `max_concurrency: 5` in the
example above.
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()
@@ -635,8 +556,8 @@ defmodule Task do
module-function-arguments tuple. `fun` must be a one-arity anonymous function.
Each `enumerable` element is passed as argument to the given function `fun` and
processed by its own task. The tasks will be linked to the caller process, similarly
to `async/1`.
processed by its own task. The tasks will be linked to the current process,
similarly to `async/1`.
## Example
@@ -657,14 +578,10 @@ defmodule Task do
end
defp build_stream(enumerable, fun, options) do
fn acc, acc_fun ->
owner = get_owner(self())
Task.Supervised.stream(enumerable, acc, acc_fun, get_callers(self()), fun, options, fn ->
{:ok, pid} = Task.Supervised.start_link(owner, :nomonitor)
{:ok, :link, pid}
end)
end
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn [owner | _] = callers, mfa ->
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), callers, :nomonitor, mfa)
{:ok, :link, pid}
end)
end
# Returns a tuple with the node where this is executed and either the
@@ -690,14 +607,14 @@ defmodule Task do
@doc ~S"""
Awaits a task reply and returns it.
In case the task process dies, the caller process will exit with the same
In case the task process dies, the current process will exit with the same
reason as the task.
A timeout, in milliseconds or `:infinity`, can be given with a default value
of `5000`. If the timeout is exceeded, then the caller process will exit.
If the task process is linked to the caller process which is the case when
a task is started with `async`, then the task process will also exit. If the
task process is trapping exits or not linked to the caller process, then it
of `5000`. If the timeout is exceeded, then the current process will exit. If
the task process is linked to the current process which is the case when a
task is started with `async`, then the task process will also exit. If the
task process is trapping exits or not linked to the current process, then it
will continue to run.
This function assumes the task's monitor is still active or the monitor's
@@ -810,50 +727,6 @@ defmodule Task do
end
end
@doc """
Ignores an existing task.
This means the task will continue running, but it will be unlinked
and you can no longer yield, await or shut it down.
Returns `{:ok, reply}` if the reply is received before ignoring the task,
`{:exit, reason}` if the task died before ignoring it, otherwise `nil`.
Important: avoid using `Task.async` and then immediately ignoring
the task. If you want to start tasks you don't care about their
results, use `Task.Supervisor.start_child/2` instead.
Requires Erlang/OTP 24+.
"""
@doc since: "1.13.0"
def ignore(%Task{ref: ref, pid: pid, owner: owner} = task) do
unless function_exported?(:erlang, :monitor, 3) do
raise "Task.ignore/1 requires Erlang/OTP 24+"
end
if owner != self() do
raise ArgumentError, invalid_owner_error(task)
end
receive do
{^ref, reply} ->
Process.unlink(pid)
Process.demonitor(ref, [:flush])
{:ok, reply}
{:DOWN, ^ref, _, proc, :noconnection} ->
exit({reason(:noconnection, proc), {__MODULE__, :ignore, [task]}})
{:DOWN, ^ref, _, _, reason} ->
{:exit, reason}
after
0 ->
Process.unlink(pid)
Process.demonitor(ref, [:flush])
nil
end
end
@doc """
Awaits replies from multiple tasks and returns them.
@@ -861,16 +734,16 @@ defmodule Task do
given time interval. It returns a list of the results, in the same order as
the tasks supplied in the `tasks` input argument.
If any of the task processes dies, the caller process will exit with the same
reason as that task.
If any of the task processes dies, the current process will exit with the
same reason as that task.
A timeout, in milliseconds or `:infinity`, can be given with a default value
of `5000`. If the timeout is exceeded, then the caller process will exit.
Any task processes that are linked to the caller process (which is the case
of `5000`. If the timeout is exceeded, then the current process will exit.
Any task processes that are linked to the current process (which is the case
when a task is started with `async`) will also exit. Any task processes that
are trapping exits or not linked to the caller process will continue to run.
are trapping exits or not linked to the current process will continue to run.
This function assumes the tasks' monitors are still active or the monitor's
This function assumes the tasks' monitors are still active or the monitors'
`:DOWN` message is in the message queue. If any tasks have been demonitored,
or the message already received, this function will wait for the duration of
the timeout.
@@ -983,17 +856,16 @@ defmodule Task do
end
@doc ~S"""
Temporarily blocks the caller process waiting for a task reply.
Temporarily blocks the current process waiting for a task reply.
Returns `{:ok, reply}` if the reply is received, `nil` if
no reply has arrived, or `{:exit, reason}` if the task has already
exited. Keep in mind that normally a task failure also causes
the process owning the task to exit. Therefore this function can
return `{:exit, reason}` if at least one of the conditions below apply:
return `{:exit, reason}` only if
* the task process exited with the reason `:normal`
* the task isn't linked to the caller (the task was started
with `Task.Supervisor.async_nolink/2` or `Task.Supervisor.async_nolink/4`)
* it isn't linked to the caller
* the caller is trapping exits
A timeout, in milliseconds or `:infinity`, can be given with a default value
@@ -1018,18 +890,6 @@ defmodule Task do
nil
end
If you intend to check on the task but leave it running after the timeout,
you can chain this together with `ignore/1`, like so:
case Task.yield(task, timeout) || Task.ignore(task) do
{:ok, result} ->
result
nil ->
Logger.warn("Failed to get a result in #{timeout}ms")
nil
end
That ensures that if the task completes after the `timeout` but before `shutdown/1`
has been called, you will still get the result, since `shutdown/1` is designed to
handle this case and return the result.
@@ -1082,9 +942,8 @@ defmodule Task do
`Task.yield_many/2` allows developers to spawn multiple tasks
and retrieve the results received in a given timeframe.
If we combine it with `Task.shutdown/2` (or `Task.ignore/1`),
it allows us to gather those results and cancel (or ignore)
the tasks that have not replied in time.
If we combine it with `Task.shutdown/2`, it allows us to gather
those results and cancel the tasks that have not replied in time.
Let's see an example.
@@ -1169,8 +1028,7 @@ defmodule Task do
Unlinks and shuts down the task, and then checks for a reply.
Returns `{:ok, reply}` if the reply is received while shutting down the task,
`{:exit, reason}` if the task died, otherwise `nil`. Once shut down,
you can no longer await or yield it.
`{:exit, reason}` if the task died, otherwise `nil`.
The second argument is either a timeout or `:brutal_kill`. In case
of a timeout, a `:shutdown` exit signal is sent to the task process
+64 -45
View File
@@ -10,28 +10,29 @@ defmodule Task.Supervised do
{:ok, :proc_lib.spawn_link(__MODULE__, :noreply, [owner, callers, fun])}
end
def start_link(owner, monitor) do
{:ok, :proc_lib.spawn_link(__MODULE__, :reply, [owner, monitor])}
def start_link(owner, callers, monitor, fun) do
{:ok, :proc_lib.spawn_link(__MODULE__, :reply, [owner, callers, monitor, fun])}
end
def reply({_, _, owner_pid} = owner, monitor) do
def reply({_, _, owner_pid} = owner, callers, monitor, mfa) do
initial_call(mfa)
put_callers(callers)
case monitor do
:monitor ->
mref = Process.monitor(owner_pid)
reply(owner, owner_pid, mref, @ref_timeout)
reply(owner, owner_pid, mref, @ref_timeout, mfa)
:nomonitor ->
reply(owner, owner_pid, nil, :infinity)
reply(owner, owner_pid, nil, :infinity, mfa)
end
end
defp reply(owner, owner_pid, mref, timeout) do
defp reply(owner, owner_pid, mref, timeout, mfa) do
receive do
{^owner_pid, ref, reply_to, callers, mfa} ->
initial_call(mfa)
put_callers(callers)
_ = mref && Process.demonitor(mref, [:flush])
send(reply_to, {ref, invoke_mfa(owner, mfa)})
{^owner_pid, ref} ->
_ = if mref, do: Process.demonitor(mref, [:flush])
send(owner_pid, {ref, invoke_mfa(owner, mfa)})
{:DOWN, ^mref, _, _, reason} ->
exit({:shutdown, reason})
@@ -173,7 +174,7 @@ defmodule Task.Supervised do
## Stream
def stream(enumerable, acc, reducer, callers, mfa, options, spawn) 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())
@@ -185,6 +186,7 @@ defmodule Task.Supervised do
timeout = Keyword.get(options, :timeout, 5000)
on_timeout = Keyword.get(options, :on_timeout, :exit)
parent = self()
callers = get_callers()
{:trap_exit, trap_exit?} = Process.info(self(), :trap_exit)
@@ -195,7 +197,7 @@ defmodule Task.Supervised do
{monitor_pid, monitor_ref} =
Process.spawn(
fn -> stream_monitor(parent, spawn, trap_exit?, timeout) end,
fn -> stream_monitor(callers, mfa, spawn, trap_exit?, timeout) end,
spawn_opts
)
@@ -210,9 +212,7 @@ defmodule Task.Supervised do
monitor_ref: monitor_ref,
ordered: ordered?,
timeout: timeout,
on_timeout: on_timeout,
callers: callers,
mfa: mfa
on_timeout: on_timeout
}
stream_reduce(
@@ -226,8 +226,16 @@ defmodule Task.Supervised do
)
end
defp get_callers do
case :erlang.get(:"$callers") do
[_ | _] = list -> [self() | list]
_ -> [self()]
end
end
defp stream_reduce({:halt, acc}, _max, _spawned, _delivered, _waiting, next, config) do
stream_close(config)
%{monitor_pid: monitor_pid, monitor_ref: monitor_ref, timeout: timeout} = config
stream_close(monitor_pid, monitor_ref, timeout)
is_function(next) && next.({:halt, []})
{:halted, acc}
end
@@ -240,7 +248,13 @@ defmodule Task.Supervised do
# All spawned, all delivered, next is :done.
defp stream_reduce({:cont, acc}, _max, spawned, delivered, _waiting, next, config)
when spawned == delivered and next == :done do
stream_close(config)
%{
monitor_pid: monitor_pid,
monitor_ref: monitor_ref,
timeout: timeout
} = config
stream_close(monitor_pid, monitor_ref, timeout)
{:done, acc}
end
@@ -309,26 +323,28 @@ defmodule Task.Supervised do
# The monitor process died. We just cleanup the messages from the monitor
# process and exit.
{:DOWN, ^monitor_ref, _, _, reason} ->
{:DOWN, ^monitor_ref, _, ^monitor_pid, reason} ->
stream_cleanup_inbox(monitor_pid, monitor_ref)
exit({reason, {__MODULE__, :stream, [timeout]}})
end
end
defp stream_reduce({:cont, acc}, max, spawned, delivered, waiting, next, config) do
%{monitor_pid: monitor_pid, monitor_ref: monitor_ref, timeout: timeout} = config
try do
next.({:cont, []})
catch
kind, reason ->
stream_close(config)
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, [value], next} ->
waiting = stream_spawn(value, spawned, waiting, config)
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
stream_reduce({:cont, acc}, max - 1, spawned + 1, delivered, waiting, next, config)
{_, [value]} ->
waiting = stream_spawn(value, spawned, waiting, config)
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
stream_reduce({:cont, acc}, max - 1, spawned + 1, delivered, waiting, :done, config)
{_, []} ->
@@ -337,14 +353,19 @@ defmodule Task.Supervised do
end
defp deliver_now(reply, acc, next, config) do
%{reducer: reducer} = config
%{
reducer: reducer,
monitor_pid: monitor_pid,
monitor_ref: monitor_ref,
timeout: timeout
} = config
try do
reducer.(reply, acc)
catch
kind, reason ->
is_function(next) && next.({:halt, []})
stream_close(config)
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
end
end
@@ -359,7 +380,12 @@ defmodule Task.Supervised do
end
defp stream_deliver({:cont, acc}, max, spawned, delivered, waiting, next, config) do
%{reducer: reducer} = config
%{
reducer: reducer,
monitor_pid: monitor_pid,
monitor_ref: monitor_ref,
timeout: timeout
} = config
case waiting do
%{^delivered => {:done, reply}} ->
@@ -368,7 +394,7 @@ defmodule Task.Supervised do
catch
kind, reason ->
is_function(next) && next.({:halt, []})
stream_close(config)
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
else
pair ->
@@ -381,7 +407,7 @@ defmodule Task.Supervised do
end
end
defp stream_close(%{monitor_pid: monitor_pid, monitor_ref: monitor_ref, timeout: timeout}) do
defp stream_close(monitor_pid, monitor_ref, timeout) do
send(monitor_pid, {:stop, monitor_ref})
receive do
@@ -418,25 +444,16 @@ defmodule Task.Supervised do
# This function spawns a task for the given "value", and puts the pid of this
# new task in the map of "waiting" tasks, which is returned.
defp stream_spawn(value, spawned, waiting, config) do
%{
monitor_pid: monitor_pid,
monitor_ref: monitor_ref,
timeout: timeout,
callers: callers,
mfa: mfa
} = config
send(monitor_pid, {:spawn, spawned})
defp stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout) do
send(monitor_pid, {:spawn, spawned, value})
receive do
{:spawned, {^monitor_ref, ^spawned}, pid} ->
mfa_with_value = normalize_mfa_with_arg(mfa, value)
send(pid, {self(), {monitor_ref, spawned}, self(), callers, mfa_with_value})
send(pid, {self(), {monitor_ref, spawned}})
Map.put(waiting, spawned, {pid, :running})
{:max_children, ^monitor_ref} ->
stream_close(config)
stream_close(monitor_pid, monitor_ref, timeout)
raise """
reached the maximum number of tasks for this task supervisor. The maximum number \
@@ -453,7 +470,7 @@ defmodule Task.Supervised do
end
end
defp stream_monitor(parent_pid, spawn, trap_exit?, timeout) do
defp stream_monitor([parent_pid | _] = callers, mfa, spawn, trap_exit?, timeout) do
Process.flag(:trap_exit, trap_exit?)
parent_ref = Process.monitor(parent_pid)
@@ -463,8 +480,9 @@ defmodule Task.Supervised do
receive do
{^parent_pid, monitor_ref} ->
config = %{
parent_pid: parent_pid,
callers: callers,
parent_ref: parent_ref,
mfa: mfa,
spawn: spawn,
monitor_ref: monitor_ref,
timeout: timeout
@@ -479,8 +497,9 @@ defmodule Task.Supervised do
defp stream_monitor_loop(running_tasks, config) do
%{
callers: [parent_pid | _] = callers,
mfa: mfa,
spawn: spawn,
parent_pid: parent_pid,
monitor_ref: monitor_ref,
timeout: timeout
} = config
@@ -488,8 +507,8 @@ defmodule Task.Supervised do
receive do
# The parent process is telling us to spawn a new task to process
# "value". We spawn it and notify the parent about its pid.
{:spawn, position} ->
case spawn.() do
{:spawn, position, value} ->
case spawn.(callers, normalize_mfa_with_arg(mfa, value)) do
{:ok, type, pid} ->
ref = Process.monitor(pid)
+27 -41
View File
@@ -74,7 +74,7 @@ defmodule Task.Supervisor do
This function could also receive `:restart` and `:shutdown` as options
but those two options have been deprecated and it is now preferred to
give them directly to `start_child`.
give them directly to `start_child` and `async`.
"""
@spec start_link([option]) :: Supervisor.on_start()
def start_link(options \\ []) do
@@ -105,7 +105,7 @@ defmodule Task.Supervisor do
The `supervisor` must be a reference as defined in `Supervisor`.
The task will still be linked to the caller, see `Task.async/3` for
more information and `async_nolink/3` for a non-linked variant.
more information and `async_nolink/2` for a non-linked variant.
Raises an error if `supervisor` has reached the maximum number of
children.
@@ -126,7 +126,7 @@ defmodule Task.Supervisor do
The `supervisor` must be a reference as defined in `Supervisor`.
The task will still be linked to the caller, see `Task.async/3` for
more information and `async_nolink/3` for a non-linked variant.
more information and `async_nolink/2` for a non-linked variant.
Raises an error if `supervisor` has reached the maximum number of
children.
@@ -235,7 +235,7 @@ defmodule Task.Supervisor do
children.
Note this function requires the task supervisor to have `:temporary`
as the `:restart` option (the default), as `async_nolink/5` keeps a
as the `:restart` option (the default), as `async_nolink/4` keeps a
direct reference to the task which is lost if the task is restarted.
"""
@spec async_nolink(Supervisor.supervisor(), module, atom, [term], Keyword.t()) :: Task.t()
@@ -249,7 +249,7 @@ defmodule Task.Supervisor do
Each element will be prepended to the given `args` and processed by its
own task. The tasks will be spawned under the given `supervisor` and
linked to the caller process, similarly to `async/5`.
linked to the current process, similarly to `async/4`.
When streamed, each task will emit `{:ok, value}` upon successful
completion or `{:exit, reason}` if the caller is trapping exits.
@@ -308,7 +308,7 @@ defmodule Task.Supervisor do
Each element in `enumerable` is passed as argument to the given function `fun`
and processed by its own task. The tasks will be spawned under the given
`supervisor` and linked to the caller process, similarly to `async/3`.
`supervisor` and linked to the current process, similarly to `async/2`.
See `async_stream/6` for discussion, options, and examples.
"""
@@ -325,7 +325,7 @@ defmodule Task.Supervisor do
Each element in `enumerable` will be prepended to the given `args` and processed
by its own task. The tasks will be spawned under the given `supervisor` and
will not be linked to the caller process, similarly to `async_nolink/5`.
will not be linked to the current process, similarly to `async_nolink/4`.
See `async_stream/6` for discussion, options, and examples.
"""
@@ -349,8 +349,7 @@ defmodule Task.Supervisor do
Each element in `enumerable` is passed as argument to the given function `fun`
and processed by its own task. The tasks will be spawned under the given
`supervisor` and will not be linked to the caller process, similarly
to `async_nolink/3`.
`supervisor` and will not be linked to the current process, similarly to `async_nolink/2`.
See `async_stream/6` for discussion and examples.
"""
@@ -387,7 +386,7 @@ defmodule Task.Supervisor do
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 you have no
interest in its results nor if it completes successfully.
interest on its results nor if it completes successfully.
## Options
@@ -395,9 +394,9 @@ defmodule Task.Supervisor do
`:transient` or `:permanent`. `:temporary` means the task is never
restarted, `:transient` means it is restarted if the exit is not
`:normal`, `:shutdown` or `{:shutdown, reason}`. A `:permanent` restart
strategy means it is always restarted.
strategy means it is always restarted. It defaults to `:temporary`.
* `:shutdown` - `:brutal_kill` if the task must be killed directly on shutdown
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
or an integer indicating the timeout value, defaults to 5000 milliseconds.
"""
@@ -413,7 +412,7 @@ defmodule Task.Supervisor do
@doc """
Starts a task as a child of the given `supervisor`.
Similar to `start_child/3` except the task is specified
Similar to `start_child/2` except the task is specified
by the given `module`, `fun` and `args`.
"""
@spec start_child(Supervisor.supervisor(), module, atom, [term], keyword) ::
@@ -452,26 +451,16 @@ defmodule Task.Supervisor do
end
end
# TODO: Remove conditional on Erlang/OTP 24
@compile {:no_warn_undefined, {:erlang, :monitor, 3}}
defp async(supervisor, link_type, module, fun, args, options) do
owner = self()
args = [get_owner(owner), get_callers(owner), :monitor, {module, fun, args}]
shutdown = options[:shutdown]
case start_child_with_spec(supervisor, [get_owner(owner), :monitor], :temporary, shutdown) do
case start_child_with_spec(supervisor, args, :temporary, shutdown) do
{:ok, pid} ->
if link_type == :link, do: Process.link(pid)
{reply_to, ref} =
if function_exported?(:erlang, :monitor, 3) do
ref = :erlang.monitor(:process, pid, alias: :demonitor)
{ref, ref}
else
{owner, Process.monitor(pid)}
end
send(pid, {owner, ref, reply_to, get_callers(owner), {module, fun, args}})
ref = Process.monitor(pid)
send(pid, {owner, ref})
%Task{pid: pid, ref: ref, owner: owner}
{:error, :max_children} ->
@@ -484,22 +473,19 @@ defmodule Task.Supervisor do
end
defp build_stream(supervisor, link_type, enumerable, fun, options) do
fn acc, acc_fun ->
shutdown = options[:shutdown]
owner = get_owner(self())
shutdown = options[:shutdown]
Task.Supervised.stream(enumerable, acc, acc_fun, get_callers(self()), fun, options, fn ->
args = [owner, :monitor]
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn [owner | _] = callers, mfa ->
args = [get_owner(owner), callers, :monitor, mfa]
case start_child_with_spec(supervisor, args, :temporary, shutdown) do
{:ok, pid} ->
if link_type == :link, do: Process.link(pid)
{:ok, link_type, pid}
case start_child_with_spec(supervisor, args, :temporary, shutdown) do
{:ok, pid} ->
if link_type == :link, do: Process.link(pid)
{:ok, link_type, pid}
{:error, :max_children} ->
{:error, :max_children}
end
end)
end
{:error, :max_children} ->
{:error, :max_children}
end
end)
end
end
+47 -266
View File
@@ -5,10 +5,6 @@ defmodule URI do
This module provides functions for working with URIs (for example, parsing
URIs or encoding query strings). The functions in this module are implemented
according to [RFC 3986](https://tools.ietf.org/html/rfc3986).
URIs are structs behind the scenes. You can access the URI fields directly
but you should not create a new `URI` directly via the struct syntax. Instead
use the functions in this module.
"""
defstruct scheme: nil,
@@ -21,28 +17,16 @@ defmodule URI do
port: nil
@type t :: %__MODULE__{
authority: authority,
fragment: nil | binary,
host: nil | binary,
path: nil | binary,
port: nil | :inet.port_number(),
query: nil | binary,
scheme: nil | binary,
userinfo: nil | binary
path: nil | binary,
query: nil | binary,
fragment: nil | binary,
authority: nil | binary,
userinfo: nil | binary,
host: nil | binary,
port: nil | :inet.port_number()
}
@typedoc deprecated: "The authority field is deprecated"
@opaque authority :: nil | binary
defmodule Error do
defexception [:action, :reason, :part]
@doc false
def message(%Error{action: action, reason: reason, part: part}) do
"cannot #{action} due to reason #{reason}: #{inspect(part)}"
end
end
import Bitwise
@reserved_characters ':/?#[]@!$&\'()*+,;='
@@ -430,6 +414,9 @@ defmodule URI do
@spec decode(binary) :: binary
def decode(uri) do
unpercent(uri, "", false)
catch
:malformed_uri ->
raise ArgumentError, "malformed URI #{inspect(uri)}"
end
@doc """
@@ -448,238 +435,47 @@ defmodule URI do
@spec decode_www_form(binary) :: binary
def decode_www_form(string) when is_binary(string) do
unpercent(string, "", true)
catch
:malformed_uri ->
raise ArgumentError, "malformed URI #{inspect(string)}"
end
defp unpercent(<<?+, tail::binary>>, acc, spaces = true) do
unpercent(tail, <<acc::binary, ?\s>>, spaces)
end
defp unpercent(<<?%, tail::binary>>, acc, spaces) do
with <<hex1, hex2, tail::binary>> <- tail,
dec1 when is_integer(dec1) <- hex_to_dec(hex1),
dec2 when is_integer(dec2) <- hex_to_dec(hex2) do
unpercent(tail, <<acc::binary, bsl(dec1, 4) + dec2>>, spaces)
else
_ -> unpercent(tail, <<acc::binary, ?%>>, spaces)
end
defp unpercent(<<?%, hex1, hex2, tail::binary>>, acc, spaces) do
unpercent(tail, <<acc::binary, bsl(hex_to_dec(hex1), 4) + hex_to_dec(hex2)>>, spaces)
end
defp unpercent(<<?%, _::binary>>, _acc, _spaces), do: throw(:malformed_uri)
defp unpercent(<<head, tail::binary>>, acc, spaces) do
unpercent(tail, <<acc::binary, head>>, spaces)
end
defp unpercent(<<>>, acc, _spaces), do: acc
@compile {:inline, hex_to_dec: 1}
defp hex_to_dec(n) when n in ?A..?F, do: n - ?A + 10
defp hex_to_dec(n) when n in ?a..?f, do: n - ?a + 10
defp hex_to_dec(n) when n in ?0..?9, do: n - ?0
defp hex_to_dec(_n), do: nil
defp hex_to_dec(_n), do: throw(:malformed_uri)
@doc """
Creates a new URI struct from a URI or a string.
If a `%URI{}` struct is given, it returns `{:ok, uri}`. If a string is
given, it will parse and validate it. If the string is valid, it returns
`{:ok, uri}`, otherwise it returns `{:error, part}` with the invalid part
of the URI. For parsing URIs without further validation, see `parse/1`.
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.
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. The scheme is also
normalized to lowercase.
## Examples
iex> URI.new("https://elixir-lang.org/")
{:ok, %URI{
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: 443,
query: nil,
scheme: "https",
userinfo: nil
}}
iex> URI.new("//elixir-lang.org/")
{:ok, %URI{
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}}
iex> URI.new("/foo/bar")
{:ok, %URI{
fragment: nil,
host: nil,
path: "/foo/bar",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}}
iex> URI.new("foo/bar")
{:ok, %URI{
fragment: nil,
host: nil,
path: "foo/bar",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}}
iex> URI.new("//[fe80::]/")
{:ok, %URI{
fragment: nil,
host: "fe80::",
path: "/",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}}
iex> URI.new("https:?query")
{:ok, %URI{
fragment: nil,
host: nil,
path: nil,
port: 443,
query: "query",
scheme: "https",
userinfo: nil
}}
iex> URI.new("/invalid_greater_than_in_path/>")
{:error, ">"}
Giving an existing URI simply returns it wrapped in a tuple:
iex> {:ok, uri} = URI.new("https://elixir-lang.org/")
iex> URI.new(uri)
{:ok, %URI{
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: 443,
query: nil,
scheme: "https",
userinfo: nil
}}
"""
@doc since: "1.13.0"
@spec new(t() | String.t()) :: {:ok, t()} | {:error, String.t()}
def new(%URI{} = uri), do: {:ok, uri}
def new(binary) when is_binary(binary) do
case :uri_string.parse(binary) do
%{} = map -> {:ok, uri_from_map(map)}
{:error, :invalid_uri, term} -> {:error, Kernel.to_string(term)}
end
end
@doc """
Similar to `new/0` but raises `URI.Error` if an invalid string is given.
## Examples
iex> URI.new!("https://elixir-lang.org/")
%URI{
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: 443,
query: nil,
scheme: "https",
userinfo: nil
}
iex> URI.new!("/invalid_greater_than_in_path/>")
** (URI.Error) cannot parse due to reason invalid_uri: ">"
Giving an existing URI simply returns it:
iex> uri = URI.new!("https://elixir-lang.org/")
iex> URI.new!(uri)
%URI{
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: 443,
query: nil,
scheme: "https",
userinfo: nil
}
"""
@doc since: "1.13.0"
@spec new!(t() | String.t()) :: t()
def new!(%URI{} = uri), do: uri
def new!(binary) when is_binary(binary) do
case :uri_string.parse(binary) do
%{} = map ->
uri_from_map(map)
{:error, reason, part} ->
raise Error, action: :parse, reason: reason, part: Kernel.to_string(part)
end
end
defp uri_from_map(%{path: ""} = map), do: uri_from_map(%{map | path: nil})
defp uri_from_map(map) do
uri = Map.merge(%URI{}, map)
case map do
%{scheme: scheme} ->
scheme = String.downcase(scheme, :ascii)
case map do
%{port: _} ->
%{uri | scheme: scheme}
%{} ->
case default_port(scheme) do
nil -> %{uri | scheme: scheme}
port -> %{uri | scheme: scheme, port: port}
end
end
%{} ->
uri
end
end
@doc """
Parses a URI into its components, without further validation.
Parses a well-formed URI into its components.
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. Use `new/1` if you want more strict
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. The scheme is also
normalized to lowercase.
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.
If a `%URI{}` struct is given to this function, this function returns it
unmodified.
> Note: this function sets the field :authority for backwards
> compatibility reasons but it is deprecated.
## Examples
iex> URI.parse("https://elixir-lang.org/")
@@ -730,36 +526,6 @@ defmodule URI do
userinfo: nil
}
In contrast to `URI.new/1`, this function will parse poorly-formed
URIs, for example:
iex> URI.parse("/invalid_greater_than_in_path/>")
%URI{
authority: nil,
fragment: nil,
host: nil,
path: "/invalid_greater_than_in_path/>",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}
Another example is a URI with brackets in query strings. It is accepted
by `parse/1` but it will be refused by `new/1`:
iex> URI.parse("/?foo[bar]=baz")
%URI{
authority: nil,
fragment: nil,
host: nil,
path: "/",
port: nil,
query: "foo[bar]=baz",
scheme: nil,
userinfo: nil
}
"""
@spec parse(t | binary) :: t
def parse(%URI{} = uri), do: uri
@@ -794,8 +560,8 @@ defmodule URI do
],
parts
path = nillify(path)
scheme = nillify(scheme)
path = nillify(path)
query = nillify_query(query_with_question_mark)
{authority, userinfo, host, port} = split_authority(authority_with_slashes)
@@ -818,9 +584,6 @@ defmodule URI do
defp nillify_query(_other), do: nil
# Split an authority into its userinfo, host and port parts.
#
# Note that the host field is returned *without* [] even if, according to
# RFC3986 grammar, a native IPv6 address requires them.
defp split_authority("") do
{nil, nil, nil, nil}
end
@@ -859,6 +622,24 @@ defmodule URI do
iex> URI.to_string(uri)
"foo://bar.baz"
Note that when creating this string representation, the `:authority` value will be
used if the `:host` is `nil`. Otherwise, the `:userinfo`, `:host`, and `:port` will
be used.
iex> URI.to_string(%URI{authority: "foo@example.com:80"})
"//foo@example.com:80"
iex> URI.to_string(%URI{userinfo: "bar", host: "example.org", port: 81})
"//bar@example.org:81"
iex> URI.to_string(%URI{
...> authority: "foo@example.com:80",
...> userinfo: "bar",
...> host: "example.org",
...> port: 81
...> })
"//bar@example.org:81"
"""
@spec to_string(t) :: binary
defdelegate to_string(uri), to: String.Chars.URI
@@ -881,7 +662,7 @@ defmodule URI do
@spec merge(t | binary, t | binary) :: t
def merge(uri, rel)
def merge(%URI{host: nil}, _rel) do
def merge(%URI{authority: nil}, _rel) do
raise ArgumentError, "you must merge onto an absolute URI"
end
@@ -889,11 +670,10 @@ defmodule URI do
%{rel | path: remove_dot_segments_from_path(rel.path)}
end
def merge(base, %URI{host: host} = rel) when host != nil do
def merge(base, %URI{authority: authority} = rel) when authority != nil do
%{rel | scheme: base.scheme, path: remove_dot_segments_from_path(rel.path)}
end
# TODO: Check only for nils in future versions
def merge(%URI{} = base, %URI{path: rel_path} = rel) when rel_path in ["", nil] do
%{base | query: rel.query || base.query, fragment: rel.fragment}
end
@@ -946,11 +726,12 @@ defmodule URI do
end
defimpl String.Chars, for: URI do
def to_string(%{host: host, path: path} = uri)
when host != nil and is_binary(path) and
def to_string(%{host: host, authority: authority, path: path} = uri)
when (host != nil or authority != nil) and is_binary(path) and
path != "" and binary_part(path, 0, 1) != "/" do
raise ArgumentError,
":path in URI must be empty or an absolute path if URL has a :host, got: #{inspect(uri)}"
":path in URI must be nil or an absolute path if :host or :authority are given, " <>
"got: #{inspect(uri)}"
end
def to_string(%{scheme: scheme, port: port, path: path, query: query, fragment: fragment} = uri) do
+95 -70
View File
@@ -30,17 +30,14 @@ defmodule Version do
The version is represented by the `Version` struct and fields
are named according to SemVer 2.0: `:major`, `:minor`, `:patch`,
`:pre`, and `:build`. You can read those fields but you should
not create a new `Version` directly via the struct syntax. Instead
use the functions in this module.
`:pre`, and `:build`.
## Requirements
Requirements allow you to specify which versions of a given
dependency you are willing to work against. Requirements support the common
comparison operators such as `>`, `>=`, `<`, `<=`, and `==` that work as one
would expect, and additionally the special operator `~>` described in detail
further below.
comparison operators such as `>`, `>=`, `<`, `<=`, `==`, `!=` that work as one would expect,
and additionally the special operator `~>` described in detail further below.
# Only version 2.0.0
"== 2.0.0"
@@ -134,25 +131,28 @@ defmodule Version do
@compile inline: [compare: 2]
@doc false
@spec new(String.t(), [atom | matchable]) :: t
@spec new(String.t(), :ets.match_spec()) :: t
def new(source, lexed) do
%__MODULE__{source: source, lexed: lexed}
end
@doc false
@spec match?(t, tuple) :: boolean
def match?(%__MODULE__{lexed: [operator, req | rest]}, version) do
match_lexed?(rest, version, match_op?(operator, req, version))
def match?(%__MODULE__{lexed: lexed}, matchable_pattern) do
match_lexed?(lexed, matchable_pattern)
end
defp match_lexed?([:and, operator, req | rest], version, acc),
do: match_lexed?(rest, version, acc and match_op?(operator, req, version))
defp match_lexed?([operator, req, :&& | rest], version) do
match_op?(operator, req, version) and match_lexed?(rest, version)
end
defp match_lexed?([:or, operator, req | rest], version, acc),
do: acc or match_lexed?(rest, version, match_op?(operator, req, version))
defp match_lexed?([operator, req, :|| | rest], version) do
match_op?(operator, req, version) or match_lexed?(rest, version)
end
defp match_lexed?([], _version, acc),
do: acc
defp match_lexed?([operator, req], version) do
match_op?(operator, req, version)
end
defp match_op?(:==, req, version) do
compare(version, req) == :eq
@@ -440,7 +440,7 @@ defmodule Version do
The internal representation is opaque.
"""
@spec compile_requirement(Requirement.t()) :: Requirement.t()
def compile_requirement(%Requirement{} = requirement) do
def compile_requirement(requirement) do
requirement
end
@@ -468,77 +468,64 @@ defmodule Version do
{">", :>},
{"<", :<},
{"==", :==},
{" or ", :or},
{" and ", :and}
{"!=", :!=},
{"!", :!=},
{" or ", :||},
{" and ", :&&}
]
def lexer(string) do
lexer(string, "", [])
end
for {string_op, atom_op} <- operators do
defp lexer(unquote(string_op) <> rest, buffer, acc) do
lexer(rest, "", [unquote(atom_op) | maybe_prepend_buffer(buffer, acc)])
def lexer(unquote(string_op) <> rest, acc) do
lexer(rest, [unquote(atom_op) | acc])
end
end
defp lexer("!=" <> rest, buffer, acc) do
IO.warn("!= inside Version requirements is deprecated, use ~> or >= instead")
lexer(rest, "", [:!= | maybe_prepend_buffer(buffer, acc)])
def lexer(" " <> rest, acc) do
lexer(rest, acc)
end
defp lexer("!" <> rest, buffer, acc) do
IO.warn("! inside Version requirements is deprecated, use ~> or >= instead")
lexer(rest, "", [:!= | maybe_prepend_buffer(buffer, acc)])
def lexer(<<char::utf8, rest::binary>>, []) do
lexer(rest, [<<char::utf8>>, :==])
end
defp lexer(" " <> rest, buffer, acc) do
lexer(rest, "", maybe_prepend_buffer(buffer, acc))
def lexer(<<char::utf8, body::binary>>, [head | acc]) do
acc =
case head do
head when is_binary(head) ->
[<<head::binary, char::utf8>> | acc]
head when head in [:||, :&&] ->
[<<char::utf8>>, :==, head | acc]
_other ->
[<<char::utf8>>, head | acc]
end
lexer(body, acc)
end
defp lexer(<<char::utf8, rest::binary>>, buffer, acc) do
lexer(rest, <<buffer::binary, char::utf8>>, acc)
end
def lexer("", acc) do
Enum.map(Enum.reverse(acc), fn
op when is_atom(op) ->
op
defp lexer(<<>>, buffer, acc) do
maybe_prepend_buffer(buffer, acc)
end
defp maybe_prepend_buffer("", acc), do: acc
defp maybe_prepend_buffer(buffer, [head | _] = acc)
when is_atom(head) and head not in [:and, :or],
do: [buffer | acc]
defp maybe_prepend_buffer(buffer, acc),
do: [buffer, :== | acc]
defp revert_lexed([version, op, cond | rest], acc)
when is_binary(version) and is_atom(op) and cond in [:or, :and] do
with {:ok, version} <- validate_requirement(op, version) do
revert_lexed(rest, [cond, op, version | acc])
end
end
defp revert_lexed([version, op], acc) when is_binary(version) and is_atom(op) do
with {:ok, version} <- validate_requirement(op, version) do
{:ok, [op, version | acc]}
end
end
defp revert_lexed(_rest, _acc), do: :error
defp validate_requirement(op, version) do
case parse_version(version, true) do
{:ok, version} when op == :~> -> {:ok, version}
{:ok, {_, _, patch, _, _} = version} when is_integer(patch) -> {:ok, version}
_ -> :error
end
version when is_binary(version) ->
case Version.Parser.parse_version(version, true) do
{:ok, version} -> version
:error -> :error
end
end)
end
@spec parse_requirement(String.t()) :: {:ok, term} | :error
def parse_requirement(source) do
revert_lexed(lexer(source), [])
lexed = lexer(source, [])
if valid_requirement?(lexed) do
{:ok, lexed}
else
:error
end
end
def parse_version(string, approximate? \\ false) when is_binary(string) do
@@ -623,6 +610,44 @@ defmodule Version do
defp valid_identifier?(_other) do
false
end
defp valid_requirement?([]), do: false
defp valid_requirement?([a | next]), do: valid_requirement?(a, next)
# it must finish with a version
defp valid_requirement?(a, []) when is_tuple(a) do
true
end
# or <op> | and <op>
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_atom(b) and a in [:||, :&&] do
valid_requirement?(b, next)
end
# <version> or | <version> and
defp valid_requirement?(a, [b | next]) when is_tuple(a) and is_atom(b) and b in [:||, :&&] do
valid_requirement?(b, next)
end
# or <version> | and <version>
defp valid_requirement?(a, [b | next]) when is_atom(a) and is_tuple(b) and a in [:||, :&&] do
valid_requirement?(b, next)
end
# ~> <version>
defp valid_requirement?(:~>, [b | next]) when is_tuple(b) do
valid_requirement?(b, next)
end
# <op> <version>
defp valid_requirement?(a, [{_major, _minor, patch, _pre, _build} = b | next])
when is_atom(a) and is_integer(patch) do
valid_requirement?(b, next)
end
defp valid_requirement?(_, _) do
false
end
end
end
@@ -8,11 +8,12 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
Elixir version | Support
:------------- | :-----------------------------
1.13 | Bug fixes and security patches
1.12 | Security patches only
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
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).
@@ -42,7 +43,6 @@ Erlang/OTP versioning is independent from the versioning of Elixir. Erlang relea
Elixir version | Supported Erlang/OTP versions
:------------- | :-------------------------------
1.13 | 22 - 24
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)
@@ -77,12 +77,7 @@ The first column is the version the feature was hard deprecated. The second colu
Version | Deprecated feature | Replaced by (available since)
:-------| :-------------------------------------------------- | :---------------------------------------------------------------
[v1.13] | `!` and `!=` in Version requirements | `~>` or `>=` (v1.0)
[v1.13] | `Mix.Config` | `Config` (v1.9)
[v1.13] | `:strip_beam` config to `mix escript.build` | `:strip_beams` (v1.9)
[v1.13] | `Macro.to_string/2` | `Macro.to_string/1` (v1.0)
[v1.13] | `System.get_pid/0` | `System.pid/0` (v1.9)
[v1.12] | `^^^/2` | `bxor/2` (v1.0)
[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)
@@ -187,4 +182,3 @@ Version | Deprecated feature | Replaced by (ava
[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/v1.11/CHANGELOG.md#4-hard-deprecations
[v1.12]: https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md#4-hard-deprecations
[v1.13]: https://github.com/elixir-lang/elixir/blob/v1.13/CHANGELOG.md#4-hard-deprecations
-1
View File
@@ -11,7 +11,6 @@ Operator | Associativity
`@` | Unary
`.` | Left
`+` `-` `!` `^` `not` `~~~` | Unary
`**` | Left
`*` `/` | Left
`+` `-` | Left
`++` `--` `+++` `---` `..` `<>` | Right
+1 -38
View File
@@ -205,43 +205,6 @@ iex> %{} = %{name: "meg"}
Finally, note map keys in patterns must always be literals or previously bound variables matched with the pin operator.
### Structs
Structs may appear in patterns using the percentage sign, the struct module name or a variable followed by the curly brackets syntax (`%{}`).
Given the following struct:
```elixir
defmodule User do
defstruct [:name]
end
```
Here is an example where all keys match:
```iex
iex> %User{name: name} = %User{name: "meg"}
%User{name: "meg"}
iex> name
"meg"
```
If an unknown key is given, the compiler will raise an error:
```iex
iex> %User{type: type} = %User{name: "meg"}
** (CompileError) iex: unknown key :type for struct User
```
The struct name can be extracted when putting a variable instead of a module name:
```
iex> %struct_name{} = %User{name: "meg"}
%User{name: "meg"}
iex> struct_name
User
```
### Binaries
Binaries may appear in patterns using the double less-than/greater-than syntax ([`<<>>`](`<<>>/1`)). A binary in a pattern can match multiple segments at the same, each with different type, size, and unit:
@@ -287,7 +250,7 @@ You can find the built-in list of guards [in the `Kernel` module](Kernel.html#gu
The module `Bitwise` also includes a handful of [Erlang bitwise operations as guards](Bitwise.html#guards).
Macros constructed out of any combination of the above guards are also valid guards - for example, `Integer.is_even/1`. For more information, see the "Custom patterns and guards expressions" section shown below.
Macros constructed out of any combination of the above guards are also valid guards - for example, `Integer.is_even/1`. For more information, see the "Defining custom guard expressions" section shown below.
### Why guards
+19 -24
View File
@@ -11,7 +11,7 @@ These are the reserved words in the Elixir language. They are detailed throughou
* `true`, `false`, `nil` - used as atoms
* `when`, `and`, `or`, `not`, `in` - used as operators
* `fn` - used for anonymous function definitions
* `do`, `end`, `catch`, `rescue`, `after`, `else` - used in do-end blocks
* `do`, `end`, `catch`, `rescue`, `after`, `else` - used in do/end blocks
## Data types
@@ -122,9 +122,9 @@ Blocks are multiple Elixir expressions separated by newlines or semi-colons. A n
### Left to right arrow
The left to right arrow (`->`) is used to establish a relationship between left and right, commonly referred as clauses. The left side may have zero, one, or more arguments; the right side is zero, one, or more expressions separated by new line. The `->` may appear one or more times between one of the following terminators: `do`-`end`, `fn`-`end` or `(`-`)`. When `->` is used, only other clauses are allowed between those terminators. Mixing clauses and regular expressions is invalid syntax.
The left to right arrow (`->`) is used to establish a relationship between left and right, commonly referred as clauses. The left side may have zero, one, or more arguments; the right side is zero, one, or more expressions separated by new line. The `->` may appear one or more times between one of the following terminators: `do`/`end`, `fn`/`end` or `(`/`)`. When `->` is used, only other clauses are allowed between those terminators. Mixing clauses and regular expressions is invalid syntax.
It is seen on `case` and `cond` constructs between `do` and `end`:
It is seen on `case` and `cond` constructs between `do`/`end`:
```elixir
case 1 do
@@ -137,13 +137,13 @@ cond do
end
```
Seen in typespecs between `(` and `)`:
Seen in typespecs between `(`/`)`:
```elixir
(integer(), boolean() -> integer())
```
It is also used between `fn` and `end` for building anonymous functions:
It is also used between `fn/end` for building anonymous functions:
```elixir
fn
@@ -352,7 +352,7 @@ end
#=> {:cond, [], [[do: [{:->, [], [[true], false]}]]]}
```
Between `(` and `)`:
Between `(`/`)`:
```elixir
quote do
@@ -362,7 +362,7 @@ end
#=> [{:->, [], [[1, 2], 3]}, {:->, [], [[4, 5], 6]}]
```
Between `fn` and `end`:
Between `fn/end`:
```elixir
quote do
@@ -374,17 +374,6 @@ end
#=> {:fn, [], [{:->, [], [[1, 2], 3]}, {:->, [], [[4, 5], 6]}]}
```
### Qualified tuples
Qualified tuples (`foo.{bar, baz}`) is represented by a `{:., [], [expr, :{}]}` call, where the `expr` represents the left hand side of the dot, and the arguments represent the elements inside the curly braces. This is used in Elixir to provide multi aliases:
```elixir
quote do
Foo.{Bar, Baz}
end
#=> {{:., [], [{:__aliases__, [], [:Foo]}, :{}]}, [], [{:__aliases__, [], [:Bar]}, {:__aliases__, [], [:Baz]}]}
```
## Syntactic sugar
All of the constructs above are part of Elixir's syntax and have their own representation as part of the Elixir AST. This section will discuss the remaining constructs that "desugar" to one of the constructs explored above. In other words, the constructs below can be represented in more than one way in your Elixir code and retain AST equivalence.
@@ -425,7 +414,7 @@ end
### Optional parentheses
Elixir provides optional parentheses:
Elixir provides optional parentheses for non-qualified and qualified calls.
```elixir
quote do
@@ -434,9 +423,15 @@ end
#=> {:sum, [], [1, 2, 3]}
```
The above is treated the same as `sum(1, 2, 3)` by the parser. You can remove the parentheses on all calls with at least one argument.
The above is treated the same as `sum(1, 2, 3)` by the parser.
You can also skip parentheses on qualified calls, such as `Foo.bar 1, 2, 3`. Parentheses are required when invoking anonymous functions, such as `f.(1, 2, 3)`.
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.
@@ -482,9 +477,9 @@ which in turn is the same as
if(condition, [{:do, this}, {:else, that}])
```
### `do`-`end` blocks
### `do`/`end` blocks
The last syntax convenience are `do`-`end` blocks. `do`-`end` blocks are equivalent to keywords as the last argument of a function call where the block contents are wrapped in parentheses. For example:
The last syntax convenience are `do`/`end` blocks. `do`/`end` blocks are equivalent to keywords as the last argument of a function call where the block contents are wrapped in parentheses. For example:
```elixir
if true do
@@ -520,7 +515,7 @@ if(true, do: (
))
```
Inside `do`-`end` blocks you may introduce other keywords, such as `else` used in the `if` above. The supported keywords between `do`-`end` are static and are:
Inside `do`/`end` blocks you may introduce other keywords, such as `else` used in the `if` above. The supported keywords between `do`/`end` are static and are:
* `after`
* `catch`
-1
View File
@@ -96,7 +96,6 @@ canonical = System.fetch_env!("CANONICAL")
],
"Code & Macros": [
Code,
Code.Fragment,
Kernel.ParallelCompiler,
Macro,
Macro.Env
+38 -55
View File
@@ -4,7 +4,8 @@
-behaviour(application).
-export([start_cli/0,
string_to_tokens/5, tokens_to_quoted/3, 'string_to_quoted!'/5,
env_for_eval/1, quoted_to_erl/2, eval_forms/3, eval_quoted/3]).
env_for_eval/1, env_for_eval/2, quoted_to_erl/2,
eval_forms/3, eval_quoted/3]).
-include("elixir.hrl").
-define(system, 'Elixir.System').
@@ -54,6 +55,10 @@ start(_Type, _Args) ->
{argv, []},
{no_halt, false},
%% Static options
{bootstrap, false},
{identifier_tokenizer, Tokenizer},
%% Compiler options
{docs, true},
{ignore_module_conflict, false},
@@ -66,8 +71,6 @@ start(_Type, _Args) ->
| URIConfig
],
elixir_config:static(#{bootstrap => false, identifier_tokenizer => Tokenizer}),
Tab = elixir_config:new(Config),
case elixir_sup:start_link() of
{ok, Sup} ->
@@ -162,22 +165,10 @@ start_cli() ->
%% EVAL HOOKS
env_for_eval(#{lexical_tracker := Pid} = Env) ->
NewEnv = Env#{
context := nil,
context_modules := [],
macro_aliases := [],
versioned_vars := #{}
},
case is_pid(Pid) andalso is_process_alive(Pid) of
true -> NewEnv;
false -> NewEnv#{lexical_tracker := nil, tracers := []}
end;
%% TODO: Deprecate all options except line and file.
env_for_eval(Opts) when is_list(Opts) ->
Env = elixir_env:new(),
env_for_eval(Opts) ->
env_for_eval(elixir_env:new(), Opts).
env_for_eval(Env, Opts) ->
Line = case lists:keyfind(line, 1, Opts) of
{line, LineOpt} when is_integer(LineOpt) -> LineOpt;
false -> ?key(Env, line)
@@ -253,11 +244,11 @@ eval_quoted(Tree, Binding, #{line := Line} = E) ->
eval_forms(Tree, Binding, Opts) when is_list(Opts) ->
eval_forms(Tree, Binding, env_for_eval(Opts));
eval_forms(Tree, RawBinding, OrigE) ->
{Vars, Binding} = normalize_binding(RawBinding, #{}, [], 0),
E = elixir_env:with_vars(OrigE, Vars),
{_, S} = elixir_env:env_to_erl(E),
{Erl, NewErlS, NewExS, NewE} = quoted_to_erl(Tree, E, S),
eval_forms(Tree, RawBinding, OE) ->
{Vars, Binding} = normalize_binding(RawBinding, [], []),
E = elixir_env:with_vars(OE, Vars),
{_, S} = elixir_env:env_to_scope(E),
{Erl, NewE, NewS} = quoted_to_erl(Tree, E, S),
case Erl of
{atom, _, Atom} ->
@@ -272,22 +263,15 @@ eval_forms(Tree, RawBinding, OrigE) ->
ErlBinding = elixir_erl_var:load_binding(Binding, E, S),
{value, Value, NewBinding} = recur_eval(Exprs, ErlBinding, NewE),
{Value, elixir_erl_var:dump_binding(NewBinding, NewExS, NewErlS), NewE}
{Value, elixir_erl_var:dump_binding(NewBinding, NewE, NewS), NewE}
end.
normalize_binding([Binding | NextBindings], VarsMap, Normalized, Counter) ->
{Pair, Value} = normalize_pair(Binding),
case VarsMap of
#{Pair := _} ->
normalize_binding(NextBindings, VarsMap, [{Pair, Value} | Normalized], Counter);
#{} ->
normalize_binding(NextBindings, VarsMap#{Pair => Counter}, [{Pair, Value} | Normalized], Counter + 1)
end;
normalize_binding([], VarsMap, Normalized, _Counter) ->
{VarsMap, Normalized}.
normalize_pair({Key, Value}) when is_atom(Key) -> {{Key, nil}, Value};
normalize_pair({Pair, Value}) -> {Pair, Value}.
normalize_binding([{Key, Value} | Binding], Vars, Acc) when is_atom(Key) ->
normalize_binding(Binding, [{Key, nil} | Vars], [{{Key, nil}, Value} | Acc]);
normalize_binding([{Pair, Value} | Binding], Vars, Acc) ->
normalize_binding(Binding, [Pair | Vars], [{Pair, Value} | Acc]);
normalize_binding([], Vars, Acc) ->
{Vars, Acc}.
recur_eval([Expr | Exprs], Binding, Env) ->
{value, Value, NewBinding} =
@@ -343,31 +327,30 @@ merge_stacktrace([StackItem | Stacktrace], CurrentStack) ->
%% Converts a quoted expression to Erlang abstract format
quoted_to_erl(Quoted, E) ->
{_, S} = elixir_env:env_to_erl(E),
{_, S} = elixir_env:env_to_scope(E),
quoted_to_erl(Quoted, E, S).
quoted_to_erl(Quoted, Env, Scope) ->
{Expanded, NewExS, NewEnv} = elixir_expand:expand(Quoted, elixir_env:env_to_ex(Env), Env),
{Erl, NewErlS} = elixir_erl_pass:translate(Expanded, erl_anno:new(?key(Env, line)), Scope),
{Erl, NewErlS, NewExS, NewEnv}.
{Expanded, NewEnv} = elixir_expand:expand(Quoted, Env),
{Erl, NewScope} = elixir_erl_pass:translate(Expanded, Scope),
{Erl, NewEnv, NewScope}.
%% Converts a given string (charlist) into quote expression
string_to_tokens(String, StartLine, StartColumn, File, Opts) when is_integer(StartLine), is_binary(File) ->
case elixir_tokenizer:tokenize(String, StartLine, StartColumn, [{file, File} | Opts]) of
{ok, _Line, _Column, Warnings, Tokens} ->
[elixir_errors:erl_warn(L, F, M) || {L, F, M} <- lists:reverse(Warnings)],
{ok, Tokens};
{error, {Line, Column, {ErrorPrefix, ErrorSuffix}, Token}, _Rest, _Warnings, _SoFar} ->
{ok, _Tokens} = Ok ->
Ok;
{error, {Line, Column, {ErrorPrefix, ErrorSuffix}, Token}, _Rest, _SoFar} ->
Location = [{line, Line}, {column, Column}],
{error, {Location, {to_binary(ErrorPrefix), to_binary(ErrorSuffix)}, to_binary(Token)}};
{error, {Line, Column, Error, Token}, _Rest, _Warnings, _SoFar} ->
{error, {Line, Column, Error, Token}, _Rest, _SoFar} ->
Location = [{line, Line}, {column, Column}],
{error, {Location, to_binary(Error), to_binary(Token)}}
end.
tokens_to_quoted(Tokens, WarningFile, Opts) ->
handle_parsing_opts(WarningFile, Opts),
tokens_to_quoted(Tokens, File, Opts) ->
handle_parsing_opts(File, Opts),
try elixir_parser:parse(Tokens) of
{ok, Forms} ->
@@ -377,7 +360,7 @@ tokens_to_quoted(Tokens, WarningFile, Opts) ->
{error, {Line, _, [Error, Token]}} ->
{error, {parser_location(Line), to_binary(Error), to_binary(Token)}}
after
erase(elixir_parser_warning_file),
erase(elixir_parser_file),
erase(elixir_parser_columns),
erase(elixir_token_metadata),
erase(elixir_literal_encoder)
@@ -403,17 +386,17 @@ parser_location(Meta) ->
case tokens_to_quoted(Tokens, File, Opts) of
{ok, Forms} ->
Forms;
{error, {Meta, Error, Token}} ->
elixir_errors:parse_error(Meta, File, Error, Token, {String, StartLine, StartColumn})
{error, {Line, Error, Token}} ->
elixir_errors:parse_error(Line, File, Error, Token)
end;
{error, {Meta, Error, Token}} ->
elixir_errors:parse_error(Meta, File, Error, Token, {String, StartLine, StartColumn})
{error, {Line, Error, Token}} ->
elixir_errors:parse_error(Line, File, Error, Token)
end.
to_binary(List) when is_list(List) -> elixir_utils:characters_to_binary(List);
to_binary(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8).
handle_parsing_opts(WarningFile, Opts) ->
handle_parsing_opts(File, Opts) ->
LiteralEncoder =
case lists:keyfind(literal_encoder, 1, Opts) of
{literal_encoder, Fun} -> Fun;
@@ -421,7 +404,7 @@ handle_parsing_opts(WarningFile, Opts) ->
end,
TokenMetadata = lists:keyfind(token_metadata, 1, Opts) == {token_metadata, true},
Columns = lists:keyfind(columns, 1, Opts) == {columns, true},
put(elixir_parser_warning_file, WarningFile),
put(elixir_parser_file, File),
put(elixir_parser_columns, Columns),
put(elixir_token_metadata, TokenMetadata),
put(elixir_literal_encoder, LiteralEncoder).
+3 -11
View File
@@ -1,18 +1,10 @@
-define(key(M, K), maps:get(K, M)).
-define(ann(Meta), elixir_erl:get_ann(Meta)).
-define(line(Meta), elixir_utils:get_line(Meta)).
-define(ann(Opts), elixir_erl:get_ann(Opts)).
-define(line(Opts), elixir_utils:get_line(Opts)).
-define(generated(Meta), [{generated, true} | Meta]).
-define(var_context, ?MODULE).
-define(remote(Ann, Module, Function, Args), {call, Ann, {remote, Ann, {atom, Ann, Module}, {atom, Ann, Function}}, Args}).
-record(elixir_ex, {
caller=false, %% stores if __CALLER__ is allowed
prematch=warn, %% {Read, Counter} | warn | raise | pin
stacktrace=false, %% stores if __STACKTRACE__ is allowed
unused={#{}, 0}, %% a map of unused vars and a version counter for vars
vars={#{}, false} %% a tuple with maps of read and optional write current vars
}).
-record(elixir_erl, {
context=nil, %% can be match, guards or nil
extra=nil, %% extra information about the context, like pin_guard and map_key
@@ -28,7 +20,7 @@
file=(<<"nofile">>),
terminators=[],
unescape=true,
cursor_completion=false,
check_terminators=true,
existing_atoms_only=false,
static_atoms_encoder=nil,
preserve_comments=nil,
+7 -22
View File
@@ -1,10 +1,10 @@
-module(elixir_aliases).
-export([inspect/1, last/1, concat/1, safe_concat/1, format_error/1,
ensure_loaded/3, expand/2, expand_or_concat/2, store/5]).
ensure_loaded/3, expand/2, store/5]).
-include("elixir.hrl").
inspect(Atom) when is_atom(Atom) ->
case elixir_config:is_bootstrap() of
case elixir_config:get(bootstrap) of
true -> atom_to_binary(Atom, utf8);
false -> 'Elixir.Code.Identifier':inspect_as_atom(Atom)
end.
@@ -56,12 +56,10 @@ expand({'__aliases__', Meta, _} = Alias, #{aliases := Aliases, macro_aliases :=
expand({'__aliases__', Meta, [H | T]}, Aliases, E) when is_atom(H) ->
Lookup = list_to_atom("Elixir." ++ atom_to_list(H)),
Counter = case lists:keyfind(counter, 1, Meta) of
{counter, C} -> C;
_ -> nil
end,
case lookup(Lookup, Aliases, Counter) of
Lookup -> [H | T];
Atom ->
@@ -75,14 +73,6 @@ expand({'__aliases__', Meta, [H | T]}, Aliases, E) when is_atom(H) ->
expand({'__aliases__', _Meta, List}, _Aliases, _E) ->
List.
%% Expands or concat if possible.
expand_or_concat(Aliases, E) ->
case expand(Aliases, E) of
[H | T] when is_atom(H) -> concat([H | T]);
AtomOrList -> AtomOrList
end.
%% Ensure a module is loaded before its usage.
ensure_loaded(_Meta, 'Elixir.Kernel', _E) -> ok;
@@ -114,21 +104,16 @@ ensure_loaded(Meta, Module, E) ->
end.
wait_for_module(Module) ->
case erlang:get(elixir_compiler_info) of
undefined -> not_found;
_ -> 'Elixir.Kernel.ErrorHandler':ensure_compiled(Module, module, hard)
case is_pid(erlang:get(elixir_compiler_pid)) of
true -> 'Elixir.Kernel.ErrorHandler':ensure_compiled(Module, module, hard);
false -> not_found
end.
%% Receives an atom and returns the last bit as an alias.
last(Atom) ->
case atom_to_list(Atom) of
("Elixir." ++ [FirstLetter | _]) = List when FirstLetter >= $A, FirstLetter =< $Z ->
Last = last(lists:reverse(List), []),
{ok, list_to_atom("Elixir." ++ Last)};
_ ->
error
end.
Last = last(lists:reverse(atom_to_list(Atom)), []),
list_to_atom("Elixir." ++ Last).
last([$. | _], Acc) -> Acc;
last([H | T], Acc) -> last(T, [H | Acc]);
+60 -64
View File
@@ -1,47 +1,47 @@
-module(elixir_bitstring).
-export([expand/5, format_error/1]).
-export([expand/4, format_error/1]).
-import(elixir_errors, [form_error/4]).
-include("elixir.hrl").
expand_match(Expr, {S, OriginalS}, E) ->
{EExpr, SE, EE} = elixir_expand:expand(Expr, S, E),
{EExpr, {SE, OriginalS}, EE}.
expand_match(Expr, {E, OriginalE}) ->
{EExpr, EE} = elixir_expand:expand(Expr, E),
{EExpr, {EE, OriginalE}}.
expand(Meta, Args, S, E, RequireSize) ->
expand(Meta, Args, E, RequireSize) ->
case ?key(E, context) of
match ->
{EArgs, Alignment, {SA, _}, EA} =
expand(Meta, fun expand_match/3, Args, [], {S, S}, E, 0, RequireSize),
{EArgs, Alignment, {EA, _}} =
expand(Meta, fun expand_match/2, Args, [], {E, E}, 0, RequireSize),
case find_match(EArgs) of
false ->
{{'<<>>', [{alignment, Alignment} | Meta], EArgs}, SA, EA};
{{'<<>>', [{alignment, Alignment} | Meta], EArgs}, EA};
Match ->
form_error(Meta, EA, ?MODULE, {nested_match, Match})
end;
_ ->
PairS = {elixir_env:prepare_write(S), S},
PairE = {elixir_env:prepare_write(E), E},
{EArgs, Alignment, {SA, _}, EA} =
expand(Meta, fun elixir_expand:expand_arg/3, Args, [], PairS, E, 0, RequireSize),
{EArgs, Alignment, {EA, _}} =
expand(Meta, fun elixir_expand:expand_arg/2, Args, [], PairE, 0, RequireSize),
{{'<<>>', [{alignment, Alignment} | Meta], EArgs}, elixir_env:close_write(SA, S), EA}
{{'<<>>', [{alignment, Alignment} | Meta], EArgs}, elixir_env:close_write(EA, E)}
end.
expand(_BitstrMeta, _Fun, [], Acc, S, E, Alignment, _RequireSize) ->
{lists:reverse(Acc), Alignment, S, E};
expand(BitstrMeta, Fun, [{'::', Meta, [Left, Right]} | T], Acc, S, E, Alignment, RequireSize) ->
{ELeft, {SL, OriginalS}, EL} = expand_expr(Meta, Left, Fun, S, E),
expand(_BitstrMeta, _Fun, [], Acc, E, Alignment, _RequireSize) ->
{lists:reverse(Acc), Alignment, E};
expand(BitstrMeta, Fun, [{'::', Meta, [Left, Right]} | T], Acc, E, Alignment, RequireSize) ->
{ELeft, {EL, OriginalE}} = expand_expr(Meta, Left, Fun, E),
MatchOrRequireSize = RequireSize or is_match_size(T, EL),
EType = expr_type(ELeft),
{ERight, EAlignment, SS, ES} = expand_specs(EType, Meta, Right, SL, OriginalS, EL, MatchOrRequireSize),
{ERight, EAlignment, ES} = expand_specs(EType, Meta, Right, EL, OriginalE, MatchOrRequireSize),
EAcc = concat_or_prepend_bitstring(Meta, ELeft, ERight, Acc, ES, MatchOrRequireSize),
expand(BitstrMeta, Fun, T, EAcc, {SS, OriginalS}, ES, alignment(Alignment, EAlignment), RequireSize);
expand(BitstrMeta, Fun, [H | T], Acc, S, E, Alignment, RequireSize) ->
expand(BitstrMeta, Fun, T, EAcc, {ES, OriginalE}, alignment(Alignment, EAlignment), RequireSize);
expand(BitstrMeta, Fun, [H | T], Acc, E, Alignment, RequireSize) ->
Meta = extract_meta(H, BitstrMeta),
{ELeft, {SS, OriginalS}, ES} = expand_expr(Meta, H, Fun, S, E),
{ELeft, {ES, OriginalE}} = expand_expr(Meta, H, Fun, E),
MatchOrRequireSize = RequireSize or is_match_size(T, ES),
EType = expr_type(ELeft),
@@ -49,7 +49,7 @@ expand(BitstrMeta, Fun, [H | T], Acc, S, E, Alignment, RequireSize) ->
InferredMeta = [{inferred_bitstring_spec, true} | Meta],
EAcc = concat_or_prepend_bitstring(InferredMeta, ELeft, ERight, Acc, ES, MatchOrRequireSize),
expand(Meta, Fun, T, EAcc, {SS, OriginalS}, ES, Alignment, RequireSize).
expand(Meta, Fun, T, EAcc, {ES, OriginalE}, Alignment, RequireSize).
extract_meta({_, Meta, _}, _) -> Meta;
extract_meta(_, Meta) -> Meta.
@@ -133,39 +133,37 @@ compute_alignment(_, _, _) -> unknown.
%% If we are inside a match/guard, we inline interpolations explicitly,
%% otherwise they are inlined by elixir_rewrite.erl.
expand_expr(_Meta, {{'.', _, [Mod, to_string]}, _, [Arg]} = AST, Fun, S, #{context := Context} = E)
expand_expr(_Meta, {{'.', _, [Mod, to_string]}, _, [Arg]} = AST, Fun, {#{context := Context}, _} = E)
when Context /= nil, (Mod == 'Elixir.Kernel') orelse (Mod == 'Elixir.String.Chars') ->
case Fun(Arg, S, E) of
{EBin, SE, EE} when is_binary(EBin) -> {EBin, SE, EE};
_ -> Fun(AST, S, E) % Let it raise
case Fun(Arg, E) of
{EBin, EE} when is_binary(EBin) -> {EBin, EE};
_ -> Fun(AST, E) % Let it raise
end;
expand_expr(Meta, Component, Fun, S, E) ->
case Fun(Component, S, E) of
{EComponent, _, ErrorE} when is_list(EComponent); is_atom(EComponent) ->
expand_expr(Meta, Component, Fun, E) ->
case Fun(Component, E) of
{EComponent, {ErrorE, _}} when is_list(EComponent); is_atom(EComponent) ->
form_error(Meta, ErrorE, ?MODULE, {invalid_literal, EComponent});
{_, _, _} = Expanded ->
{_, _} = Expanded ->
Expanded
end.
%% Expands and normalizes types of a bitstring.
expand_specs(ExprType, Meta, Info, S, OriginalS, E, RequireSize) ->
expand_specs(ExprType, Meta, Info, E, OriginalE, RequireSize) ->
Default =
#{size => default,
unit => default,
sign => default,
type => default,
endianness => default},
{#{size := Size, unit := Unit, type := Type, endianness := Endianness, sign := Sign}, SS, ES} =
expand_each_spec(Meta, unpack_specs(Info, []), Default, S, OriginalS, E),
{#{size := Size, unit := Unit, type := Type, endianness := Endianness, sign := Sign}, ES} =
expand_each_spec(Meta, unpack_specs(Info, []), Default, E, OriginalE),
MergedType = type(Meta, ExprType, Type, E),
validate_size_required(Meta, RequireSize, ExprType, MergedType, Size, ES),
SizeAndUnit = size_and_unit(Meta, ExprType, Size, Unit, ES),
Alignment = compute_alignment(MergedType, Size, Unit),
[H | T] = build_spec(Meta, Size, Unit, MergedType, Endianness, Sign, SizeAndUnit, ES),
{lists:foldl(fun(I, Acc) -> {'-', Meta, [Acc, I]} end, H, T), Alignment, SS, ES}.
{lists:foldl(fun(I, Acc) -> {'-', Meta, [Acc, I]} end, H, T), Alignment, ES}.
type(_, default, default, _) ->
integer;
@@ -184,11 +182,11 @@ type(_, default, Type, _) ->
type(Meta, Other, Value, E) ->
form_error(Meta, E, ?MODULE, {bittype_mismatch, Value, Other, type}).
expand_each_spec(Meta, [{Expr, _, Args} = H | T], Map, S, OriginalS, E) when is_atom(Expr) ->
expand_each_spec(Meta, [{Expr, _, Args} = H | T], Map, E, OriginalE) when is_atom(Expr) ->
case validate_spec(Expr, Args) of
{Key, Arg} ->
{Value, SE, EE} = expand_spec_arg(Arg, S, OriginalS, E),
validate_spec_arg(Meta, Key, Value, SE, OriginalS, EE),
{Value, EE} = expand_spec_arg(Arg, E, OriginalE),
validate_spec_arg(Meta, Key, Value, EE, OriginalE),
case maps:get(Key, Map) of
default -> ok;
@@ -196,21 +194,19 @@ expand_each_spec(Meta, [{Expr, _, Args} = H | T], Map, S, OriginalS, E) when is_
Other -> form_error(Meta, E, ?MODULE, {bittype_mismatch, Value, Other, Key})
end,
expand_each_spec(Meta, T, maps:put(Key, Value, Map), SE, OriginalS, EE);
expand_each_spec(Meta, T, maps:put(Key, Value, Map), EE, OriginalE);
none ->
case 'Elixir.Macro':expand(H, E#{line := ?line(Meta)}) of
case 'Elixir.Macro':expand(H, elixir_env:linify({?line(Meta), E})) of
H ->
form_error(Meta, E, ?MODULE, {undefined_bittype, H});
NewTypes ->
expand_each_spec(Meta, unpack_specs(NewTypes, []) ++ T, Map, S, OriginalS, E)
expand_each_spec(Meta, unpack_specs(NewTypes, []) ++ T, Map, E, OriginalE)
end
end;
expand_each_spec(Meta, [Expr | _], _Map, _S, _OriginalS, E) ->
expand_each_spec(Meta, [Expr | _], _Map, E, _OriginalE) ->
form_error(Meta, E, ?MODULE, {undefined_bittype, Expr});
expand_each_spec(_Meta, [], Map, S, _OriginalS, E) ->
{Map, S, E}.
expand_each_spec(_Meta, [], Map, E, _OriginalE) ->
{Map, E}.
unpack_specs({'-', _, [H, T]}, Acc) ->
unpack_specs(H, unpack_specs(T, Acc));
@@ -244,20 +240,20 @@ validate_spec(signed, []) -> {sign, signed};
validate_spec(unsigned, []) -> {sign, unsigned};
validate_spec(_, _) -> none.
expand_spec_arg(Expr, S, _OriginalS, E) when is_atom(Expr); is_integer(Expr) ->
{Expr, S, E};
expand_spec_arg(Expr, S, _OriginalS, #{context := match} = E) ->
{EExpr, SE, EE} = elixir_expand:expand(Expr, S#elixir_ex{prematch=raise}, E#{context := nil}),
{EExpr, SE#elixir_ex{prematch=S#elixir_ex.prematch}, EE#{context := match}};
expand_spec_arg(Expr, S, OriginalS, E) ->
elixir_expand:expand(Expr, elixir_env:reset_read(S, OriginalS), E).
expand_spec_arg(Expr, E, _OriginalE) when is_atom(Expr); is_integer(Expr) ->
{Expr, E};
expand_spec_arg(Expr, #{context := match} = E, _OriginalE) ->
{EExpr, EE} = elixir_expand:expand(Expr, E#{context := nil, prematch_vars := raise}),
{EExpr, EE#{context := match, prematch_vars := ?key(E, prematch_vars)}};
expand_spec_arg(Expr, E, OriginalE) ->
elixir_expand:expand(Expr, elixir_env:reset_read(E, OriginalE)).
validate_spec_arg(Meta, size, Value, S, OriginalS, E) ->
validate_spec_arg(Meta, size, Value, E, OriginalE) ->
case Value of
{Var, VarMeta, Context} when is_atom(Var) and is_atom(Context) ->
Tuple = {Var, elixir_utils:var_context(VarMeta, Context)},
case is_valid_spec_arg_var(Tuple, S, OriginalS, E) of
case is_valid_spec_arg_var(Tuple, E, OriginalE) of
true -> ok;
false -> form_error(Meta, E, ?MODULE, {undefined_var_in_spec, Value})
end;
@@ -268,20 +264,20 @@ validate_spec_arg(Meta, size, Value, S, OriginalS, E) ->
_ ->
form_error(Meta, E, ?MODULE, {bad_size_argument, Value})
end;
validate_spec_arg(Meta, unit, Value, _S, _OriginalS, E) when not is_integer(Value) ->
validate_spec_arg(Meta, unit, Value, E, _OriginalE) when not is_integer(Value) ->
form_error(Meta, E, ?MODULE, {bad_unit_argument, Value});
validate_spec_arg(_Meta, _Key, _Value, _S, _OriginalS, _E) ->
validate_spec_arg(_Meta, _Key, _Value, _E, _OriginalE) ->
ok.
is_valid_spec_arg_var(Var, S, OriginalS, #{context := match}) ->
case S#elixir_ex.prematch of
{#{Var := _}, _} -> true;
_ -> is_var(Var, S) andalso not is_var(Var, OriginalS)
is_valid_spec_arg_var(Var, E, #{context := match} = OriginalE) ->
case OriginalE of
#{prematch_vars := {#{Var := _}, _}} -> true;
_ -> is_var(Var, E) andalso not is_var(Var, OriginalE)
end;
is_valid_spec_arg_var(_Var, _S, _OriginalS, _E) ->
is_valid_spec_arg_var(_Var, _E, _OriginalE) ->
true.
is_var(Var, #elixir_ex{vars={Read, _}}) ->
is_var(Var, #{current_vars := {Read, _}}) ->
maps:is_key(Var, Read).
validate_size_required(Meta, true, default, Type, default, E) when Type == binary; Type == bitstring ->
@@ -340,7 +336,7 @@ number_size(Size, default) when is_integer(Size) -> Size;
number_size(Size, Unit) when is_integer(Size) -> Size * Unit;
number_size(Size, _) -> Size.
%% TODO: Simplify when we require Erlang/OTP 24
%% TODO: Simplify when we require OTP 24
valid_float_size(16) -> erlang:system_info(otp_release) >= "24";
valid_float_size(32) -> true;
valid_float_size(64) -> true;
+4 -4
View File
@@ -19,7 +19,7 @@
'MACRO-defmacrop'(Caller, Call, Expr) -> define(Caller, defmacrop, Call, Expr).
'MACRO-defmodule'(_Caller, Alias, [{do, Block}]) ->
Escaped = elixir_quote:escape(Block, none, false),
Escaped = elixir_quote:escape(Block, default, false),
Args = [Alias, Escaped, [], env()],
{{'.', [], [elixir_module, compile]}, [], Args}.
@@ -35,11 +35,11 @@
{defmodule, 2},
{defp, 2}].
define({Line, _S, E}, Kind, Call, Expr) ->
define({Line, E}, Kind, Call, Expr) ->
UC = elixir_quote:has_unquotes(Call),
UE = elixir_quote:has_unquotes(Expr),
EscapedCall = elixir_quote:escape(Call, none, true),
EscapedExpr = elixir_quote:escape(Expr, none, true),
EscapedCall = elixir_quote:escape(Call, default, true),
EscapedExpr = elixir_quote:escape(Expr, default, true),
Args = [Kind, not(UC or UE), EscapedCall, EscapedExpr, elixir_locals:cache_env(E#{line := Line})],
{{'.', [], [elixir_def, store_definition]}, [], Args}.
+162 -163
View File
@@ -1,75 +1,79 @@
%% Handle code related to args, guard and -> matching for case,
%% fn, receive and friends. try is handled in elixir_try.
-module(elixir_clauses).
-export([match/5, clause/6, def/3, head/3,
'case'/4, 'receive'/4, 'try'/4, 'cond'/4, with/4,
-export([match/4, clause/5, def/2, head/2, head/3,
'case'/3, 'receive'/3, 'try'/3, 'cond'/3, with/3,
format_error/1]).
-import(elixir_errors, [form_error/4, form_warn/4]).
-include("elixir.hrl").
match(Fun, Expr, AfterS, _BeforeS, #{context := match} = E) ->
Fun(Expr, AfterS, E);
match(Fun, Expr, AfterS, BeforeS, E) ->
#elixir_ex{vars=Current, unused={_, Counter} = Unused} = AfterS,
#elixir_ex{vars={Read, _}, prematch=Prematch} = BeforeS,
match(Fun, Expr, E, #{context := match}) ->
Fun(Expr, E);
match(Fun, Expr, #{current_vars := Current, unused_vars := {_, Counter} = Unused} = AfterE, BeforeE) ->
#{
context := Context,
prematch_vars := Prematch,
current_vars := {Read, _}
} = BeforeE,
CallS = BeforeS#elixir_ex{
prematch={Read, Counter},
unused=Unused,
vars=Current
CallE = BeforeE#{
context := match,
prematch_vars := {Read, Counter},
current_vars := Current,
unused_vars := Unused
},
CallE = E#{context := match},
{EExpr, #elixir_ex{vars=NewCurrent, unused=NewUnused}, EE} = Fun(Expr, CallS, CallE),
{EExpr, #{current_vars := NewCurrent, unused_vars := NewUnused}} = Fun(Expr, CallE),
EndS = AfterS#elixir_ex{
prematch=Prematch,
unused=NewUnused,
vars=NewCurrent
EndE = AfterE#{
context := Context,
prematch_vars := Prematch,
current_vars := NewCurrent,
unused_vars := NewUnused
},
EndE = EE#{context := ?key(E, context)},
{EExpr, EndS, EndE}.
{EExpr, EndE}.
def({Meta, Args, Guards, Body}, S, E) ->
{EArgs, SA, EA} = elixir_expand:expand_args(Args, S#elixir_ex{prematch={#{}, 0}}, E#{context := match}),
{EGuards, SG, EG} = guard(Guards, SA#elixir_ex{prematch=warn}, EA#{context := guard}),
{EBody, SB, EB} = elixir_expand:expand(Body, SG, EG#{context := nil}),
elixir_env:check_unused_vars(SB, EB),
def({Meta, Args, Guards, Body}, E) ->
{EArgs, EA} = elixir_expand:expand_args(Args, E#{context := match, prematch_vars := {#{}, 0}}),
{EGuards, EG} = guard(Guards, EA#{context := guard, prematch_vars := warn}),
{EBody, EB} = elixir_expand:expand(Body, EG#{context := nil}),
elixir_env:check_unused_vars(EB),
{Meta, EArgs, EGuards, EBody}.
clause(Meta, Kind, Fun, {'->', ClauseMeta, [_, _]} = Clause, S, E) when is_function(Fun, 4) ->
clause(Meta, Kind, fun(X, SA, EA) -> Fun(ClauseMeta, X, SA, EA) end, Clause, S, E);
clause(_Meta, _Kind, Fun, {'->', Meta, [Left, Right]}, S, E) ->
{ELeft, SL, EL} = Fun(Left, S, E),
{ERight, SR, ER} = elixir_expand:expand(Right, SL, EL),
{{'->', Meta, [ELeft, ERight]}, SR, ER};
clause(Meta, Kind, _Fun, _, _, E) ->
clause(Meta, Kind, Fun, {'->', ClauseMeta, [_, _]} = Clause, E) when is_function(Fun, 3) ->
clause(Meta, Kind, fun(X, Acc) -> Fun(ClauseMeta, X, Acc) end, Clause, E);
clause(_Meta, _Kind, Fun, {'->', Meta, [Left, Right]}, E) ->
{ELeft, EL} = Fun(Left, E),
{ERight, ER} = elixir_expand:expand(Right, EL),
{{'->', Meta, [ELeft, ERight]}, ER};
clause(Meta, Kind, _Fun, _, E) ->
form_error(Meta, E, ?MODULE, {bad_or_missing_clauses, Kind}).
head([{'when', Meta, [_ | _] = All}], S, E) ->
head(Args, E) ->
head(Args, E, E).
head([{'when', Meta, [_ | _] = All}], AfterE, BeforeE) ->
{Args, Guard} = elixir_utils:split_last(All),
Prematch = S#elixir_ex.prematch,
{{EArgs, EGuard}, SG, EG} =
match(fun(ok, SM, EM) ->
{EArgs, SA, EA} = elixir_expand:expand_args(Args, SM, EM),
{EGuard, SG, EG} = guard(Guard, SA#elixir_ex{prematch=Prematch}, EA#{context := guard}),
{{EArgs, EGuard}, SG, EG}
end, ok, S, S, E),
{{EArgs, EGuard}, EG} =
match(fun(ok, EM) ->
{EArgs, EA} = elixir_expand:expand_args(Args, EM),
{EGuard, EG} = guard(Guard, EA#{context := guard, prematch_vars := ?key(BeforeE, prematch_vars)}),
{{EArgs, EGuard}, EG}
end, ok, AfterE, BeforeE),
{[{'when', Meta, EArgs ++ [EGuard]}], SG, EG};
head(Args, S, E) ->
match(fun elixir_expand:expand_args/3, Args, S, S, E).
{[{'when', Meta, EArgs ++ [EGuard]}], EG};
head(Args, AfterE, BeforeE) ->
match(fun elixir_expand:expand_args/2, Args, AfterE, BeforeE).
guard({'when', Meta, [Left, Right]}, S, E) ->
{ELeft, SL, EL} = guard(Left, S, E),
{ERight, SR, ER} = guard(Right, SL, EL),
{{'when', Meta, [ELeft, ERight]}, SR, ER};
guard(Guard, S, E) ->
{EGuard, SG, EG} = elixir_expand:expand(Guard, S, E),
guard({'when', Meta, [Left, Right]}, E) ->
{ELeft, EL} = guard(Left, E),
{ERight, ER} = guard(Right, EL),
{{'when', Meta, [ELeft, ERight]}, ER};
guard(Guard, E) ->
{EGuard, EG} = elixir_expand:expand(Guard, E),
warn_zero_length_guard(EGuard, EG),
{EGuard, SG, EG}.
{EGuard, EG}.
warn_zero_length_guard({{'.', _, [erlang, Op]}, Meta,
[{{'.', _, [erlang, length]}, _, [Arg]}, 0]}, E) when Op == '=='; Op == '>' ->
@@ -87,73 +91,70 @@ warn_zero_length_guard(_, _) ->
%% Case
'case'(Meta, [], _S, E) ->
'case'(Meta, [], E) ->
form_error(Meta, E, elixir_expand, {missing_option, 'case', [do]});
'case'(Meta, Opts, _S, E) when not is_list(Opts) ->
'case'(Meta, Opts, E) when not is_list(Opts) ->
form_error(Meta, E, elixir_expand, {invalid_args, 'case'});
'case'(Meta, Opts, S, E) ->
'case'(Meta, Opts, E) ->
ok = assert_at_most_once('do', Opts, 0, fun(Key) ->
form_error(Meta, E, ?MODULE, {duplicated_clauses, 'case', Key})
end),
{Case, SA} = lists:mapfoldl(fun(X, SA) -> expand_case(Meta, X, SA, E) end, S, Opts),
{Case, SA, E}.
lists:mapfoldl(fun(X, Acc) -> expand_case(Meta, X, Acc) end, E, Opts).
expand_case(Meta, {'do', _} = Do, S, E) ->
expand_case(Meta, {'do', _} = Do, E) ->
Fun = expand_head(Meta, 'case', 'do'),
expand_clauses(Meta, 'case', Fun, Do, S, E);
expand_case(Meta, {Key, _}, _S, E) ->
expand_clauses(Meta, 'case', Fun, Do, E);
expand_case(Meta, {Key, _}, E) ->
form_error(Meta, E, ?MODULE, {unexpected_option, 'case', Key}).
%% Cond
'cond'(Meta, [], _S, E) ->
'cond'(Meta, [], E) ->
form_error(Meta, E, elixir_expand, {missing_option, 'cond', [do]});
'cond'(Meta, Opts, _S, E) when not is_list(Opts) ->
'cond'(Meta, Opts, E) when not is_list(Opts) ->
form_error(Meta, E, elixir_expand, {invalid_args, 'cond'});
'cond'(Meta, Opts, S, E) ->
'cond'(Meta, Opts, E) ->
ok = assert_at_most_once('do', Opts, 0, fun(Key) ->
form_error(Meta, E, ?MODULE, {duplicated_clauses, 'cond', Key})
end),
{Cond, SA} = lists:mapfoldl(fun(X, SA) -> expand_cond(Meta, X, SA, E) end, S, Opts),
{Cond, SA, E}.
lists:mapfoldl(fun(X, Acc) -> expand_cond(Meta, X, Acc) end, E, Opts).
expand_cond(Meta, {'do', _} = Do, S, E) ->
Fun = expand_one(Meta, 'cond', 'do', fun elixir_expand:expand_args/3),
expand_clauses(Meta, 'cond', Fun, Do, S, E);
expand_cond(Meta, {Key, _}, _S, E) ->
expand_cond(Meta, {'do', _} = Do, E) ->
Fun = expand_one(Meta, 'cond', 'do', fun elixir_expand:expand_args/2),
expand_clauses(Meta, 'cond', Fun, Do, E);
expand_cond(Meta, {Key, _}, E) ->
form_error(Meta, E, ?MODULE, {unexpected_option, 'cond', Key}).
%% Receive
'receive'(Meta, [], _S, E) ->
'receive'(Meta, [], E) ->
form_error(Meta, E, elixir_expand, {missing_option, 'receive', [do, 'after']});
'receive'(Meta, Opts, _S, E) when not is_list(Opts) ->
'receive'(Meta, Opts, E) when not is_list(Opts) ->
form_error(Meta, E, elixir_expand, {invalid_args, 'receive'});
'receive'(Meta, Opts, S, E) ->
'receive'(Meta, Opts, E) ->
RaiseError = fun(Key) ->
form_error(Meta, E, ?MODULE, {duplicated_clauses, 'receive', Key})
end,
ok = assert_at_most_once('do', Opts, 0, RaiseError),
ok = assert_at_most_once('after', Opts, 0, RaiseError),
{Receive, SA} = lists:mapfoldl(fun(X, SA) -> expand_receive(Meta, X, SA, E) end, S, Opts),
{Receive, SA, E}.
lists:mapfoldl(fun(X, Acc) -> expand_receive(Meta, X, Acc) end, E, Opts).
expand_receive(_Meta, {'do', {'__block__', _, []}} = Do, S, _E) ->
{Do, S};
expand_receive(Meta, {'do', _} = Do, S, E) ->
expand_receive(_Meta, {'do', {'__block__', _, []}} = Do, E) ->
{Do, E};
expand_receive(Meta, {'do', _} = Do, E) ->
Fun = expand_head(Meta, 'receive', 'do'),
expand_clauses(Meta, 'receive', Fun, Do, S, E);
expand_receive(Meta, {'after', [_]} = After, S, E) ->
Fun = expand_one(Meta, 'receive', 'after', fun elixir_expand:expand_args/3),
expand_clauses(Meta, 'receive', Fun, After, S, E);
expand_receive(Meta, {'after', _}, _S, E) ->
expand_clauses(Meta, 'receive', Fun, Do, E);
expand_receive(Meta, {'after', [_]} = After, E) ->
Fun = expand_one(Meta, 'receive', 'after', fun elixir_expand:expand_args/2),
expand_clauses(Meta, 'receive', Fun, After, E);
expand_receive(Meta, {'after', _}, E) ->
form_error(Meta, E, ?MODULE, multiple_after_clauses_in_receive);
expand_receive(Meta, {Key, _}, _S, E) ->
expand_receive(Meta, {Key, _}, E) ->
form_error(Meta, E, ?MODULE, {unexpected_option, 'receive', Key}).
%% With
with(Meta, Args, S, E) ->
with(Meta, Args, E) ->
{Exprs, Opts0} =
case elixir_utils:split_last(Args) of
{_, LastArg} = SplitResult when is_list(LastArg) ->
@@ -162,10 +163,10 @@ with(Meta, Args, S, E) ->
{Args, []}
end,
S0 = elixir_env:reset_unused_vars(S),
{EExprs, {S1, E1, HasMatch}} = lists:mapfoldl(fun expand_with/2, {S0, E, false}, Exprs),
{EDo, Opts1, S2} = expand_with_do(Meta, Opts0, S, S1, E1),
{EOpts, Opts2, S3} = expand_with_else(Meta, Opts1, S2, E, HasMatch),
E0 = elixir_env:reset_unused_vars(E),
{EExprs, {E1, HasMatch}} = lists:mapfoldl(fun expand_with/2, {E0, false}, Exprs),
{EDo, Opts1, E2} = expand_with_do(Meta, Opts0, E, E1),
{EOpts, Opts2, E3} = expand_with_else(Meta, Opts1, E2, HasMatch),
case Opts2 of
[{Key, _} | _] ->
@@ -174,27 +175,28 @@ with(Meta, Args, S, E) ->
ok
end,
{{with, Meta, EExprs ++ [[{do, EDo} | EOpts]]}, S3, E}.
{{with, Meta, EExprs ++ [[{do, EDo} | EOpts]]}, E3}.
expand_with({'<-', Meta, [Left, Right]}, {S, E, _HasMatch}) ->
{ERight, SR, ER} = elixir_expand:expand(Right, S, E),
SM = elixir_env:reset_read(SR, S),
{[ELeft], SL, EL} = head([Left], SM, ER),
{{'<-', Meta, [ELeft, ERight]}, {SL, EL, true}};
expand_with(Expr, {S, E, HasMatch}) ->
{EExpr, SE, EE} = elixir_expand:expand(Expr, S, E),
{EExpr, {SE, EE, HasMatch}}.
expand_with({'<-', Meta, [{Name, _, Ctx}, _] = Args}, Acc) when is_atom(Name), is_atom(Ctx) ->
expand_with({'=', Meta, Args}, Acc);
expand_with({'<-', Meta, [Left, Right]}, {E, _HasMatch}) ->
{ERight, ER} = elixir_expand:expand(Right, E),
{[ELeft], EL} = head([Left], ER, E),
{{'<-', Meta, [ELeft, ERight]}, {EL, true}};
expand_with(Expr, {E, HasMatch}) ->
{EExpr, EE} = elixir_expand:expand(Expr, E),
{EExpr, {EE, HasMatch}}.
expand_with_do(Meta, Opts, S, Acc, E) ->
expand_with_do(Meta, Opts, E, Acc) ->
case lists:keytake(do, 1, Opts) of
{value, {do, Expr}, RestOpts} ->
{EExpr, SAcc, EAcc} = elixir_expand:expand(Expr, Acc, E),
{EExpr, RestOpts, elixir_env:merge_and_check_unused_vars(SAcc, S, EAcc)};
{EExpr, EAcc} = elixir_expand:expand(Expr, Acc),
{EExpr, RestOpts, elixir_env:merge_and_check_unused_vars(E, EAcc)};
false ->
form_error(Meta, E, elixir_expand, {missing_option, 'with', [do]})
end.
expand_with_else(Meta, Opts, S, E, HasMatch) ->
expand_with_else(Meta, Opts, E, HasMatch) ->
case lists:keytake(else, 1, Opts) of
{value, Pair, RestOpts} ->
if
@@ -202,21 +204,21 @@ expand_with_else(Meta, Opts, S, E, HasMatch) ->
true -> form_warn(Meta, ?key(E, file), ?MODULE, unmatchable_else_in_with)
end,
Fun = expand_head(Meta, 'with', 'else'),
{EPair, SE} = expand_clauses(Meta, 'with', Fun, Pair, S, E),
{[EPair], RestOpts, SE};
{EPair, EE} = expand_clauses(Meta, 'with', Fun, Pair, E),
{[EPair], RestOpts, EE};
false ->
{[], Opts, S}
{[], Opts, E}
end.
%% Try
'try'(Meta, [], _S, E) ->
'try'(Meta, [], E) ->
form_error(Meta, E, elixir_expand, {missing_option, 'try', [do]});
'try'(Meta, [{do, _}], _S, E) ->
'try'(Meta, [{do, _}], E) ->
form_error(Meta, E, elixir_expand, {missing_option, 'try', ['catch', 'rescue', 'after']});
'try'(Meta, Opts, _S, E) when not is_list(Opts) ->
'try'(Meta, Opts, E) when not is_list(Opts) ->
form_error(Meta, E, elixir_expand, {invalid_args, 'try'});
'try'(Meta, Opts, S, E) ->
'try'(Meta, Opts, E) ->
% TODO: Make this an error on v2.0
case Opts of
[{do, _}, {else, _}] ->
@@ -233,77 +235,76 @@ expand_with_else(Meta, Opts, S, E, HasMatch) ->
ok = assert_at_most_once('else', Opts, 0, RaiseError),
ok = assert_at_most_once('after', Opts, 0, RaiseError),
ok = warn_catch_before_rescue(Opts, Meta, E, false),
{Try, SA} = lists:mapfoldl(fun(X, SA) -> expand_try(Meta, X, SA, E) end, S, Opts),
{Try, SA, E}.
lists:mapfoldl(fun(X, Acc) -> expand_try(Meta, X, Acc) end, E, Opts).
expand_try(_Meta, {'do', Expr}, S, E) ->
{EExpr, SE, EE} = elixir_expand:expand(Expr, elixir_env:reset_unused_vars(S), E),
{{'do', EExpr}, elixir_env:merge_and_check_unused_vars(SE, S, EE)};
expand_try(_Meta, {'after', Expr}, S, E) ->
{EExpr, SE, EE} = elixir_expand:expand(Expr, elixir_env:reset_unused_vars(S), E),
{{'after', EExpr}, elixir_env:merge_and_check_unused_vars(SE, S, EE)};
expand_try(Meta, {'else', _} = Else, S, E) ->
expand_try(_Meta, {'do', Expr}, E) ->
{EExpr, EE} = elixir_expand:expand(Expr, elixir_env:reset_unused_vars(E)),
{{'do', EExpr}, elixir_env:merge_and_check_unused_vars(E, EE)};
expand_try(_Meta, {'after', Expr}, E) ->
{EExpr, EE} = elixir_expand:expand(Expr, elixir_env:reset_unused_vars(E)),
{{'after', EExpr}, elixir_env:merge_and_check_unused_vars(E, EE)};
expand_try(Meta, {'else', _} = Else, E) ->
Fun = expand_head(Meta, 'try', 'else'),
expand_clauses(Meta, 'try', Fun, Else, S, E);
expand_try(Meta, {'catch', _} = Catch, S, E) ->
expand_clauses_with_stacktrace(Meta, fun expand_catch/4, Catch, S, E);
expand_try(Meta, {'rescue', _} = Rescue, S, E) ->
expand_clauses_with_stacktrace(Meta, fun expand_rescue/4, Rescue, S, E);
expand_try(Meta, {Key, _}, _S, E) ->
expand_clauses(Meta, 'try', Fun, Else, E);
expand_try(Meta, {'catch', _} = Catch, E) ->
expand_clauses_with_stacktrace(Meta, fun expand_catch/3, Catch, E);
expand_try(Meta, {'rescue', _} = Rescue, E) ->
expand_clauses_with_stacktrace(Meta, fun expand_rescue/3, Rescue, E);
expand_try(Meta, {Key, _}, E) ->
form_error(Meta, E, ?MODULE, {unexpected_option, 'try', Key}).
expand_clauses_with_stacktrace(Meta, Fun, Clauses, S, E) ->
OldStacktrace = S#elixir_ex.stacktrace,
SS = S#elixir_ex{stacktrace=true},
{Ret, SE} = expand_clauses(Meta, 'try', Fun, Clauses, SS, E),
{Ret, SE#elixir_ex{stacktrace=OldStacktrace}}.
expand_clauses_with_stacktrace(Meta, Fun, Clauses, E) ->
OldContextualVars = ?key(E, contextual_vars),
ES = E#{contextual_vars := ['__STACKTRACE__' | OldContextualVars]},
{Ret, EE} = expand_clauses(Meta, 'try', Fun, Clauses, ES),
{Ret, EE#{contextual_vars := OldContextualVars}}.
expand_catch(_Meta, [_] = Args, S, E) ->
head(Args, S, E);
expand_catch(_Meta, [_, _] = Args, S, E) ->
head(Args, S, E);
expand_catch(Meta, _, _, E) ->
expand_catch(_Meta, [_] = Args, E) ->
head(Args, E);
expand_catch(_Meta, [_, _] = Args, E) ->
head(Args, E);
expand_catch(Meta, _, E) ->
Error = {wrong_number_of_args_for_clause, "one or two args", origin(Meta, 'try'), 'catch'},
form_error(Meta, E, ?MODULE, Error).
expand_rescue(Meta, [Arg], S, E) ->
case expand_rescue(Arg, S, E) of
{EArg, SA, EA} ->
{[EArg], SA, EA};
expand_rescue(Meta, [Arg], E) ->
case expand_rescue(Arg, E) of
{EArg, EA} ->
{[EArg], EA};
false ->
form_error(Meta, E, ?MODULE, invalid_rescue_clause)
end;
expand_rescue(Meta, _, _, E) ->
expand_rescue(Meta, _, E) ->
Error = {wrong_number_of_args_for_clause, "one argument", origin(Meta, 'try'), 'rescue'},
form_error(Meta, E, ?MODULE, Error).
%% rescue var
expand_rescue({Name, _, Atom} = Var, S, E) when is_atom(Name), is_atom(Atom) ->
match(fun elixir_expand:expand/3, Var, S, S, E);
expand_rescue({Name, _, Atom} = Var, E) when is_atom(Name), is_atom(Atom) ->
match(fun elixir_expand:expand/2, Var, E, E);
%% rescue var in _ => rescue var
expand_rescue({in, _, [{Name, _, VarContext} = Var, {'_', _, UnderscoreContext}]}, S, E)
expand_rescue({in, _, [{Name, _, VarContext} = Var, {'_', _, UnderscoreContext}]}, E)
when is_atom(Name), is_atom(VarContext), is_atom(UnderscoreContext) ->
expand_rescue(Var, S, E);
expand_rescue(Var, E);
%% rescue var in [Exprs]
expand_rescue({in, Meta, [Left, Right]}, S, E) ->
{ELeft, SL, EL} = match(fun elixir_expand:expand/3, Left, S, S, E),
{ERight, SR, ER} = elixir_expand:expand(Right, SL, EL),
expand_rescue({in, Meta, [Left, Right]}, E) ->
{ELeft, EL} = match(fun elixir_expand:expand/2, Left, E, E),
{ERight, ER} = elixir_expand:expand(Right, EL),
case ELeft of
{Name, _, Atom} when is_atom(Name), is_atom(Atom) ->
case normalize_rescue(ERight) of
false -> false;
Other -> {{in, Meta, [ELeft, Other]}, SR, ER}
Other -> {{in, Meta, [ELeft, Other]}, ER}
end;
_ ->
false
end;
%% rescue Error => _ in [Error]
expand_rescue(Arg, S, E) ->
expand_rescue({in, [], [{'_', [], ?key(E, module)}, Arg]}, S, E).
expand_rescue(Arg, E) ->
expand_rescue({in, [], [{'_', [], ?key(E, module)}, Arg]}, E).
normalize_rescue({'_', _, Atom} = N) when is_atom(Atom) -> N;
normalize_rescue(Atom) when is_atom(Atom) -> [Atom];
@@ -314,11 +315,11 @@ normalize_rescue(Other) ->
expand_head(Meta, Kind, Key) ->
fun
([{'when', _, [_, _, _ | _]}], _, E) ->
([{'when', _, [_, _, _ | _]}], E) ->
form_error(Meta, E, ?MODULE, {wrong_number_of_args_for_clause, "one argument", Kind, Key});
([_] = Args, S, E) ->
head(Args, S, E);
(_, _, E) ->
([_] = Args, E) ->
head(Args, E);
(_, E) ->
form_error(Meta, E, ?MODULE, {wrong_number_of_args_for_clause, "one argument", Kind, Key})
end.
@@ -326,27 +327,25 @@ expand_head(Meta, Kind, Key) ->
%% considering we have at maximum one entry.
expand_one(Meta, Kind, Key, Fun) ->
fun
([_] = Args, S, E) ->
Fun(Args, S, E);
(_, _, E) ->
([_] = Args, E) ->
Fun(Args, E);
(_, E) ->
form_error(Meta, E, ?MODULE, {wrong_number_of_args_for_clause, "one argument", Kind, Key})
end.
%% Expands all -> pairs in a given key but do not keep the overall vars.
expand_clauses(Meta, Kind, Fun, Clauses, S, E) ->
expand_clauses(Meta, Kind, Fun, Clauses, E) ->
NewKind = origin(Meta, Kind),
expand_clauses_origin(Meta, NewKind, Fun, Clauses, S, E).
expand_clauses_origin(Meta, NewKind, Fun, Clauses, E).
expand_clauses_origin(Meta, Kind, Fun, {Key, [_ | _] = Clauses}, S, E) ->
Transformer = fun(Clause, SA) ->
{EClause, SAcc, EAcc} =
clause(Meta, {Kind, Key}, Fun, Clause, elixir_env:reset_unused_vars(SA), E),
{EClause, elixir_env:merge_and_check_unused_vars(SAcc, SA, EAcc)}
expand_clauses_origin(Meta, Kind, Fun, {Key, [_ | _] = Clauses}, E) ->
Transformer = fun(Clause, Acc) ->
{EClause, EAcc} = clause(Meta, {Kind, Key}, Fun, Clause, elixir_env:reset_unused_vars(Acc)),
{EClause, elixir_env:merge_and_check_unused_vars(Acc, EAcc)}
end,
{Values, SE} = lists:mapfoldl(Transformer, S, Clauses),
{{Key, Values}, SE};
expand_clauses_origin(Meta, Kind, _Fun, {Key, _}, _, E) ->
{Values, EE} = lists:mapfoldl(Transformer, E, Clauses),
{{Key, Values}, EE};
expand_clauses_origin(Meta, Kind, _Fun, {Key, _}, E) ->
form_error(Meta, E, ?MODULE, {bad_or_missing_clauses, {Kind, Key}}).
assert_at_most_once(_Kind, [], _Count, _Fun) -> ok;
+16 -40
View File
@@ -41,25 +41,27 @@ file_to_path(File, Dest, Callback) when is_binary(File), is_binary(Dest) ->
%% more efficient strategy depending on the code snippet.
eval_forms(Forms, Args, E) ->
case (?key(E, module) == nil) andalso allows_fast_compilation(Forms) andalso
(not elixir_config:is_bootstrap()) of
true -> fast_compile(Forms, E);
false -> compile(Forms, Args, E)
case (?key(E, module) == nil) andalso allows_fast_compilation(Forms) of
true ->
{Result, _Binding, EE} = elixir:eval_forms(Forms, [], E),
{Result, EE};
false ->
compile(Forms, Args, E)
end.
compile(Quoted, ArgsList, E) ->
{Expanded, SE, EE} = elixir_expand:expand(Quoted, elixir_env:env_to_ex(E), E),
elixir_env:check_unused_vars(SE, EE),
Args = list_to_tuple(ArgsList),
{Expanded, EE} = elixir_expand:expand(Quoted, E),
elixir_env:check_unused_vars(EE),
{Module, Fun, Purgeable} =
elixir_erl_compiler:spawn(fun() -> spawned_compile(Expanded, E) end),
elixir_erl_compiler:spawn(fun spawned_compile/2, [Expanded, E]),
Args = list_to_tuple(ArgsList),
{dispatch(Module, Fun, Args, Purgeable), EE}.
spawned_compile(ExExprs, #{line := Line, file := File} = E) ->
{Vars, S} = elixir_env:env_to_erl(E),
{ErlExprs, _} = elixir_erl_pass:translate(ExExprs, erl_anno:new(Line), S),
{Vars, S} = elixir_env:env_to_scope(E),
{ErlExprs, _} = elixir_erl_pass:translate(ExExprs, S),
Module = retrieve_compiler_module(),
Fun = code_fun(?key(E, module)),
@@ -106,41 +108,16 @@ is_purgeable(Module, Binary) ->
allows_fast_compilation({'__block__', _, Exprs}) ->
lists:all(fun allows_fast_compilation/1, Exprs);
allows_fast_compilation({defmodule, _, [_, [{do, _}]]}) ->
allows_fast_compilation({defmodule, _, _}) ->
true;
allows_fast_compilation(_) ->
false.
fast_compile({'__block__', _, Exprs}, E) ->
lists:foldl(fun(Expr, _) -> fast_compile(Expr, E) end, nil, Exprs);
fast_compile({defmodule, Meta, [Mod, [{do, TailBlock}]]}, NoLineE) ->
E = NoLineE#{line := ?line(Meta)},
Block = {'__block__', Meta, [
{'=', Meta, [{result, Meta, ?MODULE}, TailBlock]},
{{'.', Meta, [elixir_utils, noop]}, Meta, []},
{result, Meta, ?MODULE}
]},
Expanded = case Mod of
{'__aliases__', _, _} ->
case elixir_aliases:expand_or_concat(Mod, E) of
Receiver when is_atom(Receiver) -> Receiver;
_ -> 'Elixir.Macro':expand(Mod, E)
end;
_ ->
'Elixir.Macro':expand(Mod, E)
end,
ContextModules = [Expanded | ?key(E, context_modules)],
elixir_module:compile(Expanded, Block, [], E#{context_modules := ContextModules}).
%% Bootstrapper
bootstrap() ->
{ok, _} = application:ensure_all_started(elixir),
elixir_config:static(#{bootstrap => true}),
elixir_config:put(bootstrap, true),
elixir_config:put(docs, false),
elixir_config:put(relative_paths, false),
elixir_config:put(ignore_module_conflict, true),
@@ -148,6 +125,7 @@ bootstrap() ->
elixir_config:put(parser_options, []),
{Init, Main} = bootstrap_files(),
[bootstrap_file(File) || File <- [<<"lib/elixir/lib/kernel.ex">> | Init]],
elixir_config:put(bootstrap, true),
elixir_config:put(docs, true),
[bootstrap_file(File) || File <- [<<"lib/elixir/lib/kernel.ex">> | Main]].
@@ -201,8 +179,6 @@ bootstrap_files() ->
<<"lib/elixir/lib/access.ex">>,
<<"lib/elixir/lib/io.ex">>,
<<"lib/elixir/lib/system.ex">>,
<<"lib/elixir/lib/code/formatter.ex">>,
<<"lib/elixir/lib/code/normalizer.ex">>,
<<"lib/elixir/lib/kernel/cli.ex">>,
<<"lib/elixir/lib/kernel/error_handler.ex">>,
<<"lib/elixir/lib/kernel/parallel_compiler.ex">>,
@@ -210,7 +186,7 @@ bootstrap_files() ->
]
}.
binary_to_path({{ModuleName, Binary}, _Info}, CompilePath) ->
binary_to_path({ModuleName, _ModuleMap, Binary}, CompilePath) ->
Path = filename:join(CompilePath, atom_to_list(ModuleName) ++ ".beam"),
case file:write_file(Path, Binary) of
ok -> Path;
+1 -10
View File
@@ -1,18 +1,9 @@
-module(elixir_config).
-compile({no_auto_import, [get/1]}).
-export([new/1, warn/2, serial/1]).
-export([static/1, is_bootstrap/0, identifier_tokenizer/0]).
-export([delete/1, put/2, get/1, get/2, update/2, get_and_put/2]).
-export([new/1, delete/1, put/2, get/1, get/2, update/2, get_and_put/2, warn/2, serial/1]).
-export([start_link/0, init/1, handle_call/3, handle_cast/2]).
-behaviour(gen_server).
static(Map) when is_map(Map) ->
persistent_term:put(?MODULE, maps:merge(persistent_term:get(?MODULE, #{}), Map)).
is_bootstrap() ->
maps:get(bootstrap, persistent_term:get(?MODULE, #{}), false).
identifier_tokenizer() ->
maps:get(identifier_tokenizer, persistent_term:get(?MODULE, #{}), 'Elixir.String.Tokenizer').
get(Key) ->
[{_, Value}] = ets:lookup(?MODULE, Key),
Value.
+19 -20
View File
@@ -162,10 +162,10 @@ store_definition(Kind, CheckClauses, Call, Body, Pos) ->
store_definition(Meta, Kind, CheckClauses, Name, Arity, DefaultsArgs, Guards, Body, File, ER) ->
Module = ?key(ER, module),
Tuple = {Name, Arity},
{S, E} = env_for_expansion(Kind, Tuple, ER),
E = env_for_expansion(Kind, Tuple, ER),
{Args, Defaults} = unpack_defaults(Kind, Meta, Name, DefaultsArgs, S, E),
Clauses = [elixir_clauses:def(Clause, S, E) ||
{Args, Defaults} = unpack_defaults(Kind, Meta, Name, DefaultsArgs, E),
Clauses = [elixir_clauses:def(Clause, E) ||
Clause <- def_to_clauses(Kind, Meta, Args, Guards, Body, E)],
DefaultsLength = length(Defaults),
@@ -181,10 +181,9 @@ store_definition(Meta, Kind, CheckClauses, Name, Arity, DefaultsArgs, Guards, Bo
Tuple.
env_for_expansion(Kind, Tuple, E) when Kind =:= defmacro; Kind =:= defmacrop ->
S = elixir_env:env_to_ex(E),
{S#elixir_ex{caller=true}, E#{function := Tuple}};
E#{function := Tuple, contextual_vars := ['__CALLER__']};
env_for_expansion(_Kind, Tuple, E) ->
{elixir_env:env_to_ex(E), E#{function := Tuple}}.
E#{function := Tuple, contextual_vars := []}.
retrieve_location(Location, Module) ->
{Set, _} = elixir_module:data_tables(Module),
@@ -260,26 +259,26 @@ store_definition(Check, Kind, Meta, Name, Arity, File, Module, Defaults, Clauses
%% Handling of defaults
unpack_defaults(Kind, Meta, Name, Args, S, E) ->
Expanded = expand_defaults(Args, S, E#{context := nil}),
unpack_expanded(Kind, Meta, Name, Expanded, [], []).
unpack_defaults(Kind, Meta, Name, Args, E) ->
Expanded = expand_defaults(Args, E#{context := nil}),
unpack_defaults(Kind, Meta, Name, Expanded, [], []).
unpack_expanded(Kind, Meta, Name, [{'\\\\', DefaultMeta, [Expr, _]} | T] = List, Acc, Clauses) ->
unpack_defaults(Kind, Meta, Name, [{'\\\\', DefaultMeta, [Expr, _]} | T] = List, Acc, Clauses) ->
Base = match_defaults(Acc, length(Acc), []),
{Args, Invoke} = extract_defaults(List, length(Base), [], []),
Clause = {Meta, Base ++ Args, [], {super, [{super, {Kind, Name}} | DefaultMeta], Base ++ Invoke}},
unpack_expanded(Kind, Meta, Name, T, [Expr | Acc], [Clause | Clauses]);
unpack_expanded(Kind, Meta, Name, [H | T], Acc, Clauses) ->
unpack_expanded(Kind, Meta, Name, T, [H | Acc], Clauses);
unpack_expanded(_Kind, _Meta, _Name, [], Acc, Clauses) ->
unpack_defaults(Kind, Meta, Name, T, [Expr | Acc], [Clause | Clauses]);
unpack_defaults(Kind, Meta, Name, [H | T], Acc, Clauses) ->
unpack_defaults(Kind, Meta, Name, T, [H | Acc], Clauses);
unpack_defaults(_Kind, _Meta, _Name, [], Acc, Clauses) ->
{lists:reverse(Acc), lists:reverse(Clauses)}.
expand_defaults([{'\\\\', Meta, [Expr, Default]} | Args], S, E) ->
{ExpandedDefault, _, _} = elixir_expand:expand(Default, S, E),
[{'\\\\', Meta, [Expr, ExpandedDefault]} | expand_defaults(Args, S, E)];
expand_defaults([Arg | Args], S, E) ->
[Arg | expand_defaults(Args, S, E)];
expand_defaults([], _S, _E) ->
expand_defaults([{'\\\\', Meta, [Expr, Default]} | Args], E) ->
{ExpandedDefault, _} = elixir_expand:expand(Default, E),
[{'\\\\', Meta, [Expr, ExpandedDefault]} | expand_defaults(Args, E)];
expand_defaults([Arg | Args], E) ->
[Arg | expand_defaults(Args, E)];
expand_defaults([], _E) ->
[].
extract_defaults([{'\\\\', _, [_Expr, Default]} | T], Counter, NewArgs, NewInvoke) ->
+59 -41
View File
@@ -2,9 +2,9 @@
%% This module access the information stored on the scope
%% by elixir_import and therefore assumes it is normalized (ordsets)
-module(elixir_dispatch).
-export([dispatch_import/6, dispatch_require/7,
-export([dispatch_import/5, dispatch_require/6,
require_function/5, import_function/4,
expand_import/7, expand_require/6,
expand_import/6, expand_require/5,
default_functions/0, default_macros/0, default_requires/0,
find_import/4, format_error/1]).
-include("elixir.hrl").
@@ -69,8 +69,7 @@ import_function(Meta, Name, Arity, E) ->
require_function(Meta, Receiver, Name, Arity, E) ->
Required = is_element(Receiver, ?key(E, requires)),
case is_macro({Name, Arity}, Receiver, Required) of
case is_element({Name, Arity}, get_macros(Receiver, Required)) of
true -> false;
false ->
elixir_env:trace({remote_function, Meta, Receiver, Name, Arity}, E),
@@ -87,47 +86,50 @@ remote_function(Meta, Receiver, Name, Arity, E) ->
%% Dispatches
dispatch_import(Meta, Name, Args, S, E, Callback) ->
dispatch_import(Meta, Name, Args, E, Callback) ->
Arity = length(Args),
case expand_import(Meta, {Name, Arity}, Args, S, E, [], false) of
case expand_import(Meta, {Name, Arity}, Args, E, [], false) of
{ok, Receiver, Quoted} ->
expand_quoted(Meta, Receiver, Name, Arity, Quoted, S, E);
expand_quoted(Meta, Receiver, Name, Arity, Quoted, E);
{ok, Receiver, NewName, NewArgs} ->
elixir_expand:expand({{'.', Meta, [Receiver, NewName]}, Meta, NewArgs}, S, E);
elixir_expand:expand({{'.', Meta, [Receiver, NewName]}, Meta, NewArgs}, E);
error ->
Callback()
end.
%% TODO: Remove this rewrite when we require Erlang/OTP 23+
dispatch_require(_Meta, 'Elixir.System', stacktrace, [], #elixir_ex{stacktrace=true} = S, E, _Callback) ->
{{'__STACKTRACE__', [], nil}, S, E};
dispatch_require(_Meta, 'Elixir.System', stacktrace, [], #{contextual_vars := Vars} = E, Callback) ->
case lists:member('__STACKTRACE__', Vars) of
true -> {{'__STACKTRACE__', [], nil}, E};
false -> Callback('Elixir.System', stacktrace, [])
end;
dispatch_require(Meta, Receiver, Name, Args, S, E, Callback) when is_atom(Receiver) ->
dispatch_require(Meta, Receiver, Name, Args, E, Callback) when is_atom(Receiver) ->
Arity = length(Args),
case elixir_rewrite:inline(Receiver, Name, Arity) of
{AR, AN} ->
Callback(AR, AN, Args);
false ->
case expand_require(Meta, Receiver, {Name, Arity}, Args, S, E) of
{ok, Receiver, Quoted} -> expand_quoted(Meta, Receiver, Name, Arity, Quoted, S, E);
case expand_require(Meta, Receiver, {Name, Arity}, Args, E) of
{ok, Receiver, Quoted} -> expand_quoted(Meta, Receiver, Name, Arity, Quoted, E);
error -> Callback(Receiver, Name, Args)
end
end;
dispatch_require(_Meta, Receiver, Name, Args, _S, _E, Callback) ->
dispatch_require(_Meta, Receiver, Name, Args, _E, Callback) ->
Callback(Receiver, Name, Args).
%% Macros expansion
expand_import(Meta, {Name, Arity} = Tuple, Args, S, E, Extra, External) ->
expand_import(Meta, {Name, Arity} = Tuple, Args, E, Extra, External) ->
Module = ?key(E, module),
Function = ?key(E, function),
Dispatch = find_dispatch(Meta, Tuple, Extra, E),
case Dispatch of
{import, _} ->
do_expand_import(Meta, Tuple, Args, Module, S, E, Dispatch);
do_expand_import(Meta, Tuple, Args, Module, E, Dispatch);
_ ->
AllowLocals = External orelse ((Function /= nil) andalso (Function /= Tuple)),
Local = AllowLocals andalso
@@ -142,17 +144,17 @@ expand_import(Meta, {Name, Arity} = Tuple, Args, S, E, Extra, External) ->
%% There is no local. Dispatch the import.
_ when Local == false ->
do_expand_import(Meta, Tuple, Args, Module, S, E, Dispatch);
do_expand_import(Meta, Tuple, Args, Module, E, Dispatch);
%% Dispatch to the local.
_ ->
elixir_env:trace({local_macro, Meta, Name, Arity}, E),
elixir_locals:record_local(Tuple, Module, Function, Meta, true),
{ok, Module, expand_macro_fun(Meta, Local, Module, Name, Args, S, E)}
{ok, Module, expand_macro_fun(Meta, Local, Module, Name, Args, E)}
end
end.
do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, S, E, Result) ->
do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, E, Result) ->
case Result of
{function, Receiver} ->
elixir_env:trace({imported_function, Meta, Receiver, Name, Arity}, E),
@@ -162,9 +164,9 @@ do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, S, E, Result) ->
check_deprecated(Meta, macro, Receiver, Name, Arity, E),
elixir_env:trace({imported_macro, Meta, Receiver, Name, Arity}, E),
elixir_locals:record_import(Tuple, Receiver, Module, ?key(E, function)),
{ok, Receiver, expand_macro_named(Meta, Receiver, Name, Arity, Args, S, E)};
{ok, Receiver, expand_macro_named(Meta, Receiver, Name, Arity, Args, E)};
{import, Receiver} ->
case expand_require([{required, true} | Meta], Receiver, Tuple, Args, S, E) of
case expand_require([{required, true} | Meta], Receiver, Tuple, Args, E) of
{ok, _, _} = Response -> Response;
error -> {ok, Receiver, Name, Args}
end;
@@ -177,16 +179,17 @@ do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, S, E, Result) ->
error
end.
expand_require(Meta, Receiver, {Name, Arity} = Tuple, Args, S, E) ->
Required =
(Receiver == ?key(E, module)) orelse required(Meta) orelse
is_element(Receiver, ?key(E, requires)),
expand_require(Meta, Receiver, {Name, Arity} = Tuple, Args, E) ->
Required = (Receiver == ?key(E, module)) orelse required(Meta) orelse is_element(Receiver, ?key(E, requires)),
case is_macro(Tuple, Receiver, Required) of
true ->
case is_element(Tuple, get_macros(Receiver, Required)) of
true when Required ->
check_deprecated(Meta, macro, Receiver, Name, Arity, E),
elixir_env:trace({remote_macro, Meta, Receiver, Name, Arity}, E),
{ok, Receiver, expand_macro_named(Meta, Receiver, Name, Arity, Args, S, E)};
{ok, Receiver, expand_macro_named(Meta, Receiver, Name, Arity, Args, E)};
true ->
Info = {unrequired_module, {Receiver, Name, length(Args)}},
elixir_errors:form_error(Meta, E, ?MODULE, Info);
false ->
check_deprecated(Meta, function, Receiver, Name, Arity, E),
error
@@ -194,9 +197,9 @@ expand_require(Meta, Receiver, {Name, Arity} = Tuple, Args, S, E) ->
%% Expansion helpers
expand_macro_fun(Meta, Fun, Receiver, Name, Args, S, E) ->
expand_macro_fun(Meta, Fun, Receiver, Name, Args, E) ->
Line = ?line(Meta),
EArg = {Line, S, E},
EArg = {Line, E},
try
apply(Fun, [EArg | Args])
@@ -208,19 +211,20 @@ expand_macro_fun(Meta, Fun, Receiver, Name, Args, S, E) ->
erlang:raise(Kind, Reason, prune_stacktrace(Stacktrace, MFA, Info, {ok, EArg}))
end.
expand_macro_named(Meta, Receiver, Name, Arity, Args, S, E) ->
expand_macro_named(Meta, Receiver, Name, Arity, Args, E) ->
ProperName = elixir_utils:macro_name(Name),
ProperArity = Arity + 1,
Fun = fun Receiver:ProperName/ProperArity,
expand_macro_fun(Meta, Fun, Receiver, Name, Args, S, E).
expand_macro_fun(Meta, Fun, Receiver, Name, Args, E).
expand_quoted(Meta, Receiver, Name, Arity, Quoted, S, E) ->
expand_quoted(Meta, Receiver, Name, Arity, Quoted, E) ->
Line = ?line(Meta),
Next = elixir_module:next_counter(?key(E, module)),
try
ToExpand = elixir_quote:linify_with_context_counter(Line, {Receiver, Next}, Quoted),
elixir_expand:expand(ToExpand, S, E)
elixir_expand:expand(
elixir_quote:linify_with_context_counter(Line, {Receiver, Next}, Quoted),
E)
catch
Kind:Reason:Stacktrace ->
MFA = {Receiver, elixir_utils:macro_name(Name), Arity+1},
@@ -287,6 +291,10 @@ prune_stacktrace([], _MFA, Info, _E) ->
%% ERROR HANDLING
format_error({unrequired_module, {Receiver, Name, Arity}}) ->
Module = elixir_aliases:inspect(Receiver),
io_lib:format("you must require ~ts before invoking the macro ~ts.~ts/~B",
[Module, Module, Name, Arity]);
format_error({macro_conflict, {Receiver, Name, Arity}}) ->
io_lib:format("call to local macro ~ts/~B conflicts with imported ~ts.~ts/~B, "
"please rename the local macro or remove the conflicting import",
@@ -301,13 +309,23 @@ format_error({deprecated, Mod, Fun, Arity, Message}) ->
%% INTROSPECTION
is_macro(_Tuple, _Module, false) ->
false;
is_macro(Tuple, Receiver, true) ->
try Receiver:'__info__'(macros) of
Macros -> is_element(Tuple, Macros)
%% Do not try to get macros from Erlang. Speeds up compilation a bit.
get_macros(erlang, _) -> [];
%% Module was not required, so we don't try to load it.
get_macros(Receiver, false) ->
case erlang:module_loaded(Receiver) of
true -> get_info(Receiver, macros);
false -> []
end;
%% If module was required, do a function call to force
%% the error handler in.
get_macros(Receiver, true) ->
try
Receiver:'__info__'(macros)
catch
error:_ -> false
error:_ -> []
end.
%% Deprecations checks only happen at the module body,

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