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
462 changed files with 18367 additions and 57135 deletions
+2 -2
View File
@@ -23,10 +23,10 @@ test_freebsd_task:
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
PATH: $PATH:/usr/local/lib/erlang24/bin
PATH: $PATH:/usr/local/lib/erlang22/bin
install_script:
- pkg install -y erlang-runtime24 git gmake
- pkg install -y erlang-runtime22 git gmake
- rm -rf .git
- gmake compile
+1 -2
View File
@@ -14,6 +14,5 @@
# Errors tests
assert_eval_raise: 3
],
normalize_bitstring_modifiers: false
]
]
+20
View File
@@ -0,0 +1,20 @@
### 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
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang/OTP versions (elixir --version):
* Operating system:
### Current behavior
Include code samples, errors and stacktraces if appropriate.
### Expected behavior
A short description on how you expect the code to behave.
-11
View File
@@ -1,11 +0,0 @@
---
blank_issues_enabled: true
contact_links:
- name: Discuss proposals
url: https://groups.google.com/g/elixir-lang-core
about: Send proposals for new ideas to our mailing list
- name: Ask questions and support
url: https://elixirforum.com/
about: Ask questions, provide support and more on Elixir Forum
-53
View File
@@ -1,53 +0,0 @@
---
name: Report an issue
description:
Tell us about something that is not working the way we (probably) intend
body:
- type: markdown
attributes:
value: >
Thank you for contributing to Elixir! :heart:
Please, do not use this form for guidance, questions or support.
Try instead in [Elixir Forum](https://elixirforum.com),
the [IRC Chat](https://web.libera.chat/#elixir),
[Stack Overflow](https://stackoverflow.com/questions/tagged/elixir),
[Slack](https://elixir-slackin.herokuapp.com),
[Discord](https://discord.gg/elixir) or in other online communities.
- type: textarea
id: elixir-and-otp-version
attributes:
label: Elixir and Erlang/OTP versions
description: Paste the output of `elixir --version` here.
validations:
required: true
- type: input
id: os
attributes:
label: Operating system
description: The operating system that this issue is happening on.
validations:
required: true
- type: textarea
id: current-behavior
attributes:
label: Current behavior
description: >
Include code samples, errors, and stacktraces if appropriate.
If reporting a bug, please include the reproducing steps.
validations:
required: true
- type: textarea
id: expected-behavior
attributes:
label: Expected behavior
description: A short description on how you expect the code to behave.
validations:
required: true
-6
View File
@@ -1,6 +0,0 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
+87
View File
@@ -0,0 +1,87 @@
name: builds.hex.pm
on:
push:
branches:
- main
- v*.*
tags:
- v*
env:
ELIXIR_OPTS: "--warnings-as-errors"
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
concurrency: builds_txt
jobs:
release_pre_built:
strategy:
fail-fast: true
max-parallel: 1
matrix:
include:
- otp: 23
otp_version: '23.3'
build_docs: build_docs
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- name: Get tags
run: git fetch --tags origin
- uses: ./.github/workflows/release_pre_built
with:
otp_version: ${{ matrix.otp_version }}
otp: ${{ matrix.otp }}
build_docs: ${{ matrix.build_docs }}
- name: Utils.sh
run: |
cat << 'EOF' > utils.sh
function purge_key() {
curl \
-X POST \
-H "Fastly-Key: ${FASTLY_KEY}" \
-H "Accept: application/json" \
-H "Content-Length: 0" \
"https://api.fastly.com/service/$1/purge/$2"
}
function purge() {
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
sleep 2
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
sleep 2
purge_key ${FASTLY_REPO_SERVICE_ID} $1
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
}
EOF
chmod +x utils.sh
- name: Upload Docs to S3
if: ${{ matrix.build_docs }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
run: |
source utils.sh
version=$(echo ${{ github.ref_name }} | sed -e 's/^v//g')
for f in doc/*; do
if [ -d "$f" ]; then
app=`echo $f | sed s/"doc\/"//`
tarball="${app}-${version}.tar.gz"
surrogate_key="docs/${app}-${version}"
tar -czf "${tarball}" -C "doc/${app}" .
aws s3 cp "${tarball}" "s3://${{ env.AWS_S3_BUCKET }}/docs/${tarball}" \
--cache-control "public,max-age=3600" \
--metadata "{\"surrogate-key\":\"${surrogate_key}\",\"surrogate-control\":\"public,max-age=604800\"}"
purge "${surrogate_key}"
fi
done
+14 -38
View File
@@ -8,35 +8,29 @@ env:
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
permissions:
contents: read
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.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
development: [false]
include:
- otp_release: OTP-25.0
otp_latest: true
- otp_release: OTP-24.3
- otp_release: OTP-24.0
- otp_release: OTP-23.3
- otp_release: OTP-23.0
- otp_release: master
development: true
- otp_release: maint
development: true
runs-on: ubuntu-18.04
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Erlang/OTP
run: |
cd $RUNNER_TEMP
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-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
@@ -45,52 +39,34 @@ jobs:
run: |
rm -rf .git
make compile
echo "$PWD/bin" >> $GITHUB_PATH
- name: Build info
run: bin/elixir --version
- name: Check format
run: make test_formatted && echo "All Elixir source code files are properly formatted."
- name: Run Dialyzer
- name: Dyalizer
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_latest }}
- name: Build docs
if: ${{ matrix.otp_latest }}
run: |
git config --global advice.detachedHead false
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
for branch in main v${EX_DOC_LATEST_STABLE_VERSION}; do
echo "Building docs with ExDoc ${branch}"
cd ..
git clone https://github.com/elixir-lang/ex_doc.git --branch ${branch} --depth 1
cd ex_doc
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
cd ../elixir/
make docs
rm -rf ../ex_doc/
done
if: matrix.otp_release == 'OTP-24.0'
test_windows:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
strategy:
matrix:
otp_release: ['23.3']
otp_release: ['22.0']
runs-on: windows-2019
steps:
- name: Configure Git
run: git config --global core.autocrlf input
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Cache Erlang/OTP package
uses: actions/cache@v3
uses: actions/cache@v2
with:
path: C:\Users\runneradmin\AppData\Local\Temp\chocolatey\erlang
key: OTP-${{ matrix.otp_release }}-windows-2019
@@ -113,9 +89,9 @@ jobs:
check_posix_compliant:
name: Check POSIX-compliant
runs-on: ubuntu-18.04
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Shellcheck
-72
View File
@@ -1,72 +0,0 @@
# #!/usr/bin/env elixir
[tag] = System.argv()
Mix.install([
{:req, "~> 0.2.1"},
{:jason, "~> 1.0"}
])
%{status: 200, body: release} =
Req.get!("https://api.github.com/repos/elixir-lang/elixir/releases/tags/#{tag}")
if release["draft"] do
raise "cannot notify a draft release"
end
## Notify on elixir-lang-ann
names_and_checksums =
for asset <- release["assets"],
name = asset["name"],
name =~ ~r/.sha\d+sum$/,
do: {name, Req.get!(asset["browser_download_url"]).body}
line_items =
for {name, checksum_and_name} <- Enum.sort(names_and_checksums) do
[checksum | _] = String.split(checksum_and_name, " ")
root = Path.rootname(name)
"." <> type = Path.extname(name)
" * #{root} - #{type} - #{checksum}\n"
end
mail = %{
"From" => "jose.valim@dashbit.co",
"To" => "elixir-lang-ann@googlegroups.com",
"Subject" => "Elixir #{tag} released",
"HtmlBody" => "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{line_items}",
"MessageStream" => "outbound"
}
if System.get_env("DRYRUN") do
IO.puts("MAIL")
IO.inspect(mail)
else
headers = %{
"X-Postmark-Server-Token" => System.fetch_env!("ELIXIR_LANG_ANN_TOKEN")
}
resp = Req.post!("https://api.postmarkapp.com/email", {:json, mail}, headers: headers)
IO.puts("#{resp.status} elixir-lang-ann\n#{inspect(resp.body)}")
end
## Notify on Elixir Forum
post = %{
"title" => "Elixir #{tag} released",
"raw" => "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{release["body"]}",
# Elixir News
"category" => 28
}
if System.get_env("DRYRUN") do
IO.puts("POST")
IO.inspect(post)
else
headers = %{
"api-key" => System.fetch_env!("ELIXIR_FORUM_TOKEN"),
"api-username" => "Elixir"
}
resp = Req.post!("https://elixirforum.com/posts.json", {:json, post}, headers: headers)
IO.puts("#{resp.status} Elixir Forum\n#{inspect(resp.body)}")
end
-28
View File
@@ -1,28 +0,0 @@
name: Notify
on:
release:
types:
- published
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-18.04
name: Notify
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- uses: erlef/setup-beam@v1
with:
otp-version: 24.3
elixir-version: 1.13.4
- name: Run Elixir script
env:
ELIXIR_FORUM_TOKEN: ${{ secrets.ELIXIR_FORUM_TOKEN }}
ELIXIR_LANG_ANN_TOKEN: ${{ secrets.ELIXIR_LANG_ANN_TOKEN }}
run: |
elixir .github/workflows/notify.exs ${{ github.ref_name }}
-96
View File
@@ -1,96 +0,0 @@
name: Release
on:
push:
tags:
- v*
env:
ELIXIR_OPTS: "--warnings-as-errors"
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
jobs:
create_draft_release:
permissions:
contents: write
runs-on: ubuntu-18.04
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Create draft release
run: |
gh release create \
--repo ${{ github.repository }} \
--title ${{ github.ref_name }} \
--notes '' \
--draft \
${{ github.ref_name }}
release_pre_built:
needs: create_draft_release
strategy:
fail-fast: true
matrix:
include:
- otp: 23
otp_version: 23.3
- otp: 24
otp_version: 24.3
- otp: 25
otp_version: 25.0
build_docs: build_docs
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp_version }}
version-type: strict
- name: Build Elixir Release
run: |
make Precompiled.zip
mv Precompiled.zip elixir-otp-${{ matrix.otp }}.zip
shasum -a 1 elixir-otp-${{ matrix.otp }}.zip > elixir-otp-${{ matrix.otp }}.zip.sha1sum
shasum -a 256 elixir-otp-${{ matrix.otp }}.zip > elixir-otp-${{ matrix.otp }}.zip.sha256sum
echo "$PWD/bin" >> $GITHUB_PATH
- name: Upload Pre-built
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload --clobber "${{ github.ref_name }}" \
elixir-otp-${{ matrix.otp }}.zip \
elixir-otp-${{ matrix.otp }}.zip.sha{1,256}sum
- name: Get latest stable ExDoc version
if: ${{ matrix.build_docs }}
run: |
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
echo "EX_DOC_LATEST_STABLE_VERSION=${EX_DOC_LATEST_STABLE_VERSION}" >> $GITHUB_ENV
- uses: actions/checkout@v3
if: ${{ matrix.build_docs }}
with:
repository: elixir-lang/ex_doc
ref: v${{ env.EX_DOC_LATEST_STABLE_VERSION }}
path: ex_doc
- name: Build ex_doc
if: ${{ matrix.build_docs }}
run: |
mv ex_doc ../ex_doc
cd ../ex_doc
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
cd ../elixir
- name: Build Docs
if: ${{ matrix.build_docs }}
run: |
make Docs.zip
shasum -a 1 Docs.zip > Docs.zip.sha1sum
shasum -a 256 Docs.zip > Docs.zip.sha256sum
- name: Upload Docs
if: ${{ matrix.build_docs }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload --clobber "${{ github.ref_name }}" \
Docs.zip \
Docs.zip.sha{1,256}sum
@@ -0,0 +1,51 @@
name: "Release pre built"
description: "Builds elixir release, ExDoc and generates docs"
inputs:
otp:
description: "The major OTP version"
otp_version:
description: "The exact OTP version (major.minor[.patch])"
build_docs:
description: "If docs have to be built or not"
runs:
using: "composite"
steps:
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ inputs.otp_version }}
version-type: strict
- name: Build Elixir Release
shell: bash
run: |
make Precompiled.zip
mv Precompiled.zip elixir-otp-${{ inputs.otp }}.zip
shasum -a 1 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha1sum
shasum -a 256 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha256sum
echo "$PWD/bin" >> $GITHUB_PATH
- name: Get latest stable ExDoc version
if: ${{ inputs.build_docs }}
shell: bash
run: |
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
echo "EX_DOC_LATEST_STABLE_VERSION=${EX_DOC_LATEST_STABLE_VERSION}" >> $GITHUB_ENV
- uses: actions/checkout@v3
if: ${{ inputs.build_docs }}
with:
repository: elixir-lang/ex_doc
ref: v${{ env.EX_DOC_LATEST_STABLE_VERSION }}
path: ex_doc
- name: Build ex_doc
if: ${{ inputs.build_docs }}
shell: bash
run: |
mv ex_doc ../ex_doc
cd ../ex_doc
../elixir/bin/mix do local.rebar --force, local.hex --force, deps.get, compile
cd ../elixir
- name: Build Docs
if: ${{ inputs.build_docs }}
shell: bash
run: |
make Docs.zip
shasum -a 1 Docs.zip > Docs.zip.sha1sum
shasum -a 256 Docs.zip > Docs.zip.sha256sum
+221 -425
View File
@@ -1,513 +1,309 @@
# Changelog for Elixir v1.14
# Changelog for Elixir v1.12
Elixir v1.14 brings many improvements to the debugging experience in Elixir
and data-type inspection. It also includes a new abstraction for easy
partitioning of processes called `PartitionSupervisor`, as well as improved
compilation times and error messages.
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.
Elixir v1.14 is the last version to support Erlang/OTP 23. Consider updating
to Erlang/OTP 24 or Erlang/OTP 25.
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+.
## `dbg`
## Scripting improvements: `Mix.install/2` and `System.trap_signal/3`
`Kernel.dbg/2` is a new macro that's somewhat similar to `IO.inspect/2`, but
specifically tailored for **debugging**.
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.
When called, it prints the value of whatever you pass to it, plus the debugged
code itself as well as its location. This code:
`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
# In my_file.exs
feature = %{name: :dbg, inspiration: "Rust"}
dbg(feature)
dbg(Map.put(feature, :in_version, "1.14.0"))
Mix.install([:jason])
IO.puts Jason.encode!(%{hello: :world})
```
Prints this:
`Mix.install/2` also performs protocol consolidation, which gives script developers an option to execute their code in the most performant format possible.
```shell
$ elixir my_file.exs
[my_file.exs:2: (file)]
feature #=> %{inspiration: "Rust", name: :dbg}
**Note:** `Mix.install/2` is currently experimental and it may change in future releases.
[my_file.exs:3: (file)]
Map.put(feature, :in_version, "1.14.0") #=> %{in_version: "1.14.0", inspiration: "Rust", name: :dbg}
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
```
`dbg/2` can do more. It's a macro, so it *understands Elixir code*. You can see
that when you pass a series of `|>` pipes to it. `dbg/2` will print the value
for every step of the pipeline. This code:
This is particularly useful when your tests get stuck and you want to know which one is the culprit.
```elixir
# In dbg_pipes.exs
__ENV__.file
|> String.split("/", trim: true)
|> List.last()
|> File.exists?()
|> dbg()
**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)
```
Prints this:
However, in Elixir v1.12 with Erlang/OTP 24:
```shell
$ elixir dbg_pipes.exs
[dbg_pipes.exs:5: (file)]
__ENV__.file #=> "/home/myuser/dbg_pipes.exs"
|> String.split("/", trim: true) #=> ["home", "myuser", "dbg_pipes.exs"]
|> List.last() #=> "dbg_pipes.exs"
|> File.exists?() #=> true
```
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)
```
### IEx and Prying
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.
`dbg/2` supports configurable backends. IEx automatically replaces the default
backend by one that halts the code execution with `IEx.Pry`, giving developers
the option to access local variables, imports, and more. This also works with
pipelines: if you pass a series of `|>` pipe calls to `dbg` (or pipe into it at the
end, like `|> dbg()`), you'll be able to step through every line in the pipeline.
## Stepped ranges
You can keep the default behaviour by passing the `--no-pry` option to IEx.
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.
## PartitionSupervisor
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.
`PartitionSupervisor` is a new module that implements a new supervisor type. The
partition supervisor is designed to help with situations where you have a single
supervised process that becomes a bottleneck. If that process's state can be
easily partitioned, then you can use `PartitionSupervisor` to supervise multiple
isolated copies of that process running concurrently, each assigned its own
partition.
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.
For example, imagine you have an `ErrorReporter` process that you use to report
errors to a monitoring service.
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).
```elixir
# Application supervisor:
children = [
# ...,
ErrorReporter
]
## Additional functions
Supervisor.start_link(children, strategy: :one_for_one)
```
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.
As the concurrency of your application goes up, the `ErrorReporter` process
might receive requests from many other processes and eventually become a
bottleneck. In a case like this, it could help to spin up multiple copies of the
`ErrorReporter` process under a `PartitionSupervisor`.
## v1.12.3 (2021-09-05)
```elixir
# Application supervisor
children = [
{PartitionSupervisor, child_spec: ErrorReporter, name: Reporters}
]
```
The `PartitionSupervisor` will spin up a number of processes equal to
`System.schedulers_online()` by default (most often one per core). Now, when
routing requests to `ErrorReporter` processes we can use a `:via` tuple and
route the requests through the partition supervisor.
```elixir
partitioning_key = self()
ErrorReporter.report({:via, PartitionSupervisor, {Reporters, partitioning_key}}, error)
```
Using `self()` as the partitioning key here means that the same process will
always report errors to the same `ErrorReporter` process, ensuring a form of
back-pressure. You can use any term as the partitioning key.
### A Common Example
A common and practical example of a good use case for `PartitionSupervisor` is
partitioning something like a `DynamicSupervisor`. When starting many processes
under it, a dynamic supervisor can be a bottleneck, especially if said processes
take a long time to initialize. Instead of starting a single `DynamicSupervisor`,
you can start multiple:
```elixir
children = [
{PartitionSupervisor, child_spec: DynamicSupervisor, name: MyApp.DynamicSupervisors}
]
Supervisor.start_link(children, strategy: :one_for_one)
```
Now you start processes on the dynamic supervisor for the right partition.
For instance, you can partition by PID, like in the previous example:
```elixir
DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {MyApp.DynamicSupervisors, self()}},
my_child_specification
)
```
## Improved errors on binaries and evaluation
Erlang/OTP 25 improved errors on binary construction and evaluation. These improvements
apply to Elixir as well. Before v1.14, errors when constructing binaries would
often be hard-to-debug generic "argument errors". With Erlang/OTP 25 and Elixir v1.14,
more detail is provided for easier debugging. This work is part of [EEP
54](https://www.erlang.org/eeps/eep-0054).
Before:
```elixir
int = 1
bin = "foo"
int <> bin
#=> ** (ArgumentError) argument error
```
Now:
```elixir
int = 1
bin = "foo"
int <> bin
#=> ** (ArgumentError) construction of binary failed:
#=> segment 1 of type 'binary':
#=> expected a binary but got: 1
```
## Slicing with steps
Elixir v1.12 introduced **stepped ranges**, which are ranges where you can
specify the "step":
```elixir
Enum.to_list(1..10//3)
#=> [1, 4, 7, 10]
```
Stepped ranges are particularly useful for numerical operations involving
vectors and matrices (see [Nx](https://github.com/elixir-nx/nx), for example).
However, the Elixir standard library was not making use of stepped ranges in its
APIs. Elixir v1.14 starts to take advantage of steps with support for stepped
ranges in a couple of functions. One of them is `Enum.slice/2`:
```elixir
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
Enum.slice(letters, 0..5//2)
#=> ["a", "c", "e"]
```
`binary_slice/2` (and `binary_slice/3` for completeness) has been added to the
`Kernel` module, that works with bytes and also support stepped ranges:
```elixir
binary_slice("Elixir", 1..5//2)
#=> "lxr"
```
## Expression-based inspection and `Inspect` improvements
In Elixir, it's conventional to implement the `Inspect` protocol for opaque
structs so that they're inspected with a special notation, resembling this:
```elixir
MapSet.new([:apple, :banana])
#MapSet<[:apple, :banana]>
```
This is generally done when the struct content or part of it is private and the
`%name{...}` representation would reveal fields that are not part of the public
API.
The downside of the `#name<...>` convention is that *the inspected output is not
valid Elixir code*. For example, you cannot copy the inspected output and paste
it into an IEx session.
Elixir v1.14 changes the convention for some of the standard-library structs.
The `Inspect` implementation for those structs now returns a string with a valid
Elixir expression that recreates the struct when evaluated. In the `MapSet`
example above, this is what we have now:
```elixir
fruits = MapSet.new([:apple, :banana])
MapSet.put(fruits, :pear)
#=> MapSet.new([:apple, :banana, :pear])
```
The `MapSet.new/1` expression evaluates to exactly the struct that we're
inspecting. This allows us to hide the internals of `MapSet`, while keeping
it as valid Elixir code. This expression-based inspection has been
implemented for `Version.Requirement`, `MapSet`, and `Date.Range`.
Finally, we have improved the `Inspect` protocol for structs so that
fields are inspected in the order they are declared in `defstruct`.
The option `:optional` has also been added when deriving the `Inspect`
protocol, giving developers more control over the struct representation.
See the updated documentation for `Inspect` for a general rundown on
the approaches and options available.
## v1.14.2 (2022-11-11)
### 1. Enhancements
### 1. Bug fixes
#### Elixir
* [Code] Add `Code.eval_quoted_with_env/4` with support for the `:prune_binding` option
#### ExUnit
* [ExUnit.Case] Allow test cases to not be registered on use
* [ExUnit.DocTest] Include `:doctest` and `:doctest_line` as meta tags
* [ExUnit.Formatter] Expose `ExUnit.Formatter.format_assertion_diff/4`
#### Mix
* [Mix] `Mix.install/2` accepts atoms as paths
### 2. Bug fixes
#### Elixir
* [Code.Formatter] Fix `size*unit` shortcut in bitstring
* [Kernel] Generate unique variables for macro expansion of `defguard`
* [Protocol] Expand `:for` in protocols with the appropriate env
#### ExUnit
* [ExUnit] Do not run duplicate cases on `ExUnit.run/1`
#### Mix
* [mix test] Ensure proper error message when there is no test directory
## v1.14.1 (2022-10-10)
### 1. Enhancements
#### Elixir
* [Kernel] Perform partial expansion of literals in module attributes
* [Kernel] Do not add compile-time dependencies for literals as defaults in `Application.compile_env/3` inside module attributes
* [Macro] Add `Macro.expand_literals/2` and `Macro.expand_literals/3`
* [System] Add `:close_stdin` to `System.shell/2`
#### Mix
* [mix test] Accept `--all-warnings` option
## 2. Bug fixes
#### Elixir
* [Kernel] Fix misleading warning when `:uniq` is given in `for` comprehensions and the result is unused
* [Kernel] Improve error message for when there is a conflicting struct and ignoring module conflict
* [Kernel] Do not delete `@enforce_keys` attribute after `defstruct` declaration
* [Kernel] Do not crash the checker on modules with missing `:debug_info` chunk
* [Macro] Fix error in `Macro.to_string/2` when converting an AST with `:erlang.binary_to_atom/2`
* [String] Fix `String.split/3` and `String.next_grapheme/1` returning invalid results on invalid UTF-8 encoding
* [System] Do not close stdin by default in `System.shell/2`
* [URI] Do not return `uri.port` as `:undefined` in certain cases in `URI.new/1`
#### ExUnit
* [ExUnit.DocTest] Do not crash when both `:moduledoc` and functions are specified in `:only`
* [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
* [CLI] Fix invalid argument handling when `--no-pry` is given
* [IEx.Autocomplete] Do not error autocompletion with module attribute
#### Mix
* [mix format] Do not cache inputs from `.formatter.exs` so they are properly re-evaluted on every call
* [Mix] Rename inconsistent `:exit_code` option to `:exit_status` on `Mix.raise/2`
## v1.14.0 (2022-09-01)
## 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] Support multi-line comments to EEx via `<%!-- --%>`
* [EEx] Add `EEx.tokenize/2`
* [EEx.Engine] Add `c:EEx.Engine.handle_text/3` callback that receives text metadata
* [EEx.Engine] Emit warnings for unused "do" expression in EEx
#### Elixir
* [Access] Add `Access.slice/1`
* [Application] Add `Application.compile_env/4` and `Application.compile_env!/3` to read the compile-time environment inside macros
* [Calendar] Support ISO8601 basic format parsing with `DateTime.from_iso8601/2`
* [Calendar] Add `day`/`hour`/`minute` on `add`/`diff` across different calendar modules
* [Code] Add `:normalize_bitstring_modifiers` to `Code.format_string!/2`
* [Code] Emit deprecation and type warnings for invalid options in on `Code.compile_string/2` and `Code.compile_quoted/2`
* [Code] Warn if an outdated lexical tracker is given on eval
* [Code] Add `Code.env_for_eval/1` and `Code.eval_quoted_with_env/3`
* [Code] Improve stacktraces from eval operations on Erlang/OTP 25+
* [Code.Fragment] Add support for `__MODULE__` in several functions
* [Code.Fragment] Support surround and context suggestions across multiple lines
* [Enum] Allow slicing with steps in `Enum.slice/2`
* [File] Support `dereference_symlinks: true` in `File.cp/3` and `File.cp_r/3`
* [Float] Do not show floats in scientific notation if below `1.0e16` and the fractional value is precisely zero
* [Float] Add `Float.min_finite/0` and `Float.max_finite/0`
* [Inspect] Improve error reporting when there is a faulty implementation of the `Inspect` protocol
* [Inspect] Allow `:optional` when deriving the Inspect protocol for hiding fields that match their default value
* [Inspect] Inspect struct fields in the order they are declared in `defstruct`
* [Inspect] Use expression-based inspection for `Date.Range`, `MapSet`, and `Version.Requirement`
* [IO] Support `Macro.Env` and keywords as stacktrace definitions in `IO.warn/2`
* [IO] Add `IO.ANSI.syntax_colors/0` and related configuration to be shared across IEx and `dbg`
* [Kernel] Add new `dbg/0-2` macro
* [Kernel] Allow any guard expression as the size of a bitstring in a pattern match
* [Kernel] Allow composite types with pins as the map key in a pattern match
* [Kernel] Print escaped version of control chars when they show up as unexpected tokens
* [Kernel] Warn on confusable non-ASCII identifiers
* [Kernel] Add `..` as a nullary operator that returns `0..-1//1`
* [Kernel] Implement Unicode Technical Standard #39 recommendations. In particular, we warn for confusable scripts and restrict identifiers to single-scripts or highly restrictive mixed-scripts
* [Kernel] Automatically perform NFC conversion of identifiers
* [Kernel] Add `binary_slice/2` and `binary_slice/3`
* [Kernel] Lazily expand module attributes to avoid compile-time deps
* [Kernel] Automatically cascade `generated: true` annotations on macro expansion
* [Keyword] Add `Keyword.from_keys/2` and `Keyword.replace_lazy/3`
* [List] Add `List.keysort/3` with support for a `sorter` function
* [Macro] Add `Macro.classify_atom/1` and `Macro.inspect_atom/2`
* [Macro] Add `Macro.path/2`
* [Macro.Env] Add `Macro.Env.prune_compile_info/1`
* [Map] Add `Map.from_keys/2` and `Map.replace_lazy/3`
* [MapSet] Add `MapSet.filter/2`, `MapSet.reject/2`, and `MapSet.symmetric_difference/2`
* [Node] Add `Node.spawn_monitor/2` and `Node.spawn_monitor/4`
* [Module] Support new `@after_verify` attribute for executing code whenever a module is verified
* [PartitionSupervisor] Add `PartitionSupervisor` that starts multiple isolated partitions of the same child for scalability
* [Path] Add `Path.safe_relative/1` and `Path.safe_relative_to/2`
* [Registry] Add `Registry.count_select/2`
* [Stream] Add `Stream.duplicate/2` and `Stream.transform/5`
* [String] Support empty lookup lists in `String.replace/3`, `String.split/3`, and `String.splitter/3`
* [String] Allow slicing with steps in `String.slice/2`
* [Task] Add `:zip_input_on_exit` option to `Task.async_stream/3`
* [Task] Store `:mfa` in the `Task` struct for reflection purposes
* [URI] Add `URI.append_query/2`
* [Version] Add `Version.to_string/1`
* [Version] Colorize `Version.Requirement` source in the `Inspect` protocol
* [Code] Add `Code.cursor_context/2` to return the context of a code snippet
* [Code] Do not add newlines around interpolation on code formatting. Note this means formatted code that has interpolation after the line length on Elixir v1.12 won't be considered as formatted on earlier Elixir versions
* [Code] Do not add brackets when keywords is used in the access syntax
* [Calendar] Support basic datetime format in `Calendar.ISO` parsing functions
* [Code] Improve evaluation performance on systems running on Erlang/OTP 24+
* [Date] Support steps via `Date.range/3`
* [DateTime] Add `offset` to `DateTime.to_iso8601/2` (now `to_iso8601/3`)
* [Enum] Add `Enum.count_until/2` and `Enum.count_until/3`
* [Enum] Add `Enum.product/1`
* [Enum] Add `Enum.zip_with/2`, `Enum.zip_with/3`, `Enum.zip_reduce/3`, and `Enum.zip_reduce/4`
* [Enum] Add support for functions as the second argument of `Enum.with_index/2`
* [Exception] Show `error_info` data for exceptions coming from Erlang
* [Float] Add `Float.pow/2`
* [Integer] Add `Integer.pow/2` and `Integer.extended_gcd/2`
* [IO] Add `IO.stream/0` and `IO.binstream/0` which default to STDIO with line orientation
* [List] Add default value for `List.first/1` and `List.last/1`
* [Kernel] Add `first..last//step` as support for stepped ranges
* [Kernel] Also warn for literal structs on `min/2` and `max/2`
* [Kernel] Add `Kernel.tap/2` and `Kernel.then/2`
* [Kernel] Do not add runtime dependencies to remotes in typespecs
* [Kernel] When there is an unused variable warning and there is a variable with the same name previously defined, suggest the user may have wanted to use the pin operator
* [Kernel] Improve error messages on invalid character right after a number
* [Kernel] Show removal and deprecated tips from Erlang/OTP
* [Macro] Add export dependencies on `Macro.struct!/2`
* [Macro] Support `:newline` to customize newlines escaping in `Macro.unescape_string/2`
* [Module] Raise on invalid `@dialyzer` attributes
* [Module] Add `Module.get_definition/2` and `Module.delete_definition/2`
* [Module] Allow `@on_load` to be a private function
* [Module] Validate `@dialyzer` related module attributes
* [Module] Add `Module.reserved_attributes/0` to list all reserved attributes by the language
* [Range] Add `Range.new/3` and `Range.size/1`
* [Regex] Add offset option to `Regex.scan/3` and `Regex.run/3`
* [Registry] Support `:compression` on `Registry` tables
* [Registry] Support `Registry.values/3` for reading values under a given key-pid pair
* [Stream] Add `Stream.zip_with/2` and `Stream.zip_with/3`
* [String] Add `:turkic` mode option to String case functions
* [String] Update to Unicode 13.0
* [System] Add `System.trap_signal/3` and `System.untrap_signal/2`
* [System] Add `System.shell/2` to invoke a command that is interpreted by the shell
* [Tuple] Add `Tuple.sum/1` and `Tuple.product/1`
* [URI] Support RFC3986 compliant encoding and decoding of queries via the `:rfc3986` option
#### ExUnit
* [ExUnit] Add `ExUnit.Callbacks.start_link_supervised!/2`
* [ExUnit] Add `ExUnit.run/1` to rerun test modules
* [ExUnit] Colorize summary in yellow with message when all tests are excluded
* [ExUnit] Display friendly error when test name is too long
* [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] Evaluate `--dot-iex` line by line
* [IEx] Add line-by-line evaluation of IEx breakpoints
* [IEx.Autocomplete] Autocomplete bitstrings modifiers (after `::` inside `<<...>>`)
* [IEx.Helpers] Allow an atom to be given to `pid/1`
* [IEx.Helpers] Support sigils in `h/1`
#### Logger
* [Logger] Add `Logger.put_process_level/2`
* [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 `:config_path` and `:lockfile` options to `Mix.install/2`
* [mix compile] Add `--no-optional-deps` to skip optional dependencies to test compilation works without optional dependencies
* [mix compile] Include column information on error diagnostics when possible
* [mix deps] `Mix.Dep.Converger` now tells which deps formed a cycle
* [mix do] Support `--app` option to restrict recursive tasks in umbrella projects
* [mix do] Allow using `+` as a task separator instead of comma
* [mix format] Support filename in `mix format -` when reading from stdin
* [mix format] Compile if `mix format` plugins are missing
* [mix new] Do not allow projects to be created with application names that conflict with multi-arg Erlang VM switches
* [mix profile] Return the return value of the profiled function
* [mix release] Make BEAM compression opt-in
* [mix release] Let `:runtime_config_path` accept `false` to skip the `config/runtime.exs`
* [mix test] Improve error message when suite fails due to coverage
* [mix test] Support `:test_elixirc_options` and default to not generating docs nor debug info chunk for tests
* [mix xref] Support `--group` flag in `mix xref graph`
* [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
#### Elixir
* [Calendar] Handle widths with "0" in them in `Calendar.strftime/3`
* [CLI] Improve errors on incorrect `--rpc-eval` usage
* [CLI] Return proper exit code on Windows
* [Code] Do not emit warnings when formatting code
* [Enum] Allow slices to overflow on both starting and ending positions
* [Kernel] Do not allow restricted characters in identifiers according to UTS39
* [Kernel] Define `__exception__` field as `true` when expanding exceptions in typespecs
* [Kernel] Warn if any of `True`, `False`, and `Nil` aliases are used
* [Kernel] Warn on underived `@derive` attributes
* [Kernel] Remove compile-time dependency from `defimpl :for`
* [Kernel] Track all arities on imported functions
* [Kernel] Fix equality in guards for dynamic ranges without steps
* [Module] Fix loop while unifying type variables
* [Protocol] Warn if a protocol has no definitions
* [Regex] Show list options when inspecting a Regex manually defined with `Regex.compile/2`
* [String] Allow slices to overflow on both starting and ending positions
* [System] Raise non-generic exception on missing env in `System.fetch_env!/1` to mirror map operations
* [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] Do not crash when diffing unknown bindings in guards
* [ExUnit] Properly print diffs when comparing improper lists with strings at the tail position
* [ExUnit] Add short hash to `tmp_dir` in ExUnit to avoid test name collision
* [ExUnit] Do not store logs in the CLI formatter (this reduces memory usage for suites with `capture_log`)
* [ExUnit] Run `ExUnit.after_suite/1` callback even when no tests run
* [ExUnit] Fix scenario where `setup` with imported function from within `describe` failed to compile
* [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] Disallow short-hand pipe after matches
* [IEx] Fix `exports/1` in IEx for long function names
* [IEx] Fix auto-completion inside remote shells
#### Mix
* [mix compile.elixir] Fix `--warnings-as-errors` when used with `--all-warnings`
* [mix compile.elixir] Ensure semantic recompilation cascades to path dependencies
* [mix compile.elixir] Lock the compiler to avoid concurrent usage
* [mix format] Do not add new lines if the formatted file is empty
* [mix format] Properly compile dependencies on `mix format`
* [mix release] Only set `RELEASE_MODE` after `env.{sh,bat}` are executed
* [mix release] Allow application mode configuration to cascade to dependencies
* [mix xref] Do not emit already consolidated warnings during `mix xref trace`
* [Mix] Do not start apps with `runtime: false` on `Mix.install/2`
* [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)
### 3. Soft-deprecations (no warnings emitted)
#### Elixir
* [File] Passing a callback as third argument to `File.cp/3` and `File.cp_r/3` is deprecated.
Instead pass the callback the `:on_conflict` key of a keyword list
* [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] Using `<%# ... %>` for comments is deprecated. Please use `<% # ... %>` or the new multi-line comments with `<%!-- ... --%>`
#### Logger
* [Logger] Deprecate `Logger.enable/1` and `Logger.disable/1` in favor of `Logger.put_process_level/2`
#### Mix
* [mix cmd] The `--app` option in `mix cmd CMD` is deprecated in favor of the more efficient `mix do --app app cmd CMD`
### 4. Hard deprecations
* [EEx.Engine] `use EEx.Engine` is deprecated in favor of explicit delegation
#### Elixir
* [Application] Calling `Application.get_env/3` and friends in the module body is now discouraged, use `Application.compile_env/3` instead
* [Bitwise] `use Bitwise` is deprecated, use `import Bitwise` instead
* [Bitwise] `~~~` is deprecated in favor of `bnot` for clarity
* [Kernel.ParallelCompiler] Returning a list or two-element tuple from `:each_cycle` is deprecated, return a `{:compile | :runtime, modules, warnings}` tuple instead
* [Kernel] Deprecate the operator `<|>` to avoid ambiguity with upcoming extended numerical operators
* [String] Deprecate passing a binary compiled pattern to `String.starts_with?/2`
#### Logger
* [Logger] Deprecate `$levelpad` on message formatting
* [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] `Mix.Tasks.Xref.calls/1` is deprecated in favor of compilation tracers
* [mix compile] The `:xref` compiler is deprecated and it has no effect. Please remove it from your mix.exs file.
### 5. Backwards incompatible changes
#### Mix
A backward incompatible change was found on Elixir v1.12.0 after the release. This was not planned and it is documented below:
* [mix local.rebar] Remove support for rebar2, which has not been updated in 5 years, and is no longer supported on recent Erlang/OTP versions
#### ExUnit
## v1.13
* [ExUnit.Formatter] `{:suite_finished, load_time, run_time}` message has been updated to `{:suite_finished, %{load: load_time, run: run_time, async: async_time}}`
The CHANGELOG for v1.13 releases can be found [in the v1.13 branch](https://github.com/elixir-lang/elixir/blob/v1.13/CHANGELOG.md).
## 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
+40 -23
View File
@@ -2,9 +2,8 @@ PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := 1.14/
CANONICAL ?= main/
DOCS_FORMAT ?= html
CANONICAL := 1.12/
CANONICAL ?= master/
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
ERL_MAKE := if [ -n "$(ERLC_OPTS)" ]; then ERL_COMPILER_OPTIONS=$(ERLC_OPTS) erl -make; else erl -make; fi
@@ -29,9 +28,9 @@ SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
#==> Functions
define CHECK_ERLANG_RELEASE
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 23)])' -s erlang halt | grep -q '^true'; \
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 22)])' -s erlang halt | grep -q '^true'; \
if [ $$? != 0 ]; then \
echo "At least Erlang/OTP 23.0 is required to build Elixir"; \
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -93,10 +92,10 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
echo "==> bootstrap (compile)"; \
$(ERL) -s elixir_compiler bootstrap -s erlang halt; \
fi
$(Q) "$(MAKE)" unicode
$(Q) $(MAKE) unicode
@ echo "==> elixir (compile)";
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
$(Q) "$(MAKE)" app
$(Q) $(MAKE) app
app: $(APP)
$(APP): lib/elixir/src/elixir.app.src lib/elixir/ebin VERSION $(GENERATE_APP)
@@ -106,7 +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/security.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))
@@ -128,7 +127,7 @@ install: compile
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/*; do \
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/$(BINDIR)/"; \
done
"$(MAKE)" install_man
$(MAKE) install_man
check_reproducible: compile
$(Q) echo "==> Checking for reproducible builds..."
@@ -146,7 +145,7 @@ check_reproducible: compile
$(Q) mv lib/iex/ebin/* lib/iex/tmp/ebin_reproducible/
$(Q) mv lib/logger/ebin/* lib/logger/tmp/ebin_reproducible/
$(Q) mv lib/mix/ebin/* lib/mix/tmp/ebin_reproducible/
SOURCE_DATE_EPOCH=$(call READ_SOURCE_DATE_EPOCH) "$(MAKE)" compile
SOURCE_DATE_EPOCH=$(call READ_SOURCE_DATE_EPOCH) $(MAKE) compile
$(Q) echo "Diffing..."
$(Q) bin/elixir lib/elixir/diff.exs lib/elixir/ebin/ lib/elixir/tmp/ebin_reproducible/
$(Q) bin/elixir lib/elixir/diff.exs lib/eex/ebin/ lib/eex/tmp/ebin_reproducible/
@@ -160,7 +159,7 @@ clean:
rm -rf ebin
rm -rf lib/*/ebin
rm -rf $(PARSER)
$(Q) "$(MAKE)" clean_residual_files
$(Q) $(MAKE) clean_residual_files
clean_elixir:
$(Q) rm -f lib/*/ebin/Elixir.*.beam
@@ -173,47 +172,55 @@ 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
$(Q) $(MAKE) clean_man
#==> Documentation tasks
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}")
COMPILE_DOCS = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
DOCS_FORMAT = html
DOCS_COMPILE = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
DOCS_CONFIG = bin/elixir lib/elixir/scripts/docs_config.exs "$(1)"
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
docs_elixir: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (elixir)"
$(Q) rm -rf doc/elixir
$(call COMPILE_DOCS,Elixir,elixir,Kernel,--config "lib/elixir/docs.exs")
$(call DOCS_COMPILE,Elixir,elixir,Kernel,--config "lib/elixir/scripts/elixir_docs.exs")
$(call DOCS_CONFIG,elixir)
docs_eex: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (eex)"
$(Q) rm -rf doc/eex
$(call COMPILE_DOCS,EEx,eex,EEx,--config "lib/mix/docs.exs")
$(call DOCS_COMPILE,EEx,eex,EEx,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,eex)
docs_mix: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (mix)"
$(Q) rm -rf doc/mix
$(call COMPILE_DOCS,Mix,mix,Mix,--config "lib/mix/docs.exs")
$(call DOCS_COMPILE,Mix,mix,Mix,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,mix)
docs_iex: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (iex)"
$(Q) rm -rf doc/iex
$(call COMPILE_DOCS,IEx,iex,IEx,--config "lib/mix/docs.exs")
$(call DOCS_COMPILE,IEx,iex,IEx,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,iex)
docs_ex_unit: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (ex_unit)"
$(Q) rm -rf doc/ex_unit
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit,--config "lib/mix/docs.exs")
$(call DOCS_COMPILE,ExUnit,ex_unit,ExUnit,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,ex_unit)
docs_logger: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (logger)"
$(Q) rm -rf doc/logger
$(call COMPILE_DOCS,Logger,logger,Logger,--config "lib/mix/docs.exs")
$(call DOCS_COMPILE,Logger,logger,Logger,--config "lib/elixir/scripts/mix_docs.exs")
$(call DOCS_CONFIG,logger)
../ex_doc/bin/ex_doc:
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
@@ -231,6 +238,16 @@ Precompiled.zip: build_man compile
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
@ echo "Precompiled file created $(CURDIR)/Precompiled.zip"
zips: Precompiled.zip Docs.zip
@ echo ""
@ echo "### Checksums"
@ echo ""
@ shasum -a 1 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
@ shasum -a 512 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
@ shasum -a 1 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
@ shasum -a 512 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
@ echo ""
#==> Test tasks
# If you modify this task, please update .cirrus.yml accordingly
@@ -269,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)"
@@ -287,7 +304,7 @@ PLT = .elixir.plt
$(PLT):
@ echo "==> Building PLT with Elixir's dependencies..."
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets crypto runtime_tools ftp tftp mnesia public_key asn1 sasl
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets crypto runtime_tools ftp tftp mnesia public_key asn1 hipe sasl
clean_plt:
$(Q) rm -f $(PLT)
@@ -326,4 +343,4 @@ install_man: build_man
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(MAN_PREFIX)/man1
"$(MAKE)" clean_man
$(MAKE) clean_man
-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.
+44 -73
View File
@@ -1,7 +1,6 @@
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/main/images/logo/logo.png#gh-light-mode-only" width="200" alt="Elixir">
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/main/images/logo/logo-dark.png#gh-dark-mode-only" width="200" alt="Elixir">
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=main)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amain+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=main)](https://cirrus-ci.com/github/elixir-lang/elixir)
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=master)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=master)](https://cirrus-ci.com/github/elixir-lang/elixir)
Elixir is a dynamic, functional language designed for building scalable
and maintainable applications.
@@ -12,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
@@ -27,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
@@ -88,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
@@ -115,7 +92,7 @@ in applications inside the `lib` folder:
You can run all tests in the root directory with `make test` and you can
also run tests for a specific framework `make test_#{APPLICATION}`, for example,
`make test_ex_unit`. If you just changed something in Elixir's standard
`make test_ex_unit`. If you just changed something in the Elixir's standard
library, you can run only that portion through `make test_stdlib`.
If you are changing just one file, you can choose to compile and run tests only
@@ -127,12 +104,6 @@ bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
bin/elixir lib/elixir/test/elixir/string_test.exs
```
You can also use the `LINE` env var to run a single test:
```sh
LINE=123 bin/elixir lib/elixir/test/elixir/string_test.exs
````
To recompile (including Erlang modules):
```sh
@@ -152,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
@@ -171,12 +142,12 @@ We outline our process below to clarify the roles of everyone involved.
All pull requests must be approved by two committers before being merged into
the repository. If any changes are necessary, the team will leave appropriate
comments requesting changes to the code. Unfortunately, we cannot guarantee a
comments requesting changes to the code. Unfortunately we cannot guarantee a
pull request will be merged, even when modifications are requested, as the Elixir
team will re-evaluate the contribution as it changes.
Committers may also push style changes directly to your branch. If you would
rather manage all changes yourself, you can disable the "Allow edits from maintainers"
rather manage all changes yourself, you can disable "Allow edits from maintainers"
feature when submitting your pull request.
The Elixir team may optionally assign someone to review a pull request.
@@ -195,8 +166,8 @@ to be installed and built alongside Elixir:
```sh
# After cloning and compiling Elixir, in its parent directory:
git clone https://github.com/elixir-lang/ex_doc.git
cd ex_doc && ../elixir/bin/mix do deps.get + compile
git clone git://github.com/elixir-lang/ex_doc.git
cd ex_doc && ../elixir/bin/mix do deps.get, compile
```
Now go back to Elixir's root directory and run:
@@ -219,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
@@ -234,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.
+15 -19
View File
@@ -4,19 +4,25 @@
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
4. Update "Compatibility and Deprecations" if a new OTP version is supported
5. Commit changes above with title "Release vVERSION", generate a new tag, and push it
5. Commit changes above with title "Release vVERSION" and generate a new tag
6. Wait until GitHub Actions publish artifacts to the draft release and the CI is green
6. Run `make clean test` to ensure all tests pass from scratch and the CI is green
7. Copy the relevant bits from /CHANGELOG.md to the GitHub release and publish it
7. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
8. Add the release to `elixir.csv` with the minimum supported OTP version (all releases), update `erlang.csv` to the latest supported OTP version, and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
8. Push branch and the new tag
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases, and include SHAs+CHANGELOG
10. Add the release to `elixir.csv` (all releases), update `erlang.csv` to the precompiled OTP version, and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
11. Send an e-mail to elixir-lang-ann@googlegroups.com with title "Elixir vVERSION released". The body should be a link to the Release page on GitHub and the checksums. If it is a security release, prefix the title with the `[security]` tag
## Creating a new vMAJOR.MINOR branch
@@ -26,24 +32,14 @@
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 main
### Back in master
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
1. Bump /VERSION file
2. Start new /CHANGELOG.md
3. Update tables in /SECURITY.md and in "Compatibility and Deprecations"
3. Update tables in /SECURITY.md in "Compatibility and Deprecations"
4. Commit "Start vMAJOR.MINOR+1"
## Changing supported Erlang/OTP versions
1. Update the table in Compatibility and Deprecations
2. Update `otp_release` checks in /Makefile and `/lib/elixir/src/elixir.erl`
3. Update CI workflows in `/.cirrus.yml`, `/.github/workflows/ci.yml`, and `/.github/workflows/releases.yml`
4. Remove `otp_release` version checks that are no longer needed
+9 -8
View File
@@ -4,19 +4,20 @@
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
Elixir version | Support
:------------- | :-----------------------------
1.14 | Bug fixes and security patches
1.13 | Security patches only
1.12 | Security patches only
1.11 | Security patches only
1.10 | 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
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
Security notifications [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
## Reporting a vulnerability
+1 -1
View File
@@ -1 +1 @@
1.14.2
1.12.3
+17 -35
View File
@@ -1,22 +1,20 @@
#!/bin/sh
set -e
ELIXIR_VERSION=1.14.2
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=""
@@ -100,29 +91,32 @@ LENGTH=$#
set -- "$@" -extra
while [ $I -le $LENGTH ]; do
# S counts to be shifted, C counts to be copied
S=0
C=0
S=1
case "$1" in
+iex)
C=1
set -- "$@" "$1"
MODE="iex"
;;
+elixirc)
C=1
set -- "$@" "$1"
MODE="elixirc"
;;
-v|--no-halt|--no-pry)
C=1
-v|--no-halt)
set -- "$@" "$1"
;;
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex)
C=2
S=2
set -- "$@" "$1" "$2"
;;
--rpc-eval)
C=3
S=3
set -- "$@" "$1" "$2" "$3"
;;
--detached)
echo "warning: the --detached option is deprecated" >&2
ERL="$ERL -detached"
;;
--hidden)
S=1
ERL="$ERL -hidden"
;;
--logger-otp-reports)
@@ -183,7 +177,6 @@ while [ $I -le $LENGTH ]; do
fi
;;
--werl)
S=1
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
;;
*)
@@ -196,13 +189,6 @@ while [ $I -le $LENGTH ]; do
;;
esac
while [ $I -le $LENGTH ] && [ $C -gt 0 ]; do
C=$((C - 1))
I=$((I + 1))
set -- "$@" "$1"
shift
done
I=$((I + S))
shift $S
done
@@ -224,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
+11 -33
View File
@@ -1,14 +1,10 @@
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
set ELIXIR_VERSION=1.14.2
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,12 +125,10 @@ 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)
if ""==!par:--dot-iex=! (set "parsElixir=!parsElixir! --dot-iex %1" && shift && goto startloop)
if ""==!par:--no-pry=! (set "parsElixir=!parsElixir! --no-pry" && goto startloop)
rem ******* ERLANG PARAMETERS **********************
if ""==!par:--boot=! (set "parsErlang=!parsErlang! -boot %1" && shift && goto startloop)
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var %1 %2" && shift && shift && goto startloop)
@@ -175,19 +162,10 @@ if %errorlevel% == 0 (
if not !runMode! == "iex" (
set beforeExtra=-noshell -s elixir start_cli !beforeExtra!
)
if defined ELIXIR_CLI_DRY_RUN (
if defined useWerl (
echo start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
echo "!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
if defined useWerl (
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
if defined useWerl (
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
exit /B %ERRORLEVEL%
: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
+3 -5
View File
@@ -7,11 +7,9 @@ Usage: $(basename "$0") [options] [.exs file] [data]
The following options are exclusive to IEx:
--dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
If FILE is empty, then no file will be loaded.
--remsh NAME Connects to a node using a remote shell.
--no-pry Doesn't start pry sessions when dbg/2 is called.
--dot-iex "PATH" Overrides default .iex.exs file and uses path instead;
path can be empty, then no file will be loaded
--remsh NAME Connects to a node using a remote shell
It accepts all other options listed by "elixir --help".
USAGE
+3 -5
View File
@@ -11,19 +11,17 @@ echo Usage: %~nx0 [options] [.exs file] [data]
echo.
echo The following options are exclusive to IEx:
echo.
echo --dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
echo Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
echo If FILE is empty, then no file will be loaded.
echo --dot-iex "PATH" Overrides default .iex.exs file and uses path instead;
echo path can be empty, then no file will be loaded
echo --remsh NAME Connects to a node using a remote shell
echo --werl Uses Erlang's Windows shell GUI (Windows only)
echo --no-pry Doesn't start pry sessions when dbg/2 is called.
echo.
echo Set the IEX_WITH_WERL environment variable to always use werl.
echo It accepts all other options listed by "elixir --help".
goto end
:run
if defined IEX_WITH_WERL (set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
call "%~dp0\elixir.bat" --no-halt --erl "-noshell -user Elixir.IEx.CLI" +iex %__ELIXIR_IEX_FLAGS% %*
:end
endlocal
+44 -106
View File
@@ -9,14 +9,14 @@ end
defmodule EEx do
@moduledoc ~S"""
EEx stands for Embedded Elixir.
Embedded Elixir allows you to embed Elixir code inside a string
in a robust way.
EEx stands for Embedded Elixir. It allows you to embed
Elixir code inside a string in a robust way.
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
"foo baz"
## API
This module provides three main APIs for you to use:
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
@@ -34,49 +34,21 @@ defmodule EEx do
above and is available to you if you want to provide your own
ways of handling the compiled template.
The APIs above support several options, documented below. You may
also pass an engine which customizes how the EEx code is compiled.
## Options
All functions in this module, unless otherwise noted, accept EEx-related
options. They are:
All functions in this module accept EEx-related options.
They are:
* `:file` - the file to be used in the template. Defaults to the given
file the template is read from or to `"nofile"` when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to `1`.
* `:indentation` - (since v1.11.0) an integer added to the column after every
new line. Defaults to `0`.
* `:engine` - the EEx engine to be used for compilation.
* `: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).
## Tags
EEx supports multiple tags, declared below:
<% Elixir expression: executes code but discards output %>
<%= Elixir expression: executes code and prints result %>
<%% EEx quotation: returns the contents inside the tag as is %>
<%!-- Comments: they are discarded from source --%>
EEx supports additional tags, that may be used by some engines,
but they do not have a meaning by default:
<%| ... %>
<%/ ... %>
## Engine
EEx has the concept of engines which allows you to modify or
@@ -85,10 +57,42 @@ defmodule EEx do
By default, `EEx` uses the `EEx.SmartEngine` that provides some
conveniences on top of the simple `EEx.Engine`.
### `EEx.SmartEngine`
### Tags
The smart engine uses EEx default rules and adds the `@` construct
for reading template assigns:
`EEx.SmartEngine` supports the following tags:
<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>
<%% EEx quotation - returns the contents inside %>
<%# Comments - they are discarded from source %>
All expressions that output something to the template
**must** use the equals sign (`=`). Since everything in
Elixir is an expression, there are no exceptions for this rule.
For example, while some template languages would special-case
`if` clauses, they are treated the same in EEx and
also require `=` in order to have their result printed:
<%= if true do %>
It is obviously true
<% else %>
This will never appear
<% end %>
To escape an EEx expression in EEx use `<%% content %>`. For example:
<%%= x + 3 %>
will be rendered as `<%= x + 3 %>`.
Note that different engines may have different rules
for each tag. Other tags may be added in future versions.
### Macros
`EEx.SmartEngine` also adds some macros to your template.
An example is the `@` macro which allows easy data access
in a template:
iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
"1"
@@ -101,16 +105,6 @@ defmodule EEx do
required by the template is not specified at compilation time.
"""
@type line :: non_neg_integer
@type column :: non_neg_integer
@type marker :: '=' | '/' | '|' | ''
@type metadata :: %{column: column, line: line}
@type token ::
{:comment, charlist, metadata}
| {:text, charlist, metadata}
| {:expr | :start_expr | :middle_expr | :end_expr, marker, charlist, metadata}
| {:eof, metadata}
@doc """
Generates a function definition from the given string.
@@ -118,9 +112,7 @@ defmodule EEx do
The `name` argument is the name that the generated function will have.
`template` is the string containing the EEx template. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template.
The supported `options` are described [in the module docs](#module-options).
template. `options` is a list of EEx compilation options (see the module documentation).
## Examples
@@ -152,13 +144,11 @@ defmodule EEx do
The `name` argument is the name that the generated function will have.
`file` is the path to the EEx template file. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template.
template. `options` is a list of EEx compilation options (see the module documentation).
This function is useful in case you have templates but
you want to precompile inside a module for speed.
The supported `options` are described [in the module docs](#module-options).
## Examples
# sample.eex
@@ -203,8 +193,6 @@ defmodule EEx do
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
iex> quoted = EEx.compile_string("<%= a + b %>")
@@ -215,14 +203,7 @@ defmodule EEx do
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
case tokenize(source, options) do
{:ok, tokens} ->
EEx.Compiler.compile(tokens, options)
{:error, message, %{column: column, line: line}} ->
file = options[:file] || "nofile"
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
end
EEx.Compiler.compile(source, options)
end
@doc """
@@ -238,8 +219,6 @@ defmodule EEx do
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
# sample.eex
@@ -262,8 +241,6 @@ defmodule EEx do
@doc """
Gets a string `source` and evaluate the values using the `bindings`.
The supported `options` are described [in the module docs](#module-options).
## Examples
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
@@ -280,8 +257,6 @@ defmodule EEx do
@doc """
Gets a `filename` and evaluate the values using the `bindings`.
The supported `options` are described [in the module docs](#module-options).
## Examples
# sample.eex
@@ -301,43 +276,6 @@ defmodule EEx do
do_eval(compiled, bindings, options)
end
@doc """
Tokenize the given contents according to the given options.
## Options
* `:line` - An integer to start as line. Default is 1.
* `:column` - An integer to start as column. Default is 1.
* `:indentation` - An integer that indicates the indentation. Default is 0.
* `:trim` - Tells the tokenizer to either trim the content or not. Default is false.
* `:file` - Can be either a file or a string "nofile".
## Examples
iex> EEx.tokenize('foo', line: 1, column: 1)
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
## Result
It returns `{:ok, [token]}` where a token is one of:
* `{:text, content, %{column: column, line: line}}`
* `{:expr, marker, content, %{column: column, line: line}}`
* `{:start_expr, marker, content, %{column: column, line: line}}`
* `{:middle_expr, marker, content, %{column: column, line: line}}`
* `{:end_expr, marker, content, %{column: column, line: line}}`
* `{:eof, %{column: column, line: line}}`
Or `{:error, message, %{column: column, line: line}}` in case of errors.
Note new tokens may be added in the future.
"""
@doc since: "1.14.0"
@spec tokenize(IO.chardata(), opts :: keyword) ::
{:ok, [token()]} | {:error, String.t(), metadata()}
def tokenize(contents, opts \\ []) do
EEx.Compiler.tokenize(contents, opts)
end
### Helpers
defp do_eval(compiled, bindings, options) do
+92 -326
View File
@@ -3,345 +3,78 @@ defmodule EEx.Compiler do
# When changing this setting, don't forget to update the docs for EEx
@default_engine EEx.SmartEngine
@h_spaces [?\s, ?\t]
@all_spaces [?\s, ?\t, ?\n, ?\r]
@doc """
Tokenize EEx contents.
"""
def tokenize(contents, opts) when is_binary(contents) do
tokenize(String.to_charlist(contents), opts)
end
def tokenize(contents, opts) when is_list(contents) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
trim = opts[:trim] || false
indentation = opts[:indentation] || 0
column = indentation + (opts[:column] || 1)
state = %{trim: trim, indentation: indentation, file: file}
{contents, line, column} =
(trim && trim_init(contents, line, column, state)) || {contents, line, column}
tokenize(contents, line, column, state, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, state, buffer, acc) do
tokenize(t, line, column + 3, state, [?%, ?< | buffer], acc)
end
defp tokenize('<%!--' ++ t, line, column, state, buffer, acc) do
case comment(t, line, column + 5, state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{:ok, new_line, new_column, rest, comments} ->
token = {:comment, Enum.reverse(comments), %{line: line, column: column}}
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
end
end
# TODO: Deprecate this on Elixir v1.18
defp tokenize('<%#' ++ t, line, column, state, buffer, acc) do
case expr(t, line, column + 3, state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{:ok, _, new_line, new_column, rest} ->
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, & &1)
end
end
defp tokenize('<%' ++ t, line, column, state, buffer, acc) do
{marker, t} = retrieve_marker(t)
case expr(t, line, column + 2 + length(marker), state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{: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}
end
marker =
if key in [:middle_expr, :end_expr] and marker != '' do
message =
"unexpected beginning of EEx tag \"<%#{marker}\" on \"<%#{marker}#{expr}%>\", " <>
"please remove \"#{marker}\""
:elixir_errors.erl_warn({line, column}, state.file, message)
''
else
marker
end
token = {key, marker, expr, %{line: line, column: column}}
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
end
end
defp tokenize('\n' ++ t, line, _column, state, buffer, acc) do
tokenize(t, line + 1, state.indentation + 1, state, [?\n | buffer], acc)
end
defp tokenize([h | t], line, column, state, buffer, acc) do
tokenize(t, line, column + 1, state, [h | buffer], acc)
end
defp tokenize([], line, column, _state, buffer, acc) do
eof = {:eof, %{line: line, column: column}}
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
end
defp trim_and_tokenize(rest, line, column, state, buffer, acc, fun) do
{rest, line, column, buffer} = trim_if_needed(rest, line, column, state, buffer)
acc = tokenize_text(buffer, acc)
tokenize(rest, line, column, state, [{line, column}], fun.(acc))
end
# Retrieve marker for <%
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
{[marker], t}
end
defp retrieve_marker(t) do
{'', t}
end
# Tokenize a multi-line comment until we find --%>
defp comment([?-, ?-, ?%, ?> | t], line, column, _state, buffer) do
{:ok, line, column + 4, t, buffer}
end
defp comment('\n' ++ t, line, _column, state, buffer) do
comment(t, line + 1, state.indentation + 1, state, '\n' ++ buffer)
end
defp comment([head | t], line, column, state, buffer) do
comment(t, line, column + 1, state, [head | buffer])
end
defp comment([], line, column, _state, _buffer) do
{:error, line, column, "missing token '--%>'"}
end
# Tokenize an expression until we find %>
defp expr([?%, ?> | t], line, column, _state, buffer) do
{:ok, Enum.reverse(buffer), line, column + 2, t}
end
defp expr('\n' ++ t, line, _column, state, buffer) do
expr(t, line + 1, state.indentation + 1, state, [?\n | buffer])
end
defp expr([h | t], line, column, state, buffer) do
expr(t, line, column + 1, state, [h | buffer])
end
defp expr([], line, column, _state, _buffer) do
{:error, line, column, "missing token '%>'"}
end
# 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
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
{:start_expr, expr}
else
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
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)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, buffer, %{line: line, column: column}} | acc]
end
## Trim
defp trim_if_needed(rest, line, column, state, buffer) do
if state.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0, state)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column, state) when h in @h_spaces,
do: trim_init(t, line, column + 1, state)
defp trim_init([?\r, ?\n | t], line, _column, state),
do: trim_init(t, line + 1, state.indentation + 1, state)
defp trim_init([?\n | t], line, _column, state),
do: trim_init(t, line + 1, state.indentation + 1, state)
defp trim_init([?<, ?% | _] = rest, line, column, _state),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, last_column, state) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, state.indentation + 1, column + 1, state)
{[?\n | rest], column} ->
trim_right(rest, line + 1, state.indentation + 1, column, state)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t], column) when h in @h_spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
@doc """
This is the compilation entry point. It glues the tokenizer
and the engine together by handling the tokens and invoking
the engine every time a full expression or text is received.
"""
@spec compile([EEx.token()], keyword) :: Macro.t()
def compile(tokens, opts) do
@spec compile(String.t(), keyword) :: Macro.t()
def compile(source, opts) when is_binary(source) and is_list(opts) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
engine = opts[:engine] || @default_engine
column = 1
indentation = opts[:indentation] || 0
trim = opts[:trim] || false
tokenizer_options = %{trim: trim, indentation: indentation}
state = %{
engine: engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: parser_options
}
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
{:ok, tokens} ->
state = %{
engine: opts[:engine] || @default_engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: Code.get_compiler_option(:parser_options)
}
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
end
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
# Ignore tokens related to comment.
defp generate_buffer([{:comment, _chars, _meta} | rest], buffer, scope, state) do
generate_buffer(rest, buffer, scope, state)
{:error, line, column, message} ->
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
end
end
# Generates the buffers by handling each expression from the tokenizer.
# It returns Macro.t/0 or it raises.
defp generate_buffer([{:text, chars, meta} | rest], buffer, scope, state) do
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
buffer =
if function_exported?(state.engine, :handle_text, 3) do
meta = [line: meta.line, column: meta.column]
meta = [line: line, column: column]
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
else
# TODO: Deprecate this branch on Elixir v1.18.
# We should most likely move this check to init to emit the deprecation once.
# TODO: Remove this branch on Elixir v2.0
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
end
generate_buffer(rest, buffer, scope, state)
end
defp generate_buffer([{:expr, mark, chars, meta} | rest], buffer, scope, state) do
options =
[file: state.file, line: meta.line, column: column(meta.column, mark)] ++
state.parser_options
defp generate_buffer([{:expr, line, column, mark, chars} | rest], buffer, scope, state) do
options = [file: state.file, line: line, column: column(column, mark)] ++ state.parser_options
expr = Code.string_to_quoted!(chars, options)
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
generate_buffer(rest, buffer, scope, state)
end
defp generate_buffer(
[{:start_expr, mark, chars, meta} | rest],
[{:start_expr, start_line, start_column, mark, chars} | rest],
buffer,
scope,
state
) do
if mark == '' do
if mark != '=' do
message =
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
:elixir_errors.erl_warn({meta.line, meta.column}, state.file, message)
:elixir_errors.erl_warn(start_line, state.file, message)
end
{rest, line, contents} = look_ahead_middle(rest, meta.line, chars) || {rest, meta.line, chars}
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
{contents, rest} =
generate_buffer(
@@ -352,8 +85,8 @@ defmodule EEx.Compiler do
state
| quoted: [],
line: line,
start_line: meta.line,
start_column: column(meta.column, mark)
start_line: start_line,
start_column: column(start_column, mark)
}
)
@@ -362,31 +95,47 @@ defmodule EEx.Compiler do
end
defp generate_buffer(
[{:middle_expr, '', chars, meta} | rest],
[{:middle_expr, line, _column, '', chars} | rest],
buffer,
[current | scope],
state
) do
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
state = %{state | line: meta.line}
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
state = %{state | line: line}
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], state)
end
defp generate_buffer([{:middle_expr, _, chars, meta} | _], _buffer, [], state) do
defp generate_buffer(
[{:middle_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
"please remove \"#{modifier}\" accordingly"
: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
end
defp generate_buffer([{:middle_expr, line, column, _, chars} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected middle of expression <%#{chars}%>",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
defp generate_buffer(
[{:end_expr, '', chars, meta} | rest],
[{:end_expr, line, _column, '', chars} | rest],
buffer,
[current | _],
state
) do
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
column = state.start_column
options = [file: state.file, line: state.start_line, column: column] ++ state.parser_options
tuples = Code.string_to_quoted!(wrapped, options)
@@ -394,24 +143,40 @@ defmodule EEx.Compiler do
{buffer, rest}
end
defp generate_buffer([{:end_expr, _, chars, meta} | _], _buffer, [], state) do
defp generate_buffer(
[{:end_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
: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
end
defp generate_buffer([{:end_expr, line, column, _, chars} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected end of expression <%#{chars}%>",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
defp generate_buffer([{:eof, _meta}], buffer, [], state) do
defp generate_buffer([{:eof, _, _}], buffer, [], state) do
state.engine.handle_body(buffer)
end
defp generate_buffer([{:eof, meta}], _buffer, _scope, state) do
defp generate_buffer([{:eof, line, column}], _buffer, _scope, state) do
raise EEx.SyntaxError,
message: "unexpected end of string, expected a closing '<% end %>'",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
# Creates a placeholder and wrap it inside the expression block
@@ -428,27 +193,28 @@ defmodule EEx.Compiler do
# Look middle expressions that immediately follow a start_expr
defp look_ahead_middle([{:comment, _comment, _meta} | rest], start, contents),
do: look_ahead_middle(rest, start, contents)
defp look_ahead_middle([{:text, text, _meta} | 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, _, chars, meta} | rest], _start, contents) do
{rest, meta.line, contents ++ chars}
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
{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
Enum.all?(chars, &(&1 in @all_spaces))
Enum.all?(chars, &(&1 in [?\s, ?\t, ?\r, ?\n]))
end
# Changes placeholder to real expression
+234
View File
@@ -0,0 +1,234 @@
defmodule EEx.Tokenizer do
@moduledoc false
@type content :: IO.chardata()
@type line :: non_neg_integer
@type column :: non_neg_integer
@type marker :: '=' | '/' | '|' | ''
@type token ::
{:text, line, column, content}
| {:expr | :start_expr | :middle_expr | :end_expr, line, column, marker, content}
| {:eof, line, column}
@spaces [?\s, ?\t]
@doc """
Tokenizes the given charlist or binary.
It returns {:ok, list} with the following tokens:
* `{:text, line, column, content}`
* `{:expr, line, column, marker, content}`
* `{:start_expr, line, column, marker, content}`
* `{:middle_expr, line, column, marker, content}`
* `{:end_expr, line, column, marker, content}`
* `{:eof, line, column}`
Or `{:error, line, column, message}` in case of errors.
"""
@spec tokenize(binary | charlist, line, column, map) ::
{:ok, [token]} | {:error, line, column, String.t()}
def tokenize(bin, line, column, opts) when is_binary(bin) do
tokenize(String.to_charlist(bin), line, column, opts)
end
def tokenize(list, line, column, opts)
when is_list(list) and is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
column = opts.indentation + column
{list, line, column} =
(opts.trim && trim_init(list, line, column, opts)) || {list, line, column}
tokenize(list, line, column, opts, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, opts, buffer, acc) do
tokenize(t, line, column + 3, opts, [?%, ?< | buffer], acc)
end
defp tokenize('<%#' ++ t, line, column, opts, buffer, acc) do
case expr(t, line, column + 3, opts, []) do
{:error, _, _, _} = error ->
error
{:ok, _, new_line, new_column, rest} ->
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], acc)
end
end
defp tokenize('<%' ++ t, line, column, opts, buffer, acc) do
{marker, t} = retrieve_marker(t)
case expr(t, line, column + 2 + length(marker), opts, []) do
{:error, _, _, _} = error ->
error
{:ok, expr, new_line, new_column, rest} ->
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, tokens} -> token_key(tokens, expr)
{:error, _, _, _} -> {:expr, expr}
end
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
final = {key, line, column, marker, expr}
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], [final | acc])
end
end
defp tokenize('\n' ++ t, line, _column, opts, buffer, acc) do
tokenize(t, line + 1, opts.indentation + 1, opts, [?\n | buffer], acc)
end
defp tokenize([h | t], line, column, opts, buffer, acc) do
tokenize(t, line, column + 1, opts, [h | buffer], acc)
end
defp tokenize([], line, column, _opts, buffer, acc) do
eof = {:eof, line, column}
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
end
# Retrieve marker for <%
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
{[marker], t}
end
defp retrieve_marker(t) do
{'', t}
end
# Tokenize an expression until we find %>
defp expr([?%, ?> | t], line, column, _opts, buffer) do
{:ok, Enum.reverse(buffer), line, column + 2, t}
end
defp expr('\n' ++ t, line, _column, opts, buffer) do
expr(t, line + 1, opts.indentation + 1, opts, [?\n | buffer])
end
defp expr([h | t], line, column, opts, buffer) do
expr(t, line, column + 1, opts, [h | buffer])
end
defp expr([], line, column, _opts, _buffer) do
{:error, line, column, "missing token '%>'"}
end
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens, expr) do
case {tokens, Enum.reverse(tokens)} do
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
{:start_expr, expr}
else
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
end
end
defp maybe_append_space([?\s]), do: [?\s]
defp maybe_append_space([h]), do: [h, ?\s]
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, line, column, buffer} | acc]
end
defp trim_if_needed(rest, line, column, opts, buffer) do
if opts.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0, opts)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column, opts) when h in @spaces,
do: trim_init(t, line, column + 1, opts)
defp trim_init([?\r, ?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?<, ?% | _] = rest, line, column, _opts),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, last_column, opts) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column + 1, opts)
{[?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column, opts)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t], column) when h in @spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
end
+120 -244
View File
@@ -2,67 +2,41 @@ Code.require_file("../test_helper.exs", __DIR__)
defmodule EEx.TokenizerTest do
use ExUnit.Case, async: true
require EEx.Tokenizer, as: T
@opts [indentation: 0, trim: false]
@opts %{indentation: 0, trim: false}
test "simple chars lists" do
assert EEx.tokenize('foo', @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
assert T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "simple strings" do
assert EEx.tokenize("foo", @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "strings with embedded code" do
assert EEx.tokenize('foo <% bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 14, line: 1}}
]}
assert T.tokenize('foo <% bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
end
test "strings with embedded equals code" do
assert EEx.tokenize('foo <%= bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '=', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded slash code" do
assert EEx.tokenize('foo <%/ bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '/', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%/ bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded pipe code" do
assert EEx.tokenize('foo <%| bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '|', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%| bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
end
test "strings with more than one line" do
assert EEx.tokenize('foo\n<%= bar %>', @opts) ==
{:ok,
[
{:text, 'foo\n', %{column: 1, line: 1}},
{:expr, '=', ' bar ', %{column: 1, line: 2}},
{:eof, %{column: 11, line: 2}}
]}
assert T.tokenize('foo\n<%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
end
test "strings with more than one line and expression with more than one line" do
@@ -74,235 +48,157 @@ defmodule EEx.TokenizerTest do
'''
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '=', ' bar\n\nbaz ', %{column: 5, line: 1}},
{:text, '\n', %{column: 7, line: 3}},
{:expr, '', ' foo ', %{column: 1, line: 4}},
{:text, '\n', %{column: 10, line: 4}},
{:eof, %{column: 1, line: 5}}
{:text, 1, 1, 'foo '},
{:expr, 1, 5, '=', ' bar\n\nbaz '},
{:text, 3, 7, '\n'},
{:expr, 4, 1, '', ' foo '},
{:text, 4, 10, '\n'},
{:eof, 5, 1}
]
assert EEx.tokenize(string, @opts) == {:ok, exprs}
assert T.tokenize(string, 1, 1, @opts) == {:ok, exprs}
end
test "quotation" do
assert EEx.tokenize('foo <%% true %>', @opts) ==
{:ok,
[
{:text, 'foo <% true %>', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
]}
assert T.tokenize('foo <%% true %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
end
test "quotation with do-end" do
assert EEx.tokenize('foo <%% true do %>bar<%% end %>', @opts) ==
{:ok,
[
{:text, 'foo <% true do %>bar<% end %>', %{column: 1, line: 1}},
{:eof, %{column: 32, line: 1}}
]}
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
test "quotation with interpolation" do
exprs = [
{:text, 'a <% b ', %{column: 1, line: 1}},
{:expr, '=', ' c ', %{column: 9, line: 1}},
{:text, ' ', %{column: 17, line: 1}},
{:expr, '=', ' d ', %{column: 18, line: 1}},
{:text, ' e %> f', %{column: 26, line: 1}},
{:eof, %{column: 33, line: 1}}
{:text, 1, 1, 'a <% b '},
{:expr, 1, 9, '=', ' c '},
{:text, 1, 17, ' '},
{:expr, 1, 18, '=', ' d '},
{:text, 1, 26, ' e %> f'},
{:eof, 1, 33}
]
assert EEx.tokenize('a <%% b <%= c %> <%= d %> e %> f', @opts) == {:ok, exprs}
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1, 1, @opts) == {:ok, exprs}
end
test "improperly formatted quotation with interpolation" do
exprs = [
{:text, '<%% a <%= b %> c %>', %{column: 1, line: 1}},
{:eof, %{column: 22, line: 1}}
{:text, 1, 1, '<%% a <%= b %> c %>'},
{:eof, 1, 22}
]
assert EEx.tokenize('<%%% a <%%= b %> c %>', @opts) == {:ok, exprs}
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments" do
test "eex comments" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
{:text, 1, 1, 'foo '},
{:eof, 1, 16}
]
assert EEx.tokenize('foo <%# true %>', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:eof, %{column: 8, line: 2}}
]
assert EEx.tokenize('foo <%#\ntrue %>', @opts) == {:ok, exprs}
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, 'foo ', %{column: 1, line: 1}},
{:text, 'bar', %{column: 19, line: 1}},
{:eof, %{column: 32, line: 1}}
{:text, 1, 1, 'foo '},
{:text, 1, 19, 'bar'},
{:eof, 1, 32}
]
assert EEx.tokenize('foo <%# true do %>bar<%# end %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments inside do-end" do
test "elixir comments" do
exprs = [
{:start_expr, '', ' if true do ', %{column: 1, line: 1}},
{:text, 'bar', %{column: 31, line: 1}},
{:end_expr, [], ' end ', %{column: 34, line: 1}},
{:eof, %{column: 43, line: 1}}
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
{:eof, 1, 35}
]
assert EEx.tokenize('<% if true do %><%# comment %>bar<% end %>', @opts) == {:ok, exprs}
exprs = [
{:start_expr, [], ' case true do ', %{column: 1, line: 1}},
{:middle_expr, '', ' true -> ', %{column: 33, line: 1}},
{:text, 'bar', %{column: 46, line: 1}},
{:end_expr, [], ' end ', %{column: 49, line: 1}},
{:eof, %{column: 58, line: 1}}
]
assert EEx.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', @opts) ==
{:ok, exprs}
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx multi-line comments" do
test "elixir comments with do/end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' true ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 20, line: 1}},
{:eof, %{column: 24, line: 1}}
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 1, 27, 'text'},
{:end_expr, 1, 31, [], ' end # closeif '},
{:eof, 1, 50}
]
assert EEx.tokenize('foo <%!-- true --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' \ntrue\n ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 6, line: 3}},
{:eof, %{column: 10, line: 3}}
]
assert EEx.tokenize('foo <%!-- \ntrue\n --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' <%= true %> ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 27, line: 1}},
{:eof, %{column: 31, line: 1}}
]
assert EEx.tokenize('foo <%!-- <%= true %> --%> bar', @opts) == {:ok, exprs}
end
test "Elixir comments" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, [], ' true # this is a boolean ', %{column: 5, line: 1}},
{:eof, %{column: 35, line: 1}}
]
assert EEx.tokenize('foo <% true # this is a boolean %>', @opts) == {:ok, exprs}
end
test "Elixir comments with do-end" do
exprs = [
{:start_expr, [], ' if true do # startif ', %{column: 1, line: 1}},
{:text, 'text', %{column: 27, line: 1}},
{:end_expr, [], ' end # closeif ', %{column: 31, line: 1}},
{:eof, %{column: 50, line: 1}}
]
assert EEx.tokenize('<% if true do # startif %>text<% end # closeif %>', @opts) ==
assert T.tokenize('<% if true do # startif %>text<% end # closeif %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with embedded do end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' if true do ', %{column: 5, line: 1}},
{:text, 'bar', %{column: 21, line: 1}},
{:end_expr, '', ' end ', %{column: 24, line: 1}},
{:eof, %{column: 33, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 1, 21, 'bar'},
{:end_expr, 1, 24, '', ' end '},
{:eof, 1, 33}
]
assert EEx.tokenize('foo <% if true do %>bar<% end %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <% if true do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
end
test "strings with embedded -> end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' cond do ', %{column: 5, line: 1}},
{:middle_expr, '', ' false -> ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 32, line: 1}},
{:middle_expr, '', ' true -> ', %{column: 35, line: 1}},
{:text, 'baz', %{column: 48, line: 1}},
{:end_expr, '', ' end ', %{column: 51, line: 1}},
{:eof, %{column: 60, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' cond do '},
{:middle_expr, 1, 18, '', ' false -> '},
{:text, 1, 32, 'bar'},
{:middle_expr, 1, 35, '', ' true -> '},
{:text, 1, 48, 'baz'},
{:end_expr, 1, 51, '', ' end '},
{:eof, 1, 60}
]
assert EEx.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', @opts) ==
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with fn-end with newline" do
test "strings with multiple callbacks" do
exprs = [
{:start_expr, '=', ' a fn ->\n', %{column: 1, line: 1}},
{:text, 'foo', %{column: 3, line: 2}},
{:end_expr, [], ' end ', %{column: 6, line: 2}},
{:eof, %{column: 15, line: 2}}
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end, fn -> '},
{:text, 1, 34, 'bar'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
assert EEx.tokenize('<%= a fn ->\n%>foo<% end %>', @opts) ==
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with multiple fn-end" do
test "strings with callback followed by do block" do
exprs = [
{:start_expr, '=', ' a fn -> ', %{column: 1, line: 1}},
{:text, 'foo', %{column: 15, line: 1}},
{:middle_expr, '', ' end, fn -> ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 34, line: 1}},
{:end_expr, '', ' end ', %{column: 37, line: 1}},
{:eof, %{column: 46, line: 1}}
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end do '},
{:text, 1, 30, 'bar'},
{:end_expr, 1, 33, '', ' end '},
{:eof, 1, 42}
]
assert EEx.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', @opts) ==
{:ok, exprs}
end
test "strings with fn-end followed by do block" do
exprs = [
{:start_expr, '=', ' a fn -> ', %{column: 1, line: 1}},
{:text, 'foo', %{column: 15, line: 1}},
{:middle_expr, '', ' end do ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 30, line: 1}},
{:end_expr, '', ' end ', %{column: 33, line: 1}},
{:eof, %{column: 42, line: 1}}
]
assert EEx.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', @opts) == {:ok, exprs}
assert T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
end
test "strings with embedded keywords blocks" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' if true do ', %{column: 5, line: 1}},
{:text, 'bar', %{column: 21, line: 1}},
{:middle_expr, '', ' else ', %{column: 24, line: 1}},
{:text, 'baz', %{column: 34, line: 1}},
{:end_expr, '', ' end ', %{column: 37, line: 1}},
{:eof, %{column: 46, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 1, 21, 'bar'},
{:middle_expr, 1, 24, '', ' else '},
{:text, 1, 34, 'baz'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
assert EEx.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', @opts) ==
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
@@ -310,61 +206,51 @@ defmodule EEx.TokenizerTest do
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> \n\n '
exprs = [
{:start_expr, '=', ' if true do ', %{column: 2, line: 1}},
{:text, '\n TRUE \n', %{column: 20, line: 1}},
{:middle_expr, '', ' else ', %{column: 3, line: 3}},
{:text, '\n FALSE \n', %{column: 13, line: 3}},
{:end_expr, '', ' end ', %{column: 3, line: 5}},
{:eof, %{column: 3, line: 7}}
{:start_expr, 1, 2, '=', ' if true do '},
{:text, 1, 20, '\n TRUE \n'},
{:middle_expr, 3, 3, '', ' else '},
{:text, 3, 13, '\n FALSE \n'},
{:end_expr, 5, 3, '', ' end '},
{:eof, 7, 3}
]
assert EEx.tokenize(template, [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize(template, 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode with comment" do
exprs = [
{:text, '\n123', %{column: 19, line: 1}},
{:eof, %{column: 4, line: 2}}
{:text, 1, 19, '\n123'},
{:eof, 2, 4}
]
assert EEx.tokenize(' <%# comment %> \n123', [trim: true] ++ @opts) == {:ok, exprs}
end
test "trim mode with multi-line comment" do
exprs = [
{:comment, ' comment ', %{column: 3, line: 1}},
{:text, '\n123', %{column: 23, line: 1}},
{:eof, %{column: 4, line: 2}}
]
assert EEx.tokenize(' <%!-- comment --%> \n123', [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize(' <%# comment %> \n123', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode with CRLF" do
exprs = [
{:text, '0\n', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 3, line: 2}},
{:text, '\n34', %{column: 15, line: 2}},
{:eof, %{column: 3, line: 3}}
{:text, 1, 1, '0\n'},
{:expr, 2, 3, '=', ' 12 '},
{:text, 2, 15, '\n34'},
{:eof, 3, 3}
]
assert EEx.tokenize('0\r\n <%= 12 %> \r\n34', [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode set to false" do
exprs = [
{:text, ' ', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 2, line: 1}},
{:text, ' \n', %{column: 11, line: 1}},
{:eof, %{column: 1, line: 2}}
{:text, 1, 1, ' '},
{:expr, 1, 2, '=', ' 12 '},
{:text, 1, 11, ' \n'},
{:eof, 2, 1}
]
assert EEx.tokenize(' <%= 12 %> \n', [trim: false] ++ @opts) == {:ok, exprs}
assert T.tokenize(' <%= 12 %> \n', 1, 1, %{@opts | trim: false}) == {:ok, exprs}
end
test "trim mode no false positives" do
assert_not_trimmed = fn x ->
assert EEx.tokenize(x, [trim: false] ++ @opts) == EEx.tokenize(x, @opts)
assert T.tokenize(x, 1, 1, %{@opts | trim: false}) == T.tokenize(x, 1, 1, @opts)
end
assert_not_trimmed.('foo <%= "bar" %> ')
@@ -374,22 +260,12 @@ defmodule EEx.TokenizerTest do
end
test "returns error when there is start mark and no end mark" do
assert EEx.tokenize('foo <% :bar', @opts) ==
{:error, "missing token '%>'", %{column: 12, line: 1}}
assert EEx.tokenize('<%# true ', @opts) ==
{:error, "missing token '%>'", %{column: 10, line: 1}}
assert EEx.tokenize('<%!-- foo ', @opts) ==
{:error, "missing token '--%>'", %{column: 11, line: 1}}
assert T.tokenize('foo <% :bar', 1, 1, @opts) == {:error, 1, 12, "missing token '%>'"}
assert T.tokenize('<%# true ', 1, 1, @opts) == {:error, 1, 10, "missing token '%>'"}
end
test "marks invalid expressions as regular expressions" do
assert EEx.tokenize('<% 1 $ 2 %>', @opts) ==
{:ok,
[
{:expr, [], ' 1 $ 2 ', %{column: 1, line: 1}},
{:eof, %{column: 12, line: 1}}
]}
assert T.tokenize('<% 1 $ 2 %>', 1, 1, @opts) ==
{:ok, [{:expr, 1, 1, [], ' 1 $ 2 '}, {:eof, 1, 12}]}
end
end
+1 -34
View File
@@ -198,24 +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 multi-line 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
@@ -233,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] %>"
@@ -317,7 +294,7 @@ defmodule EExTest do
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
EEx.compile_string("foo <%= if true do %>true<% else %>false<%= end %>")
end) =~
~s[unexpected beginning of EEx tag \"<%=\" on \"<%= end %>\"]
~s[unexpected beginning of EEx tag \"<%=\" on end of expression \"<%= end %>\"]
end
test "when trying to use marker '/' without implementation" do
@@ -741,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)
+1 -8
View File
@@ -1,8 +1 @@
{line_exclude, line_include} =
if line = System.get_env("LINE"), do: {[:test], [line: line]}, else: {[], []}
ExUnit.start(
trace: !!System.get_env("TRACE"),
include: line_include,
exclude: line_exclude
)
ExUnit.start(trace: "--trace" in System.argv())
+8 -122
View File
@@ -94,7 +94,8 @@ defmodule Access do
@type container :: keyword | struct | map
@type nil_container :: nil
@type t :: container | nil_container | any
@type any_container :: any
@type t :: container | nil_container | any_container
@type key :: any
@type value :: any
@@ -152,7 +153,7 @@ defmodule Access do
"""
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: value, data: container
when current_value: value, data: container | any_container
@doc """
Invoked to "pop" the value under `key` out of the given data structure.
@@ -166,18 +167,14 @@ defmodule Access do
See the implementations for `Map.pop/3` or `Keyword.pop/3` for more examples.
"""
@callback pop(data, key) :: {value, data} when data: container
@callback pop(data, key) :: {value, data} when data: container | any_container
defmacrop raise_undefined_behaviour(exception, module, top) do
quote do
exception =
case __STACKTRACE__ do
[unquote(top) | _] ->
reason =
"#{inspect(unquote(module))} does not implement the Access behaviour. " <>
"If you are using get_in/put_in/update_in, you can specify the field " <>
"to be accessed using Access.key!/1"
reason = "#{inspect(unquote(module))} does not implement the Access behaviour"
%{unquote(exception) | reason: reason}
_ ->
@@ -448,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: []
@@ -829,118 +829,4 @@ defmodule Access do
defp get_and_update_filter([], _func, _next, updates, gets) do
{:lists.reverse(gets), :lists.reverse(updates)}
end
@doc ~S"""
Returns a function that accesses all items of a list that are within the provided range.
The range will be normalized following the same rules from `Enum.slice/2`.
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
`Kernel.get_and_update_in/3`, and friends.
## Examples
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}, %{name: "vitor", salary: 25}]
iex> get_in(list, [Access.slice(1..2), :name])
["francine", "vitor"]
iex> get_and_update_in(list, [Access.slice(1..3//2), :name], fn prev ->
...> {prev, String.upcase(prev)}
...> end)
{["francine"], [%{name: "john", salary: 10}, %{name: "FRANCINE", salary: 30}, %{name: "vitor", salary: 25}]}
`slice/1` can also be used to pop elements out of a list or
a key inside of a list:
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}, %{name: "vitor", salary: 25}]
iex> pop_in(list, [Access.slice(-2..-1)])
{[%{name: "francine", salary: 30}, %{name: "vitor", salary: 25}], [%{name: "john", salary: 10}]}
iex> pop_in(list, [Access.slice(-2..-1), :name])
{["francine", "vitor"], [%{name: "john", salary: 10}, %{salary: 30}, %{salary: 25}]}
When no match is found, an empty list is returned and the update function is never called
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}, %{name: "vitor", salary: 25}]
iex> get_in(list, [Access.slice(5..10//2), :name])
[]
iex> get_and_update_in(list, [Access.slice(5..10//2), :name], fn prev ->
...> {prev, String.upcase(prev)}
...> end)
{[], [%{name: "john", salary: 10}, %{name: "francine", salary: 30}, %{name: "vitor", salary: 25}]}
An error is raised if the accessed structure is not a list:
iex> get_in(%{}, [Access.slice(2..10//3)])
** (ArgumentError) Access.slice/1 expected a list, got: %{}
An error is raised if the step of the range is negative:
iex> get_in([], [Access.slice(2..10//-1)])
** (ArgumentError) Access.slice/1 does not accept ranges with negative steps, got: 2..10//-1
"""
@doc since: "1.14"
@spec slice(Range.t()) :: access_fun(data :: list, current_value :: list)
def slice(%Range{} = range) do
if range.step > 0 do
fn op, data, next -> slice(op, data, range, next) end
else
raise ArgumentError,
"Access.slice/1 does not accept ranges with negative steps, got: #{inspect(range)}"
end
end
defp slice(:get, data, %Range{} = range, next) when is_list(data) do
data
|> Enum.slice(range)
|> Enum.map(next)
end
defp slice(:get_and_update, data, range, next) when is_list(data) do
range = normalize_range(range, data)
if range.first > range.last do
{[], data}
else
get_and_update_slice(data, range, next, [], [], 0)
end
end
defp slice(_op, data, _range, _next) do
raise ArgumentError, "Access.slice/1 expected a list, got: #{inspect(data)}"
end
defp normalize_range(%Range{first: first, last: last, step: step}, list)
when first < 0 or last < 0 do
count = length(list)
first = if first >= 0, do: first, else: Kernel.max(first + count, 0)
last = if last >= 0, do: last, else: last + count
Range.new(first, last, step)
end
defp normalize_range(range, _list), do: range
defp get_and_update_slice([head | rest], range, next, updates, gets, index) do
if index in range do
case next.(head) do
:pop ->
get_and_update_slice(rest, range, next, updates, [head | gets], index + 1)
{get, update} ->
get_and_update_slice(
rest,
range,
next,
[update | updates],
[get | gets],
index + 1
)
end
else
get_and_update_slice(rest, range, next, [head | updates], gets, index + 1)
end
end
defp get_and_update_slice([], _range, _next, updates, gets, _index) do
{:lists.reverse(gets), :lists.reverse(updates)}
end
end
+62 -123
View File
@@ -42,32 +42,18 @@ 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`.
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
module for directly accessing or modifying the environment of other applications.
> Note: The config files `config/config.exs` and `config/runtime.exs`
> are rarely used by libraries. Libraries typically define their environment
> in the `def application` function of their `mix.exs`. Configuration files
> are rather used by applications to configure their libraries.
> Note: Each application is responsible for its own environment. Do not
> use the functions in this module for directly accessing or modifying
> the environment of other applications. Whenever you change the application
> environment, Elixir's build tool will only recompile the files that
> belong to that application. So if you read the application environment
> of another application, there is a chance you will be depending on
> outdated configuration, as your file won't be recompiled as it changes.
## Compile-time environment
### Compile-time environment
In the previous example, we read the application environment at runtime:
@@ -85,48 +71,36 @@ defmodule Application do
will only be read when `MyApp.DBClient` effectively starts. While reading
the application environment at runtime is the preferred approach, in some
rare occasions you may want to use the application environment to configure
the compilation of a certain project. However, if you try to access
`Application.fetch_env!/2` outside of a function:
the compilation of a certain project. This is often done by calling `get_env/3`
outside of a function:
defmodule MyApp.DBClient do
@db_host Application.fetch_env!(:my_app, :db_host)
@db_host Application.get_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
You might see warnings and errors:
This approach has one big limitation: if you change the value of the
application environment after the code is compiled, the value used at
runtime is not going to change! For example, if your `config/runtime.exs`
has:
warning: Application.fetch_env!/2 is discouraged in the module body,
use Application.compile_env/3 instead
iex:3: MyApp.DBClient
config :my_app, :db_host, "db.production"
** (ArgumentError) could not fetch application environment :db_host
for application :my_app because the application was not loaded nor
configured
This value will have no effect as the code was compiled to connect to "db.local",
which is mostly likely unavailable in the production environment.
This happens because, when defining modules, the application environment
is not yet available. Luckily, the warning tells us how to solve this
issue, by using `Application.compile_env/3` instead:
For those reasons, reading the application environment at runtime should be the
first choice. However, if you really have to read the application environment
during compilation, we recommend you to use `compile_env/3` instead:
defmodule MyApp.DBClient do
@db_host Application.compile_env(:my_app, :db_host, "db.local")
@db_host Application.compile_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
The difference here is that `compile_env` expects the default value to be
given as an argument, instead of using the `def application` function of
your `mix.exs`. Furthermore, by using `compile_env/3`, tools like Mix will
store the values used during compilation and compare the compilation values
with the runtime values whenever your system starts, raising an error in
case they differ.
In any case, compile-time environments should be avoided. Whenever possible,
reading the application environment at runtime should be the first choice.
By using `compile_env/3`, tools like Mix will store the values used during
compilation and compare the compilation values with the runtime values whenever
your system starts, raising an error in case they differ.
## The application callback module
@@ -189,7 +163,7 @@ defmodule Application do
In the sections above, we have configured an application in the
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
this configuration to create an [*application resource
file*](https://www.erlang.org/doc/man/application.html), which is a file called
file*](https://erlang.org/doc/man/application.html), which is a file called
`APP_NAME.app`. For example, the application resource file of the OTP
application `ex_unit` is called `ex_unit.app`.
@@ -294,9 +268,9 @@ defmodule Application do
For further details on applications please check the documentation of the
[`:application` Erlang module](`:application`), and the
[Applications](https://www.erlang.org/doc/design_principles/applications.html)
[Applications](https://erlang.org/doc/design_principles/applications.html)
section of the [OTP Design Principles User's
Guide](https://www.erlang.org/doc/design_principles/users_guide.html).
Guide](https://erlang.org/doc/design_principles/users_guide.html).
"""
@doc """
@@ -411,7 +385,6 @@ defmodule Application do
:maxT,
:registered,
:included_applications,
:optional_applications,
:applications,
:mod,
:start_phases
@@ -525,34 +498,33 @@ defmodule Application do
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
# compile time instead of compile_env
@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
key_or_path = Macro.expand_literals(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.compile_env(__ENV__, unquote(app), unquote(key_or_path), unquote(default))
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
end
end
@doc """
Reads the application environment at compilation time from a macro.
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
Typically, developers will use `compile_env/3`. This function must
only be invoked from macros which aim to read the compilation environment
dynamically.
defp expand_key_or_path(list, env) when is_list(list),
do: Enum.map(list, &expand_key_or_path(&1, env))
It expects a `Macro.Env` as first argument, where the `Macro.Env` is
typically the `__CALLER__` in a macro. It raises if `Macro.Env` comes
from a function.
"""
@doc since: "1.14.0"
@spec compile_env(Macro.Env.t(), app, key | list, value) :: value
def compile_env(%Macro.Env{} = env, app, key_or_path, default) do
defp expand_key_or_path(other, _env),
do: other
@doc false
def __compile_env__(app, key_or_path, default, env) do
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} -> value
:error -> default
@@ -567,33 +539,20 @@ 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
key_or_path = Macro.expand_literals(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.compile_env!(__ENV__, unquote(app), unquote(key_or_path))
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
end
end
@doc """
Reads the application environment at compilation time from a macro
or raises.
Typically, developers will use `compile_env!/2`. This function must
only be invoked from macros which aim to read the compilation environment
dynamically.
It expects a `Macro.Env` as first argument, where the `Macro.Env` is
typically the `__CALLER__` in a macro. It raises if `Macro.Env` comes
from a function.
"""
@doc since: "1.14.0"
@spec compile_env!(Macro.Env.t(), app, key | list) :: value
def compile_env!(%Macro.Env{} = env, app, key_or_path) do
@doc false
def __compile_env__!(app, key_or_path, env) do
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} ->
value
@@ -605,9 +564,8 @@ defmodule Application do
end
end
defp fetch_compile_env(app, key, env) when is_atom(key) do
fetch_compile_env(app, key, [], env)
end
defp fetch_compile_env(app, key, env) when is_atom(key),
do: fetch_compile_env(app, key, [], env)
defp fetch_compile_env(app, [key | paths], env) when is_atom(key),
do: fetch_compile_env(app, key, paths, env)
@@ -632,13 +590,14 @@ defmodule Application do
If the configuration parameter does not exist, the function returns the
`default` value.
> **Important:** you must use this function to read only your own application
> environment. Do not read the environment of other applications.
**Important:** if you are reading the application environment at compilation
time, for example, inside the module definition instead of inside of a
function, see `compile_env/3` instead.
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. For more information,
> read our [library guidelines](library-guidelines.md).
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. For more information,
read our [library guidelines](library-guidelines.md).
## Examples
@@ -665,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
@@ -688,14 +639,6 @@ defmodule Application do
Returns the value for `key` in `app`'s environment in a tuple.
If the configuration parameter does not exist, the function returns `:error`.
> **Important:** you must use this function to read only your own application
> environment. Do not read the environment of other applications.
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. For more information,
> read our [library guidelines](library-guidelines.md).
"""
@spec fetch_env(app, key) :: {:ok, value} | :error
def fetch_env(app, key) when is_atom(app) do
@@ -712,13 +655,9 @@ defmodule Application do
If the configuration parameter does not exist, raises `ArgumentError`.
> **Important:** you must use this function to read only your own application
> environment. Do not read the environment of other applications.
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. For more information,
> read our [library guidelines](library-guidelines.md).
**Important:** if you are reading the application environment at compilation
time, for example, inside the module definition instead of inside of a
function, see `compile_env!/2` instead.
"""
@spec fetch_env!(app, key) :: value
def fetch_env!(app, key) when is_atom(app) do
@@ -824,7 +763,7 @@ defmodule Application do
@doc """
Ensures the given `app` is loaded.
Same as `load/1` but returns `:ok` if the application was already
Same as `load/2` but returns `:ok` if the application was already
loaded.
"""
@doc since: "1.10.0"
+1 -1
View File
@@ -56,7 +56,7 @@ defmodule Atom do
"""
@spec to_string(atom) :: String.t()
def to_string(atom) do
:erlang.atom_to_binary(atom)
:erlang.atom_to_binary(atom, :utf8)
end
@doc """
+674 -600
View File
File diff suppressed because it is too large Load Diff
+45 -9
View File
@@ -3,11 +3,31 @@ defmodule Bitwise do
A set of functions that perform calculations on bits.
All bitwise functions work only on integers; otherwise an
`ArithmeticError` is raised. The functions `band/2`,
`bor/2`, `bsl/2`, and `bsr/2` also have operators,
respectively: `&&&/2`, `|||/2`, `<<</2`, and `>>>/2`.
`ArithmeticError` is raised.
## Guards
The functions in this module come in two flavors: named or
operators. For example:
iex> use Bitwise
iex> bnot(1) # named
-2
iex> 1 &&& 1 # operator
1
If you prefer to use only operators or skip them, you can
pass the following options:
* `:only_operators` - includes only operators
* `:skip_operators` - skips operators
For example:
iex> use Bitwise, only_operators: true
iex> 1 &&& 1
1
When invoked with no options, `use Bitwise` is equivalent
to `import Bitwise`.
All bitwise functions can be used in guards:
@@ -22,7 +42,6 @@ defmodule Bitwise do
"""
@doc false
@deprecated "import Bitwise instead"
defmacro __using__(options) do
except =
cond do
@@ -30,7 +49,7 @@ defmodule Bitwise do
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
Keyword.get(options, :skip_operators) ->
["~~~": 1, &&&: 2, |||: 2, "^^^": 2, <<<: 2, >>>: 2]
[~~~: 1, &&&: 2, |||: 2, ^^^: 2, <<<: 2, >>>: 2]
true ->
[]
@@ -61,8 +80,25 @@ defmodule Bitwise do
:erlang.bnot(expr)
end
@doc false
def unquote(:"~~~")(expr) do
@doc """
Bitwise NOT unary operator.
Calculates the bitwise NOT of the argument.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> ~~~2
-3
iex> ~~~2 &&& 3
1
"""
@doc guard: true
@spec ~~~integer :: integer
def ~~~expr do
:erlang.bnot(expr)
end
@@ -156,7 +192,7 @@ defmodule Bitwise do
end
@doc false
def unquote(:"^^^")(left, right) do
def unquote(:^^^)(left, right) do
:erlang.bxor(left, right)
end
+6 -6
View File
@@ -57,7 +57,7 @@ defmodule Calendar do
representing the microseconds to external format. If the precision is 0,
it means microseconds must be skipped.
"""
@type microsecond :: {value :: non_neg_integer, precision :: non_neg_integer}
@type microsecond :: {non_neg_integer, non_neg_integer}
@typedoc "A calendar implementation"
@type calendar :: module
@@ -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`.
@@ -365,7 +365,7 @@ defmodule Calendar do
"""
@doc since: "1.8.0"
@spec put_time_zone_database(time_zone_database()) :: :ok
def put_time_zone_database(database) when is_atom(database) do
def put_time_zone_database(database) do
Application.put_env(:elixir, :time_zone_database, database)
end
@@ -375,7 +375,7 @@ defmodule Calendar do
@doc since: "1.8.0"
@spec get_time_zone_database() :: time_zone_database()
def get_time_zone_database() do
Application.fetch_env!(:elixir, :time_zone_database)
Application.get_env(:elixir, :time_zone_database, Calendar.UTCOnlyTimeZoneDatabase)
end
@doc """
@@ -544,8 +544,8 @@ defmodule Calendar do
parse_modifiers(rest, width, "", parser_data)
end
defp parse_modifiers("0" <> rest, nil, nil, parser_data) do
parse_modifiers(rest, nil, ?0, parser_data)
defp parse_modifiers("0" <> rest, width, nil, parser_data) do
parse_modifiers(rest, width, ?0, parser_data)
end
defp parse_modifiers("_" <> rest, width, nil, parser_data) do
+10 -25
View File
@@ -4,7 +4,7 @@ defmodule Date do
The Date struct contains the fields year, month, day and calendar.
New dates can be built with the `new/3` function or using the
`~D` (see `sigil_D/2`) sigil:
`~D` (see `Kernel.sigil_D/2`) sigil:
iex> ~D[2000-01-01]
~D[2000-01-01]
@@ -31,12 +31,7 @@ defmodule Date do
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
and based on the `Date` struct fields. For proper comparison between
dates, use the `compare/2` function. The existence of the `compare/2`
function in this module also allows using `Enum.min/2` and `Enum.max/2`
functions to get the minimum and maximum date of an `Enum`. For example:
iex> Enum.min([~D[2017-03-31], ~D[2017-04-01]], Date)
~D[2017-03-31]
dates, use the `compare/2` function.
## Using epochs
@@ -77,18 +72,16 @@ defmodule Date do
## Examples
iex> Date.range(~D[1999-01-01], ~D[2000-01-01])
Date.range(~D[1999-01-01], ~D[2000-01-01])
#DateRange<~D[1999-01-01], ~D[2000-01-01]>
A range of dates implements the `Enumerable` protocol, which means
functions in the `Enum` module can be used to work with
ranges:
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01])
iex> range
Date.range(~D[2001-01-01], ~D[2002-01-01])
iex> Enum.count(range)
366
iex> ~D[2001-02-01] in range
iex> Enum.member?(range, ~D[2001-02-01])
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
@@ -115,10 +108,10 @@ defmodule Date do
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> range
Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
iex> Enum.count(range)
183
iex> ~D[2001-01-03] in range
iex> Enum.member?(range, ~D[2001-01-03])
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
@@ -837,6 +830,8 @@ defmodule Date do
~D[2020-07-05]
iex> Date.end_of_week(~D[2020-07-06], :sunday)
~D[2020-07-11]
iex> Date.end_of_week(~D[2020-07-06], :sunday)
~D[2020-07-11]
iex> Date.end_of_week(~D[2020-07-06], :saturday)
~D[2020-07-10]
iex> Date.end_of_week(~N[2020-07-11 01:23:45])
@@ -948,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 """
@@ -997,8 +986,6 @@ defmodule Date do
"""
@doc since: "1.11.0"
@spec beginning_of_month(Calendar.date()) :: t()
def beginning_of_month(date)
def beginning_of_month(%{year: year, month: month, calendar: calendar}) do
%Date{year: year, month: month, day: 1, calendar: calendar}
end
@@ -1018,8 +1005,6 @@ defmodule Date do
"""
@doc since: "1.11.0"
@spec end_of_month(Calendar.date()) :: t()
def end_of_month(date)
def end_of_month(%{year: year, month: month, calendar: calendar} = date) do
day = Date.days_in_month(date)
%Date{year: year, month: month, day: day, calendar: calendar}
+6 -7
View File
@@ -16,14 +16,13 @@ defmodule Date.Range do
@type t :: %__MODULE__{
first: Date.t(),
last: Date.t(),
first_in_iso_days: days(),
last_in_iso_days: days(),
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days(),
step: pos_integer | neg_integer
}
@typep days() :: integer()
@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
@@ -75,7 +74,7 @@ defmodule Date.Range do
step: step
} = range
) do
{:ok, size(range), &slice(first + &1 * step, step + &3 - 1, &2, calendar)}
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
end
# TODO: Remove me on v2.0
@@ -211,11 +210,11 @@ defmodule Date.Range do
import Kernel, except: [inspect: 2]
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ")"
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
end
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ", #{step})"
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
end
# TODO: Remove me on v2.0
+37 -199
View File
@@ -2,24 +2,15 @@ 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
comparison between datetimes, use the `compare/2` function. The
existence of the `compare/2` function in this module also allows
using `Enum.min/2` and `Enum.max/2` functions to get the minimum and
maximum datetime of an `Enum`. For example:
iex> Enum.min([~U[2022-01-12 00:01:00.00Z], ~U[2021-01-12 00:01:00.00Z]], DateTime)
~U[2021-01-12 00:01:00.00Z]
comparison between datetimes, use the `compare/2` function.
Developers should avoid creating the `DateTime` struct directly
and instead rely on the functions provided by this module as
@@ -34,74 +25,22 @@ defmodule DateTime do
datetimes and returns `{:error, :utc_only_time_zone_database}`
for any other time zone.
Other time zone databases can also be configured. Here are some
available options and libraries:
Other time zone databases can also be configured. For example,
two of the available options are:
* [`tz`](https://github.com/mathieuprog/tz)
* [`tzdata`](https://github.com/lau/tzdata)
* [`zoneinfo`](https://github.com/smartrent/zoneinfo) -
recommended for embedded devices
* [`tz`](https://hexdocs.pm/tz/)
* [`tzdata`](https://hexdocs.pm/tzdata/)
To use them, first make sure it is added as a dependency in `mix.exs`.
It can then be configured either via configuration:
config :elixir, :time_zone_database, Tz.TimeZoneDatabase
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
or by calling `Calendar.put_time_zone_database/1`:
Calendar.put_time_zone_database(Tz.TimeZoneDatabase)
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] ++
@@ -143,9 +82,6 @@ defmodule DateTime do
@doc """
Returns the current datetime in UTC.
If you want the current time in Unix seconds,
use `System.os_time/1` instead.
## Examples
iex> datetime = DateTime.utc_now()
@@ -514,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
@@ -817,6 +753,10 @@ defmodule DateTime do
It will return the integer with the given unit,
according to `System.convert_time_unit/3`.
If you want to get the current time in Unix seconds,
do not do `DateTime.utc_now() |> DateTime.to_unix()`.
Simply call `System.os_time(:second)` instead.
## Examples
iex> 1_464_096_368 |> DateTime.from_unix!() |> DateTime.to_unix()
@@ -861,8 +801,6 @@ defmodule DateTime do
"""
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
def to_naive(datetime)
def to_naive(%{
calendar: calendar,
year: year,
@@ -902,8 +840,6 @@ defmodule DateTime do
"""
@spec to_date(Calendar.datetime()) :: Date.t()
def to_date(datetime)
def to_date(%{
year: year,
month: month,
@@ -934,8 +870,6 @@ defmodule DateTime do
"""
@spec to_time(Calendar.datetime()) :: Time.t()
def to_time(datetime)
def to_time(%{
year: _,
month: _,
@@ -1119,10 +1053,6 @@ defmodule DateTime do
iex> datetime
~U[-2015-01-23 21:20:07.123Z]
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("20150123T235007.123+0230", :basic)
iex> datetime
~U[2015-01-23 21:20:07.123Z]
iex> DateTime.from_iso8601("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> DateTime.from_iso8601("2015-01-23T23:50:07")
@@ -1136,37 +1066,11 @@ defmodule DateTime do
"""
@doc since: "1.4.0"
@spec from_iso8601(String.t(), Calendar.calendar(), :extended | :basic) ::
@spec from_iso8601(String.t(), Calendar.calendar()) ::
{:ok, t, Calendar.utc_offset()} | {:error, atom}
def from_iso8601(string, format_or_calendar \\ Calendar.ISO)
def from_iso8601(string, format) when format in [:basic, :extended] do
from_iso8601(string, Calendar.ISO, format)
end
def from_iso8601(string, calendar) when is_atom(calendar) do
from_iso8601(string, calendar, :extended)
end
@doc """
Converts to ISO8601 specifying both a calendar and a mode.
See `from_iso8601/2` for more information.
## Examples
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("2015-01-23T23:50:07,123+02:30", Calendar.ISO, :extended)
iex> datetime
~U[2015-01-23 21:20:07.123Z]
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("20150123T235007.123+0230", Calendar.ISO, :basic)
iex> datetime
~U[2015-01-23 21:20:07.123Z]
"""
def from_iso8601(string, calendar, format) do
def from_iso8601(string, calendar \\ Calendar.ISO) do
with {:ok, {year, month, day, hour, minute, second, microsecond}, offset} <-
Calendar.ISO.parse_utc_datetime(string, format) do
Calendar.ISO.parse_utc_datetime(string) do
datetime = %DateTime{
year: year,
month: month,
@@ -1387,11 +1291,12 @@ defmodule DateTime do
@doc """
Subtracts `datetime2` from `datetime1`.
The answer can be returned in any `:day`, `:hour`, `:minute`, or any `unit`
available from `t:System.time_unit/0`. The unit is measured according to
`Calendar.ISO` and defaults to `:second`.
The answer can be returned in any `unit` available from `t:System.time_unit/0`.
Fractional results are not supported and are truncated.
Leap seconds are not taken into account.
This function returns the difference in seconds where seconds are measured
according to `Calendar.ISO`.
## Examples
@@ -1405,36 +1310,14 @@ defmodule DateTime do
18000
iex> DateTime.diff(dt2, dt1)
-18000
iex> DateTime.diff(dt1, dt2, :hour)
5
iex> DateTime.diff(dt2, dt1, :hour)
-5
"""
@doc since: "1.5.0"
@spec diff(
Calendar.datetime(),
Calendar.datetime(),
:day | :hour | :minute | System.time_unit()
) :: integer()
def diff(datetime1, datetime2, unit \\ :second)
def diff(datetime1, datetime2, :day) do
diff(datetime1, datetime2, :second) |> div(86400)
end
def diff(datetime1, datetime2, :hour) do
diff(datetime1, datetime2, :second) |> div(3600)
end
def diff(datetime1, datetime2, :minute) do
diff(datetime1, datetime2, :second) |> div(60)
end
@spec diff(Calendar.datetime(), Calendar.datetime(), System.time_unit()) :: integer()
def diff(
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2,
unit
unit \\ :second
) do
naive_diff =
(datetime1 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)) -
@@ -1447,30 +1330,15 @@ defmodule DateTime do
@doc """
Adds a specified amount of time to a `DateTime`.
Accepts an `amount_to_add` in any `unit`. `unit` can be `:day`,
`:hour`, `:minute`, `:second` or any subsecond precision from
`t:System.time_unit/0`. It defaults to `:second`. Negative values
will move backwards in time.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in time.
This function always consider the unit to be computed according
to the `Calendar.ISO`.
Takes changes such as summer time/DST into account. This means that adding time
can cause the wall time to "go backwards" during "fall back" during autumn.
Adding just a few seconds to a datetime just before "spring forward" can cause wall
time to increase by more than an hour.
This function uses relies on a contiguous representation of time,
ignoring the wall time and timezone changes. For example, if you add
one day when there are summer time/daylight saving time changes,
it will also change the time forward or backward by one hour,
so the ellapsed time is precisely 24 hours. Similarly, adding just
a few seconds to a datetime just before "spring forward" can cause
wall time to increase by more than an hour.
While this means this function is precise in terms of ellapsed time,
its result may be misleading in certain use cases. For example, if a
user requests a meeting to happen every day at 15:00 and you use this
function to compute all future meetings by adding day after day, this
function may change the meeting time to 14:00 or 16:00 if there are
changes to the current timezone. Computing of recurring datetimes is
not currently supported in Elixir's standard library but it is available
by third-party libraries.
Fractional second precision stays the same in a similar way to `NaiveDateTime.add/2`.
### Examples
@@ -1481,26 +1349,15 @@ defmodule DateTime do
iex> DateTime.add(~U[2018-11-15 10:00:00Z], 3600, :second)
~U[2018-11-15 11:00:00Z]
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02:
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:59:59.123], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(3, :second, FakeTimeZoneDatabase)
#DateTime<2019-03-31 03:00:02.123+02:00 CEST Europe/Copenhagen>
When adding 1 day during "spring forward", the hour also changes:
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(1, :day, FakeTimeZoneDatabase)
#DateTime<2019-04-01 02:00:00+02:00 CEST Europe/Copenhagen>
"""
@doc since: "1.8.0"
@spec add(
Calendar.datetime(),
integer,
:day | :hour | :minute | System.time_unit(),
Calendar.time_zone_database()
) ::
@spec add(Calendar.datetime(), integer, System.time_unit(), Calendar.time_zone_database()) ::
t()
def add(
datetime,
@@ -1508,20 +1365,7 @@ defmodule DateTime do
unit \\ :second,
time_zone_database \\ Calendar.get_time_zone_database()
)
def add(datetime, amount_to_add, :day, time_zone_database) when is_integer(amount_to_add) do
add(datetime, amount_to_add * 86400, :second, time_zone_database)
end
def add(datetime, amount_to_add, :hour, time_zone_database) when is_integer(amount_to_add) do
add(datetime, amount_to_add * 3600, :second, time_zone_database)
end
def add(datetime, amount_to_add, :minute, time_zone_database) when is_integer(amount_to_add) do
add(datetime, amount_to_add * 60, :second, time_zone_database)
end
def add(datetime, amount_to_add, unit, time_zone_database) when is_integer(amount_to_add) do
when is_integer(amount_to_add) do
%{
utc_offset: utc_offset,
std_offset: std_offset,
@@ -1529,12 +1373,6 @@ defmodule DateTime do
microsecond: {_, precision}
} = datetime
if not is_integer(unit) and
unit not in ~w(second millisecond microsecond nanosecond)a do
raise ArgumentError,
"unsupported time unit. Expected :day, :hour, :minute, :second, :millisecond, :microsecond, :nanosecond, or a positive integer, got #{inspect(unit)}"
end
ppd = System.convert_time_unit(86400, :second, unit)
total_offset = System.convert_time_unit(utc_offset + std_offset, :second, unit)
+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`.
+19 -94
View File
@@ -5,7 +5,7 @@ defmodule NaiveDateTime do
The NaiveDateTime struct contains the fields year, month, day, hour,
minute, second, microsecond and calendar. New naive datetimes can be
built with the `new/2` and `new/8` functions or using the
`~N` (see `sigil_N/2`) sigil:
`~N` (see `Kernel.sigil_N/2`) sigil:
iex> ~N[2000-01-01 23:00:07]
~N[2000-01-01 23:00:07]
@@ -36,18 +36,12 @@ defmodule NaiveDateTime do
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
and based on the `NaiveDateTime` struct fields. For proper comparison
between naive datetimes, use the `compare/2` function. The existence of the
`compare/2` function in this module also allows using `Enum.min/2` and
`Enum.max/2` functions to get the minimum and maximum naive datetime of an
`Enum`. For example:
iex> Enum.min([~N[2020-01-01 23:00:07], ~N[2000-01-01 23:00:07]], NaiveDateTime)
~N[2000-01-01 23:00:07]
between naive datetimes, use the `compare/2` function.
## Using epochs
The `add/3` and `diff/3` functions can be used for computing date
times or retrieving the number of seconds between instants.
The `add/3` and `diff/3` functions can be used for computing with
date times or retrieving the number of seconds between instants.
For example, if there is an interest in computing the number of
seconds from the Unix epoch (1970-01-01 00:00:00):
@@ -356,18 +350,11 @@ defmodule NaiveDateTime do
@doc """
Adds a specified amount of time to a `NaiveDateTime`.
Accepts an `amount_to_add` in any `unit`. `unit` can be `:day`,
`:hour`, `:minute`, `:second` or any subsecond precision from
`t:System.time_unit/0`. It defaults to `:second`. Negative values
will move backwards in time.
This function always consider the unit to be computed according
to the `Calendar.ISO`.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in time.
## Examples
It uses seconds by default:
# adds seconds by default
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], 2)
~N[2014-10-02 00:29:12]
@@ -376,33 +363,19 @@ defmodule NaiveDateTime do
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], -2)
~N[2014-10-02 00:29:08]
It can also work with subsecond precisions:
# can work with other units
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], 2_000, :millisecond)
~N[2014-10-02 00:29:12]
As well as days/hours/minutes:
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 2, :day)
~N[2015-03-02 00:29:10]
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 36, :hour)
~N[2015-03-01 12:29:10]
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 60, :minute)
~N[2015-02-28 01:29:10]
This operation keeps the precision of the naive date time:
# keeps the same precision
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10.021], 21, :second)
~N[2014-10-02 00:29:31.021]
And ignores any changes below the precision:
# changes below the precision will not be visible
iex> hidden = NaiveDateTime.add(~N[2014-10-02 00:29:10], 21, :millisecond)
iex> hidden.microsecond # ~N[2014-10-02 00:29:10]
{21000, 0}
Operations on top of gregorian seconds or the Unix epoch are optimized:
# from Gregorian seconds
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
~N[2014-10-02 00:29:10]
@@ -418,25 +391,11 @@ defmodule NaiveDateTime do
"""
@doc since: "1.4.0"
@spec add(Calendar.naive_datetime(), integer, :day | :hour | :minute | System.time_unit()) :: t
def add(naive_datetime, amount_to_add, unit \\ :second)
def add(naive_datetime, amount_to_add, :day) when is_integer(amount_to_add) do
add(naive_datetime, amount_to_add * 86400, :second)
end
def add(naive_datetime, amount_to_add, :hour) when is_integer(amount_to_add) do
add(naive_datetime, amount_to_add * 3600, :second)
end
def add(naive_datetime, amount_to_add, :minute) when is_integer(amount_to_add) do
add(naive_datetime, amount_to_add * 60, :second)
end
@spec add(Calendar.naive_datetime(), integer, System.time_unit()) :: t
def add(
%{microsecond: {_, precision}, calendar: calendar} = naive_datetime,
amount_to_add,
unit
unit \\ :second
)
when is_integer(amount_to_add) do
ppd = System.convert_time_unit(86400, :second, unit)
@@ -450,11 +409,10 @@ defmodule NaiveDateTime do
@doc """
Subtracts `naive_datetime2` from `naive_datetime1`.
The answer can be returned in any `:day`, `:hour`, `:minute`, or any `unit`
available from `t:System.time_unit/0`. The unit is measured according to
`Calendar.ISO` and defaults to `:second`.
The answer can be returned in any `unit` available from `t:System.time_unit/0`.
Fractional results are not supported and are truncated.
This function returns the difference in seconds where seconds are measured
according to `Calendar.ISO`.
## Examples
@@ -462,57 +420,24 @@ defmodule NaiveDateTime do
2
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:12], ~N[2014-10-02 00:29:10], :microsecond)
2_000_000
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10.042], ~N[2014-10-02 00:29:10.021])
0
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10.042], ~N[2014-10-02 00:29:10.021], :millisecond)
21
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10], ~N[2014-10-02 00:29:12])
-2
iex> NaiveDateTime.diff(~N[-0001-10-02 00:29:10], ~N[-0001-10-02 00:29:12])
-2
It can also compute the difference in days, hours, or minutes:
iex> NaiveDateTime.diff(~N[2014-10-10 00:29:10], ~N[2014-10-02 00:29:10], :day)
8
iex> NaiveDateTime.diff(~N[2014-10-02 12:29:10], ~N[2014-10-02 00:29:10], :hour)
12
iex> NaiveDateTime.diff(~N[2014-10-02 00:39:10], ~N[2014-10-02 00:29:10], :minute)
10
But it also rounds incomplete days to zero:
iex> NaiveDateTime.diff(~N[2014-10-10 00:29:09], ~N[2014-10-02 00:29:10], :day)
7
# to Gregorian seconds
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10], ~N[0000-01-01 00:00:00])
63579428950
"""
@doc since: "1.4.0"
@spec diff(
Calendar.naive_datetime(),
Calendar.naive_datetime(),
:day | :hour | :minute | System.time_unit()
) :: integer
def diff(naive_datetime1, naive_datetime2, unit \\ :second)
def diff(naive_datetime1, naive_datetime2, :day) do
diff(naive_datetime1, naive_datetime2, :second) |> div(86400)
end
def diff(naive_datetime1, naive_datetime2, :hour) do
diff(naive_datetime1, naive_datetime2, :second) |> div(3600)
end
def diff(naive_datetime1, naive_datetime2, :minute) do
diff(naive_datetime1, naive_datetime2, :second) |> div(60)
end
@spec diff(Calendar.naive_datetime(), Calendar.naive_datetime(), System.time_unit()) :: integer
def diff(
%{calendar: calendar1} = naive_datetime1,
%{calendar: calendar2} = naive_datetime2,
unit
unit \\ :second
) do
if not Calendar.compatible_calendars?(calendar1, calendar2) do
raise ArgumentError,
+24 -73
View File
@@ -4,7 +4,7 @@ defmodule Time do
The Time struct contains the fields hour, minute, second and microseconds.
New times can be built with the `new/4` function or using the
`~T` (see `sigil_T/2`) sigil:
`~T` (see `Kernel.sigil_T/2`) sigil:
iex> ~T[23:00:07.001]
~T[23:00:07.001]
@@ -31,12 +31,7 @@ defmodule Time do
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
and based on the `Time` struct fields. For proper comparison between
times, use the `compare/2` function. The existence of the `compare/2`
function in this module also allows using `Enum.min/2` and `Enum.max/2`
functions to get the minimum and maximum time of an `Enum`. For example:
iex> Enum.min([~T[23:00:07.001], ~T[10:00:07.001]], Time)
~T[10:00:07.001]
times, use the `compare/2` function.
"""
@enforce_keys [:hour, :minute, :second]
@@ -454,15 +449,10 @@ defmodule Time do
end
@doc """
Adds the `amount_to_add` of `unit`s to the given `time`.
Adds the `number` of `unit`s to the given `time`.
Accepts an `amount_to_add` in any `unit`. `unit` can be `:day`,
`:hour`, `:minute`, `:second` or any subsecond precision from
`t:System.time_unit/0`. It defaults to `:second`. Negative values
will move backwards in time.
This function always consider the unit to be computed according
to the `Calendar.ISO`.
This function accepts the `number` measured according to `Calendar.ISO`.
The time is returned in the same calendar as it was given in.
Note the result value represents the time of day, meaning that it is cyclic,
for instance, it will never go over 24 hours for the ISO calendar.
@@ -470,56 +460,30 @@ defmodule Time do
## Examples
iex> Time.add(~T[10:00:00], 27000)
~T[17:30:00]
~T[17:30:00.000000]
iex> Time.add(~T[11:00:00.005], 2400)
~T[11:40:00.005]
iex> Time.add(~T[00:00:00.000], 86_399_999, :millisecond)
~T[23:59:59.999]
Negative values are allowed:
iex> Time.add(~T[23:00:00], -60)
~T[22:59:00]
Note that the time is cyclic:
~T[11:40:00.005000]
iex> Time.add(~T[00:00:00], 86_399_999, :millisecond)
~T[23:59:59.999000]
iex> Time.add(~T[17:10:05], 86400)
~T[17:10:05]
Hours and minutes are also supported:
iex> Time.add(~T[17:10:05], 2, :hour)
~T[19:10:05]
iex> Time.add(~T[17:10:05], 30, :minute)
~T[17:40:05]
~T[17:10:05.000000]
iex> Time.add(~T[23:00:00], -60)
~T[22:59:00.000000]
"""
@doc since: "1.6.0"
@spec add(Calendar.time(), integer, :hour | :minute | System.time_unit()) :: t
def add(time, amount_to_add, unit \\ :second)
def add(time, amount_to_add, :hour) when is_integer(amount_to_add) do
add(time, amount_to_add * 3600, :second)
end
def add(time, amount_to_add, :minute) when is_integer(amount_to_add) do
add(time, amount_to_add * 60, :second)
end
def add(%{calendar: calendar, microsecond: {_, precision}} = time, amount_to_add, unit)
when is_integer(amount_to_add) do
amount_to_add = System.convert_time_unit(amount_to_add, unit, :microsecond)
total = time_to_microseconds(time) + amount_to_add
@spec add(Calendar.time(), integer, System.time_unit()) :: t
def add(%{calendar: calendar} = time, number, unit \\ :second) when is_integer(number) do
number = System.convert_time_unit(number, unit, :microsecond)
total = time_to_microseconds(time) + number
parts = Integer.mod(total, @parts_per_day)
{hour, minute, second, {microsecond, _}} =
calendar.time_from_day_fraction({parts, @parts_per_day})
{hour, minute, second, microsecond} = calendar.time_from_day_fraction({parts, @parts_per_day})
%Time{
hour: hour,
minute: minute,
second: second,
microsecond: {microsecond, precision},
microsecond: microsecond,
calendar: calendar
}
end
@@ -686,12 +650,12 @@ defmodule Time do
additional information about a date or time zone is ignored when calculating
the difference.
The answer can be returned in any `:hour`, `:minute`, `:second` or any
subsecond `unit` available from `t:System.time_unit/0`. If the first time
value is earlier than the second, a negative number is returned.
The answer can be returned in any `unit` available from
`t:System.time_unit/0`. If the first time value is earlier than
the second, a negative number is returned.
The unit is measured according to `Calendar.ISO` and defaults to `:second`.
Fractional results are not supported and are truncated.
This function returns the difference in seconds where seconds
are measured according to `Calendar.ISO`.
## Examples
@@ -712,24 +676,11 @@ defmodule Time do
iex> Time.diff(~T[00:29:10], ~T[00:29:12], :microsecond)
-2_000_000
iex> Time.diff(~T[02:29:10], ~T[00:29:10], :hour)
2
iex> Time.diff(~T[02:29:10], ~T[00:29:11], :hour)
1
"""
@doc since: "1.5.0"
@spec diff(Calendar.time(), Calendar.time(), :hour | :minute | System.time_unit()) :: integer
@spec diff(Calendar.time(), Calendar.time(), System.time_unit()) :: integer
def diff(time1, time2, unit \\ :second)
def diff(time1, time2, :hour) do
diff(time1, time2, :second) |> div(3600)
end
def diff(time1, time2, :minute) do
diff(time1, time2, :second) |> div(60)
end
def diff(
%{
calendar: Calendar.ISO,
+399 -412
View File
File diff suppressed because it is too large Load Diff
+239 -257
View File
@@ -22,7 +22,7 @@ defmodule Code.Formatter do
@no_newline_binary_operators [:\\, :in]
# Left associative operators that start on the next line in case of breaks (always pipes)
@pipeline_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :"<|>"]
@pipeline_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :<|>]
# Right associative operators that start on the next line in case of breaks
@right_new_line_before_binary_operators [:|, :when]
@@ -43,8 +43,8 @@ defmodule Code.Formatter do
:<<~,
:~>>,
:<~>,
:"<|>",
:"^^^",
:<|>,
:^^^,
:+++,
:---,
:in,
@@ -141,113 +141,172 @@ defmodule Code.Formatter do
@do_end_keywords [:rescue, :catch, :else, :after]
@bitstring_modifiers [
:integer,
:float,
:bits,
:bitstring,
:binary,
:bytes,
:utf8,
:utf16,
:utf32,
:signed,
:unsigned,
:little,
:big,
:native,
:_
]
@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, [])
normalize_bitstring_modifiers = Keyword.get(opts, :normalize_bitstring_modifiers, true)
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,
normalize_bitstring_modifiers: normalize_bitstring_modifiers
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
@@ -255,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
@@ -292,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}
@@ -314,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}
@@ -377,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['] ->
@@ -391,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)
@@ -399,8 +457,8 @@ defmodule Code.Formatter do
end
end
defp quoted_to_algebra({:__block__, meta, [atom]}, _context, state) when is_atom(atom) do
{atom_to_algebra(atom, meta), state}
defp quoted_to_algebra({:__block__, _, [atom]}, _context, state) when is_atom(atom) do
{atom_to_algebra(atom), state}
end
defp quoted_to_algebra({:__block__, meta, [integer]}, _context, state)
@@ -465,15 +523,6 @@ defmodule Code.Formatter do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
end
# ..
defp quoted_to_algebra({:.., _meta, []}, context, state) do
if context in [:no_parens_arg, :no_parens_one_arg] do
{"(..)", state}
else
{"..", state}
end
end
# 1..2//3
defp quoted_to_algebra({:"..//", meta, [left, middle, right]}, context, state) do
quoted_to_algebra({:"//", meta, [{:.., meta, [left, middle]}, right]}, context, state)
@@ -518,10 +567,12 @@ defmodule Code.Formatter do
{:__block__, _, [atom]} when is_atom(atom) ->
key =
if Macro.classify_atom(atom) in [:identifier, :unquoted] do
IO.iodata_to_binary([Atom.to_string(atom), ?:])
else
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
case Code.Identifier.classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([Atom.to_string(atom), ?:])
_ ->
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
end
{string(key), state}
@@ -547,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
@@ -894,7 +940,7 @@ defmodule Code.Formatter do
# @foo(bar)
defp module_attribute_to_algebra(meta, {name, call_meta, [_] = args} = expr, context, state)
when is_atom(name) and name not in [:__block__, :__aliases__] do
if Macro.classify_atom(name) == :identifier do
if Code.Identifier.classify(name) == :callable_local do
{{call_doc, state}, wrap_in_parens?} =
call_args_to_algebra(args, call_meta, context, :skip_unless_many_args, false, state)
@@ -939,7 +985,7 @@ defmodule Code.Formatter do
)
when is_atom(fun) and is_integer(arity) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Macro.inspect_atom(:remote_call, fun)
fun = Code.Identifier.inspect_as_function(fun)
{target_doc |> nest(1) |> concat(string(".#{fun}/#{arity}")), state}
end
@@ -984,7 +1030,7 @@ defmodule Code.Formatter do
defp remote_to_algebra({{:., _, [target, fun]}, meta, args}, context, state)
when is_atom(fun) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Macro.inspect_atom(:remote_call, fun)
fun = Code.Identifier.inspect_as_function(fun)
remote_doc = target_doc |> concat(".") |> concat(string(fun))
if args == [] and not remote_target_is_a_module?(target) and not meta?(meta, :closing) do
@@ -1034,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?} =
@@ -1220,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
@@ -1297,7 +1347,7 @@ defmodule Code.Formatter do
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
{doc, state} = interpolation_to_algebra(quoted, state)
{doc, state} = interpolation_to_string(quoted, state)
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1313,7 +1363,7 @@ defmodule Code.Formatter do
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{:"::", _, [{{:., _, [Kernel, :to_string]}, _meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = interpolation_to_algebra(quoted, state)
{doc, state} = interpolation_to_string(quoted, state)
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1321,9 +1371,21 @@ defmodule Code.Formatter do
{concat(acc, last), state}
end
defp interpolation_to_algebra(quoted, %{skip_eol: skip_eol} = state) do
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
{no_limit(surround("\#{", doc, "}")), %{state | skip_eol: skip_eol}}
doc = interpolation_to_string(surround("\#{", doc, "}"))
{doc, %{state | skip_eol: skip_eol}}
end
defp interpolation_to_string(doc) do
[head | tail] =
doc
|> format_to_string()
|> String.split("\n")
Enum.reduce(tail, string(head), fn line, acc ->
concat([acc, line(), string(line)])
end)
end
## Sigils
@@ -1334,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
@@ -1411,7 +1449,7 @@ defmodule Code.Formatter do
defp bitstring_segment_to_algebra({{:"::", _, [segment, spec]}, i}, state, last) do
{doc, state} = quoted_to_algebra(segment, :parens_arg, state)
{spec, state} = bitstring_spec_to_algebra(spec, state, state.normalize_bitstring_modifiers)
{spec, state} = bitstring_spec_to_algebra(spec, state)
spec = wrap_in_parens_if_inspected_atom(spec)
spec = if i == last, do: bitstring_wrap_parens(spec, i, last), else: spec
@@ -1430,35 +1468,16 @@ defmodule Code.Formatter do
{bitstring_wrap_parens(doc, i, last), state}
end
defp bitstring_spec_to_algebra({op, _, [left, right]}, state, normalize_modifiers)
when op in [:-, :*] do
normalize_modifiers = normalize_modifiers && op != :*
{left, state} = bitstring_spec_to_algebra(left, state, normalize_modifiers)
{right, state} = bitstring_spec_element_to_algebra(right, state, normalize_modifiers)
defp bitstring_spec_to_algebra({op, _, [left, right]}, state) when op in [:-, :*] do
{left, state} = bitstring_spec_to_algebra(left, state)
{right, state} = quoted_to_algebra_with_parens_if_operator(right, :parens_arg, state)
{concat(concat(left, Atom.to_string(op)), right), state}
end
defp bitstring_spec_to_algebra(spec, state, normalize_modifiers) do
bitstring_spec_element_to_algebra(spec, state, normalize_modifiers)
defp bitstring_spec_to_algebra(spec, state) do
quoted_to_algebra_with_parens_if_operator(spec, :parens_arg, state)
end
defp bitstring_spec_element_to_algebra(
{atom, meta, empty_args},
state,
_normalize_modifiers = true
)
when is_atom(atom) and empty_args in [nil, []] do
empty_args = bitstring_spec_normalize_empty_args(atom)
quoted_to_algebra_with_parens_if_operator({atom, meta, empty_args}, :parens_arg, state)
end
defp bitstring_spec_element_to_algebra(spec_element, state, _normalize_modifiers) do
quoted_to_algebra_with_parens_if_operator(spec_element, :parens_arg, state)
end
defp bitstring_spec_normalize_empty_args(atom) when atom in @bitstring_modifiers, do: nil
defp bitstring_spec_normalize_empty_args(_atom), do: []
defp bitstring_wrap_parens(doc, i, last) when i == 0 or i == last do
string = format_to_string(doc)
@@ -1526,36 +1545,25 @@ defmodule Code.Formatter do
end
end
defp atom_to_algebra(atom, _) when atom in [nil, true, false] do
defp atom_to_algebra(atom) when atom in [nil, true, false] do
Atom.to_string(atom)
end
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
defp atom_to_algebra(:"..//", _) do
defp atom_to_algebra(:"..//") do
string(":\"..//\"")
end
defp atom_to_algebra(:\\, meta) do
# Since we parse strings without unescaping, the atoms
# :\\ and :"\\" have the same representation, so we need
# to check the delimiter and handle them accordingly.
string =
case Keyword.get(meta, :delimiter) do
"\"" -> ":\"\\\\\""
_ -> ":\\\\"
end
string(string)
end
defp atom_to_algebra(atom, _) do
defp atom_to_algebra(atom) do
string = Atom.to_string(atom)
iodata =
if Macro.classify_atom(atom) in [:unquoted, :identifier] do
[?:, string]
else
[?:, ?", String.replace(string, "\"", "\\\""), ?"]
case Code.Identifier.classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
[?:, string]
_ ->
[?:, ?", String.replace(string, "\"", "\\\""), ?"]
end
iodata |> IO.iodata_to_binary() |> string()
@@ -1603,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
@@ -1702,15 +1708,12 @@ defmodule Code.Formatter do
) do
min_line = line(meta)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
break_or_line = clause_break_or_line(clauses, state)
doc =
"fn ->"
|> concat(break_or_line)
|> concat(body_doc)
|> glue(body_doc)
|> nest(2)
|> concat(break_or_line)
|> concat("end")
|> glue("end")
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1739,16 +1742,12 @@ defmodule Code.Formatter do
|> nest(:cursor)
|> group()
break_or_line = clause_break_or_line(clauses, state)
doc =
"fn "
|> concat(head)
|> concat(break_or_line)
|> concat(body_doc)
|> glue(body_doc)
|> nest(2)
|> concat(break_or_line)
|> concat("end")
|> glue("end")
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1790,14 +1789,13 @@ defmodule Code.Formatter do
min_line = line(meta)
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
break_or_line = clause_break_or_line(clauses, state)
doc =
args_doc
|> ungroup_if_group()
|> concat(" ->")
|> group()
|> concat(break_or_line |> concat(body_doc) |> nest(2))
|> concat(break() |> concat(body_doc) |> nest(2))
|> wrap_in_parens()
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1818,21 +1816,12 @@ defmodule Code.Formatter do
## Clauses
defp multi_line_clauses?(clauses, state) do
Enum.any?(clauses, fn {:->, meta, [_, block]} ->
eol?(meta, state) or multi_line_block?(block)
end)
end
defp multi_line_block?({:__block__, _, [_, _ | _]}), do: true
defp multi_line_block?(_), do: false
defp clause_break_or_line(clauses, state) do
if multi_line_clauses?(clauses, state), do: line(), else: break()
end
defp maybe_force_clauses(doc, clauses, state) do
if multi_line_clauses?(clauses, state), do: force_unfit(doc), else: doc
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
force_unfit(doc)
else
doc
end
end
defp clauses_to_algebra([{:->, _, _} | _] = clauses, min_line, max_line, state) do
@@ -1931,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} =
@@ -1947,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
@@ -1984,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)
@@ -1996,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
@@ -2114,7 +2097,7 @@ defmodule Code.Formatter do
defp module_attribute_read?({:@, _, [{var, _, var_context}]})
when is_atom(var) and is_atom(var_context) do
Macro.classify_atom(var) == :identifier
Code.Identifier.classify(var) == :callable_local
end
defp module_attribute_read?(_), do: false
@@ -2203,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
@@ -2228,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}
_ ->
@@ -2247,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
@@ -2257,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
File diff suppressed because it is too large Load Diff
+148 -10
View File
@@ -14,7 +14,7 @@ defmodule Code.Identifier do
def unary_op(op) do
cond do
op in [:&] -> {:non_associative, 90}
op in [:!, :^, :not, :+, :-, :"~~~"] -> {:non_associative, 300}
op in [:!, :^, :not, :+, :-, :~~~] -> {:non_associative, 300}
op in [:@] -> {:non_associative, 320}
true -> :error
end
@@ -41,19 +41,161 @@ defmodule Code.Identifier do
op in [:&&, :&&&, :and] -> {:left, 130}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
op in [:<, :<=, :>=, :>] -> {:left, 150}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :"<|>"] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
op in [:in] -> {:left, 170}
op in [:"^^^"] -> {:left, 180}
op in [:^^^] -> {:left, 180}
op in [:"//"] -> {:right, 190}
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
op in [:**] -> {:left, 230}
op in [:.] -> {:left, 310}
true -> :error
end
end
@doc """
Classifies the given atom into one of the following categories:
* `:alias` - a valid Elixir alias, like `Foo`, `Foo.Bar` and so on
* `:callable_local` - an atom that can be used as a local call;
this category includes identifiers like `:foo`
* `:callable_operator` - all callable operators, such as `:<>`. Note
operators such as `:..` are not callable because of ambiguity
* `:not_atomable` - callable operators that must be wrapped in quotes when
defined as an atom. For example, `::` must be written as `:"::"` to avoid
the ambiguity between the atom and the keyword identifier
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator. Those are typically AST nodes that are special forms (such as
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
`:..` and `:...`). This category also includes atoms like `:Foo`, since
they are valid identifiers but they need quotes to be used in function
calls (`Foo."Bar"`)
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
"""
def classify(atom) when is_atom(atom) do
charlist = Atom.to_charlist(atom)
cond do
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
:not_callable
atom in [:"::", :"//"] ->
:not_atomable
unary_op(atom) != :error or binary_op(atom) != :error ->
:callable_operator
valid_alias?(charlist) ->
:alias
true ->
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
else
:not_callable
end
_ ->
:other
end
end
end
defp valid_alias?('Elixir' ++ rest), do: valid_alias_piece?(rest)
defp valid_alias?(_other), do: false
defp valid_alias_piece?([?., char | rest]) when char >= ?A and char <= ?Z,
do: valid_alias_piece?(trim_leading_while_valid_identifier(rest))
defp valid_alias_piece?([]), do: true
defp valid_alias_piece?(_other), do: false
defp trim_leading_while_valid_identifier([char | rest])
when char >= ?a and char <= ?z
when char >= ?A and char <= ?Z
when char >= ?0 and char <= ?9
when char == ?_ do
trim_leading_while_valid_identifier(rest)
end
defp trim_leading_while_valid_identifier(other) do
other
end
@doc """
Inspects the identifier as an atom.
"""
def inspect_as_atom(atom) when is_nil(atom) or is_boolean(atom) do
Atom.to_string(atom)
end
def inspect_as_atom(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
:alias ->
case binary do
binary when binary in ["Elixir", "Elixir.Elixir"] -> binary
"Elixir.Elixir." <> _rest -> binary
"Elixir." <> rest -> rest
end
type when type in [:callable_local, :callable_operator, :not_callable] ->
":" <> binary
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?:, ?", escaped, ?"])
end
end
@doc """
Inspects the given identifier as a key.
"""
def inspect_as_key(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([binary, ?:])
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?", escaped, ?", ?:])
end
end
@doc """
Inspects the given identifier as a function name.
"""
def inspect_as_function(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_atomable] ->
binary
type ->
escaped =
if type in [:not_callable, :alias] do
binary
else
elem(escape(binary, ?"), 0)
end
IO.iodata_to_binary([?", escaped, ?"])
end
end
@doc """
Extracts the name and arity of the parent from the anonymous function identifier.
"""
@@ -71,12 +213,8 @@ defmodule Code.Identifier do
@doc """
Escapes the given identifier.
"""
@spec escape(binary(), char() | nil, :infinity | non_neg_integer, (char() -> iolist() | false)) ::
{escaped :: iolist(), remaining :: binary()}
def escape(binary, char, limit \\ :infinity, fun \\ &escape_map/1)
when ((char in 0..0x10FFFF or is_nil(char)) and limit == :infinity) or
(is_integer(limit) and limit >= 0) do
escape(binary, char, limit, [], fun)
def escape(other, char, count \\ :infinity, fun \\ &escape_map/1) do
escape(other, char, count, [], fun)
end
defp escape(<<_, _::binary>> = binary, _char, 0, acc, _fun) do
-593
View File
@@ -1,593 +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)
utf8 =
if args == [] or interpolated?(string) do
# a non-normalized :utf8 atom signals an atom interpolation
:utf8
else
normalize_literal(:utf8, [], state)
end
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) and 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 Macro.classify_atom(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), nil)
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, nil)
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
+74 -80
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
]
@@ -174,11 +173,9 @@ defmodule Code.Typespec do
end
defp get_module_and_beam(module) when is_atom(module) do
with {^module, beam, _filename} <- :code.get_object_code(module),
{:ok, ^module} <- beam |> :beam_lib.info() |> Keyword.fetch(:module) do
{module, beam}
else
_ -> :error
case :code.get_object_code(module) do
{^module, beam, _filename} -> {module, beam}
:error -> :error
end
end
@@ -191,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
@@ -219,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} ->
@@ -278,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} ->
@@ -302,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
@@ -363,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
@@ -371,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
@@ -418,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
+5 -6
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
@@ -35,7 +35,7 @@ defprotocol Collectable do
...> collector_fun.(acc, {:cont, elem})
...> end)
iex> collector_fun.(updated_acc, :done)
MapSet.new([1, 2, 3])
#MapSet<[1, 2, 3]>
To show how the protocol can be implemented, we can again look at the
simplified implementation for `MapSet`. In this implementation "collecting" elements
@@ -63,7 +63,7 @@ defprotocol Collectable do
So now we can call `Enum.into/2`:
iex> Enum.into([1, 2, 3], MapSet.new())
MapSet.new([1, 2, 3])
#MapSet<[1, 2, 3]>
"""
@@ -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."
+17 -45
View File
@@ -34,12 +34,12 @@ defmodule Config do
`Config` also provides a low-level API for evaluating and reading
configuration, under the `Config.Reader` module.
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. Also note that
> the `config/config.exs` of a library is not evaluated when the library is
> used as a dependency, as configuration is always meant to configure the
> current project. For more information, read our [library guidelines](library-guidelines.md).
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. Also note that
the `config/config.exs` of a library is not evaluated when the library is
used as a dependency, as configuration is always meant to configure the
current project. For more information, read our [library guidelines](library-guidelines.md).
## Migrating from `use Mix.Config`
@@ -62,27 +62,7 @@ defmodule Config do
import_config config
end
The last step is to replace all `Mix.env()` calls in the config files with `config_env()`.
Keep in mind you must also avoid using `Mix.env()` inside your project files.
To check the environment at _runtime_, you may add a configuration key:
# config.exs
...
config :my_app, env: config_env()
Then, in other scripts and modules, you may get the environment with
`Application.fetch_env!/2`:
# router.exs
...
if Application.fetch_env!(:my_app, :env) == :prod do
...
end
The only files where you may access functions from the `Mix` module are
the `mix.exs` file and inside custom Mix tasks, which always within the
`Mix.Tasks` namespace.
The last step is to replace all `Mix.env()` calls by `config_env()`.
## config/runtime.exs
@@ -95,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)
@@ -122,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,
@@ -159,30 +138,21 @@ 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,
adapter: Ecto.Adapters.Postgres,
metadata: [read_only: true]
adapter: Ecto.Adapters.Postgres
config :ecto, Repo,
log_level: :info,
pool_size: 10,
metadata: [replica: true]
pool_size: 10
will have a final value of the configuration for the `Repo`
key in the `:ecto` application of:
Application.get_env(:ecto, Repo)
#=> [
#=> log_level: :info,
#=> pool_size: 10,
#=> adapter: Ecto.Adapters.Postgres,
#=> metadata: [read_only: true, replica: true]
#=> ]
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
"""
@doc since: "1.9.0"
@@ -321,6 +291,8 @@ defmodule Config do
:ok
end
# TODO: Emit a warning if Mix.env() is found in said files in Elixir v1.15.
# Note this won't be a deprecation warning as it will always be emitted.
Code.eval_string(contents, [], file: file)
end
+15 -8
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 =
@@ -341,7 +339,8 @@ defmodule Config.Provider do
defp restart_and_sleep() do
mode = Application.get_env(:elixir, @reboot_mode_key)
if mode in [:embedded, :interactive] do
# TODO: Remove otp_release check once we require Erlang/OTP 23+
if :erlang.system_info(:otp_release) >= '23' and mode in [:embedded, :interactive] do
:init.restart(mode: mode)
else
:init.restart()
@@ -353,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]} ->
@@ -409,7 +416,7 @@ defmodule Config.Provider do
end
defp abort(msg) do
IO.puts("ERROR! " <> msg)
IO.puts(:stderr, "ERROR! " <> msg)
:erlang.raise(:error, "aborting boot", [{Config.Provider, :boot, 2, []}])
end
end
+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.Provider` 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
+1 -1
View File
@@ -23,7 +23,7 @@ defmodule Dict do
import Kernel, except: [size: 1]
if __CALLER__.module != HashDict do
IO.warn("use Dict is deprecated. " <> unquote(message), __CALLER__)
IO.warn("use Dict is deprecated. " <> unquote(message), Macro.Env.stacktrace(__CALLER__))
end
quote do
+71 -123
View File
@@ -1,21 +1,21 @@
defmodule DynamicSupervisor do
@moduledoc ~S"""
A supervisor optimized to only start children dynamically.
A supervisor that starts children dynamically.
The `Supervisor` module was designed to handle mostly static children
that are started in the given order when the supervisor starts. A
`DynamicSupervisor` starts with no children. Instead, children are
started on demand via `start_child/2` and there is no ordering between
children. This allows the `DynamicSupervisor` to hold millions of
children by using efficient data structures and to execute certain
operations, such as shutting down, concurrently.
started on demand via `start_child/2`. When a dynamic supervisor
terminates, all children are shut down at the same time, with no guarantee
of ordering.
## Examples
A dynamic supervisor is started with no children and often a name:
A dynamic supervisor is started with no children, a supervision strategy
(the only strategy currently supported is `:one_for_one`), and a name:
children = [
{DynamicSupervisor, name: MyApp.DynamicSupervisor}
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
@@ -37,46 +37,6 @@ defmodule DynamicSupervisor do
DynamicSupervisor.count_children(MyApp.DynamicSupervisor)
#=> %{active: 2, specs: 2, supervisors: 0, workers: 2}
## Scalability and partitioning
The `DynamicSupervisor` is a single process responsible for starting
other processes. In some applications, the `DynamicSupervisor` may
become a bottleneck. To address this, you can start multiple instances
of the `DynamicSupervisor` and then pick a "random" instance to start
the child on.
Instead of:
children = [
{DynamicSupervisor, name: MyApp.DynamicSupervisor}
]
and:
DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end})
You can do this:
children = [
{PartitionSupervisor,
child_spec: DynamicSupervisor,
name: MyApp.DynamicSupervisors}
]
and then:
DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {MyApp.DynamicSupervisors, self()}},
{Agent, fn -> %{} end}
)
In the code above, we start a partition supervisor that will by default
start a dynamic supervisor for each core in your machine. Then, instead
of calling the `DynamicSupervisor` by name, you call it through the
partition supervisor, using `self()` as the routing key. This means each
process will be assigned one of the existing dynamic supervisors.
Read the `PartitionSupervisor` docs for more information.
## Module-based supervisors
Similar to `Supervisor`, dynamic supervisors also support module-based
@@ -272,74 +232,12 @@ defmodule DynamicSupervisor do
@doc """
Starts a supervisor with the given options.
This function is typically not invoked directly, instead it is invoked
when using a `DynamicSupervisor` as a child of another supervisor:
The `:strategy` is a required option and the currently supported
value is `:one_for_one`. The remaining options can be found in the
`init/1` docs.
children = [
{DynamicSupervisor, name: MySupervisor}
]
If the supervisor is successfully spawned, this function returns
`{:ok, pid}`, where `pid` is the PID of the supervisor. If the supervisor
is given a name and a process with the specified name already exists,
the function returns `{:error, {:already_started, pid}}`, where `pid`
is the PID of that process.
Note that a supervisor started with this function is linked to the parent
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
## Options
* `:name` - registers the supervisor under the given name.
The supported values are described under the "Name registration"
section in the `GenServer` module docs.
* `:strategy` - the restart strategy option. The only supported
value is `:one_for_one` which means that no other child is
terminated if a child process terminates. You can learn more
about strategies in the `Supervisor` module docs.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
* `:max_children` - the maximum amount of children to be running
under this supervisor at the same time. When `:max_children` is
exceeded, `start_child/2` returns `{:error, :max_children}`. Defaults
to `:infinity`.
* `:extra_arguments` - arguments that are prepended to the arguments
specified in the child spec given to `start_child/2`. Defaults to
an empty list.
"""
@doc since: "1.6.0"
@spec start_link([option | init_option]) :: Supervisor.on_start()
def start_link(options) when is_list(options) do
keys = [:extra_arguments, :max_children, :max_seconds, :max_restarts, :strategy]
{sup_opts, start_opts} = Keyword.split(options, keys)
start_link(Supervisor.Default, init(sup_opts), start_opts)
end
@doc """
Starts a module-based supervisor process with the given `module` and `init_arg`.
To start the supervisor, the `c:init/1` callback will be invoked in the given
`module`, with `init_arg` as its argument. The `c:init/1` callback must return a
supervisor specification which can be created with the help of the `init/1`
function.
If the `c:init/1` callback returns `:ignore`, this function returns
`:ignore` as well and the supervisor terminates with reason `:normal`.
If it fails or returns an incorrect value, this function returns
`{:error, term}` where `term` is a term with information about the
error, and the supervisor terminates with reason `term`.
The `:name` option can also be given in order to register a supervisor
name, the supported values are described in the "Name registration"
The `:name` option can also be used to register a supervisor name.
The supported values are described under the "Name registration"
section in the `GenServer` module docs.
If the supervisor is successfully spawned, this function returns
@@ -353,9 +251,35 @@ defmodule DynamicSupervisor do
with `:normal` reason.
"""
@doc since: "1.6.0"
@spec start_link([option | init_option]) :: Supervisor.on_start()
def start_link(options) when is_list(options) do
keys = [:extra_arguments, :max_children, :max_seconds, :max_restarts, :strategy]
{sup_opts, start_opts} = Keyword.split(options, keys)
start_link(Supervisor.Default, init(sup_opts), start_opts)
end
@doc """
Starts a module-based supervisor process with the given `module` and `arg`.
To start the supervisor, the `c:init/1` callback will be invoked in the given
`module`, with `arg` as its argument. The `c:init/1` callback must return a
supervisor specification which can be created with the help of the `init/1`
function.
If the `c:init/1` callback returns `:ignore`, this function returns
`:ignore` as well and the supervisor terminates with reason `:normal`.
If it fails or returns an incorrect value, this function returns
`{:error, term}` where `term` is a term with information about the
error, and the supervisor terminates with reason `term`.
The `:name` option can also be given in order to register a supervisor
name, the supported values are described in the "Name registration"
section in the `GenServer` module docs.
"""
@doc since: "1.6.0"
@spec start_link(module, term, [option]) :: Supervisor.on_start()
def start_link(module, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {module, init_arg, opts[:name]}, opts)
def start_link(mod, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
end
@doc """
@@ -363,9 +287,7 @@ defmodule DynamicSupervisor do
`child_spec` should be a valid child specification as detailed in the
"Child specification" section of the documentation for `Supervisor`. The child
process will be started as defined in the child specification. Note that while
the `:id` field is still required in the spec, the value is ignored and
therefore does not need to be unique.
process will be started as defined in the child specification.
If the child process start function returns `{:ok, child}` or `{:ok, child,
info}`, then child specification and PID are added to the supervisor and
@@ -554,20 +476,46 @@ defmodule DynamicSupervisor do
module-based supervisors. See the "Module-based supervisors" section
in the module documentation for more information.
It accepts the same `options` as `start_link/1` (except for `:name`)
and it returns a tuple containing the supervisor options.
The `options` received by this function are also supported by `start_link/1`.
This function returns a tuple containing the supervisor options.
## Examples
def init(_arg) do
DynamicSupervisor.init(max_children: 1000)
DynamicSupervisor.init(max_children: 1000, strategy: :one_for_one)
end
## Options
* `:strategy` - the restart strategy option. The only supported
value is `:one_for_one` which means that no other child is
terminated if a child process terminates. You can learn more
about strategies in the `Supervisor` module docs.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
* `:max_children` - the maximum amount of children to be running
under this supervisor at the same time. When `:max_children` is
exceeded, `start_child/2` returns `{:error, :max_children}`. Defaults
to `:infinity`.
* `:extra_arguments` - arguments that are prepended to the arguments
specified in the child spec given to `start_child/2`. Defaults to
an empty list.
"""
@doc since: "1.6.0"
@spec init([init_option]) :: {:ok, sup_flags()}
def init(options) when is_list(options) do
strategy = Keyword.get(options, :strategy, :one_for_one)
unless strategy = options[:strategy] do
raise ArgumentError, "expected :strategy option to be given"
end
intensity = Keyword.get(options, :max_restarts, 3)
period = Keyword.get(options, :max_seconds, 5)
max_children = Keyword.get(options, :max_children, :infinity)
+189 -643
View File
File diff suppressed because it is too large Load Diff
+81 -315
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,21 +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 =
guards
|> Enum.map(&blame_guard(&1, ann, scope, binding))
|> Enum.map(&Macro.prewalk(&1, fn guard -> translate_guard(guard) end))
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
{args, guards}
end
@@ -241,71 +230,6 @@ defmodule Exception do
end
end
defp is_map_node?({:is_map, _, [_]}), do: true
defp is_map_node?(_), do: false
defp is_map_key_node?({:is_map_key, _, [_, _]}), do: true
defp is_map_key_node?(_), do: false
defp struct_validation_node?(
{:is_atom, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}]}
),
do: true
defp struct_validation_node?(
{:==, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}, _module]}
),
do: true
defp struct_validation_node?(_), do: false
defp is_struct_macro?(
{:and, _,
[
{:and, _, [%{node: node_1 = {_, _, [arg]}}, %{node: node_2 = {_, _, [arg, _]}}]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(
{:and, _,
[
{:and, _,
[
{:and, _,
[
%{node: node_1 = {_, _, [arg]}},
{:or, _, [%{node: {:is_atom, _, [_]}}, %{node: :fail}]}
]},
%{node: node_2 = {_, _, [arg, _]}}
]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}, _]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(_), do: false
defp translate_guard(guard) do
if is_struct_macro?(guard) do
undo_is_struct_guard(guard)
else
guard
end
end
defp undo_is_struct_guard(
{:and, meta, [_, %{node: {_, _, [{_, _, [_, {struct, _, _}]} | optional]}}]}
) do
args =
case optional do
[] -> [{struct, meta, nil}]
[module] -> [{struct, meta, nil}, module]
end
%{match?: meta[:value], node: {:is_struct, meta, args}}
end
defp blame_arg({call_arg, ex_arg, erl_arg}, binding) do
{match?, binding} = blame_arg(erl_arg, call_arg, binding)
{blame_wrap(match?, rewrite_arg(ex_arg)), binding}
@@ -336,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 =
@@ -349,38 +273,23 @@ defmodule Exception do
:andalso -> :and
end
evaluate_guard(kernel_op, meta, guards)
{kernel_op, meta, guards}
end
defp blame_guard(ex_guard, ann, scope, binding) do
ex_guard
|> blame_guard?(binding, ann, scope)
|> blame_wrap(rewrite_guard(ex_guard))
end
defp blame_guard(ex_guard, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
defp blame_guard?(ex_guard, binding, ann, scope) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
end
defp evaluate_guard(kernel_op, meta, guards = [_, _]) do
[x, y] = Enum.map(guards, &evaluate_guard/1)
logic_value =
case kernel_op do
:or -> x or y
:and -> x and y
match? =
try do
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
end
{kernel_op, Keyword.put(meta, :value, logic_value), guards}
blame_wrap(match?, rewrite_guard(ex_guard))
end
defp evaluate_guard(%{match?: value}), do: value
defp evaluate_guard({_, meta, _}) when is_list(meta), do: meta[:value]
defp rewrite_guard(guard) do
Macro.prewalk(guard, fn
{{:., _, [mod, fun]}, meta, args} -> erl_to_ex(mod, fun, args, meta)
@@ -706,12 +615,12 @@ defmodule Exception do
case Code.Identifier.extract_anonymous_fun_parent(fun) do
{outer_name, outer_arity} ->
"anonymous fn#{format_arity(arity)} in " <>
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, outer_name)}/#{outer_arity}"
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(outer_name)}/#{outer_arity}"
:error ->
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, fun)}#{format_arity(arity)}"
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(fun)}#{format_arity(arity)}"
end
end
@@ -781,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
@@ -816,25 +714,24 @@ 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. " <>
"If you are using the dot syntax, such as module.function(), " <>
"make sure the left-hand side of the dot is a module 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"
not is_atom(module) ->
"you attempted to apply a function on #{inspect(module)}. " <>
"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}
@@ -843,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
@@ -893,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
@@ -1114,8 +972,8 @@ defmodule UndefinedFunctionError do
end
defp hint(nil, _function, 0, _loaded?) do
". If you are using the dot syntax, such as module.function(), " <>
"make sure the left-hand side of the dot is a module 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"
end
defp hint(module, function, arity, true) do
@@ -1175,12 +1033,12 @@ 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
defp format_fa({_dist, fun, arity}) do
[" * ", Macro.inspect_atom(:remote_call, fun), ?/, Integer.to_string(arity), ?\n]
[" * ", Code.Identifier.inspect_as_function(fun), ?/, Integer.to_string(arity), ?\n]
end
defp behaviour_hint(module, function, arity) do
@@ -1206,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
@@ -1250,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
@@ -1272,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" <>
@@ -1297,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)
@@ -1338,13 +1189,11 @@ defmodule FunctionClauseError do
end
defmodule Code.LoadError do
defexception [:file, :message, :reason]
defexception [:file, :message]
def exception(opts) do
file = Keyword.fetch!(opts, :file)
reason = Keyword.fetch!(opts, :reason)
message = "could not load #{file}. Reason: #{reason}"
%Code.LoadError{message: message, file: file, reason: reason}
%Code.LoadError{message: "could not load #{file}", file: file}
end
end
@@ -1448,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
@@ -1559,24 +1408,22 @@ defmodule File.LinkError do
end
defmodule ErlangError do
defexception [:original, :reason]
defexception [:original]
@impl true
def message(exception)
def message(%__MODULE__{original: original, reason: nil}) do
"Erlang error: #{inspect(original)}"
end
def message(%__MODULE__{original: original, reason: reason}) do
IO.iodata_to_binary(["Erlang error: ", inspect(original), reason])
def message(exception) do
"Erlang error: #{inspect(exception.original)}"
end
@doc false
def normalize(:badarg, stacktrace) do
case error_info(:badarg, stacktrace, "errors were found at the given arguments") do
{:ok, reason, details} -> %ArgumentError{message: reason <> details}
:error -> %ArgumentError{}
case error_info(:badarg, stacktrace) do
{:ok, args} ->
message = "errors were found at the given arguments:\n\n#{args}"
%ArgumentError{message: message}
:error ->
%ArgumentError{}
end
end
@@ -1585,11 +1432,15 @@ defmodule ErlangError do
end
def normalize(:system_limit, stacktrace) do
default_reason = "a system limit has been reached due to errors at the given arguments"
case error_info(:system_limit, stacktrace) do
{:ok, args} ->
message =
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
case error_info(:system_limit, stacktrace, default_reason) do
{:ok, reason, details} -> %SystemLimitError{message: reason <> details}
:error -> %SystemLimitError{}
%SystemLimitError{message: message}
:error ->
%SystemLimitError{}
end
end
@@ -1635,19 +1486,10 @@ defmodule ErlangError do
%KeyError{key: key, term: term}
end
def normalize({:badkey, key, map}, _stacktrace) when is_map(map) do
def normalize({:badkey, key, map}, _stacktrace) do
%KeyError{key: key, term: map}
end
def normalize({:badkey, key, term}, _stacktrace) do
message =
"key #{inspect(key)} not found in: #{inspect(term)}. " <>
"If you are using the dot syntax, such as map.field, " <>
"make sure the left-hand side of the dot is a map"
%KeyError{key: key, term: term, message: message}
end
def normalize({:case_clause, term}, _stacktrace) do
%CaseClauseError{term: term}
end
@@ -1674,11 +1516,8 @@ defmodule ErlangError do
%ArgumentError{message: "argument error: #{inspect(payload)}"}
end
def normalize(other, stacktrace) do
case error_info(other, stacktrace, "") do
{:ok, _reason, details} -> %ErlangError{original: other, reason: details}
:error -> %ErlangError{original: other}
end
def normalize(other, _stacktrace) do
%ErlangError{original: other}
end
defp from_stacktrace([{module, function, args, _} | _]) when is_list(args) do
@@ -1693,35 +1532,20 @@ defmodule ErlangError do
{nil, nil, nil}
end
defp error_info(erl_exception, stacktrace, default_reason) do
with [{module, fun, args_or_arity, opts} | tail] <- stacktrace,
@doc false
def error_info(erl_exception, stacktrace) do
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
%{} = error_info <- opts[:error_info] do
error_module = Map.get(error_info, :module, module)
error_fun = Map.get(error_info, :function, :format_error)
error_info = Map.put(error_info, :pretty_printer, &inspect/1)
head = {module, fun, args_or_arity, Keyword.put(opts, :error_info, error_info)}
extra =
try do
apply(error_module, error_fun, [erl_exception, [head | tail]])
rescue
_ -> %{}
end
module = Map.get(error_info, :module, module)
function = Map.get(error_info, :function, :format_error)
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
extra = apply(module, function, [erl_exception, stacktrace])
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
reason = Map.get(extra, :reason, default_reason)
cond do
map_size(args_errors) > 0 ->
{:ok, reason, IO.iodata_to_binary([":\n\n" | Enum.map(args_errors, &arg_error/1)])}
general = extra[:general] ->
{:ok, reason, ": " <> general}
true ->
:error
if map_size(args_errors) > 0 do
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
else
:error
end
else
_ -> :error
@@ -1735,61 +1559,3 @@ defmodule ErlangError do
defp nth(3), do: "3rd"
defp nth(n), do: "#{n}th"
end
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
@enforce_keys [:exception_module, :exception_message, :stacktrace, :inspected_struct]
defexception @enforce_keys
@impl true
def exception(arguments) when is_list(arguments) do
exception = Keyword.fetch!(arguments, :exception)
exception_module = exception.__struct__
exception_message = Exception.message(exception) |> String.trim_trailing("\n")
stacktrace = Keyword.fetch!(arguments, :stacktrace)
inspected_struct = Keyword.fetch!(arguments, :inspected_struct)
%Inspect.Error{
exception_module: exception_module,
exception_message: exception_message,
stacktrace: stacktrace,
inspected_struct: inspected_struct
}
end
@impl true
def message(%__MODULE__{
exception_module: exception_module,
exception_message: exception_message,
inspected_struct: inspected_struct
}) do
~s'''
got #{inspect(exception_module)} with message:
"""
#{pad(exception_message, 4)}
"""
while inspecting:
#{pad(inspected_struct, 4)}
'''
end
@doc false
def pad(message, padding_length)
when is_binary(message) and is_integer(padding_length) and padding_length >= 0 do
padding = String.duplicate(" ", padding_length)
message
|> String.split("\n")
|> Enum.map(fn
"" -> "\n"
line -> [padding, line, ?\n]
end)
|> IO.iodata_to_binary()
|> String.trim_trailing("\n")
end
end
+86 -134
View File
@@ -112,7 +112,6 @@ defmodule File do
encoding_mode()
| :append
| :compressed
| :delayed_write
| :trim_bom
| {:read_ahead, pos_integer | false}
| {:delayed_write, non_neg_integer, non_neg_integer}
@@ -123,8 +122,6 @@ defmodule File do
@type posix_time :: integer()
@type on_conflict_callback :: (Path.t(), Path.t() -> boolean)
@doc """
Returns `true` if the path is a regular file.
@@ -696,10 +693,7 @@ defmodule File do
@spec copy(Path.t() | io_device, Path.t() | io_device, pos_integer | :infinity) ::
{:ok, non_neg_integer} | {:error, posix}
def copy(source, destination, bytes_count \\ :infinity) do
source = normalize_path_or_io_device(source)
destination = normalize_path_or_io_device(destination)
:file.copy(source, destination, bytes_count)
:file.copy(maybe_to_string(source), maybe_to_string(destination), bytes_count)
end
@doc """
@@ -717,8 +711,8 @@ defmodule File do
raise File.CopyError,
reason: reason,
action: "copy",
source: normalize_path_or_io_device(source),
destination: normalize_path_or_io_device(destination)
source: maybe_to_string(source),
destination: maybe_to_string(destination)
end
end
@@ -746,8 +740,6 @@ defmodule File do
@doc since: "1.1.0"
@spec rename(Path.t(), Path.t()) :: :ok | {:error, posix}
def rename(source, destination) do
source = IO.chardata_to_string(source)
destination = IO.chardata_to_string(destination)
:file.rename(source, destination)
end
@@ -778,6 +770,11 @@ defmodule File do
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
will be returned.
The `callback` function is invoked if the `destination_file` already exists.
The function receives arguments for `source_file` and `destination_file`;
it should return `true` if the existing file should be overwritten, `false` if
otherwise. The default callback returns `true`.
The function returns `:ok` in case of success. Otherwise, it returns
`{:error, reason}`.
@@ -789,30 +786,13 @@ defmodule File do
whether the destination is an existing directory or not. We have chosen to
explicitly disallow copying to a destination which is a directory,
and an error will be returned if tried.
## Options
* `:on_conflict` - (since v1.14.0) Invoked when a file already exists in the destination.
The function receives arguments for `source_file` and `destination_file`. It should
return `true` if the existing file should be overwritten, `false` if otherwise.
The default callback returns `true`. On earlier versions, this callback could be
given as third argument, but such behaviour is now deprecated.
"""
@spec cp(Path.t(), Path.t(), on_conflict: on_conflict_callback) :: :ok | {:error, posix}
def cp(source_file, destination_file, options \\ [])
# TODO: Deprecate me on Elixir v1.19
def cp(source_file, destination_file, callback) when is_function(callback, 2) do
cp(source_file, destination_file, on_conflict: callback)
end
def cp(source_file, destination_file, options) when is_list(options) do
on_conflict = Keyword.get(options, :on_conflict, fn _, _ -> true end)
@spec cp(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok | {:error, posix}
def cp(source_file, destination_file, callback \\ fn _, _ -> true end) do
source_file = IO.chardata_to_string(source_file)
destination_file = IO.chardata_to_string(destination_file)
case do_cp_file(source_file, destination_file, on_conflict, []) do
case do_cp_file(source_file, destination_file, callback, []) do
{:error, reason, _} -> {:error, reason}
_ -> :ok
end
@@ -828,9 +808,9 @@ defmodule File do
The same as `cp/3`, but raises a `File.CopyError` exception if it fails.
Returns `:ok` otherwise.
"""
@spec cp!(Path.t(), Path.t(), on_conflict: on_conflict_callback) :: :ok
def cp!(source_file, destination_file, options \\ []) do
case cp(source_file, destination_file, options) do
@spec cp!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok
def cp!(source_file, destination_file, callback \\ fn _, _ -> true end) do
case cp(source_file, destination_file, callback) do
:ok ->
:ok
@@ -853,15 +833,18 @@ defmodule File do
If `source` is a directory, or a symbolic link to it, then `destination` must
be an existent `directory` or a symbolic link to one, or a path to a non-existent directory.
If the source is a file, it copies `source` to `destination`. If the `source`
is a directory, it copies the contents inside source into the `destination` directory.
If the source is a file, it copies `source` to
`destination`. If the `source` is a directory, it copies
the contents inside source into the `destination` directory.
If a file already exists in the destination, it invokes the optional `on_conflict`
callback given as an option. See "Options" for more information.
If a file already exists in the destination, it invokes `callback`.
`callback` must be a function that takes two arguments: `source` and `destination`.
The callback should return `true` if the existing file should be overwritten and `false` otherwise.
This function may fail while copying files, in such cases, it will leave the
destination directory in a dirty state, where file which have already been
copied won't be removed.
This function may fail while copying files,
in such cases, it will leave the destination
directory in a dirty state, where file which have already been copied
won't be removed.
The function returns `{:ok, files_and_directories}` in case of
success, `files_and_directories` lists all files and directories copied in no
@@ -872,19 +855,6 @@ defmodule File do
explicitly disallow this behaviour. If `source` is a `file` and `destination`
is a directory, `{:error, :eisdir}` will be returned.
## Options
* `:on_conflict` - (since v1.14.0) Invoked when a file already exists in the destination.
The function receives arguments for `source` and `destination`. It should return
`true` if the existing file should be overwritten, `false` if otherwise. The default
callback returns `true`. On earlier versions, this callback could be given as third
argument, but such behaviour is now deprecated.
* `:dereference_symlinks` - (since v1.14.0) By default, this function will copy symlinks
by creating symlinks that point to the same location. This option forces symlinks to be
dereferenced and have their contents copied instead when set to `true`. If the dereferenced
files do not exist, than the operation fails. The default is `false`.
## Examples
# Copies file "a.txt" to "b.txt"
@@ -894,28 +864,14 @@ defmodule File do
File.cp_r("samples", "tmp")
# Same as before, but asks the user how to proceed in case of conflicts
File.cp_r("samples", "tmp", on_conflict: fn source, destination ->
File.cp_r("samples", "tmp", fn source, destination ->
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm. ") == "y\n"
end)
"""
@spec cp_r(Path.t(), Path.t(),
on_conflict: on_conflict_callback,
dereference_symlinks: boolean()
) ::
@spec cp_r(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) ::
{:ok, [binary]} | {:error, posix, binary}
def cp_r(source, destination, options \\ [])
# TODO: Deprecate me on Elixir v1.19
def cp_r(source, destination, callback) when is_function(callback, 2) do
cp_r(source, destination, on_conflict: callback)
end
def cp_r(source, destination, options) when is_list(options) do
on_conflict = Keyword.get(options, :on_conflict, fn _, _ -> true end)
dereference? = Keyword.get(options, :dereference_symlinks, false)
def cp_r(source, destination, callback \\ fn _, _ -> true end) when is_function(callback, 2) do
source =
source
|> IO.chardata_to_string()
@@ -926,7 +882,7 @@ defmodule File do
|> IO.chardata_to_string()
|> assert_no_null_byte!("File.cp_r/3")
case do_cp_r(source, destination, on_conflict, dereference?, []) do
case do_cp_r(source, destination, callback, []) do
{:error, _, _} = error -> error
res -> {:ok, res}
end
@@ -936,12 +892,9 @@ defmodule File do
The same as `cp_r/3`, but raises a `File.CopyError` exception if it fails.
Returns the list of copied files otherwise.
"""
@spec cp_r!(Path.t(), Path.t(),
on_conflict: on_conflict_callback,
dereference_symlinks: boolean()
) :: [binary]
def cp_r!(source, destination, options \\ []) do
case cp_r(source, destination, options) do
@spec cp_r!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: [binary]
def cp_r!(source, destination, callback \\ fn _, _ -> true end) do
case cp_r(source, destination, callback) do
{:ok, files} ->
files
@@ -955,21 +908,15 @@ defmodule File do
end
end
defp do_cp_r(src, dest, on_conflict, dereference?, acc) when is_list(acc) do
defp do_cp_r(src, dest, callback, acc) when is_list(acc) do
case :elixir_utils.read_link_type(src) do
{:ok, :regular} ->
do_cp_file(src, dest, on_conflict, acc)
do_cp_file(src, dest, callback, acc)
{:ok, :symlink} ->
case :file.read_link(src) do
{:ok, link} when dereference? ->
do_cp_r(Path.expand(link, Path.dirname(src)), dest, on_conflict, dereference?, acc)
{:ok, link} ->
do_cp_link(link, src, dest, on_conflict, acc)
{:error, reason} ->
{:error, reason, src}
{:ok, link} -> do_cp_link(link, src, dest, callback, acc)
{:error, reason} -> {:error, reason, src}
end
{:ok, :directory} ->
@@ -978,7 +925,7 @@ defmodule File do
case mkdir(dest) do
success when success in [:ok, {:error, :eexist}] ->
Enum.reduce(files, [dest | acc], fn x, acc ->
do_cp_r(Path.join(src, x), Path.join(dest, x), on_conflict, dereference?, acc)
do_cp_r(Path.join(src, x), Path.join(dest, x), callback, acc)
end)
{:error, reason} ->
@@ -997,8 +944,9 @@ defmodule File do
end
end
# If we reach this clause, there was an error while processing a file.
defp do_cp_r(_, _, _, _, acc) do
# If we reach this clause, there was an error while
# processing a file.
defp do_cp_r(_, _, _, acc) do
acc
end
@@ -1007,14 +955,14 @@ defmodule File do
end
# Both src and dest are files.
defp do_cp_file(src, dest, on_conflict, acc) do
defp do_cp_file(src, dest, callback, acc) do
case :file.copy(src, {dest, [:exclusive]}) do
{:ok, _} ->
copy_file_mode!(src, dest)
[dest | acc]
{:error, :eexist} ->
if path_differs?(src, dest) and on_conflict.(src, dest) do
if path_differs?(src, dest) and callback.(src, dest) do
case copy(src, dest) do
{:ok, _} ->
copy_file_mode!(src, dest)
@@ -1033,13 +981,13 @@ defmodule File do
end
# Both src and dest are files.
defp do_cp_link(link, src, dest, on_conflict, acc) do
defp do_cp_link(link, src, dest, callback, acc) do
case :file.make_symlink(link, dest) do
:ok ->
[dest | acc]
{:error, :eexist} ->
if path_differs?(src, dest) and on_conflict.(src, dest) do
if path_differs?(src, dest) and callback.(src, dest) do
# If rm/1 fails, :file.make_symlink/2 will fail
_ = rm(dest)
@@ -1096,7 +1044,9 @@ defmodule File do
"""
@spec write!(Path.t(), iodata, [mode]) :: :ok
def write!(path, content, modes \\ []) do
case write(path, content, modes) do
modes = normalize_modes(modes, false)
case :file.write_file(path, content, modes) do
:ok ->
:ok
@@ -1244,79 +1194,82 @@ defmodule File do
"""
@spec rm_rf(Path.t()) :: {:ok, [binary]} | {:error, posix, binary}
def rm_rf(path) do
{major, _} = :os.type()
path
|> IO.chardata_to_string()
|> assert_no_null_byte!("File.rm_rf/1")
|> do_rm_rf([], major)
|> do_rm_rf({:ok, []})
end
defp do_rm_rf(path, acc, major) do
case safe_list_dir(path, major) do
defp do_rm_rf(path, {:ok, _} = entry) do
case safe_list_dir(path) do
{:ok, files} when is_list(files) ->
acc =
Enum.reduce(files, acc, fn file, acc ->
# In case we can't delete, continue anyway, we might succeed
# to delete it on Windows due to how they handle symlinks.
case do_rm_rf(Path.join(path, file), acc, major) do
{:ok, acc} -> acc
{:error, _, _} -> acc
end
res =
Enum.reduce(files, entry, fn file, tuple ->
do_rm_rf(Path.join(path, file), tuple)
end)
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> {:ok, acc}
{:error, reason} -> {:error, reason, path}
case res do
{:ok, acc} ->
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> res
{:error, reason} -> {:error, reason, path}
end
reason ->
reason
end
{:ok, :directory} ->
do_rm_directory(path, acc)
do_rm_directory(path, entry)
{:ok, :regular} ->
do_rm_regular(path, acc)
do_rm_regular(path, entry)
{:error, reason} when reason in [:enoent, :enotdir] ->
{:ok, acc}
entry
{:error, reason} ->
{:error, reason, path}
end
end
defp do_rm_regular(path, acc) do
defp do_rm_rf(_, reason) do
reason
end
defp do_rm_regular(path, {:ok, acc} = entry) do
case rm(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> {:ok, acc}
{:error, :enoent} -> entry
{:error, reason} -> {:error, reason, path}
end
end
# On Windows, symlinks are treated as directory and must be removed
# with rmdir/1. But on Unix-like systems, we remove them via rm/1.
# So we first try to remove it as a directory and, if we get :enotdir,
# we fall back to a file removal.
defp do_rm_directory(path, acc) do
# with rmdir/1. But on Unix-like systems, we remove them via rm/1. So we first try
# to remove it as a directory and, if we get :enotdir, we fall back to
# a file removal.
defp do_rm_directory(path, {:ok, acc} = entry) do
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enotdir} -> do_rm_regular(path, acc)
{:error, :enoent} -> {:ok, acc}
{:error, :enotdir} -> do_rm_regular(path, entry)
{:error, :enoent} -> entry
{:error, reason} -> {:error, reason, path}
end
end
defp safe_list_dir(path, major) do
defp safe_list_dir(path) do
case :elixir_utils.read_link_type(path) do
{:ok, :directory} ->
:file.list_dir_all(path)
{:ok, :symlink} when major == :win32 ->
{:ok, :symlink} ->
case :elixir_utils.read_file_type(path) do
{:ok, :directory} -> {:ok, :directory}
_ -> {:ok, :regular}
end
{:ok, :directory} ->
:file.list_dir(path)
{:ok, _} ->
{:ok, :regular}
@@ -1654,7 +1607,7 @@ defmodule File do
:file.close(io_device)
end
@doc ~S"""
@doc """
Returns a `File.Stream` for the given `path` with the given `modes`.
The stream implements both `Enumerable` and `Collectable` protocols,
@@ -1852,8 +1805,7 @@ defmodule File do
defp normalize_modes([], true), do: [:binary]
defp normalize_modes([], false), do: []
defp normalize_path_or_io_device(path) when is_list(path), do: IO.chardata_to_string(path)
defp normalize_path_or_io_device(path) when is_binary(path), do: path
defp normalize_path_or_io_device(io_device) when is_pid(io_device), do: io_device
defp normalize_path_or_io_device(io_device = {:file_descriptor, _, _}), do: io_device
defp maybe_to_string(path) when is_list(path), do: IO.chardata_to_string(path)
defp maybe_to_string(path) when is_binary(path), do: path
defp maybe_to_string(path), do: path
end
+13 -66
View File
@@ -15,7 +15,7 @@ defmodule Float do
## Known issues
There are some very well known problems with floating-point numbers
and arithmetic due to the fact most decimal fractions cannot be
and arithmetics due to the fact most decimal fractions cannot be
represented by a floating-point binary and most operations are not exact,
but operate on approximations. Those issues are not specific
to Elixir, they are a property of floating point representation itself.
@@ -46,31 +46,6 @@ defmodule Float do
@precision_range 0..15
@type precision_range :: 0..15
@min_finite then(<<0xFFEFFFFFFFFFFFFF::64>>, fn <<num::float>> -> num end)
@max_finite then(<<0x7FEFFFFFFFFFFFFF::64>>, fn <<num::float>> -> num end)
@doc """
Returns the maximum finite value for a float.
## Examples
iex> Float.max_finite()
1.7976931348623157e308
"""
def max_finite, do: @max_finite
@doc """
Returns the minimum finite value for a float.
## Examples
iex> Float.min_finite()
-1.7976931348623157e308
"""
def min_finite, do: @min_finite
@doc """
Computes `base` raised to power of `exponent`.
@@ -119,8 +94,7 @@ defmodule Float do
returned.
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
`:error` is returned even though the textual representation itself might be
well formed.
the `ArgumentError` exception is raised.
If you want to convert a string-formatted float directly to a float,
`String.to_float/1` can be used instead.
@@ -136,8 +110,6 @@ defmodule Float do
iex> Float.parse("pi")
:error
iex> Float.parse("1.7976931348623159e+308")
:error
"""
@spec parse(binary) :: {float, binary} | :error
@@ -175,18 +147,7 @@ defmodule Float do
when exp_marker in 'eE' and sign in '-+' and digit in ?0..?9,
do: parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, sign, digit>>)
# When floats are expressed in scientific notation, :erlang.binary_to_float/1 can raise an
# ArgumentError if the e exponent is too big. For example, "1.0e400". Because of this, we
# rescue the ArgumentError here and return an error.
defp parse_unsigned(rest, dot?, true = _e?, acc) do
:erlang.binary_to_float(add_dot(acc, dot?))
rescue
ArgumentError -> :error
else
float -> {float, rest}
end
defp parse_unsigned(rest, dot?, false = _e?, acc),
defp parse_unsigned(rest, dot?, _e?, acc),
do: {:erlang.binary_to_float(add_dot(acc, dot?)), rest}
defp add_dot(acc, true), do: acc
@@ -548,20 +509,13 @@ defmodule Float do
defp sign(1, num), do: -num
@doc """
Returns a charlist which corresponds to the shortest text representation
Returns a charlist which corresponds to the text representation
of the given float.
The underlying algorithm changes depending on the Erlang/OTP version:
* For OTP >= 24, it uses the algorithm presented in "Ryū: fast
float-to-string conversion" in Proceedings of the SIGPLAN '2018
Conference on Programming Language Design and Implementation.
* For OTP < 24, it uses the algorithm presented in "Printing Floating-Point
Numbers Quickly and Accurately" in Proceedings of the SIGPLAN '1996
Conference on Programming Language Design and Implementation.
For a configurable representation, use `:erlang.float_to_list/2`.
It uses the shortest representation according to algorithm described
in "Printing Floating-Point Numbers Quickly and Accurately" in
Proceedings of the SIGPLAN '96 Conference on Programming Language
Design and Implementation.
## Examples
@@ -575,20 +529,13 @@ defmodule Float do
end
@doc """
Returns a binary which corresponds to the shortest text representation
Returns a binary which corresponds to the text representation
of the given float.
The underlying algorithm changes depending on the Erlang/OTP version:
* For OTP >= 24, it uses the algorithm presented in "Ryū: fast
float-to-string conversion" in Proceedings of the SIGPLAN '2018
Conference on Programming Language Design and Implementation.
* For OTP < 24, it uses the algorithm presented in "Printing Floating-Point
Numbers Quickly and Accurately" in Proceedings of the SIGPLAN '1996
Conference on Programming Language Design and Implementation.
For a configurable representation, use `:erlang.float_to_binary/2`.
It uses the shortest representation according to algorithm described
in "Printing Floating-Point Numbers Quickly and Accurately" in
Proceedings of the SIGPLAN '96 Conference on Programming Language
Design and Implementation.
## Examples
+1 -1
View File
@@ -22,7 +22,7 @@ defmodule Function do
It is also possible to capture public module functions and pass them
around as if they were anonymous functions by using the capture
operator `&/1`:
operator `Kernel.SpecialForms.&/1`:
iex> add = &Kernel.+/2
iex> add.(1, 2)
+2 -2
View File
@@ -2,7 +2,7 @@ defmodule GenEvent do
# Functions from this module are deprecated in elixir_dispatch.
@moduledoc """
An event manager with event handlers behaviour.
A event manager with event handlers behaviour.
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
@@ -91,7 +91,7 @@ defmodule GenEvent do
deprecation_message =
"the GenEvent module is deprecated, see its documentation for alternatives"
IO.warn(deprecation_message, __CALLER__)
IO.warn(deprecation_message, Macro.Env.stacktrace(__CALLER__))
quote location: :keep do
@behaviour :gen_event
+43 -54
View File
@@ -153,19 +153,14 @@ defmodule GenServer do
detailed information. The `@doc` annotation immediately preceding
`use GenServer` will be attached to the generated `child_spec/1` function.
When stopping the GenServer, for example by returning a `{:stop, reason, new_state}`
tuple from a callback, the exit reason is used by the supervisor to determine
whether the GenServer needs to be restarted. See the "Exit reasons and restarts"
section in the `Supervisor` module.
## Name registration
Both `start_link/3` and `start/3` support the `GenServer` to register
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`).
@@ -212,7 +207,7 @@ defmodule GenServer do
the GenServer callbacks as doing so will cause the GenServer to misbehave.
Besides the synchronous and asynchronous communication provided by `call/3`
and `cast/2`, "regular" messages sent by functions such as `send/2`,
and `cast/2`, "regular" messages sent by functions such as `Kernel.send/2`,
`Process.send_after/4` and similar, can be handled inside the `c:handle_info/2`
callback.
@@ -259,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
@@ -322,7 +312,7 @@ defmodule GenServer do
## Debugging with the :sys module
GenServers, as [special processes](https://www.erlang.org/doc/design_principles/spec_proc.html),
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](`:sys`).
Through various hooks, this module allows developers to introspect the state of
the process and trace system events that happen during its execution, such as
@@ -412,7 +402,7 @@ defmodule GenServer do
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [`:gen_server` module documentation](`:gen_server`)
* [gen_server Behaviour - OTP Design Principles](https://www.erlang.org/doc/design_principles/gen_server_concepts.html)
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.html)
* [Clients and Servers - Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
"""
@@ -434,10 +424,10 @@ defmodule GenServer do
except the process is hibernated before entering the loop. See
`c:handle_call/3` for more information on hibernation.
Returning `{:ok, state, {:continue, continue_arg}}` is similar to
Returning `{:ok, state, {:continue, continue}}` is similar to
`{:ok, state}` except that immediately after entering the loop,
the `c:handle_continue/2` callback will be invoked with `continue_arg`
as the first argument and `state` as the second one.
the `c:handle_continue/2` callback will be invoked with the value
`continue` as first argument.
Returning `:ignore` will cause `start_link/3` to return `:ignore` and
the process will exit normally without entering the loop or calling
@@ -460,7 +450,7 @@ defmodule GenServer do
"""
@callback init(init_arg :: term) ::
{:ok, state}
| {:ok, state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:ok, state, timeout | :hibernate | {:continue, term}}
| :ignore
| {:stop, reason :: any}
when state: any
@@ -487,10 +477,9 @@ defmodule GenServer do
`GenServer` causes garbage collection and leaves a continuous heap that
minimises the memory used by the process.
Returning `{:reply, reply, new_state, {:continue, continue_arg}}` is similar to
`{:reply, reply, new_state}` except that `c:handle_continue/2` will be invoked
immediately after with `continue_arg` as the first argument and
`state` as the second one.
Returning `{:reply, reply, new_state, {:continue, continue}}` is similar to
`{:reply, reply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with the value `continue` as first argument.
Hibernating should not be used aggressively as too much time could be spent
garbage collecting. Normally it should only be used when a message is not
@@ -513,7 +502,7 @@ defmodule GenServer do
process exits without replying as the caller will be blocking awaiting a
reply.
Returning `{:noreply, new_state, timeout | :hibernate | {:continue, continue_arg}}`
Returning `{:noreply, new_state, timeout | :hibernate | {:continue, continue}}`
is similar to `{:noreply, new_state}` except a timeout, hibernation or continue
occurs as with a `:reply` tuple.
@@ -529,10 +518,9 @@ defmodule GenServer do
"""
@callback handle_call(request :: term, from, state :: term) ::
{:reply, reply, new_state}
| {:reply, reply, new_state,
timeout | :hibernate | {:continue, continue_arg :: term}}
| {:reply, reply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:stop, reason, reply, new_state}
| {:stop, reason, new_state}
when reply: term, new_state: term, reason: term
@@ -553,10 +541,9 @@ defmodule GenServer do
`{:noreply, new_state}` except the process is hibernated before continuing the
loop. See `c:handle_call/3` for more information.
Returning `{:noreply, new_state, {:continue, continue_arg}}` is similar to
Returning `{:noreply, new_state, {:continue, continue}}` is similar to
`{:noreply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with `continue_arg` as the first argument and
`state` as the second one.
immediately after with the value `continue` as first argument.
Returning `{:stop, reason, new_state}` stops the loop and `c:terminate/2` is
called with the reason `reason` and state `new_state`. The process exits with
@@ -567,7 +554,7 @@ defmodule GenServer do
"""
@callback handle_cast(request :: term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:stop, reason :: term, new_state}
when new_state: term
@@ -584,12 +571,12 @@ defmodule GenServer do
"""
@callback handle_info(msg :: :timeout | term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:stop, reason :: term, new_state}
when new_state: term
@doc """
Invoked to handle continue instructions.
Invoked to handle `continue` instructions.
It is useful for performing work after initialization or for splitting the work
in a callback in multiple steps, updating the process state along the way.
@@ -599,11 +586,11 @@ defmodule GenServer do
This callback is optional. If one is not implemented, the server will fail
if a continue instruction is used.
"""
@callback handle_continue(continue_arg, state :: term) ::
@callback handle_continue(continue :: term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg}}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:stop, reason :: term, new_state}
when new_state: term, continue_arg: term
when new_state: term
@doc """
Invoked when the server is about to exit. It should do any cleanup required.
@@ -616,7 +603,7 @@ defmodule GenServer do
does one of the following:
* returns a `:stop` tuple
* raises (via `raise/2`) or exits (via `exit/1`)
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
* returns an invalid value
If part of a supervision tree, a `GenServer` will receive an exit
@@ -683,7 +670,11 @@ defmodule GenServer do
when old_vsn: term | {:down, term}
@doc """
Invoked in some cases to retrieve a formatted version of the `GenServer` status:
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
This callback can be useful to control the *appearance* of the status of the
`GenServer`. For example, it can be used to return a compact representation of
the `GenServer`'s state to avoid having large state terms printed.
* one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the
status of the `GenServer`; in such cases, `reason` is `:normal`
@@ -691,10 +682,6 @@ defmodule GenServer do
* the `GenServer` terminates abnormally and logs an error; in such cases,
`reason` is `:terminate`
This callback can be useful to control the *appearance* of the status of the
`GenServer`. For example, it can be used to return a compact representation of
the `GenServer`'s state to avoid having large state terms printed.
`pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a
list of `{key, value}` tuples representing the current process dictionary of
the `GenServer` and `state` is the current state of the `GenServer`.
@@ -866,7 +853,7 @@ defmodule GenServer do
the arguments given to GenServer.start_link/3 to the server state.
"""
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
quote do
@doc false
@@ -1026,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]}})
@@ -1174,8 +1160,11 @@ defmodule GenServer do
"""
@spec reply(from, term) :: :ok
def reply(client, reply) do
:gen.reply(client, reply)
def reply(client, reply)
def reply({to, tag}, reply) when is_pid(to) do
send(to, {tag, reply})
:ok
end
@doc """
+76 -225
View File
@@ -8,85 +8,16 @@ defprotocol Inspect do
The `Inspect` protocol converts an Elixir data structure into an
algebra document.
This is typically done when you want to customize how your own
structs are inspected in logs and the terminal.
This documentation refers to implementing the `Inspect` protocol
for your own data structures. To learn more about using inspect,
see `Kernel.inspect/2` and `IO.inspect/2`.
## Inspect representation
The `inspect/2` function receives the entity to be inspected
followed by the inspecting options, represented by the struct
`Inspect.Opts`. Building of the algebra document is done with
`Inspect.Algebra`.
There are typically three choices of inspect representation. In order
to understand them, let's imagine we have the following `User` struct:
defmodule User do
defstruct [:id, :name, :address]
end
Our choices are:
1. Print the struct using Elixir's struct syntax, for example:
`%User{address: "Earth", id: 13, name: "Jane"}`. This is the
default representation and best choice if all struct fields
are public.
2. Print using the `#User<...>` notation, for example: `#User<id: 13, name: "Jane", ...>`.
This notation does not emit valid Elixir code and is typically
used when the struct has private fields (for example, you may want
to hide the field `:address` to redact person identifiable information).
3. Print the struct using the expression syntax, for example:
`User.new(13, "Jane", "Earth")`. This assumes there is a `User.new/3`
function. This option is mostly used as an alternative to option 2
for representing custom data structures, such as `MapSet`, `Date.Range`,
and others.
You can implement the Inspect protocol for your own structs while
adhering to the conventions above. Option 1 is the default representation
and you can quickly achieve option 2 by deriving the `Inspect` protocol.
For option 3, you need your custom implementation.
## Deriving
The `Inspect` protocol can be derived to customize the order of fields
(the default is alphabetical) and hide certain fields from structs,
so they don't show up in logs, inspects and similar. The latter is
especially useful for fields containing private information.
The supported options are:
* `:only` - only include the given fields when inspecting.
* `:except` - remove the given fields when inspecting.
* `:optional` - (since v1.14.0) do not include a field if it
matches its default value. This can be used to simplify the
struct representation at the cost of hiding information.
Whenever `:only` or `:except` are used to restrict fields,
the struct will be printed using the `#User<...>` notation,
as the struct can no longer be copy and pasted as valid Elixir
code. Let's see an example:
defmodule User do
@derive {Inspect, only: [:id, :name]}
defstruct [:id, :name, :address]
end
inspect(%User{id: 1, name: "Jane", address: "Earth"})
#=> #User<id: 1, name: "Jane", ...>
If you use only the `:optional` option, the struct will still be
printed as `%User{...}`.
## Custom implementation
You can also define your custom protocol implementation by
defining the `inspect/2` function. The function receives the
entity to be inspected followed by the inspecting options,
represented by the struct `Inspect.Opts`. Building of the
algebra document is done with `Inspect.Algebra`.
## Examples
Many times, inspecting a structure can be implemented in function
of existing entities. For example, here is `MapSet`'s `inspect/2`
@@ -96,24 +27,23 @@ defprotocol Inspect do
import Inspect.Algebra
def inspect(map_set, opts) do
concat(["MapSet.new(", Inspect.List.inspect(MapSet.to_list(map_set), opts), ")"])
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
end
end
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from
`Inspect.Algebra` and it concatenates algebra documents together.
In the example above it is concatenating the string `"MapSet.new("`,
In the example above it is concatenating the string `"#MapSet<"`,
the document returned by `Inspect.Algebra.to_doc/2`, and the final
string `")"`. Therefore, the MapSet with the numbers 1, 2, and 3
will be printed as:
string `">"`. We prefix the module name `#` to denote the inspect
presentation is not actually valid Elixir syntax.
iex> MapSet.new([1, 2, 3], fn x -> x * 2 end)
MapSet.new([2, 4, 6])
Finally, note strings themselves are valid algebra documents that
keep their formatting when pretty printed. This means your `Inspect`
implementation may simply return a string, although that will devoid
it of any pretty-printing.
In other words, `MapSet`'s inspect representation returns an expression
that, when evaluated, builds the `MapSet` itself.
### Error handling
## Error handling
In case there is an error while your structure is being inspected,
Elixir will raise an `ArgumentError` error and will automatically fall back
@@ -125,6 +55,24 @@ defprotocol Inspect do
Inspect.MapSet.inspect(MapSet.new(), %Inspect.Opts{})
## Deriving
The `Inspect` protocol can be derived to hide certain fields from
structs, so they don't show up in logs, inspects and similar. This
is especially useful for fields containing private information.
The options `:only` and `:except` can be used with `@derive` to
specify which fields should and should not appear in the
algebra document:
defmodule User do
@derive {Inspect, only: [:id, :name]}
defstruct [:id, :name, :address]
end
inspect(%User{id: 1, name: "Homer", address: "742 Evergreen Terrace"})
#=> #User<id: 1, name: "Homer", ...>
"""
# Handle structs in Any
@@ -146,7 +94,7 @@ defimpl Inspect, for: Atom do
require Macro
def inspect(atom, opts) do
color(Macro.inspect_atom(:literal, atom), color_key(atom), opts)
color(Identifier.inspect_as_atom(atom), color_key(atom), opts)
end
defp color_key(atom) when is_boolean(atom), do: :boolean
@@ -209,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}
@@ -262,7 +210,7 @@ defimpl Inspect, for: List do
{escaped, _} -> [?', escaped, ?', " ++ ..."]
end
color(IO.iodata_to_binary(inspected), :charlist, opts)
IO.iodata_to_binary(inspected)
keyword?(term) ->
container_doc(open, term, close, opts, &keyword/2, separator: sep, break: :strict)
@@ -274,7 +222,7 @@ defimpl Inspect, for: List do
@doc false
def keyword({key, value}, opts) do
key = color(Macro.inspect_atom(:key, key), :atom, opts)
key = color(Identifier.inspect_as_key(key), :atom, opts)
concat(key, concat(" ", to_doc(value, opts)))
end
@@ -302,33 +250,28 @@ end
defimpl Inspect, for: Map do
def inspect(map, opts) do
list = Map.to_list(map)
fun =
if Inspect.List.keyword?(list) do
&Inspect.List.keyword/2
else
sep = color(" => ", :map, opts)
&to_assoc(&1, &2, sep)
end
map_container_doc(list, "", opts, fun)
inspect(map, "", opts)
end
def inspect(map, name, infos, opts) do
fun = fn %{field: field}, opts -> Inspect.List.keyword({field, Map.get(map, field)}, opts) end
map_container_doc(infos, name, opts, fun)
end
defp to_assoc({key, value}, opts, sep) do
concat(concat(to_doc(key, opts), sep), to_doc(value, opts))
end
defp map_container_doc(list, name, opts, fun) do
def inspect(map, name, opts) do
map = Map.to_list(map)
open = color("%" <> name <> "{", :map, opts)
sep = color(",", :map, opts)
close = color("}", :map, opts)
container_doc(open, list, close, opts, fun, separator: sep, break: :strict)
container_doc(open, map, close, opts, traverse_fun(map, opts), separator: sep, break: :strict)
end
defp traverse_fun(list, opts) do
if Inspect.List.keyword?(list) do
&Inspect.List.keyword/2
else
sep = color(" => ", :map, opts)
&to_map(&1, &2, sep)
end
end
defp to_map({key, value}, opts, sep) do
concat(concat(to_doc(key, opts), sep), to_doc(value, opts))
end
end
@@ -366,31 +309,13 @@ defimpl Inspect, for: Integer do
end
defimpl Inspect, for: Float do
def inspect(float, opts) do
abs = abs(float)
formatted =
if abs >= 1.0 and abs < 1.0e16 and trunc(float) == float do
[Integer.to_string(trunc(float)), ?., ?0]
else
:io_lib_format.fwrite_g(float)
end
color(IO.iodata_to_binary(formatted), :number, opts)
def inspect(term, opts) do
inspected = IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
color(inspected, :number, opts)
end
end
defimpl Inspect, for: Regex do
def inspect(regex = %{opts: regex_opts}, opts) when is_list(regex_opts) do
concat([
"Regex.compile!(",
Inspect.BitString.inspect(regex.source, opts),
", ",
Inspect.List.inspect(regex_opts, opts),
")"
])
end
def inspect(regex, opts) do
{escaped, _} =
regex.source
@@ -422,12 +347,9 @@ defimpl Inspect, for: Function do
name = fun_info[:name]
cond do
not is_atom(mod) ->
"#Function<#{uniq(fun_info)}/#{fun_info[:arity]}>"
fun_info[:type] == :external and fun_info[:env] == [] ->
inspected_as_atom = Macro.inspect_atom(:literal, mod)
inspected_as_function = Macro.inspect_atom(:remote_call, name)
inspected_as_atom = Identifier.inspect_as_atom(mod)
inspected_as_function = Identifier.inspect_as_function(name)
"&#{inspected_as_atom}.#{inspected_as_function}/#{fun_info[:arity]}"
match?('elixir_compiler_' ++ _, Atom.to_charlist(mod)) ->
@@ -443,7 +365,7 @@ defimpl Inspect, for: Function do
end
defp default_inspect(mod, fun_info) do
inspected_as_atom = Macro.inspect_atom(:literal, mod)
inspected_as_atom = Identifier.inspect_as_atom(mod)
extracted_name = extract_name(fun_info[:name])
"#Function<#{uniq(fun_info)}/#{fun_info[:arity]} in #{inspected_as_atom}#{extracted_name}>"
end
@@ -455,10 +377,10 @@ defimpl Inspect, for: Function do
defp extract_name(name) do
case Identifier.extract_anonymous_fun_parent(name) do
{name, arity} ->
"." <> Macro.inspect_atom(:remote_call, name) <> "/" <> arity
"." <> Identifier.inspect_as_function(name) <> "/" <> arity
:error ->
"." <> Macro.inspect_atom(:remote_call, name)
"." <> Identifier.inspect_as_function(name)
end
end
@@ -467,36 +389,6 @@ defimpl Inspect, for: Function do
end
end
defimpl Inspect, for: Inspect.Error do
@impl true
def inspect(%{stacktrace: stacktrace} = inspect_error, _opts) do
message = Exception.message(inspect_error)
format_output(message, stacktrace)
end
defp format_output(message, [_ | _] = stacktrace) do
stacktrace = Exception.format_stacktrace(stacktrace)
"""
#Inspect.Error<
#{Inspect.Error.pad(message, 2)}
Stacktrace:
#{stacktrace}
>\
"""
end
defp format_output(message, []) do
"""
#Inspect.Error<
#{Inspect.Error.pad(message, 2)}
>\
"""
end
end
defimpl Inspect, for: PID do
def inspect(pid, _opts) do
"#PID" <> IO.iodata_to_binary(:erlang.pid_to_list(pid))
@@ -521,11 +413,11 @@ defimpl Inspect, for: Any do
fields = Map.keys(struct) -- [:__exception__, :__struct__]
only = Keyword.get(options, :only, fields)
except = Keyword.get(options, :except, [])
optional = Keyword.get(options, :optional, [])
:ok = validate_option(:only, only, fields, module)
:ok = validate_option(:except, except, fields, module)
:ok = validate_option(:optional, optional, fields, module)
filtered_fields =
fields
|> Enum.reject(&(&1 in except))
|> Enum.filter(&(&1 in only))
inspect_module =
if fields == only and except == [] do
@@ -534,86 +426,45 @@ defimpl Inspect, for: Any do
Inspect.Any
end
filtered_fields =
fields
|> Enum.reject(&(&1 in except))
|> Enum.filter(&(&1 in only))
optional? =
if optional == [] do
false
else
optional_map = for field <- optional, into: %{}, do: {field, Map.fetch!(struct, field)}
quote do
case unquote(Macro.escape(optional_map)) do
%{^var!(field) => var!(default)} ->
var!(default) == Map.get(var!(struct), var!(field))
%{} ->
false
end
end
end
quote do
defimpl Inspect, for: unquote(module) do
def inspect(var!(struct), var!(opts)) do
var!(infos) =
for %{field: var!(field)} = var!(info) <- unquote(module).__info__(:struct),
var!(field) in unquote(filtered_fields) and not unquote(optional?),
do: var!(info)
var!(name) = Macro.inspect_atom(:literal, unquote(module))
unquote(inspect_module).inspect(var!(struct), var!(name), var!(infos), var!(opts))
var!(map) = Map.take(var!(struct), unquote(filtered_fields))
var!(name) = Identifier.inspect_as_atom(unquote(module))
unquote(inspect_module).inspect(var!(map), var!(name), var!(opts))
end
end
end
end
defp validate_option(option, option_list, fields, module) do
case option_list -- fields do
[] ->
:ok
unknown_fields ->
raise ArgumentError,
"unknown fields #{Kernel.inspect(unknown_fields)} in #{Kernel.inspect(option)} " <>
"when deriving the Inspect protocol for #{Kernel.inspect(module)}"
end
end
def inspect(%module{} = struct, opts) do
try do
{module.__struct__(), module.__info__(:struct)}
module.__struct__()
rescue
_ -> Inspect.Map.inspect(struct, opts)
else
{dunder, fields} ->
dunder ->
if Map.keys(dunder) == Map.keys(struct) do
infos =
for %{field: field} = info <- fields,
field not in [:__struct__, :__exception__],
do: info
Inspect.Map.inspect(struct, Macro.inspect_atom(:literal, module), infos, opts)
pruned = Map.drop(struct, [:__struct__, :__exception__])
Inspect.Map.inspect(pruned, Identifier.inspect_as_atom(module), opts)
else
Inspect.Map.inspect(struct, opts)
end
end
end
def inspect(map, name, infos, opts) do
def inspect(map, name, opts) do
map = Map.to_list(map) ++ [:...]
open = color("#" <> name <> "<", :map, opts)
sep = color(",", :map, opts)
close = color(">", :map, opts)
fun = fn
%{field: field}, opts -> Inspect.List.keyword({field, Map.get(map, field)}, opts)
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
:..., _opts -> "..."
end
container_doc(open, infos ++ [:...], close, opts, fun, separator: sep, break: :strict)
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
end
end
+27 -113
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
@@ -53,7 +53,7 @@ defmodule Inspect.Opts do
* `:safe` - when `false`, failures while inspecting structs will be raised
as errors instead of being wrapped in the `Inspect.Error` exception. This
is useful when debugging failures and crashes for custom inspect
implementations. Defaults to `true`.
implementations.
* `:structs` - when `false`, structs are not formatted by the inspect
protocol, they are instead printed as maps. Defaults to `true`.
@@ -64,7 +64,6 @@ defmodule Inspect.Opts do
`:atom`, `:binary`, `:boolean`, `:list`, `:map`, `:number`, `:regex`,
`:string`, and `:tuple`. Custom data types may provide their own options.
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
A default list of colors can be retrieved from `IO.ANSI.syntax_colors/0`.
* `:width` - number of characters per line used when pretty is `true` or when
printing to IO devices. Set to `0` to force each item to be printed on its
@@ -90,9 +89,11 @@ defmodule Inspect.Opts do
@type color_key :: atom
# TODO: Remove :char_lists key and :as_char_lists value on v2.0
@type t :: %__MODULE__{
base: :decimal | :binary | :hex | :octal,
binaries: :infer | :as_binaries | :as_strings,
char_lists: :infer | :as_lists | :as_char_lists,
charlists: :infer | :as_lists | :as_charlists,
custom_options: keyword,
inspect_fun: (any, t -> Inspect.Algebra.t()),
@@ -104,62 +105,13 @@ defmodule Inspect.Opts do
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
width: non_neg_integer | :infinity
}
end
@doc """
Builds an `Inspect.Opts` struct.
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
@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
defexception [:message]
end
defmodule Inspect.Algebra do
@@ -254,18 +206,12 @@ defmodule Inspect.Algebra do
| doc_group
| doc_nest
| doc_string
| doc_limit
@typep doc_string :: {:doc_string, t, non_neg_integer}
defmacrop doc_string(string, length) do
quote do: {:doc_string, unquote(string), unquote(length)}
end
@typep doc_limit :: {:doc_limit, t, pos_integer | :infinity}
defmacrop doc_limit(doc, limit) do
quote do: {:doc_limit, unquote(doc), unquote(limit)}
end
@typep doc_cons :: {:doc_cons, t, t}
defmacrop doc_cons(left, right) do
quote do: {:doc_cons, unquote(left), unquote(right)}
@@ -315,8 +261,7 @@ defmodule Inspect.Algebra do
:doc_force,
:doc_group,
:doc_nest,
:doc_string,
:doc_limit
:doc_string
]
defguard is_doc(doc)
@@ -353,28 +298,19 @@ defmodule Inspect.Algebra do
try do
Process.put(:inspect_trap, true)
inspected_struct =
struct
|> Inspect.Map.inspect(%{
opts
| syntax_colors: [],
inspect_fun: Inspect.Opts.default_inspect_fun()
})
|> format(opts.width)
|> IO.iodata_to_binary()
res = Inspect.Map.inspect(struct, %{opts | syntax_colors: []})
res = IO.iodata_to_binary(format(res, :infinity))
inspect_error =
Inspect.Error.exception(
exception: caught_exception,
stacktrace: __STACKTRACE__,
inspected_struct: inspected_struct
)
message =
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(Exception.message(caught_exception))} while inspecting #{res}"
exception = Inspect.Error.exception(message: message)
if opts.safe do
opts = %{opts | inspect_fun: Inspect.Opts.default_inspect_fun()}
Inspect.inspect(inspect_error, opts)
Inspect.inspect(exception, opts)
else
reraise(inspect_error, __STACKTRACE__)
reraise(exception, __STACKTRACE__)
end
after
Process.delete(:inspect_trap)
@@ -468,9 +404,8 @@ defmodule Inspect.Algebra do
defp container_each([term | terms], limit, opts, fun, acc, simple?)
when is_list(terms) and is_limit(limit) do
new_limit = decrement(limit)
doc = fun.(term, %{opts | limit: new_limit})
limit = if doc == :doc_nil, do: limit, else: new_limit
limit = decrement(limit)
doc = fun.(term, %{opts | limit: limit})
container_each(terms, limit, opts, fun, [doc | acc], simple? and simple?(doc))
end
@@ -582,10 +517,6 @@ defmodule Inspect.Algebra do
doc_cons(doc1, doc2)
end
def no_limit(doc) do
doc_limit(doc, :infinity)
end
@doc ~S"""
Concatenates a list of documents returning a new document.
@@ -605,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)
@@ -635,7 +566,7 @@ defmodule Inspect.Algebra do
["hello", "\n ", "world"]
"""
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest | t
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest
def nest(doc, level, mode \\ :always)
def nest(doc, :cursor, mode) when is_doc(doc) and mode in [:always, :break] do
@@ -976,7 +907,7 @@ defmodule Inspect.Algebra do
@typep mode :: :flat | :flat_no_break | :break | :break_no_flat
@spec fits?(
width :: non_neg_integer() | :infinity,
width :: non_neg_integer(),
column :: non_neg_integer(),
break? :: boolean(),
entries
@@ -1041,17 +972,9 @@ defmodule Inspect.Algebra do
defp fits?(w, k, b?, [{i, m, doc_group(x, _)} | t]),
do: fits?(w, k, b?, [{i, m, x} | {:tail, b?, t}])
defp fits?(w, k, b?, [{i, m, doc_limit(x, :infinity)} | t]) when w != :infinity,
do: fits?(:infinity, k, b?, [{i, :flat, x}, {i, m, doc_limit(empty(), w)} | t])
defp fits?(_w, k, b?, [{i, m, doc_limit(x, w)} | t]),
do: fits?(w, k, b?, [{i, m, x} | t])
@spec format(
width :: non_neg_integer() | :infinity,
column :: non_neg_integer(),
[{integer, mode, t}]
) :: [binary]
@spec format(width :: non_neg_integer() | :infinity, column :: non_neg_integer(), [
{integer, mode, t}
]) :: [binary]
defp format(_, _, []), do: []
defp format(w, k, [{_, _, :doc_nil} | t]), do: format(w, k, t)
defp format(w, _, [{i, _, :doc_line} | t]), do: [indent(i) | format(w, i, t)]
@@ -1105,15 +1028,6 @@ defmodule Inspect.Algebra do
end
end
# Limit is set to infinity and then reverts
defp format(w, k, [{i, m, doc_limit(x, :infinity)} | t]) when w != :infinity do
format(:infinity, k, [{i, :flat, x}, {i, m, doc_limit(empty(), w)} | t])
end
defp format(_w, k, [{i, m, doc_limit(x, w)} | t]) do
format(w, k, [{i, m, x} | t])
end
defp collapse(["\n" <> _ | t], max, count, i) do
collapse(t, max, count + 1, i)
end
+18 -12
View File
@@ -4,11 +4,11 @@ defmodule Integer do
Some functions that work on integers are found in `Kernel`:
* `Kernel.abs/1`
* `Kernel.div/2`
* `Kernel.max/2`
* `Kernel.min/2`
* `Kernel.rem/2`
* `abs/1`
* `div/2`
* `max/2`
* `min/2`
* `rem/2`
"""
@@ -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)
+55 -110
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,20 +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 as specified by the `line_or_chars` argument:
* if `line_or_chars` is an integer, it represents a number of bytes. The device is
iterated by that number of bytes.
* if `line_or_chars` is `:line`, the device is iterated line by line.
* if `line_or_chars` is `:eof`, the device is iterated until `:eof`. `line_or_chars`
can only be `:eof` since Elixir 1.13.0. `:eof` replaces the deprecated `:all`,
with the difference that `:all` returns `""` on end of file, while `:eof` returns
`:eof` itself.
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:
@@ -194,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
@@ -224,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
@@ -290,29 +293,19 @@ 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
@doc """
Writes a `message` to stderr, along with the given `stacktrace_info`.
The `stacktrace_info` must be one of:
* a `__STACKTRACE__`, where all entries in the stacktrace will be
included in the error message
* a `Macro.Env` structure (since v1.14.0), where a single stacktrace
entry from the compilation environment will be used
* a keyword list with at least the `:file` option representing
a single stacktrace entry (since v1.14.0). The `:line`, `:module`,
`:function` options are also supported
Writes a `message` to stderr, along with the given `stacktrace`.
This function also notifies the compiler a warning was printed
(in case --warnings-as-errors was enabled). It returns `:ok`
if it succeeds.
An empty list can be passed to avoid stacktrace printing.
## Examples
stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
@@ -321,34 +314,10 @@ defmodule IO do
#=> my_app.ex:4: MyApp.main/1
"""
@spec warn(chardata | String.Chars.t(), Exception.stacktrace() | keyword() | Macro.Env.t()) ::
:ok
def warn(message, stacktrace_info)
@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)
end
def warn(message, %Macro.Env{} = env) do
warn(message, Macro.Env.stacktrace(env))
end
def warn(message, [{_, _} | _] = keyword) do
if file = keyword[:file] do
warn(
message,
%{
__ENV__
| module: keyword[:module],
function: keyword[:function],
line: keyword[:line],
file: file
}
)
else
warn(message, [])
end
:elixir_errors.io_warn(0, nil, message, message)
end
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
@@ -357,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,
@@ -459,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])
@@ -476,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
@@ -514,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`.
@@ -588,7 +535,7 @@ defmodule IO do
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
`stream/0` has been introduced in Elixir v1.12.0,
`stream/1` has been introduced in Elixir v1.12.0,
while `stream/2` has been available since v1.0.0.
## Examples
@@ -633,7 +580,7 @@ defmodule IO do
Finally, do not use this function on IO devices in Unicode
mode as it will return the wrong result.
`binstream/0` has been introduced in Elixir v1.12.0,
`binstream/1` has been introduced in Elixir v1.12.0,
while `binstream/2` has been available since v1.0.0.
"""
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
@@ -665,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
+4 -35
View File
@@ -3,7 +3,6 @@ defmodule IO.ANSI.Sequence do
defmacro defsequence(name, code, terminator \\ "m") do
quote bind_quoted: [name: name, code: code, terminator: terminator] do
@spec unquote(name)() :: String.t()
def unquote(name)() do
"\e[#{unquote(code)}#{unquote(terminator)}"
end
@@ -69,34 +68,6 @@ defmodule IO.ANSI do
Application.get_env(:elixir, :ansi_enabled, false)
end
@doc """
Syntax colors to be used by `Inspect`.
Those colors are used throughout Elixir's standard library,
such as `dbg/2` and `IEx`.
The colors can be changed by setting the `:ansi_syntax_colors`
in the `:elixir` application configuration. Configuration for
most built-in data types are supported: `:atom`, `:binary`,
`:boolean`, `:charlist`, `:list`, `:map`, `:nil`, `:number`,
`:string`, and `:tuple`. The default is:
[
atom: :cyan
boolean: :magenta,
charlist: :yellow,
nil: :magenta,
number: :yellow,
string: :green
]
"""
@doc since: "1.14.0"
@spec syntax_colors :: Keyword.t(ansidata)
def syntax_colors do
Application.fetch_env!(:elixir, :ansi_syntax_colors)
end
@doc "Sets foreground color."
@spec color(0..255) :: String.t()
def color(code) when code in 0..255, do: "\e[38;5;#{code}m"
@@ -280,9 +251,8 @@ defmodule IO.ANSI do
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
"""
@spec format(ansidata, boolean) :: IO.chardata()
def format(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(ansidata, [], [], emit?, :maybe)
def format(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(chardata, [], [], emit?, :maybe)
end
@doc ~S"""
@@ -301,9 +271,8 @@ defmodule IO.ANSI do
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
"""
@spec format_fragment(ansidata, boolean) :: IO.chardata()
def format_fragment(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(ansidata, [], [], emit?, false)
def format_fragment(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(chardata, [], [], emit?, false)
end
defp do_format([term | rest], rem, acc, emit?, append_reset) do
+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
+1 -5
View File
@@ -26,11 +26,7 @@ defmodule IO.Stream do
defstruct device: nil, raw: true, line_or_bytes: :line
@type t :: %__MODULE__{
device: IO.device(),
raw: boolean(),
line_or_bytes: :line | non_neg_integer()
}
@type t :: %__MODULE__{}
@doc false
def __build__(device, raw, line_or_bytes) do
+345 -769
View File
File diff suppressed because it is too large Load Diff
+20 -41
View File
@@ -13,12 +13,9 @@ defmodule Kernel.CLI do
pa: [],
pz: [],
verbose_compile: false,
profile: nil,
pry: false
profile: nil
}
@standalone_opts ["-h", "--help", "--short-version"]
@doc """
This is the API invoked by Elixir boot process.
"""
@@ -29,10 +26,6 @@ defmodule Kernel.CLI do
System.argv(argv)
System.no_halt(config.no_halt)
if config.pry do
Application.put_env(:elixir, :dbg_callback, {IEx.Pry, :dbg, []})
end
fun = fn _ ->
errors = process_commands(config)
@@ -95,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]
_ ->
@@ -183,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
@@ -201,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)
@@ -223,16 +216,7 @@ defmodule Kernel.CLI do
# Parse shared options
defp halt_standalone(opt) do
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
System.halt(1)
end
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
halt_standalone(opt)
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
@@ -240,11 +224,7 @@ defmodule Kernel.CLI do
IO.puts("Elixir " <> System.build_info()[:build])
end
if t != [] do
halt_standalone(opt)
else
System.halt(0)
end
System.halt(0)
end
defp parse_shared(["-pa", h | t], config) do
@@ -280,11 +260,6 @@ defmodule Kernel.CLI do
parse_shared(t, %{config | commands: [{:rpc_eval, node, h} | config.commands]})
end
defp parse_shared(["--rpc-eval" | _], config) do
new_config = %{config | errors: ["--rpc-eval : wrong number of arguments" | config.errors]}
{[], new_config}
end
defp parse_shared(["-r", h | t], config) do
parse_shared(t, %{config | commands: [{:require, h} | config.commands]})
end
@@ -324,7 +299,7 @@ defmodule Kernel.CLI do
end
defp parse_argv(["+iex" | t], config) do
parse_iex(t, %{config | pry: true})
parse_iex(t, config)
end
defp parse_argv(["-S", h | t], config) do
@@ -409,16 +384,20 @@ defmodule Kernel.CLI do
{config, t}
end
# This clause is here so that Kernel.CLI does not
# error out with "unknown option"
defp parse_iex(["--dot-iex", _ | t], config) do
parse_iex(t, config)
end
defp parse_iex([opt, _ | t], config) when opt in ["--remsh"] do
parse_iex(t, config)
end
defp parse_iex(["-S", h | t], config) do
{%{config | commands: [{:script, h} | config.commands]}, t}
end
# These clauses are here so that Kernel.CLI does not error out with "unknown option"
defp parse_iex(["--dot-iex", _ | t], config), do: parse_iex(t, config)
defp parse_iex(["--remsh", _ | t], config), do: parse_iex(t, config)
defp parse_iex(["--no-pry" | t], config), do: parse_iex(t, %{config | pry: false})
defp parse_iex([h | t] = list, config) do
case h do
"-" <> _ -> shared_option?(list, config, &parse_iex(&1, &2))
+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
+56 -68
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
@@ -59,11 +59,6 @@ defmodule Kernel.LexicalTracker do
:gen_server.cast(pid, {:alias_dispatch, module})
end
@doc false
def import_quoted(pid, module, function, arities) when is_atom(module) do
:gen_server.cast(pid, {:import_quoted, module, function, arities})
end
@doc false
def add_compile_env(pid, app, path, return) do
:gen_server.cast(pid, {:compile_env, app, path, return})
@@ -93,24 +88,23 @@ defmodule Kernel.LexicalTracker do
@doc false
def collect_unused_imports(pid) do
unused(pid, :unused_imports)
unused(pid, :import)
end
@doc false
def collect_unused_aliases(pid) do
unused(pid, :unused_aliases)
unused(pid, :alias)
end
defp unused(pid, tag) do
:gen_server.call(pid, tag, @timeout)
:gen_server.call(pid, {:unused, tag}, @timeout)
end
# Callbacks
def init(:ok) do
state = %{
aliases: %{},
imports: %{},
directives: %{},
references: %{},
exports: %{},
cache: %{},
@@ -122,12 +116,13 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def handle_call(:unused_aliases, _from, state) do
{:reply, Enum.sort(state.aliases), state}
end
def handle_call({:unused, tag}, _from, state) do
directives =
for {{^tag, module_or_mfa}, marker} <- state.directives, is_integer(marker) do
{module_or_mfa, marker}
end
def handle_call(:unused_imports, _from, state) do
{:reply, Enum.sort(state.imports), state}
{:reply, Enum.sort(directives), state}
end
def handle_call(:references, _from, state) do
@@ -153,44 +148,12 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
%{imports: imports, references: references} = state
imports =
case imports do
%{^module => modules_and_fas} ->
modules_and_fas
|> Map.delete(module)
|> Map.delete({function, arity})
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
references = add_reference(references, module, mode)
{:noreply, %{state | imports: imports, references: references}}
state = add_import_dispatch(state, module, function, arity, mode)
{:noreply, state}
end
def handle_cast({:alias_dispatch, module}, %{aliases: aliases} = state) do
{:noreply, %{state | aliases: Map.delete(aliases, module)}}
end
def handle_cast({:import_quoted, module, function, arities}, state) do
%{imports: imports} = state
imports =
case imports do
%{^module => modules_and_fas} ->
arities
|> Enum.reduce(modules_and_fas, &Map.delete(&2, {function, &1}))
|> Map.delete(module)
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
{:noreply, %{state | imports: imports}}
def handle_cast({:alias_dispatch, module}, state) do
{:noreply, %{state | directives: add_dispatch(state.directives, module, :alias)}}
end
def handle_cast({:set_file, file}, state) do
@@ -205,25 +168,28 @@ 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
if warn do
imports = for module_or_fa <- [module | fas], do: {module_or_fa, line}, into: %{}
{:noreply, put_in(state.imports[module], imports)}
else
{:noreply, state}
end
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
directives =
state.directives
|> Map.drop(to_remove)
|> add_directive(module, line, warn, :import)
directives =
Enum.reduce(fas, directives, fn {function, arity}, directives ->
add_directive(directives, {module, function, arity}, line, warn, :import)
end)
{:noreply, %{state | directives: directives}}
end
def handle_cast({:add_alias, module, line, warn}, state) do
if warn do
{:noreply, put_in(state.aliases[module], line)}
else
{:noreply, state}
end
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
end
@doc false
@@ -255,9 +221,31 @@ 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
defp add_import_dispatch(state, module, function, arity, mode) do
directives =
state.directives
|> add_dispatch(module, :import)
|> add_dispatch({module, function, arity}, :import)
references = add_reference(state.references, module, mode)
%{state | directives: directives, references: references}
end
# In the map we keep imports and aliases.
# If the value is a line, it was imported/aliased and has a pending warning
# If the value is true, it was imported/aliased and used
defp add_directive(directives, module_or_mfa, line, warn, tag) do
marker = if warn, do: line, else: true
Map.put(directives, {tag, module_or_mfa}, marker)
end
defp add_dispatch(directives, module_or_mfa, tag) do
Map.put(directives, {tag, module_or_mfa}, true)
end
end
+87 -128
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() | {pos_integer(), column :: non_neg_integer}
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
@type error() :: {file :: Path.t(), location(), 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)
{_parent, 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
@@ -241,7 +204,7 @@ defmodule Kernel.ParallelCompiler do
defp write_module_binaries(result, {:compile, path}, timestamp) do
Enum.flat_map(result, fn
{{:module, module}, binary} ->
{{:module, module}, {binary, _map}} ->
full_path = Path.join(path, Atom.to_string(module) <> ".beam")
File.write!(full_path, binary)
if timestamp, do: File.touch!(full_path, timestamp)
@@ -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}, _} <- result,
do: module
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, 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,
@@ -479,45 +452,37 @@ defmodule Kernel.ParallelCompiler do
Enum.count(result, &match?({{:module, _}, _}, &1))
end
# TODO: Deprecate other returns on v1.14
defp each_cycle_return({kind, modules, warnings}), do: {kind, modules, warnings}
defp each_cycle_return(other) do
IO.warn(
"the :each_cycle callback must return a tuple of format {:compile | :runtime, modules, warnings}"
)
case other do
{kind, modules} -> {kind, modules, []}
modules when is_list(modules) -> {:compile, modules, []}
end
end
defp each_cycle_return({kind, modules}), do: {kind, modules, []}
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules, []}
# 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
nilify_empty(
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}
)
end
defp deadlocked(waiting, type, defining?) do
nilify_empty(
for {_, _, ref, _, on, _, ^type} <- waiting,
nillify_empty(
for {_, _, ref, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
)
end
defp defining?(on, waiting) do
Enum.any?(waiting, fn {_, _, _, _, _, defining, _} -> on in defining end)
Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end)
end
defp nilify_empty([]), do: nil
defp nilify_empty([_ | _] = list), do: list
defp nillify_empty([]), do: nil
defp nillify_empty([_ | _] = list), do: list
# Wait for messages from child processes
defp wait_for_messages(queue, spawned, waiting, files, result, warnings, state) do
@@ -530,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} ->
{: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)
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)
@@ -585,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} ->
@@ -710,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)
@@ -757,12 +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}
end
defp get_line(_file, %{line: line, column: column}, _stack)
when is_integer(line) and line > 0 and is_integer(column) and column >= 0 do
{line, column}
{file, line, message}
end
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
@@ -775,8 +735,7 @@ defmodule Kernel.ParallelCompiler do
end
end
defp get_line(file, _reason, [{_, _, _, [file: expanding]}, {_, _, _, info} | _])
when expanding in ['expanding macro', 'expanding struct'] do
defp get_line(file, _reason, [{_, _, _, [file: 'expanding macro']}, {_, _, _, info} | _]) do
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
Keyword.get(info, :line)
end
+14 -119
View File
@@ -182,7 +182,7 @@ defmodule Kernel.SpecialForms do
<<1, 2, 3>>
Elixir also accepts by default the segment to be a literal
string which expands to integers:
string or a literal charlist, which are by default expanded to integers:
iex> <<0, "foo">>
<<0, 102, 111, 111>>
@@ -246,20 +246,20 @@ defmodule Kernel.SpecialForms do
iex> {name, species}
{"Frank", "Walrus"}
The size can be a variable or any valid guard expression:
The size can be a variable:
iex> name_size = 5
iex> <<name::binary-size(name_size), " the ", species::binary>> = <<"Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
The size can access prior variables defined in the binary itself:
And the variable can be defined in the match itself (prior to its use):
iex> <<name_size::size(8), name::binary-size(name_size), " the ", species::binary>> = <<5, "Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
However, it cannot access variables defined in the match outside of the binary/bitstring:
However, the size cannot be defined in the match outside the binary/bitstring match:
{name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, <<"Frank the Walrus">>}
** (CompileError): undefined variable "name_size" in bitstring segment
@@ -366,7 +366,7 @@ defmodule Kernel.SpecialForms do
To learn more about specific optimizations and performance considerations,
check out the
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
"""
defmacro unquote(:<<>>)(args), do: error!([args])
@@ -604,12 +604,11 @@ defmodule Kernel.SpecialForms do
import List
A developer can filter to import only functions, macros, or sigils
(which can be functions or macros) via the `:only` option:
A developer can filter to import only macros or functions via
the only option:
import List, only: :functions
import List, only: :macros
import Kernel, only: :sigils
Alternatively, Elixir allows a developer to pass pairs of
name/arities to `:only` or `:except` as a fine grained control
@@ -779,7 +778,7 @@ defmodule Kernel.SpecialForms do
<<int::integer-little, rest::bits>> = bits
Read the documentation on the [Typespecs page](typespecs.md) and
Read the documentation on the `Typespec` page and
`<<>>/1` for more information on typespecs and
bitstrings respectively.
"""
@@ -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
@@ -1316,13 +1309,11 @@ defmodule Kernel.SpecialForms do
sum(1, value, 3)
end
Which the argument for the `:sum` function call is not the
expected result:
Which would then return:
{:sum, [], [1, {:value, [], Elixir}, 3]}
For this, we use `unquote`:
Which is not the expected result. For this, we use `unquote`:
iex> value =
...> quote do
@@ -1389,10 +1380,6 @@ defmodule Kernel.SpecialForms do
iex> for n <- [1, 2, 3, 4, 5, 6], rem(n, 2) == 0, do: n
[2, 4, 6]
Filters must evaluate to truthy values (everything but `nil`
and `false`). If a filter is falsy, then the current value is
discarded.
Generators can also be used to filter as it removes any value
that doesn't match the pattern on the left side of `<-`:
@@ -1413,35 +1400,6 @@ defmodule Kernel.SpecialForms do
filters or inside the block, are not reflected outside of the
comprehension.
Variable assignments inside filters must still return a truthy value,
otherwise values are discarded. Let's see an example. Imagine you have
a keyword list where the key is a programming language and the value
is its direct parent. Then let's try to compute the grandparent of each
language. You could try this:
iex> languages = [elixir: :erlang, erlang: :prolog, prolog: nil]
iex> for {language, parent} <- languages, grandparent = languages[parent], do: {language, grandparent}
[elixir: :prolog]
Given the grandparents of Erlang and Prolog were nil, those values were
filtered out. If you don't want this behaviour, a simple option is to
move the filter inside the do-block:
iex> languages = [elixir: :erlang, erlang: :prolog, prolog: nil]
iex> for {language, parent} <- languages do
...> grandparent = languages[parent]
...> {language, grandparent}
...> end
[elixir: :prolog, erlang: nil, prolog: nil]
However, such option is not always available, as you may have further
filters. An alternative is to convert the filter into a generator by
wrapping the right side of `=` in a list:
iex> languages = [elixir: :erlang, erlang: :prolog, prolog: nil]
iex> for {language, parent} <- languages, grandparent <- [languages[parent]], do: {language, grandparent}
[elixir: :prolog, erlang: nil, prolog: nil]
## The `:into` and `:uniq` options
In the examples above, the result returned by the comprehension was
@@ -1561,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(
@@ -1582,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:
@@ -1606,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_extension(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])
@@ -2093,25 +2007,6 @@ defmodule Kernel.SpecialForms do
File.rm("tmp/story.txt")
end
Although `after` clauses are invoked whether or not there was an error, they do not
modify the return value. All of the following examples return `:return_me`:
try do
:return_me
after
IO.puts("I will be printed")
:not_returned
end
try do
raise "boom"
rescue
_ -> :return_me
after
IO.puts("I will be printed")
:not_returned
end
## `else` clauses
`else` clauses allow the result of the body passed to `try/1` to be pattern
+11 -22
View File
@@ -104,7 +104,7 @@ defmodule Kernel.Typespec do
@doc """
Defines a typespec.
Invoked by `@/1` expansion.
Invoked by `Kernel.@/1` expansion.
"""
def deftypespec(:spec, expr, _line, _file, module, pos) do
{_set, bag} = :elixir_module.data_tables(module)
@@ -194,14 +194,14 @@ defmodule Kernel.Typespec do
defp get_doc_info(set, attr, line) do
case :ets.take(set, attr) do
[{^attr, {line, doc}, _, _}] -> {line, doc}
[{^attr, {line, doc}, _}] -> {line, doc}
[] -> {line, nil}
end
end
defp get_doc_meta(spec_meta, doc_kind, set) do
case :ets.take(set, {doc_kind, :meta}) do
[{{^doc_kind, :meta}, metadata}] -> Map.merge(metadata, spec_meta)
[{{^doc_kind, :meta}, metadata, _}] -> Map.merge(metadata, spec_meta)
[] -> spec_meta
end
end
@@ -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
@@ -567,10 +569,7 @@ defmodule Kernel.Typespec do
types =
:lists.map(
fn
{:__exception__ = field, true} -> {field, Keyword.get(fields, field, true)}
{field, _} -> {field, Keyword.get(fields, field, quote(do: term()))}
end,
fn {field, _} -> {field, Keyword.get(fields, field, quote(do: term()))} end,
:lists.sort(struct)
)
@@ -578,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
@@ -681,20 +680,10 @@ defmodule Kernel.Typespec do
end
end
defp typespec({:"::", meta, [left, right]}, vars, caller, state) do
defp typespec({:"::", meta, [left, right]} = expr, vars, caller, state) do
message =
"invalid type annotation. The left side of :: must be a variable, got: #{Macro.to_string(left)}"
message =
case left do
{:|, _, _} ->
message <>
". Note \"left | right :: ann\" is the same as \"(left | right) :: ann\". " <>
"To solve this, use parentheses around the union operands: \"left | (right :: ann)\""
_ ->
message
end
"invalid type annotation. When using the | operator to represent the union of types, " <>
"make sure to wrap type annotations in parentheses: #{Macro.to_string(expr)}"
# TODO: Make this an error on v2.0, and remove the code below and
# the :undefined_type_error_enabled? key from the state
+14 -121
View File
@@ -22,52 +22,16 @@ defmodule Kernel.Utils do
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
@doc """
Callback for defdelegate entry point.
Callback for defdelegate.
"""
def defdelegate_all(funs, opts, env) do
to = Keyword.get(opts, :to) || raise ArgumentError, "expected to: to be given as argument"
as = Keyword.get(opts, :as)
if to == env.module and is_nil(as) do
raise ArgumentError,
"defdelegate function is calling itself, which will lead to an infinite loop. You should either change the value of the :to option or specify the :as option"
end
if is_list(funs) do
IO.warn(
"passing a list to Kernel.defdelegate/2 is deprecated, please define each delegate separately",
Macro.Env.stacktrace(env)
)
end
if Keyword.has_key?(opts, :append_first) do
IO.warn(
"Kernel.defdelegate/2 :append_first option is deprecated",
Macro.Env.stacktrace(env)
)
end
to
end
@doc """
Callback for each function in defdelegate.
"""
def defdelegate_each(fun, opts) when is_list(opts) do
def defdelegate(fun, opts) when is_list(opts) do
# TODO: Remove on v2.0
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)
@@ -100,15 +64,7 @@ defmodule Kernel.Utils do
@doc """
Callback for defstruct.
"""
def defstruct(module, fields, bootstrapped?) do
{set, bag} = :elixir_module.data_tables(module)
if :ets.member(set, :__struct__) do
raise ArgumentError,
"defstruct has already been called for " <>
"#{Kernel.inspect(module)}, defstruct can only be called once per module"
end
def defstruct(module, fields) do
case fields do
fs when is_list(fs) ->
:ok
@@ -120,7 +76,7 @@ defmodule Kernel.Utils do
mapper = fn
{key, val} when is_atom(key) ->
try do
:elixir_quote.escape(val, false, :none)
Macro.escape(val)
rescue
e in [ArgumentError] ->
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
@@ -136,20 +92,7 @@ defmodule Kernel.Utils do
end
fields = :lists.map(mapper, fields)
enforce_keys =
case :ets.lookup(set, :enforce_keys) do
[{_, enforce_keys, _, _}] when is_list(enforce_keys) ->
:ets.update_element(set, :enforce_keys, {3, :used})
enforce_keys
[{_, enforce_key, _, _}] ->
:ets.update_element(set, :enforce_keys, {3, :used})
[enforce_key]
[] ->
[]
end
enforce_keys = List.wrap(Module.get_attribute(module, :enforce_keys))
# TODO: Make it raise on v2.0
warn_on_duplicate_struct_key(:lists.keysort(1, fields))
@@ -163,62 +106,12 @@ defmodule Kernel.Utils do
end
:lists.foreach(foreach, enforce_keys)
struct = :maps.put(:__struct__, module, :maps.from_list(fields))
body =
case bootstrapped? do
true ->
case enforce_keys do
[] ->
quote do
Enum.reduce(kv, @__struct__, fn {key, val}, map ->
%{map | key => val}
end)
end
_ ->
quote do
{map, keys} =
Enum.reduce(kv, {@__struct__, unquote(enforce_keys)}, fn
{key, val}, {map, keys} ->
{%{map | key => val}, List.delete(keys, key)}
end)
case keys do
[] ->
map
_ ->
raise ArgumentError,
"the following keys must also be given when building " <>
"struct #{inspect(__MODULE__)}: #{inspect(keys)}"
end
end
end
false ->
quote do
:lists.foldl(
fn {key, val}, acc -> %{acc | key => val} end,
@__struct__,
kv
)
end
end
case enforce_keys -- :maps.keys(struct) do
[] ->
# The __struct__ field is used for expansion and for loading remote structs
:ets.insert(set, {:__struct__, struct, nil, []})
# Store all field metadata to go into __info__(:struct)
mapper = fn {key, val} ->
%{field: key, default: val, required: :lists.member(key, enforce_keys)}
end
:ets.insert(set, {{:elixir, :struct}, :lists.map(mapper, fields)})
derive = :lists.map(fn {_, value} -> value end, :ets.take(bag, {:accumulate, :derive}))
{struct, :lists.reverse(derive), quote(do: kv), body}
{struct, enforce_keys, Module.get_attribute(module, :derive)}
error_keys ->
raise ArgumentError,
@@ -244,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
@@ -326,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
@@ -375,7 +268,7 @@ defmodule Kernel.Utils do
%{} ->
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
new_var = Macro.unique_var(generated, Elixir)
new_var = Macro.var(generated, Elixir)
{new_var, Map.put(acc, pair, {new_var, var})}
end
+129 -394
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}]}
@@ -92,7 +94,6 @@ defmodule Keyword do
iex> %{1 => 2, foo: :bar}
%{1 => 2, :foo => :bar}
"""
@compile :inline_list_funcs
@@ -104,24 +105,7 @@ defmodule Keyword do
@type t(value) :: [{key, value}]
@doc """
Builds a keyword from the given `keys` and the fixed `value`.
## Examples
iex> Keyword.from_keys([:foo, :bar, :baz], :atom)
[foo: :atom, bar: :atom, baz: :atom]
"""
@doc since: "1.14.0"
@spec from_keys([key], value) :: t(value)
def from_keys(keys, value) when is_list(keys) do
:lists.map(&{&1, value}, keys)
end
@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
@@ -161,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.
@@ -174,7 +158,7 @@ defmodule Keyword do
[a: 3]
"""
@spec new(Enumerable.t()) :: t
@spec new(Enum.t()) :: t
def new(pairs) do
new(pairs, fn pair -> pair end)
end
@@ -182,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.
@@ -192,7 +176,7 @@ defmodule Keyword do
[a: :a, b: :b]
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: t
@spec new(Enum.t(), (term -> {key, value})) :: t
def new(pairs, transform) when is_function(transform, 1) do
fun = fn el, acc ->
{k, v} = transform.(el)
@@ -203,166 +187,12 @@ defmodule Keyword do
end
@doc """
Ensures the given `keyword` has only the keys given in `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]}
Passing the same key multiple times also errors:
iex> Keyword.validate([one: 1, two: 2, one: 1], [:one, :two])
{:error, [:one]}
"""
@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]
Passing the same key multiple times also errors:
iex> Keyword.validate!([one: 1, two: 2, one: 1], [:one, :two])
** (ArgumentError) duplicate keys [:one] in [one: 1, two: 2, one: 1]
"""
@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))
message =
case Enum.split_with(invalid_keys, &(&1 in keys)) do
{_, [_ | _] = unknown} ->
"unknown keys #{inspect(unknown)} in #{inspect(keyword)}, " <>
"the allowed keys are: #{inspect(keys)}"
{[_ | _] = known, _} ->
"duplicate keys #{inspect(known)} in #{inspect(keyword)}"
end
raise ArgumentError, message
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
@@ -376,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
@@ -393,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
@@ -428,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
@@ -450,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, []}
@@ -498,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
@@ -551,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
@@ -578,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
@@ -592,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
@@ -621,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
@@ -637,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
@@ -691,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
@@ -721,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
@@ -754,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
@@ -779,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
@@ -804,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
@@ -825,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
@@ -858,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
@@ -886,50 +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 """
Replaces the value under `key` using the given function only if
`key` already exists in `keywords`.
In comparison to `replace/3`, this can be useful when it's expensive to calculate the value.
If `key` does not exist, the original keyword list is returned unchanged.
## Examples
iex> Keyword.replace_lazy([a: 1, b: 2], :a, fn v -> v * 4 end)
[a: 4, b: 2]
iex> Keyword.replace_lazy([a: 2, b: 2, a: 1], :a, fn v -> v * 4 end)
[a: 8, b: 2]
iex> Keyword.replace_lazy([a: 1, b: 2], :c, fn v -> v * 4 end)
[a: 1, b: 2]
"""
@doc since: "1.14.0"
@spec replace_lazy(t, key, (existing_value :: value -> new_value :: value)) :: t
def replace_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
do_replace_lazy(keywords, key, fun)
end
defp do_replace_lazy([{key, value} | keywords], key, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp do_replace_lazy([{_, _} = e | keywords], key, fun) do
[e | do_replace_lazy(keywords, key, fun)]
end
defp do_replace_lazy([], _key, _value), do: []
@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
@@ -956,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
@@ -1000,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
@@ -1080,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
@@ -1112,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
@@ -1154,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
@@ -1186,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
@@ -1201,18 +995,16 @@ defmodule Keyword do
"""
@spec take(t, [key]) :: t
def take(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> :lists.member(k, keys) end, keywords)
:lists.filter(fn {k, _} -> k in keys end, keywords)
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])
@@ -1221,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 """
@@ -1229,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
@@ -1258,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
@@ -1285,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
@@ -1319,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
@@ -1340,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
@@ -1380,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
+15 -157
View File
@@ -8,7 +8,7 @@ defmodule List do
[1, "two", 3, :four]
Two lists can be concatenated and subtracted using the
`++/2` and `--/2` operators:
`Kernel.++/2` and `Kernel.--/2` operators:
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
@@ -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
@@ -240,9 +240,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
def foldl(list, acc, fun) when is_list(list) and is_function(fun) do
@@ -251,16 +248,13 @@ 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
def foldr(list, acc, fun) when is_list(list) and is_function(fun) do
@@ -339,52 +333,12 @@ defmodule List do
iex> List.keyfind([a: 1, b: 2], :c, 0)
nil
This function works for any list of tuples:
iex> List.keyfind([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
"""
@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]
This function works for any list of tuples:
iex> List.keyfind!([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
"""
@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
@@ -401,14 +355,9 @@ defmodule List do
iex> List.keymember?([a: 1, b: 2], :c, 0)
false
This function works for any list of tuples:
iex> List.keymember?([{22, "SSH"}, {80, "HTTP"}], 22, 0)
true
"""
@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
@@ -424,26 +373,15 @@ defmodule List do
iex> List.keyreplace([a: 1, b: 2], :a, 1, {:a, 3})
[a: 1, b: 2]
This function works for any list of tuples:
iex> List.keyreplace([{22, "SSH"}, {80, "HTTP"}], 22, 0, {22, "Secure Shell"})
[{22, "Secure Shell"}, {80, "HTTP"}]
"""
@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
@doc """
Receives a list of tuples and sorts the elements
at `position` of the tuples.
The sort is stable.
A `sorter` argument is available since Elixir v1.14.0. Similar to
`Enum.sort/2`, the sorter can be an anonymous function, the atoms
`:asc` or `:desc`, or module that implements a compare function.
at `position` of the tuples. The sort is stable.
## Examples
@@ -453,69 +391,12 @@ defmodule List do
iex> List.keysort([a: 5, c: 1, b: 3], 0)
[a: 5, b: 3, c: 1]
To sort in descending order:
iex> List.keysort([a: 5, c: 1, b: 3], 0, :desc)
[c: 1, b: 3, a: 5]
As in `Enum.sort/2`, avoid using the default sorting function to sort
structs, as by default it performs structural comparison instead of a
semantic one. In such cases, you shall pass a sorting function as third
element or any module that implements a `compare/2` function. For example,
if you have tuples with user names and their birthday, and you want to
sort on their birthday, in both ascending and descending order, you should
do:
iex> users = [
...> {"Ellis", ~D[1943-05-11]},
...> {"Lovelace", ~D[1815-12-10]},
...> {"Turing", ~D[1912-06-23]}
...> ]
iex> List.keysort(users, 1, Date)
[
{"Lovelace", ~D[1815-12-10]},
{"Turing", ~D[1912-06-23]},
{"Ellis", ~D[1943-05-11]}
]
iex> List.keysort(users, 1, {:desc, Date})
[
{"Ellis", ~D[1943-05-11]},
{"Turing", ~D[1912-06-23]},
{"Lovelace", ~D[1815-12-10]}
]
"""
@doc since: "1.14.0"
@spec keysort(
[tuple],
non_neg_integer,
(any, any -> boolean) | :asc | :desc | module() | {:asc | :desc, module()}
) :: [tuple]
def keysort(list, position, sorter \\ :asc)
def keysort(list, position, :asc) when is_list(list) and is_integer(position) do
@spec keysort([tuple], non_neg_integer) :: [tuple]
def keysort(list, position) do
:lists.keysort(position + 1, list)
end
def keysort(list, position, sorter) when is_list(list) and is_integer(position) do
:lists.sort(keysort_fun(sorter, position + 1), list)
end
defp keysort_fun(sorter, position) when is_function(sorter, 2),
do: &sorter.(:erlang.element(position, &1), :erlang.element(position, &2))
defp keysort_fun(:desc, position),
do: &(:erlang.element(position, &1) >= :erlang.element(position, &2))
defp keysort_fun(module, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :gt)
defp keysort_fun({:asc, module}, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :gt)
defp keysort_fun({:desc, module}, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :lt)
@doc """
Receives a `list` of tuples and replaces the element
identified by `key` at `position` with `new_tuple`.
@@ -530,14 +411,9 @@ defmodule List do
iex> List.keystore([a: 1, b: 2], :c, 0, {:c, 3})
[a: 1, b: 2, c: 3]
This function works for any list of tuples:
iex> List.keystore([{22, "SSH"}], 80, 0, {80, "HTTP"})
[{22, "SSH"}, {80, "HTTP"}]
"""
@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
@@ -557,14 +433,9 @@ defmodule List do
iex> List.keydelete([a: 1, b: 2], :c, 0)
[a: 1, b: 2]
This function works for any list of tuples:
iex> List.keydelete([{22, "SSH"}, {80, "HTTP"}], 80, 0)
[{22, "SSH"}]
"""
@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
@@ -586,14 +457,9 @@ defmodule List do
iex> List.keytake([a: 1, b: 2], :c, 0)
nil
This function works for any list of tuples:
iex> List.keytake([{22, "SSH"}, {80, "HTTP"}], 80, 0)
{{80, "HTTP"}, [{22, "SSH"}]}
"""
@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
@@ -947,22 +813,14 @@ defmodule List do
end
@doc """
Converts a charlist to an existing atom.
Converts a charlist to an existing atom. Raises an `ArgumentError`
if the atom does not exist.
Elixir supports conversions from charlists which contains any Unicode
code point. Raises an `ArgumentError` if the atom does not exist.
code point.
Inlined by the compiler.
> #### Atoms and modules {: .info}
>
> Since Elixir is a compiled language, the atoms defined in a module
> will only exist after said module is loaded, which typically happens
> whenever a function in the module is executed. Therefore, it is
> generally recommended to call `List.to_existing_atom/1` only to
> convert atoms defined within the module making the function call
> to `to_existing_atom/1`.
## Examples
iex> _ = :my_atom
+93 -927
View File
File diff suppressed because it is too large Load Diff
+79 -168
View File
@@ -21,51 +21,75 @@ 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
* `file` - the current absolute file name as a binary
* `file` - the current file name as a binary
* `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,44 +98,43 @@ 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
}
fields = [
aliases: [],
context: nil,
context_modules: [],
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
requires: [],
tracers: [],
versioned_vars: %{}
]
# Define the __struct__ callbacks by hand for bootstrap reasons.
{struct, [], kv, body} = Kernel.Utils.defstruct(__MODULE__, fields, false)
def __struct__(), do: unquote(:elixir_quote.escape(struct, false, :none))
def __struct__(unquote(kv)), do: unquote(body)
# TODO: Remove :vars field on v2.0
@doc false
def __struct__ do
%{
__struct__: __MODULE__,
aliases: [],
context: nil,
context_modules: [],
contextual_vars: [],
current_vars: {%{}, %{}},
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
prematch_vars: :warn,
requires: [],
tracers: [],
unused_vars: {%{}, 0},
vars: []
}
end
@doc """
Prunes compile information from the environment.
This happens when the environment is captured at compilation
time, for example, in the module body, and then used to
evaluate code after the module has been defined.
"""
@doc since: "1.14.0"
@spec prune_compile_info(t) :: t
def prune_compile_info(env) do
%{env | lexical_tracker: nil, tracers: []}
@doc false
def __struct__(kv) do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
end
@doc """
@@ -126,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 """
@@ -163,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 """
+7 -152
View File
@@ -88,18 +88,12 @@ defmodule Map do
%{1 => :one, 2 => :two, 3 => :three}
Maps also support a specific update syntax to update the value stored under
*existing* keys. You can update using the atom keys syntax:
*existing* atom keys:
iex> map = %{one: 1, two: 2}
iex> %{map | one: "one"}
%{one: "one", two: 2}
Or any other key:
iex> other_map = %{"three" => 3, "four" => 4}
iex> %{other_map | "three" => "three"}
%{"four" => 4, "three" => "three"}
When a key that does not exist in the map is updated a `KeyError` exception will be raised:
%{map | three: 3}
@@ -123,27 +117,6 @@ defmodule Map do
@type value :: any
@compile {:inline, fetch: 2, fetch!: 2, get: 2, put: 3, delete: 2, has_key?: 2, replace!: 3}
# TODO: Remove conditional on Erlang/OTP 24+
@compile {:no_warn_undefined, {:maps, :from_keys, 2}}
@doc """
Builds a map from the given `keys` and the fixed `value`.
## Examples
iex> Map.from_keys([1, 2, 3], :number)
%{1 => :number, 2 => :number, 3 => :number}
"""
@doc since: "1.14.0"
@spec from_keys([key], value) :: map
def from_keys(keys, value) do
if function_exported?(:maps, :from_keys, 2) do
:maps.from_keys(keys, value)
else
:maps.from_list(:lists.map(&{&1, value}, keys))
end
end
@doc """
Returns all keys from `map`.
@@ -241,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()
@@ -362,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
@@ -394,32 +350,6 @@ defmodule Map do
:maps.update(key, value, map)
end
@doc """
Replaces the value under `key` using the given function only if
`key` already exists in `map`.
In comparison to `replace/3`, this can be useful when it's expensive to calculate the value.
If `key` does not exist, the original map is returned unchanged.
## Examples
iex> Map.replace_lazy(%{a: 1, b: 2}, :a, fn v -> v * 4 end)
%{a: 4, b: 2}
iex> Map.replace_lazy(%{a: 1, b: 2}, :c, fn v -> v * 4 end)
%{a: 1, b: 2}
"""
@doc since: "1.14.0"
@spec replace_lazy(map, key, (existing_value :: value -> new_value :: value)) :: map
def replace_lazy(map, key, fun) when is_map(map) and is_function(fun, 1) do
case map do
%{^key => val} -> %{map | key => fun.(val)}
%{} -> map
end
end
@doc """
Evaluates `fun` and puts the result under `key`
in `map` unless `key` is already present.
@@ -616,7 +546,7 @@ defmodule Map do
If you have a struct and you would like to merge a set of keys into the
struct, do not use this function, as it would merge all keys on the right
side into the struct, even if the key is not part of the struct. Instead,
use `struct/2`.
use `Kernel.struct/2`.
Inlined by the compiler.
@@ -684,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)
@@ -880,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 """
@@ -965,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)}
@@ -1041,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 (invoke Map.from_struct/1 before if you have a struct)"
def map(map, fun) when is_map(map) do
:maps.map(fn k, v -> fun.({k, v}) end, map)
end
end
+60 -150
View File
@@ -8,18 +8,18 @@ defmodule MapSet do
A set can be constructed using `MapSet.new/0`:
iex> MapSet.new()
MapSet.new([])
#MapSet<[]>
Elements in a set don't have to be of the same type and they can be
populated from an [enumerable](`t:Enumerable.t/0`) using `MapSet.new/1`:
iex> MapSet.new([1, :two, {"three"}])
MapSet.new([1, :two, {"three"}])
#MapSet<[1, :two, {"three"}]>
Elements can be inserted using `MapSet.put/2`:
iex> MapSet.new([2]) |> MapSet.put(4) |> MapSet.put(0)
MapSet.new([0, 2, 4])
#MapSet<[0, 2, 4]>
By definition, sets can't contain duplicate elements: when
inserting an element in a set where it's already present, the insertion is
@@ -27,9 +27,9 @@ defmodule MapSet do
iex> map_set = MapSet.new()
iex> MapSet.put(map_set, "foo")
MapSet.new(["foo"])
#MapSet<["foo"]>
iex> map_set |> MapSet.put("foo") |> MapSet.put("foo")
MapSet.new(["foo"])
#MapSet<["foo"]>
A `MapSet` is represented internally using the `%MapSet{}` struct. This struct
can be used whenever there's a need to pattern match on something being a `MapSet`:
@@ -54,12 +54,10 @@ defmodule MapSet do
@type value :: term
@opaque internal(value) :: %{optional(value) => []}
@type t(value) :: %__MODULE__{map: internal(value)}
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
@type t :: t(term)
# TODO: Remove version key when we require Erlang/OTP 24
# TODO: Implement the functions in this module using Erlang/OTP 24 new sets
# TODO: Remove version key on Elixir v2.0
defstruct map: %{}, version: 2
@doc """
@@ -68,7 +66,7 @@ defmodule MapSet do
## Examples
iex> MapSet.new()
MapSet.new([])
#MapSet<[]>
"""
@spec new :: t
@@ -80,19 +78,23 @@ defmodule MapSet do
## Examples
iex> MapSet.new([:b, :a, 3])
MapSet.new([3, :a, :b])
#MapSet<[3, :a, :b]>
iex> MapSet.new([3, 3, 3, 2, 2, 1])
MapSet.new([1, 2, 3])
#MapSet<[1, 2, 3]>
"""
@spec new(Enumerable.t()) :: t
@spec new(Enum.t()) :: t
def new(enumerable)
def new(%__MODULE__{} = map_set), do: map_set
def new(enumerable) do
keys = Enum.to_list(enumerable)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
map =
enumerable
|> Enum.to_list()
|> new_from_list([])
%MapSet{map: map}
end
@doc """
@@ -101,13 +103,33 @@ defmodule MapSet do
## Examples
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
MapSet.new([2, 4])
#MapSet<[2, 4]>
"""
@spec new(Enumerable.t(), (term -> val)) :: t(val) when val: value
@spec new(Enum.t(), (term -> val)) :: t(val) when val: value
def new(enumerable, transform) when is_function(transform, 1) do
keys = Enum.map(enumerable, transform)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
map =
enumerable
|> Enum.to_list()
|> new_from_list_transform(transform, [])
%MapSet{map: map}
end
defp new_from_list([], acc) do
Map.new(acc)
end
defp new_from_list([element | rest], acc) do
new_from_list(rest, [{element, @dummy_value} | acc])
end
defp new_from_list_transform([], _fun, acc) do
Map.new(acc)
end
defp new_from_list_transform([element | rest], fun, acc) do
new_from_list_transform(rest, fun, [{fun.(element), @dummy_value} | acc])
end
@doc """
@@ -119,9 +141,9 @@ defmodule MapSet do
iex> map_set = MapSet.new([1, 2, 3])
iex> MapSet.delete(map_set, 4)
MapSet.new([1, 2, 3])
#MapSet<[1, 2, 3]>
iex> MapSet.delete(map_set, 2)
MapSet.new([1, 3])
#MapSet<[1, 3]>
"""
@spec delete(t(val1), val2) :: t(val1) when val1: value, val2: value
@@ -135,7 +157,7 @@ defmodule MapSet do
## Examples
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
MapSet.new([1])
#MapSet<[1]>
"""
@spec difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
@@ -161,51 +183,13 @@ defmodule MapSet do
%{map_set | map: Map.drop(map1, Map.keys(map2))}
end
defp filter_not_in(:none, _map2, acc), do: Map.from_keys(acc, @dummy_value)
defp filter_not_in(:none, _map2, acc), do: Map.new(acc)
defp filter_not_in({key, _val, iter}, map2, acc) do
if is_map_key(map2, key) do
if :erlang.is_map_key(key, map2) do
filter_not_in(:maps.next(iter), map2, acc)
else
filter_not_in(:maps.next(iter), map2, [key | acc])
end
end
@doc """
Returns a set with elements that are present in only one but not both sets.
## Examples
iex> MapSet.symmetric_difference(MapSet.new([1, 2, 3]), MapSet.new([2, 3, 4]))
MapSet.new([1, 4])
"""
@doc since: "1.14.0"
@spec symmetric_difference(t(val1), t(val2)) :: t(val1 | val2) when val1: value, val2: value
def symmetric_difference(%MapSet{map: map1}, %MapSet{map: map2}) do
{small, large} = order_by_size(map1, map2)
map =
large
|> :maps.iterator()
|> :maps.next()
|> disjointer(small, [])
%MapSet{map: map}
end
defp disjointer(:none, small, list) do
list |> Map.from_keys(@dummy_value) |> Map.merge(small)
end
defp disjointer({key, _val, iter}, small, list) do
if is_map_key(small, key) do
iter
|> :maps.next()
|> disjointer(Map.delete(small, key), list)
else
iter
|> :maps.next()
|> disjointer(small, [key | list])
filter_not_in(:maps.next(iter), map2, [{key, @dummy_value} | acc])
end
end
@@ -233,7 +217,7 @@ defmodule MapSet do
defp none_in?(:none, _), do: true
defp none_in?({key, _val, iter}, map2) do
not is_map_key(map2, key) and none_in?(:maps.next(iter), map2)
not :erlang.is_map_key(key, map2) and none_in?(:maps.next(iter), map2)
end
@doc """
@@ -270,10 +254,10 @@ defmodule MapSet do
## Examples
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
MapSet.new([2])
#MapSet<[2]>
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))
MapSet.new([])
#MapSet<[]>
"""
@spec intersection(t(val), t(val)) :: t(val) when val: value
@@ -295,7 +279,7 @@ defmodule MapSet do
"""
@spec member?(t, value) :: boolean
def member?(%MapSet{map: map}, value) do
is_map_key(map, value)
:erlang.is_map_key(value, map)
end
@doc """
@@ -304,9 +288,9 @@ defmodule MapSet do
## Examples
iex> MapSet.put(MapSet.new([1, 2, 3]), 3)
MapSet.new([1, 2, 3])
#MapSet<[1, 2, 3]>
iex> MapSet.put(MapSet.new([1, 2, 3]), 4)
MapSet.new([1, 2, 3, 4])
#MapSet<[1, 2, 3, 4]>
"""
@spec put(t(val), new_val) :: t(val | new_val) when val: value, new_val: value
@@ -379,7 +363,7 @@ defmodule MapSet do
## Examples
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
MapSet.new([1, 2, 3, 4])
#MapSet<[1, 2, 3, 4]>
"""
@spec union(t(val1), t(val2)) :: t(val1 | val2) when val1: value, val2: value
@@ -390,88 +374,14 @@ defmodule MapSet do
end
def union(%MapSet{map: map1}, %MapSet{map: map2}) do
keys = Map.keys(map1) ++ Map.keys(map2)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
map = new_from_list(Map.keys(map1) ++ Map.keys(map2), [])
%MapSet{map: map}
end
@compile {:inline, [order_by_size: 2]}
defp order_by_size(map1, map2) when map_size(map1) > map_size(map2), do: {map2, map1}
defp order_by_size(map1, map2), do: {map1, map2}
@doc """
Filters the set by returning only the elements from `set` for which invoking
`fun` returns a truthy value.
Also see `reject/2` which discards all elements where the function returns
a truthy value.
> Note: if you find yourself doing multiple calls to `MapSet.filter/2`
> and `MapSet.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> MapSet.filter(MapSet.new(1..5), fn x -> x > 3 end)
MapSet.new([4, 5])
iex> MapSet.filter(MapSet.new(["a", :b, "c"]), &is_atom/1)
MapSet.new([:b])
"""
@doc since: "1.14.0"
@spec filter(t(a), (a -> as_boolean(term))) :: t(a) when a: value
def filter(%MapSet{map: map}, fun) when is_map(map) and is_function(fun) do
iter = :maps.iterator(map)
next = :maps.next(iter)
keys = filter_keys(next, fun)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
end
defp filter_keys(:none, _fun), do: []
defp filter_keys({key, _value, iter}, fun) do
if fun.(key) do
[key | filter_keys(:maps.next(iter), fun)]
else
filter_keys(:maps.next(iter), fun)
end
end
@doc """
Returns a set by excluding the elements from `set` for which invoking `fun`
returns a truthy value.
See also `filter/2`.
## Examples
iex> MapSet.reject(MapSet.new(1..5), fn x -> rem(x, 2) != 0 end)
MapSet.new([2, 4])
iex> MapSet.reject(MapSet.new(["a", :b, "c"]), &is_atom/1)
MapSet.new(["a", "c"])
"""
@doc since: "1.14.0"
@spec reject(t(a), (a -> as_boolean(term))) :: t(a) when a: value
def reject(%MapSet{map: map}, fun) when is_map(map) and is_function(fun) do
iter = :maps.iterator(map)
next = :maps.next(iter)
keys = reject_keys(next, fun)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
end
defp reject_keys(:none, _fun), do: []
defp reject_keys({key, _value, iter}, fun) do
if fun.(key) do
reject_keys(:maps.next(iter), fun)
else
[key | reject_keys(:maps.next(iter), fun)]
end
end
defimpl Enumerable do
def count(map_set) do
{:ok, MapSet.size(map_set)}
@@ -483,7 +393,7 @@ defmodule MapSet do
def slice(map_set) do
size = MapSet.size(map_set)
{:ok, size, &MapSet.to_list/1}
{:ok, size, &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2, size)}
end
def reduce(map_set, acc, fun) do
@@ -494,8 +404,8 @@ defmodule MapSet do
defimpl Collectable do
def into(map_set) do
fun = fn
list, {:cont, x} -> [x | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.from_keys(list, []))}
list, {:cont, x} -> [{x, []} | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.new(list))}
_, :halt -> :ok
end
@@ -508,7 +418,7 @@ defmodule MapSet do
def inspect(map_set, opts) do
opts = %Inspect.Opts{opts | charlists: :as_lists}
concat(["MapSet.new(", Inspect.List.inspect(MapSet.to_list(map_set), opts), ")"])
concat(["#MapSet<", Inspect.List.inspect(MapSet.to_list(map_set), opts), ">"])
end
end
end
+274 -473
View File
File diff suppressed because it is too large Load Diff
+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}}
+170 -279
View File
@@ -1,165 +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.
"""
def start_link(schedulers \\ nil) do
:gen_server.start_link(__MODULE__, schedulers, [])
end
@doc """
Stops the parallel checker process.
"""
def stop(checker) do
send(checker, {__MODULE__, :stop})
:ok
end
@doc """
Gets the parallel checker data from pdict.
"""
def get do
case :erlang.get(:elixir_checker_info) do
{parent, nil} ->
{:ok, checker} = start_link()
put(parent, checker)
{parent, checker}
{parent, checker} ->
{parent, checker}
end
end
@doc """
Stores the parallel checker information.
"""
def put(pid, checker) when is_pid(pid) and is_pid(checker) do
:erlang.put(:elixir_checker_info, {pid, checker})
end
@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} ->
module_map =
if is_map(info) do
info
else
info |> File.read!() |> maybe_module_map(module)
end
module_map && cache_from_module_map(ets, module_map)
send(checker, {ref, :cached})
receive do
{^ref, :check} ->
# Set the compiler info so we can collect warnings
:erlang.put(:elixir_compiler_info, {pid, self()})
warnings =
if module_map do
check_module(module_map, {checker, ets})
else
[]
end
send(pid, {__MODULE__, module, warnings})
send(checker, {__MODULE__, :done})
end
{:DOWN, ^mon_ref, _, _, _} ->
:ok
end
end)
register(checker, spawned, ref)
:ok
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.put(:elixir_checker_info, {self(), nil})
try do
result = fun.()
case :erlang.get(:elixir_checker_info) do
{_, nil} -> :ok
{_, checker} -> verify(checker, [])
end
result
after
{_, checker} = :erlang.get(:elixir_checker_info)
if previous != :undefined do
:erlang.put(:elixir_checker_info, previous)
else
:erlang.erase(:elixir_checker_info)
end
checker && stop(checker)
end
_ ->
# If we are during compilation, then they will be
# reported to the compiler, which will validate them.
fun.()
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.
binaries and a list of warnings from the verification.
"""
@spec verify(pid(), [{module(), binary()}]) :: [warning()]
def verify(checker, runtime_files) do
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
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)
modules = :gen_server.call(checker, :start)
collect_results(modules, [])
case compiled_maps ++ runtime_binaries do
[] ->
[]
check_modules ->
schedulers = schedulers || max(:erlang.system_info(:schedulers_online), 2)
{:ok, server} =
:gen_server.start_link(__MODULE__, [check_modules, self(), schedulers], [])
preload_cache(get_ets(server), check_modules)
start(server)
collect_results(length(check_modules), [])
end
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
{:warning, file, location, message} ->
file = file && Path.absname(file)
message = :unicode.characters_to_binary(message)
warning = {file, location, message}
collect_results(modules, [warning | warnings])
{__MODULE__, _module, new_warnings} ->
collect_results(tl(modules), new_warnings ++ warnings)
collect_results(count - 1, new_warnings ++ warnings)
end
end
@@ -167,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 """
@@ -219,26 +98,35 @@ defmodule Module.ParallelChecker do
## Module checking
defp check_module(module_map, cache) do
%{module: module, file: file, compile_opts: compile_opts, definitions: definitions} =
module_map
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()
|> emit_warnings()
:error ->
[]
end
end
defp extract_definitions({module, module_map}) when is_map(module_map) do
no_warn_undefined =
compile_opts
module_map.compile_opts
|> extract_no_warn_undefined()
|> merge_compiler_no_warn_undefined()
warnings =
module
|> Module.Types.warnings(file, definitions, no_warn_undefined, cache)
|> group_warnings()
|> emit_warnings()
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
end
module_map
|> Map.get(:after_verify, [])
|> Enum.each(fn {verify_mod, verify_fun} -> apply(verify_mod, verify_fun, [module]) end)
warnings
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})
else
_ -> :error
end
end
defp extract_no_warn_undefined(compile_opts) do
@@ -251,8 +139,11 @@ defmodule Module.ParallelChecker do
defp merge_compiler_no_warn_undefined(no_warn_undefined) do
case Code.get_compiler_option(:no_warn_undefined) do
:all -> :all
list when is_list(list) -> no_warn_undefined ++ list
:all ->
:all
list when is_list(list) ->
no_warn_undefined ++ list
end
end
@@ -285,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
@@ -313,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
@@ -323,9 +313,14 @@ defmodule Module.ParallelChecker do
end
defp cache_from_chunk(ets, module) do
with {^module, binary, _filename} <- :code.get_object_code(module),
{:ok, ^module} <- binary |> :beam_lib.info() |> Keyword.fetch(:module),
{:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
case :code.get_object_code(module) do
{^module, binary, _filename} -> cache_from_chunk(ets, module, binary)
_other -> false
end
end
defp cache_from_chunk(ets, module, binary) do
with {:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
cache_chunk(ets, module, contents.exports)
true
@@ -334,6 +329,16 @@ defmodule Module.ParallelChecker do
end
end
defp cache_from_module_map(ets, map) do
exports =
[{{:__info__, 1}, :def}] ++
behaviour_exports(map) ++
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated, :elixir)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
{mode, exports} = info_exports(module)
@@ -364,28 +369,6 @@ defmodule Module.ParallelChecker do
_ -> %{}
end
defp maybe_module_map(binary, module) when is_binary(binary) do
# If a module was compiled without debug_info,
# then there is no module_map for further verification.
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
module_map
else
_ -> nil
end
end
defp cache_from_module_map(ets, map) do
exports =
[{{:__info__, 1}, :def}] ++
behaviour_exports(map) ++
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated, :elixir)
end
defp cache_info(ets, module, exports, deprecated, mode) do
Enum.each(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
@@ -427,96 +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}, :infinity)
end
defp register(server, pid, ref) do
:gen_server.cast(server, {:register, pid, ref})
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(:start, _from, %{ets: ets, modules: modules} = 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
{:reply, modules, run_checkers(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({:register, pid, ref}, %{modules: modules} = state) do
{:noreply, %{state | modules: [{pid, ref} | 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 -121
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,87 +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
{original_module, original_function, arity} = mfa
{_, _, args} = mfa_or_fa = erl_to_ex(original_module, original_function, args, [])
{module, function, ^arity} = call_to_mfa(mfa_or_fa)
format_mfa = Exception.format_mfa(module, function, arity)
{traces, [] = _hints} = format_traces(traces, [], false)
clauses =
Enum.map(signature, fn {ins, out} ->
{_, _, ins} = erl_to_ex(original_module, original_function, ins, [])
{:fun, [{ins, out}]}
|> Unify.format_type(false)
|> IO.iodata_to_binary()
|> binary_slice(1..-2//1)
end)
[
"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
@@ -303,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 \"",
@@ -358,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
@@ -401,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
@@ -420,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}
@@ -448,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
@@ -486,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)
@@ -520,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
+27 -56
View File
@@ -151,20 +151,16 @@ defmodule Module.Types.Expr do
dynamic_value_pairs =
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
{:ok, type, context} <- unify(map_type, args_type, stack, context) do
{:ok, type, context} <- unify(args_type, map_type, stack, context) do
# Retrieve map type and overwrite with the new value types from the map update
case resolve_var(type, context) do
{:map, pairs} ->
updated_pairs =
Enum.reduce(arg_pairs, pairs, fn {:required, key, value}, pairs ->
List.keyreplace(pairs, key, 1, {:required, key, value})
end)
{:map, pairs} = resolve_var(type, context)
{:ok, {:map, updated_pairs}, context}
updated_pairs =
Enum.reduce(arg_pairs, pairs, fn {:required, key, value}, pairs ->
List.keyreplace(pairs, key, 1, {:required, key, value})
end)
_ ->
{:ok, :dynamic, context}
end
{:ok, {:map, updated_pairs}, context}
end
end
@@ -220,30 +216,6 @@ defmodule Module.Types.Expr do
end
end
# cond do pat -> expr end
def of_expr({:cond, _meta, [[{:do, clauses}]]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
case of_expr(head, :dynamic, 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
end)
case result do
:ok -> {:ok, :dynamic, context}
:error -> {:error, context}
end
end
# case expr do pat -> expr end
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
@@ -329,7 +301,7 @@ defmodule Module.Types.Expr do
end
# for pat <- expr do expr end
def of_expr({:for, _meta, [_ | _] = args} = expr, _expected, stack, context) do
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
@@ -348,7 +320,7 @@ defmodule Module.Types.Expr do
end
# with pat <- expr do expr end
def of_expr({:with, _meta, [_ | _] = clauses} = expr, _expected, stack, context) do
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
@@ -362,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
@@ -398,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
@@ -497,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
+2 -2
View File
@@ -237,7 +237,7 @@ defmodule Module.Types.Of do
def remote(module, fun, arity, meta, context) when is_atom(module) do
# TODO: In the future we may want to warn for modules defined
# in the local context
if Keyword.get(meta, :context_module, false) do
if Keyword.get(meta, :context_module, false) and context.module != module do
context
else
ParallelChecker.preload_module(context.cache, module)
@@ -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
+138 -274
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)
@@ -128,10 +145,30 @@ defmodule Module.Types.Unify do
{:ok, {:var, var}, context}
end
%{^var => {:var, _} = var_type} ->
# Do not recursively traverse type vars for now
# to avoid pathological cases related to performance.
{:ok, var_type, context}
%{^var => {:var, new_var} = var_type} ->
unify_result =
if var_source? do
unify(var_type, type, stack, context)
else
unify(type, var_type, stack, context)
end
case unify_result do
{:ok, type, context} ->
{:ok, type, context}
{:error, {type, reason, %{traces: error_traces} = error_context}} ->
old_var_traces = Map.get(context.traces, new_var, [])
new_var_traces = Map.get(error_traces, new_var, [])
add_var_traces = Enum.drop(new_var_traces, -length(old_var_traces))
error_traces =
error_traces
|> Map.update(var, add_var_traces, &(add_var_traces ++ &1))
|> Map.put(new_var, old_var_traces)
{:error, {type, reason, %{error_context | traces: error_traces}}}
end
%{^var => var_type} ->
# Only add trace if the variable wasn't already "expanded"
@@ -168,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)
@@ -199,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
@@ -219,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
@@ -239,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
@@ -259,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
@@ -288,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.
@@ -359,11 +396,11 @@ defmodule Module.Types.Unify do
end
@doc """
Maybe resolves a variable.
Resolves a variable raising if it is unbound.
"""
def resolve_var({:var, var}, context) do
case context.types do
%{^var => :unbound} -> {:var, var}
%{^var => :unbound} -> raise "cannot resolve unbound var"
%{^var => type} -> resolve_var(type, context)
end
end
@@ -468,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
@@ -481,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.
@@ -628,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
@@ -666,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,
@@ -677,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
@@ -735,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
@@ -758,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.
@@ -868,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()"
@@ -876,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
@@ -909,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
-30
View File
@@ -257,36 +257,6 @@ defmodule Node do
:erlang.spawn_link(node, module, fun, args)
end
@doc """
Spawns the given function on a node, monitors it and returns its PID
and monitoring reference.
This functionality was added on Erlang/OTP 23. Using this function to
communicate with nodes running on earlier versions will fail.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, (() -> any)) :: {pid, reference}
def spawn_monitor(node, fun) do
:erlang.spawn_monitor(node, fun)
end
@doc """
Spawns the given module and function passing the given args on a node,
monitors it and returns its PID and monitoring reference.
This functionality was added on Erlang/OTP 23. Using this function
to communicate with nodes running on earlier versions will fail.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, module, atom, [any]) :: {pid, reference}
def spawn_monitor(node, module, fun, args) do
:erlang.spawn_monitor(node, module, fun, args)
end
@doc """
Sets the magic cookie of `node` to the atom `cookie`.
+3 -45
View File
@@ -29,12 +29,7 @@ defmodule OptionParser do
@type argv :: [String.t()]
@type parsed :: keyword
@type errors :: [{String.t(), String.t() | nil}]
@type options :: [
switches: keyword,
strict: keyword,
aliases: keyword,
allow_nonexistent_atoms: boolean
]
@type options :: [switches: keyword, strict: keyword, aliases: keyword]
defmodule ParseError do
defexception [:message]
@@ -492,7 +487,7 @@ defmodule OptionParser do
Keys must be atoms. Keys with `nil` value are discarded,
boolean values are converted to `--key` or `--no-key`
(if the value is `true` or `false`, respectively),
and all other values are converted using `to_string/1`.
and all other values are converted using `Kernel.to_string/1`.
It is advised to pass to `to_argv/2` the same set of `options`
given to `parse/2`. Some switches can only be reconstructed
@@ -601,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 ->
@@ -621,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
@@ -833,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
@@ -860,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
-386
View File
@@ -1,386 +0,0 @@
defmodule PartitionSupervisor do
@moduledoc """
A supervisor that starts multiple partitions of the same child.
Certain processes may become bottlenecks in large systems.
If those processes can have their state trivially partitioned,
in a way there is no dependency between them, then they can use
the `PartitionSupervisor` to create multiple isolated and
independent partitions.
Once the `PartitionSupervisor` starts, you can dispatch to its
children using `{:via, PartitionSupervisor, {name, key}}`, where
`name` is the name of the `PartitionSupervisor` and key is used
for routing.
## Example
The `DynamicSupervisor` is a single process responsible for starting
other processes. In some applications, the `DynamicSupervisor` may
become a bottleneck. To address this, you can start multiple instances
of the `DynamicSupervisor` through a `PartitionSupervisor`, and then
pick a "random" instance to start the child on.
Instead of starting a single `DynamicSupervisor`:
children = [
{DynamicSupervisor, name: MyApp.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
and starting children on that dynamic supervisor directly:
DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end})
You can do start the dynamic supervisors under a `PartitionSupervisor`:
children = [
{PartitionSupervisor,
child_spec: DynamicSupervisor,
name: MyApp.DynamicSupervisors}
]
Supervisor.start_link(children, strategy: :one_for_one)
and then:
DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {MyApp.DynamicSupervisors, self()}},
{Agent, fn -> %{} end}
)
In the code above, we start a partition supervisor that will by default
start a dynamic supervisor for each core in your machine. Then, instead
of calling the `DynamicSupervisor` by name, you call it through the
partition supervisor using the `{:via, PartitionSupervisor, {name, key}}`
format. We picked `self()` as the routing key, which means each process
will be assigned one of the existing dynamic supervisors. See `start_link/1`
to see all options supported by the `PartitionSupervisor`.
## Implementation notes
The `PartitionSupervisor` uses either an ETS table or a `Registry` to
manage all of the partitions. Under the hood, the `PartitionSupervisor`
generates a child spec for each partition and then acts as a regular
supervisor. The ID of each child spec is the partition number.
For routing, two strategies are used. If `key` is an integer, it is routed
using `rem(abs(key), partitions)` where `partitions` is the number of
partitions. Otherwise it uses `:erlang.phash2(key, partitions)`.
The particular routing may change in the future, and therefore must not
be relied on. If you want to retrieve a particular PID for a certain key,
you can use `GenServer.whereis({:via, PartitionSupervisor, {name, key}})`.
"""
@behaviour Supervisor
@registry PartitionSupervisor.Registry
@typedoc """
The name of the `PartitionSupervisor`.
"""
@type name :: atom() | {:via, module(), term()}
@doc false
def child_spec(opts) when is_list(opts) do
id =
case Keyword.get(opts, :name, DynamicSupervisor) do
name when is_atom(name) -> name
{:via, _module, name} -> name
end
%{
id: id,
start: {PartitionSupervisor, :start_link, [opts]},
type: :supervisor
}
end
@doc """
Starts a partition supervisor with the given options.
This function is typically not invoked directly, instead it is invoked
when using a `PartitionSupervisor` as a child of another supervisor:
children = [
{PartitionSupervisor, child_spec: SomeChild, name: MyPartitionSupervisor}
]
If the supervisor is successfully spawned, this function returns
`{:ok, pid}`, where `pid` is the PID of the supervisor. If the given name
for the partition supervisor is already assigned to a process,
the function returns `{:error, {:already_started, pid}}`, where `pid`
is the PID of that process.
Note that a supervisor started with this function is linked to the parent
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
## Options
* `:name` - an atom or via tuple representing the name of the partition
supervisor (see `t:name/0`).
* `:partitions` - a positive integer with the number of partitions.
Defaults to `System.schedulers_online()` (typically the number of cores).
* `:strategy` - the restart strategy option, defaults to `:one_for_one`.
You can learn more about strategies in the `Supervisor` module docs.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
* `:with_arguments` - a two-argument anonymous function that allows
the partition to be given to the child starting function. See the
`:with_arguments` section below.
## `:with_arguments`
Sometimes you want each partition to know their partition assigned number.
This can be done with the `:with_arguments` option. This function receives
the list of arguments of the child specification and the partition. It
must return a new list of arguments that will be passed to the child specification
of children.
For example, most processes are started by calling `start_link(opts)`,
where `opts` is a keyword list. You could inject the partition into the
options given to the child:
with_arguments: fn [opts], partition ->
[Keyword.put(opts, :partition, partition)]
end
"""
@doc since: "1.14.0"
@spec start_link(keyword) :: Supervisor.on_start()
def start_link(opts) when is_list(opts) do
name = opts[:name]
unless name do
raise ArgumentError, "the :name option must be given to PartitionSupervisor"
end
{child_spec, opts} = Keyword.pop(opts, :child_spec)
unless child_spec do
raise ArgumentError, "the :child_spec option must be given to PartitionSupervisor"
end
{partitions, opts} = Keyword.pop(opts, :partitions, System.schedulers_online())
unless is_integer(partitions) and partitions >= 1 do
raise ArgumentError,
"the :partitions option must be a positive integer, got: #{inspect(partitions)}"
end
{with_arguments, opts} = Keyword.pop(opts, :with_arguments, fn args, _partition -> args end)
unless is_function(with_arguments, 2) do
raise ArgumentError,
"the :with_arguments option must be a function that receives two arguments, " <>
"the current call arguments and the partition, got: #{inspect(with_arguments)}"
end
%{start: {mod, fun, args}} = map = Supervisor.child_spec(child_spec, [])
modules = map[:modules] || [mod]
children =
for partition <- 0..(partitions - 1) do
args = with_arguments.(args, partition)
unless is_list(args) do
raise "the call to the function in :with_arguments must return a list, got: #{inspect(args)}"
end
start = {__MODULE__, :start_child, [mod, fun, args, name, partition]}
Map.merge(map, %{id: partition, start: start, modules: modules})
end
{init_opts, start_opts} = Keyword.split(opts, [:strategy, :max_seconds, :max_restarts])
Supervisor.start_link(__MODULE__, {name, partitions, children, init_opts}, start_opts)
end
@doc false
def start_child(mod, fun, args, name, partition) do
case apply(mod, fun, args) do
{:ok, pid} ->
register_child(name, partition, pid)
{:ok, pid}
{:ok, pid, info} ->
register_child(name, partition, pid)
{:ok, pid, info}
other ->
other
end
end
defp register_child(name, partition, pid) when is_atom(name) do
:ets.insert(name, {partition, pid})
end
defp register_child({:via, _, _}, partition, pid) do
Registry.register(@registry, {self(), partition}, pid)
end
@impl true
def init({name, partitions, children, init_opts}) do
init_partitions(name, partitions)
Supervisor.init(children, Keyword.put_new(init_opts, :strategy, :one_for_one))
end
defp init_partitions(name, partitions) when is_atom(name) do
:ets.new(name, [:set, :named_table, :protected, read_concurrency: true])
:ets.insert(name, {:partitions, partitions})
end
defp init_partitions({:via, _, _}, partitions) do
child_spec = {Registry, keys: :unique, name: @registry}
unless Process.whereis(@registry) do
Supervisor.start_child(:elixir_sup, child_spec)
end
Registry.register(@registry, self(), partitions)
end
@doc """
Returns the number of partitions for the partition supervisor.
"""
@doc since: "1.14.0"
@spec partitions(name()) :: pos_integer()
def partitions(name) do
{_name, partitions} = name_partitions(name)
partitions
end
# For whereis_name, we want to lookup on GenServer.whereis/1
# just once, so we lookup the name and partitions together.
defp name_partitions(name) when is_atom(name) do
try do
{name, :ets.lookup_element(name, :partitions, 2)}
rescue
_ -> exit({:noproc, {__MODULE__, :partitions, [name]}})
end
end
defp name_partitions(name) when is_tuple(name) do
with pid when is_pid(pid) <- GenServer.whereis(name),
[name_partitions] <- Registry.lookup(@registry, pid) do
name_partitions
else
_ -> exit({:noproc, {__MODULE__, :partitions, [name]}})
end
end
@doc """
Returns a list with information about all children.
This function returns a list of tuples containing:
* `id` - the partition number
* `child` - the PID of the corresponding child process or the
atom `:restarting` if the process is about to be restarted
* `type` - `:worker` or `:supervisor` as defined in the child
specification
* `modules` - as defined in the child specification
"""
@doc since: "1.14.0"
@spec which_children(name()) :: [
# Inlining [module()] | :dynamic here because :supervisor.modules() is not exported
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
]
def which_children(name) when is_atom(name) or elem(name, 0) == :via do
Supervisor.which_children(name)
end
@doc """
Returns a map containing count values for the supervisor.
The map contains the following keys:
* `:specs` - the number of partitions (children processes)
* `:active` - the count of all actively running child processes managed by
this supervisor
* `:supervisors` - the count of all supervisors whether or not the child
process is still alive
* `:workers` - the count of all workers, whether or not the child process
is still alive
"""
@doc since: "1.14.0"
@spec count_children(name()) :: %{
specs: non_neg_integer,
active: non_neg_integer,
supervisors: non_neg_integer,
workers: non_neg_integer
}
def count_children(supervisor) when is_atom(supervisor) do
Supervisor.count_children(supervisor)
end
@doc """
Synchronously stops the given partition supervisor with the given `reason`.
It returns `:ok` if the supervisor terminates with the given
reason. If it terminates with another reason, the call exits.
This function keeps OTP semantics regarding error reporting.
If the reason is any other than `:normal`, `:shutdown` or
`{:shutdown, _}`, an error report is logged.
"""
@doc since: "1.14.0"
@spec stop(name(), reason :: term, timeout) :: :ok
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) when is_atom(supervisor) do
Supervisor.stop(supervisor, reason, timeout)
end
## Via callbacks
@doc false
def whereis_name({name, key}) when is_atom(name) or is_tuple(name) do
{name, partitions} = name_partitions(name)
partition =
if is_integer(key), do: rem(abs(key), partitions), else: :erlang.phash2(key, partitions)
whereis_name(name, partition)
end
defp whereis_name(name, partition) when is_atom(name) do
:ets.lookup_element(name, partition, 2)
end
defp whereis_name(name, partition) when is_pid(name) do
@registry
|> Registry.values({name, partition}, name)
|> List.first(:undefined)
end
@doc false
def send(name_key, msg) do
Kernel.send(whereis_name(name_key), msg)
end
@doc false
def register_name(_, _) do
raise "{:via, PartitionSupervisor, _} cannot be given on registration"
end
@doc false
def unregister_name(_, _) do
raise "{:via, PartitionSupervisor, _} cannot be given on unregistration"
end
end
+10 -100
View File
@@ -3,25 +3,20 @@ defmodule Path do
This module provides conveniences for manipulating or
retrieving file system paths.
The functions in this module may receive chardata as
arguments and will always return a string encoded in UTF-8. Chardata
is a string or a list of characters and strings, see `t:IO.chardata/0`.
If a binary is given, in whatever encoding, its encoding will be kept.
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).
The majority of the functions in this module do not
interact with the file system, except for a few functions
that require it (like `wildcard/2` and `expand/1`).
"""
@typedoc """
A path.
"""
@type t :: IO.chardata()
@doc """
Converts the given path to an absolute one.
Unlike `expand/1`, no attempt is made to resolve `..`, `.`, or `~`.
Converts the given path to an absolute one. Unlike
`expand/1`, no attempt is made to resolve `..`, `.` or `~`.
## Examples
@@ -152,8 +147,8 @@ defmodule Path do
defp reverse_maybe_remove_dir_sep(name, _), do: :lists.reverse(name)
@doc """
Converts the path to an absolute one, expanding
any `.` and `..` components and a leading `~`.
Converts the path to an absolute one and expands
any `.` and `..` characters and a leading `~`.
## Examples
@@ -371,9 +366,6 @@ defmodule Path do
iex> Path.basename("foo/bar")
"bar"
iex> Path.basename("lib/module/submodule.ex")
"submodule.ex"
iex> Path.basename("/")
""
@@ -433,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")
@@ -500,8 +482,6 @@ defmodule Path do
This function should be used to convert a list of paths to a path.
Note that any trailing slash is removed when joining.
Raises an error if the given list of paths is empty.
## Examples
iex> Path.join(["~", "foo"])
@@ -541,7 +521,6 @@ defmodule Path do
iex> Path.join(["foo", "bar"], "fiz")
"foobar/fiz"
Use `join/1` if you need to join a list of paths instead.
"""
@spec join(t, t) :: binary
def join(left, right) do
@@ -575,7 +554,7 @@ defmodule Path do
If an empty string is given, returns an empty list.
On Windows, path is split on both `"\"` and `"/"` separators
On Windows, path is split on both "\" and "/" separators
and the driver letter, if there is one, is always returned
in lowercase.
@@ -592,6 +571,7 @@ defmodule Path do
"""
@spec split(t) :: [binary]
def split(path) do
:filename.split(IO.chardata_to_string(path))
end
@@ -667,7 +647,7 @@ defmodule Path do
"""
@spec wildcard(t, keyword) :: [binary]
def wildcard(glob, opts \\ []) when is_list(opts) do
def wildcard(glob, opts \\ []) do
mod = if Keyword.get(opts, :match_dot), do: :file, else: Path.Wildcard
glob
@@ -730,74 +710,4 @@ defmodule Path do
defp major_os_type do
:os.type() |> elem(0)
end
@doc """
Returns a relative path that is protected from directory-traversal attacks.
The given relative path is sanitized by eliminating `..` and `.` components.
This function checks that, after expanding those components, the path is still "safe".
Paths are considered unsafe if either of these is true:
* The path is not relative, such as `"/foo/bar"`.
* A `..` component would make it so that the path would travers up above
the root of `relative_to`.
* A symbolic link in the path points to something above the root of `relative_to`.
## Examples
iex> Path.safe_relative_to("deps/my_dep/app.beam", "deps")
{:ok, "deps/my_dep/app.beam"}
iex> Path.safe_relative_to("deps/my_dep/./build/../app.beam", "deps")
{:ok, "deps/my_dep/app.beam"}
iex> Path.safe_relative_to("my_dep/../..", "deps")
:error
iex> Path.safe_relative_to("/usr/local", ".")
:error
"""
@doc since: "1.14.0"
@spec safe_relative_to(t, t) :: {:ok, binary} | :error
def safe_relative_to(path, relative_to) do
path = IO.chardata_to_string(path)
case :filelib.safe_relative_path(path, relative_to) do
:unsafe -> :error
relative_path -> {:ok, IO.chardata_to_string(relative_path)}
end
end
@doc """
Returns a path relative to the current working directory that is
protected from directory-traversal attacks.
Same as `safe_relative_to/2` with the current working directory as
the second argument. If there is an issue retrieving the current working
directory, this function raises an error.
## Examples
iex> Path.safe_relative("foo")
{:ok, "foo"}
iex> Path.safe_relative("foo/../bar")
{:ok, "bar"}
iex> Path.safe_relative("foo/../..")
:error
iex> Path.safe_relative("/usr/local")
:error
"""
@doc since: "1.14.0"
@spec safe_relative(t) :: {:ok, binary} | :error
def safe_relative(path) do
safe_relative_to(path, File.cwd!())
end
end
+7 -7
View File
@@ -24,7 +24,7 @@ defmodule Port do
receives data from multiple inputs and concatenates them in the output.
After the port was created, we sent it two commands in the form of
messages using `send/2`. The first command has the binary payload
messages using `Kernel.send/2`. The first command has the binary payload
of "hello" and the second has "world".
After sending those two messages, we invoked the IEx helper `flush()`,
@@ -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.
"""
@@ -250,7 +250,7 @@ defmodule Port do
"""
@spec info(port) :: keyword | nil
def info(port) do
nilify(:erlang.port_info(port))
nillify(:erlang.port_info(port))
end
@doc """
@@ -270,7 +270,7 @@ defmodule Port do
end
def info(port, item) do
nilify(:erlang.port_info(port, item))
nillify(:erlang.port_info(port, item))
end
@doc """
@@ -323,7 +323,7 @@ defmodule Port do
:erlang.ports()
end
@compile {:inline, nilify: 1}
defp nilify(:undefined), do: nil
defp nilify(other), do: other
@compile {:inline, nillify: 1}
defp nillify(:undefined), do: nil
defp nillify(other), do: other
end
+8 -15
View File
@@ -116,7 +116,7 @@ defmodule Process do
"""
@spec put(term, term) :: term | nil
def put(key, value) do
nilify(:erlang.put(key, value))
nillify(:erlang.put(key, value))
end
@doc """
@@ -136,7 +136,7 @@ defmodule Process do
"""
@spec delete(term) :: term | nil
def delete(key) do
nilify(:erlang.erase(key))
nillify(:erlang.erase(key))
end
@doc """
@@ -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.
@@ -650,7 +643,7 @@ defmodule Process do
"""
@spec whereis(atom) :: pid | port | nil
def whereis(name) do
nilify(:erlang.whereis(name))
nillify(:erlang.whereis(name))
end
@doc """
@@ -749,7 +742,7 @@ defmodule Process do
"""
@spec info(pid) :: keyword | nil
def info(pid) do
nilify(:erlang.process_info(pid))
nillify(:erlang.process_info(pid))
end
@doc """
@@ -770,7 +763,7 @@ defmodule Process do
end
def info(pid, spec) when is_atom(spec) or is_list(spec) do
nilify(:erlang.process_info(pid, spec))
nillify(:erlang.process_info(pid, spec))
end
@doc """
@@ -788,7 +781,7 @@ defmodule Process do
@spec hibernate(module, atom, list) :: no_return
defdelegate hibernate(mod, fun_name, args), to: :erlang
@compile {:inline, nilify: 1}
defp nilify(:undefined), do: nil
defp nilify(other), do: other
@compile {:inline, nillify: 1}
defp nillify(:undefined), do: nil
defp nillify(other), do: other
end

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