Skip to content

Commit 802f1df

Browse files
authored
[JuliaLowering] Fix handling of unnamed args in @generated functions (#60226)
This is required to get Dates pre-compiling with JuliaLowering.
1 parent 8a0e033 commit 802f1df

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

JuliaLowering/src/desugaring.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,16 @@ function expand_function_generator(ctx, srcref, callex_srcref, func_name, func_n
25342534
]
25352535
]
25362536

2537+
function stub_argname(n::SyntaxTree, i)
2538+
if kind(n) == K"Identifier"
2539+
return n.name_val::String
2540+
elseif kind(n) == K"BindingId"
2541+
# flisp lowering calls these unnamed arguments "#unused#", but JL does
2542+
# not accept that as a repeated argument name
2543+
return "#arg" * string(i) * "#"
2544+
else @assert false "Unexpected argument kind: $(kind(n))" end
2545+
end
2546+
25372547
# Extract non-generated body
25382548
nongen_body = @ast ctx body [K"block"
25392549
# The Julia runtime associates the code generator with the
@@ -2562,7 +2572,7 @@ function expand_function_generator(ctx, srcref, callex_srcref, func_name, func_n
25622572
[K"call"
25632573
"svec"::K"core"
25642574
"#self#"::K"Symbol"
2565-
(n.name_val::K"Symbol"(n) for n in arg_names[2:end])...
2575+
(stub_argname(n,i)::K"Symbol"(n) for (i,n) in enumerate(arg_names[2:end]))...
25662576
]
25672577
[K"call"
25682578
"svec"::K"core"

JuliaLowering/src/runtime.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
339339
ex0 = copy_ast(ctx1, ex0)
340340
end
341341
else
342-
ex0 = @ast ctx ex expanded::K"Value"
342+
ex0 = @ast ctx1 g.srcref ex0::K"Value"
343343
end
344344
# Expand any macros emitted by the generator
345345
ex1 = expand_forms_1(ctx1, reparent(ctx1, ex0))

JuliaLowering/test/functions.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ end
513513
end
514514
""") == (NTuple{5,Int}, 5, Int)
515515

516+
@test JuliaLowering.include_string(test_mod, """
517+
begin
518+
@generated function f_gen_unnamed_args(::Type{T}, y, ::Type{U}) where {T, U}
519+
return (T, y, U)
520+
end
521+
522+
f_gen_unnamed_args(Int, UInt8(3), Float64)
523+
end
524+
""") == (Int, UInt8, Float64)
525+
516526
@test JuliaLowering.include_string(test_mod, raw"""
517527
begin
518528
function f_partially_gen(x::NTuple{N,T}) where {N,T}

0 commit comments

Comments
 (0)