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(
92199219 return Air.internedToRef(if (opt_func_index != .none) opt_func_index else func_ty);
92209220}
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
92229237fn zirParam(
92239238 sema: *Sema,
92249239 block: *Block,
......@@ -9226,7 +9241,6 @@ fn zirParam(
92269241 param_index: u32,
92279242 comptime_syntax: bool,
92289243) CompileError!void {
9229 const mod = sema.mod;
92309244 const gpa = sema.gpa;
92319245 const inst_data = sema.code.instructions.items(.data)[inst].pl_tok;
92329246 const src = inst_data.src();
......@@ -9330,15 +9344,8 @@ fn zirParam(
93309344 sema.comptime_args[param_index] = val.toIntern();
93319345 return;
93329346 }
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;
93409347 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);
93429349 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
93439350 @as([]const u8, "runtime-known argument passed to comptime parameter"),
93449351 });
......@@ -9384,7 +9391,6 @@ fn zirParamAnytype(
93849391 param_index: u32,
93859392 comptime_syntax: bool,
93869393) CompileError!void {
9387 const mod = sema.mod;
93889394 const gpa = sema.gpa;
93899395 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
93909396 const param_name: Zir.NullTerminatedString = @enumFromInt(inst_data.start);
......@@ -9398,13 +9404,6 @@ fn zirParamAnytype(
93989404 sema.comptime_args[param_index] = opv.toIntern();
93999405 return;
94009406 }
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
94099408 if (comptime_syntax) {
94109409 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {
......@@ -9412,7 +9411,7 @@ fn zirParamAnytype(
94129411 return;
94139412 }
94149413 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);
94169415 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
94179416 @as([]const u8, "runtime-known argument passed to comptime parameter"),
94189417 });
......@@ -9432,7 +9431,7 @@ fn zirParamAnytype(
94329431 return;
94339432 }
94349433 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);
94369435 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
94379436 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),
94389437 });
test/compile_errors.zig+36
......@@ -183,4 +183,40 @@ pub fn addCases(ctx: *Cases) !void {
183183 ":1:1: note: invalid byte: '\\xff'",
184184 });
185185 }
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 }
186222}