Skip to content

Commit 73ecea0

Browse files
committed
Simplify handling of warnings as errors
1 parent 9856c02 commit 73ecea0

File tree

7 files changed

+37
-35
lines changed

7 files changed

+37
-35
lines changed

lib/elixir/lib/code.ex

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ defmodule Code do
250250
:ignore_already_consolidated,
251251
:ignore_module_conflict,
252252
:infer_signatures,
253-
:relative_paths,
254-
:warnings_as_errors
253+
:relative_paths
255254
]
256255

257256
@list_compiler_options [:no_warn_undefined, :tracers, :parser_options]
@@ -1563,8 +1562,8 @@ defmodule Code do
15631562
15641563
## Examples
15651564
1566-
Code.compiler_options(warnings_as_errors: true)
1567-
#=> %{warnings_as_errors: false}
1565+
Code.compiler_options(infer_signatures: false)
1566+
#=> %{infer_signatures: true}
15681567
15691568
"""
15701569
@spec compiler_options(Enumerable.t({atom, term})) :: %{optional(atom) => term}
@@ -1593,6 +1592,12 @@ defmodule Code do
15931592
:elixir_config.get(key)
15941593
end
15951594

1595+
# TODO: Remove me in Elixir v2.0
1596+
def get_compiler_option(:warnings_as_errors) do
1597+
IO.warn(":warnings_as_errors is deprecated as part of Code.get_compiler_option/1")
1598+
:ok
1599+
end
1600+
15961601
@doc """
15971602
Returns a list with all available compiler options.
15981603
@@ -1647,9 +1652,6 @@ defmodule Code do
16471652
warnings, and errors generated by the compiler. Note disabling this option
16481653
won't affect runtime warnings and errors. Defaults to `true`.
16491654
1650-
* `:warnings_as_errors` - causes compilation to fail when warnings are
1651-
generated. Defaults to `false`.
1652-
16531655
* `:no_warn_undefined` (since v1.10.0) - list of modules and `{Mod, fun, arity}`
16541656
tuples that will not emit warnings that the module or function does not exist
16551657
at compilation time. Pass atom `:all` to skip warning for all undefined
@@ -1695,6 +1697,16 @@ defmodule Code do
16951697
:ok
16961698
end
16971699

1700+
# TODO: Remove me in Elixir v2.0
1701+
def put_compiler_option(:warnings_as_errors, value) do
1702+
IO.warn(
1703+
":warnings_as_errors is deprecated as part of Code.put_compiler_option/2, " <>
1704+
"pass it as option to Kernel.ParallelCompiler instead"
1705+
)
1706+
1707+
:ok
1708+
end
1709+
16981710
def put_compiler_option(:no_warn_undefined, value) do
16991711
if value != :all and not is_list(value) do
17001712
raise "compiler option :no_warn_undefined should be a list or the atom :all, " <>

lib/elixir/lib/kernel/cli.ex

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Kernel.CLI do
99
compile: [],
1010
no_halt: false,
1111
compiler_options: [],
12+
warnings_as_errors: false,
1213
errors: [],
1314
verbose_compile: false,
1415
profile: nil,
@@ -315,8 +316,7 @@ defmodule Kernel.CLI do
315316
end
316317

317318
defp parse_argv([~c"--warnings-as-errors" | t], %{mode: :elixirc} = config) do
318-
compiler_options = [{:warnings_as_errors, true} | config.compiler_options]
319-
parse_argv(t, %{config | compiler_options: compiler_options})
319+
parse_argv(t, %{config | warnings_as_errors: true})
320320
end
321321

322322
defp parse_argv([~c"--verbose" | t], %{mode: :elixirc} = config) do
@@ -499,15 +499,11 @@ defmodule Kernel.CLI do
499499
]
500500
end
501501

502-
profile_opts =
503-
if config.profile do
504-
[profile: config.profile]
505-
else
506-
[]
507-
end
508-
509502
output = IO.chardata_to_string(config.output)
510-
opts = verbose_opts ++ profile_opts
503+
504+
opts =
505+
verbose_opts ++
506+
[profile: config.profile, warnings_as_errors: config.warnings_as_errors]
511507

512508
case Kernel.ParallelCompiler.compile_to_path(files, output, opts) do
513509
{:ok, _, _} -> :ok

lib/elixir/lib/kernel/parallel_compiler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ defmodule Kernel.ParallelCompiler do
258258
{status, modules_or_errors, info} =
259259
try do
260260
outcome = spawn_workers(schedulers, checker, files, output, options)
261-
{outcome, Code.get_compiler_option(:warnings_as_errors)}
261+
{outcome, Keyword.get(options, :warnings_as_errors, false)}
262262
else
263263
{{:ok, _, %{runtime_warnings: r_warnings, compile_warnings: c_warnings} = info}, true}
264264
when r_warnings != [] or c_warnings != [] ->

