Skip to content

Packing an empty NamedTuple into a struct with other elements causes UndefRefError: access to undefined reference (Julia 1.7) #43783

@alex-lew

Description

@alex-lew

The following code fails with an UndefRefError on Julia 1.7:

struct BadStruct{R,S}
    named_tuple::NamedTuple{R,S}
    n::Int
end

BadStruct(NamedTuple{}(), 0)

It also exits the REPL.

I'm not sure what's going on here, but here are some observations:

  1. I can't reproduce the error with only the named tuple in the struct.
  2. With no type parameters (R, S), there's no crash.
  3. When the Named Tuple is nonempty, there's no crash.
  4. The construction of the struct succeeds, because let x = BadStruct(NamedTuple{}(), 0); x.n end works. But let x = BadStruct(NamedTuple{}(), 0); x.named_tuple end raises the UndefRefError, this time in Base.getproperty. So it seems the struct is being constructed, but the named_tuple field is not getting initialized properly? Note this example gives an error but does not kill the REPL.
  5. This is possibly related to Wrapping NamedTuple in struct is no longer bitstype in 1.7 #43411.

My versioninfo() is

Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.2.0)
  CPU: Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)

but @femtomc has reproduced the error, with

Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake-avx512)
Environment:
  JULIA_VERSION = 1.6.1

This issue came up because Gen.jl's StaticChoiceMap datatype is similar to the BadStruct above, and we noticed crashes when upgrading to Julia 1.7. (https://github.com/probcomp/Gen.jl/blob/44f5f1408caf05ff95932bb58d9f57907c5084e3/src/choice_map.jl#L350).

The code works fine on my machine on Julia 1.6.3.

Thanks!

Activity

changed the title [-]Packing two NamedTuples into a struct causes `UndefRefError: access to undefined reference` (Julia 1.7)[/-] [+]Packing one empty and one non-empty NamedTuple into a struct causes `UndefRefError: access to undefined reference` (Julia 1.7)[/+] on Jan 12, 2022
changed the title [-]Packing one empty and one non-empty NamedTuple into a struct causes `UndefRefError: access to undefined reference` (Julia 1.7)[/-] [+]Packing an empty NamedTuple into a struct with other elements causes `UndefRefError: access to undefined reference` (Julia 1.7)[/+] on Jan 12, 2022
femtomc

femtomc commented on Jan 12, 2022

@femtomc
Contributor

Here's an additional yikes:

julia> struct BadStruct{R,S}
           named_tuple::NamedTuple{R,S}
           n::Int
       end

julia> hasfield(BadStruct(NamedTuple(), 5), :named_tuple)
ERROR:
signal (11): Segmentation fault
in expression starting at none:0
sig_match_simple at /buildworker/worker/package_linux64/build/src/typemap.c:194 [inlined]
jl_typemap_entry_assoc_exact at /buildworker/worker/package_linux64/build/src/typemap.c:959
jl_typemap_assoc_exact at /buildworker/worker/package_linux64/build/src/julia_internal.h:1267 [inlined]
jl_lookup_generic_ at /buildworker/worker/package_linux64/build/src/gf.c:2369 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2425
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
do_apply at /buildworker/worker/package_linux64/build/src/builtins.c:713
showerror at ./errorshow.jl:222
#showerror#813 at ./errorshow.jl:88
showerror##kw at ./errorshow.jl:87
jfptr_showerrorYY.YY.kw_32910.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
show_exception_stack at ./errorshow.jl:866
display_error at ./client.jl:104
jfptr_display_error_34726.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
jl_f__call_latest at /buildworker/worker/package_linux64/build/src/builtins.c:757
#invokelatest#2 at ./essentials.jl:716 [inlined]
invokelatest at ./essentials.jl:714 [inlined]
print_response at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:286
#45 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:275
jfptr_YY.45_48142.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
with_repl_linfo at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:508
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
print_response at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:273
do_respond at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:844
jfptr_do_respond_46689.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
jl_f__call_latest at /buildworker/worker/package_linux64/build/src/builtins.c:757
#invokelatest#2 at ./essentials.jl:716 [inlined]
invokelatest at ./essentials.jl:714 [inlined]
run_interface at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2493
jfptr_run_interface_47484.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
run_frontend at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1230
#49 at ./task.jl:423
jfptr_YY.49_48145.clone_1 at /home/ubuntu/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
start_task at /buildworker/worker/package_linux64/build/src/task.c:877
Allocations: 2721 (Pool: 2713; Big: 8); GC: 0
[1]    4190442 segmentation fault (core dumped)  julia
added this to the 1.8 milestone on Jan 12, 2022
added
regressionRegression in behavior compared to a previous version
on Jan 12, 2022
femtomc

femtomc commented on Jan 13, 2022

@femtomc
Contributor

Is this #40947 from Tim's comment on #43411 (?)

JeffBezanson

JeffBezanson commented on Feb 23, 2022

@JeffBezanson
SponsorMember

Also fixed by #44311, so yes I think this is a duplicate of #43411.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    regressionRegression in behavior compared to a previous version

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vtjnash@JeffBezanson@alex-lew@femtomc

        Issue actions

          Packing an empty NamedTuple into a struct with other elements causes `UndefRefError: access to undefined reference` (Julia 1.7) · Issue #43783 · JuliaLang/julia