cli: validate structural source type positions before AIR
This commit is contained in:
1 parent
5f76e92ff3
commit
caf3b86e80
48 files changed
+423
-33
No files matched your search
@@ -2,6 +2,7 @@
|
||||
const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const lower = @import("lower.zig");
|
||||
const type_positions = @import("type_positions.zig");
|
||||
const authority = @import("../air/authority.zig");
|
||||
const A = std.mem.Allocator;
|
||||
const Error = error{ OutOfMemory, InvalidGeneric };
|
||||
@@ -162,8 +163,8 @@ const Generator = struct {
|
||||
for (params[0..i]) |previous| if (std.mem.eql(u8, p.name, previous.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate type parameter", p.name);
|
||||
if (p.callable) |c| {
|
||||
if (p.constraints.len != 0 or g.function(p.name, p.span) != null) return g.invalid(.invalid_static_function, p.span, "invalid static function parameter", p.name);
|
||||
for (c.params) |t| try g.headerType(t, params, 0);
|
||||
try g.headerType(c.ret, params, 0);
|
||||
for (c.params) |t| try g.headerAt(t, params, 0, .param);
|
||||
try g.headerAt(c.ret, params, 0, .ret);
|
||||
if (c.ret.kind == .wrapped) {
|
||||
const w = c.ret.kind.wrapped.wrapper;
|
||||
if (std.mem.eql(u8, w, "ref") or std.mem.eql(u8, w, "mut") or std.mem.eql(u8, w, "slice") or std.mem.eql(u8, w, "slice_mut")) return g.invalid(.invalid_static_function, c.ret.span, "static function contracts do not support borrowed returns", p.name);
|
||||
@@ -180,6 +181,17 @@ const Generator = struct {
|
||||
}
|
||||
}
|
||||
|
||||
fn headerAt(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize, position: type_positions.Position) Error!void {
|
||||
if (type_positions.firstInvalid(t, position)) |bad| {
|
||||
g.problem = .{ .code = "E7005", .span = bad.span, .message = "type constructor not allowed in this position", .fields = try g.arena.dupe(lower.Problem.Field, &.{
|
||||
.{ .name = "position", .value = @tagName(bad.position) },
|
||||
.{ .name = "type_constructor", .value = bad.constructor },
|
||||
}) };
|
||||
return error.InvalidGeneric;
|
||||
}
|
||||
return g.headerType(t, params, depth);
|
||||
}
|
||||
|
||||
fn headerType(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize) Error!void {
|
||||
if (depth >= 64) return g.invalid(.implementation_limit, t.span, "type traversal exceeds depth 64", "type");
|
||||
switch (t.kind) {
|
||||
@@ -212,7 +224,7 @@ const Generator = struct {
|
||||
if (depth >= 64) return g.invalid(.implementation_limit, arg.span, "generic argument traversal exceeds depth 64", expected.name);
|
||||
if (expected.callable == null) {
|
||||
if (arg.kind == .function_ref) return g.fail("E7005", arg.span, "expected a type argument, found a static function", expected.name);
|
||||
return g.headerType(arg, params, depth);
|
||||
return g.headerAt(arg, params, depth, .param);
|
||||
}
|
||||
if (arg.kind != .function_ref) return g.fail("E7005", arg.span, "expected an explicit static function argument", expected.name);
|
||||
const f = arg.kind.function_ref;
|
||||
@@ -266,8 +278,8 @@ const Generator = struct {
|
||||
},
|
||||
}
|
||||
if (t.generics.len != 0) switch (t.kind) {
|
||||
.@"struct" => |fs| for (fs) |field| try g.headerType(field.ty, t.generics, 0),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerType(payload_type, t.generics, 0),
|
||||
.@"struct" => |fs| for (fs) |field| try g.headerAt(field.ty, t.generics, 0, .member),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerAt(payload_type, t.generics, 0, .member),
|
||||
};
|
||||
generic = generic or t.generics.len != 0;
|
||||
}
|
||||
@@ -281,9 +293,9 @@ const Generator = struct {
|
||||
for (f.params, 0..) |p, j| {
|
||||
try g.checkBinding(p.name, p.span, .{ .params = f.generics });
|
||||
for (f.params[0..j]) |prev| if (std.mem.eql(u8, p.name, prev.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate value parameter", p.name);
|
||||
try g.headerType(p.ty, f.generics, 0);
|
||||
try g.headerAt(p.ty, f.generics, 0, .param);
|
||||
}
|
||||
try g.headerType(f.ret, f.generics, 0);
|
||||
try g.headerAt(f.ret, f.generics, 0, .ret);
|
||||
try g.borrowReturnContract(f);
|
||||
}
|
||||
generic = generic or f.generics.len != 0;
|
||||
|
||||
Reference in new issue
Block a user