[Issue 24025] New: Expressions contained in parentheses should not be assumed to be C casts

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 2 18:26:59 UTC 2023


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

          Issue ID: 24025
           Summary: Expressions contained in parentheses should not be
                    assumed to be C casts
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: diagnostic, rejects-valid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: schveiguy at gmail.com

```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);
```

--


More information about the Digitalmars-d-bugs mailing list