Skip to content

Commit

Permalink
declare result as noinit if routine is noinit (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn authored Jan 17, 2025
1 parent bf748a3 commit e681e80
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/nimony/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type
kind*: SymKind
inGeneric*, inLoop*, inBlock*: int
returnType*: TypeCursor
pragmas*: set[PragmaKind]
resId*: SymId
parent*: SemRoutine

Expand Down
14 changes: 14 additions & 0 deletions tests/nimony/sysbasics/tresultnoinit.nif
Original file line number Diff line number Diff line change
@@ -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))))
1 change: 1 addition & 0 deletions tests/nimony/sysbasics/tresultnoinit.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
proc foo(): int {.noinit.} = discard

0 comments on commit e681e80

Please sign in to comment.