Small scope refactoring (#18263)
* Small scope refactoring * Add test for #10251 * Add inline where appropriate
This commit is contained in:
parent
0adb47aa15
commit
8c42f5be02
4 changed files with 33 additions and 24 deletions
|
|
@ -777,9 +777,10 @@ proc strTableInclReportConflict*(t: var TStrTable, n: PSym;
|
||||||
replaceSlot = h
|
replaceSlot = h
|
||||||
h = nextTry(h, high(t.data))
|
h = nextTry(h, high(t.data))
|
||||||
if replaceSlot >= 0:
|
if replaceSlot >= 0:
|
||||||
|
result = t.data[replaceSlot] # found it
|
||||||
if not onConflictKeepOld:
|
if not onConflictKeepOld:
|
||||||
t.data[replaceSlot] = n # overwrite it with newer definition!
|
t.data[replaceSlot] = n # overwrite it with newer definition!
|
||||||
return t.data[replaceSlot] # found it
|
return result
|
||||||
elif mustRehash(t.data.len, t.counter):
|
elif mustRehash(t.data.len, t.counter):
|
||||||
strTableEnlarge(t)
|
strTableEnlarge(t)
|
||||||
strTableRawInsert(t.data, n)
|
strTableRawInsert(t.data, n)
|
||||||
|
|
|
||||||
|
|
@ -293,29 +293,28 @@ proc wrongRedefinition*(c: PContext; info: TLineInfo, s: string;
|
||||||
"redefinition of '$1'; previous declaration here: $2" %
|
"redefinition of '$1'; previous declaration here: $2" %
|
||||||
[s, c.config $ conflictsWith])
|
[s, c.config $ conflictsWith])
|
||||||
|
|
||||||
proc addDecl*(c: PContext, sym: PSym, info: TLineInfo) =
|
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym, info: TLineInfo) =
|
||||||
let conflict = c.currentScope.addUniqueSym(sym)
|
let conflict = scope.addUniqueSym(sym)
|
||||||
if conflict != nil:
|
if conflict != nil:
|
||||||
wrongRedefinition(c, info, sym.name.s, conflict.info)
|
wrongRedefinition(c, info, sym.name.s, conflict.info)
|
||||||
|
|
||||||
proc addDecl*(c: PContext, sym: PSym) =
|
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym) {.inline.} =
|
||||||
let conflict = strTableInclReportConflict(c.currentScope.symbols, sym, true)
|
addDeclAt(c, scope, sym, sym.info)
|
||||||
if conflict != nil:
|
|
||||||
wrongRedefinition(c, sym.info, sym.name.s, conflict.info)
|
proc addDecl*(c: PContext, sym: PSym, info: TLineInfo) {.inline.} =
|
||||||
|
addDeclAt(c, c.currentScope, sym, info)
|
||||||
|
|
||||||
|
proc addDecl*(c: PContext, sym: PSym) {.inline.} =
|
||||||
|
addDeclAt(c, c.currentScope, sym)
|
||||||
|
|
||||||
proc addPrelimDecl*(c: PContext, sym: PSym) =
|
proc addPrelimDecl*(c: PContext, sym: PSym) =
|
||||||
discard c.currentScope.addUniqueSym(sym)
|
discard c.currentScope.addUniqueSym(sym)
|
||||||
|
|
||||||
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym) =
|
|
||||||
let conflict = scope.addUniqueSym(sym)
|
|
||||||
if conflict != nil:
|
|
||||||
wrongRedefinition(c, sym.info, sym.name.s, conflict.info)
|
|
||||||
|
|
||||||
from ic / ic import addHidden
|
from ic / ic import addHidden
|
||||||
|
|
||||||
proc addInterfaceDeclAux*(c: PContext, sym: PSym, forceExport = false) =
|
proc addInterfaceDeclAux(c: PContext, sym: PSym) =
|
||||||
## adds symbol to the module for either private or public access.
|
## adds symbol to the module for either private or public access.
|
||||||
if sfExported in sym.flags or forceExport:
|
if sfExported in sym.flags:
|
||||||
# add to interface:
|
# add to interface:
|
||||||
if c.module != nil: exportSym(c, sym)
|
if c.module != nil: exportSym(c, sym)
|
||||||
else: internalError(c.config, sym.info, "addInterfaceDeclAux")
|
else: internalError(c.config, sym.info, "addInterfaceDeclAux")
|
||||||
|
|
@ -331,6 +330,10 @@ proc addInterfaceDeclAt*(c: PContext, scope: PScope, sym: PSym) =
|
||||||
# adding into a non-shadow scope, we need to handle exports, etc
|
# adding into a non-shadow scope, we need to handle exports, etc
|
||||||
addInterfaceDeclAux(c, sym)
|
addInterfaceDeclAux(c, sym)
|
||||||
|
|
||||||
|
proc addInterfaceDecl*(c: PContext, sym: PSym) {.inline.} =
|
||||||
|
## adds a decl and the interface if appropriate
|
||||||
|
addInterfaceDeclAt(c, c.currentScope, sym)
|
||||||
|
|
||||||
proc addOverloadableSymAt*(c: PContext; scope: PScope, fn: PSym) =
|
proc addOverloadableSymAt*(c: PContext; scope: PScope, fn: PSym) =
|
||||||
## adds an symbol to the given scope, will check for and raise errors if it's
|
## adds an symbol to the given scope, will check for and raise errors if it's
|
||||||
## a redefinition as opposed to an overload.
|
## a redefinition as opposed to an overload.
|
||||||
|
|
@ -343,16 +346,11 @@ proc addOverloadableSymAt*(c: PContext; scope: PScope, fn: PSym) =
|
||||||
else:
|
else:
|
||||||
scope.addSym(fn)
|
scope.addSym(fn)
|
||||||
|
|
||||||
proc addInterfaceDecl*(c: PContext, sym: PSym) =
|
|
||||||
## adds a decl and the interface if appropriate
|
|
||||||
addDecl(c, sym)
|
|
||||||
if not c.currentScope.isShadowScope:
|
|
||||||
addInterfaceDeclAux(c, sym)
|
|
||||||
|
|
||||||
proc addInterfaceOverloadableSymAt*(c: PContext, scope: PScope, sym: PSym) =
|
proc addInterfaceOverloadableSymAt*(c: PContext, scope: PScope, sym: PSym) =
|
||||||
## adds an overloadable symbol on the scope and the interface if appropriate
|
## adds an overloadable symbol on the scope and the interface if appropriate
|
||||||
addOverloadableSymAt(c, scope, sym)
|
addOverloadableSymAt(c, scope, sym)
|
||||||
if not scope.isShadowScope:
|
if not scope.isShadowScope:
|
||||||
|
# adding into a non-shadow scope, we need to handle exports, etc
|
||||||
addInterfaceDeclAux(c, sym)
|
addInterfaceDeclAux(c, sym)
|
||||||
|
|
||||||
proc openShadowScope*(c: PContext) =
|
proc openShadowScope*(c: PContext) =
|
||||||
|
|
|
||||||
|
|
@ -138,15 +138,13 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
|
||||||
identToReplace[] = symNode
|
identToReplace[] = symNode
|
||||||
if e.position == 0: hasNull = true
|
if e.position == 0: hasNull = true
|
||||||
if result.sym != nil and sfExported in result.sym.flags:
|
if result.sym != nil and sfExported in result.sym.flags:
|
||||||
incl(e.flags, {sfUsed, sfExported})
|
e.flags.incl {sfUsed, sfExported}
|
||||||
if result.sym != nil and not isPure:
|
|
||||||
addInterfaceDeclAux(c, e, forceExport = sfExported in result.sym.flags)
|
|
||||||
|
|
||||||
result.n.add symNode
|
result.n.add symNode
|
||||||
styleCheckDef(c.config, e)
|
styleCheckDef(c.config, e)
|
||||||
onDef(e.info, e)
|
onDef(e.info, e)
|
||||||
if sfGenSym notin e.flags:
|
if sfGenSym notin e.flags:
|
||||||
if not isPure: addDecl(c, e)
|
if not isPure: addInterfaceDecl(c, e)
|
||||||
else: declarePureEnumField(c, e)
|
else: declarePureEnumField(c, e)
|
||||||
if isPure and (let conflict = strTableInclReportConflict(symbols, e); conflict != nil):
|
if isPure and (let conflict = strTableInclReportConflict(symbols, e); conflict != nil):
|
||||||
wrongRedefinition(c, e.info, e.name.s, conflict.info)
|
wrongRedefinition(c, e.info, e.name.s, conflict.info)
|
||||||
|
|
|
||||||
12
tests/errmsgs/t10251.nim
Normal file
12
tests/errmsgs/t10251.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "redefinition of 'foo'; previous declaration here: t10251.nim(9, 9)"
|
||||||
|
line: 11
|
||||||
|
column: 9
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
Enum1 = enum
|
||||||
|
foo, bar, baz
|
||||||
|
Enum2 = enum
|
||||||
|
foo, bar, baz
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue