Fatal error when .jl file included

question

#1

I have the following code in a file named badconvert.jl. When included into julia either on the command line or using include(“badconvert.jl”) it produces a fatal error (shown a the end of this message).

This isn’t the actual code I am trying to use, I’ve just tried to reduce the problem down to small example that I could.

I’m using Julia 1.0.0 on macOS. Any hints on what is going wrong is appreciated.

This code produces the error.

import Base.convert

struct s1
    value::Float64
end

function convert(s1, x::Int64)
    s1(Float64(x))
end

However this code works as expected.

import Base.convert

struct s1
    value::Float64
end

function convert(s1, x::Int16)
    s1(Float64(x))
end

The only difference between this examples is the second argument for the convert function is Int64 in the first example and Int16 in the second.

Daniels-Mac-Pro-2:research danielsullivan$ julia -i badconvert.jl
fatal: error thrown and no exception handler available.
MethodError(f=typeof(Base.convert)(), args=(Int32, 0), world=0x00000000000061b1)
rec_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/stackwalk.c:94
record_backtrace at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:246 [inlined]
jl_throw at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:577
jl_method_error_bare at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1614
jl_method_error at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1632
jl_lookup_generic_ at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:2159
jl_apply_generic at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:2179
cconvert at ./essentials.jl:344
containsnul at ./c.jl:209
unsafe_convert at ./c.jl:216
_getenv at ./env.jl:40
access_env at ./env.jl:44
get at ./env.jl:77
repl_color at ./client.jl:16
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1829
error_color at ./client.jl:22
display_error at ./client.jl:93
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1829
display_error at ./client.jl:102
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1829
jl_apply at /Users/osx/buildbot/slave/package_osx64/build/src/./julia.h:1536 [inlined]
jl_f__apply at /Users/osx/buildbot/slave/package_osx64/build/src/builtins.c:556
jl_f__apply_latest at /Users/osx/buildbot/slave/package_osx64/build/src/builtins.c:594
#invokelatest#1 at ./essentials.jl:686 [inlined]
invokelatest at ./essentials.jl:685 [inlined]
_start at ./client.jl:423
true_main at /usr/local/bin/julia (unknown line)
main at /usr/local/bin/julia (unknown line)
error in running finalizer: MethodError(f=typeof(Base.convert)(), args=(Int64, 5), world=0x00000000000061b1)
error in running finalizer: MethodError(f=typeof(Base.convert)(), args=(Int64, 5), world=0x00000000000061b1)
error in running finalizer: MethodError(f=typeof(Base.convert)(), args=(Int64, 5), world=0x00000000000061b1)
**julia>** versioninfo()

Julia Version 1.0.0

Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)

Platform Info:

OS: macOS (x86_64-apple-darwin14.5.0)

CPU: Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz

WORD_SIZE: 64

LIBM: libopenlibm

LLVM: libLLVM-6.0.0 (ORCJIT, ivybridge)

#2

This should be function convert(::Type{s1}, x::Int64) — you want the first argument to be the type s1 not just named s1.

By defining convert(s1, x::Int64), where the type of the first argument is not specified, you are potentially redefining how Julia converts any Int64 value to any type, which can break the core libraries. This is also known as type piracy. (The analogue in Python would be something like monkey-patching the standard libraries.)

See also this section of the manual.


#3

Thank you!