Skip to content

Commit

Permalink
[backport] fixes #23711; C code contains backtick`gensym (#23716)
Browse files Browse the repository at this point in the history
fixes #23711

(cherry picked from commit 646bd99)
  • Loading branch information
ringabout authored and narimiran committed Jun 24, 2024
1 parent 2735fd2 commit d0b3b7e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
3 changes: 2 additions & 1 deletion compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ type
TNodeKinds* = set[TNodeKind]

type
TSymFlag* = enum # 51 flags!
TSymFlag* = enum # 52 flags!
sfUsed, # read access of sym (for warnings) or simply used
sfExported, # symbol is exported from module
sfFromGeneric, # symbol is instantiation of a generic; this is needed
Expand Down Expand Up @@ -315,6 +315,7 @@ type
sfVirtual # proc is a C++ virtual function
sfByCopy # param is marked as pass bycopy
sfCodegenDecl # type, proc, global or proc param is marked as codegenDecl
sfWasGenSym # symbol was 'gensym'ed

TSymFlags* = set[TSymFlag]

Expand Down
1 change: 1 addition & 0 deletions compiler/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
# internalAssert c.config, false
idTablePut(c.mapping, s, x)
if sfGenSym in s.flags:
# TODO: getIdent(c.ic, "`" & x.name.s & "`gensym" & $c.instID)
result.add newIdentNode(getIdent(c.ic, x.name.s & "`gensym" & $c.instID),
if c.instLines: actual.info else: templ.info)
else:
Expand Down
4 changes: 3 additions & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym =
result.owner = getCurrOwner(c)
else:
result = newSym(kind, considerQuotedIdent(c, n), c.idgen, getCurrOwner(c), n.info)
if find(result.name.s, '`') >= 0:
result.flags.incl sfWasGenSym
#if kind in {skForVar, skLet, skVar} and result.owner.kind == skModule:
# incl(result.flags, sfGlobal)
when defined(nimsuggest):
Expand All @@ -320,7 +322,7 @@ proc semIdentVis(c: PContext, kind: TSymKind, n: PNode,
allowed: TSymFlags): PSym
# identifier with visibility
proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
allowed: TSymFlags): PSym
allowed: TSymFlags, fromTopLevel = false): PSym

proc typeAllowedCheck(c: PContext; info: TLineInfo; typ: PType; kind: TSymKind;
flags: TTypeAllowedFlags = {}) =
Expand Down
2 changes: 1 addition & 1 deletion compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ proc identWithin(n: PNode, s: PIdent): bool =

proc semIdentDef(c: PContext, n: PNode, kind: TSymKind, reportToNimsuggest = true): PSym =
if isTopLevel(c):
result = semIdentWithPragma(c, kind, n, {sfExported})
result = semIdentWithPragma(c, kind, n, {sfExported}, fromTopLevel = true)
incl(result.flags, sfGlobal)
#if kind in {skVar, skLet}:
# echo "global variable here ", n.info, " ", result.name.s
Expand Down
12 changes: 8 additions & 4 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ proc semIdentVis(c: PContext, kind: TSymKind, n: PNode,
result = newSymG(kind, n, c)

proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
allowed: TSymFlags): PSym =
allowed: TSymFlags, fromTopLevel = false): PSym =
if n.kind == nkPragmaExpr:
checkSonsLen(n, 2, c.config)
result = semIdentVis(c, kind, n[0], allowed)
Expand All @@ -536,11 +536,15 @@ proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
else: discard
else:
result = semIdentVis(c, kind, n, allowed)
let invalidPragmasForPush = if fromTopLevel and sfWasGenSym notin result.flags:
{}
else:
{wExportc, wExportCpp, wDynlib}
case kind
of skField: implicitPragmas(c, result, n.info, fieldPragmas)
of skVar: implicitPragmas(c, result, n.info, varPragmas)
of skLet: implicitPragmas(c, result, n.info, letPragmas)
of skConst: implicitPragmas(c, result, n.info, constPragmas)
of skVar: implicitPragmas(c, result, n.info, varPragmas-invalidPragmasForPush)
of skLet: implicitPragmas(c, result, n.info, letPragmas-invalidPragmasForPush)
of skConst: implicitPragmas(c, result, n.info, constPragmas-invalidPragmasForPush)
else: discard

proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) =
Expand Down
26 changes: 26 additions & 0 deletions tests/pragmas/tpush.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,29 @@ proc main(): void =
{.push staticBoundChecks: on.}

main()


{.push exportC.}

block:
proc foo11() =
const factor = [1, 2, 3, 4]
doAssert factor[0] == 1
proc foo21() =
const factor = [1, 2, 3, 4]
doAssert factor[0] == 1

foo11()
foo21()

template foo31() =
let factor = [1, 2, 3, 4]
doAssert factor[0] == 1
template foo41() =
let factor = [1, 2, 3, 4]
doAssert factor[0] == 1

foo31()
foo41()

{.pop.}

0 comments on commit d0b3b7e

Please sign in to comment.