feat: generic struct monomorphization + tkUnknown fix

- Add generic struct instantiation (Box<int>, Pair<T,U>) in HIR lowering
- Fix tkUnknown/tkNamed/tkTypeParam ambiguity in sema.nim (qualify with TypeKind)
- Add generics_struct example
- Update Makefile with new example
This commit is contained in:
2026-05-31 10:33:08 +03:00
parent 7ee6a73ea4
commit 9f733aca7d
9 changed files with 136 additions and 15 deletions
+12 -3
View File
@@ -503,7 +503,7 @@ proc parsePostfix(p: var Parser): Expr =
discard p.advance()
left = Expr(kind: ekTry, loc: loc, exprTryOperand: left, exprTryType: nil)
of tkLBrace:
if p.structInitAllowed and left.kind in {ekIdent, ekPath}:
if p.structInitAllowed and left.kind in {ekIdent, ekPath, ekGenericCall}:
discard p.advance()
var fields: seq[tuple[name: string, value: Expr]] = @[]
while not p.check(tkRBrace) and not p.isAtEnd:
@@ -514,8 +514,17 @@ proc parsePostfix(p: var Parser): Expr =
if p.check(tkComma):
discard p.advance()
discard p.expect(tkRBrace, "expected '}'")
let typeName = if left.kind == ekIdent: left.exprIdent else: left.exprPath.join("::")
left = Expr(kind: ekStructInit, loc: loc, exprStructInitName: typeName, exprStructInitFields: fields)
var typeName = ""
var typeArgs: seq[TypeExpr] = @[]
if left.kind == ekIdent:
typeName = left.exprIdent
elif left.kind == ekPath:
typeName = left.exprPath.join("::")
elif left.kind == ekGenericCall:
typeName = left.exprGenericCallee
typeArgs = left.exprGenericTypeArgs
left = Expr(kind: ekStructInit, loc: loc, exprStructInitName: typeName,
exprStructInitTypeArgs: typeArgs, exprStructInitFields: fields)
else:
break
else: