strict effects (#18777)

* fixes #17369
* megatest is green for --cpu:arm64
* docgen output includes more tags/raises
* implemented 'effectsOf' 
* algorithm.nim: uses new effectsOf annotation
* closes #18376
* closes #17475
* closes #13905
* allow effectsOf: [a, b]
* added a test case
* parameters that are not ours cannot be declared as .effectsOf
* documentation
* manual: added the 'sort' example
* bootstrap with the new better options
This commit is contained in:
Andreas Rumpf 2021-09-02 12:10:14 +02:00 • committed by GitHub
commit e0ef859130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 482 additions and 203 deletions

View file

@ -6,7 +6,7 @@ teffects1.nim(22, 29) Hint: 'lier' cannot raise 'IO2Error' [XCannotRaiseY]
teffects1.nim(38, 21) Error: type mismatch: got <proc (x: int): string{.noSideEffect, gcsafe, locks: 0.}> but expected 'MyProcType = proc (x: int): string{.closure.}'
.raise effects differ'''
"""
{.push warningAsError[Effect]: on.}
type
TObj {.pure, inheritable.} = object
TObjB = object of TObj
@ -41,3 +41,4 @@ type mismatch: got <proc (x: int): string{.noSideEffect, gcsafe, locks: 0.}> but
]#
{.pop.}
{.pop.}

View file

@ -2,7 +2,7 @@ discard """
errormsg: "can raise an unlisted exception: ref IOError"
line: 19
"""
{.push warningAsError[Effect]: on.}
type
TObj = object {.pure, inheritable.}
TObjB = object of TObj
@ -17,3 +17,4 @@ proc lier(): int {.raises: [IOError].} =
proc forw: int =
raise newException(IOError, "arg")
{.pop.}

View file

@ -2,7 +2,7 @@ discard """
errormsg: "can raise an unlisted exception: ref ValueError"
line: 10
"""
{.push warningAsError[Effect]: on.}
proc foo() {.raises: [].} =
try:
discard
@ -12,3 +12,5 @@ proc foo() {.raises: [].} =
discard
foo()
{.pop.}

View file

@ -2,7 +2,7 @@ discard """
errormsg: "can raise an unlisted exception: Exception"
line: 10
"""
{.push warningAsError[Effect]: on.}
proc foo() {.raises: [].} =
try:
discard
@ -10,3 +10,4 @@ proc foo() {.raises: [].} =
raise
foo()
{.pop.}

View file

@ -0,0 +1,27 @@
discard """
errormsg: "s1 can raise an unlisted exception: CatchableError"
line: 27
"""
{.push warningAsError[Effect]: on.}
{.experimental: "strictEffects".}
# bug #18376
{.push raises: [Defect].}
type Call = proc (x: int): int {.gcsafe, raises: [Defect, CatchableError].}
type Bar* = object
foo*: Call
proc passOn*(x: Call) = discard
proc barCal(b: var Bar, s: string, s1: Call) =
#compiler complains that his line can throw CatchableError
passOn s1
proc passOnB*(x: Call) {.effectsOf: x.} = discard
proc barCal2(b: var Bar, s: string, s1: Call) =
passOnB s1

View file

@ -0,0 +1,28 @@
discard """
errormsg: "can raise an unlisted exception: Exception"
line: 23
"""
{.push warningAsError[Effect]: on.}
{.experimental: "strictEffects".}
# bug #13905
proc atoi(v: cstring): cint {.importc: "atoi", cdecl, raises: [].}
type Conv = proc(v: cstring): cint {.cdecl, raises: [].}
var x: Conv = atoi
# bug #17475
type
Callback = proc()
proc f(callback: Callback) {.raises: [].} =
callback()
proc main =
f(proc () = raise newException(IOError, "IO"))
main()

View file

@ -0,0 +1,17 @@
discard """
action: compile
"""
{.push warningAsError[Effect]: on.}
{.experimental: "strictEffects".}
proc fn(a: int, p1, p2: proc()) {.effectsOf: p1.} =
if a == 7:
p1()
if a<0:
raise newException(ValueError, $a)
proc main() {.raises: [ValueError].} =
fn(1, proc()=discard, proc() = raise newException(IOError, "foo"))
main()

View file

@ -0,0 +1,27 @@
discard """
errormsg: "cmpE can raise an unlisted exception: Exception"
line: 27
"""
{.push warningAsError[Effect]: on.}
{.experimental: "strictEffects".}
import algorithm
type
MyInt = distinct int
var toSort = @[MyInt 1, MyInt 2, MyInt 3]
proc cmpN(a, b: MyInt): int =
cmp(a.int, b.int)
proc harmless {.raises: [].} =
toSort.sort cmpN
proc cmpE(a, b: MyInt): int {.raises: [Exception].} =
cmp(a.int, b.int)
proc harmfull {.raises: [].} =
toSort.sort cmpE