Commit ff0e2ab398

Anton Lilja <12533691+antlilja@users.noreply.github.com>
2023-07-12 08:35:50
Fixes wrong error location for unionInit when first parameter is not a type (#16384)
1 parent 660955c
Changed files (3)
src/AstGen.zig
@@ -7936,7 +7936,7 @@ fn unionInit(
 ) InnerError!Zir.Inst.Ref {
     const union_type = try typeExpr(gz, scope, params[0]);
     const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]);
-    const field_type = try gz.addPlNode(.field_type_ref, params[1], Zir.Inst.FieldTypeRef{
+    const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
         .container_type = union_type,
         .field_name = field_name,
     });
src/Sema.zig
@@ -19368,8 +19368,8 @@ fn addConstantMaybeRef(
 fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
     const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
     const extra = sema.code.extraData(Zir.Inst.FieldTypeRef, inst_data.payload_index).data;
-    const ty_src = inst_data.src();
-    const field_src = inst_data.src();
+    const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
+    const field_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
     const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type);
     const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, "field name must be comptime-known");
     return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);
test/cases/compile_errors/union_init_with_non_type_as_first_param.zig
@@ -0,0 +1,9 @@
+export fn u() void {
+    _ = @unionInit(0, "a", 0);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:20: error: expected type 'type', found 'comptime_int'