www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Scoped in a foreach loop.

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
I tried something like this:

foreach(e; scoped!SomeRangeType(args)) {
}

And my program segment faults. But this works:

{
     auto x = scoped!SomeRangeType(args);
     foreach(e; x) {
     }
}

Shouldn't the range be alive until the end of the loop?
Jun 04 2015
next sibling parent Lionello Lunesu <lionello lunesu.remove.com> writes:
On 05/06/15 05:07, Yuxuan Shui wrote:
 I tried something like this:

 foreach(e; scoped!SomeRangeType(args)) {
 }

 And my program segment faults. But this works:

 {
      auto x = scoped!SomeRangeType(args);
      foreach(e; x) {
      }
 }

 Shouldn't the range be alive until the end of the loop?
Looks like a bug. I can repro it as well. Filed as https://issues.dlang.org/show_bug.cgi?id=14653 L.
Jun 05 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 5 June 2015 at 03:07:53 UTC, Yuxuan Shui wrote:
 I tried something like this:

 foreach(e; scoped!SomeRangeType(args)) {
 }

 And my program segment faults. But this works:

 {
     auto x = scoped!SomeRangeType(args);
     foreach(e; x) {
     }
 }

 Shouldn't the range be alive until the end of the loop?
It should. Which compiler version are you using? I remember there recently was a bugfix for a very similar bug, it might not have been released yet. Can you please either test with DMD from Git (you'd need to build it yourself), or provide a minimal testcase so I can test it for you?
Jun 05 2015
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 5 June 2015 at 09:06:56 UTC, Marc Schütz wrote:
 On Friday, 5 June 2015 at 03:07:53 UTC, Yuxuan Shui wrote:
 I tried something like this:

 foreach(e; scoped!SomeRangeType(args)) {
 }

 And my program segment faults. But this works:

 {
    auto x = scoped!SomeRangeType(args);
    foreach(e; x) {
    }
 }

 Shouldn't the range be alive until the end of the loop?
It should. Which compiler version are you using? I remember there recently was a bugfix for a very similar bug, it might not have been released yet. Can you please either test with DMD from Git (you'd need to build it yourself), or provide a minimal testcase so I can test it for you?
I see Lionello already provided one. The bug is still there in DMD master.
Jun 05 2015