authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-05-10 18:22:43+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-05-16 13:28:15+01:00
log429672705090f073adedb885057a4603eaf66c95
tree0776a0d58f9550258bbd3ebcc3232f61663b7764
parentd717c96877355090ea3a8e3662a2584eea02445c
signaturelock-open Commit is signed but in an unrecognized format.

Sema: improve "called from here" notes

To an average user, it may be unclear why these notes are not just in the reference trace; that's because they are more important, because they are inline calls through which comptime values may propagate. There are now 3 possible wordings for this note: * "called at comptime here" * "called inline here" * "generic function instantiated here" An alternative could be these wordings: * "while analyzing comptime call here" * "while analyzing inline call here" * "while analyzing generic instantiation here" I'm not sure which is better -- but this commit is certainly better than status quo.

1 files changed, 26 insertions(+), 14 deletions(-)

src/Sema.zig+26-14
......@@ -444,13 +444,19 @@ pub const Block = struct {
444444 pub const Inlining = struct {
445445 call_block: *Block,
446446 call_src: LazySrcLoc,
447 has_comptime_args: bool,
448447 func: InternPool.Index,
449 comptime_result: Air.Inst.Ref,
450 merges: Merges,
448
451449 /// Populated lazily by `refFrame`.
452450 ref_frame: Zcu.InlineReferenceFrame.Index.Optional = .none,
453451
452 /// If `true`, the following fields are `undefined`. This doesn't represent a true inline
453 /// call, but rather a generic call analyzing the instantiation's generic type bodies.
454 is_generic_instantiation: bool,
455
456 has_comptime_args: bool,
457 comptime_result: Air.Inst.Ref,
458 merges: Merges,
459
454460 fn refFrame(inlining: *Inlining, zcu: *Zcu) Allocator.Error!Zcu.InlineReferenceFrame.Index {
455461 if (inlining.ref_frame == .none) {
456462 inlining.ref_frame = (try zcu.addInlineReferenceFrame(.{
......@@ -2604,12 +2610,12 @@ pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Zcu.ErrorMsg
26042610 if (block) |start_block| {
26052611 var block_it = start_block;
26062612 while (block_it.inlining) |inlining| {
2607 try sema.errNote(
2608 inlining.call_src,
2609 err_msg,
2610 "called from here",
2611 .{},
2612 );
2613 const note_str = note: {
2614 if (inlining.is_generic_instantiation) break :note "generic function instantiated here";
2615 if (inlining.call_block.isComptime()) break :note "called at comptime here";
2616 break :note "called inline here";
2617 };
2618 try sema.errNote(inlining.call_src, err_msg, "{s}", .{note_str});
26132619 block_it = inlining.call_block;
26142620 }
26152621 }
......@@ -7736,9 +7742,10 @@ fn analyzeCall(
77367742 .call_block = block,
77377743 .call_src = call_src,
77387744 .func = func_val.?.toIntern(),
7739 .has_comptime_args = false, // unused by error reporting
7740 .comptime_result = .none, // unused by error reporting
7741 .merges = undefined, // unused because we'll never `return`
7745 .is_generic_instantiation = true, // this allows the following fields to be `undefined`
7746 .has_comptime_args = undefined,
7747 .comptime_result = undefined,
7748 .merges = undefined,
77427749 } else undefined;
77437750
77447751 // This is the block in which we evaluate generic function components: that is, generic parameter
......@@ -8216,10 +8223,11 @@ fn analyzeCall(
82168223 var inlining: Block.Inlining = .{
82178224 .call_block = block,
82188225 .call_src = call_src,
8226 .func = func_val.?.toIntern(),
8227 .is_generic_instantiation = false,
82198228 .has_comptime_args = for (args) |a| {
82208229 if (try sema.isComptimeKnown(a)) break true;
82218230 } else false,
8222 .func = func_val.?.toIntern(),
82238231 .comptime_result = undefined,
82248232 .merges = .{
82258233 .block_inst = block_inst,
......@@ -8250,7 +8258,10 @@ fn analyzeCall(
82508258 if (!inlining.has_comptime_args) {
82518259 var block_it = block;
82528260 while (block_it.inlining) |parent_inlining| {
8253 if (!parent_inlining.has_comptime_args and parent_inlining.func == func_val.?.toIntern()) {
8261 if (!parent_inlining.is_generic_instantiation and
8262 !parent_inlining.has_comptime_args and
8263 parent_inlining.func == func_val.?.toIntern())
8264 {
82548265 return sema.fail(block, call_src, "inline call is recursive", .{});
82558266 }
82568267 block_it = parent_inlining.call_block;
......@@ -19454,6 +19465,7 @@ fn analyzeRet(
1945419465 };
1945519466
1945619467 if (block.inlining) |inlining| {
19468 assert(!inlining.is_generic_instantiation); // can't `return` in a generic param/ret ty expr
1945719469 if (block.isComptime()) {
1945819470 const ret_val = try sema.resolveConstValue(block, operand_src, operand, null);
1945919471 inlining.comptime_result = operand;