Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed May 25, 2021
1 parent a174db1 commit 8c4cf3f
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 27 deletions.
29 changes: 26 additions & 3 deletions compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,35 @@ proc someSymFromImportTable*(c: PContext; name: PIdent; ambiguous: var bool): PS
ambiguous = true

proc searchInScopes*(c: PContext, s: PIdent; ambiguous: var bool): PSym =
#[
This is the key algorithm to solve the generic sandwich problem:
Inside generics, we stop the search at c.genericInstStack[^1] unless we found
a mixin; this means:
# module m1:
proc bar1()=discard
proc fn*[T]
mixin bar3
proc bar2() = discard
bar1() # ok, this is resolved as a symbol during generic prepass
bar2() # ok, ditto
bar3() # ok, this is resolved as a skMixin symbol during generic prepass
bar4() # error, this is not visible
bar5() # error, ditto, even if bar5 is at module scope in m2
# module m2:
import m1
proc bar5() = discard
proc main =
proc bar3() = discard
proc bar4() = discard
fn[int]()
]#
var foundMixin = false
for scope in allScopes(c.currentScope):
result = strTableGet(scope.symbols, s)
if result != nil:
if result.kind == skMixin: # TODO: not for generic prepass?
if result.kind == skMixin:
foundMixin = true
continue
if c.inGenericInst > 0 and not foundMixin:
Expand All @@ -205,8 +229,7 @@ proc searchInScopes*(c: PContext, s: PIdent; ambiguous: var bool): PSym =
else:
return nil
return result
if c.inGenericInst > 0 and not foundMixin: # PRTEMP
return nil
if c.inGenericInst > 0 and not foundMixin: return nil
result = someSymFromImportTable(c, s, ambiguous)

proc debugScopes*(c: PContext; limit=0, max = int.high) {.deprecated.} =
Expand Down
3 changes: 0 additions & 3 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,6 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
let oldInUnrolledContext = c.inUnrolledContext
let oldInGenericInst = c.inGenericInst
let oldgenericInstStackLen = c.genericInstStack.len
# assert c.genericInstStack.len == c.inGenericInst
let oldInStaticContext = c.inStaticContext
let oldProcCon = c.p
c.generics = @[]
Expand All @@ -2169,8 +2168,6 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
c.inUnrolledContext = oldInUnrolledContext
c.inGenericInst = oldInGenericInst

# assert c.genericInstStack.len >= oldInGenericInst
# c.genericInstStack.setLen oldInGenericInst
assert c.genericInstStack.len >= oldgenericInstStackLen
c.genericInstStack.setLen oldgenericInstStackLen

Expand Down
5 changes: 0 additions & 5 deletions compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ proc instGenericContainer2(cl: var TReplTypeVars, info: TLineInfo, header: PType
# perhaps the code can be extracted in a shared function.
openScope(c)

# if t != nil and t.sym != nil:
# cl.c.genericInstStack.add t.sym
cl.c.genericInstStack.add header.sym

let genericTyp = header.base
Expand All @@ -269,8 +267,6 @@ proc instGenericContainer2(cl: var TReplTypeVars, info: TLineInfo, header: PType

result = replaceTypeVarsT(cl, header)

# if t != nil and t.sym != nil:
# discard cl.c.genericInstStack.pop
discard cl.c.genericInstStack.pop

closeScope(c)
Expand Down Expand Up @@ -472,7 +468,6 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
popInfoContext(c.config)
closeScope(c) # close scope for parameters
closeScope(c) # close scope for 'mixin' declarations
# discard c.genericInstStack.pop
popOwner(c)
c.currentScope = oldScope
discard c.friendModules.pop()
Expand Down
3 changes: 1 addition & 2 deletions compiler/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ proc semMixinStmt(c: PContext, n: PNode, toMixin: var IntSet): PNode =
result = copyNode(n)
for i in 0..<n.len:
toMixin.incl(considerQuotedIdent(c, n[i]).id)
let s = symChoice(c, n[i], nil, scForceOpen)
result.add s
result.add symChoice(c, n[i], nil, scForceOpen)

proc replaceIdentBySym(c: PContext; n: var PNode, s: PNode) =
case n.kind
Expand Down
7 changes: 1 addition & 6 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,6 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
of nkTupleConstr: result = semAnonTuple(c, n, prev)
of nkCallKinds:
let x = n[0]

let ident = case x.kind
of nkIdent: x.ident
of nkSym: x.sym.name
Expand Down Expand Up @@ -1837,11 +1836,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
result = semTypeExpr(c, n[1], prev)
else:
if c.inGenericContext > 0 and n.kind == nkCall:
#[
eg:
type Foo[T; N: static int]=object
f0: fun(T, N, int, 3)
]#
# e.g.: type Foo[T; N: static int]=object: f0: fun(T, N, int, 3)
let n2 = semGenericStmtInTypeSection(c, n.copyTree)
result = makeTypeFromExpr(c, n2)
else:
Expand Down
3 changes: 1 addition & 2 deletions compiler/semtypinst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0): PNode =
case n.kind
of nkNone..pred(nkSym), succ(nkSym)..nkNilLit:
discard
of nkOpenSymChoice, nkClosedSymChoice:
result = n
of nkOpenSymChoice, nkClosedSymChoice: result = n
of nkSym:
result.sym = replaceTypeVarsS(cl, n.sym)
if result.sym.typ.kind == tyVoid:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ proc matchUserTypeClass*(m: var TCandidate; ff, a: PType): PType =
typeClass[0][0] = a
c.matchedConcept = addr(matchedConceptContext)
let genericInstStackLenOld = c.genericInstStack.len
c.genericInstStack.add typeClass.sym # PRTEMP : desync w inGenericInst ?
c.genericInstStack.add typeClass.sym

# PRTEMP similar to D20210521T170223
pushOwner(c, typeClass.sym)
Expand Down
3 changes: 1 addition & 2 deletions compiler/suggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Sugge

proc nameFits(c: PContext, s: PSym, n: PNode): bool =
var op = if n.kind in nkCallKinds: n[0] else: n
if op.kind in {nkOpenSymChoice, nkClosedSymChoice}:
op = op[0]
if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op[0]
if op.kind == nkDotExpr: op = op[1]
var opr: PIdent
case op.kind
Expand Down
3 changes: 0 additions & 3 deletions lib/system/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,6 @@ proc contains[T](list, x: T): bool =
it = it.next

proc listAdd[T](head: var T, c: T) {.inline.} =
# mixin cstderr
# let zoo = cstderr
# discard cstderr
sysAssert(c notin head, "listAdd 1")
sysAssert c.prev == nil, "listAdd 2"
sysAssert c.next == nil, "listAdd 3"
Expand Down

0 comments on commit 8c4cf3f

Please sign in to comment.