stack out of scope ?

H. S. Teoh hsteoh at quickfur.ath.cx
Sun May 16 18:27:40 UTC 2021


On Sun, May 16, 2021 at 05:24:40PM +0000, Alain De Vos via Digitalmars-d-learn wrote:
> On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
> > On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via
> > Digitalmars-d-learn wrote:
> > > This works also,
> > > 
> > > ```
> > > import std.stdio:writeln;
> > > 
> > > int [] fun(){
> > > 	int[3]s=[1,2,3];
> > > 	int[] r=s;
> > > 	return r;
> > > }
> > > 
> > > void main(){
> > > 	writeln(fun()[0]);
> > > }
> > > ```
> > 
> > https://issues.dlang.org/show_bug.cgi?id=15932
> > 
> > Though I believe if you compile with -dip25 -dip1000 the compiler should
> > emit an error for the above code.  If not, please file a bug against
> > -dip1000.
[...]
> I use ldc2. No dip flags here.

---------snip---------
import std.stdio:writeln;

int [] fun() @safe {		// N.B.: need @safe
	int[3]s=[1,2,3];
	int[] r=s;
	return r;
}

void main() @safe {		// N.B.: need @safe
	writeln(fun()[0]);
}
---------snip---------


LDC output:

---------snip---------
$ ldc2 -dip1000 /tmp/test.d
/tmp/test.d(6): Error: scope variable `r` may not be returned
---------snip---------


T

-- 
People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG


More information about the Digitalmars-d-learn mailing list