Thread overview
assumeNoGC works but can't get an assumePure to work
Sep 03, 2018
aliak
Sep 04, 2018
Paul Backus
Sep 04, 2018
aliak
September 03, 2018
In another thread [0] this function can be used to call non nogc code from nogc code

import std.traits;

auto assumeNoGC(T)(T t) {
    enum attrs = functionAttributes!T | FunctionAttribute.nogc;
    return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}

And then you can use it like:

@nogc unittest {
  auto allocate() {
    return [1];
  }
  assumeNoGC({allocate;})();
}

So I tried to the same with pure, wrote assumePure and changed the attribute to FunctionAttribute.pure_, but that doesn't seem to be treated the same:

pure unittest {
  static int thing = 3;
  void modify() {
    thing = 4;
  }
  assumePure({modify;})();
}

Ye get: pure function modify cannot access mutable static data thing

Why does it work with nogc but not with pure?

Cheers,
- Ali

[0]: https://forum.dlang.org/thread/awalwokejtywzkxgdqyg@forum.dlang.org
September 04, 2018
On Monday, 3 September 2018 at 22:07:10 UTC, aliak wrote:
> Why does it work with nogc but not with pure?
>
> Cheers,
> - Ali

You can't define an impure function inside a pure unittest. If you move `modify` outside the unittest block, and change the argument from a lambda to a function pointer, it works:

https://run.dlang.io/is/xRS75H
September 04, 2018
On Tuesday, 4 September 2018 at 01:33:52 UTC, Paul Backus wrote:
> On Monday, 3 September 2018 at 22:07:10 UTC, aliak wrote:
>> Why does it work with nogc but not with pure?
>>
>> Cheers,
>> - Ali
>
> You can't define an impure function inside a pure unittest. If you move `modify` outside the unittest block, and change the argument from a lambda to a function pointer, it works:
>
> https://run.dlang.io/is/xRS75H

Seems you be right. Hmm, I wonder if it's a bug because you can define a non-nogc function inside a nogc block :/