underscore as special word (#21766)

* underscore as special word

* fix really hard to notice error
This commit is contained in:
metagn
2023-05-02 12:15:06 +03:00
committed by GitHub
parent c2bcfd8cd9
commit ca82b4ea16
6 changed files with 10 additions and 9 deletions

View File

@@ -15,7 +15,7 @@ when defined(nimPreviewSlimSystem):
import
intsets, ast, astalgo, idents, semdata, types, msgs, options,
renderer, nimfix/prettybase, lineinfos, modulegraphs, astmsgs, sets
renderer, nimfix/prettybase, lineinfos, modulegraphs, astmsgs, sets, wordrecg
proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope)
@@ -340,7 +340,7 @@ proc wrongRedefinition*(c: PContext; info: TLineInfo, s: string;
# xxx pending bootstrap >= 1.4, replace all those overloads with a single one:
# proc addDecl*(c: PContext, sym: PSym, info = sym.info, scope = c.currentScope) {.inline.} =
proc addDeclAt*(c: PContext; scope: PScope, sym: PSym, info: TLineInfo) =
if sym.name.s == "_": return
if sym.name.id == ord(wUnderscore): return
let conflict = scope.addUniqueSym(sym)
if conflict != nil:
if sym.kind == skModule and conflict.kind == skModule:
@@ -397,7 +397,7 @@ proc addOverloadableSymAt*(c: PContext; scope: PScope, fn: PSym) =
if fn.kind notin OverloadableSyms:
internalError(c.config, fn.info, "addOverloadableSymAt")
return
if fn.name.s != "_":
if fn.name.id != ord(wUnderscore):
let check = strTableGet(scope.symbols, fn.name)
if check != nil and check.kind notin OverloadableSyms:
wrongRedefinition(c, fn.info, fn.name.s, check.info)

View File

@@ -1117,7 +1117,7 @@ proc track(tracked: PEffects, n: PNode) =
elif child.kind == nkVarTuple:
for i in 0..<child.len-1:
if child[i].kind == nkEmpty or
child[i].kind == nkSym and child[i].sym.name.s == "_":
child[i].kind == nkSym and child[i].sym.name.id == ord(wUnderscore):
continue
varDecl(tracked, child[i])
if last.kind != nkEmpty:

View File

@@ -393,7 +393,7 @@ proc addToVarSection(c: PContext; result: var PNode; orig, identDefs: PNode) =
result.add identDefs
proc isDiscardUnderscore(v: PSym): bool =
if v.name.s == "_":
if v.name.id == ord(wUnderscore):
v.flags.incl(sfGenSym)
result = true
@@ -613,7 +613,7 @@ proc makeVarTupleSection(c: PContext, n, a, def: PNode, typ: PType, symkind: TSy
result = newNodeI(n.kind, a.info)
for j in 0..<a.len-2:
let name = a[j]
if useTemp and name.kind == nkIdent and name.ident.s == "_":
if useTemp and name.kind == nkIdent and name.ident.id == ord(wUnderscore):
# skip _ assignments if we are using a temp as they are already evaluated
continue
if name.kind == nkVarTuple:

View File

@@ -227,7 +227,7 @@ proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
closeScope(c)
let ident = getIdentNode(c, n)
if not isTemplParam(c, ident):
if n.kind != nkSym and not (n.kind == nkIdent and n.ident.s == "_"):
if n.kind != nkSym and not (n.kind == nkIdent and n.ident.id == ord(wUnderscore)):
let local = newGenSym(k, ident, c)
addPrelimDecl(c.c, local)
styleCheckDef(c.c, n.info, local)
@@ -645,7 +645,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
# body by the absence of the sfGenSym flag:
for i in 1..<s.typ.n.len:
let param = s.typ.n[i].sym
if param.name.s != "_":
if param.name.id != ord(wUnderscore):
param.flags.incl sfTemplateParam
param.flags.excl sfGenSym
if param.typ.kind != tyUntyped: allUntyped = false

View File

@@ -1306,7 +1306,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
for j in 0..<a.len-2:
var arg = newSymG(skParam, if a[j].kind == nkPragmaExpr: a[j][0] else: a[j], c)
if arg.name.s == "_":
if arg.name.id == ord(wUnderscore):
arg.flags.incl(sfGenSym)
elif containsOrIncl(check, arg.name.id):
localError(c.config, a[j].info, "attempt to redefine: '" & arg.name.s & "'")

View File

@@ -32,6 +32,7 @@ type
wColon = ":", wColonColon = "::", wEquals = "=", wDot = ".", wDotDot = "..",
wStar = "*", wMinus = "-",
wUnderscore = "_",
wMagic = "magic", wThread = "thread", wFinal = "final", wProfiler = "profiler",
wMemTracker = "memtracker", wObjChecks = "objchecks",
wIntDefine = "intdefine", wStrDefine = "strdefine", wBoolDefine = "booldefine",