Skip to content

Commit 665a459

Browse files
committed
Handle nil values in IO.warn
1 parent 1b6fb26 commit 665a459

File tree

2 files changed

+23
-83
lines changed

2 files changed

+23
-83
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ get_snippet(File, Position) ->
5757
LineNumber = extract_line(Position),
5858
get_file_line(File, LineNumber).
5959

60-
get_file_line(_, 0) -> nil;
61-
get_file_line(File, LineNumber) ->
60+
get_file_line(File, LineNumber) when is_integer(LineNumber), LineNumber > 0 ->
6261
case file:open(File, [read, binary]) of
6362
{ok, IoDevice} ->
6463
Line = traverse_file_line(IoDevice, LineNumber),
6564
ok = file:close(IoDevice),
6665
Line;
6766
{error, _} ->
6867
nil
69-
end.
68+
end;
69+
get_file_line(_, _) -> nil.
7070

7171
traverse_file_line(IoDevice, 1) ->
7272
case file:read_line(IoDevice) of

lib/elixir/test/elixir/kernel/diagnostics_test.exs

Lines changed: 20 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -544,101 +544,41 @@ defmodule Kernel.DiagnosticsTest do
544544
end
545545

546546
@tag :tmp_dir
547-
test "long message (file)", %{tmp_dir: tmp_dir} do
548-
path = make_relative_tmp(tmp_dir, "long-warning.ex")
547+
test "IO.warn file+line+column", %{tmp_dir: tmp_dir} do
548+
path = make_relative_tmp(tmp_dir, "io-warn-file-line-column.ex")
549549

550550
source = """
551-
defmodule Sample do
552-
@file "#{path}"
553-
554-
def atom_case do
555-
v = "bc"
556-
557-
case v do
558-
_ when is_atom(v) -> :ok
559-
_ -> :fail
560-
end
561-
end
562-
end
551+
IO.warn("oops\\nmulti\\nline", file: __ENV__.file, line: __ENV__.line, column: 4)
563552
"""
564553

565554
File.write!(path, source)
566555

567556
expected = """
568-
warning: incompatible types:
569-
570-
binary() !~ atom()
571-
572-
in expression:
573-
574-
# #{path}:8
575-
is_atom(v)
576-
577-
where "v" was given the type binary() in:
578-
579-
# #{path}:5
580-
v = "bc"
581-
582-
where "v" was given the type atom() in:
583-
584-
# #{path}:8
585-
is_atom(v)
586-
587-
Conflict found at
557+
warning: oops
558+
multi
559+
line
588560
589-
8 _ when is_atom(v) -> :ok
590-
~
561+
1IO.warn("oops\\nmulti\\nline", file: __ENV__.file, line: __ENV__.line, column: 4)
562+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
591563
592-
└─ #{path}:8:14: Sample.atom_case/0
564+
└─ tmp\
593565
"""
594566

595-
assert capture_eval(source) =~ expected
596-
after
597-
purge(Sample)
567+
assert capture_io(:stderr, fn -> Code.eval_file(path) end) =~ expected
598568
end
599569

600-
test "long message (nofile)" do
601-
source = """
602-
defmodule Sample do
603-
def atom_case do
604-
v = "bc"
605-
606-
case v do
607-
_ when is_atom(v) -> :ok
608-
_ -> :fail
609-
end
610-
end
611-
end
612-
"""
613-
614-
expected = """
615-
warning: incompatible types:
616-
617-
binary() !~ atom()
618-
619-
in expression:
620-
621-
# nofile:6
622-
is_atom(v)
570+
test "IO.warn with missing data" do
571+
assert capture_eval("""
572+
IO.warn("oops-bad", file: #{inspect(__ENV__.file)}, line: 3, column: nil)
573+
""") =~ "warning: oops-bad"
623574

624-
where "v" was given the type binary() in:
575+
assert capture_eval("""
576+
IO.warn("oops-bad", file: #{inspect(__ENV__.file)}, line: nil)
577+
""") =~ "oops-bad"
625578

626-
# nofile:3
627-
v = "bc"
628-
629-
where "v" was given the type atom() in:
630-
631-
# nofile:6
632-
is_atom(v)
633-
634-
Conflict found at
635-
└─ nofile:6:14: Sample.atom_case/0
636-
637-
"""
638-
639-
assert capture_eval(source) =~ expected
640-
after
641-
purge(Sample)
579+
assert capture_eval("""
580+
IO.warn("oops-bad", file: nil)
581+
""") =~ "oops-bad"
642582
end
643583

644584
@tag :tmp_dir

0 commit comments

Comments
 (0)