package-type imports

This commit is contained in:
2026-06-10 00:08:33 +02:00
parent 087fdb45d5
commit d6e03b6f08
104 changed files with 1799 additions and 273 deletions
+622 -26
View File
@@ -7,12 +7,14 @@ import "./compiler/checker"
import "./compiler/hir"
import "./compiler/ir"
import "./compiler/lexer"
import "./compiler/loader"
import "./compiler/llvm"
import "./compiler/lower"
import "./compiler/parser"
import "./compiler/source"
import "./compiler/token"
import "./compiler/types"
import "core:fmt"
import "core:os"
import "core:os/os2"
import "core:strings"
@@ -53,6 +55,153 @@ main :: func() void {}
testing.expect_value(t, len(module.functions[0].params), 2)
}
@(test)
parser_accepts_single_statement_one_line_functions :: proc(t: ^testing.T) {
text := `give :: func() i8 { return 7 }
main :: func() void { _ = give() }
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&module)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, len(module.functions), 2)
testing.expect_value(t, len(module.functions[0].body), 1)
testing.expect_value(t, len(module.functions[1].body), 1)
testing.expect_value(t, module.statements[module.functions[0].body[0]].kind, ast.Stmt_Kind.Return)
testing.expect_value(t, module.statements[module.functions[1].body[0]].kind, ast.Stmt_Kind.Assignment)
}
@(test)
parser_accepts_bare_and_aliased_imports :: proc(t: ^testing.T) {
text := `import
"../math"
other :: import "../math"
escaped :: import "dir\"name\\tail"
main :: func() void {}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&module)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, len(module.imports), 3)
testing.expect_value(t, module.imports[0].alias, "")
testing.expect_value(t, module.imports[0].path, "../math")
testing.expect_value(t, module.imports[1].alias, "other")
testing.expect_value(t, module.imports[2].path, "dir\"name\\tail")
}
@(test)
parser_rejects_chained_package_access :: proc(t: ^testing.T) {
text := `main :: func() void {
_ = first.second.value
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&module)
testing.expect(t, len(diagnostics.items) > 0)
}
@(test)
lexer_diagnoses_invalid_import_strings :: proc(t: ^testing.T) {
text := "import \"bad\\q\"\nimport \"unterminated\n"
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
testing.expect_value(t, len(diagnostics.items), 2)
}
@(test)
package_loader_discovers_lexical_immediate_bro_files :: proc(t: ^testing.T) {
sources := source.init_store()
defer source.destroy_store(&sources)
diagnostics := source.init_store_diagnostics(&sources)
defer source.destroy_diagnostics(&diagnostics)
module, loaded := loader.load("examples/packages/basic/app", &sources, &diagnostics)
defer ast.destroy_module(&module)
testing.expect(t, loaded)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, len(module.packages), 2)
testing.expect_value(t, len(module.files), 3)
testing.expect(t, strings.has_suffix(sources.items[module.files[0].source].path, "/main.bro"))
testing.expect(t, strings.has_suffix(sources.items[module.files[1].source].path, "/value.bro"))
testing.expect(t, strings.has_suffix(sources.items[module.files[2].source].path, "/math.bro"))
}
@(test)
multi_source_diagnostics_report_the_originating_file :: proc(t: ^testing.T) {
sources := source.init_store()
defer source.destroy_store(&sources)
diagnostics := source.init_store_diagnostics(&sources)
defer source.destroy_diagnostics(&diagnostics)
module, loaded := loader.load("examples/packages/file_local/app", &sources, &diagnostics)
defer ast.destroy_module(&module)
hir_module := checker.check(&module, &diagnostics)
defer hir.destroy_module(&hir_module)
testing.expect(t, loaded)
found := false
for _, diagnostic_id in diagnostics.items {
message := source.format(&diagnostics, diagnostic_id)
if strings.contains(message, "/b.bro:2:9:") &&
strings.contains(message, "unknown package alias 'math'") {
found = true
}
delete(message)
}
testing.expect(t, found)
}
@(test)
semantic_lookup_diagnoses_wrong_declaration_kinds :: proc(t: ^testing.T) {
text := `value :: 1
give :: func() i8 {
return 1
}
main :: func() void {
_ = value()
_ = give
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
ast_module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics)
defer hir.destroy_module(&hir_module)
found_global := false
found_function := false
for diagnostic in diagnostics.items {
found_global = found_global || strings.contains(diagnostic.message, "'value' is a global, not a function")
found_function = found_function || strings.contains(diagnostic.message, "'give' is a function, not a global value")
}
testing.expect(t, found_global)
testing.expect(t, found_function)
}
@(test)
pipeline_emits_specialized_calling_conventions_and_checked_add :: proc(t: ^testing.T) {
text := `sum_c :: c func(a, b int) int {
@@ -84,8 +233,8 @@ main :: func() void {
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, llvm_text, second_llvm_text)
testing.expect(t, strings.contains(llvm_text, "define i8 @sum_c__i8__i8"))
testing.expect(t, strings.contains(llvm_text, "define internal fastcc i8 @bro__sum_bro__i8__i8"))
testing.expect(t, strings.contains(llvm_text, "define i8 @bro_c__p0__sum_c__i8__i8"))
testing.expect(t, strings.contains(llvm_text, "define internal fastcc i8 @bro__p0__sum_bro__i8__i8"))
testing.expect(t, strings.contains(llvm_text, "@llvm.sadd.with.overflow.i8"))
}
@@ -123,7 +272,7 @@ main :: func() void {
defer delete(llvm_text)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect(t, strings.contains(llvm_text, "@bro__take_int__i16"))
testing.expect(t, strings.contains(llvm_text, "@bro__p0__take_int__i16"))
for function in ir_module.functions {
for instruction in function.instructions {
testing.expect(t, instruction.op != ir.Opcode.Add_Checked)
@@ -272,6 +421,92 @@ main :: func() void {
testing.expect_value(t, len(hir_module.functions), 3)
}
@(test)
long_generic_call_chain_reaches_a_fixed_point :: proc(t: ^testing.T) {
builder := strings.builder_make()
defer strings.builder_destroy(&builder)
for index in 0 ..< 70 {
fmt.sbprintf(&builder, "f%d :: func(value int) int ", index)
strings.write_string(&builder, "{ return ")
if index == 69 {
strings.write_string(&builder, "value")
} else {
fmt.sbprintf(&builder, "f%d(value)", index+1)
}
strings.write_string(&builder, " }\n")
}
strings.write_string(&builder, "main :: func() i32 { return f0(1) }\n")
source_file := source.Source{path="test.bro", text=strings.to_string(builder)}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
ast_module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics)
defer hir.destroy_module(&hir_module)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, len(hir_module.functions), 71)
}
@(test)
globals_and_generic_results_reach_a_shared_fixed_point :: proc(t: ^testing.T) {
text := `derived :: identity(base)
base :: make()
identity :: func(value int) int {
return value
}
make :: func() int {
return 1
}
main :: func() i32 {
return derived
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
ast_module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics)
defer hir.destroy_module(&hir_module)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, len(hir_module.functions), 3)
for global in hir_module.globals {
testing.expect(t, types.equal(global.type, types.I8))
}
}
@(test)
unused_function_signatures_are_validated_eagerly :: proc(t: ^testing.T) {
text := `broken :: func(value, value i8, nope void) void {}
main :: func() void {}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
stream := lexer.lex(&source_file, &diagnostics)
defer delete(stream.items)
ast_module := parser.parse(&stream, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics)
defer hir.destroy_module(&hir_module)
found_duplicate := false
found_void := false
for diagnostic in diagnostics.items {
found_duplicate = found_duplicate || strings.contains(diagnostic.message, "duplicate parameter 'value'")
found_void = found_void || strings.contains(diagnostic.message, "void is only valid as a function result type")
}
testing.expect(t, found_duplicate)
testing.expect(t, found_void)
}
run_executable :: proc(path: string) -> os2.Process_State {
state, stdout, stderr, _ := os2.process_exec(
os2.Process_Desc{command=[]string{path}},
@@ -286,17 +521,27 @@ run_executable :: proc(path: string) -> os2.Process_State {
valid_program_compiles_and_runs :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-valid"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/prototype.bro", output)
status := compiler_core.compile_package("examples/programs/prototype", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
one_line_main_compiles_and_runs :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-one-line-main"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/programs/one_line", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 7)
}
@(test)
folded_constant_addition_compiles_and_runs :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-constant-fold"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/constant_fold.bro", output)
status := compiler_core.compile_package("examples/programs/constant_fold", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -306,7 +551,7 @@ folded_constant_addition_compiles_and_runs :: proc(t: ^testing.T) {
unused_invalid_global_does_not_trap :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-invalid-unused"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/invalid_unused_global.bro", output)
status := compiler_core.compile_package("examples/programs/invalid_unused_global", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -316,7 +561,7 @@ unused_invalid_global_does_not_trap :: proc(t: ^testing.T) {
used_invalid_global_traps :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-invalid-used"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/invalid_used_global.bro", output)
status := compiler_core.compile_package("examples/programs/invalid_used_global", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -326,7 +571,7 @@ used_invalid_global_traps :: proc(t: ^testing.T) {
main_int_is_constrained_to_i32 :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-main-int"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/main_int.bro", output)
status := compiler_core.compile_package("examples/programs/main_int", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 3)
@@ -336,7 +581,7 @@ main_int_is_constrained_to_i32 :: proc(t: ^testing.T) {
main_i32_returns_directly :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-main-i32"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/main_i32.bro", output)
status := compiler_core.compile_package("examples/programs/main_i32", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 4)
@@ -346,7 +591,7 @@ main_i32_returns_directly :: proc(t: ^testing.T) {
transitive_problematic_global_is_deferred :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-transitive-unused"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/invalid_transitive_unused_global.bro", output)
status := compiler_core.compile_package("examples/programs/invalid_transitive_unused_global", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -356,7 +601,7 @@ transitive_problematic_global_is_deferred :: proc(t: ^testing.T) {
checked_addition_traps_on_overflow :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-overflow"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/overflow.bro", output)
status := compiler_core.compile_package("examples/programs/overflow", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -366,7 +611,7 @@ checked_addition_traps_on_overflow :: proc(t: ^testing.T) {
constant_that_does_not_fit_context_produces_trap_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-constant-context-error"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/constant_context_error.bro", output)
status := compiler_core.compile_package("examples/programs/constant_context_error", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -376,7 +621,7 @@ constant_that_does_not_fit_context_produces_trap_executable :: proc(t: ^testing.
constant_beyond_i64_produces_trap_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-constant-i64-overflow"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/constant_i64_overflow.bro", output)
status := compiler_core.compile_package("examples/programs/constant_i64_overflow", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -384,7 +629,7 @@ constant_beyond_i64_produces_trap_executable :: proc(t: ^testing.T) {
@(test)
same_line_statements_are_diagnosed :: proc(t: ^testing.T) {
text := "main :: func() void { _ = 1 _ = 2\n}\n"
text := "main :: func() void { _ = 1 _ = 2 }\n"
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
@@ -399,7 +644,7 @@ same_line_statements_are_diagnosed :: proc(t: ^testing.T) {
missing_main_produces_trap_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-missing-main"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/missing_main.bro", output)
status := compiler_core.compile_package("examples/programs/missing_main", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -422,7 +667,7 @@ backend_failure_preserves_existing_output :: proc(t: ^testing.T) {
mutable_local_reassignment_uses_runtime_storage :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-mutable"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/mutable_local.bro", output)
status := compiler_core.compile_package("examples/programs/mutable_local", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 3)
@@ -432,7 +677,7 @@ mutable_local_reassignment_uses_runtime_storage :: proc(t: ^testing.T) {
implicit_narrowing_produces_trap_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-narrowing"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/narrowing_error.bro", output)
status := compiler_core.compile_package("examples/programs/narrowing_error", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -442,7 +687,7 @@ implicit_narrowing_produces_trap_executable :: proc(t: ^testing.T) {
valid_runtime_global_initializes_before_main :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-runtime-global"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/runtime_global.bro", output)
status := compiler_core.compile_package("examples/programs/runtime_global", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -452,7 +697,7 @@ valid_runtime_global_initializes_before_main :: proc(t: ^testing.T) {
unused_global_cycle_is_deferred :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-cycle-unused"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/cycle_unused.bro", output)
status := compiler_core.compile_package("examples/programs/cycle_unused", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -462,7 +707,7 @@ unused_global_cycle_is_deferred :: proc(t: ^testing.T) {
used_global_cycle_traps :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-cycle-used"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/cycle_used.bro", output)
status := compiler_core.compile_package("examples/programs/cycle_used", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
@@ -472,27 +717,37 @@ used_global_cycle_traps :: proc(t: ^testing.T) {
malformed_typed_values_still_produce_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-malformed-typed"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/malformed_typed_recovery.bro", output)
status := compiler_core.compile_package("examples/programs/malformed_typed_recovery", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
unused_invalid_function_is_diagnosed_but_not_reached :: proc(t: ^testing.T) {
unused_invalid_function_body_is_not_checked :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-invalid-unused-function"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/invalid_unused_function.bro", output)
testing.expect_value(t, status, 1)
status := compiler_core.compile_package("examples/programs/invalid_unused_function", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
used_invalid_function_is_diagnosed_and_traps :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-invalid-used-function"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/programs/invalid_used_function", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
function_mediated_problematic_global_is_deferred :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-function-global-unused"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/function_global_unused.bro", output)
status := compiler_core.compile_package("examples/programs/function_global_unused", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
@@ -502,8 +757,349 @@ function_mediated_problematic_global_is_deferred :: proc(t: ^testing.T) {
function_mediated_problematic_global_traps_when_used :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-function-global-used"
defer _ = os.remove(output)
status := compiler_core.compile_file("examples/function_global_used.bro", output)
status := compiler_core.compile_package("examples/programs/function_global_used", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
package_files_merge_and_qualified_imports_run :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-basic"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/basic/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 3)
}
@(test)
unused_import_is_diagnosed_but_remains_executable :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-unused"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/unused/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
unused_missing_package_does_not_trap :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-missing-unused"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/missing_unused/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
referenced_missing_package_traps_at_reference :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-missing-used"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/missing_used/app", output)
testing.expect_value(t, status, 1)
state, stdout, stderr, _ := os2.process_exec(
os2.Process_Desc{command=[]string{output}},
context.allocator,
)
defer delete(stdout)
defer delete(stderr)
testing.expect(t, !state.success)
testing.expect(t, strings.contains(string(stderr), "/missing_used/app/main.bro:4:6:"))
}
@(test)
imports_are_file_local :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-file-local"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/file_local/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
package_import_cycles_are_valid :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-cycle"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/cycle/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 4)
}
@(test)
imported_main_is_an_ordinary_callable_function :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-imported-main"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/imported_main/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 7)
}
@(test)
same_named_c_functions_in_packages_do_not_collide :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-c-symbols"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/c_symbols/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
invalid_root_package_preserves_existing_output :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-preserved-output"
defer _ = os.remove(output)
previous := "previous artifact"
testing.expect(t, os.write_entire_file(output, transmute([]byte)previous))
testing.expect_value(t, compiler_core.compile_package("examples/programs/prototype/main.bro", output), 2)
data, ok := os.read_entire_file(output)
defer delete(data)
testing.expect(t, ok)
testing.expect_value(t, string(data), previous)
}
@(test)
empty_root_package_is_an_infrastructure_error :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-empty"
defer _ = os.remove(output)
testing.expect_value(t, compiler_core.compile_package("examples/packages/empty", output), 2)
testing.expect(t, !os.exists(output))
}
@(test)
same_package_can_be_imported_under_distinct_aliases :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-aliases"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/aliases/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 4)
}
@(test)
duplicate_same_file_import_keeps_first_binding :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-duplicate"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/duplicate/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 2)
}
@(test)
identical_imports_in_different_files_are_independent :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-repeated"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/repeated/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 4)
}
@(test)
imports_do_not_reexport_members :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-non-transitive"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/non_transitive/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
self_import_via_dot_is_valid :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-self"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/self_import/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 5)
}
@(test)
unused_absolute_import_is_diagnosed_without_trapping :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-absolute"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/absolute/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
unreferenced_problematic_imported_global_is_deferred :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-problematic-unused"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/problematic_unused/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
valid_eager_global_in_unused_package_still_initializes :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-eager-unused"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/eager_unused/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
cross_package_global_initialization_cycle_traps_when_used :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-global-cycle"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/global_cycle/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
cross_package_generic_specializes_from_folded_argument :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-generic"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/generic/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 128)
}
@(test)
lazy_function_body_marks_import_as_used_without_resolving_it :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-lazy-import"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/lazy_import/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
qualified_global_inference_ignores_same_named_local :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-qualified-shadow"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/qualified_shadow/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 300)
}
@(test)
cross_package_recursive_specialization_reaches_fixed_point :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-recursive"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/recursive/app", output)
testing.expect_value(t, status, 0)
testing.expect(t, os.exists(output))
}
@(test)
imported_main_does_not_satisfy_root_main_requirement :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-missing-root-main"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/missing_root_main/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect(t, !state.success)
}
@(test)
package_llvm_is_deterministic_and_symbols_include_package_ids :: proc(t: ^testing.T) {
sources := source.init_store()
defer source.destroy_store(&sources)
diagnostics := source.init_store_diagnostics(&sources)
defer source.destroy_diagnostics(&diagnostics)
ast_module, loaded := loader.load("examples/packages/c_symbols/app", &sources, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics)
defer hir.destroy_module(&hir_module)
ir_module := lower.lower(&hir_module)
defer ir.destroy_module(&ir_module)
llvm_text := llvm.emit(&ir_module, &diagnostics)
defer delete(llvm_text)
second_llvm_text := llvm.emit(&ir_module, &diagnostics)
defer delete(second_llvm_text)
testing.expect(t, loaded)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, llvm_text, second_llvm_text)
testing.expect(t, strings.contains(llvm_text, "define i8 @bro_c__p1__same"))
testing.expect(t, strings.contains(llvm_text, "define i8 @bro_c__p2__same"))
testing.expect(t, strings.contains(llvm_text, "define i32 @main()"))
}
@(test)
invalid_default_alias_requires_an_explicit_alias :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-default-alias-invalid"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/default_alias_invalid/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
explicit_alias_allows_invalid_directory_basename :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-explicit-alias"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/explicit_alias/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 3)
}
@(test)
import_alias_conflict_is_diagnosed_without_trapping :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-alias-conflict"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/alias_conflict/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 7)
}
@(test)
empty_imported_package_is_a_source_diagnostic :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-empty-import"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/empty_import/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
non_directory_import_is_a_source_diagnostic :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-file-import"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/file_import/app", output)
testing.expect_value(t, status, 1)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
import_is_available_before_its_textual_declaration :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-import-order"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/import_order/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 6)
}
@(test)
deeply_nested_child_packages_load_recursively :: proc(t: ^testing.T) {
output := "/tmp/brolang-test-package-deep"
defer _ = os.remove(output)
status := compiler_core.compile_package("examples/packages/deep/app", output)
testing.expect_value(t, status, 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 9)
}