Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #10833 type expected error for proc resolution with static[int]] #20423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,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 @@ -660,6 +660,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 @@ -118,6 +118,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 @@ -1293,7 +1293,8 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
if s.astdef.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 or efInTypeof in flags) and typ.n != nil:
result = typ.n
result.typ = typ.base
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/generics/t10833.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Foo*[A; B; C: static[int]] = object
s: string
f: typeof(C)

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

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"
doAssert r.f == 1
27 changes: 27 additions & 0 deletions tests/generics/t10833_2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type MyObject = object
x, y: int
s: string

const c = MyObject(x: 1, y: 2, s: "")

type Foo[A, B; C: static MyObject] = object
s: string
f: typeof(C)

proc build*[A; B; C: static MyObject](s: string;): Foo[A, B, C] =
const d = C
doAssert d.x == c.x
doAssert d.x == 1
result.f = d
result.s = s

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

type
Bar = object
Baz = object

let r = build[Bar, Baz, c]()
doAssert r.s == "foo"
doAssert r.f == c
27 changes: 27 additions & 0 deletions tests/generics/t10833_3.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type MyObject = object
x, y: int
s: string

const c = @[MyObject(x: 1, y: 2, s: "")]

type Foo[A, B; C: static seq[MyObject]] = object
s: string
f: typeof(C)

proc build*[A; B; C: static seq[MyObject]](s: string;): Foo[A, B, C] =
const d = C
doAssert d[0].x == c[0].x
doAssert d[0].x == 1
result.f = d
result.s = s

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

type
Bar = object
Baz = object

let r = build[Bar, Baz, c]()
doAssert r.s == "foo"
doAssert r.f == c