Skip to content

Commit

Permalink
Merge branch 'master' into fieldnames
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacLN committed Apr 28, 2020
2 parents 1b14634 + 6efc9fa commit ddde81f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/bindings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ function add_binding(x, state, scope = state.scope)
end
end
end
# hoist binding for inner constructor to parent scope
if (typof(scope.expr) === CSTParser.Struct || typof(scope.expr) === CSTParser.Mutable) && CSTParser.defines_function(x) && parentof(scope) isa Scope
return add_binding(x, state, parentof(scope))
end
scope.names[name] = bindingof(x)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ end
function refers_to_nonimported_type(arg::EXPR)
if hasref(arg) && refof(arg) isa Binding
return true
elseif typof(arg) === CSTParser.UnaryOpCall && length(arg) == 2 && kindof(arg[1]) === CSTParser.Tokens.DECLARATION
elseif typof(arg) === CSTParser.UnaryOpCall && length(arg) == 2 && (kindof(arg[1]) === CSTParser.Tokens.DECLARATION || kindof(arg[1]) === CSTParser.Tokens.ISSUBTYPE)
return refers_to_nonimported_type(arg[2])
elseif _binary_assert(arg, CSTParser.Tokens.DECLARATION)
return refers_to_nonimported_type(arg[3])
Expand Down
2 changes: 2 additions & 0 deletions src/scope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function introduces_scope(x::EXPR, state)
elseif typof(x) === CSTParser.WhereOpCall
# unless in func def signature
return !_in_func_def(x)
elseif typof(x) === CSTParser.TupleH && length(x) > 2 && typof(x[1]) === CSTParser.PUNCTUATION && is_assignment(x[2])
return true
elseif typof(x) === CSTParser.FunctionDef ||
typof(x) === CSTParser.Macro ||
typof(x) === CSTParser.For ||
Expand Down
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,23 @@ end
StaticLint.check_for_pirates(cst[2])
@test errorof(cst[2]) === nothing
end
let cst = parse_and_pass("""
import Base:sin
abstract type T end
sin(x::Array{T}) = 1
sin(x::Array{<:T}) = 1
sin(x::Array{Number}) = 1
sin(x::Array{<:Number}) = 1
""")
StaticLint.check_for_pirates(cst[3])
StaticLint.check_for_pirates(cst[4])
StaticLint.check_for_pirates(cst[5])
StaticLint.check_for_pirates(cst[6])
@test errorof(cst[3]) === nothing
@test errorof(cst[4]) === nothing
@test errorof(cst[5]) === StaticLint.TypePiracy
@test errorof(cst[6]) === StaticLint.TypePiracy
end
end

@testset "docs for undescribed variables" begin
Expand Down Expand Up @@ -790,6 +807,18 @@ end
end
end

@testset "hoisting of inner constructors" begin
let cst = parse_and_pass("""
struct ASDF
x::Int
y::Int
ASDF(x::Int) = new(x, 1)
end
ASDF() = something
""")
@test bindingof(cst[1]) === bindingof(cst[1][3][3]).prev
@test bindingof(cst[1][3][3]) === bindingof(cst[2]).prev
end
include("type_inf.jl")
end
end

0 comments on commit ddde81f

Please sign in to comment.