diag: guide parameter mutability and explicit array slice arguments
This commit is contained in:
1 parent
6a02659f31
commit
9549a3dec6
22 files changed
+197
-4
No files matched your search
+9
-2
@@ -198,6 +198,8 @@ pub const SourceRepairKind = enum {
|
||||
borrow_binding,
|
||||
zero_storage,
|
||||
numeric_cast_modes,
|
||||
parameter_mutability,
|
||||
array_slice_argument,
|
||||
|
||||
pub fn hint(kind: SourceRepairKind) []const u8 {
|
||||
return switch (kind) {
|
||||
@@ -210,17 +212,22 @@ pub const SourceRepairKind = enum {
|
||||
.borrow_binding => "Bind a borrow with `ref name = place;` or `mut name = place;`, rather than a let/var initializer.",
|
||||
.zero_storage => "`zero` is only a whole var initializer: `var name: T = zero;`; T must be zeroable.",
|
||||
.numeric_cast_modes => "The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.",
|
||||
.parameter_mutability => "Write `name: T` for a value parameter; use a local `var` for mutable storage, or `name: mut<T>` for an exclusive borrowed parameter. Choose the intended ownership contract.",
|
||||
.array_slice_argument => "Pass an array as an explicit borrow: `ref array` for `slice<T>` or `mut array` for `slice_mut<T>`. The place and loan must still verify.",
|
||||
};
|
||||
}
|
||||
|
||||
pub fn code(kind: SourceRepairKind) []const u8 {
|
||||
return if (kind == .numeric_cast_modes) "E7005" else "E7001";
|
||||
return switch (kind) {
|
||||
.numeric_cast_modes, .array_slice_argument => "E7005",
|
||||
else => "E7001",
|
||||
};
|
||||
}
|
||||
|
||||
pub fn unit(kind: SourceRepairKind) []const u8 {
|
||||
return switch (kind) {
|
||||
.slice_mut_type, .slice_ref_type => "complete type",
|
||||
.borrow_argument => "prefix",
|
||||
.borrow_argument, .array_slice_argument => "prefix",
|
||||
.numeric_cast_modes => "operator",
|
||||
else => "none",
|
||||
};
|
||||
|
||||
+18
-1
@@ -1945,7 +1945,7 @@ const Lowerer = struct {
|
||||
for (args, sig.params, 0..) |a, pt, i| {
|
||||
const v = try l.argument(a, pt, &temps);
|
||||
if (sig.from == i) parent = v.name;
|
||||
try l.checkOperandTy(a.span, v.ty, pt);
|
||||
try l.checkCallArgumentTy(a, v.ty, pt);
|
||||
try arg_names.appendSlice(l.arena, try l.fmt("{s}%{s}", .{ if (i == 0) "" else ", ", v.name }));
|
||||
}
|
||||
const text = try l.fmt("call @{s}({s})", .{ sig.air_name, arg_names.items });
|
||||
@@ -2313,6 +2313,23 @@ const Lowerer = struct {
|
||||
return .{ .name = st, .ty = ty };
|
||||
}
|
||||
|
||||
/// Advice belongs to the existing ordinary-call mismatch, after argument
|
||||
/// lowering. Never change earlier name/move errors or infer a borrow.
|
||||
fn checkCallArgumentTy(l: *Lowerer, a: ast.Expr, found: Ty, want: Ty) Error!void {
|
||||
l.checkOperandTy(a.span, found, want) catch |err| {
|
||||
if (err == error.Lower and a.kind == .name and found == .array and
|
||||
(want == .slice or want == .slice_mut) and found.array.elem.eql(want.inner().?) and !l.isLinear(found))
|
||||
{
|
||||
const kind = @import("../diag.zig").SourceRepairKind.array_slice_argument;
|
||||
l.problem.?.repair_candidates = if (want == .slice)
|
||||
comptime &.{kind.candidate(&.{"ref"})}
|
||||
else
|
||||
comptime &.{kind.candidate(&.{"mut"})};
|
||||
}
|
||||
return err;
|
||||
};
|
||||
}
|
||||
|
||||
/// A call argument: `ref place` / `mut place` and byte strings are
|
||||
/// borrowed for the call (`temps` end after it); anything else is a value.
|
||||
fn argument(l: *Lowerer, a: ast.Expr, want: ?Ty, temps: *std.ArrayList([]const u8)) Error!Value {
|
||||
|
||||
@@ -374,6 +374,8 @@ const Parser = struct {
|
||||
var params: std.ArrayList(ast.Param) = .empty;
|
||||
while (p.peek().tag != .r_paren) {
|
||||
const ps = p.peek().pos;
|
||||
if (p.atWord("mut") and p.peekAt(1).tag == .ident and !isKeyword(p.peekAt(1).text) and p.peekAt(2).tag == .colon)
|
||||
return p.failAdvice(p.peek(), "parameter name", .parameter_mutability);
|
||||
const pname = try p.ident("parameter name");
|
||||
_ = try p.expect(.colon, "`:`");
|
||||
const ty = try p.typeExpr();
|
||||
|
||||
Reference in new issue
Block a user