Compiler now catches when an expression is raised which is no Exception

This commit is contained in:
Veladus
2017-12-02 17:54:35 +01:00
parent 1699d7c2a4
commit 6a2b31226e
2 changed files with 18 additions and 1 deletions

View File

@@ -61,7 +61,7 @@ type
errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects,
errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX,
errCannotInstantiateX, errExprHasNoAddress, errXStackEscape,
errVarForOutParamNeededX,
errVarForOutParamNeededX, errExprIsNoException,
errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX,
errAmbiguousCallXYZ, errWrongNumberOfArguments,
errWrongNumberOfArgumentsInCall,
@@ -269,6 +269,7 @@ const
errExprHasNoAddress: "expression has no address",
errXStackEscape: "address of '$1' may not escape its stack frame",
errVarForOutParamNeededX: "for a \'var\' type a variable needs to be passed; but '$1' is immutable",
errExprIsNoException: "raised object does not inherit from Exception",
errPureTypeMismatch: "type mismatch",
errTypeMismatch: "type mismatch: got (",
errButExpected: "but expected one of: ",

View File

@@ -721,6 +721,8 @@ proc semFor(c: PContext, n: PNode): PNode =
result.typ = enforceVoidContext
closeScope(c)
var exceptionID = -1
proc semRaise(c: PContext, n: PNode): PNode =
result = n
checkSonsLen(n, 1)
@@ -729,6 +731,20 @@ proc semRaise(c: PContext, n: PNode): PNode =
var typ = n.sons[0].typ
if typ.kind != tyRef or typ.lastSon.kind != tyObject:
localError(n.info, errExprCannotBeRaised)
# check if the given object inherits from Exception
var base = typ.lastSon
while true:
if exceptionID == -1:
if base.sym.name.s == "Exception":
exceptionID = base.id
break
elif base.id == exceptionID:
break
if base.lastSon == nil:
localError(n.info, errExprIsNoException)
return
base = base.lastSon
proc addGenericParamListToScope(c: PContext, n: PNode) =
if n.kind != nkGenericParams: illFormedAst(n)