Skip to content

Commit 79d96c2

Browse files
authored
Fix @nospecialize with zero args (JuliaLang/JuliaLowering.jl#112)
1 parent 0ed8343 commit 79d96c2

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

JuliaLowering/src/compat.jl

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ end
8080

8181
"""
8282
Return `e.args`, but with any parameters in SyntaxTree (flattened, source) order.
83-
Parameters are expected to be as `e.args[pos]`.
83+
Parameters are expected to be at `e.args[pos]`.
8484
8585
e.g. orderings of (a,b,c;d;e;f):
8686
Expr: (tuple (parameters (parameters (parameters f) e) d) a b c)
@@ -463,36 +463,35 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
463463
if e.args[1] isa Expr && e.args[1].head === :purity
464464
st_k = K"meta"
465465
child_exprs = [Expr(:quoted_symbol, :purity), Base.EffectsOverride(e.args[1].args...)]
466-
else
467-
@assert e.args[1] isa Symbol
468-
if e.args[1] === :nospecialize
469-
if nargs > 2
470-
st_k = K"block"
471-
# Kick the can down the road (should only be simple atoms?)
472-
child_exprs = map(c->Expr(:meta, :nospecialize, c), child_exprs[2:end])
473-
else
474-
st_id, src = _insert_convert_expr(e.args[2], graph, src)
475-
setmeta!(SyntaxTree(graph, st_id); nospecialize=true)
476-
return st_id, src
477-
end
478-
elseif e.args[1] in (:inline, :noinline, :generated, :generated_only,
479-
:max_methods, :optlevel, :toplevel, :push_loc, :pop_loc,
480-
:no_constprop, :aggressive_constprop, :specialize, :compile, :infer,
481-
:nospecializeinfer, :force_compile, :propagate_inbounds, :doc)
482-
# TODO: Some need to be handled in lowering
483-
for (i, ma) in enumerate(e.args)
484-
if ma isa Symbol
485-
# @propagate_inbounds becomes (meta inline
486-
# propagate_inbounds), but usually(?) only args[1] is
487-
# converted here
488-
child_exprs[i] = Expr(:quoted_symbol, e.args[i])
489-
end
466+
elseif nargs === 0
467+
# pass
468+
elseif e.args[1] === :nospecialize
469+
if nargs === 1
470+
child_exprs[1] = Expr(:quoted_symbol, :nospecialize)
471+
elseif nargs > 2
472+
st_k = K"block"
473+
# Kick the can down the road (should only be simple atoms?)
474+
child_exprs = map(c->Expr(:meta, :nospecialize, c), child_exprs[2:end])
475+
elseif nargs === 2
476+
st_id, src = _insert_convert_expr(e.args[2], graph, src)
477+
setmeta!(SyntaxTree(graph, st_id); nospecialize=true)
478+
return st_id, src
479+
end
480+
elseif e.args[1] in (:inline, :noinline, :generated, :generated_only,
481+
:max_methods, :optlevel, :toplevel, :push_loc, :pop_loc,
482+
:no_constprop, :aggressive_constprop, :specialize, :compile, :infer,
483+
:nospecializeinfer, :force_compile, :propagate_inbounds, :doc)
484+
# TODO: Some need to be handled in lowering
485+
for (i, ma) in enumerate(e.args)
486+
if ma isa Symbol
487+
# @propagate_inbounds becomes (meta inline propagate_inbounds)
488+
child_exprs[i] = Expr(:quoted_symbol, e.args[i])
490489
end
491-
else
492-
# Can't throw a hard error; it is explicitly tested that meta can take arbitrary keys.
493-
@error("Unknown meta form at $src: `$e`\n$(sprint(dump, e))")
494-
child_exprs[1] = Expr(:quoted_symbol, e.args[1])
495490
end
491+
else
492+
# Can't throw a hard error; it is explicitly tested that meta can take arbitrary keys.
493+
@error("Unknown meta form at $src: `$e`\n$(sprint(dump, e))")
494+
child_exprs[1] = Expr(:quoted_symbol, e.args[1])
496495
end
497496
elseif e.head === :scope_layer
498497
@assert nargs === 2

JuliaLowering/test/macros_ir.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,25 @@ cmdmac`hello`
186186
cmdmac`hello`12345
187187
#---------------------
188188
1 (return "hello from cmdmac with suffix 12345")
189+
190+
########################################
191+
# @nospecialize (zero args)
192+
function foo()
193+
@nospecialize
194+
end
195+
#---------------------
196+
1 (method TestMod.foo)
197+
2 latestworld
198+
3 TestMod.foo
199+
4 (call core.Typeof %₃)
200+
5 (call core.svec %₄)
201+
6 (call core.svec)
202+
7 SourceLocation::1:10
203+
8 (call core.svec %%%₇)
204+
9 --- method core.nothing %
205+
slots: [slot₁/#self#(!read)]
206+
1 (meta :nospecialize)
207+
2 (return core.nothing)
208+
10 latestworld
209+
11 TestMod.foo
210+
12 (return %₁₁)

JuliaLowering/test/utils.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ function uncomment_description(desc)
114114
end
115115

116116
function comment_description(desc)
117-
replace(desc, r"^"m=>"# ")
117+
lines = replace(split(desc, '\n')) do line
118+
strip("# " * line)
119+
end
120+
join(lines, '\n')
118121
end
119122

120123
function match_ir_test_case(case_str)
@@ -231,7 +234,7 @@ function refresh_ir_test_cases(filename, pattern=nothing)
231234
else
232235
ir = case.output
233236
end
234-
println(io,
237+
(case == cases[end] ? print : println)(io,
235238
"""
236239
########################################
237240
$(comment_description(case.description))

0 commit comments

Comments
 (0)