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 17934 - [scope] scopeness entrypoint for unique/ref-counted… #7284

Merged
merged 1 commit into from
Nov 10, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ddmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

module ddmd.dsymbolsem;

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

import core.stdc.stdio;
import core.stdc.string;
Expand Down Expand Up @@ -2610,6 +2615,23 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
dsym.edtor = dsym.callScopeDtor(sc);
if (dsym.edtor)
{
/* If dsym is a local variable, who's type is a struct with a scope destructor,
* then make dsym scope, too.
*/
if (global.params.vsafe &&
!(dsym.storage_class & (STCparameter | STCtemp | STCfield | STCin | STCforeach | STCresult | STCmanifest)) &&
!dsym.isDataseg() &&
!dsym.doNotInferScope &&
dsym.type.hasPointers())
{
auto tv = dsym.type.baseElemOf();
if (tv.ty == Tstruct &&
(cast(TypeStruct)tv).sym.dtor.storage_class & STCscope)
{
dsym.storage_class |= STCscope;
}
}

if (sc.func && dsym.storage_class & (STCstatic | STCgshared))
dsym.edtor = dsym.edtor.expressionSemantic(sc._module._scope);
else
Expand Down
42 changes: 42 additions & 0 deletions test/fail_compilation/retscope3.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,45 @@ void bar4()
{
}
}

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

/*
TEST_OUTPUT:
---
fail_compilation/retscope3.d(3027): Error: scope variable `l` assigned to `elem` with longer lifetime
---
*/

#line 3000

struct List
{
Elem front() @safe return scope;

~this() @trusted scope;

@disable this(this);

void* data;
}

struct Elem
{
void* data;
}

List list() @trusted
{
return List();
}

void test3000() @safe
{
Elem elem;
{
auto l = list(); // inferred as scope
elem = l.front; // escapes, b/c l isn't scoped
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that defeated by just using list().front though ?

Copy link
Member Author

Choose a reason for hiding this comment

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

The lifetime of the return value of list().front is the lifetime of list().

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, and if you change your test case from auto l = list(); elem = l.front; to elem = list().front it no longer fails. I suppose that's a case similar to static arrays but it doesn't look like this case would be caught either.

Copy link
Member Author

@WalterBright WalterBright Nov 10, 2017

Choose a reason for hiding this comment

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

As always, additional issues should be filed on bugzilla.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't see it, so I added it: https://issues.dlang.org/show_bug.cgi?id=17977

Copy link
Member

Choose a reason for hiding this comment

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

Thanks ! I indeed forgot :)

}
}