[Issue 16228] New: Insufficient diagnostics for wrong application of DIP25

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jul 2 02:49:16 PDT 2016


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

          Issue ID: 16228
           Summary: Insufficient diagnostics for wrong application of
                    DIP25
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: public at dicebot.lv

Most simple example:

==================
void foo ( return ref int ) { }

void main ( )
{
    int x;
    foo(x);
}
===================

This code successfully compiles with `dmd -dip25` despite the fact function
doesn't return anything by reference and usage of `return ref` on parameter is
a certain mistake.

Compiler in general doesn't check nonsense applications of return ref making it
impossible to reason about feature based on experimentation because "everything
is checked good" and "return ref is ignored" produce the same outcome.

With more complicated functions it easily misleads developer into believing
that `return ref` is capable of more complicated lifetime checking than it is
intended too:

=====================
int* wrap ( return ref int input )
{
    return &input;
}

int* badWrapper()
{
    int x = 5;
    return wrap(x); // OK
}

void main()
{
    auto badWrap = badWrapper();
}
=====================

Here `return ref` has no effect for same reason as in first example. But lack
of compiler errors about such nonsense application may easily lead to believe
it is supposed to work.

--


More information about the Digitalmars-d-bugs mailing list