@@ -308,17 +308,20 @@ defmodule IO do
308308 entry from the compilation environment will be used
309309
310310 * a keyword list with at least the `:file` option representing
311- a single stacktrace entry (since v1.14.0). The `:line`, `:module `,
312- `:function` options are also supported
311+ a single stacktrace entry (since v1.14.0). The `:line`, `:column `,
312+ `:module`, and `: function` options are also supported
313313
314- This function also notifies the compiler a warning was printed
315- (in case --warnings-as-errors was enabled). It returns `:ok`
316- if it succeeds.
314+ This function notifies the compiler a warning was printed
315+ and emits a compiler diagnostic (`t:Code.diagnostic/1`).
316+ The diagnostic will include precise file and location information
317+ if a `Macro.Env` is given or those values have been passed as
318+ keyword list, but not for stacktraces, as they are often imprecise.
319+
320+ It returns `:ok` if it succeeds.
317321
318322 ## Examples
319323
320- stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
321- IO.warn("variable bar is unused", stacktrace)
324+ IO.warn("variable bar is unused", module: MyApp, function: {:main, 1}, line: 4, file: "my_app.ex")
322325 #=> warning: variable bar is unused
323326 #=> my_app.ex:4: MyApp.main/1
324327
@@ -337,15 +340,22 @@ defmodule IO do
337340
338341 def warn ( message , [ { _ , _ } | _ ] = keyword ) do
339342 if file = keyword [ :file ] do
340- warn (
341- message ,
342- % {
343+ line = keyword [ :line ]
344+ column = keyword [ :column ]
345+ position = if line && column , do: { line , column } , else: line
346+ message = to_chardata ( message )
347+
348+ stacktrace =
349+ Macro.Env . stacktrace ( % {
343350 __ENV__
344351 | module: keyword [ :module ] ,
345352 function: keyword [ :function ] ,
346- line: keyword [ : line] ,
353+ line: line ,
347354 file: file
348- }
355+ } )
356+
357+ :elixir_errors . emit_diagnostic ( :warning , position , file , message , stacktrace ,
358+ read_snippet: true
349359 )
350360 else
351361 warn ( message , [ ] )
0 commit comments