D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22067 - importC: cast-expression accepted as lvalue in assignment-expression
Summary: importC: cast-expression accepted as lvalue in assignment-expression
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 critical
Assignee: No Owner
URL:
Keywords: accepts-invalid, ImportC, pull
Depends on:
Blocks:
 
Reported: 2021-06-22 13:17 UTC by Iain Buclaw
Modified: 2021-06-24 12:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Iain Buclaw 2021-06-22 13:17:05 UTC
This isn't valid assignment as per C11 6.5.16.
---
(int) i = 42;


The grammar is:
---
(6.5.16)
assignment-expression:
    conditional-expression
    unary-expression assignment-operator assignment-expression

(6.5.3)
unary-expression:
    postfix-expression
    ++ unary-expression
    -- unary-expression
    unary-operator cast-expression
    sizeof unary-expression
    sizeof ( type-name )
    _Alignof ( type-name )

(6.5.2)
postfix-expression:
    primary-expression
    postfix-expression [ expression ]
    postfix-expression ( argument-expression-list(opt) )
    postfix-expression . identifier
    postfix-expression -> identifier
    postfix-expression ++
    postfix-expression --
    ( type-name ) { initializer-list }
    ( type-name ) { initializer-list , }
---

However, currently unary-expression and cast-expression are conflated in the parser, where they should really be separate functions.

---
(6.5.4)
cast-expression:
    unary-expression
    ( type-name ) cast-expression
Comment 1 Iain Buclaw 2021-06-22 14:12:51 UTC
DMD accepts any conditional expression on the LHS and diagnose the invalid lvalue rather than producing a syntax error.

So the issue is that this isn't detected during semantic, unlike issue 22068 (though we could also move that to semantic too to for a consistent error message).
Comment 2 Iain Buclaw 2021-06-22 18:05:28 UTC
Actually, the list of valid lvalues in C is so simple, we can do a first pass in the parser without having to go through semantic.

- CompoundLiteralExp
- StringExp
- PtrExp
- ArrayExp
- IdentifierExp
- DotIdExp (if e1 is an lvalue)
Comment 3 Iain Buclaw 2021-06-22 18:06:05 UTC
(In reply to Iain Buclaw from comment #2)
> Actually, the list of valid lvalues in C is so simple, we can do a first
> pass in the parser without having to go through semantic.
Semantic can deal with more complicated matters, such as const.
Comment 4 Dlang Bot 2021-06-22 22:17:40 UTC
@ibuclaw created dlang/dmd pull request #12736 "fix Issue 22067 - importC: cast-expression accepted as lvalue in assignment-expression" fixing this issue:

- fix Issue 22067 - importC: cast-expression accepted as lvalue in assignment-expression

https://github.com/dlang/dmd/pull/12736
Comment 5 Dlang Bot 2021-06-24 12:23:30 UTC
dlang/dmd pull request #12736 "fix Issue 22067 - importC: cast-expression accepted as lvalue in assignment-expression" was merged into master:

- 41b50c9eebc999a9b28d8301cb2e2c79e806559c by Iain Buclaw:
  fix Issue 22067 - importC: cast-expression accepted as lvalue in assignment-expression

https://github.com/dlang/dmd/pull/12736