Skip to content

Commit ddde81f

Browse files
committed
Merge branch 'master' into fieldnames
2 parents 1b14634 + 6efc9fa commit ddde81f

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/bindings.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ function add_binding(x, state, scope = state.scope)
329329
end
330330
end
331331
end
332+
# hoist binding for inner constructor to parent scope
333+
if (typof(scope.expr) === CSTParser.Struct || typof(scope.expr) === CSTParser.Mutable) && CSTParser.defines_function(x) && parentof(scope) isa Scope
334+
return add_binding(x, state, parentof(scope))
335+
end
332336
scope.names[name] = bindingof(x)
333337
end
334338
end

src/linting/checks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ end
507507
function refers_to_nonimported_type(arg::EXPR)
508508
if hasref(arg) && refof(arg) isa Binding
509509
return true
510-
elseif typof(arg) === CSTParser.UnaryOpCall && length(arg) == 2 && kindof(arg[1]) === CSTParser.Tokens.DECLARATION
510+
elseif typof(arg) === CSTParser.UnaryOpCall && length(arg) == 2 && (kindof(arg[1]) === CSTParser.Tokens.DECLARATION || kindof(arg[1]) === CSTParser.Tokens.ISSUBTYPE)
511511
return refers_to_nonimported_type(arg[2])
512512
elseif _binary_assert(arg, CSTParser.Tokens.DECLARATION)
513513
return refers_to_nonimported_type(arg[3])

src/scope.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function introduces_scope(x::EXPR, state)
4141
elseif typof(x) === CSTParser.WhereOpCall
4242
# unless in func def signature
4343
return !_in_func_def(x)
44+
elseif typof(x) === CSTParser.TupleH && length(x) > 2 && typof(x[1]) === CSTParser.PUNCTUATION && is_assignment(x[2])
45+
return true
4446
elseif typof(x) === CSTParser.FunctionDef ||
4547
typof(x) === CSTParser.Macro ||
4648
typof(x) === CSTParser.For ||

test/runtests.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,23 @@ end
464464
StaticLint.check_for_pirates(cst[2])
465465
@test errorof(cst[2]) === nothing
466466
end
467+
let cst = parse_and_pass("""
468+
import Base:sin
469+
abstract type T end
470+
sin(x::Array{T}) = 1
471+
sin(x::Array{<:T}) = 1
472+
sin(x::Array{Number}) = 1
473+
sin(x::Array{<:Number}) = 1
474+
""")
475+
StaticLint.check_for_pirates(cst[3])
476+
StaticLint.check_for_pirates(cst[4])
477+
StaticLint.check_for_pirates(cst[5])
478+
StaticLint.check_for_pirates(cst[6])
479+
@test errorof(cst[3]) === nothing
480+
@test errorof(cst[4]) === nothing
481+
@test errorof(cst[5]) === StaticLint.TypePiracy
482+
@test errorof(cst[6]) === StaticLint.TypePiracy
483+
end
467484
end
468485

469486
@testset "docs for undescribed variables" begin
@@ -790,6 +807,18 @@ end
790807
end
791808
end
792809

810+
@testset "hoisting of inner constructors" begin
811+
let cst = parse_and_pass("""
812+
struct ASDF
813+
x::Int
814+
y::Int
815+
ASDF(x::Int) = new(x, 1)
816+
end
817+
ASDF() = something
818+
""")
819+
@test bindingof(cst[1]) === bindingof(cst[1][3][3]).prev
820+
@test bindingof(cst[1][3][3]) === bindingof(cst[2]).prev
821+
end
793822
include("type_inf.jl")
794823
end
795824
end

0 commit comments

Comments
 (0)