Bug 24025 – Expressions contained in parentheses should not be assumed to be C casts

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-07-02T18:26:59Z
Last change time
2023-07-16T14:53:05Z
Keywords
diagnostic, pull, rejects-valid
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2023-07-02T18:26:59Z
```d void bar(int x) {} void main() { (&bar)(5); // ok auto foo = &bar; (foo = foo)(5); // ok (*foo)(5) // ok (foo)(5); // error C style cast not allowed (bar)(5); // error C style cast not allowed } ``` The error should not occur. `foo` and `bar` are not types, so this is not a C-style cast. According to the grammar, `PostfixExpression` covers function calls. Its grammar is: ``` PostfixExpression: PrimaryExpression PostfixExpression . Identifier PostfixExpression . TemplateInstance PostfixExpression . NewExpression PostfixExpression ++ PostfixExpression -- PostfixExpression ( ArgumentListopt ) TypeCtorsopt BasicType ( ArgumentListopt ) IndexExpression SliceExpression ``` `PrimaryExpression` can be `( Expression )`, which gets all the way back to a `( PostfixExpression )`, so this should be valid grammar. Indeed, the fact that making it look less like a C cast negates the warning, as the other lines in the function show. C style casts should only be diagnosed when the expression in the parentheses is a type, not just any expression that *might* be a type. Therefore, the error should be deferred to semantic. This came up when I ported some code from C that looked like: ```c (obj->fnptr)(args); ```
Comment #1 by schveiguy — 2023-07-02T18:35:56Z
An alternative to delaying the C style cast to semantic is to just remove the error. If a cast from C is desired, then it will work if the type can be constructed with that parameter. Back when this error was created, something like `int(5)` was not valid code. Now it is. So most cases of casting will just have a failed implicit cast error. Ironically, this error *hurts* porting C code, instead of helping.
Comment #2 by dlang-bot — 2023-07-03T10:40:36Z
@ntrel created dlang/dmd pull request #15377 "Fix Issue 24025 - Expressions contained in parentheses should not be …" fixing this issue: - Fix Issue 24025 - Expressions contained in parentheses should not be assumed to be C casts Allow `(identifier)` and `(BasicType)` when followed by a left parenthesis. These can be a function call or an implicit conversion - the latter may produce a semantic error (not a parse error). https://github.com/dlang/dmd/pull/15377
Comment #3 by dlang-bot — 2023-07-04T08:49:18Z
dlang/dmd pull request #15377 "Fix Issue 24025 - Expressions contained in parentheses should not be …" was merged into master: - 9f782c456e31a4fad5027961d64c346d388f1b7e by Nick Treleaven: Fix Issue 24025 - Expressions contained in parentheses should not be assumed to be C casts Allow `(identifier)` and `(BasicType)` when followed by a left parenthesis. These can be a function call or an implicit conversion - the latter may produce a semantic error (not a parse error). https://github.com/dlang/dmd/pull/15377
Comment #4 by nick — 2023-07-16T14:53:05Z
*** Issue 22217 has been marked as a duplicate of this issue. ***