Fixes 7845

This commit is contained in:
cooldome 2018-06-10 22:49:09 +01:00
commit 7302a8ce7a
3 changed files with 24 additions and 9 deletions

View file

@ -1667,7 +1667,7 @@ proc skipStmtList*(n: PNode): PNode =
proc toRef*(typ: PType): PType = proc toRef*(typ: PType): PType =
## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and ## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
## returned. Otherwise ``typ`` is simply returned as-is. ## returned. Otherwise ``typ`` is simply returned as-is.
result = typ let typ = typ.skipTypes({tyAlias, tyGenericInst})
if typ.kind == tyObject: if typ.kind == tyObject:
result = newType(tyRef, typ.owner) result = newType(tyRef, typ.owner)
rawAddSon(result, typ) rawAddSon(result, typ)
@ -1676,7 +1676,7 @@ proc toObject*(typ: PType): PType =
## If ``typ`` is a tyRef then its immediate son is returned (which in many ## If ``typ`` is a tyRef then its immediate son is returned (which in many
## cases should be a ``tyObject``). ## cases should be a ``tyObject``).
## Otherwise ``typ`` is simply returned as-is. ## Otherwise ``typ`` is simply returned as-is.
result = typ result = typ.skipTypes({tyAlias, tyGenericInst})
if result.kind == tyRef: if result.kind == tyRef:
result = result.lastSon result = result.lastSon
@ -1684,14 +1684,12 @@ proc isException*(t: PType): bool =
# check if `y` is object type and it inherits from Exception # check if `y` is object type and it inherits from Exception
assert(t != nil) assert(t != nil)
if t.kind != tyObject:
return false
var base = t var base = t
while base != nil: while base != nil and base.kind in {tyObject, tyRef, tyGenericInst, tyAlias}:
if base.sym != nil and base.sym.magic == mException: if base.sym != nil and base.sym.magic == mException:
return true return true
base = base.lastSon if base.len == 0: break
else: base = base.lastSon
return false return false
proc isImportedException*(t: PType; conf: ConfigRef): bool = proc isImportedException*(t: PType; conf: ConfigRef): bool =

View file

@ -755,9 +755,10 @@ proc semRaise(c: PContext, n: PNode): PNode =
checkSonsLen(n, 1, c.config) checkSonsLen(n, 1, c.config)
if n[0].kind != nkEmpty: if n[0].kind != nkEmpty:
n[0] = semExprWithType(c, n[0]) n[0] = semExprWithType(c, n[0])
let typ = n[0].typ var typ = n[0].typ
if not isImportedException(typ, c.config): 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) localError(c.config, n.info, errExprCannotBeRaised)
if not isException(typ.lastSon): if not isException(typ.lastSon):
localError(c.config, n.info, "raised object of type $1 does not inherit from Exception", localError(c.config, n.info, "raised object of type $1 does not inherit from Exception",

View file

@ -1,9 +1,12 @@
discard """ discard """
outputsub: "-6" outputsub: "-6"
targets: "c cpp"
""" """
type type
ESomething = object of Exception ESomething = object of Exception
ESomeOtherErr = object of Exception ESomeOtherErr = object of Exception
ESomethingGen[T] = object of Exception
ESomethingGenRef[T] = ref object of Exception
proc genErrors(s: string) = proc genErrors(s: string) =
if s == "error!": if s == "error!":
@ -27,4 +30,17 @@ proc blah(): int =
echo blah() 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