Skip to content

Commit d04d349

Browse files
committed
test: include test of ambiguous operator error
1 parent bf4607c commit d04d349

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

src/OperatorEnumConstruction.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ function _unpack_broadcast_function(f)
118118
end
119119
end
120120

121+
function _validate_no_ambiguous_broadcasts(operators::AbstractOperatorEnum)
122+
for ops in (operators.binops, operators.unaops), op in ops
123+
if op isa Broadcast.BroadcastFunction &&
124+
(op.f in operators.binops || op.f in operators.unaops)
125+
throw(
126+
ArgumentError(
127+
"Usage of both broadcasted and unbroadcasted operator `$(op.f)` is ambiguous",
128+
),
129+
)
130+
end
131+
end
132+
return nothing
133+
end
134+
121135
function empty_all_globals!(; force=true)
122136
if force || islocked(LATEST_LOCK)
123137
lock(LATEST_LOCK) do
@@ -280,6 +294,7 @@ function _extend_operators(operators, skip_user_operators, kws, __module__::Modu
280294
#! format: off
281295
return quote
282296
local $type_requirements, $build_converters, $binary_exists, $unary_exists
297+
$(_validate_no_ambiguous_broadcasts)($operators)
283298
lock($LATEST_LOCK)
284299
if isa($operators, $OperatorEnum)
285300
$type_requirements = $(on_type == nothing ? Number : on_type)
@@ -437,17 +452,6 @@ redefine operators for `AbstractExpressionNode` types, as well as `show`, `print
437452
end
438453
end
439454

440-
for ops in (binary_operators, unary_operators), op in ops
441-
if op isa Broadcast.BroadcastFunction &&
442-
(op.f in binary_operators || op.f in unary_operators)
443-
throw(
444-
ArgumentError(
445-
"Usage of both broadcasted and unbroadcasted operator $(op.f) is ambiguous",
446-
),
447-
)
448-
end
449-
end
450-
451455
operators = OperatorEnum(Tuple(binary_operators), Tuple(unary_operators))
452456

453457
if define_helper_functions

test/test_broadcasted_operators.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@testitem "Test broadcasted operators in enum" begin
2+
using DynamicExpressions
3+
4+
operators = OperatorEnum(;
5+
binary_operators=[*, +, (.-)],
6+
unary_operators=[sin, Broadcast.BroadcastFunction(cos)],
7+
)
8+
@extend_operators operators
9+
10+
x1 = Node{Float64}(; feature=1)
11+
x2 = Node{Float64}(; feature=2)
12+
13+
@test x1 + x2 isa Node{Float64}
14+
@test repr(x1 + x2) == "x1 + x2"
15+
@test x1 - x2 isa Node{Float64}
16+
@test repr(x1 - x2) == "x1 .- x2"
17+
18+
@test cos(x1) isa Node{Float64}
19+
@test repr(cos(x1)) == "cos.(x1)"
20+
end
21+
22+
@testitem "Test error upon ambiguous operators" begin
23+
using DynamicExpressions
24+
25+
operators = OperatorEnum(;
26+
unary_operators=[cos, Broadcast.BroadcastFunction(cos)],
27+
define_helper_functions=false,
28+
)
29+
@test_throws ArgumentError @eval begin
30+
@extend_operators operators
31+
end
32+
if VERSION >= v"1.9"
33+
@test_throws "Usage of both broadcasted and unbroadcasted operator `cos` is ambiguous" @eval begin
34+
@extend_operators operators
35+
end
36+
end
37+
end

test/unittest.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ end
6262
include("test_non_number_eval_tree_array.jl")
6363
end
6464

65+
include("test_broadcasted_operators.jl")
66+
6567
@testitem "Test hash of tree" begin
6668
include("test_hash.jl")
6769
end

0 commit comments

Comments
 (0)