Commit Graph
152 Commits
Author SHA1 Message Date
alyekypo b9c4067f78 fix(llvm): emit union constants for non-constantable unions
`constant: U : value` produced a constant of the wrong type:
lb_const_value took its union-lowering branch only when
`is_type_union_constantable(type)` held, so for a union with a variant
that whitelist rejects (`union{bool, ^int}`) the value fell through to
the scalar branch and reached `LLVMConstInt(lb_type(m, union_type),
...)`.  The constant emitted there does not match the union's type:
bytes that change between runs for `true`, a compiler crash for `1.0`,
an `is_type_string` assertion for a string literal.

The checker already resolves which variant a constant represents and
records it in `ExactValue::variant_type` - the variant type, tag index,
payload and padding the union branch needs - so that branch is now also
taken when the value pins a variant this backend can build as a
constant.  The type-level whitelist was re-guessing information the
value already carries; variant types this backend cannot build as
constants (`any`) stay excluded.

lb_build_static_variables had the same defect in another place: it
lowered an initializer with the expression's type instead of the
declared type, so `@(static) s: U` emitted a payload-only constant and
LLVM rejected the module with “Global variable initializer type does not
match global variable type!”.  It now passes the declared type and gives
the global the constant's layout-compatible type, the way the file-scope
path already does.

Blast radius: every union-typed constant, i.e. typed constants, global
static initializers, @(static)/@(thread_local)/@(rodata), default
parameters and constant aggregate elements.  Unions whose variants are
all constantable keep the previous path.
2026-09-28 19:32:04 +00:00
mlgudi a5d0f130c0 Fix field sorting for bit_field literals 2026-09-27 10:44:05 +01:00
gingerBill f04f867b28 Fix local struct literals losing fields set through using 2026-09-02 11:37:17 +01:00
kalsprite ae549bf34b broadcast a constant into every array level 2026-08-25 21:47:02 -07:00
korvahkh f97274fd0a Fix #7135
We have to handle unpacked structs in
llvm_const_named_struct
2026-08-19 20:13:00 -05:00
korvahkh 92ec71fa75 Prevent underflow of union padding 2026-08-19 20:13:00 -05:00
korvahkh 02246ac6fd Implement fixes for constant unions
Note that with this commit, casting through
multiply-nested unions is forbidden, e.g.
```
U :: union {int, V}
V :: union {bool}
x: U = true
```
does not compile.
(Previously the compiler would simply crash)

This is to avoid situations where adding variants
can lead to unexpected changes in the value.

For example if `U` is changed to have a `bool`
variant of its own:
```
U :: union {int, bool, V}
```
Then `x: U = true` would equal
`U(true)` instead of `U(V(true))`.

Single-variant unions are exempt, primarily to
improve the ergonomics of `Maybe` in cases like:
```
x: Maybe(union{int, bool}) = 1
```

