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 16589 - Taking address of stack variables in @safe code is … #6172
Conversation
Should it be part of |
099677b
to
d91a93e
Compare
Unfortunately, yes, as it is tripping in Phobos. |
…allowed in some cases
Isn't it unnecessarily limiting though to implement this separately from DIP1000? It is perfectly valid to take address of the field and pass it to some function (expecting pointer) for processing, I would be very surprised if there isn't plenty of such code in the wild. Thus if it gets merged alone, it would break some legit use cases without a tool to make it work again (== |
It's been an error for years to take the address of a local in It does break at least one Phobos function (the autotester quits at the first error), which is why I amended it to only check if If the user needs to BTW, this rule nicely resolves the ambiguity of what |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few nits to improve the lot of readers and reduce LOC.
@@ -10581,6 +10581,28 @@ extern (C++) final class AddrExp : UnaExp | |||
|
|||
type = e1.type.pointerTo(); | |||
|
|||
bool checkAddressVar(VarDeclaration v) | |||
{ | |||
if (v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!v)
return true; // nuttin' to do
... rest of the code ...
} | ||
if (sc.func && !sc.intypeof && !v.isDataseg()) | ||
{ | ||
if (sc.func.setUnsafe()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
+ if
= &&
if (sc.func && !sc.intypeof && !v.isDataseg() && sc.func.setUnsafe())
{
...
}
VarDeclaration v = ve.var.isVarDeclaration(); | ||
if (v) | ||
{ | ||
if (!checkAddressVar(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (v && !checkAddressVar(v))
return new ErrorExp();
VarDeclaration v = ve.var.isVarDeclaration(); | ||
if (v && v.storage_class & STCref) | ||
{ | ||
if (!checkAddressVar(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (v && v.storage_class & STCref) && !checkAddressVar(v))
return new ErrorExp();
VarDeclaration v = ve.var.isVarDeclaration(); | ||
if (v) | ||
{ | ||
if (!checkAddressVar(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (v && !checkAddressVar(v))
return new ErrorExp();
Auto-merge toggled on |
…allowed in some cases