Skip to content

Print raise source on exception, gcsafe violation, #25402

@arnetheduck

Description

@arnetheduck

Summary

When an exception specification is violated, it can be very difficult to tell where it is coming from since the source of an exception is often deeply nested. The poor error message leads exception handlers being introduced in the wrong place

Description

proc a() =
  raise (ref ValueError)()

proc b() =
  a()

proc c() {.raises: [].} =
  b()
  b() # second call


c()
testit.nim(8, 4) Error: b() can raise an unlisted exception: ref ValueError

A better error message would be:

testit.nim(8, 4) Error: b() can raise an unlisted exception: ref ValueError
  testit.nim(5, 4) Error: a() can raise a deduced exception: ref ValueError
    testit.nim(2, 4) Error: source of deduced exception: ref ValueError

Most of the time, exception violations like this are due to forward declarations - the error message should then indicate this, ie "forward declaration assumed to raise Exception, annotate with actually raised exceptions to fix this"

Often, there may be many exceptions raised (ie IOError, ValueError etc) and many call stacks that cause them - it's enough to list the first source, ie in the above example, there's no need to put the second call in the error message as doing so potentially could lead to an explosion of traces for "top-level" eh specifications.

The same logic should also be applied to gcsafe.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions