Bug 16228 – Insufficient diagnostics for wrong application of DIP25
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-07-02T09:49:00Z
Last change time
2016-10-01T11:48:17Z
Keywords
pull, safe
Assigned to
nobody
Creator
public
Comments
Comment #0 by public — 2016-07-02T09:49:16Z
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.
BTW, you'll see errors diagnosed if @safe is added. Leaving off @safe disables safety checks.
Comment #3 by public — 2016-07-03T09:08:55Z
> BTW, you'll see errors diagnosed if @safe is added. Leaving off @safe disables safety checks.
This doesn't sound right - these errors are akin to syntax errors in spirit, such usage of `return ref` can't be possibly correct, @safe or not.
Comment #4 by public — 2016-07-03T09:20:42Z
Also: is `return ref` supposed to work with @trusted exactly the same as with @safe?
Comment #5 by github-bugzilla — 2016-07-05T02:38:26Z