Skip to content

Commit 73e661d

Browse files
authored
modernize compiler/reorder, which exposes yet another strictdefs bug (#22415)
```nim {.experimental: "strictdefs".} type NodeKind = enum nkImportStmt nkStmtList nkNone PNode = ref object kind: NodeKind proc hasImportStmt(n: PNode): bool = # Checks if the node is an import statement or # i it contains one case n.kind of nkImportStmt: return true of nkStmtList: if false: return true else: result = false var n = PNode() echo hasImportStmt(n) ``` It compiles without warnings, but shouldn't. As a contrast, ```nim {.experimental: "strictdefs".} type NodeKind = enum nkImportStmt nkStmtList nkNone PNode = ref object kind: NodeKind proc hasImportStmt(n: PNode): bool = # Checks if the node is an import statement or # i it contains one case n.kind of nkImportStmt: result = true of nkStmtList: if false: return true else: result = false var n = PNode() echo hasImportStmt(n) ``` This gives a proper warning.
1 parent 10a6e4c commit 73e661d

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

compiler/reorder.nim

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ when defined(nimDebugReorder):
2525
var idNames = newTable[int, string]()
2626

2727
proc newDepN(id: int, pnode: PNode): DepN =
28-
new(result)
29-
result.id = id
30-
result.pnode = pnode
31-
result.idx = -1
32-
result.lowLink = -1
33-
result.onStack = false
34-
result.kids = @[]
35-
result.hAQ = -1
36-
result.hIS = -1
37-
result.hB = -1
38-
result.hCmd = -1
28+
result = DepN(id: id, pnode: pnode, idx: -1,
29+
lowLink: -1, onStack: false,
30+
kids: @[], hAQ: -1, hIS: -1,
31+
hB: -1, hCmd: -1
32+
)
3933
when defined(nimDebugReorder):
4034
result.expls = @[]
4135

@@ -114,7 +108,7 @@ proc computeDeps(cache: IdentCache; n: PNode, declares, uses: var IntSet; topLev
114108
# XXX: for callables, this technically adds the return type dep before args
115109
for i in 0..<n.safeLen: deps(n[i])
116110

117-
proc hasIncludes(n:PNode): bool =
111+
proc hasIncludes(n: PNode): bool =
118112
result = false
119113
for a in n:
120114
if a.kind == nkIncludeStmt:
@@ -235,8 +229,9 @@ proc hasImportStmt(n: PNode): bool =
235229
# i it contains one
236230
case n.kind
237231
of nkImportStmt, nkFromStmt, nkImportExceptStmt:
238-
return true
232+
result = true
239233
of nkStmtList, nkStmtListExpr, nkWhenStmt, nkElifBranch, nkElse, nkStaticStmt:
234+
result = false
240235
for a in n:
241236
if a.hasImportStmt:
242237
return true

0 commit comments

Comments
 (0)