Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodError thrown while preparing to throw InexactError in conversion of Rational to Integer or Complex to Real #55756

Open
nsajko opened this issue Sep 12, 2024 · 2 comments · May be fixed by #55758
Labels
complex Complex numbers error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages rationals The Rational type and values thereof

Comments

@nsajko
Copy link
Contributor

nsajko commented Sep 12, 2024

Minimal reproducer:

julia> struct A <: Integer end

julia> struct B <: Integer end

julia> Union{A,B}(1//2)
ERROR: MethodError: no method matching nameof(::Type{Union{A, B}})
The function `nameof` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  nameof(::Core.IntrinsicFunction)
   @ Base reflection.jl:2369
  nameof(::Module)
   @ Base essentials.jl:401
  nameof(::UnionAll)
   @ Base reflection.jl:343
  ...

Stacktrace:
 [1] Union{A, B}(x::Rational{Int64})
   @ Base ./rational.jl:151
 [2] top-level scope
   @ REPL[3]:1

It's not valid to call nameof on an arbitrary subtype of Integer, because it may be an Union:

julia/base/rational.jl

Lines 149 to 152 in 945517b

Bool(x::Rational) = x==0 ? false : x==1 ? true :
throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity
(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num)::T :
throw(InexactError(nameof(T), T, x)))

It seems like this could be fixed by simply replacing the nameof call with :convert.

@nsajko nsajko added error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages labels Sep 12, 2024
nsajko added a commit to nsajko/julia that referenced this issue Sep 12, 2024
@nsajko nsajko added the rationals The Rational type and values thereof label Sep 12, 2024
@nsajko
Copy link
Contributor Author

nsajko commented Sep 13, 2024

The same issue as for Rational is present for Complex:

julia/base/complex.jl

Lines 44 to 45 in e52a46c

(::Type{T})(z::Complex) where {T<:Real} =
isreal(z) ? T(real(z))::T : throw(InexactError(nameof(T), T, z))

@nsajko nsajko added the complex Complex numbers label Sep 13, 2024
@nsajko nsajko changed the title MethodError thrown while preparing to throw InexactError in conversion of Rational to Integer MethodError thrown while preparing to throw InexactError in conversion of Rational or Complex to Integer Sep 13, 2024
@nsajko nsajko changed the title MethodError thrown while preparing to throw InexactError in conversion of Rational or Complex to Integer MethodError thrown while preparing to throw InexactError in conversion of Rational to Integer or Complex to Real Sep 13, 2024
@nsajko
Copy link
Contributor Author

nsajko commented Sep 13, 2024

Also with BigFloat:

julia/base/mpfr.jl

Lines 410 to 413 in 467ab85

function (::Type{T})(x::BigFloat) where T<:Integer
isinteger(x) || throw(InexactError(nameof(T), T, x))
trunc(T,x)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages rationals The Rational type and values thereof
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant