Re: pointer escaping return scope bug?

2022-12-18 Thread j via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf =

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 15 December 2022 at 20:02:38 UTC, Nick Treleaven wrote: auto f() return @trusted => p ? p : v.ptr; Whoops, that can't be @trusted unless I `assert(p)`.

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:24:33 UTC, Dukc wrote: On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? Have the `int*` inside it to point to a local, or assign another `scope int*` to it. Thanks, this works: ```d @safe:

Re: pointer escaping return scope bug?

2022-11-27 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 25 November 2022 at 15:03:57 UTC, ShadoLight wrote: I don't grok how `lf` can survive the local scope. Or am I missing something? Perhaps because the local scope is not pushed as a separate (anonymous) function on the stack... if true then, yes, then `lf` will indeed have the same

Re: pointer escaping return scope bug?

2022-11-25 Thread Tejas via Digitalmars-d-learn
On Friday, 25 November 2022 at 17:45:57 UTC, Paul Backus wrote: On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: Since, in your example, `lf` has global lifetime, the compiler deduces that `lf.fp` also has global

Re: pointer escaping return scope bug?

2022-11-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: Since, in your example, `lf` has global lifetime, the compiler deduces that `lf.fp` also has global lifetime, and therefore there is nothing wrong with assigning it

Re: pointer escaping return scope bug?

2022-11-25 Thread ShadoLight via Digitalmars-d-learn
On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf = LockedFile(new int);

Re: pointer escaping return scope bug?

2022-11-25 Thread ShadoLight via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return

Re: pointer escaping return scope bug?

2022-11-19 Thread Dukc via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: On Saturday, 19 November 2022 at 14:52:23 UTC, ag0aep6g wrote: That's essentially just a function that returns its pointer parameter. So the program boils down to this: ```D @safe: int* fp(return scope int* p) { return p; }

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? By explicit/implicit I just meant this: scope explicit = new int; int x; auto implicit = That's probably not helping with whatever you want to accomplish.

Re: pointer escaping return scope bug?

2022-11-19 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:52:23 UTC, ag0aep6g wrote: That's essentially just a function that returns its pointer parameter. So the program boils down to this: @safe: int* fp(return scope int* p) { return p; } void main() { int* p; { auto lf = new int; p =

Re: pointer escaping return scope bug?

2022-11-19 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf =

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.11.22 15:07, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile {     private int* fps;     auto fp() return scope => fps; } void main() {     int* p;     {     auto lf = LockedFile(new int);  

pointer escaping return scope bug?

2022-11-19 Thread Nick Treleaven via Digitalmars-d-learn
Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf = LockedFile(new int); p = lf.fp; } assert(p != null); //