Closed davidgm94 closed 5 months ago
If it serves to you this all happened when concatenating an enum array with one-element tuple with enum literal that does not belong to the enum (the enum doesn't have a field by that name)
Reduction:
const Foo = enum {
a,
};
test {
const arr = [_]Foo{.a};
_ = arr ++ .{.b};
}
I started investigating this and I think I'm really close to having it fixed. I might be able to finish it tomorrow.
With this patch:
diff --git a/src/Sema.zig b/src/Sema.zig
index f703c6154..c41c118b6 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -13353,7 +13353,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const elem_default_val = if (rhs_is_tuple) rhs_ty.structFieldDefaultValue(rhs_elem_i, mod) else Value.@"unreachable";
const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try rhs_sub_val.elemValue(mod, rhs_elem_i) else elem_default_val;
const elem_val_inst = try sema.addConstant(elem_ty, elem_val);
- const coerced_elem_val_inst = try sema.coerce(block, resolved_elem_ty, elem_val_inst, .unneeded);
+ const coerced_elem_val_inst = sema.coerce(block, resolved_elem_ty, elem_val_inst, rhs_src);
const coerced_elem_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, coerced_elem_val_inst, "");
element_vals[elem_i] = try coerced_elem_val.intern(resolved_elem_ty, mod);
}
It generates an error saying that Foo doesn't have a field called b
, but it points to all of .{.b}
instead of just .b
. For the life of me, I can't figure out how to get the proper source location here.
Here is a complete test case:
export fn a() void {
const Foo = enum {
a,
};
const arr = [_]Foo{.a};
_ = arr ++ .{.b};
}
export fn b() void {
const Foo = enum {
a,
};
const arr = [_]Foo{.a};
_ = .{.b} ++ arr;
}
export fn c() void {
comptime var things: []const i32 = &.{};
things = things ++ .{&1};
}
export fn d() void {
comptime var things: []const i32 = &.{};
things = .{&1} ++ things;
}
@Vexu The compiler works correctly with 0.12.0-dev.2341+92211135
on Linux:
test.zig:7:19: error: no field named 'b' in enum 'test.Foo'
_ = arr ++ .{.b};
~^
test.zig:1:13: note: enum declared here
const Foo = enum {
^~~~
Fixed by #18162 which added test coverage.
Zig Version
0.11.0-dev.3658+5d9e8f27d
Steps to Reproduce and Observed Behavior
Edit: I posted a reduction a couple of comments below:
Log:
Expected Behavior
Build or error