Skip to content

Commit

Permalink
fix Issue 21324 - @LiVe not detecting overwrite of Owner without disp…
Browse files Browse the repository at this point in the history
…osing of previous owned value
  • Loading branch information
WalterBright authored and dkorpel committed Mar 24, 2022
1 parent 1c60908 commit b5b08e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dmd/ob.d
Expand Up @@ -1977,7 +1977,12 @@ void checkObErrors(ref ObState obstate)
else if (isReadonlyPtr(v))
pvs.state = PtrState.Readonly;
else
{
if (pvs.state == PtrState.Owner && v.type.hasPointersToMutableFields())
v.error(e.loc, "assigning to Owner without disposing of owned value");

pvs.state = PtrState.Owner;
}
pvs.deps.zero();

EscapeByResults er;
Expand Down
1 change: 1 addition & 0 deletions test/fail_compilation/fob1.d
Expand Up @@ -17,6 +17,7 @@ fail_compilation/fob1.d(104): Error: variable `fob1.foo1.p` is returned but is U

/* TEST_OUTPUT:
---
fail_compilation/fob1.d(204): Error: variable `fob1.foo2.p` assigning to Owner without disposing of owned value
fail_compilation/fob1.d(203): Error: variable `fob1.foo2.p` is left dangling at return
---
*/
Expand Down
18 changes: 18 additions & 0 deletions test/fail_compilation/fob2.d
Expand Up @@ -175,3 +175,21 @@ void free7(int*);
{
free7(p);
}

/* TEST_OUTPUT:
---
fail_compilation/fob2.d(807): Error: variable `fob2.test8.p` assigning to Owner without disposing of owned value
---
*/

#line 800

int* malloc8();
void free8(int*);

@live void test8()
{
int* p = malloc8();
p = malloc8(); // error here
free8(p);
}

0 comments on commit b5b08e7

Please sign in to comment.