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

dmd accepts conversion from shared(int)* to int* when value comes from method #19306

Open
dlangBugzillaToGithub opened this issue Aug 20, 2017 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

ag0aep6g reported this on 2017-08-20T18:09:10Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=17769

CC List

  • Lucia Cojocaru

Description

----
struct S()
{
    shared(int)* method() { return ptr; }
    shared(int)* ptr;
}

void main()
{
    S!() s;
    int* foo = s.method(); /* accepted; should be rejected */
}
----

These variations are correctly rejected:

----
    auto foo = s.method();
    int* bar = foo; // rejected as expected
    int* baz = s.ptr; // ditto
----
@dlangBugzillaToGithub
Copy link
Author

lucia.mcojocaru commented on 2017-11-02T09:06:44Z

The reason for the bug is that there are different overloads in dcast.d for implicitConvTo for CallExp (int* foo = s.method() falling here) and Expression (where the other examples in this issue go).
https://github.com/dlang/dmd/blob/master/src/ddmd/dcast.d#L179

Expression: https://github.com/dlang/dmd/blob/master/src/ddmd/dcast.d#L194
CallExp: https://github.com/dlang/dmd/blob/master/src/ddmd/dcast.d#L809

For: int* foo = s.method(); 
Code goes into visit(CallExp) and calls visit(Expression) which defaults to NOMATCH(false).
https://github.com/dlang/dmd/blob/master/src/ddmd/dcast.d#L817

On NOMATCH, the checks continue in visit(CallExp) and if nothing is found, it dafaults to success (true).
https://github.com/dlang/dmd/blob/master/src/ddmd/dcast.d#L940


Everything else hits the default in visit(Expression) which is NOMATCH - hence the rejecting.

Now I have a fix here disallowing pointer conversions from shared to nonshared
https://github.com/somzzz/dmd/commit/6fdead01822071b464016f1c0313358f40494de2

But DIP29 allows this kind of conversions as long as the pointers are unique.
https://wiki.dlang.org/DIP29

This results in my fix breaking the following compiler tests for DIP29:
https://github.com/dlang/dmd/blob/master/test/runnable/implicit.d#L217

I'm not sure how to check if the pointer is unique. Also, are there any other subtleties related to DIP29 that these changes could be disregarding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant