[PATCH] D153375: [Clang] Fix incorrect use of direct initialization with copy initialization

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 20 13:57:32 PDT 2023


shafik created this revision.
shafik added reviewers: aaron.ballman, erichkeane, rsmith.
Herald added a project: All.
shafik requested review of this revision.

In `Sema::CreateBuiltinBinOp()` we were incorrectly using direct initialization instead of copy initialization. This led to a crash in a copy initialization case with an enum with a fixed underlying type.

see dcl.init.general p14 <https://eel.is/c++draft/dcl.init#general-14>

This fixes: https://github.com/llvm/llvm-project/issues/62503


https://reviews.llvm.org/D153375

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp


Index: clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
===================================================================
--- clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
+++ clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
@@ -283,3 +283,12 @@
   F f4(const bool x) { return F{x}; }
 #endif
 }
+
+namespace GH62503 {
+enum class E {};
+
+void f() {
+  E e;
+  e = {0}; // expected-error {{cannot initialize a value of type 'E' with an rvalue of type 'int'}}
+}
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15428,8 +15428,8 @@
     // 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());
+    InitializationKind Kind =
+        InitializationKind::CreateCopy(RHSExpr->getBeginLoc(), OpLoc);
     InitializedEntity Entity =
         InitializedEntity::InitializeTemporary(LHSExpr->getType());
     InitializationSequence InitSeq(*this, Entity, Kind, RHSExpr);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153375.533037.patch
Type: text/x-patch
Size: 1229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230620/6647b2d9/attachment.bin>


More information about the cfe-commits mailing list