Commit bf62cb453b

Veikka Tuominen <git@vexu.eu>
2022-11-25 01:05:58
Sema: handle anytype parameter requiring comptime
Closes #13645
1 parent d0dd0bd
Changed files (2)
src
test
src/Sema.zig
@@ -7031,16 +7031,21 @@ fn instantiateGenericCall(
             }
             const arg = uncasted_args[arg_i];
             if (is_comptime) {
-                if (try sema.resolveMaybeUndefVal(arg)) |arg_val| {
-                    const child_arg = try child_sema.addConstant(sema.typeOf(arg), arg_val);
-                    child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
-                } else {
-                    return sema.failWithNeededComptime(block, .unneeded, "");
-                }
+                const arg_val = (try sema.resolveMaybeUndefVal(arg)).?;
+                const child_arg = try child_sema.addConstant(sema.typeOf(arg), arg_val);
+                child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
             } else if (is_anytype) {
                 const arg_ty = sema.typeOf(arg);
                 if (try sema.typeRequiresComptime(arg_ty)) {
-                    const arg_val = try sema.resolveConstValue(block, .unneeded, arg, "");
+                    const arg_val = sema.resolveConstValue(block, .unneeded, arg, "") catch |err| switch (err) {
+                        error.NeededSourceLocation => {
+                            const decl = sema.mod.declPtr(block.src_decl);
+                            const arg_src = Module.argSrc(call_src.node_offset.x, sema.gpa, decl, arg_i, bound_arg_src);
+                            _ = try sema.resolveConstValue(block, arg_src, arg, "argument to parameter with comptime-only type must be comptime-known");
+                            return error.AnalysisFail;
+                        },
+                        else => |e| return e,
+                    };
                     const child_arg = try child_sema.addConstant(arg_ty, arg_val);
                     child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
                 } else {
test/cases/compile_errors/anytype_param_requires_comptime.zig
@@ -0,0 +1,20 @@
+const S = struct {
+    fn foo(b: u32, c: anytype) void {
+        const C = struct {
+            c: @TypeOf(c),
+            b: u32,
+        };
+        bar(C{ .c = c, .b = b });
+    }
+    fn bar(_: anytype) void {}
+};
+pub export fn entry() void {
+    S.foo(0, u32);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :7:14: error: unable to resolve comptime value
+// :7:14: note: argument to parameter with comptime-only type must be comptime-known