Skip to content

Commit 556e7c6

Browse files
committed
makes using the type defined in type section in var section works
1 parent 10722d9 commit 556e7c6

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,11 @@ proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
106106
result[0] = newNodeI(nkIdentDefs, unknownLineInfo, 3)
107107
inc n
108108
result[0][0] = fromNifSymDef(c, n, kind)
109+
result[0][1] = newNode(nkEmpty)
109110
if n.kind == DotToken:
110-
result[0][1] = newNode(nkEmpty)
111111
inc n
112112
else:
113-
let typeSym = fromNifType(c, n).sym
114-
assert typeSym != nil
115-
result[0][1] = typeSym.newSymNode
116-
result[0][0].sym.typ = typeSym.typ
113+
result[0][0].sym.typ = fromNifType(c, n)
117114
result[0][2] = fromNif(c, n)
118115
assert n.kind == ParRi # nkIdentDefs
119116
inc n
@@ -142,8 +139,8 @@ proc fromNifTypeSection(c: var DecodeContext; n: var Cursor): PNode =
142139
result[1] = fromNif(c, n)
143140

144141
# type body
145-
result[2] = fromNifType(c, n).sym.newSymNode
146-
sym.sym.typ = result[2].sym.typ
142+
result[2] = newNode(nkEmpty)
143+
sym.sym.typ = fromNifType(c, n)
147144

148145
assert n.kind == ParRi
149146
inc n

compiler/icnif/nifdecodertypes.nim

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,18 @@ proc fromNifType(c: var DecodeContext; n: var Cursor): PType =
9696
of Symbol:
9797
let s = n.symId
9898
result = c.types.getOrDefault(s)
99-
when true:
100-
assert result != nil
101-
else:
102-
if result == nil:
103-
let symA = c.syms.getOrDefault(s).sym
104-
if symA != nil:
105-
assert symA.kind == skType
106-
result = symA.typ
107-
else:
108-
result = loadType(s, c)
109-
c.types[s] = LoadedType(state: Loaded, typ: result)
99+
if result == nil:
100+
let symA = c.nifSymToPSym.getOrDefault(s)
101+
if symA != nil:
102+
assert symA.kind == skType
103+
result = symA.typ
104+
assert symA != nil
105+
#[
106+
else:
107+
result = loadType(s, c)
108+
c.types[s] = LoadedType(state: Loaded, typ: result)
109+
]#
110+
inc n
110111
of ParLe:
111112
let tag = pool.tags[n.tag]
112113
if tag == "missing":

compiler/icnif/nifencoder.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ proc toNifSym(c: var EncodeContext; sym: PSym): string =
4545
result.add modsuf
4646

4747
proc symToNif(c: var EncodeContext; sym: PSym) =
48-
if sym.kind == skType and sym.typ != nil:
48+
if sfSystemModule in sym.owner.flags and sym.kind == skType and sym.typ != nil:
4949
toNif c, sym.typ
5050
else:
5151
c.b.addSymbol toNifSym(c, sym)
@@ -192,10 +192,11 @@ proc toNif(c: var EncodeContext; n: PNode) =
192192
c.b.addTree toNifTag(n.kind)
193193
assert n.len == 3
194194
symdefToNif(c, n[0])
195-
if n[0].kind == nkSym:
196-
toNif c, n[0].sym.typ
195+
if n[1].kind == nkSym:
196+
symToNif c, n[1].sym
197197
else:
198-
toNif c, n[1]
198+
assert n[0].kind == nkSym
199+
toNif c, n[0].sym.typ
199200
toNif c, n[2]
200201
c.b.endTree()
201202
of nkTypeDef:

tests/icnif/tencode_node2node.nim

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ proc sem(graph: ModuleGraph; path: AbsoluteFile): PNode =
5959
const SysTypeKinds = {tyBool, tyChar, tyString, tyInt .. tyUInt64}
6060

6161
# Compare PType, PSym and PNode but ignores fields nifencoder and nifdecoder doesn't support
62+
# Assumes `x` is generated by sem.nim and `y` is decoded by icnif/nifdecoder.
63+
# So `eql(x, y) == eql(y, x)` is not always true.
6264
proc eql(x, y: PType): bool =
6365
if x == nil and y == nil:
6466
result = true
@@ -116,12 +118,15 @@ proc eql(x, y: PNode): bool =
116118
of nkCharLit .. nkTripleStrLit:
117119
result = sameValue(x, y)
118120
of nkIdentDefs:
119-
assert x.len == 3
121+
assert x.len == 3 and y.len == 3
120122
if eql(x[0], y[0]) and eql(x[2], y[2]):
121-
if x[1].kind == nkEmpty:
122-
result = true
123-
else:
124-
result = eql(x[1], y[1])
123+
result = y[1].kind == nkEmpty and y[0].sym.typ != nil
124+
else:
125+
result = false
126+
of nkTypeDef:
127+
assert x.len == 3 and y.len == 3
128+
if eql(x[0], y[0]) and eql(x[1], y[1]):
129+
result = y[2].kind == nkEmpty and y[0].sym.typ != nil
125130
else:
126131
result = false
127132
else:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
type
22
TestInt = int
33

4-
#var x: TestInt
4+
var x: TestInt

0 commit comments

Comments
 (0)