mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-17 22:41:14 +00:00
Compiler now catches when an expression is raised which is no Exception
This commit is contained in:
@@ -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: ",
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user