Skip to content

Commit

Permalink
fix #29718, union field alignment (#29722)
Browse files Browse the repository at this point in the history
(cherry picked from commit 70be5f2)
  • Loading branch information
JeffBezanson authored and KristofferC committed Feb 20, 2020
1 parent aa222aa commit 4a82fe9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,9 @@ static Type *julia_struct_to_llvm(jl_value_t *jt, jl_unionall_t *ua, bool *isbox
jlasttype = ty;
bool isptr;
size_t fsz = 0, al = 0;
if (jst->layout) {
isptr = jl_field_isptr(jst, i);
fsz = jl_field_size(jst, i);
al = jl_field_align(jst, i);
}
else { // compute what jl_compute_field_offsets would say
isptr = !jl_islayout_inline(ty, &fsz, &al);
if (!isptr && jl_is_uniontype(jst))
fsz += 1;
}
isptr = !jl_islayout_inline(ty, &fsz, &al);
if (!isptr && jl_is_uniontype(ty))
fsz += 1;
Type *lty;
if (isptr) {
lty = T_pjlvalue;
Expand Down
27 changes: 27 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5718,6 +5718,13 @@ let x5 = UnionField5(nothing, Int8(3))
@test hash(x5) === hash(x5copy)
end

struct UnionField6
alignment::Int32
padding::NTuple{3, UInt8}
#= implicit-padding::UInt8 =#
maybe_val::Union{UInt16, Nothing} # offset = 8, align = 8, size = 2
end
@test UnionField6(1,(1,1,1),2018).maybe_val == 2018

# PR #23367
struct A23367
Expand Down Expand Up @@ -6320,6 +6327,26 @@ let A=[0, missing], B=[missing, 0], C=Vector{Union{Int, Missing}}(undef, 6)
@test isequal(C, [0, missing, missing, missing, 0, missing])
end

# issue #29718
function f29718()
nt = NamedTuple{(:a, :b, :c, :d, :e, :f,),
Tuple{Union{Missing, Float64},
Tuple{UInt8},
Union{Missing, Int8},
Int8,
Tuple{UInt8,UInt8},
Union{Missing, Int16}}
}((missing,
(1,),
1,
41,
(1,2),
1915,
))
return Ref{Any}(nt)[].f
end
@test f29718() == 1915

end # module UnionOptimizations

# issue #6614, argument destructuring
Expand Down

0 comments on commit 4a82fe9

Please sign in to comment.