diag: add stable source reasons and diagnostic incidence reporting
This commit is contained in:
1 parent
911982dd1c
commit
dbd9763713
21 files changed
+571
-156
No files matched your search
+90
-81
@@ -139,7 +139,10 @@ fn intBits(n: []const u8) u16 {
|
||||
return std.fmt.parseInt(u16, n[1..], 10) catch 64;
|
||||
}
|
||||
|
||||
pub const InvalidReason = @import("../diag.zig").SourceInvalidReason;
|
||||
|
||||
pub const Problem = struct {
|
||||
reason: ?InvalidReason = null,
|
||||
code: []const u8,
|
||||
span: Span,
|
||||
message: []const u8,
|
||||
@@ -355,12 +358,18 @@ const Lowerer = struct {
|
||||
};
|
||||
}
|
||||
|
||||
fn fail(l: *Lowerer, code: []const u8, span: Span, message: []const u8, fields: []const Problem.Field) Error {
|
||||
fn fail(l: *Lowerer, comptime code: []const u8, span: Span, message: []const u8, fields: []const Problem.Field) Error {
|
||||
if (comptime std.mem.eql(u8, code, "E7006")) @compileError("E7006 requires an explicit reason via invalid()");
|
||||
// `fields` usually points at a temporary array: keep a copy.
|
||||
l.problem = .{ .code = code, .span = span, .message = message, .fields = try l.arena.dupe(Problem.Field, fields) };
|
||||
return error.Lower;
|
||||
}
|
||||
|
||||
fn invalid(l: *Lowerer, reason: InvalidReason, span: Span, message: []const u8, fields: []const Problem.Field) Error {
|
||||
l.problem = .{ .code = "E7006", .reason = reason, .span = span, .message = message, .fields = try l.arena.dupe(Problem.Field, fields) };
|
||||
return error.Lower;
|
||||
}
|
||||
|
||||
fn notYet(l: *Lowerer, span: Span, construct: []const u8) Error {
|
||||
l.unimpl = .{ .span = span, .construct = construct };
|
||||
return error.Unimplemented;
|
||||
@@ -443,12 +452,12 @@ const Lowerer = struct {
|
||||
fn run(l: *Lowerer) Error!void {
|
||||
const names = try l.arena.alloc([]const u8, l.m.types.len);
|
||||
for (l.m.types, names, 0..) |t, *n, i| {
|
||||
for (l.m.types[0..i]) |prev| if (std.mem.eql(u8, prev.name, t.name)) return l.fail("E7006", t.span, "type defined twice", &.{.{ .name = "name", .value = t.name }});
|
||||
for (l.m.types[0..i]) |prev| if (std.mem.eql(u8, prev.name, t.name)) return l.invalid(.duplicate_declaration, t.span, "type defined twice", &.{.{ .name = "name", .value = t.name }});
|
||||
n.* = try l.fmt("@{s}", .{t.air_name orelse t.name});
|
||||
}
|
||||
l.typeNames = names;
|
||||
for (l.m.functions) |f| {
|
||||
if (l.sigs.contains(f.name)) return l.fail("E7006", f.span, "function defined twice", &.{.{ .name = "name", .value = f.name }});
|
||||
if (l.sigs.contains(f.name)) return l.invalid(.duplicate_declaration, f.span, "function defined twice", &.{.{ .name = "name", .value = f.name }});
|
||||
const ps = try l.arena.alloc(Ty, f.params.len);
|
||||
for (f.params, ps) |p, *t| t.* = try l.typeOf(p.ty);
|
||||
const ret = try l.typeOf(f.ret);
|
||||
@@ -461,8 +470,8 @@ const Lowerer = struct {
|
||||
const i = origin orelse return l.unknown(f.span, name);
|
||||
if (!ret.isBorrow() or !ps[i].isBorrow()) return l.fail("E7005", f.span, "`from` requires a borrow return and a borrow parameter", &.{});
|
||||
if (ret.isMutBorrow() and !ps[i].isMutBorrow()) return l.fail("E7005", f.span, "an exclusive return requires an exclusive `from` parameter", &.{});
|
||||
} else if (ret.isBorrow()) return l.fail("E7006", f.span, "a borrow return requires `from parameter`", &.{});
|
||||
if (std.mem.eql(u8, f.name, "main") and ret.isBorrow()) return l.fail("E7006", f.span, "main cannot return a borrow", &.{});
|
||||
} else if (ret.isBorrow()) return l.invalid(.invalid_borrow_return, f.span, "a borrow return requires `from parameter`", &.{});
|
||||
if (std.mem.eql(u8, f.name, "main") and ret.isBorrow()) return l.invalid(.invalid_borrow_return, f.span, "main cannot return a borrow", &.{});
|
||||
try l.sigs.put(l.arena, f.name, .{ .params = ps, .ret = ret, .from = origin, .air_name = f.air_name orelse f.name, .display_name = f.display_name orelse f.name });
|
||||
}
|
||||
if (!l.sigs.contains("main")) return l.fail("E7002", .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 1 } }, "the module has no `main` function", &.{.{ .name = "name", .value = "main" }});
|
||||
@@ -550,7 +559,7 @@ const Lowerer = struct {
|
||||
for (l.m.requirements) |r| {
|
||||
const ty = try l.typeOf(r.ty);
|
||||
const t = try l.airType(ty);
|
||||
if (!boundedType(m, t, 0)) return l.fail("E7006", r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (!boundedType(m, t, 0)) return l.invalid(.implementation_limit, r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (r.property.len == 0) {
|
||||
var context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
|
||||
try verifier_types.checkTypeArgument(&context, .{ .line = 1, .col = 1 }, t);
|
||||
@@ -632,17 +641,17 @@ const Lowerer = struct {
|
||||
.@"struct" => |fields| {
|
||||
try w.appendSlice(l.arena, "struct {");
|
||||
for (fields, 0..) |f, i| {
|
||||
for (fields[0..i]) |prev| if (std.mem.eql(u8, prev.name, f.name)) return l.fail("E7006", f.span, "field declared twice", &.{.{ .name = "field", .value = f.name }});
|
||||
for (fields[0..i]) |prev| if (std.mem.eql(u8, prev.name, f.name)) return l.invalid(.duplicate_declaration, f.span, "field declared twice", &.{.{ .name = "field", .value = f.name }});
|
||||
const ft = try l.memberType(f.ty);
|
||||
try w.appendSlice(l.arena, try l.fmt("{s}{s}: {s}", .{ if (i == 0) " " else ", ", f.name, l.tyText(ft) }));
|
||||
}
|
||||
try w.appendSlice(l.arena, if (fields.len == 0) "}" else " }");
|
||||
},
|
||||
.@"enum" => |vs| {
|
||||
if (vs.len == 0) return l.fail("E7006", t.span, "an enum needs at least one variant", &.{.{ .name = "type", .value = t.name }});
|
||||
if (vs.len == 0) return l.invalid(.invalid_declaration, t.span, "an enum needs at least one variant", &.{.{ .name = "type", .value = t.name }});
|
||||
try w.appendSlice(l.arena, "enum { ");
|
||||
for (vs, 0..) |v, i| {
|
||||
for (vs[0..i]) |prev| if (std.mem.eql(u8, prev.name, v.name)) return l.fail("E7006", v.span, "variant declared twice", &.{.{ .name = "variant", .value = v.name }});
|
||||
for (vs[0..i]) |prev| if (std.mem.eql(u8, prev.name, v.name)) return l.invalid(.duplicate_declaration, v.span, "variant declared twice", &.{.{ .name = "variant", .value = v.name }});
|
||||
if (i != 0) try w.appendSlice(l.arena, ", ");
|
||||
try w.appendSlice(l.arena, v.name);
|
||||
if (v.payload.len != 0) {
|
||||
@@ -666,7 +675,7 @@ const Lowerer = struct {
|
||||
fn typeOf(l: *Lowerer, t: ast.Type) Error!Ty {
|
||||
switch (t.kind) {
|
||||
.applied => return l.fail("E7002", t.span, "unknown generic type", &.{}),
|
||||
.function_ref => return l.fail("E7006", t.span, "static functions are not runtime types", &.{}),
|
||||
.function_ref => return l.invalid(.invalid_static_function, t.span, "static functions are not runtime types", &.{}),
|
||||
.name => |n| {
|
||||
if (isIntName(n)) return .{ .int = n };
|
||||
if (std.mem.eql(u8, n, "bool")) return .bool;
|
||||
@@ -735,7 +744,7 @@ const Lowerer = struct {
|
||||
l.borrow_parents = .empty;
|
||||
// `trusted` is the boundary of runtime, std and toolchain modules; an
|
||||
// ordinary program cannot declare its own code trusted (spec 15 §8).
|
||||
if (f.trusted != null) return l.fail("E7006", f.span, "`trusted` is not available in .aji programs; unsafe operations need `effects [unsafe]`, which propagates to callers", &.{});
|
||||
if (f.trusted != null) return l.invalid(.trusted_unavailable, f.span, "`trusted` is not available in .aji programs; unsafe operations need `effects [unsafe]`, which propagates to callers", &.{});
|
||||
for (f.effects) |e| if (std.mem.eql(u8, e, "unsafe")) l.uses.insert(.unsafe);
|
||||
|
||||
var header: std.ArrayList(u8) = .empty;
|
||||
@@ -755,14 +764,14 @@ const Lowerer = struct {
|
||||
const tail = try l.blockBodyWant(f.body, sig.ret);
|
||||
if (l.cur != null) {
|
||||
if (tail != null and tail.?.ty == .void and sig.ret != .void) {
|
||||
return l.fail("E7006", f.body.tail.?.span, "the function can end without returning a value", &.{.{ .name = "function", .value = f.name }});
|
||||
return l.invalid(.missing_return, f.body.tail.?.span, "the function can end without returning a value", &.{.{ .name = "function", .value = f.name }});
|
||||
}
|
||||
if (tail) |v| {
|
||||
try l.checkTy(f.body.tail.?.span, v.ty, sig.ret);
|
||||
try l.term(try l.retText(v), f.body.tail.?.span);
|
||||
} else if (sig.ret == .void) {
|
||||
try l.term("ret", f.body.span);
|
||||
} else return l.fail("E7006", f.body.span, "the function can end without returning a value", &.{.{ .name = "function", .value = f.name }});
|
||||
} else return l.invalid(.missing_return, f.body.span, "the function can end without returning a value", &.{.{ .name = "function", .value = f.name }});
|
||||
}
|
||||
|
||||
var eff: std.ArrayList(u8) = .empty;
|
||||
@@ -979,14 +988,14 @@ const Lowerer = struct {
|
||||
l.pending_ends = .empty;
|
||||
}
|
||||
for (b.stmts, 0..) |s, i| {
|
||||
if (l.cur == null) return l.fail("E7006", s.span, "unreachable statement", &.{});
|
||||
if (l.cur == null) return l.invalid(.@"unreachable", s.span, "unreachable statement", &.{});
|
||||
l.rest.items[l.rest.items.len - 1].stmts = b.stmts[i + 1 ..];
|
||||
l.rest.items[l.rest.items.len - 1].current = s;
|
||||
try l.stmt(s);
|
||||
}
|
||||
var result: ?Value = null;
|
||||
if (b.tail) |t| {
|
||||
if (l.cur == null) return l.fail("E7006", t.span, "unreachable expression", &.{});
|
||||
if (l.cur == null) return l.invalid(.@"unreachable", t.span, "unreachable expression", &.{});
|
||||
l.rest.items[l.rest.items.len - 1].stmts = &.{};
|
||||
l.rest.items[l.rest.items.len - 1].current = null;
|
||||
l.rest.items[l.rest.items.len - 1].tail = null;
|
||||
@@ -995,7 +1004,7 @@ const Lowerer = struct {
|
||||
result = try l.returnValue(t.*);
|
||||
} else {
|
||||
result = try l.expr(t.*, want);
|
||||
if (result.?.ty.isBorrow()) return l.fail("E7006", t.span, "a block cannot yield a borrow", &.{});
|
||||
if (result.?.ty.isBorrow()) return l.invalid(.invalid_borrow_return, t.span, "a block cannot yield a borrow", &.{});
|
||||
}
|
||||
}
|
||||
// The borrows bound in this block end here (lexical scope, spec 15 §9).
|
||||
@@ -1035,7 +1044,7 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn returnValue(l: *Lowerer, e: ast.Expr) Error!Value {
|
||||
if (l.ret.isBorrow() and e.kind != .name) return l.fail("E7006", e.span, "return a named borrow; bind a returned view or place first", &.{});
|
||||
if (l.ret.isBorrow() and e.kind != .name) return l.invalid(.invalid_borrow_return, e.span, "return a named borrow; bind a returned view or place first", &.{});
|
||||
return l.consuming(e, l.ret);
|
||||
}
|
||||
|
||||
@@ -1056,13 +1065,13 @@ const Lowerer = struct {
|
||||
switch (s.kind) {
|
||||
.let => |x| {
|
||||
if (x.e.kind == .call) if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "struct.unpack") or std.mem.eql(u8, p, "array.unpack")) return l.unpackLet(s, x.names, x.e, x.tuple);
|
||||
if (x.tuple and x.names.len == 1) return l.fail("E7006", s.span, "a single parenthesized binding requires aggregate unpack", &.{});
|
||||
if (x.tuple and x.names.len == 1) return l.invalid(.invalid_binding, s.span, "a single parenthesized binding requires aggregate unpack", &.{});
|
||||
if (x.names.len != 1) {
|
||||
if (x.e.kind != .call or x.names.len != 2) return l.fail("E7006", s.span, "this multi-binding requires aggregate unpack", &.{});
|
||||
if (x.e.kind != .call or x.names.len != 2) return l.invalid(.invalid_binding, s.span, "this multi-binding requires aggregate unpack", &.{});
|
||||
if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "atomic.compare_exchange")) return l.compareExchangeLet(s, x.names, x.e);
|
||||
return l.splitLet(s, x.names, x.e);
|
||||
}
|
||||
if (x.e.kind == .borrow) return l.fail("E7006", x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
|
||||
if (x.e.kind == .borrow) return l.invalid(.invalid_binding, x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
|
||||
if (x.e.kind == .bytes) {
|
||||
const st = try l.materializeBytes(x.e.kind.bytes, x.e.span);
|
||||
try l.bindings.append(l.arena, .{ .name = x.names[0], .mutable = false, .value = st.name, .ty = st.ty, .kind = .storage });
|
||||
@@ -1085,7 +1094,7 @@ const Lowerer = struct {
|
||||
const v = try l.consuming(x.e, want);
|
||||
if (want) |w| try l.checkTy(x.e.span, v.ty, w);
|
||||
if (v.ty == .void or v.ty == .never) return l.fail("E7005", x.e.span, "this expression has no value", &.{});
|
||||
if (v.ty.isBorrow()) return l.fail("E7006", x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
|
||||
if (v.ty.isBorrow()) return l.invalid(.invalid_binding, x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
|
||||
// Linear values (owners, regions, resources, endpoints, tasks,
|
||||
// mutexes) are borrowed in place, never moved to frame storage.
|
||||
if (l.addressable.contains(x.names[0]) and !l.isLinear(v.ty)) {
|
||||
@@ -1096,7 +1105,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.@"var" => |x| {
|
||||
const ty = try l.typeOf(x.ty);
|
||||
if (ty.isBorrow()) return l.fail("E7006", s.span, "a `var` holds a value; bind a borrow with `ref` or `mut`", &.{});
|
||||
if (ty.isBorrow()) return l.invalid(.invalid_binding, s.span, "a `var` holds a value; bind a borrow with `ref` or `mut`", &.{});
|
||||
if (l.addressable.contains(x.name) and !l.isLinear(ty)) {
|
||||
const st = try l.storageFor(x.name, ty, s.span);
|
||||
if (x.init) |e| {
|
||||
@@ -1117,8 +1126,8 @@ const Lowerer = struct {
|
||||
.assign => |x| {
|
||||
if (x.place.kind != .name) return l.assignPlace(s.span, x.place, x.e);
|
||||
const b = l.lookup(x.place.kind.name) orelse return l.unknown(x.place.span, x.place.kind.name);
|
||||
if (b.kind == .borrow) return l.fail("E7006", x.place.span, "a borrow cannot be reassigned; write through it with `*r = …`, `r.f = …` or `r[i] = …`", &.{.{ .name = "name", .value = b.name }});
|
||||
if (!b.mutable) return l.fail("E7006", x.place.span, "assignment to an immutable binding; declare it with `var`", &.{.{ .name = "name", .value = b.name }});
|
||||
if (b.kind == .borrow) return l.invalid(.invalid_assignment, x.place.span, "a borrow cannot be reassigned; write through it with `*r = …`, `r.f = …` or `r[i] = …`", &.{.{ .name = "name", .value = b.name }});
|
||||
if (!b.mutable) return l.invalid(.invalid_assignment, x.place.span, "assignment to an immutable binding; declare it with `var`", &.{.{ .name = "name", .value = b.name }});
|
||||
const ty = b.ty;
|
||||
const kind = b.kind;
|
||||
const storage = b.value;
|
||||
@@ -1135,9 +1144,9 @@ const Lowerer = struct {
|
||||
// No shadowing of an active borrow by the same name in the same scope.
|
||||
const scope_start = l.scopeBindingStart();
|
||||
for (l.bindings.items[scope_start..]) |b| if (b.kind == .borrow and std.mem.eql(u8, b.name, x.name))
|
||||
return l.fail("E7006", s.span, "a borrow of this name is already active in this scope", &.{.{ .name = "name", .value = x.name }});
|
||||
return l.invalid(.invalid_binding, s.span, "a borrow of this name is already active in this scope", &.{.{ .name = "name", .value = x.name }});
|
||||
if (x.place.kind == .call) if (l.borrowReturnSig(x.place.kind.call.callee.*)) |sig| if (sig.ret.isBorrow()) {
|
||||
if (x.unchecked) return l.fail("E7006", s.span, "unchecked applies to element places, not calls", &.{});
|
||||
if (x.unchecked) return l.invalid(.invalid_place, s.span, "unchecked applies to element places, not calls", &.{});
|
||||
if (x.mutable != sig.ret.isMutBorrow()) return l.fail("E7005", s.span, "binding mode must match the returned borrow; use ref for shared or mut for exclusive", &.{});
|
||||
const call_expr = x.place.kind.call;
|
||||
const v = try l.call(x.place, call_expr.callee.*, call_expr.args, true);
|
||||
@@ -1175,7 +1184,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.trap => try l.term("trap", s.span),
|
||||
.@"break", .@"continue" => {
|
||||
if (l.loops.items.len == 0) return l.fail("E7006", s.span, "`break` or `continue` outside a loop", &.{});
|
||||
if (l.loops.items.len == 0) return l.invalid(.loop_control_outside_loop, s.span, "`break` or `continue` outside a loop", &.{});
|
||||
const lp = l.loops.items[l.loops.items.len - 1];
|
||||
const vars = if (s.kind == .@"break") lp.exit_vars else lp.header_vars;
|
||||
const target = if (s.kind == .@"break") lp.exit else lp.@"continue";
|
||||
@@ -1184,7 +1193,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.expr => |e| {
|
||||
const v = try l.expr(e, null);
|
||||
if (s.block_statement and v.ty != .void and v.ty != .never) return l.fail("E7006", s.span, "value-producing block statement: use `;` to discard the value or parentheses to continue the expression", &.{});
|
||||
if (s.block_statement and v.ty != .void and v.ty != .never) return l.invalid(.value_block_statement, s.span, "value-producing block statement: use `;` to discard the value or parentheses to continue the expression", &.{});
|
||||
},
|
||||
.@"while" => |x| try l.whileLoop(s.span, x.cond, x.body, null),
|
||||
.loop => |b| try l.whileLoop(s.span, null, b, null),
|
||||
@@ -1486,7 +1495,7 @@ const Lowerer = struct {
|
||||
|
||||
fn expr(l: *Lowerer, e: ast.Expr, want: ?Ty) Error!Value {
|
||||
switch (e.kind) {
|
||||
.specialize => return l.fail("E7006", e.span, "explicit type arguments require a generic function", &.{}),
|
||||
.specialize => return l.invalid(.invalid_generic, e.span, "explicit type arguments require a generic function", &.{}),
|
||||
.int => |x| {
|
||||
const ty: Ty = if (parse_mod.intSuffix(x.text)) |s| .{ .int = s } else want orelse
|
||||
return l.fail("E7007", e.span, "the type of this integer literal cannot be determined; add a suffix such as `0u64` or a type annotation", &.{});
|
||||
@@ -1507,20 +1516,20 @@ const Lowerer = struct {
|
||||
.deref => |x| {
|
||||
const r = try l.expr(x.*, null);
|
||||
const in = r.ty.inner() orelse return l.fail("E7005", e.span, "`*` reads through a borrow", &.{.{ .name = "found", .value = l.diagnosticTyText(r.ty) }});
|
||||
if (r.ty == .slice or r.ty == .slice_mut or in == .array) return l.fail("E7006", e.span, "read slice and array elements with `s[i]`, not `*`", &.{});
|
||||
if (r.ty == .slice or r.ty == .slice_mut or in == .array) return l.invalid(.invalid_expression, e.span, "read slice and array elements with `s[i]`, not `*`", &.{});
|
||||
// The verifier's code (spec 15 §1 rule 2, spec 04 §28).
|
||||
if (in == .atomic) return l.fail("E4202", e.span, "an atomic cell is read only by atomic operations: use `atomic.load(r, ordering)`", &.{});
|
||||
return l.def("v", in, try l.fmt("load %{s}", .{r.name}), e.span);
|
||||
},
|
||||
.index => |x| {
|
||||
if (x.hi != null) return l.fail("E7006", e.span, "a range `s[a..b]` is a borrow: bind it with `ref r = s[a..b];` or pass `ref s[a..b]`", &.{});
|
||||
if (x.hi != null) return l.invalid(.invalid_borrow, e.span, "a range `s[a..b]` is a borrow: bind it with `ref r = s[a..b];` or pass `ref s[a..b]`", &.{});
|
||||
const p = try l.placeBorrow(e, false, false, e.span);
|
||||
const v = try l.def("v", p.v.ty.inner().?, try l.fmt("load %{s}", .{p.v.name}), e.span);
|
||||
try l.endNow(p.ends, e.span);
|
||||
return v;
|
||||
},
|
||||
.array_list => |xs| {
|
||||
if (xs.len == 0 or xs.len > 64) return l.fail("E7006", e.span, "array literal requires 1..64 complete elements", &.{});
|
||||
if (xs.len == 0 or xs.len > 64) return l.invalid(.implementation_limit, e.span, "array literal requires 1..64 complete elements", &.{});
|
||||
var ty: ?Ty = if (want) |w| if (w == .array) w.array.elem else null else null;
|
||||
var names: std.ArrayList(u8) = .empty;
|
||||
for (xs, 0..) |x, i| {
|
||||
@@ -1535,12 +1544,12 @@ const Lowerer = struct {
|
||||
if (x.len == 0 or x.len > (@as(u64, 1) << 32)) return l.fail("E7005", e.span, "array length must be between 1 and 2^32", &.{});
|
||||
const want_elem: ?Ty = if (want) |w| (if (w == .array) w.array.elem else null) else null;
|
||||
const v = try l.expr(x.e.*, want_elem);
|
||||
if (v.ty.isBorrow()) return l.fail("E7006", x.e.span, "an array element cannot be a borrow", &.{});
|
||||
if (v.ty.isBorrow()) return l.invalid(.invalid_literal, x.e.span, "an array element cannot be a borrow", &.{});
|
||||
const at = try l.arrayOf(v.ty, x.len);
|
||||
return l.def("arr", at, try l.fmt("array.splat {s} %{s}", .{ l.tyText(at), v.name }), e.span);
|
||||
},
|
||||
.bytes => return l.fail("E7006", e.span, "a byte string is an array in storage: bind it with `let`/`ref`, or pass it where a slice is expected", &.{}),
|
||||
.borrow => return l.fail("E7006", e.span, "`ref`/`mut` here: borrows are bound with `ref name = place;` / `mut name = place;` or passed as arguments", &.{}),
|
||||
.bytes => return l.invalid(.invalid_literal, e.span, "a byte string is an array in storage: bind it with `let`/`ref`, or pass it where a slice is expected", &.{}),
|
||||
.borrow => return l.invalid(.invalid_borrow, e.span, "`ref`/`mut` here: borrows are bound with `ref name = place;` / `mut name = place;` or passed as arguments", &.{}),
|
||||
.neg => |x| {
|
||||
const ty = want orelse l.peekType(x.*) orelse return l.fail("E7007", e.span, "the type of this negation cannot be determined", &.{});
|
||||
if (ty != .int) return l.fail("E7005", e.span, "negation of a non-integer", &.{});
|
||||
@@ -1600,7 +1609,7 @@ const Lowerer = struct {
|
||||
.or_trap => |x| return l.orTrap(e, x.*),
|
||||
.move => |n| {
|
||||
const b = l.lookup(n) orelse return l.unknown(e.span, n);
|
||||
if (!l.isLinear(b.ty) and !(e.generic_transfer and b.ty != .void and b.ty != .never and !b.ty.isBorrow() and b.ty != .task and b.ty != .scope and b.ty != .guard and b.ty != .shield)) return l.fail("E7006", e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
|
||||
if (!l.isLinear(b.ty) and !(e.generic_transfer and b.ty != .void and b.ty != .never and !b.ty.isBorrow() and b.ty != .task and b.ty != .scope and b.ty != .guard and b.ty != .shield)) return l.invalid(if (b.ty.isBorrow()) .move_borrow else if (b.ty == .void or b.ty == .never) .invalid_move else .move_copy, e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
|
||||
if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span);
|
||||
return .{ .name = b.value, .ty = b.ty };
|
||||
},
|
||||
@@ -1686,10 +1695,10 @@ const Lowerer = struct {
|
||||
const b = try l.bufferBorrow(args[0], false);
|
||||
return l.def("size", .{ .int = "usize" }, try l.fmt("{s} %{s}", .{ p, b.name }), e.span);
|
||||
}
|
||||
if (std.mem.eql(u8, p, "buffer.view")) return l.fail("E7006", e.span, "bind a buffer view with ref/mut name = buffer.view(borrow)", &.{});
|
||||
if (std.mem.eql(u8, p, "buffer.view")) return l.invalid(.invalid_borrow, e.span, "bind a buffer view with ref/mut name = buffer.view(borrow)", &.{});
|
||||
}
|
||||
if (pathOf(callee)) |path| if (std.mem.eql(u8, path, "value.swap")) {
|
||||
if (args.len != 2 or args[0].kind != .borrow or !args[0].kind.borrow.mutable) return l.fail("E7006", e.span, "value.swap requires mut place and replacement", &.{});
|
||||
if (args.len != 2 or args[0].kind != .borrow or !args[0].kind.borrow.mutable) return l.invalid(.invalid_operation, e.span, "value.swap requires mut place and replacement", &.{});
|
||||
const b = args[0].kind.borrow;
|
||||
const p = try l.placeBorrow(b.place.*, true, false, e.span);
|
||||
if (p.v.ty != .mut) return l.fail("E7005", e.span, "value.swap requires mut<T>", &.{});
|
||||
@@ -1709,7 +1718,7 @@ const Lowerer = struct {
|
||||
}
|
||||
if (pathOf(callee)) |p| {
|
||||
if (std.mem.eql(u8, p, "slice.copy")) return l.sliceCopy(e, args);
|
||||
if (l.fallible(p) != null) return l.fail("E7006", e.span, "a fallible operation is handled with `match` over its outcomes or with `or trap`", &.{.{ .name = "operation", .value = p }});
|
||||
if (l.fallible(p) != null) return l.invalid(.invalid_operation, e.span, "a fallible operation is handled with `match` over its outcomes or with `or trap`", &.{.{ .name = "operation", .value = p }});
|
||||
if (std.mem.eql(u8, p, "own.take")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`own.take` takes one owner", &.{});
|
||||
const o = try l.consuming(args[0], null);
|
||||
@@ -1749,7 +1758,7 @@ const Lowerer = struct {
|
||||
return l.notYet(callee.span, "this built-in operation (later layers)");
|
||||
}
|
||||
if (callee.kind == .field and std.mem.eql(u8, callee.kind.field.name, "split_at"))
|
||||
return l.fail("E7006", e.span, "`split_at` yields two borrows: `let (left, right) = s.split_at(k);`", &.{});
|
||||
return l.invalid(.invalid_unpack, e.span, "`split_at` yields two borrows: `let (left, right) = s.split_at(k);`", &.{});
|
||||
if (callee.kind != .name) return l.notYet(callee.span, "built-in operations (later layers)");
|
||||
const name = callee.kind.name;
|
||||
if (std.mem.eql(u8, name, "assert")) {
|
||||
@@ -1769,12 +1778,12 @@ const Lowerer = struct {
|
||||
}
|
||||
const sig = l.functionSig(name, callee.span) orelse return l.unknown(callee.span, name);
|
||||
if (args.len != sig.params.len) return l.fail("E7005", e.span, "wrong number of arguments", &.{ .{ .name = "function", .value = sig.display_name }, .{ .name = "expected", .value = try l.fmt("{d}", .{sig.params.len}) }, .{ .name = "found", .value = try l.fmt("{d}", .{args.len}) } });
|
||||
if (sig.ret.isBorrow() and !borrow_allowed) return l.fail("E7006", e.span, "bind a returned borrow with ref/mut name = call(...) first", &.{});
|
||||
if (sig.ret.isBorrow() and !borrow_allowed) return l.invalid(.invalid_borrow_return, e.span, "bind a returned borrow with ref/mut name = call(...) first", &.{});
|
||||
if (sig.from) |origin| {
|
||||
const a = args[origin];
|
||||
if (a.kind != .name) return l.fail("E7006", a.span, "the from argument must be a named borrow; bind it first", &.{});
|
||||
if (a.kind != .name) return l.invalid(.invalid_borrow_return, a.span, "the from argument must be a named borrow; bind it first", &.{});
|
||||
const b = l.lookup(a.kind.name) orelse return l.unknown(a.span, a.kind.name);
|
||||
if (b.kind != .borrow) return l.fail("E7006", a.span, "the from argument must be a named borrow; bind it first", &.{});
|
||||
if (b.kind != .borrow) return l.invalid(.invalid_borrow_return, a.span, "the from argument must be a named borrow; bind it first", &.{});
|
||||
}
|
||||
var parent: ?[]const u8 = null;
|
||||
var arg_names: std.ArrayList(u8) = .empty;
|
||||
@@ -1810,7 +1819,7 @@ const Lowerer = struct {
|
||||
if (d.kind != .@"struct") return l.fail("E7005", e.span, "`Name { … }` constructs a struct; this is an enum (use `Enum::Variant`)", &.{.{ .name = "type", .value = d.display_name orelse name }});
|
||||
const fields = d.kind.@"struct";
|
||||
for (inits, 0..) |fi, i| {
|
||||
for (inits[0..i]) |prev| if (std.mem.eql(u8, prev.name, fi.name)) return l.fail("E7006", fi.span, "field given twice", &.{.{ .name = "field", .value = fi.name }});
|
||||
for (inits[0..i]) |prev| if (std.mem.eql(u8, prev.name, fi.name)) return l.invalid(.invalid_literal, fi.span, "field given twice", &.{.{ .name = "field", .value = fi.name }});
|
||||
const known = for (fields) |f| {
|
||||
if (std.mem.eql(u8, f.name, fi.name)) break true;
|
||||
} else false;
|
||||
@@ -1820,7 +1829,7 @@ const Lowerer = struct {
|
||||
for (fields, 0..) |f, i| {
|
||||
const fi = for (inits) |x| {
|
||||
if (std.mem.eql(u8, x.name, f.name)) break x;
|
||||
} else return l.fail("E7006", e.span, "missing field", &.{ .{ .name = "type", .value = d.display_name orelse name }, .{ .name = "field", .value = f.name } });
|
||||
} else return l.invalid(.invalid_literal, e.span, "missing field", &.{ .{ .name = "type", .value = d.display_name orelse name }, .{ .name = "field", .value = f.name } });
|
||||
const ft = try l.typeOf(f.ty);
|
||||
const v = try l.consuming(fi.e, ft);
|
||||
try l.checkTy(fi.e.span, v.ty, ft);
|
||||
@@ -1932,7 +1941,7 @@ const Lowerer = struct {
|
||||
try head.appendSlice(l.arena, try l.fmt("{s} {s} %{s}", .{ if (l.isLinear(st)) "enum.take" else "enum.switch", l.tyText(st), sv.name }));
|
||||
for (m.arms, blocks, 0..) |arm, *blk, i| {
|
||||
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier == null)
|
||||
return l.fail("E7006", arm.pattern.span, "an arm of a `match` over an enum names one variant as `Enum::Variant`; `_` is not allowed", &.{.{ .name = "enum", .value = d.display_name orelse d.name }});
|
||||
return l.invalid(.invalid_match, arm.pattern.span, "an arm of a `match` over an enum names one variant as `Enum::Variant`; `_` is not allowed", &.{.{ .name = "enum", .value = d.display_name orelse d.name }});
|
||||
const c = arm.pattern.kind.ctor;
|
||||
const pattern_type = l.enumType(c.qualifier.?, arm.pattern.span);
|
||||
if (std.mem.indexOfScalar(u8, c.qualifier.?, '.') != null and pattern_type == null) return l.unknown(arm.pattern.span, c.qualifier.?);
|
||||
@@ -1940,7 +1949,7 @@ const Lowerer = struct {
|
||||
const vi = for (vs, 0..) |v, k| {
|
||||
if (std.mem.eql(u8, v.name, c.name)) break k;
|
||||
} else return l.fail("E7002", arm.pattern.span, "no such variant", &.{ .{ .name = "enum", .value = d.display_name orelse d.name }, .{ .name = "variant", .value = c.name } });
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.fail("E7006", arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
|
||||
if (c.binds.len != vs[vi].payload.len) return l.fail("E7005", arm.pattern.span, "the pattern binds a different number of payload values", &.{ .{ .name = "variant", .value = c.name }, .{ .name = "expected", .value = try l.fmt("{d}", .{vs[vi].payload.len}) } });
|
||||
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}));
|
||||
for (vs[vi].payload, c.binds) |pt, bn| {
|
||||
@@ -1982,7 +1991,7 @@ const Lowerer = struct {
|
||||
for (m.arms, blocks, 0..) |arm, *blk, i| {
|
||||
switch (arm.pattern.kind) {
|
||||
.wildcard => {
|
||||
if (default != null) return l.fail("E7006", arm.pattern.span, "`_` given twice", &.{});
|
||||
if (default != null) return l.invalid(.invalid_match, arm.pattern.span, "`_` given twice", &.{});
|
||||
default = i;
|
||||
blk.* = try l.newBlock("case_default");
|
||||
},
|
||||
@@ -1995,7 +2004,7 @@ const Lowerer = struct {
|
||||
const limit: u128 = if (signed) (@as(u128, 1) << @intCast(width - 1)) - @as(u128, if (negative) 0 else 1) else if (width == 128) std.math.maxInt(u128) else (@as(u128, 1) << @intCast(width)) - 1;
|
||||
if ((negative and !signed) or mag > limit) return l.fail("E7005", arm.pattern.span, "integer pattern out of range for its scrutinee type", &.{ .{ .name = "type", .value = st.int }, .{ .name = "literal", .value = text } });
|
||||
const bits: u128 = if (neg) (0 -% mag) else mag;
|
||||
for (seen.items) |prev| if (prev == bits) return l.fail("E7006", arm.pattern.span, "integer matched twice", &.{.{ .name = "value", .value = text }});
|
||||
for (seen.items) |prev| if (prev == bits) return l.invalid(.invalid_match, arm.pattern.span, "integer matched twice", &.{.{ .name = "value", .value = text }});
|
||||
try seen.append(l.arena, bits);
|
||||
blk.* = try l.newBlock("case");
|
||||
// AIR uses canonical decimal literals without source
|
||||
@@ -2031,11 +2040,11 @@ const Lowerer = struct {
|
||||
try head.appendSlice(l.arena, try l.fmt("enum.switch {s} %{s}", .{ name, sv.name }));
|
||||
for (m.arms, blocks, 0..) |arm, *blk, i| {
|
||||
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier == null or !std.mem.eql(u8, arm.pattern.kind.ctor.qualifier.?, name))
|
||||
return l.fail("E7006", arm.pattern.span, try l.fmt("an arm names one variant as `{s}::variant`; `_` is not allowed", .{name}), &.{});
|
||||
return l.invalid(.invalid_match, arm.pattern.span, try l.fmt("an arm names one variant as `{s}::variant`; `_` is not allowed", .{name}), &.{});
|
||||
const c = arm.pattern.kind.ctor;
|
||||
if (b.variantIndex(c.name) == null) return l.fail("E7002", arm.pattern.span, "no such variant", &.{ .{ .name = "enum", .value = name }, .{ .name = "variant", .value = c.name } });
|
||||
if (c.binds.len != 0) return l.fail("E7005", arm.pattern.span, "built-in error variants have no payload", &.{});
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.fail("E7006", arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "variant matched twice", &.{.{ .name = "variant", .value = c.name }});
|
||||
blk.* = try l.newBlock(try l.fmt("case_{s}", .{c.name}));
|
||||
try head.appendSlice(l.arena, try l.fmt(", {s} -> @{s}", .{ c.name, l.blocks.items[blk.*].name }));
|
||||
}
|
||||
@@ -2122,13 +2131,13 @@ const Lowerer = struct {
|
||||
const z = try l.zeroValue(a.elem, base, span);
|
||||
break :blk l.def(base, ty, try l.fmt("array.splat {s} %{s}", .{ l.tyText(ty), z.name }), span);
|
||||
},
|
||||
else => l.fail("E7006", span, "`zero` needs a zeroable type (integers, booleans, arrays of them)", &.{}),
|
||||
else => l.invalid(.zero_not_zeroable, span, "`zero` needs a zeroable type (integers, booleans, arrays of them)", &.{}),
|
||||
};
|
||||
}
|
||||
|
||||
/// A byte string in frame storage: `[u8; N]`, one store per byte (no heap).
|
||||
fn materializeBytes(l: *Lowerer, bytes: []const u8, span: Span) Error!Value {
|
||||
if (bytes.len == 0) return l.fail("E7006", span, "an empty byte string has no array type", &.{});
|
||||
if (bytes.len == 0) return l.invalid(.invalid_literal, span, "an empty byte string has no array type", &.{});
|
||||
const ty = try l.arrayOf(.{ .int = "u8" }, bytes.len);
|
||||
const st = try l.storageFor("bytes", ty, span);
|
||||
// Everything from here to the final `borrow.end` is the constant's
|
||||
@@ -2189,7 +2198,7 @@ const Lowerer = struct {
|
||||
/// the verifier's (`E6001`).
|
||||
fn uncheckedPlace(l: *Lowerer, place: ast.Expr, mutable: bool, unchecked: bool, span: Span) Error!Placed {
|
||||
if (!unchecked) return l.placeBorrow(place, mutable, true, span);
|
||||
if (place.kind != .index or place.kind.index.hi != null) return l.fail("E7006", place.span, "`unchecked` applies to one element `s[i]`", &.{});
|
||||
if (place.kind != .index or place.kind.index.hi != null) return l.invalid(.invalid_place, place.span, "`unchecked` applies to one element `s[i]`", &.{});
|
||||
l.unchecked = true;
|
||||
defer l.unchecked = false;
|
||||
return l.placeBorrow(place, mutable, true, span);
|
||||
@@ -2254,7 +2263,7 @@ const Lowerer = struct {
|
||||
.atomic => if (mutable) return l.fail("E7005", place.span, "an atomic cell is borrowed with `ref`: every atomic operation goes through a shared borrow", &.{.{ .name = "name", .value = n }}) else try l.wrap(.ref, b.ty),
|
||||
else => null,
|
||||
};
|
||||
const t = bt orelse return l.fail("E7006", place.span, "this value is not addressable here; borrow a variable, a field or an element", &.{.{ .name = "name", .value = n }});
|
||||
const t = bt orelse return l.invalid(.invalid_place, place.span, "this value is not addressable here; borrow a variable, a field or an element", &.{.{ .name = "name", .value = n }});
|
||||
const v = try l.def(n, t, try l.fmt("{s} %{s}", .{ op, b.value }), span);
|
||||
return .{ .v = v, .ends = try l.arena.dupe([]const u8, &.{v.name}) };
|
||||
},
|
||||
@@ -2318,7 +2327,7 @@ const Lowerer = struct {
|
||||
const v = try l.defBorrow("e", if (bt.isMutBorrow()) try l.wrap(.mut, elem) else try l.wrap(.ref, elem), try l.fmt("{s} %{s}, %{s}", .{ index_op, base.v.name, i.name }), base.v.name, span);
|
||||
return .{ .v = v, .ends = try l.concat(base.ends, v.name) };
|
||||
},
|
||||
else => return l.fail("E7006", place.span, "not a place: borrow a variable, a field `x.f`, an element `s[i]` or a range `s[a..b]`", &.{}),
|
||||
else => return l.invalid(.invalid_place, place.span, "not a place: borrow a variable, a field `x.f`, an element `s[i]` or a range `s[a..b]`", &.{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2333,15 +2342,15 @@ const Lowerer = struct {
|
||||
fn assignPlace(l: *Lowerer, span: Span, place: ast.Expr, value: ast.Expr) Error!void {
|
||||
switch (place.kind) {
|
||||
.deref, .field, .index => {},
|
||||
else => return l.fail("E7006", place.span, "cannot assign to this expression", &.{}),
|
||||
else => return l.invalid(.invalid_assignment, place.span, "cannot assign to this expression", &.{}),
|
||||
}
|
||||
if (place.kind == .index and place.kind.index.hi != null) return l.fail("E7006", place.span, "assign elements one by one, or use `slice.copy(mut dst, ref src)`", &.{});
|
||||
if (place.kind == .index and place.kind.index.hi != null) return l.invalid(.invalid_assignment, place.span, "assign elements one by one, or use `slice.copy(mut dst, ref src)`", &.{});
|
||||
// The value is evaluated first, then the place is borrowed for the
|
||||
// store (spec 15 §4), so `s[i] = s[i] + 1` reads before it writes.
|
||||
const v = try l.expr(value, l.placeType(place));
|
||||
const p = try l.placeBorrow(place, true, false, span);
|
||||
const in = p.v.ty.inner() orelse return l.fail("E7005", place.span, "assignment needs a mutable place", &.{});
|
||||
if (in == .array and place.kind == .deref) return l.fail("E7006", place.span, "write array elements with `s[i] = …`", &.{});
|
||||
if (in == .array and place.kind == .deref) return l.invalid(.invalid_assignment, place.span, "write array elements with `s[i] = …`", &.{});
|
||||
try l.checkTy(value.span, v.ty, in);
|
||||
try l.emit(try l.fmt("store %{s}, %{s}", .{ p.v.name, v.name }), span);
|
||||
try l.endNow(p.ends, span);
|
||||
@@ -2373,20 +2382,20 @@ const Lowerer = struct {
|
||||
/// `let (left, right) = s.split_at(k);`: two disjoint borrows of `s`,
|
||||
/// bound for the rest of the block (spec 15 §5).
|
||||
fn unpackLet(l: *Lowerer, s: ast.Stmt, names: []const []const u8, e: ast.Expr, tuple: bool) Error!void {
|
||||
if (!tuple or s.kind.let.ty != null or e.kind.call.args.len != 1) return l.fail("E7006", s.span, "unpack requires a parenthesized complete binding and one argument", &.{});
|
||||
if (!tuple or s.kind.let.ty != null or e.kind.call.args.len != 1) return l.invalid(.invalid_unpack, s.span, "unpack requires a parenthesized complete binding and one argument", &.{});
|
||||
const v = try l.consuming(e.kind.call.args[0], null);
|
||||
const operation = pathOf(e.kind.call.callee.*).?;
|
||||
var components: std.ArrayList(Ty) = .empty;
|
||||
if (std.mem.eql(u8, operation, "struct.unpack") and v.ty == .named and l.m.types[v.ty.named].kind == .@"struct") {
|
||||
for (l.m.types[v.ty.named].kind.@"struct") |f| try components.append(l.arena, try l.typeOf(f.ty));
|
||||
} else if (std.mem.eql(u8, operation, "array.unpack") and v.ty == .array) {
|
||||
if (v.ty.array.len > 64) return l.fail("E7006", s.span, "unpack exceeds 64 block parameters", &.{});
|
||||
if (v.ty.array.len > 64) return l.invalid(.implementation_limit, s.span, "unpack exceeds 64 block parameters", &.{});
|
||||
for (0..@intCast(v.ty.array.len)) |_| try components.append(l.arena, v.ty.array.elem);
|
||||
} else return l.fail("E7005", e.span, "unpack needs the corresponding struct or array", &.{});
|
||||
if (names.len != components.items.len or names.len > 64) return l.fail("E7005", s.span, "unpack must bind every component", &.{});
|
||||
const next = try l.newBlock("unpacked");
|
||||
for (names, components.items, 0..) |n, t, i| {
|
||||
for (names[0..i]) |previous| if (!std.mem.eql(u8, n, "_") and std.mem.eql(u8, n, previous)) return l.fail("E7006", s.span, "duplicate unpack binding", &.{});
|
||||
for (names[0..i]) |previous| if (!std.mem.eql(u8, n, "_") and std.mem.eql(u8, n, previous)) return l.invalid(.invalid_unpack, s.span, "duplicate unpack binding", &.{});
|
||||
const pn = try l.freshName(if (std.mem.eql(u8, n, "_")) "unused" else n);
|
||||
try l.blocks.items[next].params.append(l.arena, .{ .name = pn, .ty = t });
|
||||
if (!std.mem.eql(u8, n, "_")) try l.bindings.append(l.arena, .{ .name = n, .value = pn, .ty = t, .mutable = false });
|
||||
@@ -2398,7 +2407,7 @@ const Lowerer = struct {
|
||||
|
||||
fn splitLet(l: *Lowerer, s: ast.Stmt, names: []const []const u8, e: ast.Expr) Error!void {
|
||||
if (e.kind != .call or e.kind.call.callee.kind != .field or !std.mem.eql(u8, e.kind.call.callee.kind.field.name, "split_at") or e.kind.call.args.len != 1)
|
||||
return l.fail("E7006", e.span, "`let (a, b) = …` is only for `s.split_at(k)`", &.{});
|
||||
return l.invalid(.invalid_unpack, e.span, "`let (a, b) = …` is only for `s.split_at(k)`", &.{});
|
||||
const base = e.kind.call.callee.kind.field.e.*;
|
||||
const rb = l.rootBinding(base) orelse return l.fail("E7002", base.span, "unknown name", &.{});
|
||||
const mutable = rb.ty.isMutBorrow() or rb.kind == .storage and rb.mutable;
|
||||
@@ -2549,7 +2558,7 @@ const Lowerer = struct {
|
||||
fn ordering(l: *Lowerer, a: ast.Expr) Error![]const u8 {
|
||||
const words = [_][]const u8{ "relaxed", "acquire", "release", "acq_rel", "seq_cst" };
|
||||
if (a.kind == .name) for (words) |w| if (std.mem.eql(u8, a.kind.name, w)) return w;
|
||||
return l.fail("E7006", a.span, "an ordering is one of the words relaxed, acquire, release, acq_rel, seq_cst, written in place", &.{});
|
||||
return l.invalid(.invalid_operation, a.span, "an ordering is one of the words relaxed, acquire, release, acq_rel, seq_cst, written in place", &.{});
|
||||
}
|
||||
|
||||
/// The cell operand of an atomic operation: a `ref<atomic<T>>` binding or
|
||||
@@ -2590,7 +2599,7 @@ const Lowerer = struct {
|
||||
return .{ .name = "", .ty = .void };
|
||||
}
|
||||
if (eq(u8, p, "atomic.compare_exchange"))
|
||||
return l.fail("E7006", e.span, "`atomic.compare_exchange` has two results: `let (old, exchanged) = atomic.compare_exchange(c, expected, desired, success, failure);`", &.{});
|
||||
return l.invalid(.invalid_unpack, e.span, "`atomic.compare_exchange` has two results: `let (old, exchanged) = atomic.compare_exchange(c, expected, desired, success, failure);`", &.{});
|
||||
var temps: std.ArrayList([]const u8) = .empty;
|
||||
const want: usize = if (eq(u8, p, "atomic.load")) 2 else 3;
|
||||
if (args.len != want) return l.fail("E7005", e.span, "wrong number of operands", &.{ .{ .name = "operation", .value = p }, .{ .name = "expected", .value = try l.fmt("{d}", .{want}) } });
|
||||
@@ -2647,7 +2656,7 @@ const Lowerer = struct {
|
||||
/// endpoint by name, borrowed for the operation, or a borrow binding of it.
|
||||
fn endpointOperand(l: *Lowerer, a: ast.Expr, tag: std.meta.Tag(Ty), mutable: bool, temps: *std.ArrayList([]const u8)) Error!Value {
|
||||
const what = if (tag == .sender) "a sender" else "a receiver";
|
||||
if (a.kind == .borrow) return l.fail("E7006", a.span, "an endpoint operand is written by name; the operation borrows it", &.{});
|
||||
if (a.kind == .borrow) return l.invalid(.invalid_borrow, a.span, "an endpoint operand is written by name; the operation borrows it", &.{});
|
||||
const b = try l.bindingOf(a, what);
|
||||
if (std.meta.activeTag(b.ty) == tag) {
|
||||
const bt = if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty);
|
||||
@@ -2674,14 +2683,14 @@ const Lowerer = struct {
|
||||
const scope_name = sb.value;
|
||||
const fname = try expressionPath(l.arena, args[1]) orelse return l.fail("E7005", args[1].span, "the function to spawn is named", &.{});
|
||||
const sig = l.functionSig(fname, args[1].span) orelse return l.unknown(args[1].span, fname);
|
||||
if (sig.ret.isBorrow()) return l.fail("E7006", e.span, "a spawned function cannot return a borrow (reserved: task_borrow_returns)", &.{.{ .name = "function", .value = sig.display_name }});
|
||||
if (sig.ret.isBorrow()) return l.invalid(.invalid_borrow_return, e.span, "a spawned function cannot return a borrow (reserved: task_borrow_returns)", &.{.{ .name = "function", .value = sig.display_name }});
|
||||
const fargs = args[2..];
|
||||
if (fargs.len != sig.params.len) return l.fail("E7005", e.span, "wrong number of arguments", &.{ .{ .name = "function", .value = sig.display_name }, .{ .name = "expected", .value = try l.fmt("{d}", .{sig.params.len}) }, .{ .name = "found", .value = try l.fmt("{d}", .{fargs.len}) } });
|
||||
var names: std.ArrayList(u8) = .empty;
|
||||
for (fargs, sig.params, 0..) |a, pt, i| {
|
||||
// A borrow is lent to the task until its join, so it is bound
|
||||
// by the program, not made for the call (spec 15 §7).
|
||||
if (a.kind == .borrow or a.kind == .bytes) return l.fail("E7006", a.span, "a borrow lent to a task stays lent until the join: bind it first (`mut part = …;`) and pass the name", &.{});
|
||||
if (a.kind == .borrow or a.kind == .bytes) return l.invalid(.invalid_borrow, a.span, "a borrow lent to a task stays lent until the join: bind it first (`mut part = …;`) and pass the name", &.{});
|
||||
const v = try l.consuming(a, pt);
|
||||
try l.checkTy(a.span, v.ty, pt);
|
||||
try names.appendSlice(l.arena, try l.fmt("{s}%{s}", .{ if (i == 0) "" else ", ", v.name }));
|
||||
@@ -2753,7 +2762,7 @@ const Lowerer = struct {
|
||||
return l.fail("E7005", args[0].span, "`arc.clone` takes an `arc<T>`", &.{.{ .name = "found", .value = l.diagnosticTyText(b.ty) }});
|
||||
}
|
||||
if (eq(p, "island.at") or eq(p, "frozen.at") or eq(p, "arc.at"))
|
||||
return l.fail("E7006", e.span, "a graph access is a borrow: bind it with `ref x = island.at(g, r);` or `mut x = island.at(g, r);`", &.{});
|
||||
return l.invalid(.invalid_borrow, e.span, "a graph access is a borrow: bind it with `ref x = island.at(g, r);` or `mut x = island.at(g, r);`", &.{});
|
||||
if (eq(p, "mutex.make")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`mutex.make` takes the initial value", &.{});
|
||||
const v = try l.consuming(args[0], null);
|
||||
@@ -2828,7 +2837,7 @@ const Lowerer = struct {
|
||||
/// appearance; every edge binds its own value (a received `T`) and then
|
||||
/// the values of `V` not sent on it.
|
||||
fn selectStmt(l: *Lowerer, s: ast.Stmt, sel: ast.Select) Error!void {
|
||||
if (sel.arms.len == 0) return l.fail("E7006", s.span, "a `select` has at least one `recv` or `send` arm", &.{});
|
||||
if (sel.arms.len == 0) return l.invalid(.invalid_match, s.span, "a `select` has at least one `recv` or `send` arm", &.{});
|
||||
const Sent = struct { key: ?[]const u8, v: Value };
|
||||
var values: std.ArrayList(Sent) = .empty;
|
||||
const arm_value = try l.arena.alloc(?usize, sel.arms.len);
|
||||
@@ -2892,10 +2901,10 @@ const Lowerer = struct {
|
||||
// Every outcome of every arm is matched exactly once.
|
||||
for (sel.arms) |a| {
|
||||
for (a.arms, 0..) |arm, k| {
|
||||
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier != null) return l.fail("E7006", arm.pattern.span, "a `select` arm names its outcomes: `ok(…)` and `closed(…)`", &.{});
|
||||
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier != null) return l.invalid(.invalid_match, arm.pattern.span, "a `select` arm names its outcomes: `ok(…)` and `closed(…)`", &.{});
|
||||
const c = arm.pattern.kind.ctor;
|
||||
if (!std.mem.eql(u8, c.name, "ok") and !std.mem.eql(u8, c.name, "closed")) return l.fail("E7002", arm.pattern.span, "a `select` arm has the outcomes `ok` and `closed`; `cancelled` is the `select`'s own arm", &.{.{ .name = "outcome", .value = c.name }});
|
||||
for (a.arms[0..k]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.fail("E7006", arm.pattern.span, "outcome matched twice", &.{.{ .name = "outcome", .value = c.name }});
|
||||
for (a.arms[0..k]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "outcome matched twice", &.{.{ .name = "outcome", .value = c.name }});
|
||||
}
|
||||
for ([_][]const u8{ "ok", "closed" }) |role| {
|
||||
const covered = for (a.arms) |arm| {
|
||||
@@ -2987,7 +2996,7 @@ const Lowerer = struct {
|
||||
/// The named borrow binding of a graph owner that `.root`, `.at` and
|
||||
/// `island.alloc` take (spec 15 §8: container borrows are explicit).
|
||||
fn graphBorrow(l: *Lowerer, a: ast.Expr, owner: []const u8, mutable: bool) Error!Value {
|
||||
if (a.kind == .borrow) return l.fail("E7006", a.span, "the graph's borrow is bound first (`mut g = isl;`), so it outlives the borrows taken from it", &.{});
|
||||
if (a.kind == .borrow) return l.invalid(.invalid_borrow, a.span, "the graph's borrow is bound first (`mut g = isl;`), so it outlives the borrows taken from it", &.{});
|
||||
if (a.kind != .name) return l.fail("E7005", a.span, "the graph's borrow binding is expected here, by name", &.{});
|
||||
const b = l.lookup(a.kind.name) orelse return l.unknown(a.span, a.kind.name);
|
||||
const tag = std.meta.stringToEnum(std.meta.Tag(Ty), owner).?;
|
||||
@@ -3082,7 +3091,7 @@ const Lowerer = struct {
|
||||
/// (borrowed `mut` for the operation) or a `mut` borrow binding of one.
|
||||
fn resourceOperand(l: *Lowerer, a: ast.Expr, rs: []const authority.Resource, temps: *std.ArrayList([]const u8)) Error!Value {
|
||||
const expected = try l.resourceList(rs);
|
||||
if (a.kind == .borrow) return l.fail("E7006", a.span, "a resource operand is written by name; the operation borrows it", &.{.{ .name = "expected", .value = expected }});
|
||||
if (a.kind == .borrow) return l.invalid(.invalid_borrow, a.span, "a resource operand is written by name; the operation borrows it", &.{.{ .name = "expected", .value = expected }});
|
||||
if (a.kind != .name) return l.fail("E7005", a.span, "a resource is expected here, by name", &.{.{ .name = "expected", .value = expected }});
|
||||
const b = l.lookup(a.kind.name) orelse return l.unknown(a.span, a.kind.name);
|
||||
const r: ?authority.Resource = switch (b.ty) {
|
||||
@@ -3122,8 +3131,8 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn lowerFallible(l: *Lowerer, e: ast.Expr) Error!Fallible {
|
||||
if (e.kind != .call) return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
const path = pathOf(e.kind.call.callee.*) orelse return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
if (e.kind != .call) return l.invalid(.invalid_match, e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
const path = pathOf(e.kind.call.callee.*) orelse return l.invalid(.invalid_match, e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
const args = e.kind.call.args;
|
||||
const want = struct {
|
||||
fn n(lw: *Lowerer, sp: Span, as: []const ast.Expr, k: usize, op: []const u8) Error!void {
|
||||
@@ -3206,7 +3215,7 @@ const Lowerer = struct {
|
||||
}
|
||||
if (authority.intrinsicIndex(path)) |ix| {
|
||||
const x = authority.intrinsics[ix];
|
||||
if (!x.fallible) return l.fail("E7006", e.span, "this intrinsic cannot fail: call it plainly", &.{.{ .name = "operation", .value = x.name }});
|
||||
if (!x.fallible) return l.invalid(.invalid_operation, e.span, "this intrinsic cannot fail: call it plainly", &.{.{ .name = "operation", .value = x.name }});
|
||||
var temps: std.ArrayList([]const u8) = .empty;
|
||||
const ops = try l.intrinsicOperands(e, x, args, &temps);
|
||||
l.uses.insert(.capabilities);
|
||||
@@ -3293,7 +3302,7 @@ const Lowerer = struct {
|
||||
try l.checkTy(args[1].span, n.ty, usize_ty);
|
||||
return .{ .text = try l.fmt("cap.narrow.budget %{s}, %{s}", .{ c.name, n.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{c.ty} }, .{ .role = "err", .params = &.{.{ .builtin = .cap_error }} } }) };
|
||||
}
|
||||
return l.fail("E7006", e.span, "not a fallible operation", &.{.{ .name = "operation", .value = path }});
|
||||
return l.invalid(.invalid_operation, e.span, "not a fallible operation", &.{.{ .name = "operation", .value = path }});
|
||||
}
|
||||
|
||||
fn outs(l: *Lowerer, o: []const Outcome) Error![]const Outcome {
|
||||
@@ -3332,12 +3341,12 @@ const Lowerer = struct {
|
||||
const f = try l.lowerFallible(m.scrutinee);
|
||||
for (m.arms, 0..) |arm, i| {
|
||||
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier != null)
|
||||
return l.fail("E7006", arm.pattern.span, "every arm of an outcome match names an outcome of the operation (`ok(…)`, `err(e)`, …)", &.{});
|
||||
return l.invalid(.invalid_match, arm.pattern.span, "every arm of an outcome match names an outcome of the operation (`ok(…)`, `err(e)`, …)", &.{});
|
||||
const c = arm.pattern.kind.ctor;
|
||||
const o = for (f.outcomes) |o| {
|
||||
if (std.mem.eql(u8, o.role, c.name)) break o;
|
||||
} else return l.fail("E7002", arm.pattern.span, "this operation has no such outcome", &.{.{ .name = "outcome", .value = c.name }});
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.fail("E7006", arm.pattern.span, "outcome matched twice", &.{.{ .name = "outcome", .value = c.name }});
|
||||
for (m.arms[0..i]) |prev| if (std.mem.eql(u8, prev.pattern.kind.ctor.name, c.name)) return l.invalid(.invalid_match, arm.pattern.span, "outcome matched twice", &.{.{ .name = "outcome", .value = c.name }});
|
||||
if (c.binds.len != o.params.len) return l.fail("E7005", arm.pattern.span, "the outcome binds a different number of values", &.{ .{ .name = "outcome", .value = c.name }, .{ .name = "expected", .value = try l.fmt("{d}", .{o.params.len}) } });
|
||||
}
|
||||
var missing: std.ArrayList(u8) = .empty;
|
||||
@@ -3398,7 +3407,7 @@ const Lowerer = struct {
|
||||
l.cur = ok;
|
||||
const ps = l.blocks.items[ok].params.items;
|
||||
if (ps.len == 0) return .{ .name = "", .ty = .void };
|
||||
if (ps.len != 1) return l.fail("E7006", e.span, "`or trap` on an operation with several results: use `match`", &.{});
|
||||
if (ps.len != 1) return l.invalid(.invalid_operation, e.span, "`or trap` on an operation with several results: use `match`", &.{});
|
||||
return .{ .name = ps[0].name, .ty = ps[0].ty };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user