authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-05 03:31:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-29 14:20:52-07:00
log8c94e993e4bfe5015297083bba2120ba75ea9e58
tree213a9983804a9e2d0d5b0b0fc81ecf35a5952c8a
parentc118c45125b9ca5e07255ee6d4822634460431cd

Sema: fix issues passing an invalid type to a generic method

Closes #16601

2 files changed, 22 insertions(+), 12 deletions(-)

src/Sema.zig+1-12
...@@ -7622,7 +7622,7 @@ fn instantiateGenericCall(...@@ -7622,7 +7622,7 @@ fn instantiateGenericCall(
7622 else if (call_src == .node_offset) .{ .call_arg = .{7622 else if (call_src == .node_offset) .{ .call_arg = .{
7623 .decl = block.src_decl,7623 .decl = block.src_decl,
7624 .call_node_offset = call_src.node_offset.x,7624 .call_node_offset = call_src.node_offset.x,
7625 .arg_index = @intCast(total_i),7625 .arg_index = @intCast(total_i - @intFromBool(bound_arg_src != null)),
7626 } } else .unneeded;7626 } } else .unneeded;
76277627
7628 const comptime_arg = callee.comptime_args.get(ip)[total_i];7628 const comptime_arg = callee.comptime_args.get(ip)[total_i];
...@@ -9343,17 +9343,6 @@ fn zirParam(...@@ -9343,17 +9343,6 @@ fn zirParam(
9343 assert(sema.inst_map.remove(inst));9343 assert(sema.inst_map.remove(inst));
9344 }9344 }
93459345
9346 if (sema.generic_owner != .none) {
9347 if (try sema.typeHasOnePossibleValue(param_ty)) |opv| {
9348 // In this case we are instantiating a generic function call with a non-comptime
9349 // non-anytype parameter that ended up being a one-possible-type.
9350 // We don't want the parameter to be part of the instantiated function type.
9351 sema.inst_map.putAssumeCapacity(inst, Air.internedToRef(opv.toIntern()));
9352 sema.comptime_args[param_index] = opv.toIntern();
9353 return;
9354 }
9355 }
9356
9357 try block.params.append(sema.arena, .{9346 try block.params.append(sema.arena, .{
9358 .ty = param_ty.toIntern(),9347 .ty = param_ty.toIntern(),
9359 .is_comptime = comptime_syntax,9348 .is_comptime = comptime_syntax,
test/cases/compile_errors/generic_method_call_invalid_coercion.zig created+21
...@@ -0,0 +1,21 @@
1export fn callBoolMethod() void {
2 const s = S{};
3 s.boolMethod({});
4}
5
6export fn callVoidMethod() void {
7 const s = S{};
8 s.voidMethod(false);
9}
10
11const S = struct {
12 fn boolMethod(comptime _: @This(), _: bool) void {}
13 fn voidMethod(comptime _: @This(), _: void) void {}
14};
15
16// error
17// backend=stage2
18// target=native
19//
20// :3:18: error: expected type 'bool', found 'void'
21// :8:18: error: expected type 'void', found 'bool'