Skip to content

Conversation

@juancarlospaco
Copy link
Collaborator

Interesting, ...so this is like strictFuncs but for "Effects" ?. 😲:+1:

@timotheecour
Copy link
Member

timotheecour commented Sep 1, 2021

bugs:

when defined case6: proc fn(a: int, p1, p2: proc()) {.effectsOf: p1.} = # proc fn(a: int, p1, p2: proc()) = # ditto if a == 7: p1() if a<0: raise newException(ValueError, $a) proc main() {.raises: [ValueError].} = # fn(1, proc() = raise newException(OsError, "foo"), proc()=discard) # ok: can raise an unlisted exception: ref OSError is correct fn(1, proc()=discard, proc() = raise newException(IOError, "foo")) # bug: `can raise an unlisted exception: ref IOError` shouldn't be generated because of `effectsOf: p1` main()

also this, slightly different as it involves parent scope:

when defined case8: proc bar(a: proc()) = proc baz(b: proc()) {.effectsOf: a.} = a() # not calling b but calling a param from parent scope proc x()=raise newException(ValueError, "") baz(x) proc main(){.raises: [].} = bar(proc()=raise newException(OsError, "")) main()

produces incorrect diagnostic (in particular, ValueError is never raised)

can raise an unlisted exception: Exception [Effect] can raise an unlisted exception: ref ValueError [Effect] can raise an unlisted exception: ref OSError [Effect] 
if r.sym.owner == owner:
incl r.sym.flags, sfEffectsDelayed
else:
localError(c.config, n.info, errGenerated, "parameter cannot be declared as .effectsOf")
Copy link
Member

@timotheecour timotheecour Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this should be legal:

 proc bar(a: proc()) = proc baz(b: proc()) {.effectsOf: [a].} = a()
@Araq
Copy link
Member Author

Araq commented Sep 2, 2021

but this should be legal

Should be done later as it's much harder to implement. I also doubt this will ever be needed.

@Araq Araq added this to the 1.6.0 milestone Sep 2, 2021
sfUsedInFinallyOrExcept # symbol is used inside an 'except' or 'finally'
sfSingleUsedTemp # For temporaries that we know will only be used once
sfNoalias # 'noalias' annotation, means C's 'restrict'
sfEffectsDelayed # an 'effectsDelayed' parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bugs i mentioned in #18777 (comment) is still there, it looks like the compiler ignores the effectsOf: p1 annotations and assumes every param is called

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your snippet was even added as a test case, it works...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i was missing {.experimental: "strictEffects".}; however, the problem remains if i remove the explicit annotaton:

when defined case9: {.push warningAsError[Effect]: on.} {.experimental: "strictEffects".} # proc fn(a: int, p1, p2: proc()) {.effectsOf: p1.} = # proc fn(a: int, p1, p2: proc()) {.effectsOf: p2.} = proc fn(a: int, p1, p2: proc()) = 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() 

this gives:

t12754.nim(111, 7) Error: fn(1, proc () = discard , proc () = raise
(ref IOError)(msg: "foo", parent: nil)) can raise an unlisted exception: Exception [Effect]

but compiler should infer that this is {.effectsOf: p1.} since only p1 is called, not p2, and then it should not issue any warning since main can't raise anything other than ValueError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no inference for the .effectsOf pragma and there is no plan to add any, it would repeat the mistakes of "sink inference".

@Araq
Copy link
Member Author

Araq commented Sep 2, 2021

In a follow up PR we should patch map.

@Araq Araq merged commit e0ef859 into devel Sep 2, 2021
@Araq Araq deleted the araq-strict-effects branch September 2, 2021 10:10
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
* fixes nim-lang#17369 * megatest is green for --cpu:arm64 * docgen output includes more tags/raises * implemented 'effectsOf' * algorithm.nim: uses new effectsOf annotation * closes nim-lang#18376 * closes nim-lang#17475 * closes nim-lang#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants