Skip to content

Commit 07f434b

Browse files
committed
Make macro more robust against symbol overlap
1 parent 9caa14b commit 07f434b

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/OperatorEnumConstruction.jl

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -201,63 +201,64 @@ function _extend_operators(operators, skip_user_operators, kws, __module__::Modu
201201
else
202202
true
203203
end
204-
binary_ex = _extend_binary_operator(:f, :type_requirements, :build_converters)
205-
unary_ex = _extend_unary_operator(:f, :type_requirements)
204+
@gensym f skip type_requirements build_converters binary_exists unary_exists
205+
binary_ex = _extend_binary_operator(f, type_requirements, build_converters)
206+
unary_ex = _extend_unary_operator(f, type_requirements)
206207
return quote
207-
local type_requirements
208-
local build_converters
209-
local binary_exists
210-
local unary_exists
208+
local $type_requirements
209+
local $build_converters
210+
local $binary_exists
211+
local $unary_exists
211212
if isa($operators, $OperatorEnum)
212-
type_requirements = Number
213-
build_converters = true
214-
binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum
215-
unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum
213+
$type_requirements = Number
214+
$build_converters = true
215+
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).operator_enum
216+
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).operator_enum
216217
else
217-
type_requirements = Any
218-
build_converters = false
219-
binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum
220-
unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum
218+
$type_requirements = Any
219+
$build_converters = false
220+
$binary_exists = $(ALREADY_DEFINED_BINARY_OPERATORS).generic_operator_enum
221+
$unary_exists = $(ALREADY_DEFINED_UNARY_OPERATORS).generic_operator_enum
221222
end
222223
if $(empty_old_operators)
223224
# Trigger errors if operators are not yet defined:
224225
empty!($(LATEST_BINARY_OPERATOR_MAPPING))
225226
empty!($(LATEST_UNARY_OPERATOR_MAPPING))
226227
end
227228
for (op, func) in enumerate($(operators).binops)
228-
local f = Symbol(func)
229-
local skip = false
230-
if isdefined(Base, f)
231-
f = :(Base.$(f))
229+
local $f = Symbol(func)
230+
local $skip = false
231+
if isdefined(Base, $f)
232+
$f = :(Base.$($f))
232233
elseif $(skip_user_operators)
233-
skip = true
234+
$skip = true
234235
else
235-
f = :($($__module__).$(f))
236+
$f = :($($__module__).$($f))
236237
end
237238
$(LATEST_BINARY_OPERATOR_MAPPING)[func] = op
238-
skip && continue
239+
$skip && continue
239240
# Avoid redefining methods:
240-
if !haskey(unary_exists, func)
241+
if !haskey($unary_exists, func)
241242
eval($binary_ex)
242-
unary_exists[func] = true
243+
$(unary_exists)[func] = true
243244
end
244245
end
245246
for (op, func) in enumerate($(operators).unaops)
246-
local f = Symbol(func)
247-
local skip = false
248-
if isdefined(Base, f)
249-
f = :(Base.$(f))
247+
local $f = Symbol(func)
248+
local $skip = false
249+
if isdefined(Base, $f)
250+
$f = :(Base.$($f))
250251
elseif $(skip_user_operators)
251-
skip = true
252+
$skip = true
252253
else
253-
f = :($($__module__).$(f))
254+
$f = :($($__module__).$($f))
254255
end
255256
$(LATEST_UNARY_OPERATOR_MAPPING)[func] = op
256-
skip && continue
257+
$skip && continue
257258
# Avoid redefining methods:
258-
if !haskey(binary_exists, func)
259+
if !haskey($binary_exists, func)
259260
eval($unary_ex)
260-
binary_exists[func] = true
261+
$(binary_exists)[func] = true
261262
end
262263
end
263264
end

0 commit comments

Comments
 (0)