Commit 9e276d32f3

Veikka Tuominen <git@vexu.eu>
2022-11-20 12:58:37
Sema: fix memory management of missing field error
Closes #13590
1 parent 9e72936
Changed files (2)
src/Sema.zig
@@ -4111,6 +4111,7 @@ fn validateStructInit(
                     .{fqn},
                 );
             }
+            root_msg = null;
             return sema.failWithOwnedErrorMsg(msg);
         }
 
@@ -4230,7 +4231,6 @@ fn validateStructInit(
     }
 
     if (root_msg) |msg| {
-        root_msg = null;
         if (struct_ty.castTag(.@"struct")) |struct_obj| {
             const fqn = try struct_obj.data.getFullyQualifiedName(sema.mod);
             defer gpa.free(fqn);
@@ -4241,6 +4241,7 @@ fn validateStructInit(
                 .{fqn},
             );
         }
+        root_msg = null;
         return sema.failWithOwnedErrorMsg(msg);
     }
 
@@ -17098,7 +17099,6 @@ fn finishStructInit(
     }
 
     if (root_msg) |msg| {
-        root_msg = null;
         if (struct_ty.castTag(.@"struct")) |struct_obj| {
             const fqn = try struct_obj.data.getFullyQualifiedName(sema.mod);
             defer gpa.free(fqn);
@@ -17109,6 +17109,7 @@ fn finishStructInit(
                 .{fqn},
             );
         }
+        root_msg = null;
         return sema.failWithOwnedErrorMsg(msg);
     }
 
@@ -27225,8 +27226,8 @@ fn coerceTupleToStruct(
     }
 
     if (root_msg) |msg| {
-        root_msg = null;
         try sema.addDeclaredHereNote(msg, struct_ty);
+        root_msg = null;
         return sema.failWithOwnedErrorMsg(msg);
     }
 
@@ -27331,8 +27332,8 @@ fn coerceTupleToTuple(
     }
 
     if (root_msg) |msg| {
-        root_msg = null;
         try sema.addDeclaredHereNote(msg, tuple_ty);
+        root_msg = null;
         return sema.failWithOwnedErrorMsg(msg);
     }
 
test/cases/compile_errors/missing_struct_field_in_fn_called_at_comptime.zig
@@ -0,0 +1,18 @@
+const S = struct {
+    a: u32,
+    b: comptime_int,
+    fn init() S {
+        return .{ .a = 1 };
+    }
+};
+comptime {
+    _ = S.init();
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :5:17: error: missing struct field: b
+// :1:11: note: struct 'tmp.S' declared here
+// :9:15: note: called from here