Skip to content

Commit

Permalink
Better support for args with default values (#359)
Browse files Browse the repository at this point in the history
Julia's naming of slots with default values seems different for
`::Type{}` slots. Fixes #350.
  • Loading branch information
timholy authored Mar 8, 2023
1 parent 3ec146d commit bb49cd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion TypedSyntax/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TypedSyntax"
uuid = "d265eb64-f81a-44ad-a842-4247ee1503de"
authors = ["Tim Holy <[email protected]> and contributors"]
version = "1.0.2"
version = "1.0.3"

[deps]
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
Expand Down
22 changes: 18 additions & 4 deletions TypedSyntax/src/node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ TypedSyntaxData(sd::SyntaxData, src::CodeInfo, typ=nothing) = TypedSyntaxData(sd
const TypedSyntaxNode = TreeNode{TypedSyntaxData}
const MaybeTypedSyntaxNode = Union{SyntaxNode,TypedSyntaxNode}

struct NoDefaultValue end
const no_default_value = NoDefaultValue()

# These are TypedSyntaxNode constructor helpers
# Call these directly if you want both the TypedSyntaxNode and the `mappings` list,
# where `mappings[i]` corresponds to the list of nodes matching `(src::CodeInfo).code[i]`.
Expand Down Expand Up @@ -69,30 +72,36 @@ function TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, mappings, symtyps)
sig = child(sig, 1)
end
@assert kind(sig) == K"call"
i = 1
i = j = 1
for arg in Iterators.drop(children(sig), 1)
kind(arg) == K"parameters" && break # kw args
if kind(arg) == K"..."
arg = only(children(arg))
end
defaultval = no_default_value
if kind(arg) == K"="
arg = first(children(arg))
defaultval = child(arg, 2)
arg = child(arg, 1)
end
if kind(arg) == K"::"
nchildren = length(children(arg))
if nchildren == 1
# unnamed argument
argc = child(arg, 1)
found = false
while i <= length(src.slotnames)
if src.slotnames[i] == Symbol("#unused#")
if src.slotnames[i] == Symbol("#unused#") || (defaultval != no_default_value && kind(argc) == K"curly" && src.slotnames[i] == Symbol(""))
arg.typ = unwrapconst(src.slottypes[i])
i += 1
found = true
break
end
i += 1
end
@assert found
found && continue
@assert kind(argc) == K"curly"
arg.typ = unwrapconst(src.ssavaluetypes[j])
j += 1
continue
elseif nchildren == 2
arg = child(arg, 1) # extract the name
Expand All @@ -102,6 +111,11 @@ function TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, mappings, symtyps)
end
kind(arg) == K"Identifier" || @show sig arg
@assert kind(arg) == K"Identifier"
if i > length(src.slotnames)
@assert defaultval != no_default_value
arg.typ = Core.Typeof(unwrapconst(defaultval.val))
continue
end
argname = arg.val
while i <= length(src.slotnames)
if src.slotnames[i] == argname
Expand Down
11 changes: 11 additions & 0 deletions TypedSyntax/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ splats(x, y) = vcat(x..., y...)
myoftype(ref, val) = typeof(ref)(val)

defaultarg(x, y=2) = x + y
hasdefaulttypearg(::Type{T}=Rational{Int}) where T = zero(T)

charset1 = 'a':'z'
getchar1(idx) = charset1[idx]
Expand Down Expand Up @@ -243,13 +244,23 @@ end
tsn = TypedSyntaxNode(TSN.defaultarg, (Float32,))
sig, body = children(tsn)
@test has_name_typ(child(sig, 2), :x, Float32)
@test has_name_typ(child(sig, 3, 1), :y, Int)
# there is no argument 2 in tsn.typedsource
tsn = TypedSyntaxNode(TSN.defaultarg, (Float32,Int))
sig, body = children(tsn)
@test has_name_typ(child(sig, 2), :x, Float32)
nodearg = child(sig, 3)
@test kind(nodearg) == K"="
@test has_name_typ(child(nodearg, 1), :y, Int)
# default position args that are types
tsn = TypedSyntaxNode(TSN.hasdefaulttypearg, (Type{Float32},))
sig, body = children(tsn)
arg = child(sig, 1, 2, 1)
@test kind(arg) == K"::" && arg.typ === Type{Float32}
tsn = TypedSyntaxNode(TSN.hasdefaulttypearg, ())
sig, body = children(tsn)
arg = child(sig, 1, 2, 1)
@test kind(arg) == K"::" && arg.typ === Type{Rational{Int}}

# macros in function definition
tsn = TypedSyntaxNode(TSN.mysin, (Int,))
Expand Down

4 comments on commit bb49cd2

@timholy
Copy link
Member Author

@timholy timholy commented on bb49cd2 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir=TypedSyntax

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/79184

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a TypedSyntax-v1.0.3 -m "<description of version>" bb49cd2905ae675ca000288d1decc7d2c283ecfd
git push origin TypedSyntax-v1.0.3

@timholy
Copy link
Member Author

@timholy timholy commented on bb49cd2 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/79185

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.8.0 -m "<description of version>" bb49cd2905ae675ca000288d1decc7d2c283ecfd
git push origin v2.8.0

Please sign in to comment.