[clang] [Clang] Initializer list on RHS of assignment (PR #100548)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 25 21:55:08 PDT 2024


================
@@ -14578,21 +14578,51 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
                                     BinaryOperatorKind Opc,
                                     Expr *LHSExpr, Expr *RHSExpr) {
   if (getLangOpts().CPlusPlus11 && isa<InitListExpr>(RHSExpr)) {
-    // The syntax only allows initializer lists on the RHS of assignment,
-    // so we don't need to worry about accepting invalid code for
-    // non-assignment operators.
-    // C++11 5.17p9:
-    //   The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning
-    //   of x = {} is x = T().
-    InitializationKind Kind = InitializationKind::CreateDirectList(
-        RHSExpr->getBeginLoc(), RHSExpr->getBeginLoc(), RHSExpr->getEndLoc());
+    // C++11 [expr.ass]p9, per CWG2768:
+    //   A braced-init-list B may appear on the right-hand side of
+    //    - an assignment to a scalar of type T, in which case B shall have at
+    //      most a single element. The meaning of x = B is x = t, where t is an
+    //      invented temporary variable declared and initialized as T t = B.
+    if (Opc != BO_Assign) {
----------------
MitalAshok wrote:

Here's the test: https://github.com/llvm/llvm-project/pull/100548/files#diff-b618bc278c2279c9a4b407c5d4863f53c258e1f8bf6629c4daff11bc38c6e452R28

Previously this was accepted. This line in the standard only applies to "scalar = B", not "scalar op= B".

Actually this might be a bit ambiguous. The following example in the standard (<https://eel.is/c++draft/expr.ass#example-1>) does not use a compound assignment with scalars. But "E1 op= E2" seems to only be defined as "E1 = E1 op E2" (<https://eel.is/c++draft/expr.ass#6.sentence-1>), which shouldn't be accepted if E2 is a braced-init-list

Also GCC rejects this (`int i; i += {0};`)

https://github.com/llvm/llvm-project/pull/100548


More information about the cfe-commits mailing list