fix #20883 Unspecified generic on default value segfaults the compiler (#21172)

* fix #20883 Unspecified generic on default value segfaults the compiler

* fallback to isGeneric

* change to closer error

* Update t20883.nim

(cherry picked from commit 26f183043f)
This commit is contained in:
Bung
2023-08-04 19:35:43 +08:00
committed by narimiran
parent d097028307
commit f4d99c8d56
3 changed files with 20 additions and 0 deletions

View File

@@ -238,6 +238,9 @@ proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
result = convNotInRange
else:
# we use d, s here to speed up that operation a bit:
if d.kind == tyFromExpr:
result = convNotLegal
return
case cmpTypes(c, d, s)
of isNone, isGeneric:
if not compareTypes(targetTyp.skipTypes(abstractVar), srcTyp.skipTypes({tyOwned}), dcEqIgnoreDistinct):

View File

@@ -819,6 +819,8 @@ proc tryResolvingStaticExpr(c: var TCandidate, n: PNode,
# This proc is used to evaluate such static expressions.
let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil,
allowMetaTypes = allowUnresolved)
if instantiated.kind in nkCallKinds:
return nil
result = c.c.semExpr(c.c, instantiated)
proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool =
@@ -1887,6 +1889,9 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
# fix the expression, so it contains the already instantiated types
if f.n == nil or f.n.kind == nkEmpty: return isGeneric
let reevaluated = tryResolvingStaticExpr(c, f.n)
if reevaluated == nil:
result = isNone
return
case reevaluated.typ.kind
of tyTypeDesc:
result = typeRel(c, a, reevaluated.typ.base, flags)

12
tests/misc/t20883.nim Normal file
View File

@@ -0,0 +1,12 @@
discard """
action: reject
errormsg: "type mismatch: got <float64> but expected 'typeof(U(0.000001))'"
line: 8
column: 22
"""
proc foo*[U](x: U = U(1e-6)) =
echo x
foo[float]()
foo()