Skip to content

Commit 66d3ff2

Browse files
committed
Fix kwarg ref/binding handling
1 parent 665aae8 commit 66d3ff2

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/references.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ function resolve_ref(x::EXPR, scope::Scope, state::State)::Bool
5151
return resolve_getfield(x, scope, state)
5252
elseif iskwarg(x)
5353
# Note to self: this seems wronge - Binding should be attached to entire Kw EXPR.
54-
if isidentifier(x.args[1])
55-
setref!(x.args[1], Binding(noname, nothing, nothing, []))
56-
elseif isdeclaration(x.args[1]) && isidentifier(x.args[1].args[1])
57-
setref!(x.args[1].args[1], Binding(noname, nothing, nothing, []))
54+
if isidentifier(x.args[1]) && !hasbinding(x.args[1])
55+
setref!(x.args[1], Binding(x.args[1], nothing, nothing, []))
56+
elseif isdeclaration(x.args[1]) && isidentifier(x.args[1].args[1]) && !hasbinding(x.args[1].args[1])
57+
if hasbinding(x.args[1])
58+
setref!(x.args[1].args[1], bindingof(x.args[1]))
59+
else
60+
setref!(x.args[1].args[1], Binding(x.args[1], nothing, nothing, []))
61+
end
5862
end
5963
return true
6064
elseif is_special_macro_term(x) || new_within_struct(x)

test/runtests.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,3 +1789,40 @@ end
17891789
""")
17901790
@test isempty(StaticLint.collect_hints(cst, server))
17911791
end
1792+
1793+
@testset "kwarg refs" begin
1794+
cst = parse_and_pass("""
1795+
function foo(aaa, bbb; ccc)
1796+
return aaa + bbb + ccc
1797+
end
1798+
""")
1799+
for (_, b) in cst.args[1].meta.scope.names
1800+
@test length(b.refs) == 2
1801+
end
1802+
1803+
cst = parse_and_pass("""
1804+
function foo(aaa, bbb::Foo; ccc::Bar)
1805+
return aaa + bbb + ccc
1806+
end
1807+
""")
1808+
for (_, b) in cst.args[1].meta.scope.names
1809+
@test length(b.refs) == 2
1810+
end
1811+
1812+
cst = parse_and_pass("""
1813+
function foo(aaa, bbb=1; ccc=2)
1814+
return aaa + bbb + ccc
1815+
end
1816+
""")
1817+
for (_, b) in cst.args[1].meta.scope.names
1818+
@test length(b.refs) == 2
1819+
end
1820+
cst = parse_and_pass("""
1821+
function foo(aaa, bbb::Foo=1; ccc::Bar=2)
1822+
return aaa + bbb + ccc
1823+
end
1824+
""")
1825+
for (_, b) in cst.args[1].meta.scope.names
1826+
@test length(b.refs) == 2
1827+
end
1828+
end

0 commit comments

Comments
 (0)