summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBung <crc32@qq.com>2023-08-04 19:35:43 +0800
committerGitHub <noreply@github.com>2023-08-04 13:35:43 +0200
commit26f183043f9e58eb4954d50a5d130d8684909936 (patch)
tree22935c744575ffed86fddc340317502e1c73dd1d
parent3efabd3ec669914ad2bb42a614f7277caf662562 (diff)
downloadNim-26f183043f9e58eb4954d50a5d130d8684909936.tar.gz
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
-rw-r--r--compiler/semexprs.nim3
-rw-r--r--compiler/sigmatch.nim5
-rw-r--r--tests/misc/t20883.nim12
3 files changed, 20 insertions, 0 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 398424bbf..b7fc7a9bd 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -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):
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index d1b3b5dfb..12cc1fcb1 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -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)
diff --git a/tests/misc/t20883.nim b/tests/misc/t20883.nim
new file mode 100644
index 000000000..d98feaa14
--- /dev/null
+++ b/tests/misc/t20883.nim
@@ -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()