lower: generalise the *T-where-T-expected diagnostic to any pointer
The check only caught `for xs: (*m)` loop captures; passing a `*T` parameter or any pointer local where `T` is expected still slipped through to the LLVM verifier. Key the diagnostic on the lowered argument's type instead of the capture, so a `*Move` parameter forwarded into a by-value parameter is reported the same way. Ref-capture wording is preserved. Add example 216 (pointer-parameter case) alongside 215 (loop capture).
This commit is contained in:
18
examples/216-pointer-param-value-arg-diagnostic.sx
Normal file
18
examples/216-pointer-param-value-arg-diagnostic.sx
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Passing a `*T` where a `T` value is expected is caught at the call site —
|
||||||
|
// not only for `for xs: (*m)` loop captures (see 215) but for any pointer,
|
||||||
|
// here a `*Move` parameter forwarded into a by-value parameter. Without the
|
||||||
|
// check this slipped through to the LLVM verifier as "Call parameter type
|
||||||
|
// does not match function signature".
|
||||||
|
|
||||||
|
#import "modules/std.sx";
|
||||||
|
|
||||||
|
Move :: struct { flag: s64; }
|
||||||
|
|
||||||
|
take :: (m: Move) -> s64 { return m.flag; }
|
||||||
|
|
||||||
|
forward :: (m: *Move) -> s64 { return take(m); }
|
||||||
|
|
||||||
|
main :: () -> s32 {
|
||||||
|
mv : Move = .{ flag = 7 };
|
||||||
|
return xx forward(@mv);
|
||||||
|
}
|
||||||
@@ -6569,25 +6569,32 @@ pub const Lowering = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A by-reference loop capture (`for xs: (*m)`) binds `m` to a `*T`.
|
const val = self.lowerExpr(arg);
|
||||||
// Passing it where a `T` value is expected used to slip through as a
|
self.target_type = saved_target;
|
||||||
// type-mismatched call that only LLVM's verifier rejected; report it
|
// Passing a `*T` where a `T` value is expected — a by-reference loop
|
||||||
// here with a fix-it instead.
|
// capture (`for xs: (*m)`), a `*T` parameter, or any pointer local —
|
||||||
if (ai < param_types.len and arg.data == .identifier) {
|
// otherwise slips through to LLVM as an opaque "call parameter type
|
||||||
if (self.refCapturePointee(arg)) |pointee| {
|
// does not match function signature" verifier error. Flag it at the
|
||||||
if (pointee == param_types[ai]) {
|
// call site with a `.*` fix-it.
|
||||||
if (self.diagnostics) |d| {
|
if (ai < param_types.len) {
|
||||||
|
const vt = self.builder.getRefType(val);
|
||||||
|
const vti = self.module.types.get(vt);
|
||||||
|
if (vti == .pointer and vti.pointer.pointee == param_types[ai]) {
|
||||||
|
if (self.diagnostics) |d| {
|
||||||
|
const tn = self.formatTypeName(param_types[ai]);
|
||||||
|
if (arg.data == .identifier) {
|
||||||
const nm = arg.data.identifier.name;
|
const nm = arg.data.identifier.name;
|
||||||
const tn = self.formatTypeName(pointee);
|
const lead: []const u8 = if (self.refCapturePointee(arg) != null) "by-reference loop capture" else "argument";
|
||||||
const fix = std.fmt.allocPrint(self.alloc, "{s}.*", .{nm}) catch nm;
|
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 });
|
const pid = d.addFmtId(.err, arg.span, "{s} '{s}' has type '*{s}', but '{s}' is expected here", .{ lead, nm, tn, tn });
|
||||||
d.addHelpFmt(pid, arg.span, fix, "dereference it to pass the value: `{s}`", .{fix});
|
d.addHelpFmt(pid, arg.span, fix, "dereference it to pass the value: `{s}`", .{fix});
|
||||||
|
} else {
|
||||||
|
const pid = d.addFmtId(.err, arg.span, "this argument has type '*{s}', but '{s}' is expected here", .{ tn, tn });
|
||||||
|
d.addHelpFmt(pid, arg.span, null, "dereference it with `.*` to pass the value", .{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const val = self.lowerExpr(arg);
|
|
||||||
self.target_type = saved_target;
|
|
||||||
args.append(self.alloc, val) catch unreachable;
|
args.append(self.alloc, val) catch unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
1
|
||||||
10
tests/expected/216-pointer-param-value-arg-diagnostic.txt
Normal file
10
tests/expected/216-pointer-param-value-arg-diagnostic.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
error: argument 'm' has type '*Move', but 'Move' is expected here
|
||||||
|
--> /Users/agra/projects/sx/examples/216-pointer-param-value-arg-diagnostic.sx:13:44
|
||||||
|
|
|
||||||
|
13 | forward :: (m: *Move) -> s64 { return take(m); }
|
||||||
|
| ^
|
||||||
|
|
||||||
|
help: dereference it to pass the value: `m.*`
|
||||||
|
|
|
||||||
|
13 | m.*
|
||||||
|
| ^
|
||||||
Reference in New Issue
Block a user