@@ -78,12 +78,16 @@ struct MacroExpansionError <: Exception
7878 context:: Union{Nothing,MacroContext}
7979 ex:: SyntaxTree
8080 msg:: String
81+ " The source position relative to the node - may be `:begin` or `:end` or `:all`"
8182 position:: Symbol
83+ " Error that occurred inside the macro function call (`nothing` if no inner exception)"
84+ err
85+ MacroExpansionError (
86+ context:: Union{Nothing,MacroContext} , ex:: SyntaxTree , msg:: AbstractString , position:: Symbol ,
87+ @nospecialize err = nothing
88+ ) = new (context, ex, msg, position, err)
8289end
8390
84- """
85- `position` - the source position relative to the node - may be `:begin` or `:end` or `:all`
86- """
8791function MacroExpansionError (ex:: SyntaxTree , msg:: AbstractString ; position= :all )
8892 MacroExpansionError (nothing , ex, msg, position)
8993end
@@ -126,8 +130,8 @@ function eval_macro_name(ctx::MacroExpansionContext, mctx::MacroContext, ex::Syn
126130 expr_form = to_lowered_expr (mod, ex5)
127131 try
128132 eval (mod, expr_form)
129- catch
130- throw (MacroExpansionError (mctx, ex, " Macro not found" , :all ))
133+ catch err
134+ throw (MacroExpansionError (mctx, ex, " Macro not found" , :all , err ))
131135 end
132136end
133137
@@ -152,14 +156,14 @@ function expand_macro(ctx::MacroExpansionContext, ex::SyntaxTree)
152156 # TODO : Allow invoking old-style macros for compat
153157 invokelatest (macfunc, macro_args... )
154158 catch exc
155- # TODO : Using rethrow() is kinda ugh. Is there a way to avoid it?
156- # NOTE: Although currently rethrow() is necessary to allow outside catchers to access full stacktrace information
157159 if exc isa MacroExpansionError
158160 # Add context to the error.
159- rethrow ( MacroExpansionError (mctx, exc. ex, exc. msg, exc. position) )
161+ newexc = MacroExpansionError (mctx, exc. ex, exc. msg, exc. position, exc . err )
160162 else
161- rethrow ( MacroExpansionError (mctx, ex, " Error expanding macro" , :all ) )
163+ newexc = MacroExpansionError (mctx, ex, " Error expanding macro" , :all , exc )
162164 end
165+ # TODO : We can delete this rethrow when we move to AST-based error propagation.
166+ rethrow (newexc)
163167 end
164168
165169 if expanded isa SyntaxTree
0 commit comments