Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
6ad033d
PRTEMP
timotheecour Aug 28, 2021
595638f
works even across modules
timotheecour Aug 29, 2021
e8b5184
PRTEMP
timotheecour Aug 29, 2021
3ce87f2
PRTEMP2
timotheecour Aug 30, 2021
1164590
works much better; retrieves the right PContext when re-visiting a sym
timotheecour Aug 30, 2021
4b22ba9
importc works; and we now avoid to generate a fake proto
timotheecour Aug 30, 2021
33afaa1
fix D20210829T232613
timotheecour Aug 30, 2021
a62c6b8
PRTEMP
timotheecour Aug 30, 2021
258bffb
_
timotheecour Aug 30, 2021
af29bcd
fix a bug thanks to tracking c.p
timotheecour Aug 30, 2021
9e16bca
small patch to times makes D20210830T020045 work
timotheecour Aug 30, 2021
39c1ff9
works with D20210829T213407
timotheecour Aug 30, 2021
c95ea1d
cleanups
timotheecour Aug 30, 2021
29e6dba
fixes defined issue by resetting the correct scope
timotheecour Aug 30, 2021
4599e3a
cleanups
timotheecour Aug 30, 2021
4740766
cleanups
timotheecour Aug 30, 2021
9a8a0c1
works up to link error
timotheecour Aug 31, 2021
ed86675
nimLazySemcheck works with system!
timotheecour Aug 31, 2021
38d77c7
cleanups
timotheecour Aug 31, 2021
07d35cd
cleanups
timotheecour Aug 31, 2021
94ca07c
PRTEMP
timotheecour Aug 31, 2021
3c35b52
revert temp stuff
timotheecour Aug 31, 2021
93b27c4
works better w fwd procs
timotheecour Aug 31, 2021
fc0baf5
works
timotheecour Aug 31, 2021
7816c51
cleanups
timotheecour Aug 31, 2021
e5bc4d0
field access now works, thanks to fixing owner (push/pop) which fixes…
timotheecour Aug 31, 2021
c67b1bb
cleanups
timotheecour Sep 1, 2021
2fac452
fix D20210831T180635 avoid suggesting impl that have a decl defined, …
timotheecour Sep 1, 2021
62ca207
bugfix by avoiding returning lazyDecl in initOverloadIter, nextOverlo…
timotheecour Sep 1, 2021
9ab6527
add tests
timotheecour Sep 1, 2021
47df2d4
improve tests
timotheecour Sep 1, 2021
b4de412
fixup
timotheecour Sep 1, 2021
afc6e55
tests/misc/tlazysemcheck.nim
timotheecour Sep 1, 2021
2af0632
fixup
timotheecour Sep 2, 2021
a53c57e
fix D20210901T233613 via sfLazySemcheckInprogress: avoid instantiatin…
timotheecour Sep 2, 2021
56089e7
fix lots of issues
timotheecour Sep 2, 2021
b63880a
cleanup
timotheecour Sep 2, 2021
6fb1a15
can compile a half working nim with a small nb of modifs
timotheecour Sep 2, 2021
dc4ad2f
builds a nim that works
timotheecour Sep 2, 2021
0b50d09
building a working compiler works with a tiny patch; via optionStackE…
timotheecour Sep 2, 2021
124a011
fix methods
timotheecour Sep 2, 2021
cf4a4e9
fix coverter; fix a bug in jsgen
timotheecour Sep 2, 2021
68a5ff2
improve tests
timotheecour Sep 2, 2021
226448f
fixup
timotheecour Sep 2, 2021
2f570bd
fixup
timotheecour Sep 2, 2021
4b772fd
fixup
timotheecour Sep 2, 2021
bea9aec
simplify
timotheecour Sep 2, 2021
965e81d
fixup
timotheecour Sep 2, 2021
dab1a73
fixup
timotheecour Sep 2, 2021
1911be8
fixup
timotheecour Sep 2, 2021
232dd85
improve
timotheecour Sep 2, 2021
57db1e3
fixup
timotheecour Sep 2, 2021
866d51b
fixup
timotheecour Sep 2, 2021
55d8ebd
cleanup
timotheecour Sep 2, 2021
fcdfc03
cleanup semexprs
timotheecour Sep 2, 2021
aaee4cb
cleanup semgnrc
timotheecour Sep 2, 2021
c7d32a4
cleanup semstmts a bit
timotheecour Sep 2, 2021
2a66e2d
cleanup
timotheecour Sep 2, 2021
d46212f
refactor
timotheecour Sep 2, 2021
8fb6263
remove 1 determineType2
timotheecour Sep 3, 2021
dbf7bb0
fixup
timotheecour Sep 3, 2021
181c556
add regression test; clarify some dosc
timotheecour Sep 3, 2021
10b2ca9
fix D20210902T184355
timotheecour Sep 3, 2021
77605b6
finally works with D20210902T221232
timotheecour Sep 3, 2021
ec300b3
fix a bug and add tests
timotheecour Sep 3, 2021
98d5005
PRTEMP
timotheecour Sep 3, 2021
ebe7884
fix D20210827T174229_bug_semTypeNode finally
timotheecour Sep 3, 2021
53ababb
revert some now un-needed changes
timotheecour Sep 3, 2021
fc92bbc
fix semIterator
timotheecour Sep 4, 2021
f86e0d8
cleanup
timotheecour Sep 4, 2021
f15d7f3
nimLazyVisitAll works
timotheecour Sep 4, 2021
d0aba54
nim doc with recursive deps works and shows docs for even unsued syms
timotheecour Sep 4, 2021
d4472c8
nim doc implies isSemcheckUnusedSymbols
timotheecour Sep 4, 2021
7df0be6
fix tests/nimdoc/trunnableexamples.nim
timotheecour Sep 4, 2021
7f2512a
make more more tests pass by using -d:nimLazySemcheckComplete
timotheecour Sep 4, 2021
dee34d3
force semcheck of attached ops (we could relax this later but it is t…
timotheecour Sep 4, 2021
1283a9a
fix things like {.async, gcsafe.} (eg tests/ccgbugs/t9655.nim)
timotheecour Sep 5, 2021
2a0f782
fix case_D20210904T193500 ompiler/vmgen.nim(987, 10) `t != nil` [Ass…
timotheecour Sep 5, 2021
f038c70
PRTEMP
timotheecour Sep 5, 2021
84e67b1
works
timotheecour Sep 5, 2021
c2064e5
fixup
timotheecour Sep 5, 2021
e3df0e0
_
timotheecour Sep 5, 2021
1f55cb8
PRTEMP
timotheecour Sep 5, 2021
2813b16
fix Closure iterators are not supported by JS backend; tests/js/t7109…
timotheecour Sep 5, 2021
c55d127
cleanup
timotheecour Sep 5, 2021
48a2324
PRTEMP
timotheecour Sep 5, 2021
efb6e12
fix D20210905T170542 and tests/misc/tlazysemcheck.nim
timotheecour Sep 6, 2021
8ba2726
fix tests/misc/tconv.nim D20210905T125411_forceSemcheck_compiles
timotheecour Sep 6, 2021
6cca2c2
revert changes to lib/pure/asyncdispatch.nim D20210827T174229_None_ty…
timotheecour Sep 6, 2021
a426b51
workaround D20210905T181502_callsoon related to generic sandwich
timotheecour Sep 6, 2021
936fb15
fix a few tests
timotheecour Sep 6, 2021
3d874e6
fix some SIGSEGV
timotheecour Sep 6, 2021
bba9aa8
snapshotOptionEntry
timotheecour Sep 6, 2021
fe2959b
PRTEMP
timotheecour Sep 6, 2021
bb8f990
works using POptionEntry similar to PScope
timotheecour Sep 6, 2021
eca1449
cleanups; nim now bootstraps without --stylecheck:off workaround; opt…
timotheecour Sep 6, 2021
a100627
fix SIGSEGV tests/statictypes/tstatictypes.nim
timotheecour Sep 6, 2021
e54438a
fix tests/errmsgs/treportunused.nim
timotheecour Sep 7, 2021
c6e45f0
PRTEMP
timotheecour Sep 7, 2021
0b76a2f
fix tests/pragmas/tused2.nim
timotheecour Sep 7, 2021
b824297
fix D20210906T193359 tests/astspec/tastspec.nim (workaround: non-lazy…
timotheecour Sep 7, 2021
23b4218
fix tests/bind/tbind.nim
timotheecour Sep 7, 2021
2688641
fix tests/stdlib/trepr.nim
timotheecour Sep 7, 2021
da48f47
fix "Error: cannot instantiate" D20210906T234812
timotheecour Sep 7, 2021
8c5febe
fix tests/macros/tdumpast.nim
timotheecour Sep 7, 2021
bdd11a9
fix tests/trmacros/trmacros_various.nim
timotheecour Sep 7, 2021
cb9e454
fix tests/pragmas/tcustom_pragma.nim
timotheecour Sep 7, 2021
c1bffa4
fix tests/concepts/tspec.nim
timotheecour Sep 7, 2021
f84ca30
fix tests/overload/toverload_various.nim
timotheecour Sep 7, 2021
50dd045
fix tests
timotheecour Sep 7, 2021
12eaacc
fix tests
timotheecour Sep 7, 2021
520198c
fix tests/method/tmethod_various.nim
timotheecour Sep 7, 2021
02c2071
fix tests/niminaction/Chapter2/resultreject.nim
timotheecour Sep 7, 2021
c250879
enable nimLazySemcheckAfterSystem, nimLazySemcheckComplete for NIM_TE…
timotheecour Sep 7, 2021
9eaf076
fixup
timotheecour Sep 7, 2021
1e07862
fix issue with semstmts.visitName assert failure
timotheecour Sep 7, 2021
9110ec0
address a comment
timotheecour Sep 7, 2021
8b42ca1
enable -d:nimLazySemcheckAfterSystem for NIM_TEST_PACKAGES
timotheecour Sep 7, 2021
b3221be
tests/macros/tmacrotypes.nim works via nimSemcheckTree
timotheecour Sep 7, 2021
937860d
maybe works with tests/stdlib/tsugar.nim and tests/macros/tmacrotypes…
timotheecour Sep 7, 2021
3881450
cleanup
timotheecour Sep 7, 2021
6ef2ce6
better fix
timotheecour Sep 8, 2021
6e27156
PRTEMP2
timotheecour Sep 8, 2021
fa55e81
instantiationScope works to solve D20210827T174229_macrp_typed_param_…
timotheecour Sep 8, 2021
f3a561f
cleanups
timotheecour Sep 8, 2021
f778cf4
revert some test changes that now work
timotheecour Sep 8, 2021
d36692d
add tests for D20210827T174229_macrp_typed_param_lazy
timotheecour Sep 8, 2021
e795c33
`compiles(foo)` triggers epilogue for generated tree
timotheecour Sep 8, 2021
41fb7d3
revert some test changes that now work
timotheecour Sep 8, 2021
9951aa4
add tests
timotheecour Sep 8, 2021
02d6c2d
cleanup
timotheecour Sep 8, 2021
9c10ffc
bugfix; fixes XDeclaredButNotUsed
timotheecour Sep 8, 2021
a7c0f30
fix tests/pragmas/twarning_off.nim
timotheecour Sep 9, 2021
ae82655
cleanup
timotheecour Sep 9, 2021
cd58c58
further simplification
timotheecour Sep 9, 2021
0923f63
fix tests/arc/tarcmisc.nim
timotheecour Sep 9, 2021
06a3071
fix for bootstrap
timotheecour Sep 9, 2021
e451049
fix tests/importalls/tmain2.nim via calling openScope+closeScope
timotheecour Sep 9, 2021
f4957a9
refactor with addInterfaceDeclSelect
timotheecour Sep 9, 2021
ce13dd3
simplify code via isTopLevel
timotheecour Sep 9, 2021
1876dcb
revert some test changes that now work
timotheecour Sep 9, 2021
02f99d3
fix tests/untestable/thttpclient_ssl_remotenetwork.nim, refs D2021090…
timotheecour Sep 9, 2021
0c2ae1b
fix tests/misc/mlazysemcheck.nim
timotheecour Sep 9, 2021
99d0556
make skMethod non lazy for now
timotheecour Sep 9, 2021
1bf8619
PRTEMP try lazy in config/config.nims
timotheecour Sep 9, 2021
a9706dd
workaround to make nimsuggest compile
timotheecour Sep 9, 2021
87d3709
add test for methods, refs D20210909T094624; fix for nim doc0
timotheecour Sep 9, 2021
4e393f5
D20210909T120651
timotheecour Sep 9, 2021
cd1c2de
fix `nim doc lib/pure/asyncfile.nim`
timotheecour Sep 9, 2021
8d08456
fix lots of IC tests
timotheecour Sep 9, 2021
ca0368f
PRTEMP warning instead of error for effects to see what passes
timotheecour Sep 9, 2021
91c9612
fix semstmts.visitName
timotheecour Sep 10, 2021
85db58c
fix for tests/stdlib/tlwip.nim
timotheecour Sep 10, 2021
1ff33c8
Revert "PRTEMP warning instead of error for effects to see what passes"
timotheecour Sep 10, 2021
764fe81
keep the default as non-lazy semchecking
timotheecour Oct 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ type
sfGlobal, # symbol is at global scope

sfForward, # symbol is forward declared
sfLazy, # symbol is lazy declared
sfLazyForwardRequested, # semchecking was requested for a lazy symbol; PRTEMP RENAME
sfLazySemcheckInprogress,
sfLazySemcheckStarted,
sfLazyDeadSymTansf, # for transf.nim D20210904T200315
sfWasForwarded, # symbol had a forward declaration
# (implies it's too dangerous to patch its type signature)
sfImportc, # symbol is external; imported
Expand Down Expand Up @@ -869,6 +874,10 @@ type
bitsize*: int
alignment*: int # for alignment
else: nil
lazyDecl*: PSym
# could be in routineKinds branch
# proc f(); proc f()=discard; => 2nd sym.lazyDecl = 1st sym
# this was resolved as an impl for a fwd proc; we shouldn't pick such a symbol and instead pick the fwd decl
magic*: TMagic
typ*: PType
name*: PIdent
Expand Down Expand Up @@ -1206,6 +1215,9 @@ template `[]=`*(n: Indexable, i: int; x: Indexable) = n.sons[i] = x
template `[]`*(n: Indexable, i: BackwardsIndex): Indexable = n[n.len - i.int]
template `[]=`*(n: Indexable, i: BackwardsIndex; x: Indexable) = n[n.len - i.int] = x

proc isVarargsUntyped*(x: PType): bool {.inline.} =
x.kind == tyVarargs and x[0].kind == tyUntyped

proc getDeclPragma*(n: PNode): PNode =
## return the `nkPragma` node for declaration `n`, or `nil` if no pragma was found.
## Currently only supports routineDefs + {nkTypeDef}.
Expand Down
26 changes: 19 additions & 7 deletions compiler/astalgo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ proc debug*(n: PSym; conf: ConfigRef = nil) {.exportc: "debugSym", deprecated.}
proc debug*(n: PType; conf: ConfigRef = nil) {.exportc: "debugType", deprecated.}
proc debug*(n: PNode; conf: ConfigRef = nil) {.exportc: "debugNode", deprecated.}

proc isNotDup(s: PSym): bool =
s.lazyDecl == nil
# s.lazyDecl == nil and sfLazySemcheckStarted notin s.flags # PRTEMP
# sfLazySemcheckStarted

proc typekinds*(t: PType) {.deprecated.} =
var t = t
var s = ""
Expand Down Expand Up @@ -527,6 +532,9 @@ proc value(this: var DebugPrinter; value: PSym) =
this.value(value.name.s)
this.key("id")
this.value(value.id)
if value.ast!=nil and this.conf!=nil:
this.key("loc")
this.value(this.conf$value.ast.info)
if value.kind in {skField, skEnumField, skParam}:
this.key("position")
this.value(value.position)
Expand Down Expand Up @@ -594,6 +602,7 @@ proc value(this: var DebugPrinter; value: PNode) =
if this.conf != nil:
this.key "info"
this.value $lineInfoToStr(this.conf, value.info)
# TODO: loc this.value(this.conf$value.ast.info)
if value.flags != {}:
this.key "flags"
this.value value.flags
Expand Down Expand Up @@ -640,23 +649,23 @@ proc value(this: var DebugPrinter; value: PNode) =


proc debug(n: PSym; conf: ConfigRef) =
var this: DebugPrinter
var this = DebugPrinter(conf: conf)
this.visited = initTable[pointer, int]()
this.renderSymType = true
this.useColor = not defined(windows)
this.value(n)
echo($this.res)

proc debug(n: PType; conf: ConfigRef) =
var this: DebugPrinter
var this = DebugPrinter(conf: conf)
this.visited = initTable[pointer, int]()
this.renderSymType = true
this.useColor = not defined(windows)
this.value(n)
echo($this.res)

proc debug(n: PNode; conf: ConfigRef) =
var this: DebugPrinter
var this = DebugPrinter(conf: conf)
this.visited = initTable[pointer, int]()
#this.renderSymType = true
this.useColor = not defined(windows)
Expand Down Expand Up @@ -802,7 +811,7 @@ proc strTableGet*(t: TStrTable, name: PIdent): PSym =
while true:
result = t.data[h]
if result == nil: break
if result.name.id == name.id: break
if result.name.id == name.id and result.isNotDup: break
h = nextTry(h, high(t.data))


Expand All @@ -816,7 +825,7 @@ proc nextIdentIter*(ti: var TIdentIter, tab: TStrTable): PSym =
var start = h
result = tab.data[h]
while result != nil:
if result.name.id == ti.name.id: break
if result.name.id == ti.name.id and result.isNotDup: break
h = nextTry(h, high(tab.data))
if h == start:
result = nil
Expand All @@ -836,15 +845,18 @@ proc nextIdentExcluding*(ti: var TIdentIter, tab: TStrTable,
var start = h
result = tab.data[h]
while result != nil:
if result.name.id == ti.name.id and not contains(excluding, result.id):
if result.name.id == ti.name.id and result.isNotDup and not contains(excluding, result.id):
break
h = nextTry(h, high(tab.data))
if h == start:
result = nil
break
result = tab.data[h]
ti.h = nextTry(h, high(tab.data))
if result != nil and contains(excluding, result.id): result = nil
if result != nil:
# bugfix for pre-existing issue: avoid redundant check
assert not contains(excluding, result.id)
assert result.isNotDup

proc firstIdentExcluding*(ti: var TIdentIter, tab: TStrTable, s: PIdent,
excluding: IntSet): PSym =
Expand Down
3 changes: 3 additions & 0 deletions compiler/concepts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ast, astalgo, semdata, lookups, lineinfos, idents, msgs, renderer, types, intsets

from magicsys import addSonSkipIntLit
from modulegraphs import determineType2

const
logBindings = false
Expand Down Expand Up @@ -269,8 +270,10 @@ proc matchSym(c: PContext; candidate: PSym, n: PNode; m: var MatchCon): bool =
proc matchSyms(c: PContext, n: PNode; kinds: set[TSymKind]; m: var MatchCon): bool =
## Walk the current scope, extract candidates which the same name as 'n[namePos]',
## 'n' is the nkProcDef or similar from the concept that we try to match.
determineType2 c.graph, n[namePos].sym
let candidates = searchInScopesFilterBy(c, n[namePos].sym.name, kinds)
for candidate in candidates:
determineType2(c.graph, candidate)
#echo "considering ", typeToString(candidate.typ), " ", candidate.magic
m.magic = candidate.magic
if matchSym(c, candidate, n, m): return true
Expand Down
1 change: 1 addition & 0 deletions compiler/condsyms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimHasHintAll")
defineSymbol("nimHasTrace")
defineSymbol("nimHasEffectsOf")
defineSymbol("nimHasLazySemcheck")
2 changes: 2 additions & 0 deletions compiler/debugutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ proc isCompilerDebug*(): bool =
{.undef(nimCompilerDebug).}
echo 'x'
conf0.isDefined("nimCompilerDebug")

include timn/exp/nim_compiler_debugutils
5 changes: 5 additions & 0 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,11 @@ proc generateDoc*(d: PDoc, n, orig: PNode, docFlags: DocFlags = kDefault) =
## which is implemented in ``docgen2.nim``.
template genItemAux(skind) =
genItem(d, n, n[namePos], skind, docFlags)
if n.kind in routineDefs:
if n[0].kind != nkSym:
# e.g. for: nim doc0 lib/system/threads.nim
assert d.conf.cmd == cmdDoc0, $d.conf.cmd
elif n[0].sym.typ == nil: return # lazy semchecking; we could also defer to epilogue
case n.kind
of nkPragma:
let pragmaNode = findPragma(n, wDeprecated)
Expand Down
6 changes: 3 additions & 3 deletions compiler/docgen2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ proc myOpenTex(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassConte
proc myOpenJson(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
myOpenImpl(JsonExt)

const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
const docgen2Pass* = makePass(open = myOpen, process = processNode, closeEpilogue = close)
const docgen2TexPass* = makePass(open = myOpenTex, process = processNode,
close = close)
closeEpilogue = close)
const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
close = closeJson)
closeEpilogue = closeJson)

proc finishDoc2Pass*(project: string) =
discard
8 changes: 6 additions & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,10 @@ proc genCopyForParamIfNeeded(p: PProc, n: PNode) =

proc genVarInit(p: PProc, v: PSym, n: PNode)

proc checkClosureIteratorJs(p: PProc, n: PNode) =
if sfLazy notin n[0].sym.flags and n[0].sym.typ.callConv == TCallingConvention.ccClosure:
globalError(p.config, n.info, "Closure iterators are not supported by JS backend!")

proc genSym(p: PProc, n: PNode, r: var TCompRes) =
var s = n.sym
case s.kind
Expand Down Expand Up @@ -1440,6 +1444,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
else:
genProcForSymIfNeeded(p, s)
else:
if s.kind == skIterator: checkClosureIteratorJs(p, s.ast)
if s.loc.r == nil:
internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
r.res = s.loc.r
Expand Down Expand Up @@ -2623,8 +2628,7 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
nkFromStmt, nkTemplateDef, nkMacroDef, nkStaticStmt,
nkMixinStmt, nkBindStmt: discard
of nkIteratorDef:
if n[0].sym.typ.callConv == TCallingConvention.ccClosure:
globalError(p.config, n.info, "Closure iterators are not supported by JS backend!")
checkClosureIteratorJs(p, n)
of nkPragma: genPragma(p, n)
of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef:
var s = n[namePos].sym
Expand Down
101 changes: 63 additions & 38 deletions compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import
intsets, ast, astalgo, idents, semdata, types, msgs, options,
renderer, nimfix/prettybase, lineinfos, modulegraphs, astmsgs

proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope)

proc noidentError(conf: ConfigRef; n, origin: PNode) =
var m = ""
if origin != nil:
Expand Down Expand Up @@ -70,10 +68,6 @@ proc openScope*(c: PContext): PScope {.discardable.} =
proc rawCloseScope*(c: PContext) =
c.currentScope = c.currentScope.parent

proc closeScope*(c: PContext) =
ensureNoMissingOrUnusedSymbols(c, c.currentScope)
rawCloseScope(c)

iterator allScopes*(scope: PScope): PScope =
var current = scope
while current != nil:
Expand Down Expand Up @@ -196,19 +190,26 @@ proc searchInScopes*(c: PContext, s: PIdent; ambiguous: var bool): PSym =
if result != nil: return result
result = someSymFromImportTable(c, s, ambiguous)

proc debugScopes*(c: PContext; limit=0, max = int.high) {.deprecated.} =
proc debugScopes*(conf: ConfigRef, scope: PScope; limit=0, max = int.high) {.deprecated.} =
var i = 0
var count = 0
for scope in allScopes(c.currentScope):
echo "scope ", i
for scope in allScopes(scope):
echo "scope $# depth: $#" % [$i, $scope.depthLevel]
for h in 0..high(scope.symbols.data):
if scope.symbols.data[h] != nil:
if count >= max: return
echo count, ": ", scope.symbols.data[h].name.s
let s = scope.symbols.data[h]
var msg = $count & ": " & $s & $(s.flags, s.owner, s.kind, s.typ)
if s.ast!=nil:
msg.add " " & conf$s.ast.info
echo msg
count.inc
if i == limit: return
inc i

proc debugScopes*(c: PContext; limit=0, max = int.high) {.deprecated.} =
debugScopes(c.config, c.currentScope, limit, max)

proc searchInScopesFilterBy*(c: PContext, s: PIdent, filter: TSymKinds): seq[PSym] =
result = @[]
block outer:
Expand Down Expand Up @@ -268,30 +269,50 @@ proc getSymRepr*(conf: ConfigRef; s: PSym, getDeclarationPath = true): string =
if getDeclarationPath:
result.addDeclaredLoc(conf, s)

proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) =
# check if all symbols have been used and defined:
type EpilogueData = object
missingImpls: int
unusedSyms: seq[tuple[sym: PSym, key: string]]

proc ensureNoMissingOrUnusedSymbol*(data: var EpilogueData, config: ConfigRef, s: PSym) =
if sfForward in s.flags and s.kind notin {skType, skModule}:
# too many 'implementation of X' errors are annoying
# and slow 'suggest' down:
if data.missingImpls == 0:
if false:
# PRTEMP
localError(config, s.info, "implementation of '$1' expected" %
getSymRepr(config, s, getDeclarationPath=false))
inc data.missingImpls
elif {sfUsed, sfExported} * s.flags == {}:
if s.kind notin {skForVar, skParam, skMethod, skUnknown, skGenericParam, skEnumField}:
# XXX: implicit type params are currently skTypes
# maybe they can be made skGenericParam as well.
if s.typ != nil and tfImplicitTypeParam notin s.typ.flags and
s.typ.kind != tyGenericParam:
data.unusedSyms.add (s, toFileLineCol(config, s.info))

iterator getSymbols(scope: PScope): PSym =
var it: TTabIter
var s = initTabIter(it, scope.symbols)
var missingImpls = 0
var unusedSyms: seq[tuple[sym: PSym, key: string]]
while s != nil:
if sfForward in s.flags and s.kind notin {skType, skModule}:
# too many 'implementation of X' errors are annoying
# and slow 'suggest' down:
if missingImpls == 0:
localError(c.config, s.info, "implementation of '$1' expected" %
getSymRepr(c.config, s, getDeclarationPath=false))
inc missingImpls
elif {sfUsed, sfExported} * s.flags == {}:
if s.kind notin {skForVar, skParam, skMethod, skUnknown, skGenericParam, skEnumField}:
# XXX: implicit type params are currently skTypes
# maybe they can be made skGenericParam as well.
if s.typ != nil and tfImplicitTypeParam notin s.typ.flags and
s.typ.kind != tyGenericParam:
unusedSyms.add (s, toFileLineCol(c.config, s.info))
yield s
s = nextIter(it, scope.symbols)
for (s, _) in sortedByIt(unusedSyms, it.key):
message(c.config, s.info, hintXDeclaredButNotUsed, s.name.s)

template ensureNoMissingOrUnusedSymbols*(conf: ConfigRef; syms: untyped) = # syms: iterable[PSym]
# check if all symbols have been used and defined:
var data: EpilogueData
for s in syms:
ensureNoMissingOrUnusedSymbol(data, conf, s)
for (s, _) in sortedByIt(data.unusedSyms, it.key):
# if s.getnimblePkgId == conf.mainPackageId:
if s.getModule.getnimblePkgId == conf.mainPackageId:
# see also foreignPackageNotes
message(conf, s.info, hintXDeclaredButNotUsed, s.name.s)

proc closeScope*(c: PContext) =
if not c.config.isSemcheckUnusedSymbols:
ensureNoMissingOrUnusedSymbols(c.config, getSymbols(c.currentScope))
rawCloseScope(c)

proc wrongRedefinition*(c: PContext; info: TLineInfo, s: string;
conflictsWith: TLineInfo, note = errGenerated) =
Expand Down Expand Up @@ -330,13 +351,13 @@ proc addPrelimDecl*(c: PContext, sym: PSym) =

from ic / ic import addHidden

proc addInterfaceDeclAux(c: PContext, sym: PSym) =
proc addInterfaceDeclAux(c: PContext, sym: PSym, isTopLevel: bool) =
## adds symbol to the module for either private or public access.
if sfExported in sym.flags:
# add to interface:
if c.module != nil: exportSym(c, sym)
else: internalError(c.config, sym.info, "addInterfaceDeclAux")
elif sym.kind in ExportableSymKinds and c.module != nil and isTopLevelInsideDeclaration(c, sym):
elif sym.kind in ExportableSymKinds and c.module != nil and isTopLevel:
strTableAdd(semtabAll(c.graph, c.module), sym)
if c.config.symbolFiles != disabledSf:
addHidden(c.encoder, c.packedRepr, sym)
Expand All @@ -346,7 +367,7 @@ proc addInterfaceDeclAt*(c: PContext, scope: PScope, sym: PSym) =
addDeclAt(c, scope, sym)
if not scope.isShadowScope:
# adding into a non-shadow scope, we need to handle exports, etc
addInterfaceDeclAux(c, sym)
addInterfaceDeclAux(c, sym, scope.isTopLevel)

proc addInterfaceDecl*(c: PContext, sym: PSym) {.inline.} =
## adds a decl and the interface if appropriate
Expand All @@ -369,7 +390,13 @@ proc addInterfaceOverloadableSymAt*(c: PContext, scope: PScope, sym: PSym) =
addOverloadableSymAt(c, scope, sym)
if not scope.isShadowScope:
# adding into a non-shadow scope, we need to handle exports, etc
addInterfaceDeclAux(c, sym)
addInterfaceDeclAux(c, sym, scope.isTopLevel)

proc addInterfaceDeclSelect*(c: PContext, scope: PScope, sym: PSym) {.inline.} =
if sym.kind in OverloadableSyms:
addInterfaceOverloadableSymAt(c, scope, sym)
else:
addInterfaceDeclAt(c, scope, sym)

proc openShadowScope*(c: PContext) =
## opens a shadow scope, just like any other scope except the depth is the
Expand All @@ -394,10 +421,7 @@ proc mergeShadowScope*(c: PContext) =
let shadowScope = c.currentScope
c.rawCloseScope
for sym in shadowScope.symbols:
if sym.kind in OverloadableSyms:
c.addInterfaceOverloadableSymAt(c.currentScope, sym)
else:
c.addInterfaceDecl(sym)
addInterfaceDeclSelect(c, c.currentScope, sym)

when false:
# `nimfix` used to call `altSpelling` and prettybase.replaceDeprecated(n.info, ident, alt)
Expand Down Expand Up @@ -539,6 +563,7 @@ type
checkAmbiguity, checkUndeclared, checkModule, checkPureEnumFields

proc qualifiedLookUp*(c: PContext, n: PNode, flags: set[TLookupFlag]): PSym =
# `determineType2` should not be called inside this; instead callers should selectively call it as needed.
const allExceptModule = {low(TSymKind)..high(TSymKind)} - {skModule, skPackage}
case n.kind
of nkIdent, nkAccQuoted:
Expand Down
Loading