Skip to content

Commit 0f16d23

Browse files
committed
Merge pull request c42f#23 from aviatesk/avi/macro-expansion-error-stacktrace
Allow outside catchers to see full stacktrace information of macro expansion.
1 parent ced50f4 commit 0f16d23

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ version = "1.0.0-DEV"
66
[deps]
77
JuliaSyntax = "70703baa-626e-46a2-a12c-08ffd08c73b4"
88

9+
[sources]
10+
JuliaSyntax = {rev = "e02f29f", url = "https://github.com/JuliaLang/JuliaSyntax.jl"}
11+
912
[compat]
1013
julia = "1"
1114

src/macro_expansion.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function Base.showerror(io::IO, exc::MacroExpansionError)
123123
end
124124
end
125125

126-
function eval_macro_name(ctx::MacroExpansionContext, ex)
126+
function eval_macro_name(ctx::MacroExpansionContext, ex::SyntaxTree)
127127
# `ex1` might contain a nontrivial mix of scope layers so we can't just
128128
# `eval()` it, as it's already been partially lowered by this point.
129129
# Instead, we repeat the latter parts of `lower()` here.
@@ -154,7 +154,7 @@ function has_syntax_tree_macro(mac)
154154
Base.methods(mac))
155155
end
156156

157-
function expand_macro(ctx::MacroExpansionContext, ex)
157+
function expand_macro(ctx::MacroExpansionContext, ex::SyntaxTree)
158158
@assert kind(ex) == K"macrocall"
159159

160160
macname = ex[1]
@@ -186,12 +186,13 @@ function expand_macro(ctx::MacroExpansionContext, ex)
186186
expanded = try
187187
invokelatest(macfunc, macro_args...)
188188
catch exc
189+
# TODO: Using rethrow() is kinda ugh. Is there a way to avoid it?
190+
# NOTE: Although currently rethrow() is necessary to allow outside catchers to access full stacktrace information
189191
if exc isa MacroExpansionError
190192
# Add context to the error.
191-
# TODO: Using rethrow() is kinda ugh. Is there a way to avoid it?
192193
rethrow(MacroExpansionError(mctx, exc.ex, exc.msg, exc.position))
193194
else
194-
throw(MacroExpansionError(mctx, ex, "Error expanding macro", :all))
195+
rethrow(MacroExpansionError(mctx, ex, "Error expanding macro", :all))
195196
end
196197
end
197198

@@ -282,7 +283,7 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
282283
@chk numchildren(ex) == 1
283284
# TODO: Upstream should set a general flag for detecting parenthesized
284285
# expressions so we don't need to dig into `green_tree` here. Ugh!
285-
plain_symbol = has_flags(ex, JuliaSyntax.COLON_QUOTE) &&
286+
plain_symbol = has_flags(ex, JuliaSyntax.COLON_QUOTE) &&
286287
kind(ex[1]) == K"Identifier" &&
287288
(sr = sourceref(ex); sr isa SourceRef && kind(sr.green_tree[2]) != K"parens")
288289
if plain_symbol
@@ -404,4 +405,3 @@ function expand_forms_1(mod::Module, ex::SyntaxTree, recurse::Bool=true)
404405
ctx.current_layer)
405406
return ctx2, reparent(ctx2, ex2)
406407
end
407-

test/ccall_demo.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function ccall_macro_lower(ex, convention, func, rettype, types, args, num_varar
105105
push!(roots, argi)
106106
push!(cargs, ast":(Base.unsafe_convert($type, $argi))")
107107
end
108-
push!(statements,
108+
push!(statements,
109109
@ast ex ex [K"foreigncall"
110110
func
111111
rettype
@@ -126,5 +126,4 @@ function var"@ccall"(ctx::JuliaLowering.MacroContext, ex)
126126
ccall_macro_lower(ex, "ccall", ccall_macro_parse(ex)...)
127127
end
128128

129-
end
130-
129+
end # module CCall

test/macros.jl

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
@testset "macros" begin
1+
module macros
22

3-
test_mod = Module()
3+
using JuliaLowering, Test
4+
5+
module test_mod end
46

57
JuliaLowering.include_string(test_mod, """
68
module M
@@ -75,7 +77,7 @@ end
7577
""")
7678

7779
@test JuliaLowering.include_string(test_mod, """
78-
let
80+
let
7981
x = "`x` from outer scope"
8082
M.@foo x
8183
end
@@ -89,7 +91,7 @@ end
8991

9092
@test !isdefined(test_mod.M, :a_global)
9193
@test JuliaLowering.include_string(test_mod, """
92-
begin
94+
begin
9395
M.@set_a_global 42
9496
M.a_global
9597
end
@@ -133,13 +135,43 @@ M.@recursive 3
133135
""") == (3, (2, (1, 0)))
134136

135137
@test let
136-
ex = parsestmt(SyntaxTree, "M.@outer()", filename="foo.jl")
138+
ex = JuliaLowering.parsestmt(JuliaLowering.SyntaxTree, "M.@outer()", filename="foo.jl")
137139
expanded = JuliaLowering.macroexpand(test_mod, ex)
138-
sourcetext.(flattened_provenance(expanded[2]))
140+
JuliaLowering.sourcetext.(JuliaLowering.flattened_provenance(expanded[2]))
139141
end == [
140142
"M.@outer()"
141143
"@inner"
142144
"2"
143145
]
144146

147+
JuliaLowering.include_string(test_mod, """
148+
f_throw(x) = throw(x)
149+
macro m_throw(x)
150+
:(\$(f_throw(x)))
151+
end
152+
""")
153+
let (err, st) = try
154+
JuliaLowering.include_string(test_mod, "_never_exist = @m_throw 42")
155+
catch e
156+
e, stacktrace(catch_backtrace())
157+
end
158+
@test err isa JuliaLowering.MacroExpansionError
159+
# Check that `catch_backtrace` can capture the stacktrace of the macro functions
160+
@test any(sf->sf.func===:f_throw, st)
161+
@test any(sf->sf.func===Symbol("@m_throw"), st)
145162
end
163+
164+
include("ccall_demo.jl")
165+
@test JuliaLowering.include_string(CCall, "@ccall strlen(\"foo\"::Cstring)::Csize_t") == 3
166+
let (err, st) = try
167+
JuliaLowering.include_string(CCall, "@ccall strlen(\"foo\"::Cstring)")
168+
catch e
169+
e, stacktrace(catch_backtrace())
170+
end
171+
@test err isa JuliaLowering.MacroExpansionError
172+
@test err.msg == "Expected a return type annotation like `::T`"
173+
# Check that `catch_backtrace` can capture the stacktrace of the macro function
174+
@test any(sf->sf.func===:ccall_macro_parse, st)
175+
end
176+
177+
end # module macros

test/runtests.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Test
33
include("utils.jl")
44

55
@testset "JuliaLowering.jl" begin
6-
76
include("syntax_graph.jl")
87

98
include("ir_tests.jl")
@@ -20,11 +19,10 @@ include("utils.jl")
2019
include("generators.jl")
2120
include("import.jl")
2221
include("loops.jl")
23-
include("macros.jl")
22+
@testset "macros" include("macros.jl")
2423
include("misc.jl")
2524
include("modules.jl")
2625
include("quoting.jl")
2726
include("scopes.jl")
2827
include("typedefs.jl")
29-
3028
end

0 commit comments

Comments
 (0)