Skip to content

Commit

Permalink
fix scoping regression with calls in generic bodies (#22115)
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Jun 17, 2023
1 parent 647d961 commit 6d21637
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ proc semExprFlagDispatched(c: PContext, n: PNode, flags: TExprFlags; expectedTyp
evaluated = evalAtCompileTime(c, result)
if evaluated != nil: return evaluated

proc semGenericStmt(c: PContext, n: PNode): PNode

include hlo, seminst, semcall

proc resetSemFlag(n: PNode) =
Expand Down Expand Up @@ -513,8 +515,6 @@ proc semConstBoolExpr(c: PContext, n: PNode): PNode =
result = forceBool(c, semConstExpr(c, n, getSysType(c.graph, n.info, tyBool)))
if result.kind != nkIntLit:
localError(c.config, n.info, errConstExprExpected)

proc semGenericStmt(c: PContext, n: PNode): PNode
proc semConceptBody(c: PContext, n: PNode): PNode

include semtypes
Expand Down
2 changes: 1 addition & 1 deletion compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ proc semOverloadedCall(c: PContext, n, nOrig: PNode,
result = semResolvedCall(c, r, n, flags)
else:
if efDetermineType in flags and c.inGenericContext > 0 and c.matchedConcept == nil:
result = n
result = semGenericStmt(c, n)
result.typ = makeTypeFromExpr(c, result.copyTree)
elif efExplain notin flags:
# repeat the overload resolution,
Expand Down
26 changes: 26 additions & 0 deletions tests/generics/muninstantiatedgenericcalls.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import std/bitops

const
lengths = block:
var v: array[64, int8]
for i in 0..<64:
v[i] = int8((i + 7) div 7)
v

type
Leb128* = object

{.push checks: off.}
func len(T: type Leb128, x: SomeUnsignedInt): int8 =
if x == 0: 1
else: lengths[fastLog2(x)]
{.pop.}

# note private to test scoping issue:
func maxLen(T: type Leb128, I: type): int8 =
Leb128.len(I.high)

type
Leb128Buf*[T: SomeUnsignedInt] = object
data*: array[maxLen(Leb128, T), byte]
len*: int8
8 changes: 8 additions & 0 deletions tests/generics/tuninstantiatedgenericcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ block thardcases:
doAssert high(f2.data3) == 4 # length of int8

doAssert f2.data3[0] is float

import muninstantiatedgenericcalls

block:
var x: Leb128Buf[uint32]
doAssert x.data.len == 5
var y: Leb128Buf[uint16]
doAssert y.data.len == 3

0 comments on commit 6d21637

Please sign in to comment.