From 2de6d1c762878be9967b3c66c80bd186e723f363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 14 Aug 2025 15:45:31 +0200 Subject: [PATCH 1/3] Simplify promote_type_fallback --- src/interface.jl | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 33dd132..2526d38 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -25,37 +25,11 @@ function promote_operation_fallback( ) end -_instantiate_zero(::Type{S}) where {S} = zero(S) -_instantiate_oneunit(::Type{S}) where {S} = oneunit(S) - -# this is valid because Irrational numbers are defined in global scope as const -_instantiate(::Type{S}) where {S<:Irrational} = S() -_instantiate_zero(::Type{S}) where {S<:AbstractIrrational} = _instantiate(S) -_instantiate_oneunit(::Type{S}) where {S<:AbstractIrrational} = _instantiate(S) - -# Julia v1.0.x has trouble with inference with the `Vararg` method, see -# https://travis-ci.org/jump-dev/JuMP.jl/jobs/617606373 -function promote_operation_fallback( - op::Union{typeof(/),typeof(div)}, - ::Type{S}, - ::Type{T}, -) where {S,T} - return typeof(op(_instantiate_zero(S), _instantiate_oneunit(T))) -end - -function promote_operation_fallback( - op::F, - ::Type{S}, - ::Type{T}, -) where {F<:Function,S,T} - return typeof(op(_instantiate_zero(S), _instantiate_zero(T))) -end - function promote_operation_fallback( op::F, args::Vararg{Type,N}, ) where {F<:Function,N} - return typeof(op(_instantiate_zero.(args)...)) + return promote_type(args...) end promote_operation_fallback(::typeof(*), ::Type{T}) where {T} = T From 1df1f9127e1b8b10aa8e9993b72c67ee9686eb49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 14 Aug 2025 16:01:04 +0200 Subject: [PATCH 2/3] Remove _instantiate from tests --- test/interface.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/interface.jl b/test/interface.jl index 8c4262a..3334867 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -51,11 +51,6 @@ Base.@irrational theodorus 1.73205080756887729353 sqrt(big(3)) i_theodorus() = MA.promote_operation(+, Int, typeof(theodorus)) @test i_theodorus() == Float64 alloc_test(i_theodorus, 0) - # test _instantiate(::Type{S}) where {S<:Irrational} return value - @test MA._instantiate(typeof(π)) == π - @test MA._instantiate(typeof(MathConstants.catalan)) == - MathConstants.catalan - @test MA._instantiate(typeof(theodorus)) == theodorus end end From 0267f18197134f8842fdc1090f6f25721ac4dc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 14 Aug 2025 17:23:00 +0200 Subject: [PATCH 3/3] Fixes --- src/interface.jl | 2 +- test/broadcast.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 2526d38..bb2af89 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -26,7 +26,7 @@ function promote_operation_fallback( end function promote_operation_fallback( - op::F, + ::F, args::Vararg{Type,N}, ) where {F<:Function,N} return promote_type(args...) diff --git a/test/broadcast.jl b/test/broadcast.jl index 53b57ee..e6bbc72 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -31,7 +31,7 @@ end @test y == 5 # FIXME This should not allocate but I couldn't figure out where these # allocations come from. - n = (VERSION >= v"1.11" ? 42 : 30) * sizeof(Int) + n = (VERSION >= v"1.11" ? 14 : 10) * sizeof(Int) alloc_test(() -> MA.broadcast!!(+, a, b), n) alloc_test(() -> MA.broadcast!!(+, a, c), 0) end