From e681e80c6309a1d5a3af443567e0d522e28d0ba0 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 17 Jan 2025 14:31:25 +0300 Subject: [PATCH] declare result as noinit if routine is noinit (#374) --- src/nimony/sem.nim | 10 ++++++++-- src/nimony/semdata.nim | 1 + tests/nimony/sysbasics/tresultnoinit.nif | 14 ++++++++++++++ tests/nimony/sysbasics/tresultnoinit.nim | 1 + 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/nimony/sysbasics/tresultnoinit.nif create mode 100644 tests/nimony/sysbasics/tresultnoinit.nim diff --git a/src/nimony/sem.nim b/src/nimony/sem.nim index 6c8eff66..54c42ebc 100644 --- a/src/nimony/sem.nim +++ b/src/nimony/sem.nim @@ -369,8 +369,13 @@ proc declareResult(c: var SemContext; info: PackedLineInfo): SymId = buildTree c.dest, ResultS, info: c.dest.add symdefToken(result, info) # name c.dest.addDotToken() # export marker - c.dest.addDotToken() # pragmas - # XXX ^ pragma should be `.noinit` if the proc decl has it + if NoInit in c.routine.pragmas: + c.dest.add parLeToken(PragmasS, info) + c.dest.add parLeToken(NoInit, info) + c.dest.addParRi() + c.dest.addParRi() + else: + c.dest.addDotToken() # pragmas c.dest.copyTree(c.routine.returnType) # type c.dest.addDotToken() # value publish c, result, declStart @@ -2837,6 +2842,7 @@ proc semProc(c: var SemContext; it: var Item; kind: SymKind; pass: PassKind) = c.routine.returnType = semReturnType(c, it.n) var crucial = default CrucialPragma semPragmas c, it.n, crucial, kind + c.routine.pragmas = crucial.flags if crucial.hasVarargs.isValid: addVarargsParameter c, beforeParams, crucial.hasVarargs if crucial.magic.len > 0: diff --git a/src/nimony/semdata.nim b/src/nimony/semdata.nim index bf9b9d00..ad18df53 100644 --- a/src/nimony/semdata.nim +++ b/src/nimony/semdata.nim @@ -19,6 +19,7 @@ type kind*: SymKind inGeneric*, inLoop*, inBlock*: int returnType*: TypeCursor + pragmas*: set[PragmaKind] resId*: SymId parent*: SemRoutine diff --git a/tests/nimony/sysbasics/tresultnoinit.nif b/tests/nimony/sysbasics/tresultnoinit.nif new file mode 100644 index 00000000..044ec8d1 --- /dev/null +++ b/tests/nimony/sysbasics/tresultnoinit.nif @@ -0,0 +1,14 @@ +(.nif24) +,1,tests/nimony/sysbasics/tresultnoinit.nim(stmts + (proc 5 :foo.0.tremd49gb1 . . . 8 + (params) 4,6,lib/std/system.nim + (i -1) 16 + (pragmas 2 + (noinit)) . 29 + (stmts + (result :result.0 . + (pragmas + (noinit)) ~25,6,lib/std/system.nim + (i -1) .) + (discard .) ~29 + (ret result.0)))) \ No newline at end of file diff --git a/tests/nimony/sysbasics/tresultnoinit.nim b/tests/nimony/sysbasics/tresultnoinit.nim new file mode 100644 index 00000000..fcfbeaac --- /dev/null +++ b/tests/nimony/sysbasics/tresultnoinit.nim @@ -0,0 +1 @@ +proc foo(): int {.noinit.} = discard