D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18592 - Associative array assignment with a destructor should be @safe if the destructor is @safe
Summary: Associative array assignment with a destructor should be @safe if the destruc...
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2018-03-11 20:27 UTC by Seb
Modified: 2018-03-15 03:16 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Seb 2018-03-11 20:27:47 UTC
cat > main.d << EOF
void main() @safe
{
    struct ThrowingElement
    {
        int i;
        ~this()
        {
            assert(1);
        }
    }
    
    ThrowingElement[int] aa;
    aa[0] = ThrowingElement(0);
}
EOF


The problem is that the generated opAssign is @system:

ref return @system ThrowingElement opAssign(ThrowingElement p)
{
	(ThrowingElement __swap2 = void;) , __swap2 = this , this = p , __swap2.~this();
	return this;
}
Comment 1 Walter Bright 2018-03-13 06:05:20 UTC
The solution is likely the same as for https://github.com/dlang/dmd/pull/8011
Comment 2 Walter Bright 2018-03-15 03:16:58 UTC
The example compiles successfully if ~this() is given the @safe attribute. @safe for the outer function is not transitively applied - perhaps it should be, but that isn't the way it works at the moment.