Skip to content

Commit fad4689

Browse files
hz-xiaxzdevmotion
andauthored
Add descriptive error to name collision (#46)
* add warning to name collision * fix 1.3 ci * avoid type collision in module * fix ci julia min * test and docstring * revert docstring change * apply suggested changes * test the real description * support only julia >1.10 * Update src/macro.jl Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com> * Update src/macro.jl Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com> * Bump version from 0.2.5 to 0.2.6 * Add more tests --------- Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
1 parent 5e76b4b commit fad4689

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IrrationalConstants"
22
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
33
authors = ["JuliaMath"]
4-
version = "0.2.5"
4+
version = "0.2.6"
55

66
[compat]
77
julia = "1.10"

src/macro.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ julia> IrrationalConstants.@irrational twoπ 2*big(π)
4747
julia> twoπ
4848
twoπ = 6.2831853071795...
4949
50+
julia> typeof(twoπ)
51+
Twoπ
52+
5053
julia> IrrationalConstants.@irrational sqrt2 1.4142135623730950488 √big(2)
5154
5255
julia> sqrt2
@@ -65,12 +68,18 @@ ERROR: AssertionError: Float64($(Expr(:escape, :sqrt5))) == Float64(big($(Expr(:
6568
```
6669
"""
6770
macro irrational(sym::Symbol, val::Float64, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
68-
irrational(sym, val, def, T)
71+
irrational(__module__, sym, val, def, T)
6972
end
7073
macro irrational(sym::Symbol, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
71-
irrational(sym, :(big($(esc(sym)))), def, T)
74+
irrational(__module__, sym, :(big($(esc(sym)))), def, T)
7275
end
73-
function irrational(sym::Symbol, val::Union{Float64,Expr}, def::Union{Symbol,Expr}, T::Symbol)
76+
function irrational(mod::Module, sym::Symbol, val::Union{Float64,Expr}, def::Union{Symbol,Expr}, T::Symbol)
77+
if isdefined(mod, T)
78+
throw(ArgumentError(LazyString("Type `", T, "` of irrational constant `", sym, "` is already defined in module `", mod, "`.")))
79+
end
80+
if sym == T
81+
throw(ArgumentError(LazyString("The name of the irrational constant (", sym, ") and its type (", T, ") cannot be the same. Please choose a different name for the constant or specify a different type name as the last argument to the macro.")))
82+
end
7483
esym = esc(sym)
7584
qsym = esc(Expr(:quote, sym))
7685
eT = esc(T)

test/runtests.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,27 @@ end
257257
@testset "slow comparisons" begin
258258
@test iszero(@allocated(3.0 <= invsqrt2))
259259
end
260+
261+
# issues #43
262+
@testset "macro error" begin
263+
msg = "The name of the irrational constant (Myπ) and its type (Myπ) cannot be the same. Please choose a different name for the constant or specify a different type name as the last argument to the macro."
264+
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ big(π))
265+
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ 1.0 big(π))
266+
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ big(π) Myπ)
267+
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ 1.0 big(π) Myπ)
268+
end
269+
270+
# test that defining a type that already exists throws an error
271+
module TestTypeCollision
272+
using IrrationalConstants
273+
using Test
274+
struct MyExistingType end
275+
276+
@testset "type collision" begin
277+
msg1 = "Type `MyExistingType` of irrational constant `myExistingType` is already defined in module `Main.TestTypeCollision`."
278+
@test_throws ArgumentError(msg1) @macroexpand(IrrationalConstants.@irrational myExistingType big(π))
279+
msg2 = "Type `MyExistingType` of irrational constant `myconst` is already defined in module `Main.TestTypeCollision`."
280+
@test_throws ArgumentError(msg2) @macroexpand(IrrationalConstants.@irrational myconst big(π) MyExistingType)
281+
@test_throws ArgumentError(msg2) @macroexpand(IrrationalConstants.@irrational myconst 1.0 big(π) MyExistingType)
282+
end
283+
end

0 commit comments

Comments
 (0)