ir: fix tuple literal element widths (construction was garbage)

A tuple_init's element values must match its field types exactly — LLVM
`insertvalue` does no implicit conversion. An inferred `pair := (40, 2)`
lowered its elements under the enclosing fn's `target_type` (e.g. main's
s32 return), producing i32 values, while the field types were inferred
independently as s64. The {i64,i64} aggregate was filled with i32
constants, so reading any element back returned garbage (40 + 2^32) and
tuple equality was always false.

lowerTupleLiteral now lowers each element under its resolved field type
(the contextual target tuple's fields when present, else per-element
inference) and coerces to it, so value width always matches field width.
Assignment to a tuple-typed field/element now also propagates the target
tuple type. Adds examples/190-tuple-values.sx as a regression test and
examples/probes/tuple-baseline.sx as the Step 0.4 audit artifact.
This commit is contained in:
agra
2026-05-29 11:52:28 +03:00
parent 9bf3dc75e6
commit 9618f99d0d
5 changed files with 162 additions and 10 deletions

View File

@@ -1729,7 +1729,7 @@ pub const Lowering = struct {
// unchanged into method-call arg slots (`resolveCallParamTypes` can't
// override target_type per-arg).
const needs_target = switch (asgn.value.data) {
.enum_literal, .struct_literal, .if_expr, .match_expr, .block, .unary_op, .binary_op => true,
.enum_literal, .struct_literal, .tuple_literal, .if_expr, .match_expr, .block, .unary_op, .binary_op => true,
.call => |vc| vc.callee.data == .enum_literal,
else => false,
};
@@ -4548,11 +4548,35 @@ pub const Lowering = struct {
defer name_ids.deinit(self.alloc);
var has_names = false;
for (tl.elements) |elem| {
const val = self.lowerExpr(elem.value);
// A tuple_init's element values must match its field types exactly
// (LLVM `insertvalue` does no implicit conversion). When a contextual
// target tuple of matching arity is in scope (annotation, assignment
// LHS, call/return slot), its field types drive element lowering so an
// ambient scalar `target_type` (e.g. the enclosing fn's int return
// type) can't narrow an element below its field width. Otherwise each
// element's type is inferred independently.
var target_fields: ?[]const TypeId = null;
if (self.target_type) |tt| {
if (!tt.isBuiltin()) {
const tinfo = self.module.types.get(tt);
if (tinfo == .tuple and tinfo.tuple.fields.len == tl.elements.len) {
target_fields = tinfo.tuple.fields;
}
}
}
const saved_target = self.target_type;
for (tl.elements, 0..) |elem, i| {
const field_ty = if (target_fields) |tf| tf[i] else self.inferExprType(elem.value);
self.target_type = field_ty;
var val = self.lowerExpr(elem.value);
self.target_type = saved_target;
const val_ty = self.builder.getRefType(val);
if (val_ty != field_ty and val_ty != .void) {
val = self.coerceToType(val, val_ty, field_ty);
}
elems.append(self.alloc, val) catch unreachable;
const ety = self.inferExprType(elem.value);
field_type_ids.append(self.alloc, ety) catch unreachable;
field_type_ids.append(self.alloc, field_ty) catch unreachable;
if (elem.name) |name| {
name_ids.append(self.alloc, self.module.types.internString(name)) catch unreachable;
has_names = true;
@@ -4561,11 +4585,16 @@ pub const Lowering = struct {
}
}
// Create a tuple type
const tuple_ty = self.module.types.intern(.{ .tuple = .{
.fields = self.alloc.dupe(TypeId, field_type_ids.items) catch unreachable,
.names = if (has_names) self.alloc.dupe(types.StringId, name_ids.items) catch unreachable else null,
} });
// Reuse the contextual target tuple type when it drove lowering so the
// value's type identity (incl. field names) matches the destination
// slot; otherwise build the tuple type from the inferred fields.
const tuple_ty = if (target_fields != null and self.target_type != null)
self.target_type.?
else
self.module.types.intern(.{ .tuple = .{
.fields = self.alloc.dupe(TypeId, field_type_ids.items) catch unreachable,
.names = if (has_names) self.alloc.dupe(types.StringId, name_ids.items) catch unreachable else null,
} });
const owned = self.alloc.dupe(Ref, elems.items) catch unreachable;
return self.builder.emit(.{ .tuple_init = .{ .fields = owned } }, tuple_ty);