diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index 3d91d27df8..f91af40023 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -843,9 +843,27 @@ defmodule Code do #=> %{debug_info: true, docs: true, ...} """ - @spec compiler_options() :: %{optional(atom) => boolean} + # TODO: Deprecate me on Elixir v1.12 + @doc deprecated: "Use Code.compiler_option/1 instead" def compiler_options do - :elixir_config.get(:compiler_options) + for key <- @available_compiler_options, into: %{} do + {key, :elixir_config.get(key)} + end + end + + @doc """ + Returns the value of a given compiler option. + + Check `compiler_options/1` for more information. + + ## Examples + + Code.compiler_option(:debug_info) + #=> true + + """ + def compiler_option(key) when key in @available_compiler_options do + :elixir_config.get(key) end @doc """ @@ -918,29 +936,31 @@ defmodule Code do ## Examples - Code.compiler_options(debug_info: true) - #=> %{debug_info: true, docs: true, - #=> warnings_as_errors: false, ignore_module_conflict: false} + Code.compiler_options(debug_info: true, ...) + #=> %{debug_info: true, ...} """ @spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean} def compiler_options(opts) do - Enum.each(opts, fn - {key, value} when key in @boolean_compiler_options -> - if not is_boolean(value) do - raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}" - end + for {key, value} <- opts, into: %{} do + cond do + key in @boolean_compiler_options -> + if not is_boolean(value) do + raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}" + end - {key, value} when key in @list_compiler_options -> - if not is_list(value) do - raise "compiler option #{inspect(key)} should be a list, got: #{inspect(value)}" - end + key in @list_compiler_options -> + if not is_list(value) do + raise "compiler option #{inspect(key)} should be a list, got: #{inspect(value)}" + end - {key, _} -> - raise "unknown compiler option: #{inspect(key)}" - end) + true -> + raise "unknown compiler option: #{inspect(key)}" + end - :elixir_config.update(:compiler_options, &Enum.into(opts, &1)) + :elixir_config.put(key, value) + {key, value} + end end @doc """ diff --git a/lib/elixir/lib/module/checker.ex b/lib/elixir/lib/module/checker.ex index 8b4485b356..41f2b98d55 100644 --- a/lib/elixir/lib/module/checker.ex +++ b/lib/elixir/lib/module/checker.ex @@ -52,7 +52,7 @@ defmodule Module.Checker do {:no_warn_undefined, values} <- module_map.compile_opts, value <- List.wrap(values), do: value - ) ++ Code.compiler_options()[:no_warn_undefined] + ) ++ Code.compiler_option(:no_warn_undefined) end defp check_definitions(definitions, state) do diff --git a/lib/elixir/src/elixir.erl b/lib/elixir/src/elixir.erl index 99bdb3445d..37042c4073 100644 --- a/lib/elixir/src/elixir.erl +++ b/lib/elixir/src/elixir.erl @@ -51,25 +51,25 @@ start(_Type, _Args) -> {{uri, <<"ldap">>}, 389} ], - CompilerOpts = #{ - docs => true, - ignore_module_conflict => false, - debug_info => true, - warnings_as_errors => false, - relative_paths => true, - no_warn_undefined => [] - }, - {ok, [[Home] | _]} = init:get_argument(home), Config = [ {at_exit, []}, {argv, []}, + {no_halt, false}, + + %% Static options {bootstrap, false}, - {compiler_options, CompilerOpts}, {home, unicode:characters_to_binary(Home, Encoding, Encoding)}, {identifier_tokenizer, Tokenizer}, - {no_halt, false} + + %% Compiler options + {docs, true}, + {ignore_module_conflict, false}, + {debug_info, true}, + {warnings_as_errors, false}, + {relative_paths, true}, + {no_warn_undefined, []} | URIConfig ], diff --git a/lib/elixir/src/elixir_code_server.erl b/lib/elixir/src/elixir_code_server.erl index 62ac0cd71f..b8dfc6f154 100644 --- a/lib/elixir/src/elixir_code_server.erl +++ b/lib/elixir/src/elixir_code_server.erl @@ -83,10 +83,9 @@ handle_call(Request, _From, Config) -> handle_cast({register_warning, CompilerPid}, Config) -> CompilationStatusCurrent = Config#elixir_code_server.compilation_status, CompilationStatusNew = maps:put(CompilerPid, error, CompilationStatusCurrent), - CompilerOptions = elixir_config:get(compiler_options), - case maps:find(warnings_as_errors, CompilerOptions) of - {ok, true} -> {noreply, Config#elixir_code_server{compilation_status=CompilationStatusNew}}; - _ -> {noreply, Config} + case elixir_config:get(warnings_as_errors) of + true -> {noreply, Config#elixir_code_server{compilation_status=CompilationStatusNew}}; + false -> {noreply, Config} end; handle_cast({reset_warnings, CompilerPid}, Config) -> diff --git a/lib/elixir/src/elixir_compiler.erl b/lib/elixir/src/elixir_compiler.erl index b6cb83905a..bde1088837 100644 --- a/lib/elixir/src/elixir_compiler.erl +++ b/lib/elixir/src/elixir_compiler.erl @@ -1,16 +1,9 @@ %% Elixir compiler front-end to the Erlang backend. -module(elixir_compiler). --export([get_opt/1, string/3, quoted/3, bootstrap/0, +-export([string/3, quoted/3, bootstrap/0, file/2, file_to_path/3, eval_forms/3]). -include("elixir.hrl"). -get_opt(Key) -> - Map = elixir_config:get(compiler_options), - case maps:find(Key, Map) of - {ok, Value} -> Value; - error -> false - end. - string(Contents, File, Callback) -> Forms = elixir:'string_to_quoted!'(Contents, 1, File, []), quoted(Forms, File, Callback). @@ -122,9 +115,10 @@ allows_fast_compilation(_) -> bootstrap() -> {ok, _} = application:ensure_all_started(elixir), - Update = fun(Old) -> maps:merge(Old, #{docs => false, relative_paths => false, ignore_module_conflict => true}) end, - _ = elixir_config:update(compiler_options, Update), - _ = elixir_config:put(bootstrap, true), + elixir_config:put(bootstrap, true), + elixir_config:put(docs, false), + elixir_config:put(relative_paths, false), + elixir_config:put(ignore_module_conflict, true), [bootstrap_file(File) || File <- bootstrap_main()]. bootstrap_file(File) -> diff --git a/lib/elixir/src/elixir_erl.erl b/lib/elixir/src/elixir_erl.erl index a2a24ca108..0db9fb5967 100644 --- a/lib/elixir/src/elixir_erl.erl +++ b/lib/elixir/src/elixir_erl.erl @@ -452,14 +452,14 @@ take_debug_opts(Opts) -> case proplists:get_value(debug_info, Opts) of true -> {true, proplists:delete(debug_info, Opts)}; false -> {false, proplists:delete(debug_info, Opts)}; - undefined -> {elixir_compiler:get_opt(debug_info), Opts} + undefined -> {elixir_config:get(debug_info), Opts} end. extra_chunks_opts([], Opts) -> Opts; extra_chunks_opts(Chunks, Opts) -> [{extra_chunks, Chunks} | Opts]. docs_chunk(Set, Module, Line, Def, Defmacro, Types, Callbacks) -> - case elixir_compiler:get_opt(docs) of + case elixir_config:get(docs) of true -> {ModuleDocLine, ModuleDoc} = get_moduledoc(Line, Set), ModuleDocMeta = get_moduledoc_meta(Set), diff --git a/lib/elixir/src/elixir_module.erl b/lib/elixir/src/elixir_module.erl index 4aaf0aabda..1afd8faa42 100644 --- a/lib/elixir/src/elixir_module.erl +++ b/lib/elixir/src/elixir_module.erl @@ -205,7 +205,7 @@ check_module_availability(Line, File, Module) -> false -> ok end, - case elixir_compiler:get_opt(ignore_module_conflict) of + case elixir_config:get(ignore_module_conflict) of false -> case code:ensure_loaded(Module) of {module, _} -> diff --git a/lib/elixir/src/elixir_utils.erl b/lib/elixir/src/elixir_utils.erl index afececb034..41551156e1 100644 --- a/lib/elixir/src/elixir_utils.erl +++ b/lib/elixir/src/elixir_utils.erl @@ -99,7 +99,7 @@ change_universal_time(Name, {{Y, M, D}, {H, Min, Sec}}=Time) file:write_file_info(Name, #file_info{mtime=Time}, [{time, universal}]). relative_to_cwd(Path) -> - try elixir_compiler:get_opt(relative_paths) of + try elixir_config:get(relative_paths) of true -> 'Elixir.Path':relative_to_cwd(Path); false -> Path catch diff --git a/lib/elixir/test/elixir/kernel/parallel_compiler_test.exs b/lib/elixir/test/elixir/kernel/parallel_compiler_test.exs index cbcbed8f64..da4ac670c2 100644 --- a/lib/elixir/test/elixir/kernel/parallel_compiler_test.exs +++ b/lib/elixir/test/elixir/kernel/parallel_compiler_test.exs @@ -265,7 +265,7 @@ defmodule Kernel.ParallelCompilerTest do end test "supports warnings as errors" do - warnings_as_errors = Code.compiler_options()[:warnings_as_errors] + warnings_as_errors = Code.compiler_option(:warnings_as_errors) [fixture] = write_tmp( @@ -408,7 +408,7 @@ defmodule Kernel.ParallelCompilerTest do end test "supports warnings as errors" do - warnings_as_errors = Code.compiler_options()[:warnings_as_errors] + warnings_as_errors = Code.compiler_option(:warnings_as_errors) [fixture] = write_tmp( diff --git a/lib/elixir/test/elixir/module/checker_test.exs b/lib/elixir/test/elixir/module/checker_test.exs index 800df134fd..52c54afe0a 100644 --- a/lib/elixir/test/elixir/module/checker_test.exs +++ b/lib/elixir/test/elixir/module/checker_test.exs @@ -486,7 +486,7 @@ defmodule Module.CheckerTest do end test "excludes global no_warn_undefined" do - compiler_options = Code.compiler_options() + no_warn_undefined = Code.compiler_option(:no_warn_undefined) try do Code.compiler_options( @@ -532,7 +532,7 @@ defmodule Module.CheckerTest do assert_warnings(files, warning) after - Code.compiler_options(compiler_options) + Code.compiler_options(no_warn_undefined: no_warn_undefined) end end end