Skip to content

Commit 6008dc1

Browse files
authored
fixes a part of #985; don't run Hexer for modules imported under when false: (#1087)
You stil get errors when imported a module under `when false:` that causes error in nimsem. nimsem produces `.2.deps.nif` files that doesn't contains modules imported under `when false:`. Nimony reads these files to generate final Makefile file so that Hexer and later phases don't read modules imported under `when false:`.
1 parent 473f136 commit 6008dc1

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/nimony/deps.nim

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type
2828
proc indexFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".2.idx.nif"
2929
proc parsedFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".1.nif"
3030
proc depsFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".1.deps.nif"
31+
proc deps2File(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".2.deps.nif"
3132
proc semmedFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".2.nif"
3233
proc nifcFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".c.nif"
3334
proc cFile(config: NifConfig; f: FilePair): string = config.nifcachePath / f.modname & ".c"
@@ -66,6 +67,7 @@ type
6667
includeStack: seq[string]
6768
processedModules: HashSet[string]
6869
moduleFlags: set[ModuleFlag]
70+
isGeneratingFinal: bool
6971

7072
proc toPair(c: DepContext; f: string): FilePair =
7173
FilePair(nimFile: f, modname: moduleSuffix(f, c.config.paths))
@@ -176,6 +178,7 @@ proc processDep(c: var DepContext; n: var Cursor; current: Node) =
176178
of ImportS:
177179
processImport c, n, current
178180
of IncludeS:
181+
assert not c.isGeneratingFinal
179182
processInclude c, n, current
180183
of FromimportS, ImportexceptS:
181184
processSingleImport c, n, current
@@ -215,7 +218,7 @@ proc importSystem(c: var DepContext; current: Node) =
215218
proc parseDeps(c: var DepContext; p: FilePair; current: Node) =
216219
execNifler c, p.nimFile, c.config.parsedFile(p)
217220

218-
let depsFile = c.config.depsFile(p)
221+
let depsFile = if c.isGeneratingFinal: c.config.deps2File(p) else: c.config.depsFile(p)
219222
var stream = nifstreams.open(depsFile)
220223
try:
221224
discard processDirectives(stream.r)
@@ -385,14 +388,17 @@ proc buildGraph*(config: sink NifConfig; project: string; forceRebuild, silentMa
385388
quoteShell(cfgNif)
386389
parseNifConfig cfgNif, config
387390

388-
var c = DepContext(nifler: nifler, config: config, rootNode: nil, includeStack: @[],
389-
forceRebuild: forceRebuild, moduleFlags: moduleFlags, nimsem: findTool("nimsem"),
390-
cmd: cmd)
391-
let p = c.toPair(project)
392-
c.rootNode = Node(files: @[p], id: 0, parent: -1, active: 0, isSystem: IsSystem in moduleFlags)
393-
c.nodes.add c.rootNode
394-
c.processedModules.incl p.modname
395-
parseDeps c, p, c.rootNode
391+
template initDepContext(isFinal: bool): DepContext =
392+
var c = DepContext(nifler: nifler, config: config, rootNode: nil, includeStack: @[],
393+
forceRebuild: forceRebuild, moduleFlags: moduleFlags, nimsem: findTool("nimsem"),
394+
cmd: cmd, isGeneratingFinal: isFinal)
395+
let p = c.toPair(project)
396+
c.rootNode = Node(files: @[p], id: 0, parent: -1, active: 0, isSystem: IsSystem in moduleFlags)
397+
c.nodes.add c.rootNode
398+
c.processedModules.incl p.modname
399+
parseDeps c, p, c.rootNode
400+
c
401+
var c = initDepContext(false)
396402
generateCachedConfigFile c, passC, passL
397403
let makeFilename = generateFrontendMakefile(c, commandLineArgs)
398404
#echo "run with: make -f ", makeFilename
@@ -404,6 +410,10 @@ proc buildGraph*(config: sink NifConfig; project: string; forceRebuild, silentMa
404410
" -f "
405411
exec makeCommand & quoteShell(makeFilename)
406412

413+
# Parse `.2.deps.nif`.
414+
# It is generated by nimsem and doesn't contains modules imported under `when false:`.
415+
# https://github.com/nim-lang/nimony/issues/985
416+
c = initDepContext(true)
407417
let makeFinalFilename = generateFinalMakefile(c, commandLineArgsNifc, passC, passL)
408418
exec makeCommand & quoteShell(makeFinalFilename)
409419
if cmd == DoRun:

src/nimony/sem.nim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## type checking.
1010

1111
import std / [tables, sets, syncio, formatfloat, assertions, strutils]
12-
from std/os import getCurrentDir
12+
from std/os import changeFileExt, getCurrentDir
1313
include nifprelude
1414
import nimony_model, symtabs, builtintypes, decls, symparser, asthelpers,
1515
programs, sigmatch, magics, reporters, nifconfig, nifindexes,
@@ -7121,6 +7121,17 @@ proc writeOutput(c: var SemContext; outfile: string) =
71217121
toBuild: move c.toBuild,
71227122
exportBuf: buildIndexExports(c))
71237123

7124+
# Update .2.deps.nif file that doesn't contain modules imported under `when false:`
7125+
# so that Hexer and following phases doesn't read such modules.
7126+
var deps = createTokenBuf(16)
7127+
deps.buildTree StmtsS, NoLineInfo:
7128+
if c.importedModules.len != 0:
7129+
deps.buildTree ImportS, NoLineInfo:
7130+
for _, i in c.importedModules:
7131+
deps.addStrLit i.path.toAbsolutePath
7132+
let depsFile = changeFileExt(outfile, ".deps.nif")
7133+
writeFile depsFile, "(.nif24)\n" & toString(deps)
7134+
71247135
proc phaseX(c: var SemContext; n: Cursor; x: SemPhase): TokenBuf =
71257136
assert n.stmtKind == StmtsS
71267137
c.phase = x

0 commit comments

Comments
 (0)