Bug 14062 – Not pure std.variant.Variant ctor

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-27T15:23:25Z
Last change time
2024-12-01T16:23:39Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: phobos#9652 →

Comments

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'
Comment #1 by sinkuupump — 2015-01-31T12:32:28Z
introduced by: https://github.com/D-Programming-Language/phobos/pull/2147 I'm not sure this can be fixed. opAssign(called by constructor) and destructor of Variant are changed to call the handler, and it's impure.
Comment #2 by sinkuupump — 2015-02-01T11:47:42Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9652 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB