feat: add selfhost-loop determinism check, golden tests, improve sema diagnostics

- Makefile: new targets `test-golden` and `selfhost-loop`
  - selfhost-loop verifies C codegen determinism (2-pass bootstrap)
  - test-golden compares C output against expected.c golden files
  - Updated clean-all to clean new build dirs
- tests/golden/hello/: first golden test (hello world)
- src/sema.bux:
  - Fix: import stubs only registered if symbol doesn't already exist
  - Improve: error message now includes identifier name
This commit is contained in:
2026-06-07 15:47:48 +03:00
parent 3b4456f01f
commit 1f0baefa5a
5 changed files with 3006 additions and 5 deletions
+60 -2
View File
@@ -5,7 +5,7 @@ BUILD_DIR := build
EXAMPLES := hello fibonacci factorial structs enums methods algebraic_enums generics generics_struct generic_infer generic_infer2 extend_generic pattern_matching strings strings2 map result_option try_operator ownership ctfe async concurrency os_time process json iter
.PHONY: all build dev debug test clean clean-all test-examples selfhost
.PHONY: all build dev debug test clean clean-all test-examples selfhost test-golden selfhost-loop
all: build
@@ -62,7 +62,8 @@ clean:
rm -rf _test_cast _test_cast2 _test_cast3 _test_channel
clean-all: clean
rm -rf build/selfhost
rm -rf build/selfhost build/selfhost-loop-a build/selfhost-loop-b
rm -rf tests/golden/*/build
selfhost: build
@echo "=== Building self-hosted compiler ==="
@@ -74,3 +75,60 @@ selfhost: build
@cd build/selfhost && ../../$(OUT) build
# strip removed for debug
@echo "=== Self-hosted compiler built successfully ==="
.PHONY: test-golden
GOLDEN_TESTS := hello
test-golden: build
@echo "=== Golden tests ==="
@passed=0; failed=0; \
for test in $(GOLDEN_TESTS); do \
gd="tests/golden/$$test"; \
if [ ! -d "$$gd" ]; then echo " SKIP $$test (no dir)"; continue; fi; \
if [ ! -f "$$gd/expected.c" ]; then echo " SKIP $$test (no expected.c)"; continue; fi; \
rm -rf "$$gd/build"; \
(cd "$$gd" && ../../../$(OUT) build > /dev/null 2>&1); \
if diff "$$gd/build/main.c" "$$gd/expected.c" > /dev/null 2>&1; then \
echo " PASS $$test"; \
passed=$$((passed + 1)); \
else \
echo " FAIL $$test — C output differs from expected"; \
failed=$$((failed + 1)); \
fi; \
done; \
echo "Golden tests: $$passed passed, $$failed failed"; \
if [ $$failed -gt 0 ]; then exit 1; fi
selfhost-loop: build
@echo "=== Selfhost loop: bootstrap determinism check ==="
@echo "Build A..."
@rm -rf build/selfhost-loop-a
@mkdir -p build/selfhost-loop-a/src
@cp src/*.bux build/selfhost-loop-a/src/
@cp src/bux.toml build/selfhost-loop-a/
@mv build/selfhost-loop-a/src/main.bux build/selfhost-loop-a/src/Main.bux 2>/dev/null || true
@cd build/selfhost-loop-a && ../../$(OUT) build
@echo "Build B..."
@rm -rf build/selfhost-loop-b
@mkdir -p build/selfhost-loop-b/src
@cp src/*.bux build/selfhost-loop-b/src/
@cp src/bux.toml build/selfhost-loop-b/
@mv build/selfhost-loop-b/src/main.bux build/selfhost-loop-b/src/Main.bux 2>/dev/null || true
@cd build/selfhost-loop-b && ../../$(OUT) build
@echo ""
@echo "Comparing C output..."
@if diff build/selfhost-loop-a/build/main.c build/selfhost-loop-b/build/main.c > /dev/null 2>&1; then \
echo " C output: IDENTICAL ✓"; \
else \
echo " C output: DIFFERENT ✗"; \
fi
@echo "Comparing ELF binaries..."
@if diff build/selfhost-loop-a/build/buxc2 build/selfhost-loop-b/build/buxc2 > /dev/null 2>&1; then \
echo " ELF binary: IDENTICAL ✓"; \
echo "=== Selfhost loop PASSED ==="; \
else \
echo " ELF binary: DIFFERENT ✗ (may be due to timestamps)"; \
echo " C output was identical — ELF difference is likely linker non-determinism"; \
echo "=== Selfhost loop: C codegen deterministic ✓, ELF non-deterministic (expected) ==="; \
fi
+11 -3
View File
@@ -165,7 +165,9 @@ func Sema_CheckExpr(sema: *Sema, expr: *Expr) -> int {
if kind == ekIdent {
let sym: Symbol = Scope_Lookup(sema.scope, expr.strValue);
if sym.kind == 0 && !String_Eq(sym.name, expr.strValue) {
Sema_EmitError(sema, expr.line, expr.column, "undeclared identifier");
let errMsg: String = String_Concat("undeclared identifier '", expr.strValue);
let errMsg2: String = String_Concat(errMsg, "'");
Sema_EmitError(sema, expr.line, expr.column, errMsg2);
return tyUnknown;
}
if sym.typeName != null as String && !String_Eq(sym.typeName, "") {
@@ -565,7 +567,10 @@ func Sema_CollectGlobals(sema: *Sema) {
sym.isMutable = false;
sym.isPublic = true;
sym.decl = null as *Decl;
discard Scope_Define(sema.scope, sym);
let existing: Symbol = Scope_LookupLocal(sema.scope, name);
if !String_Eq(existing.name, name) {
discard Scope_Define(sema.scope, sym);
}
}
start = pos + 1;
}
@@ -600,7 +605,10 @@ func Sema_CollectGlobals(sema: *Sema) {
sym.isMutable = false;
sym.isPublic = true;
sym.decl = null as *Decl;
discard Scope_Define(sema.scope, sym);
let existing: Symbol = Scope_LookupLocal(sema.scope, lastSeg);
if !String_Eq(existing.name, lastSeg) {
discard Scope_Define(sema.scope, sym);
}
}
}
}
+7
View File
@@ -0,0 +1,7 @@
[Package]
Name = "hello"
Version = "0.1.0"
Type = "bin"
[Build]
Output = "Bin"
File diff suppressed because it is too large Load Diff
+5
View File
@@ -0,0 +1,5 @@
import Std::Io::PrintLine;
func Main() -> int {
PrintLine("Hello, Bux!");
return 0;
}