Skip to content

Commit

Permalink
handle errors in calling code_reflections methods on non-generic func…
Browse files Browse the repository at this point in the history
…tions

fix #15606
  • Loading branch information
vtjnash committed Jul 21, 2016
1 parent df4b5d2 commit 6a6ccb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ uncompressed_ast(l::LambdaInfo) =
# Printing code representations in IR and assembly
function _dump_function(f, t::ANY, native, wrapper, strip_ir_metadata, dump_module)
ccall(:jl_is_in_pure_context, Bool, ()) && error("native reflection cannot be used from generated functions")
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
end
t = tt_cons(Core.Typeof(f), to_tuple_type(t))
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Bool, Bool), t, wrapper, native)

Expand Down Expand Up @@ -363,6 +366,9 @@ end

function code_typed(f::ANY, types::ANY=Tuple; optimize=true)
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
end
types = to_tuple_type(types)
asts = []
for x in _methods(f,types,-1)
Expand All @@ -380,6 +386,9 @@ end

function return_types(f::ANY, types::ANY=Tuple)
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
end
types = to_tuple_type(types)
rt = []
for x in _methods(f,types,-1)
Expand Down
7 changes: 7 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,16 @@ void *jl_get_llvmf(jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
}
}
if (linfo == NULL) {
// no function found for argument tuple type
JL_GC_POP();
return NULL;
}
if (linfo->def->lambda_template->code == jl_nothing) {
// not a generic function
JL_GC_POP();
return NULL;
}

// make sure to compile this normally first,
// since `emit_function` doesn't handle recursive compilation correctly
linfo = jl_compile_for_dispatch(linfo);
Expand Down
4 changes: 4 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ let
end

@test_throws ArgumentError which(is, Tuple{Int, Int})
@test_throws ArgumentError code_typed(is, Tuple{Int, Int})
@test_throws ArgumentError code_llvm(is, Tuple{Int, Int})
@test_throws ArgumentError code_native(is, Tuple{Int, Int})
@test_throws ArgumentError Base.return_types(is, Tuple{Int, Int})

module TestingExported
using Base.Test
Expand Down

0 comments on commit 6a6ccb8

Please sign in to comment.