Bug 22432 – ImportC: casting result of postfix operator on a parenthesized expression to a typedef’d type is parsed incorrectly
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-10-22T23:24:26Z
Last change time
2021-12-14T10:34:20Z
Keywords
ImportC, pull, rejects-valid
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2021-10-22T23:24:26Z
The following c program fails to compile:
// test.c
struct Foo {
int x;
};
typedef int int32_t;
void foo(void){
struct Foo f = {1};
int x0 = ((int)(f).x);
int x1 = (int)(f).x;
int32_t x2 = (int32_t)(f).x; // Error
int32_t x3 = ((int32_t)(f).x); // Error
int32_t x4 = (int32_t)f.x;
}
// test.c(11): Error: `=`, `;` or `,` expected
// test.c(12): Error: found `.` when expecting `)`
// test.c(12): Error: missing comma
// test.c(12): Error: `=`, `;` or `,` expected
Works with builtin types, fails with typedef’d types.
Comment #1 by dave287091 — 2021-11-03T07:34:31Z
Some more examples:
int ints[] = {1,2,3,4};
int y0 = ints[0];
int y1 = (int)ints[0]; // ok
int y2 = (int)(ints)[0]; // ok
int y3 = (int32_t)ints[0]; // ok
int y4 = (int32_t)(ints)[0]; // Error
int y5 = (int)y4++; // ok
int y6 = (int)(y4)++; // Error
int y7 = (int32_t)y4++; // ok
int y8 = (int32_t)(y4)++; // Error
It looks like postfix expressions aren’t properly being considered for cast expressions.
Comment #2 by dave287091 — 2021-11-03T07:38:00Z
(In reply to dave287091 from comment #1)
> Some more examples:
>
> int ints[] = {1,2,3,4};
> int y0 = ints[0];
> int y1 = (int)ints[0]; // ok
> int y2 = (int)(ints)[0]; // ok
> int y3 = (int32_t)ints[0]; // ok
> int y4 = (int32_t)(ints)[0]; // Error
> int y5 = (int)y4++; // ok
> int y6 = (int)(y4)++; // Error
> int y7 = (int32_t)y4++; // ok
> int y8 = (int32_t)(y4)++; // Error
>
> It looks like postfix expressions aren’t properly being considered for cast
> expressions.
Another one:
int y4b = (int32_t)(ints+1)[0]; // Error
I’m guessing using `->` would also fail but I didn’t test it.
Comment #3 by dave287091 — 2021-11-12T01:07:42Z
Another weird construct that should parse, but fails:
struct Foo {
int x;
};
struct Foo ident(struct Foo f){return f;}
int main(){
int y11 = (ident)(f).x; // should be equivalent to ident(f).x
}
Comment #4 by dlang-bot — 2021-11-14T05:43:36Z
@WalterBright created dlang/dmd pull request #13299 "fix Issue 22432 - ImportC: casting result of postfix operator on a pa…" fixing this issue:
- fix Issue 22432 - ImportC: casting result of postfix operator on a parenthesized expression to a typedef.d type is parsed incorrectly
https://github.com/dlang/dmd/pull/13299
Comment #5 by dlang-bot — 2021-11-14T08:44:43Z
dlang/dmd pull request #13299 "fix Issue 22432 - ImportC: casting result of postfix operator on a pa…" was merged into stable:
- 43bdfcf765551cb772d9dbf0fe75a13fdbaa78b0 by Walter Bright:
fix Issue 22432 - ImportC: casting result of postfix operator on a parenthesized expression to a typedef.d type is parsed incorrectly
https://github.com/dlang/dmd/pull/13299
Comment #6 by dlang-bot — 2021-12-14T10:34:20Z
dlang/dmd pull request #13421 "merge stable" was merged into master:
- f6cb3ed3e340ac37b87d1f58dbfa5ba45aba7f92 by Walter Bright:
fix Issue 22432 - ImportC: casting result of postfix operator on a parenthesized expression to a typedef.d type is parsed incorrectly
https://github.com/dlang/dmd/pull/13421