Skip to content

Commit

Permalink
fix Issue 7517 - Interface contracts broken
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Dec 6, 2015
1 parent 539d87c commit 1364454
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/toir.c
Expand Up @@ -148,7 +148,33 @@ elem *getEthis(Loc loc, IRState *irs, Dsymbol *fd)
* adding this frame into the linked list of stack
* frames.
*/
if (thisfd->hasNestedFrameRefs())
if (fdp != thisfd)
{
//printf("L%d fd = %s, fdparent = %s, fd->toParent2() = %s\n",
// __LINE__, fd->toPrettyChars(), fdparent->toPrettyChars(), fdp->toPrettyChars());
assert(fd->ident == Id::require || fd->ident == Id::ensure);
assert(thisfd->hasNestedFrameRefs());

ClassDeclaration *cdp = fdp->isThis()->isClassDeclaration();
ClassDeclaration *cd = thisfd->isThis()->isClassDeclaration();
assert(cdp && cd);

int offset;
cdp->isBaseOf(cd, &offset);
assert(offset != OFFSET_RUNTIME);
//printf("%s to %s, offset = %d\n", cd->toChars(), cdp->toChars(), offset);
if (offset)
{
Symbol *stmp = symbol_genauto(TYnptr);
ethis = el_bin(OPadd, TYnptr, el_var(irs->sthis), el_long(TYsize_t, offset));
ethis = el_bin(OPeq, TYnptr, el_var(stmp), ethis);
ethis = el_combine(ethis, el_ptr(stmp));
//elem_print(ethis);
}
else
ethis = el_ptr(irs->sthis);
}
else if (thisfd->hasNestedFrameRefs())
{
/* Local variables are referenced, can't skip.
* Address of 'sthis' gives the 'this' for the nested
Expand Down
85 changes: 85 additions & 0 deletions test/runnable/testcontracts.d
Expand Up @@ -401,6 +401,90 @@ void test7218()
size_t baz()/*in{} out{}*/body{ return 0; } // NG2
}

/*******************************************/
// 7517

void test7517()
{
static string result;

interface I
{
static I self;

void setEnable()
in
{
assert(self is this);
result ~= "I.setEnable.in/";
assert(!enabled);
}
out
{
assert(self is this);
result ~= "I.setEnable.out/";
assert( enabled);
}

void setDisable()
in
{
assert(self is this);
result ~= "I.setDisable.in/";
assert( enabled);
}
out
{
assert(self is this);
result ~= "I.setDisable.out/";
assert(!enabled);
}

@property bool enabled() const;
}

class C : I
{
static C self;

void setEnable()
in {} // supply in-contract to invoke I.setEnable.in
body
{
assert(self is this);
result ~= "C.setEnable/";
_enabled = true;
}

void setDisable()
{
assert(self is this);
result ~= "C.setDisable/";
_enabled = false;
}

@property bool enabled() const
{
assert(self is this);
result ~= "C.enabled/";
return _enabled;
}

bool _enabled;
}

C c = C.self = new C;
I i = I.self = c;

result = null;
i.setEnable();
assert(result == "I.setEnable.in/C.enabled/C.setEnable/I.setEnable.out/C.enabled/");

result = null;
i.setDisable();
assert(result == "C.setDisable/I.setDisable.out/C.enabled/");
}

/*******************************************/
// 7699

Expand Down Expand Up @@ -788,6 +872,7 @@ int main()
test4785();
test6417();
test7218();
test7517();
test8073();
test8093();
test9383();
Expand Down

0 comments on commit 1364454

Please sign in to comment.