cli: parse dereferenced places in inline borrow arguments

This commit is contained in:
dev committed 2026-10-07 22:55:10 +03:00
1 parent 65e7a0a97c
commit 5f76e92ff3
17 files changed
+185 -2

No files matched your search

+48 -1
View File
@@ -736,7 +736,9 @@ const Parser = struct {
_ = p.next();
unchecked = true;
}
const place = try p.postfix();
// The grammar's place production also permits `* unary`.
// Keep other prefix forms out of an inline borrow operand.
const place = if (p.peek().tag == .star) try p.unary() else try p.postfix();
return .{ .span = p.span(start), .kind = .{ .borrow = .{ .mutable = isWord(t, "mut"), .unchecked = unchecked, .place = try p.box(ast.Expr, place) } } };
}
if (isWord(t, "move")) {
@@ -1193,3 +1195,48 @@ test "each half of a nested type closer contributes to its type range" {
try expectSourceRange(source, box_type.kind.applied.args[0].span, "ref<own<u64>>");
try expectSourceRange(source, box_type.kind.applied.args[0].kind.wrapped.inner.span, "own<u64>");
}
test "inline borrows accept dereferenced places with postfix operands" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
for ([_][]const u8{
"ref *value", "mut *value", "ref **value", "ref *value[index]",
"mut *(value).field", "ref *make(value)",
"ref // UTF-8: λ\n *value",
}) |fragment| {
for ([_][]const u8{ "\n", "\r\n" }) |newline| {
const argument = try std.mem.replaceOwned(u8, a, fragment, "\n", newline);
const source = try std.fmt.allocPrint(a, "module spans{s}fn f() -> void effects [] {{ apply({s}); }}", .{ newline, argument });
const r = try parseWithFile(a, source, "ranges.aji");
try std.testing.expect(r == .ok);
try expectContainedRanges(r.ok, null);
const arg = r.ok.functions[0].body.stmts[0].kind.expr.kind.call.args[0];
try std.testing.expect(arg.kind == .borrow);
try std.testing.expect(arg.kind.borrow.place.kind == .deref);
try expectSourceRange(source, arg.span, argument);
try expectSourceRange(source, arg.kind.borrow.place.span, argument[std.mem.indexOfScalar(u8, argument, '*').?..]);
}
}
}
test "dereferenced borrow consumes unary place without swallowing outer operations" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const r = try parse(a, "module places fn f() -> void effects [] { apply(ref *p[0], mut *q); apply(ref *p + 1u64); }");
try std.testing.expect(r == .ok);
const statements = r.ok.functions[0].body.stmts;
const arguments = statements[0].kind.expr.kind.call.args;
try std.testing.expect(arguments[0].kind.borrow.place.kind.deref.kind == .index);
try std.testing.expect(arguments[1].kind.borrow.mutable);
const addition = statements[1].kind.expr.kind.call.args[0];
try std.testing.expect(addition.kind == .binary);
try std.testing.expect(addition.kind.binary.l.kind == .borrow);
try std.testing.expect(addition.kind.binary.r.kind == .int);
// Do not broaden invalid operand spellings into new prefix forms.
for ([_][]const u8{ "ref -p", "mut not p", "ref move p", "ref ref p", "ref *", "mut *p." }) |argument| {
const source = try std.fmt.allocPrint(a, "module places fn f() -> void effects [] {{ apply({s}); }}", .{argument});
try std.testing.expect((try parse(a, source)) == .err);
}
}