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
commit 6a2b31226e
2 changed files with 18 additions and 1 deletions

View file

@ -61,7 +61,7 @@ type
errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects, errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects,
errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX, errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX,
errCannotInstantiateX, errExprHasNoAddress, errXStackEscape, errCannotInstantiateX, errExprHasNoAddress, errXStackEscape,
errVarForOutParamNeededX, errVarForOutParamNeededX, errExprIsNoException,
errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX, errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX,
errAmbiguousCallXYZ, errWrongNumberOfArguments, errAmbiguousCallXYZ, errWrongNumberOfArguments,
errWrongNumberOfArgumentsInCall, errWrongNumberOfArgumentsInCall,
@ -269,6 +269,7 @@ const
errExprHasNoAddress: "expression has no address", errExprHasNoAddress: "expression has no address",
errXStackEscape: "address of '$1' may not escape its stack frame", 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", 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", errPureTypeMismatch: "type mismatch",
errTypeMismatch: "type mismatch: got (", errTypeMismatch: "type mismatch: got (",
errButExpected: "but expected one of: ", errButExpected: "but expected one of: ",

View file

@ -721,6 +721,8 @@ proc semFor(c: PContext, n: PNode): PNode =
result.typ = enforceVoidContext result.typ = enforceVoidContext
closeScope(c) closeScope(c)
var exceptionID = -1
proc semRaise(c: PContext, n: PNode): PNode = proc semRaise(c: PContext, n: PNode): PNode =
result = n result = n
checkSonsLen(n, 1) checkSonsLen(n, 1)
@ -730,6 +732,20 @@ proc semRaise(c: PContext, n: PNode): PNode =
if typ.kind != tyRef or typ.lastSon.kind != tyObject: if typ.kind != tyRef or typ.lastSon.kind != tyObject:
localError(n.info, errExprCannotBeRaised) 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) = proc addGenericParamListToScope(c: PContext, n: PNode) =
if n.kind != nkGenericParams: illFormedAst(n) if n.kind != nkGenericParams: illFormedAst(n)
for i in countup(0, sonsLen(n)-1): for i in countup(0, sonsLen(n)-1):