Compare commits

..
87 Commits
Author SHA1 Message Date
José Valim 4e6d67c169 Add branch docs publishing 2023-05-30 11:41:49 +02:00
jendis 614ae049e6 Fix elixir test assertion for erlang >=OTP-24.1 (#11311) 2021-10-14 16:43:34 +02:00
MatteoandJosé Valim b1492cb0cf Fix logger translator with otp-24 (#11222)
* Handle new significant key in sasl reports

* Add OTP-24.0 to CI matrix

Co-authored-by: José Valim <jose.valim@dashbit.co>
2021-08-31 23:46:27 +02:00
Wojtek Mach 237a71311c Update warnings for OTP master (#10807) 2021-08-31 19:27:51 +02:00
José Valim e40b280fd8 Print task times on MIX_DEBUG=1 2021-06-01 11:43:49 +02:00
José Valim 308255bda8 Release v1.11.4 2021-03-16 12:25:59 +01:00
José Valim 1e81a5b9a5 Run formatter 2021-02-28 01:33:19 +01:00
José Valim 0627d7b0d4 Pass env and target to Mix.Config readers 2021-02-27 21:52:56 +01:00
Wojtek Mach 99ec7522d3 Backport OTP 24 compatibility patches to v1.11 (#10743)
* Update Erlang warnings translation (#10694)

Since https://github.com/erlang/otp/commit/8ecc648df22dbfe9dd68a3e2b968ba2b6a2ca7fe
instead of `{eval_failure, Reason}` we'd get `{eval_failure, Call, Reason}`

* Fix translating erl compiler errors to diagnostics on OTP 24 (#10719)

The compiler previously emitted just `line` and now emits `{line,
column}`.

The diagnostic struct accepts as position either:

    nil
    line
    {start_line, start_col, end_line, end_col}

so we could have used the `column` to create the 4-tuple but we don't
have enough information to do that correctly, we don't know where the
line ends.

* Fix remaining warning translations for OTP 24 (#10720)

Yecc error message changed so we need to loosen up our assertion:

OTP 23

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

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

OTP 24

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

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

    :error

* Support 16bit floats in bitstrings (#10740)

On OTP 24:

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

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

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

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

    iex> (fn <<x::float-16>> -> x; _ -> :nomatch end).(<<60, 0>>)
    :nomatch

* Handle :compile.file/2 returning :error on OTP 24 (#10742)

Before OTP 24 when given invalid args, :compile.file/2 would return:

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

On OTP 24:

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

    *** Internal compiler error ***
    exception error: bad argument
      in function  io_lib:format/2
         called as io_lib:format("badly formed '~s'",[{"foo","bar"}])
      in call from sys_messages:list_errors/3 (sys_messages.erl, line 53)
      in call from lists:foreach/2 (lists.erl, line 1342)
      in call from compile:comp_ret_err/1 (compile.erl, line 546)
      in call from compile:'-internal_fun/2-anonymous-0-'/2 (compile.erl, line 229)
      in call from compile:'-do_compile/2-anonymous-0-'/1 (compile.erl, line 219)
    :error
2021-02-20 14:33:11 +01:00
José Valim d30c5c0185 Release v1.11.3 2021-01-04 16:47:18 +01:00
José Valim 3fc334ebc3 Support all literals in guards, closes #10613 2021-01-02 17:19:30 +01:00
Julian Doherty df8955c0dd Improve Enum.chunk_while/4 docs to clarify after_fun semantics (#10595)
- add `{:halt, acc}` tuple return doc for `chunk_fun`
- explain `after_fun` semantics in more detail
- format return tuples for `chunk_fun` and `after_fun` as markdown lists
- added note that `acc` in `after_fun` return tuple is ignored
- note that `Enum.chunk_while/4` returns a list of *chunks*, not a list
  of lists (emitted chunks can be anything, not necessarily just lists)
2021-01-02 17:09:25 +01:00
José Valim 6b87a54825 Clarify writing documentation guide, closes #10610 2021-01-02 17:09:20 +01:00
José Valim 72ec902554 Revert "Add support for new rebar3 output env option (#10018)"
This option will be enabled on Elixir v1.12 instead.

This reverts commit fc9f7690c1.
2020-12-21 13:01:34 +01:00
José Valim 2c9fff0f93 Update CHANGELOG 2020-12-17 13:23:40 +01:00
José Valim c42f7af02b Do not match on error_info on OTP 24 2020-12-17 13:21:32 +01:00
José Valim 9c77960a26 Consider macros and module attributes on the left side of ++ on ExUnit.Diff
Closes #10520
2020-12-17 13:17:19 +01:00
José Valim a20f413841 Do not warn for attributes used in after_compile
Closes #10579.
2020-12-17 13:08:07 +01:00
José Valim ffbae20014 Improve xref error message, closes #10582 2020-12-17 08:18:16 +01:00
José Valim 9c81578ab5 Update CHANGELOG 2020-12-13 09:24:38 +01:00
José Valim a9f7b8e18c Run formatter 2020-12-13 09:15:02 +01:00
José Valim 61c3c34f44 Do not discard context from with, closes #10538 2020-12-13 09:14:39 +01:00
José Valim d8faf5bb7b Fix bootstrapping 2020-12-13 09:13:47 +01:00
José Valim 9d03032cfd Add line and column to EEx tokenizer 2020-12-13 09:06:15 +01:00
José Valim 3c4c68b3d5 Fix variable overriding in records, closes #10540 2020-12-13 09:05:09 +01:00
José Valim bed0a68176 Do not use unicode chars if ANSI is disabled
See https://github.com/elixir-lang/elixir/issues/10481#issuecomment-742448132
2020-12-10 12:19:19 +01:00
José Valim 60642c340d Improve error message on do-block in module attr, closes #10544 2020-12-10 12:19:14 +01:00
Eric Meadows-Jönsson 35e4263934 Fix docs tests on OTP 23 (#10510) 2020-11-29 10:19:53 +01:00
José Valim 43327512da Do not use private and removed ram_file:compress/1 (#10439) 2020-11-29 10:19:22 +01:00
José Valim 32692f14ff Fix test on Erlang master 2020-11-29 09:51:00 +01:00
Eric Meadows-Jönsson f80bd3c478 Fix intermittent test failure caused by archive warning (#10493) 2020-11-29 09:43:36 +01:00
José Valim 23934b48ba Fix failure on Erlang/OTP master 2020-11-29 09:43:28 +01:00
Dominic Letz c370a8a724 Add :compressed to stream_modes spec (#10490)
:compressed is already supported here so that gzipped files
can be streamed `File.stream!("log.gz", [:compressed])`.
2020-11-29 09:43:17 +01:00
José Valim 8c2237f80d Handle in memory modules in Xref.calls, closes #10502 2020-11-21 08:34:30 +01:00
José Valim a5e9c39023 Do not raise when checking for ambiguity with static_atoms_encoder, closes #10501 2020-11-20 12:43:14 +01:00
José Valim 6d918f5fb4 Wrap around :file instead of calling internals 2020-11-20 07:14:43 +01:00
Eksperimental 4347468d8a Update GitHub actions/checkout to v2 (#10496) 2020-11-18 13:21:49 +01:00
José Valim f044def5a1 Handle recursive vars in map values (#10498) 2020-11-18 12:42:23 +01:00
José Valim 05c9ccad12 Check for stacktrace at compile-time and runtime 2020-11-03 20:18:50 +01:00
José Valim 7533e56c1b Release v1.11.2 2020-11-03 15:23:56 +01:00
José Valim 3f083795b6 Do not trace protocol impl checks
A more complete solution is found in master but this
approach is less disruptive in the v1.11 branch.
2020-11-02 11:35:51 +01:00
José Valim 1bbc959d1a Prune tracers when fetching __ENV__ inside functions, closes #10466 2020-10-30 13:30:17 +01:00
José Valim 1603f21b0a Fix race condition when loading test helper, closes #10464 2020-10-29 19:29:06 +01:00
José Valim 02c3957667 Clarify wording about siblings in umbrella 2020-10-22 10:53:14 +02:00
José Valim eeab9f5aa1 Ensure manually set tracers are kept on compilation
Closes #10451.
2020-10-22 08:50:10 +02:00
José Valim 3f3e987a23 Handle aliases with code recursion
It addresses a regression introduced in 97e8b0242d.

Closes https://github.com/nerves-project/nerves/issues/557.
2020-10-20 20:33:58 +02:00
José Valim 53c8b27ee2 Fix bootstrap issue in Code module 2020-10-17 20:10:26 +02:00
José Valim 3550dd10af Do not crash when getting docs for missing erts appdir
Closes #10437.
2020-10-17 19:46:51 +02:00
José Valim 7e44b88122 Raise for is_record guards, closes #10436 2020-10-17 19:33:10 +02:00
José Valim 5c5259826f Release v1.11.1 2020-10-16 09:31:35 +02:00
Xavier Noria ccdefd904f Improves the docs of config/runtime.exs and Config.config_env/0 (#10414) 2020-10-16 09:23:13 +02:00
José Valim ff95a59ec0 Add :append to stream_mode, closes #10421 2020-10-16 09:22:44 +02:00
José Valim 4081590742 Add inet6 fallback to Mix usage of httpc, closes #10423 2020-10-16 09:22:16 +02:00
José Valim 693df5fe8c Properly handle tags inside typespec in Erlang docs, closes #10430 2020-10-16 09:22:11 +02:00
José Valim b4605374b5 Do not deadlock Logger if handler crashes on sync mode, closes #10420 2020-10-12 09:27:32 +02:00
Eksperimental 36ec08e0c8 Fix warning message in app requirement (#10417)
It was missing a closing square bracket.

Previously it was saying

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

Now

  3. In case you don't want to add a requirement to :crypto, you may optionally skip this warning by adding [xref: [exclude: :crypto]] to your "def project" in mix.exs
2020-10-11 00:15:59 +02:00
José Valim ae0ac9b3c9 Ignore tracers if lexical tracker is dead or explicitly nil 2020-10-11 00:15:41 +02:00
Milton Mazzarri e3479831ba Do not show warning when super is in GenServer.child_spec/1 (#10418)
The original goal[1] was to deprecate the usage of `super` on every `GenServer`
callback, but `child_spec/1` is not a callback, it's a default implementation
that can be re-implemented.

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

Fixes: #10415

[1] https://github.com/elixir-lang/elixir/commit/c024b0eeb2dc69a759fe5ceeb1c8216d58ae9386#diff-ed0344c13ed25389035f337d94902894R301-R316
2020-10-11 00:15:40 +02:00
José Valim ffbcc004ec Do not crash when :reduce is set to nil in comprehensions, closes #10411 2020-10-08 15:37:53 +02:00
José Valim e0d61c7875 Fix order dependent tests in logger suite 2020-10-08 09:43:51 +02:00
Michał Łępicki b2059031a6 Fix returning errors in Module.Types.Expr.of_expr (#10410) 2020-10-08 09:37:13 +02:00
José Valim ed2829d8ea Keep warnings when traversing clauses 2020-10-07 23:53:23 +02:00
José Valim 081f99cd37 Consider targets when filtering apps, closes #10405 2020-10-06 19:31:36 +02:00
José Valim 7837cc5c59 Release v1.11.0 2020-10-06 15:43:09 +02:00
Daniil Fedotov b96fc39422 Restrict Logger.configure on config_change callback (#10388)
The `config_change` callback is called from `application_controller` process,
which is receiving env updates on releases. `Logger.configure` does a blocking
call to the `Logger` process, which calls `Application.put_env`

This causes a crash on logger config update.

The fix is to call `Logger.configure` only for the config that have not yet
been persisted.
2020-10-03 08:33:31 +02:00
Waldemar Rachwał e1cb898198 Take --dot-iex into account after shell respawn (#10384) 2020-10-01 20:36:14 +02:00
Dave Lucia 88e2894925 Enum.max_by/3 and Enum.min_by/3 dialyzer fixes (#10383) 2020-10-01 13:23:19 +02:00
José Valim f7cb5ae053 Fix inference of open maps with non-singleton keys
Closes #10371.
2020-09-29 13:11:22 +02:00
José Valim b4845e6854 Update iso.ex 2020-09-25 13:34:38 +02:00
José Valim 0f1200f99b Add a note on async_stream+take 2020-09-25 13:33:15 +02:00
José Valim cec1a7190b Make config/runtime.exs deep merge, closes #10365 2020-09-25 13:32:54 +02:00
Gabriel Alves 53d1b98a05 Raise instead of crashing on when trying to use list operators in guards (#10363) 2020-09-23 14:30:39 +02:00
José Valim 4828f11cd2 Keep invoking loadconfig on cli boot 2020-09-22 15:47:35 +02:00
José Valim 501271cad0 Make replace and replace! in Keyword consistent, closes #10345 2020-09-18 16:13:16 +02:00
José Valim fd4a6ad5cf Handle empty map in other places, closes #10343 2020-09-18 10:53:56 +02:00
José Valim 196aab87ca Align doc chunks return with EEP 48 2020-09-18 10:41:39 +02:00
José Valim 59b266506a Append space on middle and start expr if necessary, closes #10340 2020-09-17 17:11:28 +02:00
José Valim 1c8e625d77 Speed up compilation with multiple describe 2020-09-17 14:24:54 +02:00
José Valim f9d7adbfdf Introduce register_test/6 to speed up test definition 2020-09-17 10:57:00 +02:00
José Valim f1721c8a58 Reduce amount of injected code in case 2020-09-17 10:29:40 +02:00
José Valim 57abca4c16 Properly handle reduce in types, closes #10224 2020-09-14 23:02:07 +02:00
José Valim 8e9ece0d62 More typing improvements (#10333)
1. Move push_expr_stack to unify and keep helpers typing free

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

3. Properly process all variables found missing on step 2
2020-09-14 22:45:34 +02:00
José Valim 54ba096d34 Remove boolean and number as builtin types (#10329) 2020-09-14 22:45:34 +02:00
José Valim 6042fd0bcc Infer -> Unify 2020-09-14 22:45:34 +02:00
Eksperimental 475b73b46f Use proper casing in CHANGELOG (#10331) 2020-09-14 15:24:51 +02:00
José Valim 3cf6069ad9 Fix typo in CHANGELOG 2020-09-14 09:43:19 +02:00
José Valim 48cee5aaa5 Release v1.11.0-rc.0 2020-09-14 09:16:34 +02:00
494 changed files with 22698 additions and 70768 deletions
+3 -4
View File
@@ -12,21 +12,20 @@ test_template: &DEFAULT_TEST_SETTINGS
test_freebsd_task:
<<: *DEFAULT_TEST_SETTINGS
name: FreeBSD 13.0
name: FreeBSD 12.1
alias: FreeBSD Stable
freebsd_instance:
image_family: freebsd-13-0
image_family: freebsd-12-1
cpu: 8
memory: 7424Mi
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
PATH: $PATH:/usr/local/lib/erlang24/bin
install_script:
- pkg install -y erlang-runtime24 git gmake
- pkg install -y erlang git gmake
- rm -rf .git
- gmake compile
+3 -5
View File
@@ -1,10 +1,9 @@
[
inputs: [
"lib/*/{lib,unicode,test}/**/*.{ex,exs}",
"lib/*/*.exs",
"lib/ex_unit/examples/*.exs",
".formatter.exs"
"lib/*/mix.exs"
],
locals_without_parens: [
# Formatter tests
assert_format: 2,
@@ -14,6 +13,5 @@
# Errors tests
assert_eval_raise: 3
],
normalize_bitstring_modifiers: false
]
]
+20
View File
@@ -0,0 +1,20 @@
### Precheck
* Do not use the issue tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
* For bugs, do a quick search and make sure the bug has not yet been reported
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang/OTP versions (elixir --version):
* Operating system:
### Current behavior
Include code samples, errors and stacktraces if appropriate.
### Expected behavior
A short description on how you expect the code to behave.
-11
View File
@@ -1,11 +0,0 @@
---
blank_issues_enabled: true
contact_links:
- name: Discuss proposals
url: https://groups.google.com/g/elixir-lang-core
about: Send proposals for new ideas to our mailing list
- name: Ask questions and support
url: https://elixirforum.com/
about: Ask questions, provide support and more on Elixir Forum
-53
View File
@@ -1,53 +0,0 @@
---
name: Report an issue
description:
Tell us about something that is not working the way we (probably) intend
body:
- type: markdown
attributes:
value: >
Thank you for contributing to Elixir! :heart:
Please, do not use this form for guidance, questions or support.
Try instead in [Elixir Forum](https://elixirforum.com),
the [IRC Chat](https://web.libera.chat/#elixir),
[Stack Overflow](https://stackoverflow.com/questions/tagged/elixir),
[Slack](https://elixir-slackin.herokuapp.com),
[Discord](https://discord.gg/elixir) or in other online communities.
- type: textarea
id: elixir-and-otp-version
attributes:
label: Elixir and Erlang/OTP versions
description: Paste the output of `elixir --version` here.
validations:
required: true
- type: input
id: os
attributes:
label: Operating system
description: The operating system that this issue is happening on.
validations:
required: true
- type: textarea
id: current-behavior
attributes:
label: Current behavior
description: >
Include code samples, errors, and stacktraces if appropriate.
If reporting a bug, please include the reproducing steps.
validations:
required: true
- type: textarea
id: expected-behavior
attributes:
label: Expected behavior
description: A short description on how you expect the code to behave.
validations:
required: true
-6
View File
@@ -1,6 +0,0 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
+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:
+15 -41
View File
@@ -8,35 +8,29 @@ env:
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
permissions:
contents: read
jobs:
test_linux:
name: Linux, ${{ matrix.otp_release }}, Ubuntu 20.04
name: Linux, ${{ matrix.otp_release }}, Ubuntu 16.04
continue-on-error: ${{ matrix.development }}
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-24.0', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0', 'OTP-21.3.8', 'OTP-21.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-16.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Erlang/OTP
run: |
cd $RUNNER_TEMP
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-20.04/${{ matrix.otp_release }}.tar.gz
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-16.04/${{ matrix.otp_release }}.tar.gz
mkdir -p otp
tar zxf otp.tar.gz -C otp --strip-components=1
otp/Install -minimal $(pwd)/otp
@@ -45,52 +39,34 @@ jobs:
run: |
rm -rf .git
make compile
echo "$PWD/bin" >> $GITHUB_PATH
- name: Build info
run: bin/elixir --version
- name: Check format
run: make test_formatted && echo "All Elixir source code files are properly formatted."
- name: Run Dialyzer
- name: Dyalizer
run: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
- name: Erlang test suite
run: make test_erlang
continue-on-error: ${{ matrix.development }}
- name: Elixir test suite
run: make test_elixir
continue-on-error: ${{ matrix.development }}
- name: Check reproducible builds
run: taskset 1 make check_reproducible
if: ${{ matrix.otp_latest }}
- name: Build docs
if: ${{ matrix.otp_latest }}
run: |
git config --global advice.detachedHead false
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
for branch in main v${EX_DOC_LATEST_STABLE_VERSION}; do
echo "Building docs with ExDoc ${branch}"
cd ..
git clone https://github.com/elixir-lang/ex_doc.git --branch ${branch} --depth 1
cd ex_doc
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
cd ../elixir/
make docs
rm -rf ../ex_doc/
done
if: matrix.otp_release == 'OTP-23.0'
test_windows:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
strategy:
matrix:
otp_release: ['23.3']
otp_release: ['22.0']
runs-on: windows-2019
steps:
- name: Configure Git
run: git config --global core.autocrlf input
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Cache Erlang/OTP package
uses: actions/cache@v3
uses: actions/cache@v2
with:
path: C:\Users\runneradmin\AppData\Local\Temp\chocolatey\erlang
key: OTP-${{ matrix.otp_release }}-windows-2019
@@ -107,15 +83,13 @@ jobs:
- name: Erlang test suite
run: make --keep-going test_erlang
- name: Elixir test suite
run: |
del c:/Windows/System32/drivers/etc/hosts
make --keep-going test_elixir
run: make --keep-going test_elixir
check_posix_compliant:
name: Check POSIX-compliant
runs-on: ubuntu-20.04
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
with:
fetch-depth: 50
- name: Install Shellcheck
-72
View File
@@ -1,72 +0,0 @@
# #!/usr/bin/env elixir
[tag] = System.argv()
Mix.install([
{:req, "~> 0.2.1"},
{:jason, "~> 1.0"}
])
%{status: 200, body: release} =
Req.get!("https://api.github.com/repos/elixir-lang/elixir/releases/tags/#{tag}")
if release["draft"] do
raise "cannot notify a draft release"
end
## Notify on elixir-lang-ann
names_and_checksums =
for asset <- release["assets"],
name = asset["name"],
name =~ ~r/.sha\d+sum$/,
do: {name, Req.get!(asset["browser_download_url"]).body}
line_items =
for {name, checksum_and_name} <- Enum.sort(names_and_checksums) do
[checksum | _] = String.split(checksum_and_name, " ")
root = Path.rootname(name)
"." <> type = Path.extname(name)
" * #{root} - #{type} - #{checksum}\n"
end
mail = %{
"From" => "jose.valim@dashbit.co",
"To" => "elixir-lang-ann@googlegroups.com",
"Subject" => "Elixir #{tag} released",
"HtmlBody" => "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{line_items}",
"MessageStream" => "outbound"
}
if System.get_env("DRYRUN") do
IO.puts("MAIL")
IO.inspect(mail)
else
headers = %{
"X-Postmark-Server-Token" => System.fetch_env!("ELIXIR_LANG_ANN_TOKEN")
}
resp = Req.post!("https://api.postmarkapp.com/email", {:json, mail}, headers: headers)
IO.puts("#{resp.status} elixir-lang-ann\n#{inspect(resp.body)}")
end
## Notify on Elixir Forum
post = %{
"title" => "Elixir #{tag} released",
"raw" => "https://github.com/elixir-lang/elixir/releases/tag/#{tag}\n\n#{release["body"]}",
# Elixir News
"category" => 28
}
if System.get_env("DRYRUN") do
IO.puts("POST")
IO.inspect(post)
else
headers = %{
"api-key" => System.fetch_env!("ELIXIR_FORUM_TOKEN"),
"api-username" => "Elixir"
}
resp = Req.post!("https://elixirforum.com/posts.json", {:json, post}, headers: headers)
IO.puts("#{resp.status} Elixir Forum\n#{inspect(resp.body)}")
end
-28
View File
@@ -1,28 +0,0 @@
name: Notify
on:
release:
types:
- published
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-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 }}
+406 -510
View File
File diff suppressed because it is too large Load Diff
+10 -16
View File
@@ -13,23 +13,17 @@ The goal of the Code of Conduct is to specify a baseline standard of behavior so
These are the values Elixir developers should aspire to:
* Be friendly and welcoming
* Be kind
* Be patient
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
* Interpret the arguments of others in good faith, do not seek to disagree.
* When we do disagree, try to understand why.
* Be thoughtful
* Productive communication requires effort. Think about how your words will be interpreted.
* Remember that sometimes it is best to refrain entirely from commenting.
* Be respectful
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
* Be constructive
* Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation.
* Avoid unconstructive criticism: don't merely decry the current state of affairs; offer — or at least solicit — suggestions as to how things may be improved.
* Avoid harsh words and stern tone: we are all aligned towards the well-being of the community and the progress of the ecosystem. Harsh words exclude, demotivate, and lead to unnecessary conflict.
* Avoid snarking (pithy, unproductive, sniping comments).
* Avoid microaggressions (brief and commonplace verbal, behavioral and environmental indignities that communicate hostile, derogatory or negative slights and insults towards a project, person or group).
* Be responsible
* What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
* Avoid destructive behavior
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
* Snarking (pithy, unproductive, sniping comments).
The following actions are explicitly forbidden:
@@ -47,11 +41,11 @@ Explicit enforcement of the Code of Conduct applies to the official mediums oper
* The [official GitHub projects][1] and code reviews.
* The official elixir-lang mailing lists.
* The **[#elixir][2]** IRC channel on [Libera.Chat][3].
* The **[#elixir-lang][2]** IRC channel on [Freenode][3].
Other Elixir activities (such as conferences, meetups, and unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
Project maintainers may block, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
@@ -59,8 +53,8 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be repor
## Acknowledgements
This document was based on the Code of Conduct from the Go project (dated Sep/2021) and the Contributor Covenant (v1.4).
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
[1]: https://github.com/elixir-lang/
[2]: https://web.libera.chat/#elixir
[3]: https://libera.chat/
[2]: https://webchat.freenode.net/?channels=#elixir-lang
[3]: https://www.freenode.net
+27 -18
View File
@@ -2,9 +2,7 @@ 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 := v1.11/
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 +27,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 >= 21)])' -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 21.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -84,7 +82,7 @@ $(PARSER): lib/elixir/src/elixir_parser.yrl
# Since Mix depends on EEx and EEx depends on Mix,
# we first compile EEx without the .app file,
# then Mix, and then compile EEx fully
# then Mix and then compile EEx fully
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
stdlib: $(KERNEL) VERSION
@@ -93,10 +91,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 +104,7 @@ unicode: $(UNICODE)
$(UNICODE): lib/elixir/unicode/*
@ echo "==> unicode (compile)";
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/security.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/properties.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
@@ -128,7 +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
@@ -173,14 +171,15 @@ clean_residual_files:
rm -rf lib/mix/test/fixtures/git_rebar/
rm -rf lib/mix/test/fixtures/git_repo/
rm -rf lib/mix/test/fixtures/git_sparse_repo/
rm -rf lib/mix/test/fixtures/archive/ebin/
rm -f erl_crash.dump
$(Q) "$(MAKE)" clean_man
$(Q) $(MAKE) clean_man
#==> Documentation tasks
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}")
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 +234,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
@@ -276,7 +285,7 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
$(Q) mkdir -p $(TEST_EBIN)
$(Q) $(ERLC) -o $(TEST_EBIN) $<
test_elixir: test_stdlib test_ex_unit test_logger test_eex test_iex test_mix
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
test_stdlib: compile
@ echo "==> elixir (ex_unit)"
@@ -294,7 +303,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 +342,4 @@ install_man: build_man
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(MAN_PREFIX)/man1
"$(MAKE)" clean_man
$(MAKE) clean_man
-1
View File
@@ -22,7 +22,6 @@ limitations under the License.
== All other files
Copyright 2012 Plataformatec
Copyright 2021 The Elixir Team
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
+47 -76
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">
# ![Elixir](https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png)
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=main)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amain+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=main)](https://cirrus-ci.com/github/elixir-lang/elixir)
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=master)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=master)](https://cirrus-ci.com/github/elixir-lang/elixir)
Elixir is a dynamic, functional language designed for building scalable
and maintainable applications.
@@ -12,14 +11,11 @@ For more about Elixir, installation and documentation,
## Policies
New releases are announced in the [announcement mailing list][8].
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com
and replying to the confirmation email.
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
All security releases [will be tagged with `[security]`][10]. For more
information, please read our [Security Policy][9].
All security releases [will be tagged with `[security]`][10]. For more information, please read our [Security Policy][9].
All interactions in our official communication channels follow our
[Code of Conduct][1].
All interactions in our official communication channels follow our [Code of Conduct][1].
## Bug reports
@@ -27,56 +23,13 @@ For reporting bugs, [visit our issue tracker][2] and follow the steps
for reporting a new issue. **Please disclose security vulnerabilities
privately at elixir-security@googlegroups.com**.
## Issues tracker management
All currently open bugs related to the Elixir repository are listed
in the issues tracker. The Elixir team uses the issues tracker to focus
on *actionable items*, including planned enhancements in the short- and
medium-term. We also do our best to label entries for clarity and to ease
collaboration.
Our *actionable item policy* has some important consequences, such as:
* Proposing new features as well as request for support, help, and
guidance must be done in their own spaces, detailed next.
* Issues where we have identified to be outside of Elixir scope,
such as a bug upstream, will be closed (and requested to be moved
elsewhere if appropriate).
* We actively close unrelated and non-actionable issues to keep the
issues tracker tidy. However, we may get things wrong from time to
time, so we are glad to revisit issues and reopen if necessary.
Keep the tone positive and be kind! For more information, see the
[Code of Conduct][1].
### Proposing new features
For proposing new features, please start a discussion in the
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
to argue and explain why a feature is useful and how it will impact the
codebase and the community.
Once a proposal is accepted, it will be added to [the issue tracker][2].
Features and bug fixes that have already been merged and will be included
in the next release are then "closed" and added to the [changelog][7].
### Discussions, support, and help
For general discussions, support, and help, please use many of the community
spaces [listed on the sidebar of the Elixir website](https://elixir-lang.org/),
such as forums, chat platforms, etc, where the wider community will be available
to help you.
## Compiling from source
For the many different ways to install Elixir,
[see our installation instructions on the website](https://elixir-lang.org/install.html).
However, if you want to contribute to Elixir, you will need to compile from source.
To compile from source, you can follow the steps below.
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang).
After that, clone this repository to your machine, compile and test it:
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then clone this repository to your machine, compile and test it:
```sh
git clone https://github.com/elixir-lang/elixir.git
@@ -88,13 +41,37 @@ make clean test
[this article includes important notes for compiling Elixir from source
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
In case you want to use this Elixir version as your system version,
you need to add the `bin` directory to [your PATH environment variable](https://elixir-lang.org/install.html#setting-path-environment-variable).
If Elixir fails to build (specifically when pulling in a new version via
`git`), be sure to remove any previous build artifacts by running
`make clean`, then `make test`.
If tests pass, you can use Interactive Elixir by running `bin/iex` in your terminal.
However, if tests fail, it is likely that you have an outdated Erlang/OTP version
(Elixir requires Erlang/OTP 21.0 or later). You can check your Erlang/OTP version
by calling `erl` in the command line. You will see some information similar to:
Erlang/OTP 21 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
If you have properly set up your dependencies and tests still fail,
you may want to open up a bug report, as explained next.
## Proposing new features
For proposing new features, please start a discussion in the
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
to argue and explain why a feature is useful and how it will impact the
codebase and the community.
Once a proposal is accepted, it will be added to [the issue tracker][2].
The issue tracker focuses on *actionable items* and it holds a list of
upcoming enhancements and pending bugs. All entries in the tracker are
tagged for clarity and to ease collaboration.
Features and bug fixes that have already been merged and will be included
in the next release are marked as "closed" in the issue tracker and are
added to the [changelog][7].
## Contributing
We welcome everyone to contribute to Elixir. To do so, there are a few
@@ -115,7 +92,7 @@ in applications inside the `lib` folder:
You can run all tests in the root directory with `make test` and you can
also run tests for a specific framework `make test_#{APPLICATION}`, for example,
`make test_ex_unit`. If you just changed something in Elixir's standard
`make test_ex_unit`. If you just changed something in the Elixir's standard
library, you can run only that portion through `make test_stdlib`.
If you are changing just one file, you can choose to compile and run tests only
@@ -127,12 +104,6 @@ bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
bin/elixir lib/elixir/test/elixir/string_test.exs
```
You can also use the `LINE` env var to run a single test:
```sh
LINE=123 bin/elixir lib/elixir/test/elixir/string_test.exs
````
To recompile (including Erlang modules):
```sh
@@ -152,7 +123,7 @@ make clean_elixir compile
Similarly, if you can't get Elixir to compile or the tests to pass after
updating an existing checkout, run `make clean compile`. You can check
[the official build status](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml).
[the official build status on Cirrus CI](https://cirrus-ci.com/github/elixir-lang/elixir).
More tasks can be found by reading the [Makefile](Makefile).
With tests running and passing, you are ready to contribute to Elixir and
@@ -160,9 +131,9 @@ With tests running and passing, you are ready to contribute to Elixir and
We have saved some excellent pull requests we have received in the past in
case you are looking for some examples:
* [Implement Enum.member? - Pull request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull request](https://github.com/elixir-lang/elixir/pull/1059)
* [Implement Enum.member? - Pull Request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
### Reviewing changes
@@ -171,12 +142,12 @@ We outline our process below to clarify the roles of everyone involved.
All pull requests must be approved by two committers before being merged into
the repository. If any changes are necessary, the team will leave appropriate
comments requesting changes to the code. Unfortunately, we cannot guarantee a
comments requesting changes to the code. Unfortunately we cannot guarantee a
pull request will be merged, even when modifications are requested, as the Elixir
team will re-evaluate the contribution as it changes.
Committers may also push style changes directly to your branch. If you would
rather manage all changes yourself, you can disable the "Allow edits from maintainers"
rather manage all changes yourself, you can disable "Allow edits from maintainers"
feature when submitting your pull request.
The Elixir team may optionally assign someone to review a pull request.
@@ -195,8 +166,8 @@ to be installed and built alongside Elixir:
```sh
# After cloning and compiling Elixir, in its parent directory:
git clone https://github.com/elixir-lang/ex_doc.git
cd ex_doc && ../elixir/bin/mix do deps.get + compile
git clone git://github.com/elixir-lang/ex_doc.git
cd ex_doc && ../elixir/bin/mix do deps.get, compile
```
Now go back to Elixir's root directory and run:
@@ -219,13 +190,13 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
* [Issue tracker][2]
* [Changelog][7]
* [Security Policy][9]
* **[#elixir][4]** on [Libera.Chat][5] IRC
* **[#elixir-lang][4]** on [Freenode][5] IRC
[1]: CODE_OF_CONDUCT.md
[2]: https://github.com/elixir-lang/elixir/issues
[3]: https://groups.google.com/group/elixir-lang-core
[4]: https://web.libera.chat/#elixir
[5]: https://libera.chat
[4]: https://webchat.freenode.net/?channels=#elixir-lang
[5]: https://www.freenode.net
[6]: https://elixir-lang.org/docs.html
[7]: CHANGELOG.md
[8]: https://groups.google.com/group/elixir-lang-ann
@@ -234,7 +205,7 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
## License
"Elixir" and the Elixir logo are registered trademarks of The Elixir Team.
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
Elixir source code is released under Apache License 2.0.
+15 -19
View File
@@ -4,19 +4,25 @@
1. Ensure you are running on the oldest supported Erlang version
2. Update version in /VERSION, bin/elixir and bin/elixir.bat
2. Update version in /VERSION
3. Ensure /CHANGELOG.md is updated, versioned and add the current date
4. Update "Compatibility and Deprecations" if a new OTP version is supported
5. Commit changes above with title "Release vVERSION", generate a new tag, and push it
5. Commit changes above with title "Release vVERSION" and generate a new tag
6. Wait until GitHub Actions publish artifacts to the draft release and the CI is green
6. Run `make clean test` to ensure all tests pass from scratch and the CI is green
7. Copy the relevant bits from /CHANGELOG.md to the GitHub release and publish it
7. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
8. Add the release to `elixir.csv` with the minimum supported OTP version (all releases), update `erlang.csv` to the latest supported OTP version, and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
8. Push branch and the new tag
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases, and include SHAs+CHANGELOG
10. Add the release to `elixir.csv` (all releases), update `erlang.csv` to the precompiled OTP version, and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
11. Send an e-mail to elixir-lang-ann@googlegroups.com with title "Elixir vVERSION released". The body should be a link to the Release page on GitHub and the checksums. If it is a security release, prefix the title with the `[security]` tag
## Creating a new vMAJOR.MINOR branch
@@ -26,24 +32,14 @@
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
3. Commit "Branch out vMAJOR.MINOR"
3. Commit "Prepare vMAJOR.MINOR for release"
### Back in main
### Back in master
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
1. Bump /VERSION file
2. Start new /CHANGELOG.md
3. Update tables in /SECURITY.md and in "Compatibility and Deprecations"
3. Update tables in /SECURITY.md in "Compatibility and Deprecations"
4. Commit "Start vMAJOR.MINOR+1"
## Changing supported Erlang/OTP versions
1. Update the table in Compatibility and Deprecations
2. Update `otp_release` checks in /Makefile and `/lib/elixir/src/elixir.erl`
3. Update CI workflows in `/.cirrus.yml`, `/.github/workflows/ci.yml`, and `/.github/workflows/releases.yml`
4. Remove `otp_release` version checks that are no longer needed
+8 -8
View File
@@ -4,19 +4,19 @@
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
Elixir version | Support
:------------- | :-----------------------------
1.14 | Bug fixes and security patches
1.13 | Security patches only
1.12 | Security patches only
1.11 | Security patches only
1.10 | Security patches only
| Elixir version | Support
| -------------- | ------------------------------
| 1.11 | Bug fixes and security patches
| 1.10 | Security patches only
| 1.9 | Security patches only
| 1.8 | Security patches only
| 1.7 | Security patches only
## Announcements
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
Security notifications [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
## Reporting a vulnerability
+1 -1
View File
@@ -1 +1 @@
1.14.5
1.11.4
+31 -49
View File
@@ -1,22 +1,20 @@
#!/bin/sh
set -e
ELIXIR_VERSION=1.14.5
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
cat <<USAGE >&2
Usage: $(basename "$0") [options] [.exs file] [data]
## General options
-e "COMMAND" Evaluates the given command (*)
-h, --help Prints this message (standalone)
-h, --help Prints this message and exits
-r "FILE" Requires the given files/patterns (*)
-S SCRIPT Finds and executes the given script in \$PATH
-S SCRIPT   Finds and executes the given script in \$PATH
-pr "FILE" Requires the given files/patterns in parallel (*)
-pa "PATH" Prepends the given path to Erlang code path (*)
-pz "PATH" Appends the given path to Erlang code path (*)
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
-v, --version Prints Elixir version and exits
--app APP Starts the given app and its dependencies (*)
--erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -24,7 +22,6 @@ Usage: $(basename "$0") [options] [.exs file] [data]
--logger-otp-reports BOOL Enables or disables OTP reporting
--logger-sasl-reports BOOL Enables or disables SASL reporting
--no-halt Does not halt the Erlang VM after execution
--short-version Prints Elixir version (standalone)
--werl Uses Erlang's Windows shell GUI (Windows only)
Options given after the .exs file or -- are passed down to the executed code.
@@ -55,7 +52,6 @@ It will attempt to create PIPEDIR and LOGDIR if they don't exist.
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
** Options marked with (*) can be given more than once.
** Standalone options can't be combined with other options.
USAGE
exit 1
fi
@@ -70,16 +66,11 @@ readlink_f () {
fi
}
if [ $# -eq 1 ] && [ "$1" = "--short-version" ]; then
echo "$ELIXIR_VERSION"
exit 0
fi
# Stores static Erlang arguments and --erl (which is passed as is)
ERL=""
# Stores erl arguments preserving spaces/quotes (mimics an array)
erl_set () {
erl () {
eval "E${E}=\$1"
E=$((E + 1))
}
@@ -100,29 +91,32 @@ LENGTH=$#
set -- "$@" -extra
while [ $I -le $LENGTH ]; do
# S counts to be shifted, C counts to be copied
S=0
C=0
S=1
case "$1" in
+iex)
C=1
set -- "$@" "$1"
MODE="iex"
;;
+elixirc)
C=1
set -- "$@" "$1"
MODE="elixirc"
;;
-v|--no-halt|--no-pry)
C=1
-v|--no-halt)
set -- "$@" "$1"
;;
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex)
C=2
S=2
set -- "$@" "$1" "$2"
;;
--rpc-eval)
C=3
S=3
set -- "$@" "$1" "$2" "$3"
;;
--detached)
echo "warning: the --detached option is deprecated" >&2
ERL="$ERL -detached"
;;
--hidden)
S=1
ERL="$ERL -hidden"
;;
--logger-otp-reports)
@@ -143,34 +137,34 @@ while [ $I -le $LENGTH ]; do
;;
--cookie)
S=2
erl_set "-setcookie"
erl_set "$2"
erl "-setcookie"
erl "$2"
;;
--sname|--name)
S=2
erl_set "$(echo "$1" | cut -c 2-)"
erl_set "$2"
erl "$(echo "$1" | cut -c 2-)"
erl "$2"
;;
--erl-config)
S=2
erl_set "-config"
erl_set "$2"
erl "-config"
erl "$2"
;;
--vm-args)
S=2
erl_set "-args_file"
erl_set "$2"
erl "-args_file"
erl "$2"
;;
--boot)
S=2
erl_set "-boot"
erl_set "$2"
erl "-boot"
erl "$2"
;;
--boot-var)
S=3
erl_set "-boot_var"
erl_set "$2"
erl_set "$3"
erl "-boot_var"
erl "$2"
erl "$3"
;;
--pipe-to)
S=3
@@ -183,7 +177,6 @@ while [ $I -le $LENGTH ]; do
fi
;;
--werl)
S=1
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
;;
*)
@@ -196,13 +189,6 @@ while [ $I -le $LENGTH ]; do
;;
esac
while [ $I -le $LENGTH ] && [ $C -gt 0 ]; do
C=$((C - 1))
I=$((I + 1))
set -- "$@" "$1"
shift
done
I=$((I + S))
shift $S
done
@@ -224,11 +210,7 @@ if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
fi
# One MAY change ERTS_BIN= but you MUST NOT change
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
ERTS_BIN=
ERTS_BIN="$ERTS_BIN"
set -- "$ERTS_BIN$ERL_EXEC" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
if [ -n "$RUN_ERL_PIPE" ]; then
+11 -36
View File
@@ -1,14 +1,10 @@
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
set ELIXIR_VERSION=1.14.5
setlocal enabledelayedexpansion
if ""%1""=="""" if ""%2""=="""" goto documentation
if /I ""%1""==""--help"" if ""%2""=="""" goto documentation
if /I ""%1""==""-h"" if ""%2""=="""" goto documentation
if /I ""%1""==""/h"" if ""%2""=="""" goto documentation
if ""%1""==""/?"" if ""%2""=="""" goto documentation
if /I ""%1""==""--short-version"" if ""%2""=="""" goto shortversion
if ""%1""=="""" goto documentation
if /I ""%1""==""--help"" goto documentation
if /I ""%1""==""-h"" goto documentation
if /I ""%1""==""/h"" goto documentation
if ""%1""==""/?"" goto documentation
goto parseopts
:documentation
@@ -17,13 +13,13 @@ echo.
echo ## General options
echo.
echo -e "COMMAND" Evaluates the given command (*)
echo -h, --help Prints this message (standalone)
echo -h, --help Prints this message and exits
echo -r "FILE" Requires the given files/patterns (*)
echo -S SCRIPT Finds and executes the given script in $PATH
echo -pr "FILE" Requires the given files/patterns in parallel (*)
echo -pa "PATH" Prepends the given path to Erlang code path (*)
echo -pz "PATH" Appends the given path to Erlang code path (*)
echo -v, --version Prints Erlang/OTP and Elixir versions (standalone)
echo -v, --version Prints Elixir version and exits
echo.
echo --app APP Starts the given app and its dependencies (*)
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -31,7 +27,6 @@ echo --eval "COMMAND" Evaluates the given command, same as -e (*)
echo --logger-otp-reports BOOL Enables or disables OTP reporting
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
echo --no-halt Does not halt the Erlang VM after execution
echo --short-version Prints Elixir version (standalone)
echo --werl Uses Erlang's Windows shell GUI (Windows only)
echo.
echo Options given after the .exs file or -- are passed down to the executed code.
@@ -59,11 +54,6 @@ echo.
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
echo.
echo ** Options marked with (*) can be given more than once.
echo ** Standalone options can't be combined with other options.
goto end
:shortversion
echo !ELIXIR_VERSION!
goto end
:parseopts
@@ -88,7 +78,6 @@ set SCRIPT_PATH=%~dp0
rem Designates the path to the ERTS system
set ERTS_BIN=
set ERTS_BIN=!ERTS_BIN!
rem Recursive loop called for each parameter that parses the cmd line parameters
:startloop
@@ -110,21 +99,18 @@ if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixi
rem ******* EVAL PARAMETERS ************************
if ""==!par:-e=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--eval=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--rpc-eval=! (
set "VAR=%~2"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
shift
shift
@@ -136,12 +122,10 @@ if ""==!par:-pr=! (set "parsElixir=!parsElixir! -pr %1" && shift && goto
if ""==!par:-pa=! (set "parsElixir=!parsElixir! -pa %1" && shift && goto startloop)
if ""==!par:-pz=! (set "parsElixir=!parsElixir! -pz %1" && shift && goto startloop)
if ""==!par:-v=! (set "parsElixir=!parsElixir! -v" && goto startloop)
if ""==!par:--version=! (set "parsElixir=!parsElixir! --version" && goto startloop)
if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && goto startloop)
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
if ""==!par:--dot-iex=! (set "parsElixir=!parsElixir! --dot-iex %1" && shift && goto startloop)
if ""==!par:--no-pry=! (set "parsElixir=!parsElixir! --no-pry" && goto startloop)
rem ******* ERLANG PARAMETERS **********************
if ""==!par:--boot=! (set "parsErlang=!parsErlang! -boot %1" && shift && goto startloop)
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var %1 %2" && shift && shift && goto startloop)
@@ -175,19 +159,10 @@ if %errorlevel% == 0 (
if not !runMode! == "iex" (
set beforeExtra=-noshell -s elixir start_cli !beforeExtra!
)
if defined ELIXIR_CLI_DRY_RUN (
if defined useWerl (
echo start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
echo "!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
if defined useWerl (
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
if defined useWerl (
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
) else (
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
exit /B %ERRORLEVEL%
:end
endlocal
endlocal
+2 -3
View File
@@ -7,14 +7,13 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
-h, --help Prints this message and exits
-o The directory to output compiled files
-v, --version Prints Elixir version and exits (standalone)
-v, --version Prints Elixir version and exits
--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
--no-docs Does not attach documentation to compiled modules
--profile time Profile the time to compile modules
--verbose Prints compilation status
--warnings-as-errors Treats warnings as errors and return non-zero exit status
--warnings-as-errors Treats warnings as errors and return non-zero exit code
Options given after -- are passed down to the executed code.
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS.
+3 -4
View File
@@ -14,16 +14,15 @@ goto run
:documentation
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
echo.
echo -h, --help Prints this message and exits
echo -o The directory to output compiled files
echo -v, --version Prints Elixir version and exits (standalone)
echo.
echo --help, -h Prints this message and exits
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
echo --no-debug-info Does not attach debug info to compiled modules
echo --no-docs Does not attach documentation to compiled modules
echo --profile time Profile the time to compile modules
echo --verbose Prints compilation status
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
echo --version, -v Prints Elixir version and exits
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit code
echo.
echo ** Options given after -- are passed down to the executed code
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
+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
+67 -176
View File
@@ -9,19 +9,19 @@ 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"
This module provides three main APIs for you to use:
## API
This module provides 3 main APIs for you to use:
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
directly. This is the simplest API to use but also the
slowest, since the code is evaluated at runtime and not precompiled.
slowest, since the code is evaluated and not compiled before.
2. Define a function from a string (`function_from_string/5`)
or a file (`function_from_file/5`). This allows you to embed
@@ -34,48 +34,19 @@ 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`.
file the template is read from or to "nofile" when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to 1.
* `:indentation` - (since v1.11.0) an integer added to the column after every
new line. Defaults to `0`.
new line. Defaults to 0.
* `:engine` - the EEx engine to be used for compilation.
* `:trim` - if `true`, trims whitespace left 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:
<%| ... %>
<%/ ... %>
* `:trim` - if true, trims whitespace left/right of quotation tags up until
newlines. At least one newline is retained. Defaults to false.
## Engine
@@ -85,10 +56,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,26 +104,11 @@ 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.
Generates a function definition from the string.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`template` is the string containing the EEx template. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template.
The supported `options` are described [in the module docs](#module-options).
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
## Examples
@@ -132,11 +120,11 @@ defmodule EEx do
"3"
"""
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge([file: __ENV__.file, line: __ENV__.line], options)
args = Enum.map(args, fn arg -> {arg, [line: info[:line]], nil} end)
compiled = EEx.compile_string(template, info)
compiled = EEx.compile_string(source, info)
case kind do
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
@@ -148,17 +136,12 @@ defmodule EEx do
@doc """
Generates a function definition from the file contents.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`file` is the path to the EEx template file. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template.
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
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
@@ -177,7 +160,7 @@ defmodule EEx do
"""
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
info = Keyword.merge(options, file: file, line: 1)
args = Enum.map(args, fn arg -> {arg, [line: 1], nil} end)
compiled = EEx.compile_file(file, info)
@@ -191,86 +174,34 @@ defmodule EEx do
end
@doc """
Gets a string `source` and generates a quoted expression
Gets a string `source` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
iex> quoted = EEx.compile_string("<%= a + b %>")
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
iex> result
"3"
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
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 """
Gets a `filename` and generates a quoted expression
Gets a `filename` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
# sample.eex
<%= a + b %>
# In code:
quoted = EEx.compile_file("sample.eex")
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
result
#=> "3"
"""
@spec compile_file(Path.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.merge([file: filename, line: 1], options)
@spec compile_file(String.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
options = Keyword.merge(options, file: filename, line: 1)
compile_string(File.read!(filename), options)
end
@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")
"foo baz"
"""
@spec eval_string(String.t(), keyword, keyword) :: String.t()
@spec eval_string(String.t(), keyword, keyword) :: any
def eval_string(source, bindings \\ [], options \\ [])
when is_binary(source) and is_list(bindings) and is_list(options) do
compiled = compile_string(source, options)
@@ -280,64 +211,24 @@ 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
foo <%= bar %>
# IEx
# iex
EEx.eval_file("sample.eex", bar: "baz")
#=> "foo baz"
"""
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
@spec eval_file(String.t(), keyword, keyword) :: any
def eval_file(filename, bindings \\ [], options \\ [])
when is_list(bindings) and is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.put_new(options, :file, filename)
when is_binary(filename) and is_list(bindings) and is_list(options) do
options = Keyword.put(options, :file, filename)
compiled = compile_file(filename, options)
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
+89 -338
View File
@@ -3,345 +3,63 @@ defmodule EEx.Compiler do
# When changing this setting, don't forget to update the docs for EEx
@default_engine EEx.SmartEngine
@h_spaces [?\s, ?\t]
@all_spaces [?\s, ?\t, ?\n, ?\r]
@doc """
Tokenize EEx contents.
"""
def tokenize(contents, opts) when is_binary(contents) do
tokenize(String.to_charlist(contents), opts)
end
def tokenize(contents, opts) when is_list(contents) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
trim = opts[:trim] || false
indentation = opts[:indentation] || 0
column = indentation + (opts[:column] || 1)
state = %{trim: trim, indentation: indentation, file: file}
{contents, line, column} =
(trim && trim_init(contents, line, column, state)) || {contents, line, column}
tokenize(contents, line, column, state, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, state, buffer, acc) do
tokenize(t, line, column + 3, state, [?%, ?< | buffer], acc)
end
defp tokenize('<%!--' ++ t, line, column, state, buffer, acc) do
case comment(t, line, column + 5, state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{:ok, new_line, new_column, rest, comments} ->
token = {:comment, Enum.reverse(comments), %{line: line, column: column}}
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
end
end
# TODO: Deprecate this on Elixir v1.18
defp tokenize('<%#' ++ t, line, column, state, buffer, acc) do
case expr(t, line, column + 3, state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{:ok, _, new_line, new_column, rest} ->
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, & &1)
end
end
defp tokenize('<%' ++ t, line, column, state, buffer, acc) do
{marker, t} = retrieve_marker(t)
case expr(t, line, column + 2 + length(marker), state, []) do
{:error, line, column, message} ->
{:error, message, %{line: line, column: column}}
{:ok, expr, new_line, new_column, rest} ->
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, _line, _column, warnings, tokens} ->
Enum.each(Enum.reverse(warnings), fn {location, file, msg} ->
:elixir_errors.erl_warn(location, file, msg)
end)
token_key(tokens, expr)
{:error, _, _, _, _} ->
{:expr, expr}
end
marker =
if key in [:middle_expr, :end_expr] and marker != '' do
message =
"unexpected beginning of EEx tag \"<%#{marker}\" on \"<%#{marker}#{expr}%>\", " <>
"please remove \"#{marker}\""
:elixir_errors.erl_warn({line, column}, state.file, message)
''
else
marker
end
token = {key, marker, expr, %{line: line, column: column}}
trim_and_tokenize(rest, new_line, new_column, state, buffer, acc, &[token | &1])
end
end
defp tokenize('\n' ++ t, line, _column, state, buffer, acc) do
tokenize(t, line + 1, state.indentation + 1, state, [?\n | buffer], acc)
end
defp tokenize([h | t], line, column, state, buffer, acc) do
tokenize(t, line, column + 1, state, [h | buffer], acc)
end
defp tokenize([], line, column, _state, buffer, acc) do
eof = {:eof, %{line: line, column: column}}
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
end
defp trim_and_tokenize(rest, line, column, state, buffer, acc, fun) do
{rest, line, column, buffer} = trim_if_needed(rest, line, column, state, buffer)
acc = tokenize_text(buffer, acc)
tokenize(rest, line, column, state, [{line, column}], fun.(acc))
end
# Retrieve marker for <%
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
{[marker], t}
end
defp retrieve_marker(t) do
{'', t}
end
# Tokenize a multi-line comment until we find --%>
defp comment([?-, ?-, ?%, ?> | t], line, column, _state, buffer) do
{:ok, line, column + 4, t, buffer}
end
defp comment('\n' ++ t, line, _column, state, buffer) do
comment(t, line + 1, state.indentation + 1, state, '\n' ++ buffer)
end
defp comment([head | t], line, column, state, buffer) do
comment(t, line, column + 1, state, [head | buffer])
end
defp comment([], line, column, _state, _buffer) do
{:error, line, column, "missing token '--%>'"}
end
# Tokenize an expression until we find %>
defp expr([?%, ?> | t], line, column, _state, buffer) do
{:ok, Enum.reverse(buffer), line, column + 2, t}
end
defp expr('\n' ++ t, line, _column, state, buffer) do
expr(t, line + 1, state.indentation + 1, state, [?\n | buffer])
end
defp expr([h | t], line, column, state, buffer) do
expr(t, line, column + 1, state, [h | buffer])
end
defp expr([], line, column, _state, _buffer) do
{:error, line, column, "missing token '%>'"}
end
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens, expr) do
case {tokens, tokens |> Enum.reverse() |> drop_eol()} do
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
{:start_expr, expr}
else
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
end
end
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
defp drop_eol(rest), do: rest
defp maybe_append_space([?\s]), do: [?\s]
defp maybe_append_space([h]), do: [h, ?\s]
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, buffer, %{line: line, column: column}} | acc]
end
## Trim
defp trim_if_needed(rest, line, column, state, buffer) do
if state.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0, state)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column, state) when h in @h_spaces,
do: trim_init(t, line, column + 1, state)
defp trim_init([?\r, ?\n | t], line, _column, state),
do: trim_init(t, line + 1, state.indentation + 1, state)
defp trim_init([?\n | t], line, _column, state),
do: trim_init(t, line + 1, state.indentation + 1, state)
defp trim_init([?<, ?% | _] = rest, line, column, _state),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, last_column, state) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, state.indentation + 1, column + 1, state)
{[?\n | rest], column} ->
trim_right(rest, line + 1, state.indentation + 1, column, state)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t], column) when h in @h_spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
@doc """
This is the compilation entry point. It glues the tokenizer
and the engine together by handling the tokens and invoking
the engine every time a full expression or text is received.
"""
@spec compile([EEx.token()], keyword) :: Macro.t()
def compile(tokens, opts) do
@spec compile(String.t(), keyword) :: Macro.t()
def compile(source, opts) when is_binary(source) and is_list(opts) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
engine = opts[:engine] || @default_engine
column = 1
indentation = opts[:indentation] || 0
trim = opts[:trim] || false
tokenizer_options = %{trim: trim, indentation: indentation}
state = %{
engine: engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: parser_options
}
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
{:ok, tokens} ->
state = %{
engine: opts[:engine] || @default_engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: Code.get_compiler_option(:parser_options)
}
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
end
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
# Ignore tokens related to comment.
defp generate_buffer([{:comment, _chars, _meta} | rest], buffer, scope, state) do
generate_buffer(rest, buffer, scope, state)
{:error, line, column, message} ->
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
end
end
# Generates the buffers by handling each expression from the tokenizer.
# It returns Macro.t/0 or it raises.
defp generate_buffer([{:text, chars, meta} | rest], buffer, scope, state) do
buffer =
if function_exported?(state.engine, :handle_text, 3) do
meta = [line: meta.line, column: meta.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.
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
end
defp generate_buffer([{:text, _line, _column, chars} | rest], buffer, scope, state) do
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
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
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)
end
{rest, line, contents} = look_ahead_middle(rest, meta.line, chars) || {rest, meta.line, chars}
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
{contents, rest} =
generate_buffer(
@@ -352,8 +70,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 +80,47 @@ defmodule EEx.Compiler do
end
defp generate_buffer(
[{:middle_expr, '', chars, meta} | rest],
[{:middle_expr, line, _column, '', chars} | rest],
buffer,
[current | scope],
state
) do
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
state = %{state | line: meta.line}
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
state = %{state | line: line}
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], state)
end
defp generate_buffer([{:middle_expr, _, chars, meta} | _], _buffer, [], state) do
defp generate_buffer(
[{:middle_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
"please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn(line, state.file, message)
generate_buffer([{:middle_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line
end
defp generate_buffer([{:middle_expr, line, column, _, chars} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected middle of expression <%#{chars}%>",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
defp generate_buffer(
[{:end_expr, '', chars, meta} | rest],
[{:end_expr, line, _column, '', chars} | rest],
buffer,
[current | _],
state
) do
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
column = state.start_column
options = [file: state.file, line: state.start_line, column: column] ++ state.parser_options
tuples = Code.string_to_quoted!(wrapped, options)
@@ -394,24 +128,40 @@ defmodule EEx.Compiler do
{buffer, rest}
end
defp generate_buffer([{:end_expr, _, chars, meta} | _], _buffer, [], state) do
defp generate_buffer(
[{:end_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn(line, state.file, message)
generate_buffer([{:end_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line, column: column
end
defp generate_buffer([{:end_expr, line, column, _, chars} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected end of expression <%#{chars}%>",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
defp generate_buffer([{:eof, _meta}], buffer, [], state) do
defp generate_buffer([{:eof, _, _}], buffer, [], state) do
state.engine.handle_body(buffer)
end
defp generate_buffer([{:eof, meta}], _buffer, _scope, state) do
defp generate_buffer([{:eof, line, column}], _buffer, _scope, state) do
raise EEx.SyntaxError,
message: "unexpected end of string, expected a closing '<% end %>'",
file: state.file,
line: meta.line,
column: meta.column
line: line,
column: column
end
# Creates a placeholder and wrap it inside the expression block
@@ -428,27 +178,28 @@ defmodule EEx.Compiler do
# Look middle expressions that immediately follow a start_expr
defp look_ahead_middle([{:comment, _comment, _meta} | rest], start, contents),
do: look_ahead_middle(rest, start, contents)
defp look_ahead_middle([{:text, text, _meta} | rest], start, contents) do
defp look_ahead_middle(
[{:text, _, _, text}, {:middle_expr, line, _, _, chars} | rest] = tokens,
start,
contents
) do
if only_spaces?(text) do
look_ahead_middle(rest, start, contents ++ text)
{contents ++ text ++ chars, line, rest}
else
nil
{contents, start, tokens}
end
end
defp look_ahead_middle([{:middle_expr, _, chars, meta} | rest], _start, contents) do
{rest, meta.line, contents ++ chars}
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
{contents ++ chars, line, rest}
end
defp look_ahead_middle(_tokens, _start, _contents) do
nil
defp look_ahead_middle(tokens, start, contents) do
{contents, start, tokens}
end
defp only_spaces?(chars) do
Enum.all?(chars, &(&1 in @all_spaces))
Enum.all?(chars, &(&1 in [?\s, ?\t, ?\r, ?\n]))
end
# Changes placeholder to real expression
+13 -15
View File
@@ -4,8 +4,10 @@ defmodule EEx.Engine do
An engine needs to implement all callbacks below.
This module also ships with a default engine implementation
you can delegate to. See `EEx.SmartEngine` as an example.
An engine may also `use EEx.Engine` to get the default behaviour
but this is not advised. In such cases, if any of the callbacks
are overridden, they must call `super()` to delegate to the
underlying `EEx.Engine`.
"""
@type state :: term
@@ -29,8 +31,7 @@ defmodule EEx.Engine do
It must return the updated state.
"""
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
state
@callback handle_text(state, text :: String.t()) :: state
@doc """
Called for the dynamic/code parts of a template.
@@ -68,7 +69,6 @@ defmodule EEx.Engine do
@callback handle_end(state) :: Macro.t()
@doc false
@deprecated "Use explicit delegation to EEx.Engine instead"
defmacro __using__(_) do
quote do
@behaviour EEx.Engine
@@ -90,7 +90,7 @@ defmodule EEx.Engine do
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, [], text)
EEx.Engine.handle_text(state, text)
end
def handle_expr(state, marker, expr) do
@@ -147,7 +147,7 @@ defmodule EEx.Engine do
end
end
@doc "Default implementation for `c:init/1`."
@doc false
def init(_opts) do
%{
binary: [],
@@ -156,18 +156,18 @@ defmodule EEx.Engine do
}
end
@doc "Default implementation for `c:handle_begin/1`."
@doc false
def handle_begin(state) do
check_state!(state)
%{state | binary: [], dynamic: []}
end
@doc "Default implementation for `c:handle_end/1`."
@doc false
def handle_end(quoted) do
handle_body(quoted)
end
@doc "Default implementation for `c:handle_body/1`."
@doc false
def handle_body(state) do
check_state!(state)
%{binary: binary, dynamic: dynamic} = state
@@ -176,16 +176,14 @@ defmodule EEx.Engine do
{:__block__, [], Enum.reverse(dynamic)}
end
@doc "Default implementation for `c:handle_text/3`."
def handle_text(state, _meta, text) do
check_state!(state)
@doc false
def handle_text(state, text) do
%{binary: binary} = state
%{state | binary: [text | binary]}
end
@doc "Default implementation for `c:handle_expr/3`."
@doc false
def handle_expr(state, "=", ast) do
check_state!(state)
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
+3 -19
View File
@@ -33,26 +33,10 @@ defmodule EEx.SmartEngine do
"""
@behaviour EEx.Engine
use EEx.Engine
@impl true
defdelegate init(opts), to: EEx.Engine
@impl true
defdelegate handle_body(state), to: EEx.Engine
@impl true
defdelegate handle_begin(state), to: EEx.Engine
@impl true
defdelegate handle_end(state), to: EEx.Engine
@impl true
defdelegate handle_text(state, meta, text), to: EEx.Engine
@impl true
def handle_expr(state, marker, expr) do
def handle_expr(buffer, mark, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
EEx.Engine.handle_expr(state, marker, expr)
super(buffer, mark, expr)
end
end
+234
View File
@@ -0,0 +1,234 @@
defmodule EEx.Tokenizer do
@moduledoc false
@type content :: IO.chardata()
@type line :: non_neg_integer
@type column :: non_neg_integer
@type marker :: '=' | '/' | '|' | ''
@type token ::
{:text, line, column, content}
| {:expr | :start_expr | :middle_expr | :end_expr, line, column, marker, content}
| {:eof, line, column}
@spaces [?\s, ?\t]
@doc """
Tokenizes the given charlist or binary.
It returns {:ok, list} with the following tokens:
* `{:text, line, column, content}`
* `{:expr, line, column, marker, content}`
* `{:start_expr, line, column, marker, content}`
* `{:middle_expr, line, column, marker, content}`
* `{:end_expr, line, column, marker, content}`
* `{:eof, line, column}`
Or `{:error, line, column, message}` in case of errors.
"""
@spec tokenize(binary | charlist, line, column, map) ::
{:ok, [token]} | {:error, line, column, String.t()}
def tokenize(bin, line, column, opts) when is_binary(bin) do
tokenize(String.to_charlist(bin), line, column, opts)
end
def tokenize(list, line, column, opts)
when is_list(list) and is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
column = opts.indentation + column
{list, line, column} =
(opts.trim && trim_init(list, line, column, opts)) || {list, line, column}
tokenize(list, line, column, opts, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, opts, buffer, acc) do
tokenize(t, line, column + 3, opts, [?%, ?< | buffer], acc)
end
defp tokenize('<%#' ++ t, line, column, opts, buffer, acc) do
case expr(t, line, column + 3, opts, []) do
{:error, _, _, _} = error ->
error
{:ok, _, new_line, new_column, rest} ->
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], acc)
end
end
defp tokenize('<%' ++ t, line, column, opts, buffer, acc) do
{marker, t} = retrieve_marker(t)
case expr(t, line, column + 2 + length(marker), opts, []) do
{:error, _, _, _} = error ->
error
{:ok, expr, new_line, new_column, rest} ->
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, tokens} -> token_key(tokens, expr)
{:error, _, _, _} -> {:expr, expr}
end
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
final = {key, line, column, marker, expr}
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], [final | acc])
end
end
defp tokenize('\n' ++ t, line, _column, opts, buffer, acc) do
tokenize(t, line + 1, opts.indentation + 1, opts, [?\n | buffer], acc)
end
defp tokenize([h | t], line, column, opts, buffer, acc) do
tokenize(t, line, column + 1, opts, [h | buffer], acc)
end
defp tokenize([], line, column, _opts, buffer, acc) do
eof = {:eof, line, column}
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
end
# Retrieve marker for <%
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
{[marker], t}
end
defp retrieve_marker(t) do
{'', t}
end
# Tokenize an expression until we find %>
defp expr([?%, ?> | t], line, column, _opts, buffer) do
{:ok, Enum.reverse(buffer), line, column + 2, t}
end
defp expr('\n' ++ t, line, _column, opts, buffer) do
expr(t, line + 1, opts.indentation + 1, opts, [?\n | buffer])
end
defp expr([h | t], line, column, opts, buffer) do
expr(t, line, column + 1, opts, [h | buffer])
end
defp expr([], line, column, _opts, _buffer) do
{:error, line, column, "missing token '%>'"}
end
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens, expr) do
case {tokens, Enum.reverse(tokens)} do
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
{:start_expr, expr}
else
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
end
end
defp maybe_append_space([?\s]), do: [?\s]
defp maybe_append_space([h]), do: [h, ?\s]
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, line, column, buffer} | acc]
end
defp trim_if_needed(rest, line, column, opts, buffer) do
if opts.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0, opts)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column, opts) when h in @spaces,
do: trim_init(t, line, column + 1, opts)
defp trim_init([?\r, ?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?<, ?% | _] = rest, line, column, _opts),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, last_column, opts) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column + 1, opts)
{[?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column, opts)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t], column) when h in @spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
end
-9
View File
@@ -43,15 +43,6 @@ defmodule EEx.SmartEngineTest do
assert_received :found
end
test "error with unused \"do\" block without \"<%=\" modifier" do
stderr =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
end)
assert stderr =~ "the contents of this expression won't be output"
end
defp assert_eval(expected, actual, binding \\ []) do
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.SmartEngine)
assert result == expected
+120 -244
View File
@@ -2,67 +2,41 @@ Code.require_file("../test_helper.exs", __DIR__)
defmodule EEx.TokenizerTest do
use ExUnit.Case, async: true
require EEx.Tokenizer, as: T
@opts [indentation: 0, trim: false]
@opts %{indentation: 0, trim: false}
test "simple chars lists" do
assert EEx.tokenize('foo', @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
assert T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "simple strings" do
assert EEx.tokenize("foo", @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
end
test "strings with embedded code" do
assert EEx.tokenize('foo <% bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 14, line: 1}}
]}
assert T.tokenize('foo <% bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
end
test "strings with embedded equals code" do
assert EEx.tokenize('foo <%= bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '=', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded slash code" do
assert EEx.tokenize('foo <%/ bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '/', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%/ bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
end
test "strings with embedded pipe code" do
assert EEx.tokenize('foo <%| bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '|', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
assert T.tokenize('foo <%| bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
end
test "strings with more than one line" do
assert EEx.tokenize('foo\n<%= bar %>', @opts) ==
{:ok,
[
{:text, 'foo\n', %{column: 1, line: 1}},
{:expr, '=', ' bar ', %{column: 1, line: 2}},
{:eof, %{column: 11, line: 2}}
]}
assert T.tokenize('foo\n<%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
end
test "strings with more than one line and expression with more than one line" do
@@ -74,235 +48,157 @@ defmodule EEx.TokenizerTest do
'''
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '=', ' bar\n\nbaz ', %{column: 5, line: 1}},
{:text, '\n', %{column: 7, line: 3}},
{:expr, '', ' foo ', %{column: 1, line: 4}},
{:text, '\n', %{column: 10, line: 4}},
{:eof, %{column: 1, line: 5}}
{:text, 1, 1, 'foo '},
{:expr, 1, 5, '=', ' bar\n\nbaz '},
{:text, 3, 7, '\n'},
{:expr, 4, 1, '', ' foo '},
{:text, 4, 10, '\n'},
{:eof, 5, 1}
]
assert EEx.tokenize(string, @opts) == {:ok, exprs}
assert T.tokenize(string, 1, 1, @opts) == {:ok, exprs}
end
test "quotation" do
assert EEx.tokenize('foo <%% true %>', @opts) ==
{:ok,
[
{:text, 'foo <% true %>', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
]}
assert T.tokenize('foo <%% true %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
end
test "quotation with do-end" do
assert EEx.tokenize('foo <%% true do %>bar<%% end %>', @opts) ==
{:ok,
[
{:text, 'foo <% true do %>bar<% end %>', %{column: 1, line: 1}},
{:eof, %{column: 32, line: 1}}
]}
test "quotation with do/end" do
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
end
test "quotation with interpolation" do
exprs = [
{:text, 'a <% b ', %{column: 1, line: 1}},
{:expr, '=', ' c ', %{column: 9, line: 1}},
{:text, ' ', %{column: 17, line: 1}},
{:expr, '=', ' d ', %{column: 18, line: 1}},
{:text, ' e %> f', %{column: 26, line: 1}},
{:eof, %{column: 33, line: 1}}
{:text, 1, 1, 'a <% b '},
{:expr, 1, 9, '=', ' c '},
{:text, 1, 17, ' '},
{:expr, 1, 18, '=', ' d '},
{:text, 1, 26, ' e %> f'},
{:eof, 1, 33}
]
assert EEx.tokenize('a <%% b <%= c %> <%= d %> e %> f', @opts) == {:ok, exprs}
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1, 1, @opts) == {:ok, exprs}
end
test "improperly formatted quotation with interpolation" do
exprs = [
{:text, '<%% a <%= b %> c %>', %{column: 1, line: 1}},
{:eof, %{column: 22, line: 1}}
{:text, 1, 1, '<%% a <%= b %> c %>'},
{:eof, 1, 22}
]
assert EEx.tokenize('<%%% a <%%= b %> c %>', @opts) == {:ok, exprs}
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments" do
test "eex comments" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
{:text, 1, 1, 'foo '},
{:eof, 1, 16}
]
assert EEx.tokenize('foo <%# true %>', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:eof, %{column: 8, line: 2}}
]
assert EEx.tokenize('foo <%#\ntrue %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <%# true %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments with do-end" do
test "eex comments with do/end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:text, 'bar', %{column: 19, line: 1}},
{:eof, %{column: 32, line: 1}}
{:text, 1, 1, 'foo '},
{:text, 1, 19, 'bar'},
{:eof, 1, 32}
]
assert EEx.tokenize('foo <%# true do %>bar<%# end %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx comments inside do-end" do
test "elixir comments" do
exprs = [
{:start_expr, '', ' if true do ', %{column: 1, line: 1}},
{:text, 'bar', %{column: 31, line: 1}},
{:end_expr, [], ' end ', %{column: 34, line: 1}},
{:eof, %{column: 43, line: 1}}
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
{:eof, 1, 35}
]
assert EEx.tokenize('<% if true do %><%# comment %>bar<% end %>', @opts) == {:ok, exprs}
exprs = [
{:start_expr, [], ' case true do ', %{column: 1, line: 1}},
{:middle_expr, '', ' true -> ', %{column: 33, line: 1}},
{:text, 'bar', %{column: 46, line: 1}},
{:end_expr, [], ' end ', %{column: 49, line: 1}},
{:eof, %{column: 58, line: 1}}
]
assert EEx.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', @opts) ==
{:ok, exprs}
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
end
test "EEx multi-line comments" do
test "elixir comments with do/end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' true ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 20, line: 1}},
{:eof, %{column: 24, line: 1}}
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 1, 27, 'text'},
{:end_expr, 1, 31, [], ' end # closeif '},
{:eof, 1, 50}
]
assert EEx.tokenize('foo <%!-- true --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' \ntrue\n ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 6, line: 3}},
{:eof, %{column: 10, line: 3}}
]
assert EEx.tokenize('foo <%!-- \ntrue\n --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' <%= true %> ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 27, line: 1}},
{:eof, %{column: 31, line: 1}}
]
assert EEx.tokenize('foo <%!-- <%= true %> --%> bar', @opts) == {:ok, exprs}
end
test "Elixir comments" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, [], ' true # this is a boolean ', %{column: 5, line: 1}},
{:eof, %{column: 35, line: 1}}
]
assert EEx.tokenize('foo <% true # this is a boolean %>', @opts) == {:ok, exprs}
end
test "Elixir comments with do-end" do
exprs = [
{:start_expr, [], ' if true do # startif ', %{column: 1, line: 1}},
{:text, 'text', %{column: 27, line: 1}},
{:end_expr, [], ' end # closeif ', %{column: 31, line: 1}},
{:eof, %{column: 50, line: 1}}
]
assert EEx.tokenize('<% if true do # startif %>text<% end # closeif %>', @opts) ==
assert T.tokenize('<% if true do # startif %>text<% end # closeif %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with embedded do end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' if true do ', %{column: 5, line: 1}},
{:text, 'bar', %{column: 21, line: 1}},
{:end_expr, '', ' end ', %{column: 24, line: 1}},
{:eof, %{column: 33, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 1, 21, 'bar'},
{:end_expr, 1, 24, '', ' end '},
{:eof, 1, 33}
]
assert EEx.tokenize('foo <% if true do %>bar<% end %>', @opts) == {:ok, exprs}
assert T.tokenize('foo <% if true do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
end
test "strings with embedded -> end" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' cond do ', %{column: 5, line: 1}},
{:middle_expr, '', ' false -> ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 32, line: 1}},
{:middle_expr, '', ' true -> ', %{column: 35, line: 1}},
{:text, 'baz', %{column: 48, line: 1}},
{:end_expr, '', ' end ', %{column: 51, line: 1}},
{:eof, %{column: 60, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' cond do '},
{:middle_expr, 1, 18, '', ' false -> '},
{:text, 1, 32, 'bar'},
{:middle_expr, 1, 35, '', ' true -> '},
{:text, 1, 48, 'baz'},
{:end_expr, 1, 51, '', ' end '},
{:eof, 1, 60}
]
assert EEx.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', @opts) ==
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with fn-end with newline" do
test "strings with multiple callbacks" do
exprs = [
{:start_expr, '=', ' a fn ->\n', %{column: 1, line: 1}},
{:text, 'foo', %{column: 3, line: 2}},
{:end_expr, [], ' end ', %{column: 6, line: 2}},
{:eof, %{column: 15, line: 2}}
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end, fn -> '},
{:text, 1, 34, 'bar'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
assert EEx.tokenize('<%= a fn ->\n%>foo<% end %>', @opts) ==
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
test "strings with multiple fn-end" do
test "strings with callback followed by do block" do
exprs = [
{:start_expr, '=', ' a fn -> ', %{column: 1, line: 1}},
{:text, 'foo', %{column: 15, line: 1}},
{:middle_expr, '', ' end, fn -> ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 34, line: 1}},
{:end_expr, '', ' end ', %{column: 37, line: 1}},
{:eof, %{column: 46, line: 1}}
{:start_expr, 1, 1, '=', ' a fn -> '},
{:text, 1, 15, 'foo'},
{:middle_expr, 1, 18, '', ' end do '},
{:text, 1, 30, 'bar'},
{:end_expr, 1, 33, '', ' end '},
{:eof, 1, 42}
]
assert EEx.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', @opts) ==
{:ok, exprs}
end
test "strings with fn-end followed by do block" do
exprs = [
{:start_expr, '=', ' a fn -> ', %{column: 1, line: 1}},
{:text, 'foo', %{column: 15, line: 1}},
{:middle_expr, '', ' end do ', %{column: 18, line: 1}},
{:text, 'bar', %{column: 30, line: 1}},
{:end_expr, '', ' end ', %{column: 33, line: 1}},
{:eof, %{column: 42, line: 1}}
]
assert EEx.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', @opts) == {:ok, exprs}
assert T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
end
test "strings with embedded keywords blocks" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:start_expr, '', ' if true do ', %{column: 5, line: 1}},
{:text, 'bar', %{column: 21, line: 1}},
{:middle_expr, '', ' else ', %{column: 24, line: 1}},
{:text, 'baz', %{column: 34, line: 1}},
{:end_expr, '', ' end ', %{column: 37, line: 1}},
{:eof, %{column: 46, line: 1}}
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 1, 21, 'bar'},
{:middle_expr, 1, 24, '', ' else '},
{:text, 1, 34, 'baz'},
{:end_expr, 1, 37, '', ' end '},
{:eof, 1, 46}
]
assert EEx.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', @opts) ==
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1, 1, @opts) ==
{:ok, exprs}
end
@@ -310,61 +206,51 @@ defmodule EEx.TokenizerTest do
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> \n\n '
exprs = [
{:start_expr, '=', ' if true do ', %{column: 2, line: 1}},
{:text, '\n TRUE \n', %{column: 20, line: 1}},
{:middle_expr, '', ' else ', %{column: 3, line: 3}},
{:text, '\n FALSE \n', %{column: 13, line: 3}},
{:end_expr, '', ' end ', %{column: 3, line: 5}},
{:eof, %{column: 3, line: 7}}
{:start_expr, 1, 2, '=', ' if true do '},
{:text, 1, 20, '\n TRUE \n'},
{:middle_expr, 3, 3, '', ' else '},
{:text, 3, 13, '\n FALSE \n'},
{:end_expr, 5, 3, '', ' end '},
{:eof, 7, 3}
]
assert EEx.tokenize(template, [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize(template, 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode with comment" do
exprs = [
{:text, '\n123', %{column: 19, line: 1}},
{:eof, %{column: 4, line: 2}}
{:text, 1, 19, '\n123'},
{:eof, 2, 4}
]
assert EEx.tokenize(' <%# comment %> \n123', [trim: true] ++ @opts) == {:ok, exprs}
end
test "trim mode with multi-line comment" do
exprs = [
{:comment, ' comment ', %{column: 3, line: 1}},
{:text, '\n123', %{column: 23, line: 1}},
{:eof, %{column: 4, line: 2}}
]
assert EEx.tokenize(' <%!-- comment --%> \n123', [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize(' <%# comment %> \n123', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode with CRLF" do
exprs = [
{:text, '0\n', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 3, line: 2}},
{:text, '\n34', %{column: 15, line: 2}},
{:eof, %{column: 3, line: 3}}
{:text, 1, 1, '0\n'},
{:expr, 2, 3, '=', ' 12 '},
{:text, 2, 15, '\n34'},
{:eof, 3, 3}
]
assert EEx.tokenize('0\r\n <%= 12 %> \r\n34', [trim: true] ++ @opts) == {:ok, exprs}
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
end
test "trim mode set to false" do
exprs = [
{:text, ' ', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 2, line: 1}},
{:text, ' \n', %{column: 11, line: 1}},
{:eof, %{column: 1, line: 2}}
{:text, 1, 1, ' '},
{:expr, 1, 2, '=', ' 12 '},
{:text, 1, 11, ' \n'},
{:eof, 2, 1}
]
assert EEx.tokenize(' <%= 12 %> \n', [trim: false] ++ @opts) == {:ok, exprs}
assert T.tokenize(' <%= 12 %> \n', 1, 1, %{@opts | trim: false}) == {:ok, exprs}
end
test "trim mode no false positives" do
assert_not_trimmed = fn x ->
assert EEx.tokenize(x, [trim: false] ++ @opts) == EEx.tokenize(x, @opts)
assert T.tokenize(x, 1, 1, %{@opts | trim: false}) == T.tokenize(x, 1, 1, @opts)
end
assert_not_trimmed.('foo <%= "bar" %> ')
@@ -374,22 +260,12 @@ defmodule EEx.TokenizerTest do
end
test "returns error when there is start mark and no end mark" do
assert EEx.tokenize('foo <% :bar', @opts) ==
{:error, "missing token '%>'", %{column: 12, line: 1}}
assert EEx.tokenize('<%# true ', @opts) ==
{:error, "missing token '%>'", %{column: 10, line: 1}}
assert EEx.tokenize('<%!-- foo ', @opts) ==
{:error, "missing token '--%>'", %{column: 11, line: 1}}
assert T.tokenize('foo <% :bar', 1, 1, @opts) == {:error, 1, 12, "missing token '%>'"}
assert T.tokenize('<%# true ', 1, 1, @opts) == {:error, 1, 10, "missing token '%>'"}
end
test "marks invalid expressions as regular expressions" do
assert EEx.tokenize('<% 1 $ 2 %>', @opts) ==
{:ok,
[
{:expr, [], ' 1 $ 2 ', %{column: 1, line: 1}},
{:eof, %{column: 12, line: 1}}
]}
assert T.tokenize('<% 1 $ 2 %>', 1, 1, @opts) ==
{:ok, [{:expr, 1, 1, [], ' 1 $ 2 '}, {:eof, 1, 12}]}
end
end
+25 -68
View File
@@ -87,11 +87,7 @@ defmodule EExTest do
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n\n456\n\n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n \n456\n \n <%= 789 %>"
string = "<%= 123 %> \n <%= 456 %> \n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
@@ -198,24 +194,6 @@ defmodule EExTest do
assert_eval("foo baz", "foo <%= if false do %>bar<% else %>baz<% end %>")
end
test "embedded code with comments in do end" do
assert_eval("foo bar", "foo <%= case true do %><%# comment %><% true -> %>bar<% end %>")
assert_eval(
"foo\n\nbar\n",
"foo\n<%= case true do %>\n<%# comment %>\n<% true -> %>\nbar\n<% end %>"
)
end
test "embedded code with multi-line comments in do end" do
assert_eval("foo bar", "foo <%= case true do %><%!-- comment --%><% true -> %>bar<% end %>")
assert_eval(
"foo\n\nbar\n",
"foo\n<%= case true do %>\n<%!-- comment --%>\n<% true -> %>\nbar\n<% end %>"
)
end
test "embedded code with nested do end" do
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
end
@@ -233,11 +211,6 @@ defmodule EExTest do
"<%= Enum.map([1, 2, 3], fn x -> %> <%= 100 + x %> <% end) %>"
)
assert_eval(
" 101 102 103 ",
"<%= Enum.map([1, 2, 3], fn x ->\n%> <%= 100 + x %> <% end) %>"
)
assert_eval(
" 101 102 103 ",
"<%= apply Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end] %>"
@@ -283,7 +256,7 @@ defmodule EExTest do
assert_raise EEx.SyntaxError,
"nofile:1:18: unexpected middle of expression <% else %>",
fn ->
EEx.compile_string("<%= if true %>foo<% else %>bar<% end %>")
EEx.compile_string("<% if true %> foo<% else %>bar<% end %>")
end
end
@@ -294,16 +267,16 @@ defmodule EExTest do
end
test "when start expression is found without an end expression" do
msg = "nofile:2:18: unexpected end of string, expected a closing '<% end %>'"
msg = "nofile:2:17: unexpected end of string, expected a closing '<% end %>'"
assert_raise EEx.SyntaxError, msg, fn ->
EEx.compile_string("foo\n<%= if true do %>")
EEx.compile_string("foo\n<% if true do %>")
end
end
test "when nested end expression is found without a start expression" do
assert_raise EEx.SyntaxError, "nofile:1:31: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <%= if true do %><% end %><% end %>")
assert_raise EEx.SyntaxError, "nofile:1:30: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <% if true do %><% end %><% end %>")
end
end
@@ -317,7 +290,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
@@ -551,7 +524,7 @@ defmodule EExTest do
<% "a" in y -> %>
Good
<% true -> %>
<%= if true do %>true<% else %>false<% end %>
<% if true do %>true<% else %>false<% end %>
Bad
<% end %>
"""
@@ -596,6 +569,18 @@ defmodule EExTest do
end
describe "buffers" do
test "unused buffers are kept out" do
string = """
<%= 123 %>
<% if true do %>
<%= 456 %>
<% end %>
<%= 789 %>
"""
assert_eval("123\n\n789\n", string)
end
test "inside comprehensions" do
string = """
<%= for _name <- packages || [] do %>
@@ -633,24 +618,6 @@ defmodule EExTest do
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
end
test "supports t:Path.t() paths" do
filename = to_charlist(Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex"))
result = EEx.eval_file(filename, bar: 1)
assert_normalized_newline_equal("foo 1\n", result)
end
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
EEx.compile_string("foo <%= bar", file: "my_file.eex")
end
test "supports overriding file and line through options" do
filename = Path.join(__DIR__, "fixtures/eex_template_with_syntax_error.eex")
assert_raise EEx.SyntaxError, "my_file.eex:11:1: missing token '%>'", fn ->
EEx.eval_file(filename, _bindings = [], file: "my_file.eex", line: 10)
end
end
end
describe "precompiled" do
@@ -696,8 +663,8 @@ defmodule EExTest do
buffer <> ":END"
end
def handle_text(buffer, meta, text) do
buffer <> ":TEXT-#{meta[:line]}-#{meta[:column]}(#{String.trim(text)})"
def handle_text(buffer, text) do
buffer <> ":TEXT(#{String.trim(text)})"
end
def handle_expr(buffer, "/", expr) do
@@ -715,16 +682,16 @@ defmodule EExTest do
describe "custom engines" do
test "text" do
assert_eval("BODY(INIT:TEXT-1-1(foo))", "foo", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT(foo))", "foo", [], engine: TestEngine)
end
test "custom marker" do
assert_eval("BODY(INIT:TEXT-1-1(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
end
test "begin/end" do
assert_eval(
~s[BODY(INIT:TEXT-1-1(foo):EQUAL(if do\n "BEGIN:TEXT-1-17(this):END"\nelse\n "BEGIN:TEXT-1-31(that):END"\nend))],
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(that):END"\nend))],
"foo <%= if do %>this<% else %>that<% end %>",
[],
engine: TestEngine
@@ -741,16 +708,6 @@ defmodule EExTest do
end
end
describe "parser options" do
test "customizes parsed code" do
atoms_encoder = fn "not_jose", _ -> {:ok, :jose} end
assert_eval("valid", "<%= not_jose %>", [jose: "valid"],
parser_options: [static_atoms_encoder: atoms_encoder]
)
end
end
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
opts = Keyword.merge([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
result = EEx.eval_string(actual, binding, opts)
@@ -1 +0,0 @@
foo <%= bar
+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())
+42 -157
View File
@@ -7,7 +7,7 @@ defmodule Access do
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
of the box. Keywords supports only atoms keys, keys for maps can
be of any type. Both return `nil` if the key does not exist:
be of any type. Both returns `nil` if the key does not exist:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
@@ -47,11 +47,12 @@ defmodule Access do
> `map[key]`, if your map is made of predefined atom keys,
> you should prefer to access those atom keys with `map.key`
> instead of `map[key]`, as `map.key` will raise if the key
> is missing (which is not supposed to happen if the keys are
> predefined). Similarly, since structs are maps and structs
> have predefined keys, they only allow the `struct.key`
> syntax and they do not allow the `struct[key]` access syntax.
> See the `Map` module for more information.
> is missing. This is important because, if a map has a predefined
> set of keys and a key is missing, it is most likely a bug
> in your software or a typo on the key name. For this reason,
> because structs are predefined in nature, they only allow
> the `struct.key` syntax and they do not allow the `struct[key]`
> access syntax. See the `Map` module for more information.
## Nested data structures
@@ -94,19 +95,21 @@ 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
@type get_fun(data) ::
(:get, data, (term -> term) -> new_data :: container)
@type get_fun(data, get_value) ::
(:get, data, (term -> term) ->
{get_value, new_data :: container})
@type get_and_update_fun(data, current_value) ::
@type get_and_update_fun(data, get_value) ::
(:get_and_update, data, (term -> term) ->
{current_value, new_data :: container} | :pop)
{get_value, new_data :: container} | :pop)
@type access_fun(data, current_value) ::
get_fun(data) | get_and_update_fun(data, current_value)
@type access_fun(data, get_value) ::
get_fun(data, get_value) | get_and_update_fun(data, get_value)
@doc """
Invoked in order to access the value stored under `key` in the given term `term`.
@@ -132,16 +135,16 @@ defmodule Access do
The implementation of this callback should invoke `fun` with the value under
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
This function must return either `{current_value, new_value}` or `:pop`.
This function must return either `{get_value, update_value}` or `:pop`.
If the passed function returns `{current_value, new_value}`,
the return value of this callback should be `{current_value, new_data}`, where:
If the passed function returns `{get_value, update_value}`,
the return value of this callback should be `{get_value, new_data}`, where:
* `current_value` is the retrieved value (which can be operated on before being returned)
* `get_value` is the retrieved value (which can be operated on before being returned)
* `new_value` is the new value to be stored under `key`
* `update_value` is the new value to be stored under `key`
* `new_data` is `data` after updating the value of `key` with `new_value`.
* `new_data` is `data` after updating the value of `key` with `update_value`.
If the passed function returns `:pop`, the return value of this callback
must be `{value, new_data}` where `value` is the value under `key`
@@ -150,9 +153,8 @@ defmodule Access do
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
for more examples.
"""
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: value, data: container
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container | any_container
@doc """
Invoked to "pop" the value under `key` out of the given data structure.
@@ -166,18 +168,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}
_ ->
@@ -323,9 +321,9 @@ defmodule Access do
a struct that implements the `Access` behaviour).
The `fun` argument receives the value of `key` (or `nil` if `key` is not
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
the "get" value `current_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`new_value`).
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
the "get" value `get_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`update_value`).
`fun` may also return `:pop`, which means the current value
should be removed from the container and returned.
@@ -340,9 +338,8 @@ defmodule Access do
{1, [a: 2]}
"""
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: var, data: container
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container
def get_and_update(container, key, fun)
def get_and_update(%module{} = container, key, fun) do
@@ -425,7 +422,7 @@ defmodule Access do
The returned function uses the default value if the key does not exist.
This can be used to specify defaults and safely traverse missing keys:
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
iex> get_in(%{}, [Access.key(:user, %{name: "meg"}), Access.key(:name)])
"meg"
Such is also useful when using update functions, allowing us to introduce
@@ -448,11 +445,14 @@ defmodule Access do
An error is raised if the accessed structure is not a map or a struct:
iex> get_in(nil, [Access.key(:foo)])
** (BadMapError) expected a map, got: nil
iex> get_in([], [Access.key(:foo)])
** (BadMapError) expected a map, got: []
"""
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
def key(key, default \\ nil) do
fn
:get, data, next ->
@@ -496,7 +496,7 @@ defmodule Access do
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
"""
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
def key!(key) do
fn
:get, %{} = data, next ->
@@ -544,7 +544,7 @@ defmodule Access do
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
"""
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
def elem(index) when is_integer(index) and index >= 0 do
pos = index + 1
@@ -598,7 +598,7 @@ defmodule Access do
** (RuntimeError) Access.all/0 expected a list, got: %{}
"""
@spec all() :: access_fun(data :: list, current_value :: list)
@spec all() :: access_fun(data :: list, get_value :: list)
def all() do
&all/3
end
@@ -673,7 +673,7 @@ defmodule Access do
** (RuntimeError) Access.at/1 expected a list, got: %{}
"""
@spec at(integer) :: access_fun(data :: list, current_value :: term)
@spec at(integer) :: access_fun(data :: list, get_value :: term)
def at(index) when is_integer(index) do
fn op, data, next -> at(op, data, index, next) end
end
@@ -727,8 +727,7 @@ defmodule Access do
** (Enum.OutOfBoundsError) out of bounds error
"""
@doc since: "1.11.0"
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
@spec at!(integer) :: access_fun(data :: list, get_value :: term)
def at!(index) when is_integer(index) do
fn op, data, next -> at!(op, data, index, next) end
end
@@ -795,7 +794,7 @@ defmodule Access do
"""
@doc since: "1.6.0"
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
def filter(func) when is_function(func) do
fn op, data, next -> filter(op, data, func, next) end
end
@@ -829,118 +828,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
+1 -1
View File
@@ -241,7 +241,7 @@ defmodule Agent do
and the start function will return `{:error, :timeout}`.
If the `:debug` option is present, the corresponding function in the
[`:sys` module](`:sys`) will be invoked.
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
If the `:spawn_opt` option is present, its value will be passed as options
to the underlying process as in `Process.spawn/4`.
+74 -132
View File
@@ -42,32 +42,18 @@ defmodule Application do
end
In Mix projects, the environment of the application and its dependencies can
be overridden via the `config/config.exs` and `config/runtime.exs` files. The
former is loaded at build-time, before your code compiles, and the latter at
runtime, just before your app starts. For example, someone using your application
can override its `:db_host` environment variable as follows:
be overridden via the `config/config.exs` file. For example, someone using
your application can override its `:db_host` environment variable as follows:
import Config
config :my_app, :db_host, "db.local"
See the "Configuration" section in the `Mix` module for more information.
You can also change the application environment dynamically by using functions
such as `put_env/3` and `delete_env/2`.
such as `put_env/3` and `delete_env/2`. However, as a rule of thumb, each application
is responsible for its own environment. Please do not use the functions in this
module for directly accessing or modifying the environment of other applications.
> Note: The config files `config/config.exs` and `config/runtime.exs`
> are rarely used by libraries. Libraries typically define their environment
> in the `def application` function of their `mix.exs`. Configuration files
> are rather used by applications to configure their libraries.
> Note: Each application is responsible for its own environment. Do not
> use the functions in this module for directly accessing or modifying
> the environment of other applications. Whenever you change the application
> environment, Elixir's build tool will only recompile the files that
> belong to that application. So if you read the application environment
> of another application, there is a chance you will be depending on
> outdated configuration, as your file won't be recompiled as it changes.
## Compile-time environment
### Compile-time environment
In the previous example, we read the application environment at runtime:
@@ -85,48 +71,36 @@ defmodule Application do
will only be read when `MyApp.DBClient` effectively starts. While reading
the application environment at runtime is the preferred approach, in some
rare occasions you may want to use the application environment to configure
the compilation of a certain project. However, if you try to access
`Application.fetch_env!/2` outside of a function:
the compilation of a certain project. This is often done by calling `get_env/3`
outside of a function:
defmodule MyApp.DBClient do
@db_host Application.fetch_env!(:my_app, :db_host)
@db_host Application.get_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
You might see warnings and errors:
This approach has one big limitation: if you change the value of the
application environment after the code is compiled, the value used at
runtime is not going to change! For example, if your `config/runtime.exs`
has:
warning: Application.fetch_env!/2 is discouraged in the module body,
use Application.compile_env/3 instead
iex:3: MyApp.DBClient
config :my_app, :db_host, "db.production"
** (ArgumentError) could not fetch application environment :db_host
for application :my_app because the application was not loaded nor
configured
This value will have no effect as the code was compiled to connect to "db.local",
which is mostly likely unavailable in the production environment.
This happens because, when defining modules, the application environment
is not yet available. Luckily, the warning tells us how to solve this
issue, by using `Application.compile_env/3` instead:
For those reasons, reading the application environment at runtime should be the
first choice. However, if you really have to read the application environment
during compilation, we recommend you to use `compile_env/3` instead:
defmodule MyApp.DBClient do
@db_host Application.compile_env(:my_app, :db_host, "db.local")
@db_host Application.compile_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
The difference here is that `compile_env` expects the default value to be
given as an argument, instead of using the `def application` function of
your `mix.exs`. Furthermore, by using `compile_env/3`, tools like Mix will
store the values used during compilation and compare the compilation values
with the runtime values whenever your system starts, raising an error in
case they differ.
In any case, compile-time environments should be avoided. Whenever possible,
reading the application environment at runtime should be the first choice.
By using `compile_env/3`, tools like Mix will store the values used during
compilation and compare the compilation values with the runtime values whenever
your system starts, raising an error in case they differ.
## The application callback module
@@ -189,7 +163,7 @@ defmodule Application do
In the sections above, we have configured an application in the
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
this configuration to create an [*application resource
file*](https://www.erlang.org/doc/man/application.html), which is a file called
file*](http://erlang.org/doc/man/app.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`.
@@ -293,10 +267,11 @@ defmodule Application do
## Further information
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)
[`application`](http://www.erlang.org/doc/man/application.html) Erlang module,
and the
[Applications](http://www.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](http://erlang.org/doc/design_principles/users_guide.html).
"""
@doc """
@@ -411,7 +386,6 @@ defmodule Application do
:maxT,
:registered,
:included_applications,
:optional_applications,
:applications,
:mod,
:start_phases
@@ -525,34 +499,21 @@ defmodule Application do
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at compile time instead of compile_env
@doc since: "1.10.0"
@spec compile_env(app, key | list, value) :: value
defmacro compile_env(app, key_or_path, default \\ nil) do
defmacro compile_env(app, key_or_path, default \\ nil) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
key_or_path = Macro.expand_literals(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
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.
Typically, developers will use `compile_env/3`. 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) :: value
def compile_env(%Macro.Env{} = env, app, key_or_path, default) do
@doc false
def __compile_env__(app, key_or_path, default, env) do
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} -> value
:error -> default
@@ -567,33 +528,18 @@ defmodule Application do
"""
@doc since: "1.10.0"
@spec compile_env!(app, key | list) :: value
defmacro compile_env!(app, key_or_path) do
defmacro compile_env!(app, key_or_path) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
key_or_path = Macro.expand_literals(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
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 +551,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 +577,14 @@ defmodule Application do
If the configuration parameter does not exist, the function returns the
`default` value.
> **Important:** you must use this function to read only your own application
> environment. Do not read the environment of other applications.
**Important:** if you are reading the application environment at compilation
time, for example, inside the module definition instead of inside of a
function, see `compile_env/3` instead.
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. For more information,
> read our [library guidelines](library-guidelines.md).
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. For more information,
read our [library guidelines](library-guidelines.md).
## Examples
@@ -665,18 +611,10 @@ defmodule Application do
Our database engine used by `:my_app` needs to know what databases exist, and
what the database configurations are. The database engine can make a call to
`Application.get_env(:my_app, :my_app_databases, [])` to retrieve the list of
databases (specified by module names).
The engine can then traverse each repository in the list and call
`Application.get_env(:my_app, Databases.RepoOne)` and so forth to retrieve the
configuration of each one. In this case, each configuration will be a keyword
list, so you can use the functions in the `Keyword` module or even the `Access`
module to traverse it, for example:
config = Application.get_env(:my_app, Databases.RepoOne)
config[:ip]
`get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
by module names). Our database engine can then traverse each repository in the
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
the configuration of each one.
"""
@spec get_env(app, key, value) :: value
def get_env(app, key, default \\ nil) when is_atom(app) do
@@ -688,14 +626,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 +642,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
@@ -768,6 +694,9 @@ defmodule Application do
:application.set_env(app, key, value, opts)
end
# TODO: Remove this once we support Erlang/OTP 22+ exclusively.
@compile {:no_warn_undefined, {:application, :set_env, 2}}
@doc """
Puts the environment for multiple apps at the same time.
@@ -776,14 +705,28 @@ defmodule Application do
* have the same application listed more than once
* have the same key inside the same application listed more than once
If those conditions are not met, it will raise.
If those conditions are not met, the behaviour is undefined
(on Erlang/OTP 21 and earlier) or will raise (on Erlang/OTP 22
and later).
It receives the same options as `put_env/4`. Returns `:ok`.
"""
@doc since: "1.9.0"
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
:application.set_env(config, opts)
# TODO: Remove function exported? check when we require Erlang/OTP 22+
if function_exported?(:application, :set_env, 2) do
:application.set_env(config, opts)
else
for app_keyword <- config,
{app, keyword} = app_keyword,
key_value <- keyword,
{key, value} = key_value do
:application.set_env(app, key, value, opts)
end
:ok
end
end
@doc """
@@ -800,7 +743,6 @@ defmodule Application do
defp maybe_warn_on_app_env_key(_app, key) when is_atom(key),
do: :ok
# TODO: Remove this deprecation warning on 2.0+ and allow list lookups as in compile_env.
defp maybe_warn_on_app_env_key(app, key) do
message = "passing non-atom as application env key is deprecated, got: #{inspect(key)}"
IO.warn_once({Application, :key, app, key}, message, _stacktrace_drop_levels = 2)
@@ -824,7 +766,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"
@@ -945,7 +887,7 @@ defmodule Application do
#=> "bar-123"
For more information on code paths, check the `Code` module in
Elixir and also Erlang's [`:code` module](`:code`).
Elixir and also Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html).
"""
@spec app_dir(app) :: String.t()
def app_dir(app) when is_atom(app) do
+1 -1
View File
@@ -56,7 +56,7 @@ defmodule Atom do
"""
@spec to_string(atom) :: String.t()
def to_string(atom) do
:erlang.atom_to_binary(atom)
:erlang.atom_to_binary(atom, :utf8)
end
@doc """
+674 -600
View File
File diff suppressed because it is too large Load Diff
+1 -3
View File
@@ -4,7 +4,7 @@ defmodule Behaviour do
This module is deprecated. Instead of `defcallback/1` and
`defmacrocallback/1`, the `@callback` and `@macrocallback`
module attributes can be used respectively. See the
module attributes can be used (respectively). See the
documentation for `Module` for more information on these
attributes.
@@ -17,7 +17,6 @@ defmodule Behaviour do
@doc """
Defines a function callback according to the given type specification.
"""
@deprecated "Use the @callback module attribute instead"
defmacro defcallback(spec) do
do_defcallback(:def, split_spec(spec, quote(do: term)))
end
@@ -25,7 +24,6 @@ defmodule Behaviour do
@doc """
Defines a macro callback according to the given type specification.
"""
@deprecated "Use the @macrocallback module attribute instead"
defmacro defmacrocallback(spec) do
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
end
+60 -10
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
@@ -155,8 +191,22 @@ defmodule Bitwise do
:erlang.bxor(left, right)
end
@doc false
def unquote(:"^^^")(left, right) do
@doc """
Bitwise XOR operator.
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 ^^^ 3
10
"""
@doc guard: true
@spec integer ^^^ integer :: integer
def left ^^^ right do
:erlang.bxor(left, right)
end
+8 -18
View File
@@ -57,7 +57,7 @@ defmodule Calendar do
representing the microseconds to external format. If the precision is 0,
it means microseconds must be skipped.
"""
@type microsecond :: {value :: non_neg_integer, precision :: non_neg_integer}
@type microsecond :: {non_neg_integer, non_neg_integer}
@typedoc "A calendar implementation"
@type calendar :: module
@@ -189,7 +189,7 @@ defmodule Calendar do
@doc """
Calculates the year and era from the given `year`.
"""
@callback year_of_era(year, month, day) :: {year, era}
@callback year_of_era(year) :: {year, era}
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
@@ -365,7 +365,7 @@ defmodule Calendar do
"""
@doc since: "1.8.0"
@spec put_time_zone_database(time_zone_database()) :: :ok
def put_time_zone_database(database) when is_atom(database) do
def put_time_zone_database(database) do
Application.put_env(:elixir, :time_zone_database, database)
end
@@ -375,7 +375,7 @@ defmodule Calendar do
@doc since: "1.8.0"
@spec get_time_zone_database() :: time_zone_database()
def get_time_zone_database() do
Application.fetch_env!(:elixir, :time_zone_database)
Application.get_env(:elixir, :time_zone_database, Calendar.UTCOnlyTimeZoneDatabase)
end
@doc """
@@ -453,7 +453,7 @@ defmodule Calendar do
b | Abbreviated month name | Jan
B | Full month name | January
c | Preferred date+time representation | 2018-10-17 12:34:56
d | Day of the month | 01, 31
d | Day of the month | 01, 12
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
H | Hour using a 24-hour clock | 00, 23
I | Hour using a 12-hour clock | 01, 12
@@ -485,9 +485,6 @@ defmodule Calendar do
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
"Mon, August 26 2019"
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
"April 2, 2020"
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
"2019-08-26 13:52:06"
@@ -544,8 +541,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 +788,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
+22 -80
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,21 +72,19 @@ 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]]
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
-366
"""
@doc since: "1.5.0"
@@ -99,61 +92,19 @@ defmodule Date do
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.16
step = if first_days <= last_days, do: 1, else: -1
range(first, first_days, last, last_days, calendar, step)
end
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
raise ArgumentError, "both dates must have matching calendars"
end
@doc """
Returns a range of dates with a step.
## Examples
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> range
Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> Enum.count(range)
183
iex> ~D[2001-01-03] in range
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
"""
@doc since: "1.12.0"
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) ::
Date.Range.t()
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step)
when is_integer(step) and step != 0 do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
range(first, first_days, last, last_days, calendar, step)
end
def range(
%{calendar: _, year: _, month: _, day: _} = first,
%{calendar: _, year: _, month: _, day: _} = last,
step
) do
raise ArgumentError,
"both dates must have matching calendar and the step must be a " <>
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}"
end
defp range(first, first_days, last, last_days, calendar, step) do
%Date.Range{
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
last_in_iso_days: last_days
}
end
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
raise ArgumentError, "both dates must have matching calendars"
end
@doc """
Returns the current date in UTC.
@@ -322,7 +273,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
The year parsed by this function is limited to four digits.
@@ -347,7 +298,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -372,7 +323,7 @@ defmodule Date do
@doc """
Converts the given `date` to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Date.to_iso8601/2` returns dates formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
@@ -549,8 +500,8 @@ defmodule Date do
end
end
def compare(%{calendar: calendar1} = date1, %{calendar: calendar2} = date2) do
if Calendar.compatible_calendars?(calendar1, calendar2) do
def compare(date1, date2) do
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
case {to_iso_days(date1), to_iso_days(date2)} do
{first, second} when first > second -> :gt
{first, second} when first < second -> :lt
@@ -708,12 +659,11 @@ defmodule Date do
end
end
@doc false
def to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
end
def to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
end
@@ -837,6 +787,8 @@ defmodule Date do
~D[2020-07-05]
iex> Date.end_of_week(~D[2020-07-06], :sunday)
~D[2020-07-11]
iex> Date.end_of_week(~D[2020-07-06], :sunday)
~D[2020-07-11]
iex> Date.end_of_week(~D[2020-07-06], :saturday)
~D[2020-07-10]
iex> Date.end_of_week(~N[2020-07-11 01:23:45])
@@ -948,14 +900,8 @@ defmodule Date do
@spec year_of_era(Calendar.date()) :: {Calendar.year(), non_neg_integer()}
def year_of_era(date)
def year_of_era(%{calendar: calendar, year: year, month: month, day: day}) do
# TODO: Remove me on 1.17
# The behaviour implementation already warns on missing callback.
if function_exported?(calendar, :year_of_era, 3) do
calendar.year_of_era(year, month, day)
else
calendar.year_of_era(year)
end
def year_of_era(%{calendar: calendar, year: year}) do
calendar.year_of_era(year)
end
@doc """
@@ -997,8 +943,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 +962,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}
+66 -161
View File
@@ -2,13 +2,12 @@ defmodule Date.Range do
@moduledoc """
Returns an inclusive range between dates.
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
Ranges must be created with the `Date.range/2` function.
The following fields are public:
* `:first` - the initial date on the range
* `:last` - the last date on the range
* `:step` - (since v1.12.0) the step
The remaining fields are private and should not be accessed.
"""
@@ -16,134 +15,99 @@ defmodule Date.Range do
@type t :: %__MODULE__{
first: Date.t(),
last: Date.t(),
first_in_iso_days: days(),
last_in_iso_days: days(),
step: pos_integer | neg_integer
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days()
}
@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]
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
defimpl Enumerable do
def member?(
%Date.Range{
first: %{calendar: calendar},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
} = range,
%Date{calendar: calendar} = date
) do
{days, _} = Date.to_iso_days(date)
def member?(%{first: %{calendar: calendar}} = range, %Date{calendar: calendar} = date) do
%{
first: first,
last: last,
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days
} = range
cond do
empty?(range) ->
{:ok, false}
%{year: first_year, month: first_month, day: first_day} = first
%{year: last_year, month: last_month, day: last_day} = last
%{year: year, month: month, day: day} = date
first = {first_year, first_month, first_day}
last = {last_year, last_month, last_day}
date = {year, month, day}
first_days <= last_days ->
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
true ->
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
if first_in_iso_days <= last_in_iso_days do
{:ok, date >= first and date <= last}
else
{:ok, date >= last and date <= first}
end
end
def member?(%Date.Range{step: _}, _) do
def member?(_, _) do
{:ok, false}
end
# TODO: Remove me on v2.0
def member?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
date
) do
step = if first_days <= last_days, do: 1, else: -1
member?(Map.put(date_range, :step, step), date)
def count(%{first_in_iso_days: first, last_in_iso_days: last}) do
{:ok, abs(first - last) + 1}
end
def count(range) do
{:ok, size(range)}
def slice(range) do
%{
first_in_iso_days: first,
last_in_iso_days: last,
first: %{calendar: calendar}
} = range
if first <= last do
{:ok, last - first + 1, &slice_asc(first + &1, &2, calendar)}
else
{:ok, first - last + 1, &slice_desc(first - &1, &2, calendar)}
end
end
def slice(
%Date.Range{
first_in_iso_days: first,
first: %{calendar: calendar},
step: step
} = range
) do
{:ok, size(range), &slice(first + &1 * step, step + &3 - 1, &2, calendar)}
defp slice_asc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_asc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_asc(current + 1, remaining - 1, calendar)]
end
# TODO: Remove me on v2.0
def slice(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
slice(Map.put(date_range, :step, step))
defp slice_desc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_desc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_desc(current - 1, remaining - 1, calendar)]
end
defp slice(current, _step, 1, calendar) do
[date_from_iso_days(current, calendar)]
def reduce(range, acc, fun) do
%{
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days,
first: %{calendar: calendar}
} = range
up? = first_in_iso_days <= last_in_iso_days
reduce(first_in_iso_days, last_in_iso_days, acc, fun, calendar, up?)
end
defp slice(current, step, remaining, calendar) do
[
date_from_iso_days(current, calendar)
| slice(current + step, step, remaining - 1, calendar)
]
end
def reduce(
%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
first: %{calendar: calendar},
step: step
},
acc,
fun
) do
reduce(first_days, last_days, acc, fun, step, calendar)
end
# TODO: Remove me on v2.0
def reduce(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
acc,
fun
) do
step = if first_days <= last_days, do: 1, else: -1
reduce(Map.put(date_range, :step, step), acc, fun)
end
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
{:halted, acc}
end
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
end
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
when step > 0 and first_days <= last_days
when step < 0 and first_days >= last_days do
reduce(
first_days + step,
last_days,
fun.(date_from_iso_days(first_days, calendar), acc),
fun,
step,
calendar
)
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
reduce(x + 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
end
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
reduce(x - 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
end
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
{:done, acc}
end
@@ -158,70 +122,11 @@ defmodule Date.Range do
%Date{year: year, month: month, day: day, calendar: calendar}
end
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step > 0 and first_days > last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step < 0 and first_days < last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
do: abs(div(last_days - first_days, step)) + 1
# TODO: Remove me on v2.0
defp size(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
size(Map.put(date_range, :step, step))
end
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step > 0 and first_days > last_days,
do: true
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step < 0 and first_days < last_days,
do: true
defp empty?(%Date.Range{step: _}), do: false
# TODO: Remove me on v2.0
defp empty?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
empty?(Map.put(date_range, :step, step))
end
end
defimpl Inspect do
import Kernel, except: [inspect: 2]
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ")"
end
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ", #{step})"
end
# TODO: Remove me on v2.0
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
step = if first <= last, do: 1, else: -1
inspect(Map.put(date_range, :step, step), opts)
def inspect(%Date.Range{first: first, last: last}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
end
end
end
+59 -282
View File
@@ -2,24 +2,15 @@ defmodule DateTime do
@moduledoc """
A datetime implementation with a time zone.
This datetime can be seen as a snapshot of a date and time
at a given time zone. For such purposes, it also includes both
UTC and Standard offsets, as well as the zone abbreviation
field used exclusively for formatting purposes. Note future
datetimes are not necessarily guaranteed to exist, as time
zones may change any time in the future due to geopolitical
reasons. See the "Datetimes as snapshots" section for more
information.
This datetime can be seen as an ephemeral snapshot
of a datetime at a given time zone. For such purposes,
it also includes both UTC and Standard offsets, as
well as the zone abbreviation field used exclusively
for formatting purposes.
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
are structural and based on the DateTime struct fields. For proper
comparison between datetimes, use the `compare/2` function. The
existence of the `compare/2` function in this module also allows
using `Enum.min/2` and `Enum.max/2` functions to get the minimum and
maximum datetime of an `Enum`. For example:
iex> Enum.min([~U[2022-01-12 00:01:00.00Z], ~U[2021-01-12 00:01:00.00Z]], DateTime)
~U[2021-01-12 00:01:00.00Z]
comparison between datetimes, use the `compare/2` function.
Developers should avoid creating the `DateTime` struct directly
and instead rely on the functions provided by this module as
@@ -34,74 +25,22 @@ defmodule DateTime do
datetimes and returns `{:error, :utc_only_time_zone_database}`
for any other time zone.
Other time zone databases can also be configured. Here are some
available options and libraries:
Other time zone databases can also be configured. For example,
two of the available options are:
* [`tz`](https://github.com/mathieuprog/tz)
* [`tzdata`](https://github.com/lau/tzdata)
* [`zoneinfo`](https://github.com/smartrent/zoneinfo) -
recommended for embedded devices
* [`tz`](https://hexdocs.pm/tz/)
* [`tzdata`](https://hexdocs.pm/tzdata/)
To use them, first make sure it is added as a dependency in `mix.exs`.
It can then be configured either via configuration:
config :elixir, :time_zone_database, Tz.TimeZoneDatabase
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
or by calling `Calendar.put_time_zone_database/1`:
Calendar.put_time_zone_database(Tz.TimeZoneDatabase)
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
See the proper names in the library installation instructions.
## Datetimes as snapshots
In the first section, we described datetimes as a "snapshot of
a date and time at a given time zone". To understand precisely
what we mean, let's see an example.
Imagine someone in Poland wants to schedule a meeting with someone
in Brazil in the next year. The meeting will happen at 2:30 AM
in the Polish time zone. At what time will the meeting happen in
Brazil?
You can consult the time zone database today, one year before,
using the API in this module and it will give you an answer that
is valid right now. However, this answer may not be valid in the
future. Why? Because both Brazil and Poland may change their timezone
rules, ultimately affecting the result. For example, a country may
choose to enter or abandon "Daylight Saving Time", which is a
process where we adjust the clock one hour forward or one hour
back once per year. Whenener the rules change, the exact instant
that 2:30 AM in Polish time will be in Brazil may change.
In other words, whenever working with future DateTimes, there is
no guarantee the results you get will always be correct, until
the event actually happens. Therefore, when you ask for a future
time, the answers you get are a snapshot that reflects the current
state of the time zone rules. For datetimes in the past, this is
not a problem, because time zone rules do not change for past
events.
To make matters worse, it may be that the 2:30 AM in Polish time
does not actually even exist or it is ambiguous. If a certain
time zone observes "Daylight Saving Time", they will move their
clock forward once a year. When this happens, there is a whole
hour that does not exist. Then, when they move the clock back,
there is a certain hour that will happen twice. So if you want
to schedule a meeting when this shift back happens, you would
need to explicitly say which of the 2:30 AM you precisely mean.
Applications that are date and time sensitive, need to take
these scenarios into account and correctly communicate them to
users.
The good news is: Elixir contains all of the building blocks
necessary to tackle those problems. The default timezone database
used by Elixir, `Calendar.UTCOnlyTimeZoneDatabase`, only works
with UTC, which does not observe those issues. Once you bring
a proper time zone database, the functions in this module will
query the database and return the relevant information. For
example, look at how `DateTime.new/4` returns different results
based on the scenarios described in this section.
"""
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
@@ -143,9 +82,6 @@ defmodule DateTime do
@doc """
Returns the current datetime in UTC.
If you want the current time in Unix seconds,
use `System.os_time/1` instead.
## Examples
iex> datetime = DateTime.utc_now()
@@ -173,9 +109,8 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
iex> {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -204,7 +139,7 @@ defmodule DateTime do
@doc since: "1.11.0"
@spec new(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) ::
{:ok, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:ambiguous, t, t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -305,7 +240,9 @@ defmodule DateTime do
{:error, reason} ->
raise ArgumentError,
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{inspect(reason)}"
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{
inspect(reason)
}"
end
end
@@ -429,9 +366,8 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -482,7 +418,7 @@ defmodule DateTime do
Calendar.time_zone_database()
) ::
{:ok, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:ambiguous, t, t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -514,7 +450,7 @@ defmodule DateTime do
# we get the last microsecond just before.
before_naive =
first_period_until_wall
|> Map.replace!(:microsecond, {999_999, 6})
|> Map.put(:microsecond, {999_999, 6})
|> NaiveDateTime.add(-1)
after_naive = second_period_from_wall
@@ -817,6 +753,10 @@ defmodule DateTime do
It will return the integer with the given unit,
according to `System.convert_time_unit/3`.
If you want to get the current time in Unix seconds,
do not do `DateTime.utc_now() |> DateTime.to_unix()`.
Simply call `System.os_time(:second)` instead.
## Examples
iex> 1_464_096_368 |> DateTime.from_unix!() |> DateTime.to_unix()
@@ -861,8 +801,6 @@ defmodule DateTime do
"""
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
def to_naive(datetime)
def to_naive(%{
calendar: calendar,
year: year,
@@ -902,8 +840,6 @@ defmodule DateTime do
"""
@spec to_date(Calendar.datetime()) :: Date.t()
def to_date(datetime)
def to_date(%{
year: year,
month: month,
@@ -934,8 +870,6 @@ defmodule DateTime do
"""
@spec to_time(Calendar.datetime()) :: Time.t()
def to_time(datetime)
def to_time(%{
year: _,
month: _,
@@ -958,14 +892,13 @@ defmodule DateTime do
@doc """
Converts the given datetime to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601) format.
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601) format.
By default, `DateTime.to_iso8601/2` returns datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
Only supports converting datetimes which are in the ISO calendar,
attempting to convert datetimes from other calendars will raise.
You can also optionally specify an offset for the formatted string.
WARNING: the ISO 8601 datetime format does not contain the time zone nor
its abbreviation, which means information is lost when converting to such
@@ -997,31 +930,11 @@ defmodule DateTime do
iex> DateTime.to_iso8601(dt, :basic)
"20000229T230007-0400"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 3600)
"2000-03-01T04:00:07+01:00"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07+00:00"
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07Z"
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
"""
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
def to_iso8601(datetime, format \\ :extended)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
when format in [:extended, :basic] do
%{
year: year,
@@ -1036,56 +949,21 @@ defmodule DateTime do
std_offset: std_offset
} = datetime
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format) <>
Calendar.ISO.offset_to_string(utc_offset, std_offset, time_zone, format)
end
def to_iso8601(
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
format,
0
)
when format in [:extended, :basic] do
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
"Z"
end
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
when format in [:extended, :basic] do
{_, precision} = datetime.microsecond
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
Calendar.ISO.offset_to_string(offset, 0, nil, format)
end
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
datetime
|> convert!(Calendar.ISO)
|> to_iso8601(format, offset)
end
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
total_offset = datetime.utc_offset + datetime.std_offset
datetime
|> to_iso_days()
# Subtract total original offset in order to get UTC and add the new offset
|> Calendar.ISO.add_day_fraction_to_iso_days(offset - total_offset, 86400)
|> calendar.naive_datetime_from_iso_days()
end
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|> to_iso8601(format)
end
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Since ISO 8601 does not include the proper time zone, the given
string will be converted to UTC and its offset in seconds will be
@@ -1095,6 +973,9 @@ defmodule DateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
@@ -1119,10 +1000,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 +1013,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 +1238,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 +1257,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 +1277,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 +1296,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 +1312,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 +1320,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
+149 -428
View File
@@ -1,127 +1,22 @@
defmodule Calendar.ISO do
@moduledoc """
The default calendar implementation, a Gregorian calendar following ISO 8601.
A calendar implementation that follows to ISO 8601.
This calendar implements a proleptic Gregorian calendar and
This calendar implements the proleptic Gregorian calendar and
is therefore compatible with the calendar used in most countries
today. The proleptic means the Gregorian rules for leap years are
applied for all time, consequently the dates give different results
before the year 1583 from when the Gregorian calendar was adopted.
## ISO 8601 compliance
The ISO 8601 specification is feature-rich, but allows applications
to selectively implement most parts of it. The choices Elixir makes
are catalogued below.
### Features
The standard library supports a minimal set of possible ISO 8601 features.
Specifically, the parser only supports calendar dates and does not support
ordinal and week formats.
By default Elixir only parses extended-formatted date/times. You can opt-in
to parse basic-formatted date/times.
`NaiveDateTime.to_iso8601/2` and `DateTime.to_iso8601/2` allow you to produce
either basic or extended formatted strings, and `Calendar.strftime/2` allows
you to format datetimes however else you desire.
Elixir does not support reduced accuracy formats (for example, a date without
the day component) nor decimal precisions in the lowest component (such as
`10:01:25,5`). No functions exist to parse ISO 8601 durations or time intervals.
#### Examples
Elixir expects the extended format by default when parsing:
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007")
{:error, :invalid_format}
Parsing can be restricted to basic if desired:
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :extended)
{:error, :invalid_format}
Only calendar dates are supported in parsing; ordinal and week dates are not.
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-105")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W16")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W016-3")
{:error, :invalid_format}
Years, months, days, hours, minutes, and seconds must be fully specified:
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-04")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23")
{:error, :invalid_format}
### Extensions
The parser and formatter adopt one ISO 8601 extension: extended year notation.
This allows dates to be prefixed with a `+` or `-` sign, extending the range of
expressible years from the default (`0000..9999`) to `-9999..9999`. Elixir still
restricts years in this format to four digits.
#### Examples
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("+2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_naive_datetime("-2015-01-23 23:50:07")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("+2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23 23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("+2015-01-23 23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
### Additions
ISO 8601 does not allow a whitespace instead of `T` as a separator
between date and times, both when parsing and formatting.
This is a common enough representation, Elixir allows it during parsing.
The formatting of dates in `NaiveDateTime.to_iso8601/1` and `DateTime.to_iso8601/1`
do produce specification-compliant string representations using the `T` separator.
#### Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
Given this is the default calendar used by Elixir, it has one
difference compared to the ISO8601 specification in that it allows
a whitespace instead of `T` as a seperator between date and times
both when parsing and formatting. Strict formatting can be done
by using the `to_iso8601` found in `NaiveDateTime` and `DateTime`.
Note that while ISO 8601 allows times and datetimes to specify
24:00:00 as the zero hour of the next day, this notation is not
supported by Elixir.
"""
@behaviour Calendar
@@ -131,8 +26,6 @@ defmodule Calendar.ISO do
unix_end = 315_569_519_999_999_999 - @unix_epoch * 1_000_000
@unix_range_microseconds unix_start..unix_end
defguardp is_format(term) when term in [:basic, :extended]
@typedoc """
"Before the Current Era" or "Before the Common Era" (BCE), for those years less than `1`.
"""
@@ -158,8 +51,6 @@ defmodule Calendar.ISO do
@type minute :: 0..59
@type second :: 0..59
@type weekday :: :monday | :tuesday | :wednesday | :thursday | :friday | :saturday | :sunday
@type utc_offset :: integer
@type format :: :basic | :extended
@typedoc """
Microseconds with stored precision.
@@ -187,10 +78,7 @@ defmodule Calendar.ISO do
@microseconds_per_second 1_000_000
@parts_per_day @seconds_per_day * @microseconds_per_second
@datetime_seps [?\s, ?T]
@ext_date_sep ?-
@ext_time_sep ?:
@sep [?\s, ?T]
@days_per_nonleap_year 365
@days_per_leap_year 366
@@ -199,11 +87,10 @@ defmodule Calendar.ISO do
# on ~D[0001-01-01] which is 366 days later.
@iso_epoch 366
[match_basic_date, match_ext_date, guard_date, read_date] =
[match_date, guard_date, read_date] =
quote do
[
<<y1, y2, y3, y4, m1, m2, d1, d2>>,
<<y1, y2, y3, y4, @ext_date_sep, m1, m2, @ext_date_sep, d1, d2>>,
<<y1, y2, y3, y4, ?-, m1, m2, ?-, d1, d2>>,
y1 >= ?0 and y1 <= ?9 and y2 >= ?0 and y2 <= ?9 and y3 >= ?0 and y3 <= ?9 and y4 >= ?0 and
y4 <= ?9 and m1 >= ?0 and m1 <= ?9 and m2 >= ?0 and m2 <= ?9 and d1 >= ?0 and d1 <= ?9 and
d2 >= ?0 and d2 <= ?9,
@@ -215,11 +102,10 @@ defmodule Calendar.ISO do
]
end
[match_basic_time, match_ext_time, guard_time, read_time] =
[match_time, guard_time, read_time] =
quote do
[
<<h1, h2, i1, i2, s1, s2>>,
<<h1, h2, @ext_time_sep, i1, i2, @ext_time_sep, s1, s2>>,
<<h1, h2, ?:, i1, i2, ?:, s1, s2>>,
h1 >= ?0 and h1 <= ?9 and h2 >= ?0 and h2 <= ?9 and i1 >= ?0 and i1 <= ?9 and i2 >= ?0 and
i2 <= ?9 and s1 >= ?0 and s1 <= ?9 and s2 >= ?0 and s2 <= ?9,
{
@@ -248,87 +134,49 @@ defmodule Calendar.ISO do
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.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a time string.
## Examples
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("T23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07,0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.123Z")
{:ok, {23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_time("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:61")
{:error, :invalid_time}
"""
@doc since: "1.10.0"
@impl true
@spec parse_time(String.t()) ::
{:ok, {hour, minute, second, microsecond}}
| {:error, atom}
def parse_time("T" <> string) when is_binary(string),
do: do_parse_time(string)
def parse_time(string) when is_binary(string),
do: parse_time(string, :extended)
do: do_parse_time(string)
@doc """
Parses a time `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_time("235007", :basic)
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
@spec parse_time(String.t(), format) ::
{:ok, {hour, minute, second, microsecond}}
| {:error, atom}
def parse_time(string, format) when is_binary(string) and is_format(format) do
case string do
"T" <> rest -> do_parse_time(rest, format)
_ -> do_parse_time(string, format)
end
end
defp do_parse_time(<<unquote(match_basic_time), rest::binary>>, :basic)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(<<unquote(match_ext_time), rest::binary>>, :extended)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(_, _) do
{:error, :invalid_format}
end
defp parse_formatted_time(hour, minute, second, rest) do
with {microsecond, rest} <- parse_microsecond(rest),
defp do_parse_time(string) do
with <<unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{hour, minute, second} = unquote(read_time)
if valid_time?(hour, minute, second, microsecond) do
{:ok, {hour, minute, second, microsecond}}
else
@@ -340,16 +188,14 @@ defmodule Calendar.ISO do
end
@doc """
Parses a date `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a date string.
## Examples
iex> Calendar.ISO.parse_date("2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("2015:01:23")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-01-32")
@@ -358,160 +204,91 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
@spec parse_date(String.t()) ::
{:ok, {year, month, day}}
| {:error, atom}
def parse_date("-" <> string) when is_binary(string),
do: parse_date(string, -1)
def parse_date(string) when is_binary(string),
do: parse_date(string, :extended)
do: parse_date(string, 1)
@doc """
Parses a date `string` according to a given `format`.
defp parse_date(string, multiplier) do
with unquote(match_date) <- string, true <- unquote(guard_date) do
{year, month, day} = unquote(read_date)
year = multiplier * year
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_date("20150123", :basic)
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("20150123", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
@spec parse_date(String.t(), format) ::
{:ok, {year, month, day}}
| {:error, atom}
def parse_date(string, format) when is_binary(string) and is_format(format),
do: parse_date_guarded(string, format)
defp parse_date_guarded("-" <> string, format),
do: do_parse_date(string, -1, format)
defp parse_date_guarded("+" <> string, format),
do: do_parse_date(string, 1, format)
defp parse_date_guarded(string, format),
do: do_parse_date(string, 1, format)
defp do_parse_date(unquote(match_basic_date), multiplier, :basic) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(unquote(match_ext_date), multiplier, :extended) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_date(year, month, day, multiplier) do
year = multiplier * year
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
else
{:error, :invalid_date}
end
else
{:error, :invalid_date}
_ ->
{:error, :invalid_format}
end
end
@doc """
Parses a naive datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a naive datetime string.
## Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07Z")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07-02:30")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 1}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07,0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123Z")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_naive_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+00:00")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:60")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-24:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
@spec parse_naive_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}}
| {:error, atom}
def parse_naive_datetime("-" <> string) when is_binary(string),
do: parse_naive_datetime(string, -1)
def parse_naive_datetime(string) when is_binary(string),
do: parse_naive_datetime(string, :extended)
do: parse_naive_datetime(string, 1)
@doc """
Parses a naive datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
@spec parse_naive_datetime(String.t(), format) ::
{:ok, {year, month, day, hour, minute, second, microsecond}}
| {:error, atom}
def parse_naive_datetime(string, format) when is_binary(string) and is_format(format),
do: parse_naive_datetime_guarded(string, format)
defp parse_naive_datetime_guarded("-" <> string, format),
do: do_parse_naive_datetime(string, -1, format)
defp parse_naive_datetime_guarded("+" <> string, format),
do: do_parse_naive_datetime(string, 1, format)
defp parse_naive_datetime_guarded(string, format),
do: do_parse_naive_datetime(string, 1, format)
defp do_parse_naive_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
defp parse_naive_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -528,94 +305,54 @@ defmodule Calendar.ISO do
end
@doc """
Parses a UTC datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a UTC datetime string.
## Examples
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07Z")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {0, 0}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07,123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07,123+02:30")
{:ok, {-2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07")
{:error, :missing_offset}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_utc_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
@spec parse_utc_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
| {:error, atom}
def parse_utc_datetime("-" <> string) when is_binary(string),
do: parse_utc_datetime(string, -1)
def parse_utc_datetime(string) when is_binary(string),
do: parse_utc_datetime(string, :extended)
do: parse_utc_datetime(string, 1)
@doc """
Parses a UTC datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
@spec parse_utc_datetime(String.t(), format) ::
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
| {:error, atom}
def parse_utc_datetime(string, format) when is_binary(string) and is_format(format),
do: parse_utc_datetime_guarded(string, format)
defp parse_utc_datetime_guarded("-" <> string, format),
do: do_parse_utc_datetime(string, -1, format)
defp parse_utc_datetime_guarded("+" <> string, format),
do: do_parse_utc_datetime(string, 1, format)
defp parse_utc_datetime_guarded(string, format),
do: do_parse_utc_datetime(string, 1, format)
defp do_parse_utc_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
defp parse_utc_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -645,7 +382,8 @@ defmodule Calendar.ISO do
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
end
else
_ -> {:error, :invalid_format}
_ ->
{:error, :invalid_format}
end
end
@@ -1034,33 +772,14 @@ defmodule Calendar.ISO do
"""
@doc since: "1.8.0"
@spec year_of_era(year) :: {1..10000, era}
def year_of_era(year) when is_year_CE(year), do: {year, 1}
def year_of_era(year) when is_year_BCE(year), do: {abs(year) + 1, 0}
@doc """
Calendar callback to compute the year and era from the
given `year`, `month` and `day`.
In the ISO calendar, the new year coincides with the new era,
so the `month` and `day` arguments are discarded. If you only
have the year available, you can `year_of_era/1` instead.
## Examples
iex> Calendar.ISO.year_of_era(1, 1, 1)
{1, 1}
iex> Calendar.ISO.year_of_era(2018, 12, 1)
{2018, 1}
iex> Calendar.ISO.year_of_era(0, 1, 1)
{1, 0}
iex> Calendar.ISO.year_of_era(-1, 12, 1)
{2, 0}
"""
@doc since: "1.13.0"
@impl true
@spec year_of_era(year, month, day) :: {1..10000, era}
def year_of_era(year, _month, _day), do: year_of_era(year)
def year_of_era(year) when is_year_CE(year) do
{year, 1}
end
def year_of_era(year) when is_year_BCE(year) do
{abs(year) + 1, 0}
end
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
@@ -1356,7 +1075,9 @@ defmodule Calendar.ISO do
@doc """
Determines if the date given is valid according to the proleptic Gregorian calendar.
Leap seconds are not supported by the built-in Calendar.ISO.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
## Examples
+31 -99
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,
@@ -669,7 +598,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in naive date
@@ -678,6 +607,9 @@ defmodule NaiveDateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
@@ -744,7 +676,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -772,7 +704,7 @@ defmodule NaiveDateTime do
@doc """
Converts the given naive datetime to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `NaiveDateTime.to_iso8601/2` returns naive datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
+35 -86
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]
@@ -216,7 +211,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in times.
@@ -224,6 +219,12 @@ defmodule Time do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
Time representations with reduced accuracy are not supported.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
## Examples
iex> Time.from_iso8601("23:50:07")
@@ -262,7 +263,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -289,7 +290,7 @@ defmodule Time do
@doc """
Converts the given time to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
format, for human readability. It also supports the "basic" format through
@@ -454,15 +455,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 +466,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 +656,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 +682,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
+247 -607
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+152 -16
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
@@ -37,23 +37,163 @@ defmodule Code.Identifier do
op in [:"::"] -> {:right, 60}
op in [:|] -> {:right, 70}
op in [:=] -> {:right, 100}
op in [:||, :|||, :or] -> {:left, 120}
op in [:&&, :&&&, :and] -> {:left, 130}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
op in [:<, :<=, :>=, :>] -> {:left, 150}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :"<|>"] -> {:left, 160}
op in [:in] -> {:left, 170}
op in [:"^^^"] -> {:left, 180}
op in [:"//"] -> {:right, 190}
op in [:||, :|||, :or] -> {:left, 130}
op in [:&&, :&&&, :and] -> {:left, 140}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
op in [:<, :<=, :>=, :>] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 170}
op in [:in] -> {:left, 180}
op in [:^^^] -> {:left, 190}
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
op in [:**] -> {:left, 230}
op in [:.] -> {:left, 310}
true -> :error
end
end
@doc """
Classifies the given atom into one of the following categories:
* `:alias` - a valid Elixir alias, like `Foo`, `Foo.Bar` and so on
* `:callable_local` - an atom that can be used as a local call;
this category includes identifiers like `:foo`
* `:callable_operator` - all callable operators, such as `:<>`. Note
operators such as `:..` are not callable because of ambiguity
* `:not_atomable` - callable operators that must be wrapped in quotes when
defined as an atom. For example, `::` must be written as `:"::"` to avoid
the ambiguity between the atom and the keyword identifier
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
syntax error); this category includes atoms like `:Foo`, since they are
valid identifiers but they need quotes to be used in function calls
(`Foo."Bar"`)
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
"""
def classify(atom) when is_atom(atom) do
charlist = Atom.to_charlist(atom)
cond do
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
:not_callable
atom in [:"::"] ->
:not_atomable
unary_op(atom) != :error or binary_op(atom) != :error ->
:callable_operator
valid_alias?(charlist) ->
:alias
true ->
case :elixir_config.get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
{kind, _acc, [], _, _, special} ->
if kind == :identifier and not :lists.member(?@, special) do
:callable_local
else
:not_callable
end
_ ->
:other
end
end
end
defp valid_alias?('Elixir' ++ rest), do: valid_alias_piece?(rest)
defp valid_alias?(_other), do: false
defp valid_alias_piece?([?., char | rest]) when char >= ?A and char <= ?Z,
do: valid_alias_piece?(trim_leading_while_valid_identifier(rest))
defp valid_alias_piece?([]), do: true
defp valid_alias_piece?(_other), do: false
defp trim_leading_while_valid_identifier([char | rest])
when char >= ?a and char <= ?z
when char >= ?A and char <= ?Z
when char >= ?0 and char <= ?9
when char == ?_ do
trim_leading_while_valid_identifier(rest)
end
defp trim_leading_while_valid_identifier(other) do
other
end
@doc """
Inspects the identifier as an atom.
"""
def inspect_as_atom(atom) when is_nil(atom) or is_boolean(atom) do
Atom.to_string(atom)
end
def inspect_as_atom(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
:alias ->
case binary do
binary when binary in ["Elixir", "Elixir.Elixir"] -> binary
"Elixir.Elixir." <> _rest -> binary
"Elixir." <> rest -> rest
end
type when type in [:callable_local, :callable_operator, :not_callable] ->
":" <> binary
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?:, ?", escaped, ?"])
end
end
@doc """
Inspects the given identifier as a key.
"""
def inspect_as_key(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([binary, ?:])
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?", escaped, ?", ?:])
end
end
@doc """
Inspects the given identifier as a function name.
"""
def inspect_as_function(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_atomable] ->
binary
type ->
escaped =
if type in [:not_callable, :alias] do
binary
else
elem(escape(binary, ?"), 0)
end
IO.iodata_to_binary([?", escaped, ?"])
end
end
@doc """
Extracts the name and arity of the parent from the anonymous function identifier.
"""
@@ -71,12 +211,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
-600
View File
@@ -1,600 +0,0 @@
defmodule Code.Normalizer do
@moduledoc false
defguard is_literal(x)
when is_integer(x) or
is_float(x) or
is_binary(x) or
is_atom(x)
@doc """
Wraps literals in the quoted expression to conform to the AST format expected
by the formatter.
"""
def normalize(quoted, opts \\ []) do
line = Keyword.get(opts, :line, nil)
escape = Keyword.get(opts, :escape, true)
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
state = %{
escape: escape,
parent_meta: [line: line],
locals_without_parens: locals_without_parens ++ Code.Formatter.locals_without_parens()
}
do_normalize(quoted, state)
end
# Wrapped literals should receive the block meta
defp do_normalize({:__block__, meta, [literal]}, state)
when not is_tuple(literal) or tuple_size(literal) == 2 do
normalize_literal(literal, meta, state)
end
# Only normalize the first argument of an alias if it's not an atom
defp do_normalize({:__aliases__, meta, [first | rest]}, state) when not is_atom(first) do
meta = patch_meta_line(meta, state.parent_meta)
first = do_normalize(first, %{state | parent_meta: meta})
{:__aliases__, meta, [first | rest]}
end
defp do_normalize({:__aliases__, _, _} = quoted, _state) do
quoted
end
# Skip captured arguments like &1
defp do_normalize({:&, meta, [term]}, state) when is_integer(term) do
meta = patch_meta_line(meta, state.parent_meta)
{:&, meta, [term]}
end
# Ranges
defp do_normalize(left..right//step, state) do
left = do_normalize(left, state)
right = do_normalize(right, state)
meta = meta_line(state)
if step == 1 do
{:.., meta, [left, right]}
else
step = do_normalize(step, state)
{:"..//", meta, [left, right, step]}
end
end
# Bit containers
defp do_normalize({:<<>>, _, args} = quoted, state) when is_list(args) do
normalize_bitstring(quoted, state)
end
# Atoms with interpolations
defp do_normalize(
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
[{:<<>>, _, parts} = string, :utf8]},
state
)
when is_list(parts) 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)
else
normalize_bitstring(string, state)
end
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, utf8]}
end
# Charlists with interpolations
defp do_normalize({{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]} = 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)
{{:., 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)
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
else
normalize_call(quoted, state)
end
end
# Don't normalize the `Access` atom in access syntax
defp do_normalize({:., meta, [Access, :get]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
{:., meta, [Access, :get]}
end
# Only normalize the left side of the dot operator
# The right hand side is an atom in the AST but it's not an atom literal, so
# it should not be wrapped
defp do_normalize({:., meta, [left, right]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
left = do_normalize(left, %{state | parent_meta: meta})
{:., meta, [left, right]}
end
# A list of left to right arrows is not considered as a list literal, so it's not wrapped
defp do_normalize([{:->, _, [_ | _]} | _] = quoted, state) do
normalize_args(quoted, state)
end
# left -> right
defp do_normalize({:->, meta, [left, right]}, state) do
meta = patch_meta_line(meta, state.parent_meta)
left = normalize_args(left, %{state | parent_meta: meta})
right = do_normalize(right, %{state | parent_meta: meta})
{:->, meta, [left, right]}
end
# Maps
defp do_normalize({:%{}, meta, args}, state) when is_list(args) do
meta =
if meta == [] do
line = state.parent_meta[:line]
[line: line, closing: [line: line]]
else
meta
end
state = %{state | parent_meta: meta}
args =
case args do
[{:|, pipe_meta, [left, right]}] ->
left = do_normalize(left, state)
right = normalize_map_args(right, state)
[{:|, pipe_meta, [left, right]}]
[{_, _, _} = call] ->
[do_normalize(call, state)]
args ->
normalize_map_args(args, state)
end
{:%{}, meta, args}
end
# Sigils
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
when is_list(args) and is_atom(sigil) do
case Atom.to_string(sigil) do
<<"sigil_", _name>> ->
meta =
meta
|> patch_meta_line(state.parent_meta)
|> Keyword.put_new(:delimiter, "\"")
{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
_ ->
normalize_call(quoted, state)
end
end
# Tuples
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
{last_arg, args} = List.pop_at(args, -1)
if args != [] and match?([_ | _], last_arg) and keyword?(last_arg) do
args = normalize_args(args, state)
kw_list = normalize_kw_args(last_arg, state, true)
{:{}, meta, args ++ kw_list}
else
normalize_call(quoted, state)
end
end
# Module attributes
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
value =
cond do
keyword?(value) and value != [] ->
normalize_kw_args(value, state, true)
is_list(value) ->
normalize_literal(value, meta, state)
true ->
do_normalize(value, state)
end
{:@, meta, [{name, name_meta, [value]}]}
end
# Regular blocks
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
{:__block__, meta, normalize_args(args, state)}
end
# Calls
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
normalize_call(quoted, state)
end
# Vars
defp do_normalize({_, _, context} = quoted, _state) when is_atom(context) do
quoted
end
# Literals
defp do_normalize(quoted, state) do
normalize_literal(quoted, [], state)
end
# Numbers
defp normalize_literal(number, meta, state) when is_number(number) do
meta =
meta
|> Keyword.put_new(:token, inspect(number))
|> patch_meta_line(state.parent_meta)
{:__block__, meta, [number]}
end
# Atom, Strings
defp normalize_literal(literal, meta, state) when is_atom(literal) or is_binary(literal) do
meta = patch_meta_line(meta, state.parent_meta)
literal = maybe_escape_literal(literal, state)
if is_atom(literal) and Macro.classify_atom(literal) == :alias and
is_nil(meta[:delimiter]) do
segments =
case Atom.to_string(literal) do
"Elixir" ->
[:"Elixir"]
"Elixir." <> segments ->
segments
|> String.split(".")
|> Enum.map(&String.to_atom/1)
end
{:__aliases__, meta, segments}
else
{:__block__, meta, [literal]}
end
end
# 2-tuples
defp normalize_literal({left, right}, meta, state) do
meta = patch_meta_line(meta, state.parent_meta)
state = %{state | parent_meta: meta}
if match?([_ | _], right) and keyword?(right) do
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state, true)}]}
else
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
end
end
# Lists
defp normalize_literal(list, meta, state) when is_list(list) do
if list != [] and List.ascii_printable?(list) do
# It's a charlist
list =
if state.escape do
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), nil)
IO.iodata_to_binary(string) |> to_charlist()
else
list
end
meta =
meta
|> Keyword.put_new(:delimiter, "'")
|> patch_meta_line(state.parent_meta)
{:__block__, meta, [list]}
else
meta =
if line = state.parent_meta[:line] do
meta
|> Keyword.put_new(:closing, line: line)
|> patch_meta_line(state.parent_meta)
else
meta
end
{:__block__, meta, [normalize_kw_args(list, state, false)]}
end
end
# Probably an invalid value, wrap it and send it upstream
defp normalize_literal(quoted, meta, _state) do
{:__block__, meta, [quoted]}
end
defp normalize_call({form, meta, args}, state) do
meta = patch_meta_line(meta, state.parent_meta)
arity = length(args)
# Only normalize the form if it's a qualified call
form =
if is_atom(form) do
form
else
do_normalize(form, %{state | parent_meta: meta})
end
meta =
if is_nil(meta[:no_parens]) and is_nil(meta[:closing]) and is_nil(meta[:do]) and
not Code.Formatter.local_without_parens?(form, arity, state.locals_without_parens) do
[closing: [line: meta[:line]]] ++ meta
else
meta
end
cond do
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], List.last(args)) ->
# def foo do :ok end
# def foo, do: :ok
normalize_kw_blocks(form, meta, args, state)
match?([{:do, _} | _], List.last(args)) ->
# Non normalized kw blocks
line = state.parent_meta[:line]
meta = meta ++ [do: [line: line], end: [line: line]]
normalize_kw_blocks(form, meta, args, state)
allow_keyword?(form, arity) ->
args = normalize_args(args, %{state | parent_meta: meta})
{last_arg, leading_args} = List.pop_at(args, -1, [])
last_args =
case last_arg do
{:__block__, _, [[{{:__block__, key_meta, _}, _} | _]] = last_args} ->
if key_meta[:format] == :keyword do
last_args
else
[last_arg]
end
[] ->
[]
_ ->
[last_arg]
end
{form, meta, leading_args ++ last_args}
true ->
args = normalize_args(args, %{state | parent_meta: meta})
{form, meta, args}
end
end
defp allow_keyword?(:when, 2), do: true
defp allow_keyword?(:{}, _), do: false
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
defp normalize_bitstring({:<<>>, meta, parts}, state, escape_interpolation \\ false) do
meta = patch_meta_line(meta, state.parent_meta)
parts =
if binary_interpolated?(parts) do
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
else
state = %{state | parent_meta: meta}
Enum.map(parts, fn part ->
with {:"::", meta, [left, _]} <- part,
true <- meta[:inferred_bitstring_spec] do
do_normalize(left, state)
else
_ -> do_normalize(part, state)
end
end)
end
{:<<>>, meta, parts}
end
defp normalize_interpolation_parts(parts, state, escape_interpolation) do
Enum.map(parts, fn
{:"::", interpolation_meta,
[
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
{:binary, binary_meta, context}
]} ->
middle = do_normalize(middle, %{state | parent_meta: dot_meta})
{:"::", interpolation_meta,
[
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
{:binary, binary_meta, context}
]}
part ->
if escape_interpolation do
maybe_escape_literal(part, state)
else
part
end
end)
end
defp normalize_map_args(args, state) do
Enum.map(normalize_kw_args(args, state, false), fn
{:__block__, _, [{_, _} = pair]} -> pair
pair -> pair
end)
end
defp normalize_kw_blocks(form, meta, args, state) do
{kw_blocks, leading_args} = List.pop_at(args, -1)
kw_blocks =
Enum.map(kw_blocks, fn {tag, block} ->
block = do_normalize(block, %{state | parent_meta: meta})
block =
case block do
{_, _, [[{:->, _, _} | _] = block]} -> block
block -> block
end
# Only wrap the tag if it isn't already wrapped
tag =
case tag do
{:__block__, _, _} -> tag
_ -> {:__block__, [line: meta[:line]], [tag]}
end
{tag, block}
end)
leading_args = normalize_args(leading_args, %{state | parent_meta: meta})
{form, meta, leading_args ++ [kw_blocks]}
end
defp normalize_kw_args(elems, state, keyword?)
defp normalize_kw_args(
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
state,
keyword?
)
when is_atom(key) do
keyword? = keyword? or keyword?(current)
first =
if key_meta[:format] == :keyword and not keyword? do
key_meta = Keyword.delete(key_meta, :format)
line = key_meta[:line] || meta_line(state)
{:__block__, [line: line], [{{:__block__, key_meta, [key]}, value}]}
else
first
end
[first | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([{left, right} | rest] = current, state, keyword?) do
keyword? = keyword? or keyword?(current)
left =
if keyword? do
meta = [format: :keyword] ++ meta_line(state)
{:__block__, meta, [maybe_escape_literal(left, state)]}
else
do_normalize(left, state)
end
right = do_normalize(right, state)
pair =
with {:__block__, meta, _} <- left,
:keyword <- meta[:format] do
{left, right}
else
_ -> {:__block__, meta_line(state), [{left, right}]}
end
[pair | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([first | rest], state, keyword?) do
[do_normalize(first, state) | normalize_kw_args(rest, state, keyword?)]
end
defp normalize_kw_args([], _state, _keyword?) do
[]
end
defp normalize_args(args, state) do
Enum.map(args, &do_normalize(&1, state))
end
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
{string, _} = Code.Identifier.escape(string, nil)
IO.iodata_to_binary(string)
end
defp maybe_escape_literal(atom, %{escape: true} = state) when is_atom(atom) do
atom
|> Atom.to_string()
|> maybe_escape_literal(state)
|> String.to_atom()
end
defp maybe_escape_literal(term, _) do
term
end
defp binary_interpolated?(parts) do
Enum.all?(parts, fn
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
binary when is_binary(binary) -> true
_ -> false
end)
end
defp list_interpolated?(parts) do
Enum.all?(parts, fn
{{:., _, [Kernel, :to_string]}, _, [_]} -> true
binary when is_binary(binary) -> true
_ -> false
end)
end
defp patch_meta_line(meta, parent_meta) do
with nil <- meta[:line],
line when is_integer(line) <- parent_meta[:line] do
[line: line] ++ meta
else
_ -> meta
end
end
defp meta_line(state) do
if line = state.parent_meta[:line] do
[line: line]
else
[]
end
end
defp keyword?([{{:__block__, key_meta, [key]}, _} | rest]) when is_atom(key) do
if key_meta[:format] == :keyword do
keyword?(rest)
else
false
end
end
defp keyword?([{key, _value} | rest]) when is_atom(key) do
case Atom.to_charlist(key) do
'Elixir.' ++ _ -> false
_ -> keyword?(rest)
end
end
defp keyword?([]), do: true
defp keyword?(_other), do: false
end
+74 -80
View File
@@ -7,9 +7,9 @@ defmodule Code.Typespec do
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
def spec_to_quoted(name, spec)
def spec_to_quoted(name, {:type, anno, :fun, [{:type, _, :product, args}, result]})
def spec_to_quoted(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
when is_atom(name) do
meta = meta(anno)
meta = [line: line]
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
vars =
@@ -27,13 +27,11 @@ defmodule Code.Typespec do
end
end
def spec_to_quoted(name, {:type, anno, :fun, []}) when is_atom(name) do
meta = meta(anno)
{:"::", meta, [{name, meta, []}, quote(do: term)]}
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
{:"::", [line: line], [{name, [line: line], []}, quote(do: term)]}
end
def spec_to_quoted(name, {:type, anno, :bounded_fun, [type, constrs]}) when is_atom(name) do
meta = meta(anno)
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
guards =
@@ -41,6 +39,7 @@ defmodule Code.Typespec do
{erl_to_ex_var(var), typespec_to_quoted(type)}
end
meta = [line: line]
ignore_vars = Keyword.keys(guards)
vars =
@@ -53,7 +52,7 @@ defmodule Code.Typespec do
args = for arg <- args, do: typespec_to_quoted(arg)
when_args = [
{:"::", meta, [{name, meta, args}, typespec_to_quoted(result)]},
{:"::", meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
guards ++ vars
]
@@ -174,11 +173,9 @@ defmodule Code.Typespec do
end
defp get_module_and_beam(module) when is_atom(module) do
with {^module, beam, _filename} <- :code.get_object_code(module),
{:ok, ^module} <- beam |> :beam_lib.info() |> Keyword.fetch(:module) do
{module, beam}
else
_ -> :error
case :code.get_object_code(module) do
{^module, beam, _filename} -> {module, beam}
:error -> :error
end
end
@@ -191,27 +188,27 @@ defmodule Code.Typespec do
## To AST conversion
defp collect_vars({:ann_type, _anno, args}) when is_list(args) do
defp collect_vars({:ann_type, _line, args}) when is_list(args) do
[]
end
defp collect_vars({:type, _anno, _kind, args}) when is_list(args) do
defp collect_vars({:type, _line, _kind, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:remote_type, _anno, args}) when is_list(args) do
defp collect_vars({:remote_type, _line, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:typed_record_field, _anno, type}) do
defp collect_vars({:typed_record_field, _line, type}) do
collect_vars(type)
end
defp collect_vars({:paren_type, _anno, [type]}) do
defp collect_vars({:paren_type, _line, [type]}) do
collect_vars(type)
end
defp collect_vars({:var, _anno, var}) do
defp collect_vars({:var, _line, var}) do
[erl_to_ex_var(var)]
end
@@ -219,48 +216,48 @@ defmodule Code.Typespec do
[]
end
defp typespec_to_quoted({:user_type, anno, name, args}) do
defp typespec_to_quoted({:user_type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, meta(anno), args}
{name, [line: line], args}
end
defp typespec_to_quoted({:type, anno, :tuple, :any}) do
{:tuple, meta(anno), []}
defp typespec_to_quoted({:type, line, :tuple, :any}) do
{:tuple, [line: line], []}
end
defp typespec_to_quoted({:type, anno, :tuple, args}) do
defp typespec_to_quoted({:type, line, :tuple, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{:{}, meta(anno), args}
{:{}, [line: line], args}
end
defp typespec_to_quoted({:type, _anno, :list, [{:type, _, :union, unions} = arg]}) do
defp typespec_to_quoted({:type, _line, :list, [{:type, _, :union, unions} = arg]}) do
case unpack_typespec_kw(unions, []) do
{:ok, ast} -> ast
:error -> [typespec_to_quoted(arg)]
end
end
defp typespec_to_quoted({:type, anno, :list, []}) do
{:list, meta(anno), []}
defp typespec_to_quoted({:type, line, :list, []}) do
{:list, [line: line], []}
end
defp typespec_to_quoted({:type, _anno, :list, [arg]}) do
defp typespec_to_quoted({:type, _line, :list, [arg]}) do
[typespec_to_quoted(arg)]
end
defp typespec_to_quoted({:type, anno, :nonempty_list, []}) do
[{:..., meta(anno), nil}]
defp typespec_to_quoted({:type, line, :nonempty_list, []}) do
[{:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, anno, :nonempty_list, [arg]}) do
[typespec_to_quoted(arg), {:..., meta(anno), nil}]
defp typespec_to_quoted({:type, line, :nonempty_list, [arg]}) do
[typespec_to_quoted(arg), {:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, anno, :map, :any}) do
{:map, meta(anno), []}
defp typespec_to_quoted({:type, line, :map, :any}) do
{:map, [line: line], []}
end
defp typespec_to_quoted({:type, anno, :map, fields}) do
defp typespec_to_quoted({:type, line, :map, fields}) do
fields =
Enum.map(fields, fn
{:type, _, :map_field_assoc, :any} ->
@@ -278,17 +275,16 @@ defmodule Code.Typespec do
case List.keytake(fields, :__struct__, 0) do
{{:__struct__, struct}, fields_pruned} when is_atom(struct) and struct != nil ->
map_pruned = {:%{}, meta(anno), fields_pruned}
{:%, meta(anno), [struct, map_pruned]}
map_pruned = {:%{}, [line: line], fields_pruned}
{:%, [line: line], [struct, map_pruned]}
_ ->
{:%{}, meta(anno), fields}
{:%{}, [line: line], fields}
end
end
defp typespec_to_quoted({:type, anno, :binary, [arg1, arg2]}) do
defp typespec_to_quoted({:type, line, :binary, [arg1, arg2]}) do
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
line = meta(anno)[:line]
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
{arg1, 0} ->
@@ -302,57 +298,57 @@ defmodule Code.Typespec do
end
end
defp typespec_to_quoted({:type, anno, :union, args}) do
defp typespec_to_quoted({:type, line, :union, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, meta(anno), [arg, expr]} end)
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, [line: line], [arg, expr]} end)
end
defp typespec_to_quoted({:type, anno, :fun, [{:type, _, :product, args}, result]}) do
defp typespec_to_quoted({:type, line, :fun, [{:type, _, :product, args}, result]}) do
args = for arg <- args, do: typespec_to_quoted(arg)
[{:->, meta(anno), [args, typespec_to_quoted(result)]}]
[{:->, [line: line], [args, typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, anno, :fun, [args, result]}) do
[{:->, meta(anno), [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
defp typespec_to_quoted({:type, line, :fun, [args, result]}) do
[{:->, [line: line], [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, anno, :fun, []}) do
typespec_to_quoted({:type, anno, :fun, [{:type, anno, :any}, {:type, anno, :any, []}]})
defp typespec_to_quoted({:type, line, :fun, []}) do
typespec_to_quoted({:type, line, :fun, [{:type, line, :any}, {:type, line, :any, []}]})
end
defp typespec_to_quoted({:type, anno, :range, [left, right]}) do
{:.., meta(anno), [typespec_to_quoted(left), typespec_to_quoted(right)]}
defp typespec_to_quoted({:type, line, :range, [left, right]}) do
{:.., [line: line], [typespec_to_quoted(left), typespec_to_quoted(right)]}
end
defp typespec_to_quoted({:type, _anno, nil, []}) do
defp typespec_to_quoted({:type, _line, nil, []}) do
[]
end
defp typespec_to_quoted({:type, anno, name, args}) do
defp typespec_to_quoted({:type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, meta(anno), args}
{name, [line: line], args}
end
defp typespec_to_quoted({:var, anno, var}) do
{erl_to_ex_var(var), meta(anno), nil}
defp typespec_to_quoted({:var, line, var}) do
{erl_to_ex_var(var), [line: line], nil}
end
defp typespec_to_quoted({:op, anno, op, arg}) do
{op, meta(anno), [typespec_to_quoted(arg)]}
defp typespec_to_quoted({:op, line, op, arg}) do
{op, [line: line], [typespec_to_quoted(arg)]}
end
defp typespec_to_quoted({:remote_type, anno, [mod, name, args]}) do
remote_type(anno, mod, name, args)
defp typespec_to_quoted({:remote_type, line, [mod, name, args]}) do
remote_type(line, mod, name, args)
end
defp typespec_to_quoted({:ann_type, anno, [var, type]}) do
{:"::", meta(anno), [typespec_to_quoted(var), typespec_to_quoted(type)]}
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
{:"::", [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
end
defp typespec_to_quoted(
{:typed_record_field, {:record_field, anno1, {:atom, anno2, name}}, type}
{:typed_record_field, {:record_field, line, {:atom, line1, name}}, type}
) do
typespec_to_quoted({:ann_type, anno1, [{:var, anno2, name}, type]})
typespec_to_quoted({:ann_type, line, [{:var, line1, name}, type]})
end
defp typespec_to_quoted({:type, _, :any}) do
@@ -363,7 +359,7 @@ defmodule Code.Typespec do
typespec_to_quoted(type)
end
defp typespec_to_quoted({type, _anno, atom}) when is_atom(type) do
defp typespec_to_quoted({type, _line, atom}) when is_atom(type) do
atom
end
@@ -371,30 +367,30 @@ defmodule Code.Typespec do
## Helpers
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
typespec_to_quoted({:type, anno, :charlist, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
typespec_to_quoted({:type, line, :charlist, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
typespec_to_quoted({:type, anno, :nonempty_charlist, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
typespec_to_quoted({:type, line, :nonempty_charlist, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
typespec_to_quoted({:type, anno, :struct, []})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
typespec_to_quoted({:type, line, :struct, []})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
typespec_to_quoted({:type, anno, :as_boolean, [arg]})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
typespec_to_quoted({:type, line, :as_boolean, [arg]})
end
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
typespec_to_quoted({:type, anno, :keyword, args})
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
typespec_to_quoted({:type, line, :keyword, args})
end
defp remote_type(anno, mod, name, args) do
defp remote_type(line, mod, name, args) do
args = for arg <- args, do: typespec_to_quoted(arg)
dot = {:., meta(anno), [typespec_to_quoted(mod), typespec_to_quoted(name)]}
{dot, meta(anno), args}
dot = {:., [line: line], [typespec_to_quoted(mod), typespec_to_quoted(name)]}
{dot, [line: line], args}
end
defp erl_to_ex_var(var) do
@@ -418,6 +414,4 @@ defmodule Code.Typespec do
defp unpack_typespec_kw(_, _acc) do
:error
end
defp meta(anno), do: [line: :erl_anno.line(anno)]
end
+31 -57
View File
@@ -17,8 +17,8 @@ defprotocol Collectable do
This design is intentional. `Enumerable` was designed to support infinite
collections, resources and other structures with fixed shape. For example,
it doesn't make sense to insert values into a `Range`, as it has a
fixed shape where only the range limits and step are stored.
it doesn't make sense to insert values into a range, as it has a fixed
shape where just the range limits are stored.
The `Collectable` module was designed to fill the gap left by the
`Enumerable` protocol. `Collectable.into/1` can be seen as the opposite of
@@ -27,44 +27,32 @@ defprotocol Collectable do
## Examples
To show how to manually use the `Collectable` protocol, let's play with a
simplified implementation for `MapSet`.
To show how to manually use the `Collectable` protocol, let's play with its
implementation for `MapSet`.
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
...> 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
implementation for `MapSet`. In this implementation "collecting" elements
simply means inserting them in the set through `MapSet.put/2`.
defimpl Collectable, for: MapSet do
def into(map_set) do
def into(original) do
collector_fun = fn
map_set_acc, {:cont, elem} ->
MapSet.put(map_set_acc, elem)
map_set_acc, :done ->
map_set_acc
_map_set_acc, :halt ->
:ok
set, {:cont, elem} -> MapSet.put(set, elem)
set, :done -> set
_set, :halt -> :ok
end
initial_acc = map_set
{initial_acc, collector_fun}
{original, collector_fun}
end
end
So now we can call `Enum.into/2`:
iex> Enum.into([1, 2, 3], MapSet.new())
MapSet.new([1, 2, 3])
"""
@type command :: {:cont, term} | :done | :halt
@@ -72,11 +60,8 @@ defprotocol Collectable do
@doc """
Returns an initial accumulator and a "collector" function.
Receives a `collectable` which can be used as the initial accumulator that will
be passed to the function.
The collector function receives a term and a command and injects the term into
the collectable accumulator on every `{:cont, term}` command.
The returned function receives a term and a command and injects the term into
the collectable on every `{:cont, term}` command.
`:done` is passed as a command when no further values will be injected. This
is useful when there's a need to close resources or normalizing values. A
@@ -88,17 +73,16 @@ defprotocol Collectable do
For examples on how to use the `Collectable` protocol and `into/1` see the
module documentation.
"""
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
@spec into(t) :: {term, (term, command -> t | term)}
def into(collectable)
end
defimpl Collectable, for: List do
def into(list) do
# TODO: Change the behaviour so the into always comes last on Elixir v2.0
if list != [] do
def into(original) do
if original != [] do
IO.warn(
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
"Enum.into/2 and \"for\" comprehensions with an :into option is incorrect " <>
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
"list, consider concatenating the two lists with the ++ operator."
@@ -106,14 +90,9 @@ defimpl Collectable, for: List do
end
fun = fn
list_acc, {:cont, elem} ->
[elem | list_acc]
list_acc, :done ->
list ++ :lists.reverse(list_acc)
_list_acc, :halt ->
:ok
list, {:cont, x} -> [x | list]
list, :done -> original ++ :lists.reverse(list)
_, :halt -> :ok
end
{[], fun}
@@ -121,7 +100,7 @@ defimpl Collectable, for: List do
end
defimpl Collectable, for: BitString do
def into(binary) when is_binary(binary) do
def into(original) when is_binary(original) do
fun = fn
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
[acc | x]
@@ -138,14 +117,14 @@ defimpl Collectable, for: BitString do
acc, :done ->
IO.iodata_to_binary(acc)
__acc, :halt ->
_, :halt ->
:ok
end
{[binary], fun}
{[original], fun}
end
def into(bitstring) do
def into(original) when is_bitstring(original) do
fun = fn
acc, {:cont, x} when is_bitstring(x) ->
<<acc::bitstring, x::bitstring>>
@@ -153,27 +132,22 @@ defimpl Collectable, for: BitString do
acc, :done ->
acc
_acc, :halt ->
_, :halt ->
:ok
end
{bitstring, fun}
{original, fun}
end
end
defimpl Collectable, for: Map do
def into(map) do
def into(original) do
fun = fn
map_acc, {:cont, {key, value}} ->
Map.put(map_acc, key, value)
map_acc, :done ->
map_acc
_map_acc, :halt ->
:ok
map, {:cont, {k, v}} -> Map.put(map, k, v)
map, :done -> map
_, :halt -> :ok
end
{map, fun}
{original, fun}
end
end
+16 -47
View File
@@ -16,8 +16,7 @@ defmodule Config do
import_config "#{config_env()}.exs"
`import Config` will import the functions `config/2`, `config/3`
`config_env/0`, `config_target/0`, and `import_config/1`
to help you manage your configuration.
and `import_config/1` to help you manage your configuration.
`config/2` and `config/3` are used to define key-value configuration
for a given application. Once Mix starts, it will automatically
@@ -34,12 +33,10 @@ 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. For more information,
read our [library guidelines](library-guidelines.md).
## Migrating from `use Mix.Config`
@@ -62,27 +59,7 @@ defmodule Config do
import_config config
end
The last step is to replace all `Mix.env()` calls in the config files with `config_env()`.
Keep in mind you must also avoid using `Mix.env()` inside your project files.
To check the environment at _runtime_, you may add a configuration key:
# config.exs
...
config :my_app, env: config_env()
Then, in other scripts and modules, you may get the environment with
`Application.fetch_env!/2`:
# router.exs
...
if Application.fetch_env!(:my_app, :env) == :prod do
...
end
The only files where you may access functions from the `Mix` module are
the `mix.exs` file and inside custom Mix tasks, which always within the
`Mix.Tasks` namespace.
The last step is to replace all `Mix.env()` calls by `config_env()`.
## config/runtime.exs
@@ -95,7 +72,7 @@ defmodule Config do
@config_key {__MODULE__, :config}
@imports_key {__MODULE__, :imports}
defp get_opts!(), do: Process.get(@opts_key) || raise_improper_use!()
defp get_opts!(), do: Process.get(@opts_key)
defp put_opts(value), do: Process.put(@opts_key, value)
defp delete_opts(), do: Process.delete(@opts_key)
@@ -122,9 +99,8 @@ defmodule Config do
The given `opts` are merged into the existing configuration
for the given `root_key`. Conflicting keys are overridden by the
ones specified in `opts`, unless they are keywords, which are
deep merged recursively. For example, the application configuration
below
ones specified in `opts`. For example, the application
configuration below
config :logger,
level: :warn,
@@ -159,30 +135,21 @@ defmodule Config do
The given `opts` are merged into the existing values for `key`
in the given `root_key`. Conflicting keys are overridden by the
ones specified in `opts`, unless they are keywords, which are
deep merged recursively. For example, the application configuration
below
ones specified in `opts`. For example, the application
configuration below
config :ecto, Repo,
log_level: :warn,
adapter: Ecto.Adapters.Postgres,
metadata: [read_only: true]
adapter: Ecto.Adapters.Postgres
config :ecto, Repo,
log_level: :info,
pool_size: 10,
metadata: [replica: true]
pool_size: 10
will have a final value of the configuration for the `Repo`
key in the `:ecto` application of:
Application.get_env(:ecto, Repo)
#=> [
#=> log_level: :info,
#=> pool_size: 10,
#=> adapter: Ecto.Adapters.Postgres,
#=> metadata: [read_only: true, replica: true]
#=> ]
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
"""
@doc since: "1.9.0"
@@ -321,6 +288,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
+23 -48
View File
@@ -11,55 +11,18 @@ defmodule Config.Provider do
the file system. For more information on runtime configuration,
see `mix release`.
## Multiple config files
## Sample config provider
One common use of config providers is to specify multiple
configuration files in a release. Elixir ships with one provider,
called `Config.Reader`, which is capable of handling Elixir's
built-in config files.
For example, imagine you want to list some basic configuration
on Mix's built-in `config/runtime.exs` file, but you also want
to support additional configuration files. To do so, you can add
this inside the `def project` portion of your `mix.exs`:
releases: [
demo: [
config_providers: [
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
]
]
]
You can place this `extra_config.exs` file in your release in
multiple ways:
1. If it is available on the host when assembling the release,
you can place it on "rel/overlays/extra_config.exs" and it
will be automatically copied to the release root
2. If it is available on the target during deployment, you can
simply copy it to the release root as a step in your deployment
Now once the system boots, it will load both `config/runtime.exs`
and `extra_config.exs` early in the boot process. You can learn
more options on `Config.Reader`.
## Custom config provider
You can also implement custom config providers, similar to how
`Config.Reader` works. For example, imagine you need to load
some configuration from a JSON file and load that into the system.
Said configuration provider would look like:
For example, imagine you need to load some configuration from
a JSON file and load that into the system. Said configuration
provider would look like:
defmodule JSONConfigProvider do
@behaviour Config.Provider
# Let's pass the path to the JSON file as config
@impl true
def init(path) when is_binary(path), do: path
@impl true
def load(config, path) do
# We need to start any app we may depend on.
{:ok, _} = Application.ensure_all_started(:jason)
@@ -76,17 +39,19 @@ defmodule Config.Provider do
end
end
Then, when specifying your release, you can specify the provider in
Then when specifying your release, you can specify the provider in
the release configuration:
releases: [
demo: [
config_providers: [
{JSONConfigProvider, "/etc/config.json"}
]
# ...,
config_providers: [{JSONConfigProvider, "/etc/config.json"}]
]
]
Now once the system boots, it will invoke the provider early in
the boot process, save the merged configuration to the disk, and
reboot the system with the new values in place.
"""
@type config :: keyword
@@ -128,7 +93,7 @@ defmodule Config.Provider do
Loads configuration (typically during system boot).
It receives the current `config` and the `state` returned by
`c:init/1`. Then, you typically read the extra configuration
`c:init/1`. Then you typically read the extra configuration
from an external source and merge it into the received `config`.
Merging should be done with `Config.Reader.merge/2`, as it
performs deep merge. It should return the updated config.
@@ -230,6 +195,7 @@ defmodule Config.Provider do
end
defp boot_providers(path, provider, reboot_config, reboot_fun) do
validate_no_cyclic_boot!(path)
original_config = read_config!(path)
config =
@@ -341,7 +307,8 @@ defmodule Config.Provider do
defp restart_and_sleep() do
mode = Application.get_env(:elixir, @reboot_mode_key)
if mode in [:embedded, :interactive] do
# TODO: Remove otp_release check once we require Erlang/OTP 23+
if :erlang.system_info(:otp_release) >= '23' and mode in [:embedded, :interactive] do
:init.restart(mode: mode)
else
:init.restart()
@@ -353,6 +320,14 @@ defmodule Config.Provider do
defp booted_value(%{prune_runtime_sys_config_after_boot: true}, path), do: {:booted, path}
defp booted_value(%{prune_runtime_sys_config_after_boot: false}, _path), do: {:booted, nil}
defp validate_no_cyclic_boot!(path) do
if System.get_env("ELIXIR_CONFIG_PROVIDER_BOOTED") do
bad_path_abort("Got infinite loop when running Config.Provider", path)
else
System.put_env("ELIXIR_CONFIG_PROVIDER_BOOTED", "1")
end
end
defp read_config!(path) do
case :file.consult(path) do
{:ok, [inner]} ->
@@ -409,7 +384,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
+11 -17
View File
@@ -4,27 +4,19 @@ defmodule Config.Reader do
## As a provider
`Config.Reader` can also be used as a `Config.Provider`. A config
provider is used during releases to customize how applications are
configured. When used as a provider, it expects a single argument:
the configuration path (as outlined in `t:Config.Provider.config_path/0`)
for the file to be read and loaded during the system boot.
`Config.Reader` can also be used as a `Config.Provider`. When used
as a provider, it expects a single argument: the configuration path
(as outlined in `t:Config.Provider.config_path/0`) for the file to
be read and loaded during the system boot.
For example, if you expect the target system to have a config file
in an absolute path, you can add this inside the `def project` portion
of your `mix.exs`:
in an absolute path, you can configure your `mix release` as:
releases: [
demo: [
config_providers: [
{Config.Reader, "/etc/config.exs"}
]
]
]
config_providers: [{Config.Reader, "/etc/config.exs"}]
Or if you want to read a custom path inside the release:
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
You can also pass a keyword list of options to the reader,
where the `:path` is a required key:
@@ -36,8 +28,10 @@ defmodule Config.Reader do
imports: :disabled}
]
Remember Mix already loads `config/runtime.exs` by default.
For more examples and scenarios, see the `Config.Provider` module.
Note by default Mix releases supports runtime configuration via
a `config/runtime.exs`. If a `config/runtime.exs` exists in your
application, it is automatically copied inside the release and
automatically set as a config provider.
"""
@behaviour Config.Provider
+1 -1
View File
@@ -23,7 +23,7 @@ defmodule Dict do
import Kernel, except: [size: 1]
if __CALLER__.module != HashDict do
IO.warn("use Dict is deprecated. " <> unquote(message), __CALLER__)
IO.warn("use Dict is deprecated. " <> unquote(message), Macro.Env.stacktrace(__CALLER__))
end
quote do
+74 -126
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
@@ -187,10 +147,10 @@ defmodule DynamicSupervisor do
extra_arguments: [term()]
}
@typedoc "Options given to `start_link` functions"
@typedoc "Option values used by the `start*` functions"
@type option :: GenServer.option()
@typedoc "Options given to `start_link` and `init/1` functions"
@typedoc "Options given to `start_link/1` and `init/1`"
@type init_option ::
{:strategy, strategy()}
| {:max_restarts, non_neg_integer()}
@@ -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
@@ -497,7 +419,7 @@ defmodule DynamicSupervisor do
@doc since: "1.6.0"
@spec which_children(Supervisor.supervisor()) :: [
# module() | :dynamic here because :supervisor.modules() is not exported
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
{:undefined, pid | :restarting, :worker | :supervisor, module() | :dynamic}
]
def which_children(supervisor) do
call(supervisor, :which_children)
@@ -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)
+301 -1265
View File
File diff suppressed because it is too large Load Diff
+74 -360
View File
@@ -43,16 +43,10 @@ defmodule Exception do
@callback blame(t, stacktrace) :: {t, stacktrace}
@optional_callbacks [blame: 2]
@doc false
# Callback for formatting Erlang exceptions
def format_error(%struct{} = exception, _stacktrace) do
%{general: message(exception), reason: "#" <> Atom.to_string(struct)}
end
@doc """
Returns `true` if the given `term` is an exception.
"""
# TODO: Deprecate this on Elixir v1.15
# TODO: Remove this on Elixir v1.15
@doc deprecated: "Use Kernel.is_exception/1 instead"
def exception?(term)
def exception?(%_{__exception__: true}), do: true
@@ -68,7 +62,7 @@ defmodule Exception do
caught_exception ->
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(message(caught_exception))} while retrieving Exception.message/1 " <>
"for #{inspect(exception)}. Stacktrace:\n#{format_stacktrace(__STACKTRACE__)}"
"for #{inspect(exception)}"
else
result when is_binary(result) ->
result
@@ -217,21 +211,16 @@ defmodule Exception do
clauses =
for {meta, ex_args, guards, _block} <- clauses do
scope = :elixir_erl.scope(meta, true)
ann = :elixir_erl.get_ann(meta)
{erl_args, scope} =
:elixir_erl_clauses.match(ann, &:elixir_erl_pass.translate_args/3, ex_args, scope)
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
{args, binding} =
[call_args, ex_args, erl_args]
|> Enum.zip()
|> Enum.map_reduce([], &blame_arg/2)
guards =
guards
|> Enum.map(&blame_guard(&1, ann, scope, binding))
|> Enum.map(&Macro.prewalk(&1, fn guard -> translate_guard(guard) end))
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
{args, guards}
end
@@ -241,69 +230,6 @@ defmodule Exception do
end
end
defp is_map_node?({:is_map, _, [_]}), do: true
defp is_map_node?(_), do: false
defp is_map_key_node?({:is_map_key, _, [_, _]}), do: true
defp is_map_key_node?(_), do: false
defp struct_validation_node?(
{:is_atom, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}]}
),
do: true
defp struct_validation_node?(
{:==, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}, _module]}
),
do: true
defp struct_validation_node?(_), do: false
defp is_struct_macro?(
{:and, _,
[
{:and, _, [%{node: node_1 = {_, _, [arg]}}, %{node: node_2 = {_, _, [arg, _]}}]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(
{:and, _,
[
{:and, _,
[
{:and, _,
[
%{node: node_1 = {_, _, [arg]}},
{:or, _, [%{node: {:is_atom, _, [_]}}, %{node: :fail}]}
]},
%{node: node_2 = {_, _, [arg, _]}}
]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}, _]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(_), do: false
defp translate_guard(guard) do
if is_struct_macro?(guard) do
undo_is_struct_guard(guard)
else
guard
end
end
defp undo_is_struct_guard({:and, meta, [_, %{node: {_, _, [{_, _, [_, 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}
@@ -326,19 +252,19 @@ defmodule Exception do
defp rewrite_arg(arg) do
Macro.prewalk(arg, fn
{:%{}, meta, [__struct__: Range, first: first, last: last, step: step]} ->
{:"..//", meta, [first, last, step]}
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
{:.., meta, [first, last]}
other ->
other
end)
end
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, ann, scope, binding)
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, scope, binding)
when op == :andalso or op == :orelse do
guards = [
blame_guard(left, ann, scope, binding),
blame_guard(right, ann, scope, binding)
blame_guard(left, scope, binding),
blame_guard(right, scope, binding)
]
kernel_op =
@@ -347,38 +273,23 @@ defmodule Exception do
:andalso -> :and
end
evaluate_guard(kernel_op, meta, guards)
{kernel_op, meta, guards}
end
defp blame_guard(ex_guard, ann, scope, binding) do
ex_guard
|> blame_guard?(binding, ann, scope)
|> blame_wrap(rewrite_guard(ex_guard))
end
defp blame_guard(ex_guard, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
defp blame_guard?(ex_guard, binding, ann, scope) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
end
defp evaluate_guard(kernel_op, meta, guards = [_, _]) do
[x, y] = Enum.map(guards, &evaluate_guard/1)
logic_value =
case kernel_op do
:or -> x or y
:and -> x and y
match? =
try do
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
end
{kernel_op, Keyword.put(meta, :value, logic_value), guards}
blame_wrap(match?, rewrite_guard(ex_guard))
end
defp evaluate_guard(%{match?: value}), do: value
defp evaluate_guard({_, meta, _}) when is_list(meta), do: meta[:value]
defp rewrite_guard(guard) do
Macro.prewalk(guard, fn
{{:., _, [mod, fun]}, meta, args} -> erl_to_ex(mod, fun, args, meta)
@@ -704,12 +615,12 @@ defmodule Exception do
case Code.Identifier.extract_anonymous_fun_parent(fun) do
{outer_name, outer_arity} ->
"anonymous fn#{format_arity(arity)} in " <>
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, outer_name)}/#{outer_arity}"
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(outer_name)}/#{outer_arity}"
:error ->
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, fun)}#{format_arity(arity)}"
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(fun)}#{format_arity(arity)}"
end
end
@@ -779,17 +690,6 @@ defmodule Exception do
end
end
@doc false
def format_snippet(snippet, error_line) do
line_digits = error_line |> Integer.to_string() |> byte_size()
placeholder = String.duplicate(" ", max(line_digits, 2))
padding = if line_digits < 2, do: " "
" #{placeholder} |\n" <>
" #{padding}#{error_line} | #{snippet.content}\n" <>
" #{placeholder} | #{String.duplicate(" ", snippet.offset)}^"
end
defp format_location(opts) when is_list(opts) do
format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line), " ")
end
@@ -809,30 +709,29 @@ defmodule ArgumentError do
@impl true
def blame(
exception,
%{message: "argument error"} = exception,
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
) do
message =
cond do
not proper_list?(args) ->
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
# Note that args may be an empty list even if they were supplied
not is_atom(module) and is_atom(function) and args == [] ->
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
"If you are using Kernel.apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as module.function(), " <>
"make sure the left-hand side of the dot is a module atom"
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
"If you are using apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as map.field or module.function(), " <>
"make sure the left side of the dot is an atom or a map"
not is_atom(module) ->
"you attempted to apply a function on #{inspect(module)}. " <>
"Modules (the first argument of apply) must always be an atom"
not is_atom(function) ->
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)}. " <>
"However #{inspect(function)} is not a valid function name. Function names (the second argument " <>
"of apply) must always be an atom"
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
"Functions (the second argument of apply) must always be an atom"
not is_list(args) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
end
{%{exception | message: message}, stacktrace}
@@ -841,9 +740,6 @@ defmodule ArgumentError do
def blame(exception, stacktrace) do
{exception, stacktrace}
end
defp proper_list?(list) when length(list) >= 0, do: true
defp proper_list?(_), do: false
end
defmodule ArithmeticError do
@@ -887,60 +783,29 @@ defmodule ArithmeticError do
end
defmodule SystemLimitError do
defexception message: "a system limit has been reached"
defexception []
@impl true
def message(_) do
"a system limit has been reached"
end
end
defmodule SyntaxError do
defexception [:file, :line, :column, :snippet, description: "syntax error"]
defexception [:file, :line, :column, description: "syntax error"]
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description,
snippet: snippet
})
when not is_nil(snippet) and not is_nil(column) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
end
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description
}) do
def message(%{file: file, line: line, column: column, description: description}) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description
end
end
defmodule TokenMissingError do
defexception [:file, :line, :snippet, :column, description: "expression is incomplete"]
defexception [:file, :line, :column, description: "expression is incomplete"]
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description,
snippet: snippet
})
when not is_nil(snippet) and not is_nil(column) do
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
end
@impl true
def message(%{
file: file,
line: line,
column: column,
description: description
}) do
def message(%{file: file, line: line, column: column, description: description}) do
Exception.format_file_line_column(file && Path.relative_to_cwd(file), line, column) <>
" " <> description
end
@@ -1112,8 +977,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
@@ -1127,23 +992,11 @@ defmodule UndefinedFunctionError do
@doc false
def hint_for_loaded_module(module, function, arity, exports) do
cond do
macro_exported?(module, function, arity) ->
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
message = otp_obsolete(module, function, arity) ->
", #{message}"
true ->
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
defp otp_obsolete(module, function, arity) do
case :otp_internal.obsolete(module, function, arity) do
{:removed, [_ | _] = string} -> string
_ -> nil
if macro_exported?(module, function, arity) do
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
else
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
@@ -1173,12 +1026,12 @@ defmodule UndefinedFunctionError do
case result do
[] -> []
suggestions -> [". Did you mean:\n\n" | Enum.map(suggestions, &format_fa/1)]
suggestions -> [". Did you mean one of:\n\n" | Enum.map(suggestions, &format_fa/1)]
end
end
defp format_fa({_dist, fun, arity}) do
[" * ", Macro.inspect_atom(:remote_call, fun), ?/, Integer.to_string(arity), ?\n]
[" * ", Code.Identifier.inspect_as_function(fun), ?/, Integer.to_string(arity), ?\n]
end
defp behaviour_hint(module, function, arity) do
@@ -1204,9 +1057,7 @@ defmodule UndefinedFunctionError do
end
defp expects_callback?(behaviour, function, arity) do
callbacks =
behaviour.behaviour_info(:callbacks) -- behaviour.behaviour_info(:optional_callbacks)
callbacks = behaviour.behaviour_info(:callbacks)
Enum.member?(callbacks, {function, arity})
end
@@ -1248,7 +1099,7 @@ defmodule FunctionClauseError do
%{module: module, function: function, arity: arity} ->
formatted = Exception.format_mfa(module, function, arity)
blamed = blame(exception, &inspect/1, &blame_match/1)
blamed = blame(exception, &inspect/1, &blame_match/2)
"no function clause matching in #{formatted}" <> blamed
end
end
@@ -1270,24 +1121,24 @@ defmodule FunctionClauseError do
end
end
defp blame_match(%{match?: true, node: node}), do: Macro.to_string(node)
defp blame_match(%{match?: false, node: node}), do: "-" <> Macro.to_string(node) <> "-"
defp blame_match(%{match?: true, node: node}, _), do: Macro.to_string(node)
defp blame_match(%{match?: false, node: node}, _), do: "-" <> Macro.to_string(node) <> "-"
defp blame_match(_, string), do: string
@doc false
def blame(%{args: nil}, _, _) do
""
end
def blame(exception, inspect_fun, fun) do
def blame(exception, inspect_fun, ast_fun) do
%{module: module, function: function, arity: arity, kind: kind, args: args, clauses: clauses} =
exception
mfa = Exception.format_mfa(module, function, arity)
format_clause_fun = fn {args, guards} ->
args = Enum.map_join(args, ", ", fun)
base = " #{kind} #{function}(#{args})"
Enum.reduce(guards, base, &"#{&2} when #{clause_to_string(&1, fun)}") <> "\n"
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
end
"\n\nThe following arguments were given to #{mfa}:\n" <>
@@ -1295,11 +1146,6 @@ defmodule FunctionClauseError do
"#{format_clauses(clauses, format_clause_fun, @clause_limit)}"
end
defp clause_to_string({op, _, [left, right]}, fun),
do: clause_to_string(left, fun) <> " #{op} " <> clause_to_string(right, fun)
defp clause_to_string(node, fun), do: fun.(node)
defp format_args(args, inspect_fun) do
args
|> Enum.with_index(1)
@@ -1336,13 +1182,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
@@ -1404,10 +1248,6 @@ defmodule KeyError do
end
@impl true
def blame(exception = %{message: message}, stacktrace) when is_binary(message) do
{exception, stacktrace}
end
def blame(exception = %{term: nil}, stacktrace) do
message = message(exception.key, exception.term)
{%{exception | message: message}, stacktrace}
@@ -1446,7 +1286,7 @@ defmodule KeyError do
case suggestions do
[] -> []
suggestions -> [". Did you mean:\n\n" | format_suggestions(suggestions)]
suggestions -> [". Did you mean one of:\n\n" | format_suggestions(suggestions)]
end
end
@@ -1557,38 +1397,24 @@ 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{}
end
def normalize(:badarg, _stacktrace) do
%ArgumentError{}
end
def normalize(:badarith, _stacktrace) do
%ArithmeticError{}
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, default_reason) do
{:ok, reason, details} -> %SystemLimitError{message: reason <> details}
:error -> %SystemLimitError{}
end
def normalize(:system_limit, _stacktrace) do
%SystemLimitError{}
end
def normalize(:cond_clause, _stacktrace) do
@@ -1633,19 +1459,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 +1489,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
@@ -1690,104 +1504,4 @@ defmodule ErlangError do
defp from_stacktrace(_) do
{nil, nil, nil}
end
defp error_info(erl_exception, stacktrace, default_reason) do
with [{module, fun, args_or_arity, opts} | tail] <- 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
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
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
end
else
_ -> :error
end
end
defp arg_error({n, message}), do: " * #{nth(n)} argument: #{message}\n"
defp nth(1), do: "1st"
defp nth(2), do: "2nd"
defp nth(3), do: "3rd"
defp nth(n), do: "#{n}th"
end
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
+90 -147
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
@@ -772,11 +764,15 @@ defmodule File do
end
@doc """
Copies the contents of `source_file` to `destination_file` preserving its modes.
Copies the contents in `source_file` to `destination_file` preserving its modes.
`source_file` must be a file or a symbolic link to one. `destination_file` must
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
will be returned.
`source_file` and `destination_file` must be a file or a symbolic link to one,
or in the case of destination, a path to a non-existent file. If either one of
them is a directory, `{:error, :eisdir}` will be returned.
If a file already exists in the destination, it invokes a
callback which should return `true` if the existing file
should be overwritten, `false` otherwise. The callback defaults to return `true`.
The function returns `:ok` in case of success. Otherwise, it returns
`{:error, reason}`.
@@ -789,30 +785,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 +807,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 +832,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 +854,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 +863,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 +881,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 +891,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 +907,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 +924,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 +943,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 +954,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 +980,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 +1043,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 +1193,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}
@@ -1589,12 +1541,6 @@ defmodule File do
executes the given function and then reverts back
to the previous path regardless of whether there is an exception.
The current working directory is temporarily set for the BEAM globally. This
can lead to race conditions if multiple processes are changing the current
working directory concurrently. To run an external command in a given
directory without changing the global current working directory, use the
`:cd` option of `System.cmd/3` and `Port.open/2`.
Raises an error if retrieving or changing the current
directory fails.
"""
@@ -1654,16 +1600,14 @@ 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,
which means it can be used both for read and write.
The `line_or_bytes` argument configures how the file is read when
streaming, by `:line` (default) or by a given number of bytes. When
using the `:line` option, CRLF line breaks (`"\r\n"`) are normalized
to LF (`"\n"`).
streaming, by `:line` (default) or by a given number of bytes.
Operating the stream can fail on open for the same reasons as
`File.open!/2`. Note that the file is automatically opened each time streaming
@@ -1852,8 +1796,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
+53 -162
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,71 +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`.
`base` must be a float and `exponent` can be any number.
However, if a negative base and a fractional exponent
are given, it raises `ArithmeticError`.
It always returns a float. See `Integer.pow/2` for
exponentiation that returns integers.
## Examples
iex> Float.pow(2.0, 0)
1.0
iex> Float.pow(2.0, 1)
2.0
iex> Float.pow(2.0, 10)
1024.0
iex> Float.pow(2.0, -1)
0.5
iex> Float.pow(2.0, -3)
0.125
iex> Float.pow(3.0, 1.5)
5.196152422706632
iex> Float.pow(-2.0, 3)
-8.0
iex> Float.pow(-2.0, 4)
16.0
iex> Float.pow(-1.0, 0.5)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(float, number) :: float
def pow(base, exponent) when is_float(base) and is_number(exponent),
do: :math.pow(base, exponent)
@doc """
Parses a binary into a float.
@@ -119,8 +54,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 +70,6 @@ defmodule Float do
iex> Float.parse("pi")
:error
iex> Float.parse("1.7976931348623159e+308")
:error
"""
@spec parse(binary) :: {float, binary} | :error
@@ -175,18 +107,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
@@ -347,11 +268,11 @@ defmodule Float do
raise ArgumentError, invalid_precision_message(precision)
end
defp round(0.0 = num, _precision, _rounding), do: num
defp round(0.0, _precision, _rounding), do: 0.0
defp round(float, precision, rounding) do
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
{num, count} = decompose(significant, 1)
{num, count, _} = decompose(significant, 1)
count = count - exp + 1023
cond do
@@ -403,22 +324,6 @@ defmodule Float do
end
end
defp decompose(significant, initial) do
decompose(significant, 1, 0, initial)
end
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
end
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, last_count, acc)
end
defp decompose(<<>>, _count, last_count, acc) do
{acc, last_count}
end
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
@@ -498,70 +403,63 @@ defmodule Float do
def ratio(0.0), do: {0, 1}
def ratio(float) when is_float(float) do
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
case <<float::float>> do
<<sign::1, 0::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 0)
{sign(sign, num), shift_left(den, 1022)}
{num, den_exp} =
if exp != 0 do
# Floats are expressed like this:
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
#
# We compute the root factors of the mantissa so we have this:
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
{mantissa, count} = root_factors(mantissa, 0)
<<sign::1, exp::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 1)
num = sign(sign, num)
# Now we can move the count around so we have this:
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
if mantissa == 0 do
{1, exp - 1023}
else
num = (1 <<< (52 - count)) + mantissa
den_exp = count - 52 + exp - 1023
{num, den_exp}
case exp - 1023 do
exp when exp > 0 ->
{den, exp} = shift_right(den, exp)
{shift_left(num, exp), den}
exp when exp < 0 ->
{num, shift_left(den, -exp)}
0 ->
{num, den}
end
else
# Subnormals are expressed like this:
# (mantissa) * 2**(-52 + 1 - 1023)
#
# So we compute it to this:
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
#
# Which becomes:
# mantissa * 2**(count-1074)
root_factors(mantissa, -1074)
end
if den_exp > 0 do
{sign(sign, num <<< den_exp), 1}
else
{sign(sign, num), 1 <<< -den_exp}
end
end
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
do: root_factors(mantissa >>> 1, count + 1)
defp decompose(significant, initial) do
decompose(significant, 1, 0, 2, 1, initial)
end
defp root_factors(mantissa, count),
do: {mantissa, count}
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
end
@compile {:inline, sign: 2}
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
end
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
{acc, last_count, last_power}
end
@compile {:inline, sign: 2, shift_left: 2}
defp sign(0, num), do: num
defp sign(1, num), do: -num
defp shift_left(num, times), do: num <<< times
defp shift_right(num, 0), do: {num, 0}
defp shift_right(1, times), do: {1, times}
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
@doc """
Returns a charlist which corresponds to the 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 +473,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)
+4 -4
View File
@@ -2,12 +2,12 @@ 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
integrate with an existing system, such as Elixir's Logger, please use
[`:gen_event`](`:gen_event`) instead.
[`:gen_event`](https://erlang.org/doc/man/gen_event.html) instead.
## Alternatives
@@ -38,7 +38,7 @@ defmodule GenEvent do
If your use case requires exactly what GenEvent provided, or you have to
integrate with an existing `:gen_event`-based system, you can still use the
[`:gen_event`](`:gen_event`) Erlang module.
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
"""
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
@@ -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
+73 -71
View File
@@ -61,7 +61,7 @@ defmodule GenServer do
Every time you do a `GenServer.call/3`, the client will send a message
that must be handled by the `c:handle_call/3` callback in the GenServer.
A `cast/2` message must be handled by `c:handle_cast/2`. There are 8 possible
A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
callbacks to be implemented when you use a `GenServer`. The only required
callback is `c:init/1`.
@@ -153,27 +153,22 @@ defmodule GenServer do
detailed information. The `@doc` annotation immediately preceding
`use GenServer` will be attached to the generated `child_spec/1` function.
When stopping the GenServer, for example by returning a `{:stop, reason, new_state}`
tuple from a callback, the exit reason is used by the supervisor to determine
whether the GenServer needs to be restarted. See the "Exit reasons and restarts"
section in the `Supervisor` module.
## Name registration
Both `start_link/3` and `start/3` support the `GenServer` to register
a name on start via the `:name` option. Registered names are also
automatically cleaned up on termination. The supported values are:
* an atom - the GenServer is registered locally (to the current node)
with the given name using `Process.register/2`.
* an atom - the GenServer is registered locally with the given name
using `Process.register/2`.
* `{:global, term}` - the GenServer is registered globally with the given
term using the functions in the [`:global` module](`:global`).
term using the functions in the [`:global` module](http://www.erlang.org/doc/man/global.html).
* `{:via, module, term}` - the GenServer is registered with the given
mechanism and name. The `:via` option expects a module that exports
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
One such example is the [`:global` module](`:global`) which uses these functions
One such example is the [`:global` module](http://www.erlang.org/doc/man/global.html) which uses these functions
for keeping the list of names of processes and their associated PIDs
that are available globally for a network of Elixir nodes. Elixir also
ships with a local, decentralized and scalable registry called `Registry`
@@ -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.
@@ -247,8 +242,7 @@ defmodule GenServer do
end
defp schedule_work do
# We schedule the work to happen in 2 hours (written in milliseconds).
# Alternatively, one might write :timer.hours(2)
# In 2 hours
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
end
end
@@ -259,20 +253,15 @@ defmodule GenServer do
a timeout value in milliseconds; if not, `:infinity` is assumed.
The timeout can be used to detect a lull in incoming messages.
The `timeout()` value is used as follows:
If the process has no messages waiting when the timeout is set and the
number of given milliseconds pass without any message arriving,
then `handle_info/2` will be called with `:timeout` as the first argument.
The timeout is cleared if any message is waiting or arrives before the
given timeout.
* If the process has any message already waiting when the `timeout()` value
is returned, the timeout is ignored and the waiting message is handled as
usual. This means that even a timeout of `0` milliseconds is not guaranteed
to execute (if you want to take another action immediately and unconditionally,
use a `:continue` instruction instead).
* If any message arrives before the specified number of milliseconds
elapse, the timeout is cleared and that message is handled as usual.
* Otherwise, when the specified number of milliseconds have elapsed with no
message arriving, `handle_info/2` is called with `:timeout` as the first
argument.
Because a message may arrive before the timeout is set, even a timeout of `0`
milliseconds is not guaranteed to execute. To take another action immediately
and unconditionally, use a `:continue` instruction.
## When (not) to use a GenServer
@@ -322,14 +311,14 @@ defmodule GenServer do
## Debugging with the :sys module
GenServers, as [special processes](https://www.erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](`:sys`).
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](http://www.erlang.org/doc/man/sys.html).
Through various hooks, this module allows developers to introspect the state of
the process and trace system events that happen during its execution, such as
received messages, sent replies and state changes.
Let's explore the basic functions from the
[`:sys` module](`:sys`) used for debugging:
[`:sys` module](http://www.erlang.org/doc/man/sys.html) used for debugging:
* `:sys.get_state/2` - allows retrieval of the state of the process.
In the case of a GenServer process, it will be the callback module state,
@@ -411,8 +400,8 @@ defmodule GenServer do
in Erlang can also provide extra insight.
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [`:gen_server` module documentation](`:gen_server`)
* [gen_server Behaviour - OTP Design Principles](https://www.erlang.org/doc/design_principles/gen_server_concepts.html)
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
* [gen_server Behaviour - OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
* [Clients and Servers - Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
"""
@@ -434,10 +423,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
`{: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.
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 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 +449,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
@@ -482,15 +471,14 @@ defmodule GenServer do
Returning `{:reply, reply, new_state, :hibernate}` is similar to
`{:reply, reply, new_state}` except the process is hibernated and will
continue the loop once a message is in its message queue. However, if a message is
already in the message queue, the process will continue the loop immediately. Hibernating a
continue the loop once a message is in its message queue. If a message is
already in the message queue this will be immediately. Hibernating a
`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,12 +501,12 @@ 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.
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
is called with reason `reason` and state `new_state`. Then, the `reply` is sent
is called with reason `reason` and state `new_state`. Then the `reply` is sent
as the response to call and the process exits with reason `reason`.
Returning `{:stop, reason, new_state}` is similar to
@@ -529,10 +517,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 +540,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 +553,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 +570,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.
@@ -598,12 +584,14 @@ defmodule GenServer do
This callback is optional. If one is not implemented, the server will fail
if a continue instruction is used.
This callback is only supported on Erlang/OTP 21+.
"""
@callback handle_continue(continue_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.
@@ -611,13 +599,15 @@ defmodule GenServer do
`reason` is exit reason and `state` is the current state of the `GenServer`.
The return value is ignored.
`c:terminate/2` is called if the `GenServer` traps exits (using `Process.flag/2`)
*and* the parent process sends an exit signal, or a callback (except `c:init/1`)
does one of the following:
`c:terminate/2` is called if a callback (except `c:init/1`) does one of the
following:
* returns a `:stop` tuple
* raises (via `raise/2`) or exits (via `exit/1`)
* raises
* calls `Kernel.exit/1`
* returns an invalid value
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
process sends an exit signal
If part of a supervision tree, a `GenServer` will receive an exit
signal when the tree is shutting down. The exit signal is based on
@@ -683,7 +673,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 +685,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`.
@@ -724,7 +714,7 @@ defmodule GenServer do
{:debug, debug}
| {:name, name}
| {:timeout, timeout}
| {:spawn_opt, [Process.spawn_opt()]}
| {:spawn_opt, Process.spawn_opt()}
| {:hibernate_after, timeout}
@typedoc "Debug options supported by the `start*` functions"
@@ -866,7 +856,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
@@ -903,7 +893,7 @@ defmodule GenServer do
milliseconds initializing or it will be terminated and the start function
will return `{:error, :timeout}`
* `:debug` - if present, the corresponding function in the [`:sys` module](`:sys`) is invoked
* `:debug` - if present, the corresponding function in the [`:sys` module](http://www.erlang.org/doc/man/sys.html) is invoked
* `:spawn_opt` - if present, its value is passed as options to the
underlying process as in `Process.spawn/4`
@@ -1026,7 +1016,6 @@ defmodule GenServer do
nil ->
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
# TODO: remove this clause when we require Erlang/OTP 25+
pid when pid == self() ->
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
@@ -1050,8 +1039,18 @@ defmodule GenServer do
is unknown whether the destination `server` successfully
handled the message.
`c:handle_cast/2` will be called on the server to handle
the request. In case the `server` is on a node which is
not yet connected to the caller one, the semantics differ
depending on the used Erlang/OTP version.
`server` can be any of the values described in the "Name registration"
section of the documentation for this module.
Before Erlang/OTP 21, the call is going to block until a
connection happens. This was done to guarantee ordering.
Starting with Erlang/OTP 21, both Erlang and Elixir do
not block the call.
"""
@spec cast(server, term) :: :ok
def cast(server, request)
@@ -1174,8 +1173,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 """
+80 -235
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`
@@ -95,25 +26,24 @@ defprotocol Inspect do
defimpl Inspect, for: MapSet do
import Inspect.Algebra
def inspect(map_set, opts) do
concat(["MapSet.new(", Inspect.List.inspect(MapSet.to_list(map_set), opts), ")"])
def inspect(dict, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"])
end
end
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from
`Inspect.Algebra` and it concatenates algebra documents together.
In the example above it is concatenating the string `"MapSet.new("`,
In the example above it is concatenating the string `"#MapSet<"`,
the document returned by `Inspect.Algebra.to_doc/2`, and the final
string `")"`. Therefore, the MapSet with the numbers 1, 2, and 3
will be printed as:
string `">"`. We prefix the module name `#` to denote the inspect
presentation is not actually valid Elixir syntax.
iex> MapSet.new([1, 2, 3], fn x -> x * 2 end)
MapSet.new([2, 4, 6])
Finally, note strings themselves are valid algebra documents that
keep their formatting when pretty printed. This means your `Inspect`
implementation may simply return a string, although that will devoid
it of any pretty-printing.
In other words, `MapSet`'s inspect representation returns an expression
that, when evaluated, builds the `MapSet` itself.
### Error handling
## Error handling
In case there is an error while your structure is being inspected,
Elixir will raise an `ArgumentError` error and will automatically fall back
@@ -125,6 +55,24 @@ defprotocol Inspect do
Inspect.MapSet.inspect(MapSet.new(), %Inspect.Opts{})
## Deriving
The `Inspect` protocol can be derived to hide certain fields from
structs, so they don't show up in logs, inspects and similar. This
is especially useful for fields containing private information.
The options `:only` and `:except` can be used with `@derive` to
specify which fields should and should not appear in the
algebra document:
defmodule User do
@derive {Inspect, only: [:id, :name]}
defstruct [:id, :name, :address]
end
inspect(%User{id: 1, name: "Homer", address: "742 Evergreen Terrace"})
#=> #User<id: 1, name: "Homer", ...>
"""
# Handle structs in Any
@@ -146,7 +94,7 @@ defimpl Inspect, for: Atom do
require Macro
def inspect(atom, opts) do
color(Macro.inspect_atom(:literal, atom), color_key(atom), opts)
color(Identifier.inspect_as_atom(atom), color_key(atom), opts)
end
defp color_key(atom) when is_boolean(atom), do: :boolean
@@ -209,7 +157,7 @@ defimpl Inspect, for: BitString do
defp each_bit(bitstring, _counter, opts) do
size = bit_size(bitstring)
<<h::size(size)>> = bitstring
concat(Inspect.Integer.inspect(h, opts), "::size(" <> Integer.to_string(size) <> ")")
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
end
@compile {:inline, decrement: 1}
@@ -262,7 +210,7 @@ defimpl Inspect, for: List do
{escaped, _} -> [?', escaped, ?', " ++ ..."]
end
color(IO.iodata_to_binary(inspected), :charlist, opts)
IO.iodata_to_binary(inspected)
keyword?(term) ->
container_doc(open, term, close, opts, &keyword/2, separator: sep, break: :strict)
@@ -274,7 +222,7 @@ defimpl Inspect, for: List do
@doc false
def keyword({key, value}, opts) do
key = color(Macro.inspect_atom(:key, key), :atom, opts)
key = color(Identifier.inspect_as_key(key), :atom, opts)
concat(key, concat(" ", to_doc(value, opts)))
end
@@ -302,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,44 @@ 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
# Use the :limit option and an extra element to force
# `container_doc/6` to append "...".
opts = %{opts | limit: min(opts.limit, map_size(map))}
map = Map.to_list(map) ++ ["..."]
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)
:..., _opts -> "..."
end
container_doc(open, infos ++ [:...], close, opts, fun, separator: sep, break: :strict)
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
end
end
+29 -120
View File
@@ -31,13 +31,8 @@ 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`.
Defaults to `Inspect.inspect/2`.
* `:limit` - limits the number of items that are inspected for tuples,
bitstrings, maps, lists and any other collection of items, with the exception of
@@ -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()),
@@ -109,62 +105,13 @@ defmodule Inspect.Opts do
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
width: non_neg_integer | :infinity
}
end
@doc """
Builds an `Inspect.Opts` struct.
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
@doc since: "1.13.0"
@spec new(keyword()) :: t
def new(opts) do
struct(%Inspect.Opts{inspect_fun: default_inspect_fun()}, opts)
end
@doc """
Returns the default inspect function.
"""
@doc since: "1.13.0"
@spec default_inspect_fun() :: (term, t -> Inspect.Algebra.t())
def default_inspect_fun do
:persistent_term.get({__MODULE__, :inspect_fun}, &Inspect.inspect/2)
end
@doc """
Sets the default inspect function.
Set this option with care as it will change how all values
in the system are inspected. The main use of this functionality
is to provide an entry point to filter inspected values,
in order for entities to comply with rules and legislations
on data security and data privacy.
It is **extremely discouraged** for libraries to set their own
function as this must be controlled by applications. Libraries
should instead define their own structs with custom inspect
implementations. If a library must change the default inspect
function, then it is best to define to ask users of your library
to explicitly call `default_inspect_fun/1` with your function of
choice.
The default is `Inspect.inspect/2`.
## Examples
previous_fun = Inspect.Opts.default_inspect_fun()
Inspect.Opts.default_inspect_fun(fn
%{address: _} = map, opts ->
previous_fun.(%{map | address: "[REDACTED]"}, opts)
value, opts ->
previous_fun.(value, opts)
end)
"""
@doc since: "1.13.0"
@spec default_inspect_fun((term, t -> Inspect.Algebra.t())) :: :ok
def default_inspect_fun(fun) when is_function(fun, 2) do
:persistent_term.put({__MODULE__, :inspect_fun}, fun)
end
defexception [:message]
end
defmodule Inspect.Algebra do
@@ -259,18 +206,12 @@ defmodule Inspect.Algebra do
| doc_group
| doc_nest
| doc_string
| doc_limit
@typep doc_string :: {:doc_string, t, non_neg_integer}
defmacrop doc_string(string, length) do
quote do: {:doc_string, unquote(string), unquote(length)}
end
@typep doc_limit :: {:doc_limit, t, pos_integer | :infinity}
defmacrop doc_limit(doc, limit) do
quote do: {:doc_limit, unquote(doc), unquote(limit)}
end
@typep doc_cons :: {:doc_cons, t, t}
defmacrop doc_cons(left, right) do
quote do: {:doc_cons, unquote(left), unquote(right)}
@@ -320,8 +261,7 @@ defmodule Inspect.Algebra do
:doc_force,
:doc_group,
:doc_nest,
:doc_string,
:doc_limit
:doc_string
]
defguard is_doc(doc)
@@ -358,28 +298,19 @@ defmodule Inspect.Algebra do
try do
Process.put(:inspect_trap, true)
inspected_struct =
struct
|> Inspect.Map.inspect(%{
opts
| syntax_colors: [],
inspect_fun: Inspect.Opts.default_inspect_fun()
})
|> format(opts.width)
|> IO.iodata_to_binary()
res = Inspect.Map.inspect(struct, %{opts | syntax_colors: []})
res = IO.iodata_to_binary(format(res, :infinity))
inspect_error =
Inspect.Error.exception(
exception: caught_exception,
stacktrace: __STACKTRACE__,
inspected_struct: inspected_struct
)
message =
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(Exception.message(caught_exception))} while inspecting #{res}"
exception = Inspect.Error.exception(message: message)
if opts.safe do
opts = %{opts | inspect_fun: Inspect.Opts.default_inspect_fun()}
Inspect.inspect(inspect_error, opts)
Inspect.inspect(exception, opts)
else
reraise(inspect_error, __STACKTRACE__)
reraise(exception, __STACKTRACE__)
end
after
Process.delete(:inspect_trap)
@@ -473,9 +404,8 @@ defmodule Inspect.Algebra do
defp container_each([term | terms], limit, opts, fun, acc, simple?)
when is_list(terms) and is_limit(limit) do
new_limit = decrement(limit)
doc = fun.(term, %{opts | limit: new_limit})
limit = if doc == :doc_nil, do: limit, else: new_limit
limit = decrement(limit)
doc = fun.(term, %{opts | limit: limit})
container_each(terms, limit, opts, fun, [doc | acc], simple? and simple?(doc))
end
@@ -587,10 +517,6 @@ defmodule Inspect.Algebra do
doc_cons(doc1, doc2)
end
def no_limit(doc) do
doc_limit(doc, :infinity)
end
@doc ~S"""
Concatenates a list of documents returning a new document.
@@ -610,7 +536,7 @@ defmodule Inspect.Algebra do
Colors a document if the `color_key` has a color in the options.
"""
@doc since: "1.4.0"
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: t
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
if precolor = Keyword.get(syntax_colors, color_key) do
postcolor = Keyword.get(syntax_colors, :reset, :reset)
@@ -640,7 +566,7 @@ defmodule Inspect.Algebra do
["hello", "\n ", "world"]
"""
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest | t
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest
def nest(doc, level, mode \\ :always)
def nest(doc, :cursor, mode) when is_doc(doc) and mode in [:always, :break] do
@@ -968,12 +894,12 @@ defmodule Inspect.Algebra do
format(width, 0, [{0, :flat, doc}])
end
# Type representing the document mode to be rendered:
# Type representing the document mode to be rendered
#
# * flat - represents a document with breaks as flats (a break may fit, as it may break)
# * break - represents a document with breaks as breaks (a break always fits, since it breaks)
#
# The following modes are exclusive to fitting:
# The following modes are exclusive to fitting
#
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
@@ -981,7 +907,7 @@ defmodule Inspect.Algebra do
@typep mode :: :flat | :flat_no_break | :break | :break_no_flat
@spec fits?(
width :: non_neg_integer() | :infinity,
width :: non_neg_integer(),
column :: non_neg_integer(),
break? :: boolean(),
entries
@@ -1046,17 +972,9 @@ defmodule Inspect.Algebra do
defp fits?(w, k, b?, [{i, m, doc_group(x, _)} | t]),
do: fits?(w, k, b?, [{i, m, x} | {:tail, b?, t}])
defp fits?(w, k, b?, [{i, m, doc_limit(x, :infinity)} | t]) when w != :infinity,
do: fits?(:infinity, k, b?, [{i, :flat, x}, {i, m, doc_limit(empty(), w)} | t])
defp fits?(_w, k, b?, [{i, m, doc_limit(x, w)} | t]),
do: fits?(w, k, b?, [{i, m, x} | t])
@spec format(
width :: non_neg_integer() | :infinity,
column :: non_neg_integer(),
[{integer, mode, t}]
) :: [binary]
@spec format(width :: non_neg_integer() | :infinity, column :: non_neg_integer(), [
{integer, mode, t}
]) :: [binary]
defp format(_, _, []), do: []
defp format(w, k, [{_, _, :doc_nil} | t]), do: format(w, k, t)
defp format(w, _, [{i, _, :doc_line} | t]), do: [indent(i) | format(w, i, t)]
@@ -1110,15 +1028,6 @@ defmodule Inspect.Algebra do
end
end
# Limit is set to infinity and then reverts
defp format(w, k, [{i, m, doc_limit(x, :infinity)} | t]) when w != :infinity do
format(:infinity, k, [{i, :flat, x}, {i, m, doc_limit(empty(), w)} | t])
end
defp format(_w, k, [{i, m, doc_limit(x, w)} | t]) do
format(w, k, [{i, m, x} | t])
end
defp collapse(["\n" <> _ | t], max, count, i) do
collapse(t, max, count + 1, i)
end
+50 -115
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`
"""
@@ -64,53 +64,10 @@ defmodule Integer do
"""
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
@doc """
Computes `base` raised to power of `exponent`.
Both `base` and `exponent` must be integers.
The exponent must be zero or positive.
See `Float.pow/2` for exponentiation of negative
exponents as well as floats.
## Examples
iex> Integer.pow(2, 0)
1
iex> Integer.pow(2, 1)
2
iex> Integer.pow(2, 10)
1024
iex> Integer.pow(2, 11)
2048
iex> Integer.pow(2, 64)
0x10000000000000000
iex> Integer.pow(3, 4)
81
iex> Integer.pow(4, 3)
64
iex> Integer.pow(-2, 3)
-8
iex> Integer.pow(-2, 4)
16
iex> Integer.pow(2, -2)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(integer, non_neg_integer) :: integer
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
base ** exponent
end
@doc """
Computes the modulo remainder of an integer division.
This function performs a [floored division](`floor_div/2`), which means that
`Integer.mod/2` uses floored division, which means that
the result will always have the sign of the `divisor`.
Raises an `ArithmeticError` exception if one of the arguments is not an
@@ -142,8 +99,8 @@ defmodule Integer do
Raises an `ArithmeticError` exception if one of the arguments is not an
integer, or when the `divisor` is `0`.
This function performs a *floored* integer division, which means that
the result will always be rounded towards negative infinity.
`Integer.floor_div/2` performs *floored* integer division. This means that
the result is always rounded towards negative infinity.
If you want to perform truncated integer division (rounding towards zero),
use `Kernel.div/2` instead.
@@ -269,7 +226,7 @@ defmodule Integer do
** (ArgumentError) invalid base 38
"""
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
@spec parse(binary, 2..36) :: {integer, binary} | :error
def parse(binary, base \\ 10)
def parse(_binary, base) when base not in 2..36 do
@@ -312,12 +269,12 @@ defmodule Integer do
defp count_digits_nosign(<<_::bits>>, _, count), do: count
# TODO: Remove Integer.to_string/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_string/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a binary which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
of `integer`.
Inlined by the compiler.
@@ -335,6 +292,22 @@ defmodule Integer do
iex> Integer.to_string(0123)
"123"
"""
@spec to_string(integer) :: String.t()
def to_string(integer) do
:erlang.integer_to_binary(integer)
end
@doc """
Returns a binary which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_string(100, 16)
"64"
@@ -346,16 +319,15 @@ defmodule Integer do
"""
@spec to_string(integer, 2..36) :: String.t()
def to_string(integer, base \\ 10) do
def to_string(integer, base) do
:erlang.integer_to_binary(integer, base)
end
# TODO: Remove Integer.to_charlist/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_charlist/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a charlist which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
Returns a charlist which corresponds to the text representation of the given `integer`.
Inlined by the compiler.
@@ -373,6 +345,21 @@ defmodule Integer do
iex> Integer.to_charlist(0123)
'123'
"""
@spec to_charlist(integer) :: charlist
def to_charlist(integer) do
:erlang.integer_to_list(integer)
end
@doc """
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_charlist(100, 16)
'64'
@@ -384,7 +371,7 @@ defmodule Integer do
"""
@spec to_charlist(integer, 2..36) :: charlist
def to_charlist(integer, base \\ 10) do
def to_charlist(integer, base) do
:erlang.integer_to_list(integer, base)
end
@@ -427,58 +414,6 @@ defmodule Integer do
defp gcd_positive(integer1, 0), do: integer1
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
@doc """
Returns the extended greatest common divisor of the two given integers.
This function uses the extended Euclidean algorithm to return a three-element tuple with the `gcd`
and the coefficients `m` and `n` of Bézout's identity such that:
gcd(a, b) = m*a + n*b
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
## Examples
iex> Integer.extended_gcd(240, 46)
{2, -9, 47}
iex> Integer.extended_gcd(46, 240)
{2, 47, -9}
iex> Integer.extended_gcd(-46, 240)
{2, -47, -9}
iex> Integer.extended_gcd(-46, -240)
{2, -47, 9}
iex> Integer.extended_gcd(14, 21)
{7, -1, 1}
iex> Integer.extended_gcd(10, 0)
{10, 1, 0}
iex> Integer.extended_gcd(0, 10)
{10, 0, 1}
iex> Integer.extended_gcd(0, 0)
{0, 0, 0}
"""
@doc since: "1.12.0"
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
def extended_gcd(0, 0), do: {0, 0, 0}
def extended_gcd(0, b), do: {b, 0, 1}
def extended_gcd(a, 0), do: {a, 1, 0}
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
extended_gcd(integer2, integer1, 0, 1, 1, 0)
end
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
div = div(r0, r1)
case r0 - div * r1 do
0 when r1 > 0 -> {r1, s1, t1}
0 when r1 < 0 -> {-r1, -s1, -t1}
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
end
end
@doc false
@deprecated "Use Integer.to_charlist/1 instead"
def to_char_list(integer), do: Integer.to_charlist(integer)
+62 -143
View File
@@ -34,7 +34,7 @@ defmodule IO do
IO data is a data type that can be used as a more efficient alternative to binaries
in certain situations.
A term of type **IO data** is a binary or a list containing bytes (integers within the `0..255` range)
A term of type **IO data** is a binary or a list containing bytes (integers in `0..255`)
or nested IO data. The type is recursive. Let's see an example of one of
the possible IO data representing the binary `"hello"`:
@@ -99,8 +99,8 @@ defmodule IO do
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
similar to IO data: the only difference is that integers in IO data represent
bytes while integers in chardata represent Unicode code points. Bytes
(`t:byte/0`) are integers within the `0..255` range, while Unicode code points
(`t:char/0`) are integers within the `0..0x10FFFF` range. The `IO` module provides
(`t:byte/0`) are integers in the `0..255` range, while Unicode code points
(`t:char/0`) are integers in the range `0..0x10FFFF`. The `IO` module provides
the `chardata_to_string/1` function for chardata as the "counter-part" of the
`iodata_to_binary/1` function for IO data.
@@ -108,8 +108,8 @@ defmodule IO do
argument error. For example, let's try to put a code point that is not
representable with one byte, like `?π`, inside IO data:
IO.iodata_to_binary(["The symbol for pi is: ", ?π])
#=> ** (ArgumentError) argument error
iex> IO.iodata_to_binary(["The symbol for pi is: ", ?π])
** (ArgumentError) argument error
If we use chardata instead, it will work as expected:
@@ -122,14 +122,14 @@ defmodule IO do
@type nodata :: {:error, term} | :eof
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
defguardp is_device(term) when is_atom(term) or is_pid(term)
defguardp is_iodata(data) when is_list(data) or is_binary(data)
@doc """
Reads from the IO `device`.
The `device` is iterated by the given number of characters, line by line if
`:line` is given, or until `:eof`.
The `device` is iterated by the given number of characters or line by line if
`:line` is given.
Alternatively, if `:all` is given, then whole `device` is returned.
It returns:
@@ -141,24 +141,14 @@ defmodule IO do
for instance, `{:error, :estale}` if reading from an
NFS volume
If `:all` is given, `:eof` is never returned, but an
empty string in case the device has reached EOF.
"""
@spec read(device, :eof | :line | non_neg_integer) :: chardata | nodata
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
def read(device \\ :stdio, line_or_chars)
# TODO: Deprecate me on v1.17
def read(device, :all) do
with :eof <- read(device, :eof) do
with [_ | _] = opts <- :io.getopts(device),
false <- Keyword.get(opts, :binary, true) do
''
else
_ -> ""
end
end
end
def read(device, :eof) do
getn(device, '', :eof)
do_read_all(map_dev(device), :empty)
end
def read(device, :line) do
@@ -169,20 +159,35 @@ defmodule IO do
:io.get_chars(map_dev(device), '', count)
end
defp do_read_all(mapped_dev, acc) do
case :io.get_line(mapped_dev, "") do
line when is_binary(line) or is_list(line) -> do_read_all(mapped_dev, concat(acc, line))
:eof -> read_eof(mapped_dev, acc)
other -> other
end
end
defp concat(:empty, line), do: line
defp concat(acc, line) when is_binary(acc), do: acc <> line
defp concat(acc, line) when is_list(acc), do: acc ++ line
defp read_eof(device, :empty) do
with [_ | _] = opts <- :io.getopts(device),
false <- Keyword.get(opts, :binary, true) do
''
else
_ -> ""
end
end
defp read_eof(_device, acc), do: acc
@doc """
Reads from the IO `device`. The operation is Unicode unsafe.
The `device` is iterated as specified by the `line_or_chars` argument:
* if `line_or_chars` is an integer, it represents a number of bytes. The device is
iterated by that number of bytes.
* if `line_or_chars` is `:line`, the device is iterated line by line.
* if `line_or_chars` is `:eof`, the device is iterated until `:eof`. `line_or_chars`
can only be `:eof` since Elixir 1.13.0. `:eof` replaces the deprecated `:all`,
with the difference that `:all` returns `""` on end of file, while `:eof` returns
`:eof` itself.
The `device` is iterated by the given number of bytes or line by line if
`:line` is given.
Alternatively, if `:all` is given, then whole `device` is returned.
It returns:
@@ -194,19 +199,17 @@ defmodule IO do
for instance, `{:error, :estale}` if reading from an
NFS volume
If `:all` is given, `:eof` is never returned, but an
empty string in case the device has reached EOF.
Note: do not use this function on IO devices in Unicode mode
as it will return the wrong result.
"""
@spec binread(device, :eof | :line | non_neg_integer) :: iodata | nodata
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
def binread(device \\ :stdio, line_or_chars)
# TODO: Deprecate me on v1.17
def binread(device, :all) do
with :eof <- binread(device, :eof), do: ""
end
def binread(device, :eof) do
binread_eof(map_dev(device), "")
do_binread_all(map_dev(device), "")
end
def binread(device, :line) do
@@ -224,10 +227,10 @@ defmodule IO do
end
@read_all_size 4096
defp binread_eof(mapped_dev, acc) do
defp do_binread_all(mapped_dev, acc) do
case :file.read(mapped_dev, @read_all_size) do
{:ok, data} -> binread_eof(mapped_dev, acc <> data)
:eof -> if acc == "", do: :eof, else: acc
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
:eof -> acc
other -> other
end
end
@@ -290,29 +293,19 @@ defmodule IO do
"""
@spec puts(device, chardata | String.Chars.t()) :: :ok
def puts(device \\ :stdio, item) when is_device(device) do
def puts(device \\ :stdio, item) do
:io.put_chars(map_dev(device), [to_chardata(item), ?\n])
end
@doc """
Writes a `message` to stderr, along with the given `stacktrace_info`.
The `stacktrace_info` must be one of:
* a `__STACKTRACE__`, where all entries in the stacktrace will be
included in the error message
* a `Macro.Env` structure (since v1.14.0), where a single stacktrace
entry from the compilation environment will be used
* a keyword list with at least the `:file` option representing
a single stacktrace entry (since v1.14.0). The `:line`, `:module`,
`:function` options are also supported
Writes a `message` to stderr, along with the given `stacktrace`.
This function also notifies the compiler a warning was printed
(in case --warnings-as-errors was enabled). It returns `:ok`
if it succeeds.
An empty list can be passed to avoid stacktrace printing.
## Examples
stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
@@ -321,34 +314,10 @@ defmodule IO do
#=> my_app.ex:4: MyApp.main/1
"""
@spec warn(chardata | String.Chars.t(), Exception.stacktrace() | keyword() | Macro.Env.t()) ::
:ok
def warn(message, stacktrace_info)
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
def warn(message, []) do
message = [to_chardata(message), ?\n]
:elixir_errors.log_and_print_warning(0, nil, message, message)
end
def warn(message, %Macro.Env{} = env) do
warn(message, Macro.Env.stacktrace(env))
end
def warn(message, [{_, _} | _] = keyword) do
if file = keyword[:file] do
warn(
message,
%{
__ENV__
| module: keyword[:module],
function: keyword[:function],
line: keyword[:line],
file: file
}
)
else
warn(message, [])
end
:elixir_errors.io_warn(0, nil, message, message)
end
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
@@ -357,7 +326,7 @@ defmodule IO do
line = opts[:line]
file = opts[:file]
:elixir_errors.log_and_print_warning(
:elixir_errors.io_warn(
line || 0,
file && List.to_string(file),
message,
@@ -459,9 +428,9 @@ defmodule IO do
See `inspect/2` for a full list of options.
"""
@spec inspect(device, item, keyword) :: item when item: var
def inspect(device, item, opts) when is_device(device) and is_list(opts) do
def inspect(device, item, opts) when is_list(opts) do
label = if label = opts[:label], do: [to_chardata(label), ": "], else: []
opts = Inspect.Opts.new(opts)
opts = struct(Inspect.Opts, opts)
doc = Inspect.Algebra.group(Inspect.Algebra.to_doc(item, opts))
chardata = Inspect.Algebra.format(doc, opts.width)
puts(device, [label, chardata])
@@ -476,18 +445,12 @@ defmodule IO do
Otherwise, `count` is the number of raw bytes to be retrieved.
See `IO.getn/3` for a description of return values.
"""
@spec getn(
device | chardata | String.Chars.t(),
pos_integer | :eof | chardata | String.Chars.t()
) ::
@spec getn(device | chardata | String.Chars.t(), pos_integer | chardata | String.Chars.t()) ::
chardata | nodata
def getn(prompt, count \\ 1)
def getn(prompt, :eof) do
getn(:stdio, prompt, :eof)
end
def getn(prompt, count) when is_integer(count) and count > 0 do
getn(:stdio, prompt, count)
end
@@ -514,27 +477,11 @@ defmodule IO do
NFS volume
"""
@spec getn(device, chardata | String.Chars.t(), pos_integer | :eof) :: chardata | nodata
def getn(device, prompt, :eof) do
getn_eof(map_dev(device), to_chardata(prompt), [])
end
@spec getn(device, chardata | String.Chars.t(), pos_integer) :: chardata | nodata
def getn(device, prompt, count) when is_integer(count) and count > 0 do
:io.get_chars(map_dev(device), to_chardata(prompt), count)
end
defp getn_eof(device, prompt, acc) do
case :io.get_line(device, prompt) do
line when is_binary(line) or is_list(line) -> getn_eof(device, '', [line | acc])
:eof -> wrap_eof(:lists.reverse(acc))
other -> other
end
end
defp wrap_eof([h | _] = acc) when is_binary(h), do: IO.iodata_to_binary(acc)
defp wrap_eof([h | _] = acc) when is_list(h), do: :lists.flatten(acc)
defp wrap_eof([]), do: :eof
@doc ~S"""
Reads a line from the IO `device`.
@@ -561,17 +508,6 @@ defmodule IO do
:io.get_line(map_dev(device), to_chardata(prompt))
end
@doc """
Returns a line-based `IO.Stream` on `:stdio`.
This is equivalent to:
IO.stream(:stdio, :line)
"""
@doc since: "1.12.0"
def stream, do: stream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`.
@@ -588,9 +524,6 @@ 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,
while `stream/2` has been available since v1.0.0.
## Examples
Here is an example on how we mimic an echo server
@@ -600,23 +533,12 @@ defmodule IO do
"""
@spec stream(device, :line | pos_integer) :: Enumerable.t()
def stream(device \\ :stdio, line_or_codepoints)
def stream(device, line_or_codepoints)
when line_or_codepoints == :line
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
end
@doc """
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
This is equivalent to:
IO.binstream(:stdio, :line)
"""
@doc since: "1.12.0"
def binstream, do: binstream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
@@ -625,7 +547,8 @@ defmodule IO do
and write.
The `device` is iterated by the given number of bytes or line by line if
`:line` is given. This reads from the IO device as a raw binary.
`:line` is given.
This reads from the IO device as a raw binary.
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
@@ -633,11 +556,9 @@ 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,
while `binstream/2` has been available since v1.0.0.
"""
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
def binstream(device \\ :stdio, line_or_bytes)
def binstream(device, line_or_bytes)
when line_or_bytes == :line
when is_integer(line_or_bytes) and line_or_bytes > 0 do
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
@@ -665,8 +586,6 @@ defmodule IO do
"""
@spec chardata_to_string(chardata) :: String.t()
def chardata_to_string(chardata)
def chardata_to_string(string) when is_binary(string) do
string
end
+5 -36
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"
@@ -271,7 +242,7 @@ defmodule IO.ANSI do
performed. If you don't want this behaviour, use `format_fragment/2`.
An optional boolean parameter can be passed to enable or disable
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
By default checks if ANSI is enabled using the `enabled?/0` function.
## Examples
@@ -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
+22 -31
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)
@@ -196,10 +194,6 @@ defmodule IO.ANSI.Docs do
inline_text("*", traverse_erlang_html(entries, indent, options), options)
end
defp traverse_erlang_html({tag, _, entries}, indent, options) when tag in [:strong, :b] do
inline_text("**", traverse_erlang_html(entries, indent, options), options)
end
defp traverse_erlang_html({:code, _, entries}, indent, options) do
inline_text("`", traverse_erlang_html(entries, indent, options), options)
end
@@ -301,7 +295,7 @@ defmodule IO.ANSI.Docs do
end
defp inline_html?(binary) when is_binary(binary), do: true
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :strong, :b, :br], do: true
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :br], do: true
defp inline_html?(_), do: false
## Markdown
@@ -804,14 +798,14 @@ defmodule IO.ANSI.Docs do
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
end
# We have four entries: **, __, *, _ and `.
# We have four entries: **, *, _ and `.
#
# The first four behave the same while the last one is simpler
# The first three behave the same while the last one is simpler
# when it comes to delimiters as it ignores spaces and escape
# characters. But, since the first two has two characters,
# we need to handle 3 cases:
# characters. But, since the first has two characters, we need to
# handle 3 cases:
#
# 1. __ and **
# 1. **
# 2. _ and *
# 3. `
#
@@ -825,8 +819,8 @@ defmodule IO.ANSI.Docs do
### Inline start
defp handle_inline(<<mark, mark, rest::binary>>, options) when mark in @single do
handle_inline(rest, [mark | mark], [<<mark, mark>>], [], options)
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
handle_inline(rest, ?d, ["**"], [], options)
end
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
@@ -839,10 +833,9 @@ defmodule IO.ANSI.Docs do
### Inline delimiters
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
when rest != "" and delimiter in @delimiters and mark in @single do
acc = [delimiter, Enum.reverse(buffer) | acc]
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
when rest != "" and delimiter in @delimiters do
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer) | acc], options)
end
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
@@ -857,10 +850,9 @@ defmodule IO.ANSI.Docs do
### Clauses for handling escape
defp handle_inline(<<?\\, ?\\, mark, mark, rest::binary>>, nil, buffer, acc, options)
when rest != "" and mark in @single do
acc = [?\\, Enum.reverse(buffer) | acc]
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
when rest != "" do
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer) | acc], options)
end
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
@@ -879,8 +871,8 @@ defmodule IO.ANSI.Docs do
### Inline end
defp handle_inline(<<mark, mark, delimiter, rest::binary>>, [mark | mark], buffer, acc, options)
when delimiter in @delimiters and mark in @single do
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
when delimiter in @delimiters do
inline_buffer = inline_buffer(buffer, options)
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
end
@@ -891,8 +883,8 @@ defmodule IO.ANSI.Docs do
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
end
defp handle_inline(<<mark, mark, rest::binary>>, [mark | mark], buffer, acc, options)
when rest == "" and mark in @single do
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
when rest == "" do
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
end
@@ -938,11 +930,10 @@ defmodule IO.ANSI.Docs do
defp color_for(mark, colors) do
case mark do
"__" -> color(:doc_bold, colors)
"**" -> color(:doc_bold, colors)
"_" -> color(:doc_underline, colors)
"*" -> color(:doc_underline, colors)
"`" -> color(:doc_inline_code, colors)
"_" -> color(:doc_underline, colors)
"*" -> color(:doc_bold, colors)
"**" -> color(:doc_bold, colors)
end
end
+1 -5
View File
@@ -26,11 +26,7 @@ defmodule IO.Stream do
defstruct device: nil, raw: true, line_or_bytes: :line
@type t :: %__MODULE__{
device: IO.device(),
raw: boolean(),
line_or_bytes: :line | non_neg_integer()
}
@type t :: %__MODULE__{}
@doc false
def __build__(device, raw, line_or_bytes) do
+448 -1194
View File
File diff suppressed because it is too large Load Diff
+20 -41
View File
@@ -13,12 +13,9 @@ defmodule Kernel.CLI do
pa: [],
pz: [],
verbose_compile: false,
profile: nil,
pry: false
profile: nil
}
@standalone_opts ["-h", "--help", "--short-version"]
@doc """
This is the API invoked by Elixir boot process.
"""
@@ -29,10 +26,6 @@ defmodule Kernel.CLI do
System.argv(argv)
System.no_halt(config.no_halt)
if config.pry do
Application.put_env(:elixir, :dbg_callback, {IEx.Pry, :dbg, []})
end
fun = fn _ ->
errors = process_commands(config)
@@ -95,7 +88,7 @@ defmodule Kernel.CLI do
case blamed do
%FunctionClauseError{} ->
formatted = Exception.format_banner(kind, reason, stacktrace)
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/2))
[formatted, padded_blame]
_ ->
@@ -183,8 +176,9 @@ defmodule Kernel.CLI do
IO.write(:stderr, format_error(kind, reason, stacktrace))
end
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
defp blame_match(%{match?: true, node: node}, _), do: blame_ansi(:normal, "+", node)
defp blame_match(%{match?: false, node: node}, _), do: blame_ansi(:red, "-", node)
defp blame_match(_, string), do: string
defp blame_ansi(color, no_ansi, node) do
if IO.ANSI.enabled?() do
@@ -201,9 +195,8 @@ defmodule Kernel.CLI do
end
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
[Kernel.ErrorHandler, Module.ParallelChecker]
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_pass, Kernel.ErrorHandler]
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
prune_stacktrace(t)
@@ -223,16 +216,7 @@ defmodule Kernel.CLI do
# Parse shared options
defp halt_standalone(opt) do
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
System.halt(1)
end
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
halt_standalone(opt)
end
defp parse_shared([opt | t], _config) when opt in ["-v", "--version"] do
defp parse_shared([opt | _t], _config) when opt in ["-v", "--version"] do
if function_exported?(IEx, :started?, 0) and IEx.started?() do
IO.puts("IEx " <> System.build_info()[:build])
else
@@ -240,11 +224,7 @@ defmodule Kernel.CLI do
IO.puts("Elixir " <> System.build_info()[:build])
end
if t != [] do
halt_standalone(opt)
else
System.halt(0)
end
System.halt(0)
end
defp parse_shared(["-pa", h | t], config) do
@@ -280,11 +260,6 @@ defmodule Kernel.CLI do
parse_shared(t, %{config | commands: [{:rpc_eval, node, h} | config.commands]})
end
defp parse_shared(["--rpc-eval" | _], config) do
new_config = %{config | errors: ["--rpc-eval : wrong number of arguments" | config.errors]}
{[], new_config}
end
defp parse_shared(["-r", h | t], config) do
parse_shared(t, %{config | commands: [{:require, h} | config.commands]})
end
@@ -324,7 +299,7 @@ defmodule Kernel.CLI do
end
defp parse_argv(["+iex" | t], config) do
parse_iex(t, %{config | pry: true})
parse_iex(t, config)
end
defp parse_argv(["-S", h | t], config) do
@@ -409,16 +384,20 @@ defmodule Kernel.CLI do
{config, t}
end
# This clause is here so that Kernel.CLI does not
# error out with "unknown option"
defp parse_iex(["--dot-iex", _ | t], config) do
parse_iex(t, config)
end
defp parse_iex([opt, _ | t], config) when opt in ["--remsh"] do
parse_iex(t, config)
end
defp parse_iex(["-S", h | t], config) do
{%{config | commands: [{:script, h} | config.commands]}, t}
end
# These clauses are here so that Kernel.CLI does not error out with "unknown option"
defp parse_iex(["--dot-iex", _ | t], config), do: parse_iex(t, config)
defp parse_iex(["--remsh", _ | t], config), do: parse_iex(t, config)
defp parse_iex(["--no-pry" | t], config), do: parse_iex(t, %{config | pry: false})
defp parse_iex([h | t] = list, config) do
case h do
"-" <> _ -> shared_option?(list, config, &parse_iex(&1, &2))
+2 -2
View File
@@ -30,10 +30,10 @@ defmodule Kernel.ErrorHandler do
end
def ensure_compiled(module, kind, deadlock) do
{compiler_pid, file_pid} = :erlang.get(:elixir_compiler_info)
parent = :erlang.get(:elixir_compiler_pid)
ref = :erlang.make_ref()
modules = :elixir_module.compiler_modules()
send(compiler_pid, {:waiting, kind, self(), ref, file_pid, module, modules, deadlock})
send(parent, {:waiting, kind, self(), ref, module, modules, deadlock})
:erlang.garbage_collect(self())
receive do
+55 -68
View File
@@ -30,8 +30,8 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def add_export(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_export, module})
def add_require(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_require, module})
end
@doc false
@@ -59,11 +59,6 @@ defmodule Kernel.LexicalTracker do
:gen_server.cast(pid, {:alias_dispatch, module})
end
@doc false
def import_quoted(pid, module, function, arities) when is_atom(module) do
:gen_server.cast(pid, {:import_quoted, module, function, arities})
end
@doc false
def add_compile_env(pid, app, path, return) do
:gen_server.cast(pid, {:compile_env, app, path, return})
@@ -93,24 +88,23 @@ defmodule Kernel.LexicalTracker do
@doc false
def collect_unused_imports(pid) do
unused(pid, :unused_imports)
unused(pid, :import)
end
@doc false
def collect_unused_aliases(pid) do
unused(pid, :unused_aliases)
unused(pid, :alias)
end
defp unused(pid, tag) do
:gen_server.call(pid, tag, @timeout)
:gen_server.call(pid, {:unused, tag}, @timeout)
end
# Callbacks
def init(:ok) do
state = %{
aliases: %{},
imports: %{},
directives: %{},
references: %{},
exports: %{},
cache: %{},
@@ -122,12 +116,13 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def handle_call(:unused_aliases, _from, state) do
{:reply, Enum.sort(state.aliases), state}
end
def handle_call({:unused, tag}, _from, state) do
directives =
for {{^tag, module_or_mfa}, marker} <- state.directives, is_integer(marker) do
{module_or_mfa, marker}
end
def handle_call(:unused_imports, _from, state) do
{:reply, Enum.sort(state.imports), state}
{:reply, Enum.sort(directives), state}
end
def handle_call(:references, _from, state) do
@@ -153,44 +148,12 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
%{imports: imports, references: references} = state
imports =
case imports do
%{^module => modules_and_fas} ->
modules_and_fas
|> Map.delete(module)
|> Map.delete({function, arity})
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
references = add_reference(references, module, mode)
{:noreply, %{state | imports: imports, references: references}}
state = add_import_dispatch(state, module, function, arity, mode)
{:noreply, state}
end
def handle_cast({:alias_dispatch, module}, %{aliases: aliases} = state) do
{:noreply, %{state | aliases: Map.delete(aliases, module)}}
end
def handle_cast({:import_quoted, module, function, arities}, state) do
%{imports: imports} = state
imports =
case imports do
%{^module => modules_and_fas} ->
arities
|> Enum.reduce(modules_and_fas, &Map.delete(&2, {function, &1}))
|> Map.delete(module)
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
{:noreply, %{state | imports: imports}}
def handle_cast({:alias_dispatch, module}, state) do
{:noreply, %{state | directives: add_dispatch(state.directives, module, :alias)}}
end
def handle_cast({:set_file, file}, state) do
@@ -205,25 +168,27 @@ defmodule Kernel.LexicalTracker do
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
end
def handle_cast({:add_export, module}, state) do
def handle_cast({:add_require, module}, state) do
{:noreply, put_in(state.exports[module], true)}
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
if warn do
imports = for module_or_fa <- [module | fas], do: {module_or_fa, line}, into: %{}
{:noreply, put_in(state.imports[module], imports)}
else
{:noreply, state}
end
directives =
state.directives
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|> Map.new()
|> add_directive(module, line, warn, :import)
directives =
Enum.reduce(fas, directives, fn {function, arity}, directives ->
add_directive(directives, {module, function, arity}, line, warn, :import)
end)
{:noreply, %{state | directives: directives}}
end
def handle_cast({:add_alias, module, line, warn}, state) do
if warn do
{:noreply, put_in(state.aliases[module], line)}
else
{:noreply, state}
end
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
end
@doc false
@@ -255,9 +220,31 @@ defmodule Kernel.LexicalTracker do
do: Map.put(references, module, :compile)
defp add_reference(references, module, :runtime) when is_atom(module) do
case references do
%{^module => _} -> references
%{} -> Map.put(references, module, :runtime)
case Map.fetch(references, module) do
{:ok, _} -> references
:error -> Map.put(references, module, :runtime)
end
end
defp add_import_dispatch(state, module, function, arity, mode) do
directives =
state.directives
|> add_dispatch(module, :import)
|> add_dispatch({module, function, arity}, :import)
references = add_reference(state.references, module, mode)
%{state | directives: directives, references: references}
end
# In the map we keep imports and aliases.
# If the value is a line, it was imported/aliased and has a pending warning
# If the value is true, it was imported/aliased and used
defp add_directive(directives, module_or_mfa, line, warn, tag) do
marker = if warn, do: line, else: true
Map.put(directives, {tag, module_or_mfa}, marker)
end
defp add_dispatch(directives, module_or_mfa, tag) do
Map.put(directives, {tag, module_or_mfa}, true)
end
end
+105 -167
View File
@@ -3,12 +3,6 @@ defmodule Kernel.ParallelCompiler do
A module responsible for compiling and requiring files in parallel.
"""
@typedoc "The line. 0 indicates no line."
@type line() :: non_neg_integer()
@type location() :: line() | {pos_integer(), column :: non_neg_integer}
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
@type error() :: {file :: Path.t(), location(), message :: String.t()}
@doc """
Starts a task for parallel compilation.
@@ -22,28 +16,23 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
def async(fun) when is_function(fun, 0) do
case :erlang.get(:elixir_compiler_info) do
{compiler, _} ->
file = :erlang.get(:elixir_compiler_file)
dest = :erlang.get(:elixir_compiler_dest)
if parent = :erlang.get(:elixir_compiler_pid) do
file = :erlang.get(:elixir_compiler_file)
dest = :erlang.get(:elixir_compiler_dest)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
{_parent, checker} = Module.ParallelChecker.get()
Task.async(fn ->
send(compiler, {:async, self()})
Module.ParallelChecker.put(compiler, checker)
:erlang.put(:elixir_compiler_info, {compiler, self()})
:erlang.put(:elixir_compiler_file, file)
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
:erlang.process_flag(:error_handler, error_handler)
fun.()
end)
:undefined ->
raise ArgumentError,
"cannot spawn parallel compiler task because " <>
"the current file is not being compiled/required"
Task.async(fn ->
send(parent, {:async, self()})
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_compiler_file, file)
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
:erlang.process_flag(:error_handler, error_handler)
fun.()
end)
else
raise ArgumentError,
"cannot spawn parallel compiler task because " <>
"the current file is not being compiled/required"
end
end
@@ -97,19 +86,11 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
@spec compile([Path.t()], keyword()) :: {:ok, [atom], [warning]} | {:error, [error], [warning]}
def compile(files, options \\ []) when is_list(options) do
spawn_workers(files, :compile, options)
end
@doc """
Compiles the given files and writes resulting BEAM files into path.
See `compile/2` for more information.
"""
@doc since: "1.6.0"
@spec compile_to_path([Path.t()], Path.t(), keyword()) ::
{:ok, [atom], [warning]} | {:error, [error], [warning]}
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
spawn_workers(files, {:compile, path}, options)
end
@@ -135,21 +116,10 @@ defmodule Kernel.ParallelCompiler do
"""
@doc since: "1.6.0"
@spec require([Path.t()], keyword()) ::
{:ok, [atom], [warning]} | {:error, [error], [warning]}
def require(files, options \\ []) when is_list(options) do
spawn_workers(files, :require, options)
end
@doc """
Prints a warning returned by the compiler.
"""
@doc since: "1.13.0"
@spec print_warning(warning) :: :ok
def print_warning({file, location, warning}) do
:elixir_errors.print_warning(location, file, warning)
end
@doc false
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
def files(files, options \\ []) when is_list(options) do
@@ -171,32 +141,7 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers(files, output, options) do
{:module, _} = :code.ensure_loaded(Kernel.ErrorHandler)
schedulers = max(:erlang.system_info(:schedulers_online), 2)
{:ok, checker} = Module.ParallelChecker.start_link(schedulers)
try do
outcome = spawn_workers(schedulers, checker, files, output, options)
{outcome, Code.get_compiler_option(:warnings_as_errors)}
else
{{:ok, _, [_ | _] = warnings}, true} ->
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
{:error, warnings, []}
{{:ok, outcome, warnings}, _} ->
beam_timestamp = Keyword.get(options, :beam_timestamp)
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
{{:error, errors, warnings}, true} ->
{:error, errors ++ warnings, []}
{{:error, errors, warnings}, _} ->
{:error, errors, warnings}
after
Module.ParallelChecker.stop(checker)
end
end
defp spawn_workers(schedulers, checker, files, output, options) do
beam_timestamp = Keyword.get(options, :beam_timestamp)
threshold = Keyword.get(options, :long_compilation_threshold, 10) * 1000
timer_ref = Process.send_after(self(), :threshold_check, threshold)
@@ -211,8 +156,7 @@ defmodule Kernel.ParallelCompiler do
output: output,
timer_ref: timer_ref,
long_compilation_threshold: threshold,
schedulers: schedulers,
checker: checker
schedulers: schedulers
})
Process.cancel_timer(state.timer_ref)
@@ -223,7 +167,21 @@ defmodule Kernel.ParallelCompiler do
0 -> :ok
end
outcome
case {outcome, Code.get_compiler_option(:warnings_as_errors)} do
{{:ok, _, [_ | _] = warnings}, true} ->
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
{:error, warnings, []}
{{:ok, outcome, warnings}, _} ->
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
{{:error, errors, warnings}, true} ->
{:error, errors ++ warnings, []}
{{:error, errors, warnings}, _} ->
{:error, errors, warnings}
end
end
defp each_file(fun) when is_function(fun, 1), do: fn file, _ -> fun.(file) end
@@ -241,7 +199,7 @@ defmodule Kernel.ParallelCompiler do
defp write_module_binaries(result, {:compile, path}, timestamp) do
Enum.flat_map(result, fn
{{:module, module}, binary} ->
{{:module, module}, {binary, _map}} ->
full_path = Path.join(path, Atom.to_string(module) <> ".beam")
File.write!(full_path, binary)
if timestamp, do: File.touch!(full_path, timestamp)
@@ -265,15 +223,32 @@ defmodule Kernel.ParallelCompiler do
end
defp maybe_check_modules(result, runtime_modules, state) do
%{profile: profile, checker: checker} = state
%{schedulers: schedulers, profile: profile} = state
compiled_modules =
for {{:module, module}, _} <- result,
do: module
if :elixir_config.get(:bootstrap) do
[]
else
compiled_modules = checker_compiled_modules(result)
runtime_modules = checker_runtime_modules(runtime_modules)
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(checker, runtime_modules)
end)
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(compiled_modules, runtime_modules, schedulers)
end)
end
end
defp checker_compiled_modules(result) do
for {{:module, _module}, {binary, module_map}} <- result do
{module_map, binary}
end
end
defp checker_runtime_modules(modules) do
for module <- modules,
path = :code.which(module),
is_list(path) and path != [] do
{module, File.read!(path)}
end
end
defp profile_init(:time), do: {:time, System.monotonic_time(), 0}
@@ -311,9 +286,9 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers([{ref, found} | t], spawned, waiting, files, result, warnings, state) do
{files, waiting} =
case List.keytake(waiting, ref, 2) do
{{_kind, pid, ^ref, file_pid, _on, _defining, _deadlock}, waiting} ->
{{_kind, pid, ^ref, _on, _defining, _deadlock}, waiting} ->
send(pid, {ref, found})
{update_timing(files, file_pid, :waiting), waiting}
{update_timing(files, pid, :waiting), waiting}
nil ->
# In case the waiting process died (for example, it was an async process),
@@ -325,14 +300,13 @@ defmodule Kernel.ParallelCompiler do
end
defp spawn_workers([file | queue], spawned, waiting, files, result, warnings, state) do
%{output: output, dest: dest, checker: checker} = state
%{output: output, dest: dest} = state
parent = self()
file = Path.expand(file)
{pid, ref} =
:erlang.spawn_monitor(fn ->
Module.ParallelChecker.put(parent, checker)
:erlang.put(:elixir_compiler_info, {parent, self()})
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_compiler_file, file)
try do
@@ -386,7 +360,7 @@ defmodule Kernel.ParallelCompiler do
defp spawn_workers(
[],
1,
[{_, pid, ref, _, _, _, _}] = waiting,
[{_, pid, ref, _, _, _}] = waiting,
[%{pid: pid}] = files,
result,
warnings,
@@ -401,31 +375,24 @@ defmodule Kernel.ParallelCompiler do
# There is potentially a deadlock. We will release modules with
# the following order:
#
# 1. Code.ensure_compiled/1 checks without a known definition (deadlock = soft)
# 2. Code.ensure_compiled/1 checks with a known definition (deadlock = soft)
# 3. Struct/import/require/ensure_compiled! checks without a known definition (deadlock = hard)
# 4. Modules without a known definition
# 5. Code invocation (deadlock = raise)
# 1. Code.ensure_compiled/1 checks (deadlock = soft)
# 2. Struct checks (deadlock = hard)
# 3. Modules without a known definition
# 4. Code invocation (deadlock = raise)
#
# The reason for step 3 and 4 is to not treat typos as deadlocks and
# help developers handle those sooner. However, this can have false
# positives in case multiple modules are defined in the same file
# and the module we are waiting for is defined later on.
#
# Finally, note there is no difference between hard and raise, the
# In theory there is no difference between hard and raise, the
# difference is where the raise is happening, inside the compiler
# or in the caller.
cond do
deadlocked = deadlocked(waiting, :soft) || deadlocked(waiting, :hard) ->
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
deadlocked =
deadlocked(waiting, :soft, false) ||
deadlocked(waiting, :soft, true) || deadlocked(waiting, :hard, false) ||
without_definition(waiting, files)
without_definition = without_definition(waiting, files) ->
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
if deadlocked do
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
else
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
true ->
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
end
end
@@ -473,45 +440,29 @@ defmodule Kernel.ParallelCompiler do
Enum.count(result, &match?({{:module, _}, _}, &1))
end
# TODO: Deprecate other returns on v1.14
defp each_cycle_return({kind, modules, warnings}), do: {kind, modules, warnings}
defp each_cycle_return(other) do
IO.warn(
"the :each_cycle callback must return a tuple of format {:compile | :runtime, modules, warnings}"
)
case other do
{kind, modules} -> {kind, modules, []}
modules when is_list(modules) -> {:compile, modules, []}
end
end
defp each_cycle_return({kind, modules}), do: {kind, modules, []}
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules, []}
# The goal of this function is to find leaves in the dependency graph,
# i.e. to find code that depends on code that we know is not being defined.
# Note that not all files have been compiled yet, so they may not be in waiting.
# Note that not all files have been compile yet, so they may not be in waiting.
defp without_definition(waiting, files) do
nilify_empty(
nillify_empty(
for %{pid: pid} <- files,
{_, _, ref, ^pid, on, _, _} <- waiting,
not defining?(on, waiting),
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
do: {ref, :not_found}
)
end
defp deadlocked(waiting, type, defining?) do
nilify_empty(
for {_, _, ref, _, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
)
defp deadlocked(waiting, type) do
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
end
defp defining?(on, waiting) 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
@@ -524,41 +475,41 @@ defmodule Kernel.ParallelCompiler do
{:available, kind, module} ->
available =
for {^kind, _, ref, _, ^module, _defining, _deadlock} <- waiting,
for {^kind, _, ref, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {kind, module}, true)
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
{:module_available, child, ref, file, module, binary} ->
{:module_available, child, ref, file, module, binary, module_map} ->
state.each_module.(file, module, binary)
# Release the module loader which is waiting for an ack
send(child, {ref, :ack})
available =
for {:module, _, ref, _, ^module, _defining, _deadlock} <- waiting,
for {:module, _, ref, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {:module, module}, binary)
result = Map.put(result, {:module, module}, {binary, module_map})
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
# If we are simply requiring files, we do not add to waiting.
{:waiting, _kind, child, ref, _file_pid, _on, _defining, _deadlock} when output == :require ->
{:waiting, _kind, child, ref, _on, _defining, _deadlock} when output == :require ->
send(child, {ref, :not_found})
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
{:waiting, kind, child_pid, ref, file_pid, on, defining, deadlock?} ->
{:waiting, kind, child, ref, on, defining, deadlock?} ->
# If we already got what we were waiting for, do not put it on waiting.
# If we're waiting on ourselves, send :found so that we can crash with
# a better error.
{files, waiting} =
if Map.has_key?(result, {kind, on}) or on in defining do
send(child_pid, {ref, :found})
send(child, {ref, :found})
{files, waiting}
else
files = update_timing(files, file_pid, :compiling)
{files, [{kind, child_pid, ref, file_pid, on, defining, deadlock?} | waiting]}
files = update_timing(files, child, :compiling)
{files, [{kind, child, ref, on, defining, deadlock?} | waiting]}
end
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
@@ -579,10 +530,10 @@ defmodule Kernel.ParallelCompiler do
state = %{state | timer_ref: timer_ref}
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
{:warning, file, location, message} ->
{:warning, file, line, message} ->
file = file && Path.absname(file)
message = :unicode.characters_to_binary(message)
warning = {file, location, message}
warning = {file, line, message}
wait_for_messages(queue, spawned, waiting, files, result, [warning | warnings], state)
{:file_ok, child_pid, ref, file, lexical} ->
@@ -652,13 +603,13 @@ defmodule Kernel.ParallelCompiler do
data = maybe_warn_long_compilation(data, state)
if state.profile != :none do
compiling = to_padded_ms(data.compiling)
waiting = to_padded_ms(data.waiting)
relative = Path.relative_to_cwd(data.file)
compiling = System.convert_time_unit(data.compiling, :native, :millisecond)
waiting = System.convert_time_unit(data.waiting, :native, :millisecond)
extra = if waiting > 0, do: " (plus #{waiting}ms waiting)", else: ""
IO.puts(
:stderr,
"[profile] #{compiling}ms compiling + #{waiting}ms waiting for #{relative}"
"[profile] #{Path.relative_to_cwd(data.file)} compiled in #{compiling}ms" <> extra
)
end
@@ -669,13 +620,6 @@ defmodule Kernel.ParallelCompiler do
end)
end
defp to_padded_ms(time) do
time
|> System.convert_time_unit(:native, :millisecond)
|> Integer.to_string()
|> String.pad_leading(6, " ")
end
defp discard_down(pid) do
receive do
{:DOWN, _, :process, ^pid, _} -> :ok
@@ -704,7 +648,7 @@ defmodule Kernel.ParallelCompiler do
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
Process.exit(pid, :kill)
{kind, ^pid, _, _, on, _, _} = List.keyfind(waiting, pid, 1)
{kind, ^pid, _, on, _, _} = List.keyfind(waiting, pid, 1)
description = "deadlocked waiting on #{kind} #{inspect(on)}"
error = CompileError.exception(description: description, file: nil, line: nil)
print_error(file, :error, error, stacktrace)
@@ -751,12 +695,7 @@ defmodule Kernel.ParallelCompiler do
line = get_line(file, reason, stack)
file = Path.absname(file)
message = :unicode.characters_to_binary(Kernel.CLI.format_error(kind, reason, stack))
{file, line || 0, message}
end
defp get_line(_file, %{line: line, column: column}, _stack)
when is_integer(line) and line > 0 and is_integer(column) and column >= 0 do
{line, column}
{file, line, message}
end
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
@@ -769,8 +708,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
+33 -162
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>>
@@ -201,7 +201,7 @@ defmodule Kernel.SpecialForms do
iex> <<"foo"::utf32>>
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
Otherwise we get an `ArgumentError` when constructing the binary:
Otherwise we get an `ArgumentError` when construcing the binary:
rest = "oo"
<<102, rest>>
@@ -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
@@ -365,8 +365,8 @@ defmodule Kernel.SpecialForms do
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
To learn more about specific optimizations and performance considerations,
check out the
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
check out
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
"""
defmacro unquote(:<<>>)(args), do: error!([args])
@@ -604,12 +604,11 @@ defmodule Kernel.SpecialForms do
import List
A developer can filter to import only functions, macros, or sigils
(which can be functions or macros) via the `:only` option:
A developer can filter to import only macros or functions via
the only option:
import List, only: :functions
import List, only: :macros
import Kernel, only: :sigils
Alternatively, Elixir allows a developer to pass pairs of
name/arities to `:only` or `:except` as a fine grained control
@@ -618,11 +617,9 @@ defmodule Kernel.SpecialForms do
import List, only: [flatten: 1]
import String, except: [split: 2]
Importing the same module again will erase the previous imports,
except when the `except` option is used, which is always exclusive
on a previously declared `import/2`. If there is no previous import,
then it applies to all functions and macros in the module. For
example:
Note that calling `except` is always exclusive on a previously
declared `import/2`. If there is no previous import, then it applies
to all functions and macros in the module. For example:
import List, only: [flatten: 1, keyfind: 4]
import List, except: [flatten: 1]
@@ -779,7 +776,7 @@ defmodule Kernel.SpecialForms do
<<int::integer-little, rest::bits>> = bits
Read the documentation on the [Typespecs page](typespecs.md) and
Read the documentation on the `Typespec` page and
`<<>>/1` for more information on typespecs and
bitstrings respectively.
"""
@@ -1212,12 +1209,6 @@ defmodule Kernel.SpecialForms do
reported to where `defadd` was invoked. `location: :keep` affects
only definitions inside the quote.
> **Important:** do not use location: :keep if the function definition
> also `unquote`s some of the macro arguments. If you do so, Elixir
> will store the file definition of the current location but the
> unquoted arguments may contain line information of the macro caller,
> leading to erroneous stacktraces.
## Binding and unquote fragments
Elixir quote/unquote mechanisms provide a functionality called
@@ -1316,13 +1307,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 +1378,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 +1398,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
@@ -1458,7 +1414,7 @@ defmodule Kernel.SpecialForms do
The `IO` module provides streams, that are both `Enumerable` and
`Collectable`, here is an upcase echo server using comprehensions:
for line <- IO.stream(), into: IO.stream() do
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
String.upcase(line)
end
@@ -1561,15 +1517,15 @@ defmodule Kernel.SpecialForms do
iex> width
nil
The behaviour of any expression in a clause is the same as if it was
written outside of `with`. For example, `=` will raise a `MatchError`
instead of returning the non-matched value:
The behaviour of any expression in a clause is the same as outside.
For example, `=` will raise a `MatchError` instead of returning the
non-matched value:
with :foo = :bar, do: :ok
** (MatchError) no match of right hand side value: :bar
As with any other function or macro call in Elixir, explicit parens can
also be used around the arguments before the `do`-`end` block:
also be used around the arguments before the `do`/`end` block:
iex> opts = %{width: 10, height: 15}
iex> with(
@@ -1582,8 +1538,6 @@ defmodule Kernel.SpecialForms do
The choice between parens and no parens is a matter of preference.
## Else clauses
An `else` option can be given to modify what is being returned from
`with` in the case of a failed match:
@@ -1594,60 +1548,11 @@ defmodule Kernel.SpecialForms do
...> else
...> :error ->
...> {:error, :wrong_data}
...>
...> _other_error ->
...> :unexpected_error
...> end
{:error, :wrong_data}
The `else` block works like a `case` clause: it can have multiple clauses,
and the first match will be used. Variables bound inside `with` (such as
`width` in this example) are not available in the `else` block.
If an `else` block is used and there are no matching clauses, a `WithClauseError`
exception is raised.
### Beware!
Keep in mind that, one of potential drawback of `with` is that all
failure clauses are flattened into a single `else` block. For example,
take this code that checks if a given path points to an Elixir file
and that it exists before creating a backup copy:
with ".ex" <- Path.extname(path),
true <- File.exists?(path) do
backup_path = path <> ".backup"
File.cp!(path, backup_path)
{:ok, backup_path}
else
binary when is_binary(binary) ->
{:error, :invalid_extension}
false ->
{:error, :missing_file}
end
Note how we are having to reconstruct the result types of `Path.extname/1`
and `File.exists?/1` to build error messages. In this case, it is better
to change the with clauses to already return the desired format, like this:
with :ok <- validate_extension(path),
:ok <- validate_exists(path) do
backup_path = path <> ".backup"
File.cp!(path, backup_path)
{:ok, backup_path}
end
defp validate_extension(path) do
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
end
defp validate_exists(path) do
if File.exists?(path), do: :ok, else: {:error, :missing_file}
end
Note how the code above is better organized and clearer once we
make sure each clause in `with` returns a normalized format.
"""
defmacro with(args), do: error!([args])
@@ -1693,7 +1598,7 @@ defmodule Kernel.SpecialForms do
defmacro unquote(:__block__)(args), do: error!([args])
@doc """
Capture operator. Captures or creates an anonymous function.
Caputure operator. Captures or creates an anonymous function.
## Capture
@@ -1816,21 +1721,19 @@ defmodule Kernel.SpecialForms do
## Examples
case File.read(file) do
{:ok, contents} when is_binary(contents) ->
String.split(contents, "\n")
{:error, _reason} ->
Logger.warning "could not find #{file}, assuming empty..."
[]
case thing do
{:selector, i, value} when is_integer(i) ->
value
value ->
value
end
In the example above, we match the result of `File.read/1`
against each clause "head" and execute the clause "body"
corresponding to the first clause that matches.
In the example above, we match `thing` against each clause "head"
and execute the clause "body" corresponding to the first clause
that matches.
If no clause matches, an error is raised. For this reason,
it may be necessary to add a final catch-all clause (like `_`)
If no clause matches, an error is raised.
For this reason, it may be necessary to add a final catch-all clause (like `_`)
which will always match.
x = 10
@@ -1838,7 +1741,6 @@ defmodule Kernel.SpecialForms do
case x do
0 ->
"This clause won't match"
_ ->
"This clause would match any value (x = #{x})"
end
@@ -1856,7 +1758,8 @@ defmodule Kernel.SpecialForms do
value
#=> unbound variable value
Variables in the outer context cannot be overridden either:
When binding variables with the same names as variables in the outer context,
the variables in the outer context are not affected.
value = 7
@@ -1883,19 +1786,6 @@ defmodule Kernel.SpecialForms do
end
#=> "Will match"
## Using guards to match against multiple values
While it is not possible to match against multiple patterns in a single
clause, it's possible to match against multiple values by using guards:
case data do
value when value in [:one, :two] ->
"#{value} has been matched"
:three ->
"three has been matched"
end
"""
defmacro case(condition, clauses), do: error!([condition, clauses])
@@ -2093,25 +1983,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
+19 -48
View File
@@ -104,7 +104,7 @@ defmodule Kernel.Typespec do
@doc """
Defines a typespec.
Invoked by `@/1` expansion.
Invoked by `Kernel.@/1` expansion.
"""
def deftypespec(:spec, expr, _line, _file, module, pos) do
{_set, bag} = :elixir_module.data_tables(module)
@@ -194,14 +194,14 @@ defmodule Kernel.Typespec do
defp get_doc_info(set, attr, line) do
case :ets.take(set, attr) do
[{^attr, {line, doc}, _, _}] -> {line, doc}
[{^attr, {line, doc}, _}] -> {line, doc}
[] -> {line, nil}
end
end
defp get_doc_meta(spec_meta, doc_kind, set) do
case :ets.take(set, {doc_kind, :meta}) do
[{{^doc_kind, :meta}, metadata}] -> Map.merge(metadata, spec_meta)
[{{^doc_kind, :meta}, metadata, _}] -> Map.merge(metadata, spec_meta)
[] -> spec_meta
end
end
@@ -553,7 +553,9 @@ defmodule Kernel.Typespec do
end
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller, state) do
module = Macro.expand(name, %{caller | function: {:__info__, 1}})
# We cannot set a function name to avoid tracking
# as a compile time dependency, because for structs it actually is one.
module = Macro.expand(name, caller)
struct =
module
@@ -567,10 +569,7 @@ defmodule Kernel.Typespec do
types =
:lists.map(
fn
{:__exception__ = field, true} -> {field, Keyword.get(fields, field, true)}
{field, _} -> {field, Keyword.get(fields, field, quote(do: term()))}
end,
fn {field, _} -> {field, Keyword.get(fields, field, quote(do: term()))} end,
:lists.sort(struct)
)
@@ -578,7 +577,7 @@ defmodule Kernel.Typespec do
unless Keyword.has_key?(struct, field) do
compile_error(
caller,
"undefined field #{inspect(field)} on struct #{inspect(module)}"
"undefined field #{inspect(field)} on struct #{Macro.to_string(name)}"
)
end
end
@@ -644,7 +643,10 @@ defmodule Kernel.Typespec do
end
defp typespec({:__aliases__, _, _} = alias, vars, caller, state) do
typespec(expand_remote(alias, caller), vars, caller, state)
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
typespec(atom, vars, caller, state)
end
# Handle funs
@@ -681,20 +683,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
@@ -742,7 +734,9 @@ defmodule Kernel.Typespec do
# Handle remote calls
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller, state) do
remote = expand_remote(remote, caller)
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
cond do
not is_atom(remote) ->
@@ -913,25 +907,6 @@ defmodule Kernel.Typespec do
## Helpers
# This is a backport of Macro.expand/2 because we want to expand
# aliases but we don't them to become compile-time references.
defp expand_remote({:__aliases__, _, _} = alias, env) do
case :elixir_aliases.expand(alias, env) do
receiver when is_atom(receiver) ->
receiver
aliases ->
aliases = :lists.map(&Macro.expand_once(&1, env), aliases)
case :lists.all(&is_atom/1, aliases) do
true -> :elixir_aliases.concat(aliases)
false -> alias
end
end
end
defp expand_remote(other, env), do: Macro.expand(other, env)
defp compile_error(caller, desc) do
raise CompileError, file: caller.file, line: caller.line, description: desc
end
@@ -1033,11 +1008,7 @@ defmodule Kernel.Typespec do
:elixir_errors.erl_warn(caller.line, caller.file, warning)
{_, :used_once} ->
compile_error(
caller,
"type variable #{name} is used only once. Type variables in typespecs " <>
"must be referenced at least twice, otherwise it is equivalent to term()"
)
compile_error(caller, "type variable #{name} is unused")
_ ->
:ok
+17 -133
View File
@@ -22,52 +22,16 @@ defmodule Kernel.Utils do
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
@doc """
Callback for defdelegate entry point.
Callback for defdelegate.
"""
def defdelegate_all(funs, opts, env) do
to = Keyword.get(opts, :to) || raise ArgumentError, "expected to: to be given as argument"
as = Keyword.get(opts, :as)
if to == env.module and is_nil(as) do
raise ArgumentError,
"defdelegate function is calling itself, which will lead to an infinite loop. You should either change the value of the :to option or specify the :as option"
end
if is_list(funs) do
IO.warn(
"passing a list to Kernel.defdelegate/2 is deprecated, please define each delegate separately",
Macro.Env.stacktrace(env)
)
end
if Keyword.has_key?(opts, :append_first) do
IO.warn(
"Kernel.defdelegate/2 :append_first option is deprecated",
Macro.Env.stacktrace(env)
)
end
to
end
@doc """
Callback for each function in defdelegate.
"""
def defdelegate_each(fun, opts) when is_list(opts) do
def defdelegate(fun, opts) when is_list(opts) do
# TODO: Remove on v2.0
append_first? = Keyword.get(opts, :append_first, false)
{name, args} =
case fun do
{:when, _, [_left, right]} ->
raise ArgumentError,
"guards are not allowed in defdelegate/2, got: when #{Macro.to_string(right)}"
_ ->
case Macro.decompose_call(fun) do
{_, _} = pair -> pair
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
end
case Macro.decompose_call(fun) do
{_, _} = pair -> pair
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
end
as = Keyword.get(opts, :as, name)
@@ -100,15 +64,7 @@ defmodule Kernel.Utils do
@doc """
Callback for defstruct.
"""
def defstruct(module, fields, bootstrapped?) do
{set, bag} = :elixir_module.data_tables(module)
if :ets.member(set, :__struct__) do
raise ArgumentError,
"defstruct has already been called for " <>
"#{Kernel.inspect(module)}, defstruct can only be called once per module"
end
def defstruct(module, fields) do
case fields do
fs when is_list(fs) ->
:ok
@@ -120,7 +76,7 @@ defmodule Kernel.Utils do
mapper = fn
{key, val} when is_atom(key) ->
try do
:elixir_quote.escape(val, false, :none)
Macro.escape(val)
rescue
e in [ArgumentError] ->
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
@@ -136,20 +92,7 @@ defmodule Kernel.Utils do
end
fields = :lists.map(mapper, fields)
enforce_keys =
case :ets.lookup(set, :enforce_keys) do
[{_, enforce_keys, _, _}] when is_list(enforce_keys) ->
:ets.update_element(set, :enforce_keys, {3, :used})
enforce_keys
[{_, enforce_key, _, _}] ->
:ets.update_element(set, :enforce_keys, {3, :used})
[enforce_key]
[] ->
[]
end
enforce_keys = List.wrap(Module.get_attribute(module, :enforce_keys))
# TODO: Make it raise on v2.0
warn_on_duplicate_struct_key(:lists.keysort(1, fields))
@@ -163,68 +106,9 @@ 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}
error_keys ->
raise ArgumentError,
"@enforce_keys required keys (#{inspect(error_keys)}) that are not defined in defstruct: " <>
"#{inspect(fields)}"
end
{struct, enforce_keys, Module.get_attribute(module, :derive)}
end
defp warn_on_duplicate_struct_key([]) do
@@ -244,9 +128,9 @@ defmodule Kernel.Utils do
Announcing callback for defstruct.
"""
def announce_struct(module) do
case :erlang.get(:elixir_compiler_info) do
case :erlang.get(:elixir_compiler_pid) do
:undefined -> :ok
{pid, _} -> send(pid, {:available, :struct, module})
pid -> send(pid, {:available, :struct, module})
end
end
@@ -326,12 +210,12 @@ defmodule Kernel.Utils do
def defguard(args, expr, env) do
{^args, vars} = extract_refs_from_args(args)
env = :elixir_env.with_vars(%{env | context: :guard}, vars)
{expr, _, _} = :elixir_expand.expand(expr, :elixir_env.env_to_ex(env), env)
{expr, _scope} = :elixir_expand.expand(expr, env)
quote do
case Macro.Env.in_guard?(__CALLER__) do
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 +245,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) ->
@@ -374,8 +258,8 @@ defmodule Kernel.Utils do
{new_var, acc}
%{} ->
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
new_var = Macro.unique_var(generated, module)
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc)))
new_var = Macro.var(generated, Elixir)
{new_var, Map.put(acc, pair, {new_var, var})}
end
+142 -416
View File
@@ -13,43 +13,45 @@ defmodule Keyword do
[{:exit_on_close, true}, {:active, :once}, {:packet_size, 1024}]
Elixir provides a special and more concise syntax for keyword lists:
Elixir provides a special and more concise syntax for keyword lists
that looks like this:
[exit_on_close: true, active: :once, packet_size: 1024]
The two syntaxes return the exact same value.
A *key* can be any atom, consisting of Unicode letters, numbers,
an underscore or the `@` sign. If the *key* should have any other
characters, such as spaces, you can wrap it in quotes:
The two syntaxes are completely equivalent. Like atoms, keyword
lists keys must be composed of Unicode characters such as letters,
numbers, underscore, and `@`. If the keyword has a character that
does not belong to the category above, such as spaces, you can wrap
it in quotes:
iex> ["exit on close": true]
["exit on close": true]
Wrapping an atom in quotes does not make it a string. Keyword list
*keys* are always atoms. Quotes should only be used when necessary
or Elixir will issue a warning.
Wrapping a keyword in quotes does not make it a string. Keyword lists
keys are always atoms. If you use quotes around the key when quoting
is not necessary, Elixir will warn.
## Duplicate keys and ordering
A keyword may have duplicate keys so it is not strictly a key-value
data type. However most of the functions in this module work on a
key-value structure and behave similar to the functions you would
find in the `Map` module. For example, `Keyword.get/3` will get the first
entry matching the given key, regardless if duplicate entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicate
A keyword may have duplicated keys so it is not strictly a key-value
data type. However most of the functions in this module behave exactly
as a key-value so they work similarly to the functions you would find
in the `Map` module. For example, `Keyword.get/3` will get the first
entry matching the given key, regardless if duplicated entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicated
entries for a given key are removed when invoked. Note, however, that
keyword list operations need to traverse the whole list in order to find
keys, so these operations are slower than their map counterparts.
A handful of functions exist to handle duplicate keys, for example,
A handful of functions exist to handle duplicated keys, for example,
`get_values/2` returns all values for a given key and `delete_first/2`
deletes just the first entry of the existing ones.
deletes just one of the existing entries.
Even though lists preserve the existing order, the functions in
Even though lists preserve the user ordering, the functions in
`Keyword` do not guarantee any ordering. For example, if you invoke
`Keyword.put(opts, new_key, new_value)`, there is no guarantee for
where `new_key` will be added to (the front, the end or anywhere else).
`Keyword.put(opts, new_key, new_value)`, there is no guarantee to
where `new_key` will be added (to the front, to the end, or
anywhere else).
Given ordering is not guaranteed, it is not recommended to pattern
match on keyword lists either. For example, a function such as:
@@ -65,7 +67,7 @@ defmodule Keyword do
my_function([another_key: :bar, some_key: :foo])
Most of the functions in this module work in linear time. This means
that the time it takes to perform an operation grows at the same
that, the time it takes to perform an operation grows at the same
rate as the length of the list.
## Call syntax
@@ -81,8 +83,8 @@ defmodule Keyword do
String.split("1-0", "-", trim: true, parts: 2)
Since tuples, lists and maps are treated similarly to function
arguments in Elixir syntax, this property is also available to them:
Since tuples, lists, maps, and others are treated the same as function
calls in Elixir syntax, this property is also available to them:
iex> {1, 2, foo: :bar}
{1, 2, [{:foo, :bar}]}
@@ -92,7 +94,6 @@ defmodule Keyword do
iex> %{1 => 2, foo: :bar}
%{1 => 2, :foo => :bar}
"""
@compile :inline_list_funcs
@@ -104,24 +105,7 @@ defmodule Keyword do
@type t(value) :: [{key, value}]
@doc """
Builds a keyword from the given `keys` and the fixed `value`.
## Examples
iex> Keyword.from_keys([:foo, :bar, :baz], :atom)
[foo: :atom, bar: :atom, baz: :atom]
"""
@doc since: "1.14.0"
@spec from_keys([key], value) :: t(value)
def from_keys(keys, value) when is_list(keys) do
:lists.map(&{&1, value}, keys)
end
@doc """
Returns `true` if `term` is a keyword list, otherwise `false`.
When `term` is a list it is traversed to the end.
Returns `true` if `term` is a keyword list; otherwise returns `false`.
## Examples
@@ -161,7 +145,7 @@ defmodule Keyword do
@doc """
Creates a keyword list from an enumerable.
Removes duplicate entries and the last one prevails.
Duplicated entries are removed, the latest one prevails.
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
guarantees the keys are unique.
@@ -174,7 +158,7 @@ defmodule Keyword do
[a: 3]
"""
@spec new(Enumerable.t()) :: t
@spec new(Enum.t()) :: t
def new(pairs) do
new(pairs, fn pair -> pair end)
end
@@ -182,7 +166,7 @@ defmodule Keyword do
@doc """
Creates a keyword list from an enumerable via the transformation function.
Removes duplicate entries and the last one prevails.
Duplicated entries are removed, the latest one prevails.
Unlike `Enum.into(enumerable, [], fun)`,
`Keyword.new(enumerable, fun)` guarantees the keys are unique.
@@ -192,7 +176,7 @@ defmodule Keyword do
[a: :a, b: :b]
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: t
@spec new(Enum.t(), (term -> {key, value})) :: t
def new(pairs, transform) when is_function(transform, 1) do
fun = fn el, acc ->
{k, v} = transform.(el)
@@ -203,166 +187,12 @@ defmodule Keyword do
end
@doc """
Ensures the given `keyword` has only the keys given in `values`.
Gets the value for a specific `key`.
The second argument must be a list of atoms, specifying
a given key, or tuples specifying a key and a default value.
If `key` does not exist, return the default value
(`nil` if no default value).
If the keyword list has only the given keys, it returns
`{:ok, keyword}` with default values applied. Otherwise it
returns `{:error, invalid_keys}` with invalid keys.
See also: `validate!/2`.
## Examples
iex> {:ok, result} = Keyword.validate([], [one: 1, two: 2])
iex> Enum.sort(result)
[one: 1, two: 2]
iex> {:ok, result} = Keyword.validate([two: 3], [one: 1, two: 2])
iex> Enum.sort(result)
[one: 1, two: 3]
If atoms are given, they are supported as keys but do not
provide a default value:
iex> {:ok, result} = Keyword.validate([], [:one, two: 2])
iex> Enum.sort(result)
[two: 2]
iex> {:ok, result} = Keyword.validate([one: 1], [:one, two: 2])
iex> Enum.sort(result)
[one: 1, two: 2]
Passing unknown keys returns an error:
iex> Keyword.validate([three: 3, four: 4], [one: 1, two: 2])
{:error, [:four, :three]}
Passing the same key multiple times also errors:
iex> Keyword.validate([one: 1, two: 2, one: 1], [:one, :two])
{:error, [:one]}
"""
@doc since: "1.13.0"
@spec validate(keyword(), values :: [atom() | {atom(), term()}]) ::
{:ok, keyword()} | {:error, [atom]}
def validate(keyword, values) when is_list(keyword) and is_list(values) do
validate(keyword, values, [], [], [])
end
defp validate([{key, _} = pair | keyword], values1, values2, acc, bad_keys) when is_atom(key) do
case find_key!(key, values1, values2) do
{values1, values2} ->
validate(keyword, values1, values2, [pair | acc], bad_keys)
:error ->
case find_key!(key, values2, values1) do
{values1, values2} ->
validate(keyword, values1, values2, [pair | acc], bad_keys)
:error ->
validate(keyword, values1, values2, acc, [key | bad_keys])
end
end
end
defp validate([], values1, values2, acc, []) do
{:ok, move_pairs!(values1, move_pairs!(values2, acc))}
end
defp validate([], _values1, _values2, _acc, bad_keys) do
{:error, bad_keys}
end
defp validate([pair | _], _values1, _values2, _acc, []) do
raise ArgumentError,
"expected a keyword list as first argument, got invalid entry: #{inspect(pair)}"
end
defp find_key!(key, [key | rest], acc), do: {rest, acc}
defp find_key!(key, [{key, _} | rest], acc), do: {rest, acc}
defp find_key!(key, [head | tail], acc), do: find_key!(key, tail, [head | acc])
defp find_key!(_key, [], _acc), do: :error
defp move_pairs!([key | rest], acc) when is_atom(key),
do: move_pairs!(rest, acc)
defp move_pairs!([{key, _} = pair | rest], acc) when is_atom(key),
do: move_pairs!(rest, [pair | acc])
defp move_pairs!([], acc),
do: acc
defp move_pairs!([other | _], _) do
raise ArgumentError,
"expected the second argument to be a list of atoms or tuples, got: #{inspect(other)}"
end
@doc """
Similar to `validate/2` but returns the keyword or raises an error.
## Examples
iex> Keyword.validate!([], [one: 1, two: 2]) |> Enum.sort()
[one: 1, two: 2]
iex> Keyword.validate!([two: 3], [one: 1, two: 2]) |> Enum.sort()
[one: 1, two: 3]
If atoms are given, they are supported as keys but do not
provide a default value:
iex> Keyword.validate!([], [:one, two: 2]) |> Enum.sort()
[two: 2]
iex> Keyword.validate!([one: 1], [:one, two: 2]) |> Enum.sort()
[one: 1, two: 2]
Passing unknown keys raises an error:
iex> Keyword.validate!([three: 3], [one: 1, two: 2])
** (ArgumentError) unknown keys [:three] in [three: 3], the allowed keys are: [:one, :two]
Passing the same key multiple times also errors:
iex> Keyword.validate!([one: 1, two: 2, one: 1], [:one, :two])
** (ArgumentError) duplicate keys [:one] in [one: 1, two: 2, one: 1]
"""
@doc since: "1.13.0"
@spec validate!(keyword(), values :: [atom() | {atom(), term()}]) :: keyword()
def validate!(keyword, values) do
case validate(keyword, values) do
{:ok, kw} ->
kw
{:error, invalid_keys} ->
keys =
for value <- values,
do: if(is_atom(value), do: value, else: elem(value, 0))
message =
case Enum.split_with(invalid_keys, &(&1 in keys)) do
{_, [_ | _] = unknown} ->
"unknown keys #{inspect(unknown)} in #{inspect(keyword)}, " <>
"the allowed keys are: #{inspect(keys)}"
{[_ | _] = known, _} ->
"duplicate keys #{inspect(known)} in #{inspect(keyword)}"
end
raise ArgumentError, message
end
end
@doc """
Gets the value under the given `key`.
Returns the default value if `key` does not exist
(`nil` if no default value is provided).
If duplicate entries exist, it returns the first one.
If duplicated entries exist, the first one is returned.
Use `get_values/2` to retrieve all entries.
## Examples
@@ -376,7 +206,7 @@ defmodule Keyword do
iex> Keyword.get([a: 1], :b, 3)
3
With duplicate keys:
With duplicated keys:
iex> Keyword.get([a: 1, a: 2], :a, 3)
1
@@ -393,14 +223,14 @@ defmodule Keyword do
end
@doc """
Gets the value under the given `key`.
Gets the value for a specific `key`.
If `key` does not exist, lazily evaluates `fun` and returns its result.
This is useful if the default value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
If duplicate entries exist, it returns the first one.
If duplicated entries exist, the first one is returned.
Use `get_values/2` to retrieve all entries.
## Examples
@@ -428,14 +258,14 @@ defmodule Keyword do
@doc """
Gets the value from `key` and updates it, all in one pass.
The `fun` argument receives the value of `key` (or `nil` if `key`
This `fun` argument receives the value of `key` (or `nil` if `key`
is not present) and must return a two-element tuple: the current value
(the retrieved value, which can be operated on before being returned)
and the new value to be stored under `key`. The `fun` may also
return `:pop`, implying the current value shall be removed from the
keyword list and returned.
Returns a tuple that contains the current value returned by
The returned value is a tuple with the current value returned by
`fun` and a new keyword list with the updated value under `key`.
## Examples
@@ -450,11 +280,6 @@ defmodule Keyword do
...> end)
{nil, [b: "new value!", a: 1]}
iex> Keyword.get_and_update([a: 2], :a, fn number ->
...> {2 * number, 3 * number}
...> end)
{4, [a: 6]}
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
{1, []}
@@ -462,8 +287,8 @@ defmodule Keyword do
{nil, [a: 1]}
"""
@spec get_and_update(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
@spec get_and_update(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, value}
when current_value: value
def get_and_update(keywords, key, fun)
when is_list(keywords) and is_atom(key),
@@ -498,15 +323,15 @@ defmodule Keyword do
end
@doc """
Gets the value under `key` and updates it. Raises if there is no `key`.
Gets the value from `key` and updates it. Raises if there is no `key`.
The `fun` argument receives the value under `key` and must return a
This `fun` argument receives the value of `key` and must return a
two-element tuple: the current value (the retrieved value, which can be
operated on before being returned) and the new value to be stored under
`key`.
Returns a tuple that contains the current value returned by
`fun` and a new keyword list with the updated value under `key`.
The returned value is a tuple with the current value returned by `fun` and a new
keyword list with the updated value under `key`.
## Examples
@@ -526,8 +351,8 @@ defmodule Keyword do
{1, []}
"""
@spec get_and_update!(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
@spec get_and_update!(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, t}
when current_value: value
def get_and_update!(keywords, key, fun) do
get_and_update!(keywords, key, fun, [])
@@ -551,13 +376,13 @@ defmodule Keyword do
end
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
raise KeyError, key: key, term: acc
raise(KeyError, key: key, term: acc)
end
@doc """
Fetches the value for a specific `key` and returns it in a tuple.
If the `key` does not exist, it returns `:error`.
If the `key` does not exist, returns `:error`.
## Examples
@@ -578,7 +403,7 @@ defmodule Keyword do
@doc """
Fetches the value for specific `key`.
If the `key` does not exist, it raises a `KeyError`.
If `key` does not exist, a `KeyError` is raised.
## Examples
@@ -592,12 +417,12 @@ defmodule Keyword do
def fetch!(keywords, key) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, value} -> value
false -> raise KeyError, key: key, term: keywords
false -> raise(KeyError, key: key, term: keywords)
end
end
@doc """
Gets all values under a specific `key`.
Gets all values for a specific `key`.
## Examples
@@ -621,7 +446,7 @@ defmodule Keyword do
@doc """
Returns all keys from the keyword list.
Keeps duplicate keys in the resulting list of keys.
Duplicated keys appear duplicated in the final list of keys.
## Examples
@@ -637,24 +462,26 @@ defmodule Keyword do
"""
@spec keys(t) :: [key]
def keys(keywords) when is_list(keywords) do
:lists.map(
fn
{key, _} when is_atom(key) -> key
element -> throw(element)
end,
keywords
)
catch
element ->
raise ArgumentError,
"expected a keyword list, but an entry in the list is not a two-element tuple " <>
"with an atom as its first element, got: #{inspect(element)}"
try do
:lists.map(
fn
{key, _} when is_atom(key) -> key
element -> throw(element)
end,
keywords
)
catch
element ->
raise ArgumentError,
"expected a keyword list, but an entry in the list is not a two-element tuple with an atom as its first element, " <>
"got: #{inspect(element)}"
end
end
@doc """
Returns all values from the keyword list.
Keeps values from duplicate keys in the resulting list of values.
Values from duplicated keys will be kept in the final list of values.
## Examples
@@ -691,11 +518,11 @@ defmodule Keyword do
end
@doc """
Deletes the entries in the keyword list under a specific `key`.
Deletes the entries in the keyword list for a specific `key`.
If the `key` does not exist, it returns the keyword list unchanged.
If the `key` does not exist, returns the keyword list unchanged.
Use `delete_first/2` to delete just the first entry in case of
duplicate keys.
duplicated keys.
## Examples
@@ -721,9 +548,9 @@ defmodule Keyword do
defp delete_key([], _key), do: []
@doc """
Deletes the first entry in the keyword list under a specific `key`.
Deletes the first entry in the keyword list for a specific `key`.
If the `key` does not exist, it returns the keyword list unchanged.
If the `key` does not exist, returns the keyword list unchanged.
## Examples
@@ -754,10 +581,10 @@ defmodule Keyword do
end
@doc """
Puts the given `value` under the specified `key`.
Puts the given `value` under `key`.
If a value under `key` already exists, it overrides the value
and removes all duplicate entries.
If a previous value is already stored, all entries are
removed and the value is overridden.
## Examples
@@ -779,19 +606,19 @@ defmodule Keyword do
in keyword list unless `key` is already present.
This is useful if the value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
## Examples
iex> keyword = [a: 1]
iex> fun = fn ->
...> # some expensive operation here
...> 13
...> 3
...> end
iex> Keyword.put_new_lazy(keyword, :a, fun)
[a: 1]
iex> Keyword.put_new_lazy(keyword, :b, fun)
[b: 13, a: 1]
[b: 3, a: 1]
"""
@spec put_new_lazy(t, key, (() -> value)) :: t
@@ -804,7 +631,8 @@ defmodule Keyword do
end
@doc """
Puts the given `value` under `key`, unless the entry `key` already exists.
Puts the given `value` under `key` unless the entry `key`
already exists.
## Examples
@@ -825,8 +653,8 @@ defmodule Keyword do
@doc """
Puts a value under `key` only if the `key` already exists in `keywords`.
In case a key exists multiple times in the keyword list,
it removes later occurrences.
In the case a value is stored multiple times in the keyword list,
later occurrences are removed.
## Examples
@@ -858,7 +686,7 @@ defmodule Keyword do
@doc """
Puts a value under `key` only if the `key` already exists in `keywords`.
If `key` is not present in `keywords`, it raises a `KeyError`.
If `key` is not present in `keywords`, a `KeyError` exception is raised.
## Examples
@@ -886,50 +714,13 @@ defmodule Keyword do
end
defp replace!([], key, _value, original) do
raise KeyError, key: key, term: original
raise(KeyError, key: key, term: original)
end
@doc """
Replaces the value under `key` using the given function only if
`key` already exists in `keywords`.
In comparison to `replace/3`, this can be useful when it's expensive to calculate the value.
If `key` does not exist, the original keyword list is returned unchanged.
## Examples
iex> Keyword.replace_lazy([a: 1, b: 2], :a, fn v -> v * 4 end)
[a: 4, b: 2]
iex> Keyword.replace_lazy([a: 2, b: 2, a: 1], :a, fn v -> v * 4 end)
[a: 8, b: 2]
iex> Keyword.replace_lazy([a: 1, b: 2], :c, fn v -> v * 4 end)
[a: 1, b: 2]
"""
@doc since: "1.14.0"
@spec replace_lazy(t, key, (existing_value :: value -> new_value :: value)) :: t
def replace_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
do_replace_lazy(keywords, key, fun)
end
defp do_replace_lazy([{key, value} | keywords], key, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp do_replace_lazy([{_, _} = e | keywords], key, fun) do
[e | do_replace_lazy(keywords, key, fun)]
end
defp do_replace_lazy([], _key, _value), do: []
@doc """
Checks if two keywords are equal.
Considers two keywords to be equal if they contain
Two keywords are considered to be equal if they contain
the same keys and those keys contain the same values.
## Examples
@@ -941,25 +732,19 @@ defmodule Keyword do
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
true
Comparison between values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Keyword.equal?([a: 1.0], [a: 1])
false
"""
@spec equal?(t, t) :: boolean
def equal?(left, right) when is_list(left) and is_list(right) do
:lists.sort(left) === :lists.sort(right)
:lists.sort(left) == :lists.sort(right)
end
@doc """
Merges two keyword lists into one.
Adds all keys, including duplicate keys, given in `keywords2`
to `keywords1`, overriding any existing ones.
All keys, including duplicated keys, given in `keywords2` will be added
to `keywords1`, overriding any existing one.
There are no guarantees about the order of the keys in the returned keyword.
There are no guarantees about the order of keys in the returned keyword.
## Examples
@@ -1000,13 +785,13 @@ defmodule Keyword do
@doc """
Merges two keyword lists into one.
Adds all keys, including duplicate keys, given in `keywords2`
to `keywords1`. Invokes the given function to solve conflicts.
All keys, including duplicated keys, given in `keywords2` will be added
to `keywords1`. The given function will be invoked to solve conflicts.
If `keywords2` has duplicate keys, it invokes the given function
If `keywords2` has duplicate keys, the given function will be invoked
for each matching pair in `keywords1`.
There are no guarantees about the order of the keys in the returned keyword.
There are no guarantees about the order of keys in the returned keyword.
## Examples
@@ -1080,11 +865,12 @@ defmodule Keyword do
end
@doc """
Updates the value under `key` using the given function.
Updates the `key` with the given function.
Raises `KeyError` if the `key` does not exist.
If the `key` does not exist, raises `KeyError`.
Removes all duplicate keys and only updates the first one.
If there are duplicated keys, they are all removed and only the first one
is updated.
## Examples
@@ -1107,21 +893,23 @@ defmodule Keyword do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update!([{_, _} = pair | keywords], key, fun, original) do
[pair | update!(keywords, key, fun, original)]
defp update!([{_, _} = e | keywords], key, fun, original) do
[e | update!(keywords, key, fun, original)]
end
defp update!([], key, _fun, original) do
raise KeyError, key: key, term: original
defp update!([], key, _fun, original) when is_atom(key) do
raise(KeyError, key: key, term: original)
end
@doc """
Updates the value under `key` in `keywords` using the given function.
Updates the `key` in `keywords` with the given function.
If the `key` does not exist, it inserts the given `default` value.
Does not pass the `default` value through the update function.
Removes all duplicate keys and only updates the first one.
If there are duplicated keys, they are all removed and only the first one
is updated.
The default value will not be passed through the update function.
## Examples
@@ -1135,33 +923,30 @@ defmodule Keyword do
[a: 1, b: 11]
"""
@spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
@spec update(t, key, default :: value, (existing_value :: value -> updated_value :: value)) :: t
def update(keywords, key, default, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
update_guarded(keywords, key, default, fun)
end
defp update_guarded([{key, value} | keywords], key, _default, fun) do
def update([{key, value} | keywords], key, _default, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update_guarded([{_, _} = pair | keywords], key, default, fun) do
[pair | update_guarded(keywords, key, default, fun)]
def update([{_, _} = e | keywords], key, default, fun) do
[e | update(keywords, key, default, fun)]
end
defp update_guarded([], key, default, _fun) do
def update([], key, default, _fun) when is_atom(key) do
[{key, default}]
end
@doc """
Takes all entries corresponding to the given `keys` and extracts them into a
Takes all entries corresponding to the given keys and extracts them into a
separate keyword list.
Returns a tuple with the new list and the old list with removed keys.
Ignores keys for which there are no entries in the keyword list.
Keys for which there are no entries in the keyword list are ignored.
Entries with duplicate keys end up in the same keyword list.
Entries with duplicated keys end up in the same keyword list.
## Examples
@@ -1186,10 +971,10 @@ defmodule Keyword do
end
@doc """
Takes all entries corresponding to the given `keys` and returns them as a new
Takes all entries corresponding to the given keys and returns them in a new
keyword list.
Preserves duplicate keys in the new keyword list.
Duplicated keys are preserved in the new keyword list.
## Examples
@@ -1201,18 +986,16 @@ defmodule Keyword do
"""
@spec take(t, [key]) :: t
def take(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> :lists.member(k, keys) end, keywords)
:lists.filter(fn {k, _} -> k in keys end, keywords)
end
@doc """
Drops the given `keys` from the keyword list.
Drops the given keys from the keyword list.
Removes duplicate keys from the new keyword list.
Duplicated keys are preserved in the new keyword list.
## Examples
iex> Keyword.drop([a: 1, a: 2], [:a])
[]
iex> Keyword.drop([a: 1, b: 2, c: 3], [:b, :d])
[a: 1, c: 3]
iex> Keyword.drop([a: 1, b: 2, b: 3, c: 3, a: 5], [:b, :d])
@@ -1221,7 +1004,7 @@ defmodule Keyword do
"""
@spec drop(t, [key]) :: t
def drop(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> k not in keys end, keywords)
:lists.filter(fn {key, _} -> key not in keys end, keywords)
end
@doc """
@@ -1229,10 +1012,11 @@ defmodule Keyword do
It returns a tuple where the first element is the first value for `key` and the
second element is a keyword list with all entries associated with `key` removed.
If the `key` is not present in the keyword list, it returns `{default, keyword_list}`.
If the `key` is not present in the keyword list, `{default, keyword_list}` is
returned.
If you don't want to remove all the entries associated with `key` use `pop_first/3`
instead, which will remove only the first entry.
instead, that function will remove only the first entry.
## Examples
@@ -1258,7 +1042,7 @@ defmodule Keyword do
Returns the first value for `key` and removes all associated entries in the keyword list,
raising if `key` is not present.
This function behaves like `pop/3`, but raises in case the `key` is not present in the
This function behaves like `pop/3`, but raises in cases the `key` is not present in the
given `keywords`.
## Examples
@@ -1285,10 +1069,11 @@ defmodule Keyword do
It returns a tuple where the first element is a list of values for `key` and the
second element is a keyword list with all entries associated with `key` removed.
If the `key` is not present in the keyword list, it returns `{[], keyword_list}`.
If the `key` is not present in the keyword list, `{[], keyword_list}` is
returned.
If you don't want to remove all the entries associated with `key` use `pop_first/3`
instead, which will remove only the first entry.
instead, that function will remove only the first entry.
## Examples
@@ -1319,9 +1104,10 @@ defmodule Keyword do
Lazily returns and removes all values associated with `key` in the keyword list.
This is useful if the default value is very expensive to calculate or
generally difficult to set up and tear down again.
generally difficult to setup and teardown again.
Removes all duplicate keys. See `pop_first/3` for removing only the first entry.
All duplicated keys are removed. See `pop_first/3` for
removing only the first entry.
## Examples
@@ -1340,15 +1126,18 @@ defmodule Keyword do
def pop_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
case fetch(keywords, key) do
{:ok, value} -> {value, delete(keywords, key)}
:error -> {fun.(), keywords}
{:ok, value} ->
{value, delete(keywords, key)}
:error ->
{fun.(), keywords}
end
end
@doc """
Returns and removes the first value associated with `key` in the keyword list.
Keeps duplicate keys in the resulting keyword list.
Duplicated keys are not removed.
## Examples
@@ -1380,76 +1169,13 @@ defmodule Keyword do
"""
@spec to_list(t) :: t
def to_list(keywords) when is_list(keywords) do
keywords
def to_list(keyword) when is_list(keyword) do
keyword
end
@doc false
@deprecated "Use Kernel.length/1 instead"
def size(keywords) do
length(keywords)
end
@doc """
Returns a keyword list containing only the entries from `keywords`
for which the function `fun` returns a truthy value.
See also `reject/2` which discards all entries where the function
returns a truthy value.
## Examples
iex> Keyword.filter([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
[one: 1, three: 3]
"""
@doc since: "1.13.0"
@spec filter(t, ({key, value} -> as_boolean(term))) :: t
def filter(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
do_filter(keywords, fun)
end
defp do_filter([], _fun), do: []
defp do_filter([{_, _} = entry | entries], fun) do
if fun.(entry) do
[entry | do_filter(entries, fun)]
else
do_filter(entries, fun)
end
end
@doc """
Returns a keyword list excluding the entries from `keywords`
for which the function `fun` returns a truthy value.
See also `filter/2`.
## Examples
iex> Keyword.reject([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
[two: 2]
"""
@doc since: "1.13.0"
@spec reject(t, ({key, value} -> as_boolean(term))) :: t
def reject(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
do_reject(keywords, fun)
end
defp do_reject([], _fun), do: []
defp do_reject([{_, _} = entry | entries], fun) do
if fun.(entry) do
do_reject(entries, fun)
else
[entry | do_reject(entries, fun)]
end
end
@doc false
@deprecated "Use Keyword.new/2 instead"
def map(keywords, fun) when is_list(keywords) do
Enum.map(keywords, fn {k, v} -> {k, fun.({k, v})} end)
def size(keyword) do
length(keyword)
end
end
+31 -190
View File
@@ -8,20 +8,13 @@ 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]
iex> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
An element can be prepended to a list using `|`:
iex> new = 0
iex> list = [1, 2, 3]
iex> [new | list]
[0, 1, 2, 3]
Lists in Elixir are effectively linked lists, which means
they are internally represented in pairs containing the
head and the tail of a list:
@@ -230,7 +223,7 @@ defmodule List do
@doc """
Folds (reduces) the given list from the left with
a function. Requires an accumulator, which can be any value.
a function. Requires an accumulator.
## Examples
@@ -240,9 +233,6 @@ defmodule List do
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
2
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
{6, -6}
"""
@spec foldl([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
def foldl(list, acc, fun) when is_list(list) and is_function(fun) do
@@ -251,16 +241,13 @@ defmodule List do
@doc """
Folds (reduces) the given list from the right with
a function. Requires an accumulator, which can be any value.
a function. Requires an accumulator.
## Examples
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
-2
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
%{product: 24, sum: 10}
"""
@spec foldr([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
def foldr(list, acc, fun) when is_list(list) and is_function(fun) do
@@ -268,18 +255,13 @@ defmodule List do
end
@doc """
Returns the first element in `list` or `default` if `list` is empty.
`first/2` has been introduced in Elixir v1.12.0, while `first/1` has been available since v1.0.0.
Returns the first element in `list` or `nil` if `list` is empty.
## Examples
iex> List.first([])
nil
iex> List.first([], 1)
1
iex> List.first([1])
1
@@ -287,25 +269,19 @@ defmodule List do
1
"""
@spec first([], any) :: any
@spec first([elem, ...], any) :: elem when elem: var
def first(list, default \\ nil)
def first([], default), do: default
def first([head | _], _default), do: head
@spec first([]) :: nil
@spec first([elem, ...]) :: elem when elem: var
def first([]), do: nil
def first([head | _]), do: head
@doc """
Returns the last element in `list` or `default` if `list` is empty.
`last/2` has been introduced in Elixir v1.12.0, while `last/1` has been available since v1.0.0.
Returns the last element in `list` or `nil` if `list` is empty.
## Examples
iex> List.last([])
nil
iex> List.last([], 1)
1
iex> List.last([1])
1
@@ -313,13 +289,11 @@ defmodule List do
3
"""
@spec last([], any) :: any
@spec last([elem, ...], any) :: elem when elem: var
@compile {:inline, last: 2}
def last(list, default \\ nil)
def last([], default), do: default
def last([head], _default), do: head
def last([_ | tail], default), do: last(tail, default)
@spec last([]) :: nil
@spec last([elem, ...]) :: elem when elem: var
def last([]), do: nil
def last([head]), do: head
def last([_ | tail]), do: last(tail)
@doc """
Receives a list of tuples and returns the first tuple
@@ -339,52 +313,12 @@ defmodule List do
iex> List.keyfind([a: 1, b: 2], :c, 0)
nil
This function works for any list of tuples:
iex> List.keyfind([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
"""
@spec keyfind([tuple], any, non_neg_integer, any) :: any
def keyfind(list, key, position, default \\ nil) when is_integer(position) do
def keyfind(list, key, position, default \\ nil) do
:lists.keyfind(key, position + 1, list) || default
end
@doc """
Receives a list of tuples and returns the first tuple
where the element at `position` in the tuple matches the
given `key`.
If no matching tuple is found, an error is raised.
## Examples
iex> List.keyfind!([a: 1, b: 2], :a, 0)
{:a, 1}
iex> List.keyfind!([a: 1, b: 2], 2, 1)
{:b, 2}
iex> List.keyfind!([a: 1, b: 2], :c, 0)
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
This function works for any list of tuples:
iex> List.keyfind!([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
"""
@doc since: "1.13.0"
@spec keyfind!([tuple], any, non_neg_integer) :: any
def keyfind!(list, key, position) when is_integer(position) do
:lists.keyfind(key, position + 1, list) ||
raise KeyError,
key: key,
term: list,
message:
"key #{inspect(key)} at position #{inspect(position)} not found in: #{inspect(list)}"
end
@doc """
Receives a list of tuples and returns `true` if there is
a tuple where the element at `position` in the tuple matches
@@ -401,14 +335,9 @@ defmodule List do
iex> List.keymember?([a: 1, b: 2], :c, 0)
false
This function works for any list of tuples:
iex> List.keymember?([{22, "SSH"}, {80, "HTTP"}], 22, 0)
true
"""
@spec keymember?([tuple], any, non_neg_integer) :: boolean
def keymember?(list, key, position) when is_integer(position) do
def keymember?(list, key, position) do
:lists.keymember(key, position + 1, list)
end
@@ -424,26 +353,15 @@ defmodule List do
iex> List.keyreplace([a: 1, b: 2], :a, 1, {:a, 3})
[a: 1, b: 2]
This function works for any list of tuples:
iex> List.keyreplace([{22, "SSH"}, {80, "HTTP"}], 22, 0, {22, "Secure Shell"})
[{22, "Secure Shell"}, {80, "HTTP"}]
"""
@spec keyreplace([tuple], any, non_neg_integer, tuple) :: [tuple]
def keyreplace(list, key, position, new_tuple) when is_integer(position) do
def keyreplace(list, key, position, new_tuple) do
:lists.keyreplace(key, position + 1, list, new_tuple)
end
@doc """
Receives a list of tuples and sorts the elements
at `position` of the tuples.
The sort is stable.
A `sorter` argument is available since Elixir v1.14.0. Similar to
`Enum.sort/2`, the sorter can be an anonymous function, the atoms
`:asc` or `:desc`, or module that implements a compare function.
at `position` of the tuples. The sort is stable.
## Examples
@@ -453,69 +371,12 @@ defmodule List do
iex> List.keysort([a: 5, c: 1, b: 3], 0)
[a: 5, b: 3, c: 1]
To sort in descending order:
iex> List.keysort([a: 5, c: 1, b: 3], 0, :desc)
[c: 1, b: 3, a: 5]
As in `Enum.sort/2`, avoid using the default sorting function to sort
structs, as by default it performs structural comparison instead of a
semantic one. In such cases, you shall pass a sorting function as third
element or any module that implements a `compare/2` function. For example,
if you have tuples with user names and their birthday, and you want to
sort on their birthday, in both ascending and descending order, you should
do:
iex> users = [
...> {"Ellis", ~D[1943-05-11]},
...> {"Lovelace", ~D[1815-12-10]},
...> {"Turing", ~D[1912-06-23]}
...> ]
iex> List.keysort(users, 1, Date)
[
{"Lovelace", ~D[1815-12-10]},
{"Turing", ~D[1912-06-23]},
{"Ellis", ~D[1943-05-11]}
]
iex> List.keysort(users, 1, {:desc, Date})
[
{"Ellis", ~D[1943-05-11]},
{"Turing", ~D[1912-06-23]},
{"Lovelace", ~D[1815-12-10]}
]
"""
@doc since: "1.14.0"
@spec keysort(
[tuple],
non_neg_integer,
(any, any -> boolean) | :asc | :desc | module() | {:asc | :desc, module()}
) :: [tuple]
def keysort(list, position, sorter \\ :asc)
def keysort(list, position, :asc) when is_list(list) and is_integer(position) do
@spec keysort([tuple], non_neg_integer) :: [tuple]
def keysort(list, position) do
:lists.keysort(position + 1, list)
end
def keysort(list, position, sorter) when is_list(list) and is_integer(position) do
:lists.sort(keysort_fun(sorter, position + 1), list)
end
defp keysort_fun(sorter, position) when is_function(sorter, 2),
do: &sorter.(:erlang.element(position, &1), :erlang.element(position, &2))
defp keysort_fun(:desc, position),
do: &(:erlang.element(position, &1) >= :erlang.element(position, &2))
defp keysort_fun(module, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :gt)
defp keysort_fun({:asc, module}, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :gt)
defp keysort_fun({:desc, module}, position) when is_atom(module),
do: &(module.compare(:erlang.element(position, &1), :erlang.element(position, &2)) != :lt)
@doc """
Receives a `list` of tuples and replaces the element
identified by `key` at `position` with `new_tuple`.
@@ -530,14 +391,9 @@ defmodule List do
iex> List.keystore([a: 1, b: 2], :c, 0, {:c, 3})
[a: 1, b: 2, c: 3]
This function works for any list of tuples:
iex> List.keystore([{22, "SSH"}], 80, 0, {80, "HTTP"})
[{22, "SSH"}, {80, "HTTP"}]
"""
@spec keystore([tuple], any, non_neg_integer, tuple) :: [tuple, ...]
def keystore(list, key, position, new_tuple) when is_integer(position) do
def keystore(list, key, position, new_tuple) do
:lists.keystore(key, position + 1, list, new_tuple)
end
@@ -557,14 +413,9 @@ defmodule List do
iex> List.keydelete([a: 1, b: 2], :c, 0)
[a: 1, b: 2]
This function works for any list of tuples:
iex> List.keydelete([{22, "SSH"}, {80, "HTTP"}], 80, 0)
[{22, "SSH"}]
"""
@spec keydelete([tuple], any, non_neg_integer) :: [tuple]
def keydelete(list, key, position) when is_integer(position) do
def keydelete(list, key, position) do
:lists.keydelete(key, position + 1, list)
end
@@ -586,14 +437,9 @@ defmodule List do
iex> List.keytake([a: 1, b: 2], :c, 0)
nil
This function works for any list of tuples:
iex> List.keytake([{22, "SSH"}, {80, "HTTP"}], 80, 0)
{{80, "HTTP"}, [{22, "SSH"}]}
"""
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
def keytake(list, key, position) when is_integer(position) do
def keytake(list, key, position) do
case :lists.keytake(key, position + 1, list) do
{:value, element, list} -> {element, list}
false -> nil
@@ -947,22 +793,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
@@ -973,6 +811,9 @@ defmodule List do
iex> List.to_existing_atom('🌢 Elixir')
:"🌢 Elixir"
iex> List.to_existing_atom('this_atom_will_never_exist')
** (ArgumentError) argument error
"""
@spec to_existing_atom(charlist) :: atom
def to_existing_atom(charlist) do
@@ -1058,7 +899,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](`:binary`).
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
@@ -1113,7 +954,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](`:binary`).
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
+123 -978
View File
File diff suppressed because it is too large Load Diff
+78 -170
View File
@@ -21,51 +21,75 @@ defmodule Macro.Env do
It contains the following fields:
* `aliases` - a list of two-element tuples, where the first
element is the aliased name and the second one the actual name
* `context` - the context of the environment; it can be `nil`
(default context), `:guard` (inside a guard) or `:match` (inside a match)
* `context_modules` - a list of modules defined in the current context
* `file` - the current absolute file name as a binary
* `file` - the current file name as a binary
* `function` - a tuple as `{atom, integer}`, where the first
element is the function name and the second its arity; returns
`nil` if not inside a function
* `functions` - a list of functions imported from each module
* `line` - the current line as an integer
* `macro_aliases` - a list of aliases defined inside the current macro
* `macros` - a list of macros imported from each module
* `module` - the current module name
* `requires` - the list of required modules
The following fields are private to Elixir's macro expansion mechanism and
must not be accessed directly:
* `aliases`
* `functions`
* `macro_aliases`
* `macros`
* `contextual_vars`
* `current_vars`
* `lexical_tracker`
* `requires`
* `prematch_vars`
* `tracers`
* `versioned_vars`
* `unused_vars`
The following fields are deprecated and must not be accessed or relied on:
* `vars` - a list keeping all defined variables as `{var, context}`
"""
@type aliases :: [{module, module}]
@type context :: :match | :guard | nil
@type context_modules :: [module]
@type file :: binary
@type functions :: [{module, [name_arity]}]
@type lexical_tracker :: pid | nil
@type line :: non_neg_integer
@type macro_aliases :: [{module, {term, module}}]
@type macros :: [{module, [name_arity]}]
@type name_arity :: {atom, arity}
@type requires :: [module]
@type variable :: {atom, atom | term}
@typep aliases :: [{module, module}]
@typep functions :: [{module, [name_arity]}]
@typep lexical_tracker :: pid | nil
@typep macro_aliases :: [{module, {term, module}}]
@typep macros :: [{module, [name_arity]}]
@typep requires :: [module]
@typep contextual_vars :: [atom]
@typep current_vars ::
{%{optional(variable) => {var_version, var_type}},
%{optional(variable) => {var_version, var_type}} | false}
@typep unused_vars ::
{%{optional({atom, var_version}) => non_neg_integer | false}, non_neg_integer}
@typep prematch_vars ::
{%{optional(variable) => {var_version, var_type}}, non_neg_integer}
| :warn
| :raise
| :pin
| :apply
@typep tracers :: [module]
@typep versioned_vars :: %{optional(variable) => var_version :: non_neg_integer}
@typep var_type :: :term
@typep var_version :: non_neg_integer
@typep vars :: [variable]
@type t :: %{
__struct__: __MODULE__,
aliases: aliases,
context: context,
context_modules: context_modules,
contextual_vars: contextual_vars,
current_vars: current_vars,
file: file,
function: name_arity | nil,
functions: functions,
@@ -73,45 +97,41 @@ defmodule Macro.Env do
line: line,
macro_aliases: macro_aliases,
macros: macros,
module: module,
module: atom,
prematch_vars: prematch_vars,
unused_vars: unused_vars,
requires: requires,
tracers: tracers,
versioned_vars: versioned_vars
vars: vars
}
fields = [
aliases: [],
context: nil,
context_modules: [],
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
requires: [],
tracers: [],
versioned_vars: %{}
]
# TODO: Remove :vars field on v2.0
def __struct__ do
%{
__struct__: __MODULE__,
aliases: [],
context: nil,
context_modules: [],
contextual_vars: [],
current_vars: {%{}, %{}},
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
prematch_vars: :warn,
requires: [],
tracers: [],
unused_vars: {%{}, 0},
vars: []
}
end
# 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 """
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: []}
def __struct__(kv) do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
end
@doc """
@@ -126,30 +146,19 @@ defmodule Macro.Env do
@spec vars(t) :: [variable]
def vars(env)
def vars(%{__struct__: Macro.Env, versioned_vars: vars}) do
Map.keys(vars)
def vars(%{__struct__: Macro.Env, current_vars: {read, _}}) do
Map.keys(read)
end
@doc """
Checks if a variable belongs to the environment.
## Examples
iex> x = 13
iex> x
13
iex> Macro.Env.has_var?(__ENV__, {:x, nil})
true
iex> Macro.Env.has_var?(__ENV__, {:unknown, nil})
false
"""
@doc since: "1.7.0"
@spec has_var?(t, variable) :: boolean()
def has_var?(env, var)
def has_var?(%{__struct__: Macro.Env, versioned_vars: vars}, var) do
Map.has_key?(vars, var)
def has_var?(%{__struct__: Macro.Env, current_vars: {read, _}}, var) do
Map.has_key?(read, var)
end
@doc """
@@ -163,117 +172,16 @@ defmodule Macro.Env do
[file: file, line: line]
end
@doc """
Fetches the alias for the given atom.
Returns `{:ok, alias}` if the alias exists, `:error`
otherwise.
## Examples
iex> alias Foo.Bar, as: Baz
iex> Baz
Foo.Bar
iex> Macro.Env.fetch_alias(__ENV__, :Baz)
{:ok, Foo.Bar}
iex> Macro.Env.fetch_alias(__ENV__, :Unknown)
:error
"""
@doc since: "1.13.0"
@spec fetch_alias(t, atom) :: {:ok, atom} | :error
def fetch_alias(%{__struct__: Macro.Env, aliases: aliases}, atom) when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
@doc """
Fetches the macro alias for the given atom.
Returns `{:ok, macro_alias}` if the alias exists, `:error`
otherwise.
A macro alias is only used inside quoted expansion. See
`fetch_alias/2` for a more general example.
"""
@doc since: "1.13.0"
@spec fetch_macro_alias(t, atom) :: {:ok, atom} | :error
def fetch_macro_alias(%{__struct__: Macro.Env, macro_aliases: aliases}, atom)
when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
@doc """
Returns the modules from which the given `{name, arity}` was
imported.
It returns a list of two element tuples in the shape of
`{:function | :macro, module}`. The elements in the list
are in no particular order and the order is not guaranteed.
## Examples
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[]
iex> import Tuple, only: [duplicate: 2], warn: false
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[{:function, Tuple}]
iex> import List, only: [duplicate: 2], warn: false
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
[{:function, List}, {:function, Tuple}]
iex> Macro.Env.lookup_import(__ENV__, {:def, 1})
[{:macro, Kernel}]
"""
@doc since: "1.13.0"
@spec lookup_import(t, name_arity) :: [{:function | :macro, module}]
def lookup_import(
%{__struct__: Macro.Env, functions: functions, macros: macros},
{name, arity} = pair
)
when is_atom(name) and is_integer(arity) do
f = for {mod, pairs} <- functions, :ordsets.is_element(pair, pairs), do: {:function, mod}
m = for {mod, pairs} <- macros, :ordsets.is_element(pair, pairs), do: {:macro, mod}
f ++ m
end
@doc """
Returns true if the given module has been required.
## Examples
iex> Macro.Env.required?(__ENV__, Integer)
false
iex> require Integer
iex> Macro.Env.required?(__ENV__, Integer)
true
iex> Macro.Env.required?(__ENV__, Kernel)
true
"""
@doc since: "1.13.0"
@spec required?(t, module) :: boolean
def required?(%{__struct__: Macro.Env, requires: requires}, mod) when is_atom(mod),
do: mod in requires
@doc """
Prepend a tracer to the list of tracers in the environment.
## Examples
Macro.Env.prepend_tracer(__ENV__, MyCustomTracer)
"""
@doc since: "1.13.0"
@spec prepend_tracer(t, module) :: t
def prepend_tracer(%{__struct__: Macro.Env, tracers: tracers} = env, tracer) do
%{env | tracers: [tracer | tracers]}
end
@doc """
Returns a `Macro.Env` in the match context.
"""
@spec to_match(t) :: t
def to_match(%{__struct__: Macro.Env} = env) do
%{env | context: :match}
def to_match(%{__struct__: Macro.Env, context: :match} = env) do
env
end
def to_match(%{__struct__: Macro.Env, current_vars: {read, _}, unused_vars: {_, counter}} = env) do
%{env | context: :match, prematch_vars: {read, counter}}
end
@doc """
+35 -186
View File
@@ -19,13 +19,8 @@ defmodule Map do
in a map literal, the last one prevails.
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
can be used (as in many other special forms):
iex> %{a: 1, b: 2}
%{a: 1, b: 2}
If you want to mix the shorthand syntax with `=>`, the shorthand syntax must come
at the end:
can be used (as in many other special forms), provided key-value pairs are put at
the end:
iex> %{"hello" => "world", a: 1, b: 2}
%{:a => 1, :b => 2, "hello" => "world"}
@@ -50,11 +45,11 @@ defmodule Map do
map.foo
#=> "bar"
map.non_existing_key
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
#=> ** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
> Note: do not add parens when accessing fields, such as in `data.key()`.
> If parenthesis are used, Elixir will expect `data` to be an atom representing
> a module and attempt to call the *function* `key/0` in it.
> If parenthesis are used, Elixir will consider it to be a function call
> on `data`, which would be expected to be an atom.
The two syntaxes for accessing keys reveal the dual nature of maps. The `map[key]`
syntax is used for dynamically created maps that may have any key, of any type.
@@ -88,18 +83,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 +112,6 @@ defmodule Map do
@type value :: any
@compile {:inline, fetch: 2, fetch!: 2, get: 2, put: 3, delete: 2, has_key?: 2, replace!: 3}
# TODO: Remove conditional on Erlang/OTP 24+
@compile {:no_warn_undefined, {:maps, :from_keys, 2}}
@doc """
Builds a map from the given `keys` and the fixed `value`.
## Examples
iex> Map.from_keys([1, 2, 3], :number)
%{1 => :number, 2 => :number, 3 => :number}
"""
@doc since: "1.14.0"
@spec from_keys([key], value) :: map
def from_keys(keys, value) do
if function_exported?(:maps, :from_keys, 2) do
:maps.from_keys(keys, value)
else
:maps.from_list(:lists.map(&{&1, value}, keys))
end
end
@doc """
Returns all keys from `map`.
@@ -241,29 +209,22 @@ defmodule Map do
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform)
def new(%_{} = enumerable, transform), do: new_from_enum(enumerable, transform)
def new(%{} = map, transform), do: new_from_map(map, transform)
def new(enumerable, transform), do: new_from_enum(enumerable, transform)
defp new_from_map(map, transform) when is_function(transform, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_map(next, transform))
end
defp do_map(:none, _fun), do: []
defp do_map({key, value, iter}, transform) do
[transform.({key, value}) | do_map(:maps.next(iter), transform)]
end
defp new_from_enum(enumerable, transform) when is_function(transform, 1) do
def new(enumerable, transform) when is_function(transform, 1) do
enumerable
|> Enum.map(transform)
|> Enum.to_list()
|> new_transform(transform, [])
end
defp new_transform([], _fun, acc) do
acc
|> :lists.reverse()
|> :maps.from_list()
end
defp new_transform([element | rest], fun, acc) do
new_transform(rest, fun, [fun.(element) | acc])
end
@doc """
Returns whether the given `key` exists in the given `map`.
@@ -362,7 +323,7 @@ defmodule Map do
def replace(map, key, value) do
case map do
%{^key => _value} ->
%{map | key => value}
put(map, key, value)
%{} ->
map
@@ -394,32 +355,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 +551,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.
@@ -679,12 +614,12 @@ defmodule Map do
%{a: 1, b: 11}
"""
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
@spec update(map, key, default :: value, (existing_value :: value -> updated_value :: value)) ::
map
def update(map, key, default, fun) when is_function(fun, 1) do
case map do
%{^key => value} ->
%{map | key => fun.(value)}
put(map, key, fun.(value))
%{} ->
put(map, key, default)
@@ -697,8 +632,8 @@ defmodule Map do
@doc """
Removes the value associated with `key` in `map` and returns the value and the updated map.
If `key` is present in `map`, it returns `{value, updated_map}` where `value` is the value of
the key and `updated_map` is the result of removing `key` from `map`. If `key`
If `key` is present in `map`, it returns `{value, new_map}` where `value` is the value of
the key and `new_map` is the result of removing `key` from `map`. If `key`
is not present in `map`, `{default, map}` is returned.
## Examples
@@ -711,7 +646,7 @@ defmodule Map do
{3, %{a: 1}}
"""
@spec pop(map, key, default) :: {value, updated_map :: map} | {default, map} when default: value
@spec pop(map, key, value) :: {value, new_map :: map}
def pop(map, key, default \\ nil) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -720,8 +655,8 @@ defmodule Map do
end
@doc """
Removes the value associated with `key` in `map` and returns the value
and the updated map, or it raises if `key` is not present.
Returns and removes the value associated with `key` in `map` or raises
if `key` is not present.
Behaves the same as `pop/3` but raises if `key` is not present in `map`.
@@ -736,7 +671,7 @@ defmodule Map do
"""
@doc since: "1.10.0"
@spec pop!(map, key) :: {value, updated_map :: map}
@spec pop!(map, key) :: {value, map}
def pop!(map, key) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -877,10 +812,10 @@ defmodule Map do
** (KeyError) key :b not found in: %{a: 1}
"""
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
@spec update!(map, key, (existing_value :: value -> updated_value :: value)) :: map
def update!(map, key, fun) when is_function(fun, 1) do
value = fetch!(map, key)
%{map | key => fun.(value)}
put(map, key, fun.(value))
end
@doc """
@@ -906,7 +841,7 @@ defmodule Map do
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, %{a: 1, b: "new value!"}}
{nil, %{b: "new value!", a: 1}}
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
{1, %{}}
@@ -915,8 +850,8 @@ defmodule Map do
{nil, %{a: 1}}
"""
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_map :: map}
@spec get_and_update(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
when current_value: value
def get_and_update(map, key, fun) when is_function(fun, 1) do
current = get(map, key)
@@ -957,7 +892,7 @@ defmodule Map do
{1, %{}}
"""
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
@spec get_and_update!(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
when current_value: value
def get_and_update!(map, key, fun) when is_function(fun, 1) do
@@ -965,7 +900,7 @@ defmodule Map do
case fun.(value) do
{get, update} ->
{get, %{map | key => update}}
{get, put(map, key, update)}
:pop ->
{value, delete(map, key)}
@@ -1010,11 +945,6 @@ defmodule Map do
Two maps are considered to be equal if they contain
the same keys and those keys contain the same values.
Note this function exists for completeness so the `Map`
and `Keyword` modules provide similar APIs. In practice,
developers often compare maps using `==/2` or `===/2`
directly.
## Examples
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
@@ -1022,12 +952,6 @@ defmodule Map do
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
false
Comparison between keys and values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Map.equal?(%{a: 1.0}, %{a: 1})
false
"""
@spec equal?(map, map) :: boolean
def equal?(map1, map2)
@@ -1041,79 +965,4 @@ defmodule Map do
def size(map) do
map_size(map)
end
@doc """
Returns a map containing only those pairs from `map`
for which `fun` returns a truthy value.
`fun` receives the key and value of each of the
elements in the map as a key-value pair.
See also `reject/2` which discards all elements where the
function returns a truthy value.
> Note: if you find yourself doing multiple calls to `Map.filter/2`
> and `Map.reject/2` in a pipeline, it is likely more efficient
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
> a map at the end using `Map.new/1`.
## Examples
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
%{one: 1, three: 3}
"""
@doc since: "1.13.0"
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
def filter(map, fun) when is_map(map) and is_function(fun, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_filter(next, fun))
end
defp do_filter(:none, _fun), do: []
defp do_filter({key, value, iter}, fun) do
if fun.({key, value}) do
[{key, value} | do_filter(:maps.next(iter), fun)]
else
do_filter(:maps.next(iter), fun)
end
end
@doc """
Returns map excluding the pairs from `map` for which `fun` returns
a truthy value.
See also `filter/2`.
## Examples
iex> Map.reject(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
%{two: 2}
"""
@doc since: "1.13.0"
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
def reject(map, fun) when is_map(map) and is_function(fun, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_reject(next, fun))
end
defp do_reject(:none, _fun), do: []
defp do_reject({key, value, iter}, fun) do
if fun.({key, value}) do
do_reject(:maps.next(iter), fun)
else
[{key, value} | do_reject(:maps.next(iter), fun)]
end
end
@doc false
@deprecated "Use Map.new/2 instead (invoke Map.from_struct/1 before if you have a struct)"
def map(map, fun) when is_map(map) do
:maps.map(fn k, v -> fun.({k, v}) end, map)
end
end
+62 -156
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 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,15 +217,13 @@ 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 """
Checks if two sets are equal.
The comparison between elements is done using `===/2`,
which a set with `1` is not equivalent to a set with
`1.0`.
The comparison between elements must be done using `===/2`.
## Examples
@@ -249,13 +231,11 @@ defmodule MapSet do
true
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
false
iex> MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))
false
"""
@spec equal?(t, t) :: boolean
def equal?(%MapSet{map: map1, version: version}, %MapSet{map: map2, version: version}) do
map1 === map2
Map.equal?(map1, map2)
end
# Elixir v1.5 changed the map representation, so on
@@ -270,10 +250,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 +275,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 +284,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 +359,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 +370,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 +389,7 @@ defmodule MapSet do
def slice(map_set) do
size = MapSet.size(map_set)
{:ok, size, &MapSet.to_list/1}
{:ok, size, &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2, size)}
end
def reduce(map_set, acc, fun) do
@@ -494,8 +400,8 @@ defmodule MapSet do
defimpl Collectable do
def into(map_set) do
fun = fn
list, {:cont, x} -> [x | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.from_keys(list, []))}
list, {:cont, x} -> [{x, []} | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.new(list))}
_, :halt -> :ok
end
@@ -508,7 +414,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
+278 -664
View File
File diff suppressed because it is too large Load Diff
+2 -3
View File
@@ -71,9 +71,8 @@ defmodule Module.LocalsTracker do
:ok
end
@doc """
Collect all conflicting imports with the given functions
"""
# Collecting all conflicting imports with the given functions
@doc false
def collect_imports_conflicts({set, _bag}, all_defined) do
for {pair, _, meta, _} <- all_defined, n = out_neighbour(set, {:import, pair}) do
{meta, {n, pair}}
+226 -333
View File
@@ -1,168 +1,43 @@
defmodule Module.ParallelChecker do
@moduledoc false
import Kernel, except: [spawn: 3]
@type cache() :: {pid(), :ets.tid()}
@type warning() :: term()
@type kind() :: :def | :defmacro
@type mode() :: :elixir | :erlang
@doc """
Initializes the parallel checker process.
"""
def start_link(schedulers \\ nil) do
:gen_server.start_link(__MODULE__, schedulers, [])
end
@doc """
Stops the parallel checker process.
"""
def stop(checker) do
send(checker, {__MODULE__, :stop})
:ok
end
@doc """
Gets the parallel checker data from pdict.
"""
def get do
case :erlang.get(:elixir_checker_info) do
{parent, nil} ->
{:ok, checker} = start_link()
put(parent, checker)
{parent, checker}
{parent, checker} ->
{parent, checker}
end
end
@doc """
Stores the parallel checker information.
"""
def put(pid, checker) when is_pid(pid) and is_pid(checker) do
:erlang.put(:elixir_checker_info, {pid, checker})
end
@doc """
Spawns a process that runs the parallel checker.
"""
def spawn({pid, checker}, module, info) do
ref = make_ref()
spawned =
spawn(fn ->
Process.link(pid)
mon_ref = Process.monitor(pid)
receive do
{^ref, :cache, ets} ->
module_map =
if is_map(info) do
info
else
case File.read(info) do
{:ok, binary} -> maybe_module_map(binary, module)
{:error, _} -> nil
end
end
module_map && cache_from_module_map(ets, module_map)
send(checker, {ref, :cached})
receive do
{^ref, :check} ->
# Set the compiler info so we can collect warnings
:erlang.put(:elixir_compiler_info, {pid, self()})
warnings =
if module_map do
check_module(module_map, {checker, ets})
else
[]
end
send(pid, {__MODULE__, module, warnings})
send(checker, {__MODULE__, :done})
end
{:DOWN, ^mon_ref, _, _, _} ->
:ok
end
end)
register(checker, spawned, ref)
:ok
end
@doc """
Verifies the given compilation function
by starting a checker if one does not exist.
See `verify/3`.
"""
def verify(fun) do
case :erlang.get(:elixir_compiler_info) do
:undefined ->
previous = :erlang.put(:elixir_checker_info, {self(), nil})
try do
result = fun.()
case :erlang.get(:elixir_checker_info) do
{_, nil} -> :ok
{_, checker} -> verify(checker, [])
end
result
after
{_, checker} = :erlang.get(:elixir_checker_info)
if previous != :undefined do
:erlang.put(:elixir_checker_info, previous)
else
:erlang.erase(:elixir_checker_info)
end
checker && stop(checker)
end
_ ->
# If we are during compilation, then they will be
# reported to the compiler, which will validate them.
fun.()
end
end
@doc """
Receives pairs of module maps and BEAM binaries. In parallel it verifies
the modules and adds the ExCk chunk to the binaries. Returns the updated
list of warnings from the verification.
binaries and a list of warnings from the verification.
"""
@spec verify(pid(), [{module(), Path.t()}]) :: [warning()]
def verify(checker, runtime_files) do
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
@spec verify([{map(), binary()}], [{module(), binary()}], pos_integer() | nil) :: [warning()]
def verify(compiled_modules, runtime_binaries, schedulers \\ nil) do
compiled_maps = Enum.map(compiled_modules, fn {map, _binary} -> {map.module, map} end)
modules = :gen_server.call(checker, :start, :infinity)
collect_results(modules, [])
case compiled_maps ++ runtime_binaries do
[] ->
[]
check_modules ->
schedulers = schedulers || max(:erlang.system_info(:schedulers_online), 2)
{:ok, server} =
:gen_server.start_link(__MODULE__, [check_modules, self(), schedulers], [])
preload_cache(get_ets(server), check_modules)
start(server)
collect_results(length(check_modules), [])
end
end
defp collect_results([], warnings) do
defp collect_results(0, warnings) do
warnings
end
defp collect_results(modules, warnings) do
defp collect_results(count, warnings) do
receive do
{:warning, file, location, message} ->
file = file && Path.absname(file)
message = :unicode.characters_to_binary(message)
warning = {file, location, message}
collect_results(modules, [warning | warnings])
{__MODULE__, _module, new_warnings} ->
collect_results(tl(modules), new_warnings ++ warnings)
collect_results(count - 1, new_warnings ++ warnings)
end
end
@@ -170,8 +45,8 @@ defmodule Module.ParallelChecker do
Test cache.
"""
def test_cache do
{:ok, checker} = start_link()
{checker, :gen_server.call(checker, :ets, :infinity)}
{:ok, pid} = :gen_server.start_link(__MODULE__, [[], self(), 1], [])
{pid, get_ets(pid)}
end
@doc """
@@ -192,17 +67,17 @@ defmodule Module.ParallelChecker do
or if the function does not exist return `{:error, :function}`.
"""
@spec fetch_export(cache(), module(), atom(), arity()) ::
{:ok, mode(), kind(), binary() | nil} | {:error, :function | :module}
{:ok, kind(), binary() | nil} | {:error, :function | :module}
def fetch_export({_server, ets}, module, fun, arity) do
case :ets.lookup(ets, {:cached, module}) do
[{_key, false}] ->
{:error, :module}
[{_key, mode}] ->
case :ets.lookup(ets, {:export, module, {fun, arity}}) do
[{_key, kind, reason}] -> {:ok, mode, kind, reason}
[{_key, true}] ->
case :ets.lookup(ets, {:export, {module, fun, arity}}) do
[{_key, kind, reason}] -> {:ok, kind, reason}
[] -> {:error, :function}
end
[{_key, false}] ->
{:error, :module}
end
end
@@ -214,34 +89,44 @@ defmodule Module.ParallelChecker do
def all_exports({_server, ets}, module) do
# This is only called after we get a deprecation notice
# so we can assume it's a cached module
ets
|> :ets.match({{:export, module, :"$1"}, :_, :_})
|> Enum.flat_map(& &1)
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
exports
|> Enum.map(fn {function, _kind} -> function end)
|> Enum.sort()
end
## Module checking
defp check_module(module_map, cache) do
%{module: module, file: file, compile_opts: compile_opts, definitions: definitions} =
module_map
defp check_module(module, cache) do
case extract_definitions(module) do
{:ok, module, file, definitions, no_warn_undefined} ->
Module.Types.warnings(module, file, definitions, no_warn_undefined, cache)
|> group_warnings()
|> emit_warnings()
:error ->
[]
end
end
defp extract_definitions({module, module_map}) when is_map(module_map) do
no_warn_undefined =
compile_opts
module_map.compile_opts
|> extract_no_warn_undefined()
|> merge_compiler_no_warn_undefined()
warnings =
module
|> Module.Types.warnings(file, definitions, no_warn_undefined, cache)
|> group_warnings()
|> emit_warnings()
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
end
module_map
|> Map.get(:after_verify, [])
|> Enum.each(fn {verify_mod, verify_fun} -> apply(verify_mod, verify_fun, [module]) end)
warnings
defp extract_definitions({module, binary}) when is_binary(binary) do
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
{:debug_info_v1, backend, data} <- chunk,
{:ok, module_map} <- backend.debug_info(:elixir_v1, module, data, []) do
extract_definitions({module, module_map})
else
_ -> :error
end
end
defp extract_no_warn_undefined(compile_opts) do
@@ -254,8 +139,11 @@ defmodule Module.ParallelChecker do
defp merge_compiler_no_warn_undefined(no_warn_undefined) do
case Code.get_compiler_option(:no_warn_undefined) do
:all -> :all
list when is_list(list) -> no_warn_undefined ++ list
:all ->
:all
list when is_list(list) ->
no_warn_undefined ++ list
end
end
@@ -288,7 +176,7 @@ defmodule Module.ParallelChecker do
defp format_locations(locations) do
[
"Invalid call found at #{length(locations)} locations:\n",
"Found at #{length(locations)} locations:\n",
Enum.map(locations, &format_location/1)
]
end
@@ -316,166 +204,23 @@ defmodule Module.ParallelChecker do
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
end
## Cache
defp cache_module({server, ets}, module) do
if lock(server, module) do
cache_from_chunk(ets, module) || cache_from_info(ets, module)
unlock(server, module)
end
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"]),
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
cache_chunk(ets, module, contents.exports)
true
else
_ -> false
end
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
{mode, exports} = info_exports(module)
deprecated = info_deprecated(module)
cache_info(ets, module, exports, deprecated, mode)
else
:ets.insert(ets, {{:cached, module}, false})
end
end
defp info_exports(module) do
map =
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
{:elixir, map}
rescue
_ -> {:erlang, Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))}
end
defp info_deprecated(module) do
Map.new(module.__info__(:deprecated))
rescue
_ -> %{}
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})
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:cached, module}, mode})
end
defp cache_chunk(ets, module, exports) do
Enum.each(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:export, module, {:__info__, 1}}, :def, nil})
:ets.insert(ets, {{:cached, module}, :elixir})
end
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
defp behaviour_exports(%{is_behaviour: false}), do: []
defp behaviour_exports(module) when is_atom(module) do
if function_exported?(module, :behaviour_info, 1) do
[{{:behaviour_info, 1}, :def}]
else
[]
end
end
defp definitions_to_exports(definitions) do
Enum.flat_map(definitions, fn {function, kind, _meta, _clauses} ->
if kind in [:def, :defmacro] do
[{function, kind}]
else
[]
end
end)
end
defp lock(server, module) do
:gen_server.call(server, {:lock, module}, :infinity)
end
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module}, :infinity)
end
defp register(server, pid, ref) do
:gen_server.cast(server, {:register, pid, ref})
end
## Server callbacks
def init(schedulers) do
ets = :ets.new(__MODULE__, [:set, :public, {:read_concurrency, true}])
def init([modules, send_results, schedulers]) do
ets = :ets.new(:checker_cache, [:set, :public, {:read_concurrency, true}])
state = %{
ets: ets,
waiting: %{},
modules: [],
send_results: send_results,
modules: modules,
spawned: 0,
schedulers: schedulers || max(:erlang.system_info(:schedulers_online), 2)
schedulers: schedulers
}
{:ok, state}
end
def handle_call(:start, _from, %{ets: ets, modules: modules} = state) do
for {pid, ref} <- modules do
send(pid, {ref, :cache, ets})
end
for {_pid, ref} <- modules do
receive do
{^ref, :cached} -> :ok
end
end
{:reply, modules, run_checkers(state)}
end
def handle_call(:ets, _from, state) do
{:reply, state.ets, state}
end
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
case waiting do
%{^module => froms} ->
@@ -495,30 +240,178 @@ defmodule Module.ParallelChecker do
{:reply, :ok, %{state | waiting: waiting}}
end
def handle_call(:get_ets, _from, %{ets: ets} = state) do
{:reply, ets, state}
end
def handle_cast(:start, state) do
{:noreply, spawn_checkers(state)}
end
def handle_info({__MODULE__, :done}, state) do
state = %{state | spawned: state.spawned - 1}
{:noreply, run_checkers(state)}
if state.spawned == 0 and state.modules == [] do
{:stop, :normal, state}
else
state = spawn_checkers(state)
{:noreply, state}
end
end
def handle_info({__MODULE__, :stop}, state) do
{:stop, :normal, state}
defp lock(server, module) do
:gen_server.call(server, {:lock, module}, :infinity)
end
def handle_cast({:register, pid, ref}, %{modules: modules} = state) do
{:noreply, %{state | modules: [{pid, ref} | modules]}}
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module})
end
defp run_checkers(%{modules: []} = state) do
defp get_ets(server) do
:gen_server.call(server, :get_ets)
end
defp start(server) do
:gen_server.cast(server, :start)
end
defp preload_cache(ets, modules) do
Enum.each(modules, fn
{_module, map} when is_map(map) -> cache_from_module_map(ets, map)
{module, binary} when is_binary(binary) -> cache_from_chunk(ets, module, binary)
end)
end
defp spawn_checkers(%{modules: []} = state) do
state
end
defp run_checkers(%{spawned: spawned, schedulers: schedulers} = state)
defp spawn_checkers(%{spawned: spawned, schedulers: schedulers} = state)
when spawned >= schedulers do
state
end
defp run_checkers(%{modules: [{pid, ref} | modules]} = state) do
send(pid, {ref, :check})
run_checkers(%{state | modules: modules, spawned: state.spawned + 1})
defp spawn_checkers(%{modules: [{module, _} = verify | modules]} = state) do
parent = self()
ets = state.ets
send_results_pid = state.send_results
spawn_link(fn ->
warnings = check_module(verify, {parent, ets})
send(send_results_pid, {__MODULE__, module, warnings})
send(parent, {__MODULE__, :done})
end)
spawn_checkers(%{state | modules: modules, spawned: state.spawned + 1})
end
defp cache_module({server, ets}, module) do
if lock(server, module) do
cache_from_chunk(ets, module) || cache_from_info(ets, module)
unlock(server, module)
end
end
defp cache_from_chunk(ets, module) do
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
else
_ -> false
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)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
exports = info_exports(module)
deprecated = info_deprecated(module)
cache_info(ets, module, exports, deprecated)
else
:ets.insert(ets, {{:cached, module}, false})
end
end
defp info_exports(module) do
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
rescue
_ -> Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))
end
defp info_deprecated(module) do
Map.new(module.__info__(:deprecated))
rescue
_ -> %{}
end
defp cache_info(ets, module, exports, deprecated) do
exports =
Enum.map(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp cache_chunk(ets, module, exports) do
exports =
Enum.map(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:export, {module, :__info__, 1}}, :def, nil})
exports = [{{:__info__, 1}, :def} | exports]
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
defp behaviour_exports(%{is_behaviour: false}), do: []
defp behaviour_exports(module) when is_atom(module) do
if {:behaviour_info, 1} in module.module_info(:functions) do
[{{:behaviour_info, 1}, :def}]
else
[]
end
end
defp definitions_to_exports(definitions) do
Enum.flat_map(definitions, fn {function, kind, _meta, _clauses} ->
if kind in [:def, :defmacro] do
[{function, kind}]
else
[]
end
end)
end
end
+154 -153
View File
@@ -60,7 +60,7 @@ defmodule Module.Types do
head_stack = Unify.push_expr_stack(def_expr, stack)
with {:ok, _types, context} <- Pattern.of_head(args, guards, head_stack, context),
{:ok, _type, context} <- Expr.of_expr(body, :dynamic, stack, context) do
{:ok, _type, context} <- Expr.of_expr(body, stack, context) do
context.warnings
else
{:error, {type, error, context}} ->
@@ -92,7 +92,7 @@ defmodule Module.Types do
traces: %{},
# Counter to give type variables unique names
counter: 0,
# Track if a variable was inferred from a type guard function such is_tuple/1
# Track if a variable was infered from a type guard function such is_tuple/1
# or a guard function that fails such as elem/2, possible values are:
# `:guarded` when `is_tuple(x)`
# `:guarded` when `is_tuple and elem(x, 0)`
@@ -115,59 +115,133 @@ defmodule Module.Types do
# When false do not add a trace when a type variable is refined,
# useful when merging contexts where the variables already have traces
trace: true,
# There are two factors that control how we track guards.
#
# * consider_type_guards?: if type guards should be considered.
# This applies only at the root and root-based "and" and "or" nodes.
#
# * keep_guarded? - if a guarded clause should remain as guarded
# even on failure. Used on the right side of and.
#
type_guards: {_consider_type_guards? = true, _keep_guarded? = false},
# Track if we are in a context where type guard functions should
# affect inference
type_guards_enabled?: true,
# Context used to determine if unification is bi-directional, :expr
# is directional, :pattern is bi-directional
context: nil
}
end
## VARIABLE LIFTING
@doc """
Lifts type variables to their infered types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &do_lift_type/2)
types
end
@doc """
Lifts a single type to its infered type from the context.
"""
def lift_type(type, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{type, _context} = do_lift_type(type, context)
type
end
# Lift type variable to its infered (hopefully concrete) types from the context
defp do_lift_type({:var, var}, context) do
case Map.fetch(context.lifted_types, var) do
{:ok, lifted_var} ->
{{:var, lifted_var}, context}
:error ->
case Map.fetch(context.types, var) do
{:ok, :unbound} ->
new_lifted_var(var, context)
{:ok, type} ->
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = %{context | types: Map.delete(context.types, var)}
{type, context} = do_lift_type(type, context)
{type, %{context | types: types}}
:error ->
new_lifted_var(var, context)
end
end
end
defp do_lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
{{:tuple, n, types}, context}
end
defp do_lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = do_lift_type(key, context)
{value, context} = do_lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp do_lift_type({:list, type}, context) do
{type, context} = do_lift_type(type, context)
{{:list, type}, context}
end
defp do_lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
## ERROR TO WARNING
# Collect relevant information from context and traces to report error
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}}
traces = type_traces(stack, context)
{[signature | args], traces} = lift_all_types([signature | args], traces, context)
error = {:unable_apply, mfa, args, expected, signature, {location, stack.last_expr, traces}}
{Module.Types, error, location}
end
def error_to_warning(:unable_unify, {left, right, stack}, context) do
{fun, arity} = context.function
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)
traces = tag_traces(traces, context)
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
{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
# in the stack since we get related variables anyway?
stack =
stack.unify_stack
|> Enum.uniq()
|> Enum.flat_map(&[&1 | related_variables(&1, context.types)])
|> Enum.uniq()
Enum.flat_map(stack, fn var_index ->
with %{^var_index => traces} <- context.traces,
%{^var_index => expr_var} <- context.types_to_vars do
Enum.map(traces, &tag_trace(expr_var, &1, context))
Enum.map(traces, &{expr_var, &1})
else
_other -> []
end
@@ -185,96 +259,56 @@ defmodule Module.Types do
end
# Tag if trace is for a concrete type or type variable
defp tag_trace(var, {type, expr, location}, context) do
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
{:var, var, expr_var, expr, location}
else
_ -> {:type, var, type, expr, location}
end
end
defp lift_all_types(types, traces, context) do
trace_types = for({:type, _, type, _, _} <- traces, do: type)
{types, lift_context} = Unify.lift_types(types, context)
{trace_types, _lift_context} = Unify.lift_types(trace_types, lift_context)
{traces, []} =
Enum.map_reduce(traces, trace_types, fn
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
other, acc -> {other, acc}
end)
{types, traces}
defp tag_traces(traces, context) do
Enum.flat_map(traces, fn {var, {type, expr, location}} ->
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
[{var, {:var, expr_var, expr, location}}]
else
_ -> [{var, {:type, type, expr, location}}]
end
end)
end
## FORMAT WARNINGS
def format_warning({:unable_apply, mfa, args, expected, signature, {location, expr, traces}}) do
{original_module, original_function, arity} = mfa
{_, _, args} = mfa_or_fa = erl_to_ex(original_module, original_function, args, [])
{module, function, ^arity} = call_to_mfa(mfa_or_fa)
format_mfa = Exception.format_mfa(module, function, arity)
{traces, [] = _hints} = format_traces(traces, [], false)
clauses =
Enum.map(signature, fn {ins, out} ->
{_, _, ins} = erl_to_ex(original_module, original_function, ins, [])
{:fun, [{ins, out}]}
|> Unify.format_type(false)
|> IO.iodata_to_binary()
|> binary_slice(1..-2//1)
end)
[
"expected #{format_mfa} to have signature:\n\n ",
Enum.map_join(args, ", ", &Unify.format_type(&1, false)),
" -> #{Unify.format_type(expected, false)}",
"\n\nbut it has signature:\n\n ",
indent(Enum.join(clauses, "\n")),
"\n\n",
format_expr(expr, location),
traces,
"Conflict found at"
]
end
def format_warning({:unable_unify, left, right, {location, expr, traces}}) do
if map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) do
{:ok, atom, known_atoms} = missing_field(left, right)
cond do
map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) ->
{:ok, atom, known_atoms} = missing_field(left, right)
# Drop the last trace which is the expression map.foo
traces = Enum.drop(traces, 1)
{traces, hints} = format_traces(traces, [left, right], true)
# Drop the last trace which is the expression map.foo
traces = Enum.drop(traces, 1)
{traces, hints} = format_traces(traces, true)
[
"undefined field \"#{atom}\" ",
format_expr(expr, location),
"expected one of the following fields: ",
Enum.join(Enum.sort(known_atoms), ", "),
"\n\n",
traces,
format_message_hints(hints),
"Conflict found at"
]
else
simplify_left? = simplify_type?(left, right)
simplify_right? = simplify_type?(right, left)
[
"undefined field \"#{atom}\" ",
format_expr(expr, location),
"expected one of the following fields: ",
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
"\n\n",
traces,
format_message_hints(hints),
"Conflict found at"
]
{traces, hints} = format_traces(traces, [left, right], simplify_left? or simplify_right?)
true ->
simplify_left? = simplify_type?(left, right)
simplify_right? = simplify_type?(right, left)
[
"incompatible types:\n\n ",
Unify.format_type(left, simplify_left?),
" !~ ",
Unify.format_type(right, simplify_right?),
"\n\n",
format_expr(expr, location),
traces,
format_message_hints(hints),
"Conflict found at"
]
{traces, hints} = format_traces(traces, simplify_left? or simplify_right?)
[
"incompatible types:\n\n ",
Unify.format_type(left, simplify_left?),
" !~ ",
Unify.format_type(right, simplify_right?),
"\n\n",
format_expr(expr, location),
traces,
format_message_hints(hints),
"Conflict found at"
]
end
end
@@ -303,17 +337,16 @@ defmodule Module.Types do
end
end
defp format_traces([], _types, _simplify?) do
defp format_traces([], _simplify?) do
{[], []}
end
defp format_traces(traces, types, simplify?) do
defp format_traces(traces, simplify?) do
traces
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map_reduce([], fn
{:type, var, type, expr, location}, hints ->
{hint, hints} = format_type_hint(type, types, expr, hints)
{var, {:type, type, expr, location}}, hints ->
{hint, hints} = format_type_hint(type, expr, hints)
trace = [
"where \"",
@@ -330,7 +363,7 @@ defmodule Module.Types do
{trace, hints}
{:var, var1, var2, expr, location}, hints ->
{var1, {:var, var2, expr, location}}, hints ->
trace = [
"where \"",
Macro.to_string(var1),
@@ -358,7 +391,7 @@ defmodule Module.Types do
end
defp simplify_type?(type, other) do
map_like_type?(type) and not map_like_type?(other)
map_type?(type) and not map_type?(other)
end
## EXPRESSION FORMATTING
@@ -401,16 +434,14 @@ defmodule Module.Types do
## Hints
defp format_message_hints(hints) do
hints
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map(&[format_message_hint(&1), "\n"])
hints |> Enum.uniq() |> Enum.reverse() |> Enum.map(&format_message_hint/1)
end
defp format_message_hint(:inferred_dot) do
"""
HINT: "var.field" (without parentheses) implies "var" is a map() while \
"var.fun()" (with parentheses) implies "var" is an atom()
"""
end
@@ -420,24 +451,18 @@ defmodule Module.Types do
integer() unless said otherwise. For example, <<expr>> assumes "expr" \
is an integer. Pass a modifier, such as <<expr::float>> or <<expr::binary>>, \
to change the default behaviour.
"""
end
defp format_message_hint({:sized_and_unsize_tuples, {size, var}}) do
"""
HINT: use pattern matching or "is_tuple(#{Macro.to_string(var)}) and \
tuple_size(#{Macro.to_string(var)}) == #{size}" to guard a sized tuple.
"""
end
defp format_type_hint(type, types, expr, hints) do
case format_type_hint(type, types, expr) do
defp format_type_hint(type, expr, hints) do
case format_type_hint(type, expr) do
{message, hint} -> {message, [hint | hints]}
:error -> {[], hints}
end
end
defp format_type_hint(type, types, expr) do
defp format_type_hint(type, expr) do
cond do
dynamic_map_dot?(type, expr) ->
{" (due to calling var.field)", :inferred_dot}
@@ -448,9 +473,6 @@ defmodule Module.Types do
inferred_bitstring_spec?(type, expr) ->
{[], :inferred_bitstring_spec}
message = sized_and_unsize_tuples(expr, types) ->
{[], {:sized_and_unsize_tuples, message}}
true ->
:error
end
@@ -486,33 +508,15 @@ defmodule Module.Types do
end
end
defp sized_and_unsize_tuples({{:., _, [:erlang, :is_tuple]}, _, [var]}, types) do
case Enum.find(types, &match?({:tuple, _, _}, &1)) do
{:tuple, size, _} ->
{size, var}
nil ->
nil
end
end
defp sized_and_unsize_tuples(_expr, _types) do
nil
end
## Formatting helpers
defp indent(string) do
String.replace(string, "\n", "\n ")
String.replace(string, "\n", " \n")
end
defp map_type?({:map, _}), do: true
defp map_type?(_other), do: false
defp map_like_type?({:map, _}), do: true
defp map_like_type?({:union, union}), do: Enum.any?(union, &map_like_type?/1)
defp map_like_type?(_other), do: false
defp atom_type?(:atom), do: true
defp atom_type?({:atom, _}), do: false
defp atom_type?({:union, union}), do: Enum.all?(union, &atom_type?/1)
@@ -520,7 +524,4 @@ defmodule Module.Types do
defp integer_type?(:integer), do: true
defp integer_type?(_other), do: false
defp call_to_mfa({{:., _, [mod, fun]}, _, args}), do: {mod, fun, length(args)}
defp call_to_mfa({fun, _, args}) when is_atom(fun), do: {Kernel, fun, length(args)}
end
+106 -161
View File
@@ -4,57 +4,58 @@ defmodule Module.Types.Expr do
alias Module.Types.{Of, Pattern}
import Module.Types.{Helpers, Unify}
def of_expr(expr, expected, %{context: stack_context} = stack, context)
when stack_context != :expr do
of_expr(expr, expected, %{stack | context: :expr}, context)
def of_expr(expr, %{context: stack_context} = stack, context) when stack_context != :expr do
of_expr(expr, %{stack | context: :expr}, context)
end
# :atom
def of_expr(atom, _expected, _stack, context) when is_atom(atom) do
def of_expr(atom, _stack, context) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
def of_expr(literal, _expected, _stack, context) when is_integer(literal) do
def of_expr(literal, _stack, context) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
def of_expr(literal, _expected, _stack, context) when is_float(literal) do
def of_expr(literal, _stack, context) when is_float(literal) do
{:ok, :float, context}
end
# "..."
def of_expr(literal, _expected, _stack, context) when is_binary(literal) do
def of_expr(literal, _stack, context) when is_binary(literal) do
{:ok, :binary, context}
end
# #PID<...>
def of_expr(literal, _expected, _stack, context) when is_pid(literal) do
def of_expr(literal, _stack, context) when is_pid(literal) do
{:ok, :dynamic, context}
end
# <<...>>>
def of_expr({:<<>>, _meta, args}, _expected, stack, context) do
case Of.binary(args, stack, context, &of_expr/4) do
def of_expr({:<<>>, _meta, args}, stack, context) do
result = Of.binary(args, stack, context, &of_expr/3)
case result do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
def of_expr({:|, _meta, [left_expr, []]} = expr, _expected, stack, context) do
def of_expr({:|, _meta, [left_expr, []]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
of_expr(left_expr, :dynamic, stack, context)
of_expr(left_expr, stack, context)
end
# left | right
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_expr(left_expr, :dynamic, stack, context) do
case of_expr(left_expr, stack, context) do
{:ok, left, context} ->
case of_expr(right_expr, :dynamic, stack, context) do
case of_expr(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
@@ -71,23 +72,22 @@ defmodule Module.Types.Expr do
end
# []
def of_expr([], _expected, _stack, context) do
def of_expr([], _stack, context) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
def of_expr(exprs, _expected, stack, context) when is_list(exprs) do
def of_expr(exprs, stack, context) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# __CALLER__
def of_expr({:__CALLER__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
def of_expr({:__CALLER__, _meta, var_context}, _stack, context) when is_atom(var_context) do
struct_pair = {:required, {:atom, :__struct__}, {:atom, Macro.Env}}
pairs =
@@ -99,8 +99,7 @@ defmodule Module.Types.Expr do
end
# __STACKTRACE__
def of_expr({:__STACKTRACE__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
def of_expr({:__STACKTRACE__, _meta, var_context}, _stack, context) when is_atom(var_context) do
file = {:tuple, 2, [{:atom, :file}, {:list, :integer}]}
line = {:tuple, 2, [{:atom, :line}, :integer]}
file_line = {:list, {:union, [file, line]}}
@@ -109,152 +108,107 @@ defmodule Module.Types.Expr do
end
# var
def of_expr(var, _expected, _stack, context) when is_var(var) do
def of_expr(var, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
end
# {left, right}
def of_expr({left, right}, expected, stack, context) do
of_expr({:{}, [], [left, right]}, expected, stack, context)
def of_expr({left, right}, stack, context) do
of_expr({:{}, [], [left, right]}, stack, context)
end
# {...}
def of_expr({:{}, _meta, exprs} = expr, _expected, stack, context) do
def of_expr({:{}, _meta, exprs} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
{:error, reason} -> {:error, reason}
end
end
# left = right
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
# TODO: We might want to bring the expected type forward in case the type of this
# pattern is not useful. For example: 1 = _ = expr
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, left_type, context} <-
Pattern.of_pattern(left_expr, stack, context),
{:ok, right_type, context} <- of_expr(right_expr, left_type, stack, context),
do: unify(right_type, left_type, stack, context)
{:ok, right_type, context} <- of_expr(right_expr, stack, context),
do: unify(right_type, left_type, %{stack | context: :pattern}, context)
end
# %{map | ...}
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, _expected, stack, context) do
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, map_type, context} <- of_expr(map, map_type, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/4),
with {:ok, map_type, context} <- of_expr(map, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/3),
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
# %Struct{map | ...}
def of_expr(
{:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr,
_expected,
stack,
context
) do
def of_expr({:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, struct, context} <- Of.struct(module, meta, context),
{:ok, update, context} <- of_expr(update, map_type, stack, context) do
{:ok, update, context} <- of_expr(update, stack, context) do
unify(update, struct, stack, context)
end
end
# %{...}
def of_expr({:%{}, _meta, args} = expr, _expected, stack, context) do
def of_expr({:%{}, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
Of.closed_map(args, stack, context, &of_expr/4)
Of.closed_map(args, stack, context, &of_expr/3)
end
# %Struct{...}
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, _expected, stack, context) do
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/4) do
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/3) do
unify(map, struct, stack, context)
end
end
# ()
def of_expr({:__block__, _meta, []}, _expected, _stack, context) do
def of_expr({:__block__, _meta, []}, _stack, context) do
{:ok, {:atom, nil}, context}
end
# (expr; expr)
def of_expr({:__block__, _meta, exprs}, expected, stack, context) do
expected_types = List.duplicate(:dynamic, length(exprs) - 1) ++ [expected]
result =
map_reduce_ok(Enum.zip(exprs, expected_types), context, fn {expr, expected}, context ->
of_expr(expr, expected, stack, context)
end)
case result do
def of_expr({:__block__, _meta, exprs}, stack, context) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, expr_types, context} -> {:ok, Enum.at(expr_types, -1), context}
{:error, reason} -> {:error, reason}
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
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, _expr_type, context} <- of_expr(case_expr, :dynamic, stack, context),
with {:ok, _expr_type, context} <- of_expr(case_expr, stack, context),
{:ok, context} <- of_clauses(clauses, stack, context),
do: {:ok, :dynamic, context}
end
# fn pat -> expr end
def of_expr({:fn, _meta, clauses} = expr, _expected, stack, context) do
def of_expr({:fn, _meta, clauses} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_clauses(clauses, stack, context) do
@@ -267,7 +221,7 @@ defmodule Module.Types.Expr do
@try_clause_blocks [:catch, :else, :after]
# try do expr end
def of_expr({:try, _meta, [blocks]} = expr, _expected, stack, context) do
def of_expr({:try, _meta, [blocks]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
@@ -277,20 +231,20 @@ defmodule Module.Types.Expr do
{:->, _, [[{:in, _, [var, _exceptions]}], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
{:->, _, [[var], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
end)
{block, body}, context = acc when block in @try_blocks ->
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
@@ -305,7 +259,7 @@ defmodule Module.Types.Expr do
end
# receive do pat -> expr end
def of_expr({:receive, _meta, [blocks]} = expr, _expected, stack, context) do
def of_expr({:receive, _meta, [blocks]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
@@ -317,8 +271,8 @@ defmodule Module.Types.Expr do
of_clauses(clauses, stack, context)
{:after, [{:->, _meta, [head, body]}]}, context = acc ->
with {:ok, _type, context} <- of_expr(head, :dynamic, stack, context),
{:ok, _type, context} <- of_expr(body, :dynamic, stack, context),
with {:ok, _type, context} <- of_expr(head, stack, context),
{:ok, _type, context} <- of_expr(body, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
@@ -329,7 +283,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, stack, context) do
stack = push_expr_stack(expr, stack)
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
@@ -340,7 +294,7 @@ defmodule Module.Types.Expr do
{:ok, :dynamic, context}
end
else
with {:ok, _type, context} <- of_expr(block, :dynamic, stack, context) do
with {:ok, _type, context} <- of_expr(block, stack, context) do
{:ok, :dynamic, context}
end
end
@@ -348,7 +302,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, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
@@ -357,25 +311,29 @@ defmodule Module.Types.Expr do
end
end
# fun.(args)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
# fun.(arg)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
{:ok, _arg_types, context} <-
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, :dynamic, context}
case of_expr(fun, stack, context) do
{:ok, _fun_type, context} ->
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# expr.key_or_fun
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, _expected, stack, context)
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, stack, context)
when not is_atom(expr1) do
stack = push_expr_stack(expr2, stack)
if Keyword.get(meta2, :no_parens, false) do
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
{value_var, context} = add_var(context),
pair_type = {:required, {:atom, key_or_fun}, value_var},
optional_type = {:optional, :dynamic, :dynamic},
@@ -383,35 +341,28 @@ defmodule Module.Types.Expr do
{:ok, _map_type, context} <- unify(map_field_type, expr_type, stack, context),
do: {:ok, value_var, context}
else
# TODO: Use expected type to infer intersection return type
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
{:ok, _map_type, context} <- unify(expr_type, :atom, stack, context),
do: {:ok, :dynamic, context}
end
end
# expr.fun(arg)
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, stack, context) do
context = Of.remote(expr1, fun, length(args), meta1, context)
stack = push_expr_stack(expr2, stack)
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
{:ok, _arg_types, context} <-
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
{:ok, :dynamic, context}
with {:ok, _expr_type, context} <- of_expr(expr1, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, stack, context) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
end
end
# &Foo.bar/1
def of_expr(
{:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]},
_expected,
_stack,
context
)
def of_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, _stack, context)
when is_atom(module) and is_atom(fun) do
context = Of.remote(module, fun, arity, meta, context)
{:ok, :dynamic, context}
@@ -419,19 +370,16 @@ defmodule Module.Types.Expr do
# &foo/1
# & &1
def of_expr({:&, _meta, _arg}, _expected, _stack, context) do
def of_expr({:&, _meta, _arg}, _stack, context) do
# TODO: Function type
{:ok, :dynamic, context}
end
# fun(arg)
def of_expr({fun, _meta, args} = expr, _expected, stack, context)
when is_atom(fun) and is_list(args) do
# TODO: Use expected type to infer intersection return type
def of_expr({fun, _meta, args} = expr, stack, context) when is_atom(fun) and is_list(args) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -440,15 +388,17 @@ defmodule Module.Types.Expr do
defp for_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
defp for_clause({:<<>>, _, [{:<-, _, [pattern, expr]}]}, stack, context) do
# TODO: the compiler guarantees pattern is a binary but we need to check expr is a binary
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
@@ -457,15 +407,15 @@ defmodule Module.Types.Expr do
end
defp for_clause(expr, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:into, expr}, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:reduce, expr}, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:uniq, _}, _stack, context) do
@@ -475,8 +425,10 @@ defmodule Module.Types.Expr do
defp with_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
@@ -485,11 +437,11 @@ defmodule Module.Types.Expr do
end
defp with_clause(expr, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp with_option({:do, body}, stack, context) do
of_expr_context(body, :dynamic, stack, context)
of_expr_context(body, stack, context)
end
defp with_option({:else, clauses}, stack, context) do
@@ -497,19 +449,12 @@ defmodule Module.Types.Expr do
end
defp of_clauses(clauses, stack, context) do
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
reduce_ok(clauses, context, fn {:->, _meta, [head, body]}, context = acc ->
{patterns, guards} = extract_head(head)
case Pattern.of_head(patterns, guards, stack, context) do
{:ok, _, context} ->
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
error ->
# Skip the clause if it the head has an error
if meta[:generated], do: {:ok, acc}, else: error
end
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
{:ok, _expr_type, context} <- of_expr(body, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
end
@@ -536,8 +481,8 @@ defmodule Module.Types.Expr do
[other]
end
defp of_expr_context(expr, expected, stack, context) do
case of_expr(expr, expected, stack, context) do
defp of_expr_context(expr, stack, context) do
case of_expr(expr, stack, context) do
{:ok, _type, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
-54
View File
@@ -101,30 +101,6 @@ defmodule Module.Types.Helpers do
defp do_map_reduce_ok([], {list, acc}, _fun), do: {:ok, Enum.reverse(list), acc}
@doc """
Like `Enum.flat_map/2` but only continues while `fun` returns `{:ok, list}`
and stops on `{:error, reason}`.
"""
def flat_map_ok(list, fun) do
do_flat_map_ok(list, [], fun)
end
defp do_flat_map_ok([head | tail], acc, fun) do
case fun.(head) do
{:ok, elem} ->
do_flat_map_ok(tail, [elem | acc], fun)
{:error, reason} ->
{:error, reason}
end
end
defp do_flat_map_ok([], acc, _fun), do: {:ok, Enum.reverse(Enum.concat(acc))}
@doc """
Like `Enum.flat_map_reduce/3` but only continues while `fun` returns `{:ok, list, acc}`
and stops on `{:error, reason}`.
"""
def flat_map_reduce_ok(list, acc, fun) do
do_flat_map_reduce_ok(list, {[], acc}, fun)
end
@@ -154,10 +130,6 @@ defmodule Module.Types.Helpers do
end
end
@doc """
Combines a list of guard expressions `when x when y when z` to an expression
combined with `or`, `x or y or z`.
"""
# TODO: Remove this and let multiple when be treated as multiple clauses,
# meaning they will be intersection types
def guards_to_or([]) do
@@ -167,30 +139,4 @@ defmodule Module.Types.Helpers do
def guards_to_or(guards) do
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} end)
end
@doc """
Like `Enum.zip/1` but will zip multiple lists together instead of only two.
"""
def zip_many(lists) do
zip_many(lists, [], [[]])
end
defp zip_many([], [], [[] | acc]) do
map_reverse(acc, [], &Enum.reverse/1)
end
defp zip_many([], remain, [last | acc]) do
zip_many(Enum.reverse(remain), [], [[] | [last | acc]])
end
defp zip_many([[] | _], remain, [last | acc]) do
zip_many(Enum.reverse(remain), [], [last | acc])
end
defp zip_many([[elem | list1] | list2], remain, [last | acc]) do
zip_many(list2, [list1 | remain], [[elem | last] | acc])
end
defp map_reverse([], acc, _fun), do: acc
defp map_reverse([head | tail], acc, fun), do: map_reverse(tail, [fun.(head) | acc], fun)
end
+28 -74
View File
@@ -11,44 +11,14 @@ defmodule Module.Types.Of do
import Module.Types.Helpers
import Module.Types.Unify
# There are important assumptions on how we work with maps.
#
# First, the keys in the map must be ordered by subtyping.
#
# Second, optional keys must be a superset of the required
# keys, i.e. %{required(atom) => integer, optional(:foo) => :bar}
# is forbidden.
#
# Third, in order to preserve co/contra-variance, a supertype
# must satisfy its subtypes. I.e. %{foo: :bar, atom() => :baz}
# is forbidden, it must be %{foo: :bar, atom() => :baz | :bar}.
#
# Once we support user declared maps, we need to validate these
# assumptions.
@doc """
Handles open maps (with dynamic => dynamic).
"""
def open_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
# If we match on a map such as %{"foo" => "bar"}, we cannot
# assert that %{binary() => binary()}, since we are matching
# only a single binary of infinite possible values. Therefore,
# the correct would be to match it to %{binary() => binary() | var}.
#
# We can skip this in two cases:
#
# 1. If the key is a singleton, then we know that it has no
# other value than the current one
#
# 2. If the value is a variable, then there is no benefit in
# creating another variable, so we can skip it
#
# For now, we skip generating the var itself and introduce
# :dynamic instead.
def open_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
pairs =
for {key, value} <- pairs, not has_unbound_var?(key, context) do
if singleton?(key, context) or match?({:var, _}, value) do
if singleton?(key, context) do
{key, value}
else
{key, to_union([value, :dynamic], context)}
@@ -63,16 +33,16 @@ defmodule Module.Types.Of do
@doc """
Handles closed maps (without dynamic => dynamic).
"""
def closed_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
def closed_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
{:ok, {:map, closed_to_unions(pairs, context)}, context}
end
end
defp map_pairs(pairs, stack, context, of_fun) do
defp map_pairs(pairs, stack, context, fun) do
map_reduce_ok(pairs, context, fn {key, value}, context ->
with {:ok, key_type, context} <- of_fun.(key, :dynamic, stack, context),
{:ok, value_type, context} <- of_fun.(value, :dynamic, stack, context),
with {:ok, key_type, context} <- fun.(key, stack, context),
{:ok, value_type, context} <- fun.(value, stack, context),
do: {:ok, {key_type, value_type}, context}
end)
end
@@ -143,39 +113,39 @@ defmodule Module.Types.Of do
In the stack, we add nodes such as <<expr>>, <<..., expr>>, etc,
based on the position of the expression within the binary.
"""
def binary([], _stack, context, _of_fun) do
def binary([], _stack, context, _fun) do
{:ok, context}
end
def binary([head], stack, context, of_fun) do
def binary([head], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head]}, stack)
binary_segment(head, head_stack, context, of_fun)
binary_segment(head, head_stack, context, fun)
end
def binary([head | tail], stack, context, of_fun) do
def binary([head | tail], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head, @suffix]}, stack)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_many([last], stack, context, of_fun) do
defp binary_many([last], stack, context, fun) do
last_stack = push_expr_stack({:<<>>, get_meta(last), [@prefix, last]}, stack)
binary_segment(last, last_stack, context, of_fun)
binary_segment(last, last_stack, context, fun)
end
defp binary_many([head | tail], stack, context, of_fun) do
defp binary_many([head | tail], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [@prefix, head, @suffix]}, stack)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, of_fun) do
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, fun) do
expected_type =
collect_binary_specifier(specifiers, &binary_type(stack.context, &1)) || :integer
@@ -192,7 +162,7 @@ defmodule Module.Types.Of do
{:ok, context}
true ->
with {:ok, type, context} <- of_fun.(expr, expected_type, stack, context),
with {:ok, type, context} <- fun.(expr, stack, context),
{:ok, _type, context} <- unify(type, expected_type, stack, context),
do: {:ok, context}
end
@@ -237,7 +207,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)
@@ -249,12 +219,12 @@ defmodule Module.Types.Of do
defp check_export(module, fun, arity, meta, context) do
case ParallelChecker.fetch_export(context.cache, module, fun, arity) do
{:ok, mode, :def, reason} ->
check_deprecated(mode, module, fun, arity, reason, meta, context)
{:ok, :def, reason} ->
check_deprecated(module, fun, arity, reason, meta, context)
{:ok, mode, :defmacro, reason} ->
{:ok, :defmacro, reason} ->
context = warn(meta, context, {:unrequired_module, module, fun, arity})
check_deprecated(mode, module, fun, arity, reason, meta, context)
check_deprecated(module, fun, arity, reason, meta, context)
{:error, :module} ->
if warn_undefined?(module, fun, arity, context) do
@@ -273,7 +243,7 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(:elixir, module, fun, arity, reason, meta, context) do
defp check_deprecated(module, fun, arity, reason, meta, context) do
if reason do
warn(meta, context, {:deprecated, module, fun, arity, reason})
else
@@ -281,22 +251,6 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(:erlang, module, fun, arity, _reason, meta, context) do
case :otp_internal.obsolete(module, fun, arity) do
{:deprecated, string} when is_list(string) ->
reason = string |> List.to_string() |> String.capitalize()
warn(meta, context, {:deprecated, module, fun, arity, reason})
{:deprecated, string, removal} when is_list(string) and is_list(removal) ->
reason = string |> List.to_string() |> String.capitalize()
reason = "It will be removed in #{removal}. #{reason}"
warn(meta, context, {:deprecated, module, fun, arity, reason})
_ ->
context
end
end
# The protocol code dispatches to unknown modules, so we ignore them here.
#
# try do
@@ -327,7 +281,7 @@ defmodule Module.Types.Of do
%{context | warnings: [{__MODULE__, warning, location} | context.warnings]}
end
## Warning formatting
## Warning formating
def format_warning({:undefined_module, module, fun, arity}) do
[
+247 -449
View File
@@ -10,8 +10,8 @@ defmodule Module.Types.Pattern do
def of_head(patterns, guards, stack, context) do
with {:ok, types, context} <-
map_reduce_ok(patterns, context, &of_pattern(&1, stack, &2)),
# TODO: Check that of_guard/4 returns boolean() | :fail
{:ok, _, context} <- of_guard(guards_to_or(guards), :dynamic, stack, context),
# TODO: Check that of_guard/3 returns boolean() | :fail
{:ok, _, context} <- of_guard(guards_to_or(guards), stack, context),
do: {:ok, types, context}
end
@@ -57,9 +57,8 @@ defmodule Module.Types.Pattern do
)
when is_atom(var_context) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
{:ok, {:map, [{:required, {:atom, :__struct__}, :atom} | pairs]}, context}
end
end
@@ -68,11 +67,10 @@ defmodule Module.Types.Pattern do
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
when not is_atom(var) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {:ok, var_type, context} = of_pattern(var, stack, context),
{:ok, _, context} <- unify(var_type, :atom, stack, context),
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
{:ok, {:map, [{:required, {:atom, :__struct__}, var_type} | pairs]}, context}
end
end
@@ -81,80 +79,78 @@ defmodule Module.Types.Pattern do
of_shared(expr, stack, context, &of_pattern/3)
end
def unify_kinds(:required, _), do: :required
def unify_kinds(_, :required), do: :required
def unify_kinds(:optional, :optional), do: :optional
## GUARDS
# TODO: Some guards can be changed to intersection types or higher order types
@boolean {:union, [{:atom, true}, {:atom, false}]}
@number {:union, [:integer, :float]}
@unary_number_fun [{[:integer], :integer}, {[@number], :float}]
@binary_number_fun [
{[:integer, :integer], :integer},
{[:float, @number], :float},
{[@number, :float], :float}
]
@guard_functions %{
{:is_atom, 1} => [{[:atom], @boolean}],
{:is_binary, 1} => [{[:binary], @boolean}],
{:is_bitstring, 1} => [{[:binary], @boolean}],
{:is_boolean, 1} => [{[@boolean], @boolean}],
{:is_float, 1} => [{[:float], @boolean}],
{:is_function, 1} => [{[:fun], @boolean}],
{:is_function, 2} => [{[:fun, :integer], @boolean}],
{:is_integer, 1} => [{[:integer], @boolean}],
{:is_list, 1} => [{[{:list, :dynamic}], @boolean}],
{:is_map, 1} => [{[{:map, [{:optional, :dynamic, :dynamic}]}], @boolean}],
{:is_map_key, 2} => [{[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic}],
{:is_number, 1} => [{[@number], @boolean}],
{:is_pid, 1} => [{[:pid], @boolean}],
{:is_port, 1} => [{[:port], @boolean}],
{:is_reference, 1} => [{[:reference], @boolean}],
{:is_tuple, 1} => [{[:tuple], @boolean}],
{:<, 2} => [{[:dynamic, :dynamic], @boolean}],
{:"=<", 2} => [{[:dynamic, :dynamic], @boolean}],
{:>, 2} => [{[:dynamic, :dynamic], @boolean}],
{:>=, 2} => [{[:dynamic, :dynamic], @boolean}],
{:"/=", 2} => [{[:dynamic, :dynamic], @boolean}],
{:"=/=", 2} => [{[:dynamic, :dynamic], @boolean}],
{:==, 2} => [{[:dynamic, :dynamic], @boolean}],
{:"=:=", 2} => [{[:dynamic, :dynamic], @boolean}],
{:*, 2} => @binary_number_fun,
{:+, 1} => @unary_number_fun,
{:+, 2} => @binary_number_fun,
{:-, 1} => @unary_number_fun,
{:-, 2} => @binary_number_fun,
{:/, 2} => @binary_number_fun,
{:abs, 1} => @unary_number_fun,
{:ceil, 1} => [{[@number], :integer}],
{:floor, 1} => [{[@number], :integer}],
{:round, 1} => [{[@number], :integer}],
{:trunc, 1} => [{[@number], :integer}],
{:element, 2} => [{[:integer, :tuple], :dynamic}],
{:hd, 1} => [{[{:list, :dynamic}], :dynamic}],
{:length, 1} => [{[{:list, :dynamic}], :integer}],
{:map_get, 2} => [{[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic}],
{:map_size, 1} => [{[{:map, [{:optional, :dynamic, :dynamic}]}], :integer}],
{:tl, 1} => [{[{:list, :dynamic}], :dynamic}],
{:tuple_size, 1} => [{[:tuple], :integer}],
{:node, 1} => [{[{:union, [:pid, :reference, :port]}], :atom}],
{:binary_part, 3} => [{[:binary, :integer, :integer], :binary}],
{:bit_size, 1} => [{[:binary], :integer}],
{:byte_size, 1} => [{[:binary], :integer}],
{:size, 1} => [{[{:union, [:binary, :tuple]}], @boolean}],
{:div, 2} => [{[:integer, :integer], :integer}],
{:rem, 2} => [{[:integer, :integer], :integer}],
{:node, 0} => [{[], :atom}],
{:self, 0} => [{[], :pid}],
{:bnot, 1} => [{[:integer], :integer}],
{:band, 2} => [{[:integer, :integer], :integer}],
{:bor, 2} => [{[:integer, :integer], :integer}],
{:bxor, 2} => [{[:integer, :integer], :integer}],
{:bsl, 2} => [{[:integer, :integer], :integer}],
{:bsr, 2} => [{[:integer, :integer], :integer}],
{:or, 2} => [{[@boolean, @boolean], @boolean}],
{:and, 2} => [{[@boolean, @boolean], @boolean}],
{:xor, 2} => [{[@boolean, @boolean], @boolean}],
{:not, 1} => [{[@boolean], @boolean}]
{:is_atom, 1} => {[:atom], @boolean},
{:is_binary, 1} => {[:binary], @boolean},
{:is_bitstring, 1} => {[:binary], @boolean},
{:is_boolean, 1} => {[@boolean], @boolean},
{:is_float, 1} => {[:float], @boolean},
{:is_function, 1} => {[:fun], @boolean},
{:is_function, 2} => {[:fun, :integer], @boolean},
{:is_integer, 1} => {[:integer], @boolean},
{:is_list, 1} => {[{:list, :dynamic}], @boolean},
{:is_map, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], @boolean},
{:is_map_key, 2} => {[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic},
{:is_number, 1} => {[@number], @boolean},
{:is_pid, 1} => {[:pid], @boolean},
{:is_port, 1} => {[:port], @boolean},
{:is_reference, 1} => {[:reference], @boolean},
{:is_tuple, 1} => {[:tuple], @boolean},
{:<, 2} => {[:dynamic, :dynamic], @boolean},
{:"=<", 2} => {[:dynamic, :dynamic], @boolean},
{:>, 2} => {[:dynamic, :dynamic], @boolean},
{:>=, 2} => {[:dynamic, :dynamic], @boolean},
{:"/=", 2} => {[:dynamic, :dynamic], @boolean},
{:"=/=", 2} => {[:dynamic, :dynamic], @boolean},
{:==, 2} => {[:dynamic, :dynamic], @boolean},
{:"=:=", 2} => {[:dynamic, :dynamic], @boolean},
{:*, 2} => {[@number, @number], @number},
{:+, 1} => {[@number], @number},
{:+, 2} => {[@number, @number], @number},
{:-, 1} => {[@number], @number},
{:-, 2} => {[@number, @number], @number},
{:/, 2} => {[@number, @number], @number},
{:abs, 1} => {[@number], @number},
{:ceil, 1} => {[@number], :integer},
{:floor, 1} => {[@number], :integer},
{:round, 1} => {[@number], :integer},
{:trunc, 1} => {[@number], :integer},
{:element, 2} => {[:integer, :tuple], :dynamic},
{:hd, 1} => {[{:list, :dynamic}], :dynamic},
{:length, 1} => {[{:list, :dynamic}], :integer},
{:map_get, 2} => {[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :dynamic},
{:map_size, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], :integer},
{:tl, 1} => {[{:list, :dynamic}], :dynamic},
{:tuple_size, 1} => {[:tuple], :integer},
{:node, 1} => {[{:union, [:pid, :reference, :port]}], :atom},
{:binary_part, 3} => {[:binary, :integer, :integer], :binary},
{:bit_size, 1} => {[:binary], :integer},
{:byte_size, 1} => {[:binary], :integer},
{:size, 1} => {[{:union, [:binary, :tuple]}], @boolean},
{:div, 2} => {[:integer, :integer], :integer},
{:rem, 2} => {[:integer, :integer], :integer},
{:node, 0} => {[], :atom},
{:self, 0} => {[], :pid},
{:bnot, 1} => {[:integer], :integer},
{:band, 2} => {[:integer, :integer], :integer},
{:bor, 2} => {[:integer, :integer], :integer},
{:bxor, 2} => {[:integer, :integer], :integer},
{:bsl, 2} => {[:integer, :integer], :integer},
{:bsr, 2} => {[:integer, :integer], :integer},
{:or, 2} => {[@boolean, @boolean], @boolean},
{:and, 2} => {[@boolean, @boolean], @boolean},
{:xor, 2} => {[@boolean, @boolean], @boolean},
{:not, 1} => {[@boolean], @boolean}
# Following guards are matched explicitly to handle
# type guard functions such as is_atom/1
@@ -169,6 +165,7 @@ defmodule Module.Types.Pattern do
:is_boolean,
:is_float,
:is_function,
:is_function,
:is_integer,
:is_list,
:is_map,
@@ -183,36 +180,29 @@ defmodule Module.Types.Pattern do
Refines the type variables in the typing context using type check guards
such as `is_integer/1`.
"""
def of_guard(expr, expected, %{context: stack_context} = stack, context)
when stack_context != :pattern do
of_guard(expr, expected, %{stack | context: :pattern}, context)
def of_guard(expr, %{context: stack_context} = stack, context) when stack_context != :pattern do
of_guard(expr, %{stack | context: :pattern}, context)
end
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, _expected, stack, context) do
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
with {:ok, left_type, context} <- of_guard(left, @boolean, stack, context),
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_and(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
{:ok, right_type, context} <- of_guard(right, :dynamic, keep_guarded(stack), context),
do: {:ok, to_union([@boolean, right_type], context), context}
{:ok, _, context} <- unify(right_type, @boolean, stack, context),
do: {:ok, @boolean, context}
end
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, _expected, stack, context) do
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
left_indexes = collect_var_indexes_from_expr(left, context)
right_indexes = collect_var_indexes_from_expr(right, context)
fresh_context = fresh_context(context)
with {:ok, left_type, left_context} <- of_guard(left, @boolean, stack, context),
{:ok, _right_type, right_context} <- of_guard(right, :dynamic, stack, context),
context =
merge_context_or(
left_indexes,
right_indexes,
context,
stack,
left_context,
right_context
),
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, _right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_or(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
do: {:ok, @boolean, context}
end
@@ -220,406 +210,218 @@ defmodule Module.Types.Pattern do
# The unary operators + and - are special cased to avoid common warnings until
# we add support for intersection types for the guard functions
# -integer / +integer
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _expected, _stack, context)
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _stack, context)
when guard in [:+, :-] and is_integer(integer) do
{:ok, :integer, context}
end
# -float / +float
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _expected, _stack, context)
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _stack, context)
when guard in [:+, :-] and is_float(float) do
{:ok, :float, context}
end
# tuple_size(arg) == integer
def of_guard(
{{:., _, [:erlang, :==]}, _, [{{:., _, [:erlang, :tuple_size]}, _, [var]}, size]} = expr,
expected,
stack,
context
)
when is_var(var) and is_integer(size) do
of_tuple_size(var, size, expr, expected, stack, context)
end
# integer == tuple_size(arg)
def of_guard(
{{:., _, [:erlang, :==]}, _, [size, {{:., _, [:erlang, :tuple_size]}, _, [var]}]} = expr,
expected,
stack,
context
)
when is_var(var) and is_integer(size) do
of_tuple_size(var, size, expr, expected, stack, context)
end
# fun(args)
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, expected, stack, context) do
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{param_types, return_type} = guard_signature(guard, length(args))
type_guard? = type_guard?(guard)
{consider_type_guards?, keep_guarded?} = stack.type_guards
signature = guard_signature(guard, length(args))
# Only check type guards in the context of and/or/not,
# a type guard in the context of is_tuple(x) > :foo
# should not affect the inference of x
if not type_guard? or consider_type_guards? do
stack = push_expr_stack(expr, stack)
expected_clauses = filter_clauses(signature, expected, stack, context)
param_unions = signature_to_param_unions(expected_clauses, context)
arg_stack = %{stack | type_guards: {false, keep_guarded?}}
mfa = {:erlang, guard, length(args)}
if not type_guard? or stack.type_guards_enabled? do
arg_stack = %{stack | type_guards_enabled?: type_guard?}
with {:ok, arg_types, context} <-
map_reduce_ok(Enum.zip(args, param_unions), context, fn {arg, param}, context ->
of_guard(arg, param, arg_stack, context)
end),
{:ok, return_type, context} <-
unify_call(
arg_types,
expected_clauses,
expected,
mfa,
signature,
stack,
context,
type_guard?
) do
guard_sources = guard_sources(arg_types, type_guard?, keep_guarded?, context)
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
{:ok, context} <- unify_call(arg_types, param_types, stack, context) do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources =
Map.update(context.guard_sources, index, [:guarded], &[:guarded | &1])
{rest_arg_types, guard_sources}
_ ->
{arg_types, context.guard_sources}
end
guard_sources =
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, [:fail], &[:fail | &1])
_, guard_sources ->
guard_sources
end)
{:ok, return_type, %{context | guard_sources: guard_sources}}
end
else
# Assume that type guards always return boolean
boolean = {:union, [atom: true, atom: false]}
[{_params, ^boolean}] = signature
{:ok, boolean, context}
{:ok, return_type, context}
end
end
# map.field
def of_guard({{:., meta1, [map, field]}, meta2, []}, expected, stack, context) do
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, expected, stack, context)
def of_guard({{:., meta1, [map, field]}, meta2, []}, stack, context) do
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, stack, context)
end
# var
def of_guard(var, _expected, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
def of_guard(var, _stack, context) when is_var(var) do
type = Map.fetch!(context.vars, var_name(var))
{:ok, type, context}
end
def of_guard(expr, _expected, stack, context) do
of_shared(expr, stack, context, &of_guard(&1, :dynamic, &2, &3))
def of_guard(expr, stack, context) do
of_shared(expr, stack, context, &of_guard/3)
end
defp of_tuple_size(var, size, expr, _expected, stack, context) do
{consider_type_guards?, _keep_guarded?} = stack.type_guards
defp fresh_context(context) do
types = Map.new(context.types, fn {var, _} -> {var, :unbound} end)
traces = Map.new(context.traces, fn {var, _} -> {var, []} end)
%{context | types: types, traces: traces}
end
result =
if consider_type_guards? do
stack = push_expr_stack(expr, stack)
tuple_elems = Enum.map(1..size//1, fn _ -> :dynamic end)
defp unify_call(args, params, stack, context) do
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
case unify(arg, param, stack, context) do
{:ok, _, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
end
with {:ok, type, context} <- of_guard(var, :dynamic, stack, context),
{:ok, _type, context} <- unify({:tuple, size, tuple_elems}, type, stack, context),
do: {:ok, context}
else
defp merge_context_and(context, stack, left, right) do
with {:ok, context} <- unify_new_types(context, stack, left),
{:ok, context} <- unify_new_types(context, stack, right) do
guard_sources = and_guard_sources(left.guard_sources, right.guard_sources)
guard_sources = merge_guard_sources([context.guard_sources, guard_sources])
{:ok, %{context | guard_sources: guard_sources}}
end
end
defp unify_new_types(context, stack, new_context) do
context = merge_traces(context, new_context)
reduce_ok(Map.to_list(new_context.types), context, fn
{_index, :unbound}, context ->
{:ok, context}
end
case result do
{:ok, context} ->
boolean = {:union, [atom: true, atom: false]}
{:ok, boolean, context}
{index, new_type}, context ->
case unify({:var, index}, new_type, %{stack | trace: false}, context) do
{:ok, _, context} ->
{:ok, context}
{:error, reason} ->
{:error, reason}
end
end
defp signature_to_param_unions(signature, context) do
signature
|> Enum.map(fn {params, _return} -> params end)
|> zip_many()
|> Enum.map(&to_union(&1, context))
end
# Collect guard sources from argument types, see type context documentation
# for more information
defp guard_sources(arg_types, type_guard?, keep_guarded?, context) do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
{rest_arg_types, guard_sources}
_ ->
{arg_types, context.guard_sources}
end
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
_, guard_sources ->
guard_sources
end)
end
defp collect_var_indexes_from_expr(expr, context) do
{_, vars} =
Macro.prewalk(expr, %{}, fn
var, acc when is_var(var) ->
var_name = var_name(var)
%{^var_name => type} = context.vars
{var, collect_var_indexes(type, context, acc)}
other, acc ->
{other, acc}
end)
Map.keys(vars)
end
defp unify_call(args, clauses, _expected, _mfa, _signature, stack, context, true = _type_guard?) do
unify_type_guard_call(args, clauses, stack, context)
end
defp unify_call(args, clauses, expected, mfa, signature, stack, context, false = _type_guard?) do
unify_call(args, clauses, expected, mfa, signature, stack, context)
end
defp unify_call([], [{[], return}], _expected, _mfa, _signature, _stack, context) do
{:ok, return, context}
end
defp unify_call(args, clauses, expected, mfa, signature, stack, context) do
# Given the arguments:
# foo | bar, {:ok, baz | bat}
# Expand unions in arguments:
# foo | bar, {:ok, baz} | {:ok, bat}
# Permute arguments:
# foo, {:ok, baz}
# foo, {:ok, bat}
# bar, {:ok, baz}
# bar, {:ok, bat}
flatten_args = Enum.map(args, &flatten_union(&1, context))
cartesian_args = cartesian_product(flatten_args)
# Remove clauses that do not match the expected type
# Ignore type variables in parameters by changing them to dynamic
clauses =
clauses
|> filter_clauses(expected, stack, context)
|> Enum.map(fn {params, return} ->
{Enum.map(params, &var_to_dynamic/1), return}
end)
# For each permuted argument find the clauses they match
# All arguments must match at least one clause, but all clauses
# do not need to match
# Collect the return values from clauses that matched and collect
# the type contexts from unifying argument and parameter to
# infer type variables in arguments
result =
flat_map_ok(cartesian_args, fn cartesian_args ->
result =
Enum.flat_map(clauses, fn {params, return} ->
result =
map_ok(Enum.zip(cartesian_args, params), fn {arg, param} ->
case unify(arg, param, stack, context) do
{:ok, _type, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
case result do
{:ok, contexts} -> [{return, contexts}]
{:error, _reason} -> []
end
end)
if result != [] do
{:ok, result}
else
{:error, args}
{:error, reason} ->
{:error, reason}
end
end)
case result do
{:ok, returns_contexts} ->
{success_returns, contexts} = Enum.unzip(returns_contexts)
contexts = Enum.concat(contexts)
indexes =
for types <- flatten_args,
type <- types,
index <- collect_var_indexes_from_type(type),
do: index,
uniq: true
# Build unions from collected type contexts to unify with
# type variables from arguments
result =
map_reduce_ok(indexes, context, fn index, context ->
union =
contexts
|> Enum.map(&Map.fetch!(&1.types, index))
|> Enum.reject(&(&1 == :unbound))
if union == [] do
{:ok, {:var, index}, context}
else
unify({:var, index}, to_union(union, context), stack, context)
end
end)
case result do
{:ok, _types, context} -> {:ok, to_union(success_returns, context), context}
{:error, reason} -> {:error, reason}
end
{:error, args} ->
error(:unable_apply, {mfa, args, expected, signature, stack}, context)
end
end
defp unify_type_guard_call(args, [{params, return}], stack, context) do
result =
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
case unify(arg, param, stack, context) do
{:ok, _, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
case result do
{:ok, context} -> {:ok, return, context}
{:error, reason} -> {:error, reason}
end
end
defp cartesian_product(lists) do
List.foldr(lists, [[]], fn list, acc ->
for elem_list <- list,
list_acc <- acc,
do: [elem_list | list_acc]
end)
end
defp var_to_dynamic(type) do
{type, _acc} =
walk(type, :ok, fn
{:var, _index}, :ok ->
{:dynamic, :ok}
other, :ok ->
{other, :ok}
end)
type
end
defp collect_var_indexes_from_type(type) do
{_type, indexes} =
walk(type, [], fn
{:var, index}, indexes ->
{{:var, index}, [index | indexes]}
other, indexes ->
{other, indexes}
end)
indexes
end
defp merge_context_or(left_indexes, right_indexes, context, stack, left, right) do
left_different = filter_different_indexes(left_indexes, left, right)
right_different = filter_different_indexes(right_indexes, left, right)
case {left_different, right_different} do
{[index], [index]} -> merge_context_or_equal(index, stack, left, right)
{_, _} -> merge_context_or_diff(left_different, context, left)
end
end
defp filter_different_indexes(indexes, left, right) do
Enum.filter(indexes, fn index ->
%{^index => left_type} = left.types
%{^index => right_type} = right.types
left_type != right_type
end)
end
defp merge_context_or_equal(index, stack, left, right) do
%{^index => left_type} = left.types
%{^index => right_type} = right.types
cond do
left_type == :unbound ->
refine_var!(index, right_type, stack, left)
right_type == :unbound ->
left
true ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
if left.guard_sources[index] == :fail do
guard_sources = Map.put(left.guard_sources, index, :fail)
left = %{left | guard_sources: guard_sources}
refine_var!(index, left_type, stack, left)
else
guard_sources = merge_guard_sources([left.guard_sources, right.guard_sources])
left = %{left | guard_sources: guard_sources}
refine_var!(index, to_union([left_type, right_type], left), stack, left)
end
end
end
# If the variable failed, we can keep them from the left side as is.
# If they didn't fail, then we need to restore them to their original value.
defp merge_context_or_diff(indexes, old_context, new_context) do
Enum.reduce(indexes, new_context, fn index, context ->
if new_context.guard_sources[index] == :fail do
context
else
restore_var!(index, new_context, old_context)
end
end)
end
defp merge_guard_sources(sources) do
Enum.reduce(sources, fn left, right ->
Map.merge(left, right, fn
_index, :guarded, :guarded -> :guarded
_index, _, _ -> :fail
end)
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
end)
end
defp guarded_if_keep_guarded(:guarded, true), do: :guarded
defp guarded_if_keep_guarded(_, _), do: :fail
defp join_guard_source(left, right) do
sources = left ++ right
defp keep_guarded(%{type_guards: {consider?, _}} = stack),
do: %{stack | type_guards: {consider?, true}}
cond do
:fail in sources -> [:fail]
:guarded in sources -> [:guarded]
true -> []
end
end
defp filter_clauses(signature, expected, stack, context) do
Enum.filter(signature, fn {_params, return} ->
match?({:ok, _type, _context}, unify(return, expected, stack, context))
defp and_guard_sources(left, right) do
Map.merge(left, right, fn _index, left, right ->
# When the failing guard function wont fail due to type check function before it,
# for example: is_list(x) and length(x)
if :guarded in left and :fail in right do
[:guarded]
else
join_guard_source(left, right)
end
end)
end
Enum.each(@guard_functions, fn {{name, arity}, signature} ->
defp guard_signature(unquote(name), unquote(arity)), do: unquote(Macro.escape(signature))
end)
defp merge_traces(context, new_context) do
traces =
:maps.fold(
fn index, new_traces, traces ->
:maps.update_with(index, &(new_traces ++ &1), new_traces, traces)
end,
context.traces,
new_context.traces
)
Enum.each(@type_guards, fn name ->
defp type_guard?(unquote(name)), do: true
end)
%{context | traces: traces}
end
defp type_guard?(name) when is_atom(name), do: false
defp merge_context_or(context, stack, left, right) do
context =
case {Map.to_list(left.types), Map.to_list(right.types)} do
{[{index, :unbound}], [{index, type}]} ->
refine_var(index, type, stack, context)
{[{index, type}], [{index, :unbound}]} ->
refine_var(index, type, stack, context)
{[{index, left_type}], [{index, right_type}]} ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources = Map.put(context.guard_sources, index, [:fail])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, to_union([left_type, right_type], context), stack, context)
end
{left_types, _right_types} ->
Enum.reduce(left_types, context, fn {index, left_type}, context ->
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
context
end
end)
end
{:ok, context}
end
defp guard_signature(name, arity) do
Map.fetch!(@guard_functions, {name, arity})
end
defp type_guard?(name) do
name in @type_guards
end
## Shared
@@ -645,9 +447,7 @@ defmodule Module.Types.Pattern do
# <<...>>>
defp of_shared({:<<>>, _meta, args}, stack, context, fun) do
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
case Of.binary(args, stack, context, expected_fun) do
case Of.binary(args, stack, context, fun) do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
@@ -741,18 +541,16 @@ defmodule Module.Types.Pattern do
# %{...}
defp of_shared({:%{}, _meta, args} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
Of.open_map(args, stack, context, expected_fun)
Of.open_map(args, stack, context, fun)
end
# %Struct{...}
defp of_shared({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context, fun)
when is_atom(module) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, map, context} <- Of.open_map(args, stack, context, fun) do
unify(map, struct, stack, context)
end
end
+103 -392
View File
@@ -10,7 +10,6 @@ defmodule Module.Types.Unify do
# {:atom, atom} < :atom
# :integer
# :float
# :binary
# :pid
# :port
# :reference
@@ -21,37 +20,65 @@ defmodule Module.Types.Unify do
# {:tuple, size, [type]} < :tuple
# {:union, [type]}
# {:map, [{:required | :optional, key_type, value_type}]}
# {:fun, [{params, return}]}
#
# Once new types are added, they should be considered in:
#
# * unify (all)
# * format_type (all)
# * subtype? (subtypes only)
# * has_unbound_var? (composite only)
# * recursive_type? (composite only)
# * collect_var_indexes (composite only)
# * lift_types (composite only)
# * flatten_union (composite only)
# * walk (composite only)
#
@doc """
Unifies two types and returns the unified type and an updated typing context
or an error in case of a typing conflict.
"""
def unify(same, same, _stack, context) do
def unify(source, target, stack, context) do
case do_unify(source, target, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, reason} ->
if stack.context == :pattern do
case do_unify(target, source, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, _} ->
{:error, reason}
end
else
{:error, reason}
end
end
end
defp do_unify(same, same, _stack, context) do
{:ok, same, context}
end
def unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
defp do_unify(type, {:var, var}, stack, context) do
case context.types do
%{^var => {:var, var_type}} ->
do_unify(type, {:var, var_type}, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = false)
end
end
def unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
defp do_unify({:var, var}, type, stack, context) do
case context.types do
%{^var => {:var, var_type}} ->
do_unify({:var, var_type}, type, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = true)
end
end
def unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
defp do_unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
result =
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
unify(source, target, stack, context)
@@ -63,41 +90,33 @@ defmodule Module.Types.Unify do
end
end
def unify({:list, source}, {:list, target}, stack, context) do
defp do_unify({:list, source}, {:list, target}, stack, context) do
case unify(source, target, stack, context) do
{:ok, type, context} -> {:ok, {:list, type}, context}
{:error, reason} -> {:error, reason}
end
end
def unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
defp do_unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
unify_maps(source_pairs, target_pairs, stack, context)
end
def unify(source, :dynamic, _stack, context) do
defp do_unify(source, :dynamic, _stack, context) do
{:ok, source, context}
end
def unify(:dynamic, target, _stack, context) do
defp do_unify(:dynamic, target, _stack, context) do
{:ok, target, context}
end
def unify({:union, types}, target, stack, context) do
unify_result =
map_reduce_ok(types, context, fn type, context ->
unify(type, target, stack, context)
end)
case unify_result do
{:ok, types, context} -> {:ok, to_union(types, context), context}
{:error, context} -> {:error, context}
end
end
def unify(source, target, stack, context) do
defp do_unify(source, target, stack, context) do
cond do
# TODO: This condition exists to handle unions with unbound vars.
match?({:union, _}, target) and has_unbound_var?(target, context) ->
# This condition exists to handle unions with unbound vars.
# TODO: handle unions properly. Note we can easily unify
# "union < type" even if union has vars as the vars must be
# type
(match?({:union, _}, source) and has_unbound_var?(source, context)) or
(match?({:union, _}, target) and has_unbound_var?(target, context)) ->
{:ok, source, context}
subtype?(source, target, context) ->
@@ -108,14 +127,14 @@ defmodule Module.Types.Unify do
end
end
def unify_var(var, :dynamic, _stack, context, _var_source?) do
defp unify_var(var, :dynamic, _stack, context, _var_source?) do
{:ok, {:var, var}, context}
end
def unify_var(var, type, stack, context, var_source?) do
defp unify_var(var, type, stack, context, var_source?) do
case context.types do
%{^var => :unbound} ->
context = refine_var!(var, type, stack, context)
context = refine_var(var, type, stack, context)
stack = push_unify_stack(var, stack)
if recursive_type?(type, [], context) do
@@ -128,11 +147,6 @@ 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_type} ->
# Only add trace if the variable wasn't already "expanded"
context =
@@ -156,7 +170,7 @@ defmodule Module.Types.Unify do
{:ok, {:var, var}, context}
{:ok, res_type, context} ->
context = refine_var!(var, res_type, stack, context)
context = refine_var(var, res_type, stack, context)
{:ok, {:var, var}, context}
{:error, reason} ->
@@ -168,7 +182,7 @@ defmodule Module.Types.Unify do
# * All required keys on each side need to match to the other side.
# * All optional keys on each side that do not match must be discarded.
def unify_maps(source_pairs, target_pairs, stack, context) do
defp unify_maps(source_pairs, target_pairs, stack, context) do
{source_required, source_optional} = split_pairs(source_pairs)
{target_required, target_optional} = split_pairs(target_pairs)
@@ -199,7 +213,7 @@ defmodule Module.Types.Unify do
end
end
def unify_source_required(source_required, target_pairs, stack, context) do
defp unify_source_required(source_required, target_pairs, stack, context) do
map_reduce_ok(source_required, context, fn {source_key, source_value}, context ->
Enum.find_value(target_pairs, fn {target_kind, target_key, target_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -219,7 +233,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_target_required(target_required, source_pairs, stack, context) do
defp unify_target_required(target_required, source_pairs, stack, context) do
map_reduce_ok(target_required, context, fn {target_key, target_value}, context ->
Enum.find_value(source_pairs, fn {source_kind, source_key, source_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -239,7 +253,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_source_optional(source_optional, target_optional, stack, context) do
defp unify_source_optional(source_optional, target_optional, stack, context) do
flat_map_reduce_ok(source_optional, context, fn {source_key, source_value}, context ->
Enum.find_value(target_optional, fn {target_key, target_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -259,7 +273,7 @@ defmodule Module.Types.Unify do
end)
end
def unify_target_optional(target_optional, source_optional, stack, context) do
defp unify_target_optional(target_optional, source_optional, stack, context) do
flat_map_reduce_ok(target_optional, context, fn {target_key, target_value}, context ->
Enum.find_value(source_optional, fn {source_key, source_value} ->
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
@@ -288,7 +302,7 @@ defmodule Module.Types.Unify do
{required, optional}
end
def error(type, reason, context), do: {:error, {type, reason, context}}
defp error(type, reason, context), do: {:error, {type, reason, context}}
@doc """
Push expression to stack.
@@ -359,11 +373,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
@@ -392,21 +406,10 @@ defmodule Module.Types.Unify do
%{stack | unify_stack: [var | stack.unify_stack]}
end
@doc """
Restores the variable information from the old context into new context.
"""
def restore_var!(var, new_context, old_context) do
%{^var => type} = old_context.types
%{^var => trace} = old_context.traces
types = Map.put(new_context.types, var, type)
traces = Map.put(new_context.traces, var, trace)
%{new_context | types: types, traces: traces}
end
@doc """
Set the type for a variable and add trace.
"""
def refine_var!(var, type, stack, context) do
def refine_var(var, type, stack, context) do
types = Map.put(context.types, var, type)
context = %{context | types: types}
trace_var(var, type, stack, context)
@@ -468,68 +471,37 @@ defmodule Module.Types.Unify do
end)
end
defp recursive_type?({:fun, clauses}, parents, context) do
Enum.any?(clauses, fn {args, return} ->
Enum.any?([return | args], &recursive_type?(&1, [clauses | parents], context))
end)
end
defp recursive_type?(_other, _parents, _context) do
false
end
@doc """
Collects all type vars recursively.
"""
def collect_var_indexes(type, context, acc \\ %{}) do
{_type, indexes} =
walk(type, acc, fn
{:var, var}, acc ->
case acc do
%{^var => _} ->
{{:var, var}, acc}
%{} ->
case context.types do
%{^var => :unbound} ->
{{:var, var}, Map.put(acc, var, true)}
%{^var => type} ->
{{:var, var}, collect_var_indexes(type, context, Map.put(acc, var, true))}
end
end
other, acc ->
{other, acc}
end)
indexes
end
@doc """
Checks if the type has a type var.
"""
def has_unbound_var?(type, context) do
walk(type, :ok, fn
{:var, var}, acc ->
case context.types do
%{^var => :unbound} ->
throw(:has_unbound_var?)
%{^var => type} ->
has_unbound_var?(type, context)
{{:var, var}, acc}
end
other, acc ->
{other, acc}
end)
false
catch
:throw, :has_unbound_var? -> true
def has_unbound_var?({:var, var}, context) do
case context.types do
%{^var => :unbound} -> true
%{^var => type} -> has_unbound_var?(type, context)
end
end
def has_unbound_var?({:tuple, _, args}, context),
do: Enum.any?(args, &has_unbound_var?(&1, context))
def has_unbound_var?({:union, args}, context),
do: Enum.any?(args, &has_unbound_var?(&1, context))
def has_unbound_var?({:list, arg}, context),
do: has_unbound_var?(arg, context)
def has_unbound_var?({:map, pairs}, context) do
Enum.any?(pairs, fn {_, key, value} ->
has_unbound_var?(key, context) or has_unbound_var?(value, context)
end)
end
def has_unbound_var?(_type, _context), do: false
@doc """
Returns true if it is a singleton type.
@@ -584,7 +556,7 @@ defmodule Module.Types.Unify do
def subtype?({:tuple, n, left_types}, {:tuple, n, right_types}, context) do
left_types
|> Enum.zip(right_types)
|> Enum.all?(fn {left, right} -> subtype?(left, right, context) end)
|> Enum.any?(fn {left, right} -> subtype?(left, right, context) end)
end
def subtype?({:map, left_pairs}, {:map, right_pairs}, context) do
@@ -628,15 +600,17 @@ defmodule Module.Types.Unify do
def to_union([type], _context), do: type
def to_union(types, context) when types != [] do
case unique_super_types(unnest_unions(types), context) do
flat_types = flatten_union(types)
case unique_super_types(flat_types, context) do
[type] -> type
types -> {:union, types}
end
end
defp unnest_unions(types) do
defp flatten_union(types) do
Enum.flat_map(types, fn
{:union, types} -> unnest_unions(types)
{:union, types} -> flatten_union(types)
type -> [type]
end)
end
@@ -661,204 +635,6 @@ defmodule Module.Types.Unify do
[]
end
## Type lifting
@doc """
Lifts type variables to their inferred types from the context.
"""
def lift_types(types, %{lifted_types: _} = context) do
Enum.map_reduce(types, context, &lift_type/2)
end
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
Enum.map_reduce(types, context, &lift_type/2)
end
# Lift type variable to its inferred (hopefully concrete) types from the context
defp lift_type({:var, var}, context) do
case context.lifted_types do
%{^var => lifted_var} ->
{{:var, lifted_var}, context}
%{} ->
case context.types do
%{^var => :unbound} ->
new_lifted_var(var, context)
%{^var => type} ->
if recursive_type?(type, [], context) do
new_lifted_var(var, context)
else
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = put_in(context.types[var], :unbound)
{type, context} = lift_type(type, context)
{type, %{context | types: types}}
end
%{} ->
new_lifted_var(var, context)
end
end
end
defp lift_type({:union, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:union, types}, context}
end
defp lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:tuple, n, types}, context}
end
defp lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = lift_type(key, context)
{value, context} = lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp lift_type({:list, type}, context) do
{type, context} = lift_type(type, context)
{{:list, type}, context}
end
defp lift_type({:fun, clauses}, context) do
clauses =
Enum.map_reduce(clauses, context, fn {args, return}, context ->
{[return | args], context} = Enum.map_reduce([return | args], context, &lift_type/2)
{{args, return}, context}
end)
{{:fun, clauses}, context}
end
defp lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
# TODO: Figure out function expansion
@doc """
Expand unions so that all unions are at the top level.
{integer() | float()} => {integer()} | {float()}
"""
def flatten_union({:union, types}, context) do
Enum.flat_map(types, &flatten_union(&1, context))
end
def flatten_union(type, context) do
List.wrap(do_flatten_union(type, context))
end
def do_flatten_union({:tuple, num, types}, context) do
flatten_union_tuple(types, num, context, [])
end
def do_flatten_union({:list, type}, context) do
case do_flatten_union(type, context) do
{:union, union_types} -> Enum.map(union_types, &{:list, &1})
_type -> [{:list, type}]
end
end
def do_flatten_union({:map, pairs}, context) do
flatten_union_map(pairs, context, [])
end
def do_flatten_union({:var, var}, context) do
if looping_var?(var, context, []) do
{:var, var}
else
case context.types do
%{^var => :unbound} -> {:var, var}
%{^var => {:union, types}} -> Enum.map(types, &do_flatten_union(&1, context))
%{^var => type} -> do_flatten_union(type, context)
end
end
end
def do_flatten_union(type, _context) do
type
end
defp flatten_union_tuple([type | types], num, context, acc) do
case do_flatten_union(type, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_tuple(types, num, context, [&1 | acc]))
type ->
flatten_union_tuple(types, num, context, [type | acc])
end
end
defp flatten_union_tuple([], num, _context, acc) do
[{:tuple, num, Enum.reverse(acc)}]
end
defp flatten_union_map([{kind, key, value} | pairs], context, acc) do
case do_flatten_union(key, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_map_value(kind, &1, value, pairs, context, acc))
type ->
flatten_union_map_value(kind, type, value, pairs, context, acc)
end
end
defp flatten_union_map([], _context, acc) do
[{:map, Enum.reverse(acc)}]
end
defp flatten_union_map_value(kind, key, value, pairs, context, acc) do
case do_flatten_union(value, context) do
{:union, union_types} ->
Enum.flat_map(union_types, &flatten_union_map(pairs, context, [{kind, key, &1} | acc]))
value ->
flatten_union_map(pairs, context, [{kind, key, value} | acc])
end
end
defp looping_var?(var, context, parents) do
case context.types do
%{^var => :unbound} ->
false
%{^var => {:var, type}} ->
if var in parents do
true
else
looping_var?(type, context, [var | parents])
end
%{^var => _type} ->
false
end
end
@doc """
Formats types.
@@ -868,7 +644,7 @@ defmodule Module.Types.Unify do
def format_type({:map, pairs}, true) do
case List.keyfind(pairs, {:atom, :__struct__}, 1) do
{:required, {:atom, :__struct__}, {:atom, struct}} ->
["%", inspect(struct), "{}"]
"%#{inspect(struct)}{}"
_ ->
"map()"
@@ -876,31 +652,24 @@ defmodule Module.Types.Unify do
end
def format_type({:union, types}, simplify?) do
types
|> Enum.map(&format_type(&1, simplify?))
|> Enum.intersperse(" | ")
"#{Enum.map_join(types, " | ", &format_type(&1, simplify?))}"
end
def format_type({:tuple, _, types}, simplify?) do
format =
types
|> Enum.map(&format_type(&1, simplify?))
|> Enum.intersperse(", ")
["{", format, "}"]
"{#{Enum.map_join(types, ", ", &format_type(&1, simplify?))}}"
end
def format_type({:list, type}, simplify?) do
["[", format_type(type, simplify?), "]"]
"[#{format_type(type, simplify?)}]"
end
def format_type({:map, pairs}, false) do
case List.keytake(pairs, {:atom, :__struct__}, 1) do
{{:required, {:atom, :__struct__}, {:atom, struct}}, pairs} ->
["%", inspect(struct), "{", format_map_pairs(pairs), "}"]
"%#{inspect(struct)}{#{format_map_pairs(pairs)}}"
_ ->
["%{", format_map_pairs(pairs), "}"]
"%{#{format_map_pairs(pairs)}}"
end
end
@@ -909,84 +678,26 @@ defmodule Module.Types.Unify do
end
def format_type({:var, index}, _simplify?) do
["var", Integer.to_string(index + 1)]
end
def format_type({:fun, clauses}, simplify?) do
format =
Enum.map(clauses, fn {params, return} ->
params = Enum.intersperse(Enum.map(params, &format_type(&1, simplify?)), ", ")
params = if params == [], do: params, else: [params, " "]
return = format_type(return, simplify?)
[params, "-> ", return]
end)
["(", Enum.intersperse(format, "; "), ")"]
"var#{index}"
end
def format_type(atom, _simplify?) when is_atom(atom) do
[Atom.to_string(atom), "()"]
"#{atom}()"
end
defp format_map_pairs(pairs) do
{atoms, others} = Enum.split_with(pairs, &match?({:required, {:atom, _}, _}, &1))
{required, optional} = Enum.split_with(others, &match?({:required, _, _}, &1))
(atoms ++ required ++ optional)
|> Enum.map(fn
Enum.map_join(atoms ++ required ++ optional, ", ", fn
{:required, {:atom, atom}, right} ->
[Atom.to_string(atom), ": ", format_type(right, false)]
"#{atom}: #{format_type(right, false)}"
{:required, left, right} ->
[format_type(left, false), " => ", format_type(right, false)]
"#{format_type(left, false)} => #{format_type(right, false)}"
{:optional, left, right} ->
["optional(", format_type(left, false), ") => ", format_type(right, false)]
"optional(#{format_type(left, false)}) => #{format_type(right, false)}"
end)
|> Enum.intersperse(", ")
end
@doc """
Performs a depth-first, pre-order traversal of the type tree using an accumulator.
"""
def walk({:map, pairs}, acc, fun) do
{pairs, acc} =
Enum.map_reduce(pairs, acc, fn {kind, key, value}, acc ->
{key, acc} = walk(key, acc, fun)
{value, acc} = walk(value, acc, fun)
{{kind, key, value}, acc}
end)
fun.({:map, pairs}, acc)
end
def walk({:union, types}, acc, fun) do
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
fun.({:union, types}, acc)
end
def walk({:tuple, num, types}, acc, fun) do
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
fun.({:tuple, num, types}, acc)
end
def walk({:list, type}, acc, fun) do
{type, acc} = walk(type, acc, fun)
fun.({:list, type}, acc)
end
def walk({:fun, clauses}, acc, fun) do
{clauses, acc} =
Enum.map_reduce(clauses, acc, fn {params, return}, acc ->
{params, acc} = Enum.map_reduce(params, acc, &walk(&1, &2, fun))
{return, acc} = walk(return, acc, fun)
{{params, return}, acc}
end)
fun.({:fun, clauses}, acc)
end
def walk(type, acc, fun) do
fun.(type, acc)
end
end
-30
View File
@@ -257,36 +257,6 @@ defmodule Node do
:erlang.spawn_link(node, module, fun, args)
end
@doc """
Spawns the given function on a node, monitors it and returns its PID
and monitoring reference.
This functionality was added on Erlang/OTP 23. Using this function to
communicate with nodes running on earlier versions will fail.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, (() -> any)) :: {pid, reference}
def spawn_monitor(node, fun) do
:erlang.spawn_monitor(node, fun)
end
@doc """
Spawns the given module and function passing the given args on a node,
monitors it and returns its PID and monitoring reference.
This functionality was added on Erlang/OTP 23. Using this function
to communicate with nodes running on earlier versions will fail.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, module, atom, [any]) :: {pid, reference}
def spawn_monitor(node, module, fun, args) do
:erlang.spawn_monitor(node, module, fun, args)
end
@doc """
Sets the magic cookie of `node` to the atom `cookie`.

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