Small scope refactoring (#18263)

* Small scope refactoring

* Add test for #10251

* Add inline where appropriate
This commit is contained in:
Clyybber
2021-06-14 19:19:58 +02:00
committed by GitHub
parent 0adb47aa15
commit 8c42f5be02
4 changed files with 33 additions and 24 deletions

View File

@@ -777,9 +777,10 @@ proc strTableInclReportConflict*(t: var TStrTable, n: PSym;
replaceSlot = h
h = nextTry(h, high(t.data))
if replaceSlot >= 0:
result = t.data[replaceSlot] # found it
if not onConflictKeepOld:
t.data[replaceSlot] = n # overwrite it with newer definition!
return t.data[replaceSlot] # found it
return result
elif mustRehash(t.data.len, t.counter):
strTableEnlarge(t)
strTableRawInsert(t.data, n)

View File

@@ -293,29 +293,28 @@ proc wrongRedefinition*(c: PContext; info: TLineInfo, s: string;
"redefinition of '$1'; previous declaration here: $2" %
[s, c.config $ conflictsWith])
proc addDecl*(c: PContext, sym: PSym, info: TLineInfo) =
let conflict = c.currentScope.addUniqueSym(sym)
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym, info: TLineInfo) =
let conflict = scope.addUniqueSym(sym)
if conflict != nil:
wrongRedefinition(c, info, sym.name.s, conflict.info)
proc addDecl*(c: PContext, sym: PSym) =
let conflict = strTableInclReportConflict(c.currentScope.symbols, sym, true)
if conflict != nil:
wrongRedefinition(c, sym.info, sym.name.s, conflict.info)
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym) {.inline.} =
addDeclAt(c, scope, sym, sym.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) =
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
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.
if sfExported in sym.flags or forceExport:
if sfExported in sym.flags:
# add to interface:
if c.module != nil: exportSym(c, sym)
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
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) =
## adds an symbol to the given scope, will check for and raise errors if it's
## a redefinition as opposed to an overload.
@@ -343,16 +346,11 @@ proc addOverloadableSymAt*(c: PContext; scope: PScope, fn: PSym) =
else:
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) =
## adds an overloadable symbol on the scope and the interface if appropriate
addOverloadableSymAt(c, scope, sym)
if not scope.isShadowScope:
# adding into a non-shadow scope, we need to handle exports, etc
addInterfaceDeclAux(c, sym)
proc openShadowScope*(c: PContext) =

View File

@@ -138,15 +138,13 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
identToReplace[] = symNode
if e.position == 0: hasNull = true
if result.sym != nil and sfExported in result.sym.flags:
incl(e.flags, {sfUsed, sfExported})
if result.sym != nil and not isPure:
addInterfaceDeclAux(c, e, forceExport = sfExported in result.sym.flags)
e.flags.incl {sfUsed, sfExported}
result.n.add symNode
styleCheckDef(c.config, e)
onDef(e.info, e)
if sfGenSym notin e.flags:
if not isPure: addDecl(c, e)
if not isPure: addInterfaceDecl(c, e)
else: declarePureEnumField(c, e)
if isPure and (let conflict = strTableInclReportConflict(symbols, e); conflict != nil):
wrongRedefinition(c, e.info, e.name.s, conflict.info)

12
tests/errmsgs/t10251.nim Normal file
View 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