Skip to content

Commit

Permalink
cleanups; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 22, 2021
1 parent f0c4109 commit c365213
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
4 changes: 3 additions & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,14 @@ proc myProcess(context: PPassContext, n: PNode): PNode {.nosinks.} =
result = semStmtAndGenerateGenerics(c, n)
else:
let oldContextLen = msgs.getInfoContextLen(c.config)
let oldInGenericInst = c.inGenericInst # PRTEMP: genericInstStack ?
let oldInGenericInst = c.inGenericInst
let oldgenericInstStackLen = c.genericInstStack.len
try:
result = semStmtAndGenerateGenerics(c, n)
except ERecoverableError, ESuggestDone:
recoverContext(c)
c.inGenericInst = oldInGenericInst
c.genericInstStack.setLen oldgenericInstStackLen
msgs.setInfoContextLen(c.config, oldContextLen)
if getCurrentException() of ESuggestDone:
c.suggestionsMade = true
Expand Down
15 changes: 1 addition & 14 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,7 @@ proc semGenericStmt(c: PContext, n: PNode,
n[bodyPos] = semGenericStmtScope(c, body, flags, ctx)
closeScope(c)
of nkPragma, nkPragmaExpr:
#[
# TODO: nkPragmaBlock ?
D20210521T115234 PRTEMP
{.define(foo).}
{.noSideEffect.}
{.warning[resultshadowed]: zook.}: discard
{.pushwarning[resultshadowed]: zook.}
{.push, warning[resultshadowed]: off.}
{.pop.}
{.push warnings: off.}
{.push warning[GcMem]: off, warning[Uninit]: off.}

PRTEMP: FACTOR with D20210521T130909
]#
# similar to treatment in `semTemplBody`
for i in 0..<n.len:
if n[i].kind == nkExprColonExpr:
if n[i][0].kind == nkIdent and getIdent(c.cache, $wPragma) == n[i][0].ident:
Expand Down
3 changes: 1 addition & 2 deletions compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ proc fixupInstantiatedSymbols(c: PContext, s: PSym) =
for i in 0..<c.generics.len:
if c.generics[i].genericSym.id == s.id:
var oldPrc = c.generics[i].inst.sym
# D20210521T170223:here
pushProcCon(c, oldPrc)
pushOwner(c, oldPrc)
pushInfoContext(c.config, oldPrc.info)
Expand Down Expand Up @@ -318,7 +317,7 @@ proc instantiateProcType(c: PContext, pt: TIdTable,
resetIdTable(cl.localCache)
cl.isReturnType = true

if result[0] != nil and result[0].kind == tyGenericInvocation: # PRTEMP
if result[0] != nil and result[0].kind == tyGenericInvocation:
result[0] = instGenericContainerImpl(cl, info, result[0], isReplaceVars = true)
else:
result[0] = replaceTypeVarsT(cl, result[0])
Expand Down
2 changes: 1 addition & 1 deletion compiler/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
result[0] = semTemplBody(c, n[0])
of nkPostfix:
result[1] = semTemplBody(c, n[1])
of nkPragma: # D20210521T130909:here
of nkPragma:
for x in n:
if x.kind == nkExprColonExpr:
x[1] = semTemplBody(c, x[1])
Expand Down
2 changes: 1 addition & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ proc matchUserTypeClass*(m: var TCandidate; ff, a: PType): PType =
let genericInstStackLenOld = c.genericInstStack.len
c.genericInstStack.add typeClass.sym

# PRTEMP similar to D20210521T170223
# similar to `fixupInstantiatedSymbols`
pushOwner(c, typeClass.sym)
# pushInfoContext(c.config, oldPrc.info)

Expand Down
40 changes: 37 additions & 3 deletions tests/generics/t13747.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
discard """
joinable: false
matrix: "-d:t13747_case1 -d:t13747_case2 -d:t13747_case3 -d:t13747_case4 -d:t13747_case5 -d:t13747_case6 -d:t13747_case7 -d:t13747_case8 -d:t13747_case9"
matrix: "-d:t13747_case1 -d:t13747_case2 -d:t13747_case3 -d:t13747_case4 -d:t13747_case5 -d:t13747_case6 -d:t13747_case7 -d:t13747_case8 -d:t13747_case9 -d:t13747_case10 -d:t13747_case11"
# this allows testing each one individually; each of those (except t13747_case1, t13747_case7, t13747_case8) were failing
"""

# bug #13747 generic sandwich non-module scope symbols were ignored

# keep these at module scope
{.define(nimCompilerDebug).}
when defined(t13747_case1):
# every symbol suffixed by V1, represents -d:case2a1 from https://github.com/nim-lang/Nim/issues/13747#issuecomment-615992993
proc byValImpl1V1(T: typedesc, valV1: int): auto =
Expand Down Expand Up @@ -146,10 +145,45 @@ when defined t13747_case8: # bug #2752

# tests that need an stdlib import come after

when defined(t13747_case9): # bug #13970
when defined t13747_case9: # bug #13970
# (also reported in https://github.com/nim-lang/Nim/issues/13747#issuecomment-612905795)
import algorithm
block:
var a = @[(1, @['a']), (4, @['d']), (3, @['c']), (2, @['b'])]
proc `<`(x, y: (int, seq[char])): bool = x[0] < y[0]
sort(a) # was CT error

when defined t13747_case10:
# example with a pragma: make sure `off` is resolved here
proc fn1[T](a: T) =
{.warning[resultshadowed]: off.}: discard
fn1(1)

when defined t13747_case11:
# BUG D20210621T173756:here: this test was failing with `Error: type mismatch`
proc fn1[T](a: T) =
{.warning[resultshadowed]: off.}:
discard
const off = "asdf"
fn1(1)

when defined t13747_case12:
# more pragmas
proc fn1[T](a: T) =
mixin off2
mixin resultshadowed2
{.define(t13747_case12_sub).}
{.noSideEffect.}: discard
{.noSideEffect2.}: discard
{.warning[resultshadowed]: off2.}: discard
{.push warning[resultshadowed]: off2.}
{.push, warning[resultshadowed]: off.}
{.pop.}
{.push warnings: off.}
{.push warning[GcMem]: off, warning[Uninit]: off.}
block:
doAssert not compiles(fn1(1))
const off2 = off
{.pragma: noSideEffect2, noSideEffect.}
doAssert compiles(fn1(1))
fn1(1)

0 comments on commit c365213

Please sign in to comment.