Issue 14062 - Not pure std.variant.Variant ctor
Summary: Not pure std.variant.Variant ctor
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-27 15:23 UTC by bearophile_hugs
Modified: 2024-12-01 16:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2015-01-27 15:23:25 UTC
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 sinkuupump 2015-01-31 12:32:28 UTC
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 sinkuupump 2015-02-01 11:47:42 UTC
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 Kenji Hara 2015-03-01 04:34:35 UTC
(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 Kenji Hara 2015-03-07 02:54:15 UTC
(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 dlangBugzillaToGithub 2024-12-01 16:23:39 UTC
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