Skip to content

Commit 2bf20fa

Browse files
committed
feat: set_children! to work with vector
1 parent 5472e8a commit 2bf20fa

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/Node.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ end
199199
set_children!(n, Base.setindex(get_children(n), child, i))
200200
return child
201201
end
202-
@inline function set_children!(n::AbstractNode{D}, children::Tuple{Vararg{AbstractNode{D},D2}}) where {D,D2}
202+
@inline function set_children!(n::AbstractNode{D}, children::Union{Tuple,AbstractVector{<:AbstractNode{D}}}) where {D}
203+
D2 = length(children)
203204
if D === D2
204205
n.children = children
205206
else
@@ -209,7 +210,7 @@ end
209210
# This poison should be efficient to insert. So
210211
# for simplicity, we can just use poison == n, which
211212
# will trigger infinite recursion errors if accessed.
212-
n.children = ntuple(i -> i <= D2 ? children[i] : poison, Val(D))
213+
n.children = ntuple(i -> i <= D2 ? @inbounds(children[i]) : poison, Val(D))
213214
end
214215
end
215216

src/ReadOnlyNode.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ import ..NodeModule: default_allocator, with_type_parameters, constructorof, get
88
abstract type AbstractReadOnlyNode{T,D,N<:AbstractExpressionNode{T,D}} <:
99
AbstractExpressionNode{T,D} end
1010

11-
"""A type of expression node that prevents writing to the inner node"""
12-
struct ReadOnlyNode{T,D,N} <: AbstractReadOnlyNode{T,D,N}
13-
_inner::N
14-
15-
ReadOnlyNode(n::N) where {T,N<:AbstractExpressionNode{T}} = new{T,max_degree(N),N}(n)
16-
end
1711
@inline inner(n::AbstractReadOnlyNode) = getfield(n, :_inner)
18-
@unstable constructorof(::Type{<:ReadOnlyNode}) = ReadOnlyNode
1912
@inline function Base.getproperty(n::AbstractReadOnlyNode, s::Symbol)
2013
out = getproperty(inner(n), s)
2114
if out isa AbstractExpressionNode
@@ -25,12 +18,20 @@ end
2518
end
2619
end
2720
@inline function get_children(node::AbstractReadOnlyNode)
28-
return map(ReadOnlyNode, get_children(inner(node)))
21+
return map(constructorof(typeof(node)), get_children(inner(node)))
2922
end
3023
function Base.setproperty!(::AbstractReadOnlyNode, ::Symbol, v)
3124
return error("Cannot set properties on a ReadOnlyNode")
3225
end
3326
Base.propertynames(n::AbstractReadOnlyNode) = propertynames(inner(n))
34-
Base.copy(n::AbstractReadOnlyNode) = ReadOnlyNode(copy(inner(n)))
27+
Base.copy(n::AbstractReadOnlyNode) = constructorof(typeof(n))(copy(inner(n)))
28+
29+
"""A type of expression node that prevents writing to the inner node"""
30+
struct ReadOnlyNode{T,D,N} <: AbstractReadOnlyNode{T,D,N}
31+
_inner::N
32+
33+
ReadOnlyNode(n::N) where {T,N<:AbstractExpressionNode{T}} = new{T,max_degree(N),N}(n)
34+
end
35+
@unstable constructorof(::Type{<:ReadOnlyNode}) = ReadOnlyNode
3536

3637
end

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ end
1818
if "jet" in test_name
1919
@safetestset "JET" begin
2020
using Preferences
21-
set_preferences!("DynamicExpressions", "dispatch_doctor_mode" => "disable"; force=true)
21+
set_preferences!(
22+
"DynamicExpressions", "dispatch_doctor_mode" => "disable"; force=true
23+
)
2224
using JET
2325
using DynamicExpressions
2426
struct MyIgnoredModule

0 commit comments

Comments
 (0)