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);
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);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits