Skip to content

Commit

Permalink
weird fixes after nim-lang#24005, might break CI, simplify diff other…
Browse files Browse the repository at this point in the history
…wise
  • Loading branch information
metagn committed Aug 27, 2024
1 parent 38337dd commit 9a5b07f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
12 changes: 0 additions & 12 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1089,18 +1089,6 @@ proc setInfoRecursive*(n: PNode, info: TLineInfo) =
for i in 0..<n.safeLen: setInfoRecursive(n[i], info)
n.info = info

proc getCallLineInfo*(n: PNode): TLineInfo =
case n.kind
of nkAccQuoted, nkBracketExpr, nkCall, nkCallStrLit, nkCommand:
if len(n) > 0:
return getCallLineInfo(n[0])
of nkDotExpr:
if len(n) > 1:
return getCallLineInfo(n[1])
else:
discard
result = n.info

when defined(useNodeIds):
const nodeIdToDebug* = -1 # 2322968
var gNodeId: int
Expand Down
37 changes: 23 additions & 14 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,18 @@ proc updateDefaultParams(c: PContext, call: PNode) =
def.typ = errorType(c)
call[i] = def

proc getCallLineInfo*(n: PNode): TLineInfo =
case n.kind
of nkAccQuoted, nkBracketExpr, nkCall, nkCallStrLit, nkCommand:
if len(n) > 0:
return getCallLineInfo(n[0])
of nkDotExpr:
if len(n) > 1:
return getCallLineInfo(n[1])
else:
discard
result = n.info

proc inheritBindings(c: PContext, x: var TCandidate, expectedType: PType) =
## Helper proc to inherit bound generic parameters from expectedType into x.
## Does nothing if 'inferGenericTypes' isn't in c.features.
Expand Down Expand Up @@ -816,19 +828,20 @@ proc explicitGenericInstError(c: PContext; n: PNode): PNode =
localError(c.config, getCallLineInfo(n), errCannotInstantiateX % renderTree(n))
result = n

proc explicitGenericSym(m: var TCandidate, n: PNode, s: PSym): PSym =
proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
if s.kind in {skTemplate, skMacro}:
internalError m.c.config, n.info, "cannot get explicitly instantiated symbol of " &
internalError c.config, n.info, "cannot get explicitly instantiated symbol of " &
(if s.kind == skTemplate: "template" else: "macro")
var m = newCandidate(c, s, nil)
matchGenericParams(m, n, s)
if m.state != csMatch:
return nil
var newInst = m.c.semGenerateInstance(m.c, s, m.bindings, n.info)
var newInst = c.semGenerateInstance(c, s, m.bindings, n.info)
newInst.typ.flags.excl tfUnresolved
result = newInst
let info = getCallLineInfo(n)
markUsed(m.c, info, s)
markUsed(c, info, s)
onUse(info, s)
result = newSymNode(newInst, info)