lib/elixir/src/elixir.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ start(_Type, _Args) ->
9494
{on_undefined_variable, raise},
9595
{parser_options, [{columns, true}]},
9696
{debug_info, true},
97-
{warnings_as_errors, false},
9897
{relative_paths, true},
9998
{no_warn_undefined, []},
10099
{tracers, []}

lib/elixir/test/elixir/kernel/parallel_compiler_test.exs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,6 @@ defmodule Kernel.ParallelCompilerTest do
422422
end
423423

424424
test "supports warnings as errors" do
425-
warnings_as_errors = Code.get_compiler_option(:warnings_as_errors)
426-
427425
[fixture] =
428426
write_tmp(
429427
"warnings_as_errors",
@@ -438,20 +436,19 @@ defmodule Kernel.ParallelCompilerTest do
438436
output = tmp_path("not_to_be_used")
439437

440438
try do
441-
Code.compiler_options(warnings_as_errors: true)
442-
443439
msg =
444440
capture_io(:stderr, fn ->
445441
assert {:error, [error], []} =
446-
Kernel.ParallelCompiler.compile_to_path([fixture], output)
442+
Kernel.ParallelCompiler.compile_to_path([fixture], output,
443+
warnings_as_errors: true
444+
)
447445

448446
assert {^fixture, {3, 7}, "this clause " <> _} = error
449447
end)
450448

451449
assert msg =~
452450
"Compilation failed due to warnings while using the --warnings-as-errors option\n"
453451
after
454-
Code.compiler_options(warnings_as_errors: warnings_as_errors)
455452
purge([WarningsSample])
456453
end
457454

@@ -592,8 +589,6 @@ defmodule Kernel.ParallelCompilerTest do
592589
end
593590

594591
test "supports warnings as errors" do
595-
warnings_as_errors = Code.get_compiler_option(:warnings_as_errors)
596-
597592
[fixture] =
598593
write_tmp(
599594
"warnings_as_errors",
@@ -606,19 +601,17 @@ defmodule Kernel.ParallelCompilerTest do
606601
)
607602

608603
try do
609-
Code.compiler_options(warnings_as_errors: true)
610-
611604
msg =
612605
capture_io(:stderr, fn ->
613-
assert {:error, [error], []} = Kernel.ParallelCompiler.require([fixture])
606+
assert {:error, [error], []} =
607+
Kernel.ParallelCompiler.require([fixture], warnings_as_errors: true)
614608

615609
assert {^fixture, {3, 7}, "this clause " <> _} = error
616610
end)
617611

618612
assert msg =~
619613
"Compilation failed due to warnings while using the --warnings-as-errors option\n"
620614
after
621-
Code.compiler_options(warnings_as_errors: warnings_as_errors)
622615
purge([WarningsSample])
623616
end
624617
end

lib/mix/lib/mix/compilers/elixir.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ defmodule Mix.Compilers.Elixir do
10421042
threshold = opts[:long_compilation_threshold] || 10
10431043
profile = opts[:profile]
10441044
verbose = opts[:verbose] || false
1045+
warnings_as_errors = opts[:warnings_as_errors] || false
10451046

10461047
pid =
10471048
spawn_link(fn ->
@@ -1063,7 +1064,8 @@ defmodule Mix.Compilers.Elixir do
10631064
long_compilation_threshold: threshold,
10641065
profile: profile,
10651066
beam_timestamp: timestamp,
1066-
return_diagnostics: true
1067+
return_diagnostics: true,
1068+
warnings_as_errors: warnings_as_errors
10671069
]
10681070

10691071
response = Kernel.ParallelCompiler.compile_to_path(stale, dest, compile_opts)

lib/mix/lib/mix/tasks/compile.elixir.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ defmodule Mix.Tasks.Compile.Elixir do
7777
Defaults to `["lib"]`.
7878
7979
* `:elixirc_options` - compilation options that apply to Elixir's compiler.
80-
See `Code.put_compiler_option/2` for a complete list of options. These
81-
options are often overridable from the command line using the switches
82-
above.
80+
It supports many of the options above plus the options listed in
81+
`Code.put_compiler_option/2`. In case conflicting options are given,
82+
the ones given through the command line are used.
8383
8484
* `[xref: [exclude: ...]]` - a list of `module` or `{module, function, arity}`
8585
that should not be warned on in case on undefined modules or undefined

0 commit comments

Comments
 (0)