@@ -108,7 +108,12 @@ function Base.showerror(io::IO, exc::MacroExpansionError)
108108 print (io, " MacroExpansionError" )
109109 ctx = exc. context
110110 if ! isnothing (ctx)
111- print (io, " while expanding " , ctx. macrocall[1 ],
111+ # Use `Expr` formatting to pretty print the macro name for now -
112+ # there's quite a lot of special cases. We could alternatively consider
113+ # calling sourcetext() though that won't work well if it's a
114+ # synthetically-generated macro name path.
115+ macname_str = string (Expr (:macrocall , Expr (ctx. macrocall[1 ]), nothing ))
116+ print (io, " while expanding " , macname_str,
112117 " in module " , ctx. scope_layer. mod)
113118 end
114119 print (io, " :\n " )
@@ -137,11 +142,31 @@ function Base.showerror(io::IO, exc::MacroExpansionError)
137142 end
138143end
139144
145+ function fixup_macro_name (ctx:: MacroExpansionContext , ex:: SyntaxTree )
146+ k = kind (ex)
147+ if k == K " StrMacroName" || k == K " CmdMacroName"
148+ layerid = get (ex, :scope_layer , current_layer_id (ctx))
149+ newname = JuliaSyntax. lower_identifier_name (ex. name_val, k)
150+ makeleaf (ctx, ex, ex, kind= K " Identifier" , scope_layer= layerid, name_val= newname)
151+ elseif k == K " macro_name"
152+ @chk numchildren (ex) === 1
153+ if kind (ex[1 ]) === K " ."
154+ @ast ctx ex [K " ." ex[1 ][1 ] [K " macro_name" ex[1 ][2 ]]]
155+ else
156+ layerid = get (ex, :scope_layer , current_layer_id (ctx))
157+ newname = JuliaSyntax. lower_identifier_name (ex[1 ]. name_val, K " macro_name" )
158+ makeleaf (ctx, ex[1 ], ex[1 ], kind= kind (ex[1 ]), name_val= newname)
159+ end
160+ else
161+ mapchildren (e-> fixup_macro_name (ctx,e), ctx, ex)
162+ end
163+ end
164+
140165function eval_macro_name (ctx:: MacroExpansionContext , mctx:: MacroContext , ex:: SyntaxTree )
141166 # `ex1` might contain a nontrivial mix of scope layers so we can't just
142167 # `eval()` it, as it's already been partially lowered by this point.
143168 # Instead, we repeat the latter parts of `lower()` here.
144- ex1 = expand_forms_1 (ctx, ex )
169+ ex1 = expand_forms_1 (ctx, fixup_macro_name (ctx, ex) )
145170 ctx2, ex2 = expand_forms_2 (ctx, ex1)
146171 ctx3, ex3 = resolve_scopes (ctx2, ex2)
147172 ctx4, ex4 = convert_closures (ctx3, ex3)
@@ -368,9 +393,10 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
368393 layerid = get (ex, :scope_layer , current_layer_id (ctx))
369394 makeleaf (ctx, ex, ex, kind= K " Identifier" , scope_layer= layerid)
370395 end
371- elseif k == K " Identifier" || k == K " MacroName" || k == K " StringMacroName" || k == K " CmdMacroName"
372- layerid = get (ex, :scope_layer , current_layer_id (ctx))
373- makeleaf (ctx, ex, ex, kind= K " Identifier" , scope_layer= layerid)
396+ elseif k == K " StrMacroName" || k == K " CmdMacroName" || k == K " macro_name"
397+ # These can appear outside of a macrocall, e.g. in `import`
398+ e2 = fixup_macro_name (ctx, ex)
399+ expand_forms_1 (ctx, e2)
374400 elseif k == K " var" || k == K " char" || k == K " parens"
375401 # Strip "container" nodes
376402 @chk numchildren (ex) == 1
@@ -431,7 +457,7 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
431457 @ast ctx ex [K " ." expand_forms_1 (ctx, ex[1 ]) e2]
432458 elseif k == K " cmdstring"
433459 @chk numchildren (ex) == 1
434- e2 = @ast ctx ex [K " macrocall" " @ cmd" :: K"core" ex[1 ]]
460+ e2 = @ast ctx ex [K " macrocall" [ K " macro_name " " cmd" :: K"core" ] ex[1 ]]
435461 expand_macro (ctx, e2)
436462 elseif (k == K " call" || k == K " dotcall" )
437463 # Do some initial desugaring of call and dotcall here to simplify
0 commit comments