Skip to content

Commit

Permalink
Merge pull request #7553 from ibuclaw/issue18143
Browse files Browse the repository at this point in the history
fix Issue 18143 - in/out contracts should be implicitly 'this' const
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
  • Loading branch information
dlang-bot authored Jan 6, 2018
2 parents 269693e + 3d024cb commit 4c7ea17
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ extern (C++) abstract class Declaration : Dsymbol
}
}

if (e1 && e1.op == TOKthis && isField())
{
VarDeclaration vthis = (cast(ThisExp)e1).var;
for (Scope* scx = sc; scx; scx = scx.enclosing)
{
if (scx.func == vthis.parent && (scx.flags & SCOPEcontract))
{
if (!flag)
error(loc, "cannot modify parameter 'this' in contract");
return 2; // do not report type related errors
}
}
}

if (v && (isCtorinit() || isField()))
{
// It's only modifiable if inside the right constructor
Expand Down
2 changes: 0 additions & 2 deletions src/dmd/semantic3.d
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ private extern(C++) final class Semantic3Visitor : Visitor
sc2.flags = (sc2.flags & ~SCOPEcontract) | SCOPErequire;

// BUG: need to error if accessing out parameters
// BUG: need to treat parameters as const
// BUG: need to disallow returns and throws
// BUG: verify that all in and ref parameters are read
freq = freq.statementSemantic(sc2);
Expand All @@ -908,7 +907,6 @@ private extern(C++) final class Semantic3Visitor : Visitor
sc2 = scout; //push
sc2.flags = (sc2.flags & ~SCOPEcontract) | SCOPEensure;

// BUG: need to treat parameters as const
// BUG: need to disallow returns and throws

if (funcdecl.fensure && f.next.ty != Tvoid)
Expand Down
43 changes: 43 additions & 0 deletions test/fail_compilation/fail18143.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail18143.d(20): Error: variable fail18143.S.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(21): Error: variable fail18143.S.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(25): Error: variable fail18143.S.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(26): Error: variable fail18143.S.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(35): Error: variable fail18143.C.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(36): Error: variable fail18143.C.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(40): Error: variable fail18143.C.a cannot modify parameter 'this' in contract
fail_compilation/fail18143.d(41): Error: variable fail18143.C.a cannot modify parameter 'this' in contract
---
*/

struct S
{
int a;

this(int n)
in { a = n; } // error, modifying this.a in contract
out { a = n; } // error, modifying this.a in contract
do { }

void foo(int n)
in { a = n; } // error, modifying this.a in contract
out { a = n; } // error, modifying this.a in contract
do { }
}

class C
{
int a;

this(int n)
in { a = n; } // error, modifying this.a in contract
out { a = n; } // error, modifying this.a in contract
do { }

void foo(int n)
in { a = n; } // error, modifying this.a in contract
out { a = n; } // error, modifying this.a in contract
do { }
}

0 comments on commit 4c7ea17

Please sign in to comment.