aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Sema.zig19
-rw-r--r--test/cases/compile_errors/anytype_param_requires_comptime.zig20
2 files changed, 32 insertions, 7 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index d414bfc1ce..65bdf7e1e0 100644
--- a/src/Sema.zig
+++ b/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 {
diff --git a/test/cases/compile_errors/anytype_param_requires_comptime.zig b/test/cases/compile_errors/anytype_param_requires_comptime.zig
new file mode 100644
index 0000000000..3e2b32b408
--- /dev/null
+++ b/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