Skip to content

Commit

Permalink
generate symchoice for ambiguous types in templates + allow types in …
Browse files Browse the repository at this point in the history
…symchoices

fixes nim-lang#23898
  • Loading branch information
metagn committed Aug 21, 2024
1 parent 1dbae4f commit 4fd4d53
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
19 changes: 17 additions & 2 deletions compiler/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule;
# (s.kind notin routineKinds or s.magic != mNone):
# for instance 'nextTry' is both in tables.nim and astalgo.nim ...
if not isField or sfGenSym notin s.flags:
result = newSymNode(s, info)
if s.kind == skType:
result = newSymNodeTypeDesc(s, c.idgen, info)
else:
result = newSymNode(s, info)
markUsed(c, info, s)
onUse(info, s)
else:
Expand All @@ -85,7 +88,10 @@ proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule;
if a.kind != skModule and (not isField or sfGenSym notin a.flags):
incl(a.flags, sfUsed)
markOwnerModuleAsUsed(c, a)
result.add newSymNode(a, info)
if a.kind == skType:
result.add newSymNodeTypeDesc(a, c.idgen, info)
else:
result.add newSymNode(a, info)
onUse(info, a)
a = nextOverloadIter(o, c, n)

Expand Down Expand Up @@ -245,6 +251,11 @@ proc semTemplSymbol(c: PContext, n: PNode, s: PSym; isField: bool): PNode =
result = n
of skType:
if isField and sfGenSym in s.flags: result = n
elif c.isAmbiguous:
# ambiguous types should be symchoices since lookup behaves differently
# for them in regular expressions,
# make sure c.isAmbiguous is set correctly before calling this proc
result = symChoice(c, n, s, scOpen, isField)
else: result = newSymNodeTypeDesc(s, c.idgen, n.info)
else:
if isField and sfGenSym in s.flags: result = n
Expand Down Expand Up @@ -345,6 +356,8 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
case n.kind
of nkIdent:
if n.ident.id in c.toInject: return n
# used in `semTemplSymbol`:
c.c.isAmbiguous = false
let s = qualifiedLookUp(c.c, n, {})
if s != nil:
if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags:
Expand Down Expand Up @@ -558,6 +571,8 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
of nkDotExpr, nkAccQuoted:
# dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
# so we use the generic code for nkDotExpr too
# used in `semTemplSymbol`:
c.c.isAmbiguous = false
let s = qualifiedLookUp(c.c, n, {})
if s != nil:
# mirror the nkIdent case
Expand Down
5 changes: 2 additions & 3 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2426,9 +2426,8 @@ proc paramTypesMatch*(m: var TCandidate, f, a: PType,
if arg[i].sym.kind in matchSet:
copyCandidate(z, m)
z.callee = arg[i].typ
if arg[i].sym.kind == skType and z.callee.kind != tyTypeDesc:
z.callee = newType(tyTypeDesc, c.idgen, arg[i].sym.owner)
z.callee.addSonSkipIntLit(arg[i].sym.typ, c.idgen)
# should read like "if symbol is type, type should be typedesc":
assert arg[i].sym.kind != skType or z.callee.kind == tyTypeDesc
if tfUnresolved in z.callee.flags: continue
z.calleeSym = arg[i].sym
z.calleeScope = cmpScopes(m.c, arg[i].sym)
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/lookups/mambtype2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ./mambtype1
export mambtype1
template K*(kind: static int): auto = typedesc[mambtype1.K]
template B*(kind: static int): auto = typedesc[mambtype1.K]
4 changes: 0 additions & 4 deletions tests/lookups/mqualifiedamb2.nim

This file was deleted.

13 changes: 13 additions & 0 deletions tests/lookups/tambtype.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ./mambtype2

block: # issue #23893
discard default(K(0)) # works
discard default(mambtype2.B(0)) # works
discard default(mambtype2.K(0)) # doesn't work

block: # issue #23898, in template
template r() =
discard default(B(0)) # compiles
discard default(mambtype2.B(0)) # compiles
discard default(K(0)) # does not compile
r()
4 changes: 0 additions & 4 deletions tests/lookups/tqualifiedamb.nim

This file was deleted.

0 comments on commit 4fd4d53

Please sign in to comment.