This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Fix incorrect use of direct initialization with copy initialization
Needs RevisionPublic

Authored by shafik on Jun 20 2023, 1:57 PM.

Details

Reviewers
aaron.ballman
erichkeane
rsmith
cor3ntin
Group Reviewers
Restricted Project
Summary

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

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

Diff Detail

Event Timeline

shafik created this revision.Jun 20 2023, 1:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 1:57 PM
shafik requested review of this revision.Jun 20 2023, 1:57 PM

It looks like the build failure happens on other PR as well.

shafik added a reviewer: Restricted Project.Jun 30 2023, 9:40 AM
cor3ntin accepted this revision.Jun 30 2023, 9:51 AM
cor3ntin added a subscriber: cor3ntin.

LGTM but can you add a line in Release Notes? Thanks

clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
292

Should we add a test that passes here? Even if it currently doesn't crash

enum class E {Foo};
E e;
e = {E::Foo};

This revision is now accepted and ready to land.Jun 30 2023, 9:51 AM
shafik updated this revision to Diff 536451.Jun 30 2023, 3:08 PM
shafik marked an inline comment as done.
  • Added release note
  • Added test case
rsmith added inline comments.Jun 30 2023, 3:42 PM
clang/lib/Sema/SemaExpr.cpp
15441–15443

The code change doesn't match this comment, which says we should use direct non-list initialization. Though the comment is slightly wrong / out of date: the standard text now says:

"an assignment to a scalar, in which case the initializer list shall have at most a single element. The meaning of x = {v}, where T is the scalar type of the expression x, is that of x = T{v}. The meaning of x = {} is x = T{}."

... which says to use direct list initialization to create a temporary in the scalar type case.

rsmith requested changes to this revision.Jun 30 2023, 3:46 PM
rsmith added inline comments.
clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
293

This looks valid to me. The language rules say we treat this as e = E{0};, which we accept.

This revision now requires changes to proceed.Jun 30 2023, 3:46 PM
shafik added inline comments.Jul 5 2023, 4:27 PM
clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
292

Sure that makes sense, more coverage is good.

293

Richard and I discussed this and I agree that based on http://eel.is/c++draft/expr.ass#8.1 this should be the same as e = E{0} and this would be direct init and we should accept this.