Fix #6100
Fix #6699
Fix #6895
Fix #6896
Fix #6897
Fix #7036
Fix #7083
Fix #7091
2026-08-19 20:12:56 -05:00
kalsprite cf5f181228 peel union constant to its variant 2026-08-19 16:30:37 -07:00
corley 7b45adf8e9 fixes swizzling for array elements in soa arrays/slices 2026-08-09 03:17:04 +03:00
kalsprite df41b93c45 fix typo 2026-08-07 15:05:35 -07:00
Jeroen van Rijn 40b28a2ded LLVM 14 removal cleanup 2026-06-30 21:12:42 +02:00
gingerBill 62511914de When inserting a constant union into a struct literal, rebuild the struct to be non-named with llvm_const_insert_value_with_rebuild 2026-06-30 12:16:21 +01:00
gingerBill 28a43fd4f7 Short-circuit further for constant unions in the LLVM constant literal syntax 2026-06-30 11:49:35 +01:00
gingerBill 1cdf314015 Remove debug print string 2026-06-29 16:04:24 +01:00
gingerBill b9a9846b7c Fix constant union stuff against tests 2026-06-29 13:00:44 +01:00
gingerBill 2f3174fe10 Fix llvm_const_cast with constant union literals 2026-06-29 11:41:27 +01:00
gingerBill feb9b6159e Fix pkg.Constant.field union access 2026-06-29 11:06:34 +01:00
gingerBill e227c736e6 Fix #6786 2026-06-29 10:49:29 +01:00
korvahkh 0aa6d8f318 Fix #6885
Broadcasting `field = value` compound literals
would assert.
This is because we were checking for a
`field = value` compound before checking
if the types matched, and thus could broadcast.
2026-06-24 16:28:02 -05:00
gingerBill baef272bbd Support constant compound literals 2026-06-22 12:53:27 +01:00
ARay 912a45769b fix issue 6853:
when iterating the values of an array literal,if one of the values
is a function with multiple returns, it doesn't offset the struct
field index by the number of values.
2026-06-19 16:52:51 +03:00
ARay 3db074900f comment out dead code: val is not used anywhere else in that scope 2026-06-19 15:05:23 +03:00
gingerBill 8d1d126e16 Rearrange context and value_type values of lb_const_value 2026-06-05 10:19:55 +01:00
gingerBill 99992e092f Fix constant array spreading for unions of constants 2026-06-05 10:15:51 +01:00
gingerBill ea5175d865 Support s: [2]u16 = "hi" 2026-05-02 12:11:39 +01:00
gingerBill ef275c5c0e Support []int{multiple_returns(), 123} 2026-04-21 17:45:36 +01:00
gingerBill 04cb889aed Begin interning ScopeMap strings 2026-03-17 11:04:32 +00:00
gingerBill c51391f8cb Fix #5129 2026-03-15 17:15:16 +00:00
gingerBill a6160770ff Support compound literals for fixed capacity dynamic arrays 2026-03-12 10:03:58 +00:00
ske 9d439bd630 Add broadcasting to strings (fix #1837) 2026-02-14 16:23:51 -03:00
gingerBill e014181abf Move values construction to after padding has been set 2026-02-02 11:12:42 +00:00
ske 2700fa86d6 fix typo 2025-12-30 22:24:10 -03:00
ske e4bf5476c3 Fix literal endianness (fix #6068) 2025-12-30 22:08:40 -03:00
korvahkh f454bbd73c Fix #5894 2025-11-10 07:54:42 -06:00
Laytan c4d1cd6ee5 fixes for 32bit with regards to typeid 2025-11-04 21:37:29 +01:00
gingerBill 7f2ab31acc Fix constant nil assignment to union of nillable types in backend 2025-11-02 12:04:21 +00:00
gingerBill f5d080789f Fix union(T){} constant initialization 2025-10-31 09:13:10 +00:00
gingerBill 55ab18f911 Fix #5831 2025-10-22 09:07:46 +01:00
A1029384756 be9384fc8d [llvm-14-fixes] use specific LLVMConst* codepaths for LLVM 14 2025-10-09 12:03:25 -04:00
A1029384756 cfe651e8d2 formatting 2025-10-07 23:26:29 -04:00
A1029384756 7179987ac9 casting 2025-10-07 23:15:12 -04:00
A1029384756 b55cf5f8f4 llvm 14 fixed 2025-10-07 22:58:50 -04:00
gingerBill 89645921e2 Only add packing if the padding is non-zero for a #raw_union constant 2025-09-29 11:00:08 +01:00
gingerBill e511f07d76 Short circuit for Union{} 2025-09-29 10:45:24 +01:00
gingerBill 10ba956d6a Rudimentary support for some constant struct #raw_union 2025-09-29 10:28:16 +01:00
gingerBill 1f2cedcf78 Remove debug code 2025-09-29 09:53:04 +01:00
gingerBill 4f442c6045 Rearrange const union initialization so that it is priority 2025-09-29 09:51:23 +01:00
gingerBill dd15a5bc8e Do not need an extra local copy for the slices 2025-09-28 23:32:37 +01:00
gingerBill 6db8943efa Check for empty compound literal early for constants 2025-09-28 23:25:27 +01:00
gingerBill 5b88d2363d Correct failure check for const cast 2025-09-28 23:19:43 +01:00