fixes #20883; Unspecified generic on default value segfaults the compiler (#20917)

test CI
This commit is contained in:
ringabout 2022-12-02 05:04:11 +08:00 • committed by GitHub
commit 10b6e4f5b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -81,6 +81,7 @@ type
info*: TLineInfo info*: TLineInfo
allowMetaTypes*: bool # allow types such as seq[Number] allowMetaTypes*: bool # allow types such as seq[Number]
# i.e. the result contains unresolved generics # i.e. the result contains unresolved generics
fromStaticExpr*: bool
skipTypedesc*: bool # whether we should skip typeDescs skipTypedesc*: bool # whether we should skip typeDescs
isReturnType*: bool isReturnType*: bool
owner*: PSym # where this instantiation comes from owner*: PSym # where this instantiation comes from
@ -238,6 +239,8 @@ proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0): PNode =
assert result.kind notin nkCallKinds assert result.kind notin nkCallKinds
else: else:
if n.len > 0: if n.len > 0:
if n.kind in nkCallKinds and n[0].kind == nkIdent and cl.fromStaticExpr:
localError(cl.c.config, n.info, "An unresolved call in staticExpr: '" & renderTree(n) & "'")
newSons(result, n.len) newSons(result, n.len)
if start > 0: if start > 0:
result[0] = n[0] result[0] = n[0]
@ -660,10 +663,11 @@ proc initTypeVars*(p: PContext, typeMap: LayeredIdTable, info: TLineInfo;
result.owner = owner result.owner = owner
proc replaceTypesInBody*(p: PContext, pt: TIdTable, n: PNode; proc replaceTypesInBody*(p: PContext, pt: TIdTable, n: PNode;
owner: PSym, allowMetaTypes = false): PNode = owner: PSym, allowMetaTypes = false, fromStaticExpr = false): PNode =
var typeMap = initLayeredTypeMap(pt) var typeMap = initLayeredTypeMap(pt)
var cl = initTypeVars(p, typeMap, n.info, owner) var cl = initTypeVars(p, typeMap, n.info, owner)
cl.allowMetaTypes = allowMetaTypes cl.allowMetaTypes = allowMetaTypes
cl.fromStaticExpr = fromStaticExpr
pushInfoContext(p.config, n.info) pushInfoContext(p.config, n.info)
result = replaceTypeVarsN(cl, n) result = replaceTypeVarsN(cl, n)
popInfoContext(p.config) popInfoContext(p.config)

View file

@ -807,7 +807,8 @@ proc tryResolvingStaticExpr(c: var TCandidate, n: PNode,
# N is bound to a concrete value during the matching of the first param. # N is bound to a concrete value during the matching of the first param.
# This proc is used to evaluate such static expressions. # This proc is used to evaluate such static expressions.
let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil, let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil,
allowMetaTypes = allowUnresolved) allowMetaTypes = allowUnresolved,
fromStaticExpr = true)
result = c.c.semExpr(c.c, instantiated) result = c.c.semExpr(c.c, instantiated)
proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool = proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool =