Understanding DIP 1000 semantics -- Where's the bug?
Mike Franklin
slavo5150 at yahoo.com
Sun Sep 22 02:29:49 UTC 2019
@safe:
// Exhibit A
//--------------------------
int getValue1(int* i)
{
return *i;
}
int* foo1()
{
int value;
int[] y;
y ~= getValue1(&value); //Error: reference to local variable
`value` assigned to non-scope parameter `i` calling
`onlineapp.getValue1`
return &y[0];
}
// Exhibit B
//--------------------------
int getValue2(ref int i)
{
return i;
}
int* foo2()
{
int value;
int[] y;
y ~= getValue2(value); // No error
return &y[0];
}
// Exhibit C (Same as Exhibit A, but with `scope` attribute on
`i`)
//--------------------------
int getValue3(scope int* i)
{
return *i;
}
int* foo3()
{
int value;
int[] y;
y ~= getValue3(&value); // No error
return &y[0];
}
Compile with `-preview=dip1000`. See it live at
https://run.dlang.io/is/0zODCr
So, there's an inconsistency.
Possibility 1:
Exhibit A should not emit an error
Possibility 2:
Exhibit B should require the `scope` attribute on `i` or emit an
error if `foo2`.
Which is it? Where's the bug? Shouldn't the compiler treat
`int* i` and `ref i` consistently?
Thanks for the help.
Mike
More information about the Digitalmars-d
mailing list