Skip to content

Commit

Permalink
fix #10833 type expected error for proc resolution with static[int]]
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Sep 26, 2022
1 parent be4bd8a commit cee38fc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ proc explicitGenericInstError(c: PContext; n: PNode): PNode =
proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
# binding has to stay 'nil' for this to work!
var m = newCandidate(c, s, nil)

inc c.inExplicitGenericSym
for i in 1..<n.len:
let formal = s.ast[genericParamsPos][i-1].typ
var arg = n[i].typ
Expand All @@ -635,6 +635,7 @@ proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
let info = getCallLineInfo(n)
markUsed(c, info, s)
onUse(info, s)
dec c.inExplicitGenericSym
result = newSymNode(newInst, info)

proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
Expand Down
1 change: 1 addition & 0 deletions compiler/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type
compilesContextId*: int # > 0 if we are in a ``compiles`` magic
compilesContextIdGenerator*: int
inGenericInst*: int # > 0 if we are instantiating a generic
inExplicitGenericSym*: int
converters*: seq[PSym]
patterns*: seq[PSym] # sequence of pattern matchers
optionStack*: seq[POptionEntry]
Expand Down
3 changes: 2 additions & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,8 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
if s.ast.safeLen == 0: result = inlineConst(c, n, s)
else: result = newSymNode(s, n.info)
of tyStatic:
if typ.n != nil:
let staticDuringGenericInst = (c.inStaticContext > 0 and c.inGenericInst > 0)
if (c.inExplicitGenericSym <= 0 or staticDuringGenericInst) and typ.n != nil:
result = typ.n
result.typ = typ.base
else:
Expand Down
7 changes: 5 additions & 2 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
if n.kind == nkSym:
result = getGenSym(c, n.sym)
else:
result = pickSym(c, n, {skType, skGenericParam, skParam})
result = pickSym(c, n, {skType, skGenericParam, skParam, skConst})
if result.isNil:
result = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared})
if result != nil:
Expand Down Expand Up @@ -437,7 +437,7 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
else:
localError(c.config, n.info, errTypeExpected)
return errorSym(c, n)
if result.kind != skType and result.magic notin {mStatic, mType, mTypeOf}:
if result.kind notin {skType, skConst} and result.magic notin {mStatic, mType, mTypeOf}:
# this implements the wanted ``var v: V, x: V`` feature ...
var ov: TOverloadIter
var amb = initOverloadIter(ov, c, n)
Expand All @@ -448,6 +448,9 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
if result.kind != skError: localError(c.config, n.info, errTypeExpected)
return errorSym(c, n)
if result.typ.kind != tyGenericParam:
if result.kind == skConst:
return result

# XXX get rid of this hack!
var oldInfo = n.info
when defined(useNodeIds):
Expand Down
16 changes: 16 additions & 0 deletions tests/generics/t10833.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Foo*[A; B; C: static[int]] = object
s: string
# d: typeof(C)

proc build*[A; B; C: static[int]](s: string;): Foo[A, B, C] =
const d = C
result.s = s

proc build*[A; B; C: static[int]](): Foo[A, B, C] =
build[A, B, C]("foo")

type
Bar = object
Baz = object
let r = build[Bar, Baz, 1]()
doAssert r.s == "foo"

0 comments on commit cee38fc

Please sign in to comment.