Skip to content

Commit 38101d1

Browse files
committed
fix: node conversion changing degree
1 parent 633cc94 commit 38101d1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Node.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,20 @@ end
218218
Base.eltype(::Type{<:AbstractExpressionNode{T}}) where {T} = T
219219
Base.eltype(::AbstractExpressionNode{T}) where {T} = T
220220

221-
max_degree(::Type{<:AbstractNode}) = 2 # Default
221+
const DEFAULT_MAX_DEGREE = 2
222+
max_degree(::Type{<:AbstractNode}) = DEFAULT_MAX_DEGREE
222223
max_degree(::Type{<:AbstractNode{D}}) where {D} = D
223224
max_degree(node::AbstractNode) = max_degree(typeof(node))
224225

225-
@unstable constructorof(::Type{N}) where {N<:Node} = Node{T,max_degree(N)} where {T}
226-
@unstable constructorof(::Type{N}) where {N<:GraphNode} =
227-
GraphNode{T,max_degree(N)} where {T}
226+
has_max_degree(::Type{<:AbstractNode}) = false
227+
has_max_degree(::Type{<:AbstractNode{D}}) where {D} = true
228+
229+
@unstable function constructorof(::Type{N}) where {N<:Node}
230+
return Node{T,max_degree(N)} where {T}
231+
end
232+
@unstable function constructorof(::Type{N}) where {N<:GraphNode}
233+
return GraphNode{T,max_degree(N)} where {T}
234+
end
228235

229236
function with_type_parameters(::Type{N}, ::Type{T}) where {N<:Node,T}
230237
return Node{T,max_degree(N)}
@@ -233,6 +240,9 @@ function with_type_parameters(::Type{N}, ::Type{T}) where {N<:GraphNode,T}
233240
return GraphNode{T,max_degree(N)}
234241
end
235242

243+
with_max_degree(::Type{N}, ::Val{D}) where {T,N<:Node{T},D} = Node{T,D}
244+
with_max_degree(::Type{N}, ::Val{D}) where {T,N<:GraphNode{T},D} = GraphNode{T,D}
245+
236246
function default_allocator(::Type{N}, ::Type{T}) where {N<:AbstractExpressionNode,T}
237247
return with_type_parameters(N, T)()
238248
end

src/base.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,11 @@ using `convert(T1, tree.val)` at constant nodes.
520520
"""
521521
function convert(
522522
::Type{N1}, tree::N2
523-
) where {T1,T2,N1<:AbstractExpressionNode{T1},N2<:AbstractExpressionNode{T2}}
523+
) where {T1,T2,D1,D2,N1<:AbstractExpressionNode{T1,D1},N2<:AbstractExpressionNode{T2,D2}}
524524
if N1 === N2
525525
return tree
526526
end
527+
@assert max_degree(N1) == max_degree(N2)
527528
return tree_mapreduce(
528529
Base.Fix1(leaf_convert, N1),
529530
identity,
@@ -533,6 +534,11 @@ function convert(
533534
)
534535
# TODO: Need to allow user to overload this!
535536
end
537+
function convert(
538+
::Type{N1}, tree::N2
539+
) where {T1,T2,D2,N1<:AbstractExpressionNode{T1},N2<:AbstractExpressionNode{T2,D2}}
540+
return convert(with_max_degree(N1, Val(D2)), tree)
541+
end
536542
function convert(
537543
::Type{N1}, tree::N2
538544
) where {T2,N1<:AbstractExpressionNode,N2<:AbstractExpressionNode{T2}}

0 commit comments

Comments
 (0)