Skip to content

Commit

Permalink
Eliminate keytype, valuetype
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Mar 19, 2017
1 parent b2e011d commit 90d1a3b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 62 deletions.
87 changes: 32 additions & 55 deletions src/guesstype.jl
@@ -1,14 +1,5 @@
using Base: isexported

keytype(::Type{Any}) = Any
valuetype(::Type{Any}) = Any

keytype{K,V}(::Type{Associative{K,V}}) = K
valuetype{K,V}(::Type{Associative{K,V}}) = V

keytype{T<:Associative}(::Type{T}) = keytype(supertype(T))
valuetype{T<:Associative}(::Type{T}) = valuetype(supertype(T))

function arraytype_dims(elt, dimst)
tuplen = StaticTypeAnalysis.length(dimst)
if isnull(tuplen)
Expand Down Expand Up @@ -88,62 +79,46 @@ function parsetype(ex)
end
end

function guesstype(ex, ctx::LintContext)
t = typeof(ex)
if t <: Number
return t
end
if t <: AbstractString
return t
function guesstype(ex::Symbol, ctx::LintContext)
stacktop = ctx.callstack[end]
sym = ex
for i in length(stacktop.localvars):-1:1
if haskey(stacktop.localvars[i], sym)
ret = stacktop.localvars[i][sym].typeactual
return ret
end
end
if t == Symbol # check if we have seen it
stacktop = ctx.callstack[end]
sym = ex
for i in length(stacktop.localvars):-1:1
if haskey(stacktop.localvars[i], sym)
ret = stacktop.localvars[i][sym].typeactual
return ret
end
for i in length(stacktop.localarguments):-1:1
if haskey(stacktop.localarguments[i], sym)
ret = stacktop.localarguments[i][sym].typeactual
return ret
end
for i in length(stacktop.localarguments):-1:1
if haskey(stacktop.localarguments[i], sym)
ret = stacktop.localarguments[i][sym].typeactual
return ret
end
end
for i in length(ctx.callstack):-1:1
if sym in ctx.callstack[i].types
return Type
end
for i in length(ctx.callstack):-1:1
if in(sym, ctx.callstack[i].types)
return Type
end
if in(sym, ctx.callstack[i].functions)
return Function
end
if in(sym, ctx.callstack[i].modules)
return Module
end
if sym in ctx.callstack[i].functions
return Function
end
val = stdlibobject(ex)
if !isnull(val)
if isa(get(val), Type)
return Type{get(val)}
else
return typeof(get(val))
end
if sym in ctx.callstack[i].modules
return Module
end
return Any
end

if t == QuoteNode
return typeof(ex.value)
end

if t != Expr
return Any
val = stdlibobject(ex)
if !isnull(val)
if isa(get(val), Type)
return Type{get(val)}
else
return typeof(get(val))
end
end
return Any
end

function guesstype(ex::Expr, ctx::LintContext)
ex = ExpressionUtils.expand_trivial_calls(ex)


if isexpr(ex, :tuple)
ts = Type[]
for a in ex.args
Expand Down Expand Up @@ -329,3 +304,5 @@ function guesstype(ex, ctx::LintContext)

return Any
end

guesstype(ex, ctx::LintContext) = lexicaltypeof(ex)
2 changes: 1 addition & 1 deletion src/statictype.jl
Expand Up @@ -13,7 +13,7 @@ Given types `S` and `T`, return `Nullable(false)` if it is not possible for
`s::S == t::T`. Return `Nullable(true)` if it is possible, and
`Nullable{Bool}()` if it cannot be determined.
```jldoctest
``jldoctest
julia> StaticTypeAnalysis.canequal(Int, Float64)
Nullable(true)
Expand Down
8 changes: 2 additions & 6 deletions src/variables.jl
Expand Up @@ -198,14 +198,10 @@ function lintassignment(ex::Expr, assign_ops::Symbol, ctx::LintContext; islocal
msg(ctx, :I672, "iteration works for a number but it may be a typo")
end

if rhstype <: Union{Tuple,Set,Array,Range,Enumerate}
rhstype = StaticTypeAnalysis.eltype(rhstype)
elseif rhstype <: Associative
rhstype = Tuple{keytype(rhstype), valuetype(rhstype)}
end
rhstype = StaticTypeAnalysis.eltype(rhstype)

# TODO: only when LHS is tuple
if rhstype <: Tuple
if rhstype <: Union{Pair, Tuple}
computedlength = StaticTypeAnalysis.length(rhstype)
if !isnull(computedlength) && get(computedlength) tuplelen
msg(ctx, :I474, rhstype, "iteration generates tuples, " *
Expand Down

0 comments on commit 90d1a3b

Please sign in to comment.