Tell us your DIP1000 woes

Paul Backus snarwin at gmail.com
Tue Aug 27 15:16:50 UTC 2024


On Sunday, 25 August 2024 at 13:10:22 UTC, Mike Parker wrote:
> First, it's pretty clear that inference by default is necessary 
> to make DIP1000 easier to use. It's not clear at the moment how 
> this can be achieved, but it's something that we need to work 
> out.

In addition to inference, it is vitally important to have 
detailed error messages showing exactly where and why inference 
failed.

Here's an illustrative example:

     int* global;

     void foo()(int* p)
     {
         global = p;
     }

     void bar()()
     {
         int n;
         foo(&n);
     }

     @safe void main()
     {
         bar();
     }

Currently, compiling this program with `-preview=dip1000` gives 
the following error message:

     example.d(16): Error: `@safe` function `D main` cannot call 
`@system` function `example.bar!().bar`
     example.d(11):        which wasn't inferred `@safe` because 
of:
     example.d(11):        reference to local variable `n` 
assigned to non-scope parameter `p` calling `foo`

The problem with this message is that it does not explain *why* 
`p` is a non-scope parameter. In order to get that information, 
we have to modify the definition of `foo` and add `scope` 
manually:

     -void foo()(int* p)
     +void foo()(scope int* p)

When we do, we get an error message that points to the *real* 
source of the problem:

     example2.d(16): Error: `@safe` function `D main` cannot call 
`@system` function `example2.bar!().bar`
     example2.d(3):        which calls `example2.foo!().foo`
     example2.d(5):        which wasn't inferred `@safe` because 
of:
     example2.d(5):        scope variable `p` assigned to global 
variable `global`

In order for normal D programmers to have any hope of using DIP 
1000 successfully, all of the relevant information *must* be 
present in the original error message. Users must not be required 
to modify their code (or, worse, the code of libraries they 
depend on) in order to reveal this information.


More information about the Digitalmars-d mailing list