This is an archive of the discontinued LLVM Phabricator instance.

[clang] Implement CWG 2397
ClosedPublic

Authored by zyounan on Apr 10 2023, 12:12 AM.

Details

Reviewers
aaron.ballman
erichkeane
rsmith
Group Reviewers
Restricted Project
Commits
rGd9826433f31c: [clang] Implement CWG 2397
Summary

This patch implements CWG 2397, allowing array of placeholder type
to be valid.

See also https://wg21.cmeerw.net/cwg/issue2397.

Diff Detail

Event Timeline

zyounan created this revision.Apr 10 2023, 12:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 12:12 AM

Note GCC had implemented such feature since GCC 12.

zyounan published this revision for review.Apr 10 2023, 1:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 1:12 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
shafik added a subscriber: shafik.Apr 10 2023, 3:34 PM

This looks good but I will let at least one more reviewer take a look

aaron.ballman added a reviewer: Restricted Project.Apr 11 2023, 8:25 AM

The changes need a release note, but also this should have changes to clang/test/CXX/drs/dr23xx.cpp with the proper dr markings and update clang/www/cxx_dr_status.html.

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp
12

I think it'd be good to also show a constexpr test, like:

constexpr int foo() {
  int a[] = { 1, 2, 3 };
  auto (&c)[3] = a;

  return c[2];
}

static_assert(foo() == 3, "");

to prove that we actually perform the assignment properly, not just figure out the deduced type correctly.

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
5–6

I've seen worse diagnostics, but the phrasing here is interesting -- if you use int a[5] = b; instead of auto, you get array initializer must be an initializer list as a diagnostic, so I wonder why we're getting such a drastically different diagnostic for auto. Same for the diagnostic below.

Endill added a subscriber: Endill.Apr 11 2023, 8:52 AM

Agree, this test clearly belongs to clang/test/CXX/drs/dr23xx.cpp.
There is a clang/www/make_cxx_dr_status script to update cxx_dr_status page, so you don't have to edit it manually.

zyounan updated this revision to Diff 512656.Apr 11 2023, 10:25 PM
zyounan marked an inline comment as done.

Address comments

Thank you for the suggestion and I've updated my patch. :)

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp
12

Indeed. I will enable it only with C++14 or later. (I didn't come up a way to get around the restriction for C++11, though.)

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
5–6

You're right that such diagnostic looks a bit strange but FYI, GCC now emits diagnostic like so:

auto a[5] = b; // error: unable to deduce 'auto [5]' from 'b'
int c[5] = b;  // error: array must be initialized with a brace-enclosed initializer

I agree that what we expect here is to emit messages just like int a[5] = b; would produce with respect to better understandability, however, the err_auto_var_deduction_failure error is actually emitted due to type deduction failure, whereas err_array_init_not_init_list would be produced later by initialization error IIUC. So, IMHO such message makes sence as per what standard says, that for each type P, after being substituted with deduced A, shall be compatible with type A. temp.deduct.type/1

// Feel free to correct me if I'm wrong :)

aaron.ballman accepted this revision.Apr 12 2023, 6:00 AM

LGTM!

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
5–6

Ah, good catch! Okay, this seems reasonable to me now that I understand it better, thank you.

This revision is now accepted and ready to land.Apr 12 2023, 6:00 AM
This revision was automatically updated to reflect the committed changes.