diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 5faefc9aab92..c6cb7ac42757 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -385,6 +385,7 @@ proc semArrayIndex(c: PContext, n: PNode): PType = let e = semExprWithType(c, n, {efDetermineType}) if e.typ.kind == tyFromExpr: result = makeRangeWithStaticExpr(c, e.typ.n) + result.flags.incl tfUnresolved elif e.kind in {nkIntLit..nkUInt64Lit}: if e.intVal < 0: localError(c.config, n.info, @@ -411,6 +412,10 @@ proc semArrayIndex(c: PContext, n: PNode): PType = # properly filled-out in semtypinst (see how tyStaticExpr # is handled there). result = makeRangeWithStaticExpr(c, e) + # makeRangeWithStaticExpr doesn't mark range as unresolved unless + # type of e is nil or has nil node, but we know it's unresolved + # even if it has a type because of hasUnresolvedArgs + result.flags.incl tfUnresolved elif e.kind == nkIdent: result = e.typ.skipTypes({tyTypeDesc}) else: diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index e519f17c8c85..cb3789c2a77e 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -996,7 +996,6 @@ proc maybeSkipDistinct(m: TCandidate; t: PType, callee: PSym): PType = result = t proc tryResolvingStaticExpr(c: var TCandidate, n: PNode, - allowUnresolved = false, allowCalls = false, expectedType: PType = nil): PNode = # Consider this example: @@ -1005,8 +1004,7 @@ proc tryResolvingStaticExpr(c: var TCandidate, n: PNode, # Here, N-1 will be initially nkStaticExpr that can be evaluated only after # N is bound to a concrete value during the matching of the first param. # This proc is used to evaluate such static expressions. - let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil, - allowMetaTypes = allowUnresolved) + let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil) if not allowCalls and instantiated.kind in nkCallKinds: return nil result = c.c.semExpr(c.c, instantiated) @@ -1100,10 +1098,8 @@ proc failureToInferStaticParam(conf: ConfigRef; n: PNode) = proc inferStaticsInRange(c: var TCandidate, inferred, concrete: PType): TTypeRelation = - let lowerBound = tryResolvingStaticExpr(c, inferred.n[0], - allowUnresolved = true) - let upperBound = tryResolvingStaticExpr(c, inferred.n[1], - allowUnresolved = true) + let lowerBound = tryResolvingStaticExpr(c, inferred.n[0], allowCalls = true) + let upperBound = tryResolvingStaticExpr(c, inferred.n[1], allowCalls = true) template doInferStatic(e: PNode, r: Int128) = var exp = e var rhs = r diff --git a/tests/proc/tstaticsignature.nim b/tests/proc/tstaticsignature.nim index 518c88ba5571..7ba4f7971426 100644 --- a/tests/proc/tstaticsignature.nim +++ b/tests/proc/tstaticsignature.nim @@ -250,3 +250,15 @@ block: # `when` in static signature proc foo[T](): T = test() proc bar[T](x = foo[T]()): T = x doAssert bar[int]() == 123 + +block: # issue #19923 + type Test[S: static[Natural]] = object + proc run(self: Test, idx: 0..(self.S * 8)) = discard + # This causes segfault ^^^^^^^^^^^^ + proc run(self: Test, a: array[self.S * 8, int]) = discard + # And this too ^^^^^^^^^^ + var x = Test[3]() + var y: array[24, int] + run(x, y.low) + var z: array[x.S * 8, int] + run(x, z)