From 6d2163724550ed3a115d22e00f9d40150534a8b9 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 17 Jun 2023 10:24:32 +0300 Subject: [PATCH] fix scoping regression with calls in generic bodies (#22115) refs #22029, refs https://github.com/status-im/nim-libp2p/actions/runs/5263850340/jobs/9514434659 --- compiler/sem.nim | 4 +-- compiler/semcall.nim | 2 +- .../generics/muninstantiatedgenericcalls.nim | 26 +++++++++++++++++++ .../generics/tuninstantiatedgenericcalls.nim | 8 ++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/generics/muninstantiatedgenericcalls.nim diff --git a/compiler/sem.nim b/compiler/sem.nim index de78667ba376..ca2c6bc005f4 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -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) = @@ -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 diff --git a/compiler/semcall.nim b/compiler/semcall.nim index c0317f4d68e3..d2460ab06df7 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -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, diff --git a/tests/generics/muninstantiatedgenericcalls.nim b/tests/generics/muninstantiatedgenericcalls.nim new file mode 100644 index 000000000000..caed07c98d45 --- /dev/null +++ b/tests/generics/muninstantiatedgenericcalls.nim @@ -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 diff --git a/tests/generics/tuninstantiatedgenericcalls.nim b/tests/generics/tuninstantiatedgenericcalls.nim index c4d95a0fb021..b12a33fe799d 100644 --- a/tests/generics/tuninstantiatedgenericcalls.nim +++ b/tests/generics/tuninstantiatedgenericcalls.nim @@ -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