lower: diagnose passing a by-ref loop capture where a value is expected

`for xs: (*m)` binds `m` to a `*T`. Passing it directly to a parameter
that wants `T` produced invalid IR that only LLVM's verifier caught, with
the opaque 'Call parameter type does not match function signature'. Detect
it at the call site and emit a clear error with a fix-it suggesting `m.*`.

Add example 215 + expected output as a regression test.
This commit is contained in:
agra
2026-05-31 13:56:45 +03:00
parent 00f6fad51c
commit 39d51fc26d
4 changed files with 46 additions and 0 deletions

View File

@@ -6569,6 +6569,23 @@ pub const Lowering = struct {
}
}
}
// A by-reference loop capture (`for xs: (*m)`) binds `m` to a `*T`.
// Passing it where a `T` value is expected used to slip through as a
// type-mismatched call that only LLVM's verifier rejected; report it
// here with a fix-it instead.
if (ai < param_types.len and arg.data == .identifier) {
if (self.refCapturePointee(arg)) |pointee| {
if (pointee == param_types[ai]) {
if (self.diagnostics) |d| {
const nm = arg.data.identifier.name;
const tn = self.formatTypeName(pointee);
const fix = std.fmt.allocPrint(self.alloc, "{s}.*", .{nm}) catch nm;
const pid = d.addFmtId(.err, arg.span, "by-reference loop capture '{s}' has type '*{s}', but '{s}' is expected here", .{ nm, tn, tn });
d.addHelpFmt(pid, arg.span, fix, "dereference it to pass the value: `{s}`", .{fix});
}
}
}
}
const val = self.lowerExpr(arg);
self.target_type = saved_target;
args.append(self.alloc, val) catch unreachable;