www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20918] New: Compiler diagnostic for templates which throw

https://issues.dlang.org/show_bug.cgi?id=20918

          Issue ID: 20918
           Summary: Compiler diagnostic for templates which throw should
                    show the code which throws
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrej.mitrovich gmail.com

Consider this case:

-----
void test2 (T)(T arg)
{
    if (arg == 1)
        throw new Exception("");
}

void test (T)(T arg)
{
    test2(arg);
}

void main (string[] args) nothrow
{
    test(1);
}
-----

$ ~/dev/d master * $ dmd test.d
test.d(14): Error: function test.test!int.test is not nothrow
test.d(12): Error: nothrow function D main may throw

This error message makes sense for functions. However for templated functions
we don't necessarily *want* to mark the template function as nothrow. We want
it to be inferred.

In this case, the compiler should give the user a hint, something like:
$ ~/dev/d master * $ dmd test.d
test.d(14): Error: function test.test!int.test is not nothrow
test.d(14): Error: function test.test!int.test2 is not nothrow
test.d(4):  Error: function test.test!int.test2 may throw here

Because it's a template the compiler knows why the template is deduced to be a
throwing function. It should tell the user where the offending line is.

--
Jun 09 2020