Compare commits

...
688 Commits
Author SHA1 Message Date
José Valim 185eeec5ec Release v1.14.0-rc.1 2022-08-15 12:10:24 +02:00
José Valim 2ab108d1c5 Do not set step to nil in ranges
Closes #12076
2022-08-15 10:07:32 +02:00
José Valim ef6a888b87 Update CHANGELOG 2022-08-13 11:54:30 +02:00
José Valim 407391d13a Fix loop while unifying type variables, closes #11658 2022-08-13 11:48:09 +02:00
José Valim 08d588dd9e Continue parsing after --no-pry, closes #12072 2022-08-12 22:39:07 +02:00
José Valim 42ecc84e87 Expand pipelines on right-hand side of |>, closes #12070 2022-08-12 17:21:34 +02:00
Wojtek Mach c457405d2c Mix.install: Add :config_path and :lockfile options (#12051) 2022-08-12 16:16:00 +02:00
Wojtek Mach c63cc10f58 URI.append_query/2: Handle trailing ampersand (#12069) 2022-08-12 16:15:51 +02:00
José Valim 78ba7aaeb9 Run full load paths in mix format 2022-08-11 20:11:35 +02:00
José Valim 30cb58e4c6 Handle sigils in IEx help 2022-08-10 17:52:38 +02:00
Thanabodee Charoenpiriyakij bf639d2871 Fix unclosed backquotes in Code.Fragment and Macro (#12061) 2022-08-08 18:07:58 +02:00
José Valim 777a11de36 Raise specific error for missing env 2022-08-04 19:24:09 +02:00
sabiwara 3dd54ad3f3 Fixing edge cases in Enum.slide/3 (#12052)
* Fix bug in Enum.slide/3: empty lists

* Fix edge case in Enum.slide/3
2022-08-04 16:34:29 +02:00
Andrea Leopardi 24daffbf7b Add guards to Calendar.put_time_zone_database/1 (#12049) 2022-08-04 16:33:46 +02:00
sabiwara 189d61b756 Add spec for Enum.slide/3 (#12050) 2022-08-04 16:33:36 +02:00
sabiwara 185689a8b1 Fix infinite loop in Enum.take/2 (#12048) 2022-08-04 09:12:04 +02:00
sabiwara 75540648d9 Fix bugs in binary_slice/2 (#12045) 2022-08-03 13:43:37 +02:00
sabiwara e81e9f79bf Fix bug: Enum.slice selecting extra element (#12043) 2022-08-02 18:27:59 +02:00
sabiwara db7c28f0e7 Fix bug in Enum.slice with step>1 (#12042) 2022-08-02 15:59:53 +02:00
sabiwara f5810492b0 Fix bug in Enum.drop/2 (#12040) 2022-08-02 10:47:38 +02:00
Łukasz Samson 6c068176d4 Emit consistent position meta on fn capture traces (#12033) 2022-08-02 09:46:10 +02:00
José Valim 1faf846555 Allow dry-run of notify workflow 2022-08-01 22:43:55 +02:00
José Valim 1ecef6c2be Fix secrets syntax 2022-08-01 22:34:21 +02:00
José Valim 6449239782 Release v1.14.0-rc.0 2022-08-01 16:02:56 +02:00
José Valim 025e29a9ca Release v1.14.0-rc.0 2022-08-01 15:47:49 +02:00
José Valim be9ae0e994 Fix deprecation table rendering, closes #12034 2022-08-01 12:06:11 +02:00
José Valim c82ad63895 Only add if_undefined: :apply if necessary 2022-07-31 12:12:34 +02:00
José Valim d71de51332 Allow key-deletions and atom keys in System.put_env/1 2022-07-30 18:22:09 +02:00
José Valim e15388b207 Do not leak body inference in cond, closes #12024 2022-07-28 12:08:09 +02:00
Michał Łępicki 4dece6c412 Keep documented return values after changing Mix.State to use :ets (#12017) 2022-07-28 08:35:31 +02:00
José Valim 05b17afc13 Use :gen.reply/2 to leverage aliases 2022-07-27 16:03:39 +02:00
José Valim 8d4b991c71 Branch out v1.14 2022-07-27 12:50:39 +02:00
José Valim 2f15ba4858 Commit pending test file 2022-07-27 12:50:28 +02:00
José Valim 7f8e8b65d4 Lock mix compile.elixir to avoid concurrent usage, closes #12013 2022-07-27 12:38:45 +02:00
José Valim 7cc21eaf88 Convert Mix.State to a process and use better storage
* Use ETS instead of going through an Agent

* Use persistent_term for long term storage
2022-07-27 10:26:54 +02:00
Michał Łępicki 06f5f31391 Fix passing of File.cp_r! on_conflict option (#12015) 2022-07-27 08:42:55 +02:00
sabiwara fc7d8de987 Remove dialyzer workarounds (#12014) 2022-07-27 08:25:13 +02:00
José Valim c47b731f75 Track all arities in imports inside quotes, closes #11651 2022-07-26 21:36:30 +02:00
Vitor Cavalcanti fd1f1c6fd3 Add Access.slice/1 (#12008) 2022-07-26 21:31:37 +02:00
José Valim b8590946b6 Update CHANGELOG 2022-07-26 15:28:48 +02:00
José Valim 820ba1aea2 Update CHANGELOG 2022-07-26 15:20:32 +02:00
José Valim e453a7547f Add dereference_symlinks to File.cp* (#12011) 2022-07-26 15:02:56 +02:00
José Valim 5d8d3698e4 Use tags instead of conditionals 2022-07-26 13:02:23 +02:00
José Valim f324750097 Use describe blocks in file_test.exs 2022-07-26 13:01:43 +02:00
sabiwara 1184fcaad4 Improve error messages for dot syntax (#12003) 2022-07-26 12:20:20 +02:00
José Valim e7bdf1ded5 Update TODOs and deprecations 2022-07-25 16:41:23 +02:00
Alex Pounds 5a3233cc84 Typo fix for Kernel.in/2 documentation. (#12010) 2022-07-25 15:50:44 +02:00
José Valim 23ab633864 Add TODO related to ... 2022-07-25 09:31:21 +02:00
Andrea Leopardi eead8deeab Add Kernel.dbg/0 which debugs binding/0 (#12009) 2022-07-25 08:59:46 +02:00
sabiwara 3ae82c9914 Case insensitive module resolution (#12007) 2022-07-24 17:33:45 +02:00
José Valim 8d3cceb8f7 Simplify and optimize fragment multi-line handling
Only convert lines to charlists when necessary
and avoid doing multiple passes on the data.
2022-07-24 17:13:06 +02:00
Łukasz Samson 0b668afd9c Make Code.Fragment functions work across newlines (#11980)
Closes #11877
2022-07-24 13:48:03 +02:00
Wojtek Mach 2c639a2f5a Return exit code in elixir.bat (#12005) 2022-07-22 21:13:47 +02:00
Wojtek Mach c6ac72c349 Escape $(MAKE) path (#12004)
This is particularly useful on Windows where make could be under for
example:

    C:\Program Files (x86)\GnuWin32\bin\make.exe
2022-07-22 18:56:06 +02:00
Uzo 42d3ce219f A better description for Enum module (#12002) 2022-07-22 13:31:12 +02:00
Bráulio Bezerra 9be08c8ac8 Fix link to typespecs page (#11999) 2022-07-21 12:25:46 +02:00
José Valim 562cce88a1 Ensure IO.warn in @after_verify are stored 2022-07-21 10:05:08 +02:00
José Valim c22c909228 Better docs for dbg/pry 2022-07-20 22:56:31 +02:00
José Valim 4056be1d4f Add @after_verify callback
@after_verify hooks are invoked right after the current module is verified for
undefined functions, deprecations, etc. A module is always verified after
it is compiled. In Mix projects, a module is also verified when any of its
runtime dependencies change. Therefore this is useful to perform verification
of the current module while avoiding compile-time dependencies.

Here are some sample use cases:

  * Ecto can use this validate associations consistently and effectively
  * Phoenix can use this to verify routes
  * Surface can use this to verify component attributes
2022-07-20 16:34:15 +02:00
Andrea LeopardiandJosé Valim 85d5dc5793 Add CHANGELOG entry for dbg (#11998)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-07-20 08:56:41 +02:00
José Valim bd828a0a48 Bring back formatter pruning 2022-07-20 08:45:20 +02:00
José Valim 1d2ba1c95f Reenable formatters after disabled run 2022-07-20 08:30:46 +02:00
sabiwara 0cec016972 Fix @type for Regex, add doctest example (#11996) 2022-07-19 08:30:00 +02:00
Andrea LeopardiandJosé Valim 95dcce01c5 Support pipelines in the IEx.Pry dbg/2 backend (#11994)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-07-18 22:55:45 +02:00
Quentin Crain ac558a3447 Update type spec and tests for Regex.opts 2022-07-18 21:37:34 +02:00
José Valim ff116c4d18 Make sure release mode configuration cascades (#11990)
Closes #11815.
2022-07-18 19:41:51 +02:00
aifrak c22e5d59a1 Improve documentation and examples about modifiers (#11993) 2022-07-18 11:54:48 +02:00
José Valim cbfd641dc5 Share syntax colors between IEx and dbg (#11992) 2022-07-18 11:54:05 +02:00
José Valim 59fa777187 Use fetch_env! for timezone database
The config is defined in app env and therefore should always be set.
2022-07-18 10:50:18 +02:00
José Valim 1ab3147967 Allow LINE to be set for tests 2022-07-18 09:58:14 +02:00
sabiwara a31c23e7cf Show list opts when inspecting Regex (#11991) 2022-07-18 09:04:05 +02:00
Andrea Leopardi 2e1381d430 Add basic IEx dbg callback (#11984) 2022-07-17 10:50:09 +02:00
José Valim ffa6bd844e Track dbg callback inside Mix projects 2022-07-16 20:48:16 +02:00
José Valim 869815c3ff Ensure semantic recompilation cascades to path dependencies
Prior to this patch, path dependencies would not detect
that dependencies changed, because only the parent lock
file was modified, never the child one. This patch makes
it so the parent configuration always cascades to non-
fetchable dependencies.

closes #11818
2022-07-16 16:43:24 +02:00
José Valim 613a93a17d Optimize variable loading in eval 2022-07-15 11:41:47 +02:00
Chris Wögi d4041687a8 Fix docs reference of stream/0 and binstream/0 (#11986) 2022-07-14 17:12:58 +02:00
José Valim 562113720c Lazily expand module attributes to avoid compile-time deps
Before this patch, this code would add a compile-time
dependency to Bar and Baz:

    defmodule Foo do
      @mods [Bar, Baz]
      def example(arg) when arg in @mods, do: arg
    end

That's because Bar and Baz were read in the module body,
even though ultimately they are only expanded and used
inside a function.

This patch postpones the expansion of aliases until they
are used. So if `@mods` is only used inside functions,
no compile-time deps are added.

Closes #11714.
2022-07-14 16:36:43 +02:00
José Valim 0722edd85c Fix old error assertion 2022-07-13 19:59:39 +02:00
José Valim 4d35b7ce32 Do not assert order on struct fields 2022-07-13 19:34:05 +02:00
Michał Łępicki 23e3a791c7 Fix specs in IO.ANSI (#11983) 2022-07-13 10:12:32 +02:00
Andrea Leopardi 2c61d41d9b Fix a couple of small bugs in Macro.dbg/3 (#11982) 2022-07-12 23:14:36 +02:00
José Valim 55eee6723b Add line-by-line evaluation of IEx breakpoints 2022-07-12 17:16:19 +02:00
José Valim 156f88fe11 Simplify passing of values to the checker 2022-07-12 12:55:48 +02:00
José Valim 8ad6933c58 Start checker processes lazily 2022-07-12 12:13:04 +02:00
aifrak 8c43741b9c Add example to Enum.chunk_every/4 and Stream.chunk_every/4 (#11981)
* Add example to Enum.chunk_every/4

* Add example to Stream.chunk_every/4
2022-07-12 11:19:24 +02:00
Andrea LeopardiandJosé Valim a8bf3498d9 First implementation of Kernel.dbg/2 (#11974)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-07-12 09:32:31 +02:00
Andrea Leopardi 4e572d53a8 Add specs to some functions in IO.ANSI (#11978) 2022-07-12 08:24:18 +02:00
José Valim 0e5ceb343e Do not reset the lexical tracker 2022-07-11 21:31:47 +02:00
aifrak ed01637ec2 Add example with nested arrays to Enum.join/2 (#11979) 2022-07-11 21:30:47 +02:00
José Valim f11b6756c8 Fix bootstrap issues 2022-07-11 17:00:17 +02:00
José Valim 7a90dbf9d8 Automatically cascade generated: true on macro expansion, closes #11976 2022-07-11 16:40:26 +02:00
José Valim 6106a37d02 Treat requires from local macros as compile-time deps 2022-07-11 13:17:01 +02:00
sabiwara 195d7cd12d Have File.rename/2 and File.copy/2 check path types (#11977)
* File.rename/2 checks path type

* File.copy/2 checks path type
2022-07-11 12:20:41 +02:00
José Valim d5a2233cc4 Undeprecate Macro.unpipe/2 2022-07-11 09:09:54 +02:00
José Valim 1a947e4a78 Register functions used in local macros as exports
Ideally we want to list them as requires but we don't have
the infrastructure to do so. So meanwhile, we list them
as exports, which is the same level used by require.
2022-07-11 09:09:54 +02:00
José Valim 71754b2e4d Use Macro.expand_literal/2 were possible 2022-07-11 09:09:54 +02:00
Thiago Majesk Goulart f847a3687b Update DynSup reference in supervisor docs (#11973) 2022-07-09 21:58:36 +02:00
José Valim fe05a2080d Update CHANGELOG 2022-07-09 19:04:39 +02:00
José Valim f49b3cbec3 Add autocompletion of binary modifiers in IEx
Macro.path/2 was added as a helper function for
traversing expressions and returning their context.
2022-07-09 17:06:04 +02:00
José Valim 897e64f5e5 Do not start apps on install with runtime: false, closes #11969 2022-07-09 12:38:00 +02:00
sabiwara beb77c4f3f Fix exports/1 in IEx for long fun names (#11972) 2022-07-09 12:07:57 +02:00
Thiago Majesk Goulart bbb7734e77 Clarify one sentence about DynamicSupervisor (#11970) 2022-07-09 10:30:20 +02:00
José Valim 8640def5c8 Improve docs to container_cursor_to_quoted 2022-07-08 13:44:23 +02:00
José Valim 828c6026a8 Remove compile-time dependency from defimpl :for, closes #11706 2022-07-08 13:34:09 +02:00
José Valim 5f061a9a54 Improve DynSup docs 2022-07-08 13:34:09 +02:00
José Valim eab57eed72 Improve docs 2022-07-08 13:34:09 +02:00
José Valim 9cb8a7606b Use only major-minor on Rebar dir (#11966) 2022-07-06 23:23:50 +02:00
José Valim 2325732525 Do not emit already consolidated warnings during mix xref trace, closes #11899 2022-07-06 11:05:42 +02:00
José Valim 564b69175e Compile if mix format plugins are missing, closes #11915 2022-07-06 10:51:43 +02:00
José Valim 835ed015d2 Include reason on Code.LoadError 2022-07-05 23:13:35 +02:00
José Valim 05a6eb3e19 Keep file env consistently as an absolute path, closes #11964 2022-07-05 23:04:03 +02:00
José Valim cadd15cb41 Include column information on error diagnostics when possible, closes #11964 2022-07-05 22:49:53 +02:00
sabiwara f2afd5e8d2 Fix bug in fn defaults (#11963)
Closes #11961.
2022-07-05 16:02:01 +02:00
Łukasz Samson aed16f0502 Improve diagnostic and compile typespec (#11962) 2022-07-05 12:10:15 +02:00
Fernando Tapia Rico 2239594fb3 Remove unneeded assert from with_io/1 example (#11960) 2022-07-03 22:52:28 +02:00
José Valim cdf4605c59 Improve Macro.pipe/3 docs 2022-07-01 20:08:02 +02:00
José Valim 263fb53200 Do not eagerly unpipe 2022-07-01 18:22:11 +02:00
José Valim bd32818a20 Update CHANGELOG 2022-07-01 11:57:50 +02:00
Marc-André Lafortune a9a8ed9572 Add mix xref graph --group option (#11943) 2022-07-01 11:51:39 +02:00
Andrea Leopardi e920303919 Add doc since to MapSet.symmetric_difference/2 (#11952) 2022-06-30 18:40:45 +02:00
felipe stival b93aadb578 Correct typespec for MapSet.symmetric_difference/2 (#11954) 2022-06-30 16:44:13 +02:00
José Valim 51884ca511 Fix regression on identifier\\ being interpreted as a single identifier 2022-06-30 15:52:18 +02:00
LJZN 35d78d69bc Delete never matched clause in string_io (#11953) 2022-06-30 13:21:12 +02:00
José Valim 9065de4fdf Remove warnings from match? docs 2022-06-30 07:25:19 +02:00
José Valim 24d58c891e Rename MapSet.disjoint to MapSet.symmetric_difference 2022-06-30 07:18:36 +02:00
Diogo Scudelletti 7c4c63cbce Add disjoint function to MapSet (#11951) 2022-06-29 14:44:30 +02:00
Nathan Long 5eba03abb7 Document that Kernel.match?/2 cannot take a variable pattern (#11950) 2022-06-28 22:01:25 +02:00
João Bernardo 12116a52be Fix Date.Range.first_in_iso_days/last_in_iso_days typespec (#11948)
first_in_iso_days and last_in_iso_days are both integers. This commit
updates the Date.Range typespec to reflect that.
2022-06-28 09:29:42 +02:00
Adam Millerchip 0f50338132 Map docs: update syntax is not for atom keys only (#11938) 2022-06-28 08:56:26 +02:00
Evelyn 71136dceb1 Include backslash, space, and null byte in string escape documentation (#11945) 2022-06-27 00:18:34 +02:00
Ben W 079a1ead89 Small changes to 1.14-dev CHANGELOG.md (#11944) 2022-06-26 15:48:51 +02:00
kenny-evitt 6d388f3568 Describe return value of Stream.each/2 (#11941) 2022-06-25 19:57:03 +02:00
Andrea LeopardiandJosé Valim 583e7eafde Improve docs for IO.binread/2 (#11940)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-06-25 18:42:26 +02:00
Marc-André Lafortune 02a83fcc02 Make mix xref graph --exclude raise error for files that are not found (#11937)
Previously this would be silently ignored
2022-06-25 11:17:39 +02:00
José Valim 9c03933a1d mix format 2022-06-25 11:17:05 +02:00
José Valim 7128b104e9 Add literal_encoder option to container_cursor_to_quoted 2022-06-25 11:11:49 +02:00
José Valim 4367e6cf38 Implement Enumerable.count/1 for lists 2022-06-24 14:06:15 +02:00
sabiwara b37129aef3 Fix dialyzer warnings in ExUnit assertions (#11930)
Close #11869
2022-06-23 17:55:02 +02:00
Andrea Leopardi bfbe2c23e5 Improve docs and specs around custom log formatting (#11935) 2022-06-23 17:32:38 +02:00
Vicente Merloandantedeguemon 0e2f29417a Removes duplicated formatter call (#11934)
Co-authored-by: antedeguemon <antedeguemon@users.noreply.github.com>
2022-06-23 09:04:10 +02:00
initialCCC f66220a6cf Add IO.Stream fields typespec (#11933) 2022-06-22 18:38:07 +02:00
Benjamin Milde 7f21a73f53 Let :runtime_config_path accept false to skip the runtime.exs (#11932) 2022-06-21 20:01:10 +02:00
LJZN 990d76f1a7 Fix ExUnit assertion when operator is overriden (#11929)
Closes #11885
2022-06-20 16:52:06 +02:00
José Valim 1dab8314aa Note IEx' recompile does not include deps, closes #11894 2022-06-19 23:15:08 +02:00
José Valim 9fbee88a0e Remove outdated TODO 2022-06-19 23:00:10 +02:00
José Valim 12c5b20278 Make Base encoding/decoding 100%/25% faster respectively
decode16, in particular, is 50% faster, as we don't need to
handle padding.

We also reduce the .beam size from 330kb to 80kb.

This is achieved by building look tables, as described in
https://www.erlang.org/blog/type-based-optimizations-in-the-jit/

For decoding, we build a sparse lookup table with nils in it.
The tables are still small (less than 80 elements) and we have
to check for nils, but that's more efficient than the previous
approach as well.

The code is also simpler, as we rely less on macros
and more on dynamic code generation.

Closes #11866.
2022-06-19 16:15:29 +02:00
Łukasz Samson a9f8595931 Raise when underlying erlang function doesn't return boolean (#11928)
This gives us better coverage over the response values,
even though they may not necessarily happen.
2022-06-18 23:34:00 +02:00
Łukasz Samson 4badb5c406 Add missing return types (#11927)
* Add missing nil to Module.get_definition typespec

* Add missing t to Agebra.nest typespec
2022-06-18 22:39:52 +02:00
José Valim 1477429f09 Add examples to break 2022-06-18 17:27:36 +02:00
José Valim befb5617f7 Dont change the exception type 2022-06-17 10:28:21 +02:00
Dimitris Zorbas 22e504ff5c Display friendly error when test name is too long (#11844) (#11864)
This commit changes ExUnit.Case to show a more specific error message
when the test name is too long that would cause SystemLimitError.
2022-06-17 10:26:18 +02:00
Łukasz Samson 86d4587425 Add support for __MODULE__ in Code.Fragment (#11920)
Fixes #11875
2022-06-17 10:14:51 +02:00
Eksperimental 83fafbab77 Correct use of "who" (#11925) 2022-06-16 08:21:24 +02:00
José Valim d12326ed5d Add Application.compile_env/4 and Application.compile_env!/3 2022-06-15 21:13:38 +02:00
José Valim 220ac01085 Wrap more of the struct expansion stacktrace 2022-06-15 20:56:00 +02:00
José Valim 396f148835 Fix inspection of Macro.Env 2022-06-15 19:29:27 +02:00
Alexandre Hamez ad8ce854e5 Reorder format_status documentation (#11924)
The list of different invokation cases was separated from
the sentence that introduces those cases.
2022-06-15 16:04:23 +02:00
Matt Glover 293ada3baf Improve ExUnit's diff output for pinned struct match failures (#11923)
* Tests for expected assert output on pinned struct match failure

ExUnit's diff formatter raises when a match fails between a struct and
non-struct type when pins are involved. The error is:
```
 ** (FunctionClauseError) no function clause matching in Macro.inspect_atom/2

     The following arguments were given to Macro.inspect_atom/2:

         # 1
         :literal

         # 2
         {:^, [line: 300], [{:expected_module, [line: 300], nil}]}

     Attempted function clauses (showing 4 out of 4):

         def inspect_atom(:literal, atom) when atom == nil or is_boolean(atom)
         def inspect_atom(:literal, atom) when is_atom(atom)
         def inspect_atom(:key, atom) when is_atom(atom)
         def inspect_atom(:remote_call, atom) when is_atom(atom)
```

The desired behavior respects the pin operator rather than trying to
treat it as if it were a literal.

* ExUnit diff algebra improved pinned struct type support

Ensure that the diff algebra generation process can handle a case where
the left element has a pinned struct type and the right element is not a
struct. In this case render the pin info and display the pin variable
in the test output.

This prevents ExUnit's diff process from raising for asserts like:
```
assert %^type{} = nil
assert %^type{} = :not_a_struct
```
2022-06-14 13:39:01 +02:00
José Valim 141996e61f Add day/hour/minute on add/diff in Calendar 2022-06-14 12:59:48 +02:00
José Valim 140673018b Improve calendar docs 2022-06-13 22:48:18 +02:00
Eksperimental fd822ff0f3 Replace misspelt :nillify_clauses option with :skip_clauses in Module.get_definition/3 (#11921)
Now it returns an empty list, instead of nil.

Closes: https://github.com/elixir-lang/elixir/issues/11918
2022-06-12 21:22:47 +02:00
José Valim d1dd9c912b Add note on dot in code fragment 2022-06-12 13:35:04 +02:00
Eksperimental 27a276e454 Replace misspelt helper nillify with nilify (#11919) 2022-06-11 18:06:46 +02:00
José Valim a59146d14d Fix example in overridables_in 2022-06-11 18:04:13 +02:00
José Valim fa7b6a874a Move custom formatting docs to Logger.Formatter 2022-06-11 09:32:44 +02:00
Michał Łępicki 76febeabca Change MapSet.t definition to make Dialyzer happy (#11917) 2022-06-11 09:10:45 +02:00
Sam Aaron c12f6c1b40 Teach elixir.bat ELIXIR_CLI_DRY_RUN (#11916)
The *nix equivalent shell script echoes the full erl.exe path and args to stdout when the environment variable ELIXIR_CLI_DRY_RUN is set instead of executing it.

This patch adds this behaviour to the Windows bat file for consistency.
2022-06-11 00:42:12 +02:00
Victor Rodrigues 888eba76ee Add documentation about using guards for map fields (#11913) 2022-06-09 13:43:53 +01:00
José Valim 9bb1202138 Update instructions for Map.map/2 deprecation, closes #11912 2022-06-08 11:09:47 +02:00
Łukasz Samson 173c15b299 Return keywords in Code.Fragment.surround_context (#11911)
Fixes #11889
2022-06-07 22:00:02 +02:00
Łukasz Samson f5c0cfc574 Make Code.Fragment.surround_context behave consistently with windows line endings (#11910) 2022-06-07 21:30:00 +02:00
dependabot[bot] 4c0a788337 Bump actions/cache from 2 to 3 (#11908)
Bumps [actions/cache](https://github.com/actions/cache) from 2 to 3.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-07 10:05:22 +02:00
dependabot[bot] f32c2a5aa1 Bump actions/checkout from 2 to 3 (#11909)
Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-07 10:02:48 +02:00
Naveen bbb492bc10 chore: Included githubactions in the dependabot config (#11907)
This should help with keeping the GitHub actions updated on new releases. This will also help with keeping it secure.

Dependabot helps in keeping the supply chain secure https://docs.github.com/en/code-security/dependabot

GitHub actions up to date https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot

https://github.com/ossf/scorecard/blob/main/docs/checks.md#dependency-update-tool
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
2022-06-07 09:00:56 +02:00
Naveen 91d345acd6 chore: Set permissions for GitHub actions (#11906)
Restrict the GitHub token permissions only to the required ones; this way, even if the attackers will succeed in compromising your workflow, they won’t be able to do much.

- Included permissions for the action. https://github.com/ossf/scorecard/blob/main/docs/checks.md#token-permissions

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions

https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs

[Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests](https://securitylab.github.com/research/github-actions-preventing-pwn-requests/)

Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
2022-06-06 10:10:56 +02:00
José Valim b35174e107 Add Float.min_finite/0 and Float.max_finite/0 2022-06-06 09:15:54 +02:00
José Valim 7d62286287 Improvements to CHANGELOG 2022-06-04 17:25:10 +02:00
Eksperimental 0ca5802526 GitHub Template: Separate paragraphs when rendered (#11904)
Two new lines are interpreted as a paragraph break.
2022-06-04 09:26:52 +02:00
José Valim f51a2399ba Update CHANGELOG 2022-06-03 23:40:44 +02:00
José Valim 8fad15023b Improve Inspect for structs (#11897)
* Inspect in the defstruct order
* Mark optional fields that match the default value when deriving
* Add __info__(:struct) with metadata
2022-06-03 23:32:17 +02:00
Andrea Leopardi 3aa5d2b876 Add remaining info for 1.14 CHANGELOG (#11896) 2022-06-03 23:24:31 +02:00
Eksperimental 98343d0e9c Improve Github issue template (#11902) 2022-06-03 19:59:24 +02:00
Udo Kramer a930ab3248 Fix typos (#11901) 2022-06-03 11:14:21 +02:00
José Valim 2a4ea70104 Show stacktrace if everything is pruned instead 2022-06-03 10:44:15 +02:00
Melody Horn 5273f7a86e Fix typo in the docs for URI.new!/1 (#11898) 2022-06-03 09:11:08 +02:00
Eksperimental 8d3e8a5a2d Rename section to "Report an issue" in GH template (#11900) 2022-06-03 09:10:29 +02:00
José Valim 43f2b447ff Remove periods for consistency 2022-06-02 20:59:51 +02:00
Yordis Prieto 18f473dd51 Add structure issue templates (#11893) 2022-06-02 20:55:05 +02:00
Andrea LeopardiandJosé Valim 74bee7385b Add PartitionSupervisor section to the changelog (#11892)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-06-02 13:08:34 +02:00
Michał DolataandAndrea Leopardi 695e45c84f Small fix in docs for Application.ensure_loaded/1 (#11895)
Co-authored-by: Andrea Leopardi <an.leopardi@gmail.com>
2022-06-02 12:30:12 +02:00
José Valim d588716d9f Improve docs for RELEASE_MODE 2022-06-01 11:53:36 +02:00
Eksperimental 4c800c85cf Make documentation for hd/1 and tl/1 more beginner friendly (#11891)
It took the inspiration from the Erlang docs for tl/1
https://www.erlang.org/doc/man/erlang.html#tl-1
2022-05-31 18:56:58 +02:00
Ryan Winchester 01361af9fe Add typespec :delayed_write to File.stream! modes (#11890) 2022-05-31 17:29:45 +02:00
José Valim f7e5887ac0 Add mix eval (#11861) 2022-05-31 14:52:41 +02:00
Andrea Leopardi 682239bbdb Improve docs for ExUnit.CaseTemplate (#11888) 2022-05-31 11:27:17 +02:00
Andrea Leopardi 22be3c9842 Validate argument passed to "use ExUnit.Case" (#11887)
The argument passed to "use ExUnit.Case" must be a list of options,
but we were not enforcing this anywhere. This could result in
hard-to-debug errors.

With this commit, we'll now check said argument with Keyword.keyword?/1
and potentially raise an ArgumentError.
2022-05-31 11:13:59 +02:00
Eksperimental 26e4624a5e Correct specs semantics for hd/1 and tl/1 (#11884)
Replace usage of tails in t:nonempty_maybe_improper_list/2,
use "last" instead.
2022-05-30 12:40:05 +02:00
José Valim 38d76d452b Skip docs on secondary bootstrap (#11883)
Docs will be compiled on the next pass.
2022-05-30 12:19:36 +02:00
José Valim e2f5884a01 Rescue errors from formatting, closes #11880 2022-05-30 10:53:44 +02:00
Ignacio Aguirrezabal 1f6ac2d2c7 Add example for Macro.traverse/4 docs (#11881) 2022-05-30 10:14:11 +02:00
Ignacio Aguirrezabal 3176951bbb Update prewalk and postwalk documentation (#11874) 2022-05-29 11:28:19 +02:00
Eksperimental 33018dcb1e Minor grammar fix (#11879) 2022-05-29 11:07:52 +02:00
José Valim 1e5932bd39 Improvements to Code module 2022-05-27 11:04:55 +03:00
José Valim fe20dd9639 Docs improvements to Macro 2022-05-27 10:58:49 +03:00
Gabriel Pereira df2e451ca9 Mix.Dep.Converger now tells which deps formed a cycle (#11873) 2022-05-26 18:52:37 +02:00
Michał Łępicki cd8da51fba Fix Supervisor.init/2 return type in spec (#11871) 2022-05-25 08:35:39 +02:00
Eksperimental 9a4d10e702 Introduce t:Supervisor.sup_flags/0 + Streamline specs (#11865)
Introduces t:Supervisor.sup_flags/0, and strealines the functions in
DynamicSupervisor and Supervisor that use Erlang typespecs.
2022-05-24 07:52:04 +02:00
Łukasz Samson 106225374e Improve position typespec (#11868)
As documented position 0 means unknown
2022-05-23 19:04:51 +02:00
José Valim 5deafbdc89 Normalize unicode characters once in tokenizer 2022-05-23 12:14:39 +03:00
José Valim f431aefcac Do not attempt to suggest starting tokens
It is hard to suggest because it is not possible to
know if the user intended an operator, alias, or
identifier, so we can give incorrect feedback.
2022-05-23 12:14:39 +03:00
Łukasz Samson 638b427477 Improve Code.Fragment.surround_context spec (#11867)
* Add missing options to Code.Fragment.surround_context spec

* Add a note about a difference between cursor_context and surround_context
2022-05-23 11:09:58 +02:00
Luc Fueston e7001455d7 nfc and additional normalizations for identifiers (#11859)
* nfc by default
* suggest nfkc on unexpected tokens when nfkc would have worked
* support additional normalizations (just micro/mu for now)
* parser, formatter tests
* tweak Macro.inner_classify(atom) not to rely on not_nfc causing error
2022-05-23 09:44:44 +02:00
José Valim b456befb4d Improvements to PartitionSupervisor
* Make sure all Registry lookups are indexed
* Compute the :id based on :via, similar to DynamicSupervisor
* Invoke `GenServer.whereis/1` only once per lookup
* Make sure partitions returns noproc exits on invalid args
2022-05-22 23:30:55 +03:00
Parker Selbert 713ce1d5f8 Allow any valid name for PartitionSupervisor (#11860)
The PartitionSupervisor used a named ETS table to manage partitions and as a
result it required the name to be an atom. Only using atom names is
limiting because it prevents the use of global or via tuples for
registration.

This removes the `:named_table` option from a PartitionSupervisor's ETS
table and instead stores a reference to the table with
`Registry`. Without the named table requirement it's possible to
register with any viable local supervisor name.
2022-05-22 21:51:25 +02:00
José Valim 0d37cc2fd8 Allow test modules to be compiled 2022-05-21 10:20:27 +03:00
José Valim c99025f88c Run Erlang/OTP 25 on CI (#11858) 2022-05-21 08:23:08 +02:00
Eksperimental f4fc493f61 Remove explicit reference to Kernel.SpecialForms.* (#11851)
As the are automatically linked by current ExDoc version
2022-05-21 08:22:14 +02:00
Eksperimental 4c909a1a6c Removes/Adds qualified calls to Kernel (#11852) 2022-05-21 08:20:30 +02:00
Eksperimental f68d2cf581 Copy-edit Supervisor docs + New types (#11849) 2022-05-21 08:17:29 +02:00
Dave Lucia 676725670b Update PartitionSupervisor implementation notes (#11853)
If the routing may change, we should **not** rely on that exact implementation
2022-05-21 08:16:22 +02:00
Eksperimental 0e5720a145 Fix missing arity in some functions (#11854) 2022-05-21 08:16:00 +02:00
Eksperimental 9ed22fbbf8 Manually link to functions with multiple arities (#11855) 2022-05-21 08:15:41 +02:00
Eksperimental 3d76d190dc Replace default sorter in Enum.sort_by/3 with :asc (#11856)
Enum.sort_by/3 works faster with :asc than with &<=/2.
The previous default could go from 1.52x to 2.21x slower; same goes for memory usage.

The benchmark:
https://github.com/eksperimental/benchmark/blob/enum_sort_by/benchmarks/enum_sort_by_default_sorter.exs

The benchmark results:
https://gist.github.com/eksperimental/f16e6df01e193cf075157a932bcd573c

Closes: https://github.com/elixir-lang/elixir/issues/11808
2022-05-21 08:15:11 +02:00
Eksperimental 442412929f Fix function link in docs for the Code module (#11850) 2022-05-20 08:45:25 +02:00
Eksperimental 1b9d2b5f05 Use zero-arity instead of 0-arity and so on (#11848) 2022-05-20 08:37:37 +03:00
Andrea Leopardi f4f71cd80c Add examples to Enum.slice/2 docs (#11847) 2022-05-19 23:07:19 +02:00
José Valim ce9f43b4e1 Consider steps when slicing a list forward 2022-05-19 17:17:38 +03:00
Andrea leopardi 76fd2f0cb7 Fix small things here and there in the CHANGELOG 2022-05-19 13:14:32 +02:00
Andrea Leopardi cc4c837ace Add spec to PartitionSupervisor.start_link/1 (#11846) 2022-05-19 11:30:26 +02:00
Andrea leopardi 81a1d57bd4 Polish docs for PartitionSupervisor 2022-05-19 09:54:23 +02:00
Andrea leopardi 2bf3c045c6 Fix typo in a TODO comment 2022-05-17 10:58:11 +02:00
7b70999fd0 Add the :normalize_bitstring_modifiers formatter option (#11840)
Co-authored-by: Andrea Leopardi <an.leopardi@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
2022-05-17 07:54:14 +02:00
Eksperimental 3c1df66f25 Add tests for warning on atoms starting with a number (#11842)
Related PR: https://github.com/elixir-lang/elixir/pull/11838
2022-05-16 21:58:35 +02:00
Łukasz Samson 04074e5e81 Add note about Module functions availability during @after_compile (#11839) 2022-05-16 21:08:43 +02:00
Colin Caine 85594ced06 Improve warning on quoted atoms, calls, and keywords (#11838)
The warning messages stated that an atom containing starting with a
number and containing only ASCII letters, numbers and underscores does
not require quoting, for example `:401`, but this is not the case; `:401` is a
syntax error, as is `:1a` and so on.

This commit changes the warning message to make clear that unquoted
atoms/calls and keywords must not start with a number.
2022-05-16 18:03:54 +02:00
Eksperimental 93f7438d1b Update section reference in Supervisor module (#11841) 2022-05-16 17:37:29 +02:00
José Valim 7d80cbcfeb Allow :warning to configure Logger Console color 2022-05-15 23:30:44 +02:00
José Valim c04c74c0c9 Use :warning level for Logger threshold 2022-05-15 23:27:51 +02:00
Eksperimental a862545e2f Update t:Supervisor.child_spec/0 to reflect specs in minimum supported OTP version (#11837) 2022-05-15 22:05:30 +02:00
Eksperimental 9fd7e404b8 Improve t:Supervisor.child_spec/0 (#11835) 2022-05-15 20:15:22 +02:00
Eksperimental cbc3f7af88 CHANGELOG: fix mention to Logger.put_process_level/2 (#11834) 2022-05-15 19:19:25 +02:00
jdewar 86e8f7023a Handle Calendar.strftime widths with "0" in them (#11833)
* test formatting of a width ending in 0

* "0" set padding, only if we haven't started parsing a width
2022-05-15 19:10:04 +02:00
Eksperimental e49fcede17 Clarify docs for Supervisor.start_link/2 (#11831) 2022-05-15 19:05:00 +02:00
Eksperimental 14bdb2333f Replace usage of "etc." (#11832)
Related PR: https://github.com/elixir-lang/elixir/pull/9569
2022-05-15 17:58:14 +02:00
José Valim 5d1798970f Clarify value of :modules option, closes #11830 2022-05-15 16:59:44 +02:00
Andrea Leopardi 07b5f2db9e Add a guard to ExUnit.configure/1 (#11828) 2022-05-14 18:42:46 +02:00
ak3nji 77bae85b03 Improve Exception blame for is_struct guard by reverting the macro (#11829)
Closes #11820.
2022-05-14 15:32:06 +02:00
Artem Solomatin 65319f57e0 When all tests are excluded, colorize summary in yellow with message (#11827) 2022-05-14 12:52:39 +02:00
Andrea leopardi 0935cabc77 Polish the docs for the ExUnit module 2022-05-14 08:54:07 +02:00
Eksperimental fb3595c231 Grammar fixes related to A vs AN (#11826) 2022-05-13 21:32:50 +02:00
Andrea Leopardi 614f41c96b Improve docs for setup_all + on_exit (#11823)
I made a pass over the docs for "ExUnit.Callbacks.setup_all/1" and
"ExUnit.Callbacks.on_exit/2". I mostly clarified the behavior of
"on_exit/2" when called from "setup_all/1".
2022-05-13 14:58:26 +02:00
Aiden Scandella fbb0f06979 Support filename in mix format - when reading from stdin (#11822) 2022-05-12 21:22:00 +02:00
Aiden Scandella 25b2811d9d Update README instructions for cloning ex_doc (#11821)
GitHub says: The unauthenticated git protocol on port 9418 is no longer
supported.

```
$ git clone git://github.com/elixir-lang/ex_doc.git
Cloning into 'ex_doc'...
fatal: remote error:
  The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.

$ git clone https://github.com/elixir-lang/ex_doc.git
Cloning into 'ex_doc'...
remote: Enumerating objects: 18840, done.
remote: Counting objects: 100% (2920/2920), done.
remote: Compressing objects: 100% (1208/1208), done.
remote: Total 18840 (delta 1813), reused 2539 (delta 1638), pack-reused 15920
Receiving objects: 100% (18840/18840), 11.29 MiB | 22.24 MiB/s, done.
Resolving deltas: 100% (10856/10856), done.
```
2022-05-12 18:43:14 +02:00
Wojtek Mach 6faa829698 Document import mod, only: :sigils (#11816) 2022-05-12 08:24:44 +02:00
Eksperimental 66681916b6 Optimize Enum.sort_by/3 when sorter is :desc (#11819)
The speed increase goes from 47% up to 93% for enumerables of up to 100,000 elements.

Benchmark results: https://gist.github.com/eksperimental/c9ed94e418b4caa7fe61aff65b18c836
Benchmark repo: https://github.com/eksperimental/benchmark/tree/enum_sort_by

The optmization is done by saving unnecesary traversing of the list when sorting descendenly.
2022-05-12 08:23:40 +02:00
José Valim a1527dce9d Add --no-optional-deps to skip optional dependencies for compilation testing
Closes #11798.
2022-05-11 13:19:38 +02:00
José Valim 21972c1940 Return proper syntax error on incomplete escape char in string, closes #11813 2022-05-11 12:05:30 +02:00
José Valim f3259958bf Do not expand expressions in IEx eagerly, closes #11807 2022-05-10 10:46:32 +02:00
Eksperimental 07ac37c547 Remove dead code in List.keysort/3 (#11809) 2022-05-10 08:01:08 +02:00
Eksperimental 1f9f6de59c Add "@doc since" attribute to List.keysort/4 (#11810) 2022-05-10 08:00:52 +02:00
José Valim aa9128caca Mention module docs for EEx options 2022-05-09 10:28:32 +02:00
Kurtis Rainbolt-GreeneandAndrea Leopardi 677e481ae9 Add docs for the "options" argument in EEx.eval_string/3 (#11805)
Co-authored-by: Andrea Leopardi <an.leopardi@gmail.com>
2022-05-09 09:56:49 +02:00
Frank Hunleth c5c31baba7 Add allow_nonexistent_atoms to OptionParser spec (#11804)
This option was missing and it caused a Dialyzer error when used.
2022-05-08 22:06:10 +02:00
José Valim 3ee4e78cfb Support more stacktrace formats in IO.warn/2 2022-05-08 19:50:53 +02:00
José Valim 169595f534 Change import metadata to imports as a list
This is preparation to better ambiguity solving
of quoted expressions.
2022-05-08 17:00:09 +02:00
Andrea Leopardi 197b79f4df Add when :asc/:desc were introduced in Enum.sort/2 (#11803)
This commit adds a notice in the docs.
2022-05-08 13:17:05 +02:00
Eksperimental 122cc003a0 GenServer: Rename continue argument in handle_continue/2 (#11793)
As I was reading the docs, it was not clear what the `continue` argument
was just by reading its name.
2022-05-08 11:52:30 +02:00
Joe Harrow c053b4b37d Raise ArgumentError when inspect protocol options don't match struct (#11801) 2022-05-08 10:41:33 +02:00
José Valim cb91088167 Update CHANGELOG 2022-05-08 09:18:30 +02:00
José Valim e41b59a749 Do not add new line on all empty mix format 2022-05-08 09:13:37 +02:00
ak3nji 9e1aa0b8bf Do not format empty lines into line breaks (#11802) 2022-05-07 10:16:06 +02:00
José Valim 549a86741e Warn on underived derives, closes #11799 2022-05-06 12:44:06 +02:00
José Valim bd49ad6ef0 Generate less code in defstruct 2022-05-06 11:24:08 +02:00
José Valim dadb49f3d0 Inline more code in Enum 2022-05-04 18:18:42 +02:00
José Valim 4cdd76be91 Fix warnings on lexical tracker suite 2022-05-04 12:45:25 +02:00
José Valim d7709a5ad9 Allow slice to overflow on both starting and ending positions
Prior to this patch, ending positions could overflow but
not starting ones.
2022-05-04 12:43:44 +02:00
Eksperimental 91fa0e3ada Improve docs for Task mfa field (#11795) 2022-05-02 20:33:05 +02:00
José Valim 1907914cf0 Store :mfa in the Task struct (#11794)
This field can be used for reflection purposes.

Closes #11716.
Closes #11720.
2022-05-02 12:08:05 +02:00
José Valim 277184535e Support :test_elixirc_options in mix test
And skip debug and docs chunks by default.

Closes #11755.
2022-05-02 11:29:54 +02:00
Artem Solomatin fd51354d2c Fix punctuation and grammar issues in guides (#11790) 2022-05-02 10:52:37 +02:00
Eksperimental 2c70bd4fa0 Update Copyright owners to manpages. (#11792) 2022-05-02 08:42:09 +02:00
Kai 5e0721841e Add ExUnit.run/1 to rerun test modules (#11788) 2022-05-01 17:45:10 +02:00
José Valim 3ba538113e mix format 2022-04-30 20:33:26 +02:00
Michał Gibowski 94bd3dd831 Update IEx.Helpers docs for open/1 (#11787) 2022-04-30 20:22:40 +02:00
José Valim 68fa9215b3 Define how to_existing_atom works within modules, closes #11786 2022-04-30 19:06:59 +02:00
José Valim abda4a1749 Clarify deps.compile will attempt to compile deps 2022-04-27 08:40:59 +02:00
José Valim 709583d2d5 Simplify recursion in ExUnit merge 2022-04-26 21:27:58 +02:00
José Valim 965505fd1e Expand imports within describe, closes #11726 2022-04-26 21:01:55 +02:00
José Valim 27c3a4a4c3 Warn if a protocol has no definition, closes #11588 2022-04-26 19:17:06 +02:00
José Valim 808e955af2 Update CHANGELOG 2022-04-26 19:05:25 +02:00
José Valim d1e6b7522d Do not collapse small floats, closes #761 2022-04-26 15:09:32 +02:00
Michał Łępicki adaa773ac0 Fix ExUnit.Callbacks.start_link_supervised!/2 spec (#11784) 2022-04-26 08:16:17 +02:00
Jonatan Männchen bdc35f7a3a Add error details to ErlangError (#11779) 2022-04-25 20:18:24 +02:00
Johanna Larsson e38c23fb57 Adds start_link_supervised (#11769)
Adds a version of `start_supervised` and `start_supervised!` that explicitly links the started process to the test process, ensuring that any crashes are propagated to the test, failing it and providing a helpful test failure output.

More context on the change here: https://github.com/elixir-lang/elixir/issues/11737
2022-04-25 19:12:42 +02:00
John Bampton 04554ba4e5 Remove trailing whitespace (#11782) 2022-04-25 15:25:31 +02:00
Eksperimental 8d99d110a9 Use double # for comments in release script (#11781) 2022-04-25 14:59:03 +02:00
Eksperimental 0ec20d3688 Mix: Remove exclamation mark from message release command (#11780)
As it is confusing to have
"Release created at my_app!" as "!" is not part of the app name.
2022-04-25 14:58:18 +02:00
José Valim 395b0534f0 Evaluate --dot-iex line by line 2022-04-25 12:10:35 +02:00
Jonatan Männchen 225241f3a2 Return no_translation error in StringIO put_chars as in Erlang (#11756) 2022-04-25 12:05:01 +02:00
Eksperimental d9bd5d6eee Supress output in doctest in String module (#11776) 2022-04-24 17:48:58 +02:00
Eksperimental 0e76dd63f4 Remove warning in Kernel default tests (#11777)
The following warning was in the output of the test

    warning: variable "foo" does not exist and is being expanded to "foo()", please use parentheses to remove the ambiguity or change the variable name
    test/elixir/kernel/defaults_test.exs:75: Kernel.DefaultsTest.Kernel.ErrorsTest.ClauseWithDefaults5.hello/2
2022-04-24 17:35:15 +02:00
Eksperimental 14dff5c761 ExUnit: Group colliding test names in tests (#11774)
This is an improvement to https://github.com/elixir-lang/elixir/pull/11764
2022-04-24 15:53:07 +02:00
Josh Price 7213ceef1b Add missing known tag :describe_line to ExUnit.Case docs (#11773) 2022-04-24 13:37:41 +02:00
Norbert Melzer 920e7bd45d Make individual files in escript.build example better distinguishable (#11772)
Previously it was not clear to everyone that the escripts entrypoint
can't be in the `mix.exs` file but has to be separate.

This resulted in rare cases where people asked in the elixir forum why
their escripts module wasn't found.

The proposed changes should make this a bit more clear, though could of
course be improved by some explaining words that accompany the 2 snippets.

Fixes #11770
2022-04-24 10:29:36 +02:00
José Valim b274310c98 Do not generate more stacktrace than support on eval errors 2022-04-23 21:57:16 +02:00
José Valim 26e5080420 Fix mix suite 2022-04-23 10:20:51 +02:00
José Valim 540b202039 Use stdio when boot fails to avoid cutoffs 2022-04-23 10:05:20 +02:00
sabiwara 7909d45b64 Optimize Enum.with_index/2 for lists (#11765) 2022-04-22 08:20:55 +02:00
Jonatan Kłosko afee927db8 Fix System.shell/2 command escaping on Unix (#11767) 2022-04-21 15:18:17 +02:00
Simon Prévost 62fb3a62de Add dark logo in README with #gh-dark-mode-only (#11768) 2022-04-21 14:08:54 +02:00
Eksperimental da409d8692 Add short hash to tmp_dir in ExUnit to avoid test name collision (#11764)
There are certain tests names that could collide since when sanitizing unknown
characters are replaced with a hyphen.

This avoids test with name "foo-bar" and "foo+bar" to use the same temporary directory.
2022-04-19 23:16:09 +02:00
José Valim de74212118 Talk about the byte representation 2022-04-19 19:33:29 +02:00
José Valim 37fbb2be5f Improve docs for Unicode on String 2022-04-19 19:30:17 +02:00
José Valim eafb7b8775 Move error cases to new defaults_test.exs 2022-04-16 15:07:30 +02:00
sabiwara 4cc92a75b3 Fix variable declaration in default argument block (#11758) 2022-04-16 14:59:20 +02:00
José Valim a566fbe56e Clarify what the correct xor precedence should have been 2022-04-16 13:50:35 +02:00
felipe stival 2780157e53 Mention key optionality for literal keyword typespecs (#11757) 2022-04-13 15:33:21 +02:00
José Valim b553fe6a35 Do not store logs when storing timings 2022-04-11 21:57:50 +02:00
José Valim e304bf3e44 Revert "Add compile-time dependencies on require"
This change added an inconsistency in that imports
also require, but they added different types of
dependencies. This must be handed properly in
libraries instead.
2022-04-11 21:52:47 +02:00
Joel C 85617ceb8a Only store test timings in CLIFormatter when --slowest is used (#11754) 2022-04-11 21:52:38 +02:00
Jared Mackey 94bab44764 Run after_suite even when no tests were ran (#11749)
This ensures that we properly call `after_suite` callbacks that were
setup in the `test_helper.exs` or other places. Without running those,
it could leave the app in a bad state. This is especially important for
umbrella testing with partitions as those may do some setup that breaks
the next app setup if not cleaned up.
2022-04-09 12:04:36 +02:00
José Valim e8b24cd3f0 Remove duplication in unicode tokenizer 2022-04-09 09:46:56 +02:00
ShigeruItoandendo-sho 700e0b7a60 Fix typo in mix escript.build docs (#11743)
Co-authored-by: endo-sho <endo-sho@klab.com>
2022-04-06 07:28:31 +02:00
Wei Huang eea4a9e89e Make Registry.send work when value part is present (#11742)
Fix #11740.
2022-04-05 10:25:45 +02:00
Wei Huang d4d0523a12 Add ws and wss port configuration to URIConfig (#11738) 2022-04-01 12:40:44 +02:00
Billy Peake feb9884a3e Fix Version.parse/1 incorrectly stripping dots from field (#11736) 2022-03-29 08:25:48 +02:00
Daven 873b7c2e28 Warn if any of True, False, and Nil aliases are used (#11731) 2022-03-28 07:28:34 +02:00
Daniel Khaapamyaki b8cd17dfee Improve docs for then and tap (#11730) 2022-03-28 07:23:54 +02:00
Eksperimental 5f33f22c41 Makefile: allow setting DOCS_FORMAT (#11733)
This allows us to generate Epub documents.

    DOCS_FORMAT=epub make docs
2022-03-28 07:18:19 +02:00
Eksperimental 8d0cfded5a CI: Use latest ex_doc version (#11732)
- release.yml now automatically fetches latest stable ExDoc version.
- ci.yml now builds docs using latest stable and main ExDoc version.
2022-03-27 21:21:45 +02:00
Eksperimental 4095d29b2a Replace usage of comma with plus sign in "mix do" commands (#11729) 2022-03-26 16:36:37 +01:00
Vasiliy Ermolovich 2f107618bf Remove new lines from multiline test name (with --trace option) (#11728)
closes https://github.com/elixir-lang/elixir/issues/11708
2022-03-25 17:36:30 +01:00
Eksperimental 6546a4366b Streamline release.yml Github Action with ci.yml (#11727) 2022-03-25 16:14:30 +01:00
José Valim 4384cbb8ef Use EEP 54 pretty printer option 2022-03-25 11:52:19 +01:00
Michał Łępicki 14b953578f Do not mention compatibility in @dialyzer attr doc (#11725)
The feature was added in OTP-18 https://github.com/erlang/otp/commit/25ab719cfaf42d196287fec2171bf3eefb845b62
Latest Elixir supports OTP-22 or newer
2022-03-25 10:08:56 +01:00
Eksperimental b535be2764 CI: Add build docs (#11724) 2022-03-25 09:32:45 +01:00
José Valim 507a91a214 Fixes for multi args before + in mix do 2022-03-25 09:32:31 +01:00
Mark McElroy ac1c322a9f fix typo in the supervisor docs (#11723) 2022-03-25 04:14:43 +01:00
Simon Hansen a1fe88f73a Fix typo (#11722) 2022-03-24 18:00:17 +01:00
José Valim b53fb305ac Improve error message clarity 2022-03-23 18:48:42 +01:00
Andrey Yugai fa621f2669 Do not mention charlist segments in <<>>/1 docs (#11718) 2022-03-23 17:28:18 +01:00
Michał Łępicki beb6b4737b Change map updates type checking direction (#11713) 2022-03-21 15:59:32 +01:00
José Valim c03e39798e Add OTP-24.3 to CI (#11711) 2022-03-19 09:12:54 +01:00
Malte d6df224ba9 Add more reasons to fallback to inet or inet6 (#11705)
`:nxdomain` appears when using an IPv6-only proxy as shown in
https://github.com/elixir-lang/elixir/issues/10423#issuecomment-805891724.

`:eprotonosupport` appears on an IPv6-only jail on freebsd 13.0
2022-03-16 11:22:07 +01:00
dariodf bd2e691325 Support ISO8601 basic format parsing with DateTime.from_iso8601 (#11703) 2022-03-15 13:01:06 +01:00
José Valim 03acf01468 Improve docs for throw 2022-03-15 11:09:18 +01:00
James Every e0294bcc33 Fix use of assert_receive[d] in catch_exit doc (#11702) 2022-03-13 18:23:08 +01:00
sabiwara 7fd45263e7 Add line in diagnostic for invalid struct key (#11701) 2022-03-13 09:06:06 +01:00
Mat Trudel 4123eee23b Document how to manually build URI structs (#11688) 2022-03-12 17:00:28 +01:00
Michał Dolata c0dcfdc5af Fix: dot path for umbrella test (#11693) 2022-03-12 16:59:56 +01:00
sabiwara 7b0988e89d Fix bug when formatting :"\\" (#11699) 2022-03-12 16:58:55 +01:00
Michał Łępicki 63e47416b5 Fix Logger docs and specs (#11697) 2022-03-11 11:06:04 +01:00
Dylan Aspden bdcf887afe Allow plus symbol in mix do as alternative to comma (#11696) 2022-03-10 18:51:45 +01:00
Wojtek Mach 21c166fa2f Fix EEx.tokenize/2 docs (#11694) 2022-03-10 13:23:54 +01:00
Stefan Chrobot b81a59a5db Add Logger.put_process_level/2 and rewrite enable/1 and disable/1 (#11691) 2022-03-10 10:16:22 +01:00
Mohamed Sabry Hegazy d4fa71d42d Add space before argument for documentation consistency (#11690) 2022-03-09 11:24:51 +01:00
José Valim c6272ce8f1 Add missing word, closes #11686 2022-03-08 09:20:14 +01:00
Thiago Romano 149823aec8 Remove duplicated comment (#11687) 2022-03-07 22:54:56 +01:00
Rudolf 2e38f5ed32 Raise when doctest's only function is undefined (#11684) 2022-03-06 19:47:57 +01:00
Frank Hunleth 20c8d102e7 Make BEAM compression in mix releases opt-in (#11685)
In releases, the final step for stripping chunks of out BEAM files is to
gzip compress the file. This change lets users pass `compress: true` to
opt-in. Somewhat unintuitively, turning compression off here
enables better compression at a later step. This is due to the archive
compressors being able to process all BEAM files rather than restarting
compression for each individual BEAM file.

For some Nerves devices, reducing the total number of bytes sent to
update software is much more important than reducing the on-disk usage.
This is due to their network connections being metered. Given enough
devices, even small size reductions can result in meaningful savings.

Here are more details:

The default Nerves configuration puts all of the BEAM files in one
SquashFS archive. While Nerves also uses gzip for the SquashFS archive
and SquashFS (mostly) individually compresses files as well, letting
SquashFS perform the compression results in 2.5% file size reduction.
This is assumed to be due to using a higher compression level with
building the SquashFS archive. As a side benefit, moving gzip
compression to SquashFS removes the decompression steps from the BEAM
load and it resulted in a ~500ms boot time improvement on GRiSP 2
hardware for a small Nerves app. Presumably the Linux kernel's gzip
decompression is faster than Erlang's or removing the Erlang gunzip
calls completely added up.

When it's possible to compress all BEAM files together (for example,
when making tar files), not compressing BEAM files also helps. This is
because the BEAM files have a lot of similar strings that are now
visible to the archive compressor. It's also possible to use better
compression methods than gzip.

A similar use is the way that delta firmware updates are handled with
Nerves. Delta firmware updates are a way to send down the difference
between the firmware image on a device and the firmware image you want.
These are generated across the full image and should benefit from being
able to work off uncompressed .beam files.
2022-03-06 14:28:03 +01:00
Michał Łępicki 8ab2fb9ad2 Update Inspect.Algebra.fits?/5 spec (#11683)
It is now being called with :infinity atom as width
2022-03-04 17:40:45 +01:00
Nathan Long b7a72ca3b5 Add examples other than keyword lists (#11680) 2022-03-03 22:07:54 +01:00
José Valim 4212fbf36e Add no_limit to Inspect.Algebra
Closes #11675.
2022-03-03 18:23:56 +01:00
José Valim fc0cb0112d Add release notes outline to CHANGELOG 2022-03-03 10:25:58 +01:00
José Valim 82d1a49b66 Update CHANGELOG 2022-03-03 10:16:42 +01:00
Joe Martinez 8fb28bc765 Add Range.shift/2 (#11676) 2022-03-03 10:05:54 +01:00
Willian Frantz d806ab33a4 Improve Macro.pipe/3 docs (#11679) 2022-03-02 18:53:18 +01:00
Aaron Tinio 32cde7f78a Fix EEx.tokenize/2 CaseClauseError (#11677) 2022-03-02 07:57:55 +01:00
Aaron Tinio bbeb8e618a Update EEx.token() type (#11678) 2022-03-02 07:57:24 +01:00
Felipe Renan 63099e7d3b Expose EEx comments as tokens in EEx.tokenize/2 (#11674) 2022-03-01 21:55:11 +01:00
Michał Łępicki abf8dea3b7 Tweak type unification to fix infinite loop with recursive vars (#11664) 2022-02-28 14:47:16 +01:00
Wojtek Mach 35389206cf Fix Enumerable.slice docs (#11672) 2022-02-28 12:23:05 +01:00
José Valim f6d9241571 Only keep input in async_stream if necessary 2022-02-28 09:59:38 +01:00
Michał Łępicki 417c8b2154 Fix spec for EEx.tokenize/2 and EEx.Compiler.compile/2 (#11670) 2022-02-28 09:59:15 +01:00
Vitalik f83f1f5691 Add zip_input_on_exit option to Task.async_stream (#11668) 2022-02-28 09:16:01 +01:00
Wojtek MachandJosé Valim f887627e97 Add doc since to EEx.tokenize/2 (#11669)
Co-authored-by: José Valim <jose.valim@dashbit.co>
2022-02-27 10:35:09 +01:00
José Valim d1eb57a08f Use main EEx tokenize API 2022-02-27 10:23:21 +01:00
Felipe Renan 31a794014b Add EEx.tokenize/2 (#11667) 2022-02-27 10:01:42 +01:00
José Valim 74a1be69c1 Remove TODO 2022-02-25 20:34:24 +01:00
José Valim 51d1eb927a Move some EEx syntax errors to tokenizer 2022-02-25 20:33:14 +01:00
Felipe Renan 1395240873 Refactor EEx tokenizer (#11666)
This commit moves the line and column to the last argument to be handled
as a metadata as you can see in the example below:

```elixir
    assert T.tokenize('foo', 1, 1, @opts) ==
             {:ok, [{:text, 'foo', %{line: 1, column: 1}}, {:eof, %{line: 1, column: 4}}]}
```
2022-02-25 20:24:25 +01:00
José Valim 57ee3e811d Improve eval stacktraces on Erlang/OTP 25+ (#11665) 2022-02-24 19:07:22 +01:00
José Valim 15319e7ac0 Skip --rpc-eval missing arg test on Windows 2022-02-24 19:05:55 +01:00
Travis GriggsandTravis Griggs e61406b287 Adjust childspec documentation to note that DynamicSupervisor require the :id field (#11663)
Co-authored-by: Travis Griggs <travis.griggs@nelsonirrigation.com>
2022-02-23 23:38:27 +01:00
José Valim 50f7a32a77 Fix CLI when Erlang flags are given after --rpc-eval 2022-02-23 23:15:50 +01:00
Wojtek Mach ff55435b16 Show errors for incorrect --rpc-eval usage (#11659)
Before this patch we had this:

    iex> :os.cmd('elixir --sname foo --rpc-eval bar')
    ''

    iex> :os.cmd('elixir --sname foo --rpc-eval')
    'No file named \n'
2022-02-23 15:05:23 +01:00
José Valim c781651052 Support general+reason metadata from EEP 54 2022-02-22 17:34:39 +01:00
José Valim 3827a319d8 Improve error messages from bad capture operator usage (#11656) 2022-02-21 21:30:48 +01:00
Tobias Pfeiffer 36eb6eb149 Profiler return the return value of the function (#11657)
As discussed in: https://groups.google.com/g/elixir-lang-core/c/f0gP0It-yuU
2022-02-20 22:34:08 +01:00
José Valim dc8c191153 Fallback to rebar3 when looking up command 2022-02-20 14:43:29 +01:00
Benjamin Milde 495e0bd69f Remove {:from_app, app} from typespec and docs (#11655) 2022-02-19 18:11:42 +01:00
Kian-Meng Ang f47e18eafb Fix typos (#11653) 2022-02-19 17:22:05 +01:00
Enrico Rivarola e29f1492a4 Handle 'mix local.rebar' command options without arguments (#11650) 2022-02-18 17:41:19 +01:00
Michał Łępicki b5f9f071f4 Remove Rebar 2 leftovers (#11649)
* Remove mentions from documentation

* Remove rebar from @scm_manager in Mix.Dep.Loader

* Change manager: :rebar to manager: :rebar3 in test

* Remove rebar_exclude test filter from mix test_helper.exs
  (there are no more Rebar 2 tests after they got removed
  in 5cca5e5fbb )
2022-02-18 17:19:20 +01:00
José Valim a55dd11795 Update local rebar3 2022-02-17 21:49:42 +01:00
José Valim 99c15ecd0c Add opening delimiter to sigil metadata/opts 2022-02-17 20:43:13 +01:00
José Valim 5cca5e5fbb Remove rebar2 support 2022-02-17 19:58:25 +01:00
Michał Łępicki 5ba5e7b168 Skip tests using Rebar2 on Erlang/OTP 25+ (#11643)
* Skip tests using Rebar2 on Erlang/OTP 25+

and clean up mix test_helper.exs exclude filters

* Remove unnecessary printing from mix test_helper.exs exclude filters
2022-02-17 18:35:05 +01:00
José Valim e6b5a91760 Deprecate rebar2 2022-02-17 17:39:03 +01:00
Michał Łępicki 79ffa657bb Recreate local.sample task fixture in test_helper.exs (#11640) 2022-02-17 16:43:55 +01:00
felipe stival 2ccf549258 Fix typo (#11641) 2022-02-17 16:16:37 +01:00
Oskar Köök 97b2585c87 Add explanation for exit reason to GenServer (#11639) 2022-02-17 15:32:19 +01:00
José Valim d1fb5a190f Allow iodata in sigil formatting functions 2022-02-17 14:57:04 +01:00
Michał Łępicki 2ddd291c53 Update Mix.TasksTestTest for Erlang/OTP 25 (#11637)
The order in which tests get executed can be different
depending on Erlang/OTP version

The test failure was:

    1) test logs and errors umbrella with file path (Mix.Tasks.TestTest)
       test/mix/tasks/test_test.exs:432
       Assertion with =~ failed
       code:  assert mix(["test", "apps/unknown_app/test"]) =~
                "==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       left:  "==> foo\nCompiling 1 file (.ex)\nGenerated foo app\n==> bar\nCompiling 1 file (.ex)\nGenerated bar app\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       right: "==> bar\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n==> foo\nPaths given to \"mix test\" did not match any directory/file: apps/unknown_app/test\n"
       stacktrace:
         test/mix/tasks/test_test.exs:436: anonymous fn/0 in Mix.Tasks.TestTest."test logs and errors umbrella with file path"/1
         (elixir 1.14.0-dev) lib/file.ex:1555: File.cd!/2
         test/test_helper.exs:127: MixTest.Case.in_fixture/3
         test/mix/tasks/test_test.exs:433: (test)
2022-02-17 10:06:26 +01:00
Michał Łępicki c67c0d6859 Update Mix.DepTest for Erlang/OTP 25 (#11636)
:digraph_utils.topsort/1 can return a different (but also valid)
topological ordering

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

    1) test deps_paths (Mix.DepTest)
       test/mix/dep_test.exs:491
       Assertion with == failed
       code:  assert Enum.map(Mix.Dep.load_on_environment([]), & &1.app) == [:git_repo, :abc_repo, :deps_repo]
       left:  [:abc_repo, :git_repo, :deps_repo]
       right: [:git_repo, :abc_repo, :deps_repo]
       stacktrace:
         test/mix/dep_test.exs:499: anonymous fn/0 in Mix.DepTest."test deps_paths"/1
         (elixir 1.14.0-dev) lib/file.ex:1555: File.cd!/2
         test/test_helper.exs:127: MixTest.Case.in_fixture/3
         test/mix/dep_test.exs:31: Mix.DepTest.with_deps/2
         test/mix/dep_test.exs:497: (test)
2022-02-17 10:05:40 +01:00
José Valim 22e3b12dec Improvements to docs in URI and Module 2022-02-17 08:21:48 +01:00
Jinkyou Son (Json) f20c0172b0 Deprecate levelpad of Logger.Formatter (#11633) 2022-02-17 08:17:40 +01:00
José Valim 8da48af786 Document delete_attribute+accumulate, closes #11635 2022-02-16 23:21:38 +01:00
Michał Dolata 8d06ee819a Fix --warnings-as-errors for compiling with --all-warnings (#11634) 2022-02-16 23:15:31 +01:00
Kevin 675ebdaa0e Only check for unwanted functions (#11632) 2022-02-16 15:32:55 +01:00
José Valim f4a4bd59ec Update CHANGELOG 2022-02-15 19:14:09 +01:00
Owen Bickford 41eeb1af5f Clarify app env usage (#11627) 2022-02-15 07:50:05 +01:00
Aaron Renner 6e1632d403 Remove child_spec/1 from StringIO (#11625) 2022-02-15 07:38:18 +01:00
José Valim 14ad5eaaae Add binary_slice/2 and binary_slice/3 (#11626)
So far Elixir only exposed binary_part/3 as part of its
functions to manipulate binaries, since that's allowed in
guards.

However, `binary_part/3` is quite limited in that it
expects both `start` and `start+size` to fall within the
binary. This could lead developers to use `String.slice/2`
or `String.slice/3` when a binary operation would be more
performant or appropriate.

This pull request therefore adds `binary_slice/2` and
`binary_slice/3` that nicely integrates with binaries
and ranges, and behaves similarly to their `Enum.slice`
and `String.slice` counterparts.
2022-02-14 17:06:53 +01:00
José Valim f7e9f62469 @doc false URI.Error.message/1 2022-02-14 13:25:10 +01:00
José Valim 45a34dae5a Add compile-time dependencies on require
Projects like Plug use require to establish compile
time dependencies inside a Plug. The fact require
only added a compile-time dependency in v1.13.0 was
therefore a regression, addressed by this commit.
2022-02-14 12:39:54 +01:00
José Valim d04bcd4624 Allow slicing with steps in Enum.slice/2 and String.slice/2 (#11624) 2022-02-13 20:23:57 +01:00
José Valim 6447f440db Add .. as a nullary operator that returns 0..-1//1 (#11623) 2022-02-13 15:18:53 +01:00
José Valim 18c44e2e4f Simplify process of copying SpecialCasing.txt 2022-02-13 09:01:05 +01:00
Eksperimental 0a07484d83 Improve Unicode module update instructions (#11622) 2022-02-13 08:54:24 +01:00
José Valim 9e0ae6140a Allow only single- and highly restricted mixed-scripts in identifiers (UTS39 C3) (#11621) 2022-02-11 20:44:06 +01:00
Eksperimental b9cfdf8a9d Fix typo in Config.Provider link (#11618) 2022-02-10 00:17:19 +01:00
José Valim 6580c7db76 Handle more unicode sequences in split_at, closes #11617 2022-02-09 15:35:17 +01:00
José Valim 5458cd7f9b Only flags with multiple args can conflict 2022-02-09 08:40:28 +01:00
José Valim 0218fbb5b6 Add all command line flags as reserved application names, closes #11613 2022-02-08 20:31:09 +01:00
José Valim 6b57ef7a0b Remove usage of outdated MapSet in doctests 2022-02-08 16:14:38 +01:00
Wei Huang e16fd5326c Change Version.Requirement inspect to use expression (#11616) 2022-02-08 15:55:58 +01:00
Wei Huang 0c0f7af679 Change MapSet inspect to use expression (#11615) 2022-02-08 14:06:22 +01:00
Wei Huang db5555b85b Change Version inspect to use expression (#11614)
Ref: #11610
2022-02-08 09:28:06 +01:00
Wojtek Mach c8a9dacf62 Change Date.Range inspect to use Date.range (#11612) 2022-02-07 23:11:11 +01:00
José Valim 870e9fb9ad Further improve docs 2022-02-07 19:33:23 +01:00
José Valim 954ea4543c Clarify docs and slightly optimize String.contains?/2
1. Change the docs for `contains?` to explicitly mention
     the verb "Searches"

  2. Add a note about using `Enum.member?/2` for membership tests

  3. Slightly optimize `String.contains?/2` when the string
     is too small

Closes #11611.
2022-02-07 19:27:18 +01:00
José Valim 8b848c1b80 Use module and init_arg on DynamicSupervisor.start_link/3 for consistency
Closes #11609.
2022-02-07 15:20:21 +01:00
Sergei Iarkin 342ea9c410 Raise exception if no PID is found (#11575) 2022-02-07 09:12:14 +01:00
Lauri Annala d17d2f2091 Fix function name in doc and spelling (#11608) 2022-02-04 19:47:41 +01:00
José Valim ac4904e0fd Track all stale modules from config/lock as exports too 2022-02-04 16:42:19 +01:00
José Valim c6e18cd17b Update CHANGELOG 2022-02-03 13:22:26 +01:00
José Valim 6d6738a57e Tiny improvements to run_in_apps 2022-02-03 13:15:43 +01:00
Jorge Bejar e6a244a8dd Add the ability to limit tasks in given apps in umbrella applications (#11593) 2022-02-03 13:00:13 +01:00
José Valim f13188ccfe Handle diff with strings as improper tail, closes #11607 2022-02-03 12:51:24 +01:00
Almir Sarajčić 73fc968744 Fix typo in ExUnit documentation (#11606) 2022-02-02 19:02:18 +01:00
José Valim 4334152bbd Improve docs for Application to mention new warning 2022-02-02 09:12:08 +01:00
José Valim e4c97ab424 Do not break signatures over multiple lines 2022-02-01 10:04:14 +01:00
José Valim 8c35f67357 Fix recursion on guards with map fields, closes #11602 2022-01-31 14:58:28 +01:00
Thales Macedo Garitezi 9a1babd709 Donnot ask to change cookie in releases if overwrite is set (#11601)
Currently, if a `releases/COOKIE` file exists with a given cookie, and
a user later manually adds a `:cookie` configuration to the release,
then, even using the flag `--overwrite` when releasing, the user is
asked if they want to overwrite the file.
2022-01-30 18:13:32 +01:00
José Valim c7278827da Clarify invalid type annotation warnings, closes #11597 2022-01-29 11:19:09 +01:00
José Valim a63c1cdf8b Use a counter as Supervisor example for simplicity 2022-01-29 11:08:04 +01:00
Marc-André Lafortune 7c15545ec3 Tweak documentation for get_in (#11599)
Makes it clearer that the example, while valid, is not useful in general.
2022-01-29 09:08:59 +01:00
José Valim ede53104ec Set release mode after loading env scripts 2022-01-28 21:45:51 +01:00
José Valim 7b981d1cc9 Ensure versioned_vars are updated at the end of evaluation 2022-01-27 08:18:23 +01:00
José Valim 826d2c8679 Do not emit warnings when formatting code
Closes #11595.
2022-01-26 13:37:53 +01:00
José Valim 2ec2d1b184 Fix indentation of options in mix format 2022-01-24 16:48:34 +01:00
Chris Wögi 790e7c582a Fix typo (#11592) 2022-01-24 13:48:37 +01:00
José Valim b035303b74 Improve Task docs regarding supervisor benefits 2022-01-24 12:15:29 +01:00
Matt McCoy 8c0bc69a89 Supply file and line to formatter plugins (#11591)
Allow formatter plugins to print errors with accurate file and
line information. The line is only provided when a sigil is being
formatted.
2022-01-24 08:36:07 +01:00
José Valim 8b624835a9 Check for fun arity in formatter 2022-01-22 09:35:44 +01:00
Jonatan Männchen b4fd48a364 Embedded Elixir Expressions in Formatter Plugins (#11587)
Support Sigils inside embedded expressions in files formatted
by a formatter plugin.
2022-01-22 09:34:20 +01:00
José Valim b401d15296 Run unicode security linting on more tokens 2022-01-21 10:50:02 +01:00
Luc Fueston a4689262d8 Warn on confusable non-ascii identifiers (UTS 39, C2) (#11582) 2022-01-21 09:37:25 +01:00
felipe stival 3f062a4bf4 Fix duplicate bindings causing weird behaviour (#11584)
Before this fix, evaluating `b = a` with assignments of: `a: 1, a: 2, c:
3` would eval AST equivalent to `^c = a`. This happened due to binding
normalization generating version numbers larger than the total number of
bindings. Later in the pipeline, the number of bindings is used to
compute the next version number, which would then conflict with an
existing binding.

This commit fixes that by not increasing the version number when
a repeated binding is normalized.
2022-01-19 19:35:33 +01:00
Eksperimental 74a24c34b9 Add example using file extension in Path.basename/1 (#11583) 2022-01-18 22:04:33 +01:00
Luc Fueston 4a5fcbe04a Don't allow restricted characters in identifiers (UTS 39, C1) (#11580) 2022-01-17 19:42:36 +01:00
Francis Chabouis 8e154d83f5 Update doc for Time and NaiveDateTime (#11578) 2022-01-17 15:22:20 +01:00
Eksperimental 4010371f1b Streamline warning message with docs in Logger functions (#11577)
See discussion:
https://github.com/elixir-lang/elixir/commit/5792c3835fdef6fbf52fc477ef9789f96c0ec68d#commitcomment-62955268
2022-01-17 08:28:06 +01:00
Eksperimental 041e3233a0 Improve docs for Enum.sort* (#11576) 2022-01-16 21:17:22 +01:00
José Valim 33a91237f6 Improve overridable examples 2022-01-16 20:43:43 +01:00
Eksperimental 7871f9326d Fix bug with edge-case when converting to algebra quoted Elixir alias (#11572)
The following code would crash:

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

Therefore, this as well:

    iex> Macro.to_string(Elixir)
    ** (MatchError) no match of right hand side value: "Elixir"
        (elixir 1.13.1) lib/code/normalizer.ex:254: Code.Normalizer.normalize_literal/3
        (elixir 1.13.1) lib/code.ex:1107: Code.quoted_to_algebra/2
        (elixir 1.13.1) lib/macro.ex:948: Macro.to_string/1
2022-01-16 09:28:09 +01:00
Steve Hall 7b0d4d6707 Fix coverage threshold ignored for Total line in Summary (#11571)
An incorrect variable is being passed to the `display/2` function on line 353 for the Total line in the coverage summary report. The result is that that Total line will always display in red no matter what the threshold is set to.
2022-01-15 14:58:48 +01:00
Julian Doherty c5e8dfb881 expand Kernel.in/2 docs re range check efficiency (#11570)
Current docs treat membership tests on lists and ranges using
`Kernel.in/2` with the same warning in the doc saying that comparison on
large lists/ranges is inefficient.

This is true for lists, but not for ranges. A more efficient check is
done there which can run in constant time.

Update docs to explain that.

Have omitted going into detail on how membership checks for ranges with
steps are done, as that seems less relevant, and the general comment
that the check can be done efficiently still holds true.
2022-01-15 12:51:24 +01:00
Wojtek Mach c7e92e4bc6 Add URI.append_query/2 (#11566) 2022-01-15 10:23:43 +01:00
Tyler Pachal 04d8b71120 Enhance documentation for SpecialForms.try (#11567) 2022-01-14 18:41:47 +01:00
Eksperimental ead6b39018 Use falsy intead of falsey (#11565) 2022-01-14 08:16:32 +01:00
José Valim 31739ad779 Optimize Map.new/2 2022-01-13 15:27:53 +01:00
José Valim 1056eafa22 Document that duplicate keys in validate errors, closes #11563 2022-01-12 17:13:11 +01:00
Francis Chabouis 8c5be81038 Mention Enum in Date/DateTime documentation (#11564) 2022-01-12 16:37:21 +01:00
José Valim 02f2ed025c Update CHANGELOG 2022-01-12 15:08:05 +01:00
palexanderm c923a30ba0 Add MapSet.filter/2 and MapSet.reject/2 (#11493) 2022-01-12 14:28:34 +01:00
José Valim e98777991b Bring back filter/reject on Map and Keyword
They are already being used in projects, which
means it is too late for a deprecation. The docs
instead discuss when to use them.
2022-01-12 14:24:18 +01:00
José Valim 00c35ad4d5 Allow any expression in bitstring size outside of matches/guards 2022-01-12 01:47:11 +01:00
Jorge Bejar c92724d9bb EEP 52: Support for size expression in bitstring matching (#11558) 2022-01-11 21:42:28 +01:00
Thanabodee Charoenpiriyakij 11bee474d4 Strip name from checksum file (#11561)
Currently, the notify.exs will generate the checksum to:

```
  * Docs.zip - sha1sum - 3812e2db7c71a6a8b03b75d06ca6debb46aebc55  Docs.zip

  * Docs.zip - sha256sum - cafe86adc01fcc700c83f353aedf746eeffd3b6e1542cedff18af64956ae539b  Docs.zip

  * elixir-otp-23.zip - sha1sum - 35f999b98cbfa5888fefbeafaaf73f661a903bb7  elixir-otp-23.zip

  * elixir-otp-23.zip - sha256sum - 11f970adc3ddc759f349b0e00b6aecd06a1d460f21488d2ffe8f6738f2518728  elixir-otp-23.zip

  * elixir-otp-24.zip - sha1sum - c4224ce1b78247d7c5e04c7cd4576496fd49f002  elixir-otp-24.zip

  * elixir-otp-24.zip - sha256sum - 677fe7a147a40e5ee876eeb7bb8e4b968d2a8a9c42793561b61a08269ca91c19  elixir-otp-24.zip
```

The result have a file name and newline at the end of each line. This change strip
that, so the result will be:

```
  * Docs.zip - sha1sum - 3812e2db7c71a6a8b03b75d06ca6debb46aebc55
  * Docs.zip - sha256sum - cafe86adc01fcc700c83f353aedf746eeffd3b6e1542cedff18af64956ae539b
  * elixir-otp-23.zip - sha1sum - 35f999b98cbfa5888fefbeafaaf73f661a903bb7
  * elixir-otp-23.zip - sha256sum - 11f970adc3ddc759f349b0e00b6aecd06a1d460f21488d2ffe8f6738f2518728
  * elixir-otp-24.zip - sha1sum - c4224ce1b78247d7c5e04c7cd4576496fd49f002
  * elixir-otp-24.zip - sha256sum - 677fe7a147a40e5ee876eeb7bb8e4b968d2a8a9c42793561b61a08269ca91c19
```
2022-01-10 12:13:24 +01:00
Andrea Leopardi 0afc5d5a5f Add some specs and clean up docs for Macro (#11560) 2022-01-10 10:30:16 +01:00
Milo Lee d2f899b04d Add spec to Macro.expand*/2 functions (#11559) 2022-01-10 08:36:53 +01:00
Andrea Leopardi 1c473e796a Polish docs in the Path module (#11556)
* Polish docs in the Path module

* FIXUP
2022-01-10 08:04:00 +01:00
tamanugi d07b58b3e3 File.write!/2 call File.write/2 to DRY (#11555) 2022-01-09 08:07:47 +01:00
Jonatan Kłosko 0aeded4dd4 Fix syntax in notifications script (#11553) 2022-01-08 16:14:54 +01:00
Paulo Daniel Gonzalez 0774e4c385 Add nudge to do expression error (#11552)
Closes https://github.com/elixir-lang/elixir/issues/11551
2022-01-07 20:41:26 +01:00
Andrea Leopardi 9929dddaed Fix Windows build (#11548) 2022-01-06 15:27:03 +02:00
felipe stival 953c730cc8 EEP 52: Allow pins inside map keys in matches (#11544) 2022-01-06 00:48:04 +01:00
Andrea Leopardi 64ac646a24 Add Path.safe_relative/1 and Path.safe_relative_to/2 (#11542) 2022-01-05 18:52:15 +02:00
José Valim 5792c3835f Document logger messages, closes #11546 2022-01-05 15:34:44 +01:00
José Valim f78e43b543 Reduce the amount of generated code for defdelegate 2022-01-05 15:23:44 +01:00
Sasha Fonseca 27b53b8be5 Raise ArgumentError when defdelegating points to the delegating module (#11545) 2022-01-05 15:17:50 +01:00
Michał Łępicki 2474c1dddf Remove references to HiPE (#11543)
* Fix typo in Dialyzer CI step name

* Remove references to HiPE
2022-01-05 13:37:17 +01:00
José Valim e658c64eee Run formatter 2022-01-05 13:23:20 +01:00
felipe stival 8c09306eec Use :erlang.atom_to_binary/1 (#11541)
This function was added in OTP 23 and its the functional equivalent to
`:erlang.atom_to_binary/2` with `:utf8` as the second argument.
2022-01-05 12:54:00 +01:00
José Valim 4090e990b7 Add Stream.transform/5
This function allows the transformation accumulator to
be processed in order to emit the last elements of the
collection.
2022-01-05 12:48:16 +01:00
José Valim 578e59e34c Bump Erlang versions on FreeBSD CI 2022-01-05 12:15:27 +01:00
Łukasz Samson 5a5e5d2446 Add Node.spawn_link (#11540) 2022-01-05 12:11:04 +01:00
José Valim 12981b15df Remove support for Erlang/OTP 22 2022-01-05 09:14:03 +01:00
Chris Dosé 2736b9465a Fix a few issues in the mix test.coverage docs (#11539) 2022-01-05 08:34:54 +02:00
Keith 7356a47047 Update docs for ExUnit.CaptureLog (#11538) 2022-01-04 18:01:10 +02:00
sabiwara 29089ab986 Use consistent argument name in DateTime doc (#11537) 2022-01-04 11:30:29 +01:00
Connor Rigby 06a02ac7f6 Add name lookup to pid/1 helper (#11536) 2022-01-03 22:03:49 +01:00
palexanderm f19b7e2d5d Fix typo in Special Forms docs (#11535) 2022-01-03 21:39:46 +01:00
calvin-kargo 4cb690440b Add assignment as generator documentation at for comprehension (#11534) 2022-01-03 21:26:53 +01:00
José Valim 40b512031f Use more precise headers on Date function docs, closes #11533 2022-01-02 19:59:50 +01:00
Aleksei Matiushkin 3c6b8a96e5 Fix the nested uniq: acc name clash by preserving a scope (#11532) 2022-01-01 09:00:18 +01:00
José Valim 6d803edf73 Access callbacks always receive structs 2021-12-30 23:42:25 +01:00
José Valim 43aa30d263 Automate notifications 2021-12-30 16:42:39 +01:00
José Valim ea632b6c56 Add mail.exs script 2021-12-30 15:02:16 +01:00
José Valim be11078849 No longer version archives 2021-12-30 14:29:11 +01:00
José Valim ade65edfc6 Update instructions 2021-12-30 14:25:44 +01:00
Thanabodee Charoenpiriyakij 93598e5530 Pre-built Elixir release (#11522) 2021-12-30 12:52:49 +01:00
Aaron Tinio f30e61456c Fix hidden escape chars in File.stream!/3 docs (#11529) 2021-12-30 09:33:44 +01:00
Michał Łępicki aeea688f8d Define exception field as true instead of term when expanding struct type (#11528) 2021-12-29 17:37:26 +01:00
José Valim d82f6dbeb6 Remove unused variables 2021-12-29 12:18:22 +01:00
José Valim 8af30017f2 Do not raise when diffing unknown bindings in guards 2021-12-29 12:06:41 +01:00
Dwi Prihandi fd135405a4 Fix typespec on Base.decode16! (#11526) 2021-12-27 10:30:58 +01:00
Eksperimental 73d3f7dcb7 Remove reference to t:Enum.t/0 in Typespecs page (#11521)
See discussion: https://github.com/elixir-lang/elixir/pull/11520#issuecomment-999901843
2021-12-22 23:30:29 +01:00
Thiago Santos aad9d6c069 Warn on zero arity callbacks inside protocols (#11519) 2021-12-22 22:01:14 +01:00
Eksperimental c384f19481 Add note about which attributes support guard notation in typespecs (#11518) 2021-12-22 20:24:33 +01:00
Eksperimental bcebc5984d Streamline t:Enumerable.t/1 docs (#11517) 2021-12-22 17:26:28 +01:00
José Valim a67ed56267 Simplify context module handling 2021-12-22 12:47:13 +01:00
José Valim 86741783b6 Ensure context modules are handled in optimized defmodule 2021-12-22 12:19:02 +01:00
José Valim 9ddd37cd17 Do not use type exclusive to Erlang/OTP 24+ 2021-12-22 10:17:15 +01:00
palexanderm 8b2cd96761 Fix failing doctest for String.replace/3 (#11515) 2021-12-22 09:24:41 +01:00
José Valim 2436f8da99 Add a note about grapheme boundaries to replace 2021-12-21 20:50:04 +01:00
Eksperimental 3ea00c6d0a Fix minor spelling (#11513) 2021-12-21 09:10:13 +01:00
Eksperimental 656fd89ef7 Link to page in current docs for current CHANGELOG (#11511) 2021-12-21 09:09:53 +01:00
Eksperimental 8a5a9a3182 Fix documentation errors found via ExDoc (#11512) 2021-12-21 09:09:23 +01:00
José Valim 0ec6cf8958 for and with special forms expect at least one arg 2021-12-20 11:33:23 +01:00
Andrea Leopardi a86ee6827e Add t:Enumerable.t/1 (#11510) 2021-12-20 10:16:34 +01:00
Thales Macedo Garitezi b9a7d5b0b3 Allow bypassing application mode validation in release spec (#11506)
Today, there is a mode validation check when doing a Mix Release that
prevents a parent application that has an application mode of
`:permanent`, for example, while a child application has mode `:load`,
as it might be unsafe.

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

To allow for better control over the mode validation check instead of
simply disabling the check completely, we introduce the
`:skip_mode_validation_for` to allow users to specify a list of
applications for which the strict application mode validations should
not be enforced.
2021-12-20 09:14:41 +01:00
Marc-André Lafortune 00aadbe8a2 Check plugins first so they can format .ex and .exs files (#11507) 2021-12-20 08:51:43 +01:00
Eksperimental af8518d234 Minor correction in TODO comment (#11508) 2021-12-20 08:27:07 +01:00
José Valim 415d6e5a2a Introduce multi-line comments to EEx via <%!-- --%> (#11505) 2021-12-19 20:48:00 +01:00
José Valim 5322d7eb27 Delete as many files even on rm_rf failures (#11502)
This makes the behaviour consistent and it may
address an error on Windows triggered by
`not_owner` errors. Closes #11501.
2021-12-18 23:09:04 +01:00
José Valim 6f6ae4a9e5 Add Code.env_for_eval/1 and Code.eval_quoted_with_env/3 2021-12-18 22:05:49 +01:00
José Valim efbaa7ece3 Simplify contract between checker and compiler 2021-12-18 22:05:49 +01:00
Mackenzie 6d5afa21dd Make Base.decode16's odd-length message more helpful (#11500) 2021-12-18 10:26:52 +01:00
Willian Frantz e93a52447d Remove double when on TODO comment (#11499) 2021-12-17 18:52:51 +01:00
Eksperimental a6af6087d5 Improve specs and docs in Macro.underscore/camelize (#11498) 2021-12-17 13:37:44 +01:00
Eksperimental 0b09d161c7 Improve docs around String pattern functionality (#11492) 2021-12-16 23:18:19 +01:00
José Valim 659261eebb Do not emit warnings on Cursor.Fragment.container_cursor_to_quoted/2 2021-12-16 16:13:39 +01:00
José Valim 2750d8a5d5 Deprecate map/filter/reject in Map and Keyword 2021-12-16 15:33:11 +01:00
José Valim ca35fe08f0 Revert "Add MapSet.map/2, MapSet.filter/2, and MapSet.reject/2 (#11493)"
The map functionality already exists in MapSet.new/2
and the other cases are not necessarily common enough.

This reverts commit 2d14273458.
2021-12-16 15:22:33 +01:00
José Valim 210bf928a8 Fix compatibility and deprecations 2021-12-16 15:21:39 +01:00
Eksperimental a28e8d4e24 Use Enumerable.t() outside Enum module (#11494)
To be consistent with b1414ee1d0
2021-12-16 15:21:28 +01:00
José Valim b1414ee1d0 Rename Enum.t to Enumerable.t for consistency 2021-12-16 13:19:39 +01:00
José Valim 57b5fd9961 Clear up MapSet's collectable implementation 2021-12-16 11:20:29 +01:00
José Valim 334730f305 Update CHANGELOG 2021-12-16 11:15:22 +01:00
José Valim 4ddc64db90 Add List.keysort/3 and performance notes to sort_by 2021-12-16 10:58:40 +01:00
palexanderm 2d14273458 Add MapSet.map/2, MapSet.filter/2, and MapSet.reject/2 (#11493) 2021-12-16 08:47:04 +01:00
José Valim a2c6f8bd54 Consider empty lists in String.split/3 and String.splitter/3 2021-12-16 08:46:46 +01:00
José Valim 458f2146d7 Make starts_with? and ends_with? consistent 2021-12-16 08:39:51 +01:00
Suss Buzz 3476a4f686 Return the original string if the list of replace patterns is empty (#11488) 2021-12-16 08:37:08 +01:00
Luka Dornhecker e6c65998a3 fix typo in Logger Runtime Configuration documentation (#11490) 2021-12-15 21:58:29 +01:00
José Valim 428af4cfd9 Add a note about chaining multiple map+filter calls 2021-12-14 22:01:18 +01:00
José Valim 4b2ee9ed49 Add pattern to Code.Fragment.surround_context/3 2021-12-14 21:58:32 +01:00
José Valim 0a248debe3 Improve DateTime docs 2021-12-14 12:21:30 +01:00
Eksperimental 8be8421b89 Reduce one level of indirection in Enum.into implementation (#11481) 2021-12-13 21:01:25 +01:00
José Valim 94a972a54c Deprecate <|> 2021-12-13 20:13:03 +01:00
Andrea Leopardi d4e29db097 Add "since" versions to some module attributes (#11479)
Namely, add a note in the docs about when "@deprecated" was introduced
as well as when "@doc" and friends started supporting keyword lists.
2021-12-13 17:09:43 +01:00
José Valim 9a9ed28a98 Ensure async streams can be consumed from a separate process 2021-12-13 15:45:28 +01:00
José Valim 79d9c70671 Further optimize Enum.into implementations 2021-12-13 15:45:28 +01:00
José Valim 2ec501e6c1 Add PartitionSupervisor (#11468)
A supervisor with that starts multiple partitions of the same child.

Certain processes may become bottlenecks in large systems.
If those processes can have their state trivially partitioned,
in a way there is no dependency between them, then they can use
the `PartitionSupervisor` to create multiple isolated and
independent partitions.

Once the `PartitionSupervisor` starts, you can dispatch to its
children using `{:via, PartitionSupervisor, {name, key}}`, where
`name` is the name of the `PartitionSupervisor` and key is used
for routing.
2021-12-13 15:07:06 +01:00
Keep Zen 4435391dc7 Fix Registry docs (#11478) 2021-12-13 11:37:02 +01:00
palexandermandPaul Mansour e5b47cd87e Fix recursive call in Keyword.replace_lazy helper (#11477)
Co-authored-by: Paul Mansour <paul.mansour@emetrotel.com>
2021-12-13 07:43:07 +01:00
Fernando Tapia Rico 4b9bb349e2 Improve Keyword docs and examples (#11476) 2021-12-12 20:47:48 +01:00
Wojtek Mach 9c600bbef7 Update CHANGELOG.md (#11475) 2021-12-12 20:08:29 +01:00
Mackenzie 1ca8086e71 Add Map.replace_lazy/3 and Keyword.replace_lazy/3 (#11474) 2021-12-12 19:26:01 +01:00
Eric Meadows-Jönsson b9b49712fd Add mix local.hex VERSION (#11473) 2021-12-12 17:10:46 +01:00
José Valim 172da446ce Improve docs for Macro.inspect_atom/2 2021-12-12 10:24:02 +01:00
José Valim 353eafb259 Consistently halt with 1 on standalone options 2021-12-11 21:30:11 +01:00
Dorgan b1bfd1fa0a Set a max line_length for Macro.to_string (#11471) 2021-12-11 10:45:18 +01:00
José Valim de7abfeb4e Handle block and multiline clauses without line length 2021-12-11 10:19:06 +01:00
Andrea Leopardi f7cbee5e74 Export types in Module (#11464)
Export t:Module.definition/0 and t:Module.def_kind/0.

They are referenced throughout the documentation for Module
so it makes sense to expose them.
2021-12-11 07:33:39 +01:00
José Valim 97ac2742f7 Improve xref docs 2021-12-10 23:23:19 +01:00
José Valim dabfc58780 Revert "Simplify inspect testing structure"
The particular error message requires OTP 24.

This reverts commit 7bac166dd4.
2021-12-10 13:09:42 +01:00
José Valim 6d00071883 Normalize important notes 2021-12-10 11:37:14 +01:00
José Valim 7bac166dd4 Simplify inspect testing structure 2021-12-10 11:13:54 +01:00
José Valim 382c6d2e0e Track environment changes from compile_env 2021-12-10 09:49:47 +01:00
José Valim 99006eb081 Do not cursor sigil after dot 2021-12-10 09:27:45 +01:00
felipe stival b13a9977d9 Change approach: never show error if line is empty (#11466) 2021-12-10 09:27:25 +01:00
José Valim a213cb46d3 Run formatter 2021-12-09 21:22:50 +01:00
José Valim e89bbdfb29 Handle improper lists on apply, closes #11465 2021-12-09 21:22:39 +01:00
José Valim 20a5050b35 Update CHANGELOG 2021-12-09 21:13:05 +01:00
José Valim 1a9e59cd4d Deprecate use Bitwise and ~~~
Use `import` instead.
2021-12-09 21:08:57 +01:00
Dorgan 2c8a9ddf53 Fix formatting of lists in module attribues (#11462) 2021-12-08 23:51:26 +01:00
José Valim 611139ef3e Fix codepoint byte counting in slice, closes #11461 2021-12-08 23:50:38 +01:00
José Valim 1eb6bdafb0 Remove unecessary @ in bat file 2021-12-08 23:50:38 +01:00
Andrea Leopardi 6cd5e861fe Add deep-merge example in docs for Config.config/3 (#11458) 2021-12-08 10:12:39 +01:00
Aaron Gunderson 1aa4633973 Mix.Tasks.Test.Coverage warns on failure (#11457)
Adds an explicit warning when exiting with an error code because of a
failed test coverage threshold.

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

Coverage test failed, threshold not met:
        Coverage:   62.35%
        Threshold: 100.00%
```
2021-12-08 08:07:04 +01:00
Chris Miller 5b8ccc55e4 Add Stream.duplicate/2 (#11456) 2021-12-07 20:42:56 +01:00
José Valim 2642226f35 Fix halt for --version 2021-12-07 08:35:17 +01:00
Zhe Cheng c633b532ab Remove doc repeat iex example for Date.end_of_week (#11454) 2021-12-07 08:24:25 +01:00
José Valim af0fd81606 Make sure --version flag halts elixir and iex, closes #11453 2021-12-07 08:18:19 +01:00
José Valim 853fd9b6b8 Only check for path as nil 2021-12-06 15:16:13 +01:00
José Valim aa41c88086 Do not deprecate URI.parse/1
Closes #11450.
2021-12-06 14:58:01 +01:00
José Valim 88bad1a3b0 Clarify summary for Keyword.validate/2 2021-12-06 11:29:33 +01:00
José Valim 822837e188 Add clarifications to Access error message 2021-12-05 20:20:37 +01:00
José Valim c329e71106 Mention Mix.install/2 on protocol consolidation 2021-12-05 19:52:33 +01:00
Parker Selbert 1f0422ac2e Specific base typespecs (#11449)
* Use specific options for base option typespecs

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

* Fix padding use in encode/decode identity test
2021-12-05 19:50:22 +01:00
José Valim 5b1b85be84 Make protocol consolidation part of the Mix.install cache 2021-12-05 19:29:53 +01:00
José Valim 475b3adccb Fix wrong autocomplete for atoms in IEx 2021-12-05 16:40:17 +01:00
José Valim c4527b3ff4 Add Macro.classify_atom/1 and Macro.inspect_atom/2 (#11446)
Today the formatter still uses some private APIs
and this commit aims to expose some of them.
In particular, Macro.classify_atom/1 was added to
expose if an atom is an alias, an identifier, quoted,
or unquoted.

Note that the API does not say anything about
operators, since they can fall within three distinct
categories:

  * unquoted and callable, such as, `+`, `-`, and
    most operators

  * unquoted and not callable, such as, `.`, `..`,
    and `..//`, which would be ambiguous when used
    as `Module.OP()`

  * quoted but callable, such as `::`, which is
    ambiguous in the atom syntax but not as calls

This information still remains private, especially
because most times operators are used, we often still
want to learn about their precedence too.

We also exposed `inspect_atom/2` with three
distinct clauses to cover the different scenarios
atoms can be inspected at runtime and in source
code.
2021-12-05 15:02:06 +01:00
José Valim 74d61e0c8d Update macro.ex 2021-12-05 14:30:04 +01:00
Eksperimental 6f98831d88 Fix spec for Code.Identifier.escape/4 (#11445)
Closes #11444
2021-12-05 10:51:52 +01:00
José Valim 4dd1301d03 Warn on ::: to avoid ambiguity
The formatter already rewrote it to :"::" since
its conception.
2021-12-05 10:40:19 +01:00
Eksperimental 5bcd0bbce9 Colorize Version.Requirement source in Inspect protocol (#11441)
This is to avoid ambiguity in cases like

    iex> Version.parse_requirement!("~> 1.2.3 or < 3.4.5 and > 5.6.7")
    #Version.Requirement<~> 1.2.3 or < 3.4.5 and > 5.6.7>
2021-12-04 22:10:49 +01:00
Wojtek Mach 4204a0a1d1 Add missing @doc since (#11443) 2021-12-04 19:57:22 +01:00
Eksperimental 9e0e0fca56 Add guards and specs to Identifier.escape/4 (#11439) 2021-12-04 15:35:05 +01:00
Aleksei Matiushkin 8c11ba5cd7 Disallow shorthand pipe after matches (#11433)
Closes #11418.
2021-12-03 20:24:09 +01:00
Kian Meng Ang 4995e06713 Update code example for unquote/1 (#11437)
The output have changed since 1.13.0-rc.0.
2021-12-03 20:23:22 +01:00
José Valim 1b1b2c84be Update CHANGELOG 2021-12-03 19:51:44 +01:00
José Valim 69fc6024aa Clarify docs for use/2 2021-12-03 19:48:51 +01:00
José Valim abc23d00c4 Decouple parser state from buffer in IEx 2021-12-03 18:56:32 +01:00
Benjamin Milde 0db4f426c7 Update microsecond typespec (#11436)
Show which part of the tuple is the value and which the precision
2021-12-03 18:34:54 +01:00
José Valim e48238950b Do not run test suite on mix test --profile-require 2021-12-03 11:13:00 +01:00
José Valim 6eb00ca7d2 Still document nil as part of the URI path, closes #11424 2021-12-02 23:23:54 +01:00
José Valim c2578c9370 Add Map.from_keys/2 and Keyword.from_keys/2 (#11432)
The map operation is optimized since Erlang/OTP 24+
as we don't need to do an additional traversal to
create tuples.
2021-12-02 21:51:22 +01:00
José Valim 6d3fa3b4f9 Unify traversal for runtime dependent modules 2021-12-02 20:16:01 +01:00
José Valim 3318d9e54e Remove pending compile_ref 2021-12-02 16:49:43 +01:00
José Valim 9591ba9b0b There is no need to check for compile time refs as they are always recompiled 2021-12-02 16:45:57 +01:00
José Valim 5d7f272eb6 Track transitive runtime dependencies coming from local deps 2021-12-02 16:38:19 +01:00
Dorgan 2dede8ea2b Fix normalization of kw list in blocks (#11431) 2021-12-02 08:21:59 +01:00
Michał Łępicki 0ff6522ab4 Fix opaque Version.Requirement usage (#11428)
Do not pattern match on internal opaque type structure
outside of the module with that opaque type definition.
Fixes related dialyzer issues.
2021-12-01 09:56:54 +01:00
felipe stival 31dd59dbd1 Add note in Float.to_{string/charlist}/1 (#11359) 2021-11-30 14:15:54 +01:00
Eksperimental a1e794fce5 Improve Version.compile_requirement/1 (#11427) 2021-11-30 11:25:19 +01:00
José Valim b131f87464 Warn if an outdated lexical tracker is given on eval 2021-11-26 23:00:18 +01:00
Moshe Paul def1b2bd1a Clarify documentation for Stream.transform/3 (#11419) 2021-11-26 21:02:51 +01:00
José Valim 3b93c6f969 Move syntax errors to parser file 2021-11-26 21:02:07 +01:00
José Valim adab68df21 Do not print control chars as unexpected tokens 2021-11-26 21:02:07 +01:00
Eric Meadows-Jönsson 0fd981a498 Add Version.to_string/1 (#11422) 2021-11-26 15:38:52 +01:00
José Valim 209688fc52 Handle outdated anonymous functions in Erlang/OTP 25 (#11421) 2021-11-26 06:47:10 +01:00
José Valim cc877ee16b Disable ssa and bool passes (#11420)
Module bodies, especially in tests, tend to be
long, which affects the performance of passes
such as beam_ssa_opt and beam_bool. This commit
disables those passes during module definition.
As an example, this makes loading Elixir's test
suite 7-8% faster.
2021-11-26 06:46:50 +01:00
José Valim f5208a39a3 Emit checker warnings on compile_string/compile_quoted, closes #11417 2021-11-25 21:14:14 +01:00
José Valim 4a88be86f8 Preserve opts while formatting failed inspect struct
Also make formatting consistent.
2021-11-25 18:11:05 +01:00
Eksperimental 6fa5018f9e Improve user experience when there is a faulty Inspect implementation (#11403) 2021-11-25 17:41:03 +01:00
José Valim 5851879430 Update CHANGELOG 2021-11-23 14:31:19 +01:00
José Valim 31f50f0961 Revert introduction of ~E sigil to avoid conflict with other projects 2021-11-23 14:29:29 +01:00
Eksperimental 6289cd6b96 Fix links to EEP 48 (#11411) 2021-11-22 13:02:23 +01:00
José Valim d0e4af9353 Skip errors on head of generated clauses, closes #11407 2021-11-21 19:33:14 +01:00
José Valim 043f1c49ec erlang.org -> www.erlang.org 2021-11-21 16:28:21 +01:00
Rudolf d6b0fcacb0 Add Registry.count_select/2 (#11405) 2021-11-20 14:35:02 +01:00
Andrea Leopardi b64afc9f67 Polish docs in Mix.Project (#11406) 2021-11-20 13:37:08 +01:00
José Valim bb386c9f51 Fix typespec for Macro.struct!/2 2021-11-19 23:15:16 +01:00
José Valim 7dbcf9aba9 Update links and include zoneinfo in docs 2021-11-19 18:51:12 +01:00
José Valim 3b52a04354 Warn on trailing commas on calls, closes #11399 2021-11-19 11:46:46 +01:00
Eksperimental 916cf08069 Make all Inspect tests asyncronous (#11402) 2021-11-18 23:09:49 +01:00
Eksperimental 85a04512b8 Document default value for :safe in Inspect.Opts (#11401) 2021-11-18 23:07:05 +01:00
Dorgan 2270c95ab3 Fix normalizer not preserving user choice on module attribute lists (#11397) 2021-11-17 15:05:37 +01:00
José Valim bbd34290cb Update CHANGELOG 2021-11-16 21:52:54 +01:00
Rudolf 4e12695f8e Add clarifications to Task.Supervisor.children/1 (#11395)
Children ignored children that are restarting.

Add a caution note about calling the function under low memory condition.
2021-11-16 21:02:58 +01:00
Wojtek Mach 6aaedc9e3f Update ~E docs (#11394) 2021-11-16 14:09:44 +01:00
José Valim 988afb3cba Add ~E sigil to EEx 2021-11-16 10:58:35 +01:00
José Valim 72bd8e1c2b Revert "Clarify risk of duplicate keys in Map.map/2 (#11360)"
This reverts commit 6074846794.
2021-11-15 21:10:34 +01:00
José Valim 39121adb57 Reject bidirectional formatting characters (#11391) 2021-11-15 21:08:37 +01:00
José Valim a618213616 Support escaping of terminators in uppercase sigils heredocs for consistency, closes #11390 2021-11-15 14:21:12 +01:00
José Valim 58455c1872 Do not raise on variable looking like an empty tuple 2021-11-13 12:37:01 +01:00
Tyler A. Young d4a2dfc336 Corrections & clarifications to Task.Supervisor docs (#11384) 2021-11-12 09:17:49 +01:00
Rudolf d9724cf660 remsh: stop evaluator before exit iex_server (#11386)
Closes #11385.
2021-11-12 09:16:10 +01:00
José Valim cf03a110ea Fix warning from call to dynamically generated module 2021-11-11 22:04:37 +01:00
Eksperimental 3947ab7117 Allow Application.compile_env* to accept attributes (#11383) 2021-11-11 22:04:20 +01:00
José Valim b2bf2a980c Ensure fixtures are executed in archive test
Closes #11382.
2021-11-11 20:16:16 +01:00
José Valim ab928366b1 Fix bootstrap 2021-11-10 19:37:21 +01:00
José Valim 34368b2680 Do not crash on duplicate bindings, closes #11378 2021-11-10 19:27:53 +01:00
José Valim 011e25fe8c Consistently return an empty list for System.stacktrace 2021-11-09 21:57:22 +01:00
José Valim f5d55062ca Update CHANGELOG 2021-11-09 18:34:01 +01:00
José Valim f0b81e8208 Deprecate features pending for v1.14 2021-11-09 18:34:01 +01:00
Eksperimental 001931fb88 Update remaining references to master branch with main (#11376)
These two have been left out in 38dce7b2cc
2021-11-09 17:46:08 +01:00
José Valim e0c60c68a0 Do not extract comments without a newline 2021-11-08 18:01:25 +01:00
José Valim 64c2248aa4 Use keyword syntax by default in tuple formatting 2021-11-08 17:25:25 +01:00
Wojtek Mach 18a0e0a275 Add Mix.installed?/0 (#11374) 2021-11-08 13:13:58 +01:00
José Valim 38dce7b2cc Update references to main branch 2021-11-07 20:07:38 +01:00
José Valim 79cd891eb8 Keep yes?/1 as a callback, closes #11372 2021-11-07 12:25:03 +01:00
Guillaume Duboc f9ec2e7c5b Fix formatting of map-like types in warnings (#11351)
This came up in issue #11204:

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

To address this, we recursively check for maps inside
of a union type when comparing it to another map-like type.
2021-11-07 09:30:08 +01:00
Akshay Narisetti be6cf224cd Fix some grammatical errors in README.md (#11366) 2021-11-05 07:57:36 +01:00
José Valim 2425ed7152 Do not reinstall dependencies on same Mix.install/2 2021-11-05 00:05:22 +01:00
Michel Boaventura e6a8b3c001 Minor typo fix (#11365) 2021-11-04 14:42:15 +01:00
José Valim 6ce87e28b9 Fix regression on URI.parse/1, closes #11363 2021-11-04 08:46:16 +01:00
José Valim 9fea92e87d Ensure structs can be in release configs, closes #11364 2021-11-04 08:41:03 +01:00
Eksperimental aa4eeae7e6 Use ERTS instead of erts (#11362) 2021-11-04 08:18:19 +01:00
Tyler A. Young 6074846794 Clarify risk of duplicate keys in Map.map/2 (#11360) 2021-11-03 23:46:05 +01:00
Tyler A. Young 368c311077 Improvements to Enum.slide/3 (#11361)
- Support negative insertion indices
- Give a clear RuntimeError, rather than a baffling CondClauseError, when you ask for an insertion point that matches the last element of your range
2021-11-03 23:41:57 +01:00
José Valim e6118608d6 Fix broken link in Task docs 2021-11-01 22:30:34 +01:00
José Valim 8fa32c960c Start v1.14.0-dev 2021-11-01 20:57:18 +01:00
362 changed files with 37869 additions and 8597 deletions
+2 -2
View File
@@ -23,10 +23,10 @@ test_freebsd_task:
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
PATH: $PATH:/usr/local/lib/erlang22/bin
PATH: $PATH:/usr/local/lib/erlang24/bin
install_script:
- pkg install -y erlang-runtime22 git gmake
- pkg install -y erlang-runtime24 git gmake
- rm -rf .git
- gmake compile
+2 -1
View File
@@ -14,5 +14,6 @@
# Errors tests
assert_eval_raise: 3
]
],
normalize_bitstring_modifiers: false
]
-21
View File
@@ -1,21 +0,0 @@
### Precheck
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
* For bugs, do a quick search and make sure the bug has not yet been reported
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
* Do not use the issues tracker for guidance, questions or support (try Elixir Forum, Stack Overflow, Slack, etc. instead)
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang/OTP versions (elixir --version):
* Operating system:
### Current behavior
Include code samples, errors and stacktraces if appropriate.
If reporting a bug, please include the reproducing steps.
### Expected behavior
A short description on how you expect the code to behave.
+11
View File
@@ -0,0 +1,11 @@
---
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
@@ -0,0 +1,53 @@
---
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
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
+32 -9
View File
@@ -8,22 +8,29 @@ env:
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
permissions:
contents: read
jobs:
test_linux:
name: Linux, ${{ matrix.otp_release }}, Ubuntu 18.04
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
development: [false]
include:
- otp_release: OTP-25.0
otp_latest: true
- otp_release: OTP-24.3
- otp_release: OTP-24.0
- otp_release: OTP-23.3
- otp_release: OTP-23.0
- otp_release: master
development: true
- otp_release: maint
development: true
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 50
- name: Install Erlang/OTP
@@ -38,11 +45,12 @@ 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: Dyalizer
- name: Run Dialyzer
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
@@ -52,22 +60,37 @@ jobs:
continue-on-error: ${{ matrix.development }}
- name: Check reproducible builds
run: taskset 1 make check_reproducible
if: matrix.otp_release == 'OTP-24.0'
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
test_windows:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
strategy:
matrix:
otp_release: ['22.3']
otp_release: ['23.3']
runs-on: windows-2019
steps:
- name: Configure Git
run: git config --global core.autocrlf input
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 50
- name: Cache Erlang/OTP package
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: C:\Users\runneradmin\AppData\Local\Temp\chocolatey\erlang
key: OTP-${{ matrix.otp_release }}-windows-2019
@@ -92,7 +115,7 @@ jobs:
name: Check POSIX-compliant
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 50
- name: Install Shellcheck
+72
View File
@@ -0,0 +1,72 @@
# #!/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
@@ -0,0 +1,28 @@
name: Notify
on:
release:
types:
- published
permissions:
contents: read
jobs:
notify:
runs-on: ubuntu-18.04
name: Notify
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- uses: erlef/setup-beam@v1
with:
otp-version: 24.3
elixir-version: 1.13.4
- name: Run Elixir script
env:
ELIXIR_FORUM_TOKEN: ${{ secrets.ELIXIR_FORUM_TOKEN }}
ELIXIR_LANG_ANN_TOKEN: ${{ secrets.ELIXIR_LANG_ANN_TOKEN }}
run: |
elixir .github./workflows/notify.exs ${{ github.ref_name }}
+96
View File
@@ -0,0 +1,96 @@
name: Release
on:
push:
tags:
- v*
env:
ELIXIR_OPTS: "--warnings-as-errors"
ERLC_OPTS: "warnings_as_errors"
LANG: C.UTF-8
jobs:
create_draft_release:
permissions:
contents: write
runs-on: ubuntu-18.04
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Create draft release
run: |
gh release create \
--repo ${{ github.repository }} \
--title ${{ github.ref_name }} \
--notes '' \
--draft \
${{ github.ref_name }}
release_pre_built:
needs: create_draft_release
strategy:
fail-fast: true
matrix:
include:
- otp: 23
otp_version: 23.3
- otp: 24
otp_version: 24.3
- otp: 25
otp_version: 25.0
build_docs: build_docs
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 50
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp_version }}
version-type: strict
- name: Build Elixir Release
run: |
make Precompiled.zip
mv Precompiled.zip elixir-otp-${{ matrix.otp }}.zip
shasum -a 1 elixir-otp-${{ matrix.otp }}.zip > elixir-otp-${{ matrix.otp }}.zip.sha1sum
shasum -a 256 elixir-otp-${{ matrix.otp }}.zip > elixir-otp-${{ matrix.otp }}.zip.sha256sum
echo "$PWD/bin" >> $GITHUB_PATH
- name: Upload Pre-built
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload --clobber "${{ github.ref_name }}" \
elixir-otp-${{ matrix.otp }}.zip \
elixir-otp-${{ matrix.otp }}.zip.sha{1,256}sum
- name: Get latest stable ExDoc version
if: ${{ matrix.build_docs }}
run: |
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
echo "EX_DOC_LATEST_STABLE_VERSION=${EX_DOC_LATEST_STABLE_VERSION}" >> $GITHUB_ENV
- uses: actions/checkout@v3
if: ${{ matrix.build_docs }}
with:
repository: elixir-lang/ex_doc
ref: v${{ env.EX_DOC_LATEST_STABLE_VERSION }}
path: ex_doc
- name: Build ex_doc
if: ${{ matrix.build_docs }}
run: |
mv ex_doc ../ex_doc
cd ../ex_doc
../elixir/bin/mix do local.rebar --force + local.hex --force + deps.get + compile
cd ../elixir
- name: Build Docs
if: ${{ matrix.build_docs }}
run: |
make Docs.zip
shasum -a 1 Docs.zip > Docs.zip.sha1sum
shasum -a 256 Docs.zip > Docs.zip.sha256sum
- name: Upload Docs
if: ${{ matrix.build_docs }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload --clobber "${{ github.ref_name }}" \
Docs.zip \
Docs.zip.sha{1,256}sum
+389 -213
View File
@@ -1,288 +1,464 @@
# Changelog for Elixir v1.13
# Changelog for Elixir v1.14
The focus behind Elixir v1.13 has been on tooling, mainly tooling related to code formatting, code fragments, code reflection, and code recompilation. A lot of this functionality will directly impact developers working on large codebases and provide meaningful quality of life improvements for those working on Elixir tooling and environments, such as IDEs, notebooks, etc.
Elixir v1.14 brings many improvements to the debugging experience in Elixir
and data-type inspection. It also includes a new abstraction for easy
partitioning of processes called `PartitionSupervisor`, as well as improved
compilation times and error messages.
## Semantic recompilation
Elixir v1.14 is the last version to support Erlang/OTP 23. Consider updating
to Erlang/OTP 24 or Erlang/OTP 25.
Elixir v1.13 comes with many improvements to the compiler, so it recompiles your files less frequently. In particular:
## `dbg`
* The digest of the files are considered in addition to their size. This avoids recompiling many files when switching or rebasing branches.
`Kernel.dbg/2` is a new macro that's somewhat similar to `IO.inspect/2`, but
specifically tailored for **debugging**.
* Changing your `mix.exs` will no longer trigger a full recompilation, unless you specifically change the configurations used by the Elixir compiler (`:elixirc_paths` and `:elixirc_options`).
* Changing compile-time configuration files (`config/config.exs` and any other file imported from it) now only recompiles the project files that depend on the reconfigured applications, instead of a full recompilation. However, if you change the configuration of your application itself, the whole project is still recompiled.
* Adding, updating or removing a dependency now only recompiles the project files that depend on the modified a dependency.
* If your project has both Erlang and Elixir files, changing an Erlang file will now recompile only the Elixir files that depend on it.
In a nutshell, Elixir went from triggering full recompilations whenever any of `mix.exs`, `config/config.exs`, `src/*`, and `mix.lock` changed on disk to semantic recompilations. Now it only fully recompiles when:
* you change the compilation options in `mix.exs`
* you change the configuration for the current project in `config/config.exs`
## mix xref
`mix xref` is a tool that analyzes relationships between files. By analyzing the compile-time and runtime dependencies between files, it allows developers to understand what files have to be recompiled whenever a file changes.
Elixir v1.13 comes with many improvements to `mix xref`, such as:
* `mix xref graph` now supports `--label` to be set to "compile-connected", which returns all compile-time dependencies that lead to additional transitive dependencies.
* A new `mix xref trace FILE` subcommand receives a file and returns all dependencies in said file, including the line and what caused said dependency (a function/macro call, an alias, a struct, etc).
* All `mix xref` subcommands support the `--fail-above` flag, which allows you to enforce your project has at most a certain number of compile-time cycles, transitive compile-time dependencies, etc.
* `mix xref graph` now supports multiple `--sink` and `--source` to be given.
With these improvements, it has become simpler to understand the impact code recompilation has in our codebases and how to limit it.
## Code fragments
The `Code` module got a companion module called `Code.Fragment`, which hosts functions that work on incomplete code, as is often the scenario in editors, interactive shells, etc. The module contains different heuristics to analyze the source code and return context informational.
Thanks to these improvements, `IEx`' autocomplete got several quality of life improvements, such as the autocompletion of sigils, structs, and paths. For example, typing `~<TAB>` now shows:
```iex
iex(1)> ~
~C (sigil_C) ~D (sigil_D) ~N (sigil_N) ~R (sigil_R)
~S (sigil_S) ~T (sigil_T) ~U (sigil_U) ~W (sigil_W)
~c (sigil_c) ~r (sigil_r) ~s (sigil_s) ~w (sigil_w)
```
Adding the sigil letter and pressing tab then shows the available delimiters:
```iex
iex(1)> ~r
" """ ' ''' ( / < [ { |
```
Similarly, `%<TAB>` now shows only the available structs (exceptions excluded), instead of all modules:
When called, it prints the value of whatever you pass to it, plus the debugged
code itself as well as its location. This code:
```elixir
iex(1)> %File.St
File.Stat File.Stream
# In my_file.exs
feature = %{name: :dbg, inspiration: "Rust"}
dbg(feature)
dbg(Map.put(feature, :in_version, "1.14.0"))
```
Once you define the struct, you can hit `tab` to show all struct fields available:
Prints this:
```elixir
iex(1)> %URI{
authority: fragment: host: path: port:
query: scheme: userinfo:
```shell
$ elixir my_file.exs
[my_file.exs:2: (file)]
feature #=> %{inspiration: "Rust", name: :dbg}
[my_file.exs:3: (file)]
Map.put(feature, :in_version, "1.14.0") #=> %{in_version: "1.14.0", inspiration: "Rust", name: :dbg}
```
As you fill them in, the already filled structs no longer show up:
`dbg/2` can do more. It's a macro, so it *understands Elixir code*. You can see
that when you pass a series of `|>` pipes to it. `dbg/2` will print the value
for every step of the pipeline. This code:
```elixir
iex(1)> %URI{path: "/example",
authority: fragment: host: port: query:
scheme: userinfo:
# In dbg_pipes.exs
__ENV__.file
|> String.split("/", trim: true)
|> List.last()
|> File.exists?()
|> dbg()
```
Finally, new compilation tracers have been added, alongside a handful of functions in `Module` to retrieve module metadata, which can be used to enrich suggestions in programming environments.
Prints this:
## Extended code formatting
The `mix format` task has been augmented with the notion of plugins. Plugins can teach the formatter how to format new files and how to format sigils, via the `Mix.Tasks.Format` behaviour.
For example, imagine that your project uses Markdown in two distinct ways: via a custom `~M` sigil and via files with the `.md` and `.markdown` extensions. A custom plugin would look like this:
```elixir
defmodule MixMarkdownFormatter do
@behaviour Mix.Tasks.Format
def features(_opts) do
[sigils: [:M], extensions: [".md", ".markdown"]]
end
def format(contents, opts) do
# logic that formats markdown
end
end
```shell
$ elixir dbg_pipes.exs
[dbg_pipes.exs:5: (file)]
__ENV__.file #=> "/home/myuser/dbg_pipes.exs"
|> String.split("/", trim: true) #=> ["home", "myuser", "dbg_pipes.exs"]
|> List.last() #=> "dbg_pipes.exs"
|> File.exists?() #=> true
```
Now any application can use your formatter as follows:
### IEx and Prying
`dbg/2` supports configurable backends. IEx automatically replaces the default
backend by one that halts the code execution with `IEx.Pry`, giving developers
the option to access local variables, imports, and more. This also works with
pipelines: if you pass a series of `|>` pipe calls to `dbg` (or pipe into it at the
end, like `|> dbg()`), you'll be able to step through every line in the pipeline.
You can keep the default behaviour by passing the `--no-pry` option to IEx.
## PartitionSupervisor
`PartitionSupervisor` is a new module that implements a new supervisor type. The
partition supervisor is designed to help with situations where you have a single
supervised process that becomes a bottleneck. If that process's state can be
easily partitioned, then you can use `PartitionSupervisor` to supervise multiple
isolated copies of that process running concurrently, each assigned its own
partition.
For example, imagine you have an `ErrorReporter` process that you use to report
errors to a monitoring service.
```elixir
# .formatter.exs
[
# Define the desired plugins
plugins: [MixMarkdownFormatter],
# Remember to update the inputs list to include the new extensions
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "posts/*.{md,markdown}"]
# Application supervisor:
children = [
# ...,
ErrorReporter
]
Supervisor.start_link(children, strategy: :one_for_one)
```
As the concurrency of your application goes up, the `ErrorReporter` process
might receive requests from many other processes and eventually become a
bottleneck. In a case like this, it could help to spin up multiple copies of the
`ErrorReporter` process under a `PartitionSupervisor`.
```elixir
# Application supervisor
children = [
{PartitionSupervisor, child_spec: ErrorReporter, name: Reporters}
]
```
Finally, the `Code` module has also been augmented with two functions: `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`. Those functions allow someone to retrieve the Elixir AST with their original source code comments, and then convert this AST to formatted code. In other words, those functions provide a wrapper around the Elixir Code Formatter, supporting developers who wish to create tools that directly manipulate and custom format Elixir source code.
The `PartitionSupervisor` will spin up a number of processes equal to
`System.schedulers_online()` by default (most often one per core). Now, when
routing requests to `ErrorReporter` processes we can use a `:via` tuple and
route the requests through the partition supervisor.
## v1.13.0-dev
```elixir
partitioning_key = self()
ErrorReporter.report({:via, PartitionSupervisor, {Reporters, partitioning_key}}, error)
```
Using `self()` as the partitioning key here means that the same process will
always report errors to the same `ErrorReporter` process, ensuring a form of
back-pressure. You can use any term as the partitioning key.
### A Common Example
A common and practical example of a good use case for `PartitionSupervisor` is
partitioning something like a `DynamicSupervisor`. When starting many processes
under it, a dynamic supervisor can be a bottleneck, especially if said processes
take a long time to initialize. Instead of starting a single `DynamicSupervisor`,
you can start multiple:
```elixir
children = [
{PartitionSupervisor, child_spec: DynamicSupervisor, name: MyApp.DynamicSupervisors}
]
Supervisor.start_link(children, strategy: :one_for_one)
```
Now you start processes on the dynamic supervisor for the right partition.
For instance, you can partition by PID, like in the previous example:
```elixir
DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {MyApp.DynamicSupervisors, self()}},
my_child_specification
)
```
## Improved errors on binaries and evaluation
Erlang/OTP 25 improved errors on binary construction and evaluation. These improvements
apply to Elixir as well. Before v1.14, errors when constructing binaries would
often be hard-to-debug generic "argument errors". With Erlang/OTP 25 and Elixir v1.14,
more detail is provided for easier debugging. This work is part of [EEP
54](https://www.erlang.org/eeps/eep-0054).
Before:
```elixir
int = 1
bin = "foo"
int <> bin
#=> ** (ArgumentError) argument error
```
Now:
```elixir
int = 1
bin = "foo"
int <> bin
#=> ** (ArgumentError) construction of binary failed:
#=> segment 1 of type 'binary':
#=> expected a binary but got: 1
```
## Slicing with steps
Elixir v1.12 introduced **stepped ranges**, which are ranges where you can
specify the "step":
```elixir
Enum.to_list(1..10//3)
#=> [1, 4, 7, 10]
```
Stepped ranges are particularly useful for numerical operations involving
vectors and matrices (see [Nx](https://github.com/elixir-nx/nx), for example).
However, the Elixir standard library was not making use of stepped ranges in its
APIs. Elixir v1.14 starts to take advantage of steps with support for stepped
ranges in a couple of functions. One of them is `Enum.slice/2`:
```elixir
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
Enum.slice(letters, 0..5//2)
#=> ["a", "c", "e"]
```
`binary_slice/2` (and `binary_slice/3` for completeness) has been added to the
`Kernel` module, that works with bytes and also support stepped ranges:
```elixir
binary_slice("Elixir", 1..5//2)
#=> "lx"
```
## Expression-based inspection and `Inspect` improvements
In Elixir, it's conventional to implement the `Inspect` protocol for opaque
structs so that they're inspected with a special notation, resembling this:
```elixir
MapSet.new([:apple, :banana])
#MapSet<[:apple, :banana]>
```
This is generally done when the struct content or part of it is private and the
`%name{...}` representation would reveal fields that are not part of the public
API.
The downside of the `#name<...>` convention is that *the inspected output is not
valid Elixir code*. For example, you cannot copy the inspected output and paste
it into an IEx session.
Elixir v1.14 changes the convention for some of the standard-library structs.
The `Inspect` implementation for those structs now returns a string with a valid
Elixir expression that recreates the struct when evaluated. In the `MapSet`
example above, this is what we have now:
```elixir
fruits = MapSet.new([:apple, :banana])
MapSet.put(fruits, :pear)
#=> MapSet.new([:apple, :banana, :pear])
```
The `MapSet.new/1` expression evaluates to exactly the struct that we're
inspecting. This allows us to hide the internals of `MapSet`, while keeping
it as valid Elixir code. This expression-based inspection has been
implemented for `Version.Requirement`, `MapSet`, and `Date.Range`.
Finally, we have improved the `Inspect` protocol for structs so that
fields are inspected in the order they are declared in `defstruct`.
The option `:optional` has also been added when deriving the `Inspect`
protocol, giving developers more control over the struct representation.
See the updated documentation for `Inspect` for a general rundown on
the approaches and options available.
## v1.14.0-rc.1 (2022-08-15)
### 1. Enhancements
#### IEx
* [IEx.Helpers] Support sigils in `h/1`
#### Mix
* [Mix] Add `:config_path` and `:lockfile` options to `Mix.install/2`
### 2. Bug fixes
#### Elixir
* [Enum] Fix usage of range with `steps != 1` in a few functions (regression)
* [Kernel] Fix usage of range with `steps != 1` on `binary_slice/2` (regression)
* [Kernel] Recursively expand pipelines on right-hand side of `|>` (regression)
* [Kernel] Fix equality in guards for dynamic ranges without steps
* [Module] Fix loop while unifying type variables
* [System] Raise non-generic exception on missing env in `System.fetch_env!/1` to mirror map operations
#### IEx
* [IEx] Continue parsing after `--no-pry` (regression)
#### Mix
* [Mix] Properly compile-dependencies on `mix format`
## v1.14.0-rc.0 (2022-08-01)
### 1. Enhancements
#### EEx
* [EEx] Add `:parser_options` to EEx functions
* [EEx] Support multi-line comments to EEx via `<%!-- --%>`
* [EEx] Add `EEx.tokenize/2`
#### Elixir
* [Calendar] Add `c:Calendar.year_of_era/3` to support calendars where the beginning of a new era does not align with the beginning of a new year
* [CLI] Support `--short-version` on the CLI that does not boot the VM
* [Code] Add `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`
* [Code] Add more `:token_metadata` to aliases and remote calls when parsing strings
* [Code] Add `Code.Fragment` module to provide best-effort information from code fragments. The module currently provides an updated `Code.Fragment.cursor_context/2` with operator support and `Code.Fragment.surround_context/2` which looks at a given position in a fragment and find its surrounding delimiters
* [Code] Allow custom sigil formatting on `Code.format_string!/2`
* [Code] Add `{:on_module, bytecode, :none}` trace to compilation tracers
* [Enum] Optimize `Enum.concat/1` for lists of lists
* [Exception] Better format Elixir exceptions in Erlang
* [Inspect] Allow default inspect fun to be set globally with `Inspect.Opts.default_inspect_fun/1`
* [IO] Allow `:eof` to be given as limit to `IO.getn/2`
* [Kernel] Support the `:sigils` option in `import Mod, only: :sigils` and allow the sigil modifiers to be also digits
* [Kernel] Make `get_in` consistently abort when `nil` values are found
* [Kernel] Improve compilation times by reducing the amount of copies of the AST across compiler processes
* [Kernel] Raise if trying to define a module with a slash in its name
* [Kernel] Warn when `?\` is used and there is no need for a escape character
* [Kernel] Track structs in typespecs as export deps instead of compile-time deps
* [Kernel] Add power operator (`**/2`)
* [Keyword] Add `Keyword.validate/2`
* [Keyword] Implement `Keyword.filter/2` and `Keyword.map/2`
* [List] Add `List.keyfind!/3`
* [Macro] Add `Macro.prewalker/1` and `Macro.postwalker/1`
* [Macro.Env] Add the following reflection functions: `required?/2`, `lookup_import/2`, `fetch_alias/2`, and `fetch_macro_alias/2`
* [Map] Implement `Map.filter/2` and `Map.map/2`
* [Module] Support `:nillify_clauses` in `Module.get_definition/3`
* [Module] Add `Module.attributes_in/1` and `Module.overridables_in/1`
* [OptionParser] Add "did you mean?" suggestions to `OptionParser.ParseError` messages
* [Record] Add record reflection via `@__records__`
* [Task] Add `Task.completed/1`
* [Task] Add `Task.ignore/1` to keep a task running but ignoring all of its results
* [Task] Reduce the amount of copying `Task.async*` functions
* [Access] Add `Access.slice/1`
* [Application] Add `Application.compile_env/4` and `Application.compile_env!/3` to read the compile-time environment inside macros
* [Calendar] Support ISO8601 basic format parsing with `DateTime.from_iso8601/2`
* [Calendar] Add `day`/`hour`/`minute` on `add`/`diff` across different calendar modules
* [Code] Add `:normalize_bitstring_modifiers` to `Code.format_string!/2`
* [Code] Emit deprecation and type warnings for invalid options in on `Code.compile_string/2` and `Code.compile_quoted/2`
* [Code] Warn if an outdated lexical tracker is given on eval
* [Code] Add `Code.env_for_eval/1` and `Code.eval_quoted_with_env/3`
* [Code] Improve stacktraces from eval operations on Erlang/OTP 25+
* [Code.Fragment] Add support for `__MODULE__` in several functions
* [Code.Fragment] Support surround and context suggestions across multiple lines
* [Enum] Allow slicing with steps in `Enum.slice/2`
* [File] Support `dereference_symlinks: true` in `File.cp/3` and `File.cp_r/3`
* [Float] Do not show floats in scientific notation if below `1.0e16` and the fractional value is precisely zero
* [Float] Add `Float.min_finite/0` and `Float.max_finite/0`
* [Inspect] Improve error reporting when there is a faulty implementation of the `Inspect` protocol
* [Inspect] Allow `:optional` when deriving the Inspect protocol for hiding fields that match their default value
* [Inspect] Inspect struct fields in the order they are declared in `defstruct`
* [Inspect] Use expression-based inspection for `Date.Range`, `MapSet`, and `Version.Requirement`
* [IO] Support `Macro.Env` and keywords as stacktrace definitions in `IO.warn/2`
* [IO] Add `IO.ANSI.syntax_colors/0` and related configuration to be shared across IEx and `dbg`
* [Kernel] Add new `dbg/0-2` macro
* [Kernel] Allow any guard expression as the size of a bitstring in a pattern match
* [Kernel] Allow composite types with pins as the map key in a pattern match
* [Kernel] Print escaped version of control chars when they show up as unexpected tokens
* [Kernel] Warn on confusable non-ASCII identifiers
* [Kernel] Add `..` as a nullary operator that returns `0..-1//1`
* [Kernel] Implement Unicode Technical Standard #39 recommendations. In particular, we warn for confusable scripts and restrict identifiers to single-scripts or highly restrictive mixed-scripts
* [Kernel] Automatically perform NFC conversion of identifiers
* [Kernel] Add `binary_slice/2` and `binary_slice/3`
* [Kernel] Lazily expand module attributes to avoid compile-time deps
* [Kernel] Automatically cascade `generated: true` annotations on macro expansion
* [Keyword] Add `Keyword.from_keys/2` and `Keyword.replace_lazy/3`
* [List] Add `List.keysort/3` with support for a `sorter` function
* [Macro] Add `Macro.classify_atom/1` and `Macro.inspect_atom/2`
* [Macro] Add `Macro.expand_literal/2` and `Macro.path/2`
* [Macro.Env] Add `Macro.Env.prune_compile_info/1`
* [Map] Add `Map.from_keys/2` and `Map.replace_lazy/3`
* [MapSet] Add `MapSet.filter/2`, `MapSet.reject/2`, and `MapSet.symmetric_difference/2`
* [Node] Add `Node.spawn_monitor/2` and `Node.spawn_monitor/4`
* [Module] Support new `@after_verify` attribute for executing code whenever a module is verified
* [PartitionSupervisor] Add `PartitionSupervisor` that starts multiple isolated partitions of the same child for scalability
* [Path] Add `Path.safe_relative/1` and `Path.safe_relative_to/2`
* [Registry] Add `Registry.count_select/2`
* [Stream] Add `Stream.duplicate/2` and `Stream.transform/5`
* [String] Support empty lookup lists in `String.replace/3`, `String.split/3`, and `String.splitter/3`
* [String] Allow slicing with steps in `String.slice/2`
* [Task] Add `:zip_input_on_exit` option to `Task.async_stream/3`
* [Task] Store `:mfa` in the `Task` struct for reflection purposes
* [URI] Add `URI.append_query/2`
* [Version] Add `Version.to_string/1`
* [Version] Colorize `Version.Requirement` source in the `Inspect` protocol
#### ExUnit
* [ExUnit.CaptureIO] Add `with_io/3` to return result with captured io
* [ExUnit.CaptureLog] Add `with_log/2` to return result with captured logs
* [ExUnit] Add `ExUnit.Callbacks.start_link_supervised!/2`
* [ExUnit] Add `ExUnit.run/1` to rerun test modules
* [ExUnit] Colorize summary in yellow with message when all tests are excluded
* [ExUnit] Display friendly error when test name is too long
#### IEx
* [IEx.Autocomplete] Add path autocompletion whenever when the cursor follows `"./` or `"/` or `"DRIVER:` where `DRIVER` is a single letter
* [IEx.Autocomplete] Add autocompletion for sigils, struct names, and struct fields
* [IEx.Helpers] Allow multiple modules to be given to `r/1`
* [IEx] Evaluate `--dot-iex` line by line
* [IEx] Add line-by-line evaluation of IEx breakpoints
* [IEx.Autocomplete] Autocomplete bitstrings modifiers (after `::` inside `<<...>>`)
* [IEx.Helpers] Allow an atom to be given to `pid/1`
#### Logger
* [Logger] Add `Logger.put_application_level/2`
* [Logger] Add `Logger.put_process_level/2`
#### Mix
* [mix archive.install] Run `loadconfig` before building archive
* [mix compile] Move Elixir version check to before deps are compiled, in order to give feedback earlier
* [mix compile.elixir] Do not recompile files if their modification time change but their contents are still the same and the .beam files are still on disk
* [mix compile.elixir] Do not recompile all Elixir sources when Erlang modules change, only dependent ones
* [mix compile.elixir] Do not recompile Elixir files if `mix.exs` changes, instead recompile only files using `Mix.Project` or trigger a recompilation if a compiler option changes
* [mix compile.elixir] Only recompile needed files when a dependency is added, updated or removed
* [mix compile.elixir] Only recompile needed files when a dependency is configured
* [mix deps] Add `:subdir` option to git deps
* [mix escript.install] Run `loadconfig` before building escript
* [mix format] Support `:plugins` in `mix format` that can hook into custom extensions and sigils
* [mix format] Add `Mix.Tasks.Format.formatter_for_file/2`
* [mix local.rebar] No longer support `sub_dirs` in Rebar 2 to help migration towards Rebar 3
* [mix local.rebar] Support `--if-missing` option when installing Rebar
* [mix local.rebar] Set `REBAR_PROFILE=prod` when compiling Rebar dependencies
* [mix test] Support `--profile-require=time` to profile the time loading test files themselves
* [mix test] Allow filtering modules from coverage using regex
* [mix test] Allow the exit status of ExUnit to be configured and set the default to 2
* [mix test] Exit with a status of 3 when coverage falls below threshold
* [mix test] Write failed manifest when suite fails due to --warnings-as-errors
* [mix test] Ignore `MIX_TEST_PARTITION` when partitions set to 1
* [mix xref] Support multiple sinks and sources in `mix xref graph`
* [mix xref] Add `trace` subcommand to print compilation dependencies between files
* [mix xref] Add `--fail-above` option to `mix xref`
* [mix xref] Add `--label compile-connected` to `mix xref`
* [mix compile] Add `--no-optional-deps` to skip optional dependencies to test compilation works without optional dependencies
* [mix compile] Include column information on error diagnostics when possible
* [mix deps] `Mix.Dep.Converger` now tells which deps formed a cycle
* [mix do] Support `--app` option to restrict recursive tasks in umbrella projects
* [mix do] Allow using `+` as a task separator instead of comma
* [mix format] Support filename in `mix format -` when reading from stdin
* [mix format] Compile if `mix format` plugins are missing
* [mix new] Do not allow projects to be created with application names that conflict with multi-arg Erlang VM switches
* [mix profile] Return the return value of the profiled function
* [mix release] Make BEAM compression opt-in
* [mix release] Let `:runtime_config_path` accept `false` to skip the `config/runtime.exs`
* [mix test] Improve error message when suite fails due to coverage
* [mix test] Support `:test_elixirc_options` and default to not generating docs nor debug info chunk for tests
* [mix xref] Support `--group` flag in `mix xref graph`
### 2. Bug fixes
#### EEx
* [EEx] Accept EEx expressions where `->` is followed by newline
#### Elixir
* [Application] Warn if `Application.compile_env` or `Application.compile_env!` are called without a require
* [Code] Make sure `:static_atoms_encoder` in `Code.string_to_quoted/2` also applies to quoted keyword keys
* [Code] Ensure bindings with no context are returned as atoms instead of `{binding, nil}` in eval operations
* [Kernel] Raise if `__CALLER__` or `__ENV__` or `__STACKTRACE__` are used in match
* [Kernel] Improve error message on invalid argument for `byte_size` from binary concat
* [Kernel] Raise when aliasing non-Elixir modules without `:as`
* [Kernel] Allow `unquote_splicing` inside `%{...}` without parens
* [Kernel] Ensure that waiting on a struct expansion inside a typespec is correctly tracked as waiting time in the compiler
* [Kernel] Correctly parse the atom `.` as a keyword list key
* [Kernel] Do not leak variables from the first generator in `with` and `for` special forms
* [Kernel] Fix column number on strings with NFD characters
* [Kernel] Fix a bug where a combination of dynamic line in `quote` with `unquote` of remote calls would emit invalid AST metadata
* [OptionParser] Validate switch types/modifiers early on to give more precise feedback
* [Protocol] Add `defdelegate` to the list of unallowed macros inside protocols as protocols do not allow function definitions
* [Protocol] Warn if `@callback`, `@macrocallback` and `@optional_callbacks` are defined inside protocol
* [Protocol] Ensure protocol metadata is deterministic on consolidation
* [Range] Always show step when range is descending
* [String] Update Unicode database to version 14.0
* [URI] Only percent decode if followed by hex digits (according to https://url.spec.whatwg.org/#percent-decode)
* [Version] Ensure proper precedence of `and`/`or` in version requirements
* [Calendar] Handle widths with "0" in them in `Calendar.strftime/3`
* [CLI] Improve errors on incorrect `--rpc-eval` usage
* [CLI] Return proper exit code on Windows
* [Code] Do not emit warnings when formatting code
* [Enum] Allow slices to overflow on both starting and ending positions
* [Kernel] Do not allow restricted characters in identifiers according to UTS39
* [Kernel] Define `__exception__` field as `true` when expanding exceptions in typespecs
* [Kernel] Warn if any of `True`, `False`, and `Nil` aliases are used
* [Kernel] Warn on underived `@derive` attributes
* [Kernel] Remove compile-time dependency from `defimpl :for`
* [Kernel] Track all arities on imported functions
* [Protocol] Warn if a protocol has no definitions
* [Regex] Show list options when inspecting a Regex manually defined with `Regex.compile/2`
* [String] Allow slices to overflow on both starting and ending positions
#### ExUnit
* [ExUnit] Fix formatter and counters from `ExUnit.run/0` to consider all tests in a module whenever if a module's `setup_all` fails
* [ExUnit] Allow doctests newlines to be terminated by CRLF
* [ExUnit] Do not crash when diffing unknown bindings in guards
* [ExUnit] Properly print diffs when comparing improper lists with strings at the tail position
* [ExUnit] Add short hash to `tmp_dir` in ExUnit to avoid test name collision
* [ExUnit] Do not store logs in the CLI formatter (this reduces memory usage for suites with `capture_log`)
* [ExUnit] Run `ExUnit.after_suite/1` callback even when no tests run
* [ExUnit] Fix scenario where `setup` with imported function from within `describe` failed to compile
#### IEx
* [IEx] Fix the loss of `.iex.exs` context after a pry session
* [IEx] Disallow short-hand pipe after matches
* [IEx] Fix `exports/1` in IEx for long function names
#### Mix
* [mix compile.elixir] Fix `--warnings-as-errors` when used with `--all-warnings`
* [mix compile.elixir] Ensure semantic recompilation cascades to path dependencies
* [mix compile.elixir] Lock the compiler to avoid concurrent usage
* [mix format] Do not add new lines if the formatted file is empty
* [mix release] Only set `RELEASE_MODE` after `env.{sh,bat}` are executed
* [mix release] Allow application mode configuration to cascade to dependencies
* [mix xref] Do not emit already consolidated warnings during `mix xref trace`
* [Mix] Do not start apps with `runtime: false` on `Mix.install/2`
### 3. Soft deprecations (no warnings emitted)
#### Elixir
* [File] Passing a callback as third argument to `File.cp/3` and `File.cp_r/3` is deprecated.
Instead pass the callback the `:on_conflict` key of a keyword list
#### EEx
* [EEx] Using `<%# ... %>` for comments is deprecated. Please use `<% # ... %>` or the new multi-line comments with `<%!-- ... --%>`
#### Logger
* [Logger] Raise clear error message for invalid `:compile_time_purge_matching` configuration
* [Logger] Deprecate `Logger.enable/1` and `Logger.disable/1` in favor of `Logger.put_process_level/2`
#### Mix
* [mix compile.elixir] Recompile file if `@external_resource` is deleted
* [mix compile.elixir] Print number of compiling files on all compiler cycles. This will make the `Compiling N files (.ex)` show up multiple times if necessary
* [mix deps] Raise if local dep is unavailable while compiling
* [mix deps.unlock] Fix blank output when dependency is not locked
* [mix local.install] Do not respect `MIX_DEPS_PATH` for install commands
* [mix release] Improve release scripts to make sure shell errors cascade by avoiding exporting and defining variables at once
* [mix release] Do not boot release if RELEASE_COOKIE is empty
* [mix release] Allow release running as a daemon to be restarted
* [mix test] Allow coverage engine to also tag `case`, `cond`, and `receive` branches where the right side is a literal
* [Mix.Shell] Add `default` option to `Mix.Shell.yes?`
* [mix cmd] The `--app` option in `mix cmd CMD` is deprecated in favor of the more efficient `mix do --app app cmd CMD`
### 3. Soft-deprecations (no warnings emitted)
### 4. Hard deprecations
#### Elixir
* [IO] `:all` on `IO.getn` is deprecated in favor of `:eof`
* [Code] Environment options in `Code.eval_quoted/3` and `Code.eval_string/3`, such as `:aliases` and `:tracers`, have been deprecated in favor of passing an environment
* [Application] Calling `Application.get_env/3` and friends in the module body is now discouraged, use `Application.compile_env/3` instead
* [Bitwise] `use Bitwise` is deprecated, use `import Bitwise` instead
* [Bitwise] `~~~` is deprecated in favor of `bnot` for clarity
* [Kernel.ParallelCompiler] Returning a list or two-element tuple from `:each_cycle` is deprecated, return a `{:compile | :runtime, modules, warnings}` tuple instead
* [Kernel] Deprecate the operator `<|>` to avoid ambiguity with upcoming extended numerical operators
* [String] Deprecate passing a binary compiled pattern to `String.starts_with?/2`
#### Logger
* [Logger] Deprecate `$levelpad` on message formatting
#### Mix
* [mix format] `Mix.Tasks.Format.formatter_opts_for_file/2` is deprecated in favor of `Mix.Tasks.Format.formatter_for_file/2`
* [Mix] `Mix.Tasks.Xref.calls/1` is deprecated in favor of compilation tracers
### 4. Hard-deprecations
#### Elixir
* [Code] `Code.cursor_context/2` is deprecated, use `Code.Fragment.cursor_context/2` instead
* [Macro] `Macro.to_string/2` is deprecated, use `Macro.to_string/1` instead
* [System] `System.get_pid/0` is deprecated, use `System.pid/0` instead
* [Version] Using `!` or `!=` in version requirements is deprecated, use `~>` or `>=` instead
### 5. Backwards incompatible changes
#### Mix
* [mix escript.build] `:strip_beam` option is deprecated in favor of `:strip_beams`
* [Mix] `:exit_code` in `Mix.raise/2` has been deprecated in favor of `:exit_status`
* [Mix.Config] `Mix.Config` is deprecated in favor of `Config` module
* [mix local.rebar] Remove support for rebar2, which has not been updated in 5 years, and is no longer supported on recent Erlang/OTP versions
## v1.12
## v1.13
The CHANGELOG for v1.12 releases can be found [in the v1.12 branch](https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md).
The CHANGELOG for v1.13 releases can be found [in the v1.13 branch](https://github.com/elixir-lang/elixir/blob/v1.13/CHANGELOG.md).
+21 -29
View File
@@ -2,8 +2,9 @@ PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
#CANONICAL := MAJOR.MINOR/
CANONICAL ?= master/
CANONICAL := 1.14/
CANONICAL ?= main/
DOCS_FORMAT ?= html
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
@@ -28,9 +29,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 >= 22)])' -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 >= 23)])' -s erlang halt | grep -q '^true'; \
if [ $$? != 0 ]; then \
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
echo "At least Erlang/OTP 23.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -92,10 +93,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)
@@ -105,6 +106,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/tokenizer.ex -o lib/elixir/ebin;
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
@@ -126,7 +128,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..."
@@ -144,7 +146,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/
@@ -158,7 +160,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
@@ -171,14 +173,14 @@ 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
COMPILE_DOCS = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
@@ -220,24 +222,14 @@ docs_logger: compile ../ex_doc/bin/ex_doc
#==> Zip tasks
Docs.zip: docs
rm -f Docs-v$(VERSION).zip
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
rm -f Docs.zip
zip -9 -r Docs.zip CHANGELOG.md doc NOTICE LICENSE README.md
@ echo "Docs file created $(CURDIR)/Docs.zip"
Precompiled.zip: build_man compile
rm -f Precompiled-v$(VERSION).zip
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
zips: Precompiled.zip Docs.zip
@ echo ""
@ echo "### Checksums"
@ echo ""
@ shasum -a 1 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
@ shasum -a 512 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
@ shasum -a 1 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
@ shasum -a 512 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
@ echo ""
rm -f Precompiled.zip
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
@ echo "Precompiled file created $(CURDIR)/Precompiled.zip"
#==> Test tasks
@@ -295,7 +287,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 hipe 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 sasl
clean_plt:
$(Q) rm -f $(PLT)
@@ -334,4 +326,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
+14 -7
View File
@@ -1,6 +1,7 @@
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
<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">
[![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)
[![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)
Elixir is a dynamic, functional language designed for building scalable
and maintainable applications.
@@ -114,7 +115,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 the Elixir's standard
`make test_ex_unit`. If you just changed something in 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
@@ -126,6 +127,12 @@ 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
@@ -164,12 +171,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 "Allow edits from maintainers"
rather manage all changes yourself, you can disable the "Allow edits from maintainers"
feature when submitting your pull request.
The Elixir team may optionally assign someone to review a pull request.
@@ -188,8 +195,8 @@ to be installed and built alongside Elixir:
```sh
# After cloning and compiling Elixir, in its parent directory:
git clone git://github.com/elixir-lang/ex_doc.git
cd ex_doc && ../elixir/bin/mix do deps.get, compile
git clone https://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:
+17 -13
View File
@@ -10,19 +10,13 @@
4. Update "Compatibility and Deprecations" if a new OTP version is supported
5. Commit changes above with title "Release vVERSION" and generate a new tag
5. Commit changes above with title "Release vVERSION", generate a new tag, and push it
6. Run `make clean test` to ensure all tests pass from scratch and the CI is green
6. Wait until GitHub Actions publish artifacts to the draft release and the CI is green
7. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
7. Copy the relevant bits from /CHANGELOG.md to the GitHub release and publish it
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
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`
## Creating a new vMAJOR.MINOR branch
@@ -32,14 +26,24 @@
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
3. Commit "Prepare vMAJOR.MINOR for release"
3. Commit "Branch out vMAJOR.MINOR"
### Back in master
### Back in main
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
2. Start new /CHANGELOG.md
3. Update tables in /SECURITY.md in "Compatibility and Deprecations"
3. Update tables in /SECURITY.md and 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
+4 -5
View File
@@ -6,18 +6,17 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
Elixir version | Support
:------------- | :-----------------------------
1.13 | Development
1.12 | Bug fixes and security patches
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
1.9 | Security patches only
1.8 | 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.
All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
Security notifications [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.13.0-dev
1.14.0-rc.1
+20 -15
View File
@@ -1,7 +1,7 @@
#!/bin/sh
set -e
ELIXIR_VERSION=1.13.0-dev
ELIXIR_VERSION=1.14.0-rc.1
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
cat <<USAGE >&2
@@ -16,7 +16,7 @@ Usage: $(basename "$0") [options] [.exs file] [data]
-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
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
--app APP Starts the given app and its dependencies (*)
--erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -100,32 +100,29 @@ LENGTH=$#
set -- "$@" -extra
while [ $I -le $LENGTH ]; do
S=1
# S counts to be shifted, C counts to be copied
S=0
C=0
case "$1" in
+iex)
set -- "$@" "$1"
C=1
MODE="iex"
;;
+elixirc)
set -- "$@" "$1"
C=1
MODE="elixirc"
;;
-v|--no-halt)
set -- "$@" "$1"
C=1
;;
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex)
S=2
set -- "$@" "$1" "$2"
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex|--no-pry)
C=2
;;
--rpc-eval)
S=3
set -- "$@" "$1" "$2" "$3"
;;
--detached)
echo "warning: the --detached option is deprecated" >&2
ERL="$ERL -detached"
C=3
;;
--hidden)
S=1
ERL="$ERL -hidden"
;;
--logger-otp-reports)
@@ -186,6 +183,7 @@ while [ $I -le $LENGTH ]; do
fi
;;
--werl)
S=1
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
;;
*)
@@ -198,6 +196,13 @@ 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
+15 -5
View File
@@ -1,6 +1,6 @@
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
set ELIXIR_VERSION=1.13.0-dev
set ELIXIR_VERSION=1.14.0-rc.1
setlocal enabledelayedexpansion
if ""%1""=="""" if ""%2""=="""" goto documentation
@@ -23,7 +23,7 @@ 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
echo -v, --version Prints Erlang/OTP and Elixir versions (standalone)
echo.
echo --app APP Starts the given app and its dependencies (*)
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
@@ -141,6 +141,7 @@ if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && go
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
if ""==!par:--dot-iex=! (set "parsElixir=!parsElixir! --dot-iex %1" && shift && goto startloop)
if ""==!par:--no-pry=! (set "parsElixir=!parsElixir! --no-pry" && goto startloop)
rem ******* ERLANG PARAMETERS **********************
if ""==!par:--boot=! (set "parsErlang=!parsErlang! -boot %1" && shift && goto startloop)
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var %1 %2" && shift && shift && goto startloop)
@@ -174,10 +175,19 @@ if %errorlevel% == 0 (
if not !runMode! == "iex" (
set beforeExtra=-noshell -s elixir start_cli !beforeExtra!
)
if defined useWerl (
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
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!
)
) else (
"!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 (
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
)
)
exit /B %ERRORLEVEL%
:end
endlocal
+1 -1
View File
@@ -7,7 +7,7 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
-h, --help Prints this message and exits
-o The directory to output compiled files
-v, --version Prints Elixir version and exits
-v, --version Prints Elixir version and exits (standalone)
--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
+1 -1
View File
@@ -16,7 +16,7 @@ echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
echo.
echo -h, --help Prints this message and exits
echo -o The directory to output compiled files
echo -v, --version Prints Elixir version and exits
echo -v, --version Prints Elixir version and exits (standalone)
echo.
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
echo --no-debug-info Does not attach debug info to compiled modules
+5 -3
View File
@@ -7,9 +7,11 @@ Usage: $(basename "$0") [options] [.exs file] [data]
The following options are exclusive to IEx:
--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
--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.
It accepts all other options listed by "elixir --help".
USAGE
+5 -3
View File
@@ -11,17 +11,19 @@ echo Usage: %~nx0 [options] [.exs file] [data]
echo.
echo The following options are exclusive to IEx:
echo.
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 --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 --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
+106 -48
View File
@@ -9,14 +9,14 @@ end
defmodule EEx do
@moduledoc ~S"""
EEx stands for Embedded Elixir. It allows you to embed
Elixir code inside a string in a robust way.
EEx stands for Embedded Elixir.
Embedded Elixir allows you to embed Elixir code inside a string
in a robust way.
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
"foo baz"
## API
This module provides three main APIs for you to use:
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
@@ -34,24 +34,48 @@ 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 accept EEx-related options.
They are:
All functions in this module, unless otherwise noted, accept EEx-related
options. They are:
* `:file` - the file to be used in the template. Defaults to the given
file the template is read from or to `"nofile"` when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to `1`.
* `:indentation` - (since v1.11.0) an integer added to the column after every
new line. Defaults to `0`.
* `:engine` - the EEx engine to be used for compilation.
* `:trim` - if `true`, trims whitespace left and right of quotation as
long as at least one newline is present. All subsequent newlines and
spaces are removed but one newline is retained. Defaults to `false`.
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code that is generated.
See `Code.string_to_quoted/2` for available options. Note that the options
`:file`, `:line` and `:column` are ignored if passed in.
Defaults to `Code.get_compiler_option(:parser_options)` (which defaults to `[]` if not set).
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code
that is generated. See `Code.string_to_quoted/2` for available options.
Note that the options `:file`, `:line` and `:column` are ignored if
passed in. Defaults to `Code.get_compiler_option(:parser_options)`
(which defaults to `[]` if not set).
## Tags
EEx supports multiple tags, declared below:
<% Elixir expression: executes code but discards output %>
<%= Elixir expression: executes code and prints result %>
<%% EEx quotation: returns the contents inside the tag as is %>
<%!-- Comments: they are discarded from source --%>
EEx supports additional tags, that may be used by some engines,
but they do not have a meaning by default:
<%| ... %>
<%/ ... %>
## Engine
@@ -61,42 +85,10 @@ defmodule EEx do
By default, `EEx` uses the `EEx.SmartEngine` that provides some
conveniences on top of the simple `EEx.Engine`.
### Tags
### `EEx.SmartEngine`
`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:
The smart engine uses EEx default rules and adds the `@` construct
for reading template assigns:
iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
"1"
@@ -109,6 +101,16 @@ 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.
@@ -116,7 +118,9 @@ defmodule EEx do
The `name` argument is the name that the generated function will have.
`template` is the string containing the EEx template. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
template.
The supported `options` are described [in the module docs](#module-options).
## Examples
@@ -148,11 +152,13 @@ defmodule EEx do
The `name` argument is the name that the generated function will have.
`file` is the path to the EEx template file. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
template.
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
@@ -197,6 +203,8 @@ defmodule EEx do
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
iex> quoted = EEx.compile_string("<%= a + b %>")
@@ -207,7 +215,14 @@ defmodule EEx do
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
EEx.Compiler.compile(source, options)
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
end
@doc """
@@ -223,6 +238,8 @@ defmodule EEx do
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
The supported `options` are described [in the module docs](#module-options).
## Examples
# sample.eex
@@ -245,6 +262,8 @@ defmodule EEx do
@doc """
Gets a string `source` and evaluate the values using the `bindings`.
The supported `options` are described [in the module docs](#module-options).
## Examples
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
@@ -261,6 +280,8 @@ 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
@@ -280,6 +301,43 @@ defmodule EEx do
do_eval(compiled, bindings, options)
end
@doc """
Tokenize the given contents according to the given options.
## Options
* `:line` - An integer to start as line. Default is 1.
* `:column` - An integer to start as column. Default is 1.
* `:indentation` - An integer that indicates the indentation. Default is 0.
* `:trim` - Tells the tokenizer to either trim the content or not. Default is false.
* `:file` - Can be either a file or a string "nofile".
## Examples
iex> EEx.tokenize('foo', line: 1, column: 1)
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
## Result
It returns `{:ok, [token]}` where a token is one of:
* `{:text, content, %{column: column, line: line}}`
* `{:expr, marker, content, %{column: column, line: line}}`
* `{:start_expr, marker, content, %{column: column, line: line}}`
* `{:middle_expr, marker, content, %{column: column, line: line}}`
* `{:end_expr, marker, content, %{column: column, line: line}}`
* `{:eof, %{column: column, line: line}}`
Or `{:error, message, %{column: column, line: line}}` in case of errors.
Note new tokens may be added in the future.
"""
@doc since: "1.14.0"
@spec tokenize(IO.chardata(), opts :: keyword) ::
{:ok, [token()]} | {:error, String.t(), metadata()}
def tokenize(contents, opts \\ []) do
EEx.Compiler.tokenize(contents, opts)
end
### Helpers
defp do_eval(compiled, bindings, options) do
+321 -85
View File
@@ -3,80 +3,345 @@ 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(String.t(), keyword) :: Macro.t()
def compile(source, opts) when is_binary(source) and is_list(opts) do
@spec compile([EEx.token()], keyword) :: Macro.t()
def compile(tokens, opts) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
column = 1
indentation = opts[:indentation] || 0
trim = opts[:trim] || false
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
tokenizer_options = %{trim: trim, indentation: indentation}
engine = opts[:engine] || @default_engine
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
{:ok, tokens} ->
state = %{
engine: opts[:engine] || @default_engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: parser_options
}
state = %{
engine: engine,
file: file,
line: line,
quoted: [],
start_line: nil,
start_column: nil,
parser_options: parser_options
}
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
init = state.engine.init(opts)
generate_buffer(tokens, init, [], state)
end
{:error, line, column, message} ->
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
end
# Ignore tokens related to comment.
defp generate_buffer([{:comment, _chars, _meta} | rest], buffer, scope, state) do
generate_buffer(rest, buffer, scope, state)
end
# Generates the buffers by handling each expression from the tokenizer.
# It returns Macro.t/0 or it raises.
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
defp generate_buffer([{:text, chars, meta} | rest], buffer, scope, state) do
buffer =
if function_exported?(state.engine, :handle_text, 3) do
meta = [line: line, column: column]
meta = [line: meta.line, column: meta.column]
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
else
# TODO: Remove this branch on Elixir v2.0
# 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
generate_buffer(rest, buffer, scope, state)
end
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
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
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, start_line, start_column, mark, chars} | rest],
[{:start_expr, mark, chars, meta} | rest],
buffer,
scope,
state
) do
if mark != '=' do
if mark == '' do
message =
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
:elixir_errors.erl_warn({start_line, start_column}, state.file, message)
:elixir_errors.erl_warn({meta.line, meta.column}, state.file, message)
end
{rest, line, contents} =
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
{rest, line, contents} = look_ahead_middle(rest, meta.line, chars) || {rest, meta.line, chars}
{contents, rest} =
generate_buffer(
@@ -87,8 +352,8 @@ defmodule EEx.Compiler do
state
| quoted: [],
line: line,
start_line: start_line,
start_column: column(start_column, mark)
start_line: meta.line,
start_column: column(meta.column, mark)
}
)
@@ -97,47 +362,31 @@ defmodule EEx.Compiler do
end
defp generate_buffer(
[{:middle_expr, line, _column, '', chars} | rest],
[{:middle_expr, '', chars, meta} | rest],
buffer,
[current | scope],
state
) do
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
state = %{state | line: line}
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
state = %{state | line: meta.line}
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], state)
end
defp generate_buffer(
[{:middle_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
"please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn({line, column}, state.file, message)
generate_buffer([{:middle_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line
end
defp generate_buffer([{:middle_expr, line, column, _, chars} | _], _buffer, [], state) do
defp generate_buffer([{:middle_expr, _, chars, meta} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected middle of expression <%#{chars}%>",
file: state.file,
line: line,
column: column
line: meta.line,
column: meta.column
end
defp generate_buffer(
[{:end_expr, line, _column, '', chars} | rest],
[{:end_expr, '', chars, meta} | rest],
buffer,
[current | _],
state
) do
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
{wrapped, state} = wrap_expr(current, meta.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)
@@ -145,40 +394,24 @@ defmodule EEx.Compiler do
{buffer, rest}
end
defp generate_buffer(
[{:end_expr, line, column, modifier, chars} | t],
buffer,
[_ | _] = scope,
state
) do
message =
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
:elixir_errors.erl_warn({line, column}, state.file, message)
generate_buffer([{:end_expr, line, column, '', chars} | t], buffer, scope, state)
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
# raise EEx.SyntaxError, message: message, file: state.file, line: line, column: column
end
defp generate_buffer([{:end_expr, line, column, _, chars} | _], _buffer, [], state) do
defp generate_buffer([{:end_expr, _, chars, meta} | _], _buffer, [], state) do
raise EEx.SyntaxError,
message: "unexpected end of expression <%#{chars}%>",
file: state.file,
line: line,
column: column
line: meta.line,
column: meta.column
end
defp generate_buffer([{:eof, _, _}], buffer, [], state) do
defp generate_buffer([{:eof, _meta}], buffer, [], state) do
state.engine.handle_body(buffer)
end
defp generate_buffer([{:eof, line, column}], _buffer, _scope, state) do
defp generate_buffer([{:eof, meta}], _buffer, _scope, state) do
raise EEx.SyntaxError,
message: "unexpected end of string, expected a closing '<% end %>'",
file: state.file,
line: line,
column: column
line: meta.line,
column: meta.column
end
# Creates a placeholder and wrap it inside the expression block
@@ -195,7 +428,10 @@ defmodule EEx.Compiler do
# Look middle expressions that immediately follow a start_expr
defp look_ahead_middle([{:text, _, _, text} | rest], start, contents) do
defp look_ahead_middle([{:comment, _comment, _meta} | rest], start, contents),
do: look_ahead_middle(rest, start, contents)
defp look_ahead_middle([{:text, text, _meta} | rest], start, contents) do
if only_spaces?(text) do
look_ahead_middle(rest, start, contents ++ text)
else
@@ -203,8 +439,8 @@ defmodule EEx.Compiler do
end
end
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
{rest, line, contents ++ chars}
defp look_ahead_middle([{:middle_expr, _, chars, meta} | rest], _start, contents) do
{rest, meta.line, contents ++ chars}
end
defp look_ahead_middle(_tokens, _start, _contents) do
@@ -212,7 +448,7 @@ defmodule EEx.Compiler do
end
defp only_spaces?(chars) do
Enum.all?(chars, &(&1 in [?\s, ?\t, ?\r, ?\n]))
Enum.all?(chars, &(&1 in @all_spaces))
end
# Changes placeholder to real expression
-244
View File
@@ -1,244 +0,0 @@
defmodule EEx.Tokenizer do
@moduledoc false
@type content :: IO.chardata()
@type line :: non_neg_integer
@type column :: non_neg_integer
@type marker :: '=' | '/' | '|' | ''
@type token ::
{:text, line, column, content}
| {:expr | :start_expr | :middle_expr | :end_expr, line, column, marker, content}
| {:eof, line, column}
@spaces [?\s, ?\t]
@doc """
Tokenizes the given charlist or binary.
It returns {:ok, list} with the following tokens:
* `{:text, line, column, content}`
* `{:expr, line, column, marker, content}`
* `{:start_expr, line, column, marker, content}`
* `{:middle_expr, line, column, marker, content}`
* `{:end_expr, line, column, marker, content}`
* `{:eof, line, column}`
Or `{:error, line, column, message}` in case of errors.
"""
@spec tokenize(binary | charlist, line, column, map) ::
{:ok, [token]} | {:error, line, column, String.t()}
def tokenize(bin, line, column, opts) when is_binary(bin) do
tokenize(String.to_charlist(bin), line, column, opts)
end
def tokenize(list, line, column, opts)
when is_list(list) and is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
column = opts.indentation + column
{list, line, column} =
(opts.trim && trim_init(list, line, column, opts)) || {list, line, column}
tokenize(list, line, column, opts, [{line, column}], [])
end
defp tokenize('<%%' ++ t, line, column, opts, buffer, acc) do
tokenize(t, line, column + 3, opts, [?%, ?< | buffer], acc)
end
defp tokenize('<%#' ++ t, line, column, opts, buffer, acc) do
case expr(t, line, column + 3, opts, []) do
{:error, _, _, _} = error ->
error
{:ok, _, new_line, new_column, rest} ->
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], acc)
end
end
defp tokenize('<%' ++ t, line, column, opts, buffer, acc) do
{marker, t} = retrieve_marker(t)
case expr(t, line, column + 2 + length(marker), opts, []) do
{:error, _, _, _} = error ->
error
{:ok, expr, new_line, new_column, rest} ->
{key, expr} =
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
{:ok, _line, _column, warnings, tokens} ->
Enum.each(Enum.reverse(warnings), fn {location, file, msg} ->
:elixir_errors.erl_warn(location, file, msg)
end)
token_key(tokens, expr)
{:error, _, _, _, _} ->
{:expr, expr}
end
{rest, new_line, new_column, buffer} =
trim_if_needed(rest, new_line, new_column, opts, buffer)
acc = tokenize_text(buffer, acc)
final = {key, line, column, marker, expr}
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], [final | acc])
end
end
defp tokenize('\n' ++ t, line, _column, opts, buffer, acc) do
tokenize(t, line + 1, opts.indentation + 1, opts, [?\n | buffer], acc)
end
defp tokenize([h | t], line, column, opts, buffer, acc) do
tokenize(t, line, column + 1, opts, [h | buffer], acc)
end
defp tokenize([], line, column, _opts, buffer, acc) do
eof = {:eof, line, column}
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
end
# Retrieve marker for <%
defp retrieve_marker([marker | t]) when marker in [?=, ?/, ?|] do
{[marker], t}
end
defp retrieve_marker(t) do
{'', t}
end
# Tokenize an expression until we find %>
defp expr([?%, ?> | t], line, column, _opts, buffer) do
{:ok, Enum.reverse(buffer), line, column + 2, t}
end
defp expr('\n' ++ t, line, _column, opts, buffer) do
expr(t, line + 1, opts.indentation + 1, opts, [?\n | buffer])
end
defp expr([h | t], line, column, opts, buffer) do
expr(t, line, column + 1, opts, [h | buffer])
end
defp expr([], line, column, _opts, _buffer) do
{:error, line, column, "missing token '%>'"}
end
# Receives tokens and check if it is a start, middle or an end token.
defp token_key(tokens, expr) do
case {tokens, tokens |> Enum.reverse() |> drop_eol()} do
{[{:end, _} | _], [{:do, _} | _]} ->
{:middle_expr, expr}
{_, [{:do, _} | _]} ->
{:start_expr, maybe_append_space(expr)}
{_, [{:block_identifier, _, _} | _]} ->
{:middle_expr, maybe_append_space(expr)}
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
{:middle_expr, expr}
{_, [{:stab_op, _, _} | reverse_tokens]} ->
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
if end_index > fn_index do
{:start_expr, expr}
else
{:middle_expr, expr}
end
{tokens, _} ->
case Enum.drop_while(tokens, &closing_bracket?/1) do
[{:end, _} | _] -> {:end_expr, expr}
_ -> {:expr, expr}
end
end
end
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
defp drop_eol(rest), do: rest
defp maybe_append_space([?\s]), do: [?\s]
defp maybe_append_space([h]), do: [h, ?\s]
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
defp closing_bracket?(_), do: false
# Tokenize the buffered text by appending
# it to the given accumulator.
defp tokenize_text([{_line, _column}], acc) do
acc
end
defp tokenize_text(buffer, acc) do
[{line, column} | buffer] = Enum.reverse(buffer)
[{:text, line, column, buffer} | acc]
end
defp trim_if_needed(rest, line, column, opts, buffer) do
if opts.trim do
buffer = trim_left(buffer, 0)
{rest, line, column} = trim_right(rest, line, column, 0, opts)
{rest, line, column, buffer}
else
{rest, line, column, buffer}
end
end
defp trim_init([h | t], line, column, opts) when h in @spaces,
do: trim_init(t, line, column + 1, opts)
defp trim_init([?\r, ?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?\n | t], line, _column, opts),
do: trim_init(t, line + 1, opts.indentation + 1, opts)
defp trim_init([?<, ?% | _] = rest, line, column, _opts),
do: {rest, line, column}
defp trim_init(_, _, _, _), do: false
defp trim_left(buffer, count) do
case trim_whitespace(buffer, 0) do
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
{[?\n | rest], _} -> trim_left(rest, count + 1)
_ when count > 0 -> [?\n | buffer]
_ -> buffer
end
end
defp trim_right(rest, line, column, last_column, opts) do
case trim_whitespace(rest, column) do
{[?\r, ?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column + 1, opts)
{[?\n | rest], column} ->
trim_right(rest, line + 1, opts.indentation + 1, column, opts)
{[], column} ->
{[], line, column}
_ when last_column > 0 ->
{[?\n | rest], line - 1, last_column}
_ ->
{rest, line, column}
end
end
defp trim_whitespace([h | t], column) when h in @spaces, do: trim_whitespace(t, column + 1)
defp trim_whitespace(list, column), do: {list, column}
end
+219 -129
View File
@@ -2,41 +2,67 @@ 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 T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
assert EEx.tokenize('foo', @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
end
test "simple strings" do
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
assert EEx.tokenize("foo", @opts) ==
{:ok, [{:text, 'foo', %{column: 1, line: 1}}, {:eof, %{column: 4, line: 1}}]}
end
test "strings with embedded code" do
assert T.tokenize('foo <% bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
assert EEx.tokenize('foo <% bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 14, line: 1}}
]}
end
test "strings with embedded equals code" do
assert T.tokenize('foo <%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
assert EEx.tokenize('foo <%= bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '=', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
end
test "strings with embedded slash code" do
assert T.tokenize('foo <%/ bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
assert EEx.tokenize('foo <%/ bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '/', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
end
test "strings with embedded pipe code" do
assert T.tokenize('foo <%| bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
assert EEx.tokenize('foo <%| bar %>', @opts) ==
{:ok,
[
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, '|', ' bar ', %{column: 5, line: 1}},
{:eof, %{column: 15, line: 1}}
]}
end
test "strings with more than one line" do
assert T.tokenize('foo\n<%= bar %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
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}}
]}
end
test "strings with more than one line and expression with more than one line" do
@@ -48,191 +74,235 @@ defmodule EEx.TokenizerTest do
'''
exprs = [
{: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}
{: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}}
]
assert T.tokenize(string, 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize(string, @opts) == {:ok, exprs}
end
test "quotation" do
assert T.tokenize('foo <%% true %>', 1, 1, @opts) ==
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
assert EEx.tokenize('foo <%% true %>', @opts) ==
{:ok,
[
{:text, 'foo <% true %>', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
]}
end
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}]}
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}}
]}
end
test "quotation with interpolation" do
exprs = [
{:text, 1, 1, 'a <% b '},
{:expr, 1, 9, '=', ' c '},
{:text, 1, 17, ' '},
{:expr, 1, 18, '=', ' d '},
{:text, 1, 26, ' e %> f'},
{:eof, 1, 33}
{: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}}
]
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('a <%% b <%= c %> <%= d %> e %> f', @opts) == {:ok, exprs}
end
test "improperly formatted quotation with interpolation" do
exprs = [
{:text, 1, 1, '<%% a <%= b %> c %>'},
{:eof, 1, 22}
{:text, '<%% a <%= b %> c %>', %{column: 1, line: 1}},
{:eof, %{column: 22, line: 1}}
]
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('<%%% a <%%= b %> c %>', @opts) == {:ok, exprs}
end
test "EEx comments" do
exprs = [
{:text, 1, 1, 'foo '},
{:eof, 1, 16}
{:text, 'foo ', %{column: 1, line: 1}},
{:eof, %{column: 16, line: 1}}
]
assert T.tokenize('foo <%# true %>', 1, 1, @opts) == {:ok, exprs}
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}
end
test "EEx comments with do-end" do
exprs = [
{:text, 1, 1, 'foo '},
{:text, 1, 19, 'bar'},
{:eof, 1, 32}
{:text, 'foo ', %{column: 1, line: 1}},
{:text, 'bar', %{column: 19, line: 1}},
{:eof, %{column: 32, line: 1}}
]
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('foo <%# true do %>bar<%# end %>', @opts) == {:ok, exprs}
end
test "EEx comments inside do-end" do
exprs = [
{:start_expr, 1, 1, '', ' if true do '},
{:text, 1, 31, 'bar'},
{:end_expr, 1, 34, [], ' end '},
{:eof, 1, 43}
{: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}}
]
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('<% if true do %><%# comment %>bar<% end %>', @opts) == {:ok, exprs}
exprs = [
{:start_expr, 1, 1, [], ' case true do '},
{:middle_expr, 1, 33, '', ' true -> '},
{:text, 1, 46, 'bar'},
{:end_expr, 1, 49, [], ' end '},
{:eof, 1, 58}
{: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 T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
assert EEx.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', @opts) ==
{:ok, exprs}
end
test "EEx multi-line comments" do
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' true ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 20, line: 1}},
{:eof, %{column: 24, line: 1}}
]
assert EEx.tokenize('foo <%!-- true --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' \ntrue\n ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 6, line: 3}},
{:eof, %{column: 10, line: 3}}
]
assert EEx.tokenize('foo <%!-- \ntrue\n --%> bar', @opts) == {:ok, exprs}
exprs = [
{:text, 'foo ', %{column: 1, line: 1}},
{:comment, ' <%= true %> ', %{column: 5, line: 1}},
{:text, ' bar', %{column: 27, line: 1}},
{:eof, %{column: 31, line: 1}}
]
assert EEx.tokenize('foo <%!-- <%= true %> --%> bar', @opts) == {:ok, exprs}
end
test "Elixir comments" do
exprs = [
{:text, 1, 1, 'foo '},
{:expr, 1, 5, [], ' true # this is a boolean '},
{:eof, 1, 35}
{:text, 'foo ', %{column: 1, line: 1}},
{:expr, [], ' true # this is a boolean ', %{column: 5, line: 1}},
{:eof, %{column: 35, line: 1}}
]
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('foo <% true # this is a boolean %>', @opts) == {:ok, exprs}
end
test "Elixir comments with do-end" do
exprs = [
{:start_expr, 1, 1, [], ' if true do # startif '},
{:text, 1, 27, 'text'},
{:end_expr, 1, 31, [], ' end # closeif '},
{:eof, 1, 50}
{: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 T.tokenize('<% if true do # startif %>text<% end # closeif %>', 1, 1, @opts) ==
assert EEx.tokenize('<% if true do # startif %>text<% end # closeif %>', @opts) ==
{:ok, exprs}
end
test "strings with embedded do end" do
exprs = [
{:text, 1, 1, 'foo '},
{:start_expr, 1, 5, '', ' if true do '},
{:text, 1, 21, 'bar'},
{:end_expr, 1, 24, '', ' end '},
{:eof, 1, 33}
{: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}}
]
assert T.tokenize('foo <% if true do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('foo <% if true do %>bar<% end %>', @opts) == {:ok, exprs}
end
test "strings with embedded -> end" do
exprs = [
{: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}
{: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}}
]
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1, 1, @opts) ==
assert EEx.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', @opts) ==
{:ok, exprs}
end
test "strings with fn-end with newline" do
exprs = [
{:start_expr, 1, 1, '=', ' a fn ->\n'},
{:text, 2, 3, 'foo'},
{:end_expr, 2, 6, [], ' end '},
{:eof, 2, 15}
{: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}}
]
assert T.tokenize('<%= a fn ->\n%>foo<% end %>', 1, 1, @opts) ==
assert EEx.tokenize('<%= a fn ->\n%>foo<% end %>', @opts) ==
{:ok, exprs}
end
test "strings with multiple fn-end" do
exprs = [
{: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}
{: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}}
]
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1, 1, @opts) ==
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, 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}
{: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 T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
assert EEx.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', @opts) == {:ok, exprs}
end
test "strings with embedded keywords blocks" do
exprs = [
{: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}
{: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}}
]
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1, 1, @opts) ==
assert EEx.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', @opts) ==
{:ok, exprs}
end
@@ -240,51 +310,61 @@ defmodule EEx.TokenizerTest do
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> \n\n '
exprs = [
{: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}
{: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}}
]
assert T.tokenize(template, 1, 1, %{@opts | trim: true}) == {:ok, exprs}
assert EEx.tokenize(template, [trim: true] ++ @opts) == {:ok, exprs}
end
test "trim mode with comment" do
exprs = [
{:text, 1, 19, '\n123'},
{:eof, 2, 4}
{:text, '\n123', %{column: 19, line: 1}},
{:eof, %{column: 4, line: 2}}
]
assert T.tokenize(' <%# comment %> \n123', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
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}
end
test "trim mode with CRLF" do
exprs = [
{:text, 1, 1, '0\n'},
{:expr, 2, 3, '=', ' 12 '},
{:text, 2, 15, '\n34'},
{:eof, 3, 3}
{:text, '0\n', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 3, line: 2}},
{:text, '\n34', %{column: 15, line: 2}},
{:eof, %{column: 3, line: 3}}
]
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
assert EEx.tokenize('0\r\n <%= 12 %> \r\n34', [trim: true] ++ @opts) == {:ok, exprs}
end
test "trim mode set to false" do
exprs = [
{:text, 1, 1, ' '},
{:expr, 1, 2, '=', ' 12 '},
{:text, 1, 11, ' \n'},
{:eof, 2, 1}
{:text, ' ', %{column: 1, line: 1}},
{:expr, '=', ' 12 ', %{column: 2, line: 1}},
{:text, ' \n', %{column: 11, line: 1}},
{:eof, %{column: 1, line: 2}}
]
assert T.tokenize(' <%= 12 %> \n', 1, 1, %{@opts | trim: false}) == {:ok, exprs}
assert EEx.tokenize(' <%= 12 %> \n', [trim: false] ++ @opts) == {:ok, exprs}
end
test "trim mode no false positives" do
assert_not_trimmed = fn x ->
assert T.tokenize(x, 1, 1, %{@opts | trim: false}) == T.tokenize(x, 1, 1, @opts)
assert EEx.tokenize(x, [trim: false] ++ @opts) == EEx.tokenize(x, @opts)
end
assert_not_trimmed.('foo <%= "bar" %> ')
@@ -294,12 +374,22 @@ defmodule EEx.TokenizerTest do
end
test "returns error when there is start mark and no end mark" do
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 '%>'"}
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}}
end
test "marks invalid expressions as regular expressions" do
assert T.tokenize('<% 1 $ 2 %>', 1, 1, @opts) ==
{:ok, [{:expr, 1, 1, [], ' 1 $ 2 '}, {:eof, 1, 12}]}
assert EEx.tokenize('<% 1 $ 2 %>', @opts) ==
{:ok,
[
{:expr, [], ' 1 $ 2 ', %{column: 1, line: 1}},
{:eof, %{column: 12, line: 1}}
]}
end
end
+10 -1
View File
@@ -207,6 +207,15 @@ defmodule EExTest do
)
end
test "embedded code with multi-line comments in do end" do
assert_eval("foo bar", "foo <%= case true do %><%!-- comment --%><% true -> %>bar<% end %>")
assert_eval(
"foo\n\nbar\n",
"foo\n<%= case true do %>\n<%!-- comment --%>\n<% true -> %>\nbar\n<% end %>"
)
end
test "embedded code with nested do end" do
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
end
@@ -308,7 +317,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 of expression \"<%= end %>\"]
~s[unexpected beginning of EEx tag \"<%=\" on \"<%= end %>\"]
end
test "when trying to use marker '/' without implementation" do
+8 -1
View File
@@ -1 +1,8 @@
ExUnit.start(trace: "--trace" in System.argv())
{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
)
+1
View File
@@ -78,6 +78,7 @@ canonical = System.fetch_env!("CANONICAL")
DynamicSupervisor,
GenServer,
Node,
PartitionSupervisor,
Process,
Registry,
Supervisor,
+122 -5
View File
@@ -94,8 +94,7 @@ defmodule Access do
@type container :: keyword | struct | map
@type nil_container :: nil
@type any_container :: any
@type t :: container | nil_container | any_container
@type t :: container | nil_container | any
@type key :: any
@type value :: any
@@ -153,7 +152,7 @@ defmodule Access do
"""
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: value, data: container | any_container
when current_value: value, data: container
@doc """
Invoked to "pop" the value under `key` out of the given data structure.
@@ -167,14 +166,18 @@ 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 | any_container
@callback pop(data, key) :: {value, data} when data: 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"
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"
%{unquote(exception) | reason: reason}
_ ->
@@ -826,4 +829,118 @@ 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
+105 -58
View File
@@ -51,13 +51,23 @@ defmodule Application do
config :my_app, :db_host, "db.local"
See the "Configuration" section in the `Mix` module for more information.
You can also change the application environment dynamically by using functions
such as `put_env/3` and `delete_env/2`. 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.
such as `put_env/3` and `delete_env/2`.
### Compile-time environment
> 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
In the previous example, we read the application environment at runtime:
@@ -75,37 +85,48 @@ 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. This is often done by calling `get_env/3`
outside of a function:
the compilation of a certain project. However, if you try to access
`Application.fetch_env!/2` outside of a function:
defmodule MyApp.DBClient do
@db_host Application.get_env(:my_app, :db_host, "db.local")
@db_host Application.fetch_env!(:my_app, :db_host)
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
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:
You might see warnings and errors:
config :my_app, :db_host, "db.production"
warning: Application.fetch_env!/2 is discouraged in the module body,
use Application.compile_env/3 instead
iex:3: MyApp.DBClient
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.
** (ArgumentError) could not fetch application environment :db_host
for application :my_app because the application was not loaded nor
configured
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:
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:
require Application
@db_host Application.compile_env(:my_app, :db_host, "db.local")
defmodule MyApp.DBClient do
@db_host Application.compile_env(:my_app, :db_host, "db.local")
By using `compile_env/3`, tools like Mix will store the values used during
compilation and compare the compilation values with the runtime values whenever
your system starts, raising an error in case they differ.
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.
## The application callback module
@@ -168,7 +189,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://erlang.org/doc/man/application.html), which is a file called
file*](https://www.erlang.org/doc/man/application.html), which is a file called
`APP_NAME.app`. For example, the application resource file of the OTP
application `ex_unit` is called `ex_unit.app`.
@@ -273,9 +294,9 @@ defmodule Application do
For further details on applications please check the documentation of the
[`:application` Erlang module](`:application`), and the
[Applications](https://erlang.org/doc/design_principles/applications.html)
[Applications](https://www.erlang.org/doc/design_principles/applications.html)
section of the [OTP Design Principles User's
Guide](https://erlang.org/doc/design_principles/users_guide.html).
Guide](https://www.erlang.org/doc/design_principles/users_guide.html).
"""
@doc """
@@ -504,33 +525,34 @@ 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) when is_atom(app) do
defmacro compile_env(app, key_or_path, default \\ nil) do
if __CALLER__.function do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
key_or_path = Macro.expand_literal(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
quote do
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
Application.compile_env(__ENV__, unquote(app), unquote(key_or_path), unquote(default))
end
end
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
@doc """
Reads the application environment at compilation time from a macro.
defp expand_key_or_path(list, env) when is_list(list),
do: Enum.map(list, &expand_key_or_path(&1, env))
Typically, developers will use `compile_env/3`. This function must
only be invoked from macros which aim to read the compilation environment
dynamically.
defp expand_key_or_path(other, _env),
do: other
@doc false
def __compile_env__(app, key_or_path, default, env) do
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
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} -> value
:error -> default
@@ -545,20 +567,33 @@ defmodule Application do
"""
@doc since: "1.10.0"
@spec compile_env!(app, key | list) :: value
defmacro compile_env!(app, key_or_path) when is_atom(app) do
defmacro compile_env!(app, key_or_path) do
if __CALLER__.function do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
key_or_path = Macro.expand_literal(key_or_path, %{__CALLER__ | function: {:__info__, 1}})
quote do
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
Application.compile_env!(__ENV__, unquote(app), unquote(key_or_path))
end
end
@doc false
def __compile_env__!(app, key_or_path, env) do
@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
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} ->
value
@@ -570,8 +605,9 @@ defmodule Application do
end
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, env) when is_atom(key) do
fetch_compile_env(app, key, [], env)
end
defp fetch_compile_env(app, [key | paths], env) when is_atom(key),
do: fetch_compile_env(app, key, paths, env)
@@ -596,14 +632,13 @@ defmodule Application do
If the configuration parameter does not exist, the function returns the
`default` value.
**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:** 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 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
@@ -653,6 +688,14 @@ 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
@@ -669,9 +712,13 @@ defmodule Application do
If the configuration parameter does not exist, raises `ArgumentError`.
**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.
> **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) :: value
def fetch_env!(app, key) when is_atom(app) do
@@ -777,7 +824,7 @@ defmodule Application do
@doc """
Ensures the given `app` is loaded.
Same as `load/2` but returns `:ok` if the application was already
Same as `load/1` but returns `:ok` if the application was already
loaded.
"""
@doc since: "1.10.0"
+1 -1
View File
@@ -56,7 +56,7 @@ defmodule Atom do
"""
@spec to_string(atom) :: String.t()
def to_string(atom) do
:erlang.atom_to_binary(atom, :utf8)
:erlang.atom_to_binary(atom)
end
@doc """
+591 -665
View File
File diff suppressed because it is too large Load Diff
+9 -45
View File
@@ -3,31 +3,11 @@ defmodule Bitwise do
A set of functions that perform calculations on bits.
All bitwise functions work only on integers; otherwise an
`ArithmeticError` is raised.
`ArithmeticError` is raised. The functions `band/2`,
`bor/2`, `bsl/2`, and `bsr/2` also have operators,
respectively: `&&&/2`, `|||/2`, `<<</2`, and `>>>/2`.
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`.
## Guards
All bitwise functions can be used in guards:
@@ -42,6 +22,7 @@ defmodule Bitwise do
"""
@doc false
@deprecated "import Bitwise instead"
defmacro __using__(options) do
except =
cond do
@@ -49,7 +30,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 ->
[]
@@ -80,25 +61,8 @@ defmodule Bitwise do
:erlang.bnot(expr)
end
@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
@doc false
def unquote(:"~~~")(expr) do
:erlang.bnot(expr)
end
@@ -192,7 +156,7 @@ defmodule Bitwise do
end
@doc false
def unquote(:^^^)(left, right) do
def unquote(:"^^^")(left, right) do
:erlang.bxor(left, right)
end
+5 -5
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 :: {non_neg_integer, non_neg_integer}
@type microsecond :: {value :: non_neg_integer, precision :: non_neg_integer}
@typedoc "A calendar implementation"
@type calendar :: module
@@ -365,7 +365,7 @@ defmodule Calendar do
"""
@doc since: "1.8.0"
@spec put_time_zone_database(time_zone_database()) :: :ok
def put_time_zone_database(database) do
def put_time_zone_database(database) when is_atom(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.get_env(:elixir, :time_zone_database, Calendar.UTCOnlyTimeZoneDatabase)
Application.fetch_env!(:elixir, :time_zone_database)
end
@doc """
@@ -544,8 +544,8 @@ defmodule Calendar do
parse_modifiers(rest, width, "", parser_data)
end
defp parse_modifiers("0" <> rest, width, nil, parser_data) do
parse_modifiers(rest, width, ?0, parser_data)
defp parse_modifiers("0" <> rest, nil, nil, parser_data) do
parse_modifiers(rest, nil, ?0, parser_data)
end
defp parse_modifiers("_" <> rest, width, nil, parser_data) do
+17 -8
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 `Kernel.sigil_D/2`) sigil:
`~D` (see `sigil_D/2`) sigil:
iex> ~D[2000-01-01]
~D[2000-01-01]
@@ -31,7 +31,12 @@ 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.
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]
## Using epochs
@@ -72,16 +77,18 @@ defmodule Date do
## Examples
iex> Date.range(~D[1999-01-01], ~D[2000-01-01])
#DateRange<~D[1999-01-01], ~D[2000-01-01]>
Date.range(~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> Enum.member?(range, ~D[2001-02-01])
iex> ~D[2001-02-01] in range
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
@@ -108,10 +115,10 @@ defmodule Date do
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> range
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> Enum.count(range)
183
iex> Enum.member?(range, ~D[2001-01-03])
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]]
@@ -830,8 +837,6 @@ 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])
@@ -992,6 +997,8 @@ 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
@@ -1011,6 +1018,8 @@ defmodule Date do
"""
@doc since: "1.11.0"
@spec end_of_month(Calendar.date()) :: t()
def end_of_month(date)
def end_of_month(%{year: year, month: month, calendar: calendar} = date) do
day = Date.days_in_month(date)
%Date{year: year, month: month, day: day, calendar: calendar}
+6 -6
View File
@@ -16,12 +16,12 @@ defmodule Date.Range do
@type t :: %__MODULE__{
first: Date.t(),
last: Date.t(),
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days(),
first_in_iso_days: days(),
last_in_iso_days: days(),
step: pos_integer | neg_integer
}
@typep iso_days() :: Calendar.iso_days()
@typep days() :: integer()
@enforce_keys [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
@@ -75,7 +75,7 @@ defmodule Date.Range do
step: step
} = range
) do
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
{:ok, size(range), &slice(first + &1 * step, step + &3 - 1, &2, calendar)}
end
# TODO: Remove me on v2.0
@@ -211,11 +211,11 @@ defmodule Date.Range do
import Kernel, except: [inspect: 2]
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ")"
end
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
"Date.range(" <> inspect(first) <> ", " <> inspect(last) <> ", #{step})"
end
# TODO: Remove me on v2.0
+192 -36
View File
@@ -2,15 +2,24 @@ defmodule DateTime do
@moduledoc """
A datetime implementation with a time zone.
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.
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.
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.
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]
Developers should avoid creating the `DateTime` struct directly
and instead rely on the functions provided by this module as
@@ -25,22 +34,74 @@ 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. For example,
two of the available options are:
Other time zone databases can also be configured. Here are some
available options and libraries:
* [`tz`](https://hexdocs.pm/tz/)
* [`tzdata`](https://hexdocs.pm/tzdata/)
* [`tz`](https://github.com/mathieuprog/tz)
* [`tzdata`](https://github.com/lau/tzdata)
* [`zoneinfo`](https://github.com/smartrent/zoneinfo) -
recommended for embedded devices
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, Tzdata.TimeZoneDatabase
config :elixir, :time_zone_database, Tz.TimeZoneDatabase
or by calling `Calendar.put_time_zone_database/1`:
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
Calendar.put_time_zone_database(Tz.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] ++
@@ -82,6 +143,9 @@ 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()
@@ -753,10 +817,6 @@ 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()
@@ -801,6 +861,8 @@ defmodule DateTime do
"""
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
def to_naive(datetime)
def to_naive(%{
calendar: calendar,
year: year,
@@ -840,6 +902,8 @@ defmodule DateTime do
"""
@spec to_date(Calendar.datetime()) :: Date.t()
def to_date(datetime)
def to_date(%{
year: year,
month: month,
@@ -870,6 +934,8 @@ defmodule DateTime do
"""
@spec to_time(Calendar.datetime()) :: Time.t()
def to_time(datetime)
def to_time(%{
year: _,
month: _,
@@ -1053,6 +1119,10 @@ 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")
@@ -1066,11 +1136,37 @@ defmodule DateTime do
"""
@doc since: "1.4.0"
@spec from_iso8601(String.t(), Calendar.calendar()) ::
@spec from_iso8601(String.t(), Calendar.calendar(), :extended | :basic) ::
{:ok, t, Calendar.utc_offset()} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO) do
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
with {:ok, {year, month, day, hour, minute, second, microsecond}, offset} <-
Calendar.ISO.parse_utc_datetime(string) do
Calendar.ISO.parse_utc_datetime(string, format) do
datetime = %DateTime{
year: year,
month: month,
@@ -1291,12 +1387,11 @@ defmodule DateTime do
@doc """
Subtracts `datetime2` from `datetime1`.
The answer can be returned in any `unit` available from `t:System.time_unit/0`.
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`.
Leap seconds are not taken into account.
This function returns the difference in seconds where seconds are measured
according to `Calendar.ISO`.
Fractional results are not supported and are truncated.
## Examples
@@ -1310,14 +1405,36 @@ 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(), System.time_unit()) :: integer()
@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
def diff(
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2,
unit \\ :second
unit
) do
naive_diff =
(datetime1 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)) -
@@ -1330,15 +1447,30 @@ defmodule DateTime do
@doc """
Adds a specified amount of time to a `DateTime`.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in 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.
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 always consider the unit to be computed according
to the `Calendar.ISO`.
Fractional second precision stays the same in a similar way to `NaiveDateTime.add/2`.
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.
### Examples
@@ -1349,15 +1481,26 @@ defmodule DateTime do
iex> DateTime.add(~U[2018-11-15 10:00:00Z], 3600, :second)
~U[2018-11-15 11:00:00Z]
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02:
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:59:59.123], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(3, :second, FakeTimeZoneDatabase)
#DateTime<2019-03-31 03:00:02.123+02:00 CEST Europe/Copenhagen>
When adding 1 day during "spring forward", the hour also changes:
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(1, :day, FakeTimeZoneDatabase)
#DateTime<2019-04-01 02:00:00+02:00 CEST Europe/Copenhagen>
"""
@doc since: "1.8.0"
@spec add(Calendar.datetime(), integer, System.time_unit(), Calendar.time_zone_database()) ::
@spec add(
Calendar.datetime(),
integer,
:day | :hour | :minute | System.time_unit(),
Calendar.time_zone_database()
) ::
t()
def add(
datetime,
@@ -1365,7 +1508,20 @@ defmodule DateTime do
unit \\ :second,
time_zone_database \\ Calendar.get_time_zone_database()
)
when is_integer(amount_to_add) do
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
%{
utc_offset: utc_offset,
std_offset: std_offset,
+94 -19
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 `Kernel.sigil_N/2`) sigil:
`~N` (see `sigil_N/2`) sigil:
iex> ~N[2000-01-01 23:00:07]
~N[2000-01-01 23:00:07]
@@ -36,12 +36,18 @@ 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.
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]
## Using epochs
The `add/3` and `diff/3` functions can be used for computing with
date times or retrieving the number of seconds between instants.
The `add/3` and `diff/3` functions can be used for computing 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):
@@ -350,11 +356,18 @@ defmodule NaiveDateTime do
@doc """
Adds a specified amount of time to a `NaiveDateTime`.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in 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`.
## 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]
@@ -363,19 +376,33 @@ defmodule NaiveDateTime do
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], -2)
~N[2014-10-02 00:29:08]
# can work with other units
It can also work with subsecond precisions:
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10], 2_000, :millisecond)
~N[2014-10-02 00:29:12]
# keeps the same precision
As well as days/hours/minutes:
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 2, :day)
~N[2015-03-02 00:29:10]
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 36, :hour)
~N[2015-03-01 12:29:10]
iex> NaiveDateTime.add(~N[2015-02-28 00:29:10], 60, :minute)
~N[2015-02-28 01:29:10]
This operation keeps the precision of the naive date time:
iex> NaiveDateTime.add(~N[2014-10-02 00:29:10.021], 21, :second)
~N[2014-10-02 00:29:31.021]
# changes below the precision will not be visible
And ignores any changes below the precision:
iex> hidden = NaiveDateTime.add(~N[2014-10-02 00:29:10], 21, :millisecond)
iex> hidden.microsecond # ~N[2014-10-02 00:29:10]
{21000, 0}
Operations on top of gregorian seconds or the Unix epoch are optimized:
# from Gregorian seconds
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
~N[2014-10-02 00:29:10]
@@ -391,11 +418,25 @@ defmodule NaiveDateTime do
"""
@doc since: "1.4.0"
@spec add(Calendar.naive_datetime(), integer, System.time_unit()) :: t
@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
def add(
%{microsecond: {_, precision}, calendar: calendar} = naive_datetime,
amount_to_add,
unit \\ :second
unit
)
when is_integer(amount_to_add) do
ppd = System.convert_time_unit(86400, :second, unit)
@@ -409,10 +450,11 @@ defmodule NaiveDateTime do
@doc """
Subtracts `naive_datetime2` from `naive_datetime1`.
The answer can be returned in any `unit` available from `t:System.time_unit/0`.
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`.
This function returns the difference in seconds where seconds are measured
according to `Calendar.ISO`.
Fractional results are not supported and are truncated.
## Examples
@@ -420,24 +462,57 @@ 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
# to Gregorian seconds
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10], ~N[0000-01-01 00:00:00])
63579428950
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
"""
@doc since: "1.4.0"
@spec diff(Calendar.naive_datetime(), Calendar.naive_datetime(), System.time_unit()) :: integer
@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
def diff(
%{calendar: calendar1} = naive_datetime1,
%{calendar: calendar2} = naive_datetime2,
unit \\ :second
unit
) do
if not Calendar.compatible_calendars?(calendar1, calendar2) do
raise ArgumentError,
+73 -24
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 `Kernel.sigil_T/2`) sigil:
`~T` (see `sigil_T/2`) sigil:
iex> ~T[23:00:07.001]
~T[23:00:07.001]
@@ -31,7 +31,12 @@ 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.
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]
"""
@enforce_keys [:hour, :minute, :second]
@@ -449,10 +454,15 @@ defmodule Time do
end
@doc """
Adds the `number` of `unit`s to the given `time`.
Adds the `amount_to_add` of `unit`s to the given `time`.
This function accepts the `number` measured according to `Calendar.ISO`.
The time is returned in the same calendar as it was given in.
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`.
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.
@@ -460,30 +470,56 @@ defmodule Time do
## Examples
iex> Time.add(~T[10:00:00], 27000)
~T[17:30:00.000000]
~T[17:30:00]
iex> Time.add(~T[11:00:00.005], 2400)
~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.000000]
~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.000000]
~T[22:59:00]
Note that the time is cyclic:
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]
"""
@doc since: "1.6.0"
@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
@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
parts = Integer.mod(total, @parts_per_day)
{hour, minute, second, microsecond} = calendar.time_from_day_fraction({parts, @parts_per_day})
{hour, minute, second, {microsecond, _}} =
calendar.time_from_day_fraction({parts, @parts_per_day})
%Time{
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
microsecond: {microsecond, precision},
calendar: calendar
}
end
@@ -650,12 +686,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 `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 `: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.
This function returns the difference in seconds where seconds
are measured according to `Calendar.ISO`.
The unit is measured according to `Calendar.ISO` and defaults to `:second`.
Fractional results are not supported and are truncated.
## Examples
@@ -676,11 +712,24 @@ 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(), System.time_unit()) :: integer
@spec diff(Calendar.time(), Calendar.time(), :hour | :minute | 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,
+139 -65
View File
@@ -3,8 +3,9 @@ defmodule Code do
Utilities for managing code compilation, code evaluation, and code loading.
This module complements Erlang's [`:code` module](`:code`)
to add behaviour which is specific to Elixir. Almost all of the functions in this module
have global side effects on the behaviour of Elixir.
to add behaviour which is specific to Elixir. For functions to
manipulate Elixir's AST (rather than evaluating it), see the
`Macro` module.
## Working with files
@@ -129,6 +130,8 @@ defmodule Code do
* `{:require, meta, module, opts}` - traced whenever `module` is required.
`meta` is the require AST metadata and `opts` are the require options.
If the `meta` option contains the `:from_macro`, then `require` was called
from within a macro and therefore must be treated as a compile-time dependency.
* `{:struct_expansion, meta, module, keys}` - traced whenever `module`'s struct
is expanded. `meta` is the struct AST metadata and `keys` are the keys being
@@ -149,11 +152,13 @@ defmodule Code do
of keys to traverse in the application environment and `return` is either
`{:ok, value}` or `:error`.
* `{:on_module, bytecode, :none}` - (since v1.11.0) traced whenever a module
* `{:on_module, bytecode, _ignore}` - (since v1.11.0) traced whenever a module
is defined. This is equivalent to the `@after_compile` callback and invoked
after any `@after_compile` in the given module. The third element is currently
`:none` but it may provide more metadata in the future. It is best to ignore
it at the moment.
it at the moment. Note that `Module` functions expecting not yet compiled modules
(such as `Module.definitions_in/1`) are still available at the time this event
is emitted.
The `:tracers` compiler option can be combined with the `:parser_options`
compiler option to enrich the metadata of the traced events above.
@@ -186,6 +191,7 @@ defmodule Code do
@boolean_compiler_options [
:docs,
:debug_info,
:ignore_already_consolidated,
:ignore_module_conflict,
:relative_paths,
:warnings_as_errors
@@ -230,6 +236,8 @@ defmodule Code do
calling this function only removes them from the list,
allowing them to be required again.
The list of files is managed per Erlang VM node.
## Examples
# Require EEx test code
@@ -259,7 +267,8 @@ defmodule Code do
Appends a path to the end of the Erlang VM code path list.
This is the list of directories the Erlang VM uses for
finding module code.
finding module code. The list of files is managed per Erlang
VM node.
The path is expanded with `Path.expand/1` before being appended.
If this path does not exist, an error is returned.
@@ -282,7 +291,7 @@ defmodule Code do
Prepends a path to the beginning of the Erlang VM code path list.
This is the list of directories the Erlang VM uses for finding
module code.
module code. The list of files is managed per Erlang VM node.
The path is expanded with `Path.expand/1` before being prepended.
If this path does not exist, an error is returned.
@@ -302,8 +311,10 @@ defmodule Code do
end
@doc """
Deletes a path from the Erlang VM code path list. This is the list of
directories the Erlang VM uses for finding module code.
Deletes a path from the Erlang VM code path list.
This is the list of directories the Erlang VM uses for finding
module code. The list of files is managed per Erlang VM node.
The path is expanded with `Path.expand/1` before being deleted. If the
path does not exist, this function returns `false`.
@@ -320,7 +331,14 @@ defmodule Code do
"""
@spec delete_path(Path.t()) :: boolean
def delete_path(path) do
:code.del_path(to_charlist(Path.expand(path)))
case :code.del_path(to_charlist(Path.expand(path))) do
result when is_boolean(result) ->
result
{:error, :bad_name} ->
raise ArgumentError,
"invalid argument #{inspect(path)}"
end
end
@doc """
@@ -399,12 +417,18 @@ defmodule Code do
end
defp validated_eval_string(string, binding, opts_or_env) do
%{line: line, file: file} = env = :elixir.env_for_eval(opts_or_env)
%{line: line, file: file} = env = env_for_eval(opts_or_env)
forms = :elixir.string_to_quoted!(to_charlist(string), line, 1, file, [])
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
{value, binding, _env} = eval_verify(:eval_forms, forms, binding, env)
{value, binding}
end
defp eval_verify(fun, forms, binding, env) do
Module.ParallelChecker.verify(fn ->
apply(:elixir, fun, [forms, binding, env])
end)
end
@doc ~S"""
Formats the given code `string`.
@@ -437,12 +461,23 @@ defmodule Code do
If you set it to `false` later on, `do`-`end` blocks won't be
converted back to keywords.
* `:normalize_bitstring_modifiers` (since v1.14.0) - when `true`,
removes unnecessary parentheses in known bitstring
[modifiers](`<<>>/1`), for example `<<foo::binary()>>`
becomes `<<foo::binary>>`, or adds parentheses for custom
modifiers, where `<<foo::custom_type>>` becomes `<<foo::custom_type()>>`.
Defaults to `true`. This option changes the AST.
## Design principles
The formatter was designed under three principles.
First, the formatter never changes the semantics of the code by
default. This means the input AST and the output AST are equivalent.
First, the formatter never changes the semantics of the code.
This means the input AST and the output AST are almost always equivalent.
The only cases where the formatter will change the AST is when the input AST
would cause *compiler warnings* and the output AST won't. The cases where
the formatter changes the AST can be disabled through formatting options
if desired.
The second principle is to provide as little configuration as possible.
This eases the formatter adoption by removing contention points while
@@ -731,18 +766,13 @@ defmodule Code do
unescape: false,
warn_on_unnecessary_quotes: false,
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
token_metadata: true
token_metadata: true,
emit_warnings: false
] ++ opts
{forms, comments} = string_to_quoted_with_comments!(string, to_quoted_opts)
to_algebra_opts =
[
comments: comments
] ++ opts
to_algebra_opts = [comments: comments] ++ opts
doc = Code.Formatter.to_algebra(forms, to_algebra_opts)
Inspect.Algebra.format(doc, line_length)
end
@@ -791,16 +821,48 @@ defmodule Code do
"""
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
def eval_quoted(quoted, binding \\ [], opts \\ [])
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(env))
def eval_quoted(quoted, binding \\ [], env_or_opts \\ []) do
{value, binding, _env} = eval_verify(:eval_quoted, quoted, binding, env_for_eval(env_or_opts))
{value, binding}
end
def eval_quoted(quoted, binding, opts) when is_list(opts) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(opts))
{value, binding}
@doc """
Returns an environment for evaluation.
It accepts either a `Macro.Env`, that is then pruned and prepared,
or a list of options. It returns an environment that is ready for
evaluation.
Most functions in this module will automatically prepare the given
environment for evaluation, so you don't need to explicitly call
this function, with the exception of `eval_quoted_with_env/3`,
which was designed precisely to be called in a loop, to implement
features such as interactive shells or anything else with multiple
evaluations.
## Options
If an env is not given, the options can be:
* `:file` - the file to be considered in the evaluation
* `:line` - the line on which the script starts
"""
@doc since: "1.14.0"
def env_for_eval(env_or_opts), do: :elixir.env_for_eval(env_or_opts)
@doc """
Evaluates the given `quoted` contents with `binding` and `env`.
This function is meant to be called in a loop, to implement features
such as interactive shells or anything else with multiple evaluations.
Therefore, the first time you call this function, you must compute
the initial environment with `env_for_eval/1`. The remaining calls
must pass the environment that was returned by this function.
"""
@doc since: "1.14.0"
def eval_quoted_with_env(quoted, binding, %Macro.Env{} = env) when is_list(binding) do
eval_verify(:eval_forms, quoted, binding, env)
end
@doc ~S"""
@@ -1119,19 +1181,19 @@ defmodule Code do
"""
@spec eval_file(binary, nil | binary) :: {term, binding}
def eval_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
eval_string(File.read!(file), [], file: file, line: 1)
{charlist, file} = find_file!(file, relative_to)
eval_string(charlist, [], file: file, line: 1)
end
@deprecated "Use Code.require_file/2 or Code.compile_file/2 instead"
@doc false
def load_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
{charlist, file} = find_file!(file, relative_to)
:elixir_code_server.call({:acquire, file})
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(file, fn _, _ -> :ok end)
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
end)
:elixir_code_server.cast({:required, file})
@@ -1150,7 +1212,7 @@ defmodule Code do
ones will block until the file is available. This means that if `require_file/2`
is called more than once with a given file, that file will be compiled only once.
The first process to call `require_file/2` will get the list of loaded modules,
others will get `nil`.
others will get `nil`. The list of required files is managed per Erlang VM node.
See `compile_file/2` if you would like to compile a file without tracking its
filenames. Finally, if you would like to get the result of evaluating a file rather
@@ -1172,7 +1234,7 @@ defmodule Code do
"""
@spec require_file(binary, nil | binary) :: [{module, binary}] | nil
def require_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
{charlist, file} = find_file!(file, relative_to)
case :elixir_code_server.call({:acquire, file}) do
:required ->
@@ -1181,7 +1243,7 @@ defmodule Code do
:proceed ->
loaded =
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(file, fn _, _ -> :ok end)
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
end)
:elixir_code_server.cast({:required, file})
@@ -1211,8 +1273,10 @@ defmodule Code do
@doc """
Stores all given compilation options.
To store individual options and for a description of all
options, see `put_compiler_option/2`.
Changing the compilation options affect all processes
running in a given Erlang VM node. To store individual
options and for a description of all options, see
`put_compiler_option/2`.
## Examples
@@ -1265,7 +1329,8 @@ defmodule Code do
@doc """
Stores a compilation option.
These options are global since they are stored by Elixir's code server.
Changing the compilation options affect all processes running in a
given Erlang VM node.
Available options are:
@@ -1276,11 +1341,15 @@ defmodule Code do
module. This allows a developer to reconstruct the original source
code. Defaults to `true`.
* `:ignore_module_conflict` - when `true`, override modules that were
already defined without raising errors. Defaults to `false`.
* `:ignore_already_consolidated` - when `true`, does not warn when a protocol
has already been consolidated and a new implementation is added. Defaults
to `false`.
* `:ignore_module_conflict` - when `true`, does not warn when a module has
already been defined. Defaults to `false`.
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
warnings and errors generated by the compiler. Note disabling this option
warnings, and errors generated by the compiler. Note disabling this option
won't affect runtime warnings and errors. Defaults to `true`.
* `:warnings_as_errors` - causes compilation to fail when warnings are
@@ -1365,6 +1434,9 @@ defmodule Code do
old compiler module names to be reused. If there are any processes running
any code from such modules, they will be terminated too.
This function is only meant to be called if you have a long running node
that is constantly evaluating code.
It returns `{:ok, number_of_modules_purged}`.
"""
@doc since: "1.7.0"
@@ -1389,8 +1461,9 @@ defmodule Code do
"""
@spec compile_string(List.Chars.t(), binary) :: [{module, binary}]
def compile_string(string, file \\ "nofile") when is_binary(file) do
loaded = :elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
Module.ParallelChecker.verify(fn ->
:elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
end)
end
@doc """
@@ -1403,8 +1476,9 @@ defmodule Code do
"""
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
loaded = :elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
Enum.map(loaded, &elem(&1, 0))
Module.ParallelChecker.verify(fn ->
:elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
end)
end
@doc """
@@ -1425,7 +1499,8 @@ defmodule Code do
@spec compile_file(binary, nil | binary) :: [{module, binary}]
def compile_file(file, relative_to \\ nil) when is_binary(file) do
Module.ParallelChecker.verify(fn ->
:elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
{charlist, file} = find_file!(file, relative_to)
:elixir_compiler.string(charlist, file, fn _, _ -> :ok end)
end)
end
@@ -1599,7 +1674,7 @@ defmodule Code do
file.
It returns the term stored in the documentation chunk in the format defined by
[EEP 48](https://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
[EEP 48](https://www.erlang.org/eeps/eep-0048.html) or `{:error, reason}` if
the chunk is not available.
## Examples
@@ -1631,8 +1706,8 @@ defmodule Code do
def fetch_docs(module_or_path)
def fetch_docs(module) when is_atom(module) do
case :code.get_object_code(module) do
{_module, bin, beam_path} ->
case get_beam_and_path(module) do
{bin, beam_path} ->
case fetch_docs_from_beam(bin) do
{:error, :chunk_not_found} ->
app_root = Path.expand(Path.join(["..", ".."]), beam_path)
@@ -1646,7 +1721,7 @@ defmodule Code do
:error ->
case :code.which(module) do
:preloaded ->
# The erts directory is not necessarily included in releases
# The ERTS directory is not necessarily included in releases
# unless it is listed as an extra application.
case :code.lib_dir(:erts) do
path when is_list(path) ->
@@ -1667,6 +1742,15 @@ defmodule Code do
fetch_docs_from_beam(String.to_charlist(path))
end
defp get_beam_and_path(module) do
with {^module, beam, filename} <- :code.get_object_code(module),
{:ok, ^module} <- beam |> :beam_lib.info() |> Keyword.fetch(:module) do
{beam, filename}
else
_ -> :error
end
end
@docs_chunk 'Docs'
defp fetch_docs_from_beam(bin_or_path) do
@@ -1699,17 +1783,8 @@ defmodule Code do
{:error, {:invalid_chunk, bin}}
end
@doc ~S"""
Deprecated function to retrieve old documentation format.
Elixir v1.7 adopts [EEP 48](https://erlang.org/eep/eeps/eep-0048.html)
which is a new documentation format meant to be shared across all
BEAM languages. The old format, used by `Code.get_docs/2`, is no
longer available, and therefore this function always returns `nil`.
Use `Code.fetch_docs/1` instead.
"""
@doc false
@deprecated "Code.get_docs/2 always returns nil as its outdated documentation is no longer stored on BEAM files. Use Code.fetch_docs/1 instead"
@spec get_docs(module, :moduledoc | :docs | :callback_docs | :type_docs | :all) :: nil
def get_docs(_module, _kind) do
nil
end
@@ -1719,7 +1794,7 @@ defmodule Code do
# Finds the file given the relative_to path.
#
# If the file is found, returns its path in binary, fails otherwise.
defp find_file(file, relative_to) do
defp find_file!(file, relative_to) do
file =
if relative_to do
Path.expand(file, relative_to)
@@ -1727,10 +1802,9 @@ defmodule Code do
Path.expand(file)
end
if File.regular?(file) do
file
else
raise Code.LoadError, file: file
case File.read(file) do
{:ok, bin} -> {String.to_charlist(bin), file}
{:error, reason} -> raise Code.LoadError, file: file, reason: reason
end
end
end
+150 -73
View File
@@ -22,7 +22,7 @@ defmodule Code.Formatter do
@no_newline_binary_operators [:\\, :in]
# Left associative operators that start on the next line in case of breaks (always pipes)
@pipeline_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :<|>]
@pipeline_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :"<|>"]
# Right associative operators that start on the next line in case of breaks
@right_new_line_before_binary_operators [:|, :when]
@@ -43,8 +43,8 @@ defmodule Code.Formatter do
:<<~,
:~>>,
:<~>,
:<|>,
:^^^,
:"<|>",
:"^^^",
:+++,
:---,
:in,
@@ -141,6 +141,24 @@ defmodule Code.Formatter do
@do_end_keywords [:rescue, :catch, :else, :after]
@bitstring_modifiers [
:integer,
:float,
:bits,
:bitstring,
:binary,
:bytes,
:utf8,
:utf16,
:utf32,
:signed,
:unsigned,
:little,
:big,
:native,
:_
]
@doc """
Converts the quoted expression into an algebra document.
"""
@@ -177,7 +195,9 @@ defmodule Code.Formatter do
defp state(comments, opts) do
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
file = Keyword.get(opts, :file, nil)
sigils = Keyword.get(opts, :sigils, [])
normalize_bitstring_modifiers = Keyword.get(opts, :normalize_bitstring_modifiers, true)
sigils =
Map.new(sigils, fn {key, value} ->
@@ -199,7 +219,9 @@ defmodule Code.Formatter do
operand_nesting: 2,
skip_eol: false,
comments: comments,
sigils: sigils
sigils: sigils,
file: file,
normalize_bitstring_modifiers: normalize_bitstring_modifiers
}
end
@@ -270,7 +292,7 @@ defmodule Code.Formatter do
{doc, state} =
entries
|> prepend_heredoc_line()
|> interpolation_to_algebra(:heredoc, state, @double_heredoc, @double_heredoc)
|> interpolation_to_algebra(~s["""], state, @double_heredoc, @double_heredoc)
{force_unfit(doc), state}
@@ -292,7 +314,7 @@ defmodule Code.Formatter do
{doc, state} =
entries
|> prepend_heredoc_line()
|> list_interpolation_to_algebra(:heredoc, state, @single_heredoc, @single_heredoc)
|> list_interpolation_to_algebra(~s['''], state, @single_heredoc, @single_heredoc)
{force_unfit(doc), state}
@@ -355,7 +377,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra({:__block__, meta, [list]}, _context, state) when is_list(list) do
case meta[:delimiter] do
~s['''] ->
string = list |> List.to_string() |> escape_heredoc()
string = list |> List.to_string() |> escape_heredoc(~s['''])
{@single_heredoc |> concat(string) |> concat(@single_heredoc) |> force_unfit(), state}
~s['] ->
@@ -369,7 +391,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra({:__block__, meta, [string]}, _context, state) when is_binary(string) do
if meta[:delimiter] == ~s["""] do
string = escape_heredoc(string)
string = escape_heredoc(string, ~s["""])
{@double_heredoc |> concat(string) |> concat(@double_heredoc) |> force_unfit(), state}
else
string = escape_string(string, @double_quote)
@@ -377,8 +399,8 @@ defmodule Code.Formatter do
end
end
defp quoted_to_algebra({:__block__, _, [atom]}, _context, state) when is_atom(atom) do
{atom_to_algebra(atom), state}
defp quoted_to_algebra({:__block__, meta, [atom]}, _context, state) when is_atom(atom) do
{atom_to_algebra(atom, meta), state}
end
defp quoted_to_algebra({:__block__, meta, [integer]}, _context, state)
@@ -443,6 +465,15 @@ defmodule Code.Formatter do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
end
# ..
defp quoted_to_algebra({:.., _meta, []}, context, state) do
if context in [:no_parens_arg, :no_parens_one_arg] do
{"(..)", state}
else
{"..", state}
end
end
# 1..2//3
defp quoted_to_algebra({:"..//", meta, [left, middle, right]}, context, state) do
quoted_to_algebra({:"//", meta, [{:.., meta, [left, middle]}, right]}, context, state)
@@ -487,12 +518,10 @@ defmodule Code.Formatter do
{:__block__, _, [atom]} when is_atom(atom) ->
key =
case Code.Identifier.classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([Atom.to_string(atom), ?:])
_ ->
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
if Macro.classify_atom(atom) in [:identifier, :unquoted] do
IO.iodata_to_binary([Atom.to_string(atom), ?:])
else
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
end
{string(key), state}
@@ -865,7 +894,7 @@ defmodule Code.Formatter do
# @foo(bar)
defp module_attribute_to_algebra(meta, {name, call_meta, [_] = args} = expr, context, state)
when is_atom(name) and name not in [:__block__, :__aliases__] do
if Code.Identifier.classify(name) == :callable_local do
if Macro.classify_atom(name) == :identifier do
{{call_doc, state}, wrap_in_parens?} =
call_args_to_algebra(args, call_meta, context, :skip_unless_many_args, false, state)
@@ -910,7 +939,7 @@ defmodule Code.Formatter do
)
when is_atom(fun) and is_integer(arity) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Code.Identifier.inspect_as_function(fun)
fun = Macro.inspect_atom(:remote_call, fun)
{target_doc |> nest(1) |> concat(string(".#{fun}/#{arity}")), state}
end
@@ -955,7 +984,7 @@ defmodule Code.Formatter do
defp remote_to_algebra({{:., _, [target, fun]}, meta, args}, context, state)
when is_atom(fun) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Code.Identifier.inspect_as_function(fun)
fun = Macro.inspect_atom(:remote_call, fun)
remote_doc = target_doc |> concat(".") |> concat(string(fun))
if args == [] and not remote_target_is_a_module?(target) and not meta?(meta, :closing) do
@@ -1268,7 +1297,7 @@ defmodule Code.Formatter do
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
{doc, state} = interpolation_to_string(quoted, state)
{doc, state} = interpolation_to_algebra(quoted, state)
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1284,7 +1313,7 @@ defmodule Code.Formatter do
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{:"::", _, [{{:., _, [Kernel, :to_string]}, _meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = interpolation_to_string(quoted, state)
{doc, state} = interpolation_to_algebra(quoted, state)
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1292,21 +1321,9 @@ defmodule Code.Formatter do
{concat(acc, last), state}
end
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
defp interpolation_to_algebra(quoted, %{skip_eol: skip_eol} = state) do
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
doc = interpolation_to_string(surround("\#{", doc, "}"))
{doc, %{state | skip_eol: skip_eol}}
end
defp interpolation_to_string(doc) do
[head | tail] =
doc
|> format_to_string()
|> String.split("\n")
Enum.reduce(tail, string(head), fn line, acc ->
concat([acc, line(), string(line)])
end)
{no_limit(surround("\#{", doc, "}")), %{state | skip_eol: skip_eol}}
end
## Sigils
@@ -1320,13 +1337,21 @@ defmodule Code.Formatter do
entries =
case state.sigils do
%{^name => callback} ->
case callback.(hd(entries), sigil: List.to_atom([name]), modifiers: modifiers) do
binary when is_binary(binary) ->
[binary]
metadata = [
file: state.file,
line: meta[:line],
sigil: List.to_atom([name]),
modifiers: modifiers,
opening_delimiter: opening_delimiter
]
case callback.(hd(entries), metadata) do
iodata when is_binary(iodata) or is_list(iodata) ->
[IO.iodata_to_binary(iodata)]
other ->
raise ArgumentError,
"expected sigil callback to return a binary, got: #{inspect(other)}"
"expected sigil callback to return iodata, got: #{inspect(other)}"
end
%{} ->
@@ -1339,7 +1364,7 @@ defmodule Code.Formatter do
{doc, state} =
entries
|> prepend_heredoc_line()
|> interpolation_to_algebra(:heredoc, state, doc, closing_delimiter)
|> interpolation_to_algebra(opening_delimiter, state, doc, closing_delimiter)
{force_unfit(doc), state}
else
@@ -1407,14 +1432,30 @@ defmodule Code.Formatter do
defp bitstring_spec_to_algebra({op, _, [left, right]}, state) when op in [:-, :*] do
{left, state} = bitstring_spec_to_algebra(left, state)
{right, state} = quoted_to_algebra_with_parens_if_operator(right, :parens_arg, state)
{right, state} = bitstring_spec_element_to_algebra(right, state)
{concat(concat(left, Atom.to_string(op)), right), state}
end
defp bitstring_spec_to_algebra(spec, state) do
quoted_to_algebra_with_parens_if_operator(spec, :parens_arg, state)
bitstring_spec_element_to_algebra(spec, state)
end
defp bitstring_spec_element_to_algebra(
{atom, meta, empty_args},
state = %{normalize_bitstring_modifiers: true}
)
when is_atom(atom) and empty_args in [nil, []] do
empty_args = bitstring_spec_normalize_empty_args(atom)
quoted_to_algebra_with_parens_if_operator({atom, meta, empty_args}, :parens_arg, state)
end
defp bitstring_spec_element_to_algebra(spec_element, state) do
quoted_to_algebra_with_parens_if_operator(spec_element, :parens_arg, state)
end
defp bitstring_spec_normalize_empty_args(atom) when atom in @bitstring_modifiers, do: nil
defp bitstring_spec_normalize_empty_args(_atom), do: []
defp bitstring_wrap_parens(doc, i, last) when i == 0 or i == last do
string = format_to_string(doc)
@@ -1482,25 +1523,36 @@ defmodule Code.Formatter do
end
end
defp atom_to_algebra(atom) when atom in [nil, true, false] do
defp atom_to_algebra(atom, _) when atom in [nil, true, false] do
Atom.to_string(atom)
end
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
defp atom_to_algebra(:"..//") do
defp atom_to_algebra(:"..//", _) do
string(":\"..//\"")
end
defp atom_to_algebra(atom) do
defp atom_to_algebra(:\\, meta) do
# Since we parse strings without unescaping, the atoms
# :\\ and :"\\" have the same representation, so we need
# to check the delimiter and handle them accordingly.
string =
case Keyword.get(meta, :delimiter) do
"\"" -> ":\"\\\\\""
_ -> ":\\\\"
end
string(string)
end
defp atom_to_algebra(atom, _) do
string = Atom.to_string(atom)
iodata =
case Code.Identifier.classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
[?:, string]
_ ->
[?:, ?", String.replace(string, "\"", "\\\""), ?"]
if Macro.classify_atom(atom) in [:unquoted, :identifier] do
[?:, string]
else
[?:, ?", String.replace(string, "\"", "\\\""), ?"]
end
iodata |> IO.iodata_to_binary() |> string()
@@ -1548,11 +1600,13 @@ defmodule Code.Formatter do
end
end
defp escape_heredoc(string) do
defp escape_heredoc(string, escape) do
string = String.replace(string, escape, "\\" <> escape)
heredoc_to_algebra(["" | String.split(string, "\n")])
end
defp escape_string(string, :heredoc) do
defp escape_string(string, <<_, _, _>> = escape) do
string = String.replace(string, escape, "\\" <> escape)
heredoc_to_algebra(String.split(string, "\n"))
end
@@ -1645,12 +1699,15 @@ defmodule Code.Formatter do
) do
min_line = line(meta)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
break_or_line = clause_break_or_line(clauses, state)
doc =
"fn ->"
|> glue(body_doc)
|> concat(break_or_line)
|> concat(body_doc)
|> nest(2)
|> glue("end")
|> concat(break_or_line)
|> concat("end")
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1679,12 +1736,16 @@ defmodule Code.Formatter do
|> nest(:cursor)
|> group()
break_or_line = clause_break_or_line(clauses, state)
doc =
"fn "
|> concat(head)
|> glue(body_doc)
|> concat(break_or_line)
|> concat(body_doc)
|> nest(2)
|> glue("end")
|> concat(break_or_line)
|> concat("end")
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1726,13 +1787,14 @@ defmodule Code.Formatter do
min_line = line(meta)
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
break_or_line = clause_break_or_line(clauses, state)
doc =
args_doc
|> ungroup_if_group()
|> concat(" ->")
|> group()
|> concat(break() |> concat(body_doc) |> nest(2))
|> concat(break_or_line |> concat(body_doc) |> nest(2))
|> wrap_in_parens()
|> maybe_force_clauses(clauses, state)
|> group()
@@ -1753,12 +1815,21 @@ defmodule Code.Formatter do
## Clauses
defp multi_line_clauses?(clauses, state) do
Enum.any?(clauses, fn {:->, meta, [_, block]} ->
eol?(meta, state) or multi_line_block?(block)
end)
end
defp multi_line_block?({:__block__, _, [_, _ | _]}), do: true
defp multi_line_block?(_), do: false
defp clause_break_or_line(clauses, state) do
if multi_line_clauses?(clauses, state), do: line(), else: break()
end
defp maybe_force_clauses(doc, clauses, state) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
force_unfit(doc)
else
doc
end
if multi_line_clauses?(clauses, state), do: force_unfit(doc), else: doc
end
defp clauses_to_algebra([{:->, _, _} | _] = clauses, min_line, max_line, state) do
@@ -1873,19 +1944,25 @@ defmodule Code.Formatter do
end
defp each_quoted_to_algebra_with_comments([arg | args], acc, max_line, state, comments?, fun) do
{doc_start, doc_end} = traverse_line(arg, {@max_line, @min_line})
case traverse_line(arg, {@max_line, @min_line}) do
{@max_line, @min_line} ->
{doc_triplet, state} = fun.(arg, args, state)
acc = [doc_triplet | acc]
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
{acc, comments, comments?} =
extract_comments_before(doc_start, acc, state.comments, comments?)
{doc_start, doc_end} ->
{acc, comments, comments?} =
extract_comments_before(doc_start, acc, state.comments, comments?)
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
{acc, comments, comments?} =
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
{acc, comments, comments?} =
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
state = %{state | comments: comments}
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
state = %{state | comments: comments}
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
end
end
defp extract_comments_before(max, acc, [%{line: line} = comment | rest], _) when line < max do
@@ -2034,7 +2111,7 @@ defmodule Code.Formatter do
defp module_attribute_read?({:@, _, [{var, _, var_context}]})
when is_atom(var) and is_atom(var_context) do
Code.Identifier.classify(var) == :callable_local
Macro.classify_atom(var) == :identifier
end
defp module_attribute_read?(_), do: false
+308 -98
View File
@@ -42,12 +42,19 @@ defmodule Code.Fragment do
* `{:alias, charlist}` - the context is an alias, potentially
a nested one, such as `Hello.Wor` or `HelloWor`
* `{:alias, inside_alias, charlist}` - the context is an alias, potentially
a nested one, where `inside_alias` is an expression `{:module_attribute, charlist}`
or `{:local_or_var, charlist}` and `charlist` is a static part
Examples are `__MODULE__.Submodule` or `@hello.Submodule`
* `{:dot, inside_dot, charlist}` - the context is a dot
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, this may either be a remote call or a map
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`. If `charlist`
is empty and `inside_dot` is an alias, then the autocompletion may either
be an alias or a remote call.
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
@@ -95,7 +102,10 @@ defmodule Code.Fragment do
of a sigil, such as `~` or `~s`, or an operator starting with `~`, such as
`~>` and `~>>`
* `{:struct, charlist}` - the context is a struct, such as `%`, `%UR` or `%URI`
* `{:struct, inside_struct}` - the context is a struct, such as `%`, `%UR` or `%URI`.
`inside_struct` can either be a `charlist` in case of a static alias or an
expression `{:alias, inside_alias, charlist}`, `{:module_attribute, charlist}`,
`{:local_or_var, charlist}`, `{:dot, inside_dot, charlist}`
* `{:unquoted_atom, charlist}` - the context is an unquoted atom. This
can be any atom or an atom representing a module
@@ -109,6 +119,7 @@ defmodule Code.Fragment do
@doc since: "1.13.0"
@spec cursor_context(List.Chars.t(), keyword()) ::
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:dot_arity, inside_dot, charlist}
| {:dot_call, inside_dot, charlist}
@@ -122,42 +133,30 @@ defmodule Code.Fragment do
| {:operator_call, charlist}
| :none
| {:sigil, charlist}
| {:struct, charlist}
| {:struct, inside_struct}
| {:unquoted_atom, charlist}
when inside_dot:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
| {:var, charlist},
inside_alias:
{:local_or_var, charlist}
| {:module_attribute, charlist},
inside_struct:
charlist
| {:alias, inside_alias, charlist}
| {:local_or_var, charlist}
| {:module_attribute, charlist}
| {:dot, inside_dot, charlist}
def cursor_context(fragment, opts \\ [])
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
binary =
case :binary.matches(binary, "\n") do
[] ->
binary
matches ->
{position, _} = List.last(matches)
binary_part(binary, position + 1, byte_size(binary) - position - 1)
end
binary
|> String.to_charlist()
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
end
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
charlist =
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
[?\n | _] -> []
rest -> rest
end
charlist
def cursor_context(fragment, opts)
when (is_binary(fragment) or is_list(fragment)) and is_list(opts) do
fragment
|> last_line()
|> :lists.reverse()
|> codepoint_cursor_context(opts)
|> elem(0)
@@ -178,7 +177,7 @@ defmodule Code.Fragment do
@operators ++ @starter_punctuation ++ @non_starter_punctuation ++ @space
@textual_operators ~w(when not and or in)c
@incomplete_operators ~w(^^ ~~ ~)c
@keywords ~w(do end after else catch rescue fn true false nil)c
defp codepoint_cursor_context(reverse, _opts) do
{stripped, spaces} = strip_spaces(reverse, 0)
@@ -250,6 +249,9 @@ defmodule Code.Fragment do
:operator ->
operator(reverse, count, [], call_op?)
{:struct, {:module_attribute, acc}, count} ->
{{:struct, {:module_attribute, acc}}, count + 1}
{:module_attribute, acc, count} ->
{{:module_attribute, acc}, count}
@@ -274,6 +276,12 @@ defmodule Code.Fragment do
{:identifier, _, acc, count} when call_op? and acc in @textual_operators ->
{{:operator, acc}, count}
{:identifier, [?%], acc, count} ->
case identifier_to_cursor_context(acc |> Enum.reverse(), count, true) do
{{:local_or_var, _} = idenifier, _} -> {{:struct, idenifier}, count + 1}
_ -> {:none, 0}
end
{:identifier, rest, acc, count} ->
case strip_spaces(rest, count) do
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
@@ -300,6 +308,7 @@ defmodule Code.Fragment do
defp rest_identifier(rest, count, [?@ | acc]) do
case tokenize_identifier(rest, count, acc) do
{:identifier, [?% | _rest], acc, count} -> {:struct, {:module_attribute, acc}, count}
{:identifier, _rest, acc, count} -> {:module_attribute, acc, count}
:none when acc == [] -> {:module_attribute, '', count}
_ -> :none
@@ -338,7 +347,7 @@ defmodule Code.Fragment do
:none
{kind, _, [], _, _, extra} ->
if ?@ in extra do
if :at in extra do
:none
else
{kind, rest, acc, count}
@@ -353,9 +362,29 @@ defmodule Code.Fragment do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:struct, prev}, count} -> {{:struct, prev ++ '.' ++ acc}, count}
{{:alias, prev}, count} -> {{:alias, prev ++ '.' ++ acc}, count}
_ -> {:none, 0}
{{:struct, prev}, count} when is_list(prev) ->
{{:struct, prev ++ '.' ++ acc}, count}
{{:struct, {:alias, parent, prev}}, count} ->
{{:struct, {:alias, parent, prev ++ '.' ++ acc}}, count}
{{:struct, prev}, count} ->
{{:struct, {:alias, prev, acc}}, count}
{{:alias, prev}, count} ->
{{:alias, prev ++ '.' ++ acc}, count}
{{:alias, parent, prev}, count} ->
{{:alias, parent, prev ++ '.' ++ acc}, count}
{{:local_or_var, prev}, count} ->
{{:alias, {:local_or_var, prev}, acc}, count}
{{:module_attribute, prev}, count} ->
{{:alias, {:module_attribute, prev}, acc}, count}
_ ->
{:none, 0}
end
end
@@ -363,13 +392,32 @@ defmodule Code.Fragment do
{rest, count} = strip_spaces(rest, count)
case identifier_to_cursor_context(rest, count, true) do
{{:local_or_var, var}, count} -> {{:dot, {:var, var}, acc}, count}
{{:unquoted_atom, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:alias, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:dot, _, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:module_attribute, _} = prev, count} -> {{:dot, prev, acc}, count}
{{:struct, acc}, count} -> {{:struct, acc ++ '.'}, count}
{_, _} -> {:none, 0}
{{:local_or_var, var}, count} ->
{{:dot, {:var, var}, acc}, count}
{{:unquoted_atom, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:alias, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:alias, _, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:struct, inner}, count} when is_list(inner) ->
{{:struct, {:dot, {:alias, inner}, acc}}, count}
{{:struct, inner}, count} ->
{{:struct, {:dot, inner, acc}}, count}
{{:dot, _, _} = prev, count} ->
{{:dot, prev, acc}, count}
{{:module_attribute, _} = prev, count} ->
{{:dot, prev, acc}, count}
{_, _} ->
{:none, 0}
end
end
@@ -377,24 +425,6 @@ defmodule Code.Fragment do
operator(rest, count + 1, [h | acc], call_op?)
end
defp operator(rest, count, acc, call_op?) when acc in @incomplete_operators do
{rest, dot_count} = strip_spaces(rest, count)
cond do
call_op? ->
{:none, 0}
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
dot(tl(rest), dot_count + 1, acc)
acc == '~' ->
{{:sigil, ''}, count}
true ->
{{:operator, acc}, count}
end
end
# If we are opening a sigil, ignore the operator.
defp operator([letter, ?~ | rest], _count, [op], _call_op?)
when op in '<|/' and (letter in ?A..?Z or letter in ?a..?z) and
@@ -402,6 +432,16 @@ defmodule Code.Fragment do
{:none, 0}
end
defp operator(rest, count, '~', call_op?) do
{rest, _} = strip_spaces(rest, count)
if call_op? or match?([?. | rest] when rest == [] or hd(rest) != ?., rest) do
{:none, 0}
else
{{:sigil, ''}, count}
end
end
defp operator(rest, count, acc, _call_op?) do
case :elixir_tokenizer.tokenize(acc, 1, 1, []) do
{:ok, _, _, _, [{:atom, _, _}]} ->
@@ -489,43 +529,63 @@ defmodule Code.Fragment do
* This function never returns empty sigils `{:sigil, ''}` or empty structs
`{:struct, ''}` as context
* This function returns keywords as `{:keyword, 'do'}`
* This function never returns `:expr`
"""
@doc since: "1.13.0"
@spec surround_context(List.Chars.t(), position(), keyword()) ::
%{begin: position, end: position, context: context} | :none
when context:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:local_or_var, charlist}
| {:local_arity, charlist}
| {:local_call, charlist}
| {:module_attribute, charlist}
| {:operator, charlist}
| {:unquoted_atom, charlist},
| {:sigil, charlist}
| {:struct, inside_struct}
| {:unquoted_atom, charlist}
| {:keyword, charlist},
inside_dot:
{:alias, charlist}
| {:alias, inside_alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
| {:var, charlist},
inside_alias:
{:local_or_var, charlist}
| {:module_attribute, charlist},
inside_struct:
charlist
| {:alias, inside_alias, charlist}
| {:local_or_var, charlist}
| {:module_attribute, charlist}
| {:dot, inside_dot, charlist}
def surround_context(fragment, position, options \\ [])
def surround_context(binary, {line, column}, opts) when is_binary(binary) do
binary
|> String.split("\n")
|> Enum.at(line - 1, '')
|> String.to_charlist()
|> position_surround_context(line, column, opts)
end
def surround_context(string, {line, column}, opts)
when (is_binary(string) or is_list(string)) and is_list(opts) do
{charlist, lines_before_lengths, lines_current_and_after_lengths} =
surround_line(string, line, column)
prepended_columns = Enum.sum(lines_before_lengths)
def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
charlist
|> :string.split('\n', :all)
|> Enum.at(line - 1, '')
|> position_surround_context(line, column, opts)
|> position_surround_context(line, column + prepended_columns, opts)
|> to_multiline_range(
prepended_columns,
lines_before_lengths,
lines_current_and_after_lengths
)
end
def surround_context(other, position, opts) do
def surround_context(other, {_, _} = position, opts) do
surround_context(to_charlist(other), position, opts)
end
@@ -549,6 +609,9 @@ defmodule Code.Fragment do
{{:alias, acc}, offset} ->
build_surround({:alias, acc}, reversed, line, offset)
{{:alias, parent, acc}, offset} ->
build_surround({:alias, parent, acc}, reversed, line, offset)
{{:dot, _, [_ | _]} = dot, offset} ->
build_surround(dot, reversed, line, offset)
@@ -561,7 +624,10 @@ defmodule Code.Fragment do
{{:local_or_var, acc}, offset} when acc in @textual_operators ->
build_surround({:operator, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} when acc not in ~w(do end after else catch rescue)c ->
{{:local_or_var, acc}, offset} when acc in @keywords ->
build_surround({:keyword, acc}, reversed, line, offset)
{{:local_or_var, acc}, offset} ->
build_surround({:local_or_var, acc}, reversed, line, offset)
{{:module_attribute, ''}, offset} ->
@@ -605,7 +671,7 @@ defmodule Code.Fragment do
reversed = reversed_post ++ reversed_pre
case codepoint_cursor_context(reversed, opts) do
{{:operator, acc}, offset} when acc not in @incomplete_operators ->
{{:operator, acc}, offset} ->
build_surround({:operator, acc}, reversed, line, offset)
{{:sigil, ''}, offset} when hd(rest) in ?A..?Z or hd(rest) in ?a..?z ->
@@ -720,10 +786,156 @@ defmodule Code.Fragment do
defp enum_reverse_at([h | t], n, acc) when n > 0, do: enum_reverse_at(t, n - 1, [h | acc])
defp enum_reverse_at(rest, _, acc), do: {acc, rest}
defp last_line(binary) when is_binary(binary) do
[last_line | lines_reverse] =
binary
|> String.split(["\r\n", "\n"])
|> Enum.reverse()
prepend_cursor_lines(lines_reverse, String.to_charlist(last_line))
end
defp last_line(charlist) when is_list(charlist) do
[last_line | lines_reverse] =
charlist
|> :string.replace('\r\n', '\n', :all)
|> :string.join('')
|> :string.split('\n', :all)
|> Enum.reverse()
prepend_cursor_lines(lines_reverse, last_line)
end
defp prepend_cursor_lines(lines, last_line) do
with [line | lines] <- lines,
{trimmed_line, incomplete?} = ends_as_incomplete(to_charlist(line), [], true),
true <- incomplete? or starts_with_dot?(last_line) do
prepend_cursor_lines(lines, Enum.reverse(trimmed_line, last_line))
else
_ -> last_line
end
end
defp starts_with_dot?([?. | _]), do: true
defp starts_with_dot?([h | t]) when h in @space, do: starts_with_dot?(t)
defp starts_with_dot?(_), do: false
defp ends_as_incomplete([?# | _], acc, incomplete?),
do: {acc, incomplete?}
defp ends_as_incomplete([h | t], acc, _incomplete?) when h in [?(, ?.],
do: ends_as_incomplete(t, [h | acc], true)
defp ends_as_incomplete([h | t], acc, incomplete?) when h in @space,
do: ends_as_incomplete(t, [h | acc], incomplete?)
defp ends_as_incomplete([h | t], acc, _incomplete?),
do: ends_as_incomplete(t, [h | acc], false)
defp ends_as_incomplete([], acc, incomplete?),
do: {acc, incomplete?}
defp surround_line(binary, line, column) when is_binary(binary) do
binary
|> String.split(["\r\n", "\n"])
|> Enum.map(&String.to_charlist/1)
|> surround_lines(line, column)
end
defp surround_line(charlist, line, column) when is_list(charlist) do
charlist
|> :string.replace('\r\n', '\n', :all)
|> :string.join('')
|> :string.split('\n', :all)
|> surround_lines(line, column)
end
defp surround_lines(lines, line, column) do
{lines_before_reverse, cursor_line, lines_after} = split_at(lines, line, [])
{trimmed_cursor_line, incomplete?} = ends_as_incomplete(to_charlist(cursor_line), [], true)
reversed_cursor_line =
if column - 1 > length(trimmed_cursor_line) do
# Don't strip comments if cursor is inside a comment
Enum.reverse(cursor_line)
else
trimmed_cursor_line
end
{cursor_line, after_lengths} =
append_surround_lines(lines_after, [], [reversed_cursor_line], incomplete?)
{cursor_line, before_lengths} = prepend_surround_lines(lines_before_reverse, [], cursor_line)
{cursor_line, before_lengths, [length(reversed_cursor_line) | after_lengths]}
end
defp split_at([line], _, acc), do: {acc, line, []}
defp split_at([line | lines], 1, acc), do: {acc, line, lines}
defp split_at([line | lines], count, acc), do: split_at(lines, count - 1, [line | acc])
defp prepend_surround_lines(lines, lengths, last_line) do
with [line | lines] <- lines,
{trimmed_line, incomplete?} = ends_as_incomplete(to_charlist(line), [], true),
true <- incomplete? or starts_with_dot?(last_line) do
lengths = [length(trimmed_line) | lengths]
prepend_surround_lines(lines, lengths, Enum.reverse(trimmed_line, last_line))
else
_ -> {last_line, Enum.reverse(lengths)}
end
end
defp append_surround_lines(lines, lengths, acc_lines, incomplete?) do
with [line | lines] <- lines,
line = to_charlist(line),
true <- incomplete? or starts_with_dot?(line) do
{trimmed_line, incomplete?} = ends_as_incomplete(line, [], true)
lengths = [length(trimmed_line) | lengths]
append_surround_lines(lines, lengths, [trimmed_line | acc_lines], incomplete?)
else
_ -> {Enum.reduce(acc_lines, [], &Enum.reverse/2), Enum.reverse(lengths)}
end
end
defp to_multiline_range(:none, _, _, _), do: :none
defp to_multiline_range(
%{begin: {begin_line, begin_column}, end: {end_line, end_column}} = context,
prepended,
lines_before_lengths,
lines_current_and_after_lengths
) do
{begin_line, begin_column} =
Enum.reduce_while(lines_before_lengths, {begin_line, begin_column - prepended}, fn
line_length, {acc_line, acc_column} ->
if acc_column < 1 do
{:cont, {acc_line - 1, acc_column + line_length}}
else
{:halt, {acc_line, acc_column}}
end
end)
{end_line, end_column} =
Enum.reduce_while(lines_current_and_after_lengths, {end_line, end_column - prepended}, fn
line_length, {acc_line, acc_column} ->
if acc_column > line_length + 1 do
{:cont, {acc_line + 1, acc_column - line_length}}
else
{:halt, {acc_line, acc_column}}
end
end)
%{context | begin: {begin_line, begin_column}, end: {end_line, end_column}}
end
@doc """
Receives a code fragment and returns a quoted expression
Receives a string and returns a quoted expression
with a cursor at the nearest argument position.
This function receives a string with an Elixir code fragment,
representing a cursor position, and converts such string to
AST with the inclusion of special `__cursor__()` node based
on the position of the cursor with a container.
A container is any Elixir expression starting with `(`,
`{`, and `[`. This includes function calls, tuples, lists,
maps, and so on. For example, take this code, which would
@@ -765,7 +977,7 @@ defmodule Code.Fragment do
max(some_value, 1 + another_val
max(some_value, 1 |> some_fun() |> another_fun
On the other hand, tuples, lists, maps, etc all retain the
On the other hand, tuples, lists, maps, and binaries all retain the
cursor position:
max(some_value, [1, 2,
@@ -789,9 +1001,21 @@ defmodule Code.Fragment do
## Examples
Function call:
iex> Code.Fragment.container_cursor_to_quoted("max(some_value, ")
{:ok, {:max, [line: 1], [{:some_value, [line: 1], nil}, {:__cursor__, [line: 1], []}]}}
Containers (for example, a list):
iex> Code.Fragment.container_cursor_to_quoted("[some, value")
{:ok, [{:some, [line: 1], nil}, {:__cursor__, [line: 1], []}]}
For binaries, the `::` is exclusively kept as an operator:
iex> Code.Fragment.container_cursor_to_quoted("<<some::integer")
{:ok, {:<<>>, [line: 1], [{:"::", [line: 1], [{:some, [line: 1], nil}, {:__cursor__, [line: 1], []}]}]}}
## Options
* `:file` - the filename to be reported in case of parsing errors.
@@ -811,31 +1035,17 @@ defmodule Code.Fragment do
tokens, for closing tokens, end of expressions, as well as delimiters
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
* `:literal_encoder` - a function to encode literals in the AST.
See the documentation for `Code.string_to_quoted/2` for more information.
"""
@doc since: "1.13.0"
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
def container_cursor_to_quoted(fragment, opts \\ []) do
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
column = Keyword.get(opts, :column, 1)
columns = Keyword.get(opts, :columns, false)
token_metadata = Keyword.get(opts, :token_metadata, false)
opts =
Keyword.take(opts, [:file, :line, :column, :columns, :token_metadata, :literal_encoder])
fragment = to_charlist(fragment)
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
{:ok, _, _, _warnings, tokens} ->
:elixir.tokens_to_quoted(tokens, file, columns: columns, token_metadata: token_metadata)
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, {to_string(prefix), to_string(suffix)}, to_string(token)}}
{:error, {line, column, error, token}, _rest, _warnings, _so_far} ->
location = [line: line, column: column]
{:error, {location, to_string(error), to_string(token)}}
end
Code.string_to_quoted(fragment, [cursor_completion: true, emit_warnings: false] ++ opts)
end
end
+9 -148
View File
@@ -14,7 +14,7 @@ defmodule Code.Identifier do
def unary_op(op) do
cond do
op in [:&] -> {:non_associative, 90}
op in [:!, :^, :not, :+, :-, :~~~] -> {:non_associative, 300}
op in [:!, :^, :not, :+, :-, :"~~~"] -> {:non_associative, 300}
op in [:@] -> {:non_associative, 320}
true -> :error
end
@@ -41,9 +41,9 @@ defmodule Code.Identifier do
op in [:&&, :&&&, :and] -> {:left, 130}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
op in [:<, :<=, :>=, :>] -> {:left, 150}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :"<|>"] -> {:left, 160}
op in [:in] -> {:left, 170}
op in [:^^^] -> {:left, 180}
op in [:"^^^"] -> {:left, 180}
op in [:"//"] -> {:right, 190}
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
@@ -54,149 +54,6 @@ defmodule Code.Identifier do
end
end
@doc """
Classifies the given atom into one of the following categories:
* `:alias` - a valid Elixir alias, like `Foo`, `Foo.Bar` and so on
* `:callable_local` - an atom that can be used as a local call;
this category includes identifiers like `:foo`
* `:callable_operator` - all callable operators, such as `:<>`. Note
operators such as `:..` are not callable because of ambiguity
* `:not_atomable` - callable operators that must be wrapped in quotes when
defined as an atom. For example, `::` must be written as `:"::"` to avoid
the ambiguity between the atom and the keyword identifier
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator. Those are typically AST nodes that are special forms (such as
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
`:..` and `:...`). This category also includes atoms like `:Foo`, since
they are valid identifiers but they need quotes to be used in function
calls (`Foo."Bar"`)
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
"""
def classify(atom) when is_atom(atom) do
charlist = Atom.to_charlist(atom)
cond do
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
:not_callable
atom in [:"::", :"//"] ->
:not_atomable
unary_op(atom) != :error or binary_op(atom) != :error ->
:callable_operator
valid_alias?(charlist) ->
:alias
true ->
case :elixir_config.identifier_tokenizer().tokenize(charlist) do
{kind, _acc, [], _, _, special} ->
if kind == :identifier and not :lists.member(?@, special) do
:callable_local
else
:not_callable
end
_ ->
:other
end
end
end
defp valid_alias?('Elixir' ++ rest), do: valid_alias_piece?(rest)
defp valid_alias?(_other), do: false
defp valid_alias_piece?([?., char | rest]) when char >= ?A and char <= ?Z,
do: valid_alias_piece?(trim_leading_while_valid_identifier(rest))
defp valid_alias_piece?([]), do: true
defp valid_alias_piece?(_other), do: false
defp trim_leading_while_valid_identifier([char | rest])
when char >= ?a and char <= ?z
when char >= ?A and char <= ?Z
when char >= ?0 and char <= ?9
when char == ?_ do
trim_leading_while_valid_identifier(rest)
end
defp trim_leading_while_valid_identifier(other) do
other
end
@doc """
Inspects the identifier as an atom.
"""
def inspect_as_atom(atom) when is_nil(atom) or is_boolean(atom) do
Atom.to_string(atom)
end
def inspect_as_atom(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
:alias ->
case binary do
binary when binary in ["Elixir", "Elixir.Elixir"] -> binary
"Elixir.Elixir." <> _rest -> binary
"Elixir." <> rest -> rest
end
type when type in [:callable_local, :callable_operator, :not_callable] ->
":" <> binary
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?:, ?", escaped, ?"])
end
end
@doc """
Inspects the given identifier as a key.
"""
def inspect_as_key(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([binary, ?:])
_ ->
{escaped, _} = escape(binary, ?")
IO.iodata_to_binary([?", escaped, ?", ?:])
end
end
@doc """
Inspects the given identifier as a function name.
"""
def inspect_as_function(atom) when is_atom(atom) do
binary = Atom.to_string(atom)
case classify(atom) do
type when type in [:callable_local, :callable_operator, :not_atomable] ->
binary
type ->
escaped =
if type in [:not_callable, :alias] do
binary
else
elem(escape(binary, ?"), 0)
end
IO.iodata_to_binary([?", escaped, ?"])
end
end
@doc """
Extracts the name and arity of the parent from the anonymous function identifier.
"""
@@ -214,8 +71,12 @@ defmodule Code.Identifier do
@doc """
Escapes the given identifier.
"""
def escape(other, char, count \\ :infinity, fun \\ &escape_map/1) do
escape(other, char, count, [], fun)
@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)
end
defp escape(<<_, _::binary>> = binary, _char, 0, acc, _fun) do
+52 -26
View File
@@ -63,15 +63,17 @@ defmodule Code.Normalizer do
end
# Bit containers
defp do_normalize({:<<>>, _, _} = quoted, state) do
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, [{:<<>>, _, _} = string, :utf8]},
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
[{:<<>>, _, args} = string, :utf8]},
state
) do
)
when is_list(args) do
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
call_meta = patch_meta_line(call_meta, dot_meta)
@@ -166,8 +168,8 @@ defmodule Code.Normalizer do
end
# Sigils
defp do_normalize({sigil, meta, [{:<<>>, _, _} = string, modifiers]} = quoted, state)
when is_atom(sigil) do
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 =
@@ -183,19 +185,40 @@ defmodule Code.Normalizer do
end
# Tuples
defp do_normalize({:{}, meta, args} = quoted, state) do
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
{last_arg, args} = List.pop_at(args, -1)
with [{{:__block__, key_meta, _}, _} | _] <- last_arg, :keyword <- key_meta[:format] do
args = normalize_kw_args(args, state)
kw_list = normalize_kw_args(last_arg, state)
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)
normalize_call(quoted, state)
end
end
# Module attributes
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
value =
cond do
keyword?(value) ->
normalize_kw_args(value, state, true)
is_list(value) ->
normalize_literal(value, meta, state)
true ->
do_normalize(value, state)
end
{:@, meta, [{name, name_meta, [value]}]}
end
# Regular blocks
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
{:__block__, meta, normalize_args(args, state)}
end
# Calls
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
normalize_call(quoted, state)
@@ -226,14 +249,18 @@ defmodule Code.Normalizer do
meta = patch_meta_line(meta, state.parent_meta)
literal = maybe_escape_literal(literal, state)
if is_atom(literal) and Code.Identifier.classify(literal) == :alias and
if is_atom(literal) and Macro.classify_atom(literal) == :alias and
is_nil(meta[:delimiter]) do
"Elixir." <> segments = Atom.to_string(literal)
segments =
segments
|> String.split(".")
|> Enum.map(&String.to_atom/1)
case Atom.to_string(literal) do
"Elixir" ->
[:"Elixir"]
"Elixir." <> segments ->
segments
|> String.split(".")
|> Enum.map(&String.to_atom/1)
end
{:__aliases__, meta, segments}
else
@@ -246,11 +273,10 @@ defmodule Code.Normalizer do
meta = patch_meta_line(meta, state.parent_meta)
state = %{state | parent_meta: meta}
with [{{:__block__, key_meta, _}, _} | _] <- right, :keyword <- key_meta[:format] do
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state)}]}
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)}]}
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
end
end
@@ -260,7 +286,7 @@ defmodule Code.Normalizer do
# It's a charlist
list =
if state.escape do
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), -1)
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), nil)
IO.iodata_to_binary(string) |> to_charlist()
else
list
@@ -282,7 +308,7 @@ defmodule Code.Normalizer do
meta
end
{:__block__, meta, [normalize_kw_args(list, state)]}
{:__block__, meta, [normalize_kw_args(list, state, false)]}
end
end
@@ -402,7 +428,7 @@ defmodule Code.Normalizer do
end
defp normalize_map_args(args, state) do
Enum.map(normalize_kw_args(args, state), fn
Enum.map(normalize_kw_args(args, state, false), fn
{:__block__, _, [{_, _} = pair]} -> pair
pair -> pair
end)
@@ -435,7 +461,7 @@ defmodule Code.Normalizer do
{form, meta, leading_args ++ [kw_blocks]}
end
defp normalize_kw_args(elems, state, keyword? \\ false)
defp normalize_kw_args(elems, state, keyword?)
defp normalize_kw_args(
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
@@ -494,7 +520,7 @@ defmodule Code.Normalizer do
end
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
{string, _} = Code.Identifier.escape(string, -1)
{string, _} = Code.Identifier.escape(string, nil)
IO.iodata_to_binary(string)
end
+5 -3
View File
@@ -174,9 +174,11 @@ defmodule Code.Typespec do
end
defp get_module_and_beam(module) when is_atom(module) do
case :code.get_object_code(module) do
{^module, beam, _filename} -> {module, beam}
:error -> :error
with {^module, beam, _filename} <- :code.get_object_code(module),
{:ok, ^module} <- beam |> :beam_lib.info() |> Keyword.fetch(:module) do
{module, beam}
else
_ -> :error
end
end
+2 -2
View File
@@ -35,7 +35,7 @@ defprotocol Collectable do
...> collector_fun.(acc, {:cont, elem})
...> end)
iex> collector_fun.(updated_acc, :done)
#MapSet<[1, 2, 3]>
MapSet.new([1, 2, 3])
To show how the protocol can be implemented, we can again look at the
simplified implementation for `MapSet`. In this implementation "collecting" elements
@@ -63,7 +63,7 @@ defprotocol Collectable do
So now we can call `Enum.into/2`:
iex> Enum.into([1, 2, 3], MapSet.new())
#MapSet<[1, 2, 3]>
MapSet.new([1, 2, 3])
"""
+38 -12
View File
@@ -34,12 +34,12 @@ defmodule Config do
`Config` also provides a low-level API for evaluating and reading
configuration, under the `Config.Reader` module.
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. Also note that
the `config/config.exs` of a library is not evaluated when the library is
used as a dependency, as configuration is always meant to configure the
current project. For more information, read our [library guidelines](library-guidelines.md).
> **Important:** if you are writing a library to be used by other developers,
> it is generally recommended to avoid the application environment, as the
> application environment is effectively a global storage. Also note that
> the `config/config.exs` of a library is not evaluated when the library is
> used as a dependency, as configuration is always meant to configure the
> current project. For more information, read our [library guidelines](library-guidelines.md).
## Migrating from `use Mix.Config`
@@ -62,7 +62,27 @@ defmodule Config do
import_config config
end
The last step is to replace all `Mix.env()` calls by `config_env()`.
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.
## config/runtime.exs
@@ -145,16 +165,24 @@ defmodule Config do
config :ecto, Repo,
log_level: :warn,
adapter: Ecto.Adapters.Postgres
adapter: Ecto.Adapters.Postgres,
metadata: [read_only: true]
config :ecto, Repo,
log_level: :info,
pool_size: 10
pool_size: 10,
metadata: [replica: true]
will have a final value of the configuration for the `Repo`
key in the `:ecto` application of:
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
Application.get_env(:ecto, Repo)
#=> [
#=> log_level: :info,
#=> pool_size: 10,
#=> adapter: Ecto.Adapters.Postgres,
#=> metadata: [read_only: true, replica: true]
#=> ]
"""
@doc since: "1.9.0"
@@ -293,8 +321,6 @@ 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
+2 -3
View File
@@ -341,8 +341,7 @@ defmodule Config.Provider do
defp restart_and_sleep() do
mode = Application.get_env(:elixir, @reboot_mode_key)
# TODO: Remove otp_release check once we require Erlang/OTP 23+
if :erlang.system_info(:otp_release) >= '23' and mode in [:embedded, :interactive] do
if mode in [:embedded, :interactive] do
:init.restart(mode: mode)
else
:init.restart()
@@ -410,7 +409,7 @@ defmodule Config.Provider do
end
defp abort(msg) do
IO.puts(:stderr, "ERROR! " <> msg)
IO.puts("ERROR! " <> msg)
:erlang.raise(:error, "aborting boot", [{Config.Provider, :boot, 2, []}])
end
end
+1 -1
View File
@@ -37,7 +37,7 @@ defmodule Config.Reader do
]
Remember Mix already loads `config/runtime.exs` by default.
For more examples and scenarios, see the `Config.Providers` module.
For more examples and scenarios, see the `Config.Provider` module.
"""
@behaviour Config.Provider
+1 -1
View File
@@ -23,7 +23,7 @@ defmodule Dict do
import Kernel, except: [size: 1]
if __CALLER__.module != HashDict do
IO.warn("use Dict is deprecated. " <> unquote(message), Macro.Env.stacktrace(__CALLER__))
IO.warn("use Dict is deprecated. " <> unquote(message), __CALLER__)
end
quote do
+123 -71
View File
@@ -1,21 +1,21 @@
defmodule DynamicSupervisor do
@moduledoc ~S"""
A supervisor that starts children dynamically.
A supervisor optimized to only start 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`. When a dynamic supervisor
terminates, all children are shut down at the same time, with no guarantee
of ordering.
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.
## Examples
A dynamic supervisor is started with no children, a supervision strategy
(the only strategy currently supported is `:one_for_one`), and a name:
A dynamic supervisor is started with no children and often a name:
children = [
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor}
{DynamicSupervisor, name: MyApp.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
@@ -37,6 +37,46 @@ 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
@@ -232,12 +272,74 @@ defmodule DynamicSupervisor do
@doc """
Starts a supervisor with the given options.
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.
This function is typically not invoked directly, instead it is invoked
when using a `DynamicSupervisor` as a child of another supervisor:
The `:name` option can also be used to register a supervisor name.
The supported values are described under the "Name registration"
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"
section in the `GenServer` module docs.
If the supervisor is successfully spawned, this function returns
@@ -251,35 +353,9 @@ 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(mod, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
def start_link(module, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {module, init_arg, opts[:name]}, opts)
end
@doc """
@@ -287,7 +363,9 @@ 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.
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.
If the child process start function returns `{:ok, child}` or `{:ok, child,
info}`, then child specification and PID are added to the supervisor and
@@ -476,46 +554,20 @@ defmodule DynamicSupervisor do
module-based supervisors. See the "Module-based supervisors" section
in the module documentation for more information.
The `options` received by this function are also supported by `start_link/1`.
This function returns a tuple containing the supervisor options.
It accepts the same `options` as `start_link/1` (except for `:name`)
and it returns a tuple containing the supervisor options.
## Examples
def init(_arg) do
DynamicSupervisor.init(max_children: 1000, strategy: :one_for_one)
DynamicSupervisor.init(max_children: 1000)
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
unless strategy = options[:strategy] do
raise ArgumentError, "expected :strategy option to be given"
end
strategy = Keyword.get(options, :strategy, :one_for_one)
intensity = Keyword.get(options, :max_restarts, 3)
period = Keyword.get(options, :max_seconds, 5)
max_children = Keyword.get(options, :max_children, :infinity)
+435 -160
View File
@@ -37,6 +37,23 @@ defprotocol Enumerable do
than linear time.
"""
@typedoc """
An enumerable of elements of type `element`.
This type is equivalent to `t:t/0` but is especially useful for documentation.
For example, imagine you define a function that expects an enumerable of
integers and returns an enumerable of strings:
@spec integers_to_strings(Enumerable.t(integer())) :: Enumerable.t(String.t())
def integers_to_strings(integers) do
Stream.map(integers, &Integer.to_string/1)
end
"""
@typedoc since: "1.14.0"
@type t(_element) :: t()
@typedoc """
The accumulator value for each step.
@@ -105,18 +122,24 @@ defprotocol Enumerable do
@type continuation :: (acc -> result)
@typedoc """
A slicing function that receives the initial position and the
number of elements in the slice.
A slicing function that receives the initial position,
the number of elements in the slice, and the step.
The `start` position is a number `>= 0` and guaranteed to
exist in the `enumerable`. The length is a number `>= 1` in a way
that `start + length <= count`, where `count` is the maximum
amount of elements in the enumerable.
exist in the `enumerable`. The length is a number `>= 1`
in a way that `start + length * step <= count`, where
`count` is the maximum amount of elements in the enumerable.
The function should return a non empty list where
the amount of elements is equal to `length`.
"""
@type slicing_fun :: (start :: non_neg_integer, length :: pos_integer -> [term()])
@type slicing_fun ::
(start :: non_neg_integer, length :: pos_integer, step :: pos_integer -> [term()])
@typedoc """
Receives an enumerable and returns a list.
"""
@type to_list_fun :: (t -> [term()])
@doc """
Reduces the `enumerable` into an element.
@@ -146,7 +169,7 @@ defprotocol Enumerable do
Retrieves the number of elements in the `enumerable`.
It should return `{:ok, count}` if you can count the number of elements
in `enumerable` without traversing it.
in `enumerable` in a faster way than fully traversing it.
Otherwise it should return `{:error, __MODULE__}` and a default algorithm
built on top of `reduce/3` that runs in linear time will be used.
@@ -173,28 +196,36 @@ defprotocol Enumerable do
@doc """
Returns a function that slices the data structure contiguously.
It should return `{:ok, size, slicing_fun}` if the `enumerable` has
a known bound and can access a position in the `enumerable` without
traversing all previous elements.
It should return either:
Otherwise it should return `{:error, __MODULE__}` and a default
algorithm built on top of `reduce/3` that runs in linear time will be
used.
* `{:ok, size, slicing_fun}` - if the `enumerable` has a known
bound and can access a position in the `enumerable` without
traversing all previous elements. The `slicing_fun` will receive
a `start` position, the `amount` of elements to fetch, and a
`step`.
* `{:ok, size, to_list_fun}` - if the `enumerable` has a known bound
and can access a position in the `enumerable` by first converting
it to a list via `to_list_fun`.
* `{:error, __MODULE__}` - the enumerable cannot be sliced efficiently
and a default algorithm built on top of `reduce/3` that runs in
linear time will be used.
## Differences to `count/1`
The `size` value returned by this function is used for boundary checks,
therefore it is extremely important that this function only returns `:ok`
if retrieving the `size` of the `enumerable` is cheap, fast and takes constant
time. Otherwise the simplest of operations, such as `Enum.at(enumerable, 0)`,
will become too expensive.
if retrieving the `size` of the `enumerable` is cheap, fast, and takes
constant time. Otherwise the simplest of operations, such as
`Enum.at(enumerable, 0)`, will become too expensive.
On the other hand, the `count/1` function in this protocol should be
implemented whenever you can count the number of elements in the collection without
traversing it.
implemented whenever you can count the number of elements in the collection
without traversing it.
"""
@spec slice(t) ::
{:ok, size :: non_neg_integer(), slicing_fun()}
{:ok, size :: non_neg_integer(), slicing_fun() | to_list_fun()}
| {:error, module()}
def slice(enumerable)
end
@@ -203,7 +234,7 @@ defmodule Enum do
import Kernel, except: [max: 2, min: 2]
@moduledoc """
Provides a set of algorithms to work with enumerables.
Functions for working with collections (known as enumerables).
In Elixir, an enumerable is any data type that implements the
`Enumerable` protocol. `List`s (`[1, 2, 3]`), `Map`s (`%{foo: 1, bar: 2}`)
@@ -434,7 +465,7 @@ defmodule Enum do
"""
@spec at(t, index, default) :: element | default
def at(enumerable, index, default \\ nil) when is_integer(index) do
case slice_any(enumerable, index, 1) do
case slice_forward(enumerable, index, 1, 1) do
[value] -> value
[] -> default
end
@@ -498,6 +529,9 @@ defmodule Enum do
iex> Enum.chunk_every([1, 2, 3, 4, 5], 2, 3, [])
[[1, 2], [4, 5]]
iex> Enum.chunk_every([1, 2, 3, 4], 3, 3, Stream.cycle([0]))
[[1, 2, 3], [4, 0, 0]]
"""
@doc since: "1.5.0"
@spec chunk_every(t, pos_integer, pos_integer, t | :discard) :: [list]
@@ -617,7 +651,7 @@ defmodule Enum do
Concatenates the enumerable on the `right` with the enumerable on the
`left`.
This function produces the same result as the `Kernel.++/2` operator
This function produces the same result as the `++/2` operator
for lists.
## Examples
@@ -850,17 +884,21 @@ defmodule Enum do
drop_list(enumerable, amount)
end
def drop(enumerable, amount) when is_integer(amount) and amount >= 0 do
def drop(enumerable, 0) do
to_list(enumerable)
end
def drop(enumerable, amount) when is_integer(amount) and amount > 0 do
{result, _} = reduce(enumerable, {[], amount}, R.drop())
if is_list(result), do: :lists.reverse(result), else: []
end
def drop(enumerable, amount) when is_integer(amount) and amount < 0 do
{count, fun} = slice_count_and_fun(enumerable)
{count, fun} = slice_count_and_fun(enumerable, 1)
amount = Kernel.min(amount + count, count)
if amount > 0 do
fun.(0, amount)
fun.(0, amount, 1)
else
[]
end
@@ -1003,7 +1041,7 @@ defmodule Enum do
"""
@spec fetch(t, index) :: {:ok, element} | :error
def fetch(enumerable, index) when is_integer(index) do
case slice_any(enumerable, index, 1) do
case slice_forward(enumerable, index, 1, 1) do
[value] -> {:ok, value}
[] -> :error
end
@@ -1029,7 +1067,7 @@ defmodule Enum do
"""
@spec fetch!(t, index) :: element
def fetch!(enumerable, index) when is_integer(index) do
case slice_any(enumerable, index, 1) do
case slice_forward(enumerable, index, 1, 1) do
[value] -> value
[] -> raise Enum.OutOfBoundsError
end
@@ -1460,9 +1498,27 @@ defmodule Enum do
defp into_protocol(enumerable, collectable) do
{initial, fun} = Collectable.into(collectable)
into_protocol(enumerable, initial, fun, fn entry, acc ->
fun.(acc, {:cont, entry})
try do
reduce_into_protocol(enumerable, initial, fun)
catch
kind, reason ->
fun.(initial, :halt)
:erlang.raise(kind, reason, __STACKTRACE__)
else
acc -> fun.(acc, :done)
end
end
defp reduce_into_protocol(enumerable, initial, fun) when is_list(enumerable) do
:lists.foldl(fn x, acc -> fun.(acc, {:cont, x}) end, initial, enumerable)
end
defp reduce_into_protocol(enumerable, initial, fun) do
enumerable
|> Enumerable.reduce({:cont, initial}, fn x, acc ->
{:cont, fun.(acc, {:cont, x})}
end)
|> elem(1)
end
@doc """
@@ -1509,14 +1565,8 @@ defmodule Enum do
defp into_protocol(enumerable, collectable, transform) do
{initial, fun} = Collectable.into(collectable)
into_protocol(enumerable, initial, fun, fn entry, acc ->
fun.(acc, {:cont, transform.(entry)})
end)
end
defp into_protocol(enumerable, initial, fun, callback) do
try do
reduce(enumerable, initial, callback)
reduce_into_protocol(enumerable, initial, transform, fun)
catch
kind, reason ->
fun.(initial, :halt)
@@ -1526,6 +1576,18 @@ defmodule Enum do
end
end
defp reduce_into_protocol(enumerable, initial, transform, fun) when is_list(enumerable) do
:lists.foldl(fn x, acc -> fun.(acc, {:cont, transform.(x)}) end, initial, enumerable)
end
defp reduce_into_protocol(enumerable, initial, transform, fun) do
enumerable
|> Enumerable.reduce({:cont, initial}, fn x, acc ->
{:cont, fun.(acc, {:cont, transform.(x)})}
end)
|> elem(1)
end
@doc """
Joins the given `enumerable` into a string using `joiner` as a
separator.
@@ -1543,6 +1605,9 @@ defmodule Enum do
iex> Enum.join([1, 2, 3], " = ")
"1 = 2 = 3"
iex> Enum.join([["a", "b"], ["c", "d", "e", ["f", "g"]], "h", "i"], " ")
"ab cdefg h i"
"""
@spec join(t, String.t()) :: String.t()
def join(enumerable, joiner \\ "")
@@ -2292,9 +2357,16 @@ defmodule Enum do
{:ok, 0, _} ->
[]
{:ok, count, fun} when is_function(fun) ->
{:ok, count, fun} when is_function(fun, 1) ->
slice_list(fun.(enumerable), random_integer(0, count - 1), 1, 1)
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
fun.(random_integer(0, count - 1), 1)
{:ok, count, fun} when is_function(fun, 3) ->
fun.(random_integer(0, count - 1), 1, 1)
{:error, _} ->
take_random(enumerable, 1)
end
@@ -2573,7 +2645,13 @@ defmodule Enum do
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], -4..-2, 1)
[:a, :d, :e, :f, :b, :c, :g]
# Insert at negative indices (counting from the end)
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 3, -1)
[:a, :b, :c, :e, :f, :g, :d]
"""
@doc since: "1.13.0"
@spec slide(t, Range.t() | index, index) :: list
def slide(enumerable, range_or_single_index, insertion_index)
def slide(enumerable, single_index, insertion_index) when is_integer(single_index) do
@@ -2587,14 +2665,18 @@ defmodule Enum do
end
# Normalize negative input ranges like Enum.slice/2
def slide(enumerable, first..last, insertion_index) when first < 0 or last < 0 do
def slide(enumerable, first..last, insertion_index)
when first < 0 or last < 0 or insertion_index < 0 do
count = Enum.count(enumerable)
normalized_first = if first >= 0, do: first, else: first + count
normalized_first = if first >= 0, do: first, else: Kernel.max(first + count, 0)
normalized_last = if last >= 0, do: last, else: last + count
if normalized_first >= 0 and normalized_first < count and normalized_first != insertion_index do
normalized_insertion_index =
if insertion_index >= 0, do: insertion_index, else: insertion_index + count
if normalized_first < count and normalized_first != normalized_insertion_index do
normalized_range = normalized_first..normalized_last//1
slide(enumerable, normalized_range, insertion_index)
slide(enumerable, normalized_range, normalized_insertion_index)
else
Enum.to_list(enumerable)
end
@@ -2605,11 +2687,16 @@ defmodule Enum do
end
def slide(_, first..last, insertion_index)
when insertion_index > first and insertion_index < last do
raise "Insertion index for slide must be outside the range being moved " <>
when insertion_index > first and insertion_index <= last do
raise ArgumentError,
"insertion index for slide must be outside the range being moved " <>
"(tried to insert #{first}..#{last} at #{insertion_index})"
end
def slide(enumerable, first..last, _insertion_index) when first > last do
Enum.to_list(enumerable)
end
# Guarantees at this point: step size == 1 and first <= last and (insertion_index < first or insertion_index > last)
def slide(enumerable, first..last, insertion_index) do
impl = if is_list(enumerable), do: &slide_list_start/4, else: &slide_any/4
@@ -2658,6 +2745,7 @@ defmodule Enum do
end
defp slide_list_start(list, 0, middle, last), do: slide_list_middle(list, middle, last, [])
defp slide_list_start([], _start, _middle, _last), do: []
defp slide_list_middle([h | t], middle, last, acc) when middle > 0 do
slide_list_middle(t, middle - 1, last - 1, [h | acc])
@@ -2780,31 +2868,49 @@ defmodule Enum do
`enumerable`, or this one is greater than the normalized `index_range.last`,
then `[]` is returned.
If a step `n` (other than `1`) is used in `index_range`, then it takes
every `n`th element from `index_range.first` to `index_range.last`
(according to the same rules described above).
## Examples
iex> Enum.slice(1..100, 5..10)
[6, 7, 8, 9, 10, 11]
iex> Enum.slice([1, 2, 3, 4, 5], 1..3)
[2, 3, 4]
iex> Enum.slice(1..10, 5..20)
[6, 7, 8, 9, 10]
iex> Enum.slice([1, 2, 3, 4, 5], 3..10)
[4, 5]
# last five elements (negative indexes)
iex> Enum.slice(1..30, -5..-1)
[26, 27, 28, 29, 30]
# Last three elements (negative indexes)
iex> Enum.slice([1, 2, 3, 4, 5], -3..-1)
[3, 4, 5]
For ranges where `start > stop`, you need to explicit
mark them as increasing:
iex> Enum.slice(1..30, 25..-1//1)
[26, 27, 28, 29, 30]
iex> Enum.slice([1, 2, 3, 4, 5], 1..-2//1)
[2, 3, 4]
If values are out of bounds, it returns an empty list:
The step can be any positive number. For example, to
get every 2 elements of the collection:
iex> Enum.slice(1..10, 11..20)
iex> Enum.slice([1, 2, 3, 4, 5], 0..-1//2)
[1, 3, 5]
To get every third element of the first ten elements:
iex> integers = Enum.to_list(1..20)
iex> Enum.slice(integers, 0..9//3)
[1, 4, 7, 10]
If the first position is after the end of the enumerable
or after the last position of the range, it returns an
empty list:
iex> Enum.slice([1, 2, 3, 4, 5], 6..10)
[]
# first is greater than last
iex> Enum.slice(1..10, 6..5)
iex> Enum.slice([1, 2, 3, 4, 5], 6..5)
[]
"""
@@ -2812,16 +2918,17 @@ defmodule Enum do
@spec slice(t, Range.t()) :: list
def slice(enumerable, first..last//step = index_range) do
# TODO: Deprecate negative steps on Elixir v1.16
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(enumerable, first, last)
else
raise ArgumentError,
"Enum.slice/2 does not accept ranges with custom steps, got: #{inspect(index_range)}"
# TODO: Support negative steps as a reverse on Elixir v2.0.
cond do
step > 0 ->
slice_range(enumerable, first, last, step)
step == -1 and first > last ->
slice_range(enumerable, first, last, 1)
true ->
raise ArgumentError,
"Enum.slice/2 does not accept ranges with negative steps, got: #{inspect(index_range)}"
end
end
@@ -2831,18 +2938,29 @@ defmodule Enum do
slice(enumerable, Map.put(index_range, :step, step))
end
defp slice_range(enumerable, first, last) when last >= first and last >= 0 and first >= 0 do
slice_any(enumerable, first, last - first + 1)
defp slice_range(enumerable, first, -1, step) when first >= 0 do
if step == 1 do
drop(enumerable, first)
else
enumerable |> drop(first) |> take_every_list(step - 1)
end
end
defp slice_range(enumerable, first, last) do
{count, fun} = slice_count_and_fun(enumerable)
first = if first >= 0, do: first, else: first + count
defp slice_range(enumerable, first, last, step)
when last >= first and last >= 0 and first >= 0 do
slice_forward(enumerable, first, last - first + 1, step)
end
defp slice_range(enumerable, first, last, step) do
{count, fun} = slice_count_and_fun(enumerable, step)
first = if first >= 0, do: first, else: Kernel.max(first + count, 0)
last = if last >= 0, do: last, else: last + count
amount = last - first + 1
if first >= 0 and first < count and amount > 0 do
fun.(first, Kernel.min(amount, count - first))
if first < count and amount > 0 do
amount = Kernel.min(amount, count - first)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(first, amount, step)
else
[]
end
@@ -2877,22 +2995,33 @@ defmodule Enum do
# using a negative start index
iex> Enum.slice(1..10, -6, 3)
[5, 6, 7]
# out of bound start index (positive)
iex> Enum.slice(1..10, 10, 5)
[]
# out of bound start index (negative)
iex> Enum.slice(1..10, -11, 5)
[1, 2, 3, 4, 5]
# out of bound start index
iex> Enum.slice(1..10, 10, 5)
[]
"""
@spec slice(t, index, non_neg_integer) :: list
def slice(_enumerable, start_index, 0) when is_integer(start_index), do: []
def slice(enumerable, start_index, amount)
when is_integer(start_index) and start_index < 0 and is_integer(amount) and amount >= 0 do
{count, fun} = slice_count_and_fun(enumerable, 1)
start_index = Kernel.max(count + start_index, 0)
amount = Kernel.min(amount, count - start_index)
if amount > 0 do
fun.(start_index, amount, 1)
else
[]
end
end
def slice(enumerable, start_index, amount)
when is_integer(start_index) and is_integer(amount) and amount >= 0 do
slice_any(enumerable, start_index, amount)
slice_forward(enumerable, start_index, amount, 1)
end
@doc """
@@ -2941,10 +3070,10 @@ defmodule Enum do
iex> Enum.sort(["some", "kind", "of", "monster"], &(byte_size(&1) < byte_size(&2)))
["of", "kind", "some", "monster"]
## Ascending and descending
## Ascending and descending (since v1.10.0)
`sort/2` allows a developer to pass `:asc` or `:desc` as the sorting
function, which is a convenience for `<=/2` and `>=/2` respectively.
`sort/2` allows a developer to pass `:asc` or `:desc` as the sorter, which is a convenience for
[`&<=/2`](`<=/2`) and [`&>=/2`](`>=/2`) respectively.
iex> Enum.sort([2, 3, 1], :asc)
[1, 2, 3]
@@ -2992,12 +3121,12 @@ defmodule Enum do
t,
(element, element -> boolean) | :asc | :desc | module() | {:asc | :desc, module()}
) :: list
def sort(enumerable, fun) when is_list(enumerable) do
:lists.sort(to_sort_fun(fun), enumerable)
def sort(enumerable, sorter) when is_list(enumerable) do
:lists.sort(to_sort_fun(sorter), enumerable)
end
def sort(enumerable, fun) do
fun = to_sort_fun(fun)
def sort(enumerable, sorter) do
fun = to_sort_fun(sorter)
reduce(enumerable, [], &sort_reducer(&1, &2, fun))
|> sort_terminator(fun)
@@ -3016,17 +3145,27 @@ defmodule Enum do
This function maps each element of the `enumerable` using the
provided `mapper` function. The enumerable is then sorted by
the mapped elements using the `sorter` function, which defaults
to `Kernel.<=/2`.
the mapped elements using the `sorter`, which defaults to `:asc`
and sorts the elements ascendingly.
`sort_by/3` differs from `sort/2` in that it only calculates the
comparison value for each element in the enumerable once instead of
once for each element in each comparison. If the same function is
being called on both elements, it's more efficient to use `sort_by/3`.
## Ascending and descending (since v1.10.0)
`sort_by/3` allows a developer to pass `:asc` or `:desc` as the sorter,
which is a convenience for [`&<=/2`](`<=/2`) and [`&>=/2`](`>=/2`) respectively:
iex> Enum.sort_by([2, 3, 1], &(&1), :asc)
[1, 2, 3]
iex> Enum.sort_by([2, 3, 1], &(&1), :desc)
[3, 2, 1]
## Examples
Using the default `sorter` of `<=/2`:
Using the default `sorter` of `:asc` :
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1)
["of", "some", "kind", "monster"]
@@ -3039,19 +3178,15 @@ defmodule Enum do
Similar to `sort/2`, you can pass a custom sorter:
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1, &>=/2)
["monster", "some", "kind", "of"]
Or use `:asc` and `:desc`:
iex> Enum.sort_by(["some", "kind", "of", "monster"], &byte_size/1, :desc)
["monster", "some", "kind", "of"]
As in `sort/2`, avoid using the default sorting function to sort structs, as by default
it performs structural comparison instead of a semantic one. In such cases,
you shall pass a sorting function as third element or any module that implements
a `compare/2` function. For example, to sort users by their birthday in both
ascending and descending order respectively:
As in `sort/2`, avoid using the default sorting function to sort
structs, as by default it performs structural comparison instead of
a semantic one. In such cases, you shall pass a sorting function as
third element or any module that implements a `compare/2` function.
For example, to sort users by their birthday in both ascending and
descending order respectively:
iex> users = [
...> %{name: "Ellis", birthday: ~D[1943-05-11]},
@@ -3071,6 +3206,42 @@ defmodule Enum do
%{name: "Lovelace", birthday: ~D[1815-12-10]}
]
## Performance characteristics
As detailed in the initial section, `sort_by/3` calculates the comparison
value for each element in the enumerable once instead of once for each
element in each comparison. This implies `sort_by/3` must do an initial
pass on the data to compute those values.
However, if those values are cheap to compute, for example, you have
already extracted the field you want to sort by into a tuple, then those
extra passes become overhead. In such cases, consider using `List.keysort/3`
instead.
Let's see an example. Imagine you have a list of products and you have a
list of IDs. You want to keep all products that are in the given IDs and
return their names sorted by their price. You could write it like this:
for(
product <- products,
product.id in ids,
do: product
)
|> Enum.sort_by(& &1.price)
|> Enum.map(& &1.name)
However, you could also write it like this:
for(
product <- products,
product.id in ids,
do: {product.name, product.price}
)
|> List.keysort(1)
|> Enum.map(&elem(&1, 0))
Using `List.keysort/3` will be a better choice for performance sensitive
code as it avoids additional traversals.
"""
@spec sort_by(
t,
@@ -3079,30 +3250,21 @@ defmodule Enum do
) ::
list
when mapped_element: element
def sort_by(enumerable, mapper, sorter \\ &<=/2) do
def sort_by(enumerable, mapper, sorter \\ :asc)
def sort_by(enumerable, mapper, :desc) when is_function(mapper, 1) do
enumerable
|> map(&{&1, mapper.(&1)})
|> sort(to_sort_by_fun(sorter))
|> map(&elem(&1, 0))
|> Enum.reduce([], &[{&1, mapper.(&1)} | &2])
|> List.keysort(1, :asc)
|> List.foldl([], &[elem(&1, 0) | &2])
end
defp to_sort_by_fun(sorter) when is_function(sorter, 2),
do: &sorter.(elem(&1, 1), elem(&2, 1))
defp to_sort_by_fun(:asc),
do: &(elem(&1, 1) <= elem(&2, 1))
defp to_sort_by_fun(:desc),
do: &(elem(&1, 1) >= elem(&2, 1))
defp to_sort_by_fun(module) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :gt)
defp to_sort_by_fun({:asc, module}) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :gt)
defp to_sort_by_fun({:desc, module}) when is_atom(module),
do: &(module.compare(elem(&1, 1), elem(&2, 1)) != :lt)
def sort_by(enumerable, mapper, sorter) when is_function(mapper, 1) do
enumerable
|> map(&{&1, mapper.(&1)})
|> List.keysort(1, sorter)
|> map(&elem(&1, 0))
end
@doc """
Splits the `enumerable` into two enumerables, leaving `count`
@@ -3294,9 +3456,9 @@ defmodule Enum do
end
def take(enumerable, amount) when is_integer(amount) and amount < 0 do
{count, fun} = slice_count_and_fun(enumerable)
{count, fun} = slice_count_and_fun(enumerable, 1)
first = Kernel.max(amount + count, 0)
fun.(first, count - first)
fun.(first, count - first, 1)
end
@doc """
@@ -3323,9 +3485,12 @@ defmodule Enum do
@spec take_every(t, non_neg_integer) :: list
def take_every(enumerable, nth)
def take_every(enumerable, 1), do: to_list(enumerable)
def take_every(_enumerable, 0), do: []
def take_every([], nth) when is_integer(nth) and nth > 1, do: []
def take_every(enumerable, 1), do: to_list(enumerable)
def take_every(list, nth) when is_list(list) and is_integer(nth) and nth > 1 do
take_every_list(list, nth - 1)
end
def take_every(enumerable, nth) when is_integer(nth) and nth > 1 do
{res, _} = reduce(enumerable, {[], :first}, R.take_every(nth))
@@ -3605,6 +3770,14 @@ defmodule Enum do
@spec with_index(t, (element, index -> value)) :: [value] when value: any
def with_index(enumerable, fun_or_offset \\ 0)
def with_index(enumerable, offset) when is_list(enumerable) and is_integer(offset) do
with_index_list(enumerable, offset)
end
def with_index(enumerable, fun) when is_list(enumerable) and is_function(fun, 2) do
with_index_list(enumerable, 0, fun)
end
def with_index(enumerable, offset) when is_integer(offset) do
enumerable
|> map_reduce(offset, fn x, i -> {{x, i}, i + 1} end)
@@ -3669,7 +3842,7 @@ defmodule Enum do
Zips corresponding elements from two enumerables into a list, transforming them with
the `zip_fun` function as it goes.
The corresponding elements from each collection are passed to the provided 2-arity `zip_fun`
The corresponding elements from each collection are passed to the provided two-arity `zip_fun`
function in turn. Returns a list that contains the result of calling `zip_fun` for each pair of
elements.
@@ -3720,7 +3893,7 @@ defmodule Enum do
into list, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put
into a list which is then passed to the 1-arity `zip_fun` function.
into a list which is then passed to the one-arity `zip_fun` function.
Then, the second elements from each of the enums are put into a list
and passed to `zip_fun`, and so on until any one of the enums in
`enumerables` runs out of elements.
@@ -4184,35 +4357,63 @@ defmodule Enum do
## slice
defp slice_any(enumerable, start, amount) when start < 0 do
{count, fun} = slice_count_and_fun(enumerable)
defp slice_forward(enumerable, start, amount, step) when start < 0 do
{count, fun} = slice_count_and_fun(enumerable, step)
start = count + start
if start >= 0 do
fun.(start, Kernel.min(amount, count - start))
amount = Kernel.min(amount, count - start)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(start, amount, step)
else
[]
end
end
defp slice_any(list, start, amount) when is_list(list) do
list |> drop_list(start) |> take_list(amount)
defp slice_forward(list, start, amount, step) when is_list(list) do
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
slice_list(list, start, amount, step)
end
defp slice_any(enumerable, start, amount) do
defp slice_forward(enumerable, start, amount, step) do
case Enumerable.slice(enumerable) do
{:ok, count, _} when start >= count ->
[]
{:ok, count, fun} when is_function(fun) ->
fun.(start, Kernel.min(amount, count - start))
{:ok, count, fun} when is_function(fun, 1) ->
amount = Kernel.min(amount, count - start)
enumerable |> fun.() |> slice_exact(start, amount, step, count)
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
amount = Kernel.min(amount, count - start)
if step == 1 do
fun.(start, amount)
else
fun.(start, Kernel.min(amount * step, count - start))
|> take_every_list(amount, step - 1)
end
{:ok, count, fun} when is_function(fun, 3) ->
amount = Kernel.min(amount, count - start)
amount = if step == 1, do: amount, else: div(amount - 1, step) + 1
fun.(start, amount, step)
{:error, module} ->
slice_enum(enumerable, module, start, amount)
slice_enum(enumerable, module, start, amount, step)
end
end
defp slice_enum(enumerable, module, start, amount) do
defp slice_list(list, start, amount, step) do
if step == 1 do
list |> drop_list(start) |> take_list(amount)
else
list |> drop_list(start) |> take_every_list(amount, step - 1)
end
end
defp slice_enum(enumerable, module, start, amount, 1) do
{_, {_, _, slice}} =
module.reduce(enumerable, {:cont, {start, amount, []}}, fn
_entry, {start, amount, _list} when start > 0 ->
@@ -4228,26 +4429,86 @@ defmodule Enum do
:lists.reverse(slice)
end
defp slice_count_and_fun(enumerable) when is_list(enumerable) do
length = length(enumerable)
{length, &Enumerable.List.slice(enumerable, &1, &2, length)}
defp slice_enum(enumerable, module, start, amount, step) do
{_, {_, _, _, slice}} =
module.reduce(enumerable, {:cont, {start, amount, 1, []}}, fn
_entry, {start, amount, to_drop, _list} when start > 0 ->
{:cont, {start - 1, amount, to_drop, []}}
entry, {start, amount, to_drop, list} when amount > 1 ->
case to_drop do
1 -> {:cont, {start, amount - 1, step, [entry | list]}}
_ -> {:cont, {start, amount - 1, to_drop - 1, list}}
end
entry, {start, amount, to_drop, list} ->
case to_drop do
1 -> {:halt, {start, amount, to_drop, [entry | list]}}
_ -> {:halt, {start, amount, to_drop, list}}
end
end)
:lists.reverse(slice)
end
defp slice_count_and_fun(enumerable) do
defp slice_count_and_fun(list, _step) when is_list(list) do
length = length(list)
{length, &slice_exact(list, &1, &2, &3, length)}
end
defp slice_count_and_fun(enumerable, step) do
case Enumerable.slice(enumerable) do
{:ok, count, fun} when is_function(fun, 2) ->
{:ok, count, fun} when is_function(fun, 3) ->
{count, fun}
# TODO: Deprecate me in Elixir v1.18.
{:ok, count, fun} when is_function(fun, 2) ->
if step == 1 do
{count, fn start, amount, 1 -> fun.(start, amount) end}
else
{count,
fn start, amount, step ->
fun.(start, Kernel.min(amount * step, count - start))
|> take_every_list(amount, step - 1)
end}
end
{:ok, count, fun} when is_function(fun, 1) ->
{count, &slice_exact(fun.(enumerable), &1, &2, &3, count)}
{:error, module} ->
{_, {list, count}} =
module.reduce(enumerable, {:cont, {[], 0}}, fn elem, {acc, count} ->
{list, count} =
enumerable
|> module.reduce({:cont, {[], 0}}, fn elem, {acc, count} ->
{:cont, {[elem | acc], count + 1}}
end)
|> elem(1)
{count, &Enumerable.List.slice(:lists.reverse(list), &1, &2, count)}
{count,
fn start, amount, step ->
list |> :lists.reverse() |> slice_exact(start, amount, step, count)
end}
end
end
# Slice a list when we know the bounds
defp slice_exact(_list, _start, 0, _step, _), do: []
defp slice_exact(list, start, amount, 1, size) when start + amount == size,
do: list |> drop_exact(start)
defp slice_exact(list, start, amount, 1, _),
do: list |> drop_exact(start) |> take_exact(amount)
defp slice_exact(list, start, amount, step, _),
do: list |> drop_exact(start) |> take_every_list(amount, step - 1)
defp drop_exact(list, 0), do: list
defp drop_exact([_ | tail], amount), do: drop_exact(tail, amount - 1)
defp take_exact(_list, 0), do: []
defp take_exact([head | tail], amount), do: [head | take_exact(tail, amount - 1)]
## sort
defp sort_reducer(entry, {:split, y, x, r, rs, bool}, fun) do
@@ -4392,10 +4653,22 @@ defmodule Enum do
## take
defp take_list([head | _], 1), do: [head]
defp take_list(_list, 0), do: []
defp take_list([head | tail], counter), do: [head | take_list(tail, counter - 1)]
defp take_list([], _counter), do: []
defp take_every_list([head | tail], to_drop),
do: [head | tail |> drop_list(to_drop) |> take_every_list(to_drop)]
defp take_every_list([], _to_drop), do: []
defp take_every_list(_list, 0, _to_drop), do: []
defp take_every_list([head | tail], counter, to_drop),
do: [head | tail |> drop_list(to_drop) |> take_every_list(counter - 1, to_drop)]
defp take_every_list([], _counter, _to_drop), do: []
## take_while
defp take_while_list([head | tail], fun) do
@@ -4425,6 +4698,20 @@ defmodule Enum do
[]
end
## with_index
defp with_index_list([head | tail], offset) do
[{head, offset} | with_index_list(tail, offset + 1)]
end
defp with_index_list([], _offset), do: []
defp with_index_list([head | tail], offset, fun) do
[fun.(head, offset) | with_index_list(tail, offset + 1, fun)]
end
defp with_index_list([], _offset, _fun), do: []
## zip
defp zip_list([head1 | next1], [head2 | next2], acc) do
@@ -4450,30 +4737,18 @@ defmodule Enum do
end
defimpl Enumerable, for: List do
def count([]), do: {:ok, 0}
def count(_list), do: {:error, __MODULE__}
def count(list), do: {:ok, length(list)}
def member?([], _value), do: {:ok, false}
def member?(_list, _value), do: {:error, __MODULE__}
def slice([]), do: {:ok, 0, fn _, _ -> [] end}
def slice([]), do: {:ok, 0, fn _, _, _ -> [] end}
def slice(_list), do: {:error, __MODULE__}
def reduce(_list, {:halt, acc}, _fun), do: {:halted, acc}
def reduce(list, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(list, &1, fun)}
def reduce([], {:cont, acc}, _fun), do: {:done, acc}
def reduce([head | tail], {:cont, acc}, fun), do: reduce(tail, fun.(head, acc), fun)
@doc false
def slice(_list, _start, 0, _size), do: []
def slice(list, start, count, size) when start + count == size, do: list |> drop(start)
def slice(list, start, count, _size), do: list |> drop(start) |> take(count)
defp drop(list, 0), do: list
defp drop([_ | tail], count), do: drop(tail, count - 1)
defp take(_list, 0), do: []
defp take([head | tail], count), do: [head | take(tail, count - 1)]
end
defimpl Enumerable, for: Map do
@@ -4491,7 +4766,7 @@ defimpl Enumerable, for: Map do
def slice(map) do
size = map_size(map)
{:ok, size, &Enumerable.List.slice(:maps.to_list(map), &1, &2, size)}
{:ok, size, &:maps.to_list/1}
end
def reduce(map, acc, fun) do
+230 -68
View File
@@ -227,7 +227,11 @@ defmodule Exception do
|> Enum.zip()
|> Enum.map_reduce([], &blame_arg/2)
guards = Enum.map(guards, &blame_guard(&1, ann, scope, binding))
guards =
guards
|> Enum.map(&blame_guard(&1, ann, scope, binding))
|> Enum.map(&Macro.prewalk(&1, fn guard -> translate_guard(guard) end))
{args, guards}
end
@@ -237,6 +241,71 @@ defmodule Exception do
end
end
defp is_map_node?({:is_map, _, [_]}), do: true
defp is_map_node?(_), do: false
defp is_map_key_node?({:is_map_key, _, [_, _]}), do: true
defp is_map_key_node?(_), do: false
defp struct_validation_node?(
{:is_atom, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}]}
),
do: true
defp struct_validation_node?(
{:==, _, [{{:., [], [:erlang, :map_get]}, _, [:__struct__, _]}, _module]}
),
do: true
defp struct_validation_node?(_), do: false
defp is_struct_macro?(
{:and, _,
[
{:and, _, [%{node: node_1 = {_, _, [arg]}}, %{node: node_2 = {_, _, [arg, _]}}]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(
{:and, _,
[
{:and, _,
[
{:and, _,
[
%{node: node_1 = {_, _, [arg]}},
{:or, _, [%{node: {:is_atom, _, [_]}}, %{node: :fail}]}
]},
%{node: node_2 = {_, _, [arg, _]}}
]},
%{node: node_3 = {_, _, [{_, _, [_, arg]}, _]}}
]}
),
do: is_map_node?(node_1) and is_map_key_node?(node_2) and struct_validation_node?(node_3)
defp is_struct_macro?(_), do: false
defp translate_guard(guard) do
if is_struct_macro?(guard) do
undo_is_struct_guard(guard)
else
guard
end
end
defp undo_is_struct_guard(
{:and, meta, [_, %{node: {_, _, [{_, _, [_, {struct, _, _}]} | optional]}}]}
) do
args =
case optional do
[] -> [{struct, meta, nil}]
[module] -> [{struct, meta, nil}, module]
end
%{match?: meta[:value], node: {:is_struct, meta, args}}
end
defp blame_arg({call_arg, ex_arg, erl_arg}, binding) do
{match?, binding} = blame_arg(erl_arg, call_arg, binding)
{blame_wrap(match?, rewrite_arg(ex_arg)), binding}
@@ -280,23 +349,38 @@ defmodule Exception do
:andalso -> :and
end
{kernel_op, meta, guards}
evaluate_guard(kernel_op, meta, guards)
end
defp blame_guard(ex_guard, ann, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
ex_guard
|> blame_guard?(binding, ann, scope)
|> blame_wrap(rewrite_guard(ex_guard))
end
match? =
try do
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
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
end
blame_wrap(match?, rewrite_guard(ex_guard))
{kernel_op, Keyword.put(meta, :value, logic_value), guards}
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)
@@ -622,12 +706,12 @@ defmodule Exception do
case Code.Identifier.extract_anonymous_fun_parent(fun) do
{outer_name, outer_arity} ->
"anonymous fn#{format_arity(arity)} in " <>
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(outer_name)}/#{outer_arity}"
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, outer_name)}/#{outer_arity}"
:error ->
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(fun)}#{format_arity(arity)}"
"#{Macro.inspect_atom(:literal, module)}." <>
"#{Macro.inspect_atom(:remote_call, fun)}#{format_arity(arity)}"
end
end
@@ -732,12 +816,16 @@ defmodule ArgumentError do
) do
message =
cond do
not proper_list?(args) ->
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
# Note that args may be an empty list even if they were supplied
not is_atom(module) and is_atom(function) and args == [] ->
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
"If you are using Kernel.apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as map.field or module.function(), " <>
"make sure the left side of the dot is an atom or a map"
"If you are using the dot syntax, such as module.function(), " <>
"make sure the left-hand side of the dot is a module atom"
not is_atom(module) ->
"you attempted to apply a function on #{inspect(module)}. " <>
@@ -747,10 +835,6 @@ defmodule ArgumentError do
"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"
not is_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 list"
end
{%{exception | message: message}, stacktrace}
@@ -759,6 +843,9 @@ 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
@@ -1027,8 +1114,8 @@ defmodule UndefinedFunctionError do
end
defp hint(nil, _function, 0, _loaded?) do
". 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"
". If you are using the dot syntax, such as module.function(), " <>
"make sure the left-hand side of the dot is a module atom"
end
defp hint(module, function, arity, true) do
@@ -1093,7 +1180,7 @@ defmodule UndefinedFunctionError do
end
defp format_fa({_dist, fun, arity}) do
[" * ", Code.Identifier.inspect_as_function(fun), ?/, Integer.to_string(arity), ?\n]
[" * ", Macro.inspect_atom(:remote_call, fun), ?/, Integer.to_string(arity), ?\n]
end
defp behaviour_hint(module, function, arity) do
@@ -1251,11 +1338,13 @@ defmodule FunctionClauseError do
end
defmodule Code.LoadError do
defexception [:file, :message]
defexception [:file, :message, :reason]
def exception(opts) do
file = Keyword.fetch!(opts, :file)
%Code.LoadError{message: "could not load #{file}", file: file}
reason = Keyword.fetch!(opts, :reason)
message = "could not load #{file}. Reason: #{reason}"
%Code.LoadError{message: message, file: file, reason: reason}
end
end
@@ -1470,22 +1559,24 @@ defmodule File.LinkError do
end
defmodule ErlangError do
defexception [:original]
defexception [:original, :reason]
@impl true
def message(exception) do
"Erlang error: #{inspect(exception.original)}"
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])
end
@doc false
def normalize(:badarg, stacktrace) do
case error_info(:badarg, stacktrace) do
{:ok, args} ->
message = "errors were found at the given arguments:\n\n#{args}"
%ArgumentError{message: message}
:error ->
%ArgumentError{}
case error_info(:badarg, stacktrace, "errors were found at the given arguments") do
{:ok, reason, details} -> %ArgumentError{message: reason <> details}
:error -> %ArgumentError{}
end
end
@@ -1494,15 +1585,11 @@ defmodule ErlangError do
end
def normalize(:system_limit, stacktrace) do
case error_info(:system_limit, stacktrace) do
{:ok, args} ->
message =
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
default_reason = "a system limit has been reached due to errors at the given arguments"
%SystemLimitError{message: message}
:error ->
%SystemLimitError{}
case error_info(:system_limit, stacktrace, default_reason) do
{:ok, reason, details} -> %SystemLimitError{message: reason <> details}
:error -> %SystemLimitError{}
end
end
@@ -1548,10 +1635,19 @@ defmodule ErlangError do
%KeyError{key: key, term: term}
end
def normalize({:badkey, key, map}, _stacktrace) do
def normalize({:badkey, key, map}, _stacktrace) when is_map(map) 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
@@ -1578,8 +1674,11 @@ defmodule ErlangError do
%ArgumentError{message: "argument error: #{inspect(payload)}"}
end
def normalize(other, _stacktrace) do
%ErlangError{original: other}
def normalize(other, stacktrace) do
case error_info(other, stacktrace, "") do
{:ok, _reason, details} -> %ErlangError{original: other, reason: details}
:error -> %ErlangError{original: other}
end
end
defp from_stacktrace([{module, function, args, _} | _]) when is_list(args) do
@@ -1594,30 +1693,35 @@ defmodule ErlangError do
{nil, nil, nil}
end
defp error_info(:badarg, [{:erlang, fun, _, _} | _]) when fun in [:byte_size, :bit_size] do
{:ok,
"""
* 1st argument: not a bitstring
This typically happens when calling Kernel.#{fun}/1 with an invalid argument \
or when performing binary construction or binary concatenation with <> and \
one of the arguments is not a binary\
"""}
end
defp error_info(erl_exception, stacktrace) do
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
defp error_info(erl_exception, stacktrace, default_reason) do
with [{module, fun, args_or_arity, opts} | tail] <- stacktrace,
%{} = error_info <- opts[:error_info] do
module = Map.get(error_info, :module, module)
function = Map.get(error_info, :function, :format_error)
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
extra = apply(module, function, [erl_exception, stacktrace])
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
error_module = Map.get(error_info, :module, module)
error_fun = Map.get(error_info, :function, :format_error)
if map_size(args_errors) > 0 do
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
else
: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
@@ -1631,3 +1735,61 @@ defmodule ErlangError do
defp nth(3), do: "3rd"
defp nth(n), do: "#{n}th"
end
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
@enforce_keys [:exception_module, :exception_message, :stacktrace, :inspected_struct]
defexception @enforce_keys
@impl true
def exception(arguments) when is_list(arguments) do
exception = Keyword.fetch!(arguments, :exception)
exception_module = exception.__struct__
exception_message = Exception.message(exception) |> String.trim_trailing("\n")
stacktrace = Keyword.fetch!(arguments, :stacktrace)
inspected_struct = Keyword.fetch!(arguments, :inspected_struct)
%Inspect.Error{
exception_module: exception_module,
exception_message: exception_message,
stacktrace: stacktrace,
inspected_struct: inspected_struct
}
end
@impl true
def message(%__MODULE__{
exception_module: exception_module,
exception_message: exception_message,
inspected_struct: inspected_struct
}) do
~s'''
got #{inspect(exception_module)} with message:
"""
#{pad(exception_message, 4)}
"""
while inspecting:
#{pad(inspected_struct, 4)}
'''
end
@doc false
def pad(message, padding_length)
when is_binary(message) and is_integer(padding_length) and padding_length >= 0 do
padding = String.duplicate(" ", padding_length)
message
|> String.split("\n")
|> Enum.map(fn
"" -> "\n"
line -> [padding, line, ?\n]
end)
|> IO.iodata_to_binary()
|> String.trim_trailing("\n")
end
end
+134 -86
View File
@@ -112,6 +112,7 @@ defmodule File do
encoding_mode()
| :append
| :compressed
| :delayed_write
| :trim_bom
| {:read_ahead, pos_integer | false}
| {:delayed_write, non_neg_integer, non_neg_integer}
@@ -122,6 +123,8 @@ 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.
@@ -693,7 +696,10 @@ 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
:file.copy(maybe_to_string(source), maybe_to_string(destination), bytes_count)
source = normalize_path_or_io_device(source)
destination = normalize_path_or_io_device(destination)
:file.copy(source, destination, bytes_count)
end
@doc """
@@ -711,8 +717,8 @@ defmodule File do
raise File.CopyError,
reason: reason,
action: "copy",
source: maybe_to_string(source),
destination: maybe_to_string(destination)
source: normalize_path_or_io_device(source),
destination: normalize_path_or_io_device(destination)
end
end
@@ -740,6 +746,8 @@ 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
@@ -770,11 +778,6 @@ defmodule File do
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
will be returned.
The `callback` function is invoked if the `destination_file` already exists.
The function receives arguments for `source_file` and `destination_file`;
it should return `true` if the existing file should be overwritten, `false` if
otherwise. The default callback returns `true`.
The function returns `:ok` in case of success. Otherwise, it returns
`{:error, reason}`.
@@ -786,13 +789,30 @@ 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(), (Path.t(), Path.t() -> boolean)) :: :ok | {:error, posix}
def cp(source_file, destination_file, callback \\ fn _, _ -> true end) do
@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)
source_file = IO.chardata_to_string(source_file)
destination_file = IO.chardata_to_string(destination_file)
case do_cp_file(source_file, destination_file, callback, []) do
case do_cp_file(source_file, destination_file, on_conflict, []) do
{:error, reason, _} -> {:error, reason}
_ -> :ok
end
@@ -808,9 +828,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(), (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
@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
:ok ->
:ok
@@ -833,18 +853,15 @@ 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 `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.
If a file already exists in the destination, it invokes the optional `on_conflict`
callback given as an option. See "Options" for more information.
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
@@ -855,6 +872,19 @@ 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"
@@ -864,14 +894,28 @@ 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", fn source, destination ->
File.cp_r("samples", "tmp", on_conflict: fn source, destination ->
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm. ") == "y\n"
end)
"""
@spec cp_r(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) ::
@spec cp_r(Path.t(), Path.t(),
on_conflict: on_conflict_callback,
dereference_symlinks: boolean()
) ::
{:ok, [binary]} | {:error, posix, binary}
def cp_r(source, destination, callback \\ fn _, _ -> true end) when is_function(callback, 2) do
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)
source =
source
|> IO.chardata_to_string()
@@ -882,7 +926,7 @@ defmodule File do
|> IO.chardata_to_string()
|> assert_no_null_byte!("File.cp_r/3")
case do_cp_r(source, destination, callback, []) do
case do_cp_r(source, destination, on_conflict, dereference?, []) do
{:error, _, _} = error -> error
res -> {:ok, res}
end
@@ -892,9 +936,12 @@ 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(), (Path.t(), Path.t() -> boolean)) :: [binary]
def cp_r!(source, destination, callback \\ fn _, _ -> true end) do
case cp_r(source, destination, callback) do
@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
{:ok, files} ->
files
@@ -908,15 +955,21 @@ defmodule File do
end
end
defp do_cp_r(src, dest, callback, acc) when is_list(acc) do
defp do_cp_r(src, dest, on_conflict, dereference?, acc) when is_list(acc) do
case :elixir_utils.read_link_type(src) do
{:ok, :regular} ->
do_cp_file(src, dest, callback, acc)
do_cp_file(src, dest, on_conflict, acc)
{:ok, :symlink} ->
case :file.read_link(src) do
{:ok, link} -> do_cp_link(link, src, dest, callback, acc)
{:error, reason} -> {:error, reason, src}
{: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}
end
{:ok, :directory} ->
@@ -925,7 +978,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), callback, acc)
do_cp_r(Path.join(src, x), Path.join(dest, x), on_conflict, dereference?, acc)
end)
{:error, reason} ->
@@ -944,9 +997,8 @@ 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
@@ -955,14 +1007,14 @@ defmodule File do
end
# Both src and dest are files.
defp do_cp_file(src, dest, callback, acc) do
defp do_cp_file(src, dest, on_conflict, 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 callback.(src, dest) do
if path_differs?(src, dest) and on_conflict.(src, dest) do
case copy(src, dest) do
{:ok, _} ->
copy_file_mode!(src, dest)
@@ -981,13 +1033,13 @@ defmodule File do
end
# Both src and dest are files.
defp do_cp_link(link, src, dest, callback, acc) do
defp do_cp_link(link, src, dest, on_conflict, acc) do
case :file.make_symlink(link, dest) do
:ok ->
[dest | acc]
{:error, :eexist} ->
if path_differs?(src, dest) and callback.(src, dest) do
if path_differs?(src, dest) and on_conflict.(src, dest) do
# If rm/1 fails, :file.make_symlink/2 will fail
_ = rm(dest)
@@ -1044,9 +1096,7 @@ defmodule File do
"""
@spec write!(Path.t(), iodata, [mode]) :: :ok
def write!(path, content, modes \\ []) do
modes = normalize_modes(modes, false)
case :file.write_file(path, content, modes) do
case write(path, content, modes) do
:ok ->
:ok
@@ -1194,82 +1244,79 @@ 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({:ok, []})
|> do_rm_rf([], major)
end
defp do_rm_rf(path, {:ok, _} = entry) do
case safe_list_dir(path) do
defp do_rm_rf(path, acc, major) do
case safe_list_dir(path, major) do
{:ok, files} when is_list(files) ->
res =
Enum.reduce(files, entry, fn file, tuple ->
do_rm_rf(Path.join(path, file), tuple)
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
end)
case res do
{:ok, acc} ->
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> res
{:error, reason} -> {:error, reason, path}
end
reason ->
reason
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> {:ok, acc}
{:error, reason} -> {:error, reason, path}
end
{:ok, :directory} ->
do_rm_directory(path, entry)
do_rm_directory(path, acc)
{:ok, :regular} ->
do_rm_regular(path, entry)
do_rm_regular(path, acc)
{:error, reason} when reason in [:enoent, :enotdir] ->
entry
{:ok, acc}
{:error, reason} ->
{:error, reason, path}
end
end
defp do_rm_rf(_, reason) do
reason
end
defp do_rm_regular(path, {:ok, acc} = entry) do
defp do_rm_regular(path, acc) do
case rm(path) do
:ok -> {:ok, [path | acc]}
{:error, :enoent} -> entry
{:error, :enoent} -> {:ok, acc}
{: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, {:ok, acc} = entry) 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, acc) do
case rmdir(path) do
:ok -> {:ok, [path | acc]}
{:error, :enotdir} -> do_rm_regular(path, entry)
{:error, :enoent} -> entry
{:error, :enotdir} -> do_rm_regular(path, acc)
{:error, :enoent} -> {:ok, acc}
{:error, reason} -> {:error, reason, path}
end
end
defp safe_list_dir(path) do
defp safe_list_dir(path, major) do
case :elixir_utils.read_link_type(path) do
{:ok, :symlink} ->
{:ok, :directory} ->
:file.list_dir_all(path)
{:ok, :symlink} when major == :win32 ->
case :elixir_utils.read_file_type(path) do
{:ok, :directory} -> {:ok, :directory}
_ -> {:ok, :regular}
end
{:ok, :directory} ->
:file.list_dir(path)
{:ok, _} ->
{:ok, :regular}
@@ -1607,7 +1654,7 @@ defmodule File do
:file.close(io_device)
end
@doc """
@doc ~S"""
Returns a `File.Stream` for the given `path` with the given `modes`.
The stream implements both `Enumerable` and `Collectable` protocols,
@@ -1805,7 +1852,8 @@ defmodule File do
defp normalize_modes([], true), do: [:binary]
defp normalize_modes([], false), do: []
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
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
end
+50 -11
View File
@@ -15,7 +15,7 @@ defmodule Float do
## Known issues
There are some very well known problems with floating-point numbers
and arithmetics due to the fact most decimal fractions cannot be
and arithmetic 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,6 +46,31 @@ 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`.
@@ -509,13 +534,20 @@ defmodule Float do
defp sign(1, num), do: -num
@doc """
Returns a charlist which corresponds to the text representation
Returns a charlist which corresponds to the shortest text representation
of the given float.
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.
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`.
## Examples
@@ -529,13 +561,20 @@ defmodule Float do
end
@doc """
Returns a binary which corresponds to the text representation
Returns a binary which corresponds to the shortest text representation
of the given float.
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.
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`.
## 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 `Kernel.SpecialForms.&/1`:
operator `&/1`:
iex> add = &Kernel.+/2
iex> add.(1, 2)
+2 -2
View File
@@ -2,7 +2,7 @@ defmodule GenEvent do
# Functions from this module are deprecated in elixir_dispatch.
@moduledoc """
A event manager with event handlers behaviour.
An event manager with event handlers behaviour.
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
@@ -91,7 +91,7 @@ defmodule GenEvent do
deprecation_message =
"the GenEvent module is deprecated, see its documentation for alternatives"
IO.warn(deprecation_message, Macro.Env.stacktrace(__CALLER__))
IO.warn(deprecation_message, __CALLER__)
quote location: :keep do
@behaviour :gen_event
+38 -33
View File
@@ -153,6 +153,11 @@ 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
@@ -207,7 +212,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 `Kernel.send/2`,
and `cast/2`, "regular" messages sent by functions such as `send/2`,
`Process.send_after/4` and similar, can be handled inside the `c:handle_info/2`
callback.
@@ -317,7 +322,7 @@ defmodule GenServer do
## Debugging with the :sys module
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
GenServers, as [special processes](https://www.erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](`:sys`).
Through various hooks, this module allows developers to introspect the state of
the process and trace system events that happen during its execution, such as
@@ -407,7 +412,7 @@ defmodule GenServer do
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [`:gen_server` module documentation](`:gen_server`)
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.html)
* [gen_server Behaviour - OTP Design Principles](https://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)
"""
@@ -429,10 +434,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}}` is similar to
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 the value
`continue` as first argument.
the `c:handle_continue/2` callback will be invoked with `continue_arg`
as the first argument and `state` as the second one.
Returning `:ignore` will cause `start_link/3` to return `:ignore` and
the process will exit normally without entering the loop or calling
@@ -455,7 +460,7 @@ defmodule GenServer do
"""
@callback init(init_arg :: term) ::
{:ok, state}
| {:ok, state, timeout | :hibernate | {:continue, term}}
| {:ok, state, timeout | :hibernate | {:continue, continue_arg :: term}}
| :ignore
| {:stop, reason :: any}
when state: any
@@ -482,9 +487,10 @@ defmodule GenServer do
`GenServer` causes garbage collection and leaves a continuous heap that
minimises the memory used by the process.
Returning `{:reply, reply, new_state, {:continue, continue}}` is similar to
`{:reply, reply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with the value `continue` as first argument.
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.
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
@@ -507,7 +513,7 @@ defmodule GenServer do
process exits without replying as the caller will be blocking awaiting a
reply.
Returning `{:noreply, new_state, timeout | :hibernate | {:continue, continue}}`
Returning `{:noreply, new_state, timeout | :hibernate | {:continue, continue_arg}}`
is similar to `{:noreply, new_state}` except a timeout, hibernation or continue
occurs as with a `:reply` tuple.
@@ -523,9 +529,10 @@ defmodule GenServer do
"""
@callback handle_call(request :: term, from, state :: term) ::
{:reply, reply, new_state}
| {:reply, reply, new_state, timeout | :hibernate | {:continue, term}}
| {:reply, reply, new_state,
timeout | :hibernate | {:continue, continue_arg :: term}}
| {:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:stop, reason, reply, new_state}
| {:stop, reason, new_state}
when reply: term, new_state: term, reason: term
@@ -546,9 +553,10 @@ 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}}` is similar to
Returning `{:noreply, new_state, {:continue, continue_arg}}` is similar to
`{:noreply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with the value `continue` as first argument.
immediately after with `continue_arg` as the first argument and
`state` as the second one.
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
@@ -559,7 +567,7 @@ defmodule GenServer do
"""
@callback handle_cast(request :: term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: term}}
| {:stop, reason :: term, new_state}
when new_state: term
@@ -576,12 +584,12 @@ defmodule GenServer do
"""
@callback handle_info(msg :: :timeout | term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg :: 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.
@@ -591,11 +599,11 @@ defmodule GenServer do
This callback is optional. If one is not implemented, the server will fail
if a continue instruction is used.
"""
@callback handle_continue(continue :: term, state :: term) ::
@callback handle_continue(continue_arg, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate | {:continue, continue_arg}}
| {:stop, reason :: term, new_state}
when new_state: term
when new_state: term, continue_arg: term
@doc """
Invoked when the server is about to exit. It should do any cleanup required.
@@ -608,7 +616,7 @@ defmodule GenServer do
does one of the following:
* returns a `:stop` tuple
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
* raises (via `raise/2`) or exits (via `exit/1`)
* returns an invalid value
If part of a supervision tree, a `GenServer` will receive an exit
@@ -675,11 +683,7 @@ defmodule GenServer do
when old_vsn: term | {:down, term}
@doc """
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.
Invoked in some cases to retrieve a formatted version of the `GenServer` status:
* 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`
@@ -687,6 +691,10 @@ 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`.
@@ -858,7 +866,7 @@ defmodule GenServer do
the arguments given to GenServer.start_link/3 to the server state.
"""
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
quote do
@doc false
@@ -1166,11 +1174,8 @@ defmodule GenServer do
"""
@spec reply(from, term) :: :ok
def reply(client, reply)
def reply({to, tag}, reply) when is_pid(to) do
send(to, {tag, reply})
:ok
def reply(client, reply) do
:gen.reply(client, reply)
end
@doc """
+224 -75
View File
@@ -8,16 +8,85 @@ 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`.
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`.
## Inspect representation
## Examples
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`.
Many times, inspecting a structure can be implemented in function
of existing entities. For example, here is `MapSet`'s `inspect/2`
@@ -27,23 +96,24 @@ defprotocol Inspect do
import Inspect.Algebra
def inspect(map_set, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
concat(["MapSet.new(", Inspect.List.inspect(MapSet.to_list(map_set), opts), ")"])
end
end
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from
`Inspect.Algebra` and it concatenates algebra documents together.
In the example above it is concatenating the string `"#MapSet<"`,
In the example above it is concatenating the string `"MapSet.new("`,
the document returned by `Inspect.Algebra.to_doc/2`, and the final
string `">"`. We prefix the module name `#` to denote the inspect
presentation is not actually valid Elixir syntax.
string `")"`. Therefore, the MapSet with the numbers 1, 2, and 3
will be printed as:
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.
iex> MapSet.new([1, 2, 3], fn x -> x * 2 end)
MapSet.new([2, 4, 6])
## Error handling
In other words, `MapSet`'s inspect representation returns an expression
that, when evaluated, builds the `MapSet` itself.
### 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
@@ -55,24 +125,6 @@ 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
@@ -94,7 +146,7 @@ defimpl Inspect, for: Atom do
require Macro
def inspect(atom, opts) do
color(Identifier.inspect_as_atom(atom), color_key(atom), opts)
color(Macro.inspect_atom(:literal, atom), color_key(atom), opts)
end
defp color_key(atom) when is_boolean(atom), do: :boolean
@@ -210,7 +262,7 @@ defimpl Inspect, for: List do
{escaped, _} -> [?', escaped, ?', " ++ ..."]
end
IO.iodata_to_binary(inspected)
color(IO.iodata_to_binary(inspected), :charlist, opts)
keyword?(term) ->
container_doc(open, term, close, opts, &keyword/2, separator: sep, break: :strict)
@@ -222,7 +274,7 @@ defimpl Inspect, for: List do
@doc false
def keyword({key, value}, opts) do
key = color(Identifier.inspect_as_key(key), :atom, opts)
key = color(Macro.inspect_atom(:key, key), :atom, opts)
concat(key, concat(" ", to_doc(value, opts)))
end
@@ -250,28 +302,33 @@ end
defimpl Inspect, for: Map do
def inspect(map, opts) do
inspect(map, "", opts)
list = Map.to_list(map)
fun =
if Inspect.List.keyword?(list) do
&Inspect.List.keyword/2
else
sep = color(" => ", :map, opts)
&to_assoc(&1, &2, sep)
end
map_container_doc(list, "", opts, fun)
end
def inspect(map, name, opts) do
map = Map.to_list(map)
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
open = color("%" <> name <> "{", :map, opts)
sep = color(",", :map, opts)
close = color("}", :map, opts)
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))
container_doc(open, list, close, opts, fun, separator: sep, break: :strict)
end
end
@@ -309,13 +366,31 @@ defimpl Inspect, for: Integer do
end
defimpl Inspect, for: Float do
def inspect(term, opts) do
inspected = IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
color(inspected, :number, opts)
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)
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
@@ -347,9 +422,12 @@ 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 = Identifier.inspect_as_atom(mod)
inspected_as_function = Identifier.inspect_as_function(name)
inspected_as_atom = Macro.inspect_atom(:literal, mod)
inspected_as_function = Macro.inspect_atom(:remote_call, name)
"&#{inspected_as_atom}.#{inspected_as_function}/#{fun_info[:arity]}"
match?('elixir_compiler_' ++ _, Atom.to_charlist(mod)) ->
@@ -365,7 +443,7 @@ defimpl Inspect, for: Function do
end
defp default_inspect(mod, fun_info) do
inspected_as_atom = Identifier.inspect_as_atom(mod)
inspected_as_atom = Macro.inspect_atom(:literal, mod)
extracted_name = extract_name(fun_info[:name])
"#Function<#{uniq(fun_info)}/#{fun_info[:arity]} in #{inspected_as_atom}#{extracted_name}>"
end
@@ -377,10 +455,10 @@ defimpl Inspect, for: Function do
defp extract_name(name) do
case Identifier.extract_anonymous_fun_parent(name) do
{name, arity} ->
"." <> Identifier.inspect_as_function(name) <> "/" <> arity
"." <> Macro.inspect_atom(:remote_call, name) <> "/" <> arity
:error ->
"." <> Identifier.inspect_as_function(name)
"." <> Macro.inspect_atom(:remote_call, name)
end
end
@@ -389,6 +467,36 @@ 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))
@@ -413,11 +521,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, [])
filtered_fields =
fields
|> Enum.reject(&(&1 in except))
|> Enum.filter(&(&1 in only))
:ok = validate_option(:only, only, fields, module)
:ok = validate_option(:except, except, fields, module)
:ok = validate_option(:optional, optional, fields, module)
inspect_module =
if fields == only and except == [] do
@@ -426,45 +534,86 @@ 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!(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))
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))
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.__struct__(), module.__info__(:struct)}
rescue
_ -> Inspect.Map.inspect(struct, opts)
else
dunder ->
{dunder, fields} ->
if Map.keys(dunder) == Map.keys(struct) do
pruned = Map.drop(struct, [:__struct__, :__exception__])
Inspect.Map.inspect(pruned, Identifier.inspect_as_atom(module), opts)
infos =
for %{field: field} = info <- fields,
field not in [:__struct__, :__exception__],
do: info
Inspect.Map.inspect(struct, Macro.inspect_atom(:literal, module), infos, opts)
else
Inspect.Map.inspect(struct, opts)
end
end
end
def inspect(map, name, opts) do
map = Map.to_list(map) ++ [:...]
def inspect(map, name, infos, opts) do
open = color("#" <> name <> "<", :map, opts)
sep = color(",", :map, opts)
close = color(">", :map, opts)
fun = fn
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
%{field: field}, opts -> Inspect.List.keyword({field, Map.get(map, field)}, opts)
:..., _opts -> "..."
end
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
container_doc(open, infos ++ [:...], close, opts, fun, separator: sep, break: :strict)
end
end
+53 -32
View File
@@ -53,7 +53,7 @@ defmodule Inspect.Opts do
* `:safe` - when `false`, failures while inspecting structs will be raised
as errors instead of being wrapped in the `Inspect.Error` exception. This
is useful when debugging failures and crashes for custom inspect
implementations.
implementations. Defaults to `true`.
* `:structs` - when `false`, structs are not formatted by the inspect
protocol, they are instead printed as maps. Defaults to `true`.
@@ -64,6 +64,7 @@ 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
@@ -89,11 +90,9 @@ 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()),
@@ -163,13 +162,6 @@ defmodule Inspect.Opts do
end
end
defmodule Inspect.Error do
@moduledoc """
Raised when a struct cannot be inspected.
"""
defexception [:message]
end
defmodule Inspect.Algebra do
@moduledoc ~S"""
A set of functions for creating and manipulating algebra
@@ -262,12 +254,18 @@ 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)}
@@ -317,7 +315,8 @@ defmodule Inspect.Algebra do
:doc_force,
:doc_group,
:doc_nest,
:doc_string
:doc_string,
:doc_limit
]
defguard is_doc(doc)
@@ -354,28 +353,28 @@ defmodule Inspect.Algebra do
try do
Process.put(:inspect_trap, true)
res =
Inspect.Map.inspect(struct, %{
inspected_struct =
struct
|> Inspect.Map.inspect(%{
opts
| syntax_colors: [],
inspect_fun: Inspect.Opts.default_inspect_fun()
})
|> format(opts.width)
|> IO.iodata_to_binary()
res = IO.iodata_to_binary(format(res, :infinity))
message =
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(Exception.message(caught_exception))} while inspecting #{res}"
exception = Inspect.Error.exception(message: message)
inspect_error =
Inspect.Error.exception(
exception: caught_exception,
stacktrace: __STACKTRACE__,
inspected_struct: inspected_struct
)
if opts.safe do
Inspect.inspect(exception, %{
opts
| inspect_fun: Inspect.Opts.default_inspect_fun()
})
opts = %{opts | inspect_fun: Inspect.Opts.default_inspect_fun()}
Inspect.inspect(inspect_error, opts)
else
reraise(exception, __STACKTRACE__)
reraise(inspect_error, __STACKTRACE__)
end
after
Process.delete(:inspect_trap)
@@ -469,8 +468,9 @@ defmodule Inspect.Algebra do
defp container_each([term | terms], limit, opts, fun, acc, simple?)
when is_list(terms) and is_limit(limit) do
limit = decrement(limit)
doc = fun.(term, %{opts | limit: limit})
new_limit = decrement(limit)
doc = fun.(term, %{opts | limit: new_limit})
limit = if doc == :doc_nil, do: limit, else: new_limit
container_each(terms, limit, opts, fun, [doc | acc], simple? and simple?(doc))
end
@@ -582,6 +582,10 @@ 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.
@@ -631,7 +635,7 @@ defmodule Inspect.Algebra do
["hello", "\n ", "world"]
"""
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest | t
def nest(doc, level, mode \\ :always)
def nest(doc, :cursor, mode) when is_doc(doc) and mode in [:always, :break] do
@@ -972,7 +976,7 @@ defmodule Inspect.Algebra do
@typep mode :: :flat | :flat_no_break | :break | :break_no_flat
@spec fits?(
width :: non_neg_integer(),
width :: non_neg_integer() | :infinity,
column :: non_neg_integer(),
break? :: boolean(),
entries
@@ -1037,9 +1041,17 @@ 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}])
@spec format(width :: non_neg_integer() | :infinity, column :: non_neg_integer(), [
{integer, mode, t}
]) :: [binary]
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]
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)]
@@ -1093,6 +1105,15 @@ defmodule Inspect.Algebra do
end
end
# Limit is set to infinity and then reverts
defp format(w, k, [{i, m, doc_limit(x, :infinity)} | t]) when w != :infinity do
format(:infinity, k, [{i, :flat, x}, {i, m, doc_limit(empty(), w)} | t])
end
defp format(_w, k, [{i, m, doc_limit(x, w)} | t]) do
format(w, k, [{i, m, x} | t])
end
defp collapse(["\n" <> _ | t], max, count, i) do
collapse(t, max, count + 1, i)
end
+5 -5
View File
@@ -4,11 +4,11 @@ defmodule Integer do
Some functions that work on integers are found in `Kernel`:
* `abs/1`
* `div/2`
* `max/2`
* `min/2`
* `rem/2`
* `Kernel.abs/1`
* `Kernel.div/2`
* `Kernel.max/2`
* `Kernel.min/2`
* `Kernel.rem/2`
"""
+51 -8
View File
@@ -172,8 +172,17 @@ defmodule IO do
@doc """
Reads from the IO `device`. The operation is Unicode unsafe.
The `device` is iterated by the given number of bytes, line by line if
`:line` is given, or until `:eof`.
The `device` is iterated 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.
It returns:
@@ -286,14 +295,24 @@ defmodule IO do
end
@doc """
Writes a `message` to stderr, along with the given `stacktrace`.
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
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]}]
@@ -302,12 +321,36 @@ defmodule IO do
#=> my_app.ex:4: MyApp.main/1
"""
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
@spec warn(chardata | String.Chars.t(), Exception.stacktrace() | keyword() | Macro.Env.t()) ::
:ok
def warn(message, stacktrace_info)
def warn(message, []) do
message = [to_chardata(message), ?\n]
:elixir_errors.log_and_print_warning(0, nil, message, message)
end
def warn(message, %Macro.Env{} = env) do
warn(message, Macro.Env.stacktrace(env))
end
def warn(message, [{_, _} | _] = keyword) do
if file = keyword[:file] do
warn(
message,
%{
__ENV__
| module: keyword[:module],
function: keyword[:function],
line: keyword[:line],
file: file
}
)
else
warn(message, [])
end
end
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
message = to_chardata(message)
formatted_trace = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
@@ -545,7 +588,7 @@ defmodule IO do
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
`stream/1` has been introduced in Elixir v1.12.0,
`stream/0` has been introduced in Elixir v1.12.0,
while `stream/2` has been available since v1.0.0.
## Examples
@@ -590,7 +633,7 @@ defmodule IO do
Finally, do not use this function on IO devices in Unicode
mode as it will return the wrong result.
`binstream/1` has been introduced in Elixir v1.12.0,
`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()
+35 -4
View File
@@ -3,6 +3,7 @@ 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
@@ -68,6 +69,34 @@ 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"
@@ -251,8 +280,9 @@ defmodule IO.ANSI do
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
"""
def format(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(chardata, [], [], emit?, :maybe)
@spec format(ansidata, boolean) :: IO.chardata()
def format(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(ansidata, [], [], emit?, :maybe)
end
@doc ~S"""
@@ -271,8 +301,9 @@ defmodule IO.ANSI do
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
"""
def format_fragment(chardata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(chardata, [], [], emit?, false)
@spec format_fragment(ansidata, boolean) :: IO.chardata()
def format_fragment(ansidata, emit? \\ enabled?()) when is_boolean(emit?) do
do_format(ansidata, [], [], emit?, false)
end
defp do_format([term | rest], rem, acc, emit?, append_reset) do
+5 -1
View File
@@ -26,7 +26,11 @@ defmodule IO.Stream do
defstruct device: nil, raw: true, line_or_bytes: :line
@type t :: %__MODULE__{}
@type t :: %__MODULE__{
device: IO.device(),
raw: boolean(),
line_or_bytes: :line | non_neg_integer()
}
@doc false
def __build__(device, raw, line_or_bytes) do
+468 -211
View File
File diff suppressed because it is too large Load Diff
+29 -15
View File
@@ -13,7 +13,8 @@ defmodule Kernel.CLI do
pa: [],
pz: [],
verbose_compile: false,
profile: nil
profile: nil,
pry: false
}
@standalone_opts ["-h", "--help", "--short-version"]
@@ -28,6 +29,10 @@ 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)
@@ -218,12 +223,16 @@ defmodule Kernel.CLI do
# Parse shared options
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
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 | t], config) when opt in ["-v", "--version"] do
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
if function_exported?(IEx, :started?, 0) and IEx.started?() do
IO.puts("IEx " <> System.build_info()[:build])
else
@@ -231,7 +240,11 @@ defmodule Kernel.CLI do
IO.puts("Elixir " <> System.build_info()[:build])
end
parse_shared(t, config)
if t != [] do
halt_standalone(opt)
else
System.halt(0)
end
end
defp parse_shared(["-pa", h | t], config) do
@@ -267,6 +280,11 @@ 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
@@ -306,7 +324,7 @@ defmodule Kernel.CLI do
end
defp parse_argv(["+iex" | t], config) do
parse_iex(t, config)
parse_iex(t, %{config | pry: true})
end
defp parse_argv(["-S", h | t], config) do
@@ -391,20 +409,16 @@ 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))
+68 -56
View File
@@ -30,8 +30,8 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def add_require(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_require, module})
def add_export(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_export, module})
end
@doc false
@@ -59,6 +59,11 @@ 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})
@@ -88,23 +93,24 @@ defmodule Kernel.LexicalTracker do
@doc false
def collect_unused_imports(pid) do
unused(pid, :import)
unused(pid, :unused_imports)
end
@doc false
def collect_unused_aliases(pid) do
unused(pid, :alias)
unused(pid, :unused_aliases)
end
defp unused(pid, tag) do
:gen_server.call(pid, {:unused, tag}, @timeout)
:gen_server.call(pid, tag, @timeout)
end
# Callbacks
def init(:ok) do
state = %{
directives: %{},
aliases: %{},
imports: %{},
references: %{},
exports: %{},
cache: %{},
@@ -116,13 +122,12 @@ defmodule Kernel.LexicalTracker do
end
@doc false
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_aliases, _from, state) do
{:reply, Enum.sort(state.aliases), state}
end
{:reply, Enum.sort(directives), state}
def handle_call(:unused_imports, _from, state) do
{:reply, Enum.sort(state.imports), state}
end
def handle_call(:references, _from, state) do
@@ -148,12 +153,44 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
state = add_import_dispatch(state, module, function, arity, mode)
{:noreply, state}
%{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}}
end
def handle_cast({:alias_dispatch, module}, state) do
{:noreply, %{state | directives: add_dispatch(state.directives, module, :alias)}}
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}}
end
def handle_cast({:set_file, file}, state) do
@@ -168,28 +205,25 @@ defmodule Kernel.LexicalTracker do
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
end
def handle_cast({:add_require, module}, state) do
def handle_cast({:add_export, module}, state) do
{:noreply, put_in(state.exports[module], true)}
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
directives =
state.directives
|> Map.drop(to_remove)
|> add_directive(module, line, warn, :import)
directives =
Enum.reduce(fas, directives, fn {function, arity}, directives ->
add_directive(directives, {module, function, arity}, line, warn, :import)
end)
{:noreply, %{state | directives: directives}}
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
end
def handle_cast({:add_alias, module, line, warn}, state) do
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
if warn do
{:noreply, put_in(state.aliases[module], line)}
else
{:noreply, state}
end
end
@doc false
@@ -221,31 +255,9 @@ defmodule Kernel.LexicalTracker do
do: Map.put(references, module, :compile)
defp add_reference(references, module, :runtime) when is_atom(module) do
case Map.fetch(references, module) do
{:ok, _} -> references
:error -> Map.put(references, module, :runtime)
case references do
%{^module => _} -> references
%{} -> 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
+31 -17
View File
@@ -5,9 +5,9 @@ defmodule Kernel.ParallelCompiler do
@typedoc "The line. 0 indicates no line."
@type line() :: non_neg_integer()
@type location() :: line() | {line(), column :: 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(), line(), message :: String.t()}
@type error() :: {file :: Path.t(), location(), message :: String.t()}
@doc """
Starts a task for parallel compilation.
@@ -28,7 +28,7 @@ defmodule Kernel.ParallelCompiler do
dest = :erlang.get(:elixir_compiler_dest)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
checker = Module.ParallelChecker.get()
{_parent, checker} = Module.ParallelChecker.get()
Task.async(fn ->
send(compiler, {:async, self()})
@@ -241,7 +241,7 @@ defmodule Kernel.ParallelCompiler do
defp write_module_binaries(result, {:compile, path}, timestamp) do
Enum.flat_map(result, fn
{{:module, module}, {binary, _map}} ->
{{:module, module}, binary} ->
full_path = Path.join(path, Atom.to_string(module) <> ".beam")
File.write!(full_path, binary)
if timestamp, do: File.touch!(full_path, timestamp)
@@ -268,8 +268,8 @@ defmodule Kernel.ParallelCompiler do
%{profile: profile, checker: checker} = state
compiled_modules =
for {{:module, _module}, {_binary, info}} <- result,
do: info
for {{:module, module}, _} <- result,
do: module
runtime_modules =
for module <- runtime_modules,
@@ -278,7 +278,7 @@ defmodule Kernel.ParallelCompiler do
do: {module, path}
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(checker, compiled_modules, runtime_modules)
Module.ParallelChecker.verify(checker, runtime_modules)
end)
end
@@ -479,16 +479,24 @@ 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({kind, modules}), do: {kind, modules, []}
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules, []}
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
# The goal of this function is to find leaves in the dependency graph,
# i.e. to find code that depends on code that we know is not being defined.
# Note that not all files have been compiled yet, so they may not be in waiting.
defp without_definition(waiting, files) do
nillify_empty(
nilify_empty(
for %{pid: pid} <- files,
{_, _, ref, ^pid, on, _, _} <- waiting,
not defining?(on, waiting),
@@ -497,7 +505,7 @@ defmodule Kernel.ParallelCompiler do
end
defp deadlocked(waiting, type, defining?) do
nillify_empty(
nilify_empty(
for {_, _, ref, _, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
@@ -508,8 +516,8 @@ defmodule Kernel.ParallelCompiler do
Enum.any?(waiting, fn {_, _, _, _, _, defining, _} -> on in defining end)
end
defp nillify_empty([]), do: nil
defp nillify_empty([_ | _] = list), do: list
defp nilify_empty([]), do: nil
defp nilify_empty([_ | _] = list), do: list
# Wait for messages from child processes
defp wait_for_messages(queue, spawned, waiting, files, result, warnings, state) do
@@ -528,7 +536,7 @@ defmodule Kernel.ParallelCompiler do
result = Map.put(result, {kind, module}, true)
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
{:module_available, child, ref, file, module, binary, checker_info} ->
{:module_available, child, ref, file, module, binary} ->
state.each_module.(file, module, binary)
# Release the module loader which is waiting for an ack
@@ -538,7 +546,7 @@ defmodule Kernel.ParallelCompiler do
for {:module, _, ref, _, ^module, _defining, _deadlock} <- waiting,
do: {ref, :found}
result = Map.put(result, {:module, module}, {binary, checker_info})
result = Map.put(result, {:module, module}, binary)
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
# If we are simply requiring files, we do not add to waiting.
@@ -752,6 +760,11 @@ defmodule Kernel.ParallelCompiler do
{file, line || 0, message}
end
defp get_line(_file, %{line: line, column: column}, _stack)
when is_integer(line) and line > 0 and is_integer(column) and column >= 0 do
{line, column}
end
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
line
end
@@ -762,7 +775,8 @@ defmodule Kernel.ParallelCompiler do
end
end
defp get_line(file, _reason, [{_, _, _, [file: 'expanding macro']}, {_, _, _, info} | _]) do
defp get_line(file, _reason, [{_, _, _, [file: expanding]}, {_, _, _, info} | _])
when expanding in ['expanding macro', 'expanding struct'] do
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
Keyword.get(info, :line)
end
+67 -12
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 or a literal charlist, which are by default expanded to integers:
string which expands to integers:
iex> <<0, "foo">>
<<0, 102, 111, 111>>
@@ -246,20 +246,20 @@ defmodule Kernel.SpecialForms do
iex> {name, species}
{"Frank", "Walrus"}
The size can be a variable:
The size can be a variable or any valid guard expression:
iex> name_size = 5
iex> <<name::binary-size(name_size), " the ", species::binary>> = <<"Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
And the variable can be defined in the match itself (prior to its use):
The size can access prior variables defined in the binary itself:
iex> <<name_size::size(8), name::binary-size(name_size), " the ", species::binary>> = <<5, "Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
However, the size cannot be defined in the match outside the binary/bitstring match:
However, it cannot access variables defined in the match outside of the binary/bitstring:
{name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, <<"Frank the Walrus">>}
** (CompileError): undefined variable "name_size" in bitstring segment
@@ -366,7 +366,7 @@ defmodule Kernel.SpecialForms do
To learn more about specific optimizations and performance considerations,
check out the
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
"""
defmacro unquote(:<<>>)(args), do: error!([args])
@@ -604,11 +604,12 @@ defmodule Kernel.SpecialForms do
import List
A developer can filter to import only macros or functions via
the only option:
A developer can filter to import only functions, macros, or sigils
(which can be functions or macros) 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
@@ -778,7 +779,7 @@ defmodule Kernel.SpecialForms do
<<int::integer-little, rest::bits>> = bits
Read the documentation on the `Typespec` page and
Read the documentation on the [Typespecs page](typespecs.md) and
`<<>>/1` for more information on typespecs and
bitstrings respectively.
"""
@@ -1315,11 +1316,13 @@ defmodule Kernel.SpecialForms do
sum(1, value, 3)
end
Which would then return:
Which the argument for the `:sum` function call is not the
expected result:
{:sum, [], [1, {:value, [], Elixir}, 3]}
Which is not the expected result. For this, we use `unquote`:
For this, we use `unquote`:
iex> value =
...> quote do
@@ -1386,6 +1389,10 @@ 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 `<-`:
@@ -1406,6 +1413,35 @@ 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
@@ -1602,7 +1638,7 @@ defmodule Kernel.SpecialForms do
{:ok, backup_path}
end
defp validate_extname(path) do
defp validate_extension(path) do
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
end
@@ -1611,7 +1647,7 @@ defmodule Kernel.SpecialForms do
end
Note how the code above is better organized and clearer once we
make sure each clause in `with` returns a normalize format.
make sure each clause in `with` returns a normalized format.
"""
defmacro with(args), do: error!([args])
@@ -2057,6 +2093,25 @@ 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
+20 -7
View File
@@ -104,7 +104,7 @@ defmodule Kernel.Typespec do
@doc """
Defines a typespec.
Invoked by `Kernel.@/1` expansion.
Invoked by `@/1` expansion.
"""
def deftypespec(:spec, expr, _line, _file, module, pos) do
{_set, bag} = :elixir_module.data_tables(module)
@@ -194,14 +194,14 @@ defmodule Kernel.Typespec do
defp get_doc_info(set, attr, line) do
case :ets.take(set, attr) do
[{^attr, {line, doc}, _}] -> {line, doc}
[{^attr, {line, doc}, _, _}] -> {line, doc}
[] -> {line, nil}
end
end
defp get_doc_meta(spec_meta, doc_kind, set) do
case :ets.take(set, {doc_kind, :meta}) do
[{{^doc_kind, :meta}, metadata, _}] -> Map.merge(metadata, spec_meta)
[{{^doc_kind, :meta}, metadata}] -> Map.merge(metadata, spec_meta)
[] -> spec_meta
end
end
@@ -567,7 +567,10 @@ defmodule Kernel.Typespec do
types =
:lists.map(
fn {field, _} -> {field, Keyword.get(fields, field, quote(do: term()))} end,
fn
{:__exception__ = field, true} -> {field, Keyword.get(fields, field, true)}
{field, _} -> {field, Keyword.get(fields, field, quote(do: term()))}
end,
:lists.sort(struct)
)
@@ -678,10 +681,20 @@ defmodule Kernel.Typespec do
end
end
defp typespec({:"::", meta, [left, right]} = expr, vars, caller, state) do
defp typespec({:"::", meta, [left, right]}, vars, caller, state) do
message =
"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)}"
"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
# TODO: Make this an error on v2.0, and remove the code below and
# the :undefined_type_error_enabled? key from the state
+100 -7
View File
@@ -22,9 +22,38 @@ defmodule Kernel.Utils do
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
@doc """
Callback for defdelegate.
Callback for defdelegate entry point.
"""
def defdelegate(fun, opts) when is_list(opts) do
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
# TODO: Remove on v2.0
append_first? = Keyword.get(opts, :append_first, false)
@@ -71,7 +100,15 @@ defmodule Kernel.Utils do
@doc """
Callback for defstruct.
"""
def defstruct(module, fields) do
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
case fields do
fs when is_list(fs) ->
:ok
@@ -83,7 +120,7 @@ defmodule Kernel.Utils do
mapper = fn
{key, val} when is_atom(key) ->
try do
Macro.escape(val)
:elixir_quote.escape(val, false, :none)
rescue
e in [ArgumentError] ->
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
@@ -99,7 +136,13 @@ defmodule Kernel.Utils do
end
fields = :lists.map(mapper, fields)
enforce_keys = List.wrap(Module.get_attribute(module, :enforce_keys))
enforce_keys =
case :ets.take(set, :enforce_keys) do
[{_, enforce_keys, _, _}] when is_list(enforce_keys) -> enforce_keys
[{_, enforce_key, _, _}] -> [enforce_key]
[] -> []
end
# TODO: Make it raise on v2.0
warn_on_duplicate_struct_key(:lists.keysort(1, fields))
@@ -113,12 +156,62 @@ 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
[] ->
{struct, enforce_keys, Module.get_attribute(module, :derive)}
# 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,
+84 -29
View File
@@ -92,6 +92,7 @@ defmodule Keyword do
iex> %{1 => 2, foo: :bar}
%{1 => 2, :foo => :bar}
"""
@compile :inline_list_funcs
@@ -102,6 +103,21 @@ defmodule Keyword do
@type t :: [{key, value}]
@type t(value) :: [{key, value}]
@doc """
Builds a keyword from the given `keys` and the fixed `value`.
## Examples
iex> Keyword.from_keys([:foo, :bar, :baz], :atom)
[foo: :atom, bar: :atom, baz: :atom]
"""
@doc since: "1.14.0"
@spec from_keys([key], value) :: t(value)
def from_keys(keys, value) when is_list(keys) do
:lists.map(&{&1, value}, keys)
end
@doc """
Returns `true` if `term` is a keyword list, otherwise `false`.
@@ -158,7 +174,7 @@ defmodule Keyword do
[a: 3]
"""
@spec new(Enum.t()) :: t
@spec new(Enumerable.t()) :: t
def new(pairs) do
new(pairs, fn pair -> pair end)
end
@@ -176,7 +192,7 @@ defmodule Keyword do
[a: :a, b: :b]
"""
@spec new(Enum.t(), (term -> {key, value})) :: t
@spec new(Enumerable.t(), (term -> {key, value})) :: t
def new(pairs, transform) when is_function(transform, 1) do
fun = fn el, acc ->
{k, v} = transform.(el)
@@ -187,8 +203,7 @@ defmodule Keyword do
end
@doc """
Ensures the first argument is a `keyword` with the given
keys and default values.
Ensures the given `keyword` has only the keys given in `values`.
The second argument must be a list of atoms, specifying
a given key, or tuples specifying a key and a default value.
@@ -224,6 +239,12 @@ defmodule Keyword do
iex> Keyword.validate([three: 3, four: 4], [one: 1, two: 2])
{:error, [:four, :three]}
Passing the same key multiple times also errors:
iex> Keyword.validate([one: 1, two: 2, one: 1], [:one, :two])
{:error, [:one]}
"""
@doc since: "1.13.0"
@spec validate(keyword(), values :: [atom() | {atom(), term()}]) ::
@@ -302,6 +323,12 @@ defmodule Keyword do
iex> Keyword.validate!([three: 3], [one: 1, two: 2])
** (ArgumentError) unknown keys [:three] in [three: 3], the allowed keys are: [:one, :two]
Passing the same key multiple times also errors:
iex> Keyword.validate!([one: 1, two: 2, one: 1], [:one, :two])
** (ArgumentError) duplicate keys [:one] in [one: 1, two: 2, one: 1]
"""
@doc since: "1.13.0"
@spec validate!(keyword(), values :: [atom() | {atom(), term()}]) :: keyword()
@@ -315,8 +342,17 @@ defmodule Keyword do
for value <- values,
do: if(is_atom(value), do: value, else: elem(value, 0))
raise ArgumentError,
"unknown keys #{inspect(invalid_keys)} in #{inspect(keyword)}, the allowed keys are: #{inspect(keys)}"
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
@@ -853,6 +889,43 @@ defmodule Keyword do
raise KeyError, key: key, term: original
end
@doc """
Replaces the value under `key` using the given function only if
`key` already exists in `keywords`.
In comparison to `replace/3`, this can be useful when it's expensive to calculate the value.
If `key` does not exist, the original keyword list is returned unchanged.
## Examples
iex> Keyword.replace_lazy([a: 1, b: 2], :a, fn v -> v * 4 end)
[a: 4, b: 2]
iex> Keyword.replace_lazy([a: 2, b: 2, a: 1], :a, fn v -> v * 4 end)
[a: 8, b: 2]
iex> Keyword.replace_lazy([a: 1, b: 2], :c, fn v -> v * 4 end)
[a: 1, b: 2]
"""
@doc since: "1.14.0"
@spec replace_lazy(t, key, (existing_value :: value -> new_value :: value)) :: t
def replace_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
do_replace_lazy(keywords, key, fun)
end
defp do_replace_lazy([{key, value} | keywords], key, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp do_replace_lazy([{_, _} = e | keywords], key, fun) do
[e | do_replace_lazy(keywords, key, fun)]
end
defp do_replace_lazy([], _key, _value), do: []
@doc """
Checks if two keywords are equal.
@@ -1128,7 +1201,7 @@ defmodule Keyword do
"""
@spec take(t, [key]) :: t
def take(keywords, keys) when is_list(keywords) and is_list(keys) do
:lists.filter(fn {k, _} -> k in keys end, keywords)
:lists.filter(fn {k, _} -> :lists.member(k, keys) end, keywords)
end
@doc """
@@ -1374,27 +1447,9 @@ defmodule Keyword do
end
end
@doc """
Maps the function `fun` over all key-value pairs in `keywords`,
returning a keyword list with all the values replaced with
the result of the function.
## Examples
iex> Keyword.map([one: 1, two: 2, three: 3], fn {_key, val} -> to_string(val) end)
[one: "1", two: "2", three: "3"]
"""
@doc since: "1.13.0"
@spec map(t, ({key, value} -> value)) :: t
def map(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
do_map(keywords, fun)
end
defp do_map([], _fun), do: []
defp do_map([{key, value} | rest], fun) do
new_value = fun.({key, value})
[{key, new_value} | do_map(rest, fun)]
@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)
end
end
+115 -9
View File
@@ -8,7 +8,7 @@ defmodule List do
[1, "two", 3, :four]
Two lists can be concatenated and subtracted using the
`Kernel.++/2` and `Kernel.--/2` operators:
`++/2` and `--/2` operators:
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
@@ -239,7 +239,7 @@ defmodule List do
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
2
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
{6, -6}
@@ -257,7 +257,7 @@ defmodule List do
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
-2
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
%{product: 24, sum: 10}
@@ -339,6 +339,11 @@ 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
@@ -363,6 +368,11 @@ defmodule List do
iex> List.keyfind!([a: 1, b: 2], :c, 0)
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
This function works for any list of tuples:
iex> List.keyfind!([{22, "SSH"}, {80, "HTTP"}], 22, 0)
{22, "SSH"}
"""
@doc since: "1.13.0"
@spec keyfind!([tuple], any, non_neg_integer) :: any
@@ -391,6 +401,11 @@ 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
@@ -409,6 +424,11 @@ 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
@@ -417,7 +437,13 @@ defmodule List do
@doc """
Receives a list of tuples and sorts the elements
at `position` of the tuples. The sort is stable.
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.
## Examples
@@ -427,12 +453,69 @@ 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]}
]
"""
@spec keysort([tuple], non_neg_integer) :: [tuple]
def keysort(list, position) when is_integer(position) do
@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
: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`.
@@ -447,6 +530,11 @@ 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
@@ -469,6 +557,11 @@ 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
@@ -493,6 +586,11 @@ 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
@@ -849,14 +947,22 @@ defmodule List do
end
@doc """
Converts a charlist to an existing atom. Raises an `ArgumentError`
if the atom does not exist.
Converts a charlist to an existing atom.
Elixir supports conversions from charlists which contains any Unicode
code point.
code point. Raises an `ArgumentError` if the atom does not exist.
Inlined by the compiler.
> #### Atoms and modules {: .info}
>
> Since Elixir is a compiled language, the atoms defined in a module
> will only exist after said module is loaded, which typically happens
> whenever a function in the module is executed. Therefore, it is
> generally recommended to call `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
+677 -75
View File
File diff suppressed because it is too large Load Diff
+34 -26
View File
@@ -24,7 +24,7 @@ defmodule Macro.Env do
* `context` - the context of the environment; it can be `nil`
(default context), `:guard` (inside a guard) or `:match` (inside a match)
* `context_modules` - a list of modules defined in the current context
* `file` - the current file name as a binary
* `file` - the current absolute file name as a binary
* `function` - a tuple as `{atom, integer}`, where the first
element is the function name and the second its arity; returns
`nil` if not inside a function
@@ -79,31 +79,39 @@ defmodule Macro.Env do
versioned_vars: versioned_vars
}
# Define the __struct__ callbacks by hand for bootstrap reasons.
@doc false
def __struct__ do
%{
__struct__: __MODULE__,
aliases: [],
context: nil,
context_modules: [],
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
requires: [],
tracers: [],
versioned_vars: %{}
}
end
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: %{}
]
@doc false
def __struct__(kv) do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) 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: []}
end
@doc """
@@ -158,7 +166,7 @@ defmodule Macro.Env do
@doc """
Fetches the alias for the given atom.
Returns `{:ok, alias}` if the alias exists, `:error`
Returns `{:ok, alias}` if the alias exists, `:error`
otherwise.
## Examples
+84 -32
View File
@@ -88,12 +88,18 @@ defmodule Map do
%{1 => :one, 2 => :two, 3 => :three}
Maps also support a specific update syntax to update the value stored under
*existing* atom keys:
*existing* keys. You can update using the atom keys syntax:
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}
@@ -117,6 +123,27 @@ 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`.
@@ -214,7 +241,24 @@ defmodule Map do
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform) when is_function(transform, 1) do
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
enumerable
|> Enum.map(transform)
|> :maps.from_list()
@@ -350,6 +394,32 @@ 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.
@@ -546,7 +616,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 `Kernel.struct/2`.
use `struct/2`.
Inlined by the compiler.
@@ -979,12 +1049,14 @@ defmodule Map do
`fun` receives the key and value of each of the
elements in the map as a key-value pair.
`Map.filter/2` is faster than using `map |> Enum.filter(fun) |> Enum.into(%{})`,
as no intermediate list is being built.
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)
@@ -1010,9 +1082,8 @@ defmodule Map do
end
@doc """
Returns map excluding the pairs from `map` for which `fun` returns a truthy value.
`Map.reject/2` is faster than using `map |> Enum.reject(fun) |> Enum.into(%{})`,
as no intermediate list is being built.
Returns map excluding the pairs from `map` for which `fun` returns
a truthy value.
See also `filter/2`.
@@ -1040,28 +1111,9 @@ defmodule Map do
end
end
@doc """
Maps the function `fun` over all key-value pairs in `map`, returning a map
with all the values replaced with the result of the function.
## Examples
iex> Map.map(%{1 => "joe", 2 => "mike", 3 => "robert"}, fn {_key, val} -> String.capitalize(val) end)
%{1 => "Joe", 2 => "Mike", 3 => "Robert"}
"""
@doc since: "1.13.0"
@spec map(map, ({key, value} -> value)) :: map
def map(map, fun) when is_map(map) and is_function(fun, 1) do
iter = :maps.iterator(map)
next = :maps.next(iter)
:maps.from_list(do_map(next, fun))
end
defp do_map(:none, _fun), do: []
defp do_map({key, value, iter}, fun) do
new_value = fun.({key, value})
[{key, new_value} | do_map(:maps.next(iter), fun)]
@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
+150 -61
View File
@@ -8,18 +8,18 @@ defmodule MapSet do
A set can be constructed using `MapSet.new/0`:
iex> MapSet.new()
#MapSet<[]>
MapSet.new([])
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<[1, :two, {"three"}]>
MapSet.new([1, :two, {"three"}])
Elements can be inserted using `MapSet.put/2`:
iex> MapSet.new([2]) |> MapSet.put(4) |> MapSet.put(0)
#MapSet<[0, 2, 4]>
MapSet.new([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<["foo"]>
MapSet.new(["foo"])
iex> map_set |> MapSet.put("foo") |> MapSet.put("foo")
#MapSet<["foo"]>
MapSet.new(["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,10 +54,12 @@ defmodule MapSet do
@type value :: term
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
@opaque internal(value) :: %{optional(value) => []}
@type t(value) :: %__MODULE__{map: internal(value)}
@type t :: t(term)
# TODO: Remove version key on Elixir v2.0
# TODO: Remove version key when we require Erlang/OTP 24
# TODO: Implement the functions in this module using Erlang/OTP 24 new sets
defstruct map: %{}, version: 2
@doc """
@@ -66,7 +68,7 @@ defmodule MapSet do
## Examples
iex> MapSet.new()
#MapSet<[]>
MapSet.new([])
"""
@spec new :: t
@@ -78,23 +80,19 @@ defmodule MapSet do
## Examples
iex> MapSet.new([:b, :a, 3])
#MapSet<[3, :a, :b]>
MapSet.new([3, :a, :b])
iex> MapSet.new([3, 3, 3, 2, 2, 1])
#MapSet<[1, 2, 3]>
MapSet.new([1, 2, 3])
"""
@spec new(Enum.t()) :: t
@spec new(Enumerable.t()) :: t
def new(enumerable)
def new(%__MODULE__{} = map_set), do: map_set
def new(enumerable) do
map =
enumerable
|> Enum.to_list()
|> new_from_list([])
%MapSet{map: map}
keys = Enum.to_list(enumerable)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
end
@doc """
@@ -103,33 +101,13 @@ defmodule MapSet do
## Examples
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
#MapSet<[2, 4]>
MapSet.new([2, 4])
"""
@spec new(Enum.t(), (term -> val)) :: t(val) when val: value
@spec new(Enumerable.t(), (term -> val)) :: t(val) when val: value
def new(enumerable, transform) when is_function(transform, 1) do
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])
keys = Enum.map(enumerable, transform)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
end
@doc """
@@ -141,9 +119,9 @@ defmodule MapSet do
iex> map_set = MapSet.new([1, 2, 3])
iex> MapSet.delete(map_set, 4)
#MapSet<[1, 2, 3]>
MapSet.new([1, 2, 3])
iex> MapSet.delete(map_set, 2)
#MapSet<[1, 3]>
MapSet.new([1, 3])
"""
@spec delete(t(val1), val2) :: t(val1) when val1: value, val2: value
@@ -157,7 +135,7 @@ defmodule MapSet do
## Examples
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1]>
MapSet.new([1])
"""
@spec difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
@@ -183,13 +161,51 @@ defmodule MapSet do
%{map_set | map: Map.drop(map1, Map.keys(map2))}
end
defp filter_not_in(:none, _map2, acc), do: Map.new(acc)
defp filter_not_in(:none, _map2, acc), do: Map.from_keys(acc, @dummy_value)
defp filter_not_in({key, _val, iter}, map2, acc) do
if :erlang.is_map_key(key, map2) do
if is_map_key(map2, key) do
filter_not_in(:maps.next(iter), map2, acc)
else
filter_not_in(:maps.next(iter), map2, [{key, @dummy_value} | acc])
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])
end
end
@@ -217,7 +233,7 @@ defmodule MapSet do
defp none_in?(:none, _), do: true
defp none_in?({key, _val, iter}, map2) do
not :erlang.is_map_key(key, map2) and none_in?(:maps.next(iter), map2)
not is_map_key(map2, key) and none_in?(:maps.next(iter), map2)
end
@doc """
@@ -254,10 +270,10 @@ defmodule MapSet do
## Examples
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[2]>
MapSet.new([2])
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))
#MapSet<[]>
MapSet.new([])
"""
@spec intersection(t(val), t(val)) :: t(val) when val: value
@@ -279,7 +295,7 @@ defmodule MapSet do
"""
@spec member?(t, value) :: boolean
def member?(%MapSet{map: map}, value) do
:erlang.is_map_key(value, map)
is_map_key(map, value)
end
@doc """
@@ -288,9 +304,9 @@ defmodule MapSet do
## Examples
iex> MapSet.put(MapSet.new([1, 2, 3]), 3)
#MapSet<[1, 2, 3]>
MapSet.new([1, 2, 3])
iex> MapSet.put(MapSet.new([1, 2, 3]), 4)
#MapSet<[1, 2, 3, 4]>
MapSet.new([1, 2, 3, 4])
"""
@spec put(t(val), new_val) :: t(val | new_val) when val: value, new_val: value
@@ -363,7 +379,7 @@ defmodule MapSet do
## Examples
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1, 2, 3, 4]>
MapSet.new([1, 2, 3, 4])
"""
@spec union(t(val1), t(val2)) :: t(val1 | val2) when val1: value, val2: value
@@ -374,14 +390,88 @@ defmodule MapSet do
end
def union(%MapSet{map: map1}, %MapSet{map: map2}) do
map = new_from_list(Map.keys(map1) ++ Map.keys(map2), [])
%MapSet{map: map}
keys = Map.keys(map1) ++ Map.keys(map2)
%MapSet{map: Map.from_keys(keys, @dummy_value)}
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)}
@@ -393,7 +483,7 @@ defmodule MapSet do
def slice(map_set) do
size = MapSet.size(map_set)
{:ok, size, &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2, size)}
{:ok, size, &MapSet.to_list/1}
end
def reduce(map_set, acc, fun) do
@@ -402,11 +492,10 @@ defmodule MapSet do
end
defimpl Collectable do
# TODO: Optimize into an empty mapset by using :maps.from_keys/2 on Erlang/OTP 24+
def into(map_set) do
fun = fn
list, {:cont, x} -> [{x, []} | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.new(list))}
list, {:cont, x} -> [x | list]
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.from_keys(list, []))}
_, :halt -> :ok
end
@@ -419,7 +508,7 @@ defmodule MapSet do
def inspect(map_set, opts) do
opts = %Inspect.Opts{opts | charlists: :as_lists}
concat(["#MapSet<", Inspect.List.inspect(MapSet.to_list(map_set), opts), ">"])
concat(["MapSet.new(", Inspect.List.inspect(MapSet.to_list(map_set), opts), ")"])
end
end
end
+372 -257
View File
@@ -22,6 +22,12 @@ defmodule Module do
Accepts a module or a `{module, function_name}`. See the "Compile callbacks"
section below.
### `@after_verify` (since v1.14.0)
A hook that will be invoked right after the current module is verified for
undefined functions, deprecations, etc. Accepts a module or a `{module, function_name}`.
See the "Compile callbacks" section below.
### `@before_compile`
A hook that will be invoked before the module is compiled.
@@ -128,7 +134,7 @@ defmodule Module do
Multiple uses of `@compile` will accumulate instead of overriding
previous ones. See the "Compile options" section below.
### `@deprecated`
### `@deprecated` (since v1.6.0)
Provides the deprecation reason for a function. For example:
@@ -176,9 +182,14 @@ defmodule Module do
`@doc` is to be used with a function, macro, callback, or
macrocallback, while `@typedoc` with a type (public or opaque).
Accepts a string (often a heredoc) or `false` where `@doc false` will
make the entity invisible to documentation extraction tools like
[`ExDoc`](https://hexdocs.pm/ex_doc/). For example:
Accepts one of these:
* a string (often a heredoc)
* `false`, which will make the entity invisible to documentation-extraction
tools like [`ExDoc`](https://hexdocs.pm/ex_doc/)
* a keyword list, since Elixir 1.7.0
For example:
defmodule MyModule do
@typedoc "This type"
@@ -199,11 +210,11 @@ defmodule Module do
end
end
As can be seen in the example above, `@doc` and `@typedoc` also accept
a keyword list that serves as a way to provide arbitrary metadata
As can be seen in the example above, since Elixir 1.7.0 `@doc` and `@typedoc`
also accept a keyword list that serves as a way to provide arbitrary metadata
about the entity. Tools like [`ExDoc`](https://hexdocs.pm/ex_doc/) and
`IEx` may use this information to display annotations. A common use
case is `since` that may be used to annotate in which version the
case is the `:since` key, which may be used to annotate in which version the
function was introduced.
As illustrated in the example, it is possible to use these attributes
@@ -221,8 +232,7 @@ defmodule Module do
### `@dialyzer`
Defines warnings to request or suppress when using a version of
`:dialyzer` that supports module attributes.
Defines warnings to request or suppress when using `:dialyzer`.
Accepts an atom, a tuple, or a list of atoms and tuples. For example:
@@ -234,8 +244,7 @@ defmodule Module do
end
end
For the list of supported warnings, see
[`:dialyzer` module](`:dialyzer`).
For the list of supported warnings, see [`:dialyzer` module](`:dialyzer`).
Multiple uses of `@dialyzer` will accumulate instead of overriding
previous ones.
@@ -296,156 +305,6 @@ defmodule Module do
A hook that will be invoked when each function or macro in the current
module is defined. Useful when annotating functions.
Accepts a module or a `{module, function_name}` tuple. See the
"Compile callbacks" section below.
### `@on_load`
A hook that will be invoked whenever the module is loaded.
Accepts the function name (as an atom) of a function in the current module or
`{function_name, 0}` tuple where `function_name` is the name of a function in
the current module. The function must have an arity of 0 (no arguments). If
the function does not return `:ok`, the loading of the module will be aborted.
For example:
defmodule MyModule do
@on_load :load_check
def load_check do
if some_condition() do
:ok
else
:abort
end
end
def some_condition do
false
end
end
Modules compiled with HiPE would not call this hook.
### `@vsn`
Specify the module version. Accepts any valid Elixir value, for example:
defmodule MyModule do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `Kernel.defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
Elixir:
* `@type` - defines a type to be used in `@spec`
* `@typep` - defines a private type to be used in `@spec`
* `@opaque` - defines an opaque type to be used in `@spec`
* `@spec` - provides a specification for a function
* `@callback` - provides a specification for a behaviour callback
* `@macrocallback` - provides a specification for a macro behaviour callback
* `@optional_callbacks` - specifies which behaviour callbacks and macro
behaviour callbacks are optional
* `@impl` - declares an implementation of a callback function or macro
For detailed documentation, see the [typespec documentation](typespecs.md).
### Custom attributes
In addition to the built-in attributes outlined above, custom attributes may
also be added. Custom attributes are expressed using the `@/1` operator followed
by a valid variable name. The value given to the custom attribute must be a valid
Elixir value:
defmodule MyModule do
@custom_attr [some: "stuff"]
end
For more advanced options available when defining custom attributes, see
`register_attribute/3`.
## Compile callbacks
There are three callbacks that are invoked when functions are defined,
as well as before and immediately after the module bytecode is generated.
### `@after_compile`
A hook that will be invoked right after the current module is compiled.
Accepts a module or a `{module, function_name}` tuple. The function
must take two arguments: the module environment and its bytecode.
When just a module is provided, the function is assumed to be
`__after_compile__/2`.
Callbacks will run in the order they are registered.
#### Example
defmodule MyModule do
@after_compile __MODULE__
def __after_compile__(env, _bytecode) do
IO.inspect(env)
end
end
### `@before_compile`
A hook that will be invoked before the module is compiled.
Accepts a module or a `{module, function_or_macro_name}` tuple. The
function/macro must take one argument: the module environment. If
it's a macro, its returned value will be injected at the end of the
module definition before the compilation starts.
When just a module is provided, the function/macro is assumed to be
`__before_compile__/1`.
Callbacks will run in the order they are registered. Any overridable
definition will be made concrete before the first callback runs.
A definition may be made overridable again in another before compile
callback and it will be made concrete one last time after all callbacks
run.
*Note*: unlike `@after_compile`, the callback function/macro must
be placed in a separate module (because when the callback is invoked,
the current module does not yet exist).
#### Example
defmodule A do
defmacro __before_compile__(_env) do
quote do
def hello, do: "world"
end
end
end
defmodule B do
@before_compile A
end
B.hello()
#=> "world"
### `@on_definition`
A hook that will be invoked when each function or macro in the current
module is defined. Useful when annotating functions.
Accepts a module or a `{module, function_name}` tuple. The function
must take 6 arguments:
@@ -492,6 +351,177 @@ defmodule Module do
end
end
### `@on_load`
A hook that will be invoked whenever the module is loaded.
Accepts the function name (as an atom) of a function in the current module.
The function must have an arity of 0 (no arguments). If the function does
not return `:ok`, the loading of the module will be aborted.
For example:
defmodule MyModule do
@on_load :load_check
def load_check do
if some_condition() do
:ok
else
:abort
end
end
def some_condition do
false
end
end
### `@vsn`
Specify the module version. Accepts any valid Elixir value, for example:
defmodule MyModule do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
Elixir:
* `@type` - defines a type to be used in `@spec`
* `@typep` - defines a private type to be used in `@spec`
* `@opaque` - defines an opaque type to be used in `@spec`
* `@spec` - provides a specification for a function
* `@callback` - provides a specification for a behaviour callback
* `@macrocallback` - provides a specification for a macro behaviour callback
* `@optional_callbacks` - specifies which behaviour callbacks and macro
behaviour callbacks are optional
* `@impl` - declares an implementation of a callback function or macro
For detailed documentation, see the [typespec documentation](typespecs.md).
### Custom attributes
In addition to the built-in attributes outlined above, custom attributes may
also be added. Custom attributes are expressed using the `@/1` operator followed
by a valid variable name. The value given to the custom attribute must be a valid
Elixir value:
defmodule MyModule do
@custom_attr [some: "stuff"]
end
For more advanced options available when defining custom attributes, see
`register_attribute/3`.
## Compile callbacks
There are three compilation callbacks, invoked in this order:
`@before_compile`, `@after_compile`, and `@after_verify`.
They are described next.
### `@before_compile`
A hook that will be invoked before the module is compiled. This is
often used to change how the current module is being compiled.
Accepts a module or a `{module, function_or_macro_name}` tuple. The
function/macro must take one argument: the module environment. If
it's a macro, its returned value will be injected at the end of the
module definition before the compilation starts.
When just a module is provided, the function/macro is assumed to be
`__before_compile__/1`.
Callbacks will run in the order they are registered. Any overridable
definition will be made concrete before the first callback runs.
A definition may be made overridable again in another before compile
callback and it will be made concrete one last time after all callbacks
run.
*Note*: the callback function/macro must be placed in a separate module
(because when the callback is invoked, the current module does not yet exist).
#### Example
defmodule A do
defmacro __before_compile__(_env) do
quote do
def hello, do: "world"
end
end
end
defmodule B do
@before_compile A
end
B.hello()
#=> "world"
### `@after_compile`
A hook that will be invoked right after the current module is compiled.
Accepts a module or a `{module, function_name}` tuple. The function
must take two arguments: the module environment and its bytecode.
When just a module is provided, the function is assumed to be
`__after_compile__/2`.
Callbacks will run in the order they are registered.
`Module` functions expecting not yet compiled modules (such as `definitions_in/1`)
are still available at the time `@after_compile` is invoked.
#### Example
defmodule MyModule do
@after_compile __MODULE__
def __after_compile__(env, _bytecode) do
IO.inspect(env)
end
end
### `@after_verify`
A hook that will be invoked right after the current module is verified for
undefined functions, deprecations, etc. A module is always verified after
it is compiled. In Mix projects, a module is also verified when any of its
runtime dependencies change. Therefore this is useful to perform verification
of the current module while avoiding compile-time dependencies.
Accepts a module or a `{module, function_name}` tuple. The function
must take one argument: the module name. When just a module is provided,
the function is assumed to be `__after_verify__/2`.
Callbacks will run in the order they are registered.
`Module` functions expecting not yet compiled modules are no longer available
at the time `@after_verify` is invoked.
#### Example
defmodule MyModule do
@after_verify __MODULE__
def __after_verify__(module) do
IO.inspect(module)
:ok
end
end
## Compile options
The `@compile` attribute accepts different options that are used by both
@@ -518,8 +548,8 @@ defmodule Module do
'''
@typep definition :: {atom, arity}
@typep def_kind :: :def | :defp | :defmacro | :defmacrop
@type definition :: {atom, arity}
@type def_kind :: :def | :defp | :defmacro | :defmacrop
@extra_error_msg_defines? "Use Kernel.function_exported?/3 and Kernel.macro_exported?/3 " <>
"to check for public functions and macros instead"
@@ -545,6 +575,8 @@ defmodule Module do
* `:module` - the module atom name
* `:struct` - if the module defines a struct and if so each field in order
"""
@callback __info__(:attributes) :: keyword()
@callback __info__(:compile) :: [term()]
@@ -552,6 +584,7 @@ defmodule Module do
@callback __info__(:macros) :: keyword()
@callback __info__(:md5) :: binary()
@callback __info__(:module) :: module()
@callback __info__(:struct) :: list(%{field: atom(), required: boolean()}) | nil
@doc """
Returns information about module attributes used by Elixir.
@@ -574,6 +607,9 @@ defmodule Module do
after_compile: %{
doc: "A hook that will be invoked right after the current module is compiled."
},
after_verify: %{
doc: "A hook that will be invoked right after the current module is verified."
},
before_compile: %{
doc: "A hook that will be invoked before the module is compiled."
},
@@ -642,10 +678,6 @@ defmodule Module do
derive: %{
doc:
"Derives an implementation for the given protocol for the struct defined in the current module."
},
enforce_keys: %{
doc:
"Ensures the given keys are always set when building the struct defined in the current module."
}
}
end
@@ -762,7 +794,7 @@ defmodule Module do
`Module.create/3` works similarly to `Kernel.defmodule/2`
and return the same results. While one could also use
`defmodule` to define modules dynamically, this function
`Kernel.defmodule/2` to define modules dynamically, this function
is preferred when the module body is given by a quoted
expression.
@@ -784,8 +816,8 @@ defmodule Module do
end
next = :elixir_module.next_counter(nil)
line = Keyword.get(opts, :line, 0)
quoted = :elixir_quote.linify_with_context_counter(line, {module, next}, quoted)
meta = Keyword.take(opts, [:line, :generated])
quoted = :elixir_quote.linify_with_context_counter(meta, {module, next}, quoted)
:elixir_module.compile(module, quoted, [], :elixir.env_for_eval(opts))
end
@@ -927,6 +959,10 @@ defmodule Module do
simplify_arg(Macro.expand_once(attr, env), counters, env)
end
defp simplify_arg({:var!, _, [{var, _, atom} | _]}, counters, _env) when is_atom(atom) do
{simplify_var(var, Elixir), counters}
end
defp simplify_arg(other, counters, _env) when is_integer(other),
do: autogenerated_key(counters, :int)
@@ -1100,7 +1136,7 @@ defmodule Module do
"""
@doc since: "1.7.0"
@spec defines_type?(module, definition) :: boolean
def defines_type?(module, definition) do
def defines_type?(module, definition) when is_atom(module) do
Kernel.Typespec.defines_type?(module, definition)
end
@@ -1141,7 +1177,7 @@ defmodule Module do
def attributes_in(module) when is_atom(module) do
assert_not_compiled!(__ENV__.function, module)
{set, _} = data_tables_for(module)
:ets.select(set, [{{:"$1", :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
:ets.select(set, [{{:"$1", :_, :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
end
@doc """
@@ -1158,10 +1194,10 @@ defmodule Module do
def foo, do: 1
def bar, do: 2
defoverridable foo: 1, bar: 1
defoverridable foo: 0, bar: 0
def foo, do: 3
[:bar, :foo] = Module.overridables_in(__MODULE__) |> Enum.sort()
[bar: 0, foo: 0] = Module.overridables_in(__MODULE__) |> Enum.sort()
end
"""
@@ -1241,14 +1277,15 @@ defmodule Module do
## Options
* `:nillify_clauses` (since v1.13.0) - returns `nil` instead
* `:skip_clauses` (since v1.14.0) - returns `[]` instead
of returning the clauses. This is useful when there is
only an interest in fetching the kind and metadata
only an interest in fetching the kind and the metadata
"""
@spec get_definition(module, definition, keyword) ::
{:v1, def_kind, meta :: keyword,
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}] | nil}
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
| nil
@doc since: "1.12.0"
def get_definition(module, {name, arity}, options \\ [])
when is_atom(module) and is_atom(name) and is_integer(arity) and is_list(options) do
@@ -1258,8 +1295,8 @@ defmodule Module do
case :ets.lookup(set, {:def, {name, arity}}) do
[{_key, kind, meta, _, _, _}] ->
clauses =
if options[:nillify_clauses],
do: nil,
if options[:skip_clauses],
do: [],
else: bag_lookup_element(bag, {:clauses, {name, arity}}, 2)
{:v1, kind, meta, clauses}
@@ -1423,7 +1460,7 @@ defmodule Module do
"""
@spec put_attribute(module, atom, term) :: :ok
def put_attribute(module, key, value) when is_atom(module) and is_atom(key) do
__put_attribute__(module, key, value, nil)
__put_attribute__(module, key, value, nil, [])
end
@doc """
@@ -1465,7 +1502,7 @@ defmodule Module do
"""
@spec get_attribute(module, atom, term) :: term
def get_attribute(module, key, default \\ nil) when is_atom(module) and is_atom(key) do
case __get_attribute__(module, key, nil) do
case __get_attribute__(module, key, nil, true) do
nil -> default
value -> value
end
@@ -1508,9 +1545,14 @@ defmodule Module do
end
@doc """
Deletes the module attribute that matches the given key.
Deletes the entry (or entries) for the given module attribute.
It returns the deleted attribute value (or `nil` if nothing was set).
It returns the deleted attribute value. If the attribute has not
been set nor configured to accumulate, it returns `nil`.
If the attribute is set to accumulate, then this function always
returns a list. Deleting the attribute removes existing entries
but the attribute will still accumulate.
## Examples
@@ -1526,10 +1568,12 @@ defmodule Module do
{set, bag} = data_tables_for(module)
case :ets.lookup(set, key) do
[{_, _, :accumulate}] ->
[{_, _, :accumulate, traces}] ->
trace_attribute(true, module, traces, set, key, [])
reverse_values(:ets.take(bag, {:accumulate, key}), [])
[{_, value, _}] ->
[{_, value, _, traces}] ->
trace_attribute(module, traces)
:ets.delete(set, key)
value
@@ -1558,7 +1602,8 @@ defmodule Module do
* `:persist` - the attribute will be persisted in the Erlang
Abstract Format. Useful when interfacing with Erlang libraries.
By default, both options are `false`.
By default, both options are `false`. Once an attribute has been
set to accumulate or persist, the behaviour cannot be reverted.
## Examples
@@ -1582,11 +1627,11 @@ defmodule Module do
end
if Keyword.get(options, :accumulate) do
:ets.insert_new(set, {attribute, [], :accumulate}) ||
:ets.insert_new(set, {attribute, [], :accumulate, []}) ||
:ets.update_element(set, attribute, {3, :accumulate})
else
:ets.insert_new(bag, {:warn_attributes, attribute})
:ets.insert_new(set, {attribute, nil, :unset})
:ets.insert_new(set, {attribute, nil, :unset, []})
end
:ok
@@ -1667,7 +1712,7 @@ defmodule Module do
"#{kind} #{name}/#{arity} is private, " <>
"@doc attribute is always discarded for private functions/macros/types"
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
IO.warn(message, %{env | line: line})
end
end
@@ -1695,7 +1740,7 @@ defmodule Module do
def #{name}(...)
'''
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
IO.warn(message, %{env | line: line})
end
signature = merge_signatures(current_sign, signature, 1)
@@ -1717,14 +1762,14 @@ defmodule Module do
defp get_doc_meta(existing_meta, set) do
case :ets.take(set, {:doc, :meta}) do
[{{:doc, :meta}, metadata, _}] -> Map.merge(existing_meta, metadata)
[{{:doc, :meta}, metadata}] -> Map.merge(existing_meta, metadata)
[] -> existing_meta
end
end
defp compile_deprecated(doc_meta, set, bag, name, arity, defaults) do
case :ets.take(set, :deprecated) do
[{:deprecated, reason, _}] when is_binary(reason) ->
[{:deprecated, reason, _, _}] when is_binary(reason) ->
:ets.insert(bag, deprecated_reasons(defaults, name, arity, reason))
Map.put(doc_meta, :deprecated, reason)
@@ -1754,7 +1799,7 @@ defmodule Module do
%{line: line, file: file} = env
case :ets.take(set, :impl) do
[{:impl, value, _}] ->
[{:impl, value, _, _}] ->
impl = {{name, arity}, context, defaults, kind, line, file, value}
:ets.insert(bag, {:impls, impl})
value
@@ -1775,7 +1820,8 @@ defmodule Module do
defp args_count([], total, defaults), do: {total, defaults}
@doc false
def check_behaviours_and_impls(env, _set, bag, all_definitions) do
def check_derive_behaviours_and_impls(env, set, bag, all_definitions) do
check_derive(env, set, bag)
behaviours = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
impls = bag_lookup_element(bag, :impls, 2)
callbacks = check_behaviours(env, behaviours)
@@ -1793,6 +1839,25 @@ defmodule Module do
:ok
end
defp check_derive(env, set, bag) do
case bag_lookup_element(bag, {:accumulate, :derive}, 2) do
[] ->
:ok
_ ->
message =
case :ets.lookup(set, :__struct__) do
[] ->
"warning: module attribute @derive was set but never used (it must come before defstruct)"
_ ->
"warning: module attribute @derive was set after defstruct, all @derive calls must come before defstruct"
end
IO.warn(message, env)
end
end
defp check_behaviours(env, behaviours) do
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
cond do
@@ -1800,18 +1865,18 @@ defmodule Module do
message =
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
acc
not function_exported?(behaviour, :behaviour_info, 1) ->
message =
"module #{inspect(behaviour)} is not a behaviour (in module #{inspect(env.module)})"
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
acc
true ->
:elixir_env.trace({:require, [], behaviour, []}, env)
:elixir_env.trace({:require, [from_macro: true], behaviour, []}, env)
optional_callbacks = behaviour_info(behaviour, :optional_callbacks)
callbacks = behaviour_info(behaviour, :callbacks)
Enum.reduce(callbacks, acc, &add_callback(&1, behaviour, env, optional_callbacks, &2))
@@ -1833,7 +1898,7 @@ defmodule Module do
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
end
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
%{} ->
:ok
@@ -1850,7 +1915,7 @@ defmodule Module do
format_callback(callback, kind, behaviour) <>
" is not implemented (in module #{inspect(env.module)})"
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
{_, wrong_kind, _, _} when kind != wrong_kind ->
message =
@@ -1858,7 +1923,7 @@ defmodule Module do
" was implemented as \"#{wrong_kind}\" but should have been \"#{kind}\" " <>
"(in module #{inspect(env.module)})"
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
_ ->
:ok
@@ -1894,7 +1959,7 @@ defmodule Module do
{:error, message} ->
formatted = format_impl_warning(fa, kind, message)
IO.warn(formatted, Macro.Env.stacktrace(%{env | line: line, file: file}))
IO.warn(formatted, %{env | line: line, file: file})
acc
end
end)
@@ -2010,7 +2075,7 @@ defmodule Module do
"This either means you forgot to add the \"@impl true\" annotation before the " <>
"definition or that you are accidentally overriding this callback"
IO.warn(message, Macro.Env.stacktrace(%{env | line: :elixir_utils.get_line(meta)}))
IO.warn(message, %{env | line: :elixir_utils.get_line(meta)})
end
end
@@ -2050,7 +2115,7 @@ defmodule Module do
@doc false
# Used internally by Kernel's @.
# This function is private and must be used only internally.
def __get_attribute__(module, key, line) when is_atom(key) do
def __get_attribute__(module, key, caller_line, trace?) when is_atom(key) do
assert_not_compiled!(
{:get_attribute, 2},
module,
@@ -2060,23 +2125,25 @@ defmodule Module do
{set, bag} = data_tables_for(module)
case :ets.lookup(set, key) do
[{_, _, :accumulate}] ->
[{_, _, :accumulate, traces}] ->
trace_attribute(trace?, module, traces, set, key, [])
:lists.reverse(bag_lookup_element(bag, {:accumulate, key}, 2))
[{_, val, line}] when is_integer(line) ->
:ets.update_element(set, key, {3, :used})
val
[{_, value, warn_line, traces}] when is_integer(warn_line) ->
trace_attribute(trace?, module, traces, set, key, [{3, :used}])
value
[{_, val, _}] ->
val
[{_, value, _, traces}] ->
trace_attribute(trace?, module, traces, set, key, [])
value
[] when is_integer(line) ->
[] when is_integer(caller_line) ->
# TODO: Consider raising instead of warning on v2.0 as it usually cascades
error_message =
"undefined module attribute @#{key}, " <>
"please remove access to @#{key} or explicitly set it before access"
IO.warn(error_message, attribute_stack(module, line))
IO.warn(error_message, attribute_stack(module, caller_line))
nil
[] ->
@@ -2084,25 +2151,90 @@ defmodule Module do
end
end
defp trace_attribute(module, traces) do
:lists.foreach(
fn {line, lexical_tracker, tracers, aliases} ->
env = %{
Macro.Env.__struct__()
| line: line,
lexical_tracker: lexical_tracker,
module: module,
tracers: tracers
}
:lists.foreach(
fn alias ->
:elixir_env.trace({:alias_reference, [line: line], alias}, env)
end,
aliases
)
end,
traces
)
end
defp trace_attribute(trace?, module, traces, set, key, updates) do
updates =
if trace? and traces != [] do
trace_attribute(module, traces)
updates ++ [{4, []}]
else
updates
end
case updates do
[] -> :ok
_ -> :ets.update_element(set, key, updates)
end
:ok
end
@doc false
# Used internally by Kernel's @.
# This function is private and must be used only internally.
def __put_attribute__(module, key, value, line) when is_atom(key) do
assert_not_readonly!(__ENV__.function, module)
def __put_attribute__(module, key, value, warn_line, traces) when is_atom(key) do
assert_not_readonly!({:put_attribute, 3}, module)
{set, bag} = data_tables_for(module)
value = preprocess_attribute(key, value)
put_attribute(module, key, value, line, set, bag)
put_attribute(module, key, value, warn_line, traces, set, bag)
:ok
end
defp put_attribute(_module, :on_load, value, warn_line, traces, set, bag) do
value =
case value do
_ when is_atom(value) ->
{value, 0}
{atom, 0} = tuple when is_atom(atom) ->
tuple
_ ->
raise ArgumentError,
"@on_load is a built-in module attribute that annotates a function to be invoked " <>
"when the module is loaded. It should be an atom or an {atom, 0} tuple, " <>
"got: #{inspect(value)}"
end
try do
:ets.lookup_element(set, :on_load, 3)
catch
:error, :badarg ->
:ets.insert(set, {:on_load, value, warn_line, traces})
:ets.insert(bag, {:warn_attributes, :on_load})
else
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
end
end
# If any of the doc attributes are called with a keyword list that
# will become documentation metadata. Multiple calls will be merged
# into the same map overriding duplicate keys.
defp put_attribute(module, key, {_, metadata}, line, set, _bag)
defp put_attribute(module, key, {_, metadata}, warn_line, _traces, set, _bag)
when key in [:doc, :typedoc, :moduledoc] and is_list(metadata) do
metadata_map = preprocess_doc_meta(metadata, module, line, %{})
metadata_map = preprocess_doc_meta(metadata, module, warn_line, %{})
case :ets.insert_new(set, {{key, :meta}, metadata_map, line}) do
case :ets.insert_new(set, {{key, :meta}, metadata_map}) do
true ->
:ok
@@ -2114,46 +2246,45 @@ defmodule Module do
# Optimize some attributes by avoiding writing to the attributes key
# in the bag table since we handle them internally.
defp put_attribute(module, key, value, line, set, _bag)
defp put_attribute(module, key, value, warn_line, traces, set, _bag)
when key in [:doc, :typedoc, :moduledoc, :impl, :deprecated] do
value = preprocess_attribute(key, value)
try do
:ets.lookup_element(set, key, 3)
catch
:error, :badarg -> :ok
else
unread_line when is_integer(line) and is_integer(unread_line) ->
unread_line when is_integer(warn_line) and is_integer(unread_line) ->
message = "redefining @#{key} attribute previously set at line #{unread_line}"
IO.warn(message, attribute_stack(module, line))
IO.warn(message, attribute_stack(module, warn_line))
_ ->
:ok
end
:ets.insert(set, {key, value, line})
:ets.insert(set, {key, value, warn_line, traces})
end
defp put_attribute(_module, :on_load, value, line, set, bag) do
try do
:ets.lookup_element(set, :on_load, 3)
catch
:error, :badarg ->
:ets.insert(set, {:on_load, value, line})
:ets.insert(bag, {:warn_attributes, :on_load})
else
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
end
end
defp put_attribute(_module, key, value, warn_line, traces, set, bag) do
value = preprocess_attribute(key, value)
defp put_attribute(_module, key, value, line, set, bag) do
try do
:ets.lookup_element(set, key, 3)
catch
:error, :badarg ->
:ets.insert(set, {key, value, line})
:ets.insert(set, {key, value, warn_line, traces})
:ets.insert(bag, {:warn_attributes, key})
else
:accumulate -> :ets.insert(bag, {{:accumulate, key}, value})
_ -> :ets.insert(set, {key, value, line})
:accumulate ->
if traces != [] do
:ets.update_element(set, key, {4, traces ++ :ets.lookup_element(set, key, 4)})
end
:ets.insert(bag, {{:accumulate, key}, value})
_ ->
:ets.insert(set, {key, value, warn_line, traces})
end
end
@@ -2169,9 +2300,6 @@ defmodule Module do
{line, doc} when is_integer(line) and (is_binary(doc) or doc == false or is_nil(doc)) ->
value
{line, [{key, _} | _]} when is_integer(line) and is_atom(key) ->
value
{line, doc} when is_integer(line) ->
raise ArgumentError,
"@#{key} is a built-in module attribute for documentation. It should be either " <>
@@ -2194,22 +2322,6 @@ defmodule Module do
end
end
defp preprocess_attribute(:on_load, value) do
case value do
_ when is_atom(value) ->
{value, 0}
{atom, 0} = tuple when is_atom(atom) ->
tuple
_ ->
raise ArgumentError,
"@on_load is a built-in module attribute that annotates a function to be invoked " <>
"when the module is loaded. It should be an atom or a {atom, 0} tuple, " <>
"got: #{inspect(value)}"
end
end
defp preprocess_attribute(:impl, value) do
if is_boolean(value) or (is_atom(value) and value != nil) do
value
@@ -2227,6 +2339,9 @@ defmodule Module do
defp preprocess_attribute(:after_compile, atom) when is_atom(atom),
do: {atom, :__after_compile__}
defp preprocess_attribute(:after_verify, atom) when is_atom(atom),
do: {atom, :__after_verify__}
defp preprocess_attribute(:on_definition, atom) when is_atom(atom),
do: {atom, :__on_definition__}
@@ -2347,7 +2462,7 @@ defmodule Module do
defp get_doc_info(table, env) do
case :ets.take(table, :doc) do
[{:doc, {_, _} = pair, _}] ->
[{:doc, {_, _} = pair, _, _}] ->
pair
[] ->
+101 -86
View File
@@ -27,14 +27,21 @@ defmodule Module.ParallelChecker do
Gets the parallel checker data from pdict.
"""
def get do
{_, checker} = :erlang.get(:elixir_checker_info)
checker
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) do
def put(pid, checker) when is_pid(pid) and is_pid(checker) do
:erlang.put(:elixir_checker_info, {pid, checker})
end
@@ -51,21 +58,21 @@ defmodule Module.ParallelChecker do
receive do
{^ref, :cache, ets} ->
loaded_info =
module_map =
if is_map(info) do
cache_from_module_map(ets, info)
info
else
info = File.read!(info)
cache_from_chunk(ets, module, info)
info
info |> File.read!() |> fetch_module_map!(module)
end
cache_from_module_map(ets, module_map)
send(checker, {ref, :cached})
receive do
{^ref, :check} ->
warnings = check_module(module, loaded_info, {checker, ets})
# Set the compiler info so we can collect warnings
:erlang.put(:elixir_compiler_info, {pid, self()})
warnings = check_module(module_map, {checker, ets})
send(pid, {__MODULE__, module, warnings})
send(checker, {__MODULE__, :done})
end
@@ -75,7 +82,8 @@ defmodule Module.ParallelChecker do
end
end)
{spawned, ref}
register(checker, spawned, ref)
:ok
end
@doc """
@@ -86,28 +94,33 @@ defmodule Module.ParallelChecker do
def verify(fun) do
case :erlang.get(:elixir_compiler_info) do
:undefined ->
previous = :erlang.get(:elixir_checker_info)
{:ok, checker} = start_link()
put(self(), checker)
previous = :erlang.put(:elixir_checker_info, {self(), nil})
try do
{result, compile_info} = Enum.unzip(fun.())
_ = verify(checker, compile_info, [])
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
stop(checker)
checker && stop(checker)
end
_ ->
# If we are during compilation, then they will be
# reported to the compiler, which will validate them.
Enum.map(fun.(), &elem(&1, 0))
fun.()
end
end
@@ -116,15 +129,13 @@ defmodule Module.ParallelChecker do
the modules and adds the ExCk chunk to the binaries. Returns the updated
list of warnings from the verification.
"""
@spec verify(pid(), [{pid(), reference()}], [{module(), binary()}]) :: [warning()]
def verify(checker, compiled_info, runtime_files) do
runtime_info =
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
@spec verify(pid(), [{module(), binary()}]) :: [warning()]
def verify(checker, runtime_files) do
for {module, file} <- runtime_files do
spawn({self(), checker}, module, file)
end
modules = compiled_info ++ runtime_info
:gen_server.cast(checker, {:start, modules})
modules = :gen_server.call(checker, :start)
collect_results(modules, [])
end
@@ -132,10 +143,16 @@ defmodule Module.ParallelChecker do
warnings
end
defp collect_results([_ | modules], warnings) do
defp collect_results(modules, warnings) do
receive do
{:warning, file, location, message} ->
file = file && Path.absname(file)
message = :unicode.characters_to_binary(message)
warning = {file, location, message}
collect_results(modules, [warning | warnings])
{__MODULE__, _module, new_warnings} ->
collect_results(modules, new_warnings ++ warnings)
collect_results(tl(modules), new_warnings ++ warnings)
end
end
@@ -195,35 +212,26 @@ defmodule Module.ParallelChecker do
## Module checking
defp check_module(module, info, cache) do
case extract_definitions(module, info) do
{:ok, module, file, definitions, no_warn_undefined} ->
Module.Types.warnings(module, file, definitions, no_warn_undefined, cache)
|> group_warnings()
|> emit_warnings()
defp check_module(module_map, cache) do
%{module: module, file: file, compile_opts: compile_opts, definitions: definitions} =
module_map
:error ->
[]
end
end
defp extract_definitions(module, module_map) when is_map(module_map) do
no_warn_undefined =
module_map.compile_opts
compile_opts
|> extract_no_warn_undefined()
|> merge_compiler_no_warn_undefined()
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
end
warnings =
module
|> Module.Types.warnings(file, definitions, no_warn_undefined, cache)
|> group_warnings()
|> emit_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
module_map
|> Map.get(:after_verify, [])
|> Enum.each(fn {verify_mod, verify_fun} -> apply(verify_mod, verify_fun, [module]) end)
warnings
end
defp extract_no_warn_undefined(compile_opts) do
@@ -236,11 +244,8 @@ 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
@@ -311,14 +316,9 @@ defmodule Module.ParallelChecker do
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']),
with {^module, binary, _filename} <- :code.get_object_code(module),
{:ok, ^module} <- binary |> :beam_lib.info() |> Keyword.fetch(:module),
{:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
cache_chunk(ets, module, contents.exports)
true
@@ -327,16 +327,6 @@ defmodule Module.ParallelChecker do
end
end
defp cache_from_module_map(ets, map) do
exports =
[{{:__info__, 1}, :def}] ++
behaviour_exports(map) ++
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated, :elixir)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
{mode, exports} = info_exports(module)
@@ -367,6 +357,23 @@ defmodule Module.ParallelChecker do
_ -> %{}
end
defp fetch_module_map!(binary, module) when is_binary(binary) do
{: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, [])
module_map
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})
@@ -414,7 +421,11 @@ defmodule Module.ParallelChecker do
end
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module})
:gen_server.call(server, {:unlock, module}, :infinity)
end
defp register(server, pid, ref) do
:gen_server.cast(server, {:register, pid, ref})
end
## Server callbacks
@@ -433,6 +444,20 @@ defmodule Module.ParallelChecker do
{:ok, state}
end
def handle_call(:start, _from, %{ets: ets, modules: modules} = state) do
for {pid, ref} <- modules do
send(pid, {ref, :cache, ets})
end
for {_pid, ref} <- modules do
receive do
{^ref, :cached} -> :ok
end
end
{:reply, modules, run_checkers(state)}
end
def handle_call(:ets, _from, state) do
{:reply, state.ets, state}
end
@@ -465,18 +490,8 @@ defmodule Module.ParallelChecker do
{:stop, :normal, state}
end
def handle_cast({:start, modules}, %{ets: ets} = state) do
for {pid, ref} <- modules do
send(pid, {ref, :cache, ets})
end
for {_pid, ref} <- modules do
receive do
{^ref, :cached} -> :ok
end
end
{:noreply, run_checkers(%{state | modules: modules})}
def handle_cast({:register, pid, ref}, %{modules: modules} = state) do
{:noreply, %{state | modules: [{pid, ref} | modules]}}
end
defp run_checkers(%{modules: []} = state) do
+5 -1
View File
@@ -354,7 +354,7 @@ defmodule Module.Types do
end
defp simplify_type?(type, other) do
map_type?(type) and not map_type?(other)
map_like_type?(type) and not map_like_type?(other)
end
## EXPRESSION FORMATTING
@@ -505,6 +505,10 @@ defmodule Module.Types do
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)
+38 -7
View File
@@ -151,7 +151,7 @@ defmodule Module.Types.Expr do
dynamic_value_pairs =
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
{:ok, type, context} <- unify(args_type, map_type, stack, context) do
{:ok, type, context} <- unify(map_type, args_type, stack, context) do
# Retrieve map type and overwrite with the new value types from the map update
{:map, pairs} = resolve_var(type, context)
@@ -216,6 +216,30 @@ defmodule Module.Types.Expr do
end
end
# cond do pat -> expr end
def of_expr({:cond, _meta, [[{:do, clauses}]]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
case of_expr(head, :dynamic, stack, context) do
{:ok, _, context} ->
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
{:ok, keep_warnings(acc, context)}
end
error ->
# Skip the clause if it the head has an error
if meta[:generated], do: {:ok, acc}, else: error
end
end)
case result do
:ok -> {:ok, :dynamic, context}
:error -> {:error, context}
end
end
# case expr do pat -> expr end
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
@@ -301,7 +325,7 @@ defmodule Module.Types.Expr do
end
# for pat <- expr do expr end
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
def of_expr({:for, _meta, [_ | _] = args} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
@@ -320,7 +344,7 @@ defmodule Module.Types.Expr do
end
# with pat <- expr do expr end
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
def of_expr({:with, _meta, [_ | _] = clauses} = expr, _expected, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
@@ -469,12 +493,19 @@ 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)
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
{:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context),
do: {:ok, keep_warnings(acc, context)}
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
end)
end
+1 -1
View File
@@ -237,7 +237,7 @@ defmodule Module.Types.Of do
def remote(module, fun, arity, meta, context) when is_atom(module) do
# TODO: In the future we may want to warn for modules defined
# in the local context
if Keyword.get(meta, :context_module, false) and context.module != module do
if Keyword.get(meta, :context_module, false) do
context
else
ParallelChecker.preload_module(context.cache, module)
+11 -7
View File
@@ -403,10 +403,8 @@ defmodule Module.Types.Pattern do
# bar, {:ok, baz}
# bar, {:ok, bat}
expanded_args =
args
|> Enum.map(&flatten_union(&1, context))
|> cartesian_product()
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
@@ -425,11 +423,11 @@ defmodule Module.Types.Pattern do
# the type contexts from unifying argument and parameter to
# infer type variables in arguments
result =
flat_map_ok(expanded_args, fn expanded_args ->
flat_map_ok(cartesian_args, fn cartesian_args ->
result =
Enum.flat_map(clauses, fn {params, return} ->
result =
map_ok(Enum.zip(expanded_args, params), fn {arg, param} ->
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}
@@ -453,7 +451,13 @@ defmodule Module.Types.Pattern do
{:ok, returns_contexts} ->
{success_returns, contexts} = Enum.unzip(returns_contexts)
contexts = Enum.concat(contexts)
indexes = Enum.uniq(Enum.flat_map(args, &collect_var_indexes_from_type/1))
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
+13 -8
View File
@@ -43,14 +43,14 @@ defmodule Module.Types.Unify do
{:ok, same, context}
end
def unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
end
def unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
end
def unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
end
def unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
result =
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
@@ -130,10 +130,15 @@ defmodule Module.Types.Unify do
%{^var => {:var, new_var} = var_type} ->
unify_result =
if var_source? do
unify(var_type, type, stack, context)
else
unify(type, var_type, stack, context)
cond do
recursive_type?(var_type, [], context) ->
{:ok, var_type, put_in(context.types[var], var_type)}
var_source? ->
unify(var_type, type, stack, context)
true ->
unify(type, var_type, stack, context)
end
case unify_result do
+30
View File
@@ -257,6 +257,36 @@ 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`.
+7 -2
View File
@@ -29,7 +29,12 @@ defmodule OptionParser do
@type argv :: [String.t()]
@type parsed :: keyword
@type errors :: [{String.t(), String.t() | nil}]
@type options :: [switches: keyword, strict: keyword, aliases: keyword]
@type options :: [
switches: keyword,
strict: keyword,
aliases: keyword,
allow_nonexistent_atoms: boolean
]
defmodule ParseError do
defexception [:message]
@@ -487,7 +492,7 @@ defmodule OptionParser do
Keys must be atoms. Keys with `nil` value are discarded,
boolean values are converted to `--key` or `--no-key`
(if the value is `true` or `false`, respectively),
and all other values are converted using `Kernel.to_string/1`.
and all other values are converted using `to_string/1`.
It is advised to pass to `to_argv/2` the same set of `options`
given to `parse/2`. Some switches can only be reconstructed
+386
View File
@@ -0,0 +1,386 @@
defmodule PartitionSupervisor do
@moduledoc """
A supervisor that starts multiple partitions of the same child.
Certain processes may become bottlenecks in large systems.
If those processes can have their state trivially partitioned,
in a way there is no dependency between them, then they can use
the `PartitionSupervisor` to create multiple isolated and
independent partitions.
Once the `PartitionSupervisor` starts, you can dispatch to its
children using `{:via, PartitionSupervisor, {name, key}}`, where
`name` is the name of the `PartitionSupervisor` and key is used
for routing.
## Example
The `DynamicSupervisor` is a single process responsible for starting
other processes. In some applications, the `DynamicSupervisor` may
become a bottleneck. To address this, you can start multiple instances
of the `DynamicSupervisor` through a `PartitionSupervisor`, and then
pick a "random" instance to start the child on.
Instead of starting a single `DynamicSupervisor`:
children = [
{DynamicSupervisor, name: MyApp.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
and starting children on that dynamic supervisor directly:
DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end})
You can do start the dynamic supervisors under a `PartitionSupervisor`:
children = [
{PartitionSupervisor,
child_spec: DynamicSupervisor,
name: MyApp.DynamicSupervisors}
]
Supervisor.start_link(children, strategy: :one_for_one)
and then:
DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {MyApp.DynamicSupervisors, self()}},
{Agent, fn -> %{} end}
)
In the code above, we start a partition supervisor that will by default
start a dynamic supervisor for each core in your machine. Then, instead
of calling the `DynamicSupervisor` by name, you call it through the
partition supervisor using the `{:via, PartitionSupervisor, {name, key}}`
format. We picked `self()` as the routing key, which means each process
will be assigned one of the existing dynamic supervisors. See `start_link/1`
to see all options supported by the `PartitionSupervisor`.
## Implementation notes
The `PartitionSupervisor` uses either an ETS table or a `Registry` to
manage all of the partitions. Under the hood, the `PartitionSupervisor`
generates a child spec for each partition and then acts as a regular
supervisor. The ID of each child spec is the partition number.
For routing, two strategies are used. If `key` is an integer, it is routed
using `rem(abs(key), partitions)` where `partitions` is the number of
partitions. Otherwise it uses `:erlang.phash2(key, partitions)`.
The particular routing may change in the future, and therefore must not
be relied on. If you want to retrieve a particular PID for a certain key,
you can use `GenServer.whereis({:via, PartitionSupervisor, {name, key}})`.
"""
@behaviour Supervisor
@registry PartitionSupervisor.Registry
@typedoc """
The name of the `PartitionSupervisor`.
"""
@type name :: atom() | {:via, module(), term()}
@doc false
def child_spec(opts) when is_list(opts) do
id =
case Keyword.get(opts, :name, DynamicSupervisor) do
name when is_atom(name) -> name
{:via, _module, name} -> name
end
%{
id: id,
start: {PartitionSupervisor, :start_link, [opts]},
type: :supervisor
}
end
@doc """
Starts a partition supervisor with the given options.
This function is typically not invoked directly, instead it is invoked
when using a `PartitionSupervisor` as a child of another supervisor:
children = [
{PartitionSupervisor, child_spec: SomeChild, name: MyPartitionSupervisor}
]
If the supervisor is successfully spawned, this function returns
`{:ok, pid}`, where `pid` is the PID of the supervisor. If the given name
for the partition supervisor is already assigned to a process,
the function returns `{:error, {:already_started, pid}}`, where `pid`
is the PID of that process.
Note that a supervisor started with this function is linked to the parent
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
## Options
* `:name` - an atom or via tuple representing the name of the partition
supervisor (see `t:name/0`).
* `:partitions` - a positive integer with the number of partitions.
Defaults to `System.schedulers_online()` (typically the number of cores).
* `:strategy` - the restart strategy option, defaults to `:one_for_one`.
You can learn more about strategies in the `Supervisor` module docs.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
* `:with_arguments` - a two-argument anonymous function that allows
the partition to be given to the child starting function. See the
`:with_arguments` section below.
## `:with_arguments`
Sometimes you want each partition to know their partition assigned number.
This can be done with the `:with_arguments` option. This function receives
the list of arguments of the child specification and the partition. It
must return a new list of arguments that will be passed to the child specification
of children.
For example, most processes are started by calling `start_link(opts)`,
where `opts` is a keyword list. You could inject the partition into the
options given to the child:
with_arguments: fn [opts], partition ->
[Keyword.put(opts, :partition, partition)]
end
"""
@doc since: "1.14.0"
@spec start_link(keyword) :: Supervisor.on_start()
def start_link(opts) when is_list(opts) do
name = opts[:name]
unless name do
raise ArgumentError, "the :name option must be given to PartitionSupervisor"
end
{child_spec, opts} = Keyword.pop(opts, :child_spec)
unless child_spec do
raise ArgumentError, "the :child_spec option must be given to PartitionSupervisor"
end
{partitions, opts} = Keyword.pop(opts, :partitions, System.schedulers_online())
unless is_integer(partitions) and partitions >= 1 do
raise ArgumentError,
"the :partitions option must be a positive integer, got: #{inspect(partitions)}"
end
{with_arguments, opts} = Keyword.pop(opts, :with_arguments, fn args, _partition -> args end)
unless is_function(with_arguments, 2) do
raise ArgumentError,
"the :with_arguments option must be a function that receives two arguments, " <>
"the current call arguments and the partition, got: #{inspect(with_arguments)}"
end
%{start: {mod, fun, args}} = map = Supervisor.child_spec(child_spec, [])
modules = map[:modules] || [mod]
children =
for partition <- 0..(partitions - 1) do
args = with_arguments.(args, partition)
unless is_list(args) do
raise "the call to the function in :with_arguments must return a list, got: #{inspect(args)}"
end
start = {__MODULE__, :start_child, [mod, fun, args, name, partition]}
Map.merge(map, %{id: partition, start: start, modules: modules})
end
{init_opts, start_opts} = Keyword.split(opts, [:strategy, :max_seconds, :max_restarts])
Supervisor.start_link(__MODULE__, {name, partitions, children, init_opts}, start_opts)
end
@doc false
def start_child(mod, fun, args, name, partition) do
case apply(mod, fun, args) do
{:ok, pid} ->
register_child(name, partition, pid)
{:ok, pid}
{:ok, pid, info} ->
register_child(name, partition, pid)
{:ok, pid, info}
other ->
other
end
end
defp register_child(name, partition, pid) when is_atom(name) do
:ets.insert(name, {partition, pid})
end
defp register_child({:via, _, _}, partition, pid) do
Registry.register(@registry, {self(), partition}, pid)
end
@impl true
def init({name, partitions, children, init_opts}) do
init_partitions(name, partitions)
Supervisor.init(children, Keyword.put_new(init_opts, :strategy, :one_for_one))
end
defp init_partitions(name, partitions) when is_atom(name) do
:ets.new(name, [:set, :named_table, :protected, read_concurrency: true])
:ets.insert(name, {:partitions, partitions})
end
defp init_partitions({:via, _, _}, partitions) do
child_spec = {Registry, keys: :unique, name: @registry}
unless Process.whereis(@registry) do
Supervisor.start_child(:elixir_sup, child_spec)
end
Registry.register(@registry, self(), partitions)
end
@doc """
Returns the number of partitions for the partition supervisor.
"""
@doc since: "1.14.0"
@spec partitions(name()) :: pos_integer()
def partitions(name) do
{_name, partitions} = name_partitions(name)
partitions
end
# For whereis_name, we want to lookup on GenServer.whereis/1
# just once, so we lookup the name and partitions together.
defp name_partitions(name) when is_atom(name) do
try do
{name, :ets.lookup_element(name, :partitions, 2)}
rescue
_ -> exit({:noproc, {__MODULE__, :partitions, [name]}})
end
end
defp name_partitions(name) when is_tuple(name) do
with pid when is_pid(pid) <- GenServer.whereis(name),
[name_partitions] <- Registry.lookup(@registry, pid) do
name_partitions
else
_ -> exit({:noproc, {__MODULE__, :partitions, [name]}})
end
end
@doc """
Returns a list with information about all children.
This function returns a list of tuples containing:
* `id` - the partition number
* `child` - the PID of the corresponding child process or the
atom `:restarting` if the process is about to be restarted
* `type` - `:worker` or `:supervisor` as defined in the child
specification
* `modules` - as defined in the child specification
"""
@doc since: "1.14.0"
@spec which_children(name()) :: [
# Inlining [module()] | :dynamic here because :supervisor.modules() is not exported
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
]
def which_children(name) when is_atom(name) or elem(name, 0) == :via do
Supervisor.which_children(name)
end
@doc """
Returns a map containing count values for the supervisor.
The map contains the following keys:
* `:specs` - the number of partitions (children processes)
* `:active` - the count of all actively running child processes managed by
this supervisor
* `:supervisors` - the count of all supervisors whether or not the child
process is still alive
* `:workers` - the count of all workers, whether or not the child process
is still alive
"""
@doc since: "1.14.0"
@spec count_children(name()) :: %{
specs: non_neg_integer,
active: non_neg_integer,
supervisors: non_neg_integer,
workers: non_neg_integer
}
def count_children(supervisor) when is_atom(supervisor) do
Supervisor.count_children(supervisor)
end
@doc """
Synchronously stops the given partition supervisor with the given `reason`.
It returns `:ok` if the supervisor terminates with the given
reason. If it terminates with another reason, the call exits.
This function keeps OTP semantics regarding error reporting.
If the reason is any other than `:normal`, `:shutdown` or
`{:shutdown, _}`, an error report is logged.
"""
@doc since: "1.14.0"
@spec stop(name(), reason :: term, timeout) :: :ok
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) when is_atom(supervisor) do
Supervisor.stop(supervisor, reason, timeout)
end
## Via callbacks
@doc false
def whereis_name({name, key}) when is_atom(name) or is_tuple(name) do
{name, partitions} = name_partitions(name)
partition =
if is_integer(key), do: rem(abs(key), partitions), else: :erlang.phash2(key, partitions)
whereis_name(name, partition)
end
defp whereis_name(name, partition) when is_atom(name) do
:ets.lookup_element(name, partition, 2)
end
defp whereis_name(name, partition) when is_pid(name) do
@registry
|> Registry.values({name, partition}, name)
|> List.first(:undefined)
end
@doc false
def send(name_key, msg) do
Kernel.send(whereis_name(name_key), msg)
end
@doc false
def register_name(_, _) do
raise "{:via, PartitionSupervisor, _} cannot be given on registration"
end
@doc false
def unregister_name(_, _) do
raise "{:via, PartitionSupervisor, _} cannot be given on unregistration"
end
end
+93 -14
View File
@@ -3,21 +3,25 @@ defmodule Path do
This module provides conveniences for manipulating or
retrieving file system paths.
The functions in this module may receive a chardata as
argument (i.e. a string or a list of characters / string)
and will always return a string (encoded in UTF-8). If a binary
is given, in whatever encoding, its encoding will be kept.
The functions in this module may receive chardata as
arguments and will always return a string encoded in UTF-8. Chardata
is a string or a list of characters and strings, see `t:IO.chardata/0`.
If a binary is given, in whatever encoding, its encoding will be kept.
The majority of the functions in this module do not
interact with the file system, except for a few functions
that require it (like `wildcard/2` and `expand/1`).
"""
@typedoc """
A path.
"""
@type t :: IO.chardata()
@doc """
Converts the given path to an absolute one. Unlike
`expand/1`, no attempt is made to resolve `..`, `.` or `~`.
Converts the given path to an absolute one.
Unlike `expand/1`, no attempt is made to resolve `..`, `.`, or `~`.
## Examples
@@ -148,8 +152,8 @@ defmodule Path do
defp reverse_maybe_remove_dir_sep(name, _), do: :lists.reverse(name)
@doc """
Converts the path to an absolute one and expands
any `.` and `..` characters and a leading `~`.
Converts the path to an absolute one, expanding
any `.` and `..` components and a leading `~`.
## Examples
@@ -367,6 +371,9 @@ defmodule Path do
iex> Path.basename("foo/bar")
"bar"
iex> Path.basename("lib/module/submodule.ex")
"submodule.ex"
iex> Path.basename("/")
""
@@ -428,10 +435,10 @@ defmodule Path do
The behaviour of this function changed in Erlang/OTP 24 for filenames
starting with a dot and without an extension. For example, for a file
named ".gitignore", `extname/1` now returns an empty string, while it
would return ".gitignore" in previous Erlang/OTP versions. This was
named `.gitignore`, `extname/1` now returns an empty string, while it
would return `".gitignore"` in previous Erlang/OTP versions. This was
done to match the behaviour of `rootname/1`, which would return
".gitignore" as its name (and therefore it cannot also be an extension).
`".gitignore"` as its name (and therefore it cannot also be an extension).
See `basename/1` and `rootname/1` for related functions to extract
information from paths.
@@ -493,6 +500,8 @@ defmodule Path do
This function should be used to convert a list of paths to a path.
Note that any trailing slash is removed when joining.
Raises an error if the given list of paths is empty.
## Examples
iex> Path.join(["~", "foo"])
@@ -532,6 +541,7 @@ defmodule Path do
iex> Path.join(["foo", "bar"], "fiz")
"foobar/fiz"
Use `join/1` if you need to join a list of paths instead.
"""
@spec join(t, t) :: binary
def join(left, right) do
@@ -565,7 +575,7 @@ defmodule Path do
If an empty string is given, returns an empty list.
On Windows, path is split on both "\" and "/" separators
On Windows, path is split on both `"\"` and `"/"` separators
and the driver letter, if there is one, is always returned
in lowercase.
@@ -582,7 +592,6 @@ defmodule Path do
"""
@spec split(t) :: [binary]
def split(path) do
:filename.split(IO.chardata_to_string(path))
end
@@ -658,7 +667,7 @@ defmodule Path do
"""
@spec wildcard(t, keyword) :: [binary]
def wildcard(glob, opts \\ []) do
def wildcard(glob, opts \\ []) when is_list(opts) do
mod = if Keyword.get(opts, :match_dot), do: :file, else: Path.Wildcard
glob
@@ -721,4 +730,74 @@ defmodule Path do
defp major_os_type do
:os.type() |> elem(0)
end
@doc """
Returns a relative path that is protected from directory-traversal attacks.
The given relative path is sanitized by eliminating `..` and `.` components.
This function checks that, after expanding those components, the path is still "safe".
Paths are considered unsafe if either of these is true:
* The path is not relative, such as `"/foo/bar"`.
* A `..` component would make it so that the path would travers up above
the root of `relative_to`.
* A symbolic link in the path points to something above the root of `relative_to`.
## Examples
iex> Path.safe_relative_to("deps/my_dep/app.beam", "deps")
{:ok, "deps/my_dep/app.beam"}
iex> Path.safe_relative_to("deps/my_dep/./build/../app.beam", "deps")
{:ok, "deps/my_dep/app.beam"}
iex> Path.safe_relative_to("my_dep/../..", "deps")
:error
iex> Path.safe_relative_to("/usr/local", ".")
:error
"""
@doc since: "1.14.0"
@spec safe_relative_to(t, t) :: {:ok, binary} | :error
def safe_relative_to(path, relative_to) do
path = IO.chardata_to_string(path)
case :filelib.safe_relative_path(path, relative_to) do
:unsafe -> :error
relative_path -> {:ok, IO.chardata_to_string(relative_path)}
end
end
@doc """
Returns a path relative to the current working directory that is
protected from directory-traversal attacks.
Same as `safe_relative_to/2` with the current working directory as
the second argument. If there is an issue retrieving the current working
directory, this function raises an error.
## Examples
iex> Path.safe_relative("foo")
{:ok, "foo"}
iex> Path.safe_relative("foo/../bar")
{:ok, "bar"}
iex> Path.safe_relative("foo/../..")
:error
iex> Path.safe_relative("/usr/local")
:error
"""
@doc since: "1.14.0"
@spec safe_relative(t) :: {:ok, binary} | :error
def safe_relative(path) do
safe_relative_to(path, File.cwd!())
end
end
+6 -6
View File
@@ -24,7 +24,7 @@ defmodule Port do
receives data from multiple inputs and concatenates them in the output.
After the port was created, we sent it two commands in the form of
messages using `Kernel.send/2`. The first command has the binary payload
messages using `send/2`. The first command has the binary payload
of "hello" and the second has "world".
After sending those two messages, we invoked the IEx helper `flush()`,
@@ -250,7 +250,7 @@ defmodule Port do
"""
@spec info(port) :: keyword | nil
def info(port) do
nillify(:erlang.port_info(port))
nilify(:erlang.port_info(port))
end
@doc """
@@ -270,7 +270,7 @@ defmodule Port do
end
def info(port, item) do
nillify(:erlang.port_info(port, item))
nilify(:erlang.port_info(port, item))
end
@doc """
@@ -323,7 +323,7 @@ defmodule Port do
:erlang.ports()
end
@compile {:inline, nillify: 1}
defp nillify(:undefined), do: nil
defp nillify(other), do: other
@compile {:inline, nilify: 1}
defp nilify(:undefined), do: nil
defp nilify(other), do: other
end
+13 -9
View File
@@ -116,7 +116,7 @@ defmodule Process do
"""
@spec put(term, term) :: term | nil
def put(key, value) do
nillify(:erlang.put(key, value))
nilify(:erlang.put(key, value))
end
@doc """
@@ -136,7 +136,7 @@ defmodule Process do
"""
@spec delete(term) :: term | nil
def delete(key) do
nillify(:erlang.erase(key))
nilify(:erlang.erase(key))
end
@doc """
@@ -383,7 +383,7 @@ defmodule Process do
@type spawn_opt ::
:link
| :monitor
| {:monitor, :erlang.monitor_option()}
| {:monitor, monitor_option()}
| {:priority, :low | :normal | :high}
| {:fullsweep_after, non_neg_integer}
| {:min_heap_size, non_neg_integer}
@@ -392,6 +392,10 @@ defmodule Process do
| {:message_queue_data, :off_heap | :on_heap}
@type spawn_opts :: [spawn_opt]
# TODO: Use :erlang.monitor_option() on Erlang/OTP 24+
@typep monitor_option ::
[alias: :explicit_unalias | :demonitor | :reply_demonitor, tag: term()]
@doc """
Spawns the given function according to the given options.
@@ -646,7 +650,7 @@ defmodule Process do
"""
@spec whereis(atom) :: pid | port | nil
def whereis(name) do
nillify(:erlang.whereis(name))
nilify(:erlang.whereis(name))
end
@doc """
@@ -745,7 +749,7 @@ defmodule Process do
"""
@spec info(pid) :: keyword | nil
def info(pid) do
nillify(:erlang.process_info(pid))
nilify(:erlang.process_info(pid))
end
@doc """
@@ -766,7 +770,7 @@ defmodule Process do
end
def info(pid, spec) when is_atom(spec) or is_list(spec) do
nillify(:erlang.process_info(pid, spec))
nilify(:erlang.process_info(pid, spec))
end
@doc """
@@ -784,7 +788,7 @@ defmodule Process do
@spec hibernate(module, atom, list) :: no_return
defdelegate hibernate(mod, fun_name, args), to: :erlang
@compile {:inline, nillify: 1}
defp nillify(:undefined), do: nil
defp nillify(other), do: other
@compile {:inline, nilify: 1}
defp nilify(:undefined), do: nil
defp nilify(other), do: other
end
+49 -19
View File
@@ -244,8 +244,16 @@ defmodule Protocol do
...
end
Although doing so is not recommended as it may affect your test suite
performance.
If you are using `Mix.install/2`, you can do by passing the `consolidate_protocols`
option:
Mix.install(
deps,
consolidate_protocols: false
)
Although doing so is not recommended as it may affect the performance of
your code.
Finally, note all protocols are compiled with `debug_info` set to `true`,
regardless of the option set by the `elixirc` compiler. The debug info is
@@ -723,14 +731,14 @@ defmodule Protocol do
defp callback_ast_to_fa({kind, {:"::", meta, [{name, _, args}, _return]}, _pos})
when kind in [:callback, :macrocallback] do
[{{name, length(args)}, meta}]
[{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa(
{kind, {:when, _, [{:"::", meta, [{name, _, args}, _return]}, _vars]}, _pos}
)
when kind in [:callback, :macrocallback] do
[{{name, length(args)}, meta}]
[{{name, length(List.wrap(args))}, meta}]
end
defp callback_ast_to_fa({kind, _, _pos}) when kind in [:callback, :macrocallback] do
@@ -747,7 +755,7 @@ defmodule Protocol do
do: :maps.get(fa, metas, [])[:line]
defp warn(message, env, nil) do
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
end
defp warn(message, env, line) when is_integer(line) do
@@ -755,13 +763,20 @@ defmodule Protocol do
IO.warn(message, stacktrace)
end
# TODO: Convert the following warnings into errors future Elixir versions
def __before_compile__(env) do
# Callbacks
callback_metas = callback_metas(env.module, :callback)
callbacks = :maps.keys(callback_metas)
functions = Module.get_attribute(env.module, :__functions__)
if functions == [] do
warn(
"protocols must define at least one function, but none was defined",
env,
nil
)
end
# TODO: Convert the following warnings into errors in future Elixir versions
:lists.map(
fn {name, arity} = fa ->
warn(
@@ -791,7 +806,7 @@ defmodule Protocol do
# Optional Callbacks
optional_callbacks = Module.get_attribute(env.module, :optional_callbacks)
if length(optional_callbacks) > 0 do
if optional_callbacks != [] do
warn(
"cannot define @optional_callbacks inside protocol, all of the protocol definitions are required",
env,
@@ -907,26 +922,40 @@ defmodule Protocol do
do: [:lists.foldl(&{:|, [], [&1, &2]}, head, tail), quote(do: ...)]
@doc false
def __impl__(protocol, opts) do
do_defimpl(protocol, :lists.keysort(1, opts))
def __impl__(protocol, opts, do_block, env) do
opts = Keyword.merge(opts, do_block)
{for, opts} =
Keyword.pop_lazy(opts, :for, fn ->
env.module ||
raise ArgumentError, "defimpl/3 expects a :for option when declared outside a module"
end)
for = Macro.expand_literal(for, %{env | module: Kernel, function: {:defimpl, 3}})
case opts do
[] -> raise ArgumentError, "defimpl expects a do-end block"
[do: block] -> __impl__(protocol, for, block)
_ -> raise ArgumentError, "unknown options given to defimpl, got: #{Macro.to_string(opts)}"
end
end
defp do_defimpl(protocol, do: block, for: for) when is_list(for) do
for f <- for, do: do_defimpl(protocol, do: block, for: f)
defp __impl__(protocol, for, block) when is_list(for) do
for f <- for, do: __impl__(protocol, f, block)
end
defp do_defimpl(protocol, do: block, for: for) do
defp __impl__(protocol, for, block) do
# Unquote the implementation just later
# when all variables will already be injected
# into the module body.
impl =
quote unquote: false do
@doc false
@spec __impl__(:for) :: unquote(for)
@spec __impl__(:target) :: __MODULE__
@spec __impl__(:for) :: unquote(for)
@spec __impl__(:protocol) :: unquote(protocol)
def __impl__(:for), do: unquote(for)
def __impl__(:target), do: __MODULE__
def __impl__(:for), do: unquote(for)
def __impl__(:protocol), do: unquote(protocol)
end
@@ -943,12 +972,12 @@ defmodule Protocol do
@protocol protocol
@for for
unquote(block)
res = unquote(block)
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: @protocol, for: @for]
unquote(impl)
res
end
end
end
@@ -1006,14 +1035,15 @@ defmodule Protocol do
@doc false
def __ensure_defimpl__(protocol, for, env) do
if Protocol.consolidated?(protocol) do
if not Code.get_compiler_option(:ignore_already_consolidated) and
Protocol.consolidated?(protocol) do
message =
"the #{inspect(protocol)} protocol has already been consolidated, an " <>
"implementation for #{inspect(for)} has no effect. If you want to " <>
"implement protocols after compilation or during tests, check the " <>
"\"Consolidation\" section in the Protocol module documentation"
IO.warn(message, Macro.Env.stacktrace(env))
IO.warn(message, env)
end
:ok
+108 -24
View File
@@ -3,17 +3,55 @@ defmodule Range do
Ranges represent a sequence of zero, one or many, ascending
or descending integers with a common difference called step.
Ranges are always inclusive and they may have custom steps.
The most common form of creating and matching on ranges is
via the [`first..last`](`../2`) and [`first..last//step`](`..///3`)
notations, auto-imported from `Kernel`:
iex> 1 in 1..10
true
iex> 5 in 1..10
true
iex> 10 in 1..10
true
Ranges are always inclusive in Elixir. When a step is defined,
integers will only belong to the range if they match the step:
iex> 5 in 1..10//2
true
iex> 4 in 1..10//2
false
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `first >= last`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicit decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
`../0` can also be used as a shortcut to create the range `0..-1//1`,
also known as the full-slice range:
iex> ..
0..-1//1
## Use cases
Ranges typically have two uses in Elixir: as a collection or
to represent a slice of another data structure.
### Ranges as collections
Ranges in Elixir are enumerables and therefore can be used
with the `Enum` module:
iex> Enum.to_list(1..3)
[1, 2, 3]
iex> Enum.to_list(1..3//2)
[1, 3]
iex> Enum.to_list(3..1//-1)
[3, 2, 1]
iex> Enum.to_list(1..5//2)
[1, 3, 5]
Ranges may also have a single element:
@@ -29,27 +67,54 @@ defmodule Range do
iex> Enum.to_list(0..10//-1)
[]
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `first >= last`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicitly decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
The full-slice range, returned by `../0`, is an empty collection:
iex> Enum.to_list(..)
[]
### Ranges as slices
Ranges are also frequently used to slice collections.
You can slice strings or any enumerable:
iex> String.slice("elixir", 1..4)
"lixi"
iex> Enum.slice([0, 1, 2, 3, 4, 5], 1..4)
[1, 2, 3, 4]
In those cases, the first and last values of the range
are mapped to positions in the collections.
If a negative number is given, it maps to a position
from the back:
iex> String.slice("elixir", 1..-2//1)
"lixi"
iex> Enum.slice([0, 1, 2, 3, 4, 5], 1..-2//1)
[1, 2, 3, 4]
The range `0..-1//1`, returned by `../0`, returns the
collection as is, which is why it is called the full-slice
range:
iex> String.slice("elixir", ..)
"elixir"
iex> Enum.slice([0, 1, 2, 3, 4, 5], ..)
[0, 1, 2, 3, 4, 5]
## Definition
An increasing range `first..last//step` is a range from
`first` to `last` increasing by `step` where `step` must be a positive
integer and all values `v` must be `first <= v and v <= last`. Therefore, a range
`10..0//1` is an empty range because there is no value `v`
that is `10 <= v and v <= 0`.
An increasing range `first..last//step` is a range from `first`
to `last` increasing by `step` where `step` must be a positive
integer and all values `v` must be `first <= v and v <= last`.
Therefore, a range `10..0//1` is an empty range because there
is no value `v` that is `10 <= v and v <= 0`.
Similarly, a decreasing range `first..last//step` is a range
from `first` to `last` decreasing by `step` where `step` must be a negative
integer and values `v` must be `first >= v and v >= last`. Therefore, a range
`0..10//-1` is an empty range because there is no value `v`
that is `0 >= v and v >= 10`.
from `first` to `last` decreasing by `step` where `step` must
be a negative integer and values `v` must be `first >= v and v >= last`.
Therefore, a range `0..10//-1` is an empty range because there
is no value `v` that is `0 >= v and v >= 10`.
## Representation
@@ -71,9 +136,8 @@ defmodule Range do
directly but you should not modify nor create ranges by hand.
Instead use the proper operators or `new/2` and `new/3`.
A range implements the `Enumerable` protocol, which means
functions in the `Enum` module can be used to work with
ranges:
Ranges implement the `Enumerable` protocol with memory
efficient versions of all `Enumerable` callbacks:
iex> range = 1..10
1..10
@@ -191,6 +255,26 @@ defmodule Range do
size(Map.put(range, :step, step))
end
@doc """
Shifts a range by the given number of steps.
## Examples
iex> Range.shift(0..10, 1)
1..11
iex> Range.shift(0..10, 2)
2..12
iex> Range.shift(0..10//2, 2)
4..14//2
"""
@doc since: "1.14.0"
def shift(first..last//step, steps_to_shift)
when is_integer(first) and is_integer(last) and is_integer(step) and
is_integer(steps_to_shift) do
new(first + steps_to_shift * step, last + steps_to_shift * step, step)
end
@doc """
Checks if two ranges are disjoint.
@@ -332,7 +416,7 @@ defimpl Enumerable, for: Range do
end
def slice(first.._//step = range) do
{:ok, Range.size(range), &slice(first + &1 * step, step, &2)}
{:ok, Range.size(range), &slice(first + &1 * step, step + &3 - 1, &2)}
end
# TODO: Remove me on v2.0
@@ -341,7 +425,7 @@ defimpl Enumerable, for: Range do
slice(Map.put(range, :step, step))
end
defp slice(current, _step, 1), do: [current]
defp slice(_current, _step, 0), do: []
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
end
+1 -1
View File
@@ -434,7 +434,7 @@ defmodule Record do
if Keyword.has_key?(keyword, :_) do
message = "updating a record with a default (:_) is equivalent to creating a new record"
IO.warn(message, Macro.Env.stacktrace(caller))
IO.warn(message, caller)
create(tag, fields, keyword, caller)
else
updates =
+36 -19
View File
@@ -7,7 +7,7 @@ defmodule Regex do
in the [`:re` module documentation](`:re`).
Regular expressions in Elixir can be created using the sigils
`~r` (see `Kernel.sigil_r/2`) or `~R` (see `Kernel.sigil_R/2`):
`~r` (see `sigil_r/2`) or `~R` (see `sigil_R/2`):
# A simple regular expression that matches foo anywhere in the string
~r/foo/
@@ -38,35 +38,35 @@ defmodule Regex do
The modifiers available when creating a Regex are:
* `unicode` (u) - enables Unicode specific patterns like `\p` and causes
character classes like `\w`, `\W`, `\s`, etc. to also match on Unicode
* `:unicode` (u) - enables Unicode specific patterns like `\p` and causes
character classes like `\w`, `\W`, `\s`, and the like to also match on Unicode
(see examples below in "Character classes"). It expects valid Unicode
strings to be given on match
* `caseless` (i) - adds case insensitivity
* `:caseless` (i) - adds case insensitivity
* `dotall` (s) - causes dot to match newlines and also set newline to
* `:dotall` (s) - causes dot to match newlines and also set newline to
anycrlf; the new line setting can be overridden by setting `(*CR)` or
`(*LF)` or `(*CRLF)` or `(*ANY)` according to `:re` documentation
* `multiline` (m) - causes `^` and `$` to mark the beginning and end of
* `:multiline` (m) - causes `^` and `$` to mark the beginning and end of
each line; use `\A` and `\z` to match the end or beginning of the string
* `extended` (x) - whitespace characters are ignored except when escaped
* `:extended` (x) - whitespace characters are ignored except when escaped
and allow `#` to delimit comments
* `firstline` (f) - forces the unanchored pattern to match before or at the
* `:firstline` (f) - forces the unanchored pattern to match before or at the
first newline, though the matched text may continue over the newline
* `ungreedy` (U) - inverts the "greediness" of the regexp
* `:ungreedy` (U) - inverts the "greediness" of the regexp
(the previous `r` option is deprecated in favor of `U`)
The options not available are:
* `anchored` - not available, use `^` or `\A` instead
* `dollar_endonly` - not available, use `\z` instead
* `no_auto_capture` - not available, use `?:` instead
* `newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or
* `:anchored` - not available, use `^` or `\A` instead
* `:dollar_endonly` - not available, use `\z` instead
* `:no_auto_capture` - not available, use `?:` instead
* `:newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or
`(*ANYCRLF)` or `(*ANY)` at the beginning of the regexp according to the
`:re` documentation
@@ -155,7 +155,7 @@ defmodule Regex do
defstruct re_pattern: nil, source: "", opts: "", re_version: ""
@type t :: %__MODULE__{re_pattern: term, source: binary, opts: binary}
@type t :: %__MODULE__{re_pattern: term, source: binary, opts: binary | [term]}
defmodule CompileError do
defexception message: "regex could not be compiled"
@@ -166,8 +166,8 @@ defmodule Regex do
The given options can either be a binary with the characters
representing the same regex options given to the
`~r` (see `Kernel.sigil_r/2`) sigil, or a list of options, as
expected by the Erlang's `:re` module.
`~r` (see `sigil_r/2`) sigil, or a list of options, as
expected by the Erlang's [`:re`](`:re`) module.
It returns `{:ok, regex}` in case of success,
`{:error, reason}` otherwise.
@@ -180,6 +180,12 @@ defmodule Regex do
iex> Regex.compile("*foo")
{:error, {'nothing to repeat', 0}}
iex> Regex.compile("foo", "i")
{:ok, ~r/foo/i}
iex> Regex.compile("foo", [:caseless])
{:ok, Regex.compile!("foo", [:caseless])}
"""
@spec compile(binary, binary | [term]) :: {:ok, t} | {:error, any}
def compile(source, options \\ "") when is_binary(source) do
@@ -203,6 +209,7 @@ defmodule Regex do
defp compile(source, opts, doc_opts, version) do
case :re.compile(source, opts) do
{:ok, re_pattern} ->
doc_opts = format_doc_opts(doc_opts, opts)
{:ok, %Regex{re_pattern: re_pattern, re_version: version, source: source, opts: doc_opts}}
error ->
@@ -210,6 +217,10 @@ defmodule Regex do
end
end
defp format_doc_opts(_doc_opts = "", _opts = []), do: ""
defp format_doc_opts(_doc_opts = "", opts), do: opts
defp format_doc_opts(doc_opts, _opts), do: doc_opts
@doc """
Compiles the regular expression and raises `Regex.CompileError` in case of errors.
"""
@@ -274,7 +285,7 @@ defmodule Regex do
iex> Regex.match?(~r/foo/, "bar")
false
Elixir also provides `Kernel.=~/2` and `String.match?/2` as
Elixir also provides text-based match operator `=~/2` and function `String.match?/2` as
an alternative to test strings against regular expressions and
strings.
"""
@@ -384,15 +395,21 @@ defmodule Regex do
end
@doc """
Returns the regex options as a string.
Returns the regex options, as a string or list depending on how
it was compiled.
See the documentation of `Regex.compile/2` for more information.
## Examples
iex> Regex.opts(~r/foo/m)
"m"
iex> Regex.opts(Regex.compile!("foo", [:caseless]))
[:caseless]
"""
@spec opts(t) :: String.t()
@spec opts(t) :: String.t() | [term]
def opts(%Regex{opts: opts}) do
opts
end
+55 -16
View File
@@ -47,9 +47,9 @@ defmodule Registry do
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
name = {:via, Registry, {Registry.ViaTest, "agent", :hello}}
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
{:ok, agent_pid} = Agent.start_link(fn -> 0 end, name: name)
Registry.lookup(Registry.ViaTest, "agent")
#=> [{self(), :hello}]
#=> [{agent_pid, :hello}]
To this point, we have been starting `Registry` using `start_link/1`.
Typically the registry is started as part of a supervision tree though:
@@ -165,7 +165,7 @@ defmodule Registry do
the value from the registry and sending it a message. Many parts of the standard
library are designed to cope with that, such as `Process.monitor/1` which will
deliver the `:DOWN` message immediately if the monitored process is already dead
and `Kernel.send/2` which acts as a no-op for dead processes.
and `send/2` which acts as a no-op for dead processes.
## ETS
@@ -260,6 +260,10 @@ defmodule Registry do
end
end
def send({registry, key, _value}, msg) do
Registry.send({registry, key}, msg)
end
@doc false
def unregister_name({registry, key}), do: unregister(registry, key)
def unregister_name({registry, key, _value}), do: unregister(registry, key)
@@ -1274,7 +1278,7 @@ defmodule Registry do
## Examples
This example shows how to get everything from the registry.
This example shows how to get everything from the registry:
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
@@ -1282,7 +1286,7 @@ defmodule Registry do
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
[{"world", self(), :value}, {"hello", self(), :value}]
Get all keys in the registry.
Get all keys in the registry:
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
@@ -1295,17 +1299,7 @@ defmodule Registry do
@spec select(registry, spec) :: [term]
def select(registry, spec)
when is_atom(registry) and is_list(spec) do
spec =
for part <- spec do
case part do
{{key, pid, value}, guards, select} ->
{{key, {pid, value}}, guards, select}
_ ->
raise ArgumentError,
"invalid match specification in Registry.select/2: #{inspect(spec)}"
end
end
spec = group_match_headers(spec, __ENV__.function)
case key_info!(registry) do
{_kind, partitions, nil} ->
@@ -1318,6 +1312,51 @@ defmodule Registry do
end
end
@doc """
Works like `select/2`, but only returns the number of matching records.
## Examples
In the example below we register the current process under different
keys in a unique registry but with the same value:
iex> Registry.start_link(keys: :unique, name: Registry.CountSelectTest)
iex> {:ok, _} = Registry.register(Registry.CountSelectTest, "hello", :value)
iex> {:ok, _} = Registry.register(Registry.CountSelectTest, "world", :value)
iex> Registry.count_select(Registry.CountSelectTest, [{{:_, :_, :value}, [], [true]}])
2
"""
@doc since: "1.14.0"
@spec count_select(registry, spec) :: non_neg_integer()
def count_select(registry, spec)
when is_atom(registry) and is_list(spec) do
spec = group_match_headers(spec, __ENV__.function)
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
count = :ets.select_count(key_ets!(registry, partition_index), spec)
acc + count
end)
{_kind, 1, key_ets} ->
:ets.select_count(key_ets, spec)
end
end
defp group_match_headers(spec, {fun, arity}) do
for part <- spec do
case part do
{{key, pid, value}, guards, select} ->
{{key, {pid, value}}, guards, select}
_ ->
raise ArgumentError,
"invalid match specification in Registry.#{fun}/#{arity}: #{inspect(spec)}"
end
end
end
## Helpers
@compile {:inline, hash: 2}
+138 -46
View File
@@ -186,6 +186,9 @@ defmodule Stream do
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list()
[[1, 2, 3], [4, 5, 6]]
iex> Stream.chunk_every([1, 2, 3, 4], 3, 3, Stream.cycle([0])) |> Enum.to_list()
[[1, 2, 3], [4, 0, 0]]
"""
@doc since: "1.5.0"
@spec chunk_every(Enumerable.t(), pos_integer, pos_integer, Enumerable.t() | :discard) ::
@@ -417,10 +420,47 @@ defmodule Stream do
lazy(enum, true, fn f1 -> R.drop_while(fun, f1) end)
end
@doc """
Duplicates the given element `n` times in a stream.
`n` is an integer greater than or equal to `0`.
If `n` is `0`, an empty stream is returned.
## Examples
iex> stream = Stream.duplicate("hello", 0)
iex> Enum.to_list(stream)
[]
iex> stream = Stream.duplicate("hi", 1)
iex> Enum.to_list(stream)
["hi"]
iex> stream = Stream.duplicate("bye", 2)
iex> Enum.to_list(stream)
["bye", "bye"]
iex> stream = Stream.duplicate([1, 2], 3)
iex> Enum.to_list(stream)
[[1, 2], [1, 2], [1, 2]]
"""
@doc since: "1.14.0"
@spec duplicate(any, non_neg_integer) :: Enumerable.t()
def duplicate(value, n) when is_integer(n) and n >= 0 do
unfold(n, fn
0 -> nil
remaining -> {value, remaining - 1}
end)
end
@doc """
Executes the given function for each element.
Useful for adding side effects (like printing) to a stream.
The values in the stream do not change, therefore this
function is useful for adding side effects (like printing)
to a stream. See `map/2` if producing a different stream
is desired.
## Examples
@@ -800,10 +840,10 @@ defmodule Stream do
@doc """
Transforms an existing stream.
It expects an accumulator and a function that receives each stream element
and an accumulator. It must return a tuple, where the first element is a new
stream (often a list) or the atom `:halt`, and the second element is the
accumulator to be used by the next element, if any, in both cases.
It expects an accumulator and a function that receives two arguments,
the stream element and the updated accumulator. It must return a tuple,
where the first element is a new stream (often a list) or the atom `:halt`,
and the second element is the accumulator to be used by the next element.
Note: this function is equivalent to `Enum.flat_map_reduce/3`, except this
function does not return the accumulator once the stream is processed.
@@ -822,44 +862,72 @@ defmodule Stream do
iex> Enum.to_list(stream)
[1001, 1002, 1003]
`Stream.transform/5` further generalizes this function to allow wrapping
around resources.
"""
@spec transform(Enumerable.t(), acc, fun) :: Enumerable.t()
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
acc: any
def transform(enum, acc, reducer) when is_function(reducer, 2) do
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil)
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil, fn acc -> acc end)
end
@doc """
Transforms an existing stream with function-based start and finish.
The accumulator is only calculated when transformation starts. It also
allows an after function to be given which is invoked when the stream
halts or completes.
Similar to `Stream.transform/5`, except `last_fun` is not supplied.
This function can be seen as a combination of `Stream.resource/3` with
`Stream.transform/3`.
"""
@spec transform(Enumerable.t(), (() -> acc), fun, (acc -> term)) :: Enumerable.t()
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
@spec transform(Enumerable.t(), start_fun, reducer, after_fun) :: Enumerable.t()
when start_fun: (() -> acc),
reducer: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
after_fun: (acc -> term),
acc: any
def transform(enum, start_fun, reducer, after_fun)
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(after_fun, 1) do
&do_transform(enum, start_fun, reducer, &1, &2, after_fun)
&do_transform(enum, start_fun, reducer, &1, &2, nil, after_fun)
end
defp do_transform(enumerables, user_acc, user, inner_acc, fun, after_fun) do
@doc """
Transforms an existing stream with function-based start, last, and after
callbacks.
Once transformation starts, `start_fun` is invoked to compute the initial
accumulator. Then, for each element in the enumerable, the `reducer` function
is invoked with the element and the accumulator, returning new elements and a
new accumulator, as in `transform/3`.
Once the collection is done, `last_fun` is invoked with the accumulator to
emit any remaining items. Then `after_fun` is invoked, to close any resource,
but not emitting any new items. `last_fun` is only invoked if the given
enumerable terminates successfully (either because it is done or it halted
itself). `after_fun` is always invoked, therefore `after_fun` must be the
one used for closing resources.
"""
@spec transform(Enumerable.t(), start_fun, reducer, last_fun, after_fun) :: Enumerable.t()
when start_fun: (() -> acc),
reducer: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
last_fun: (acc -> {Enumerable.t(), acc} | {:halt, acc}),
after_fun: (acc -> term),
acc: any
def transform(enum, start_fun, reducer, last_fun, after_fun)
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(last_fun, 1) and
is_function(after_fun, 1) do
&do_transform(enum, start_fun, reducer, &1, &2, last_fun, after_fun)
end
defp do_transform(enumerables, user_acc, user, inner_acc, fun, last_fun, after_fun) do
inner = &do_transform_each(&1, &2, fun)
step = &do_transform_step(&1, &2)
next = &Enumerable.reduce(enumerables, &1, step)
funs = {user, fun, inner, after_fun}
funs = {user, fun, inner, last_fun, after_fun}
do_transform(user_acc.(), :cont, next, inner_acc, funs)
end
defp do_transform(user_acc, _next_op, next, {:halt, inner_acc}, funs) do
{_, _, _, after_fun} = funs
{_, _, _, _, after_fun} = funs
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
{:halted, inner_acc}
end
@@ -867,72 +935,99 @@ defmodule Stream do
{:suspended, inner_acc, &do_transform(user_acc, next_op, next, &1, funs)}
end
defp do_transform(user_acc, :halt, _next, {_, inner_acc}, funs) do
{_, _, _, after_fun} = funs
do_after(after_fun, user_acc)
{:halted, inner_acc}
end
defp do_transform(user_acc, :cont, next, inner_acc, funs) do
{_, _, _, after_fun} = funs
{_, _, _, _, after_fun} = funs
try do
next.({:cont, []})
catch
kind, reason ->
do_after(after_fun, user_acc)
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, vals, next} ->
do_transform_user(:lists.reverse(vals), user_acc, :cont, next, inner_acc, funs)
{_, vals} ->
do_transform_user(:lists.reverse(vals), user_acc, :halt, next, inner_acc, funs)
do_transform_user(:lists.reverse(vals), user_acc, :last, next, inner_acc, funs)
end
end
defp do_transform(user_acc, :last, next, inner_acc, funs) do
{_, _, _, last_fun, after_fun} = funs
if last_fun do
try do
last_fun.(user_acc)
catch
kind, reason ->
next.({:halt, []})
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
result -> do_transform_result(result, [], :halt, next, inner_acc, funs)
end
else
do_transform(user_acc, :halt, next, inner_acc, funs)
end
end
defp do_transform(user_acc, :halt, _next, inner_acc, funs) do
{_, _, _, _, after_fun} = funs
after_fun.(user_acc)
{:halted, elem(inner_acc, 1)}
end
defp do_transform_user([], user_acc, next_op, next, inner_acc, funs) do
do_transform(user_acc, next_op, next, inner_acc, funs)
end
defp do_transform_user([val | vals], user_acc, next_op, next, inner_acc, funs) do
{user, fun, inner, after_fun} = funs
{user, _, _, _, after_fun} = funs
try do
user.(val, user_acc)
catch
kind, reason ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
result -> do_transform_result(result, vals, next_op, next, inner_acc, funs)
end
end
defp do_transform_result(result, vals, next_op, next, inner_acc, funs) do
{_, fun, inner, _, after_fun} = funs
case result do
{[], user_acc} ->
do_transform_user(vals, user_acc, next_op, next, inner_acc, funs)
{list, user_acc} when is_list(list) ->
reduce = &Enumerable.List.reduce(list, &1, fun)
do_list_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs)
do_transform_inner_list(vals, user_acc, next_op, next, inner_acc, reduce, funs)
{:halt, user_acc} ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
{:halted, elem(inner_acc, 1)}
{other, user_acc} ->
reduce = &Enumerable.reduce(other, &1, inner)
do_enum_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs)
do_transform_inner_enum(vals, user_acc, next_op, next, inner_acc, reduce, funs)
end
end
defp do_list_transform(vals, user_acc, next_op, next, inner_acc, reduce, funs) do
{_, _, _, after_fun} = funs
defp do_transform_inner_list(vals, user_acc, next_op, next, inner_acc, reduce, funs) do
{_, _, _, _, after_fun} = funs
try do
reduce.(inner_acc)
catch
kind, reason ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:done, acc} ->
@@ -940,24 +1035,24 @@ defmodule Stream do
{:halted, acc} ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
{:halted, acc}
{:suspended, acc, continuation} ->
resume = &do_list_transform(vals, user_acc, next_op, next, &1, continuation, funs)
resume = &do_transform_inner_list(vals, user_acc, next_op, next, &1, continuation, funs)
{:suspended, acc, resume}
end
end
defp do_enum_transform(vals, user_acc, next_op, next, {op, inner_acc}, reduce, funs) do
{_, _, _, after_fun} = funs
defp do_transform_inner_enum(vals, user_acc, next_op, next, {op, inner_acc}, reduce, funs) do
{_, _, _, _, after_fun} = funs
try do
reduce.({op, [:outer | inner_acc]})
catch
kind, reason ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
else
# Only take into account outer halts when the op is not halt itself.
@@ -967,21 +1062,18 @@ defmodule Stream do
{:halted, [_ | acc]} ->
next.({:halt, []})
do_after(after_fun, user_acc)
after_fun.(user_acc)
{:halted, acc}
{:done, [_ | acc]} ->
do_transform_user(vals, user_acc, next_op, next, {:cont, acc}, funs)
{:suspended, [_ | acc], continuation} ->
resume = &do_enum_transform(vals, user_acc, next_op, next, &1, continuation, funs)
resume = &do_transform_inner_enum(vals, user_acc, next_op, next, &1, continuation, funs)
{:suspended, acc, resume}
end
end
defp do_after(nil, _user_acc), do: :ok
defp do_after(fun, user_acc), do: fun.(user_acc)
defp do_transform_each(x, [:outer | acc], f) do
case f.(x, acc) do
{:halt, res} -> {:halt, [:inner | res]}
@@ -1188,7 +1280,7 @@ defmodule Stream do
enumerable, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put into a list which is then passed to
the 1-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
the one-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
`zip_fun`, and so on until any one of the enums in `enumerables` completes.
Returns a new enumerable with the results of calling `zip_fun`.
+332 -190
View File
@@ -17,6 +17,9 @@ defmodule String do
iex> "hello" <> " " <> "world"
"hello world"
The functions in this module act according to
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
## Interpolation
Strings in Elixir also support interpolation. This allows
@@ -37,7 +40,7 @@ defmodule String do
"2 + 2 = 4"
In case the value you want to interpolate cannot be
converted to a string, because it doesn't have an human
converted to a string, because it doesn't have a human
textual representation, a protocol error will be raised.
## Escape characters
@@ -45,6 +48,7 @@ defmodule String do
Besides allowing double-quotes to be escaped with a backslash,
strings also support the following escape characters:
* `\0` - Null byte
* `\a` - Bell
* `\b` - Backspace
* `\t` - Horizontal tab
@@ -53,7 +57,9 @@ defmodule String do
* `\f` - Form feed
* `\r` - Carriage return
* `\e` - Command Escape
* `\s` - Space
* `\#` - Returns the `#` character itself, skipping interpolation
* `\\` - Single backslash
* `\xNN` - A byte represented by the hexadecimal `NN`
* `\uNNNN` - A Unicode code point represented by `NNNN`
@@ -66,29 +72,82 @@ defmodule String do
low-level manipulations of string, so let's explore them in
detail next.
## Code points and grapheme cluster
## Unicode and code points
The functions in this module act according to
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
In order to facilitate meaningful communication between computers
across multiple languages, a standard is required so that the ones
and zeros on one machine mean the same thing when they are transmitted
to another. The Unicode Standard acts as an official registry of
virtually all the characters we know: this includes characters from
classical and historical texts, emoji, and formatting and control
characters as well.
As per the standard, a code point is a single Unicode Character,
which may be represented by one or more bytes.
Unicode organizes all of the characters in its repertoire into code
charts, and each character is given a unique numerical index. This
numerical index is known as a Code Point.
For example, although the code point "é" is a single character,
its underlying representation uses two bytes:
In Elixir you can use a `?` in front of a character literal to reveal
its code point:
iex> String.length("é")
1
iex> byte_size("é")
2
iex> ?a
97
iex> ?ł
322
Furthermore, this module also presents the concept of grapheme cluster
(from now on referenced as graphemes). Graphemes can consist of multiple
code points that may be perceived as a single character by readers. For
example, "é" can be represented either as a single "e with acute" code point
or as the letter "e" followed by a "combining acute accent" (two code points):
Note that most Unicode code charts will refer to a code point by its
hexadecimal (hex) representation, e.g. `97` translates to `0061` in hex,
and we can represent any Unicode character in an Elixir string by
using the `\u` escape character followed by its code point number:
iex> "\u0061" === "a"
true
iex> 0x0061 = 97 = ?a
97
The hex representation will also help you look up information about a
code point, e.g. [https://codepoints.net/U+0061](https://codepoints.net/U+0061)
has a data sheet all about the lower case `a`, a.k.a. code point 97.
Remember you can get the hex presentation of a number by calling
`Integer.to_string/2`:
iex> Integer.to_string(?a, 16)
"61"
## UTF-8 encoded and encodings
Now that we understand what the Unicode standard is and what code points
are, we can finally talk about encodings. Whereas the code point is **what**
we store, an encoding deals with **how** we store it: encoding is an
implementation. In other words, we need a mechanism to convert the code
point numbers into bytes so they can be stored in memory, written to disk, and such.
Elixir uses UTF-8 to encode its strings, which means that code points are
encoded as a series of 8-bit bytes. UTF-8 is a **variable width** character
encoding that uses one to four bytes to store each code point. It is capable
of encoding all valid Unicode code points. Let's see an example:
iex> string = "héllo"
"héllo"
iex> String.length(string)
5
iex> byte_size(string)
6
Although the string above has 5 characters, it uses 6 bytes, as two bytes
are used to represent the character `é`.
## Grapheme clusters
This module also works with the concept of grapheme cluster
(from now on referenced as graphemes). Graphemes can consist
of multiple code points that may be perceived as a single character
by readers. For example, "é" can be represented either as a single
"e with acute" code point, as seen above in the string `"héllo"`,
or as the letter "e" followed by a "combining acute accent"
(two code points):
iex> string = "\u0065\u0301"
"é"
iex> byte_size(string)
3
iex> String.length(string)
@@ -98,14 +157,13 @@ defmodule String do
iex> String.graphemes(string)
["é"]
Although the example above is made of two characters, it is
perceived by users as one.
Although it looks visually the same as before, the example above
is made of two characters, it is perceived by users as one.
Graphemes can also be two characters that are interpreted
as one by some languages. For example, some languages may
consider "ch" as a single character. However, since this
information depends on the locale, it is not taken into account
by this module.
Graphemes can also be two characters that are interpreted as one
by some languages. For example, some languages may consider "ch"
as a single character. However, since this information depends on
the locale, it is not taken into account by this module.
In general, the functions in this module rely on the Unicode
Standard, but do not contain any of the locale specific behaviour.
@@ -135,98 +193,31 @@ defmodule String do
* Plus a number of functions for working with binaries (bytes)
in the [`:binary` module](`:binary`)
There are many situations where using the `String` module can
be avoided in favor of binary functions or pattern matching.
For example, imagine you have a string `prefix` and you want to
remove this prefix from another string named `full`.
A `utf8` modifier is also available inside the binary syntax `<<>>`.
It can be used to match code points out of a binary/string:
One may be tempted to write:
iex> <<eacute::utf8>> = "é"
iex> eacute
233
iex> take_prefix = fn full, prefix ->
...> base = String.length(prefix)
...> String.slice(full, base, String.length(full) - base)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
You can also fully convert a string into a list of integer code points,
known as "charlists" in Elixir, by calling `String.to_charlist/1`:
Although the function above works, it performs poorly. To
calculate the length of the string, we need to traverse it
fully, so we traverse both `prefix` and `full` strings, then
slice the `full` one, traversing it again.
iex> String.to_charlist("héllo")
[104, 233, 108, 108, 111]
A first attempt at improving it could be with ranges:
If you would rather see the underlying bytes of a string, instead of
its codepoints, a common trick is to concatenate the null byte `<<0>>`
to it:
iex> take_prefix = fn full, prefix ->
...> base = String.length(prefix)
...> String.slice(full, base..-1)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
iex> "héllo" <> <<0>>
<<104, 195, 169, 108, 108, 111, 0>>
While this is much better (we don't traverse `full` twice),
it could still be improved. In this case, since we want to
extract a substring from a string, we can use `Kernel.byte_size/1`
and `Kernel.binary_part/3` as there is no chance we will slice in
the middle of a code point made of more than one byte:
Alternatively, you can view a string's binary representation by
passing an option to `IO.inspect/2`:
iex> take_prefix = fn full, prefix ->
...> base = byte_size(prefix)
...> binary_part(full, base, byte_size(full) - base)
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
Or simply use pattern matching:
iex> take_prefix = fn full, prefix ->
...> base = byte_size(prefix)
...> <<_::binary-size(base), rest::binary>> = full
...> rest
...> end
iex> take_prefix.("Mr. John", "Mr. ")
"John"
On the other hand, if you want to dynamically slice a string
based on an integer value, then using `String.slice/3` is the
best option as it guarantees we won't incorrectly split a valid
code point into multiple bytes.
## Integer code points
Although code points are represented as integers, this module
represents code points in their encoded format as strings.
For example:
iex> String.codepoints("olá")
["o", "l", "á"]
There are a couple of ways to retrieve the character code point.
One may use the `?` construct:
iex> ?o
111
iex> ?á
225
Or also via pattern matching:
iex> <<aacute::utf8>> = "á"
iex> aacute
225
As we have seen above, code points can be inserted into
a string by their hexadecimal code:
iex> "ol\u00E1"
"olá"
Finally, to convert a String into a list of integer
code points, known as "charlists" in Elixir, you can call
`String.to_charlist`:
iex> String.to_charlist("olá")
[111, 108, 225]
IO.inspect("héllo", binaries: :as_binaries)
#=> <<104, 195, 169, 108, 108, 111>>
## Self-synchronization
@@ -283,8 +274,23 @@ defmodule String do
@typedoc "Multiple code points that may be perceived as a single character by readers"
@type grapheme :: t
@typedoc "Pattern used in functions like `replace/4` and `split/3`"
@type pattern :: t | [t] | :binary.cp()
@typedoc """
Pattern used in functions like `replace/4` and `split/3`.
It must be one of:
* a string
* an empty list
* a list containing non-empty strings
* a compiled search pattern created by `:binary.compile_pattern/1`
"""
# TODO: Replace "nonempty_binary :: <<_::8, _::_*8>>" with "nonempty_binary()"
# when minimum requirement is >= OTP 24.
@type pattern ::
t()
| [nonempty_binary :: <<_::8, _::_*8>>]
| (compiled_search_pattern :: :binary.cp())
@conditional_mappings [:greek, :turkic]
@@ -486,6 +492,14 @@ defmodule String do
end
end
def split(string, [], options) when is_binary(string) and is_list(options) do
if string == "" and Keyword.get(options, :trim, false) do
[]
else
[string]
end
end
def split(string, pattern, options) when is_binary(string) and is_list(options) do
parts = Keyword.get(options, :parts, :infinity)
trim = Keyword.get(options, :trim, false)
@@ -575,6 +589,14 @@ defmodule String do
end
end
def splitter(string, [], options) when is_binary(string) and is_list(options) do
if string == "" and Keyword.get(options, :trim, false) do
Stream.duplicate(string, 0)
else
Stream.duplicate(string, 1)
end
end
def splitter(string, pattern, options) when is_binary(string) and is_list(options) do
pattern = maybe_compile_pattern(pattern)
trim = Keyword.get(options, :trim, false)
@@ -962,6 +984,8 @@ defmodule String do
iex> String.replace_leading("hello hello world", "hello ", "ola ")
"ola ola world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_leading(t, t, t) :: t
def replace_leading(string, match, replacement)
@@ -1019,6 +1043,8 @@ defmodule String do
iex> String.replace_trailing("hello world world", " world", " mundo")
"hello mundo mundo"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_trailing(t, t, t) :: t
def replace_trailing(string, match, replacement)
@@ -1079,6 +1105,8 @@ defmodule String do
iex> String.replace_prefix("world", "", "hello ")
"hello world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_prefix(t, t, t) :: t
def replace_prefix(string, match, replacement)
@@ -1119,6 +1147,8 @@ defmodule String do
iex> String.replace_suffix("hello", "", " world")
"hello world"
This function can replace across grapheme boundaries. See `replace/3`
for more information and examples.
"""
@spec replace_suffix(t, t, t) :: t
def replace_suffix(string, match, replacement)
@@ -1472,6 +1502,20 @@ defmodule String do
iex> String.replace("ELIXIR", "", "")
"ELIXIR"
Be aware that this function can replace within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following will replace only the letter "e",
moving the accent to the letter "o":
iex> String.replace(String.normalize("é", :nfd), "e", "o")
"ó"
However, if "é" is represented by the single character "e with acute"
accent, then it won't be replaced at all:
iex> String.replace(String.normalize("é", :nfc), "e", "o")
"é"
"""
@spec replace(t, pattern | Regex.t(), t | (t -> t | iodata), keyword) :: t
def replace(subject, pattern, replacement, options \\ [])
@@ -1489,6 +1533,10 @@ defmodule String do
subject
end
defp replace_guarded(subject, [], _, _) do
subject
end
defp replace_guarded(subject, "", replacement_binary, options)
when is_binary(replacement_binary) do
if Keyword.get(options, :global, true) do
@@ -2027,7 +2075,8 @@ defmodule String do
Remember this function works with Unicode graphemes and considers
the slices to represent grapheme offsets. If you want to split
on raw bytes, check `Kernel.binary_part/3` instead.
on raw bytes, check `Kernel.binary_part/3` or `Kernel.binary_slice/3`
instead.
## Examples
@@ -2040,19 +2089,19 @@ defmodule String do
iex> String.slice("elixir", 10, 3)
""
If the start position is negative, it is normalized
against the string length and clamped to 0:
iex> String.slice("elixir", -4, 4)
"ixir"
iex> String.slice("elixir", -10, 3)
""
"eli"
iex> String.slice("a", 0, 1500)
"a"
If start is more than the string length, an empty
string is returned:
iex> String.slice("a", 1, 1500)
""
iex> String.slice("a", 2, 1500)
iex> String.slice("elixir", 10, 1500)
""
"""
@@ -2071,12 +2120,8 @@ defmodule String do
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start < 0 and
length >= 0 do
start = length(string) + start
case start >= 0 do
true -> do_slice(string, start, length)
false -> ""
end
start = max(length(string) + start, 0)
do_slice(string, start, length)
end
defp do_slice(string, start, length) do
@@ -2100,42 +2145,48 @@ defmodule String do
Remember this function works with Unicode graphemes and considers
the slices to represent grapheme offsets. If you want to split
on raw bytes, check `Kernel.binary_part/3` instead.
on raw bytes, check `Kernel.binary_part/3` or
`Kernel.binary_slice/2` instead
## Examples
iex> String.slice("elixir", 1..3)
"lix"
iex> String.slice("elixir", 1..10)
"lixir"
iex> String.slice("elixir", -4..-1)
"ixir"
iex> String.slice("elixir", -4..6)
"ixir"
iex> String.slice("elixir", -100..100)
"elixir"
For ranges where `start > stop`, you need to explicitly
mark them as increasing:
iex> String.slice("elixir", 2..-1//1)
"ixir"
iex> String.slice("elixir", 1..-2//1)
"lixi"
If values are out of bounds, it returns an empty string:
You can use `../0` as a shortcut for `0..-1//1`, which returns
the whole string as is:
iex> String.slice("elixir", ..)
"elixir"
The step can be any positive number. For example, to
get every 2 characters of the string:
iex> String.slice("elixir", 0..-1//2)
"eii"
If the first position is after the string ends or after
the last position of the range, it returns an empty string:
iex> String.slice("elixir", 10..3)
""
iex> String.slice("elixir", -10..-7)
""
iex> String.slice("a", 0..1500)
"a"
iex> String.slice("a", 1..1500)
""
@@ -2143,16 +2194,16 @@ defmodule String do
@spec slice(t, Range.t()) :: t
def slice(string, first..last//step = range) when is_binary(string) do
# TODO: Deprecate negative steps on Elixir v1.16
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(string, first, last)
else
raise ArgumentError,
"String.slice/2 does not accept ranges with custom steps, got: #{inspect(range)}"
cond do
step > 0 ->
slice_range(string, first, last, step)
step == -1 and first > last ->
slice_range(string, first, last, 1)
true ->
raise ArgumentError,
"String.slice/2 does not accept ranges with negative steps, got: #{inspect(range)}"
end
end
@@ -2163,45 +2214,95 @@ defmodule String do
slice(string, Map.put(range, :step, step))
end
defp slice_range("", _, _), do: ""
defp slice_range("", _, _, _), do: ""
defp slice_range(string, first, -1) when first >= 0 do
left = byte_size_remaining_at(string, first)
binary_part(string, byte_size(string) - left, left)
defp slice_range(_string, first, last, _step) when first >= 0 and last >= 0 and first > last do
""
end
defp slice_range(string, first, last) when first >= 0 and last >= 0 do
if last >= first do
slice(string, first, last - first + 1)
else
""
defp slice_range(string, first, last, step) when first >= 0 do
from_start = byte_size_remaining_at(string, first)
rest = binary_part(string, byte_size(string) - from_start, from_start)
cond do
last == -1 ->
slice_every(rest, byte_size(rest), step)
last >= 0 and step == 1 ->
from_end = byte_size_remaining_at(rest, last - first + 1)
binary_part(rest, 0, from_start - from_end)
last >= 0 ->
slice_every(rest, last - first + 1, step)
true ->
rest
|> slice_range_negative(0, last)
|> slice_every(byte_size(string), step)
end
end
defp slice_range(string, first, last) do
{bytes, length} = acc_bytes(:unicode_util.gc(string), [], 0)
first = add_if_negative(first, length)
defp slice_range(string, first, last, step) do
string
|> slice_range_negative(first, last)
|> slice_every(byte_size(string), step)
end
defp slice_range_negative(string, first, last) do
{reversed_bytes, length} = acc_bytes(string, [], 0)
first = add_if_negative(first, length) |> max(0)
last = add_if_negative(last, length)
if first < 0 or first > last or first > length do
if first > last or first > length do
""
else
last = min(last + 1, length)
bytes = Enum.drop(bytes, length - last)
first = last - first
{length_bytes, start_bytes} = split_bytes(bytes, 0, first)
reversed_bytes = Enum.drop(reversed_bytes, length - last)
{length_bytes, start_bytes} = split_bytes(reversed_bytes, 0, last - first)
binary_part(string, start_bytes, length_bytes)
end
end
defp acc_bytes([gc | rest], bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [grapheme_byte_size(gc) | bytes], length + 1)
defp slice_every(string, _count, 1), do: string
defp slice_every(string, count, step), do: slice_every(string, count, step, [])
defp acc_bytes([], bytes, length),
do: {bytes, length}
defp slice_every(string, count, to_drop, acc) when count > 0 do
case :unicode_util.gc(string) do
[current | rest] ->
rest
|> drop(to_drop)
|> slice_every(count - to_drop, to_drop, [current | acc])
defp acc_bytes({:error, <<_, rest::bits>>}, bytes, length),
do: acc_bytes(:unicode_util.gc(rest), [1 | bytes], length + 1)
[] ->
reverse_characters_to_binary(acc)
{:error, <<byte, rest::bits>>} ->
reverse_characters_to_binary(acc) <>
<<byte>> <> slice_every(drop(rest, to_drop), count - to_drop, to_drop, [])
end
end
defp slice_every(_string, _count, _to_drop, acc) do
reverse_characters_to_binary(acc)
end
defp drop(string, 1), do: string
defp drop(string, count) do
case :unicode_util.gc(string) do
[_ | rest] -> drop(rest, count - 1)
[] -> ""
{:error, <<_, rest::bits>>} -> drop(rest, count - 1)
end
end
defp acc_bytes(string, bytes, length) do
case :unicode_util.gc(string) do
[gc | rest] -> acc_bytes(rest, [grapheme_byte_size(gc) | bytes], length + 1)
[] -> {bytes, length}
{:error, <<_, rest::bits>>} -> acc_bytes(rest, [1 | bytes], length + 1)
end
end
defp add_if_negative(value, to_add) when value < 0, do: value + to_add
defp add_if_negative(value, _to_add), do: value
@@ -2225,12 +2326,6 @@ defmodule String do
iex> String.starts_with?("elixir", ["erlang", "ruby"])
false
A compiled pattern can also be given:
iex> pattern = :binary.compile_pattern(["erlang", "elixir"])
iex> String.starts_with?("elixir", pattern)
true
An empty string will always match:
iex> String.starts_with?("elixir", "")
@@ -2238,8 +2333,16 @@ defmodule String do
iex> String.starts_with?("elixir", ["", "other"])
true
An empty list will never match:
iex> String.starts_with?("elixir", [])
false
iex> String.starts_with?("", [])
false
"""
@spec starts_with?(t, pattern) :: boolean
@spec starts_with?(t, t | [t]) :: boolean
def starts_with?(string, prefix) when is_binary(string) and is_binary(prefix) do
starts_with_string?(string, byte_size(string), prefix)
end
@@ -2250,6 +2353,7 @@ defmodule String do
end
def starts_with?(string, prefix) when is_binary(string) do
IO.warn("compiled patterns are deprecated in starts_with?")
Kernel.match?({0, _}, :binary.match(string, prefix))
end
@@ -2318,7 +2422,7 @@ defmodule String do
iex> String.match?("bar", ~r/foo/)
false
Elixir also provides `Kernel.=~/2` and `Regex.match?/2` as
Elixir also provides text-based match operator `=~/2` and function `Regex.match?/2` as
alternatives to test strings against regular expressions.
"""
@spec match?(t, Regex.t()) :: boolean
@@ -2327,10 +2431,16 @@ defmodule String do
end
@doc """
Checks if `string` contains any of the given `contents`.
Searches if `string` contains any of the given `contents`.
`contents` can be either a string, a list of strings,
or a compiled pattern.
or a compiled pattern. If `contents` is a list, this
function will search if any of the strings in `contents`
are part of `string`.
> Note: if you want to check if `string` is listed in `contents`,
> where `contents` is a list, use `Enum.member?(contents, string)`
> instead.
## Examples
@@ -2354,6 +2464,14 @@ defmodule String do
iex> String.contains?("elixir of life", ["", "other"])
true
An empty list will never match:
iex> String.contains?("elixir of life", [])
false
iex> String.contains?("", [])
false
Be aware that this function can match within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following returns `true`:
@@ -2368,19 +2486,29 @@ defmodule String do
false
"""
@spec contains?(t, pattern) :: boolean
def contains?(string, []) when is_binary(string) do
false
end
@spec contains?(t, [t] | pattern) :: boolean
def contains?(string, contents) when is_binary(string) and is_list(contents) do
"" in contents or :binary.match(string, contents) != :nomatch
list_contains?(string, byte_size(string), contents, [])
end
def contains?(string, contents) when is_binary(string) do
"" == contents or :binary.match(string, contents) != :nomatch
end
defp list_contains?(string, size, [head | tail], acc) do
case byte_size(head) do
0 -> true
head_size when head_size > size -> list_contains?(string, size, tail, acc)
_ -> list_contains?(string, size, tail, [head | acc])
end
end
defp list_contains?(_string, _size, [], []),
do: false
defp list_contains?(string, _size, [], contents),
do: :binary.match(string, contents) != :nomatch
@doc """
Converts a string into a charlist.
@@ -2441,9 +2569,19 @@ defmodule String do
Converts a string to an existing atom.
The maximum atom size is of 255 Unicode code points.
Raises an `ArgumentError` if the atom does not exist.
Inlined by the compiler.
> #### Atoms and modules {: .info}
>
> Since Elixir is a compiled language, the atoms defined in a module
> will only exist after said module is loaded, which typically happens
> whenever a function in the module is executed. Therefore, it is
> generally recommended to call `String.to_existing_atom/1` only to
> convert atoms defined within the module making the function call
> to `to_existing_atom/1`.
## Examples
iex> _ = :my_atom
@@ -2723,12 +2861,16 @@ defmodule String do
graphemes_and_length: 1,
reverse_characters_to_binary: 1}
defp byte_size_remaining_at(binary, 0) do
byte_size(binary)
defp byte_size_unicode(binary) when is_binary(binary), do: byte_size(binary)
defp byte_size_unicode([head]), do: byte_size_unicode(head)
defp byte_size_unicode([head | tail]), do: byte_size_unicode(head) + byte_size_unicode(tail)
defp byte_size_remaining_at(unicode, 0) do
byte_size_unicode(unicode)
end
defp byte_size_remaining_at(binary, n) do
case :unicode_util.gc(binary) do
defp byte_size_remaining_at(unicode, n) do
case :unicode_util.gc(unicode) do
[_] -> 0
[_ | rest] -> byte_size_remaining_at(rest, n - 1)
[] -> 0
@@ -2736,7 +2878,7 @@ defmodule String do
end
end
defp codepoint_byte_size(cp) when cp <= 0x00FF, do: 1
defp codepoint_byte_size(cp) when cp <= 0x007F, do: 1
defp codepoint_byte_size(cp) when cp <= 0x07FF, do: 2
defp codepoint_byte_size(cp) when cp <= 0xFFFF, do: 3
defp codepoint_byte_size(_), do: 4
+6 -4
View File
@@ -13,7 +13,10 @@ defmodule StringIO do
"""
use GenServer
# We're implementing the GenServer behaviour instead of using the
# `use GenServer` macro, because we don't want the `child_spec/1`
# function as it doesn't make sense to be started under a supervisor.
@behaviour GenServer
@doc ~S"""
Creates an IO device.
@@ -287,7 +290,7 @@ defmodule StringIO do
{:ok, %{state | output: state.output <> string}}
{_, _, _} ->
{{:error, req}, state}
{{:error, {:no_translation, encoding, state.encoding}}, state}
end
rescue
ArgumentError -> {{:error, req}, state}
@@ -407,7 +410,6 @@ defmodule StringIO do
end
end
defp binary_to_list(data, _) when is_list(data), do: data
defp binary_to_list(data, :unicode) when is_binary(data), do: String.to_charlist(data)
defp binary_to_list(data, :latin1) when is_binary(data), do: :erlang.binary_to_list(data)
@@ -415,7 +417,7 @@ defmodule StringIO do
defp list_to_binary(data, :unicode) when is_list(data), do: List.to_string(data)
defp list_to_binary(data, :latin1) when is_list(data), do: :erlang.list_to_binary(data)
# From https://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# From https://www.erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# Erlang term, but if it is a list(), the I/O server can convert it to a binary().
defp get_until_result(data, encoding) when is_list(data), do: list_to_binary(data, encoding)
defp get_until_result(data, _), do: data
+254 -174
View File
@@ -7,7 +7,7 @@ defmodule Supervisor do
process structure called a *supervision tree*. Supervision trees provide
fault-tolerance and encapsulate how our applications start and shutdown.
A supervisor may be started directly with a list of children via
A supervisor may be started directly with a list of child specifications via
`start_link/2` or you may define a module-based supervisor that implements
the required callbacks. The sections below use `start_link/2` to start
supervisors in most examples, but it also includes a specific section
@@ -16,48 +16,55 @@ defmodule Supervisor do
## Examples
In order to start a supervisor, we need to first define a child process
that will be supervised. As an example, we will define a GenServer that
represents a stack:
that will be supervised. As an example, we will define a `GenServer`,
a generic server, that keeps a counter. Other processes can then send
messages to this process to read the counter and bump its value.
defmodule Stack do
> Note: in practice you would not define a counter as a GenServer. Instead,
> if you need a counter, you would pass it around as inputs and outputs to
> the functions that need it. The reason we picked a counter in this example
> is due to its simplicity, as it allows us to focus on how supervisors work.
defmodule Counter do
use GenServer
def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
def start_link(arg) when is_integer(arg) do
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
end
## Callbacks
@impl true
def init(stack) do
{:ok, stack}
def init(counter) do
{:ok, counter}
end
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
def handle_call(:get, _from, counter) do
{:reply, counter, counter}
end
@impl true
def handle_cast({:push, head}, tail) do
{:noreply, [head | tail]}
def handle_call({:bump, value}, _from, counter) do
{:reply, counter, counter + value}
end
end
The stack is a small wrapper around lists. It allows us to put
an element on the top of the stack, by prepending to the list,
and to get the top of the stack by pattern matching.
The `Counter` receives an argument on `start_link`. This argument
is passed to the `init/1` callback which becomes the initial value
of the counter. Our counter handles two operations (known as calls):
`:get`, to get the current counter value, and `:bump`, that bumps
the counter by the given `value` and returns the old counter.
We can now start a supervisor that will start and supervise our
stack process. The first step is to define a list of **child
counter process. The first step is to define a list of **child
specifications** that control how each child behaves. Each child
specification is a map, as shown below:
children = [
# The Stack is a child started via Stack.start_link([:hello])
# The Counter is a child started via Counter.start_link(0)
%{
id: Stack,
start: {Stack, :start_link, [[:hello]]}
id: Counter,
start: {Counter, :start_link, [0]}
}
]
@@ -69,30 +76,30 @@ defmodule Supervisor do
#=> %{active: 1, specs: 1, supervisors: 0, workers: 1}
Note that when starting the GenServer, we are registering it
with name `Stack`, which allows us to call it directly and get
what is on the stack:
with name `Counter` via the `name: __MODULE__` option. This allows
us to call it directly and get its value:
GenServer.call(Stack, :pop)
#=> :hello
GenServer.call(Counter, :get)
#=> 0
GenServer.cast(Stack, {:push, :world})
#=> :ok
GenServer.cast(Counter, {:bump, 3})
#=> 0
GenServer.call(Stack, :pop)
#=> :world
GenServer.call(Counter, :get)
#=> 3
However, there is a bug in our stack server. If we call `:pop` and
the stack is empty, it is going to crash because no clause matches:
However, there is a bug in our counter server. If we call `:bump` with
a non-numeric value, it is going to crash:
GenServer.call(Stack, :pop)
** (exit) exited in: GenServer.call(Stack, :pop, 5000)
GenServer.call(Counter, {:bump, "oops"})
** (exit) exited in: GenServer.call(Counter, {:bump, "oops"}, 5000)
Luckily, since the server is being supervised by a supervisor, the
supervisor will automatically start a new one, with the initial stack
of `[:hello]`:
supervisor will automatically start a new one, reset back to its initial
value of `0`:
GenServer.call(Stack, :pop)
#=> :hello
GenServer.call(Counter, :get)
#=> 0
Supervisors support different strategies; in the example above, we
have chosen `:one_for_one`. Furthermore, each supervisor can have many
@@ -111,10 +118,11 @@ defmodule Supervisor do
The child specification is a map containing up to 6 elements. The first two keys
in the following list are required, and the remaining ones are optional:
* `:id` - any term used to identify the child specification
internally by the supervisor; defaults to the given module.
In the case of conflicting `:id` values, the supervisor will refuse
to initialize and require explicit IDs. This key is required.
* `:id` - any term used to identify the child specification internally by
the supervisor; defaults to the given module. This key is required.
For supervisors, in the case of conflicting `:id` values, the supervisor
will refuse to initialize and require explicit IDs. This is not the case
for [dynamic supervisors](`DynamicSupervisor`) though.
* `:start` - a tuple with the module-function-args to be invoked
to start the child process. This key is required.
@@ -131,8 +139,11 @@ defmodule Supervisor do
* `:type` - specifies that the child process is a `:worker` or a
`:supervisor`. This key is optional and defaults to `:worker`.
There is a sixth key, `:modules`, which is optional and is rarely changed.
It is set automatically based on the `:start` value.
* `:modules` - a list of modules used by hot code upgrade mechanisms
to determine which processes are using certain modules. It is typically
set to the callback module of behaviours like `GenServer`, `Supervisor`,
and such. It is set automatically based on the `:start` value and it is rarely
changed in practice.
Let's understand what the `:shutdown` and `:restart` options control.
@@ -183,154 +194,100 @@ defmodule Supervisor do
For a more complete understanding of the exit reasons and their
impact, see the "Exit reasons and restarts" section.
## child_spec/1
## `child_spec/1` function
When starting a supervisor, we pass a list of child specifications. Those
When starting a supervisor, we may pass a list of child specifications. Those
specifications are maps that tell how the supervisor should start, stop and
restart each of its children:
%{
id: Stack,
start: {Stack, :start_link, [[:hello]]}
id: Counter,
start: {Counter, :start_link, [0]}
}
The map above defines a child with `:id` of `Stack` that is started
by calling `Stack.start_link([:hello])`.
The map above defines a child with `:id` of `Counter` that is started
by calling `Counter.start_link(0)`.
However, specifying the child specification for each child as a map can be
quite error prone, as we may change the Stack implementation and forget to
update its specification. That's why Elixir allows you to pass a tuple with
However, defining the child specification for each child as a map can be
quite error prone, as we may change the `Counter` implementation and forget
to update its specification. That's why Elixir allows you to pass a tuple with
the module name and the `start_link` argument instead of the specification:
children = [
{Stack, [:hello]}
{Counter, 0}
]
The supervisor will then invoke `Stack.child_spec([:hello])` to retrieve a
child specification. Now the `Stack` module is responsible for building its
own specification, for example, we could write:
The supervisor will then invoke `Counter.child_spec(0)` to retrieve a child
specification. Now the `Counter` module is responsible for building its own
specification, for example, we could write:
def child_spec(arg) do
%{
id: Stack,
start: {Stack, :start_link, [arg]}
id: Counter,
start: {Counter, :start_link, [arg]}
}
end
Luckily for us, `use GenServer` already defines a `Stack.child_spec/1`
exactly like above. If you need to customize the `GenServer`, you can
pass the options directly to `use GenServer`:
Luckily for us, `use GenServer` already defines a `Counter.child_spec/1`
exactly like above, so you don't need to write the definition above yourself.
If you want to customize the automatically generated `child_spec/1` function,
you can pass the options directly to `use GenServer`:
use GenServer, restart: :transient
Finally, note it is also possible to simply pass the `Stack` module as
Finally, note it is also possible to simply pass the `Counter` module as
a child:
children = [
Stack
Counter
]
When only the module name is given, it is equivalent to `{Stack, []}`.
By replacing the map specification by `{Stack, [:hello]}` or `Stack`, we keep
the child specification encapsulated in the `Stack` module, using the default
implementation defined by `use GenServer`. We can now share our `Stack` worker
with other developers and they can add it directly to their supervision tree
without worrying about the low-level details of the worker.
When only the module name is given, it is equivalent to `{Counter, []}`,
which in our case would be invalid, which is why we always pass the initial
counter explicitly.
Overall, the child specification can be one of the following:
By replacing the child specification with `{Counter, 0}`, we keep it
encapsulated in the `Counter` module. We could now share our
`Counter` implementation with other developers and they can add it directly
to their supervision tree without worrying about the low-level details of
the counter.
Overall, a child specification can be one of the following:
* a map representing the child specification itself - as outlined in the
"Child specification" section
* a tuple with a module as first element and the start argument as second -
such as `{Stack, [:hello]}`. In this case, `Stack.child_spec([:hello])`
is called to retrieve the child specification
* a module - such as `Stack`. In this case, `Stack.child_spec([])`
is called to retrieve the child specification
If you need to convert a tuple or a module child specification to a map or
modify a child specification, you can use the `Supervisor.child_spec/2` function.
For example, to run the stack with a different `:id` and a `:shutdown` value of
* a tuple with a module as first element and the start argument as second -
such as `{Counter, 0}`. In this case, `Counter.child_spec(0)` is called
to retrieve the child specification
* a module - such as `Counter`. In this case, `Counter.child_spec([])`
would be called, which is invalid for the counter, but it is useful in
many other cases, especially when you want to pass a list of options
to the child process
If you need to convert a `{module, arg}` tuple or a module child specification to a
[child specification](`t:child_spec/0`) or modify a child specification itself,
you can use the `Supervisor.child_spec/2` function.
For example, to run the counter with a different `:id` and a `:shutdown` value of
10 seconds (10_000 milliseconds):
children = [
Supervisor.child_spec({Stack, [:hello]}, id: MyStack, shutdown: 10_000)
Supervisor.child_spec({Counter, 0}, id: MyCounter, shutdown: 10_000)
]
## Module-based supervisors
In the example above, a supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of children that are automatically initialized, we manually
initialize the children by calling `Supervisor.init/2` inside its
`c:init/1` callback.
`use Supervisor` also defines a `child_spec/1` function which allows
us to run `MyApp.Supervisor` as a child of another supervisor or
at the top of your supervision tree as:
children = [
MyApp.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)
A general guideline is to use the supervisor without a callback
module only at the top of your supervision tree, generally in the
`c:Application.start/2` callback. We recommend using module-based
supervisors for any other supervisor in your application, so they
can run as a child of another supervisor in the tree. The `child_spec/1`
generated automatically by `Supervisor` can be customized with the
following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
The `@doc` annotation immediately preceding `use Supervisor` will be
attached to the generated `child_spec/1` function.
## `start_link/2`, `init/2`, and strategies
## Supervisor strategies and options
So far we have started the supervisor passing a single child as a tuple
as well as a strategy called `:one_for_one`:
children = [
{Stack, [:hello]}
{Counter, 0}
]
Supervisor.start_link(children, strategy: :one_for_one)
or from inside the `c:init/1` callback:
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
The first argument given to `start_link/2` and `init/2` is a list of child
The first argument given to `start_link/2` is a list of child
specifications as defined in the "child_spec/1" section above.
The second argument is a keyword list of options:
@@ -368,13 +325,69 @@ defmodule Supervisor do
In the above, process termination refers to unsuccessful termination, which
is determined by the `:restart` option.
To dynamically supervise children, see `DynamicSupervisor`.
To efficiently supervise children started dynamically, see `DynamicSupervisor`.
### Name registration
A supervisor is bound to the same name registration rules as a `GenServer`.
Read more about these rules in the documentation for `GenServer`.
## Module-based supervisors
In the example so far, the supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
children = [
{Counter, 0}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of child specifications that are automatically initialized, we manually
initialize the children by calling `Supervisor.init/2` inside its
`c:init/1` callback. `Supervisor.init/2` accepts the same `:strategy`,
`:max_restarts`, and `:max_seconds` options as `start_link/2`.
`use Supervisor` also defines a `child_spec/1` function which allows
us to run `MyApp.Supervisor` as a child of another supervisor or
at the top of your supervision tree as:
children = [
MyApp.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)
A general guideline is to use the supervisor without a callback
module only at the top of your supervision tree, generally in the
`c:Application.start/2` callback. We recommend using module-based
supervisors for any other supervisor in your application, so they
can run as a child of another supervisor in the tree. The `child_spec/1`
generated automatically by `Supervisor` can be customized with the
following options:
* `:id` - the child specification identifier, defaults to the current module
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
The `@doc` annotation immediately preceding `use Supervisor` will be
attached to the generated `child_spec/1` function.
## Start and shutdown
When the supervisor starts, it traverses all child specifications and
@@ -474,7 +487,8 @@ defmodule Supervisor do
init callback to return the proper supervision flags.
"""
@callback init(init_arg :: term) ::
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
{:ok,
{sup_flags(), [child_spec() | (old_erlang_child_spec :: :supervisor.child_spec())]}}
| :ignore
@typedoc "Return values of `start_link` functions"
@@ -489,14 +503,27 @@ defmodule Supervisor do
| {:ok, child, info :: term}
| {:error, {:already_started, child} | :already_present | term}
@typedoc """
A child process.
It can be a PID when the child process was started, or `:undefined` when
the child was created by a [dynamic supervisor](`DynamicSupervisor`).
"""
@type child :: pid | :undefined
@typedoc "The Supervisor name"
@typedoc "The supervisor name"
@type name :: atom | {:global, term} | {:via, module, term}
@typedoc "Option values used by the `start*` functions"
@type option :: {:name, name}
@typedoc "The supervisor flags returned on init"
@type sup_flags() :: %{
strategy: strategy(),
intensity: non_neg_integer(),
period: pos_integer()
}
@typedoc "The supervisor reference"
@type supervisor :: pid | name | {atom, node}
@@ -506,35 +533,62 @@ defmodule Supervisor do
| {:max_restarts, non_neg_integer}
| {:max_seconds, pos_integer}
@typedoc "Supported restart options"
@type restart :: :permanent | :transient | :temporary
# TODO: Update :shutdown to "timeout() | :brutal_kill" when we require Erlang/OTP 24.
# Additionally apply https://github.com/elixir-lang/elixir/pull/11836
@typedoc "Supported shutdown options"
@type shutdown :: pos_integer() | :infinity | :brutal_kill
@typedoc "Supported strategies"
@type strategy :: :one_for_one | :one_for_all | :rest_for_one
@typedoc """
Supervisor type.
Whether the supervisor is a worker or a supervisor.
"""
@type type :: :worker | :supervisor
# Note we have inlined all types for readability
@typedoc "The supervisor specification"
@typedoc """
The supervisor child specification.
It defines how the supervisor should start, stop and restart each of its children.
"""
@type child_spec :: %{
required(:id) => atom() | term(),
required(:start) => {module(), atom(), [term()]},
optional(:restart) => :permanent | :transient | :temporary,
optional(:shutdown) => timeout() | :brutal_kill,
optional(:type) => :worker | :supervisor,
required(:start) => {module(), function_name :: atom(), args :: [term()]},
optional(:restart) => restart(),
optional(:shutdown) => shutdown(),
optional(:type) => type(),
optional(:modules) => [module()] | :dynamic
}
@doc """
Starts a supervisor with the given children.
The children is a list of modules, two-element tuples with module and
arguments or a map with the child specification. A strategy is required
to be provided through the `:strategy` option. See
"start_link/2, init/2, and strategies" for examples and other options.
`children` is a list of the following forms:
* a [child specification](`t:child_spec/0`)
* a module, where `module.child_spec([])` will be invoked to retrieve
its child specification
* a two-element tuple in the shape of `{module, arg}`, where `module.child_spec(arg)`
will be invoked to retrieve its child specification
A strategy is required to be provided through the `:strategy` option. See
"Supervisor strategies and options" for examples and other options.
The options can also be used to register a supervisor name.
The supported values are described under the "Name registration"
section in the `GenServer` module docs.
If the supervisor and its child processes are successfully spawned
If the supervisor and all child processes are successfully spawned
(if the start function of each child process returns `{:ok, child}`,
`{:ok, child, info}`, or `:ignore`) this function returns
`{:ok, child, info}`, or `:ignore`), this function returns
`{:ok, pid}`, where `pid` is the PID of the supervisor. If the supervisor
is given a name and a process with the specified name already exists,
the function returns `{:error, {:already_started, pid}}`, where `pid`
@@ -549,20 +603,26 @@ defmodule Supervisor do
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
"""
@spec start_link([:supervisor.child_spec() | {module, term} | module], [option | init_option]) ::
{:ok, pid} | {:error, {:already_started, pid} | {:shutdown, term} | term}
@spec start_link(
[
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
],
[option | init_option]
) :: {:ok, pid} | {:error, {:already_started, pid} | {:shutdown, term} | term}
def start_link(children, options) when is_list(children) do
{sup_opts, start_opts} = Keyword.split(options, [:strategy, :max_seconds, :max_restarts])
start_link(Supervisor.Default, init(children, sup_opts), start_opts)
end
@doc """
Receives a list of `children` to initialize and a set of `options`.
Receives a list of child specifications to initialize and a set of `options`.
This is typically invoked at the end of the `c:init/1` callback of
module-based supervisors. See the sections "Module-based supervisors"
and "start_link/2, init/2, and strategies" in the module
documentation for more information.
module-based supervisors. See the sections "Supervisor strategies and options" and
"Module-based supervisors" in the module documentation for more information.
This function returns a tuple containing the supervisor
flags and child specifications.
@@ -571,7 +631,7 @@ defmodule Supervisor do
def init(_init_arg) do
children = [
{Stack, [:hello]}
{Counter, 0}
]
Supervisor.init(children, strategy: :one_for_one)
@@ -593,7 +653,17 @@ defmodule Supervisor do
description of the available strategies.
"""
@doc since: "1.5.0"
@spec init([:supervisor.child_spec() | {module, term} | module], [init_option]) :: {:ok, tuple}
@spec init(
[
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
],
[init_option]
) ::
{:ok,
{sup_flags(), [child_spec() | (old_erlang_child_spec :: :supervisor.child_spec())]}}
def init(children, options) when is_list(children) and is_list(options) do
strategy =
case options[:strategy] do
@@ -699,10 +769,14 @@ defmodule Supervisor do
@doc """
Builds and overrides a child specification.
Similar to `start_link/2` and `init/2`, it expects a
`module`, `{module, arg}` or a map as the child specification.
If a module is given, the specification is retrieved by calling
`module.child_spec(arg)`.
Similar to `start_link/2` and `init/2`, it expects a module, `{module, arg}`,
or a [child specification](`t:child_spec/0`).
If a two-element tuple in the shape of `{module, arg}` is given,
the child specification is retrieved by calling `module.child_spec(arg)`.
If a module is given, the child specification is retrieved by calling
`module.child_spec([])`.
After the child specification is retrieved, the fields on `overrides`
are directly applied on the child spec. If `overrides` has keys that
@@ -760,8 +834,8 @@ defmodule Supervisor do
section in the `GenServer` module docs.
"""
# It is important to keep the 2-arity spec because it is a catch
# all to start_link(children, options).
# It is important to keep the two-arity spec because it is a catch-all
# to start_link(children, options).
@spec start_link(module, term) :: on_start
@spec start_link(module, term, [option]) :: on_start
def start_link(module, init_arg, options \\ []) when is_list(options) do
@@ -816,7 +890,13 @@ defmodule Supervisor do
returns `{:error, error}` where `error` is a term containing information about
the error and child specification.
"""
@spec start_child(supervisor, :supervisor.child_spec() | {module, term} | module) ::
@spec start_child(
supervisor,
child_spec()
| {module, term}
| module
| (old_erlang_child_spec :: :supervisor.child_spec())
) ::
on_start_child
def start_child(supervisor, {_, _, _, _, _, _} = child_spec) do
call(supervisor, {:start_child, child_spec})
@@ -960,7 +1040,7 @@ defmodule Supervisor do
workers: non_neg_integer
}
def count_children(supervisor) do
call(supervisor, :count_children) |> Map.new()
call(supervisor, :count_children) |> :maps.from_list()
end
@doc """

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