Comment #0 by bearophile_hugs — 2015-01-27T15:23:25Z
This seems a regression:
void main() pure {
import std.variant: Variant;
auto v = Variant(1);
}
dmd 2.067alpha gives:
test.d(3): Error: pure function 'D main' cannot call impure function 'std.variant.VariantN!20u.VariantN.__ctor!int.this'
test.d(3): Error: pure function 'D main' cannot call impure function 'std.variant.VariantN!20u.VariantN.~this'
test.d(3): Error: pure function 'D main' cannot call impure function 'std.variant.VariantN!20u.VariantN.~this'
Ah, constructor can avoid calling fptr(OpId.destruct) because fpr==&handler!void there, so it can be pure. But destructor is still impure. Any ideas?
Comment #3 by k.hara.pg — 2015-03-01T04:34:35Z
(In reply to bearophile_hugs from comment #0)
> test.d(3): Error: pure function 'D main' cannot call impure function
> 'std.variant.VariantN!20u.VariantN.__ctor!int.this'
> test.d(3): Error: pure function 'D main' cannot call impure function
> 'std.variant.VariantN!20u.VariantN.~this'
> test.d(3): Error: pure function 'D main' cannot call impure function
> 'std.variant.VariantN!20u.VariantN.~this'
The purity violation errors had been wrongly erased by compiler issue in previous dmd versions. Currently Variant implementation does not support pure construction even if the setting value can do it.
Comment #4 by k.hara.pg — 2015-03-07T02:54:15Z
(In reply to sinkuupump from comment #2)
> Ah, constructor can avoid calling fptr(OpId.destruct) because
> fpr==&handler!void there, so it can be pure. But destructor is still impure.
> Any ideas?
As you say, constructing Variant can be pure depending on the initialized value type, so it would be an enhancement.
It is more precise test case for the enhancement:
auto makeVar(int n) pure
{
import std.variant: Variant;
return Variant(1); // constructing Variant with int could be pure
}
void main()
{
auto v = makeVar(1);
}
However to support it, a compile bug need to be fixed beforehand.
Issue 14252 - Erroneous dtor attributes check even if the struct returned immediately
On the other hand, non-algebraic Variant type cannot copy or destruct in pure function.
Change the importance to 'enhancement'.
Comment #5 by robert.schadek — 2024-12-01T16:23:39Z