proc setGenericParams(c: PContext, n, expectedParams: PNode) =
## sems generic params in subscript expression
Expand Down Expand Up @@ -858,26 +871,22 @@ proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
localError(c.config, getCallLineInfo(n), errGenerated, "cannot instantiate: '" & renderTree(n) &
"'; got " & $(n.len-1) & " typeof(s) but expected " & $expected)
return n
var m = newCandidate(c, s, nil)
let x = explicitGenericSym(m, n, s)
if x == nil: result = explicitGenericInstError(c, n)
else: result = newSymNode(x, getCallLineInfo(n))
result = explicitGenericSym(c, n, s)
if result == nil: result = explicitGenericInstError(c, n)
elif a.kind in {nkClosedSymChoice, nkOpenSymChoice}:
# choose the generic proc with the proper number of type parameters.
# XXX I think this could be improved by reusing sigmatch.paramTypesMatch.
# It's good enough for now.
let info = getCallLineInfo(n)
result = newNodeI(a.kind, info)
result = newNodeI(a.kind, getCallLineInfo(n))
for i in 0..<a.len:
var candidate = a[i].sym
if candidate.kind in {skProc, skMethod, skConverter,
skFunc, skIterator}:
# it suffices that the candidate has the proper number of generic
# type parameters:
if candidate.ast[genericParamsPos].safeLen == n.len-1:
var m = newCandidate(c, candidate, nil)
let x = explicitGenericSym(m, n, candidate)
if x != nil: result.add newSymNode(x, info)
let x = explicitGenericSym(c, n, candidate)
if x != nil: result.add x
# get rid of nkClosedSymChoice if not ambiguous:
if result.len == 1 and a.kind == nkClosedSymChoice:
result = result[0]
Expand Down
11 changes: 10 additions & 1 deletion compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,18 @@ proc instantiateProcType(c: PContext, pt: TypeMapping,
# call head symbol, because this leads to infinite recursion.
if oldParam.ast != nil:
var def = oldParam.ast.copyTree
# don't know why this doesn't work:
#def = prepareNode(cl, def)
if def.kind in nkCallKinds:
var id: PIdent = nil
if def[0].kind == nkBracketExpr or (def[0].kind in nkCallKinds and
(id = def[0][0].getPIdent; id != nil and id.s == "[]")):
var start = 1
if id != nil: inc start
for j in start ..< def[0].len:
def[0][j] = replaceTypeVarsN(cl, def[0][j])
for i in 1..<def.len:
def[i] = replaceTypeVarsN(cl, def[i], 1)
def[i] = replaceTypeVarsN(cl, def[i])

# allow symchoice since node will be fit later
# although expectedType should cover it
Expand Down
13 changes: 4 additions & 9 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1358,15 +1358,10 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
"either use ';' (semicolon) or explicitly write each default value")
message(c.config, a.info, warnImplicitDefaultValue, msg)
block determineType:
var defTyp = typ
if isCurrentlyGeneric():
defTyp = nil
def = semGenericStmt(c, def)
if hasUnresolvedArgs(c, def):
def.typ = makeTypeFromExpr(c, def.copyTree)
break determineType

def = semExprWithType(c, def, {efDetermineType, efAllowSymChoice}, defTyp)
let isGeneric = isCurrentlyGeneric()
inc c.inGenericContext, ord(isGeneric)
def = semExprWithType(c, def, {efDetermineType, efAllowSymChoice}, typ)
dec c.inGenericContext, ord(isGeneric)
if def.referencesAnotherParam(getCurrOwner(c)):
def.flags.incl nfDefaultRefsParam

Expand Down
6 changes: 6 additions & 0 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ proc typeRel*(c: var TCandidate, f, aOrig: PType,

proc matchGenericParam(m: var TCandidate, formal: PType, n: PNode) =
var arg = n.typ
if m.c.inGenericContext > 0:
while arg != nil and arg.kind == tyGenericParam:
arg = idTableGet(m.bindings, arg)
if arg == nil or arg.containsGenericType:
m.state = csNoMatch
return
# fix up the type to get ready to match formal:
var formalBase = formal
while formalBase.kind == tyGenericParam and
Expand Down
9 changes: 5 additions & 4 deletions tests/proc/texplicitgenerics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ block: # sigmatch can't handle this without pre-instantiating the type:
proc bar[T](f: Foo[T]) = discard
bar[int](foo)

when false: # ditto, but needs #24005 to show the issue
block: # ditto but may be wrong minimization
# minimized from measuremancer
proc foo[T](): T = default(T)
type Foo[T] = object
proc foo[T](): Foo[T] = Foo[T]()
proc bar[T](x = foo[T]()) = discard
bar[int](123)
bar[int](Foo[int]())
# alternative version
proc baz[T](x: typeof(foo[T]())) = discard
baz[int](123)
baz[int](Foo[int]())

0 comments on commit 9a5b07f

Please sign in to comment.