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 17284 - Template function attribute inference wrongly infer… #7982

Merged
merged 1 commit into from Mar 12, 2018
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
20 changes: 14 additions & 6 deletions src/dmd/safe.d
Expand Up @@ -41,6 +41,7 @@ import dmd.tokens;

bool checkUnsafeAccess(Scope* sc, Expression e, bool readonly, bool printmsg)
{
//printf("checkUnsafeAccess(e: '%s', readonly: %d, printmsg: %d)\n", e.toChars(), readonly, printmsg);
if (e.op != TOK.dotVariable)
return false;
DotVarExp dve = cast(DotVarExp)e;
Expand All @@ -53,18 +54,25 @@ bool checkUnsafeAccess(Scope* sc, Expression e, bool readonly, bool printmsg)
if (!ad)
return false;

if (v.overlapped && v.type.hasPointers() && sc.func.setUnsafe())
const hasPointers = v.type.hasPointers();
if (hasPointers)
{
if (printmsg)
e.error("field `%s.%s` cannot access pointers in `@safe` code that overlap other fields",
ad.toChars(), v.toChars());
return true;
if (ad.sizeok != Sizeok.done)
ad.determineSize(ad.loc); // needed to set v.overlapped

if (v.overlapped && sc.func.setUnsafe())
{
if (printmsg)
e.error("field `%s.%s` cannot access pointers in `@safe` code that overlap other fields",
ad.toChars(), v.toChars());
return true;
}
}

if (readonly || !e.type.isMutable())
return false;

if (v.type.hasPointers() && v.type.toBasetype().ty != Tstruct)
if (hasPointers && v.type.toBasetype().ty != Tstruct)
{
if ((ad.type.alignment() < Target.ptrsize ||
(v.offset & (Target.ptrsize - 1))) &&
Expand Down
20 changes: 20 additions & 0 deletions test/fail_compilation/test17284.d
@@ -0,0 +1,20 @@
/* REQUIRED_ARGS:
TEST_OUTPUT:
---
fail_compilation/test17284.d(16): Error: field `U.c` cannot access pointers in `@safe` code that overlap other fields
pure nothrow @safe void(U t)
Copy link
Member

@PetarKirov PetarKirov Mar 9, 2018

Choose a reason for hiding this comment

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

How about actually verifying that attribute inference considers this function @system?
Also In the bug report me and H. S. Teoh posted several more test cases, which doesn't seem to be addressed here.

Copy link
Member Author

Choose a reason for hiding this comment

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

How about actually verifying that attribute inference considers this function @System?

The message does that.

Also In the bug report me and H. S. Teoh posted several more test cases, which doesn't seem to be addressed here.

This is the root cause. If you find more cases after this one is pulled, please point them out.

---
*/

// https://issues.dlang.org/show_bug.cgi?id=17284

class C { }
union U { C c; int i; }

@safe void func(T)(T t)
{
t.c = new C;
}

pragma(msg, typeof(func!U));
Copy link
Member

Choose a reason for hiding this comment

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

Could be converted into a static assert