summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-10-15 11:50:12 +0200
committerAraq <rumpf_a@web.de>2018-10-15 11:50:12 +0200
commitbf01d7136eaa8ac545a4fb74869bfa5834917b3c (patch)
tree15f7010e43d924a37fc03e6caba3208ba52822fd
parent8252c65cf2501b8d6401e7d8295df374ef6b1994 (diff)
parent541c2a3fecd7b1f3e6d9dc7e23a7583000cb68f1 (diff)
downloadNim-bf01d7136eaa8ac545a4fb74869bfa5834917b3c.tar.gz
Merge branch 'Fixes-7845' of https://github.com/cooldome/Nim into cooldome-Fixes-7845
-rw-r--r--compiler/ast.nim11
-rw-r--r--compiler/semstmts.nim5
-rw-r--r--tests/exception/texcpt1.nim15
3 files changed, 23 insertions, 8 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index f5575d8e5..eaf79a190 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -1714,8 +1714,7 @@ proc toVar*(typ: PType): PType =
 proc toRef*(typ: PType): PType =
   ## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
   ## returned. Otherwise ``typ`` is simply returned as-is.
-  result = typ
-  if typ.kind == tyObject:
+  if typ.skipTypes({tyAlias, tyGenericInst}).kind == tyObject:
     result = newType(tyRef, typ.owner)
     rawAddSon(result, typ)
 
@@ -1723,15 +1722,15 @@ proc toObject*(typ: PType): PType =
   ## If ``typ`` is a tyRef then its immediate son is returned (which in many
   ## cases should be a ``tyObject``).
   ## Otherwise ``typ`` is simply returned as-is.
-  result = typ
-  if result.kind == tyRef:
-    result = result.lastSon
+  let t = typ.skipTypes({tyAlias, tyGenericInst})
+  if t.kind == tyRef: t.lastSon
+  else: typ
 
 proc isException*(t: PType): bool =
   # check if `y` is object type and it inherits from Exception
   assert(t != nil)
 
-  if t.kind != tyObject:
+  if t.kind notin {tyObject, tyGenericInst}:
     return false
 
   var base = t
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index e02a38d8a..d2b866366 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -823,9 +823,10 @@ proc semRaise(c: PContext, n: PNode): PNode =
   checkSonsLen(n, 1, c.config)
   if n[0].kind != nkEmpty:
     n[0] = semExprWithType(c, n[0])
-    let typ = n[0].typ
+    var typ = n[0].typ
     if not isImportedException(typ, c.config):
-      if typ.kind != tyRef or typ.lastSon.kind != tyObject:
+      typ = typ.skipTypes({tyAlias, tyGenericInst})
+      if typ.kind != tyRef:
         localError(c.config, n.info, errExprCannotBeRaised)
       if typ.len > 0 and not isException(typ.lastSon):
         localError(c.config, n.info, "raised object of type $1 does not inherit from Exception",
diff --git a/tests/exception/texcpt1.nim b/tests/exception/texcpt1.nim
index 50a95eeec..835f3610a 100644
--- a/tests/exception/texcpt1.nim
+++ b/tests/exception/texcpt1.nim
@@ -4,6 +4,8 @@ discard """
 type
   ESomething = object of Exception
   ESomeOtherErr = object of Exception
+  ESomethingGen[T] = object of Exception
+  ESomethingGenRef[T] = ref object of Exception
 
 proc genErrors(s: string) =
   if s == "error!":
@@ -27,4 +29,17 @@ proc blah(): int =
 
 echo blah()
 
+# Issue #7845, raise generic exception
+var x: ref ESomethingGen[int]
+new(x)
+try:
+  raise x
+except ESomethingGen[int] as e:
+  discard
 
+try:
+  raise new(ESomethingGenRef[int])
+except ESomethingGenRef[int] as e:
+  discard
+except:
+  discard
\ No newline at end of file