Compare commits

..
105 Commits
Author SHA1 Message Date
José Valim 59bc1598a2 Fix string versioning 2023-05-30 11:19:39 +02:00
José Valim 46dd1f2a54 Fix mix do syntax 2023-05-30 11:13:37 +02:00
José Valim e884060f03 Fix zips paths 2023-05-30 11:09:07 +02:00
José Valim 80268ceee9 Add branch docs publishing 2023-05-30 11:03:30 +02:00
Bobby fb729784e5 Fix typo in the docs for "with" (#11958) 2022-07-03 11:36:25 +02:00
José Valim 7e4fbe657d Release v1.13.4 2022-04-07 08:08:02 +02:00
Wei Huang d52f533761 Make Registry.send work when value part is present (#11742)
Fix #11740.
2022-04-06 11:01:14 +02:00
José Valim e0c8b8af87 Manual stacktrace munging on eval is no longer necessary on Erlang/OTP 25 2022-03-25 12:02:35 +01:00
José Valim f950226e6c Add OTP-24.3 to CI (#11712) 2022-03-19 09:13:08 +01:00
Michał Łępicki 89e57882e1 Tweak type unification to fix infinite loop with recursive vars (#11664) 2022-02-28 14:47:31 +01:00
José Valim 67cf14c857 Add opening delimiter to sigil metadata/opts 2022-02-17 20:43:21 +01:00
José Valim fe43e23a25 Update local rebar3 2022-02-17 19:59:08 +01:00
Michał Łępicki 65681b83d7 Skip tests using Rebar2 on Erlang/OTP 25+ (#11643)
* Skip tests using Rebar2 on Erlang/OTP 25+

and clean up mix test_helper.exs exclude filters

* Remove unnecessary printing from mix test_helper.exs exclude filters
2022-02-17 18:36:33 +01:00
José Valim 63d682b3d7 Deprecate rebar2 2022-02-17 18:26:40 +01:00
Michał Łępicki c028c2d915 Recreate local.sample task fixture in test_helper.exs (#11640) 2022-02-17 16:44:23 +01:00
José Valim 4b480cb7e0 Allow iodata in sigil formatting functions 2022-02-17 14:57:20 +01:00
Michał Łępicki f809d03370 Update Mix.DepTest for Erlang/OTP 25 (#11636)
:digraph_utils.topsort/1 can return a different (but also valid)
topological ordering

The test failure on Erlang/OTP 25.0-rc1 was:

    1) test deps_paths (Mix.DepTest)
       test/mix/dep_test.exs:491
       Assertion with == failed
       code:  assert Enum.map(Mix.Dep.load_on_environment([]), & &1.app) == [:git_repo, :abc_repo, :deps_repo]
       left:  [:abc_repo, :git_repo, :deps_repo]
       right: [:git_repo, :abc_repo, :deps_repo]
       stacktrace:
         test/mix/dep_test.exs:499: anonymous fn/0 in Mix.DepTest."test deps_paths"/1
         (elixir 1.14.0-dev) lib/file.ex:1555: File.cd!/2
         test/test_helper.exs:127: MixTest.Case.in_fixture/3
         test/mix/dep_test.exs:31: Mix.DepTest.with_deps/2
         test/mix/dep_test.exs:497: (test)
2022-02-17 10:07:22 +01:00
Michał Łępicki ee6ed7558f Update Mix.TasksTestTest for Erlang/OTP 25 (#11637)
The order in which tests get executed can be different
depending on Erlang/OTP version

The test failure was:

    1) test logs and errors umbrella with file path (Mix.Tasks.TestTest)
       test/mix/tasks/test_test.exs:432
       Assertion with =~ failed
       code:  assert mix(["test", "apps/unknown_app/test"]) =~
                "==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       left:  "==> foo\nCompiling 1 file (.ex)\nGenerated foo app\n==> bar\nCompiling 1 file (.ex)\nGenerated bar app\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       right: "==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       stacktrace:
         test/mix/tasks/test_test.exs:436: anonymous fn/0 in Mix.Tasks.TestTest."test logs and errors umbrella with file path"/1
         (elixir 1.14.0-dev) lib/file.ex:1555: File.cd!/2
         test/test_helper.exs:127: MixTest.Case.in_fixture/3
         test/mix/tasks/test_test.exs:433: (test)
2022-02-17 10:06:54 +01:00
Kevin bb17204741 Only check for unwanted functions (#11632) 2022-02-17 09:05:59 +01:00
José Valim 1e4ed32eda @doc false URI.Error.message/1 2022-02-14 13:25:14 +01:00
José Valim bac5b1d800 Add compile-time dependencies on require
Projects like Plug use require to establish compile
time dependencies inside a Plug. The fact require
only added a compile-time dependency in v1.13.0 was
therefore a regression, addressed by this commit.
2022-02-14 12:41:00 +01:00
Dwi Prihandi 39d8675ac1 Fix typespec on Base.decode16! (#11526) 2022-02-13 20:27:30 +01:00
José Valim b63f8f541e Release v1.13.3 2022-02-09 16:26:17 +01:00
José Valim babbae7e47 Handle more unicode sequences in split_at, closes #11617 2022-02-09 15:53:39 +01:00
José Valim 0f0d4c0f6e Track all stale modules from config/lock as exports too 2022-02-04 16:50:37 +01:00
Steve Hall 5ee492af55 Fix coverage threshold ignored for Total line in Summary (#11571)
An incorrect variable is being passed to the `display/2` function on line 353 for the Total line in the coverage summary report. The result is that that Total line will always display in red no matter what the threshold is set to.
2022-02-03 13:22:32 +01:00
José Valim 25e480f3d0 Do not break signatures over multiple lines 2022-02-01 10:04:30 +01:00
José Valim 2dc6773d6c Fix recursion on guards with map fields, closes #11602 2022-01-31 14:58:39 +01:00
José Valim ff103b19b9 Fix indentation of options in mix format 2022-01-24 16:48:28 +01:00
Matt McCoy d99a16256b Supply file and line to formatter plugins (#11591)
Allow formatter plugins to print errors with accurate file and
line information. The line is only provided when a sigil is being
formatted.
2022-01-24 08:37:17 +01:00
José Valim 1601b309b8 Check for fun arity in formatter 2022-01-24 08:37:17 +01:00
Jonatan Männchen 421a77d508 Embedded Elixir Expressions in Formatter Plugins (#11587)
Support Sigils inside embedded expressions in files formatted
by a formatter plugin.
2022-01-22 09:36:02 +01:00
felipe stival cc9e9b29a7 Fix duplicate bindings causing weird behaviour (#11584)
Before this fix, evaluating `b = a` with assignments of: `a: 1, a: 2, c:
3` would eval AST equivalent to `^c = a`. This happened due to binding
normalization generating version numbers larger than the total number of
bindings. Later in the pipeline, the number of bindings is used to
compute the next version number, which would then conflict with an
existing binding.

This commit fixes that by not increasing the version number when
a repeated binding is normalized.
2022-01-19 19:38:23 +01:00
Eksperimental 67f7c6ba5b Fix bug with edge-case when converting to algebra quoted Elixir alias (#11572)
The following code would crash:

    iex> Code.quoted_to_algebra(Elixir)
    ** (MatchError) no match of right hand side value: "Elixir"
        (elixir 1.13.1) lib/code/normalizer.ex:254: Code.Normalizer.normalize_literal/3
        (elixir 1.13.1) lib/code.ex:1107: Code.quoted_to_algebra/2

Therefore, this as well:

    iex> Macro.to_string(Elixir)
    ** (MatchError) no match of right hand side value: "Elixir"
        (elixir 1.13.1) lib/code/normalizer.ex:254: Code.Normalizer.normalize_literal/3
        (elixir 1.13.1) lib/code.ex:1107: Code.quoted_to_algebra/2
        (elixir 1.13.1) lib/macro.ex:948: Macro.to_string/1
2022-01-16 09:28:25 +01:00
José Valim c99a17b8cd Optimize Map.new/2 2022-01-13 15:28:01 +01:00
José Valim 9ad182986c Release v1.13.2 2022-01-13 10:51:17 +01:00
José Valim 16292c65ba Update CHANGELOG 2022-01-12 14:43:11 +01:00
José Valim d25ce48d5d Bring back filter/reject on Map and Keyword
They are already being used in projects, which
means it is too late for a deprecation. The docs
instead discuss when to use them.
2022-01-12 14:29:13 +01:00
Aaron Gunderson 66b4ab19ae Mix.Tasks.Test.Coverage warns on failure (#11457)
Adds an explicit warning when exiting with an error code because of a
failed test coverage threshold.

Example:
```
-----------|--------------------------
    62.35% | Total

Coverage test failed, threshold not met:
        Coverage:   62.35%
        Threshold: 100.00%
```
2022-01-02 19:58:06 +01:00
Aleksei Matiushkin 01a4889e39 Fix the nested uniq: acc name clash by preserving a scope (#11532) 2022-01-01 09:56:42 +01:00
José Valim d5e7446399 Add missing Logger CHANGELOG entry 2021-12-31 11:12:22 +01:00
Thiago Santos ab1da111bf Warn on zero arity callbacks inside protocols (#11519) 2021-12-22 22:01:33 +01:00
José Valim 6549b00eba Ensure context modules are handled in optimized defmodule 2021-12-22 12:22:22 +01:00
José Valim 4431ac63e2 Do not use type exclusive to Erlang/OTP 24+ 2021-12-22 10:18:02 +01:00
Thales Macedo Garitezi 280215c24f Allow bypassing application mode validation in release spec (#11506)
Today, there is a mode validation check when doing a Mix Release that
prevents a parent application that has an application mode of
`:permanent`, for example, while a child application has mode `:load`,
as it might be unsafe.

However, some complex applications may need more control over the
application load/start order.  For such cases, the user would need a
way to tell Mix.Release to don't be strict while constructing the
`.rel` file.

To allow for better control over the mode validation check instead of
simply disabling the check completely, we introduce the
`:skip_mode_validation_for` to allow users to specify a list of
applications for which the strict application mode validations should
not be enforced.
2021-12-20 12:48:11 +01:00
Marc-André Lafortune 071285403d Check plugins first so they can format .ex and .exs files (#11507) 2021-12-20 08:51:59 +01:00
José Valim 3d3453c1e2 Do not emit warnings on Cursor.Fragment.container_cursor_to_quoted/2 2021-12-16 16:44:10 +01:00
José Valim 2dc3fd26b8 Update CHANGELOG 2021-12-16 15:39:52 +01:00
José Valim e61ed47066 Deprecate map/filter/reject in Map and Keyword 2021-12-16 15:33:41 +01:00
José Valim 33f9d04851 Release v1.13.1 2021-12-14 23:31:53 +01:00
José Valim 8804ff5488 Add a note about chaining multiple map+filter calls 2021-12-14 23:12:01 +01:00
José Valim 7e66a4c159 Add pattern to Code.Fragment.surround_context/3 2021-12-14 23:12:01 +01:00
José Valim b085ad259d Improve DateTime docs 2021-12-14 12:21:39 +01:00
José Valim 8afac93a05 Ensure async streams can be consumed from a separate process 2021-12-13 15:49:44 +01:00
Dorgan b725b8c055 Set a max line_length for Macro.to_string (#11471) 2021-12-11 10:45:32 +01:00
felipe stival 47171d934d Change approach: never show error if line is empty (#11466) 2021-12-10 09:28:47 +01:00
José Valim edafcc4e9a Handle improper lists on apply, closes #11465 2021-12-09 21:22:59 +01:00
Dorgan 233fc09e67 Fix formatting of lists in module attribues (#11462) 2021-12-08 23:51:40 +01:00
José Valim 31e24b3a12 Fix codepoint byte counting in slice, closes #11461 2021-12-08 23:50:48 +01:00
José Valim d3b38fb472 Fix halt for --version 2021-12-07 08:35:21 +01:00
José Valim 2efc1e5cac Make sure --version flag halts elixir and iex, closes #11453 2021-12-07 08:24:01 +01:00
José Valim a0b77bd884 Do not deprecate URI.parse/1
Closes #11450.
2021-12-06 14:59:48 +01:00
Parker Selbert cbba61a386 Specific base typespecs (#11449)
* Use specific options for base option typespecs

Each function had `keyword` as the option type, which didn't help guard
against typos or mismatched options.

* Fix padding use in encode/decode identity test
2021-12-05 19:50:33 +01:00
José Valim ebb347a34e Make protocol consolidation part of the Mix.install cache 2021-12-05 19:29:56 +01:00
Wojtek Mach 713633a939 Add missing @doc since (#11443) 2021-12-04 19:57:44 +01:00
Jason Axelson d239d9991a Update wording for 1.13 get_in update (#11442)
Make it more clear what the behavior change is.

Relates to this change: https://github.com/elixir-lang/elixir/commit/587c80eae6e29002370523f79273d56dabd48582#diff-43a76a6112c9082ee3ddb63acc2419db8764edd039cb52e29e436734b414574b
Related ElixirForum post: https://elixirforum.com/t/elixir-version-of-a-safe-navigation-operator-navigating-nil-in-maps-structs/6023/29
2021-12-04 19:31:34 +01:00
José Valim caed7d1d3f Release v1.13.0 2021-12-03 19:03:54 +01:00
José Valim dfd12b9cf3 Do not run test suite on mix test --profile-require 2021-12-03 11:13:15 +01:00
José Valim ed91b3373a Still document nil as part of the URI path, closes #11424 2021-12-02 23:23:59 +01:00
José Valim aeb2e079c5 Remove pending compile_ref 2021-12-02 16:49:54 +01:00
José Valim 3a8d7f4eb1 Track transitive runtime dependencies coming from local deps 2021-12-02 16:46:13 +01:00
Dorgan 1dc6e7144f Fix normalization of kw list in blocks (#11431) 2021-12-02 08:22:43 +01:00
Eksperimental 1f9bc5dc95 Improve Version.compile_requirement/1 (#11427) 2021-11-30 11:25:36 +01:00
José Valim 0974e9075a Skip errors on head of generated clauses, closes #11407 2021-11-23 14:30:44 +01:00
Eksperimental 3671ce7ebf Fix links to EEP 48 (#11412) 2021-11-22 13:05:07 +01:00
José Valim fd1c12f46c Fix typespec for Macro.struct!/2 2021-11-19 23:15:23 +01:00
José Valim 709383f142 Warn on trailing commas on calls, closes #11399 2021-11-19 11:46:37 +01:00
Dorgan e9be638d92 Fix normalizer not preserving user choice on module attribute lists (#11397) 2021-11-17 15:06:02 +01:00
José Valim 2765e0f210 Release v1.13.0-rc.1 2021-11-16 21:36:19 +01:00
José Valim c1fd4dd8da Revert "Clarify risk of duplicate keys in Map.map/2 (#11360)"
This reverts commit 6074846794.
2021-11-15 21:10:51 +01:00
José Valim 6d408bb0c0 Reject bidirectional formatting characters (#11391) 2021-11-15 21:10:51 +01:00
José Valim ffd891a349 Support escaping of terminators in uppercase sigils heredocs for consistency, closes #11390 2021-11-15 14:21:28 +01:00
José Valim d7b1591e83 Do not raise on variable looking like an empty tuple 2021-11-13 12:37:59 +01:00
Rudolf 24547bbe96 remsh: stop evaluator before exit iex_server (#11386)
Closes #11385.
2021-11-12 09:17:41 +01:00
Tyler A. Young 9b4a01fe33 Corrections & clarifications to Task.Supervisor docs (#11384) 2021-11-12 09:06:13 +01:00
José Valim 94993e0036 Ensure fixtures are executed in archive test
Closes #11382.
2021-11-11 22:05:28 +01:00
Eksperimental 00a0f07824 Allow Application.compile_env* to accept attributes (#11383) 2021-11-11 22:05:28 +01:00
José Valim c28073b4fc Fix warning from call to dynamically generated module 2021-11-11 22:04:47 +01:00
José Valim 8387dd70bc Fix bootstrap 2021-11-10 19:37:34 +01:00
José Valim 0e59c68428 Do not crash on duplicate bindings, closes #11378 2021-11-10 19:27:59 +01:00
José Valim 913d401a7a Perform implicit Application require as the compiler is aware of its constructs 2021-11-09 18:13:48 +01:00
José Valim fbfb1b4a12 Do not extract comments without a newline 2021-11-08 18:05:59 +01:00
José Valim cf5762ad24 Use keyword syntax by default in tuple formatting 2021-11-08 17:25:36 +01:00
Wojtek Mach fab158c0de Add Mix.installed?/0 (#11374) 2021-11-08 13:14:13 +01:00
José Valim 3a484a777d Fix CHANGELOG 2021-11-07 12:24:50 +01:00
José Valim 8c1b563a3c Keep yes?/1 as a callback, closes #11372 2021-11-07 12:22:52 +01:00
José Valim ece0530870 Fix regression on URI.parse/1, closes #11363 2021-11-07 10:48:26 +01:00
Guillaume Duboc 48b838a840 Fix formatting of map-like types in warnings (#11351)
This came up in issue #11204:

When emitting a warning for a type unification error,
the compiler overly simplifies the formatting of types
when these are maps or unions of maps.

To address this, we recursively check for maps inside
of a union type when comparing it to another map-like type.
2021-11-07 09:31:00 +01:00
José Valim 5aa1c9a839 Do not reinstall dependencies on same Mix.install/2 2021-11-05 00:05:15 +01:00
José Valim 3a8a7efecc Ensure structs can be in release configs, closes #11364 2021-11-04 08:41:15 +01:00
Tyler A. Young 3d038680da Clarify risk of duplicate keys in Map.map/2 (#11360) 2021-11-03 23:46:14 +01:00
Tyler A. Young 1766731dd4 Improvements to Enum.slide/3 (#11361)
- Support negative insertion indices
- Give a clear RuntimeError, rather than a baffling CondClauseError, when you ask for an insertion point that matches the last element of your range
2021-11-03 23:41:25 +01:00
José Valim b65b8e2cd3 Fix broken link in Task docs 2021-11-01 22:30:25 +01:00
José Valim 5062a154fa Release v1.13.0-rc.0 2021-11-01 20:58:47 +01:00
José Valim d7fea95157 Branch out v1.13 2021-11-01 20:53:52 +01:00
361 changed files with 9111 additions and 38409 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
]
]
+21
View File
@@ -0,0 +1,21 @@
### Precheck
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
* For bugs, do a quick search and make sure the bug has not yet been reported
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
* Do not use the issues tracker for guidance, questions or support (try Elixir Forum, Stack Overflow, Slack, etc. instead)
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang/OTP versions (elixir --version):
* Operating system:
### Current behavior
Include code samples, errors and stacktraces if appropriate.
If reporting a bug, please include the reproducing steps.
### 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"
+2 -2
View File
@@ -22,8 +22,8 @@ jobs:
max-parallel: 1
matrix:
include:
- otp: 25
otp_version: '25.3'
- otp: 23
otp_version: '23.3'
build_docs: build_docs
runs-on: ubuntu-20.04
steps:
+13 -36
View File
@@ -8,35 +8,28 @@ env:
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
permissions:
contents: read
jobs:
test_linux:
name: Linux, ${{ matrix.otp_release }}, Ubuntu 20.04
name: Linux, ${{ matrix.otp_release }}, Ubuntu 18.04
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-24.3', 'OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
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-20.04
runs-on: ubuntu-18.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-20.04/${{ matrix.otp_release }}.tar.gz
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-18.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,12 +38,11 @@ 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
@@ -60,37 +52,22 @@ jobs:
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.3']
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 +90,9 @@ jobs:
check_posix_compliant:
name: Check POSIX-compliant
runs-on: ubuntu-20.04
runs-on: ubuntu-18.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-20.04
name: Notify
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- uses: erlef/setup-beam@v1
with:
otp-version: '25.0'
elixir-version: '1.14.0'
- 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-20.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-20.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
@@ -40,7 +40,7 @@ runs:
run: |
mv ex_doc ../ex_doc
cd ../ex_doc
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
../elixir/bin/mix do local.rebar --force, local.hex --force, deps.get, compile
cd ../elixir
- name: Build Docs
if: ${{ inputs.build_docs }}
+293 -485
View File
@@ -1,600 +1,408 @@
# Changelog for Elixir v1.14
# Changelog for Elixir v1.13
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.
The focus behind Elixir v1.13 has been on tooling, mainly tooling related to code formatting, code fragments, code reflection, and code recompilation. A lot of this functionality will directly impact developers working on large codebases and provide meaningful quality of life improvements for those working on Elixir tooling and environments, such as IDEs, notebooks, etc.
Elixir v1.14 is the last version to support Erlang/OTP 23. Consider updating
to Erlang/OTP 24 or Erlang/OTP 25.
## Semantic recompilation
## `dbg`
Elixir v1.13 comes with many improvements to the compiler, so it recompiles your files less frequently. In particular:
`Kernel.dbg/2` is a new macro that's somewhat similar to `IO.inspect/2`, but
specifically tailored for **debugging**.
* The digest of the files are considered in addition to their size. This avoids recompiling many files when switching or rebasing branches.
When called, it prints the value of whatever you pass to it, plus the debugged
code itself as well as its location. This code:
* Changing your `mix.exs` will no longer trigger a full recompilation, unless you specifically change the configurations used by the Elixir compiler (`:elixirc_paths` and `:elixirc_options`).
* Changing compile-time configuration files (`config/config.exs` and any other file imported from it) now only recompiles the project files that depend on the reconfigured applications, instead of a full recompilation. However, if you change the configuration of your application itself, the whole project is still recompiled.
* Adding, updating or removing a dependency now only recompiles the project files that depend on the modified a dependency.
* If your project has both Erlang and Elixir files, changing an Erlang file will now recompile only the Elixir files that depend on it.
In a nutshell, Elixir went from triggering full recompilations whenever any of `mix.exs`, `config/config.exs`, `src/*`, and `mix.lock` changed on disk to semantic recompilations. Now it only fully recompiles when:
* you change the compilation options in `mix.exs`
* you change the configuration for the current project in `config/config.exs`
## mix xref
`mix xref` is a tool that analyzes relationships between files. By analyzing the compile-time and runtime dependencies between files, it allows developers to understand what files have to be recompiled whenever a file changes.
Elixir v1.13 comes with many improvements to `mix xref`, such as:
* `mix xref graph` now supports `--label` to be set to "compile-connected", which returns all compile-time dependencies that lead to additional transitive dependencies.
* A new `mix xref trace FILE` subcommand receives a file and returns all dependencies in said file, including the line and what caused said dependency (a function/macro call, an alias, a struct, etc).
* All `mix xref` subcommands support the `--fail-above` flag, which allows you to enforce your project has at most a certain number of compile-time cycles, transitive compile-time dependencies, etc.
* `mix xref graph` now supports multiple `--sink` and `--source` to be given.
With these improvements, it has become simpler to understand the impact code recompilation has in our codebases and how to limit it.
## Code fragments
The `Code` module got a companion module called `Code.Fragment`, which hosts functions that work on incomplete code, as is often the scenario in editors, interactive shells, etc. The module contains different heuristics to analyze the source code and return context informational.
Thanks to these improvements, `IEx`' autocomplete got several quality of life improvements, such as the autocompletion of sigils, structs, and paths. For example, typing `~<TAB>` now shows:
```iex
iex(1)> ~
~C (sigil_C) ~D (sigil_D) ~N (sigil_N) ~R (sigil_R)
~S (sigil_S) ~T (sigil_T) ~U (sigil_U) ~W (sigil_W)
~c (sigil_c) ~r (sigil_r) ~s (sigil_s) ~w (sigil_w)
```
Adding the sigil letter and pressing tab then shows the available delimiters:
```iex
iex(1)> ~r
" """ ' ''' ( / < [ { |
```
Similarly, `%<TAB>` now shows only the available structs (exceptions excluded), instead of all modules:
```elixir
# In my_file.exs
feature = %{name: :dbg, inspiration: "Rust"}
dbg(feature)
dbg(Map.put(feature, :in_version, "1.14.0"))
iex(1)> %File.St
File.Stat File.Stream
```
Prints this:
```shell
$ elixir my_file.exs
[my_file.exs:2: (file)]
feature #=> %{inspiration: "Rust", name: :dbg}
[my_file.exs:3: (file)]
Map.put(feature, :in_version, "1.14.0") #=> %{in_version: "1.14.0", inspiration: "Rust", name: :dbg}
```
`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:
Once you define the struct, you can hit `tab` to show all struct fields available:
```elixir
# In dbg_pipes.exs
__ENV__.file
|> String.split("/", trim: true)
|> List.last()
|> File.exists?()
|> dbg()
iex(1)> %URI{
authority: fragment: host: path: port:
query: scheme: userinfo:
```
Prints this:
```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
```
### IEx and Prying
`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.
You can keep the default behaviour by passing the `--no-pry` option to IEx.
## PartitionSupervisor
`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.
For example, imagine you have an `ErrorReporter` process that you use to report
errors to a monitoring service.
As you fill a field in, the already filled fields no longer show up:
```elixir
# Application supervisor:
children = [
# ...,
ErrorReporter
]
Supervisor.start_link(children, strategy: :one_for_one)
iex(1)> %URI{path: "/example",
authority: fragment: host: port: query:
scheme: userinfo:
```
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`.
Along the same lines, `SyntaxError` and `TokenMissingError` were improved to show a code snippet whenever possible:
```elixir
# Application supervisor
children = [
{PartitionSupervisor, child_spec: ErrorReporter, name: Reporters}
$ elixir -e "hello + * world"
** (SyntaxError) nofile:1:9: syntax error before: '*'
|
1 | hello + * world
| ^
```
Finally, new compilation tracers have been added, alongside a handful of functions in `Module` to retrieve module metadata, which can be used to enrich suggestions in programming environments.
## Extended code formatting
The `mix format` task has been augmented with the notion of plugins. Plugins can teach the formatter how to format new files and how to format sigils, via the `Mix.Tasks.Format` behaviour.
For example, imagine that your project uses Markdown in two distinct ways: via a custom `~M` sigil and via files with the `.md` and `.markdown` extensions. A custom plugin would look like this:
```elixir
defmodule MixMarkdownFormatter do
@behaviour Mix.Tasks.Format
def features(_opts) do
[sigils: [:M], extensions: [".md", ".markdown"]]
end
def format(contents, opts) do
# logic that formats markdown
end
end
```
Now any application can use your formatter as follows:
```elixir
# .formatter.exs
[
# Define the desired plugins
plugins: [MixMarkdownFormatter],
# Remember to update the inputs list to include the new extensions
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "posts/*.{md,markdown}"]
]
```
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.
Finally, the `Code` module has also been augmented with two functions: `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`. Those functions allow someone to retrieve the Elixir AST with their original source code comments, and then convert this AST to formatted code. In other words, those functions provide a wrapper around the Elixir Code Formatter, supporting developers who wish to create tools that directly manipulate and custom format Elixir source code.
```elixir
partitioning_key = self()
ErrorReporter.report({:via, PartitionSupervisor, {Reporters, partitioning_key}}, error)
```
## v1.13.4 (2022-04-07)
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.5 (2023-05-22)
This release contains fixes for Erlang/OTP 26.
### Bug fixes
#### Elixir
* [CLI] Fix a bug where stdout would block when there was no attached terminal on Windows when running on Erlang/OTP 26
#### Mix
* [Mix] Properly set SSL configuration for Mix downloads when running on Erlang/OTP 26
## v1.14.4 (2023-04-03)
This release adds basic support for Erlang/OTP 26. When migrating
to Erlang/OTP 26, keep it mind it changes how maps are stored
internally and they will be printed and traversed in a different
order (note maps never provided a guarantee of their order).
To aid migration, this release adds `:sort_maps` to `inspect`
custom options, in case you want to sort them before inspection:
inspect(map, custom_options: [sort_maps: true])
### Enhancements
#### Elixir
* [Inspect] Add `:sort_maps` to `Inspect.Opts.custom_options`
#### IEx
* [IEx] Support shell history in Erlang/OTP 26+
#### Mix
* [mix compile.elixir] Optimize application tracer
### Bug fixes
#### Elixir
* [Code] Properly handle blocks with comments in all cases in `Code.quoted_to_string_with_comments/2`
* [Kernel] Fix `debug_info/4` when returning core_v1
* [Kernel] Store complete path on `quote keep: true` to avoid invalid stacktraces
* [Kernel] Fix column count when tokenizing escaped interpolations
* [Stream] Fix `Stream.zip/1` hanging on empty list
#### Mix
* [mix format] Don't call formatter on directories
## v1.14.3 (2023-01-14)
This release has been verified to work with Erlang/OTP 25 RC2.
### 1. Enhancements
#### Elixir
* [Kernel] Speed up loading of runtime modules in the parallel compiler
* [Range] Optimize range membership implementation
#### ExUnit
* [ExUnit] Return values from running doctests and make their order consistent
* [Code] Allow iodata to be returned in sigil formatting functions
* [Code] Pass opening delimiter information to sigil formatting functions
### 2. Bug fixes
#### Elixir
* [Calendar] Fix handling of negative years in `Calendar.strftime/2`
* [DateTime] Consistently merge precision of subsecond operations (the bug was that subsecond precision was lost in functions like `DateTime.add/3`)
* [Exception] Improve blaming of FunctionClauseError with `is_struct/2` guards
* [Kernel] Fix invalid variable scoping in `defguard` expansion
* [Kernel] Do not warn on captured underscored vars from `defmodule`
* [Kernel] Do not crash for missing line info on type warnings
* [NaiveDateTime] Consistently merge precision of subsecond operations (the bug was that subsecond precision was lost in functions like `NaiveDateTime.add/3`)
* [Macro] Fix `Macro.to_string/1` for large negative integers
* [Macro] Properly type and escape expansion of `__ENV__` in macros
* [Path] Make sure `Path.wildcard/2` expands `..` symlinks accordingly
* [Range] Address corner cases in `Range.disjoint?/2` implementation
* [Time] Consistently merge precision of subsecond operations (the bug was that subsecond precision was lost in functions like `Time.add/3`)
* [Kernel] Tweak type unification to fix infinite loop with recursive vars
* [Kernel] Add compile-time dependencies on `require`
* [Registry] Make `Registry` send work with named triplets
#### ExUnit
* [ExUnit.DocTest] Remove unnecessary literal quotes from error message on reports
## v1.14.2 (2022-11-11)
### 1. Enhancements
#### 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`
### 3. Deprecations
#### Mix
* [Mix] `Mix.install/2` accepts atoms as paths
* [mix rebar] Deprecate Rebar 2 as it no longer works on Erlang/OTP 25
## v1.13.3 (2022-02-09)
### 1. Enhancements
#### Mix
* [mix format] Supply file and line to formatter plugins
* [mix format] Support embedded Elixir expressions inside formatter plugins
### 2. Bug fixes
#### Elixir
* [Code.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`
* [Code] Fix duplicate bindings causing errors during evaluation
* [Kernel] Make sure signatures stored in the Documentation chunk does not contain newlines
* [Kernel] Fix infinite loop when compiling guards with recursive map access
* [Macro] Fix error on `Macro.to_string/1` when the plain alias `Elixir` is given
* [String] Fix error for certain codepoint combinations in `String.split_at/2`
#### Mix
* [mix test] Ensure proper error message when there is no test directory
* [mix compile] Recompile project files when exports from dependencies change
* [mix test] Fix total coverage always showing in red even when above the threshold
## v1.14.1 (2022-10-10)
## v1.13.2 (2022-01-13)
### 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
* [mix format] Allow plugins to also format `.ex` and `.exs` files
* [mix release] Allow bypassing application mode validation in release spec
* [mix test] Print a message when the suite fails due to the coverage threshold
## 2. Bug fixes
### 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`
* [Code] Do not emit warnings on `Code.Fragment.container_cursor_to_quoted/2`
* [Kernel] Fix a crash when a for-comprehension with `:uniq` was used inside another comprehension with `:uniq`
* [Kernel] Ensure `env.context_modules` is properly set inside optimized `defmodule`
* [Keyword] Deprecate the recently added `Keyword.map/2` as it is equivalent to `Keyword.new/2`
* [Map] Deprecate the recently added `Map.map/2` as it is equivalent to `Map.new/2`
* [Protocol] Warn on zero arity callbacks inside protocols
#### ExUnit
## v1.13.1 (2021-12-14)
* [ExUnit.DocTest] Do not crash when both `:moduledoc` and functions are specified in `:only`
### 1. Bug fixes
#### Elixir
* [Code] Do not show code snippets in `SyntaxError` and `TokenMissingError` if line is empty
* [Exception] Do not fail blaming `ArgumentError` for improper lists on `apply/3`
* [Macro] Set a max `line_length` for `Macro.to_string/1`
* [Macro] Fix formatting of lists on module attributes for `Macro.to_string/1`
* [String] Fix incorrect codepoint byte counting in `slice` with negative positions in ranges
* [Task] Ensure async streams can be consumed from another process than the one that creates them
* [URI] Undeprecate `URI.parse/1` as `URI.new/1` is too strict in many common cases
* [URI] Make sure `URI.new/1` returns nil for empty paths
#### IEx
* [CLI] Fix invalid argument handling when `--no-pry` is given
* [IEx] Make sure the `--version` flag halts IEx
#### Mix
* [mix format] Do not cache inputs from `.formatter.exs` so they are properly re-evaluted on every call
* [Mix] Make protocol consolidation part of the `Mix.install/2` cache
## v1.14.0 (2022-09-01)
## v1.13.0 (2021-12-03)
### 1. Enhancements
#### EEx
* [EEx] Support multi-line comments to EEx via `<%!-- --%>`
* [EEx] Add `EEx.tokenize/2`
* [EEx] Add `:parser_options` to EEx functions
#### 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
* [Calendar] Add `c:Calendar.year_of_era/3` to support calendars where the beginning of a new era does not align with the beginning of a new year
* [CLI] Support `--short-version` on the CLI that does not boot the VM
* [Code] Add `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`
* [Code] Add more `:token_metadata` to aliases and remote calls when parsing strings
* [Code] Add `Code.Fragment` module to provide best-effort information from code fragments. The module currently provides an updated `Code.Fragment.cursor_context/2` with operator support and `Code.Fragment.surround_context/2` which looks at a given position in a fragment and find its surrounding delimiters
* [Code] Allow custom sigil formatting on `Code.format_string!/2`
* [Code] Add `{:on_module, bytecode, :none}` trace to compilation tracers
* [Enum] Optimize `Enum.concat/1` for lists of lists
* [Enum] Add `Enum.slide/3`
* [Exception] Better format Elixir exceptions in Erlang
* [Inspect] Allow default inspect fun to be set globally with `Inspect.Opts.default_inspect_fun/1`
* [IO] Allow `:eof` to be given as limit to `IO.getn/2`
* [Kernel] Support the `:sigils` option in `import Mod, only: :sigils` and allow the sigil modifiers to be also digits
* [Kernel] Make `get_in` consistently abort and return `nil` when `nil` values are found (previously Elixir would raise an error in this case). This allows a user to use `get_in` as a safe navigation operator.
* [Kernel] Improve compilation times by reducing the amount of copies of the AST across compiler processes
* [Kernel] Raise if trying to define a module with a slash in its name
* [Kernel] Warn when `?\` is used and there is no need for a escape character
* [Kernel] Track structs in typespecs as export deps instead of compile-time deps
* [Kernel] Add power operator (`**/2`)
* [Keyword] Add `Keyword.validate/2`
* [Keyword] Implement `Keyword.filter/2` and `Keyword.map/2`
* [List] Add `List.keyfind!/3`
* [Macro] Add `Macro.prewalker/1` and `Macro.postwalker/1`
* [Macro.Env] Add the following reflection functions: `required?/2`, `lookup_import/2`, `fetch_alias/2`, and `fetch_macro_alias/2`
* [Map] Implement `Map.filter/2` and `Map.map/2`
* [Module] Support `:nillify_clauses` in `Module.get_definition/3`
* [Module] Add `Module.attributes_in/1` and `Module.overridables_in/1`
* [OptionParser] Add "did you mean?" suggestions to `OptionParser.ParseError` messages
* [Record] Add record reflection via `@__records__`
* [Task] Add `Task.completed/1`
* [Task] Add `Task.ignore/1` to keep a task running but ignoring all of its results
* [Task] Reduce the amount of copying `Task.async*` functions
* [URI] Add `URI.new/1` and `URI.new!/1`
#### 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] Show hint if comparing different but equivalent strings
* [ExUnit.CaptureIO] Add `with_io/3` to return result with captured io
* [ExUnit.CaptureLog] Add `with_log/2` to return result with captured logs
#### 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`
* [IEx.Autocomplete] Add path autocompletion whenever when the cursor follows `"./` or `"/` or `"DRIVER:` where `DRIVER` is a single letter
* [IEx.Autocomplete] Add autocompletion for sigils, struct names, and struct fields
* [IEx.Helpers] Allow multiple modules to be given to `r/1`
#### Logger
* [Logger] Add `Logger.put_process_level/2`
* [Logger] Add `Logger.put_application_level/2`
* [Logger] Print all log levels in accordance to Erlang/OTP. This also means `[warn]` is now shown as `[warning]`
#### 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_FORCE` environment variable support
* [Mix] Support `:config` and `:system_env` in `Mix.install/2`
* [Mix] Add `Mix.installed?/0`
* [Mix.Shell] Add `:default` option to `Mix.Shell.yes?`
* [mix archive.install] Run `loadconfig` before building archive
* [mix compile] Move Elixir version check to before deps are compiled, in order to give feedback earlier
* [mix compile.elixir] Do not recompile files if their modification time change but their contents are still the same and the .beam files are still on disk
* [mix compile.elixir] Do not recompile all Elixir sources when Erlang modules change, only dependent ones
* [mix compile.elixir] Do not recompile Elixir files if `mix.exs` changes, instead recompile only files using `Mix.Project` or trigger a recompilation if a compiler option changes
* [mix compile.elixir] Only recompile needed files when a dependency is added, updated or removed
* [mix compile.elixir] Only recompile needed files when a dependency is configured
* [mix deps] Add `:subdir` option to git deps
* [mix escript.install] Run `loadconfig` before building escript
* [mix format] Support `:plugins` in `mix format` that can hook into custom extensions and sigils
* [mix format] Add `Mix.Tasks.Format.formatter_for_file/2`
* [mix local.rebar] No longer support `sub_dirs` in Rebar 2 to help migration towards Rebar 3
* [mix local.rebar] Support `--if-missing` option when installing Rebar
* [mix local.rebar] Set `REBAR_PROFILE=prod` when compiling Rebar dependencies
* [mix test] Support `--profile-require=time` to profile the time loading test files themselves
* [mix test] Allow filtering modules from coverage using regex
* [mix test] Allow the exit status of ExUnit to be configured and set the default to 2
* [mix test] Exit with a status of 3 when coverage falls below threshold
* [mix test] Write failed manifest when suite fails due to --warnings-as-errors
* [mix test] Ignore `MIX_TEST_PARTITION` when partitions set to 1
* [mix xref] Support multiple sinks and sources in `mix xref graph`
* [mix xref] Add `trace` subcommand to print compilation dependencies between files
* [mix xref] Add `--fail-above` option to `mix xref`
* [mix xref] Add `--label compile-connected` to `mix xref`
### 2. Bug fixes
#### EEx
* [EEx] Accept comments in EEx between do and the first clause
* [EEx] Accept EEx expressions where `->` is followed by newline
#### Elixir
* [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
* [Application] Allow any expression as first argument of `compile_env`
* [Application] Warn if `Application.compile_env` or `Application.compile_env!` are called without a require
* [Code] Make sure `:static_atoms_encoder` in `Code.string_to_quoted/2` also applies to quoted keyword keys
* [Code] Ensure bindings with no context are returned as atoms instead of `{binding, nil}` in eval operations
* [Inspect] Fix a bug when inspecting a non-binary bitstring with colors
* [Kernel] Reject bidirectional formatting characters in strings and comments
* [Kernel] Support escaping of terminators in uppercase sigils heredocs for consistency
* [Kernel] Raise if `__CALLER__` or `__ENV__` or `__STACKTRACE__` are used in match
* [Kernel] Improve error message on invalid argument for `byte_size` from binary concat
* [Kernel] Raise when aliasing non-Elixir modules without `:as`
* [Kernel] Allow `unquote_splicing` inside `%{...}` without parens
* [Kernel] Ensure that waiting on a struct expansion inside a typespec is correctly tracked as waiting time in the compiler
* [Kernel] Correctly parse the atom `.` as a keyword list key
* [Kernel] Do not leak variables from the first generator in `with` and `for` special forms
* [Kernel] Fix column number on strings with NFD characters
* [Kernel] Fix a bug where a combination of dynamic line in `quote` with `unquote` of remote calls would emit invalid AST metadata
* [OptionParser] Validate switch types/modifiers early on to give more precise feedback
* [Protocol] Add `defdelegate` to the list of unallowed macros inside protocols as protocols do not allow function definitions
* [Protocol] Warn if `@callback`, `@macrocallback` and `@optional_callbacks` are defined inside protocol
* [Protocol] Ensure protocol metadata is deterministic on consolidation
* [Range] Always show step when range is descending
* [String] Update Unicode database to version 14.0
* [URI] Only percent decode if followed by hex digits (according to https://url.spec.whatwg.org/#percent-decode)
* [Version] Ensure proper precedence of `and`/`or` in version requirements
#### 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] Fix formatter and counters from `ExUnit.run/0` to consider all tests in a module whenever if a module's `setup_all` fails
* [ExUnit] Allow doctests newlines to be terminated by CRLF
#### IEx
* [IEx] Disallow short-hand pipe after matches
* [IEx] Fix `exports/1` in IEx for long function names
#### 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`
### 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
#### EEx
* [EEx] Using `<%# ... %>` for comments is deprecated. Please use `<% # ... %>` or the new multi-line comments with `<%!-- ... --%>`
* [IEx] Fix the loss of `.iex.exs` context after a pry session
* [IEx] Stop evaluator before exiting IEx server to avoid evaluators leaking
#### Logger
* [Logger] Deprecate `Logger.enable/1` and `Logger.disable/1` in favor of `Logger.put_process_level/2`
* [Logger] Raise clear error message for invalid `:compile_time_purge_matching` configuration
* [Logger] Fix a bug where Logger would not reset its discard counter under some scenarios
#### Mix
* [mix cmd] The `--app` option in `mix cmd CMD` is deprecated in favor of the more efficient `mix do --app app cmd CMD`
* [mix compile.elixir] Track transitive runtime dependencies coming from local/path dependencies
* [mix compile.elixir] Recompile file if `@external_resource` is deleted
* [mix compile.elixir] Print number of compiling files on all compiler cycles. This will make the `Compiling N files (.ex)` show up multiple times if necessary
* [mix deps] Raise if local dep is unavailable while compiling
* [mix deps.unlock] Fix blank output when unlocking a dependency that is not locked
* [mix local.install] Do not respect `MIX_DEPS_PATH` for install commands
* [mix release] Improve release scripts by making sure shell errors cascade (this is done by avoiding exporting and defining variables in a single step)
* [mix release] Do not boot release if `RELEASE_COOKIE` is empty
* [mix release] Allow releases running as a daemon to be restarted
* [mix release] Raise proper error message when non-serializable values are in configs
* [mix test] Fix coverage engine to also tag `case`, `cond`, and `receive` branches where the right side is a literal
### 4. Hard deprecations
### 3. Soft-deprecations (no warnings emitted)
#### 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
* [Code] Environment options in `Code.eval_quoted/3` and `Code.eval_string/3`, such as `:aliases` and `:tracers`, have been deprecated in favor of passing an environment
* [IO] `:all` on `IO.getn` is deprecated in favor of `:eof`
* [URI] `URI.parse/1` is deprecated in favor of `URI.new/1` and `URI.new!/1`
#### Mix
* [Mix] `Mix.Tasks.Xref.calls/1` is deprecated in favor of compilation tracers
* [mix format] `Mix.Tasks.Format.formatter_opts_for_file/2` is deprecated in favor of `Mix.Tasks.Format.formatter_for_file/2`
### 5. Backwards incompatible changes
### 4. Hard-deprecations
#### Elixir
* [Code] `Code.cursor_context/2` is deprecated, use `Code.Fragment.cursor_context/2` instead
* [Macro] `Macro.to_string/2` is deprecated, use `Macro.to_string/1` instead
* [System] `System.get_pid/0` is deprecated, use `System.pid/0` instead
* [Version] Using `!` or `!=` in version requirements is deprecated, use `~>` or `>=` instead
#### Mix
* [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
* [mix escript.build] `:strip_beam` option is deprecated in favor of `:strip_beams`
* [Mix] `:exit_code` in `Mix.raise/2` has been deprecated in favor of `:exit_status`
* [Mix.Config] `Mix.Config` is deprecated in favor of `Config` module
## v1.13
## v1.12
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).
The CHANGELOG for v1.12 releases can be found [in the v1.12 branch](https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md).
+25 -15
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.13/
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,6 @@ 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/tokenizer.ex -o lib/elixir/ebin;
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
@@ -128,7 +126,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 +144,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 +158,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
@@ -175,12 +173,14 @@ clean_residual_files:
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}")
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)"
@@ -235,9 +235,19 @@ Docs.zip: docs
Precompiled.zip: build_man compile
rm -f Precompiled.zip
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE Makefile man NOTICE README.md VERSION
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
@@ -294,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)
@@ -333,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
+7 -14
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.
@@ -115,7 +114,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 +126,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
@@ -171,12 +164,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 +188,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:
+12 -16
View File
@@ -10,13 +10,19 @@
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
@@ -28,22 +34,12 @@
3. Commit "Branch out vMAJOR.MINOR"
### Back in main
### Back in master
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
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
+3 -3
View File
@@ -6,17 +6,17 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
Elixir version | Support
:------------- | :-----------------------------
1.14 | Bug fixes and security patches
1.13 | Security patches only
1.13 | Bug fixes and security patches
1.12 | Security patches only
1.11 | Security patches only
1.10 | Security patches only
1.9 | Security patches only
## 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.5
1.13.4
+14 -19
View File
@@ -1,7 +1,7 @@
#!/bin/sh
set -e
ELIXIR_VERSION=1.14.5
ELIXIR_VERSION=1.13.4
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
cat <<USAGE >&2
@@ -100,29 +100,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 +186,6 @@ while [ $I -le $LENGTH ]; do
fi
;;
--werl)
S=1
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
;;
*)
@@ -196,13 +198,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
+4 -14
View File
@@ -1,6 +1,6 @@
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
set ELIXIR_VERSION=1.14.5
set ELIXIR_VERSION=1.13.4
setlocal enabledelayedexpansion
if ""%1""=="""" if ""%2""=="""" goto documentation
@@ -141,7 +141,6 @@ if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && go
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 +174,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
+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
+48 -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,48 +34,24 @@ 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:
<%| ... %>
<%/ ... %>
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code that is generated.
See `Code.string_to_quoted/2` for available options. Note that the options
`:file`, `:line` and `:column` are ignored if passed in.
Defaults to `Code.get_compiler_option(:parser_options)` (which defaults to `[]` if not set).
## Engine
@@ -85,10 +61,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 +109,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 +116,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 +148,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 +197,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 +207,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 +223,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 +245,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 +261,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 +280,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
+85 -321
View File
@@ -3,345 +3,80 @@ 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
column = 1
indentation = opts[:indentation] || 0
trim = opts[:trim] || false
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
engine = opts[:engine] || @default_engine
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: 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, start_column}, state.file, message)
end
{rest, line, contents} = look_ahead_middle(rest, meta.line, chars) || {rest, meta.line, chars}
{rest, line, contents} =
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
{contents, rest} =
generate_buffer(
@@ -352,8 +87,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 +97,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, column}, 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 +145,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, column}, 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,10 +195,7 @@ 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} | rest], start, contents) do
if only_spaces?(text) do
look_ahead_middle(rest, start, contents ++ text)
else
@@ -439,8 +203,8 @@ defmodule EEx.Compiler do
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
{rest, line, contents ++ chars}
end
defp look_ahead_middle(_tokens, _start, _contents) do
@@ -448,7 +212,7 @@ defmodule EEx.Compiler do
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
+244
View File
@@ -0,0 +1,244 @@
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, _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
{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, 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, 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
+129 -219
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,191 @@ 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}}
]}
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
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
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
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}}
{:start_expr, 1, 1, '', ' if true do '},
{:text, 1, 31, 'bar'},
{:end_expr, 1, 34, [], ' end '},
{:eof, 1, 43}
]
assert EEx.tokenize('<% if true do %><%# comment %>bar<% end %>', @opts) == {:ok, exprs}
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @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}}
{:start_expr, 1, 1, [], ' case true do '},
{:middle_expr, 1, 33, '', ' true -> '},
{:text, 1, 46, 'bar'},
{:end_expr, 1, 49, [], ' end '},
{:eof, 1, 58}
]
assert EEx.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', @opts) ==
assert T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "EEx multi-line comments" 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}}
]
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}}
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
{:eof, 1, 35}
]
assert EEx.tokenize('foo <% true # this is a boolean %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @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}}
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 1, 27, 'text'},
{:end_expr, 1, 31, [], ' end # closeif '},
{:eof, 1, 50}
]
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
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 ->\n'},
{:text, 2, 3, 'foo'},
{:end_expr, 2, 6, [], ' end '},
{:eof, 2, 15}
]
assert EEx.tokenize('<%= a fn ->\n%>foo<% end %>', @opts) ==
assert T.tokenize('<%= a fn ->\n%>foo<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with multiple fn-end" 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, fn -> '},
{:text, 1, 34, 'bar'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
assert EEx.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', @opts) ==
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1, 1, @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}}
{: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 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 +240,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 +294,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 -10
View File
@@ -207,15 +207,6 @@ defmodule EExTest do
)
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
@@ -317,7 +308,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
+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())
+5 -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}
_ ->
@@ -829,118 +826,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
+56 -103
View File
@@ -51,23 +51,13 @@ defmodule Application do
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 +75,37 @@ 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")
require Application
@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 +168,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 +273,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 """
@@ -525,6 +504,8 @@ defmodule Application do
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
# compile time instead of compile_env
@doc since: "1.10.0"
@spec compile_env(app, key | list, value) :: value
defmacro compile_env(app, key_or_path, default \\ nil) do
@@ -532,27 +513,24 @@ defmodule Application 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
@@ -572,28 +550,15 @@ defmodule Application 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 +570,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 +596,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
@@ -688,14 +653,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 +669,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 +777,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 """
+661 -584
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 -13
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
@@ -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
@@ -791,14 +791,7 @@ defmodule Calendar do
# Year
defp format_modifiers("Y" <> rest, width, pad, datetime, format_options, acc) do
{sign, year} =
if datetime.year < 0 do
{?-, -datetime.year}
else
{[], datetime.year}
end
result = [sign | year |> Integer.to_string() |> pad_leading(width, pad)]
result = datetime.year |> Integer.to_string() |> pad_leading(width, pad)
parse(rest, datetime, format_options, [result | acc])
end
+8 -17
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])
@@ -997,8 +992,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 +1011,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 -6
View File
@@ -16,12 +16,12 @@ 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]
@@ -75,7 +75,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 +211,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
+31 -148
View File
@@ -13,13 +13,7 @@ defmodule DateTime do
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,22 +28,20 @@ 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.
@@ -143,9 +135,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()
@@ -817,6 +806,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 +854,6 @@ defmodule DateTime do
"""
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
def to_naive(datetime)
def to_naive(%{
calendar: calendar,
year: year,
@@ -902,8 +893,6 @@ defmodule DateTime do
"""
@spec to_date(Calendar.datetime()) :: Date.t()
def to_date(datetime)
def to_date(%{
year: year,
month: month,
@@ -934,8 +923,6 @@ defmodule DateTime do
"""
@spec to_time(Calendar.datetime()) :: Time.t()
def to_time(datetime)
def to_time(%{
year: _,
month: _,
@@ -1119,10 +1106,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 +1119,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 +1344,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 +1363,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 +1383,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,33 +1402,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>
This operation merges the precision of the naive date time with the given unit:
iex> result = DateTime.add(~U[2014-10-02 00:29:10Z], 21, :millisecond)
~U[2014-10-02 00:29:10.021Z]
iex> result.microsecond
{21000, 3}
"""
@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,
@@ -1515,20 +1418,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,
@@ -1536,15 +1426,8 @@ 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)
precision = max(Calendar.ISO.time_unit_to_precision(unit), precision)
result =
datetime
-11
View File
@@ -247,17 +247,6 @@ defmodule Calendar.ISO do
defguardp is_utc_offset(offset) when is_integer(offset)
defguardp is_std_offset(offset) when is_integer(offset)
@doc """
Converts a `t:System.time_unit/0` to precision.
Integer-based time units always get maximum precision.
"""
def time_unit_to_precision(:nanosecond), do: 6
def time_unit_to_precision(:microsecond), do: 6
def time_unit_to_precision(:millisecond), do: 3
def time_unit_to_precision(:second), do: 0
def time_unit_to_precision(_), do: 6
@doc """
Parses a time `string` in the `:extended` format.
+25 -96
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,17 +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 considers 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]
@@ -375,28 +363,18 @@ 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.000]
~N[2014-10-02 00:29:12]
As well as days/hours/minutes:
# 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]
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 merges the precision of the naive date time with the given unit:
iex> result = NaiveDateTime.add(~N[2014-10-02 00:29:10], 21, :millisecond)
~N[2014-10-02 00:29:10.021]
iex> result.microsecond
{21000, 3}
Operations on top of gregorian seconds or the Unix epoch are optimized:
# 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}
# from Gregorian seconds
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
@@ -413,29 +391,14 @@ 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)
precision = max(Calendar.ISO.time_unit_to_precision(unit), precision)
naive_datetime
|> to_iso_days()
@@ -446,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
@@ -458,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,
+26 -83
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,64 +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]
This operation merges the precision of the time with the given unit:
iex> result = Time.add(~T[00:29:10], 21, :millisecond)
~T[00:29:10.021]
iex> result.microsecond
{21000, 3}
~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)
precision = max(Calendar.ISO.time_unit_to_precision(unit), precision)
{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
@@ -694,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
@@ -720,38 +676,25 @@ 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,
hour: hour1,
minute: minute1,
second: second1,
microsecond: {microsecond1, _}
microsecond: {microsecond1, @parts_per_day}
},
%{
calendar: Calendar.ISO,
hour: hour2,
minute: minute2,
second: second2,
microsecond: {microsecond2, _}
microsecond: {microsecond2, @parts_per_day}
},
unit
) do
+81 -174
View File
@@ -3,9 +3,8 @@ defmodule Code do
Utilities for managing code compilation, code evaluation, and code loading.
This module complements Erlang's [`:code` module](`:code`)
to add behaviour which is specific to Elixir. For functions to
manipulate Elixir's AST (rather than evaluating it), see the
`Macro` module.
to add behaviour which is specific to Elixir. Almost all of the functions in this module
have global side effects on the behaviour of Elixir.
## Working with files
@@ -100,15 +99,10 @@ defmodule Code do
following events are available to tracers:
* `:start` - (since v1.11.0) invoked whenever the compiler starts to trace
a new lexical context. A lexical context is started when compiling a new
file or when defining a module within a function. Note evaluated code
does not start a new lexical context (because they don't track unused
aliases, imports, etc) but defining a module inside evaluated code will.
Note this event may be emitted in parallel, where multiple files/modules
invoke `:start` and run at the same time. The value of the `lexical_tracker`
of the macro environment, albeit opaque, can be used to uniquely identify
the environment.
a new lexical context, such as a new file. Keep in mind the compiler runs
in parallel, so multiple files may invoke `:start` and run at the same
time. The value of the `lexical_tracker` of the macro environment, albeit
opaque, can be used to uniquely identify the environment.
* `:stop` - (since v1.11.0) invoked whenever the compiler stops tracing a
new lexical context, such as a new file.
@@ -135,8 +129,6 @@ defmodule Code do
* `{:require, meta, module, opts}` - traced whenever `module` is required.
`meta` is the require AST metadata and `opts` are the require options.
If the `meta` option contains the `:from_macro`, then `require` was called
from within a macro and therefore must be treated as a compile-time dependency.
* `{:struct_expansion, meta, module, keys}` - traced whenever `module`'s struct
is expanded. `meta` is the struct AST metadata and `keys` are the keys being
@@ -157,13 +149,11 @@ defmodule Code do
of keys to traverse in the application environment and `return` is either
`{:ok, value}` or `:error`.
* `{:on_module, bytecode, _ignore}` - (since v1.11.0) traced whenever a module
* `{:on_module, bytecode, :none}` - (since v1.11.0) traced whenever a module
is defined. This is equivalent to the `@after_compile` callback and invoked
after any `@after_compile` in the given module. The third element is currently
`:none` but it may provide more metadata in the future. It is best to ignore
it at the moment. Note that `Module` functions expecting not yet compiled modules
(such as `Module.definitions_in/1`) are still available at the time this event
is emitted.
it at the moment.
The `:tracers` compiler option can be combined with the `:parser_options`
compiler option to enrich the metadata of the traced events above.
@@ -186,7 +176,7 @@ defmodule Code do
"""
@typedoc """
A list with all variables and their values.
A list with all variable bindings.
The binding keys are usually atoms, but they may be a tuple for variables
defined in a different context.
@@ -196,7 +186,6 @@ defmodule Code do
@boolean_compiler_options [
:docs,
:debug_info,
:ignore_already_consolidated,
:ignore_module_conflict,
:relative_paths,
:warnings_as_errors
@@ -241,8 +230,6 @@ defmodule Code do
calling this function only removes them from the list,
allowing them to be required again.
The list of files is managed per Erlang VM node.
## Examples
# Require EEx test code
@@ -272,8 +259,7 @@ defmodule Code do
Appends a path to the end of the Erlang VM code path list.
This is the list of directories the Erlang VM uses for
finding module code. The list of files is managed per Erlang
VM node.
finding module code.
The path is expanded with `Path.expand/1` before being appended.
If this path does not exist, an error is returned.
@@ -296,7 +282,7 @@ defmodule Code do
Prepends a path to the beginning of the Erlang VM code path list.
This is the list of directories the Erlang VM uses for finding
module code. The list of files is managed per Erlang VM node.
module code.
The path is expanded with `Path.expand/1` before being prepended.
If this path does not exist, an error is returned.
@@ -316,10 +302,8 @@ defmodule Code do
end
@doc """
Deletes a path from the Erlang VM code path list.
This is the list of directories the Erlang VM uses for finding
module code. The list of files is managed per Erlang VM node.
Deletes a path from the Erlang VM code path list. This is the list of
directories the Erlang VM uses for finding module code.
The path is expanded with `Path.expand/1` before being deleted. If the
path does not exist, this function returns `false`.
@@ -336,20 +320,13 @@ defmodule Code do
"""
@spec delete_path(Path.t()) :: boolean
def delete_path(path) do
case :code.del_path(to_charlist(Path.expand(path))) do
result when is_boolean(result) ->
result
{:error, :bad_name} ->
raise ArgumentError,
"invalid argument #{inspect(path)}"
end
:code.del_path(to_charlist(Path.expand(path)))
end
@doc """
Evaluates the contents given by `string`.
The `binding` argument is a list of all variables and their values.
The `binding` argument is a list of variable bindings.
The `opts` argument is a keyword list of environment options.
**Warning**: `string` can be any Elixir code and will be executed with
@@ -367,15 +344,17 @@ defmodule Code do
* `:line` - the line on which the script starts
Additionally, you may also pass an environment as second argument,
so the evaluation happens within that environment.
so the evaluation happens within that environment. However, if the evaluated
code requires or compiles another file, the environment given to this function
will not apply to said files.
Returns a tuple of the form `{value, binding}`, where `value` is the value
returned from evaluating `string`. If an error occurs while evaluating
`string`, an exception will be raised.
`string` an exception will be raised.
`binding` is a list with all variable names and their values after evaluating
`string`. The binding keys are usually atoms, but they may be a tuple for variables
defined in a different context. The names are in no particular order.
`binding` is a list with all variable bindings after evaluating `string`.
The binding keys are usually atoms, but they may be a tuple for variables
defined in a different context.
## Examples
@@ -420,18 +399,12 @@ defmodule Code do
end
defp validated_eval_string(string, binding, opts_or_env) do
%{line: line, file: file} = env = env_for_eval(opts_or_env)
%{line: line, file: file} = env = :elixir.env_for_eval(opts_or_env)
forms = :elixir.string_to_quoted!(to_charlist(string), line, 1, file, [])
{value, binding, _env} = eval_verify(:eval_forms, [forms, binding, env])
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
{value, binding}
end
defp eval_verify(fun, args) do
Module.ParallelChecker.verify(fn ->
apply(:elixir, fun, args)
end)
end
@doc ~S"""
Formats the given code `string`.
@@ -464,23 +437,12 @@ defmodule Code do
If you set it to `false` later on, `do`-`end` blocks won't be
converted back to keywords.
* `:normalize_bitstring_modifiers` (since v1.14.0) - when `true`,
removes unnecessary parentheses in known bitstring
[modifiers](`<<>>/1`), for example `<<foo::binary()>>`
becomes `<<foo::binary>>`, or adds parentheses for custom
modifiers, where `<<foo::custom_type>>` becomes `<<foo::custom_type()>>`.
Defaults to `true`. This option changes the AST.
## Design principles
The formatter was designed under three principles.
First, the formatter never changes the semantics of the code.
This means the input AST and the output AST are almost always equivalent.
The only cases where the formatter will change the AST is when the input AST
would cause *compiler warnings* and the output AST won't. The cases where
the formatter changes the AST can be disabled through formatting options
if desired.
First, the formatter never changes the semantics of the code by
default. This means the input AST and the output AST are equivalent.
The second principle is to provide as little configuration as possible.
This eases the formatter adoption by removing contention points while
@@ -769,13 +731,18 @@ defmodule Code do
unescape: false,
warn_on_unnecessary_quotes: false,
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
token_metadata: true,
emit_warnings: false
token_metadata: true
] ++ opts
{forms, comments} = string_to_quoted_with_comments!(string, to_quoted_opts)
to_algebra_opts = [comments: comments] ++ opts
to_algebra_opts =
[
comments: comments
] ++ opts
doc = Code.Formatter.to_algebra(forms, to_algebra_opts)
Inspect.Algebra.format(doc, line_length)
end
@@ -824,60 +791,16 @@ defmodule Code do
"""
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
def eval_quoted(quoted, binding \\ [], env_or_opts \\ []) do
{value, binding, _env} =
eval_verify(:eval_quoted, [quoted, binding, env_for_eval(env_or_opts)])
def eval_quoted(quoted, binding \\ [], opts \\ [])
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(env))
{value, binding}
end
@doc """
Returns an environment for evaluation.
It accepts either a `Macro.Env`, that is then pruned and prepared,
or a list of options. It returns an environment that is ready for
evaluation.
Most functions in this module will automatically prepare the given
environment for evaluation, so you don't need to explicitly call
this function, with the exception of `eval_quoted_with_env/3`,
which was designed precisely to be called in a loop, to implement
features such as interactive shells or anything else with multiple
evaluations.
## Options
If an env is not given, the options can be:
* `:file` - the file to be considered in the evaluation
* `:line` - the line on which the script starts
"""
@doc since: "1.14.0"
def env_for_eval(env_or_opts), do: :elixir.env_for_eval(env_or_opts)
@doc """
Evaluates the given `quoted` contents with `binding` and `env`.
This function is meant to be called in a loop, to implement features
such as interactive shells or anything else with multiple evaluations.
Therefore, the first time you call this function, you must compute
the initial environment with `env_for_eval/1`. The remaining calls
must pass the environment that was returned by this function.
## Options
* `:prune_binding` - (since v1.14.2) prune binding to keep only
variables read or written by the evaluated code. Note that
variables used by modules are always pruned, even if later used
by the modules. You can submit to the `:on_module` tracer event
and access the variables used by the module from its environment.
"""
@doc since: "1.14.0"
def eval_quoted_with_env(quoted, binding, %Macro.Env{} = env, opts \\ [])
when is_list(binding) do
eval_verify(:eval_quoted, [quoted, binding, env, opts])
def eval_quoted(quoted, binding, opts) when is_list(opts) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(opts))
{value, binding}
end
@doc ~S"""
@@ -1196,19 +1119,19 @@ defmodule Code do
"""
@spec eval_file(binary, nil | binary) :: {term, binding}
def eval_file(file, relative_to \\ nil) when is_binary(file) do
{charlist, file} = find_file!(file, relative_to)
eval_string(charlist, [], file: file, line: 1)
file = find_file(file, relative_to)
eval_string(File.read!(file), [], file: file, line: 1)
end
@deprecated "Use Code.require_file/2 or Code.compile_file/2 instead"
@doc false
def load_file(file, relative_to \\ nil) when is_binary(file) do
{charlist, file} = find_file!(file, relative_to)
file = find_file(file, relative_to)
:elixir_code_server.call({:acquire, file})
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
:elixir_compiler.file(file, fn _, _ -> :ok end)
end)
:elixir_code_server.cast({:required, file})
@@ -1227,7 +1150,7 @@ defmodule Code do
ones will block until the file is available. This means that if `require_file/2`
is called more than once with a given file, that file will be compiled only once.
The first process to call `require_file/2` will get the list of loaded modules,
others will get `nil`. The list of required files is managed per Erlang VM node.
others will get `nil`.
See `compile_file/2` if you would like to compile a file without tracking its
filenames. Finally, if you would like to get the result of evaluating a file rather
@@ -1249,7 +1172,7 @@ defmodule Code do
"""
@spec require_file(binary, nil | binary) :: [{module, binary}] | nil
def require_file(file, relative_to \\ nil) when is_binary(file) do
{charlist, file} = find_file!(file, relative_to)
file = find_file(file, relative_to)
case :elixir_code_server.call({:acquire, file}) do
:required ->
@@ -1258,7 +1181,7 @@ defmodule Code do
:proceed ->
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
:elixir_compiler.file(file, fn _, _ -> :ok end)
end)
:elixir_code_server.cast({:required, file})
@@ -1288,10 +1211,8 @@ defmodule Code do
@doc """
Stores all given compilation options.
Changing the compilation options affect all processes
running in a given Erlang VM node. To store individual
options and for a description of all options, see
`put_compiler_option/2`.
To store individual options and for a description of all
options, see `put_compiler_option/2`.
## Examples
@@ -1344,31 +1265,22 @@ defmodule Code do
@doc """
Stores a compilation option.
Changing the compilation options affect all processes running in a
given Erlang VM node.
These options are global since they are stored by Elixir's code server.
Available options are:
* `:docs` - when `true`, retains documentation in the compiled module.
* `:docs` - when `true`, retain documentation in the compiled module.
Defaults to `true`.
* `:debug_info` - when `true`, retains debug information in the compiled
module. This enables static analysis tools as it allows developers to
partially reconstruct the original source code. Therefore, disabling
`:debug_info` is not recommended as it removes the ability of the
Elixir compiler and other tools to provide feedback. If you want to
remove the `:debug_info` while deploying, tools like `mix release`
already do such by default.
* `:debug_info` - when `true`, retain debug information in the compiled
module. This allows a developer to reconstruct the original source
code. Defaults to `true`.
* `:ignore_already_consolidated` - when `true`, does not warn when a protocol
has already been consolidated and a new implementation is added. Defaults
to `false`.
* `:ignore_module_conflict` - when `true`, override modules that were
already defined without raising errors. Defaults to `false`.
* `:ignore_module_conflict` - when `true`, does not warn when a module has
already been defined. Defaults to `false`.
* `:relative_paths` - when `true`, uses relative paths in quoted nodes,
warnings, and errors generated by the compiler. Note disabling this option
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
warnings and errors generated by the compiler. Note disabling this option
won't affect runtime warnings and errors. Defaults to `true`.
* `:warnings_as_errors` - causes compilation to fail when warnings are
@@ -1453,9 +1365,6 @@ defmodule Code do
old compiler module names to be reused. If there are any processes running
any code from such modules, they will be terminated too.
This function is only meant to be called if you have a long running node
that is constantly evaluating code.
It returns `{:ok, number_of_modules_purged}`.
"""
@doc since: "1.7.0"
@@ -1480,9 +1389,8 @@ defmodule Code do
"""
@spec compile_string(List.Chars.t(), binary) :: [{module, binary}]
def compile_string(string, file \\ "nofile") when is_binary(file) do
Module.ParallelChecker.verify(fn ->
:elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
end)
loaded = :elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
end
@doc """
@@ -1495,9 +1403,8 @@ defmodule Code do
"""
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
Module.ParallelChecker.verify(fn ->
:elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
end)
loaded = :elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
end
@doc """
@@ -1518,8 +1425,7 @@ defmodule Code do
@spec compile_file(binary, nil | binary) :: [{module, binary}]
def compile_file(file, relative_to \\ nil) when is_binary(file) do
Module.ParallelChecker.verify(fn ->
{charlist, file} = find_file!(file, relative_to)
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
:elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
end)
end
@@ -1725,8 +1631,8 @@ defmodule Code do
def fetch_docs(module_or_path)
def fetch_docs(module) when is_atom(module) do
case get_beam_and_path(module) do
{bin, beam_path} ->
case :code.get_object_code(module) do
{_module, bin, beam_path} ->
case fetch_docs_from_beam(bin) do
{:error, :chunk_not_found} ->
app_root = Path.expand(Path.join(["..", ".."]), beam_path)
@@ -1740,7 +1646,7 @@ defmodule Code do
:error ->
case :code.which(module) do
:preloaded ->
# The ERTS directory is not necessarily included in releases
# The erts directory is not necessarily included in releases
# unless it is listed as an extra application.
case :code.lib_dir(:erts) do
path when is_list(path) ->
@@ -1761,15 +1667,6 @@ defmodule Code do
fetch_docs_from_beam(String.to_charlist(path))
end
defp get_beam_and_path(module) do
with {^module, beam, filename} <- :code.get_object_code(module),
{:ok, ^module} <- beam |> :beam_lib.info() |> Keyword.fetch(:module) do
{beam, filename}
else
_ -> :error
end
end
@docs_chunk 'Docs'
defp fetch_docs_from_beam(bin_or_path) do
@@ -1802,8 +1699,17 @@ defmodule Code do
{:error, {:invalid_chunk, bin}}
end
@doc false
@doc ~S"""
Deprecated function to retrieve old documentation format.
Elixir v1.7 adopts [EEP 48](https://www.erlang.org/eeps/eep-0048.html)
which is a new documentation format meant to be shared across all
BEAM languages. The old format, used by `Code.get_docs/2`, is no
longer available, and therefore this function always returns `nil`.
Use `Code.fetch_docs/1` instead.
"""
@deprecated "Code.get_docs/2 always returns nil as its outdated documentation is no longer stored on BEAM files. Use Code.fetch_docs/1 instead"
@spec get_docs(module, :moduledoc | :docs | :callback_docs | :type_docs | :all) :: nil
def get_docs(_module, _kind) do
nil
end
@@ -1813,7 +1719,7 @@ defmodule Code do
# Finds the file given the relative_to path.
#
# If the file is found, returns its path in binary, fails otherwise.
defp find_file!(file, relative_to) do
defp find_file(file, relative_to) do
file =
if relative_to do
Path.expand(file, relative_to)
@@ -1821,9 +1727,10 @@ defmodule Code do
Path.expand(file)
end
case File.read(file) do
{:ok, bin} -> {String.to_charlist(bin), file}
{:error, reason} -> raise Code.LoadError, file: file, reason: reason
if File.regular?(file) do
file
else
raise Code.LoadError, file: file
end
end
end
+57 -123
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,24 +141,6 @@ 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.
"""
@@ -197,7 +179,6 @@ defmodule Code.Formatter do
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} ->
@@ -220,8 +201,7 @@ defmodule Code.Formatter do
skip_eol: false,
comments: comments,
sigils: sigils,
file: file,
normalize_bitstring_modifiers: normalize_bitstring_modifiers
file: file
}
end
@@ -399,8 +379,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 +445,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 +489,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}
@@ -894,7 +867,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 +912,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 +957,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
@@ -1297,7 +1270,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 +1286,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 +1294,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
@@ -1411,7 +1396,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 +1415,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 +1492,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()
@@ -1583,10 +1538,6 @@ defmodule Code.Formatter do
insert_underscores(int_part) <> "." <> decimal_part
end
defp insert_underscores("-" <> digits) do
"-" <> insert_underscores(digits)
end
defp insert_underscores(digits) do
cond do
digits =~ "_" ->
@@ -1706,15 +1657,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()
@@ -1743,16 +1691,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()
@@ -1794,14 +1738,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()
@@ -1822,21 +1765,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
@@ -2118,7 +2052,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
+97 -307
View File
@@ -42,19 +42,12 @@ defmodule Code.Fragment do
* `{:alias, charlist}` - the context is an alias, potentially
a nested one, such as `Hello.Wor` or `HelloWor`
* `{:alias, inside_alias, charlist}` - the context is an alias, potentially
a nested one, where `inside_alias` is an expression `{:module_attribute, charlist}`
or `{:local_or_var, charlist}` and `charlist` is a static part
Examples are `__MODULE__.Submodule` or `@hello.Submodule`
* `{:dot, inside_dot, charlist}` - the context is a dot
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, this may either be a remote call or a map
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`. If `charlist`
is empty and `inside_dot` is an alias, then the autocompletion may either
be an alias or a remote call.
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
@@ -102,10 +95,7 @@ defmodule Code.Fragment do
of a sigil, such as `~` or `~s`, or an operator starting with `~`, such as
`~>` and `~>>`
* `{:struct, inside_struct}` - the context is a struct, such as `%`, `%UR` or `%URI`.
`inside_struct` can either be a `charlist` in case of a static alias or an
expression `{:alias, inside_alias, charlist}`, `{:module_attribute, charlist}`,
`{:local_or_var, charlist}`, `{:dot, inside_dot, charlist}`
* `{:struct, charlist}` - the context is a struct, such as `%`, `%UR` or `%URI`
* `{:unquoted_atom, charlist}` - the context is an unquoted atom. This
can be any atom or an atom representing a module
@@ -119,7 +109,6 @@ defmodule Code.Fragment do
@doc since: "1.13.0"
@spec cursor_context(List.Chars.t(), keyword()) ::
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:dot_arity, inside_dot, charlist}
| {:dot_call, inside_dot, charlist}
@@ -133,30 +122,42 @@ defmodule Code.Fragment do
| {:operator_call, charlist}
| :none
| {:sigil, charlist}
| {:struct, inside_struct}
| {:struct, charlist}
| {:unquoted_atom, charlist}
when inside_dot:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist},
inside_alias:
{:local_or_var, charlist}
| {:module_attribute, charlist},
inside_struct:
charlist
| {:alias, inside_alias, charlist}
| {:local_or_var, charlist}
| {:module_attribute, charlist}
| {:dot, inside_dot, charlist}
| {:var, charlist}
def cursor_context(fragment, opts \\ [])
def cursor_context(fragment, opts)
when (is_binary(fragment) or is_list(fragment)) and is_list(opts) do
fragment
|> last_line()
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
binary =
case :binary.matches(binary, "\n") do
[] ->
binary
matches ->
{position, _} = List.last(matches)
binary_part(binary, position + 1, byte_size(binary) - position - 1)
end
binary
|> String.to_charlist()
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
end
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
charlist =
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
[?\n | _] -> []
rest -> rest
end
charlist
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
@@ -177,7 +178,7 @@ defmodule Code.Fragment do
@operators ++ @starter_punctuation ++ @non_starter_punctuation ++ @space
@textual_operators ~w(when not and or in)c
@keywords ~w(do end after else catch rescue fn true false nil)c
@incomplete_operators ~w(^^ ~~ ~)c
defp codepoint_cursor_context(reverse, _opts) do
{stripped, spaces} = strip_spaces(reverse, 0)
@@ -249,9 +250,6 @@ defmodule Code.Fragment do
:operator ->
operator(reverse, count, [], call_op?)
{:struct, {:module_attribute, acc}, count} ->
{{:struct, {:module_attribute, acc}}, count + 1}
{:module_attribute, acc, count} ->
{{:module_attribute, acc}, count}
@@ -276,12 +274,6 @@ defmodule Code.Fragment do
{:identifier, _, acc, count} when call_op? and acc in @textual_operators ->
{{:operator, acc}, count}
{:identifier, [?%], acc, count} ->
case identifier_to_cursor_context(acc |> Enum.reverse(), count, true) do
{{:local_or_var, _} = idenifier, _} -> {{:struct, idenifier}, count + 1}
_ -> {:none, 0}
end
{:identifier, rest, acc, count} ->
case strip_spaces(rest, count) do
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
@@ -308,7 +300,6 @@ defmodule Code.Fragment do
defp rest_identifier(rest, count, [?@ | acc]) do
case tokenize_identifier(rest, count, acc) do
{:identifier, [?% | _rest], acc, count} -> {:struct, {:module_attribute, acc}, count}
{:identifier, _rest, acc, count} -> {:module_attribute, acc, count}
:none when acc == [] -> {:module_attribute, '', count}
_ -> :none
@@ -347,7 +338,7 @@ defmodule Code.Fragment do
:none
{kind, _, [], _, _, extra} ->
if :at in extra do
if ?@ in extra do
:none
else
{kind, rest, acc, count}
@@ -362,29 +353,9 @@ defmodule Code.Fragment do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:struct, prev}, count} when is_list(prev) ->
{{:struct, prev ++ '.' ++ acc}, count}
{{:struct, {:alias, parent, prev}}, count} ->
{{:struct, {:alias, parent, prev ++ '.' ++ acc}}, count}
{{:struct, prev}, count} ->
{{:struct, {:alias, prev, acc}}, count}
{{:alias, prev}, count} ->
{{:alias, prev ++ '.' ++ acc}, count}
{{:alias, parent, prev}, count} ->
{{:alias, parent, prev ++ '.' ++ acc}, count}
{{:local_or_var, prev}, count} ->
{{:alias, {:local_or_var, prev}, acc}, count}
{{:module_attribute, prev}, count} ->
{{:alias, {:module_attribute, prev}, acc}, count}
_ ->
{:none, 0}
{{:struct, prev}, count} -> {{:struct, prev ++ '.' ++ acc}, count}
{{:alias, prev}, count} -> {{:alias, prev ++ '.' ++ acc}, count}
_ -> {:none, 0}
end
end
@@ -392,32 +363,13 @@ defmodule Code.Fragment do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:local_or_var, var}, count} ->
{{:dot, {:var, var}, acc}, count}
{{:unquoted_atom, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:alias, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:alias, _, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:struct, inner}, count} when is_list(inner) ->
{{:struct, {:dot, {:alias, inner}, acc}}, count}
{{:struct, inner}, count} ->
{{:struct, {:dot, inner, acc}}, count}
{{:dot, _, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:module_attribute, _} = prev, count} ->
{{:dot, prev, acc}, count}
{_, _} ->
{:none, 0}
{{:local_or_var, var}, count} -> {{:dot, {:var, var}, acc}, count}
{{:unquoted_atom, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:alias, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:dot, _, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:module_attribute, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:struct, acc}, count} -> {{:struct, acc ++ '.'}, count}
{_, _} -> {:none, 0}
end
end
@@ -425,6 +377,24 @@ defmodule Code.Fragment do
operator(rest, count + 1, [h | acc], call_op?)
end
defp operator(rest, count, acc, call_op?) when acc in @incomplete_operators do
{rest, dot_count} = strip_spaces(rest, count)
cond do
call_op? ->
{:none, 0}
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
dot(tl(rest), dot_count + 1, acc)
acc == '~' ->
{{:sigil, ''}, count}
true ->
{{:operator, acc}, count}
end
end
# If we are opening a sigil, ignore the operator.
defp operator([letter, ?~ | rest], _count, [op], _call_op?)
when op in '<|/' and (letter in ?A..?Z or letter in ?a..?z) and
@@ -432,16 +402,6 @@ defmodule Code.Fragment do
{:none, 0}
end
defp operator(rest, count, '~', call_op?) do
{rest, _} = strip_spaces(rest, count)
if call_op? or match?([?. | rest] when rest == [] or hd(rest) != ?., rest) do
{:none, 0}
else
{{:sigil, ''}, count}
end
end
defp operator(rest, count, acc, _call_op?) do
case :elixir_tokenizer.tokenize(acc, 1, 1, []) do
{:ok, _, _, _, [{:atom, _, _}]} ->
@@ -529,60 +489,40 @@ defmodule Code.Fragment do
* This function never returns empty sigils `{:sigil, ''}` or empty structs
`{:struct, ''}` as context
* This function returns keywords as `{:keyword, 'do'}`
* This function never returns `:expr`
"""
@doc since: "1.13.0"
@spec surround_context(List.Chars.t(), position(), keyword()) ::
%{begin: position, end: position, context: context} | :none
when context:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:local_or_var, charlist}
| {:local_arity, charlist}
| {:local_call, charlist}
| {:module_attribute, charlist}
| {:operator, charlist}
| {:sigil, charlist}
| {:struct, inside_struct}
| {:unquoted_atom, charlist}
| {:keyword, charlist},
| {:unquoted_atom, charlist},
inside_dot:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist},
inside_alias:
{:local_or_var, charlist}
| {:module_attribute, charlist},
inside_struct:
charlist
| {:alias, inside_alias, charlist}
| {:local_or_var, charlist}
| {:module_attribute, charlist}
| {:dot, inside_dot, charlist}
| {:var, charlist}
def surround_context(fragment, position, options \\ [])
def surround_context(string, {line, column}, opts)
when (is_binary(string) or is_list(string)) and is_list(opts) do
{charlist, lines_before_lengths, lines_current_and_after_lengths} =
surround_line(string, line, column)
prepended_columns = Enum.sum(lines_before_lengths)
def surround_context(binary, {line, column}, opts) when is_binary(binary) do
binary
|> String.split("\n")
|> Enum.at(line - 1, '')
|> String.to_charlist()
|> position_surround_context(line, column, opts)
end
def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
charlist
|> position_surround_context(line, column + prepended_columns, opts)
|> to_multiline_range(
prepended_columns,
lines_before_lengths,
lines_current_and_after_lengths
)
|> :string.split('\n', :all)
|> Enum.at(line - 1, '')
|> position_surround_context(line, column, opts)
end
def surround_context(other, {_, _} = position, opts) do
@@ -609,9 +549,6 @@ defmodule Code.Fragment do
{{:alias, acc}, offset} ->
build_surround({:alias, acc}, reversed, line, offset)
{{:alias, parent, acc}, offset} ->
build_surround({:alias, parent, acc}, reversed, line, offset)
{{:dot, _, [_ | _]} = dot, offset} ->
build_surround(dot, reversed, line, offset)
@@ -624,10 +561,7 @@ defmodule Code.Fragment do
{{:local_or_var, acc}, offset} when acc in @textual_operators ->
build_surround({:operator, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} when acc in @keywords ->
build_surround({:keyword, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} ->
{{:local_or_var, acc}, offset} when acc not in ~w(do end after else catch rescue)c ->
build_surround({:local_or_var, acc}, reversed, line, offset)
{{:module_attribute, ''}, offset} ->
@@ -671,7 +605,7 @@ defmodule Code.Fragment do
reversed = reversed_post ++ reversed_pre
case codepoint_cursor_context(reversed, opts) do
{{:operator, acc}, offset} ->
{{:operator, acc}, offset} when acc not in @incomplete_operators ->
build_surround({:operator, acc}, reversed, line, offset)
{{:sigil, ''}, offset} when hd(rest) in ?A..?Z or hd(rest) in ?a..?z ->
@@ -786,156 +720,10 @@ defmodule Code.Fragment do
defp enum_reverse_at([h | t], n, acc) when n > 0, do: enum_reverse_at(t, n - 1, [h | acc])
defp enum_reverse_at(rest, _, acc), do: {acc, rest}
defp last_line(binary) when is_binary(binary) do
[last_line | lines_reverse] =
binary
|> String.split(["\r\n", "\n"])
|> Enum.reverse()
prepend_cursor_lines(lines_reverse, String.to_charlist(last_line))
end
defp last_line(charlist) when is_list(charlist) do
[last_line | lines_reverse] =
charlist
|> :string.replace('\r\n', '\n', :all)
|> :string.join('')
|> :string.split('\n', :all)
|> Enum.reverse()
prepend_cursor_lines(lines_reverse, last_line)
end
defp prepend_cursor_lines(lines, last_line) do
with [line | lines] <- lines,
{trimmed_line, incomplete?} = ends_as_incomplete(to_charlist(line), [], true),
true <- incomplete? or starts_with_dot?(last_line) do
prepend_cursor_lines(lines, Enum.reverse(trimmed_line, last_line))
else
_ -> last_line
end
end
defp starts_with_dot?([?. | _]), do: true
defp starts_with_dot?([h | t]) when h in @space, do: starts_with_dot?(t)
defp starts_with_dot?(_), do: false
defp ends_as_incomplete([?# | _], acc, incomplete?),
do: {acc, incomplete?}
defp ends_as_incomplete([h | t], acc, _incomplete?) when h in [?(, ?.],
do: ends_as_incomplete(t, [h | acc], true)
defp ends_as_incomplete([h | t], acc, incomplete?) when h in @space,
do: ends_as_incomplete(t, [h | acc], incomplete?)
defp ends_as_incomplete([h | t], acc, _incomplete?),
do: ends_as_incomplete(t, [h | acc], false)
defp ends_as_incomplete([], acc, incomplete?),
do: {acc, incomplete?}
defp surround_line(binary, line, column) when is_binary(binary) do
binary
|> String.split(["\r\n", "\n"])
|> Enum.map(&String.to_charlist/1)
|> surround_lines(line, column)
end
defp surround_line(charlist, line, column) when is_list(charlist) do
charlist
|> :string.replace('\r\n', '\n', :all)
|> :string.join('')
|> :string.split('\n', :all)
|> surround_lines(line, column)
end
defp surround_lines(lines, line, column) do
{lines_before_reverse, cursor_line, lines_after} = split_at(lines, line, [])
{trimmed_cursor_line, incomplete?} = ends_as_incomplete(to_charlist(cursor_line), [], true)
reversed_cursor_line =
if column - 1 > length(trimmed_cursor_line) do
# Don't strip comments if cursor is inside a comment
Enum.reverse(cursor_line)
else
trimmed_cursor_line
end
{cursor_line, after_lengths} =
append_surround_lines(lines_after, [], [reversed_cursor_line], incomplete?)
{cursor_line, before_lengths} = prepend_surround_lines(lines_before_reverse, [], cursor_line)
{cursor_line, before_lengths, [length(reversed_cursor_line) | after_lengths]}
end
defp split_at([line], _, acc), do: {acc, line, []}
defp split_at([line | lines], 1, acc), do: {acc, line, lines}
defp split_at([line | lines], count, acc), do: split_at(lines, count - 1, [line | acc])
defp prepend_surround_lines(lines, lengths, last_line) do
with [line | lines] <- lines,
{trimmed_line, incomplete?} = ends_as_incomplete(to_charlist(line), [], true),
true <- incomplete? or starts_with_dot?(last_line) do
lengths = [length(trimmed_line) | lengths]
prepend_surround_lines(lines, lengths, Enum.reverse(trimmed_line, last_line))
else
_ -> {last_line, Enum.reverse(lengths)}
end
end
defp append_surround_lines(lines, lengths, acc_lines, incomplete?) do
with [line | lines] <- lines,
line = to_charlist(line),
true <- incomplete? or starts_with_dot?(line) do
{trimmed_line, incomplete?} = ends_as_incomplete(line, [], true)
lengths = [length(trimmed_line) | lengths]
append_surround_lines(lines, lengths, [trimmed_line | acc_lines], incomplete?)
else
_ -> {Enum.reduce(acc_lines, [], &Enum.reverse/2), Enum.reverse(lengths)}
end
end
defp to_multiline_range(:none, _, _, _), do: :none
defp to_multiline_range(
%{begin: {begin_line, begin_column}, end: {end_line, end_column}} = context,
prepended,
lines_before_lengths,
lines_current_and_after_lengths
) do
{begin_line, begin_column} =
Enum.reduce_while(lines_before_lengths, {begin_line, begin_column - prepended}, fn
line_length, {acc_line, acc_column} ->
if acc_column < 1 do
{:cont, {acc_line - 1, acc_column + line_length}}
else
{:halt, {acc_line, acc_column}}
end
end)
{end_line, end_column} =
Enum.reduce_while(lines_current_and_after_lengths, {end_line, end_column - prepended}, fn
line_length, {acc_line, acc_column} ->
if acc_column > line_length + 1 do
{:cont, {acc_line + 1, acc_column - line_length}}
else
{:halt, {acc_line, acc_column}}
end
end)
%{context | begin: {begin_line, begin_column}, end: {end_line, end_column}}
end
@doc """
Receives a string and returns a quoted expression
Receives a code fragment and returns a quoted expression
with a cursor at the nearest argument position.
This function receives a string with an Elixir code fragment,
representing a cursor position, and converts such string to
AST with the inclusion of special `__cursor__()` node based
on the position of the cursor with a container.
A container is any Elixir expression starting with `(`,
`{`, and `[`. This includes function calls, tuples, lists,
maps, and so on. For example, take this code, which would
@@ -977,7 +765,7 @@ defmodule Code.Fragment do
max(some_value, 1 + another_val
max(some_value, 1 |> some_fun() |> another_fun
On the other hand, tuples, lists, maps, and binaries all retain the
On the other hand, tuples, lists, maps, etc all retain the
cursor position:
max(some_value, [1, 2,
@@ -1001,21 +789,9 @@ defmodule Code.Fragment do
## Examples
Function call:
iex> Code.Fragment.container_cursor_to_quoted("max(some_value, ")
{:ok, {:max, [line: 1], [{:some_value, [line: 1], nil}, {:__cursor__, [line: 1], []}]}}
Containers (for example, a list):
iex> Code.Fragment.container_cursor_to_quoted("[some, value")
{:ok, [{:some, [line: 1], nil}, {:__cursor__, [line: 1], []}]}
For binaries, the `::` is exclusively kept as an operator:
iex> Code.Fragment.container_cursor_to_quoted("<<some::integer")
{:ok, {:<<>>, [line: 1], [{:"::", [line: 1], [{:some, [line: 1], nil}, {:__cursor__, [line: 1], []}]}]}}
## Options
* `:file` - the filename to be reported in case of parsing errors.
@@ -1035,17 +811,31 @@ defmodule Code.Fragment do
tokens, for closing tokens, end of expressions, as well as delimiters
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
* `:literal_encoder` - a function to encode literals in the AST.
See the documentation for `Code.string_to_quoted/2` for more information.
"""
@doc since: "1.13.0"
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
def container_cursor_to_quoted(fragment, opts \\ []) do
opts =
Keyword.take(opts, [:file, :line, :column, :columns, :token_metadata, :literal_encoder])
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
column = Keyword.get(opts, :column, 1)
columns = Keyword.get(opts, :columns, false)
token_metadata = Keyword.get(opts, :token_metadata, false)
Code.string_to_quoted(fragment, [cursor_completion: true, emit_warnings: false] ++ opts)
fragment = to_charlist(fragment)
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
{:ok, _, _, _warnings, tokens} ->
:elixir.tokens_to_quoted(tokens, nil, columns: columns, token_metadata: token_metadata)
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, {to_string(prefix), to_string(suffix)}, to_string(token)}}
{:error, {line, column, error, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, to_string(error), to_string(token)}}
end
end
end
+148 -9
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,9 +41,9 @@ 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}
@@ -54,6 +54,149 @@ defmodule Code.Identifier do
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.identifier_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 +214,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
+29 -44
View File
@@ -70,21 +70,13 @@ defmodule Code.Normalizer do
# Atoms with interpolations
defp do_normalize(
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
[{:<<>>, _, parts} = string, :utf8]},
[{:<<>>, _, args} = string, :utf8]},
state
)
when is_list(parts) do
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 parts == [] or binary_interpolated?(parts) 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)
@@ -92,31 +84,27 @@ defmodule Code.Normalizer do
normalize_bitstring(string, state)
end
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, utf8]}
{{:., 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]} = quoted, state) do
if list_interpolated?(parts) do
parts =
Enum.map(parts, fn
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args} ->
args = normalize_args(args, state)
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_dot_meta, [Kernel, :to_string]}, part_call_meta, args}
part when is_binary(part) ->
if state.escape do
maybe_escape_literal(part, state)
else
part
end
end)
part ->
if state.escape do
maybe_escape_literal(part, state)
else
part
end
end)
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
else
normalize_call(quoted, state)
end
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
end
# Don't normalize the `Access` atom in access syntax
@@ -213,7 +201,7 @@ defmodule Code.Normalizer do
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
value =
cond do
keyword?(value) and value != [] ->
keyword?(value) ->
normalize_kw_args(value, state, true)
is_list(value) ->
@@ -261,7 +249,7 @@ defmodule Code.Normalizer 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
if is_atom(literal) and Code.Identifier.classify(literal) == :alias and
is_nil(meta[:delimiter]) do
segments =
case Atom.to_string(literal) do
@@ -298,7 +286,7 @@ defmodule Code.Normalizer do
# It's a charlist
list =
if state.escape do
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), nil)
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), -1)
IO.iodata_to_binary(string) |> to_charlist()
else
list
@@ -362,7 +350,7 @@ defmodule Code.Normalizer do
normalize_kw_blocks(form, meta, args, state)
allow_keyword?(form, arity) ->
args = normalize_args(args, %{state | parent_meta: meta})
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
{last_arg, leading_args} = List.pop_at(args, -1, [])
last_args =
@@ -384,7 +372,7 @@ defmodule Code.Normalizer do
{form, meta, leading_args ++ last_args}
true ->
args = normalize_args(args, %{state | parent_meta: meta})
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
{form, meta, args}
end
end
@@ -393,11 +381,11 @@ defmodule Code.Normalizer do
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}, state, escape_interpolation \\ false) do
defp normalize_bitstring({:<<>>, meta, parts} = quoted, state, escape_interpolation \\ false) do
meta = patch_meta_line(meta, state.parent_meta)
parts =
if binary_interpolated?(parts) do
if interpolated?(quoted) do
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
else
state = %{state | parent_meta: meta}
@@ -532,7 +520,7 @@ defmodule Code.Normalizer do
end
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
{string, _} = Code.Identifier.escape(string, nil)
{string, _} = Code.Identifier.escape(string, -1)
IO.iodata_to_binary(string)
end
@@ -547,7 +535,8 @@ defmodule Code.Normalizer do
term
end
defp binary_interpolated?(parts) do
# 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
@@ -555,12 +544,8 @@ defmodule Code.Normalizer do
end)
end
defp list_interpolated?(parts) do
Enum.all?(parts, fn
{{:., _, [Kernel, :to_string]}, _, [_]} -> true
binary when is_binary(binary) -> true
_ -> false
end)
defp interpolated?(_) do
false
end
defp patch_meta_line(meta, parent_meta) do
+3 -5
View File
@@ -174,11 +174,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
+2 -2
View File
@@ -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]>
"""
+12 -38
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
@@ -165,24 +145,16 @@ defmodule Config do
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 +293,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
+3 -2
View File
@@ -341,7 +341,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()
@@ -409,7 +410,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
+1 -1
View File
@@ -37,7 +37,7 @@ defmodule Config.Reader do
]
Remember Mix already loads `config/runtime.exs` by default.
For more examples and scenarios, see the `Config.Provider` module.
For more examples and scenarios, see the `Config.Providers` module.
"""
@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)
+159 -424
View File
@@ -37,23 +37,6 @@ defprotocol Enumerable do
than linear time.
"""
@typedoc """
An enumerable of elements of type `element`.
This type is equivalent to `t:t/0` but is especially useful for documentation.
For example, imagine you define a function that expects an enumerable of
integers and returns an enumerable of strings:
@spec integers_to_strings(Enumerable.t(integer())) :: Enumerable.t(String.t())
def integers_to_strings(integers) do
Stream.map(integers, &Integer.to_string/1)
end
"""
@typedoc since: "1.14.0"
@type t(_element) :: t()
@typedoc """
The accumulator value for each step.
@@ -122,24 +105,18 @@ defprotocol Enumerable do
@type continuation :: (acc -> result)
@typedoc """
A slicing function that receives the initial position,
the number of elements in the slice, and the step.
A slicing function that receives the initial position and the
number of elements in the slice.
The `start` position is a number `>= 0` and guaranteed to
exist in the `enumerable`. The length is a number `>= 1`
in a way that `start + length * step <= count`, where
`count` is the maximum amount of elements in the enumerable.
exist in the `enumerable`. The length is a number `>= 1` in a way
that `start + length <= count`, where `count` is the maximum
amount of elements in the enumerable.
The function should return a non empty list where
the amount of elements is equal to `length`.
"""
@type slicing_fun ::
(start :: non_neg_integer, length :: pos_integer, step :: pos_integer -> [term()])
@typedoc """
Receives an enumerable and returns a list.
"""
@type to_list_fun :: (t -> [term()])
@type slicing_fun :: (start :: non_neg_integer, length :: pos_integer -> [term()])
@doc """
Reduces the `enumerable` into an element.
@@ -169,7 +146,7 @@ defprotocol Enumerable do
Retrieves the number of elements in the `enumerable`.
It should return `{:ok, count}` if you can count the number of elements
in `enumerable` in a faster way than fully traversing it.
in `enumerable` without traversing it.
Otherwise it should return `{:error, __MODULE__}` and a default algorithm
built on top of `reduce/3` that runs in linear time will be used.
@@ -196,36 +173,28 @@ defprotocol Enumerable do
@doc """
Returns a function that slices the data structure contiguously.
It should return either:
It should return `{:ok, size, slicing_fun}` if the `enumerable` has
a known bound and can access a position in the `enumerable` without
traversing all previous elements.
* `{:ok, size, slicing_fun}` - if the `enumerable` has a known
bound and can access a position in the `enumerable` without
traversing all previous elements. The `slicing_fun` will receive
a `start` position, the `amount` of elements to fetch, and a
`step`.
* `{:ok, size, to_list_fun}` - if the `enumerable` has a known bound
and can access a position in the `enumerable` by first converting
it to a list via `to_list_fun`.
* `{:error, __MODULE__}` - the enumerable cannot be sliced efficiently
and a default algorithm built on top of `reduce/3` that runs in
linear time will be used.
Otherwise it should return `{:error, __MODULE__}` and a default
algorithm built on top of `reduce/3` that runs in linear time will be
used.
## Differences to `count/1`
The `size` value returned by this function is used for boundary checks,
therefore it is extremely important that this function only returns `:ok`
if retrieving the `size` of the `enumerable` is cheap, fast, and takes
constant time. Otherwise the simplest of operations, such as
`Enum.at(enumerable, 0)`, will become too expensive.
if retrieving the `size` of the `enumerable` is cheap, fast and takes constant
time. Otherwise the simplest of operations, such as `Enum.at(enumerable, 0)`,
will become too expensive.
On the other hand, the `count/1` function in this protocol should be
implemented whenever you can count the number of elements in the collection
without traversing it.
implemented whenever you can count the number of elements in the collection without
traversing it.
"""
@spec slice(t) ::
{:ok, size :: non_neg_integer(), slicing_fun() | to_list_fun()}
{:ok, size :: non_neg_integer(), slicing_fun()}
| {:error, module()}
def slice(enumerable)
end
@@ -234,7 +203,7 @@ defmodule Enum do
import Kernel, except: [max: 2, min: 2]
@moduledoc """
Functions for working with collections (known as enumerables).
Provides a set of algorithms to work with enumerables.
In Elixir, an enumerable is any data type that implements the
`Enumerable` protocol. `List`s (`[1, 2, 3]`), `Map`s (`%{foo: 1, bar: 2}`)
@@ -465,7 +434,7 @@ defmodule Enum do
"""
@spec at(t, index, default) :: element | default
def at(enumerable, index, default \\ nil) when is_integer(index) do
case slice_forward(enumerable, index, 1, 1) do
case slice_any(enumerable, index, 1) do
[value] -> value
[] -> default
end
@@ -529,9 +498,6 @@ defmodule Enum do
iex> Enum.chunk_every([1, 2, 3, 4, 5], 2, 3, [])
[[1, 2], [4, 5]]
iex> Enum.chunk_every([1, 2, 3, 4], 3, 3, Stream.cycle([0]))
[[1, 2, 3], [4, 0, 0]]
"""
@doc since: "1.5.0"
@spec chunk_every(t, pos_integer, pos_integer, t | :discard) :: [list]
@@ -651,7 +617,7 @@ defmodule Enum do
Concatenates the enumerable on the `right` with the enumerable on the
`left`.
This function produces the same result as the `++/2` operator
This function produces the same result as the `Kernel.++/2` operator
for lists.
## Examples
@@ -884,21 +850,17 @@ defmodule Enum do
drop_list(enumerable, amount)
end
def drop(enumerable, 0) do
to_list(enumerable)
end
def drop(enumerable, amount) when is_integer(amount) and amount > 0 do
def drop(enumerable, amount) when is_integer(amount) and amount >= 0 do
{result, _} = reduce(enumerable, {[], amount}, R.drop())
if is_list(result), do: :lists.reverse(result), else: []
end
def drop(enumerable, amount) when is_integer(amount) and amount < 0 do
{count, fun} = slice_count_and_fun(enumerable, 1)
{count, fun} = slice_count_and_fun(enumerable)
amount = Kernel.min(amount + count, count)
if amount > 0 do
fun.(0, amount, 1)
fun.(0, amount)
else
[]
end
@@ -1041,7 +1003,7 @@ defmodule Enum do
"""
@spec fetch(t, index) :: {:ok, element} | :error
def fetch(enumerable, index) when is_integer(index) do
case slice_forward(enumerable, index, 1, 1) do
case slice_any(enumerable, index, 1) do
[value] -> {:ok, value}
[] -> :error
end
@@ -1067,7 +1029,7 @@ defmodule Enum do
"""
@spec fetch!(t, index) :: element
def fetch!(enumerable, index) when is_integer(index) do
case slice_forward(enumerable, index, 1, 1) do
case slice_any(enumerable, index, 1) do
[value] -> value
[] -> raise Enum.OutOfBoundsError
end
@@ -1498,27 +1460,9 @@ defmodule Enum do
defp into_protocol(enumerable, collectable) do
{initial, fun} = Collectable.into(collectable)
try do
reduce_into_protocol(enumerable, initial, fun)
catch
kind, reason ->
fun.(initial, :halt)
:erlang.raise(kind, reason, __STACKTRACE__)
else
acc -> fun.(acc, :done)
end
end
defp reduce_into_protocol(enumerable, initial, fun) when is_list(enumerable) do
:lists.foldl(fn x, acc -> fun.(acc, {:cont, x}) end, initial, enumerable)
end
defp reduce_into_protocol(enumerable, initial, fun) do
enumerable
|> Enumerable.reduce({:cont, initial}, fn x, acc ->
{:cont, fun.(acc, {:cont, x})}
into_protocol(enumerable, initial, fun, fn entry, acc ->
fun.(acc, {:cont, entry})
end)
|> elem(1)
end
@doc """
@@ -1565,8 +1509,14 @@ defmodule Enum do
defp into_protocol(enumerable, collectable, transform) do
{initial, fun} = Collectable.into(collectable)
into_protocol(enumerable, initial, fun, fn entry, acc ->
fun.(acc, {:cont, transform.(entry)})
end)
end
defp into_protocol(enumerable, initial, fun, callback) do
try do
reduce_into_protocol(enumerable, initial, transform, fun)
reduce(enumerable, initial, callback)
catch
kind, reason ->
fun.(initial, :halt)
@@ -1576,18 +1526,6 @@ defmodule Enum do
end
end
defp reduce_into_protocol(enumerable, initial, transform, fun) when is_list(enumerable) do
:lists.foldl(fn x, acc -> fun.(acc, {:cont, transform.(x)}) end, initial, enumerable)
end
defp reduce_into_protocol(enumerable, initial, transform, fun) do
enumerable
|> Enumerable.reduce({:cont, initial}, fn x, acc ->
{:cont, fun.(acc, {:cont, transform.(x)})}
end)
|> elem(1)
end
@doc """
Joins the given `enumerable` into a string using `joiner` as a
separator.
@@ -1605,9 +1543,6 @@ defmodule Enum do
iex> Enum.join([1, 2, 3], " = ")
"1 = 2 = 3"
iex> Enum.join([["a", "b"], ["c", "d", "e", ["f", "g"]], "h", "i"], " ")
"ab cdefg h i"
"""
@spec join(t, String.t()) :: String.t()
def join(enumerable, joiner \\ "")
@@ -2276,13 +2211,13 @@ defmodule Enum do
iex> Enum.split_with([5, 4, 3, 2, 1, 0], fn x -> rem(x, 2) == 0 end)
{[4, 2, 0], [5, 3, 1]}
iex> Enum.split_with([a: 1, b: -2, c: 1, d: -3], fn {_k, v} -> v < 0 end)
iex> Enum.split_with(%{a: 1, b: -2, c: 1, d: -3}, fn {_k, v} -> v < 0 end)
{[b: -2, d: -3], [a: 1, c: 1]}
iex> Enum.split_with([a: 1, b: -2, c: 1, d: -3], fn {_k, v} -> v > 50 end)
iex> Enum.split_with(%{a: 1, b: -2, c: 1, d: -3}, fn {_k, v} -> v > 50 end)
{[], [a: 1, b: -2, c: 1, d: -3]}
iex> Enum.split_with([], fn {_k, v} -> v > 50 end)
iex> Enum.split_with(%{}, fn {_k, v} -> v > 50 end)
{[], []}
"""
@@ -2357,16 +2292,9 @@ defmodule Enum do
{:ok, 0, _} ->
[]
{:ok, count, fun} when is_function(fun, 1) ->
slice_list(fun.(enumerable), random_integer(0, count - 1), 1, 1)
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
{:ok, count, fun} when is_function(fun) ->
fun.(random_integer(0, count - 1), 1)
{:ok, count, fun} when is_function(fun, 3) ->
fun.(random_integer(0, count - 1), 1, 1)
{:error, _} ->
take_random(enumerable, 1)
end
@@ -2651,7 +2579,6 @@ defmodule Enum do
"""
@doc since: "1.13.0"
@spec slide(t, Range.t() | index, index) :: list
def slide(enumerable, range_or_single_index, insertion_index)
def slide(enumerable, single_index, insertion_index) when is_integer(single_index) do
@@ -2668,13 +2595,14 @@ defmodule Enum do
def slide(enumerable, first..last, insertion_index)
when first < 0 or last < 0 or insertion_index < 0 do
count = Enum.count(enumerable)
normalized_first = if first >= 0, do: first, else: Kernel.max(first + count, 0)
normalized_first = if first >= 0, do: first, else: first + count
normalized_last = if last >= 0, do: last, else: last + count
normalized_insertion_index =
if insertion_index >= 0, do: insertion_index, else: insertion_index + count
if normalized_first < count and normalized_first != normalized_insertion_index do
if normalized_first >= 0 and normalized_first < count and
normalized_first != normalized_insertion_index do
normalized_range = normalized_first..normalized_last//1
slide(enumerable, normalized_range, normalized_insertion_index)
else
@@ -2688,15 +2616,10 @@ defmodule Enum do
def slide(_, first..last, insertion_index)
when insertion_index > first and insertion_index <= last do
raise ArgumentError,
"insertion index for slide must be outside the range being moved " <>
raise "Insertion index for slide must be outside the range being moved " <>
"(tried to insert #{first}..#{last} at #{insertion_index})"
end
def slide(enumerable, first..last, _insertion_index) when first > last do
Enum.to_list(enumerable)
end
# Guarantees at this point: step size == 1 and first <= last and (insertion_index < first or insertion_index > last)
def slide(enumerable, first..last, insertion_index) do
impl = if is_list(enumerable), do: &slide_list_start/4, else: &slide_any/4
@@ -2745,7 +2668,6 @@ defmodule Enum do
end
defp slide_list_start(list, 0, middle, last), do: slide_list_middle(list, middle, last, [])
defp slide_list_start([], _start, _middle, _last), do: []
defp slide_list_middle([h | t], middle, last, acc) when middle > 0 do
slide_list_middle(t, middle - 1, last - 1, [h | acc])
@@ -2868,49 +2790,31 @@ defmodule Enum do
`enumerable`, or this one is greater than the normalized `index_range.last`,
then `[]` is returned.
If a step `n` (other than `1`) is used in `index_range`, then it takes
every `n`th element from `index_range.first` to `index_range.last`
(according to the same rules described above).
## Examples
iex> Enum.slice([1, 2, 3, 4, 5], 1..3)
[2, 3, 4]
iex> Enum.slice(1..100, 5..10)
[6, 7, 8, 9, 10, 11]
iex> Enum.slice([1, 2, 3, 4, 5], 3..10)
[4, 5]
iex> Enum.slice(1..10, 5..20)
[6, 7, 8, 9, 10]
# Last three elements (negative indexes)
iex> Enum.slice([1, 2, 3, 4, 5], -3..-1)
[3, 4, 5]
# last five elements (negative indexes)
iex> Enum.slice(1..30, -5..-1)
[26, 27, 28, 29, 30]
For ranges where `start > stop`, you need to explicit
mark them as increasing:
iex> Enum.slice([1, 2, 3, 4, 5], 1..-2//1)
[2, 3, 4]
iex> Enum.slice(1..30, 25..-1//1)
[26, 27, 28, 29, 30]
The step can be any positive number. For example, to
get every 2 elements of the collection:
If values are out of bounds, it returns an empty list:
iex> Enum.slice([1, 2, 3, 4, 5], 0..-1//2)
[1, 3, 5]
To get every third element of the first ten elements:
iex> integers = Enum.to_list(1..20)
iex> Enum.slice(integers, 0..9//3)
[1, 4, 7, 10]
If the first position is after the end of the enumerable
or after the last position of the range, it returns an
empty list:
iex> Enum.slice([1, 2, 3, 4, 5], 6..10)
iex> Enum.slice(1..10, 11..20)
[]
# first is greater than last
iex> Enum.slice([1, 2, 3, 4, 5], 6..5)
iex> Enum.slice(1..10, 6..5)
[]
"""
@@ -2918,17 +2822,16 @@ defmodule Enum do
@spec slice(t, Range.t()) :: list
def slice(enumerable, first..last//step = index_range) do
# TODO: Deprecate negative steps on Elixir v1.16
# TODO: Support negative steps as a reverse on Elixir v2.0.
cond do
step > 0 ->
slice_range(enumerable, first, last, step)
step == -1 and first > last ->
slice_range(enumerable, first, last, 1)
true ->
raise ArgumentError,
"Enum.slice/2 does not accept ranges with negative steps, got: #{inspect(index_range)}"
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(enumerable, first, last)
else
raise ArgumentError,
"Enum.slice/2 does not accept ranges with custom steps, got: #{inspect(index_range)}"
end
end
@@ -2938,29 +2841,18 @@ defmodule Enum do
slice(enumerable, Map.put(index_range, :step, step))
end
defp slice_range(enumerable, first, -1, step) when first >= 0 do
if step == 1 do
drop(enumerable, first)
else
enumerable |> drop(first) |> take_every_list(step - 1)
end
defp slice_range(enumerable, first, last) when last >= first and last >= 0 and first >= 0 do
slice_any(enumerable, first, last - first + 1)
end
defp slice_range(enumerable, first, last, step)
when last >= first and last >= 0 and first >= 0 do
slice_forward(enumerable, first, last - first + 1, step)
end
defp slice_range(enumerable, first, last, step) do
{count, fun} = slice_count_and_fun(enumerable, step)
first = if first >= 0, do: first, else: Kernel.max(first + count, 0)
defp slice_range(enumerable, first, last) do
{count, fun} = slice_count_and_fun(enumerable)
first = if first >= 0, do: first, else: first + count
last = if last >= 0, do: last, else: last + count
amount = last - first + 1
if first < count and amount > 0 do
amount = Kernel.min(amount, count - first)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(first, amount, step)
if first >= 0 and first < count and amount > 0 do
fun.(first, Kernel.min(amount, count - first))
else
[]
end
@@ -2995,33 +2887,22 @@ defmodule Enum do
# using a negative start index
iex> Enum.slice(1..10, -6, 3)
[5, 6, 7]
iex> Enum.slice(1..10, -11, 5)
[1, 2, 3, 4, 5]
# out of bound start index
# out of bound start index (positive)
iex> Enum.slice(1..10, 10, 5)
[]
# out of bound start index (negative)
iex> Enum.slice(1..10, -11, 5)
[]
"""
@spec slice(t, index, non_neg_integer) :: list
def slice(_enumerable, start_index, 0) when is_integer(start_index), do: []
def slice(enumerable, start_index, amount)
when is_integer(start_index) and start_index < 0 and is_integer(amount) and amount >= 0 do
{count, fun} = slice_count_and_fun(enumerable, 1)
start_index = Kernel.max(count + start_index, 0)
amount = Kernel.min(amount, count - start_index)
if amount > 0 do
fun.(start_index, amount, 1)
else
[]
end
end
def slice(enumerable, start_index, amount)
when is_integer(start_index) and is_integer(amount) and amount >= 0 do
slice_forward(enumerable, start_index, amount, 1)
slice_any(enumerable, start_index, amount)
end
@doc """
@@ -3070,10 +2951,10 @@ defmodule Enum do
iex> Enum.sort(["some", "kind", "of", "monster"], &(byte_size(&1) < byte_size(&2)))
["of", "kind", "some", "monster"]
## Ascending and descending (since v1.10.0)
## Ascending and descending
`sort/2` allows a developer to pass `:asc` or `:desc` as the sorter, which is a convenience for
[`&<=/2`](`<=/2`) and [`&>=/2`](`>=/2`) respectively.
`sort/2` allows a developer to pass `:asc` or `:desc` as the sorting
function, which is a convenience for `<=/2` and `>=/2` respectively.
iex> Enum.sort([2, 3, 1], :asc)
[1, 2, 3]
@@ -3121,12 +3002,12 @@ defmodule Enum do
t,
(element, element -> boolean) | :asc | :desc | module() | {:asc | :desc, module()}
) :: list
def sort(enumerable, sorter) when is_list(enumerable) do
:lists.sort(to_sort_fun(sorter), enumerable)
def sort(enumerable, fun) when is_list(enumerable) do
:lists.sort(to_sort_fun(fun), enumerable)
end
def sort(enumerable, sorter) do
fun = to_sort_fun(sorter)
def sort(enumerable, fun) do
fun = to_sort_fun(fun)
reduce(enumerable, [], &sort_reducer(&1, &2, fun))
|> sort_terminator(fun)
@@ -3145,27 +3026,17 @@ defmodule Enum do
This function maps each element of the `enumerable` using the
provided `mapper` function. The enumerable is then sorted by
the mapped elements using the `sorter`, which defaults to `:asc`
and sorts the elements ascendingly.
the mapped elements using the `sorter` function, which defaults
to `Kernel.<=/2`.
`sort_by/3` differs from `sort/2` in that it only calculates the
comparison value for each element in the enumerable once instead of
once for each element in each comparison. If the same function is
being called on both elements, it's more efficient to use `sort_by/3`.
## Ascending and descending (since v1.10.0)
`sort_by/3` allows a developer to pass `:asc` or `:desc` as the sorter,
which is a convenience for [`&<=/2`](`<=/2`) and [`&>=/2`](`>=/2`) respectively:
iex> Enum.sort_by([2, 3, 1], &(&1), :asc)
[1, 2, 3]
iex> Enum.sort_by([2, 3, 1], &(&1), :desc)
[3, 2, 1]
## Examples
Using the default `sorter` of `:asc` :
Using the default `sorter` of `<=/2`:
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1)
["of", "some", "kind", "monster"]
@@ -3178,15 +3049,19 @@ defmodule Enum do
Similar to `sort/2`, you can pass a custom sorter:
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1, &>=/2)
["monster", "some", "kind", "of"]
Or use `:asc` and `:desc`:
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1, :desc)
["monster", "some", "kind", "of"]
As in `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, to sort users by their birthday in both ascending and
descending order respectively:
As in `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, to sort users by their birthday in both
ascending and descending order respectively:
iex> users = [
...> %{name: "Ellis", birthday: ~D[1943-05-11]},
@@ -3206,42 +3081,6 @@ defmodule Enum do
%{name: "Lovelace", birthday: ~D[1815-12-10]}
]
## Performance characteristics
As detailed in the initial section, `sort_by/3` calculates the comparison
value for each element in the enumerable once instead of once for each
element in each comparison. This implies `sort_by/3` must do an initial
pass on the data to compute those values.
However, if those values are cheap to compute, for example, you have
already extracted the field you want to sort by into a tuple, then those
extra passes become overhead. In such cases, consider using `List.keysort/3`
instead.
Let's see an example. Imagine you have a list of products and you have a
list of IDs. You want to keep all products that are in the given IDs and
return their names sorted by their price. You could write it like this:
for(
product <- products,
product.id in ids,
do: product
)
|> Enum.sort_by(& &1.price)
|> Enum.map(& &1.name)
However, you could also write it like this:
for(
product <- products,
product.id in ids,
do: {product.name, product.price}
)
|> List.keysort(1)
|> Enum.map(&elem(&1, 0))
Using `List.keysort/3` will be a better choice for performance sensitive
code as it avoids additional traversals.
"""
@spec sort_by(
t,
@@ -3250,22 +3089,31 @@ defmodule Enum do
) ::
list
when mapped_element: element
def sort_by(enumerable, mapper, sorter \\ :asc)
def sort_by(enumerable, mapper, :desc) when is_function(mapper, 1) do
enumerable
|> Enum.reduce([], &[{&1, mapper.(&1)} | &2])
|> List.keysort(1, :asc)
|> List.foldl([], &[elem(&1, 0) | &2])
end
def sort_by(enumerable, mapper, sorter) when is_function(mapper, 1) do
def sort_by(enumerable, mapper, sorter \\ &<=/2) do
enumerable
|> map(&{&1, mapper.(&1)})
|> List.keysort(1, sorter)
|> sort(to_sort_by_fun(sorter))
|> map(&elem(&1, 0))
end
defp to_sort_by_fun(sorter) when is_function(sorter, 2),
do: &sorter.(elem(&1, 1), elem(&2, 1))
defp to_sort_by_fun(:asc),
do: &(elem(&1, 1) <= elem(&2, 1))
defp to_sort_by_fun(:desc),
do: &(elem(&1, 1) >= elem(&2, 1))
defp to_sort_by_fun(module) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :gt)
defp to_sort_by_fun({:asc, module}) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :gt)
defp to_sort_by_fun({:desc, module}) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :lt)
@doc """
Splits the `enumerable` into two enumerables, leaving `count`
elements in the first one.
@@ -3456,9 +3304,9 @@ defmodule Enum do
end
def take(enumerable, amount) when is_integer(amount) and amount < 0 do
{count, fun} = slice_count_and_fun(enumerable, 1)
{count, fun} = slice_count_and_fun(enumerable)
first = Kernel.max(amount + count, 0)
fun.(first, count - first, 1)
fun.(first, count - first)
end
@doc """
@@ -3485,12 +3333,9 @@ defmodule Enum do
@spec take_every(t, non_neg_integer) :: list
def take_every(enumerable, nth)
def take_every(_enumerable, 0), do: []
def take_every(enumerable, 1), do: to_list(enumerable)
def take_every(list, nth) when is_list(list) and is_integer(nth) and nth > 1 do
take_every_list(list, nth - 1)
end
def take_every(_enumerable, 0), do: []
def take_every([], nth) when is_integer(nth) and nth > 1, do: []
def take_every(enumerable, nth) when is_integer(nth) and nth > 1 do
{res, _} = reduce(enumerable, {[], :first}, R.take_every(nth))
@@ -3770,14 +3615,6 @@ defmodule Enum do
@spec with_index(t, (element, index -> value)) :: [value] when value: any
def with_index(enumerable, fun_or_offset \\ 0)
def with_index(enumerable, offset) when is_list(enumerable) and is_integer(offset) do
with_index_list(enumerable, offset)
end
def with_index(enumerable, fun) when is_list(enumerable) and is_function(fun, 2) do
with_index_list(enumerable, 0, fun)
end
def with_index(enumerable, offset) when is_integer(offset) do
enumerable
|> map_reduce(offset, fn x, i -> {{x, i}, i + 1} end)
@@ -3842,7 +3679,7 @@ defmodule Enum do
Zips corresponding elements from two enumerables into a list, transforming them with
the `zip_fun` function as it goes.
The corresponding elements from each collection are passed to the provided two-arity `zip_fun`
The corresponding elements from each collection are passed to the provided 2-arity `zip_fun`
function in turn. Returns a list that contains the result of calling `zip_fun` for each pair of
elements.
@@ -3893,7 +3730,7 @@ defmodule Enum do
into list, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put
into a list which is then passed to the one-arity `zip_fun` function.
into a list which is then passed to the 1-arity `zip_fun` function.
Then, the second elements from each of the enums are put into a list
and passed to `zip_fun`, and so on until any one of the enums in
`enumerables` runs out of elements.
@@ -4357,63 +4194,35 @@ defmodule Enum do
## slice
defp slice_forward(enumerable, start, amount, step) when start < 0 do
{count, fun} = slice_count_and_fun(enumerable, step)
defp slice_any(enumerable, start, amount) when start < 0 do
{count, fun} = slice_count_and_fun(enumerable)
start = count + start
if start >= 0 do
amount = Kernel.min(amount, count - start)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(start, amount, step)
fun.(start, Kernel.min(amount, count - start))
else
[]
end
end
defp slice_forward(list, start, amount, step) when is_list(list) do
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
slice_list(list, start, amount, step)
defp slice_any(list, start, amount) when is_list(list) do
list |> drop_list(start) |> take_list(amount)
end
defp slice_forward(enumerable, start, amount, step) do
defp slice_any(enumerable, start, amount) do
case Enumerable.slice(enumerable) do
{:ok, count, _} when start >= count ->
[]
{:ok, count, fun} when is_function(fun, 1) ->
amount = Kernel.min(amount, count - start)
enumerable |> fun.() |> slice_exact(start, amount, step, count)
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
amount = Kernel.min(amount, count - start)
if step == 1 do
fun.(start, amount)
else
fun.(start, Kernel.min(amount * step, count - start))
|> take_every_list(amount, step - 1)
end
{:ok, count, fun} when is_function(fun, 3) ->
amount = Kernel.min(amount, count - start)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(start, amount, step)
{:ok, count, fun} when is_function(fun) ->
fun.(start, Kernel.min(amount, count - start))
{:error, module} ->
slice_enum(enumerable, module, start, amount, step)
slice_enum(enumerable, module, start, amount)
end
end
defp slice_list(list, start, amount, step) do
if step == 1 do
list |> drop_list(start) |> take_list(amount)
else
list |> drop_list(start) |> take_every_list(amount, step - 1)
end
end
defp slice_enum(enumerable, module, start, amount, 1) do
defp slice_enum(enumerable, module, start, amount) do
{_, {_, _, slice}} =
module.reduce(enumerable, {:cont, {start, amount, []}}, fn
_entry, {start, amount, _list} when start > 0 ->
@@ -4429,86 +4238,26 @@ defmodule Enum do
:lists.reverse(slice)
end
defp slice_enum(enumerable, module, start, amount, step) do
{_, {_, _, _, slice}} =
module.reduce(enumerable, {:cont, {start, amount, 1, []}}, fn
_entry, {start, amount, to_drop, _list} when start > 0 ->
{:cont, {start - 1, amount, to_drop, []}}
entry, {start, amount, to_drop, list} when amount > 1 ->
case to_drop do
1 -> {:cont, {start, amount - 1, step, [entry | list]}}
_ -> {:cont, {start, amount - 1, to_drop - 1, list}}
end
entry, {start, amount, to_drop, list} ->
case to_drop do
1 -> {:halt, {start, amount, to_drop, [entry | list]}}
_ -> {:halt, {start, amount, to_drop, list}}
end
end)
:lists.reverse(slice)
defp slice_count_and_fun(enumerable) when is_list(enumerable) do
length = length(enumerable)
{length, &Enumerable.List.slice(enumerable, &1, &2, length)}
end
defp slice_count_and_fun(list, _step) when is_list(list) do
length = length(list)
{length, &slice_exact(list, &1, &2, &3, length)}
end
defp slice_count_and_fun(enumerable, step) do
defp slice_count_and_fun(enumerable) do
case Enumerable.slice(enumerable) do
{:ok, count, fun} when is_function(fun, 3) ->
{:ok, count, fun} when is_function(fun, 2) ->
{count, fun}
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
if step == 1 do
{count, fn start, amount, 1 -> fun.(start, amount) end}
else
{count,
fn start, amount, step ->
fun.(start, Kernel.min(amount * step, count - start))
|> take_every_list(amount, step - 1)
end}
end
{:ok, count, fun} when is_function(fun, 1) ->
{count, &slice_exact(fun.(enumerable), &1, &2, &3, count)}
{:error, module} ->
{list, count} =
enumerable
|> module.reduce({:cont, {[], 0}}, fn elem, {acc, count} ->
{_, {list, count}} =
module.reduce(enumerable, {:cont, {[], 0}}, fn elem, {acc, count} ->
{:cont, {[elem | acc], count + 1}}
end)
|> elem(1)
{count,
fn start, amount, step ->
list |> :lists.reverse() |> slice_exact(start, amount, step, count)
end}
{count, &Enumerable.List.slice(:lists.reverse(list), &1, &2, count)}
end
end
# Slice a list when we know the bounds
defp slice_exact(_list, _start, 0, _step, _), do: []
defp slice_exact(list, start, amount, 1, size) when start + amount == size,
do: list |> drop_exact(start)
defp slice_exact(list, start, amount, 1, _),
do: list |> drop_exact(start) |> take_exact(amount)
defp slice_exact(list, start, amount, step, _),
do: list |> drop_exact(start) |> take_every_list(amount, step - 1)
defp drop_exact(list, 0), do: list
defp drop_exact([_ | tail], amount), do: drop_exact(tail, amount - 1)
defp take_exact(_list, 0), do: []
defp take_exact([head | tail], amount), do: [head | take_exact(tail, amount - 1)]
## sort
defp sort_reducer(entry, {:split, y, x, r, rs, bool}, fun) do
@@ -4653,22 +4402,10 @@ defmodule Enum do
## take
defp take_list(_list, 0), do: []
defp take_list([head | _], 1), do: [head]
defp take_list([head | tail], counter), do: [head | take_list(tail, counter - 1)]
defp take_list([], _counter), do: []
defp take_every_list([head | tail], to_drop),
do: [head | tail |> drop_list(to_drop) |> take_every_list(to_drop)]
defp take_every_list([], _to_drop), do: []
defp take_every_list(_list, 0, _to_drop), do: []
defp take_every_list([head | tail], counter, to_drop),
do: [head | tail |> drop_list(to_drop) |> take_every_list(counter - 1, to_drop)]
defp take_every_list([], _counter, _to_drop), do: []
## take_while
defp take_while_list([head | tail], fun) do
@@ -4698,20 +4435,6 @@ defmodule Enum do
[]
end
## with_index
defp with_index_list([head | tail], offset) do
[{head, offset} | with_index_list(tail, offset + 1)]
end
defp with_index_list([], _offset), do: []
defp with_index_list([head | tail], offset, fun) do
[fun.(head, offset) | with_index_list(tail, offset + 1, fun)]
end
defp with_index_list([], _offset, _fun), do: []
## zip
defp zip_list([head1 | next1], [head2 | next2], acc) do
@@ -4737,18 +4460,30 @@ defmodule Enum do
end
defimpl Enumerable, for: List do
def count(list), do: {:ok, length(list)}
def count([]), do: {:ok, 0}
def count(_list), do: {:error, __MODULE__}
def member?([], _value), do: {:ok, false}
def member?(_list, _value), do: {:error, __MODULE__}
def slice([]), do: {:ok, 0, fn _, _, _ -> [] end}
def slice([]), do: {:ok, 0, fn _, _ -> [] end}
def slice(_list), do: {:error, __MODULE__}
def reduce(_list, {:halt, acc}, _fun), do: {:halted, acc}
def reduce(list, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(list, &1, fun)}
def reduce([], {:cont, acc}, _fun), do: {:done, acc}
def reduce([head | tail], {:cont, acc}, fun), do: reduce(tail, fun.(head, acc), fun)
@doc false
def slice(_list, _start, 0, _size), do: []
def slice(list, start, count, size) when start + count == size, do: list |> drop(start)
def slice(list, start, count, _size), do: list |> drop(start) |> take(count)
defp drop(list, 0), do: list
defp drop([_ | tail], count), do: drop(tail, count - 1)
defp take(_list, 0), do: []
defp take([head | tail], count), do: [head | take(tail, count - 1)]
end
defimpl Enumerable, for: Map do
@@ -4766,7 +4501,7 @@ defimpl Enumerable, for: Map do
def slice(map) do
size = map_size(map)
{:ok, size, &:maps.to_list/1}
{:ok, size, &Enumerable.List.slice(:maps.to_list(map), &1, &2, size)}
end
def reduce(map, acc, fun) do
+61 -218
View File
@@ -227,11 +227,7 @@ defmodule Exception do
|> 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, ann, scope, binding))
{args, guards}
end
@@ -241,69 +237,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: {_, _, [{_, _, [_, arg]} | optional]}}]}) do
args =
case optional do
[] -> [arg]
[module] -> [arg, 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}
@@ -347,38 +280,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, 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)
@@ -704,12 +622,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
@@ -822,8 +740,8 @@ defmodule ArgumentError do
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"
"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)}. " <>
@@ -1112,8 +1030,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
@@ -1178,7 +1096,7 @@ defmodule UndefinedFunctionError do
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
@@ -1336,13 +1254,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
@@ -1557,24 +1473,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
@@ -1583,11 +1497,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
@@ -1633,19 +1551,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
@@ -1672,11 +1581,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
@@ -1691,35 +1597,30 @@ 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,
defp error_info(:badarg, [{:erlang, fun, _, _} | _]) when fun in [:byte_size, :bit_size] do
{:ok,
"""
* 1st argument: not a bitstring
This typically happens when calling Kernel.#{fun}/1 with an invalid argument \
or when performing binary construction or binary concatenation with <> and \
one of the arguments is not a binary\
"""}
end
defp error_info(erl_exception, stacktrace) do
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
@@ -1733,61 +1634,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
+33 -38
View File
@@ -153,11 +153,6 @@ 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
@@ -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.
@@ -322,7 +317,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 +407,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 +429,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 +455,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 +482,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 +507,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 +523,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 +546,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 +559,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 +576,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 +591,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 +608,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 +675,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 +687,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 +858,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
@@ -1174,8 +1166,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 """
+75 -229
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
@@ -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,38 +250,28 @@ end
defimpl Inspect, for: Map do
def inspect(map, opts) do
list =
if Keyword.get(opts.custom_options, :sort_maps) do
map |> Map.to_list() |> :lists.sort()
else
Map.to_list(map)
end
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
@@ -371,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
@@ -427,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)) ->
@@ -448,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
@@ -460,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
@@ -472,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))
@@ -526,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
@@ -539,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
+32 -58
View File
@@ -31,11 +31,6 @@ defmodule Inspect.Opts do
options. Useful when implementing the `Inspect` protocol for nested structs
to pass the custom options through.
It supports some pre-defined keys:
- `:sort_maps` (since v1.15.0) - if set to `true`, sorts key-value pairs in maps.
This can be helpful to make map inspection deterministic for testing,
especially since key order is random since OTP 26.
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents.
Defaults to `Inspect.Opts.default_inspect_fun/0`.
@@ -58,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`.
@@ -69,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
@@ -95,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()),
@@ -167,6 +163,13 @@ defmodule Inspect.Opts do
end
end
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
defexception [:message]
end
defmodule Inspect.Algebra do
@moduledoc ~S"""
A set of functions for creating and manipulating algebra
@@ -259,18 +262,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)}
@@ -320,8 +317,7 @@ defmodule Inspect.Algebra do
:doc_force,
:doc_group,
:doc_nest,
:doc_string,
:doc_limit
:doc_string
]
defguard is_doc(doc)
@@ -358,28 +354,28 @@ defmodule Inspect.Algebra do
try do
Process.put(:inspect_trap, true)
inspected_struct =
struct
|> Inspect.Map.inspect(%{
res =
Inspect.Map.inspect(struct, %{
opts
| syntax_colors: [],
inspect_fun: Inspect.Opts.default_inspect_fun()
})
|> format(opts.width)
|> IO.iodata_to_binary()
inspect_error =
Inspect.Error.exception(
exception: caught_exception,
stacktrace: __STACKTRACE__,
inspected_struct: inspected_struct
)
res = IO.iodata_to_binary(format(res, :infinity))
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
| inspect_fun: Inspect.Opts.default_inspect_fun()
})
else
reraise(inspect_error, __STACKTRACE__)
reraise(exception, __STACKTRACE__)
end
after
Process.delete(:inspect_trap)
@@ -473,9 +469,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
@@ -587,10 +582,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.
@@ -640,7 +631,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
@@ -981,7 +972,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
@@ -1046,17 +1037,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)]
@@ -1110,15 +1093,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
+5 -5
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`
"""
+8 -51
View File
@@ -172,17 +172,8 @@ defmodule IO do
@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, line by line if
`:line` is given, or until `:eof`.
It returns:
@@ -295,24 +286,14 @@ defmodule IO do
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,36 +302,12 @@ 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
end
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
message = to_chardata(message)
formatted_trace = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
@@ -588,7 +545,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 +590,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()
+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
+1 -3
View File
@@ -78,9 +78,7 @@ defmodule IO.ANSI.Docs do
@metadata_filter [:deprecated, :guard, :since]
defp print_each_metadata(metadata, options) do
metadata
|> Enum.sort()
|> Enum.reduce(false, fn
Enum.reduce(metadata, false, fn
{key, value}, _printed when is_binary(value) and key in @metadata_filter ->
label = metadata_label(key, options)
indent = String.duplicate(" ", length_without_escape(label, 0) + 1)
+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
+214 -490
View File
File diff suppressed because it is too large Load Diff
+17 -26
View File
@@ -13,8 +13,7 @@ defmodule Kernel.CLI do
pa: [],
pz: [],
verbose_compile: false,
profile: nil,
pry: false
profile: nil
}
@standalone_opts ["-h", "--help", "--short-version"]
@@ -29,10 +28,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)
@@ -223,13 +218,13 @@ defmodule Kernel.CLI do
# Parse shared options
defp halt_standalone(opt) do
defp warn_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)
warn_standalone(opt)
System.halt(1)
end
defp parse_shared([opt | t], _config) when opt in ["-v", "--version"] do
@@ -240,11 +235,8 @@ defmodule Kernel.CLI do
IO.puts("Elixir " <> System.build_info()[:build])
end
if t != [] do
halt_standalone(opt)
else
System.halt(0)
end
t != [] && warn_standalone(opt)
System.halt(0)
end
defp parse_shared(["-pa", h | t], config) do
@@ -280,11 +272,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 +311,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 +396,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))
+52 -63
View File
@@ -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
@@ -210,20 +173,24 @@ defmodule Kernel.LexicalTracker do
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
%{directives: directives, exports: exports} = state
to_remove = for {{:import, {^module, _, _}} = key, _} <- directives, do: key
directives =
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, exports: Map.put(exports, module, true)}}
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
@@ -257,7 +224,29 @@ defmodule Kernel.LexicalTracker do
defp add_reference(references, module, :runtime) when is_atom(module) do
case references do
%{^module => _} -> references
%{} -> Map.put(references, module, :runtime)
_ -> 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
+23 -31
View File
@@ -5,9 +5,9 @@ defmodule Kernel.ParallelCompiler do
@typedoc "The line. 0 indicates no line."
@type line() :: non_neg_integer()
@type location() :: line() | {pos_integer(), column :: non_neg_integer}
@type location() :: line() | {line(), column :: non_neg_integer}
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
@type error() :: {file :: Path.t(), location(), message :: String.t()}
@type error() :: {file :: Path.t(), line(), message :: String.t()}
@doc """
Starts a task for parallel compilation.
@@ -28,7 +28,7 @@ defmodule Kernel.ParallelCompiler do
dest = :erlang.get(:elixir_compiler_dest)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
{_parent, checker} = Module.ParallelChecker.get()
checker = Module.ParallelChecker.get()
Task.async(fn ->
send(compiler, {:async, self()})
@@ -241,7 +241,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)
@@ -268,11 +268,17 @@ defmodule Kernel.ParallelCompiler do
%{profile: profile, checker: checker} = state
compiled_modules =
for {{:module, module}, _} <- result,
do: module
for {{:module, _module}, {_binary, info}} <- result,
do: info
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(checker, runtime_modules)
Module.ParallelChecker.verify(checker, compiled_modules, runtime_modules)
end)
end
@@ -473,24 +479,16 @@ 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.
defp without_definition(waiting, files) do
nilify_empty(
nillify_empty(
for %{pid: pid} <- files,
{_, _, ref, ^pid, on, _, _} <- waiting,
not defining?(on, waiting),
@@ -499,7 +497,7 @@ defmodule Kernel.ParallelCompiler do
end
defp deadlocked(waiting, type, defining?) do
nilify_empty(
nillify_empty(
for {_, _, ref, _, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
@@ -510,8 +508,8 @@ defmodule Kernel.ParallelCompiler do
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,7 +528,7 @@ defmodule Kernel.ParallelCompiler do
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, checker_info} ->
state.each_module.(file, module, binary)
# Release the module loader which is waiting for an ack
@@ -540,7 +538,7 @@ defmodule Kernel.ParallelCompiler do
for {:module, _, ref, _, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {:module, module}, binary)
result = Map.put(result, {:module, module}, {binary, checker_info})
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
# If we are simply requiring files, we do not add to waiting.
@@ -754,11 +752,6 @@ defmodule Kernel.ParallelCompiler do
{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}
end
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
line
end
@@ -769,8 +762,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 -68
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])
@@ -554,7 +554,9 @@ defmodule Kernel.SpecialForms do
defmacro alias(module, opts), do: error!([module, opts])
@doc """
Requires a module in order to use its macros.
Requires a module as a compile-time dependency.
Requiring a module is necessary in order to use its macros.
## Examples
@@ -576,7 +578,6 @@ defmodule Kernel.SpecialForms do
`require/2` also accepts `:as` as an option so it automatically sets
up an alias. Please check `alias/2` for more information.
"""
defmacro require(module, opts), do: error!([module, opts])
@@ -604,12 +605,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 +779,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.
"""
@@ -1316,13 +1316,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 +1387,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 +1407,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
@@ -1638,7 +1603,7 @@ defmodule Kernel.SpecialForms do
{:ok, backup_path}
end
defp validate_extension(path) do
defp validate_extname(path) do
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
end
@@ -2093,25 +2058,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
+7 -20
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
@@ -567,10 +567,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)
)
@@ -681,20 +678,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
+10 -110
View File
@@ -22,38 +22,9 @@ 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)
@@ -100,15 +71,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 +83,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 +99,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 +113,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,
@@ -331,7 +231,7 @@ defmodule Kernel.Utils do
quote do
case Macro.Env.in_guard?(__CALLER__) do
true -> unquote(literal_quote(unquote_every_ref(expr, vars)))
false -> unquote(literal_quote(unquote_refs_once(expr, vars, env.module)))
false -> unquote(literal_quote(unquote_refs_once(expr, vars)))
end
end
end
@@ -361,7 +261,7 @@ defmodule Kernel.Utils do
end
# Prefaces `guard` with unquoted versions of `refs`.
defp unquote_refs_once(guard, refs, module) do
defp unquote_refs_once(guard, refs) do
{guard, used_refs} =
Macro.postwalk(guard, %{}, fn
{ref, meta, context} = var, acc when is_atom(ref) and is_atom(context) ->
@@ -375,7 +275,7 @@ defmodule Kernel.Utils do
%{} ->
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
new_var = Macro.unique_var(generated, module)
new_var = Macro.var(generated, Elixir)
{new_var, Map.put(acc, pair, {new_var, var})}
end
+7 -80
View File
@@ -92,7 +92,6 @@ defmodule Keyword do
iex> %{1 => 2, foo: :bar}
%{1 => 2, :foo => :bar}
"""
@compile :inline_list_funcs
@@ -103,21 +102,6 @@ defmodule Keyword do
@type t :: [{key, value}]
@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`.
@@ -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
@@ -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,7 +187,8 @@ defmodule Keyword do
end
@doc """
Ensures the given `keyword` has only the keys given in `values`.
Ensures the first argument is a `keyword` with the given
keys and default values.
The second argument must be a list of atoms, specifying
a given key, or tuples specifying a key and a default value.
@@ -239,12 +224,6 @@ defmodule Keyword do
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()}]) ::
@@ -323,12 +302,6 @@ defmodule Keyword do
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()
@@ -342,17 +315,8 @@ defmodule Keyword do
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
raise ArgumentError,
"unknown keys #{inspect(invalid_keys)} in #{inspect(keyword)}, the allowed keys are: #{inspect(keys)}"
end
end
@@ -889,43 +853,6 @@ defmodule Keyword do
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.
@@ -1201,7 +1128,7 @@ 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 """
+9 -115
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]
@@ -239,7 +239,7 @@ 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}
@@ -257,7 +257,7 @@ defmodule List do
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}
@@ -339,11 +339,6 @@ 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
@@ -368,11 +363,6 @@ defmodule List do
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
@@ -401,11 +391,6 @@ 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
@@ -424,11 +409,6 @@ 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
@@ -437,13 +417,7 @@ defmodule List do
@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 +427,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) when is_integer(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,11 +447,6 @@ 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
@@ -557,11 +469,6 @@ 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
@@ -586,11 +493,6 @@ 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
@@ -947,22 +849,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
+76 -744
View File
File diff suppressed because it is too large Load Diff
+25 -33
View File
@@ -24,7 +24,7 @@ defmodule Macro.Env do
* `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
@@ -79,39 +79,31 @@ defmodule Macro.Env do
versioned_vars: versioned_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)
@doc false
def __struct__ do
%{
__struct__: __MODULE__,
aliases: [],
context: nil,
context_modules: [],
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
requires: [],
tracers: [],
versioned_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 """
@@ -166,7 +158,7 @@ defmodule Macro.Env do
@doc """
Fetches the alias for the given atom.
Returns `{:ok, alias}` if the alias exists, `:error`
Returns `{:ok, alias}` if the alias exists, `:error`
otherwise.
## Examples
+4 -57
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`.
@@ -394,32 +367,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 +563,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.
@@ -1112,8 +1059,8 @@ defmodule Map do
end
@doc false
@deprecated "Use Map.new/2 instead (invoke Map.from_struct/1 before if you have a struct)"
@deprecated "Use Map.new/2 instead"
def map(map, fun) when is_map(map) do
:maps.map(fn k, v -> fun.({k, v}) end, map)
new(map, fn {k, v} -> {k, fun.({k, v})} end)
end
end
+61 -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
@@ -492,10 +402,11 @@ defmodule MapSet do
end
defimpl Collectable do
# TODO: Optimize into an empty mapset by using :maps.from_keys/2 on Erlang/OTP 24+
def into(map_set) do
fun = fn
list, {:cont, x} -> [x | list]
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 +419,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
+260 -378
View File
@@ -22,12 +22,6 @@ defmodule Module do
Accepts a module or a `{module, function_name}`. See the "Compile callbacks"
section below.
### `@after_verify` (since v1.14.0)
A hook that will be invoked right after the current module is verified for
undefined functions, deprecations, etc. Accepts a module or a `{module, function_name}`.
See the "Compile callbacks" section below.
### `@before_compile`
A hook that will be invoked before the module is compiled.
@@ -134,7 +128,7 @@ defmodule Module do
Multiple uses of `@compile` will accumulate instead of overriding
previous ones. See the "Compile options" section below.
### `@deprecated` (since v1.6.0)
### `@deprecated`
Provides the deprecation reason for a function. For example:
@@ -182,14 +176,9 @@ defmodule Module do
`@doc` is to be used with a function, macro, callback, or
macrocallback, while `@typedoc` with a type (public or opaque).
Accepts one of these:
* a string (often a heredoc)
* `false`, which will make the entity invisible to documentation-extraction
tools like [`ExDoc`](https://hexdocs.pm/ex_doc/)
* a keyword list, since Elixir 1.7.0
For example:
Accepts a string (often a heredoc) or `false` where `@doc false` will
make the entity invisible to documentation extraction tools like
[`ExDoc`](https://hexdocs.pm/ex_doc/). For example:
defmodule MyModule do
@typedoc "This type"
@@ -210,11 +199,11 @@ defmodule Module do
end
end
As can be seen in the example above, since Elixir 1.7.0 `@doc` and `@typedoc`
also accept a keyword list that serves as a way to provide arbitrary metadata
As can be seen in the example above, `@doc` and `@typedoc` also accept
a keyword list that serves as a way to provide arbitrary metadata
about the entity. Tools like [`ExDoc`](https://hexdocs.pm/ex_doc/) and
`IEx` may use this information to display annotations. A common use
case is the `:since` key, which may be used to annotate in which version the
case is `since` that may be used to annotate in which version the
function was introduced.
As illustrated in the example, it is possible to use these attributes
@@ -232,7 +221,8 @@ defmodule Module do
### `@dialyzer`
Defines warnings to request or suppress when using `:dialyzer`.
Defines warnings to request or suppress when using a version of
`:dialyzer` that supports module attributes.
Accepts an atom, a tuple, or a list of atoms and tuples. For example:
@@ -244,7 +234,8 @@ defmodule Module do
end
end
For the list of supported warnings, see [`:dialyzer` module](`:dialyzer`).
For the list of supported warnings, see
[`:dialyzer` module](`:dialyzer`).
Multiple uses of `@dialyzer` will accumulate instead of overriding
previous ones.
@@ -305,6 +296,156 @@ defmodule Module do
A hook that will be invoked when each function or macro in the current
module is defined. Useful when annotating functions.
Accepts a module or a `{module, function_name}` tuple. See the
"Compile callbacks" section below.
### `@on_load`
A hook that will be invoked whenever the module is loaded.
Accepts the function name (as an atom) of a function in the current module or
`{function_name, 0}` tuple where `function_name` is the name of a function in
the current module. The function must have an arity of 0 (no arguments). If
the function does not return `:ok`, the loading of the module will be aborted.
For example:
defmodule MyModule do
@on_load :load_check
def load_check do
if some_condition() do
:ok
else
:abort
end
end
def some_condition do
false
end
end
Modules compiled with HiPE would not call this hook.
### `@vsn`
Specify the module version. Accepts any valid Elixir value, for example:
defmodule MyModule do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `Kernel.defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
Elixir:
* `@type` - defines a type to be used in `@spec`
* `@typep` - defines a private type to be used in `@spec`
* `@opaque` - defines an opaque type to be used in `@spec`
* `@spec` - provides a specification for a function
* `@callback` - provides a specification for a behaviour callback
* `@macrocallback` - provides a specification for a macro behaviour callback
* `@optional_callbacks` - specifies which behaviour callbacks and macro
behaviour callbacks are optional
* `@impl` - declares an implementation of a callback function or macro
For detailed documentation, see the [typespec documentation](typespecs.md).
### Custom attributes
In addition to the built-in attributes outlined above, custom attributes may
also be added. Custom attributes are expressed using the `@/1` operator followed
by a valid variable name. The value given to the custom attribute must be a valid
Elixir value:
defmodule MyModule do
@custom_attr [some: "stuff"]
end
For more advanced options available when defining custom attributes, see
`register_attribute/3`.
## Compile callbacks
There are three callbacks that are invoked when functions are defined,
as well as before and immediately after the module bytecode is generated.
### `@after_compile`
A hook that will be invoked right after the current module is compiled.
Accepts a module or a `{module, function_name}` tuple. The function
must take two arguments: the module environment and its bytecode.
When just a module is provided, the function is assumed to be
`__after_compile__/2`.
Callbacks will run in the order they are registered.
#### Example
defmodule MyModule do
@after_compile __MODULE__
def __after_compile__(env, _bytecode) do
IO.inspect(env)
end
end
### `@before_compile`
A hook that will be invoked before the module is compiled.
Accepts a module or a `{module, function_or_macro_name}` tuple. The
function/macro must take one argument: the module environment. If
it's a macro, its returned value will be injected at the end of the
module definition before the compilation starts.
When just a module is provided, the function/macro is assumed to be
`__before_compile__/1`.
Callbacks will run in the order they are registered. Any overridable
definition will be made concrete before the first callback runs.
A definition may be made overridable again in another before compile
callback and it will be made concrete one last time after all callbacks
run.
*Note*: unlike `@after_compile`, the callback function/macro must
be placed in a separate module (because when the callback is invoked,
the current module does not yet exist).
#### Example
defmodule A do
defmacro __before_compile__(_env) do
quote do
def hello, do: "world"
end
end
end
defmodule B do
@before_compile A
end
B.hello()
#=> "world"
### `@on_definition`
A hook that will be invoked when each function or macro in the current
module is defined. Useful when annotating functions.
Accepts a module or a `{module, function_name}` tuple. The function
must take 6 arguments:
@@ -351,177 +492,6 @@ defmodule Module do
end
end
### `@on_load`
A hook that will be invoked whenever the module is loaded.
Accepts the function name (as an atom) of a function in the current module.
The function must have an arity of 0 (no arguments). If the function does
not return `:ok`, the loading of the module will be aborted.
For example:
defmodule MyModule do
@on_load :load_check
def load_check do
if some_condition() do
:ok
else
:abort
end
end
def some_condition do
false
end
end
### `@vsn`
Specify the module version. Accepts any valid Elixir value, for example:
defmodule MyModule do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
Elixir:
* `@type` - defines a type to be used in `@spec`
* `@typep` - defines a private type to be used in `@spec`
* `@opaque` - defines an opaque type to be used in `@spec`
* `@spec` - provides a specification for a function
* `@callback` - provides a specification for a behaviour callback
* `@macrocallback` - provides a specification for a macro behaviour callback
* `@optional_callbacks` - specifies which behaviour callbacks and macro
behaviour callbacks are optional
* `@impl` - declares an implementation of a callback function or macro
For detailed documentation, see the [typespec documentation](typespecs.md).
### Custom attributes
In addition to the built-in attributes outlined above, custom attributes may
also be added. Custom attributes are expressed using the `@/1` operator followed
by a valid variable name. The value given to the custom attribute must be a valid
Elixir value:
defmodule MyModule do
@custom_attr [some: "stuff"]
end
For more advanced options available when defining custom attributes, see
`register_attribute/3`.
## Compile callbacks
There are three compilation callbacks, invoked in this order:
`@before_compile`, `@after_compile`, and `@after_verify`.
They are described next.
### `@before_compile`
A hook that will be invoked before the module is compiled. This is
often used to change how the current module is being compiled.
Accepts a module or a `{module, function_or_macro_name}` tuple. The
function/macro must take one argument: the module environment. If
it's a macro, its returned value will be injected at the end of the
module definition before the compilation starts.
When just a module is provided, the function/macro is assumed to be
`__before_compile__/1`.
Callbacks will run in the order they are registered. Any overridable
definition will be made concrete before the first callback runs.
A definition may be made overridable again in another before compile
callback and it will be made concrete one last time after all callbacks
run.
*Note*: the callback function/macro must be placed in a separate module
(because when the callback is invoked, the current module does not yet exist).
#### Example
defmodule A do
defmacro __before_compile__(_env) do
quote do
def hello, do: "world"
end
end
end
defmodule B do
@before_compile A
end
B.hello()
#=> "world"
### `@after_compile`
A hook that will be invoked right after the current module is compiled.
Accepts a module or a `{module, function_name}` tuple. The function
must take two arguments: the module environment and its bytecode.
When just a module is provided, the function is assumed to be
`__after_compile__/2`.
Callbacks will run in the order they are registered.
`Module` functions expecting not yet compiled modules (such as `definitions_in/1`)
are still available at the time `@after_compile` is invoked.
#### Example
defmodule MyModule do
@after_compile __MODULE__
def __after_compile__(env, _bytecode) do
IO.inspect(env)
end
end
### `@after_verify`
A hook that will be invoked right after the current module is verified for
undefined functions, deprecations, etc. A module is always verified after
it is compiled. In Mix projects, a module is also verified when any of its
runtime dependencies change. Therefore this is useful to perform verification
of the current module while avoiding compile-time dependencies.
Accepts a module or a `{module, function_name}` tuple. The function
must take one argument: the module name. When just a module is provided,
the function is assumed to be `__after_verify__/2`.
Callbacks will run in the order they are registered.
`Module` functions expecting not yet compiled modules are no longer available
at the time `@after_verify` is invoked.
#### Example
defmodule MyModule do
@after_verify __MODULE__
def __after_verify__(module) do
IO.inspect(module)
:ok
end
end
## Compile options
The `@compile` attribute accepts different options that are used by both
@@ -532,11 +502,7 @@ defmodule Module do
corresponding setting in `Code.get_compiler_option/1`
* `@compile {:debug_info, false}` - disables `:debug_info` regardless
of the corresponding setting in `Code.get_compiler_option/1`. Note
disabling `:debug_info` is not recommended as it removes the ability
of the Elixir compiler and other tools to static analyse the code.
If you want to remove the `:debug_info` while deploying, tools like
`mix release` already do such by default.
of the corresponding setting in `Code.get_compiler_option/1`
* `@compile {:inline, some_fun: 2, other_fun: 3}` - inlines the given
name/arity pairs. Inlining is applied locally, calls from another
@@ -552,8 +518,8 @@ defmodule Module do
'''
@type definition :: {atom, arity}
@type def_kind :: :def | :defp | :defmacro | :defmacrop
@typep definition :: {atom, arity}
@typep def_kind :: :def | :defp | :defmacro | :defmacrop
@extra_error_msg_defines? "Use Kernel.function_exported?/3 and Kernel.macro_exported?/3 " <>
"to check for public functions and macros instead"
@@ -579,8 +545,6 @@ defmodule Module do
* `:module` - the module atom name
* `:struct` - if the module defines a struct and if so each field in order
"""
@callback __info__(:attributes) :: keyword()
@callback __info__(:compile) :: [term()]
@@ -588,7 +552,6 @@ defmodule Module do
@callback __info__(:macros) :: keyword()
@callback __info__(:md5) :: binary()
@callback __info__(:module) :: module()
@callback __info__(:struct) :: list(%{field: atom(), required: boolean()}) | nil
@doc """
Returns information about module attributes used by Elixir.
@@ -611,9 +574,6 @@ defmodule Module do
after_compile: %{
doc: "A hook that will be invoked right after the current module is compiled."
},
after_verify: %{
doc: "A hook that will be invoked right after the current module is verified."
},
before_compile: %{
doc: "A hook that will be invoked before the module is compiled."
},
@@ -682,6 +642,10 @@ defmodule Module do
derive: %{
doc:
"Derives an implementation for the given protocol for the struct defined in the current module."
},
enforce_keys: %{
doc:
"Ensures the given keys are always set when building the struct defined in the current module."
}
}
end
@@ -798,7 +762,7 @@ defmodule Module do
`Module.create/3` works similarly to `Kernel.defmodule/2`
and return the same results. While one could also use
`Kernel.defmodule/2` to define modules dynamically, this function
`defmodule` to define modules dynamically, this function
is preferred when the module body is given by a quoted
expression.
@@ -820,9 +784,9 @@ defmodule Module do
end
next = :elixir_module.next_counter(nil)
meta = Keyword.take(opts, [:line, :generated])
quoted = :elixir_quote.linify_with_context_counter(meta, {module, next}, quoted)
:elixir_module.compile(module, quoted, [], false, :elixir.env_for_eval(opts))
line = Keyword.get(opts, :line, 0)
quoted = :elixir_quote.linify_with_context_counter(line, {module, next}, quoted)
:elixir_module.compile(module, quoted, [], :elixir.env_for_eval(opts))
end
@doc """
@@ -963,10 +927,6 @@ defmodule Module do
simplify_arg(Macro.expand_once(attr, env), counters, env)
end
defp simplify_arg({:var!, _, [{var, _, atom} | _]}, counters, _env) when is_atom(atom) do
{simplify_var(var, Elixir), counters}
end
defp simplify_arg(other, counters, _env) when is_integer(other),
do: autogenerated_key(counters, :int)
@@ -1140,7 +1100,7 @@ defmodule Module do
"""
@doc since: "1.7.0"
@spec defines_type?(module, definition) :: boolean
def defines_type?(module, definition) when is_atom(module) do
def defines_type?(module, definition) do
Kernel.Typespec.defines_type?(module, definition)
end
@@ -1181,7 +1141,7 @@ defmodule Module do
def attributes_in(module) when is_atom(module) do
assert_not_compiled!(__ENV__.function, module)
{set, _} = data_tables_for(module)
:ets.select(set, [{{:"$1", :_, :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
:ets.select(set, [{{:"$1", :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
end
@doc """
@@ -1198,10 +1158,10 @@ defmodule Module do
def foo, do: 1
def bar, do: 2
defoverridable foo: 0, bar: 0
defoverridable foo: 1, bar: 1
def foo, do: 3
[bar: 0, foo: 0] = Module.overridables_in(__MODULE__) |> Enum.sort()
[:bar, :foo] = Module.overridables_in(__MODULE__) |> Enum.sort()
end
"""
@@ -1281,15 +1241,14 @@ defmodule Module do
## Options
* `:skip_clauses` (since v1.14.0) - returns `[]` instead
* `:nillify_clauses` (since v1.13.0) - returns `nil` instead
of returning the clauses. This is useful when there is
only an interest in fetching the kind and the metadata
only an interest in fetching the kind and metadata
"""
@spec get_definition(module, definition, keyword) ::
{:v1, def_kind, meta :: keyword,
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
| nil
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}] | nil}
@doc since: "1.12.0"
def get_definition(module, {name, arity}, options \\ [])
when is_atom(module) and is_atom(name) and is_integer(arity) and is_list(options) do
@@ -1299,8 +1258,8 @@ defmodule Module do
case :ets.lookup(set, {:def, {name, arity}}) do
[{_key, kind, meta, _, _, _}] ->
clauses =
if options[:skip_clauses],
do: [],
if options[:nillify_clauses],
do: nil,
else: bag_lookup_element(bag, {:clauses, {name, arity}}, 2)
{:v1, kind, meta, clauses}
@@ -1464,7 +1423,7 @@ defmodule Module do
"""
@spec put_attribute(module, atom, term) :: :ok
def put_attribute(module, key, value) when is_atom(module) and is_atom(key) do
__put_attribute__(module, key, value, nil, [])
__put_attribute__(module, key, value, nil)
end
@doc """
@@ -1506,7 +1465,7 @@ defmodule Module do
"""
@spec get_attribute(module, atom, term) :: term
def get_attribute(module, key, default \\ nil) when is_atom(module) and is_atom(key) do
case __get_attribute__(module, key, nil, true) do
case __get_attribute__(module, key, nil) do
nil -> default
value -> value
end
@@ -1549,14 +1508,9 @@ defmodule Module do
end
@doc """
Deletes the entry (or entries) for the given module attribute.
Deletes the module attribute that matches the given key.
It returns the deleted attribute value. If the attribute has not
been set nor configured to accumulate, it returns `nil`.
If the attribute is set to accumulate, then this function always
returns a list. Deleting the attribute removes existing entries
but the attribute will still accumulate.
It returns the deleted attribute value (or `nil` if nothing was set).
## Examples
@@ -1572,12 +1526,10 @@ defmodule Module do
{set, bag} = data_tables_for(module)
case :ets.lookup(set, key) do
[{_, _, :accumulate, traces}] ->
trace_attribute(true, module, traces, set, key, [])
[{_, _, :accumulate}] ->
reverse_values(:ets.take(bag, {:accumulate, key}), [])
[{_, value, _, traces}] ->
trace_attribute(module, traces)
[{_, value, _}] ->
:ets.delete(set, key)
value
@@ -1606,8 +1558,7 @@ defmodule Module do
* `:persist` - the attribute will be persisted in the Erlang
Abstract Format. Useful when interfacing with Erlang libraries.
By default, both options are `false`. Once an attribute has been
set to accumulate or persist, the behaviour cannot be reverted.
By default, both options are `false`.
## Examples
@@ -1631,11 +1582,11 @@ defmodule Module do
end
if Keyword.get(options, :accumulate) do
:ets.insert_new(set, {attribute, [], :accumulate, []}) ||
:ets.insert_new(set, {attribute, [], :accumulate}) ||
:ets.update_element(set, attribute, {3, :accumulate})
else
:ets.insert_new(bag, {:warn_attributes, attribute})
:ets.insert_new(set, {attribute, nil, :unset, []})
:ets.insert_new(set, {attribute, nil, :unset})
end
:ok
@@ -1716,7 +1667,7 @@ defmodule Module do
"#{kind} #{name}/#{arity} is private, " <>
"@doc attribute is always discarded for private functions/macros/types"
IO.warn(message, %{env | line: line})
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
end
end
@@ -1744,7 +1695,7 @@ defmodule Module do
def #{name}(...)
'''
IO.warn(message, %{env | line: line})
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
end
signature = merge_signatures(current_sign, signature, 1)
@@ -1766,14 +1717,14 @@ defmodule Module do
defp get_doc_meta(existing_meta, set) do
case :ets.take(set, {:doc, :meta}) do
[{{:doc, :meta}, metadata}] -> Map.merge(existing_meta, metadata)
[{{:doc, :meta}, metadata, _}] -> Map.merge(existing_meta, metadata)
[] -> existing_meta
end
end
defp compile_deprecated(doc_meta, set, bag, name, arity, defaults) do
case :ets.take(set, :deprecated) do
[{:deprecated, reason, _, _}] when is_binary(reason) ->
[{:deprecated, reason, _}] when is_binary(reason) ->
:ets.insert(bag, deprecated_reasons(defaults, name, arity, reason))
Map.put(doc_meta, :deprecated, reason)
@@ -1803,7 +1754,7 @@ defmodule Module do
%{line: line, file: file} = env
case :ets.take(set, :impl) do
[{:impl, value, _, _}] ->
[{:impl, value, _}] ->
impl = {{name, arity}, context, defaults, kind, line, file, value}
:ets.insert(bag, {:impls, impl})
value
@@ -1824,8 +1775,7 @@ defmodule Module do
defp args_count([], total, defaults), do: {total, defaults}
@doc false
def check_derive_behaviours_and_impls(env, set, bag, all_definitions) do
check_derive(env, set, bag)
def check_behaviours_and_impls(env, _set, bag, all_definitions) do
behaviours = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
impls = bag_lookup_element(bag, :impls, 2)
callbacks = check_behaviours(env, behaviours)
@@ -1843,25 +1793,6 @@ defmodule Module do
:ok
end
defp check_derive(env, set, bag) do
case bag_lookup_element(bag, {:accumulate, :derive}, 2) do
[] ->
:ok
_ ->
message =
case :ets.lookup(set, :__struct__) do
[] ->
"warning: module attribute @derive was set but never used (it must come before defstruct)"
_ ->
"warning: module attribute @derive was set after defstruct, all @derive calls must come before defstruct"
end
IO.warn(message, env)
end
end
defp check_behaviours(env, behaviours) do
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
cond do
@@ -1869,18 +1800,19 @@ defmodule Module do
message =
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
acc
not function_exported?(behaviour, :behaviour_info, 1) ->
message =
"module #{inspect(behaviour)} is not a behaviour (in module #{inspect(env.module)})"
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
acc
true ->
:elixir_env.trace({:require, [from_macro: true], behaviour, []}, env)
event = {:remote_function, [], behaviour, :behaviour_info, 1}
:elixir_env.trace(event, %{env | function: {:__info__, 1}})
optional_callbacks = behaviour_info(behaviour, :optional_callbacks)
callbacks = behaviour_info(behaviour, :callbacks)
Enum.reduce(callbacks, acc, &add_callback(&1, behaviour, env, optional_callbacks, &2))
@@ -1902,7 +1834,7 @@ defmodule Module do
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
end
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
%{} ->
:ok
@@ -1919,7 +1851,7 @@ defmodule Module do
format_callback(callback, kind, behaviour) <>
" is not implemented (in module #{inspect(env.module)})"
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
{_, wrong_kind, _, _} when kind != wrong_kind ->
message =
@@ -1927,7 +1859,7 @@ defmodule Module do
" was implemented as \"#{wrong_kind}\" but should have been \"#{kind}\" " <>
"(in module #{inspect(env.module)})"
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
_ ->
:ok
@@ -1963,7 +1895,7 @@ defmodule Module do
{:error, message} ->
formatted = format_impl_warning(fa, kind, message)
IO.warn(formatted, %{env | line: line, file: file})
IO.warn(formatted, Macro.Env.stacktrace(%{env | line: line, file: file}))
acc
end
end)
@@ -2079,7 +2011,7 @@ defmodule Module do
"This either means you forgot to add the \"@impl true\" annotation before the " <>
"definition or that you are accidentally overriding this callback"
IO.warn(message, %{env | line: :elixir_utils.get_line(meta)})
IO.warn(message, Macro.Env.stacktrace(%{env | line: :elixir_utils.get_line(meta)}))
end
end
@@ -2119,7 +2051,7 @@ defmodule Module do
@doc false
# Used internally by Kernel's @.
# This function is private and must be used only internally.
def __get_attribute__(module, key, caller_line, trace?) when is_atom(key) do
def __get_attribute__(module, key, line) when is_atom(key) do
assert_not_compiled!(
{:get_attribute, 2},
module,
@@ -2129,25 +2061,23 @@ defmodule Module do
{set, bag} = data_tables_for(module)
case :ets.lookup(set, key) do
[{_, _, :accumulate, traces}] ->
trace_attribute(trace?, module, traces, set, key, [])
[{_, _, :accumulate}] ->
:lists.reverse(bag_lookup_element(bag, {:accumulate, key}, 2))
[{_, value, warn_line, traces}] when is_integer(warn_line) ->
trace_attribute(trace?, module, traces, set, key, [{3, :used}])
value
[{_, val, line}] when is_integer(line) ->
:ets.update_element(set, key, {3, :used})
val
[{_, value, _, traces}] ->
trace_attribute(trace?, module, traces, set, key, [])
value
[{_, val, _}] ->
val
[] when is_integer(caller_line) ->
[] when is_integer(line) ->
# TODO: Consider raising instead of warning on v2.0 as it usually cascades
error_message =
"undefined module attribute @#{key}, " <>
"please remove access to @#{key} or explicitly set it before access"
IO.warn(error_message, attribute_stack(module, caller_line))
IO.warn(error_message, attribute_stack(module, line))
nil
[] ->
@@ -2155,90 +2085,25 @@ defmodule Module do
end
end
defp trace_attribute(module, traces) do
:lists.foreach(
fn {line, lexical_tracker, tracers, aliases} ->
env = %{
Macro.Env.__struct__()
| line: line,
lexical_tracker: lexical_tracker,
module: module,
tracers: tracers
}
:lists.foreach(
fn alias ->
:elixir_env.trace({:alias_reference, [line: line], alias}, env)
end,
aliases
)
end,
traces
)
end
defp trace_attribute(trace?, module, traces, set, key, updates) do
updates =
if trace? and traces != [] do
trace_attribute(module, traces)
updates ++ [{4, []}]
else
updates
end
case updates do
[] -> :ok
_ -> :ets.update_element(set, key, updates)
end
:ok
end
@doc false
# Used internally by Kernel's @.
# This function is private and must be used only internally.
def __put_attribute__(module, key, value, warn_line, traces) when is_atom(key) do
assert_not_readonly!({:put_attribute, 3}, module)
def __put_attribute__(module, key, value, line) when is_atom(key) do
assert_not_readonly!(__ENV__.function, module)
{set, bag} = data_tables_for(module)
put_attribute(module, key, value, warn_line, traces, set, bag)
value = preprocess_attribute(key, value)
put_attribute(module, key, value, line, set, bag)
:ok
end
defp put_attribute(_module, :on_load, value, warn_line, traces, set, bag) do
value =
case value do
_ when is_atom(value) ->
{value, 0}
{atom, 0} = tuple when is_atom(atom) ->
tuple
_ ->
raise ArgumentError,
"@on_load is a built-in module attribute that annotates a function to be invoked " <>
"when the module is loaded. It should be an atom or an {atom, 0} tuple, " <>
"got: #{inspect(value)}"
end
try do
:ets.lookup_element(set, :on_load, 3)
catch
:error, :badarg ->
:ets.insert(set, {:on_load, value, warn_line, traces})
:ets.insert(bag, {:warn_attributes, :on_load})
else
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
end
end
# If any of the doc attributes are called with a keyword list that
# will become documentation metadata. Multiple calls will be merged
# into the same map overriding duplicate keys.
defp put_attribute(module, key, {_, metadata}, warn_line, _traces, set, _bag)
defp put_attribute(module, key, {_, metadata}, line, set, _bag)
when key in [:doc, :typedoc, :moduledoc] and is_list(metadata) do
metadata_map = preprocess_doc_meta(metadata, module, warn_line, %{})
metadata_map = preprocess_doc_meta(metadata, module, line, %{})
case :ets.insert_new(set, {{key, :meta}, metadata_map}) do
case :ets.insert_new(set, {{key, :meta}, metadata_map, line}) do
true ->
:ok
@@ -2250,45 +2115,46 @@ defmodule Module do
# Optimize some attributes by avoiding writing to the attributes key
# in the bag table since we handle them internally.
defp put_attribute(module, key, value, warn_line, traces, set, _bag)
defp put_attribute(module, key, value, line, set, _bag)
when key in [:doc, :typedoc, :moduledoc, :impl, :deprecated] do
value = preprocess_attribute(key, value)
try do
:ets.lookup_element(set, key, 3)
catch
:error, :badarg -> :ok
else
unread_line when is_integer(warn_line) and is_integer(unread_line) ->
unread_line when is_integer(line) and is_integer(unread_line) ->
message = "redefining @#{key} attribute previously set at line #{unread_line}"
IO.warn(message, attribute_stack(module, warn_line))
IO.warn(message, attribute_stack(module, line))
_ ->
:ok
end
:ets.insert(set, {key, value, warn_line, traces})
:ets.insert(set, {key, value, line})
end
defp put_attribute(_module, key, value, warn_line, traces, set, bag) do
value = preprocess_attribute(key, value)
defp put_attribute(_module, :on_load, value, line, set, bag) do
try do
:ets.lookup_element(set, :on_load, 3)
catch
:error, :badarg ->
:ets.insert(set, {:on_load, value, line})
:ets.insert(bag, {:warn_attributes, :on_load})
else
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
end
end
defp put_attribute(_module, key, value, line, set, bag) do
try do
:ets.lookup_element(set, key, 3)
catch
:error, :badarg ->
:ets.insert(set, {key, value, warn_line, traces})
:ets.insert(set, {key, value, line})
:ets.insert(bag, {:warn_attributes, key})
else
:accumulate ->
if traces != [] do
:ets.update_element(set, key, {4, traces ++ :ets.lookup_element(set, key, 4)})
end
:ets.insert(bag, {{:accumulate, key}, value})
_ ->
:ets.insert(set, {key, value, warn_line, traces})
:accumulate -> :ets.insert(bag, {{:accumulate, key}, value})
_ -> :ets.insert(set, {key, value, line})
end
end
@@ -2304,6 +2170,9 @@ defmodule Module do
{line, doc} when is_integer(line) and (is_binary(doc) or doc == false or is_nil(doc)) ->
value
{line, [{key, _} | _]} when is_integer(line) and is_atom(key) ->
value
{line, doc} when is_integer(line) ->
raise ArgumentError,
"@#{key} is a built-in module attribute for documentation. It should be either " <>
@@ -2326,6 +2195,22 @@ defmodule Module do
end
end
defp preprocess_attribute(:on_load, value) do
case value do
_ when is_atom(value) ->
{value, 0}
{atom, 0} = tuple when is_atom(atom) ->
tuple
_ ->
raise ArgumentError,
"@on_load is a built-in module attribute that annotates a function to be invoked " <>
"when the module is loaded. It should be an atom or a {atom, 0} tuple, " <>
"got: #{inspect(value)}"
end
end
defp preprocess_attribute(:impl, value) do
if is_boolean(value) or (is_atom(value) and value != nil) do
value
@@ -2343,9 +2228,6 @@ defmodule Module do
defp preprocess_attribute(:after_compile, atom) when is_atom(atom),
do: {atom, :__after_compile__}
defp preprocess_attribute(:after_verify, atom) when is_atom(atom),
do: {atom, :__after_verify__}
defp preprocess_attribute(:on_definition, atom) when is_atom(atom),
do: {atom, :__on_definition__}
@@ -2466,7 +2348,7 @@ defmodule Module do
defp get_doc_info(table, env) do
case :ets.take(table, :doc) do
[{:doc, {_, _} = pair, _, _}] ->
[{:doc, {_, _} = pair, _}] ->
pair
[] ->
+87 -116
View File
@@ -27,21 +27,14 @@ defmodule Module.ParallelChecker do
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
{_, checker} = :erlang.get(:elixir_checker_info)
checker
end
@doc """
Stores the parallel checker information.
"""
def put(pid, checker) when is_pid(pid) and is_pid(checker) do
def put(pid, checker) do
:erlang.put(:elixir_checker_info, {pid, checker})
end
@@ -58,31 +51,21 @@ defmodule Module.ParallelChecker do
receive do
{^ref, :cache, ets} ->
module_map =
loaded_info =
if is_map(info) do
cache_from_module_map(ets, info)
info
else
case File.read(info) do
{:ok, binary} -> maybe_module_map(binary, module)
{:error, _} -> nil
end
info = File.read!(info)
cache_from_chunk(ets, module, info)
info
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
warnings = check_module(module, loaded_info, {checker, ets})
send(pid, {__MODULE__, module, warnings})
send(checker, {__MODULE__, :done})
end
@@ -92,8 +75,7 @@ defmodule Module.ParallelChecker do
end
end)
register(checker, spawned, ref)
:ok
{spawned, ref}
end
@doc """
@@ -104,33 +86,28 @@ defmodule Module.ParallelChecker do
def verify(fun) do
case :erlang.get(:elixir_compiler_info) do
:undefined ->
previous = :erlang.put(:elixir_checker_info, {self(), nil})
previous = :erlang.get(:elixir_checker_info)
{:ok, checker} = start_link()
put(self(), checker)
try do
result = fun.()
case :erlang.get(:elixir_checker_info) do
{_, nil} -> :ok
{_, checker} -> verify(checker, [])
end
{result, compile_info} = Enum.unzip(fun.())
_ = verify(checker, compile_info, [])
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)
stop(checker)
end
_ ->
# If we are during compilation, then they will be
# reported to the compiler, which will validate them.
fun.()
Enum.map(fun.(), &elem(&1, 0))
end
end
@@ -139,13 +116,15 @@ defmodule Module.ParallelChecker do
the modules and adds the ExCk chunk to the binaries. Returns the updated
list of warnings from the verification.
"""
@spec verify(pid(), [{module(), Path.t()}]) :: [warning()]
def verify(checker, runtime_files) do
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
@spec verify(pid(), [{pid(), reference()}], [{module(), binary()}]) :: [warning()]
def verify(checker, compiled_info, runtime_files) do
runtime_info =
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
modules = :gen_server.call(checker, :start, :infinity)
modules = compiled_info ++ runtime_info
:gen_server.cast(checker, {:start, modules})
collect_results(modules, [])
end
@@ -153,16 +132,10 @@ defmodule Module.ParallelChecker do
warnings
end
defp collect_results(modules, warnings) do
defp collect_results([_ | modules], 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(modules, new_warnings ++ warnings)
end
end
@@ -171,7 +144,7 @@ defmodule Module.ParallelChecker do
"""
def test_cache do
{:ok, checker} = start_link()
{checker, :gen_server.call(checker, :ets, :infinity)}
{checker, :gen_server.call(checker, :ets)}
end
@doc """
@@ -222,26 +195,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, info, cache) do
case extract_definitions(module, info) 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
@@ -254,8 +236,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
@@ -326,8 +311,14 @@ defmodule Module.ParallelChecker do
end
defp cache_from_chunk(ets, module) do
with {^module, binary, _filename} <- :code.get_object_code(module),
{:ok, {^module, [{~c"ExCk", chunk}]}} <- :beam_lib.chunks(binary, [~c"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
@@ -336,6 +327,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)
@@ -366,28 +367,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})
@@ -435,11 +414,7 @@ defmodule Module.ParallelChecker do
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})
:gen_server.call(server, {:unlock, module})
end
## Server callbacks
@@ -458,20 +433,6 @@ defmodule Module.ParallelChecker do
{: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
@@ -504,8 +465,18 @@ defmodule Module.ParallelChecker do
{:stop, :normal, state}
end
def handle_cast({:register, pid, ref}, %{modules: modules} = state) do
{:noreply, %{state | modules: [{pid, ref} | modules]}}
def handle_cast({:start, modules}, %{ets: ets} = state) do
for {pid, ref} <- modules do
send(pid, {ref, :cache, ets})
end
for {_pid, ref} <- modules do
receive do
{^ref, :cached} -> :ok
end
end
{:noreply, run_checkers(%{state | modules: modules})}
end
defp run_checkers(%{modules: []} = state) do
+12 -16
View File
@@ -135,7 +135,8 @@ defmodule Module.Types do
# 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
location = {context.file, get_line(stack), {context.module, fun, arity}}
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)
@@ -145,7 +146,8 @@ defmodule Module.Types do
def error_to_warning(:unable_unify, {left, right, stack}, context) do
{fun, arity} = context.function
location = {context.file, get_line(stack), {context.module, fun, arity}}
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)
@@ -153,8 +155,6 @@ defmodule Module.Types do
{Module.Types, error, location}
end
defp get_line(stack), do: stack.last_expr |> get_meta() |> Keyword.get(:line, 0)
# Collect relevant traces from context.traces using stack.unify_stack
defp type_traces(stack, context) do
# TODO: Do we need the unify_stack or is enough to only get the last variable
@@ -211,21 +211,17 @@ defmodule Module.Types do
## 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)
{module, function, arity} = mfa
mfa_args = Macro.generate_arguments(arity, __MODULE__)
{module, function, ^arity} = call_to_mfa(erl_to_ex(module, function, mfa_args, []))
format_mfa = Exception.format_mfa(module, function, arity)
{traces, [] = _hints} = format_traces(traces, [], false)
clauses =
Enum.map(signature, 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)
Enum.map(
signature,
&String.slice(IO.iodata_to_binary(Unify.format_type({:fun, [&1]}, false)), 1..-2)
)
[
"expected #{format_mfa} to have signature:\n\n ",
@@ -252,7 +248,7 @@ defmodule Module.Types do
"undefined field \"#{atom}\" ",
format_expr(expr, location),
"expected one of the following fields: ",
Enum.join(Enum.sort(known_atoms), ", "),
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
"\n\n",
traces,
format_message_hints(hints),
+9 -37
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
+1 -1
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)
+26 -6
View File
@@ -128,10 +128,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"
@@ -359,11 +379,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
-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`.
+2 -7
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
-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
+14 -97
View File
@@ -3,25 +3,21 @@ 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). If a binary
is given, in whatever encoding, its encoding will be kept.
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 +148,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 +367,6 @@ defmodule Path do
iex> Path.basename("foo/bar")
"bar"
iex> Path.basename("lib/module/submodule.ex")
"submodule.ex"
iex> Path.basename("/")
""
@@ -435,10 +428,10 @@ defmodule Path do
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
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).
".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.
@@ -500,8 +493,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 +532,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 +565,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 +582,7 @@ defmodule Path do
"""
@spec split(t) :: [binary]
def split(path) do
:filename.split(IO.chardata_to_string(path))
end
@@ -603,10 +594,6 @@ defmodule Path do
:file.read_link_info(file)
end
def read_file_info(file) do
:file.read_file_info(file)
end
def list_dir(dir) do
case :file.list_dir(dir) do
{:ok, files} -> {:ok, for(file <- files, hd(file) != ?., do: file)}
@@ -671,7 +658,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
@@ -734,74 +721,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
+6 -6
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()`,
@@ -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 -8
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 """
@@ -650,7 +650,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 +749,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 +770,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 +788,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
+17 -48
View File
@@ -244,16 +244,8 @@ defmodule Protocol do
...
end
If you are using `Mix.install/2`, you can do by passing the `consolidate_protocols`
option:
Mix.install(
deps,
consolidate_protocols: false
)
Although doing so is not recommended as it may affect the performance of
your code.
Although doing so is not recommended as it may affect your test suite
performance.
Finally, note all protocols are compiled with `debug_info` set to `true`,
regardless of the option set by the `elixirc` compiler. The debug info is
@@ -755,7 +747,7 @@ defmodule Protocol do
do: :maps.get(fa, metas, [])[:line]
defp warn(message, env, nil) do
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
end
defp warn(message, env, line) when is_integer(line) do
@@ -763,20 +755,13 @@ defmodule Protocol do
IO.warn(message, stacktrace)
end
# TODO: Convert the following warnings into errors future Elixir versions
def __before_compile__(env) do
# Callbacks
callback_metas = callback_metas(env.module, :callback)
callbacks = :maps.keys(callback_metas)
functions = Module.get_attribute(env.module, :__functions__)
if functions == [] do
warn(
"protocols must define at least one function, but none was defined",
env,
nil
)
end
# TODO: Convert the following warnings into errors in future Elixir versions
:lists.map(
fn {name, arity} = fa ->
warn(
@@ -806,7 +791,7 @@ defmodule Protocol do
# Optional Callbacks
optional_callbacks = Module.get_attribute(env.module, :optional_callbacks)
if optional_callbacks != [] do
if length(optional_callbacks) > 0 do
warn(
"cannot define @optional_callbacks inside protocol, all of the protocol definitions are required",
env,
@@ -922,41 +907,26 @@ defmodule Protocol do
do: [:lists.foldl(&{:|, [], [&1, &2]}, head, tail), quote(do: ...)]
@doc false
def __impl__(protocol, opts, do_block, env) do
opts = Keyword.merge(opts, do_block)
{for, opts} =
Keyword.pop_lazy(opts, :for, fn ->
env.module ||
raise ArgumentError, "defimpl/3 expects a :for option when declared outside a module"
end)
for =
Macro.expand_literals(for, %{env | module: env.module || Elixir, function: {:__impl__, 1}})
case opts do
[] -> raise ArgumentError, "defimpl expects a do-end block"
[do: block] -> __impl__(protocol, for, block)
_ -> raise ArgumentError, "unknown options given to defimpl, got: #{Macro.to_string(opts)}"
end
def __impl__(protocol, opts) do
do_defimpl(protocol, :lists.keysort(1, opts))
end
defp __impl__(protocol, for, block) when is_list(for) do
for f <- for, do: __impl__(protocol, f, block)
defp do_defimpl(protocol, do: block, for: for) when is_list(for) do
for f <- for, do: do_defimpl(protocol, do: block, for: f)
end
defp __impl__(protocol, for, block) do
defp do_defimpl(protocol, do: block, for: for) do
# Unquote the implementation just later
# when all variables will already be injected
# into the module body.
impl =
quote unquote: false do
@doc false
@spec __impl__(:target) :: __MODULE__
@spec __impl__(:for) :: unquote(for)
@spec __impl__(:target) :: __MODULE__
@spec __impl__(:protocol) :: unquote(protocol)
def __impl__(:target), do: __MODULE__
def __impl__(:for), do: unquote(for)
def __impl__(:target), do: __MODULE__
def __impl__(:protocol), do: unquote(protocol)
end
@@ -973,12 +943,12 @@ defmodule Protocol do
@protocol protocol
@for for
res = unquote(block)
unquote(block)
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: @protocol, for: @for]
unquote(impl)
res
end
end
end
@@ -1036,15 +1006,14 @@ defmodule Protocol do
@doc false
def __ensure_defimpl__(protocol, for, env) do
if not Code.get_compiler_option(:ignore_already_consolidated) and
Protocol.consolidated?(protocol) do
if Protocol.consolidated?(protocol) do
message =
"the #{inspect(protocol)} protocol has already been consolidated, an " <>
"implementation for #{inspect(for)} has no effect. If you want to " <>
"implement protocols after compilation or during tests, check the " <>
"\"Consolidation\" section in the Protocol module documentation"
IO.warn(message, env)
IO.warn(message, Macro.Env.stacktrace(env))
end
:ok
+51 -139
View File
@@ -3,55 +3,17 @@ defmodule Range do
Ranges represent a sequence of zero, one or many, ascending
or descending integers with a common difference called step.
Ranges are always inclusive and they may have custom steps.
The most common form of creating and matching on ranges is
via the [`first..last`](`../2`) and [`first..last//step`](`..///3`)
notations, auto-imported from `Kernel`:
iex> 1 in 1..10
true
iex> 5 in 1..10
true
iex> 10 in 1..10
true
Ranges are always inclusive in Elixir. When a step is defined,
integers will only belong to the range if they match the step:
iex> 5 in 1..10//2
true
iex> 4 in 1..10//2
false
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `last >= first`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicit decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
`../0` can also be used as a shortcut to create the range `0..-1//1`,
also known as the full-slice range:
iex> ..
0..-1//1
## Use cases
Ranges typically have two uses in Elixir: as a collection or
to represent a slice of another data structure.
### Ranges as collections
Ranges in Elixir are enumerables and therefore can be used
with the `Enum` module:
iex> Enum.to_list(1..3)
[1, 2, 3]
iex> Enum.to_list(1..3//2)
[1, 3]
iex> Enum.to_list(3..1//-1)
[3, 2, 1]
iex> Enum.to_list(1..5//2)
[1, 3, 5]
Ranges may also have a single element:
@@ -67,54 +29,27 @@ defmodule Range do
iex> Enum.to_list(0..10//-1)
[]
The full-slice range, returned by `../0`, is an empty collection:
iex> Enum.to_list(..)
[]
### Ranges as slices
Ranges are also frequently used to slice collections.
You can slice strings or any enumerable:
iex> String.slice("elixir", 1..4)
"lixi"
iex> Enum.slice([0, 1, 2, 3, 4, 5], 1..4)
[1, 2, 3, 4]
In those cases, the first and last values of the range
are mapped to positions in the collections.
If a negative number is given, it maps to a position
from the back:
iex> String.slice("elixir", 1..-2//1)
"lixi"
iex> Enum.slice([0, 1, 2, 3, 4, 5], 1..-2//1)
[1, 2, 3, 4]
The range `0..-1//1`, returned by `../0`, returns the
collection as is, which is why it is called the full-slice
range:
iex> String.slice("elixir", ..)
"elixir"
iex> Enum.slice([0, 1, 2, 3, 4, 5], ..)
[0, 1, 2, 3, 4, 5]
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `first >= last`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicitly decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
## Definition
An increasing range `first..last//step` is a range from `first`
to `last` increasing by `step` where `step` must be a positive
integer and all values `v` must be `first <= v and v <= last`.
Therefore, a range `10..0//1` is an empty range because there
is no value `v` that is `10 <= v and v <= 0`.
An increasing range `first..last//step` is a range from
`first` to `last` increasing by `step` where `step` must be a positive
integer and all values `v` must be `first <= v and v <= last`. Therefore, a range
`10..0//1` is an empty range because there is no value `v`
that is `10 <= v and v <= 0`.
Similarly, a decreasing range `first..last//step` is a range
from `first` to `last` decreasing by `step` where `step` must
be a negative integer and values `v` must be `first >= v and v >= last`.
Therefore, a range `0..10//-1` is an empty range because there
is no value `v` that is `0 >= v and v >= 10`.
from `first` to `last` decreasing by `step` where `step` must be a negative
integer and values `v` must be `first >= v and v >= last`. Therefore, a range
`0..10//-1` is an empty range because there is no value `v`
that is `0 >= v and v >= 10`.
## Representation
@@ -136,8 +71,9 @@ defmodule Range do
directly but you should not modify nor create ranges by hand.
Instead use the proper operators or `new/2` and `new/3`.
Ranges implement the `Enumerable` protocol with memory
efficient versions of all `Enumerable` callbacks:
A range implements the `Enumerable` protocol, which means
functions in the `Enum` module can be used to work with
ranges:
iex> range = 1..10
1..10
@@ -255,26 +191,6 @@ defmodule Range do
size(Map.put(range, :step, step))
end
@doc """
Shifts a range by the given number of steps.
## Examples
iex> Range.shift(0..10, 1)
1..11
iex> Range.shift(0..10, 2)
2..12
iex> Range.shift(0..10//2, 2)
4..14//2
"""
@doc since: "1.14.0"
def shift(first..last//step, steps_to_shift)
when is_integer(first) and is_integer(last) and is_integer(step) and
is_integer(steps_to_shift) do
new(first + steps_to_shift * step, last + steps_to_shift * step, step)
end
@doc """
Checks if two ranges are disjoint.
@@ -294,24 +210,22 @@ defmodule Range do
iex> Range.disjoint?(1..10//2, 2..10//2)
true
# First element in common is 29
# First element in common in all below is 29
iex> Range.disjoint?(2..100//3, 9..100//5)
false
iex> Range.disjoint?(101..2//-3, 99..9//-5)
false
iex> Range.disjoint?(1..100//14, 8..100//21)
false
iex> Range.disjoint?(57..-1//-14, 8..100//21)
false
iex> Range.disjoint?(1..100//14, 50..8//-21)
iex> Range.disjoint?(1..100//14, 51..8//-21)
false
# If 29 is out of range
iex> Range.disjoint?(1..28//14, 8..28//21)
true
# First element in common is 14
iex> Range.disjoint?(2..28//3, 9..28//5)
false
iex> Range.disjoint?(26..2//-3, 29..9//-5)
false
# Starting from the back without alignment
iex> Range.disjoint?(27..11//-3, 30..0//-7)
true
"""
@@ -336,28 +250,21 @@ defmodule Range do
# progressions and see if they belong within the ranges
# https://math.stackexchange.com/questions/1656120/formula-to-find-the-first-intersection-of-two-arithmetic-progressions
{gcd, u, v} = Integer.extended_gcd(-step1, step2)
c = first1 - first2 + step2 - step1
t1 = -c / step1 * u
t2 = -c / step2 * v
t = max(floor(t1) + 1, floor(t2) + 1)
x = div(c * u + t * step2, gcd) - 1
y = div(c * v + t * step1, gcd) - 1
if rem(first2 - first1, gcd) == 0 do
c = first1 - first2 + step2 - step1
t1 = -c / step2 * u
t2 = -c / step1 * v
t = max(floor(t1) + 1, floor(t2) + 1)
x = div(c * u + t * step2, gcd) - 1
y = div(c * v + t * step1, gcd) - 1
x < 0 or first1 + x * step1 > last1 or
y < 0 or first2 + y * step2 > last2
else
true
end
x < 0 or first1 + x * step1 > last1 or
y < 0 or first2 + y * step2 > last2
end
end
end
@compile inline: [normalize: 3]
defp normalize(first, last, step) when first > last,
do: {first - abs(div(first - last, step) * step), first, -step}
defp normalize(first, last, step) when first > last, do: {last, first, -step}
defp normalize(first, last, step), do: {first, last, step}
@doc false
@@ -396,11 +303,16 @@ defimpl Enumerable, for: Range do
{:done, acc}
end
def member?(first..last//step, value) when is_integer(value) do
if step > 0 do
{:ok, first <= value and value <= last and rem(value - first, step) == 0}
else
{:ok, last <= value and value <= first and rem(value - first, step) == 0}
def member?(first..last//step = range, value) when is_integer(value) do
cond do
Range.size(range) == 0 ->
{:ok, false}
first <= last ->
{:ok, first <= value and value <= last and rem(value - first, step) == 0}
true ->
{:ok, last <= value and value <= first and rem(value - first, step) == 0}
end
end
@@ -420,7 +332,7 @@ defimpl Enumerable, for: Range do
end
def slice(first.._//step = range) do
{:ok, Range.size(range), &slice(first + &1 * step, step + &3 - 1, &2)}
{:ok, Range.size(range), &slice(first + &1 * step, step, &2)}
end
# TODO: Remove me on v2.0
@@ -429,7 +341,7 @@ defimpl Enumerable, for: Range do
slice(Map.put(range, :step, step))
end
defp slice(_current, _step, 0), do: []
defp slice(current, _step, 1), do: [current]
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
end
+1 -1
View File
@@ -434,7 +434,7 @@ defmodule Record do
if Keyword.has_key?(keyword, :_) do
message = "updating a record with a default (:_) is equivalent to creating a new record"
IO.warn(message, caller)
IO.warn(message, Macro.Env.stacktrace(caller))
create(tag, fields, keyword, caller)
else
updates =
+19 -36
View File
@@ -7,7 +7,7 @@ defmodule Regex do
in the [`:re` module documentation](`:re`).
Regular expressions in Elixir can be created using the sigils
`~r` (see `sigil_r/2`) or `~R` (see `sigil_R/2`):
`~r` (see `Kernel.sigil_r/2`) or `~R` (see `Kernel.sigil_R/2`):
# A simple regular expression that matches foo anywhere in the string
~r/foo/
@@ -38,35 +38,35 @@ defmodule Regex do
The modifiers available when creating a Regex are:
* `:unicode` (u) - enables Unicode specific patterns like `\p` and causes
character classes like `\w`, `\W`, `\s`, and the like to also match on Unicode
* `unicode` (u) - enables Unicode specific patterns like `\p` and causes
character classes like `\w`, `\W`, `\s`, etc. to also match on Unicode
(see examples below in "Character classes"). It expects valid Unicode
strings to be given on match
* `:caseless` (i) - adds case insensitivity
* `caseless` (i) - adds case insensitivity
* `:dotall` (s) - causes dot to match newlines and also set newline to
* `dotall` (s) - causes dot to match newlines and also set newline to
anycrlf; the new line setting can be overridden by setting `(*CR)` or
`(*LF)` or `(*CRLF)` or `(*ANY)` according to `:re` documentation
* `:multiline` (m) - causes `^` and `$` to mark the beginning and end of
* `multiline` (m) - causes `^` and `$` to mark the beginning and end of
each line; use `\A` and `\z` to match the end or beginning of the string
* `:extended` (x) - whitespace characters are ignored except when escaped
* `extended` (x) - whitespace characters are ignored except when escaped
and allow `#` to delimit comments
* `:firstline` (f) - forces the unanchored pattern to match before or at the
* `firstline` (f) - forces the unanchored pattern to match before or at the
first newline, though the matched text may continue over the newline
* `:ungreedy` (U) - inverts the "greediness" of the regexp
* `ungreedy` (U) - inverts the "greediness" of the regexp
(the previous `r` option is deprecated in favor of `U`)
The options not available are:
* `:anchored` - not available, use `^` or `\A` instead
* `:dollar_endonly` - not available, use `\z` instead
* `:no_auto_capture` - not available, use `?:` instead
* `:newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or
* `anchored` - not available, use `^` or `\A` instead
* `dollar_endonly` - not available, use `\z` instead
* `no_auto_capture` - not available, use `?:` instead
* `newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or
`(*ANYCRLF)` or `(*ANY)` at the beginning of the regexp according to the
`:re` documentation
@@ -155,7 +155,7 @@ defmodule Regex do
defstruct re_pattern: nil, source: "", opts: "", re_version: ""
@type t :: %__MODULE__{re_pattern: term, source: binary, opts: binary | [term]}
@type t :: %__MODULE__{re_pattern: term, source: binary, opts: binary}
defmodule CompileError do
defexception message: "regex could not be compiled"
@@ -166,8 +166,8 @@ defmodule Regex do
The given options can either be a binary with the characters
representing the same regex options given to the
`~r` (see `sigil_r/2`) sigil, or a list of options, as
expected by the Erlang's [`:re`](`:re`) module.
`~r` (see `Kernel.sigil_r/2`) sigil, or a list of options, as
expected by the Erlang's `:re` module.
It returns `{:ok, regex}` in case of success,
`{:error, reason}` otherwise.
@@ -180,12 +180,6 @@ defmodule Regex do
iex> Regex.compile("*foo")
{:error, {'nothing to repeat', 0}}
iex> Regex.compile("foo", "i")
{:ok, ~r/foo/i}
iex> Regex.compile("foo", [:caseless])
{:ok, Regex.compile!("foo", [:caseless])}
"""
@spec compile(binary, binary | [term]) :: {:ok, t} | {:error, any}
def compile(source, options \\ "") when is_binary(source) do
@@ -209,7 +203,6 @@ defmodule Regex do
defp compile(source, opts, doc_opts, version) do
case :re.compile(source, opts) do
{:ok, re_pattern} ->
doc_opts = format_doc_opts(doc_opts, opts)
{:ok, %Regex{re_pattern: re_pattern, re_version: version, source: source, opts: doc_opts}}
error ->
@@ -217,10 +210,6 @@ defmodule Regex do
end
end
defp format_doc_opts(_doc_opts = "", _opts = []), do: ""
defp format_doc_opts(_doc_opts = "", opts), do: opts
defp format_doc_opts(doc_opts, _opts), do: doc_opts
@doc """
Compiles the regular expression and raises `Regex.CompileError` in case of errors.
"""
@@ -285,7 +274,7 @@ defmodule Regex do
iex> Regex.match?(~r/foo/, "bar")
false
Elixir also provides text-based match operator `=~/2` and function `String.match?/2` as
Elixir also provides `Kernel.=~/2` and `String.match?/2` as
an alternative to test strings against regular expressions and
strings.
"""
@@ -395,21 +384,15 @@ defmodule Regex do
end
@doc """
Returns the regex options, as a string or list depending on how
it was compiled.
See the documentation of `Regex.compile/2` for more information.
Returns the regex options as a string.
## Examples
iex> Regex.opts(~r/foo/m)
"m"
iex> Regex.opts(Regex.compile!("foo", [:caseless]))
[:caseless]
"""
@spec opts(t) :: String.t() | [term]
@spec opts(t) :: String.t()
def opts(%Regex{opts: opts}) do
opts
end
+16 -51
View File
@@ -47,9 +47,9 @@ defmodule Registry do
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
name = {:via, Registry, {Registry.ViaTest, "agent", :hello}}
{:ok, agent_pid} = Agent.start_link(fn -> 0 end, name: name)
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
Registry.lookup(Registry.ViaTest, "agent")
#=> [{agent_pid, :hello}]
#=> [{self(), :hello}]
To this point, we have been starting `Registry` using `start_link/1`.
Typically the registry is started as part of a supervision tree though:
@@ -165,7 +165,7 @@ defmodule Registry do
the value from the registry and sending it a message. Many parts of the standard
library are designed to cope with that, such as `Process.monitor/1` which will
deliver the `:DOWN` message immediately if the monitored process is already dead
and `send/2` which acts as a no-op for dead processes.
and `Kernel.send/2` which acts as a no-op for dead processes.
## ETS
@@ -1278,7 +1278,7 @@ defmodule Registry do
## Examples
This example shows how to get everything from the registry:
This example shows how to get everything from the registry.
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
@@ -1286,7 +1286,7 @@ defmodule Registry do
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
[{"world", self(), :value}, {"hello", self(), :value}]
Get all keys in the registry:
Get all keys in the registry.
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
@@ -1299,7 +1299,17 @@ defmodule Registry do
@spec select(registry, spec) :: [term]
def select(registry, spec)
when is_atom(registry) and is_list(spec) do
spec = group_match_headers(spec, __ENV__.function)
spec =
for part <- spec do
case part do
{{key, pid, value}, guards, select} ->
{{key, {pid, value}}, guards, select}
_ ->
raise ArgumentError,
"invalid match specification in Registry.select/2: #{inspect(spec)}"
end
end
case key_info!(registry) do
{_kind, partitions, nil} ->
@@ -1312,51 +1322,6 @@ defmodule Registry do
end
end
@doc """
Works like `select/2`, but only returns the number of matching records.
## Examples
In the example below we register the current process under different
keys in a unique registry but with the same value:
iex> Registry.start_link(keys: :unique, name: Registry.CountSelectTest)
iex> {:ok, _} = Registry.register(Registry.CountSelectTest, "hello", :value)
iex> {:ok, _} = Registry.register(Registry.CountSelectTest, "world", :value)
iex> Registry.count_select(Registry.CountSelectTest, [{{:_, :_, :value}, [], [true]}])
2
"""
@doc since: "1.14.0"
@spec count_select(registry, spec) :: non_neg_integer()
def count_select(registry, spec)
when is_atom(registry) and is_list(spec) do
spec = group_match_headers(spec, __ENV__.function)
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
count = :ets.select_count(key_ets!(registry, partition_index), spec)
acc + count
end)
{_kind, 1, key_ets} ->
:ets.select_count(key_ets, spec)
end
end
defp group_match_headers(spec, {fun, arity}) do
for part <- spec do
case part do
{{key, pid, value}, guards, select} ->
{{key, {pid, value}}, guards, select}
_ ->
raise ArgumentError,
"invalid match specification in Registry.#{fun}/#{arity}: #{inspect(spec)}"
end
end
end
## Helpers
@compile {:inline, hash: 2}
+46 -140
View File
@@ -186,9 +186,6 @@ defmodule Stream do
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list()
[[1, 2, 3], [4, 5, 6]]
iex> Stream.chunk_every([1, 2, 3, 4], 3, 3, Stream.cycle([0])) |> Enum.to_list()
[[1, 2, 3], [4, 0, 0]]
"""
@doc since: "1.5.0"
@spec chunk_every(Enumerable.t(), pos_integer, pos_integer, Enumerable.t() | :discard) ::
@@ -420,47 +417,10 @@ defmodule Stream do
lazy(enum, true, fn f1 -> R.drop_while(fun, f1) end)
end
@doc """
Duplicates the given element `n` times in a stream.
`n` is an integer greater than or equal to `0`.
If `n` is `0`, an empty stream is returned.
## Examples
iex> stream = Stream.duplicate("hello", 0)
iex> Enum.to_list(stream)
[]
iex> stream = Stream.duplicate("hi", 1)
iex> Enum.to_list(stream)
["hi"]
iex> stream = Stream.duplicate("bye", 2)
iex> Enum.to_list(stream)
["bye", "bye"]
iex> stream = Stream.duplicate([1, 2], 3)
iex> Enum.to_list(stream)
[[1, 2], [1, 2], [1, 2]]
"""
@doc since: "1.14.0"
@spec duplicate(any, non_neg_integer) :: Enumerable.t()
def duplicate(value, n) when is_integer(n) and n >= 0 do
unfold(n, fn
0 -> nil
remaining -> {value, remaining - 1}
end)
end
@doc """
Executes the given function for each element.
The values in the stream do not change, therefore this
function is useful for adding side effects (like printing)
to a stream. See `map/2` if producing a different stream
is desired.
Useful for adding side effects (like printing) to a stream.
## Examples
@@ -840,10 +800,10 @@ defmodule Stream do
@doc """
Transforms an existing stream.
It expects an accumulator and a function that receives two arguments,
the stream element and the updated accumulator. It must return a tuple,
where the first element is a new stream (often a list) or the atom `:halt`,
and the second element is the accumulator to be used by the next element.
It expects an accumulator and a function that receives each stream element
and an accumulator. It must return a tuple, where the first element is a new
stream (often a list) or the atom `:halt`, and the second element is the
accumulator to be used by the next element, if any, in both cases.
Note: this function is equivalent to `Enum.flat_map_reduce/3`, except this
function does not return the accumulator once the stream is processed.
@@ -862,72 +822,44 @@ defmodule Stream do
iex> Enum.to_list(stream)
[1001, 1002, 1003]
`Stream.transform/5` further generalizes this function to allow wrapping
around resources.
"""
@spec transform(Enumerable.t(), acc, fun) :: Enumerable.t()
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
acc: any
def transform(enum, acc, reducer) when is_function(reducer, 2) do
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil, fn acc -> acc end)
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil)
end
@doc """
Similar to `Stream.transform/5`, except `last_fun` is not supplied.
Transforms an existing stream with function-based start and finish.
The accumulator is only calculated when transformation starts. It also
allows an after function to be given which is invoked when the stream
halts or completes.
This function can be seen as a combination of `Stream.resource/3` with
`Stream.transform/3`.
"""
@spec transform(Enumerable.t(), start_fun, reducer, after_fun) :: Enumerable.t()
when start_fun: (() -> acc),
reducer: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
after_fun: (acc -> term),
@spec transform(Enumerable.t(), (() -> acc), fun, (acc -> term)) :: Enumerable.t()
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
acc: any
def transform(enum, start_fun, reducer, after_fun)
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(after_fun, 1) do
&do_transform(enum, start_fun, reducer, &1, &2, nil, after_fun)
&do_transform(enum, start_fun, reducer, &1, &2, after_fun)
end
@doc """
Transforms an existing stream with function-based start, last, and after
callbacks.
Once transformation starts, `start_fun` is invoked to compute the initial
accumulator. Then, for each element in the enumerable, the `reducer` function
is invoked with the element and the accumulator, returning new elements and a
new accumulator, as in `transform/3`.
Once the collection is done, `last_fun` is invoked with the accumulator to
emit any remaining items. Then `after_fun` is invoked, to close any resource,
but not emitting any new items. `last_fun` is only invoked if the given
enumerable terminates successfully (either because it is done or it halted
itself). `after_fun` is always invoked, therefore `after_fun` must be the
one used for closing resources.
"""
@spec transform(Enumerable.t(), start_fun, reducer, last_fun, after_fun) :: Enumerable.t()
when start_fun: (() -> acc),
reducer: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
last_fun: (acc -> {Enumerable.t(), acc} | {:halt, acc}),
after_fun: (acc -> term),
acc: any
def transform(enum, start_fun, reducer, last_fun, after_fun)
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(last_fun, 1) and
is_function(after_fun, 1) do
&do_transform(enum, start_fun, reducer, &1, &2, last_fun, after_fun)
end
defp do_transform(enumerables, user_acc, user, inner_acc, fun, last_fun, after_fun) do
defp do_transform(enumerables, user_acc, user, inner_acc, fun, after_fun) do
inner = &do_transform_each(&1, &2, fun)
step = &do_transform_step(&1, &2)
next = &Enumerable.reduce(enumerables, &1, step)
funs = {user, fun, inner, last_fun, after_fun}
funs = {user, fun, inner, after_fun}
do_transform(user_acc.(), :cont, next, inner_acc, funs)
end
defp do_transform(user_acc, _next_op, next, {:halt, inner_acc}, funs) do
{_, _, _, _, after_fun} = funs
{_, _, _, after_fun} = funs
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
{:halted, inner_acc}
end
@@ -935,99 +867,72 @@ defmodule Stream do
{:suspended, inner_acc, &do_transform(user_acc, next_op, next, &1, funs)}
end
defp do_transform(user_acc, :halt, _next, {_, inner_acc}, funs) do
{_, _, _, after_fun} = funs
do_after(after_fun, user_acc)
{:halted, inner_acc}
end
defp do_transform(user_acc, :cont, next, inner_acc, funs) do
{_, _, _, _, after_fun} = funs
{_, _, _, after_fun} = funs
try do
next.({:cont, []})
catch
kind, reason ->
after_fun.(user_acc)
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, vals, next} ->
do_transform_user(:lists.reverse(vals), user_acc, :cont, next, inner_acc, funs)
{_, vals} ->
do_transform_user(:lists.reverse(vals), user_acc, :last, next, inner_acc, funs)
do_transform_user(:lists.reverse(vals), user_acc, :halt, next, inner_acc, funs)
end
end
defp do_transform(user_acc, :last, next, inner_acc, funs) do
{_, _, _, last_fun, after_fun} = funs
if last_fun do
try do
last_fun.(user_acc)
catch
kind, reason ->
next.({:halt, []})
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
result -> do_transform_result(result, [], :halt, next, inner_acc, funs)
end
else
do_transform(user_acc, :halt, next, inner_acc, funs)
end
end
defp do_transform(user_acc, :halt, _next, inner_acc, funs) do
{_, _, _, _, after_fun} = funs
after_fun.(user_acc)
{:halted, elem(inner_acc, 1)}
end
defp do_transform_user([], user_acc, next_op, next, inner_acc, funs) do
do_transform(user_acc, next_op, next, inner_acc, funs)
end
defp do_transform_user([val | vals], user_acc, next_op, next, inner_acc, funs) do
{user, _, _, _, after_fun} = funs
{user, fun, inner, after_fun} = funs
try do
user.(val, user_acc)
catch
kind, reason ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
result -> do_transform_result(result, vals, next_op, next, inner_acc, funs)
end
end
defp do_transform_result(result, vals, next_op, next, inner_acc, funs) do
{_, fun, inner, _, after_fun} = funs
case result do
{[], user_acc} ->
do_transform_user(vals, user_acc, next_op, next, inner_acc, funs)
{list, user_acc} when is_list(list) ->
reduce = &Enumerable.List.reduce(list, &1, fun)
do_transform_inner_list(vals, user_acc, next_op, next, inner_acc, reduce, funs)
do_list_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs)
{:halt, user_acc} ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
{:halted, elem(inner_acc, 1)}
{other, user_acc} ->
reduce = &Enumerable.reduce(other, &1, inner)
do_transform_inner_enum(vals, user_acc, next_op, next, inner_acc, reduce, funs)
do_enum_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs)
end
end
defp do_transform_inner_list(vals, user_acc, next_op, next, inner_acc, reduce, funs) do
{_, _, _, _, after_fun} = funs
defp do_list_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs) do
{_, _, _, after_fun} = funs
try do
reduce.(inner_acc)
catch
kind, reason ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:done, acc} ->
@@ -1035,24 +940,24 @@ defmodule Stream do
{:halted, acc} ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
{:halted, acc}
{:suspended, acc, continuation} ->
resume = &do_transform_inner_list(vals, user_acc, next_op, next, &1, continuation, funs)
resume = &do_list_transform(vals, user_acc, next_op, next, &1, continuation, funs)
{:suspended, acc, resume}
end
end
defp do_transform_inner_enum(vals, user_acc, next_op, next, {op, inner_acc}, reduce, funs) do
{_, _, _, _, after_fun} = funs
defp do_enum_transform(vals, user_acc, next_op, next, {op, inner_acc}, reduce, funs) do
{_, _, _, after_fun} = funs
try do
reduce.({op, [:outer | inner_acc]})
catch
kind, reason ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
# Only take into account outer halts when the op is not halt itself.
@@ -1062,18 +967,21 @@ defmodule Stream do
{:halted, [_ | acc]} ->
next.({:halt, []})
after_fun.(user_acc)
do_after(after_fun, user_acc)
{:halted, acc}
{:done, [_ | acc]} ->
do_transform_user(vals, user_acc, next_op, next, {:cont, acc}, funs)
{:suspended, [_ | acc], continuation} ->
resume = &do_transform_inner_enum(vals, user_acc, next_op, next, &1, continuation, funs)
resume = &do_enum_transform(vals, user_acc, next_op, next, &1, continuation, funs)
{:suspended, acc, resume}
end
end
defp do_after(nil, _user_acc), do: :ok
defp do_after(fun, user_acc), do: fun.(user_acc)
defp do_transform_each(x, [:outer | acc], f) do
case f.(x, acc) do
{:halt, res} -> {:halt, [:inner | res]}
@@ -1280,7 +1188,7 @@ defmodule Stream do
enumerable, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put into a list which is then passed to
the one-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
the 1-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
`zip_fun`, and so on until any one of the enums in `enumerables` completes.
Returns a new enumerable with the results of calling `zip_fun`.
@@ -1315,8 +1223,6 @@ defmodule Stream do
{:suspended, acc, &zip_list(enumerables, &1, fun, zip_fun)}
end
defp zip_list([], {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
defp zip_list(enumerables, {:cont, acc}, fun, zip_fun) do
case zip_list_heads_tails(enumerables, [], []) do
{heads, tails} -> zip_list(tails, fun.(zip_fun.(heads), acc), fun, zip_fun)
+187 -326
View File
@@ -17,9 +17,6 @@ defmodule String do
iex> "hello" <> " " <> "world"
"hello world"
The functions in this module act according to
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
## Interpolation
Strings in Elixir also support interpolation. This allows
@@ -40,7 +37,7 @@ defmodule String do
"2 + 2 = 4"
In case the value you want to interpolate cannot be
converted to a string, because it doesn't have a human
converted to a string, because it doesn't have an human
textual representation, a protocol error will be raised.
## Escape characters
@@ -48,7 +45,6 @@ defmodule String do
Besides allowing double-quotes to be escaped with a backslash,
strings also support the following escape characters:
* `\0` - Null byte
* `\a` - Bell
* `\b` - Backspace
* `\t` - Horizontal tab
@@ -57,9 +53,7 @@ defmodule String do
* `\f` - Form feed
* `\r` - Carriage return
* `\e` - Command Escape
* `\s` - Space
* `\#` - Returns the `#` character itself, skipping interpolation
* `\\` - Single backslash
* `\xNN` - A byte represented by the hexadecimal `NN`
* `\uNNNN` - A Unicode code point represented by `NNNN`
@@ -72,82 +66,29 @@ defmodule String do
low-level manipulations of string, so let's explore them in
detail next.
## Unicode and code points
## Code points and grapheme cluster
In order to facilitate meaningful communication between computers
across multiple languages, a standard is required so that the ones
and zeros on one machine mean the same thing when they are transmitted
to another. The Unicode Standard acts as an official registry of
virtually all the characters we know: this includes characters from
classical and historical texts, emoji, and formatting and control
characters as well.
The functions in this module act according to
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
Unicode organizes all of the characters in its repertoire into code
charts, and each character is given a unique numerical index. This
numerical index is known as a Code Point.
As per the standard, a code point is a single Unicode Character,
which may be represented by one or more bytes.
In Elixir you can use a `?` in front of a character literal to reveal
its code point:
For example, although the code point "é" is a single character,
its underlying representation uses two bytes:
iex> ?a
97
iex> ?ł
322
iex> String.length("é")
1
iex> byte_size("é")
2
Note that most Unicode code charts will refer to a code point by its
hexadecimal (hex) representation, e.g. `97` translates to `0061` in hex,
and we can represent any Unicode character in an Elixir string by
using the `\u` escape character followed by its code point number:
iex> "\u0061" === "a"
true
iex> 0x0061 = 97 = ?a
97
The hex representation will also help you look up information about a
code point, e.g. [https://codepoints.net/U+0061](https://codepoints.net/U+0061)
has a data sheet all about the lower case `a`, a.k.a. code point 97.
Remember you can get the hex presentation of a number by calling
`Integer.to_string/2`:
iex> Integer.to_string(?a, 16)
"61"
## UTF-8 encoded and encodings
Now that we understand what the Unicode standard is and what code points
are, we can finally talk about encodings. Whereas the code point is **what**
we store, an encoding deals with **how** we store it: encoding is an
implementation. In other words, we need a mechanism to convert the code
point numbers into bytes so they can be stored in memory, written to disk, and such.
Elixir uses UTF-8 to encode its strings, which means that code points are
encoded as a series of 8-bit bytes. UTF-8 is a **variable width** character
encoding that uses one to four bytes to store each code point. It is capable
of encoding all valid Unicode code points. Let's see an example:
iex> string = "héllo"
"héllo"
iex> String.length(string)
5
iex> byte_size(string)
6
Although the string above has 5 characters, it uses 6 bytes, as two bytes
are used to represent the character `é`.
## Grapheme clusters
This module also works with the concept of grapheme cluster
(from now on referenced as graphemes). Graphemes can consist
of multiple code points that may be perceived as a single character
by readers. For example, "é" can be represented either as a single
"e with acute" code point, as seen above in the string `"héllo"`,
or as the letter "e" followed by a "combining acute accent"
(two code points):
Furthermore, this module also presents the concept of grapheme cluster
(from now on referenced as graphemes). Graphemes can consist of multiple
code points that may be perceived as a single character by readers. For
example, "é" can be represented either as a single "e with acute" code point
or as the letter "e" followed by a "combining acute accent" (two code points):
iex> string = "\u0065\u0301"
"é"
iex> byte_size(string)
3
iex> String.length(string)
@@ -157,13 +98,14 @@ defmodule String do
iex> String.graphemes(string)
["é"]
Although it looks visually the same as before, the example above
is made of two characters, it is perceived by users as one.
Although the example above is made of two characters, it is
perceived by users as one.
Graphemes can also be two characters that are interpreted as one
by some languages. For example, some languages may consider "ch"
as a single character. However, since this information depends on
the locale, it is not taken into account by this module.
Graphemes can also be two characters that are interpreted
as one by some languages. For example, some languages may
consider "ch" as a single character. However, since this
information depends on the locale, it is not taken into account
by this module.
In general, the functions in this module rely on the Unicode
Standard, but do not contain any of the locale specific behaviour.
@@ -193,31 +135,98 @@ defmodule String do
* Plus a number of functions for working with binaries (bytes)
in the [`:binary` module](`:binary`)
A `utf8` modifier is also available inside the binary syntax `<<>>`.
It can be used to match code points out of a binary/string:
There are many situations where using the `String` module can
be avoided in favor of binary functions or pattern matching.
For example, imagine you have a string `prefix` and you want to
remove this prefix from another string named `full`.
iex> <<eacute::utf8>> = "é"
iex> eacute
233
One may be tempted to write:
You can also fully convert a string into a list of integer code points,
known as "charlists" in Elixir, by calling `String.to_charlist/1`:
iex> take_prefix = fn full, prefix ->
...> base = String.length(prefix)
...> String.slice(full, base, String.length(full) - base)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
iex> String.to_charlist("héllo")
[104, 233, 108, 108, 111]
Although the function above works, it performs poorly. To
calculate the length of the string, we need to traverse it
fully, so we traverse both `prefix` and `full` strings, then
slice the `full` one, traversing it again.
If you would rather see the underlying bytes of a string, instead of
its codepoints, a common trick is to concatenate the null byte `<<0>>`
to it:
A first attempt at improving it could be with ranges:
iex> "héllo" <> <<0>>
<<104, 195, 169, 108, 108, 111, 0>>
iex> take_prefix = fn full, prefix ->
...> base = String.length(prefix)
...> String.slice(full, base..-1)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
Alternatively, you can view a string's binary representation by
passing an option to `IO.inspect/2`:
While this is much better (we don't traverse `full` twice),
it could still be improved. In this case, since we want to
extract a substring from a string, we can use `Kernel.byte_size/1`
and `Kernel.binary_part/3` as there is no chance we will slice in
the middle of a code point made of more than one byte:
IO.inspect("héllo", binaries: :as_binaries)
#=> <<104, 195, 169, 108, 108, 111>>
iex> take_prefix = fn full, prefix ->
...> base = byte_size(prefix)
...> binary_part(full, base, byte_size(full) - base)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
Or simply use pattern matching:
iex> take_prefix = fn full, prefix ->
...> base = byte_size(prefix)
...> <<_::binary-size(base), rest::binary>> = full
...> rest
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
On the other hand, if you want to dynamically slice a string
based on an integer value, then using `String.slice/3` is the
best option as it guarantees we won't incorrectly split a valid
code point into multiple bytes.
## Integer code points
Although code points are represented as integers, this module
represents code points in their encoded format as strings.
For example:
iex> String.codepoints("olá")
["o", "l", "á"]
There are a couple of ways to retrieve the character code point.
One may use the `?` construct:
iex> ?o
111
iex> ?á
225
Or also via pattern matching:
iex> <<aacute::utf8>> = "á"
iex> aacute
225
As we have seen above, code points can be inserted into
a string by their hexadecimal code:
iex> "ol\u00E1"
"olá"
Finally, to convert a String into a list of integer
code points, known as "charlists" in Elixir, you can call
`String.to_charlist`:
iex> String.to_charlist("olá")
[111, 108, 225]
## Self-synchronization
@@ -274,23 +283,8 @@ defmodule String do
@typedoc "Multiple code points that may be perceived as a single character by readers"
@type grapheme :: t
@typedoc """
Pattern used in functions like `replace/4` and `split/3`.
It must be one of:
* a string
* an empty list
* a list containing non-empty strings
* a compiled search pattern created by `:binary.compile_pattern/1`
"""
# TODO: Replace "nonempty_binary :: <<_::8, _::_*8>>" with "nonempty_binary()"
# when minimum requirement is >= OTP 24.
@type pattern ::
t()
| [nonempty_binary :: <<_::8, _::_*8>>]
| (compiled_search_pattern :: :binary.cp())
@typedoc "Pattern used in functions like `replace/4` and `split/3`"
@type pattern :: t | [t] | :binary.cp()
@conditional_mappings [:greek, :turkic]
@@ -492,14 +486,6 @@ defmodule String do
end
end
def split(string, [], options) when is_binary(string) and is_list(options) do
if string == "" and Keyword.get(options, :trim, false) do
[]
else
[string]
end
end
def split(string, pattern, options) when is_binary(string) and is_list(options) do
parts = Keyword.get(options, :parts, :infinity)
trim = Keyword.get(options, :trim, false)
@@ -524,11 +510,11 @@ defmodule String do
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
defp split_empty("", true, 1), do: []
defp split_empty(string, _, 1), do: [IO.iodata_to_binary(string)]
defp split_empty(string, _, 1), do: [string]
defp split_empty(string, trim, count) do
case :unicode_util.gc(string) do
[gc] -> [grapheme_to_binary(gc) | split_empty("", trim, 1)]
[gc] -> [grapheme_to_binary(gc) | split_empty(<<>>, trim, count - 1)]
[gc | rest] -> [grapheme_to_binary(gc) | split_empty(rest, trim, count - 1)]
[] -> split_empty("", trim, 1)
{:error, <<byte, rest::bits>>} -> [<<byte>> | split_empty(rest, trim, count - 1)]
@@ -589,14 +575,6 @@ defmodule String do
end
end
def splitter(string, [], options) when is_binary(string) and is_list(options) do
if string == "" and Keyword.get(options, :trim, false) do
Stream.duplicate(string, 0)
else
Stream.duplicate(string, 1)
end
end
def splitter(string, pattern, options) when is_binary(string) and is_list(options) do
pattern = maybe_compile_pattern(pattern)
trim = Keyword.get(options, :trim, false)
@@ -984,8 +962,6 @@ defmodule String do
iex> String.replace_leading("hello hello world", "hello ", "ola ")
"ola ola world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_leading(t, t, t) :: t
def replace_leading(string, match, replacement)
@@ -1043,8 +1019,6 @@ defmodule String do
iex> String.replace_trailing("hello world world", " world", " mundo")
"hello mundo mundo"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_trailing(t, t, t) :: t
def replace_trailing(string, match, replacement)
@@ -1105,8 +1079,6 @@ defmodule String do
iex> String.replace_prefix("world", "", "hello ")
"hello world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_prefix(t, t, t) :: t
def replace_prefix(string, match, replacement)
@@ -1147,8 +1119,6 @@ defmodule String do
iex> String.replace_suffix("hello", "", " world")
"hello world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_suffix(t, t, t) :: t
def replace_suffix(string, match, replacement)
@@ -1502,20 +1472,6 @@ defmodule String do
iex> String.replace("ELIXIR", "", "")
"ELIXIR"
Be aware that this function can replace within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following will replace only the letter "e",
moving the accent to the letter "o":
iex> String.replace(String.normalize("é", :nfd), "e", "o")
"ó"
However, if "é" is represented by the single character "e with acute"
accent, then it won't be replaced at all:
iex> String.replace(String.normalize("é", :nfc), "e", "o")
"é"
"""
@spec replace(t, pattern | Regex.t(), t | (t -> t | iodata), keyword) :: t
def replace(subject, pattern, replacement, options \\ [])
@@ -1533,10 +1489,6 @@ defmodule String do
subject
end
defp replace_guarded(subject, [], _, _) do
subject
end
defp replace_guarded(subject, "", replacement_binary, options)
when is_binary(replacement_binary) do
if Keyword.get(options, :global, true) do
@@ -1930,7 +1882,6 @@ defmodule String do
def next_grapheme(string) when is_binary(string) do
case :unicode_util.gc(string) do
[gc] -> {grapheme_to_binary(gc), <<>>}
[gc, rest] -> {grapheme_to_binary(gc), rest}
[gc | rest] -> {grapheme_to_binary(gc), rest}
[] -> nil
{:error, <<byte, rest::bits>>} -> {<<byte>>, rest}
@@ -2076,8 +2027,7 @@ defmodule String do
Remember this function works with Unicode graphemes and considers
the slices to represent grapheme offsets. If you want to split
on raw bytes, check `Kernel.binary_part/3` or `Kernel.binary_slice/3`
instead.
on raw bytes, check `Kernel.binary_part/3` instead.
## Examples
@@ -2090,19 +2040,19 @@ defmodule String do
iex> String.slice("elixir", 10, 3)
""
If the start position is negative, it is normalized
against the string length and clamped to 0:
iex> String.slice("elixir", -4, 4)
"ixir"
iex> String.slice("elixir", -10, 3)
"eli"
""
If start is more than the string length, an empty
string is returned:
iex> String.slice("a", 0, 1500)
"a"
iex> String.slice("elixir", 10, 1500)
iex> String.slice("a", 1, 1500)
""
iex> String.slice("a", 2, 1500)
""
"""
@@ -2121,8 +2071,12 @@ defmodule String do
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start < 0 and
length >= 0 do
start = max(length(string) + start, 0)
do_slice(string, start, length)
start = length(string) + start
case start >= 0 do
true -> do_slice(string, start, length)
false -> ""
end
end
defp do_slice(string, start, length) do
@@ -2146,48 +2100,42 @@ defmodule String do
Remember this function works with Unicode graphemes and considers
the slices to represent grapheme offsets. If you want to split
on raw bytes, check `Kernel.binary_part/3` or
`Kernel.binary_slice/2` instead
on raw bytes, check `Kernel.binary_part/3` instead.
## Examples
iex> String.slice("elixir", 1..3)
"lix"
iex> String.slice("elixir", 1..10)
"lixir"
iex> String.slice("elixir", -4..-1)
"ixir"
iex> String.slice("elixir", -4..6)
"ixir"
iex> String.slice("elixir", -100..100)
"elixir"
For ranges where `start > stop`, you need to explicitly
mark them as increasing:
iex> String.slice("elixir", 2..-1//1)
"ixir"
iex> String.slice("elixir", 1..-2//1)
"lixi"
You can use `../0` as a shortcut for `0..-1//1`, which returns
the whole string as is:
iex> String.slice("elixir", ..)
"elixir"
The step can be any positive number. For example, to
get every 2 characters of the string:
iex> String.slice("elixir", 0..-1//2)
"eii"
If the first position is after the string ends or after
the last position of the range, it returns an empty string:
If values are out of bounds, it returns an empty string:
iex> String.slice("elixir", 10..3)
""
iex> String.slice("elixir", -10..-7)
""
iex> String.slice("a", 0..1500)
"a"
iex> String.slice("a", 1..1500)
""
@@ -2195,16 +2143,16 @@ defmodule String do
@spec slice(t, Range.t()) :: t
def slice(string, first..last//step = range) when is_binary(string) do
# TODO: Deprecate negative steps on Elixir v1.16
cond do
step > 0 ->
slice_range(string, first, last, step)
step == -1 and first > last ->
slice_range(string, first, last, 1)
true ->
raise ArgumentError,
"String.slice/2 does not accept ranges with negative steps, got: #{inspect(range)}"
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(string, first, last)
else
raise ArgumentError,
"String.slice/2 does not accept ranges with custom steps, got: #{inspect(range)}"
end
end
@@ -2215,95 +2163,45 @@ defmodule String do
slice(string, Map.put(range, :step, step))
end
defp slice_range("", _, _, _), do: ""
defp slice_range("", _, _), do: ""
defp slice_range(_string, first, last, _step) when first >= 0 and last >= 0 and first > last do
""
defp slice_range(string, first, -1) when first >= 0 do
left = byte_size_remaining_at(string, first)
binary_part(string, byte_size(string) - left, left)
end
defp slice_range(string, first, last, step) when first >= 0 do
from_start = byte_size_remaining_at(string, first)
rest = binary_part(string, byte_size(string) - from_start, from_start)
cond do
last == -1 ->
slice_every(rest, byte_size(rest), step)
last >= 0 and step == 1 ->
from_end = byte_size_remaining_at(rest, last - first + 1)
binary_part(rest, 0, from_start - from_end)
last >= 0 ->
slice_every(rest, last - first + 1, step)
true ->
rest
|> slice_range_negative(0, last)
|> slice_every(byte_size(string), step)
defp slice_range(string, first, last) when first >= 0 and last >= 0 do
if last >= first do
slice(string, first, last - first + 1)
else
""
end
end
defp slice_range(string, first, last, step) do
string
|> slice_range_negative(first, last)
|> slice_every(byte_size(string), step)
end
defp slice_range_negative(string, first, last) do
{reversed_bytes, length} = acc_bytes(string, [], 0)
first = add_if_negative(first, length) |> max(0)
defp slice_range(string, first, last) do
{bytes, length} = acc_bytes(:unicode_util.gc(string), [], 0)
first = add_if_negative(first, length)
last = add_if_negative(last, length)
if first > last or first > length do
if first < 0 or first > last or first > length do
""
else
last = min(last + 1, length)
reversed_bytes = Enum.drop(reversed_bytes, length - last)
{length_bytes, start_bytes} = split_bytes(reversed_bytes, 0, last - first)
bytes = Enum.drop(bytes, length - last)
first = last - first
{length_bytes, start_bytes} = split_bytes(bytes, 0, first)
binary_part(string, start_bytes, length_bytes)
end
end
defp slice_every(string, _count, 1), do: string
defp slice_every(string, count, step), do: slice_every(string, count, step, [])
defp acc_bytes([gc | rest], bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [grapheme_byte_size(gc) | bytes], length + 1)
defp slice_every(string, count, to_drop, acc) when count > 0 do
case :unicode_util.gc(string) do
[current | rest] ->
rest
|> drop(to_drop)
|> slice_every(count - to_drop, to_drop, [current | acc])
defp acc_bytes([], bytes, length),
do: {bytes, length}
[] ->
reverse_characters_to_binary(acc)
{:error, <<byte, rest::bits>>} ->
reverse_characters_to_binary(acc) <>
<<byte>> <> slice_every(drop(rest, to_drop), count - to_drop, to_drop, [])
end
end
defp slice_every(_string, _count, _to_drop, acc) do
reverse_characters_to_binary(acc)
end
defp drop(string, 1), do: string
defp drop(string, count) do
case :unicode_util.gc(string) do
[_ | rest] -> drop(rest, count - 1)
[] -> ""
{:error, <<_, rest::bits>>} -> drop(rest, count - 1)
end
end
defp acc_bytes(string, bytes, length) do
case :unicode_util.gc(string) do
[gc | rest] -> acc_bytes(rest, [grapheme_byte_size(gc) | bytes], length + 1)
[] -> {bytes, length}
{:error, <<_, rest::bits>>} -> acc_bytes(rest, [1 | bytes], length + 1)
end
end
defp acc_bytes({:error, <<_, rest::bits>>}, bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [1 | bytes], length + 1)
defp add_if_negative(value, to_add) when value < 0, do: value + to_add
defp add_if_negative(value, _to_add), do: value
@@ -2327,6 +2225,12 @@ defmodule String do
iex> String.starts_with?("elixir", ["erlang", "ruby"])
false
A compiled pattern can also be given:
iex> pattern = :binary.compile_pattern(["erlang", "elixir"])
iex> String.starts_with?("elixir", pattern)
true
An empty string will always match:
iex> String.starts_with?("elixir", "")
@@ -2334,16 +2238,8 @@ defmodule String do
iex> String.starts_with?("elixir", ["", "other"])
true
An empty list will never match:
iex> String.starts_with?("elixir", [])
false
iex> String.starts_with?("", [])
false
"""
@spec starts_with?(t, t | [t]) :: boolean
@spec starts_with?(t, pattern) :: boolean
def starts_with?(string, prefix) when is_binary(string) and is_binary(prefix) do
starts_with_string?(string, byte_size(string), prefix)
end
@@ -2354,7 +2250,6 @@ defmodule String do
end
def starts_with?(string, prefix) when is_binary(string) do
IO.warn("compiled patterns are deprecated in starts_with?")
Kernel.match?({0, _}, :binary.match(string, prefix))
end
@@ -2423,7 +2318,7 @@ defmodule String do
iex> String.match?("bar", ~r/foo/)
false
Elixir also provides text-based match operator `=~/2` and function `Regex.match?/2` as
Elixir also provides `Kernel.=~/2` and `Regex.match?/2` as
alternatives to test strings against regular expressions.
"""
@spec match?(t, Regex.t()) :: boolean
@@ -2432,16 +2327,10 @@ defmodule String do
end
@doc """
Searches if `string` contains any of the given `contents`.
Checks if `string` contains any of the given `contents`.
`contents` can be either a string, a list of strings,
or a compiled pattern. If `contents` is a list, this
function will search if any of the strings in `contents`
are part of `string`.
> Note: if you want to check if `string` is listed in `contents`,
> where `contents` is a list, use `Enum.member?(contents, string)`
> instead.
or a compiled pattern.
## Examples
@@ -2465,14 +2354,6 @@ defmodule String do
iex> String.contains?("elixir of life", ["", "other"])
true
An empty list will never match:
iex> String.contains?("elixir of life", [])
false
iex> String.contains?("", [])
false
Be aware that this function can match within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following returns `true`:
@@ -2487,29 +2368,19 @@ defmodule String do
false
"""
@spec contains?(t, [t] | pattern) :: boolean
@spec contains?(t, pattern) :: boolean
def contains?(string, []) when is_binary(string) do
false
end
def contains?(string, contents) when is_binary(string) and is_list(contents) do
list_contains?(string, byte_size(string), contents, [])
"" in contents or :binary.match(string, contents) != :nomatch
end
def contains?(string, contents) when is_binary(string) do
"" == contents or :binary.match(string, contents) != :nomatch
end
defp list_contains?(string, size, [head | tail], acc) do
case byte_size(head) do
0 -> true
head_size when head_size > size -> list_contains?(string, size, tail, acc)
_ -> list_contains?(string, size, tail, [head | acc])
end
end
defp list_contains?(_string, _size, [], []),
do: false
defp list_contains?(string, _size, [], contents),
do: :binary.match(string, contents) != :nomatch
@doc """
Converts a string into a charlist.
@@ -2570,19 +2441,9 @@ defmodule String do
Converts a string to an existing atom.
The maximum atom size is of 255 Unicode code points.
Raises an `ArgumentError` if the atom does not exist.
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 `String.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
+5 -8
View File
@@ -13,10 +13,7 @@ defmodule StringIO do
"""
# We're implementing the GenServer behaviour instead of using the
# `use GenServer` macro, because we don't want the `child_spec/1`
# function as it doesn't make sense to be started under a supervisor.
@behaviour GenServer
use GenServer
@doc ~S"""
Creates an IO device.
@@ -290,7 +287,7 @@ defmodule StringIO do
{:ok, %{state | output: state.output <> string}}
{_, _, _} ->
{{:error, {:no_translation, encoding, state.encoding}}, state}
{{:error, req}, state}
end
rescue
ArgumentError -> {{:error, req}, state}
@@ -366,7 +363,6 @@ defmodule StringIO do
defp get_until(encoding, prompt, mod, fun, args, %{input: input} = state) do
case get_until(input, encoding, mod, fun, args, [], 0) do
{result, input, count} ->
# Convert :eof to "" as they are both treated the same
input =
case input do
:eof -> ""
@@ -393,7 +389,7 @@ defmodule StringIO do
defp get_until(chars, encoding, mod, fun, args, continuation, count) do
case bytes_until_eol(chars, encoding, 0) do
{kind, size} when kind in [:split, :replace_split] ->
<<line::binary-size(size), rest::binary>> = chars
{line, rest} = :erlang.split_binary(chars, size)
case apply(mod, fun, [continuation, binary_to_list(line, encoding) | args]) do
{:done, result, :eof} ->
@@ -411,6 +407,7 @@ defmodule StringIO do
end
end
defp binary_to_list(data, _) when is_list(data), do: data
defp binary_to_list(data, :unicode) when is_binary(data), do: String.to_charlist(data)
defp binary_to_list(data, :latin1) when is_binary(data), do: :erlang.binary_to_list(data)
@@ -418,7 +415,7 @@ defmodule StringIO do
defp list_to_binary(data, :unicode) when is_list(data), do: List.to_string(data)
defp list_to_binary(data, :latin1) when is_list(data), do: :erlang.list_to_binary(data)
# From https://www.erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# From https://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# Erlang term, but if it is a list(), the I/O server can convert it to a binary().
defp get_until_result(data, encoding) when is_list(data), do: list_to_binary(data, encoding)
defp get_until_result(data, _), do: data
+173 -253
View File
@@ -7,7 +7,7 @@ defmodule Supervisor do
process structure called a *supervision tree*. Supervision trees provide
fault-tolerance and encapsulate how our applications start and shutdown.
A supervisor may be started directly with a list of child specifications via
A supervisor may be started directly with a list of children via
`start_link/2` or you may define a module-based supervisor that implements
the required callbacks. The sections below use `start_link/2` to start
supervisors in most examples, but it also includes a specific section
@@ -16,55 +16,48 @@ defmodule Supervisor do
## Examples
In order to start a supervisor, we need to first define a child process
that will be supervised. As an example, we will define a `GenServer`,
a generic server, that keeps a counter. Other processes can then send
messages to this process to read the counter and bump its value.
that will be supervised. As an example, we will define a GenServer that
represents a stack:
> Note: in practice you would not define a counter as a GenServer. Instead,
> if you need a counter, you would pass it around as inputs and outputs to
> the functions that need it. The reason we picked a counter in this example
> is due to its simplicity, as it allows us to focus on how supervisors work.
defmodule Counter do
defmodule Stack do
use GenServer
def start_link(arg) when is_integer(arg) do
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end
## Callbacks
@impl true
def init(counter) do
{:ok, counter}
def init(stack) do
{:ok, stack}
end
@impl true
def handle_call(:get, _from, counter) do
{:reply, counter, counter}
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
def handle_call({:bump, value}, _from, counter) do
{:reply, counter, counter + value}
@impl true
def handle_cast({:push, head}, tail) do
{:noreply, [head | tail]}
end
end
The `Counter` receives an argument on `start_link`. This argument
is passed to the `init/1` callback which becomes the initial value
of the counter. Our counter handles two operations (known as calls):
`:get`, to get the current counter value, and `:bump`, that bumps
the counter by the given `value` and returns the old counter.
The stack is a small wrapper around lists. It allows us to put
an element on the top of the stack, by prepending to the list,
and to get the top of the stack by pattern matching.
We can now start a supervisor that will start and supervise our
counter process. The first step is to define a list of **child
stack process. The first step is to define a list of **child
specifications** that control how each child behaves. Each child
specification is a map, as shown below:
children = [
# The Counter is a child started via Counter.start_link(0)
# The Stack is a child started via Stack.start_link([:hello])
%{
id: Counter,
start: {Counter, :start_link, [0]}
id: Stack,
start: {Stack, :start_link, [[:hello]]}
}
]
@@ -76,30 +69,30 @@ defmodule Supervisor do
#=> %{active: 1, specs: 1, supervisors: 0, workers: 1}
Note that when starting the GenServer, we are registering it
with name `Counter` via the `name: __MODULE__` option. This allows
us to call it directly and get its value:
with name `Stack`, which allows us to call it directly and get
what is on the stack:
GenServer.call(Counter, :get)
#=> 0
GenServer.call(Stack, :pop)
#=> :hello
GenServer.call(Counter, {:bump, 3})
#=> 0
GenServer.cast(Stack, {:push, :world})
#=> :ok
GenServer.call(Counter, :get)
#=> 3
GenServer.call(Stack, :pop)
#=> :world
However, there is a bug in our counter server. If we call `:bump` with
a non-numeric value, it is going to crash:
However, there is a bug in our stack server. If we call `:pop` and
the stack is empty, it is going to crash because no clause matches:
GenServer.call(Counter, {:bump, "oops"})
** (exit) exited in: GenServer.call(Counter, {:bump, "oops"}, 5000)
GenServer.call(Stack, :pop)
** (exit) exited in: GenServer.call(Stack, :pop, 5000)
Luckily, since the server is being supervised by a supervisor, the
supervisor will automatically start a new one, reset back to its initial
value of `0`:
supervisor will automatically start a new one, with the initial stack
of `[:hello]`:
GenServer.call(Counter, :get)
#=> 0
GenServer.call(Stack, :pop)
#=> :hello
Supervisors support different strategies; in the example above, we
have chosen `:one_for_one`. Furthermore, each supervisor can have many
@@ -118,11 +111,10 @@ defmodule Supervisor do
The child specification is a map containing up to 6 elements. The first two keys
in the following list are required, and the remaining ones are optional:
* `:id` - any term used to identify the child specification internally by
the supervisor; defaults to the given module. This key is required.
For supervisors, in the case of conflicting `:id` values, the supervisor
will refuse to initialize and require explicit IDs. This is not the case
for [dynamic supervisors](`DynamicSupervisor`) though.
* `:id` - any term used to identify the child specification
internally by the supervisor; defaults to the given module.
In the case of conflicting `:id` values, the supervisor will refuse
to initialize and require explicit IDs. This key is required.
* `:start` - a tuple with the module-function-args to be invoked
to start the child process. This key is required.
@@ -139,11 +131,8 @@ defmodule Supervisor do
* `:type` - specifies that the child process is a `:worker` or a
`:supervisor`. This key is optional and defaults to `:worker`.
* `:modules` - a list of modules used by hot code upgrade mechanisms
to determine which processes are using certain modules. It is typically
set to the callback module of behaviours like `GenServer`, `Supervisor`,
and such. It is set automatically based on the `:start` value and it is rarely
changed in practice.
There is a sixth key, `:modules`, which is optional and is rarely changed.
It is set automatically based on the `:start` value.
Let's understand what the `:shutdown` and `:restart` options control.
@@ -194,100 +183,154 @@ defmodule Supervisor do
For a more complete understanding of the exit reasons and their
impact, see the "Exit reasons and restarts" section.
## `child_spec/1` function
## child_spec/1
When starting a supervisor, we may pass a list of child specifications. Those
When starting a supervisor, we pass a list of child specifications. Those
specifications are maps that tell how the supervisor should start, stop and
restart each of its children:
%{
id: Counter,
start: {Counter, :start_link, [0]}
id: Stack,
start: {Stack, :start_link, [[:hello]]}
}
The map above defines a child with `:id` of `Counter` that is started
by calling `Counter.start_link(0)`.
The map above defines a child with `:id` of `Stack` that is started
by calling `Stack.start_link([:hello])`.
However, defining the child specification for each child as a map can be
quite error prone, as we may change the `Counter` implementation and forget
to update its specification. That's why Elixir allows you to pass a tuple with
However, specifying the child specification for each child as a map can be
quite error prone, as we may change the Stack implementation and forget to
update its specification. That's why Elixir allows you to pass a tuple with
the module name and the `start_link` argument instead of the specification:
children = [
{Counter, 0}
{Stack, [:hello]}
]
The supervisor will then invoke `Counter.child_spec(0)` to retrieve a child
specification. Now the `Counter` module is responsible for building its own
specification, for example, we could write:
The supervisor will then invoke `Stack.child_spec([:hello])` to retrieve a
child specification. Now the `Stack` module is responsible for building its
own specification, for example, we could write:
def child_spec(arg) do
%{
id: Counter,
start: {Counter, :start_link, [arg]}
id: Stack,
start: {Stack, :start_link, [arg]}
}
end
Luckily for us, `use GenServer` already defines a `Counter.child_spec/1`
exactly like above, so you don't need to write the definition above yourself.
If you want to customize the automatically generated `child_spec/1` function,
you can pass the options directly to `use GenServer`:
Luckily for us, `use GenServer` already defines a `Stack.child_spec/1`
exactly like above. If you need to customize the `GenServer`, you can
pass the options directly to `use GenServer`:
use GenServer, restart: :transient
Finally, note it is also possible to simply pass the `Counter` module as
Finally, note it is also possible to simply pass the `Stack` module as
a child:
children = [
Counter
Stack
]
When only the module name is given, it is equivalent to `{Counter, []}`,
which in our case would be invalid, which is why we always pass the initial
counter explicitly.
When only the module name is given, it is equivalent to `{Stack, []}`.
By replacing the map specification by `{Stack, [:hello]}` or `Stack`, we keep
the child specification encapsulated in the `Stack` module, using the default
implementation defined by `use GenServer`. We can now share our `Stack` worker
with other developers and they can add it directly to their supervision tree
without worrying about the low-level details of the worker.
By replacing the child specification with `{Counter, 0}`, we keep it
encapsulated in the `Counter` module. We could now share our
`Counter` implementation with other developers and they can add it directly
to their supervision tree without worrying about the low-level details of
the counter.
Overall, a child specification can be one of the following:
Overall, the child specification can be one of the following:
* a map representing the child specification itself - as outlined in the
"Child specification" section
* a tuple with a module as first element and the start argument as second -
such as `{Counter, 0}`. In this case, `Counter.child_spec(0)` is called
to retrieve the child specification
such as `{Stack, [:hello]}`. In this case, `Stack.child_spec([:hello])`
is called to retrieve the child specification
* a module - such as `Stack`. In this case, `Stack.child_spec([])`
is called to retrieve the child specification
* a module - such as `Counter`. In this case, `Counter.child_spec([])`
would be called, which is invalid for the counter, but it is useful in
many other cases, especially when you want to pass a list of options
to the child process
If you need to convert a `{module, arg}` tuple or a module child specification to a
[child specification](`t:child_spec/0`) or modify a child specification itself,
you can use the `Supervisor.child_spec/2` function.
For example, to run the counter with a different `:id` and a `:shutdown` value of
If you need to convert a tuple or a module child specification to a map or
modify a child specification, you can use the `Supervisor.child_spec/2` function.
For example, to run the stack with a different `:id` and a `:shutdown` value of
10 seconds (10_000 milliseconds):
children = [
Supervisor.child_spec({Counter, 0}, id: MyCounter, shutdown: 10_000)
Supervisor.child_spec({Stack, [:hello]}, id: MyStack, shutdown: 10_000)
]
## Supervisor strategies and options
## Module-based supervisors
In the example above, a supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of children that are automatically initialized, we manually
initialize the children by calling `Supervisor.init/2` inside its
`c:init/1` callback.
`use Supervisor` also defines a `child_spec/1` function which allows
us to run `MyApp.Supervisor` as a child of another supervisor or
at the top of your supervision tree as:
children = [
MyApp.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)
A general guideline is to use the supervisor without a callback
module only at the top of your supervision tree, generally in the
`c:Application.start/2` callback. We recommend using module-based
supervisors for any other supervisor in your application, so they
can run as a child of another supervisor in the tree. The `child_spec/1`
generated automatically by `Supervisor` can be customized with the
following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
The `@doc` annotation immediately preceding `use Supervisor` will be
attached to the generated `child_spec/1` function.
## `start_link/2`, `init/2`, and strategies
So far we have started the supervisor passing a single child as a tuple
as well as a strategy called `:one_for_one`:
children = [
{Counter, 0}
{Stack, [:hello]}
]
Supervisor.start_link(children, strategy: :one_for_one)
The first argument given to `start_link/2` is a list of child
or from inside the `c:init/1` callback:
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
The first argument given to `start_link/2` and `init/2` is a list of child
specifications as defined in the "child_spec/1" section above.
The second argument is a keyword list of options:
@@ -325,69 +368,13 @@ defmodule Supervisor do
In the above, process termination refers to unsuccessful termination, which
is determined by the `:restart` option.
To efficiently supervise children started dynamically, see `DynamicSupervisor`.
To dynamically supervise children, see `DynamicSupervisor`.
### Name registration
A supervisor is bound to the same name registration rules as a `GenServer`.
Read more about these rules in the documentation for `GenServer`.
## Module-based supervisors
In the example so far, the supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
children = [
{Counter, 0}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of child specifications that are automatically initialized, we manually
initialize the children by calling `Supervisor.init/2` inside its
`c:init/1` callback. `Supervisor.init/2` accepts the same `:strategy`,
`:max_restarts`, and `:max_seconds` options as `start_link/2`.
`use Supervisor` also defines a `child_spec/1` function which allows
us to run `MyApp.Supervisor` as a child of another supervisor or
at the top of your supervision tree as:
children = [
MyApp.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)
A general guideline is to use the supervisor without a callback
module only at the top of your supervision tree, generally in the
`c:Application.start/2` callback. We recommend using module-based
supervisors for any other supervisor in your application, so they
can run as a child of another supervisor in the tree. The `child_spec/1`
generated automatically by `Supervisor` can be customized with the
following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
The `@doc` annotation immediately preceding `use Supervisor` will be
attached to the generated `child_spec/1` function.
## Start and shutdown
When the supervisor starts, it traverses all child specifications and
@@ -487,8 +474,7 @@ defmodule Supervisor do
init callback to return the proper supervision flags.
"""
@callback init(init_arg :: term) ::
{:ok,
{sup_flags(), [child_spec() | (old_erlang_child_spec :: :supervisor.child_spec())]}}
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
| :ignore
@typedoc "Return values of `start_link` functions"
@@ -503,27 +489,14 @@ defmodule Supervisor do
| {:ok, child, info :: term}
| {:error, {:already_started, child} | :already_present | term}
@typedoc """
A child process.
It can be a PID when the child process was started, or `:undefined` when
the child was created by a [dynamic supervisor](`DynamicSupervisor`).
"""
@type child :: pid | :undefined
@typedoc "The supervisor name"
@typedoc "The Supervisor name"
@type name :: atom | {:global, term} | {:via, module, term}
@typedoc "Option values used by the `start*` functions"
@type option :: {:name, name}
@typedoc "The supervisor flags returned on init"
@type sup_flags() :: %{
strategy: strategy(),
intensity: non_neg_integer(),
period: pos_integer()
}
@typedoc "The supervisor reference"
@type supervisor :: pid | name | {atom, node}
@@ -533,62 +506,35 @@ defmodule Supervisor do
| {:max_restarts, non_neg_integer}
| {:max_seconds, pos_integer}
@typedoc "Supported restart options"
@type restart :: :permanent | :transient | :temporary
# TODO: Update :shutdown to "timeout() | :brutal_kill" when we require Erlang/OTP 24.
# Additionally apply https://github.com/elixir-lang/elixir/pull/11836
@typedoc "Supported shutdown options"
@type shutdown :: pos_integer() | :infinity | :brutal_kill
@typedoc "Supported strategies"
@type strategy :: :one_for_one | :one_for_all | :rest_for_one
@typedoc """
Supervisor type.
Whether the supervisor is a worker or a supervisor.
"""
@type type :: :worker | :supervisor
# Note we have inlined all types for readability
@typedoc """
The supervisor child specification.
It defines how the supervisor should start, stop and restart each of its children.
"""
@typedoc "The supervisor specification"
@type child_spec :: %{
required(:id) => atom() | term(),
required(:start) => {module(), function_name :: atom(), args :: [term()]},
optional(:restart) => restart(),
optional(:shutdown) => shutdown(),
optional(:type) => type(),
required(:start) => {module(), atom(), [term()]},
optional(:restart) => :permanent | :transient | :temporary,
optional(:shutdown) => timeout() | :brutal_kill,
optional(:type) => :worker | :supervisor,
optional(:modules) => [module()] | :dynamic
}
@doc """
Starts a supervisor with the given children.
`children` is a list of the following forms:
* a [child specification](`t:child_spec/0`)
* a module, where `module.child_spec([])` will be invoked to retrieve
its child specification
* a two-element tuple in the shape of `{module, arg}`, where `module.child_spec(arg)`
will be invoked to retrieve its child specification
A strategy is required to be provided through the `:strategy` option. See
"Supervisor strategies and options" for examples and other options.
The children is a list of modules, two-element tuples with module and
arguments or a map with the child specification. A strategy is required
to be provided through the `:strategy` option. See
"start_link/2, init/2, and strategies" for examples and other options.
The options 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 and all child processes are successfully spawned
If the supervisor and its child processes are successfully spawned
(if the start function of each child process returns `{:ok, child}`,
`{:ok, child, info}`, or `:ignore`), this function returns
`{:ok, child, info}`, or `:ignore`) 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`
@@ -603,26 +549,20 @@ defmodule Supervisor do
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
"""
@spec start_link(
[
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
],
[option | init_option]
) :: {:ok, pid} | {:error, {:already_started, pid} | {:shutdown, term} | term}
@spec start_link([:supervisor.child_spec() | {module, term} | module], [option | init_option]) ::
{:ok, pid} | {:error, {:already_started, pid} | {:shutdown, term} | term}
def start_link(children, options) when is_list(children) do
{sup_opts, start_opts} = Keyword.split(options, [:strategy, :max_seconds, :max_restarts])
start_link(Supervisor.Default, init(children, sup_opts), start_opts)
end
@doc """
Receives a list of child specifications to initialize and a set of `options`.
Receives a list of `children` to initialize and a set of `options`.
This is typically invoked at the end of the `c:init/1` callback of
module-based supervisors. See the sections "Supervisor strategies and options" and
"Module-based supervisors" in the module documentation for more information.
module-based supervisors. See the sections "Module-based supervisors"
and "start_link/2, init/2, and strategies" in the module
documentation for more information.
This function returns a tuple containing the supervisor
flags and child specifications.
@@ -631,7 +571,7 @@ defmodule Supervisor do
def init(_init_arg) do
children = [
{Counter, 0}
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
@@ -653,17 +593,7 @@ defmodule Supervisor do
description of the available strategies.
"""
@doc since: "1.5.0"
@spec init(
[
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
],
[init_option]
) ::
{:ok,
{sup_flags(), [child_spec() | (old_erlang_child_spec :: :supervisor.child_spec())]}}
@spec init([:supervisor.child_spec() | {module, term} | module], [init_option]) :: {:ok, tuple}
def init(children, options) when is_list(children) and is_list(options) do
strategy =
case options[:strategy] do
@@ -769,14 +699,10 @@ defmodule Supervisor do
@doc """
Builds and overrides a child specification.
Similar to `start_link/2` and `init/2`, it expects a module, `{module, arg}`,
or a [child specification](`t:child_spec/0`).
If a two-element tuple in the shape of `{module, arg}` is given,
the child specification is retrieved by calling `module.child_spec(arg)`.
If a module is given, the child specification is retrieved by calling
`module.child_spec([])`.
Similar to `start_link/2` and `init/2`, it expects a
`module`, `{module, arg}` or a map as the child specification.
If a module is given, the specification is retrieved by calling
`module.child_spec(arg)`.
After the child specification is retrieved, the fields on `overrides`
are directly applied on the child spec. If `overrides` has keys that
@@ -834,8 +760,8 @@ defmodule Supervisor do
section in the `GenServer` module docs.
"""
# It is important to keep the two-arity spec because it is a catch-all
# to start_link(children, options).
# It is important to keep the 2-arity spec because it is a catch
# all to start_link(children, options).
@spec start_link(module, term) :: on_start
@spec start_link(module, term, [option]) :: on_start
def start_link(module, init_arg, options \\ []) when is_list(options) do
@@ -890,13 +816,7 @@ defmodule Supervisor do
returns `{:error, error}` where `error` is a term containing information about
the error and child specification.
"""
@spec start_child(
supervisor,
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
) ::
@spec start_child(supervisor, :supervisor.child_spec() | {module, term} | module) ::
on_start_child
def start_child(supervisor, {_, _, _, _, _, _} = child_spec) do
call(supervisor, {:start_child, child_spec})
@@ -1040,7 +960,7 @@ defmodule Supervisor do
workers: non_neg_integer
}
def count_children(supervisor) do
call(supervisor, :count_children) |> :maps.from_list()
call(supervisor, :count_children) |> Map.new()
end
@doc """

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