Skip to content

Commit 6da4dd4

Browse files
committed
fix showing MacroExpansionError when sourceref isa LineNumberNode
1 parent b07a711 commit 6da4dd4

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/macro_expansion.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,18 @@ function Base.showerror(io::IO, exc::MacroExpansionError)
118118
# * How to deal with highlighting trivia? Could provide a token kind or
119119
# child position within the raw tree? How to abstract this??
120120
src = sourceref(exc.ex)
121-
fb = first_byte(src)
122-
lb = last_byte(src)
123-
pos = exc.position
124-
byterange = pos == :all ? (fb:lb) :
125-
pos == :begin ? (fb:fb-1) :
126-
pos == :end ? (lb+1:lb) :
127-
error("Unknown position $pos")
128-
highlight(io, src.file, byterange, note=exc.msg)
121+
if src isa LineNumberNode
122+
highlight(io, src, note=exc.msg)
123+
else
124+
fb = first_byte(src)
125+
lb = last_byte(src)
126+
pos = exc.position
127+
byterange = pos == :all ? (fb:lb) :
128+
pos == :begin ? (fb:fb-1) :
129+
pos == :end ? (lb+1:lb) :
130+
error("Unknown position $pos")
131+
highlight(io, src.file, byterange, note=exc.msg)
132+
end
129133
if !isnothing(exc.err)
130134
print(io, "\nCaused by:\n")
131135
showerror(io, exc.err)

test/macros.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ MacroExpansionError while expanding @oldstyle_error in module Main.macros.test_m
239239
Caused by:
240240
Some error in old style macro"""
241241

242+
# Parsed to prevent this test being sensitive to its line number
243+
Base.eval(test_mod, JuliaLowering.parsestmt(Expr, """
244+
macro oldstyle_calls_oldstyle_error()
245+
:(@oldstyle_error)
246+
end
247+
"""))
248+
@test try
249+
JuliaLowering.include_string(test_mod, """
250+
@oldstyle_calls_oldstyle_error
251+
""")
252+
catch exc
253+
sprint(showerror, exc)
254+
end == "MacroExpansionError while expanding @oldstyle_error in module Main.macros.test_mod:
255+
#= line 2 =# - Error expanding macro
256+
Caused by:
257+
Some error in old style macro"
258+
242259
# Old-style macros returning non-Expr values
243260
Base.eval(test_mod, :(
244261
macro oldstyle_non_Expr()

0 commit comments

Comments
 (0)