JuliaDebug / Debugger.jl

Julia debugger
MIT License
470 stars 43 forks source link

Debugger crashes after ambiguous type conversion error... #225

Closed cormullion closed 5 years ago

cormullion commented 5 years ago

I've got a problem with some code (I don't understand conversions very well...), and I thought I'd get Debugger to help me investigate the problem, but it had other ideas...

Code:

struct Point4D
    x::Float64
    y::Float64
    z::Float64
    w::Float64
end

struct Point3D
    x::Float64
    y::Float64
    z::Float64
end

struct Point2D
    x::Float64
    y::Float64
end

Point4D(a::Array{Real, 1}) = Point4D(a[1], a[2], a[3], a[4])
Base.size(pt::Point4D) = (4, )
Base.getindex(pt::Point4D, i) = [pt.x, pt.y, pt.z, pt.w][i]

Base.size(pt::Point3D) = (3, )
Base.getindex(pt::Point3D, i) = [pt.x, pt.y, pt.z][i]

const K = 4.0

function Base.convert(Point2D, pt3::Point3D)
    k = 1/(K - pt3.z)
    return Point2D(pt3.x * k, pt3.y * k)
end

function Base.convert(Point2D, arraypt3::Array{Point3D, 1})
    map(pt3 -> convert(Point2D, pt3), arraypt3)
end

function test()
    pt3 = Point3D(1.0, 1.2, 1.3)
    @show convert(Point2D, pt3)
    cube = [Point3D(-1., -1., 1.),                  # the error is around here
        Point3D(-1.,  1.,  1.),
        Point3D( 1., -1.,  1.),
        Point3D( 1.,  1.,  1.),
        Point3D(-1., -1., -1.),
        Point3D(-1.,  1., -1.),
        Point3D( 1., -1., -1.),
        Point3D( 1.,  1., -1.)]
    @show convert(Point2D, cube)    
end

The normal error is this:

ERROR: LoadError: MethodError: convert(::Type{Point3D}, ::Point3D) is ambiguous. Candidates:
  convert(Point2D, pt3::Point3D) in Main at untitled-ec5202a91b960e84e9773a4e5ceb7e8e:29
  convert(::Type{T}, x::T) where T in Base at essentials.jl:167
Possible fix, define
  convert(::Type{Point3D}, ::Point3D)

which I'm trying to investigate. I expect the answer is very simple, but I haven't found it yet...

With the debugger:

using Debugger

@enter test()

Here's the output after the first step:

signal (4): Illegal instruction: 4
in expression starting at untitled-ec5202a91b960e84e9773a4e5ceb7e8e:56
#repr#343 at ./strings/io.jl:221
repr at ./strings/io.jl:221
unknown function (ip: 0x13610e740)
print_next_expr at /Users/me/.julia/packages/Debugger/XqeyI/src/printing.jl:98
#print_status#41 at /Users/me/.julia/packages/Debugger/XqeyI/src/printing.jl:140
#print_status at ./none:0
unknown function (ip: 0x11839c2fd)
#10 at /Users/me/.julia/packages/Debugger/XqeyI/src/repl.jl:81
unknown function (ip: 0x1360ec022)
jl_apply at /Users/julia/buildbot/worker/package_macos64/build/src/./julia.h:1606 [inlined]
jl_f__apply at /Users/julia/buildbot/worker/package_macos64/build/src/builtins.c:563
jl_f__apply_latest at /Users/julia/buildbot/worker/package_macos64/build/src/builtins.c:601
#invokelatest#1 at ./essentials.jl:790 [inlined]
invokelatest at ./essentials.jl:789 [inlined]
run_interface at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.2/REPL/src/LineEdit.jl:2306
run_interface at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.2/REPL/src/LineEdit.jl:2300
#RunDebugger#7 at /Users/me/.julia/packages/Debugger/XqeyI/src/repl.jl:155
RunDebugger at /Users/me/.julia/packages/Debugger/XqeyI/src/repl.jl:4 [inlined]
RunDebugger at /Users/me/.julia/packages/Debugger/XqeyI/src/repl.jl:4
do_call at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:323
eval_stmt_value at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:362 [inlined]
eval_body at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:758
jl_interpret_toplevel_thunk_callback at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:884
unknown function (ip: 0xfffffffffffffffe)
unknown function (ip: 0x1122f2e0f)
unknown function (ip: 0x11)
jl_interpret_toplevel_thunk at /Users/julia/buildbot/worker/package_macos64/build/src/interpreter.c:893
jl_toplevel_eval_flex at /Users/julia/buildbot/worker/package_macos64/build/src/toplevel.c:815
jl_parse_eval_all at /Users/julia/buildbot/worker/package_macos64/build/src/ast.c:873
include_string at ./loading.jl:1064
#134 at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:125
withpath at /Users/me/.julia/packages/CodeTools/xGemk/src/utils.jl:30
withpath at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:46 [inlined]
#133 at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:122 [inlined]
with_logstate at ./logging.jl:395
with_logger at ./logging.jl:491 [inlined]
#132 at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:121 [inlined]
hideprompt at /Users/me/.julia/packages/Atom/BVajq/src/repl.jl:77
macro expansion at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:120 [inlined]
macro expansion at /Users/me/.julia/packages/Media/ItEPc/src/dynamic.jl:24 [inlined]
#131 at /Users/me/.julia/packages/Atom/BVajq/src/eval.jl:109
handlemsg at /Users/me/.julia/packages/Atom/BVajq/src/comm.jl:164
unknown function (ip: 0x11831f92c)
jl_apply at /Users/julia/buildbot/worker/package_macos64/build/src/./julia.h:1606 [inlined]
jl_f__apply at /Users/julia/buildbot/worker/package_macos64/build/src/builtins.c:563
#19 at ./task.jl:268
unknown function (ip: 0x11831c304)
jl_apply at /Users/julia/buildbot/worker/package_macos64/build/src/./julia.h:1606 [inlined]
start_task at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:596
Allocations: 55674911 (Pool: 55663942; Big: 10969); GC: 127

Julia has exited. Press Enter to start a new session.

Might be a stack overflow or similar?

Debugger v0.6.2, Julia v1.2.0

timholy commented 5 years ago

I can replicate the crash, but not sure what to do about it. Your convert method should be defined with Base.convert(::Type{Point2D}, obj), not Base.convert(Point2D, obj). The latter defines convert(::Any, obj), which is weird to even think about. It might be worth reporting this as a Julia bug, in case the solution is to prevent such methods from even being defined.

cormullion commented 5 years ago

Oops. 😊 Thanks, Tim.

KristofferC commented 5 years ago

Yeah, not much we can do here. Julia is pretty much broken after adding these methods.