authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-09 09:26:16-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-09 10:09:01-04:00
log736df276632ad66294a7491a5e35a039dd2c836f
tree3f81ef72acd1d2ac6ea722c9dbfaafe1d06bb1b2
parent9630379a8ec593adc65880e814e4c7161b6d5775

Sema: use the correct decl for generic argument source locations

Closes #16746

2 files changed, 54 insertions(+), 19 deletions(-)

src/Sema.zig+18-19
...@@ -9219,6 +9219,21 @@ fn finishFunc(...@@ -9219,6 +9219,21 @@ fn finishFunc(
9219 return Air.internedToRef(if (opt_func_index != .none) opt_func_index else func_ty);9219 return Air.internedToRef(if (opt_func_index != .none) opt_func_index else func_ty);
9220}9220}
92219221
9222fn genericArgSrcLoc(sema: *Sema, block: *Block, param_index: u32, param_src: LazySrcLoc) Module.SrcLoc {
9223 const mod = sema.mod;
9224 if (sema.generic_owner == .none) return param_src.toSrcLoc(mod.declPtr(block.src_decl), mod);
9225 const arg_decl = sema.generic_call_decl.unwrap().?;
9226 const arg_src: LazySrcLoc = if (param_index == 0 and sema.generic_bound_arg_src != null)
9227 sema.generic_bound_arg_src.?
9228 else
9229 .{ .call_arg = .{
9230 .decl = arg_decl,
9231 .call_node_offset = sema.generic_call_src.node_offset.x,
9232 .arg_index = param_index - @intFromBool(sema.generic_bound_arg_src != null),
9233 } };
9234 return arg_src.toSrcLoc(mod.declPtr(arg_decl), mod);
9235}
9236
9222fn zirParam(9237fn zirParam(
9223 sema: *Sema,9238 sema: *Sema,
9224 block: *Block,9239 block: *Block,
...@@ -9226,7 +9241,6 @@ fn zirParam(...@@ -9226,7 +9241,6 @@ fn zirParam(
9226 param_index: u32,9241 param_index: u32,
9227 comptime_syntax: bool,9242 comptime_syntax: bool,
9228) CompileError!void {9243) CompileError!void {
9229 const mod = sema.mod;
9230 const gpa = sema.gpa;9244 const gpa = sema.gpa;
9231 const inst_data = sema.code.instructions.items(.data)[inst].pl_tok;9245 const inst_data = sema.code.instructions.items(.data)[inst].pl_tok;
9232 const src = inst_data.src();9246 const src = inst_data.src();
...@@ -9330,15 +9344,8 @@ fn zirParam(...@@ -9330,15 +9344,8 @@ fn zirParam(
9330 sema.comptime_args[param_index] = val.toIntern();9344 sema.comptime_args[param_index] = val.toIntern();
9331 return;9345 return;
9332 }9346 }
9333 const arg_src: LazySrcLoc = if (param_index == 0 and sema.generic_bound_arg_src != null)
9334 sema.generic_bound_arg_src.?
9335 else if (sema.generic_call_src == .node_offset) .{ .call_arg = .{
9336 .decl = sema.generic_call_decl.unwrap().?,
9337 .call_node_offset = sema.generic_call_src.node_offset.x,
9338 .arg_index = param_index - @intFromBool(sema.generic_bound_arg_src != null),
9339 } } else src;
9340 const msg = msg: {9347 const msg = msg: {
9341 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);9348 const src_loc = sema.genericArgSrcLoc(block, param_index, src);
9342 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{9349 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9343 @as([]const u8, "runtime-known argument passed to comptime parameter"),9350 @as([]const u8, "runtime-known argument passed to comptime parameter"),
9344 });9351 });
...@@ -9384,7 +9391,6 @@ fn zirParamAnytype(...@@ -9384,7 +9391,6 @@ fn zirParamAnytype(
9384 param_index: u32,9391 param_index: u32,
9385 comptime_syntax: bool,9392 comptime_syntax: bool,
9386) CompileError!void {9393) CompileError!void {
9387 const mod = sema.mod;
9388 const gpa = sema.gpa;9394 const gpa = sema.gpa;
9389 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;9395 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
9390 const param_name: Zir.NullTerminatedString = @enumFromInt(inst_data.start);9396 const param_name: Zir.NullTerminatedString = @enumFromInt(inst_data.start);
...@@ -9398,13 +9404,6 @@ fn zirParamAnytype(...@@ -9398,13 +9404,6 @@ fn zirParamAnytype(
9398 sema.comptime_args[param_index] = opv.toIntern();9404 sema.comptime_args[param_index] = opv.toIntern();
9399 return;9405 return;
9400 }9406 }
9401 const arg_src: LazySrcLoc = if (param_index == 0 and sema.generic_bound_arg_src != null)
9402 sema.generic_bound_arg_src.?
9403 else if (sema.generic_call_src == .node_offset) .{ .call_arg = .{
9404 .decl = sema.generic_call_decl.unwrap().?,
9405 .call_node_offset = sema.generic_call_src.node_offset.x,
9406 .arg_index = param_index - @intFromBool(sema.generic_bound_arg_src != null),
9407 } } else src;
94089407
9409 if (comptime_syntax) {9408 if (comptime_syntax) {
9410 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {9409 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {
...@@ -9412,7 +9411,7 @@ fn zirParamAnytype(...@@ -9412,7 +9411,7 @@ fn zirParamAnytype(
9412 return;9411 return;
9413 }9412 }
9414 const msg = msg: {9413 const msg = msg: {
9415 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);9414 const src_loc = sema.genericArgSrcLoc(block, param_index, src);
9416 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{9415 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9417 @as([]const u8, "runtime-known argument passed to comptime parameter"),9416 @as([]const u8, "runtime-known argument passed to comptime parameter"),
9418 });9417 });
...@@ -9432,7 +9431,7 @@ fn zirParamAnytype(...@@ -9432,7 +9431,7 @@ fn zirParamAnytype(
9432 return;9431 return;
9433 }9432 }
9434 const msg = msg: {9433 const msg = msg: {
9435 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);9434 const src_loc = sema.genericArgSrcLoc(block, param_index, src);
9436 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{9435 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9437 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),9436 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),
9438 });9437 });
test/compile_errors.zig+36
...@@ -183,4 +183,40 @@ pub fn addCases(ctx: *Cases) !void {...@@ -183,4 +183,40 @@ pub fn addCases(ctx: *Cases) !void {
183 ":1:1: note: invalid byte: '\\xff'",183 ":1:1: note: invalid byte: '\\xff'",
184 });184 });
185 }185 }
186
187 {
188 const case = ctx.obj("imported generic method call with invalid param", .{});
189
190 case.addError(
191 \\pub const import = @import("import.zig");
192 \\
193 \\export fn callComptimeBoolFunctionWithRuntimeBool(x: bool) void {
194 \\ import.comptimeBoolFunction(x);
195 \\}
196 \\
197 \\export fn callComptimeAnytypeFunctionWithRuntimeBool(x: bool) void {
198 \\ import.comptimeAnytypeFunction(x);
199 \\}
200 \\
201 \\export fn callAnytypeFunctionWithRuntimeComptimeOnlyType(x: u32) void {
202 \\ const S = struct { x: u32, y: type };
203 \\ import.anytypeFunction(S{ .x = x, .y = u32 });
204 \\}
205 , &[_][]const u8{
206 ":4:33: error: runtime-known argument passed to comptime parameter",
207 ":1:38: note: declared comptime here",
208 ":8:36: error: runtime-known argument passed to comptime parameter",
209 ":2:41: note: declared comptime here",
210 ":13:29: error: runtime-known argument passed to comptime-only type parameter",
211 ":3:24: note: declared here",
212 ":12:35: note: struct requires comptime because of this field",
213 ":12:35: note: types are not available at runtime",
214 });
215
216 case.addSourceFile("import.zig",
217 \\pub fn comptimeBoolFunction(comptime _: bool) void {}
218 \\pub fn comptimeAnytypeFunction(comptime _: anytype) void {}
219 \\pub fn anytypeFunction(_: anytype) void {}
220 );
221 }
186}222}