feat: execute local assignments and while loops

This commit is contained in:
2026-10-10 00:17:12 +02:00
parent 021cbecac8
commit b35a68dff8
16 changed files with 1440 additions and 170 deletions
+236 -121
View File
@@ -27,7 +27,20 @@ Result :: struct { storage Storage }
no_index :: maxval!(usize)
@hide:file
Role :: enum { none, expression, declaration, parameter, function, annotation, invalid, body, return_stmt, if_stmt }
Role :: enum {
none,
expression,
declaration,
parameter,
function,
annotation,
invalid,
body,
return_stmt,
if_stmt,
assignment_stmt,
while_stmt,
}
@hide:file
Cell :: struct {
@@ -39,6 +52,8 @@ Cell :: struct {
anchored bool = false
demand TypeKind = .none
fixed bool = false
local bool = false
assignment_value bool = false
first_edge usize = no_index
return_annotation ast.NodeId = ast.NodeId(maxval!(u32))
}
@@ -57,6 +72,8 @@ State :: struct {
types []mut Type
cells []mut Cell
edges arraylist.ArrayList(Edge)
collecting_assignment bool = false
has_assignments bool = false
}
#! borrows the AST, resolution, and diagnostic store only during checking.
@@ -95,6 +112,19 @@ check proc(
.invalid: collect_invalid(&state, id)
}
}
for (0..count) |i| {
if (cells[i].role != .assignment_stmt) continue
target :: ast.assignment_stmt(tree, ast.NodeId(i)).target
binding :: sema.binding(resolution, target)
if (binding.kind == .invalid) {
cells[i].type = invalid_type(binding.data.diagnostic)
continue
}
if (binding.kind != .declaration or mutable_target(&state, target)) continue
cells[i].type = invalid_type(try diagnostics.add(
diagnostic_store, .invalid_assignment_target, ast.node_span(tree, target),
))
}
# aliases share a type variable; explicit annotations and parameters are boundaries.
for (0..count) |i| {
@@ -113,138 +143,155 @@ check proc(
}
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .identifier) continue
binding :: sema.binding(resolution, id)
if (binding.kind == .declaration and !cells[usize(binding.data.declaration)].fixed) {
if (binding.kind == .declaration and !cells[i].assignment_value and !cells[usize(binding.data.declaration)].fixed) {
join(&state, id, binding.data.declaration)
}
}
# function aliases are known before any calls or bodies are checked. Honey's
# return annotations are scalar, so a call cannot introduce a function type.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32))) {
result :: ast.function_decl(tree, signature.function).return_type
seed(&state, id, types[usize(result)])
} else if (signature.kind == .invalid) {
seed(&state, id, signature)
}
}
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32))) continue
if (signature.kind == .invalid) {
seed(&state, id, signature)
} else if (signature.kind != .none or cells[root(&state, usize(callee))].numeric) {
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
} else {
# without a function anchor this dependency cannot become callable;
# share its eventual inference error instead of emitting a cascade.
join(&state, id, callee)
}
}
for (0..count) |i| {
if (cells[i].role == .none or cells[i].role == .body) continue
types[i] = cells[root(&state, i)].type
}
# reject inherently nonnumeric operations before connecting their operands.
# their otherwise valid inputs must not join a failed constraint component.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression) continue
match ast.expr_kind(tree, id) {
.unary: try require_numeric(&state, id, ast.unary_expr(tree, id).operand)
.binary: {
expression :: ast.binary_expr(tree, id)
if (bool_equality(&state, id)) continue
try require_numeric(&state, id, expression.left)
try require_numeric(&state, id, expression.right)
# assignment inputs observe established declaration types, never contribute
# constraints back into those declarations.
for 0..2 |phase| {
if (phase == 1 and !state.has_assignments) break
assignment_values :: phase == 1
if (assignment_values) {
for (0..count) |i| {
id :: ast.NodeId(i)
if (!cells[i].assignment_value or cells[i].role != .expression or ast.expr_kind(tree, id) != .identifier) continue
binding :: sema.binding(resolution, id)
if (binding.kind == .declaration) cells[i].type = types[usize(binding.data.declaration)]
}
else: {}
}
}
propagate(&state, queue)
# function aliases are known before calls; scalar return annotations
# mean a call cannot introduce a function type.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32))) {
result :: ast.function_decl(tree, signature.function).return_type
seed(&state, id, types[usize(result)])
} else if (signature.kind == .invalid) {
seed(&state, id, signature)
}
}
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32))) continue
if (signature.kind == .invalid) {
seed(&state, id, signature)
} else if (signature.kind != .none or cells[root(&state, usize(callee))].numeric) {
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
} else {
# without a function anchor, share the eventual inference error.
join(&state, id, callee)
}
}
for (0..count) |i| {
if (cells[i].role == .none or cells[i].role == .body or cells[i].assignment_value != assignment_values or types[i].kind == .invalid) continue
types[i] = cells[root(&state, i)].type
}
# reject nonnumeric operations before connecting otherwise valid inputs.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values) continue
match ast.expr_kind(tree, id) {
.unary: try require_numeric(&state, id, ast.unary_expr(tree, id).operand)
.binary: {
expression :: ast.binary_expr(tree, id)
if (bool_equality(&state, id)) continue
try require_numeric(&state, id, expression.left)
try require_numeric(&state, id, expression.right)
}
else: {}
}
}
propagate(&state, queue)
# numeric edges are undirected constraints, not a traversal-time conversion.
# rigid values supply demands without joining the component they constrain.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression) continue
match ast.expr_kind(tree, id) {
.unary: try numeric_operand(&state, id, ast.unary_expr(tree, id).operand)
.binary: {
expression :: ast.binary_expr(tree, id)
if (comparison(expression.operator)) {
if (!bool_equality(&state, id)) try comparison_operands(&state, id)
} else {
try numeric_operand(&state, id, expression.left)
try numeric_operand(&state, id, expression.right)
if (expression.operator == .divide) {
try impose(&state, id, .float, .integer_division, expression.span)
# numeric edges join flexible operands; rigid inputs supply only demands.
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values) continue
match ast.expr_kind(tree, id) {
.unary: try numeric_operand(&state, id, ast.unary_expr(tree, id).operand)
.binary: {
expression :: ast.binary_expr(tree, id)
if (comparison(expression.operator)) {
if (!bool_equality(&state, id)) try comparison_operands(&state, id)
} else {
try numeric_operand(&state, id, expression.left)
try numeric_operand(&state, id, expression.right)
if (expression.operator == .divide) {
try impose(&state, id, .float, .integer_division, expression.span)
}
}
}
}
else: {}
}
}
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role == .declaration) {
declaration :: ast.statement_decl(tree, id)
if (declaration.type) |annotation| {
try demand(&state, declaration.initializer, types[usize(annotation)], .type_mismatch)
else: {}
}
}
if (cells[i].role == .return_stmt) {
if (ast.return_stmt(tree, id).value) |value| {
try demand(&state, value, types[usize(cells[i].return_annotation)], .return_type_mismatch)
for (0..count) |i| {
id :: ast.NodeId(i)
if (!assignment_values and cells[i].role == .declaration) {
declaration :: ast.statement_decl(tree, id)
if (declaration.type) |annotation| {
try demand(&state, declaration.initializer, types[usize(annotation)], .type_mismatch)
}
}
if (!assignment_values and cells[i].role == .return_stmt) {
if (ast.return_stmt(tree, id).value) |value| {
try demand(&state, value, types[usize(cells[i].return_annotation)], .return_type_mismatch)
}
}
if (assignment_values and cells[i].role == .assignment_stmt) {
assignment :: ast.assignment_stmt(tree, id)
if (mutable_target(&state, assignment.target)) {
try demand(&state, assignment.value, types[usize(assignment.target)], .type_mismatch)
}
}
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
signature :: group_type(&state, ast.call_expr(tree, id).callee)
if (signature.function == ast.NodeId(maxval!(u32))) continue
parameter_count :: ast.function_param_count(tree, signature.function)
for (0..ast.call_arg_count(tree, id)) |j| {
if (j >= parameter_count) continue
parameter :: ast.function_param(tree, signature.function, j)
try demand(&state, ast.call_arg(tree, id, j), cells[usize(parameter)].type, .type_mismatch)
}
}
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .call) continue
signature :: group_type(&state, ast.call_expr(tree, id).callee)
if (signature.function == ast.NodeId(maxval!(u32))) continue
parameter_count :: ast.function_param_count(tree, signature.function)
for (0..ast.call_arg_count(tree, id)) |j| {
if (j >= parameter_count) continue
parameter :: ast.function_param(tree, signature.function, j)
try demand(&state, ast.call_arg(tree, id, j), cells[usize(parameter)].type, .type_mismatch)
}
}
# default only after the complete program has supplied its numeric demands.
for (0..count) |i| {
if (root(&state, i) != i) continue
if (cells[i].type.kind == .invalid) continue
if (cells[i].numeric and cells[i].anchored) {
kind TypeKind :: if (cells[i].demand == .float) .float else .int
cells[i].type = Type{ kind = kind }
# defaults follow ordinary program demands, then isolated assignment demands.
for (0..count) |i| {
if (root(&state, i) != i or cells[i].assignment_value != assignment_values) continue
if (cells[i].type.kind == .invalid) continue
if (cells[i].numeric and cells[i].anchored) {
kind TypeKind :: if (cells[i].demand == .float) .float else .int
cells[i].type = Type{ kind = kind }
}
}
}
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32)) or signature.kind == .none or signature.kind == .invalid) continue
if (group_type(&state, id).kind != .invalid) {
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
for (0..count) |i| {
id :: ast.NodeId(i)
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
callee :: ast.call_expr(tree, id).callee
signature :: group_type(&state, callee)
if (signature.function != ast.NodeId(maxval!(u32)) or signature.kind == .none or signature.kind == .invalid) continue
if (group_type(&state, id).kind != .invalid) {
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
}
}
for (0..count) |i| {
if (assignment_values or cells[i].role != .declaration or types[i].kind == .invalid) continue
index :: root(&state, i)
if (cells[index].type.kind != .none) continue
diagnostic :: try diagnostics.add(diagnostic_store, .cannot_infer_type, ast.statement_decl(tree, ast.NodeId(i)).name)
cells[index].type = invalid_type(diagnostic)
}
for (0..count) |i| {
if (cells[i].role == .none or cells[i].role == .body or cells[i].assignment_value != assignment_values or types[i].kind == .invalid) continue
types[i] = cells[root(&state, i)].type
}
}
for (0..count) |i| {
if (cells[i].role != .declaration or types[i].kind == .invalid) continue
index :: root(&state, i)
if (cells[index].type.kind != .none) continue
diagnostic :: try diagnostics.add(diagnostic_store, .cannot_infer_type, ast.statement_decl(tree, ast.NodeId(i)).name)
cells[index].type = invalid_type(diagnostic)
}
for (0..count) |i| {
if (cells[i].role == .none or cells[i].role == .body or types[i].kind == .invalid) continue
types[i] = cells[root(&state, i)].type
}
for (0..count) |i| try validate(&state, ast.NodeId(i))
@@ -253,8 +300,9 @@ check proc(
propagate(&state, queue)
# validate conditions after operand errors have reached their users.
for (0..count) |i| {
if (cells[i].role != .if_stmt) continue
condition :: ast.if_stmt(tree, ast.NodeId(i)).condition
id :: ast.NodeId(i)
if (cells[i].role != .if_stmt and cells[i].role != .while_stmt) continue
condition :: if (cells[i].role == .if_stmt) ast.if_stmt(tree, id).condition else ast.while_stmt(tree, id).condition
actual :: types[usize(condition)]
if (actual.kind != .invalid and actual.kind != .bool) {
try fail(&state, condition, .condition_not_bool, ast.node_span(tree, condition))
@@ -465,7 +513,10 @@ collect_block proc(state @mut State, id, return_annotation ast.NodeId) void ! (m
for (0..ast.block_statement_count(tree, id)) |i| {
statement :: ast.block_statement(tree, id, i)
match ast.stmt_kind(tree, statement) {
.declaration: try collect_declaration(state, statement)
.declaration: {
state.cells[usize(statement)].local = true
try collect_declaration(state, statement)
}
.expression: try collect_expression(state, ast.statement_expr(tree, statement))
.return_stmt: {
state.cells[usize(statement)].role = .return_stmt
@@ -489,6 +540,35 @@ collect_block proc(state @mut State, id, return_annotation ast.NodeId) void ! (m
try depend(state, other, id)
}
}
.assignment_stmt: {
assignment :: ast.assignment_stmt(tree, statement)
state.cells[usize(statement)].role = .assignment_stmt
state.has_assignments = true
state.cells[usize(statement)].type = Type{ kind = .void }
binding :: sema.binding(state.resolution, assignment.target)
if (binding.kind == .builtin) {
state.cells[usize(assignment.target)].role = .expression
state.cells[usize(assignment.target)].type = invalid_type(try diagnostics.add(
state.diagnostics, .invalid_assignment_target, ast.node_span(tree, assignment.target),
))
} else {
try collect_expression(state, assignment.target)
}
state.collecting_assignment = true
try collect_expression(state, assignment.value)
state.collecting_assignment = false
try depend(state, assignment.target, statement)
try depend(state, assignment.value, statement)
}
.while_stmt: {
loop :: ast.while_stmt(tree, statement)
state.cells[usize(statement)].role = .while_stmt
state.cells[usize(statement)].type = Type{ kind = .void }
try collect_expression(state, loop.condition)
try depend(state, loop.condition, statement)
try collect_block(state, loop.body, return_annotation)
try depend(state, loop.body, id)
}
.invalid: collect_invalid(state, statement)
}
source :: if (ast.stmt_kind(tree, statement) == .expression) ast.statement_expr(tree, statement) else statement
@@ -501,6 +581,7 @@ collect_expression proc(state @mut State, id ast.NodeId) void ! (mem.AllocError
if (state.cells[usize(id)].role != .none) return
tree :: state.tree
state.cells[usize(id)].role = .expression
state.cells[usize(id)].assignment_value = state.collecting_assignment
match ast.expr_kind(tree, id) {
.literal_int: {
state.cells[usize(id)].numeric = true
@@ -641,11 +722,39 @@ fail proc(state @mut State, id ast.NodeId, code diagnostics.Code, span ast.Span)
state.types[usize(id)] = invalid_type(try diagnostics.add(state.diagnostics, code, span))
}
@hide:file
mutable_target proc(state @State, id ast.NodeId) bool {
binding :: sema.binding(state.resolution, id)
if (binding.kind != .declaration) return false
target :: binding.data.declaration
if (state.cells[usize(target)].role != .declaration or !state.cells[usize(target)].local) return false
return ast.statement_decl(state.tree, target).mutability == .variable
}
@hide:file
same_signature proc(state @State, left, right ast.NodeId) bool {
tree :: state.tree
if (ast.function_param_count(tree, left) != ast.function_param_count(tree, right)) return false
left_result :: ast.function_decl(tree, left).return_type
right_result :: ast.function_decl(tree, right).return_type
if (state.types[usize(left_result)].kind != state.types[usize(right_result)].kind) return false
for (0..ast.function_param_count(tree, left)) |i| {
left_param :: ast.function_param(tree, left, i)
right_param :: ast.function_param(tree, right, i)
if (state.types[usize(left_param)].kind != state.types[usize(right_param)].kind) return false
}
return true
}
@hide:file
compatible proc(state @mut State, value ast.NodeId, expected Type) void ! (mem.AllocError | diagnostics.CapacityError) {
actual :: state.types[usize(value)]
if (actual.kind == .invalid or expected.kind == .invalid) return
if (actual.kind != expected.kind) try fail(state, value, .type_mismatch, ast.node_span(state.tree, value))
if (actual.kind != expected.kind) {
try fail(state, value, .type_mismatch, ast.node_span(state.tree, value))
} else if (actual.kind == .function and !same_signature(state, actual.function, expected.function)) {
try fail(state, value, .type_mismatch, ast.node_span(state.tree, value))
}
}
@hide:file
@@ -659,6 +768,12 @@ validate proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnost
}
if (declaration.type) |type| try compatible(state, declaration.initializer, state.types[usize(type)])
}
.assignment_stmt: {
assignment :: ast.assignment_stmt(tree, id)
if (mutable_target(state, assignment.target)) {
try compatible(state, assignment.value, state.types[usize(assignment.target)])
}
}
.return_stmt: {
statement :: ast.return_stmt(tree, id)
expected :: state.types[usize(state.cells[usize(id)].return_annotation)]
+281
View File
@@ -968,3 +968,284 @@ analyzes_return_paths_after_trap_propagation_without_poisoning_signatures test {
try testing.expect_equal(2, code_count(&diagnostic_store, .missing_return))
try testing.expect_equal(1, code_count(&diagnostic_store, .condition_not_bool))
}
assignments_preserve_established_types_and_contextualize_numeric_values test {
strpool.strings = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.strings)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`take_float proc(value float) int { return 1 }
`main proc() void {
` number := 0
` number = 1.5
` number = number + 1.5
` number = take_float(number)
` observed :: number
` quotient float := 0
` quotient = 3 / 2
` inferred := 0
` context float :: inferred
` inferred = 5 / 2
` rigid int := 1
` quotient = rigid
` flag := true
` flag = false
` text := "before"
` text = "after"
`}
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(mem.c_allocator, scan_state.tokens.items, source, &diagnostic_store)
defer ast.deinit(&tree)
resolution := try sema.resolve(mem.c_allocator, &tree, &diagnostic_store)
defer sema.deinit(&resolution)
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
result := try check(mem.c_allocator, &tree, &resolution, &diagnostic_store)
defer deinit(&result)
body :: ast.function_decl(&tree, ast.package_decl(&tree, 1)).body
number :: ast.block_statement(&tree, body, 0)
observed :: ast.block_statement(&tree, body, 4)
quotient :: ast.block_statement(&tree, body, 5)
inferred :: ast.block_statement(&tree, body, 7)
rigid :: ast.block_statement(&tree, body, 10)
declarations [3]ast.NodeId :: [number, observed, rigid]
for declarations |id| {
try expect_kind(&result, id, .int)
try expect_kind(&result, ast.statement_decl(&tree, id).initializer, .int)
}
try expect_kind(&result, quotient, .float)
try expect_kind(&result, inferred, .float)
bad_values [2]usize :: [1, 11]
for bad_values |index| {
statement :: ast.block_statement(&tree, body, index)
assignment :: ast.assignment_stmt(&tree, statement)
original :: try expect_error(&result, &diagnostic_store, assignment.value, .type_mismatch, ast.node_span(&tree, assignment.value))
try testing.expect_equal(original, type_of(&result, statement).diagnostic)
expected :: if (index == 1) TypeKind.int else TypeKind.float
try expect_kind(&result, assignment.target, expected)
}
arithmetic_statement :: ast.block_statement(&tree, body, 2)
arithmetic :: ast.assignment_stmt(&tree, arithmetic_statement)
original :: try expect_error(&result, &diagnostic_store, arithmetic.value, .invalid_operand, ast.node_span(&tree, arithmetic.value))
try testing.expect_equal(original, type_of(&result, arithmetic_statement).diagnostic)
try expect_kind(&result, arithmetic.target, .int)
try expect_kind(&result, ast.binary_expr(&tree, arithmetic.value).left, .int)
call_statement :: ast.block_statement(&tree, body, 3)
call :: ast.assignment_stmt(&tree, call_statement)
argument :: ast.call_arg(&tree, call.value, 0)
call_error :: try expect_error(&result, &diagnostic_store, argument, .type_mismatch, ast.node_span(&tree, argument))
try testing.expect_equal(call_error, type_of(&result, call_statement).diagnostic)
try expect_kind(&result, call.target, .int)
divisions [2]usize :: [6, 9]
for divisions |index| {
statement :: ast.block_statement(&tree, body, index)
assignment :: ast.assignment_stmt(&tree, statement)
division :: ast.binary_expr(&tree, assignment.value)
try expect_kind(&result, statement, .void)
try expect_kind(&result, assignment.target, .float)
try expect_kind(&result, assignment.value, .float)
try expect_kind(&result, division.left, .float)
try expect_kind(&result, division.right, .float)
}
scalar_assignments [2]usize :: [13, 15]
for scalar_assignments |index| {
statement :: ast.block_statement(&tree, body, index)
assignment :: ast.assignment_stmt(&tree, statement)
expected :: if (index == 13) TypeKind.bool else TypeKind.string
try expect_kind(&result, statement, .void)
try expect_kind(&result, assignment.target, expected)
try expect_kind(&result, assignment.value, expected)
}
try testing.expect_equal(3, code_count(&diagnostic_store, .type_mismatch))
try testing.expect_equal(1, code_count(&diagnostic_store, .invalid_operand))
try testing.expect_equal(0, code_count(&diagnostic_store, .integer_division))
}
assignment_target_errors_preserve_bindings_and_independent_rhs_diagnostics test {
strpool.strings = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.strings)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`global := 1
`named proc() void {}
`main proc(parameter int) void {
` constant :: 1
` constant = 2
` parameter = 2
` global = 2
` named = named
` int = 2
` absent = other_missing
` constant = -false
`}
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(mem.c_allocator, scan_state.tokens.items, source, &diagnostic_store)
defer ast.deinit(&tree)
resolution := try sema.resolve(mem.c_allocator, &tree, &diagnostic_store)
defer sema.deinit(&resolution)
result := try check(mem.c_allocator, &tree, &resolution, &diagnostic_store)
defer deinit(&result)
function :: ast.package_decl(&tree, 2)
body :: ast.function_decl(&tree, function).body
for usize(1)..usize(6) |index| {
statement :: ast.block_statement(&tree, body, index)
assignment :: ast.assignment_stmt(&tree, statement)
_ = try expect_error(&result, &diagnostic_store, statement, .invalid_assignment_target, ast.node_span(&tree, assignment.target))
expected :: if (index == 4) TypeKind.function else TypeKind.int
try expect_kind(&result, assignment.value, expected)
if (index != 5) try testing.expect(type_of(&result, assignment.target).kind != .invalid)
}
unknown_statement :: ast.block_statement(&tree, body, 6)
unknown :: ast.assignment_stmt(&tree, unknown_statement)
target_error :: sema.binding(&resolution, unknown.target).data.diagnostic
value_error :: sema.binding(&resolution, unknown.value).data.diagnostic
try testing.expect(target_error != value_error)
try testing.expect_equal(target_error, type_of(&result, unknown.target).diagnostic)
try testing.expect_equal(target_error, type_of(&result, unknown_statement).diagnostic)
try testing.expect_equal(value_error, type_of(&result, unknown.value).diagnostic)
independent_statement :: ast.block_statement(&tree, body, 7)
independent :: ast.assignment_stmt(&tree, independent_statement)
_ = try expect_error(&result, &diagnostic_store, independent.value, .invalid_operand, ast.node_span(&tree, independent.value))
_ = try expect_error(&result, &diagnostic_store, independent_statement, .invalid_assignment_target, ast.node_span(&tree, independent.target))
try expect_kind(&result, ast.block_statement(&tree, body, 0), .int)
try expect_kind(&result, ast.function_param(&tree, function, 0), .int)
try expect_kind(&result, ast.package_decl(&tree, 0), .int)
try expect_kind(&result, ast.package_decl(&tree, 1), .function)
try testing.expect_equal(6, code_count(&diagnostic_store, .invalid_assignment_target))
try testing.expect_equal(2, code_count(&diagnostic_store, .unknown_name))
try testing.expect_equal(0, code_count(&diagnostic_store, .type_not_value))
}
function_assignment_checks_complete_signatures_without_retargeting_calls test {
strpool.strings = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.strings)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`first proc(a int) int { return a }
`same proc(renamed int) int { return renamed + 1 }
`wrong_parameter proc(a float) int { return 1 }
`wrong_result proc(a int) bool { return true }
`wrong_count proc() int { return 2 }
`main proc() int {
` chosen := first
` chosen = same
` chosen = wrong_parameter
` chosen = wrong_result
` chosen = wrong_count
` answer :: chosen(7)
` return answer
`}
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(mem.c_allocator, scan_state.tokens.items, source, &diagnostic_store)
defer ast.deinit(&tree)
resolution := try sema.resolve(mem.c_allocator, &tree, &diagnostic_store)
defer sema.deinit(&resolution)
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
result := try check(mem.c_allocator, &tree, &resolution, &diagnostic_store)
defer deinit(&result)
first :: ast.package_decl(&tree, 0)
body :: ast.function_decl(&tree, ast.package_decl(&tree, 5)).body
chosen :: ast.block_statement(&tree, body, 0)
try expect_kind(&result, chosen, .function)
try testing.expect_equal(first, type_of(&result, chosen).function)
try expect_kind(&result, ast.block_statement(&tree, body, 1), .void)
for usize(2)..usize(5) |index| {
statement :: ast.block_statement(&tree, body, index)
assignment :: ast.assignment_stmt(&tree, statement)
original :: try expect_error(&result, &diagnostic_store, assignment.value, .type_mismatch, ast.node_span(&tree, assignment.value))
try testing.expect_equal(original, type_of(&result, statement).diagnostic)
try expect_kind(&result, assignment.target, .function)
try testing.expect_equal(first, type_of(&result, assignment.target).function)
}
answer :: ast.block_statement(&tree, body, 5)
call :: ast.statement_decl(&tree, answer).initializer
try expect_kind(&result, answer, .int)
try expect_kind(&result, call, .int)
try testing.expect_equal(first, type_of(&result, ast.call_expr(&tree, call).callee).function)
try testing.expect_equal(3, code_count(&diagnostic_store, .type_mismatch))
try testing.expect_equal(0, code_count(&diagnostic_store, .argument_count_mismatch))
}
while_checks_nested_bodies_without_claiming_a_zero_iteration_return test {
strpool.strings = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.strings)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`missing proc() int { while true { return 1 } }
`bad_body proc() int { while false { -"bad" } }
`continues proc() int {
` while false { -"bad" }
` return 7
`}
`bad_condition proc() int { while 1 { -"bad" } }
`unknown_condition proc() int { while absent { missing_body } }
`nested proc() void {
` counter := 2
` while counter > 0 {
` inner := 1
` while inner > 0 {
` inner = inner - 1
` counter = counter - 1
` }
` }
`}
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(mem.c_allocator, scan_state.tokens.items, source, &diagnostic_store)
defer ast.deinit(&tree)
resolution := try sema.resolve(mem.c_allocator, &tree, &diagnostic_store)
defer sema.deinit(&resolution)
result := try check(mem.c_allocator, &tree, &resolution, &diagnostic_store)
defer deinit(&result)
for usize(0)..usize(2) |index| {
body :: ast.function_decl(&tree, ast.package_decl(&tree, index)).body
_ = try expect_error(&result, &diagnostic_store, body, .missing_return, ast.node_span(&tree, body))
try expect_kind(&result, ast.block_statement(&tree, body, 0), .void)
}
continues_body :: ast.function_decl(&tree, ast.package_decl(&tree, 2)).body
loop_statement :: ast.block_statement(&tree, continues_body, 0)
loop :: ast.while_stmt(&tree, loop_statement)
invalid_body :: ast.statement_expr(&tree, ast.block_statement(&tree, loop.body, 0))
original :: try expect_error(&result, &diagnostic_store, invalid_body, .invalid_operand, ast.node_span(&tree, invalid_body))
try testing.expect_equal(original, type_of(&result, loop.body).diagnostic)
try testing.expect_equal(original, type_of(&result, continues_body).diagnostic)
try expect_kind(&result, loop_statement, .void)
try expect_kind(&result, ast.block_statement(&tree, continues_body, 1), .void)
bad_condition_body :: ast.function_decl(&tree, ast.package_decl(&tree, 3)).body
bad_condition_statement :: ast.block_statement(&tree, bad_condition_body, 0)
condition :: ast.while_stmt(&tree, bad_condition_statement).condition
condition_error :: try expect_error(&result, &diagnostic_store, condition, .condition_not_bool, ast.node_span(&tree, condition))
try testing.expect_equal(condition_error, type_of(&result, bad_condition_statement).diagnostic)
unknown_body :: ast.function_decl(&tree, ast.package_decl(&tree, 4)).body
unknown_statement :: ast.block_statement(&tree, unknown_body, 0)
unknown :: ast.while_stmt(&tree, unknown_statement).condition
try testing.expect_equal(sema.binding(&resolution, unknown).data.diagnostic, type_of(&result, unknown_statement).diagnostic)
nested_body :: ast.function_decl(&tree, ast.package_decl(&tree, 5)).body
try expect_kind(&result, nested_body, .none)
outer_loop :: ast.while_stmt(&tree, ast.block_statement(&tree, nested_body, 1))
inner_loop :: ast.while_stmt(&tree, ast.block_statement(&tree, outer_loop.body, 1))
for usize(0)..usize(2) |index| {
statement :: ast.block_statement(&tree, inner_loop.body, index)
assignment :: ast.assignment_stmt(&tree, statement)
try expect_kind(&result, statement, .void)
try expect_kind(&result, assignment.target, .int)
try expect_kind(&result, assignment.value, .int)
}
try testing.expect_equal(2, code_count(&diagnostic_store, .missing_return))
try testing.expect_equal(1, code_count(&diagnostic_store, .condition_not_bool))
try testing.expect_equal(3, code_count(&diagnostic_store, .invalid_operand))
}