Skip to content

Commit eacacd7

Browse files
committed
always set err field of MacroExpansionError
1 parent 6576bff commit eacacd7

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/macro_expansion.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,11 @@ struct MacroExpansionError <: Exception
8080
msg::String
8181
"The source position relative to the node - may be `:begin` or `:end` or `:all`"
8282
position::Symbol
83-
"Error that occurred inside the macro function call (note that this may not be defined)"
83+
"Error that occurred inside the macro function call (`nothing` if no inner exception)"
8484
err
85-
MacroExpansionError(
86-
context::Union{Nothing,MacroContext}, ex::SyntaxTree, msg::AbstractString, position::Symbol
87-
) = new(context, ex, msg, position)
8885
MacroExpansionError(
8986
context::Union{Nothing,MacroContext}, ex::SyntaxTree, msg::AbstractString, position::Symbol,
90-
@nospecialize err
87+
@nospecialize err = nothing
9188
) = new(context, ex, msg, position, err)
9289
end
9390

@@ -161,9 +158,7 @@ function expand_macro(ctx::MacroExpansionContext, ex::SyntaxTree)
161158
catch exc
162159
if exc isa MacroExpansionError
163160
# Add context to the error.
164-
newexc = isdefined(exc, :err) ?
165-
MacroExpansionError(mctx, exc.ex, exc.msg, exc.position, exc.err) :
166-
MacroExpansionError(mctx, exc.ex, exc.msg, exc.position)
161+
newexc = MacroExpansionError(mctx, exc.ex, exc.msg, exc.position, exc.err)
167162
else
168163
newexc = MacroExpansionError(mctx, ex, "Error expanding macro", :all, exc)
169164
end

test/macros.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ let (err, st) = try
156156
e, stacktrace(catch_backtrace())
157157
end
158158
@test err isa JuliaLowering.MacroExpansionError
159-
@test isdefined(err, :err)
159+
@test !isnothing(err.err)
160160
# Check that `catch_backtrace` can capture the stacktrace of the macro functions
161161
@test any(sf->sf.func===:f_throw, st)
162162
@test any(sf->sf.func===Symbol("@m_throw"), st)
163163
end
164164

165-
let res = try
165+
let err = try
166166
JuliaLowering.include_string(test_mod, "_never_exist = @m_not_exist 42")
167167
catch e
168168
e
169169
end
170-
@test res isa JuliaLowering.MacroExpansionError
171-
@test res.msg == "Macro not found"
172-
@test isdefined(res, :err) && res.err isa UndefVarError
170+
@test err isa JuliaLowering.MacroExpansionError
171+
@test err.msg == "Macro not found"
172+
@test err.err isa UndefVarError
173173
end
174174

175175
include("ccall_demo.jl")
@@ -181,7 +181,7 @@ let (err, st) = try
181181
end
182182
@test err isa JuliaLowering.MacroExpansionError
183183
@test err.msg == "Expected a return type annotation like `::T`"
184-
@test !isdefined(err, :err)
184+
@test isnothing(err.err)
185185
# Check that `catch_backtrace` can capture the stacktrace of the macro function
186186
@test any(sf->sf.func===:ccall_macro_parse, st)
187187
end

0 commit comments

Comments
 (0)