From 6a2b31226e1c43b15a2758c3a6bf7495dd7696ca Mon Sep 17 00:00:00 2001 From: Veladus Date: Sat, 2 Dec 2017 17:54:35 +0100 Subject: [PATCH 1/5] Compiler now catches when an expression is raised which is no Exception --- compiler/msgs.nim | 3 ++- compiler/semstmts.nim | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 2668c72ae0..f22e766c92 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -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: ", diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index e01f867faf..2f69da62ae 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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) From 2c886823e77c2244b1407a037e87b9da62f84a92 Mon Sep 17 00:00:00 2001 From: Veladus Date: Sat, 2 Dec 2017 20:57:18 +0100 Subject: [PATCH 2/5] Fixed for diffrent Typeids of Excpetion for diffrent compilation units --- compiler/semstmts.nim | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 2f69da62ae..d804beff56 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -721,8 +721,6 @@ 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) @@ -735,11 +733,7 @@ proc semRaise(c: PContext, n: PNode): PNode = # 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: + if base.sym.name.s == "Exception": break if base.lastSon == nil: localError(n.info, errExprIsNoException) From a15ddf4013c5157e7532c71aa2045e07947782fb Mon Sep 17 00:00:00 2001 From: Veladus Date: Mon, 11 Dec 2017 21:48:22 +0100 Subject: [PATCH 3/5] Improved error reporting --- compiler/semstmts.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index d804beff56..a44b2fafc4 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -736,7 +736,7 @@ proc semRaise(c: PContext, n: PNode): PNode = if base.sym.name.s == "Exception": break if base.lastSon == nil: - localError(n.info, errExprIsNoException) + localError(n.info, "raised object of type $1 does not inherit from Exception", [typ.sym.name.s]) return base = base.lastSon From 15f72d0cf1390971e047f22dabf7a9e5b24fd85b Mon Sep 17 00:00:00 2001 From: Veladus Date: Mon, 11 Dec 2017 21:49:28 +0100 Subject: [PATCH 4/5] Now analyzes over magics instead of symbol names; but dosn't compile for me --- compiler/ast.nim | 3 ++- compiler/semstmts.nim | 2 +- lib/system.nim | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 5bf4184c95..5b923acb25 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -639,7 +639,8 @@ type mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl, mNHint, mNWarning, mNError, mInstantiationInfo, mGetTypeInfo, mNGenSym, - mNimvm, mIntDefine, mStrDefine, mRunnableExamples + mNimvm, mIntDefine, mStrDefine, mRunnableExamples, + mException # things that we can evaluate safely at compile time, even if not asked for it: const diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index a44b2fafc4..c85de35cd2 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -733,7 +733,7 @@ proc semRaise(c: PContext, n: PNode): PNode = # check if the given object inherits from Exception var base = typ.lastSon while true: - if base.sym.name.s == "Exception": + if base.sym.magic == mException: break if base.lastSon == nil: localError(n.info, "raised object of type $1 does not inherit from Exception", [typ.sym.name.s]) diff --git a/lib/system.nim b/lib/system.nim index b9f01c3065..ca17f70f6a 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -463,7 +463,7 @@ type line*: int ## line number of the proc that is currently executing filename*: cstring ## filename of the proc that is currently executing - Exception* {.compilerproc.} = object of RootObj ## \ + Exception* {.compilerproc, magic: "Exception".} = object of RootObj ## \ ## Base exception class. ## ## Each exception has to inherit from `Exception`. See the full `exception From 56aa16b1ded1cba0380e6b62973126650d65207e Mon Sep 17 00:00:00 2001 From: Veladus Date: Mon, 11 Dec 2017 22:09:29 +0100 Subject: [PATCH 5/5] removed unused constants --- compiler/msgs.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/msgs.nim b/compiler/msgs.nim index f22e766c92..2668c72ae0 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -61,7 +61,7 @@ type errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects, errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX, errCannotInstantiateX, errExprHasNoAddress, errXStackEscape, - errVarForOutParamNeededX, errExprIsNoException, + errVarForOutParamNeededX, errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX, errAmbiguousCallXYZ, errWrongNumberOfArguments, errWrongNumberOfArgumentsInCall, @@ -269,7 +269,6 @@ 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: ",