Skip to content

Commit

Permalink
make code_lowered type stable
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Feb 21, 2024
1 parent 4e72944 commit 8d59773
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 9 additions & 7 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1126,21 +1126,23 @@ function code_lowered(@nospecialize(f), @nospecialize(t=Tuple); generated::Bool=
throw(ArgumentError("'debuginfo' must be either :source or :none"))
end
world = get_world_counter()
world == typemax(UInt) && error("code reflection cannot be used from generated functions")
return map(method_instances(f, t, world)) do m
ret = CodeInfo[]
for m in method_instances(f, t, world)
if generated && hasgenerator(m)
if may_invoke_generator(m)
return ccall(:jl_code_for_staged, Any, (Any, UInt), m, world)::CodeInfo
push!(ret, ccall(:jl_code_for_staged, Any, (Any, UInt), m, world)::CodeInfo)
else
error("Could not expand generator for `@generated` method ", m, ". ",
"This can happen if the provided argument types (", t, ") are ",
"not leaf types, but the `generated` argument is `true`.")
error(
"Could not expand generator for `@generated` method ", m, ". ",
"This can happen if the provided argument types (", lt, ") are ",
"not leaf types, but the `generated` argument is `true`.")
end
end
code = uncompressed_ir(m.def::Method)
debuginfo === :none && remove_linenums!(code)
return code
push!(ret, code)
end
return ret
end

hasgenerator(m::Method) = isdefined(m, :generator)
Expand Down
3 changes: 3 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,6 @@ end
@test Base.isexported(Mod52812, :b)
@test Base.ispublic(Mod52812, :a)
@test Base.ispublic(Mod52812, :b)

@test Base.infer_return_type(code_lowered, (Any,)) == Vector{Core.CodeInfo}
@test Base.infer_return_type(code_lowered, (Any,Any)) == Vector{Core.CodeInfo}

0 comments on commit 8d59773

Please sign in to comment.