Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 19731 - auto struct methods whose address is taken don't test invariants #9467

Merged
merged 1 commit into from Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/dmd/dsymbolsem.d
Expand Up @@ -4209,6 +4209,42 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
funcDeclarationSemantic(deld);
}

/* https://issues.dlang.org/show_bug.cgi?id=19731
*
* Some aggregate member functions might have had
* semantic 3 ran on them despite being in semantic1
* (e.g. auto functions); if that is the case, then
* invariants will not be taken into account for them
* because at the time of the analysis it would appear
* as if the struct declaration does not have any
* invariants. To solve this issue, we need to redo
* semantic3 on the function declaration.
*/
private void reinforceInvariant(AggregateDeclaration ad, Scope* sc)
{
// for each member
for(int i = 0; i < ad.members.dim; i++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreachDsymbol.

{
if (!(*ad.members)[i])
continue;
auto fd = (*ad.members)[i].isFuncDeclaration();
if (!fd || fd.generated || fd.semanticRun != PASS.semantic3done)
continue;

/* if it's a user defined function declaration and semantic3
* was already performed on it, create a syntax copy and
* redo the first semantic step.
*/
auto err = global.startGagging();
auto fd_temp = fd.syntaxCopy(null);
if (auto cd = ad.isClassDeclaration())
cd.vtbl.remove(fd.vtblIndex);
fd_temp.dsymbolSemantic(sc);
global.endGagging(err);
(*ad.members)[i] = fd_temp;
}
}

override void visit(StructDeclaration sd)
{
//printf("StructDeclaration::semantic(this=%p, '%s', sizeok = %d)\n", sd, sd.toPrettyChars(), sd.sizeok);
Expand Down Expand Up @@ -4356,6 +4392,8 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
}

sd.inv = buildInv(sd, sc2);
if (sd.inv)
reinforceInvariant(sd, sc2);

Module.dprogress++;
sd.semanticRun = PASS.semanticdone;
Expand Down Expand Up @@ -4980,6 +5018,8 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
}

cldec.inv = buildInv(cldec, sc2);
if (cldec.inv)
reinforceInvariant(cldec, sc2);

Module.dprogress++;
cldec.semanticRun = PASS.semanticdone;
Expand Down
53 changes: 53 additions & 0 deletions test/runnable/test19731.d
@@ -0,0 +1,53 @@
// PERMUTE_ARGS:
struct Foo
{
Object obj_;

invariant (obj_ !is null);

auto obj7()
{
return this.obj_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For LDC, the this VarDeclaration's parent2 isn't the codegen'd Foo.obj7 anymore (so we wrongly assume it's a nested variable). It's another obj7, so I guess the one from the first semantic pass...

}

enum compiles = __traits(compiles, &Foo.init.obj7);
}

class Foo2
{
Object obj_;

invariant (obj_ !is null);

final auto obj7()
{
return this.obj_;
}

enum compiles = __traits(compiles, &Foo.init.obj7);
}

void main()
{
import core.exception : AssertError;
Foo foo = Foo();
Foo2 foo2 = new Foo2();

try
{
foo.obj7.toString();
}
catch(AssertError)
{
try
{
foo2.obj7.toString();
}
catch(AssertError)
{
return;
}
assert(0);
}
assert(0);
}