Commit 490addde27

Veikka Tuominen <git@vexu.eu>
2023-02-01 19:50:43
Sema: fix error location on comptime arg to typed generic param
Closes #14505
1 parent f3bb195
Changed files (2)
src/Sema.zig
@@ -9015,7 +9015,18 @@ fn zirParam(
         if (is_comptime and sema.preallocated_new_func != null) {
             // We have a comptime value for this parameter so it should be elided from the
             // function type of the function instruction in this block.
-            const coerced_arg = try sema.coerce(block, param_ty, arg, src);
+            const coerced_arg = sema.coerce(block, param_ty, arg, .unneeded) catch |err| switch (err) {
+                error.NeededSourceLocation => {
+                    // We are instantiating a generic function and a comptime arg
+                    // cannot be coerced to the param type, but since we don't
+                    // have the callee source location return `GenericPoison`
+                    // so that the instantiation is failed and the coercion
+                    // is handled by comptime call logic instead.
+                    assert(sema.is_generic_instantiation);
+                    return error.GenericPoison;
+                },
+                else => return err,
+            };
             sema.inst_map.putAssumeCapacity(inst, coerced_arg);
             return;
         }
test/cases/compile_errors/comptime_arg_to_generic_fn_callee_error.zig
@@ -0,0 +1,21 @@
+const std = @import("std");
+const MyStruct = struct {
+    a: i32,
+    b: i32,
+
+    pub fn getA(self: *List) i32 {
+        return self.items(.c);
+    }
+};
+const List = std.MultiArrayList(MyStruct);
+pub export fn entry() void {
+    var list = List{};
+    _ = MyStruct.getA(&list);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :7:28: error: no field named 'c' in enum 'meta.FieldEnum(tmp.MyStruct)'
+// :?:?: note: enum declared here