This is an archive of the discontinued LLVM Phabricator instance.

Implement CWG2137 (list-initialization from objects of the same type)
AcceptedPublic

Authored by MitalAshok on Jul 22 2023, 12:07 PM.

Details

Reviewers
rsmith
hubert.reinterpretcast
aaron.ballman
cor3ntin
Group Reviewers
Restricted Project
Restricted Project
Summary

CWG2137 summary: A non-aggregate class being initialized with an initializer list with a single element (previously changed by CWG1467) should prefer initializer-list constructors over copy constructors, like everywhere else.

Diff Detail

Event Timeline

MitalAshok created this revision.Jul 22 2023, 12:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 12:07 PM

clang-format

MitalAshok edited the summary of this revision. (Show Details)Jul 22 2023, 12:19 PM
MitalAshok added a reviewer: rsmith.
MitalAshok published this revision for review.Jul 22 2023, 12:32 PM
MitalAshok added inline comments.
clang/lib/Sema/SemaInit.cpp
4272–4273

This change unfortunately exposes the still-open CWG2311 but allows T{ object_of_type_T } to consider user declared constructors.

I am working on a separate fix for CWG2311 (Consider constructors as below, but then if the chosen constructor is not an initializer-list constructor, elide it).

clang/lib/Sema/SemaOverload.cpp
1470–1480

This allows with struct T { T(const T&); } t; void f(T); for f({ t }) is an exact match standard conversion instead of a user-defined conversion

Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 12:32 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MitalAshok edited the summary of this revision. (Show Details)

Removing "SelfInitIsNotListInit" test case: copy-list-init for non-aggregate classes remains as copy-list-inits, only aggregates copies are converted into copy-inits (which I don't think is observable)

MitalAshok added inline comments.Jul 23 2023, 10:30 AM
clang/test/CXX/drs/dr14xx.cpp
433

This relies on the old wording. "If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization)".

"If T is a class type" -> "If T is an aggregate class type", so this copy-list-initialization remains copy-list-initialization (and should fail for picking the explicit constructor)

MitalAshok added inline comments.Jul 23 2023, 11:01 AM
clang/lib/Sema/SemaInit.cpp
4272–4273

Fix will be here: https://reviews.llvm.org/D156062

auto{ prvalue } will elide a copy for aggregate types again. It will do so after checking constructors for non-aggregate classes now.

Add CWG2311 fix too

Too much code and tests rely on elision of T{ T_prvalue } in C++17, so add change to allow that to be elided when this does not pick a initializer-list constructor.

Target clang 18 instead; add entry to release notes

aaron.ballman added a subscriber: aaron.ballman.

Adding some more reviewers to hopefully get this unstuck.

Btw, it looks like precommit CI may have found issues in libc++ related to your changes.

cor3ntin added inline comments.
clang/lib/Sema/SemaOverload.cpp
1470

Can you specify an exact C++ version in the comment? - I am assuming C++23

clang/test/CXX/drs/dr23xx.cpp
9
50

the dr status html page is generated automatically by leaving a magic comment here - and then running clang/www/make_cxx_dr_status

54
cor3ntin added inline comments.Aug 9 2023, 7:07 AM
clang/lib/Sema/SemaInit.cpp
4351–4352

I think this needs rephrasing. What ambiguity are you referring to?

4351–4352

Fix broken libc++ pair constructor test

Herald added a project: Restricted Project. · View Herald TranscriptAug 10 2023, 12:49 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
MitalAshok marked 2 inline comments as done and an inline comment as not done.Aug 10 2023, 1:06 PM

It seems there were two places which relied on T x = { xvalue_of_type_T } being a copy-initialization: The test at https://reviews.llvm.org/D156032#inline-1509544 and that one pair constructor test.

GCC seems to allow it but I don't think that's standard. I also have a pretty short patch to implement this behaviour if we decide that it needs to be supported (maybe this is a standard defect?)

clang/test/CXX/drs/dr23xx.cpp
9

Wouldn't work in c++98 mode this file is being run in. I've copied this from other tests in this directory. I guess I can put a #if __cplusplus >= 201103L instead?

50

CWG2311 is still open, but clang did support list-initialization elision before this patch. The committee still needs to decide how exactly this elision is going to work, which might be different from how this patch implements it, so hesitant to mark this as done.

Mordante accepted this revision as: Restricted Project.Aug 11 2023, 9:18 AM
Mordante added a subscriber: Mordante.

I only looked at the libc++ changes. The libc++ AIX CI failures are unrelated to your patch. LGTM modulo one nit.

libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp
134

Please remove this line.

aaron.ballman added inline comments.
clang/lib/Sema/SemaInit.cpp
4272–4273

As this was abandoned in favor of doing the fix here, the patch title, summary, and release notes should all be updated to talk about CWG2311.

4275–4276
4354
4357
4362
clang/test/CXX/drs/dr23xx.cpp
9

The current form seems fine to me.

50

CC @Endill -- do we have a special marking for "issue still open in WG21, but we implement the current suggested resolution?"

54
cor3ntin added inline comments.Aug 15 2023, 5:47 AM
clang/test/CXX/drs/dr23xx.cpp
50

18 open sould work

Address comments; better implementation for elision (check after considering only initializer list constructors)

MitalAshok marked 11 inline comments as done.Aug 22 2023, 12:50 PM

rebased onto fast-forwarded main branch

(trying to fix seemingly unrelated CI build failure)

Remove accidental unrelated changelog

cor3ntin accepted this revision.Sep 7 2023, 2:21 AM

LGTM modulo one nit.
@hubert.reinterpretcast If you want to make sure this is conforming

clang/docs/ReleaseNotes.rst
99
This revision is now accepted and ready to land.Sep 7 2023, 2:21 AM