Skip to content

Commit

Permalink
fix Issue 17935 - [scope] auto-generated destructor not scope aware
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Nov 4, 2017
1 parent 1fa67d0 commit c9ff581
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/ddmd/clone.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

module ddmd.clone;

// Online documentation: https://dlang.org/phobos/ddmd_clone.html
/**
* Documentation:
* https://dlang.org/phobos/ddmd_clone.html
* Coverage:
* https://codecov.io/gh/dlang/dmd/src/master/src/ddmd/clone.d
*/

import core.stdc.stdio;
import ddmd.aggregate;
Expand Down Expand Up @@ -987,14 +992,20 @@ extern (C++) FuncDeclaration buildPostBlit(StructDeclaration sd, Scope* sc)
* Create inclusive destructor for struct/class by aggregating
* all the destructors in dtors[] with the destructors for
* all the members.
* Note the close similarity with StructDeclaration::buildPostBlit(),
* Params:
* ad = struct or class to build destructor for
* sc = context
* Returns:
* generated function, null if none needed
* Note:
* Close similarity with StructDeclaration::buildPostBlit(),
* and the ordering changes (runs backward instead of forwards).
*/
extern (C++) FuncDeclaration buildDtor(AggregateDeclaration ad, Scope* sc)
{
//printf("AggregateDeclaration::buildDtor() %s\n", ad.toChars());
if (ad.isUnionDeclaration())
return null;
return null; // unions don't have destructors

StorageClass stc = STCsafe | STCnothrow | STCpure | STCnogc;
Loc declLoc = ad.dtors.dim ? ad.dtors[0].loc : ad.loc;
Expand Down Expand Up @@ -1033,9 +1044,8 @@ extern (C++) FuncDeclaration buildDtor(AggregateDeclaration ad, Scope* sc)
ex = new DotVarExp(loc, ex, v);

// This is a hack so we can call destructors on const/immutable objects.
ex = new AddrExp(loc, ex);
ex = new CastExp(loc, ex, v.type.mutableOf().pointerTo());
ex = new PtrExp(loc, ex);
// Do it as a type 'paint'.
ex = new CastExp(loc, ex, v.type.mutableOf());
if (stc & STCsafe)
stc = (stc & ~STCsafe) | STCtrusted;

Expand Down Expand Up @@ -1079,7 +1089,7 @@ extern (C++) FuncDeclaration buildDtor(AggregateDeclaration ad, Scope* sc)
*/
if (e || (stc & STCdisable))
{
//printf("Building __fieldDtor()\n");
//printf("Building __fieldDtor(), %s\n", e.toChars());
auto dd = new DtorDeclaration(declLoc, Loc(), stc, Id.__fieldDtor);
dd.generated = true;
dd.storage_class |= STCinference;
Expand Down Expand Up @@ -1134,6 +1144,7 @@ extern (C++) FuncDeclaration buildDtor(AggregateDeclaration ad, Scope* sc)
ad.members.push(_alias);
_alias.addMember(sc, ad); // add to symbol table
}

return xdtor;
}

Expand Down
29 changes: 28 additions & 1 deletion test/runnable/testscope.d
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void test11()

int test17432(scope int delegate() dg)
{
return dg();
return dg();
}

// stripped down version of std.traits.Parameters
Expand Down Expand Up @@ -341,6 +341,32 @@ template test14(T)

test14!(char[] function(return char[])) x14;

/********************************************/
// https://issues.dlang.org/show_bug.cgi?id=17935

struct ByChunk(IO)
{
@safe:
~this() scope
{}

ubyte[] buf;
IO io;
}

struct IO
{
~this() @safe @nogc scope
{}
}

@safe @nogc void test17395()
{
ubyte[256] buf;
auto chunks = ByChunk!IO(buf[], IO());
chunks.__xdtor(); // auto-generated inclusive (fields and struct) dtor
}

/********************************************/

void main()
Expand All @@ -359,6 +385,7 @@ void main()
test7049();
test16747();
test11();
test17395();

printf("Success\n");
}

0 comments on commit c9ff581

Please sign in to comment.