feat: run existing ArrayList with imported C allocator
This commit is contained in:
+81
-11
@@ -312,7 +312,11 @@ check_elaborated proc(
|
||||
id :: ast.package_decl(tree, i)
|
||||
match ast.decl_kind(tree, id) {
|
||||
.statement: try collect_declaration(&state, id)
|
||||
.function: try collect_function(&state, id)
|
||||
.function: {
|
||||
if (!ast.function_external(tree, id)
|
||||
or mem.eql(ast.source_text(tree, ast.function_decl(tree, id).name), "main"))
|
||||
try collect_function(&state, id)
|
||||
}
|
||||
.invalid: collect_invalid(&state, id)
|
||||
.import: {}
|
||||
}
|
||||
@@ -436,6 +440,12 @@ check_elaborated proc(
|
||||
try require_numeric(&state, id, expression.left)
|
||||
try require_numeric(&state, id, expression.right)
|
||||
}
|
||||
.builtin_call: {
|
||||
if (mem.eql(builtin_name(&state, id), "divtrunc") and ast.call_arg_count(tree, id) == 2) {
|
||||
try require_numeric(&state, id, ast.call_arg(tree, id, 0))
|
||||
try require_numeric(&state, id, ast.call_arg(tree, id, 1))
|
||||
}
|
||||
}
|
||||
else: {}
|
||||
}
|
||||
}
|
||||
@@ -463,6 +473,12 @@ check_elaborated proc(
|
||||
}
|
||||
}
|
||||
}
|
||||
.builtin_call: {
|
||||
if (mem.eql(builtin_name(&state, id), "divtrunc") and ast.call_arg_count(tree, id) == 2) {
|
||||
try numeric_operand(&state, id, ast.call_arg(tree, id, 0))
|
||||
try numeric_operand(&state, id, ast.call_arg(tree, id, 1))
|
||||
}
|
||||
}
|
||||
else: {}
|
||||
}
|
||||
}
|
||||
@@ -522,9 +538,11 @@ check_elaborated proc(
|
||||
for (0..ast.package_decl_count(tree)) |i| {
|
||||
id :: ast.package_decl(tree, i)
|
||||
if (ast.decl_kind(tree, id) != .function or !ast.function_external(tree, id)) continue
|
||||
if (cells[usize(id)].role == .none) continue
|
||||
for (0..i) |j| {
|
||||
previous :: ast.package_decl(tree, j)
|
||||
if (ast.decl_kind(tree, previous) != .function or !ast.function_external(tree, previous)) continue
|
||||
if (cells[usize(previous)].role == .none) continue
|
||||
if (ast.name_id(tree, id) == ast.name_id(tree, previous) and !same_signature(&state, id, previous)) {
|
||||
try fail(&state, id, .type_mismatch, ast.function_decl(tree, id).name)
|
||||
}
|
||||
@@ -846,7 +864,7 @@ check_annotation proc(state @mut State, id ast.NodeId, allow_void bool) Type ! (
|
||||
if (
|
||||
(element_binding.kind == .builtin
|
||||
and (element_binding.data.builtin == .int or element_binding.data.builtin == .float))
|
||||
or element.kind == .void or element.kind == .range
|
||||
or element.kind == .void or element.kind == .range or element.kind == .anyopaque
|
||||
) {
|
||||
element = invalid_type(try diagnostics.add(state.diagnostics, .invalid_array_element_type, ast.node_span(state.tree, view.element)))
|
||||
state.types[usize(view.element)] = element
|
||||
@@ -879,6 +897,8 @@ check_annotation proc(state @mut State, id ast.NodeId, allow_void bool) Type ! (
|
||||
.invalid: type = invalid_type(binding.data.diagnostic)
|
||||
.builtin: {
|
||||
type.kind = builtin_kind(binding.data.builtin)
|
||||
if (binding.data.builtin == .unavailable)
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .invalid_operand, ast.node_span(state.tree, id)))
|
||||
if (type.kind == .anyopaque and state.pointer_depth == 0) {
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .expected_type, ast.node_span(state.tree, id)))
|
||||
}
|
||||
@@ -893,6 +913,8 @@ check_annotation proc(state @mut State, id ast.NodeId, allow_void bool) Type ! (
|
||||
initializer :: ast.statement_decl(state.tree, target).initializer
|
||||
if (type_syntax(ast.expr_kind(state.tree, initializer))) {
|
||||
type = try check_type_expression(state, initializer, true)
|
||||
if (type.kind == .anyopaque and state.pointer_depth == 0)
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .expected_type, ast.node_span(state.tree, id)))
|
||||
} else {
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .expected_type, ast.node_span(state.tree, id)))
|
||||
}
|
||||
@@ -952,6 +974,7 @@ collect_declaration proc(state @mut State, id ast.NodeId) void ! (mem.AllocError
|
||||
|
||||
@hide:file
|
||||
collect_function proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
if (state.cells[usize(id)].role == .function) return
|
||||
tree :: state.tree
|
||||
function :: ast.function_decl(tree, id)
|
||||
state.cells[usize(id)].role = .function
|
||||
@@ -977,6 +1000,7 @@ collect_function proc(state @mut State, id ast.NodeId) void ! (mem.AllocError |
|
||||
if (!ast.function_external(tree, id)) try collect_block(state, function.body, function.return_type, false)
|
||||
else {
|
||||
if (mem.eql(ast.source_text(tree, function.name), "main")) try fail(state, id, .invalid_entry, function.name)
|
||||
if (ast.callable_variadic(tree, id)) try fail(state, id, .invalid_operand, function.span)
|
||||
if (!c_abi_type(state, state.types[usize(function.return_type)])) try fail(state, id, .invalid_operand, function.span)
|
||||
for (0..ast.function_param_count(tree, id)) |i| {
|
||||
parameter :: ast.function_param(tree, id, i)
|
||||
@@ -985,6 +1009,19 @@ collect_function proc(state @mut State, id ast.NodeId) void ! (mem.AllocError |
|
||||
}
|
||||
}
|
||||
|
||||
@hide:file
|
||||
collect_foreign_symbol proc(state @mut State, target ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
if (state.cells[usize(target)].role == .function) return
|
||||
tree :: state.tree
|
||||
# declarations of the same external symbol must agree even across package namespaces.
|
||||
for 0..ast.package_decl_count(tree) |i| {
|
||||
id :: ast.package_decl(tree, i)
|
||||
if (ast.decl_kind(tree, id) == .function and ast.function_external(tree, id)
|
||||
and ast.name_id(tree, id) == ast.name_id(tree, target))
|
||||
try collect_function(state, id)
|
||||
}
|
||||
}
|
||||
|
||||
@hide:file
|
||||
collect_capture proc(state @mut State, id, body ast.NodeId) void ! mem.AllocError {
|
||||
state.cells[usize(id)].role = .loop_capture
|
||||
@@ -1252,6 +1289,7 @@ collect_expression proc(state @mut State, id ast.NodeId) void ! (mem.AllocError
|
||||
}
|
||||
if (binding.kind == .declaration) {
|
||||
target :: binding.data.declaration
|
||||
if (ast.function_external(tree, target)) try collect_foreign_symbol(state, target)
|
||||
role :: state.cells[usize(target)].role
|
||||
if (
|
||||
role != .parameter and role != .loop_capture and ast.decl_kind(tree, target) == .statement
|
||||
@@ -1441,6 +1479,7 @@ builtin_kind proc(type sema.BuiltinType) TypeKind {
|
||||
.c_uchar: .u8
|
||||
.c_short: .i16
|
||||
.c_ushort: .u16
|
||||
.unavailable: .none
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1840,7 +1879,8 @@ addressable_target proc(state @mut State, id ast.NodeId) bool {
|
||||
@hide:file
|
||||
same_signature proc(state @State, left, right ast.NodeId) bool {
|
||||
if (left == right) return true
|
||||
if (ast.function_external(state.tree, left) != ast.function_external(state.tree, right)) return false
|
||||
if (ast.callable_foreign(state.tree, left) != ast.callable_foreign(state.tree, right)) return false
|
||||
if (ast.callable_variadic(state.tree, left) != ast.callable_variadic(state.tree, right)) return false
|
||||
tree :: state.tree
|
||||
if (ast.function_param_count(tree, left) != ast.function_param_count(tree, right)) return false
|
||||
left_result :: ast.callable_result(tree, left)
|
||||
@@ -1900,7 +1940,7 @@ assignable_type proc(state @State, actual, expected Type) bool {
|
||||
a :: state.pointers.items[usize(actual.pointer_type)]
|
||||
b :: state.pointers.items[usize(expected.pointer_type)]
|
||||
if (a.many != b.many or (!a.mutable and b.mutable)) return false
|
||||
if (b.element.kind == .anyopaque) return true
|
||||
if (b.element.kind == .anyopaque and b.element.nominal == ast.NodeId(maxval!(u32))) return true
|
||||
if (b.mutable) return same_type(state, a.element, b.element)
|
||||
return assignable_type(state, a.element, b.element)
|
||||
}
|
||||
@@ -2320,11 +2360,11 @@ check_function_type proc(state @mut State, id ast.NodeId) Type ! (mem.AllocError
|
||||
if (cached.kind != .none) return cached
|
||||
type := Type{ kind = .function, function = id }
|
||||
state.cells[usize(id)].type = type
|
||||
state.pointer_depth += 1
|
||||
defer state.pointer_depth -= 1
|
||||
result :: ast.callable_result(state.tree, id)
|
||||
result_type :: try check_annotation(state, result, true)
|
||||
if (result_type.kind == .invalid) type = result_type
|
||||
if (ast.callable_variadic(state.tree, id) or (ast.callable_foreign(state.tree, id) and !c_abi_type(state, result_type)))
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .invalid_operand, ast.node_span(state.tree, id)))
|
||||
try depend(state, result, id)
|
||||
for 0..ast.function_param_count(state.tree, id) |i| {
|
||||
parameter :: ast.function_param(state.tree, id, i)
|
||||
@@ -2334,6 +2374,8 @@ check_function_type proc(state @mut State, id ast.NodeId) Type ! (mem.AllocError
|
||||
state.cells[usize(parameter)].type = parameter_type
|
||||
state.types[usize(parameter)] = parameter_type
|
||||
if (parameter_type.kind == .invalid and type.kind != .invalid) type = parameter_type
|
||||
if (ast.callable_foreign(state.tree, id) and !c_abi_type(state, parameter_type))
|
||||
type = invalid_type(try diagnostics.add(state.diagnostics, .invalid_operand, ast.node_span(state.tree, parameter)))
|
||||
try depend(state, annotation, parameter)
|
||||
try depend(state, parameter, id)
|
||||
}
|
||||
@@ -2387,10 +2429,16 @@ check_container_type proc(state @mut State, id ast.NodeId) Type ! (mem.AllocErro
|
||||
|
||||
@hide:file
|
||||
c_abi_type proc(state @State, type Type) bool {
|
||||
if (type.kind == .optional) return state.arrays.items[usize(type.array_type)].element.kind == .pointer
|
||||
if (type.kind == .optional) return c_abi_pointer(state, state.arrays.items[usize(type.array_type)].element)
|
||||
if (type.kind == .function) return ast.callable_foreign(state.tree, type.function) and !ast.callable_variadic(state.tree, type.function)
|
||||
return integer_kind(type.kind) or type.kind == .float or type.kind == .bool or type.kind == .pointer or type.kind == .void or type.kind == .invalid
|
||||
}
|
||||
|
||||
@hide:file
|
||||
c_abi_pointer proc(state @State, type Type) bool {
|
||||
return type.kind == .pointer or (type.kind == .function and c_abi_type(state, type))
|
||||
}
|
||||
|
||||
@hide:file
|
||||
builtin_name proc(state @State, id ast.NodeId) []u8 {
|
||||
return ast.literal_text(state.tree, ast.builtin_call(state.tree, id).name)
|
||||
@@ -2401,15 +2449,21 @@ collect_builtin proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | d
|
||||
name :: builtin_name(state, id)
|
||||
count :: ast.call_arg_count(state.tree, id)
|
||||
layout :: mem.eql(name, "sizeof") or mem.eql(name, "alignof")
|
||||
limit :: mem.eql(name, "maxval") or mem.eql(name, "minval")
|
||||
cast :: mem.eql(name, "ptrcast") or mem.eql(name, "constcast")
|
||||
for (0..count) |i| {
|
||||
argument :: ast.call_arg(state.tree, id, i)
|
||||
if (layout or (cast and count == 2 and i == 0)) {
|
||||
if (layout or limit or (cast and count == 2 and i == 0)) {
|
||||
_ = try check_annotation(state, argument, true)
|
||||
} else try collect_expression(state, argument)
|
||||
try depend(state, argument, id)
|
||||
}
|
||||
if (layout) state.cells[usize(id)].type = Type{ kind = .usize }
|
||||
if (limit and count == 1) state.cells[usize(id)].type = state.types[usize(ast.call_arg(state.tree, id, 0))]
|
||||
if (mem.eql(name, "divtrunc")) {
|
||||
state.cells[usize(id)].numeric = true
|
||||
state.cells[usize(id)].producer = id
|
||||
}
|
||||
if (mem.eql(name, "memcopy") or mem.eql(name, "memset")) state.cells[usize(id)].type = Type{ kind = .void }
|
||||
}
|
||||
|
||||
@@ -2483,16 +2537,30 @@ infer_builtin proc(state @mut State, id ast.NodeId, defaults bool) Type ! (mem.A
|
||||
count :: ast.call_arg_count(state.tree, id)
|
||||
span :: ast.node_span(state.tree, id)
|
||||
layout :: mem.eql(name, "sizeof") or mem.eql(name, "alignof")
|
||||
limit :: mem.eql(name, "maxval") or mem.eql(name, "minval")
|
||||
division :: mem.eql(name, "divtrunc")
|
||||
cast :: mem.eql(name, "ptrcast") or mem.eql(name, "constcast")
|
||||
memory :: mem.eql(name, "memcopy") or mem.eql(name, "memset")
|
||||
some :: mem.eql(name, "some")
|
||||
valid_count :: (layout and count == 1) or (cast and (count == 1 or count == 2)) or (memory and count == 2) or (some and count == 1)
|
||||
valid_count :: ((layout or limit) and count == 1) or (cast and (count == 1 or count == 2)) or ((memory or division) and count == 2) or (some and count == 1)
|
||||
if (!valid_count) {
|
||||
code diagnostics.Code :: if (layout or cast or memory or some) .argument_count_mismatch else .invalid_operand
|
||||
code diagnostics.Code :: if (layout or limit or division or cast or memory or some) .argument_count_mismatch else .invalid_operand
|
||||
if (defaults) try fail(state, id, code, span)
|
||||
return group_type(state, id)
|
||||
}
|
||||
if (layout) return group_type(state, id)
|
||||
if (limit) {
|
||||
type :: state.types[usize(ast.call_arg(state.tree, id, 0))]
|
||||
if (defaults and type.kind != .invalid and !integer_kind(type.kind)) try fail(state, id, .invalid_operand, span)
|
||||
return group_type(state, id)
|
||||
}
|
||||
if (division) {
|
||||
left :: try infer_shape(state, ast.call_arg(state.tree, id, 0), defaults)
|
||||
right :: try infer_shape(state, ast.call_arg(state.tree, id, 1), defaults)
|
||||
if (defaults and left.kind != .invalid and right.kind != .invalid
|
||||
and (!integer_kind(left.kind) or !same_type(state, left, right))) try fail(state, id, .invalid_operand, span)
|
||||
return group_type(state, id)
|
||||
}
|
||||
if (some) {
|
||||
result :: state.cells[root(state, usize(id))].expected
|
||||
if (result.kind != .optional) {
|
||||
@@ -3359,8 +3427,9 @@ intern_record proc(state @mut State, declaration ast.NodeId, fields []mut Record
|
||||
c_record_field_type proc(state @State, type Type, depth usize) bool {
|
||||
if (depth >= 128 or type.nominal != ast.NodeId(maxval!(u32))) return false
|
||||
if (numeric_kind(type.kind) or type.kind == .bool or type.kind == .pointer) return true
|
||||
if (type.kind == .function) return c_abi_type(state, type)
|
||||
if (type.kind == .optional)
|
||||
return state.arrays.items[usize(type.array_type)].element.kind == .pointer
|
||||
return c_abi_pointer(state, state.arrays.items[usize(type.array_type)].element)
|
||||
if (type.kind == .enumeration)
|
||||
return state.enums.items[usize(type.enum_type)].explicit_backing
|
||||
if (type.kind == .array) {
|
||||
@@ -3379,6 +3448,7 @@ c_record_field_type proc(state @State, type Type, depth usize) bool {
|
||||
|
||||
@hide:file
|
||||
check_record proc(state @mut State, id ast.NodeId, nominal bool) Type ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
if (ast.record_opaque(state.tree, id)) return Type{ kind = .anyopaque, nominal = id }
|
||||
cached := state.cells[usize(id)].type
|
||||
if (cached.kind == .invalid) return cached
|
||||
if (!state.cells[usize(id)].record_checking and (cached.kind == .record or cached.kind == .type_value)) {
|
||||
|
||||
@@ -18,6 +18,7 @@ BuiltinType :: enum {
|
||||
c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong
|
||||
c_char, c_uchar, c_short, c_ushort
|
||||
type
|
||||
unavailable
|
||||
}
|
||||
|
||||
@hide:file
|
||||
@@ -53,9 +54,13 @@ builtins [_]Builtin :: [
|
||||
{ name = "c_longlong", type = .c_longlong },
|
||||
{ name = "c_ulonglong", type = .c_ulonglong },
|
||||
{ name = "c_char", type = .c_char },
|
||||
{ name = "c_schar", type = .c_char },
|
||||
{ name = "c_uchar", type = .c_uchar },
|
||||
{ name = "c_short", type = .c_short },
|
||||
{ name = "c_ushort", type = .c_ushort },
|
||||
{ name = "c_double", type = .f64 },
|
||||
{ name = "c_float", type = .unavailable },
|
||||
{ name = "c_longdouble", type = .unavailable },
|
||||
{ name = "type", type = .type },
|
||||
]
|
||||
|
||||
@@ -194,6 +199,7 @@ resolve_packages proc(
|
||||
for (0..ast.package_decl_count_at(tree, root)) |i| {
|
||||
id :: ast.package_decl_at(tree, root, i)
|
||||
file_scopes[usize(ast.node_span(tree, id).source)].parent = scope
|
||||
if (ast.file_local(tree, id)) continue
|
||||
match ast.decl_kind(tree, id) {
|
||||
.statement: try predeclare(&state, scope, id, ast.statement_decl(tree, id).name)
|
||||
.function: try predeclare(&state, scope, id, ast.function_decl(tree, id).name)
|
||||
@@ -207,6 +213,18 @@ resolve_packages proc(
|
||||
}
|
||||
}
|
||||
}
|
||||
for (roots) |root| {
|
||||
for (0..ast.package_decl_count_at(tree, root)) |i| {
|
||||
id :: ast.package_decl_at(tree, root, i)
|
||||
if (!ast.file_local(tree, id)) continue
|
||||
scope :: &file_scopes[usize(ast.node_span(tree, id).source)]
|
||||
match ast.decl_kind(tree, id) {
|
||||
.statement: try predeclare(&state, scope, id, ast.statement_decl(tree, id).name)
|
||||
.function: try predeclare(&state, scope, id, ast.function_decl(tree, id).name)
|
||||
.import, .invalid: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (imports) |imported| try resolve_import(&state, &file_scopes[usize(ast.node_span(tree, imported.node).source)], imported)
|
||||
for (roots) |root| try resolve_package(&state, root, file_scopes)
|
||||
try canonicalize_aliases(&state)
|
||||
@@ -316,6 +334,10 @@ predeclare proc(
|
||||
name_span ast.Span,
|
||||
) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
name :: ast.name_id(state.tree, id)
|
||||
if (mem.eql(ast.source_text(state.tree, name_span), "_")) {
|
||||
state.bindings[usize(id)] = Binding{ kind = .declaration, data = BindingData{ declaration = id } }
|
||||
return
|
||||
}
|
||||
if hashmap.get(&scope.names, name) |_| {
|
||||
diagnostic :: try diagnostics.add(state.diagnostics, .duplicate_name, name_span)
|
||||
state.bindings[usize(id)] = Binding{
|
||||
|
||||
@@ -4349,6 +4349,21 @@ expression_type proc(state @mut State, id ast.NodeId, depth usize) ast.NodeId !
|
||||
.literal_bool: return try builtin(state, id, .bool)
|
||||
.literal_string: return try builtin(state, id, .string)
|
||||
.literal_void: return try builtin(state, id, .void)
|
||||
.builtin_call: {
|
||||
name :: ast.literal_text(&state.tree, ast.builtin_call(&state.tree, id).name)
|
||||
count :: ast.call_arg_count(&state.tree, id)
|
||||
if ((mem.eql(name, "sizeof") or mem.eql(name, "alignof")) and count == 1)
|
||||
return try builtin(state, id, .usize)
|
||||
if ((mem.eql(name, "maxval") or mem.eql(name, "minval")) and count == 1)
|
||||
return try canonical_type(state, ast.call_arg(&state.tree, id, 0))
|
||||
if (mem.eql(name, "divtrunc") and count == 2) {
|
||||
left :: ast.call_arg(&state.tree, id, 0)
|
||||
right :: ast.call_arg(&state.tree, id, 1)
|
||||
operand :: if (flexible(state, left)) right else left
|
||||
return try expression_type(state, operand, depth + 1)
|
||||
}
|
||||
return no_node
|
||||
}
|
||||
.catch_expr: {
|
||||
type :: try expression_type(state, ast.catch_expr(&state.tree, id).operand, depth + 1)
|
||||
if (type == no_node or ast.expr_kind(&state.tree, type) != .fallible_type) return no_node
|
||||
@@ -4447,6 +4462,25 @@ expression_type proc(state @mut State, id ast.NodeId, depth usize) ast.NodeId !
|
||||
)
|
||||
)
|
||||
return try builtin(state, id, .usize)
|
||||
if (mem.eql(member, "ptr")) {
|
||||
element := no_node
|
||||
mutable := false
|
||||
if (base_kind == .array_type) {
|
||||
element = ast.array_type(&state.tree, base).element
|
||||
mutable = ast.array_type(&state.tree, base).mutable
|
||||
}
|
||||
if (base_kind == .slice_type) {
|
||||
element = ast.slice_type(&state.tree, base).element
|
||||
mutable = ast.slice_type(&state.tree, base).mutable
|
||||
}
|
||||
if (binding(state, base).kind == .builtin and binding(state, base).data.builtin == .string)
|
||||
element = try builtin(state, id, .u8)
|
||||
if (element != no_node) {
|
||||
result :: try ast.add_pointer_type(state.builder, view.span, element, mutable, true)
|
||||
try refresh(state)
|
||||
return result
|
||||
}
|
||||
}
|
||||
if (base_kind != .record_type and base_kind != .union_type) return no_node
|
||||
slot :: type_record_slot(state, base, view.member)
|
||||
if (slot == no_index) return no_node
|
||||
@@ -4524,32 +4558,36 @@ infer proc(state @mut State, function, pattern, actual ast.NodeId, arguments []m
|
||||
resolved :: binding(state, pattern)
|
||||
if (resolved.kind == .declaration and parameter_index(state, function, resolved.data.declaration) != no_index)
|
||||
return try infer_value(state, function, pattern, Value{ kind = .type, node = actual }, arguments, contextual)
|
||||
# only normalize the actual type: the pattern may still contain unbound
|
||||
# parameters or a type-generator call whose arguments must be inferred.
|
||||
concrete :: try canonical_type(state, actual)
|
||||
if (concrete == no_node) return false
|
||||
kind :: ast.expr_kind(&state.tree, pattern)
|
||||
actual_kind :: ast.expr_kind(&state.tree, actual)
|
||||
actual_kind :: ast.expr_kind(&state.tree, concrete)
|
||||
match kind {
|
||||
.pointer_type: {
|
||||
if (actual_kind != .pointer_type) return false
|
||||
return try infer(state, function, ast.pointer_type(&state.tree, pattern).element,
|
||||
ast.pointer_type(&state.tree, actual).element, arguments, contextual)
|
||||
ast.pointer_type(&state.tree, concrete).element, arguments, contextual)
|
||||
}
|
||||
.slice_type: {
|
||||
element := no_node
|
||||
if (actual_kind == .slice_type) element = ast.slice_type(&state.tree, actual).element
|
||||
if (actual_kind == .array_type) element = ast.array_type(&state.tree, actual).element
|
||||
if (actual_kind == .slice_type) element = ast.slice_type(&state.tree, concrete).element
|
||||
if (actual_kind == .array_type) element = ast.array_type(&state.tree, concrete).element
|
||||
if (element == no_node) return false
|
||||
return try infer(state, function, ast.slice_type(&state.tree, pattern).element, element, arguments, contextual)
|
||||
}
|
||||
.array_type: {
|
||||
if (actual_kind != .array_type) return false
|
||||
p :: ast.array_type(&state.tree, pattern)
|
||||
a :: ast.array_type(&state.tree, actual)
|
||||
a :: ast.array_type(&state.tree, concrete)
|
||||
if (p.count) |count| {
|
||||
if (a.count == null or !try infer_value(state, function, count, try evaluate(state, a.count?), arguments, false)) return false
|
||||
}
|
||||
return try infer(state, function, p.element, a.element, arguments, contextual)
|
||||
}
|
||||
.optional_type: {
|
||||
element :: if (actual_kind == .optional_type) ast.type_operand(&state.tree, actual) else actual
|
||||
element :: if (actual_kind == .optional_type) ast.type_operand(&state.tree, concrete) else concrete
|
||||
return try infer(state, function, ast.type_operand(&state.tree, pattern), element, arguments, contextual)
|
||||
}
|
||||
.call: {
|
||||
@@ -4558,7 +4596,7 @@ infer proc(state @mut State, function, pattern, actual ast.NodeId, arguments []m
|
||||
for (0..(state.instances.items.len)) |i| {
|
||||
instance :: state.instances.items[i]
|
||||
if (instance.type == no_node or instance.function != callee.data.declaration) continue
|
||||
if (try canonical_type(state, instance.type) != actual) continue
|
||||
if (try canonical_type(state, instance.type) != concrete) continue
|
||||
count :: ast.call_arg_count(&state.tree, pattern)
|
||||
if (count != instance.arguments.len) return false
|
||||
for (0..count) |j| {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import "@std/mem"
|
||||
import "@source/ast"
|
||||
import "@source/diagnostics"
|
||||
import "@source/strpool"
|
||||
sema :: import "@source/sema"
|
||||
typecheck :: import "@source/sema/check"
|
||||
import "@std/testing"
|
||||
import "@source/lexer"
|
||||
import "@source/parser"
|
||||
|
||||
@hide:file
|
||||
expect_specialization proc(
|
||||
source []u8, expected diagnostics.Code, expected_count usize,
|
||||
) void ! (mem.AllocError | diagnostics.CapacityError | strpool.InternError | testing.Error) {
|
||||
strings := strpool.init(mem.c_allocator)
|
||||
defer strpool.deinit(&strings)
|
||||
diagnostic_store := diagnostics.init(mem.c_allocator)
|
||||
defer diagnostics.deinit(&diagnostic_store)
|
||||
scan_state := lexer.init(mem.c_allocator)
|
||||
defer lexer.deinit(&scan_state)
|
||||
try lexer.scan(&scan_state, source, &strings, &diagnostic_store)
|
||||
builder := ast.init_builder(mem.c_allocator, source)
|
||||
defer ast.deinit_builder(&builder)
|
||||
root :: try parser.parse_source(&builder, scan_state.tokens.items, &diagnostic_store)
|
||||
tree := ast.view(&builder, root)
|
||||
resolution := try sema.resolve(mem.c_allocator, &tree, &strings, &diagnostic_store)
|
||||
defer sema.deinit(&resolution)
|
||||
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
|
||||
elaborated := try elaborate(mem.c_allocator, &builder, root, &resolution, &strings, &diagnostic_store)
|
||||
defer deinit(&elaborated)
|
||||
tree = ast.view(&builder, root)
|
||||
checked := try typecheck.check_elaborated(mem.c_allocator, &tree, &resolution, elaborated.constraints.items, &diagnostic_store)
|
||||
defer typecheck.deinit(&checked)
|
||||
if (expected_count == 0) {
|
||||
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
|
||||
for (0..ast.package_decl_count(&tree)) |i| {
|
||||
declaration :: ast.package_decl(&tree, i)
|
||||
if (ast.node_class(&tree, declaration) != .function) continue
|
||||
try testing.expect(!ast.function_generic(&tree, declaration))
|
||||
for (0..ast.function_param_count(&tree, declaration)) |j|
|
||||
try testing.expect(!ast.param(&tree, ast.function_param(&tree, declaration, j)).comptime)
|
||||
}
|
||||
} else {
|
||||
count := usize(0)
|
||||
for (diagnostic_store.list.items) |diagnostic| {
|
||||
if (diagnostic.code == expected) count += 1
|
||||
}
|
||||
try testing.expect_equal(expected_count, count)
|
||||
}
|
||||
}
|
||||
|
||||
infers_nested_nominal_pointer_parameters_and_readonly_slice_views test {
|
||||
try expect_specialization(
|
||||
`Storage proc($T type) type { return struct { items []mut T, capacity usize } }
|
||||
`make proc($T type, values []mut T) Storage(T) {
|
||||
` return Storage(T) { items = values, capacity = values.len }
|
||||
`}
|
||||
`observe proc($T type, values []T) usize { return values.len }
|
||||
`reserve proc($T type, storage @mut Storage(T), count usize) void {
|
||||
` _ = observe(storage.items.ptr[..count])
|
||||
`}
|
||||
`append proc($T type, storage @mut Storage(T), value T) void {
|
||||
` reserve(storage, storage.items.len)
|
||||
` storage.items[0] = value
|
||||
`}
|
||||
`release proc($T type, storage @mut Storage(T)) void {
|
||||
` allocation :: storage.items.ptr[..storage.capacity]
|
||||
` _ = observe(allocation)
|
||||
`}
|
||||
`forward proc(storage @mut Storage(i16)) void {
|
||||
` append(storage, 7)
|
||||
` release(storage)
|
||||
`}
|
||||
`main proc() i16 {
|
||||
` values [2]mut i16 := [1, 2]
|
||||
` storage := make(i16, values[..])
|
||||
` forward(&storage)
|
||||
` append(&storage, 9)
|
||||
` release(&storage)
|
||||
` return storage.items[0]
|
||||
`}
|
||||
, .cannot_infer_generic, 0,
|
||||
)
|
||||
}
|
||||
|
||||
rejects_conflicting_rigid_types_and_unrelated_nominal_generators test {
|
||||
try expect_specialization(
|
||||
`same proc($T type, left, right T) void {}
|
||||
`main proc() void { same(i16(1), u8(2)) }
|
||||
, .cannot_infer_generic, 1,
|
||||
)
|
||||
try expect_specialization(
|
||||
`Box proc($T type) type { return struct { value T } }
|
||||
`Other proc($T type) type { return struct { value T } }
|
||||
`read proc($T type, value @Box(T)) void {}
|
||||
`main proc() void {
|
||||
` value Other(i16) :: { value = 1 }
|
||||
` read(&value)
|
||||
`}
|
||||
, .cannot_infer_generic, 1,
|
||||
)
|
||||
try expect_specialization(
|
||||
`unknown proc($T type, value usize) void {}
|
||||
`main proc() void { unknown(1) }
|
||||
, .cannot_infer_generic, 1,
|
||||
)
|
||||
}
|
||||
|
||||
preserves_rigid_inference_before_contextual_literals test {
|
||||
try expect_specialization(
|
||||
`same proc($T type, left, right T) T { return right }
|
||||
`main proc() i16 { return same(i16(1), 2) }
|
||||
, .cannot_infer_generic, 0,
|
||||
)
|
||||
}
|
||||
|
||||
infers_integer_builtin_results_from_their_type_or_rigid_operand test {
|
||||
try expect_specialization(
|
||||
`same proc($T type, left, right T) T { return right }
|
||||
`main proc() i16 {
|
||||
` upper :: same(maxval!(i16), i16(1))
|
||||
` lower :: same(minval!(i16), i16(2))
|
||||
` left :: same(divtrunc!(i16(8), 2), i16(3))
|
||||
` right :: same(divtrunc!(8, i16(2)), i16(4))
|
||||
` return upper + lower + left + right
|
||||
`}
|
||||
, .cannot_infer_generic, 0,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user