This commit is contained in:
2026-08-01 13:10:36 +02:00
parent a8593c00f9
commit 304880a9aa
8 changed files with 1873350 additions and 998499 deletions
+127 -2
View File
@@ -10670,6 +10670,88 @@ parser_diagnoses_braceless_if_without_parens_or_call :: proc(t: ^testing.T) {
testing.expect(t, strings.contains(diagnostics.items[0].message, "parenthesized"))
}
@(test)
parser_accepts_braceless_while_bodies :: proc(t: ^testing.T) {
text := `ready func() bool { return false }
main func() void {
cursor i32 = 0
while (cursor < 1) cursor += 1
while ready() cursor += 1
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&module)
testing.expect_value(t, len(diagnostics.items), 0)
main := module.functions[1]
testing.expect_value(t, len(main.body), 3)
parenthesized := module.statements[main.body[1]]
testing.expect_value(t, parenthesized.kind, ast.Stmt_Kind.While)
testing.expect_value(t, len(parenthesized.body), 1)
testing.expect(t, module.exprs[parenthesized.expr].parenthesized)
call := module.statements[main.body[2]]
testing.expect_value(t, call.kind, ast.Stmt_Kind.While)
testing.expect_value(t, len(call.body), 1)
testing.expect_value(t, module.exprs[call.expr].kind, ast.Expr_Kind.Call)
}
@(test)
parser_diagnoses_braceless_while_without_parens_or_call :: proc(t: ^testing.T) {
text := `main func() void {
cursor i32 = 0
limit i32 = 1
while cursor < limit cursor += 1
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&module)
testing.expect_value(t, len(diagnostics.items), 1)
testing.expect_value(
t,
diagnostics.items[0].message,
"a brace-less 'while' body requires the condition to be parenthesized unless it is a function call",
)
}
@(test)
braceless_while_compiles_and_runs :: proc(t: ^testing.T) {
directory := "/tmp/brolang-test-braceless-while"
main_path := "/tmp/brolang-test-braceless-while/main.bro"
output := "/tmp/brolang-test-braceless-while-output"
text := `main func() i32 {
cursor i32 = 0
while (cursor < 42) cursor += 1
return cursor
}
`
_ = os2.remove_all(directory)
defer _ = os2.remove_all(directory)
defer _ = os.remove(output)
testing.expect(t, os.make_directory(directory) == nil)
testing.expect(t, os.write_entire_file(main_path, transmute([]byte)text))
status := compiler_core.compile_package(directory, output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 42)
}
@(test)
parser_diagnoses_braceless_for_and_unwrap_without_parens_or_call :: proc(t: ^testing.T) {
text := `main func() void {
@@ -11179,6 +11261,49 @@ for_loop_tokens_and_parser_capture_range_shape :: proc(t: ^testing.T) {
testing.expect_value(t, module.exprs[third.expr].integer, u64(1))
}
@(test)
for_loop_sink_captures_are_throwaways :: proc(t: ^testing.T) {
text := `main func() void {
for 0..1 |_| {}
for [1] |_, _| {}
for 0..1 |unused_capture| {}
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
ast_module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
defer hir.destroy_module(&hir_module)
ir_module := lower.lower(&hir_module)
defer ir.destroy_module(&ir_module)
found_unused_capture := false
warning_count := 0
error_count := 0
for diagnostic in diagnostics.items {
if diagnostic.severity == source.Severity.Warning {
warning_count += 1
} else {
error_count += 1
}
found_unused_capture =
found_unused_capture ||
strings.contains(diagnostic.message, "unused local 'unused_capture'")
testing.expect(t, !strings.contains(diagnostic.message, "unused local '_'"))
testing.expect(t, !strings.contains(diagnostic.message, "distinct names"))
testing.expect(t, !strings.contains(diagnostic.message, "expected a for-loop"))
}
testing.expect_value(t, warning_count, 1)
testing.expect_value(t, error_count, 0)
testing.expect(t, found_unused_capture)
}
@(test)
parser_accepts_braceless_for_bodies :: proc(t: ^testing.T) {
text := `make_items func() [2]i32 { return [1, 2] }
@@ -14956,12 +15081,12 @@ main func() i32 {
})
if !map.present[0] or !map.present[1] or map.present[2] { return 9 }
if map.values[0].len != 10 or map.values[1].len != 7 { return 18 }
ident :: get(TokenKind, []u8, &map, TokenKind.ident)
ident :: get(&map, TokenKind.ident)
if ident == null or null == ident { return 19 }
if ident |value| {
if value.len != 10 { return 19 }
} else { return 20 }
eof :: get(TokenKind, []u8, &map, TokenKind.eof)
eof :: get(&map, TokenKind.eof)
if eof != null or null != eof { return 21 }
location testing.SourceLocation = {file = "test.bro", line = 1, column = 1}
testing.expect_equal(null, eof, location) catch |_| { return 24 }