Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type inference for tuples in static parameters #9016

Merged
merged 1 commit into from
Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 54 additions & 20 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,39 @@ end
t_func[fieldtype] = (2, 2, fieldtype_tfunc)
t_func[Box] = (1, 1, (a,)->Box)

valid_tparam(x::ANY) = isa(x,Int) || isa(x,Symbol) || isa(x,Bool)
function valid_tparam(x::ANY)
if isa(x,Int) || isa(x,Symbol) || isa(x,Bool)
return true
elseif isa(x,Tuple)
for t in x
if !valid_tparam(t)
return false
end
end
return true
end
return false
end

function extract_simple_tparam(Ai)
if (isa(Ai,Int) || isa(Ai,Bool))
return Ai
elseif isa(Ai,QuoteNode) && valid_tparam(Ai.value)
return Ai.value
elseif isa(inference_stack,CallStack) && isa(Ai,Expr) &&
is_known_call(Ai,tuple,inference_stack.sv)
tup = ()
for arg in Ai.args[2:end]
val = extract_simple_tparam(arg)
if val === Bottom
return val
end
tup = tuple(tup...,val)
end
return tup
end
return Bottom
end

# TODO: handle e.g. apply_type(T, R::Union(Type{Int32},Type{Float64}))
const apply_type_tfunc = function (A, args...)
Expand All @@ -421,28 +453,30 @@ const apply_type_tfunc = function (A, args...)
tparams = tuple(tparams..., ai.parameters[1])
elseif isa(ai,Tuple) && all(isType,ai)
tparams = tuple(tparams..., map(t->t.parameters[1], ai))
elseif i<=lA && (isa(A[i],Int) || isa(A[i],Bool))
tparams = tuple(tparams..., A[i])
elseif i<=lA && isa(A[i],QuoteNode) && valid_tparam(A[i].value)
tparams = tuple(tparams..., A[i].value)
else
if i<=lA && isa(A[i],Symbol) && isa(inference_stack,CallStack)
sp = inference_stack.sv.sp
s = A[i]
found = false
for j=1:2:length(sp)
if is(sp[j].name,s)
# static parameter
val = sp[j+1]
if valid_tparam(val)
tparams = tuple(tparams..., val)
found = true
break
if i<=lA
val = extract_simple_tparam(A[i])
if val !== Bottom
tparams = tuple(tparams..., val)
continue
elseif isa(inference_stack,CallStack) && isa(A[i],Symbol)
sp = inference_stack.sv.sp
s = A[i]
found = false
for j=1:2:length(sp)
if is(sp[j].name,s)
# static parameter
val = sp[j+1]
if valid_tparam(val)
tparams = tuple(tparams..., val)
found = true
break
end
end
end
end
if found
continue
if found
continue
end
end
end
if i-1 > length(headtype.parameters)
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2123,3 +2123,10 @@ function f9947()
end
end
@test f9947() == UInt128(1)

# Type inference for tuple parameters
immutable fooTuple{s}; end
barTuple1() = fooTuple{(:y,)}()
barTuple2() = fooTuple{tuple(:y)}()

@test Base.return_types(barTuple1,())[1] == Base.return_types(barTuple2,())[1] == fooTuple{(:y,)}