Skip to content

Commit b142f46

Browse files
committed
fix #11238, better tuple type checks in reflection
1 parent 1c938c0 commit b142f46

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

base/error.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
## native julia error handling ##
2020

21-
error(s::AbstractString) = throw(Main.Base.ErrorException(s))
22-
error(s...) = throw(Main.Base.ErrorException(Main.Base.string(s...)))
21+
error(s::AbstractString) = throw(Main.Base.call(Main.Base.ErrorException,s))
22+
error(s...) = throw(Main.Base.call(Main.Base.ErrorException,Main.Base.string(s...)))
2323

2424
rethrow() = ccall(:jl_rethrow, Void, ())::Bottom
2525
rethrow(e) = ccall(:jl_rethrow_other, Void, (Any,), e)::Bottom

base/inference.jl

+4-12
Original file line numberDiff line numberDiff line change
@@ -3279,15 +3279,11 @@ function replace_getfield!(ast, e::ANY, tupname, vals, sv, i0)
32793279
end
32803280

32813281
function code_typed(f, types::ANY; optimize=true)
3282-
if isa(types,Tuple)
3283-
types = Tuple{types...}
3284-
end
3282+
types = to_tuple_type(types)
32853283
code_typed(call, Tuple{isa(f,Type)?Type{f}:typeof(f), types.parameters...}, optimize=optimize)
32863284
end
32873285
function code_typed(f::Function, types::ANY; optimize=true)
3288-
if isa(types,Tuple)
3289-
types = Tuple{types...}
3290-
end
3286+
types = to_tuple_type(types)
32913287
asts = []
32923288
for x in _methods(f,types,-1)
32933289
linfo = func_for_method(x[3],types,x[2])
@@ -3306,15 +3302,11 @@ function code_typed(f::Function, types::ANY; optimize=true)
33063302
end
33073303

33083304
function return_types(f, types::ANY)
3309-
if isa(types,Tuple)
3310-
types = Tuple{types...}
3311-
end
3305+
types = to_tuple_type(types)
33123306
return_types(call, Tuple{isa(f,Type)?Type{f}:typeof(f), types.parameters...})
33133307
end
33143308
function return_types(f::Function, types::ANY)
3315-
if isa(types,Tuple)
3316-
types = Tuple{types...}
3317-
end
3309+
types = to_tuple_type(types)
33183310
rt = []
33193311
for x in _methods(f,types,-1)
33203312
linfo = func_for_method(x[3],types,x[2])

base/reflection.jl

+18-5
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,24 @@ isgeneric(f::ANY) = (isa(f,Function) && isa(f.env,MethodTable))
105105

106106
function_name(f::Function) = isgeneric(f) ? f.env.name : (:anonymous)
107107

108+
function to_tuple_type(t::ANY)
109+
if isa(t,Tuple) || isa(t,AbstractArray) || isa(t,SimpleVector)
110+
t = Tuple{t...}
111+
end
112+
if isa(t,Type) && t<:Tuple
113+
if !all(p->(isa(p,Type)||isa(p,TypeVar)), t.parameters)
114+
error("argument tuple type must contain only types")
115+
end
116+
else
117+
error("expected tuple type")
118+
end
119+
t
120+
end
121+
108122
tt_cons(t::ANY, tup::ANY) = Tuple{t, (isa(tup, Type) ? tup.parameters : tup)...}
109123

110124
code_lowered(f, t::ANY) = map(m->uncompressed_ast(m.func.code), methods(f, t))
111-
methods(f::Function,t::ANY) = Any[m[3] for m in _methods(f,t,-1)]
125+
methods(f::Function,t::ANY) = (t=to_tuple_type(t); Any[m[3] for m in _methods(f,t,-1)])
112126
methods(f::ANY,t::ANY) = methods(call, tt_cons(isa(f,Type) ? Type{f} : typeof(f), t))
113127
function _methods(f::ANY,t::ANY,lim)
114128
if isa(t,Type)
@@ -170,9 +184,8 @@ uncompressed_ast(l::LambdaStaticData) =
170184
isa(l.ast,Expr) ? l.ast : ccall(:jl_uncompress_ast, Any, (Any,Any), l, l.ast)
171185

172186
# Printing code representations in IR and assembly
173-
_dump_function(f, t::Tuple{Vararg{Type}}, native, wrapper, strip_ir_metadata) =
174-
_dump_function(f, Tuple{t...}, native, wrapper, strip_ir_metadata)
175187
function _dump_function(f, t::ANY, native, wrapper, strip_ir_metadata)
188+
t = to_tuple_type(t)
176189
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Any, Bool), f, t, wrapper)
177190

178191
if llvmf == C_NULL
@@ -183,7 +196,7 @@ function _dump_function(f, t::ANY, native, wrapper, strip_ir_metadata)
183196
str = ccall(:jl_dump_function_asm, Any, (Ptr{Void},), llvmf)::ByteString
184197
else
185198
str = ccall(:jl_dump_function_ir, Any,
186-
(Ptr{Void}, Bool), llvmf, strip_ir_metadata)::ByteString
199+
(Ptr{Void}, Bool), llvmf, strip_ir_metadata)::ByteString
187200
end
188201

189202
return str
@@ -208,8 +221,8 @@ if isdefined(Core, :Inference) && not_int(is(current_module(), Core.Inference))
208221
return_types(args...;kwargs...) = Core.Inference.return_types(args...;kwargs...)
209222
end
210223

211-
which(f::ANY, t::Tuple{Vararg{Type}}) = which(f, Tuple{t...})
212224
function which(f::ANY, t::ANY)
225+
t = to_tuple_type(t)
213226
if isleaftype(t)
214227
ms = methods(f, t)
215228
isempty(ms) && error("no method found for the specified argument types")

0 commit comments

Comments
 (0)