From 2c8223f94e7c632f5fce72b4405e3141a8467dd4 Mon Sep 17 00:00:00 2001 From: dimgigov Date: Sun, 14 Jun 2026 17:23:58 +0300 Subject: [PATCH] fix(bootstrap): avoid emitting malformed Slice_T* typedefs - Strip pointer suffix when scanning for slice types in C backend - Skip slice typedefs already emitted from module.structs Fixes _test_slice --- bootstrap/lir_c_backend.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bootstrap/lir_c_backend.nim b/bootstrap/lir_c_backend.nim index 77cbbfd..a3883c5 100644 --- a/bootstrap/lir_c_backend.nim +++ b/bootstrap/lir_c_backend.nim @@ -2,7 +2,7 @@ ## Emits clean, well-structured C code from LIR instructions. ## Since LIR is already linear and low-level, C emission is straightforward. -import std/[strutils, strformat, tables, sequtils] +import std/[strutils, strformat, tables, sequtils, sets] import lir, hir, types, token type @@ -570,12 +570,18 @@ proc emitModule*(be: var LirCBackend, builder: LirBuilder, module: HirModule): s # Slice types (collect from functions/structs) # Simple: scan function params/returns for slice types var sliceTypes: seq[tuple[name: string, elem: string]] = @[] + var structNames: HashSet[string] + for s in module.structs: + structNames.incl(s.name) for f in module.funcs: for p in f.params: - let ct = typeToCStr(p.typ) + var ct = typeToCStr(p.typ) + # Strip pointer/reference suffix to find the base slice type. + while ct.endsWith("*"): + ct = ct[0..^2] if ct.startsWith("Slice_"): let elem = ct[6 .. ^1] - if not sliceTypes.anyIt(it.name == ct): + if not sliceTypes.anyIt(it.name == ct) and not structNames.contains(ct): sliceTypes.add((ct, elem)) if sliceTypes.len > 0: for st in sliceTypes: