authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-18 19:01:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-18 19:02:06-07:00
log3145ae561dadc22105997483fe2330fb96a5c8dd
tree601db25d36018f9244ad40dec8e1616cd4686e3d
parent8daa8d255b84880de60dc1a86d2f1d4a89463bbc

Sema: fix compile error source location regressions


2 files changed, 32 insertions(+), 6 deletions(-)

src/Sema.zig+30-4
......@@ -9086,7 +9086,11 @@ fn finishFunc(
90869086 const tags = sema.code.instructions.items(.tag);
90879087 const data = sema.code.instructions.items(.data);
90889088 const param_body = sema.code.getParamBody(func_inst);
9089 for (block.params.items(.is_comptime), block.params.items(.name), param_body) |is_comptime, name_nts, param_index| {
9089 for (
9090 block.params.items(.is_comptime),
9091 block.params.items(.name),
9092 param_body[0..block.params.len],
9093 ) |is_comptime, name_nts, param_index| {
90909094 if (!is_comptime) {
90919095 const param_src = switch (tags[param_index]) {
90929096 .param => data[param_index].pl_tok.src(),
......@@ -9165,6 +9169,8 @@ fn zirParam(
91659169 param_index: u32,
91669170 comptime_syntax: bool,
91679171) CompileError!void {
9172 const mod = sema.mod;
9173 const gpa = sema.gpa;
91689174 const inst_data = sema.code.instructions.items(.data)[inst].pl_tok;
91699175 const src = inst_data.src();
91709176 const extra = sema.code.extraData(Zir.Inst.Param, inst_data.payload_index);
......@@ -9260,8 +9266,28 @@ fn zirParam(
92609266 else => |e| return e,
92619267 };
92629268 sema.inst_map.putAssumeCapacity(inst, coerced_arg);
9263 sema.comptime_args[param_index] = (try sema.resolveConstMaybeUndefVal(block, src, coerced_arg, "parameter is declared comptime")).toIntern();
9264 return;
9269 if (try sema.resolveMaybeUndefVal(coerced_arg)) |val| {
9270 sema.comptime_args[param_index] = val.toIntern();
9271 return;
9272 }
9273 const arg_src: LazySrcLoc = if (sema.generic_call_src == .node_offset) .{ .call_arg = .{
9274 .decl = sema.generic_call_decl.unwrap().?,
9275 .call_node_offset = sema.generic_call_src.node_offset.x,
9276 .arg_index = param_index,
9277 } } else src;
9278 const msg = msg: {
9279 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);
9280 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9281 @as([]const u8, "runtime-known argument passed to comptime parameter"),
9282 });
9283 errdefer msg.destroy(gpa);
9284
9285 if (sema.generic_call_decl != .none) {
9286 try sema.errNote(block, src, msg, "{s}", .{@as([]const u8, "declared comptime here")});
9287 }
9288 break :msg msg;
9289 };
9290 return sema.failWithOwnedErrorMsg(msg);
92659291 }
92669292 // Even though a comptime argument is provided, the generic function wants to treat
92679293 // this as a runtime parameter.
......@@ -9340,7 +9366,7 @@ fn zirParamAnytype(
93409366 errdefer msg.destroy(gpa);
93419367
93429368 if (sema.generic_call_decl != .none) {
9343 try sema.errNote(block, src, msg, "{s}", .{@as([]const u8, "declared here")});
9369 try sema.errNote(block, src, msg, "{s}", .{@as([]const u8, "declared comptime here")});
93449370 }
93459371 break :msg msg;
93469372 };
test/cases/compile_errors/generic_function_instance_with_non-constant_expression.zig+2-2
......@@ -13,5 +13,5 @@ export fn entry() usize {
1313// backend=stage2
1414// target=native
1515//
16// :5:16: error: unable to resolve comptime value
17// :5:16: note: parameter is comptime
16// :5:16: error: runtime-known argument passed to comptime parameter
17// :1:17: note: declared comptime here