Skip to content

Commit

Permalink
unify explicit generic param semchecking in calls (#22618)
Browse files Browse the repository at this point in the history
fixes #9040

(cherry picked from commit 6738f44)
  • Loading branch information
metagn authored and narimiran committed Sep 12, 2023
1 parent 965eeb6 commit 106cde1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
8 changes: 6 additions & 2 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,18 @@ proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
onUse(info, s)
result = newSymNode(newInst, info)

proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
assert n.kind == nkBracketExpr
proc setGenericParams(c: PContext, n: PNode) =
## sems generic params in subscript expression
for i in 1..<n.len:
let e = semExprWithType(c, n[i])
if e.typ == nil:
n[i].typ = errorType(c)
else:
n[i].typ = e.typ.skipTypes({tyTypeDesc})

proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
assert n.kind == nkBracketExpr
setGenericParams(c, n)
var s = s
var a = n[0]
if a.kind == nkSym:
Expand Down
4 changes: 0 additions & 4 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,6 @@ proc bracketedMacro(n: PNode): PSym =
if result.kind notin {skMacro, skTemplate}:
result = nil

proc setGenericParams(c: PContext, n: PNode) =
for i in 1..<n.len:
n[i].typ = semTypeNode(c, n[i], nil)

proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
if efNoSemCheck notin flags and n.typ != nil and n.typ.kind == tyError:
return errorNode(c, n)
Expand Down
21 changes: 20 additions & 1 deletion tests/generics/timplicit_and_explicit.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,23 @@ block: #4688

block: #4164
proc printStr[T](s: static[string]): T = discard
discard printStr[int]("hello static")
discard printStr[int]("hello static")

import macros

block: # issue #9040, statics with template, macro, symchoice explicit generics
block: # macro
macro fun[N: static int](): untyped =
newLit 1
const a = fun[2]()
doAssert a == 1
block: # template
template fun[N: static int](): untyped =
1
const a = fun[2]()
doAssert a == 1
block: # symchoice
proc newSeq[x: static int](): int = 1
template foo: int =
newSeq[2]()
doAssert foo() == 1

0 comments on commit 106cde1

Please sign in to comment.