<p dir="auto">The following code fails with an <code class="notranslate">UndefRefError</code> on Julia 1.7:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct BadStruct{R,S}
    named_tuple::NamedTuple{R,S}
    n::Int
end

BadStruct(NamedTuple{}(), 0)"><pre class="notranslate"><span class="pl-k">struct</span> BadStruct{R,S}
    named_tuple<span class="pl-k">::</span><span class="pl-c1">NamedTuple{R,S}</span>
    n<span class="pl-k">::</span><span class="pl-c1">Int</span>
<span class="pl-k">end</span>

<span class="pl-c1">BadStruct</span>(<span class="pl-c1">NamedTuple</span><span class="pl-c1">{}</span>(), <span class="pl-c1">0</span>)</pre></div>
<p dir="auto">It also exits the REPL.</p>
<p dir="auto">I'm not sure what's going on here, but here are some observations:</p>
<ol dir="auto">
<li>I can't reproduce the error with only the named tuple in the struct.</li>
<li>With no type parameters (R, S), there's no crash.</li>
<li>When the Named Tuple is nonempty, there's no crash.</li>
<li>The construction of the struct succeeds, because <code class="notranslate">let x = BadStruct(NamedTuple{}(), 0); x.n end</code> works. But <code class="notranslate">let x = BadStruct(NamedTuple{}(), 0); x.named_tuple end</code> raises the UndefRefError, this time in Base.getproperty. So it seems the struct is being constructed, but the <code class="notranslate">named_tuple</code> field is not getting initialized properly? Note this example gives an error but does not kill the REPL.</li>
<li>This is possibly related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1077959043" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/43411" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/43411/hovercard" href="https://github.com/JuliaLang/julia/issues/43411">#43411</a>.</li>
</ol>
<p dir="auto">My <code class="notranslate">versioninfo()</code> is</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><code class="notranslate">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)
</code></pre></div>
<p dir="auto">but <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/femtomc/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/femtomc">@femtomc</a> has reproduced the error, with</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">This issue came up because <a href="https://github.com/probcomp/Gen.jl">Gen.jl</a>'s <code class="notranslate">StaticChoiceMap</code> datatype is similar to the <code class="notranslate">BadStruct</code> above, and we noticed crashes when upgrading to Julia 1.7. (<a href="https://github.com/probcomp/Gen.jl/blob/44f5f1408caf05ff95932bb58d9f57907c5084e3/src/choice_map.jl#L350">https://github.com/probcomp/Gen.jl/blob/44f5f1408caf05ff95932bb58d9f57907c5084e3/src/choice_map.jl#L350</a>).</p>
<p dir="auto">The code works fine on my machine on Julia 1.6.3.</p>
<p dir="auto">Thanks!</p>
<p dir="auto">cf brief discussion on <a href="https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/9" rel="nofollow">discourse</a></p>
<p dir="auto">This bug(?) occurs on 1.7 but not on 1.6:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct Wrapper{SYMS, TT}
   x::NamedTuple{SYMS, TT}
end
randnt() = Wrapper((a = randn(), b = rand(), c = rand(1:5), d = randn(SVector{3, Float64})))
X = randnt()
@allocated randnt()  # 64
isbits(X)   # false
isbits(X.x)   # true "><pre class="notranslate"><span class="pl-k">struct</span> Wrapper{SYMS, TT}
   x<span class="pl-k">::</span><span class="pl-c1">NamedTuple{SYMS, TT}</span>
<span class="pl-k">end</span>
<span class="pl-en">randnt</span>() <span class="pl-k">=</span> <span class="pl-c1">Wrapper</span>((a <span class="pl-k">=</span> <span class="pl-c1">randn</span>(), b <span class="pl-k">=</span> <span class="pl-c1">rand</span>(), c <span class="pl-k">=</span> <span class="pl-c1">rand</span>(<span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">5</span>), d <span class="pl-k">=</span> <span class="pl-c1">randn</span>(SVector{<span class="pl-c1">3</span>, Float64})))
X <span class="pl-k">=</span> <span class="pl-c1">randnt</span>()
<span class="pl-c1">@allocated</span> <span class="pl-c1">randnt</span>()  <span class="pl-c"><span class="pl-c">#</span> 64</span>
<span class="pl-c1">isbits</span>(X)   <span class="pl-c"><span class="pl-c">#</span> false</span>
<span class="pl-c1">isbits</span>(X<span class="pl-k">.</span>x)   <span class="pl-c"><span class="pl-c">#</span> true </span></pre></div>
<p dir="auto">But if the wrapper is defined differently then the <code class="notranslate">isbits == true</code> is recovered:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct Wrapper1{NT} 
   x::NT
end
randnt1() = Wrapper1((a = randn(), b = rand(), c = rand(1:5), d = randn(SVector{3, Float64})))
X1 = randnt1()
@allocated randnt1() # 0 
isbits(X1) # true
isbits(X1.x) # true"><pre class="notranslate"><span class="pl-k">struct</span> Wrapper1{NT} 
   x<span class="pl-k">::</span><span class="pl-c1">NT</span>
<span class="pl-k">end</span>
<span class="pl-en">randnt1</span>() <span class="pl-k">=</span> <span class="pl-c1">Wrapper1</span>((a <span class="pl-k">=</span> <span class="pl-c1">randn</span>(), b <span class="pl-k">=</span> <span class="pl-c1">rand</span>(), c <span class="pl-k">=</span> <span class="pl-c1">rand</span>(<span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">5</span>), d <span class="pl-k">=</span> <span class="pl-c1">randn</span>(SVector{<span class="pl-c1">3</span>, Float64})))
X1 <span class="pl-k">=</span> <span class="pl-c1">randnt1</span>()
<span class="pl-c1">@allocated</span> <span class="pl-c1">randnt1</span>() <span class="pl-c"><span class="pl-c">#</span> 0 </span>
<span class="pl-c1">isbits</span>(X1) <span class="pl-c"><span class="pl-c">#</span> true</span>
<span class="pl-c1">isbits</span>(X1<span class="pl-k">.</span>x) <span class="pl-c"><span class="pl-c">#</span> true</span></pre></div>
<p dir="auto">Because of this, it is easy enough to produce a work-around, but it feels like that shouldn't be necessary and the behaviour described above should not occur?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; versioninfo()
Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.1.0)
  CPU: Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)"><pre class="notranslate"><code class="notranslate">julia&gt; versioninfo()
Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.1.0)
  CPU: Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)
</code></pre></div>
<p dir="auto">(built from source)</p>