From b5a0a729e6f80bf2f239602bb9d701291faecd42 Mon Sep 17 00:00:00 2001 From: bigmerge Date: Sat, 15 Aug 2026 21:54:10 -0500 Subject: [PATCH] codegen: Bool is int-like, so Bool comparisons stop lowering to str_eq MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fn check(label: String, cond: Bool, want: Bool) -> Void { if cond == want { ... } -> if (str_eq(cond, want)) SIGSEGV } Bool has always been an integer in the value model — type_to_c maps Bool to "int", and el_runtime.h states "Bool -> el_val_t (0 = false, nonzero = true)". But Bool names were registered NOWHERE: build_int_names_for_params tracked Int and Float params, and the `let` path tracked Int and Float bindings. Neither knew about Bool. So comparing two Bools fell through to str_eq, which dereferenced 0 or 1 as a char* and segfaulted immediately. This is the third instance of one family found tonight, after el #137 (a call on either side of == poisoned the operator) and el #136 (a missing import compiled clean). All three are the same shape: something the compiler could not type, silently handled as a string. Found while writing #137's own test harness — the first version of that harness crashed on exactly this, on both the old and new compiler, which is how it surfaced. A test harness that cannot compare two Bools is a good way to notice. VERIFIED: - the harness that segfaulted on every prior compiler (exit 139, no output) now runs clean: 14 passed, 0 failed - self-hosting fixpoint byte-identical - the compiler's own generated C differs by 8 lines — only the intended registration - neuron's full soul amalgam regenerates in 424ms, exit 0, BYTE-IDENTICAL - test_math 13/13, test_string 27/27, test_core 10/10, test_text 12/12 Adds tests/runtime/operator_typing_test.el, the 15-case suite from #137, so this family is covered going forward rather than rediscovered. --- lang/el-compiler/src/codegen.el | 14 +++++++++++ lang/tests/runtime/operator_typing_test.el | 28 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lang/tests/runtime/operator_typing_test.el diff --git a/lang/el-compiler/src/codegen.el b/lang/el-compiler/src/codegen.el index 1dd89ae..01f18c1 100644 --- a/lang/el-compiler/src/codegen.el +++ b/lang/el-compiler/src/codegen.el @@ -1510,6 +1510,11 @@ fn cg_stmt(stmt: Map, indent: String, declared: [String]) -> [Strin if str_eq(ltype, "Int") { add_int_name(name) } + // Same as params: Bool is an int in the value model. Without this a + // `let ok: Bool = ...` compared to another Bool lowered to str_eq. + if str_eq(ltype, "Bool") { + add_int_name(name) + } if str_eq(ltype, "Float") { add_float_name(name) } @@ -3127,6 +3132,15 @@ fn build_int_names_for_params(params: [Map]) -> Bool { if str_eq(ptype, "Int") { add_int_name(pname) } + // Bool is an integer in the value model (type_to_c maps Bool -> "int"; + // el_runtime.h: "Bool -> el_val_t (0 = false, nonzero = true)"), but + // Bool names were registered nowhere. So `cond == want` between two + // Bool params fell through to str_eq and dereferenced 0 or 1 as a + // char* — an immediate segfault. Track them as int-like, which is what + // they are. + if str_eq(ptype, "Bool") { + add_int_name(pname) + } if str_eq(ptype, "Float") { add_float_name(pname) } diff --git a/lang/tests/runtime/operator_typing_test.el b/lang/tests/runtime/operator_typing_test.el new file mode 100644 index 0000000..00ffe80 --- /dev/null +++ b/lang/tests/runtime/operator_typing_test.el @@ -0,0 +1,28 @@ +fn getstr(x: String) -> String { return x } +fn getint(x: Int) -> Int { return x } +fn ok(label: String) -> Void { println("ok " + label) } +fn bad(label: String) -> Void { println("FAIL " + label) } + +let s1: String = "hello" +let s2: String = "hello" +let s3: String = "world" +let i1: Int = 5 +let i2: Int = 5 +let i3: Int = 9 + +if "abc" == "abc" { ok("str literal eq") } else { bad("str literal eq") } +if "abc" == "xyz" { bad("str literal ne") } else { ok("str literal ne") } +if s1 == s2 { ok("str var eq") } else { bad("str var eq") } +if s1 == s3 { bad("str var ne") } else { ok("str var ne") } +if getstr("hi") == "hi" { ok("str call vs literal") } else { bad("str call vs literal") } +if s1 == getstr("hello") { ok("str var vs call") } else { bad("str var vs call") } +if s1 == getstr("nope") { bad("str var vs call ne") } else { ok("str var vs call ne") } +if i1 == i2 { ok("int var eq") } else { bad("int var eq") } +if i1 == i3 { bad("int var ne") } else { ok("int var ne") } +if getint(5) == i1 { ok("int call vs var") } else { bad("int call vs var") } +if getint(9) == i1 { bad("int call vs var ne") } else { ok("int call vs var ne") } +if s1 != s3 { ok("str NOTEQ") } else { bad("str NOTEQ") } +if s1 != s2 { bad("str NOTEQ same") } else { ok("str NOTEQ same") } +if i1 != i3 { ok("int NOTEQ") } else { bad("int NOTEQ") } +if getint(9) != i1 { ok("int call NOTEQ") } else { bad("int call NOTEQ") } +println("done")