Skip to content

Commit

Permalink
ic: properly handle module references
Browse files Browse the repository at this point in the history
A module ID isn't necessarily the same before and after loading/storing
a rodfile. Loading and storing of `LibId`s now considers this.
  • Loading branch information
zerbina committed Jul 15, 2023
1 parent 6f3988a commit 2ce0fb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions compiler/ic/ic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ proc storeSym*(s: PSym; c: var PackedEncoder; m: var PackedModule): PackedItemId
p.typ = s.typ.storeType(c, m)
c.addMissing s.owner
p.owner = s.owner.safeItemId(c, m)
p.annex = s.annex
if s.annex.index != 0:
p.annex = (toLitId(s.annex.module.FileIndex, c, m), s.annex.index)

# fill the reserved slot, nothing else:
m.syms[s.itemId.item] = p
Expand Down Expand Up @@ -832,10 +833,15 @@ proc loadProcBody(c: var PackedDecoder; g: var PackedModuleGraph; thisModule: in
result = loadNodes(c, g, thisModule, tree, n0)
inc i

proc moduleIndex(c: var PackedDecoder; g: PackedModuleGraph; thisModule: int;
module: LitId): int32 {.inline.} =
## Returns the module ID for the stored ``FileIndex`` identified by `module`.
result = if module == LitId(0): thisModule.int32
else: toFileIndexCached(c, g, thisModule, module).int32

proc moduleIndex*(c: var PackedDecoder; g: PackedModuleGraph; thisModule: int;
s: PackedItemId): int32 {.inline.} =
result = if s.module == LitId(0): thisModule.int32
else: toFileIndexCached(c, g, thisModule, s.module).int32
result = moduleIndex(c, g, thisModule, s.module)

proc symHeaderFromPacked(c: var PackedDecoder; g: var PackedModuleGraph;
s: PackedSym; si, item: int32): PSym =
Expand Down Expand Up @@ -870,7 +876,10 @@ proc symBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph;
loadAstBodyLazy(s, ast)
else:
loadAstBody(s, ast)
result.annex = s.annex

if s.annex.index != 0:
result.annex = LibId(module: moduleIndex(c, g, si, s.annex.module),
index: s.annex.index)

if s.kind in {skLet, skVar, skField, skForVar}:
result.guard = loadSym(c, g, si, s.guard)
Expand Down
2 changes: 1 addition & 1 deletion compiler/ic/packed_ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type
offset*: int
externalName*: LitId # instead of TLoc
extFlags*: ExternalFlags
annex*: LibId
annex*: tuple[module: LitId, index: uint32]
constraint*: NodeId

PackedType* = object
Expand Down

0 comments on commit 2ce0fb9

Please sign in to comment.