From c8a2e5df156c2af3a36197be804b08ea22174e7e Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Sun, 16 Mar 2025 09:26:34 +0100 Subject: [PATCH] fix special function `::Real` fallback stack overflow Fixes #57789 --- base/math.jl | 2 +- test/math.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/base/math.jl b/base/math.jl index 0e5f9dd41bdca..beef8de09bd8b 100644 --- a/base/math.jl +++ b/base/math.jl @@ -1542,7 +1542,7 @@ for f in (:sin, :cos, :tan, :asin, :atan, :acos, :exponent, :sqrt, :cbrt, :sinpi, :cospi, :sincospi, :tanpi) @eval function ($f)(x::Real) xf = float(x) - x === xf && throw(MethodError($f, (x,))) + xf isa typeof(x) && throw(MethodError($f, (x,))) return ($f)(xf) end @eval $(f)(::Missing) = missing diff --git a/test/math.jl b/test/math.jl index 8f5ada013557c..75ee928c62e65 100644 --- a/test/math.jl +++ b/test/math.jl @@ -1549,6 +1549,14 @@ end end end +@testset "special function `::Real` fallback shouldn't recur without bound, issue #57789" begin + mutable struct Issue57789 <: Real end + Base.float(::Issue57789) = Issue57789() + for f ∈ (sin, sinpi, log, exp) + @test_throws MethodError f(Issue57789()) + end +end + # Test that sqrt behaves correctly and doesn't exhibit fp80 double rounding. # This happened on old glibc versions. # Test case from https://sourceware.org/bugzilla/show_bug.cgi?id=14032.