Jump to page: 1 2
Thread overview
[Issue 14653] scoped!range in foreach crashes
[Issue 14653] scoped range in foreach
Jun 05, 2015
Marc Schütz
Jun 05, 2015
Lionello Lunesu
Jun 05, 2015
Marc Schütz
Jun 05, 2015
Yuxuan Shui
Jun 05, 2015
Yuxuan Shui
Jun 05, 2015
Lionello Lunesu
Jun 17, 2015
Ketmar Dark
Jun 17, 2015
Ketmar Dark
Jun 17, 2015
Ketmar Dark
Jun 17, 2015
Ketmar Dark
Jul 03, 2015
Kenji Hara
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

Marc Schütz <schuetzm@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schuetzm@gmx.net

--- Comment #1 from Marc Schütz <schuetzm@gmx.net> ---
I can confirm this for DMD git master.

--
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

Lionello Lunesu <lio+bugzilla@lunesu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ice, ice-on-valid-code
            Summary|scoped range in foreach     |scoped!range in foreach
                   |                            |crashes

--
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #2 from Marc Schütz <schuetzm@gmx.net> ---
@Lionello: Why ICE?

--
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

Yuxuan Shui <yshuiv7@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yshuiv7@gmail.com

--
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

Yuxuan Shui <yshuiv7@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|ice, ice-on-valid-code      |wrong-code

--
June 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #3 from Lionello Lunesu <lio+bugzilla@lunesu.com> ---
(In reply to Marc Schütz from comment #2)
> @Lionello: Why ICE?

My bad.

--
June 17, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #4 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
a simplified sample:

===================
import std.stdio : writeln;

struct SomeRangeType {
  int a;
  alias front = a;
  void popFront () { ++a; }
  @property bool empty () { return (a >= 2); }
}

auto wrapit () {
  static struct Wrapper {
    SomeRangeType rng;
    alias rng this;
    @disable this (this);
    ~this () { writeln("wrapper dtor"); }
  }
  return Wrapper(SomeRangeType());
}

void main () {
  writeln("before foreach");
  foreach (auto e; wrapit()) {
    writeln("in foreach; e=", e);
  }
  writeln("after foreach");
}
===================

this prints:
---
before foreach
wrapper dtor
in foreach; e=0
in foreach; e=1
after foreach
--

what i expected it to print:
---
before foreach
in foreach; e=0
in foreach; e=1
wrapper dtor
after foreach
---

--
June 17, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #5 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
p.s. seems that what triggers the bug is `alias this` in wrapper. removing `alias this` and rewriting wrapper to do simple redirections emits the correct output.

i.e. changing `Wrapper` struct to the following one makes the bug go away:

  static struct Wrapper {
    SomeRangeType rng;
    //alias rng this;
    @property int front () { return rng.front; }
    void popFront () { rng.popFront; }
    @property bool empty () { return rng.empty; }
    @disable this (this);
    ~this () { writeln("wrapper dtor"); }
  }

--
June 17, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #6 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
generated initializer for `alias this` variant:
  init: __r87 = (Wrapper __tmpfordtor86 = wrapit(); , __tmpfordtor86).rng;

generated initializer for proxy variant:
  init: __r86 = wrapit();

clearly, compiler tries to pull `rng` away of wrapper struct with `alias this` redirection, and then destroying created wrapper. this may work for other cases, but completely wrong for `scoped`.

seems that `foreach` conversion code shouldn't try to pull off aliased entity.

--
June 17, 2015
https://issues.dlang.org/show_bug.cgi?id=14653

--- Comment #7 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
that's `inferAggregate()` which pulls out aliased type from wrapper, rewriting
it from `wrapit()` to `wrapit().rng`, and semantic then rewrites it to
`(Wrapper __tmpfordtor3 = wrapit(); , __tmpfordtor3).rng`. it does this:

  if (ad->aliasthis)
  {
      if (!att && tab->checkAliasThisRec())
          att = tab;
      fes->aggr = new DotIdExp(fes->aggr->loc, fes->aggr,
ad->aliasthis->ident);
      continue;
  }

i.e. literally pulling away aliased struct with `DotIdExp` (and dtoring result
of `wrapit()` by the way, as it thinks that it's not required anymore.

seems that `inferAggregate()` must postpone dtor calling for such cases.

--
« First   ‹ Prev
1 2