Skip to content

Commit

Permalink
test removing allowMetaTypes for inferStaticParam
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 8, 2024
1 parent 7cd1777 commit 098f2d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
10 changes: 3 additions & 7 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/proc/tstaticsignature.nim
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,15 @@ block: # issue #22607, needs nkWhenStmt to be handled like nkRecWhen
test[true](1.int)
test[false](1.0)
doAssert not compiles(test[])

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)

0 comments on commit 098f2d9

Please sign in to comment.