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

mapping a NamedTuple prints Internal error: encountered unexpected error in runtime #49785

Closed
jkrumbiegel opened this issue May 12, 2023 · 3 comments · Fixed by #49863
Closed
Assignees
Labels
compiler:inference Type inference kind:bug Indicates an unexpected problem or unintended behavior

Comments

@jkrumbiegel
Copy link
Contributor

jkrumbiegel commented May 12, 2023

I tested this on Julia 1.8.5 and 1.9beta3 (on an M1 mac but we've seen this on other architectures as well)

struct DiscriminatedUnion{S, T<:S}
    type::String
    value::T
end

abstract type Car end

struct Audi <: Car end
struct Mercedes <: Car end

function concrete_type_mapping(::Type{Car})
    (
      audi = Audi,
      mercedes = Mercedes,
    )
end

function subtypes(t::Type{<:DiscriminatedUnion{S}}) where {S}
    SUP = fieldtypes(t)[2]
    map(T -> DiscriminatedUnion{SUP,T}, concrete_type_mapping(SUP))
end

subtypes(DiscriminatedUnion{Car})

Calling subtypes prints out this super long trace, at the end of which you can see the correct output (audi = DiscriminatedUnion{Car, Audi}, mercedes = DiscriminatedUnion{Car, Mercedes}).

I can avoid the error by replacing map(T -> DiscriminatedUnion{SUP,T}, concrete_type_mapping(SUP)) with

NamedTuple([
    key => DiscriminatedUnion{SUP,T} for (key, T) in pairs(concrete_type_mapping(SUP))
  ])
Internal error: encountered unexpected error in runtime:
TypeError(func=:<:, context="", expected=Type, got=S)
ijl_type_error_rt at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_type_error at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f_issubtype at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
apply_type_nothrow at ./compiler/tfuncs.jl:1360
_builtin_nothrow at ./compiler/tfuncs.jl:1737
builtin_nothrow at ./compiler/tfuncs.jl:1862 [inlined]
builtin_effects at ./compiler/tfuncs.jl:1848
abstract_call_known at ./compiler/abstractinterpretation.jl:1627
abstract_call at ./compiler/abstractinterpretation.jl:1786
abstract_call at ./compiler/abstractinterpretation.jl:1753
abstract_eval_statement at ./compiler/abstractinterpretation.jl:1910
typeinf_local at ./compiler/abstractinterpretation.jl:2386
typeinf_nocycle at ./compiler/abstractinterpretation.jl:2482
_typeinf at ./compiler/typeinfer.jl:230
typeinf at ./compiler/typeinfer.jl:213
typeinf_ext at ./compiler/typeinfer.jl:967
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1000
typeinf_ext_toplevel at ./compiler/typeinfer.jl:996
jfptr_typeinf_ext_toplevel_15369 at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_type_infer at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_generate_fptr_impl at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.8.dylib (unknown line)
jl_compile_method_internal at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
map at ./tuple.jl:222 [inlined]
map at ./namedtuple.jl:219
subtypes at ./Untitled-1:20
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_call at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval_body at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
top-level scope at Untitled-1:23
jl_toplevel_eval_flex at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_toplevel_eval_flex at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_toplevel_eval_in at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval at ./boot.jl:368 [inlined]
include_string at ./loading.jl:1428
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f__call_latest at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:729
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
invokelatest at ./essentials.jl:726
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#inlineeval#71 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:233
inlineeval##kw at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:230 [inlined]
#66 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:157
withpath at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/repl.jl:249
#65 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:155
hideprompt at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/repl.jl:38
#64 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:126
unknown function (ip: 0x1ccf6482f)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
with_logstate at ./logging.jl:511
with_logger at ./logging.jl:623 [inlined]
#63 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:225
unknown function (ip: 0x1ccf645ff)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f__call_latest at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:729 [inlined]
invokelatest at ./essentials.jl:726
unknown function (ip: 0x11fe67982)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
macro expansion at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:34 [inlined]
#61 at ./task.jl:484
unknown function (ip: 0x11fe441ff)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
start_task at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
Internal error: encountered unexpected error in runtime:
TypeError(func=:<:, context="", expected=Type, got=S)
ijl_type_error_rt at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_type_error at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f_issubtype at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
apply_type_nothrow at ./compiler/tfuncs.jl:1360
_builtin_nothrow at ./compiler/tfuncs.jl:1737
builtin_nothrow at ./compiler/tfuncs.jl:1862 [inlined]
builtin_effects at ./compiler/tfuncs.jl:1848
abstract_call_known at ./compiler/abstractinterpretation.jl:1627
abstract_call at ./compiler/abstractinterpretation.jl:1786
abstract_call at ./compiler/abstractinterpretation.jl:1753
abstract_eval_statement at ./compiler/abstractinterpretation.jl:1910
typeinf_local at ./compiler/abstractinterpretation.jl:2386
typeinf_nocycle at ./compiler/abstractinterpretation.jl:2482
_typeinf at ./compiler/typeinfer.jl:230
typeinf at ./compiler/typeinfer.jl:213
typeinf_ext at ./compiler/typeinfer.jl:967
typeinf_ext_toplevel at ./compiler/typeinfer.jl:1000
typeinf_ext_toplevel at ./compiler/typeinfer.jl:996
jfptr_typeinf_ext_toplevel_15369 at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_type_infer at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_generate_fptr_impl at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-codegen.1.8.dylib (unknown line)
jl_compile_method_internal at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
map at ./tuple.jl:222 [inlined]
map at ./namedtuple.jl:219
subtypes at ./Untitled-1:20
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_call at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval_body at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
top-level scope at Untitled-1:23
jl_toplevel_eval_flex at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_toplevel_eval_flex at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_toplevel_eval_in at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
eval at ./boot.jl:368 [inlined]
include_string at ./loading.jl:1428
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f__call_latest at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:729
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
invokelatest at ./essentials.jl:726
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#inlineeval#71 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:233
inlineeval##kw at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:230 [inlined]
#66 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:157
withpath at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/repl.jl:249
#65 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:155
hideprompt at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/repl.jl:38
#64 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:126
unknown function (ip: 0x1ccf6482f)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
with_logstate at ./logging.jl:511
with_logger at ./logging.jl:623 [inlined]
#63 at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:225
unknown function (ip: 0x1ccf645ff)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
jl_f__call_latest at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:729 [inlined]
invokelatest at ./essentials.jl:726
unknown function (ip: 0x11fe67982)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
do_apply at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
macro expansion at /Users/krumbiegel/.vscode/extensions/julialang.language-julia-1.45.1/scripts/packages/VSCodeServer/src/eval.jl:34 [inlined]
#61 at ./task.jl:484
unknown function (ip: 0x11fe441ff)
ijl_apply_generic at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
start_task at /Users/krumbiegel/.julia/juliaup/julia-1.8.5+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.8.dylib (unknown line)
(audi = DiscriminatedUnion{Car, Audi}, mercedes = DiscriminatedUnion{Car, Mercedes})
@martinholters
Copy link
Member

Reduction:

struct DiscriminatedUnion{S, T<:S} end

function subtypes()
    map(T -> DiscriminatedUnion{Union{Type{String},Type{Int}}[Int][1],T}, (a = Int,))
end

subtypes()

Confirmed to also repro on 1.9.0.

@martinholters
Copy link
Member

martinholters commented May 12, 2023

I think

julia/base/compiler/tfuncs.jl

Lines 1668 to 1670 in 6733197

if !(T <: u.var.ub)
return false
end

needs to check whether u.var.ub is a TypeVar first (and also return false in that case).

EDIT: OTOH, this seems fixed on master, so there might be a better fix to backport.

@vtjnash vtjnash changed the title mapping a NamedTuple prints Internal error: encountered unexpected error in runtime [release-1.9] mapping a NamedTuple prints Internal error: encountered unexpected error in runtime May 15, 2023
@martinholters
Copy link
Member

Hm, looks like this was fixed by 95cfd62 and then broken again by 5a684f0 -- which reverted the former, but only for 1.9. Anyway, that fix was more of a fluke anyway, since this variation also fails on master:

function subtypes()
    map(T -> DiscriminatedUnion{Union{Type{String},Type{Int}}[Int][1],T}, (a = Union{Type{String},Type{Int}}[Int][1],))
end

@vtjnash vtjnash added kind:bug Indicates an unexpected problem or unintended behavior compiler:inference Type inference labels May 17, 2023
@vtjnash vtjnash changed the title [release-1.9] mapping a NamedTuple prints Internal error: encountered unexpected error in runtime mapping a NamedTuple prints Internal error: encountered unexpected error in runtime May 17, 2023
martinholters added a commit that referenced this issue May 17, 2023
For types like `Foo{S, T<:S}`, `apply_type_nothrow` could in some
situations check whether the argument is a subtype of the upper bound of
`T`, i.e. `S`, but subtyping agaist a plain `TypeVar` would fail.
Instead return `false` in this case.

Fixes #49785.
martinholters added a commit that referenced this issue May 19, 2023
…49863)

For types like `Foo{S, T<:S}`, `apply_type_nothrow` could in some
situations check whether the argument is a subtype of the upper bound of
`T`, i.e. `S`, but subtyping agaist a plain `TypeVar` would fail.
Instead return `false` in this case.

Fixes #49785.
KristofferC pushed a commit that referenced this issue Jun 27, 2023
…49863)

For types like `Foo{S, T<:S}`, `apply_type_nothrow` could in some
situations check whether the argument is a subtype of the upper bound of
`T`, i.e. `S`, but subtyping agaist a plain `TypeVar` would fail.
Instead return `false` in this case.

Fixes #49785.

(cherry picked from commit 1acec74)
KristofferC pushed a commit that referenced this issue Jun 27, 2023
…49863)

For types like `Foo{S, T<:S}`, `apply_type_nothrow` could in some
situations check whether the argument is a subtype of the upper bound of
`T`, i.e. `S`, but subtyping agaist a plain `TypeVar` would fail.
Instead return `false` in this case.

Fixes #49785.

(cherry picked from commit 1acec74)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants