Skip to content

Commit

Permalink
Fixes for overlay tables (#41174)
Browse files Browse the repository at this point in the history
* Add support for at-overlay with parametric function definitions.

* Fix error message with at-MethodTable.

* Fix function signature to avoid compiler warning.

(cherry picked from commit 27ef9e0)
  • Loading branch information
maleadt authored and KristofferC committed Jun 17, 2021
1 parent 9388531 commit 6daa48e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions base/experimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,21 @@ method tables (e.g., using [`Core.Compiler.OverlayMethodTable`](@ref)).
"""
macro overlay(mt, def)
def = macroexpand(__module__, def) # to expand @inline, @generated, etc
if !isexpr(def, [:function, :(=)]) || !isexpr(def.args[1], :call)
if !isexpr(def, [:function, :(=)])
error("@overlay requires a function Expr")
end
if isexpr(def.args[1], :call)
def.args[1].args[1] = Expr(:overlay, mt, def.args[1].args[1])
elseif isexpr(def.args[1], :where)
def.args[1].args[1].args[1] = Expr(:overlay, mt, def.args[1].args[1].args[1])
else
error("@overlay requires a function Expr")
end
def.args[1].args[1] = Expr(:overlay, mt, def.args[1].args[1])
esc(def)
end

let new_mt(name::Symbol, mod::Module) = begin
ccall(:jl_check_top_level_effect, Cvoid, (Any, Cstring), mod, name)
ccall(:jl_check_top_level_effect, Cvoid, (Any, Cstring), mod, "@MethodTable")
ccall(:jl_new_method_table, Any, (Any, Any), name, mod)
end
@eval macro MethodTable(name::Symbol)
Expand Down
2 changes: 1 addition & 1 deletion src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ static void jl_check_open_for(jl_module_t *m, const char* funcname)
}
}

JL_DLLEXPORT void jl_check_top_level_effect(jl_value_t *m, char *fname)
JL_DLLEXPORT void jl_check_top_level_effect(jl_module_t *m, char *fname)
{
if (jl_current_task->ptls->in_pure_callback)
jl_errorf("%s cannot be used in a generated function", fname);
Expand Down
3 changes: 3 additions & 0 deletions test/compiler/contextual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ end
# short function def
@overlay mt cos(x::Float64) = 2

# parametric function def
@overlay mt tan(x::T) where {T} = 3

end

methods = Base._methods_by_ftype(Tuple{typeof(sin), Float64}, nothing, 1, typemax(UInt))
Expand Down

0 comments on commit 6daa48e

Please sign in to comment.