D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22026 - checkaction=context: Exception thrown by toString hides assertion failures
Summary: checkaction=context: Exception thrown by toString hides assertion failures
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-06-15 21:48 UTC by moonlightsentinel
Modified: 2021-12-19 01:24 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description moonlightsentinel 2021-06-15 21:48:43 UTC
checkaction=context calls user-defined toString-methods if defined. But those can throw an exception which masks the real assertion failure, e.g.:

truct MayThrow
{
    int i;
    string toString()
    {
        if (i == 1)
            throw new Exception("Error");
        return "Some message";
    }
}

assert(MayThrow(0) == MayThrow(1));
====================================================

This test will report the exception from toString(), not the assertion failure.
Comment 1 Dlang Bot 2021-06-15 21:55:01 UTC
@MoonlightSentinel created dlang/druntime pull request #3499 "Fix 22026 - Catch exceptions thrown by user-defined toString methods... " fixing this issue:

- Fix 22026 - Catch exceptions thrown by user-defined toString methods...
  
  ... when formatting values for `-checkaction=context` and print the
  aggregate members as a fallback. This ensures that an unrelated error
  does not mask the assertion failure.

https://github.com/dlang/druntime/pull/3499