Skip to content

Commit 39879e4

Browse files
authored
[Sema] Note member decl when initializer list default constructs member (#121854)
Recently I had a scenario where I had: 1. A class C with many members m_1...m_n of the same type T 2. T's default constructor was deleted 3. I accidentally omitted an explicitly constructed member in the initializer list C() : m_1(foo), m_2(bar), ... { } Clang told me that T's default constructor was deleted, and told me that the call to T() was in C() (which it implicitly was), but didn't tell me which member was being default constructed. It was difficult to fix this problem because I had no easy way to list all the members of type T in C and C's superclasses which would have let me find which member was missing, clang/test/CXX/class/class.init/p1.cpp is a simplified version of this problem (a2 is missing from the initializer list of B)
1 parent 2b4b4c0 commit 39879e4

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Attribute Changes in Clang
113113
Improvements to Clang's diagnostics
114114
-----------------------------------
115115

116+
- Improve the diagnostics for deleted default constructor errors for C++ class
117+
initializer lists that don't explicitly list a class member and thus attempt
118+
to implicitly default construct that member.
119+
116120
Improvements to Clang's time-trace
117121
----------------------------------
118122

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6113,6 +6113,8 @@ def note_deleted_special_member_class_subobject : Note<
61136113
"destructor}5"
61146114
"%select{||s||}4"
61156115
"|is an ObjC pointer}6">;
6116+
def note_default_constructed_field
6117+
: Note<"default constructed field %0 declared here">;
61166118
def note_deleted_default_ctor_uninit_field : Note<
61176119
"%select{default constructor of|constructor inherited by}0 "
61186120
"%1 is implicitly deleted because field %2 of "

clang/lib/Sema/SemaInit.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9148,6 +9148,17 @@ bool InitializationSequence::Diagnose(Sema &S,
91489148
<< (Msg ? Msg->getString() : StringRef()) << ArgsRange;
91499149
}
91509150

9151+
// If it's a default constructed member, but it's not in the
9152+
// constructor's initializer list, explicitly note where the member is
9153+
// declared so the user can see which member is erroneously initialized
9154+
// with a deleted default constructor.
9155+
if (Kind.getKind() == InitializationKind::IK_Default &&
9156+
(Entity.getKind() == InitializedEntity::EK_Member ||
9157+
Entity.getKind() == InitializedEntity::EK_ParenAggInitMember)) {
9158+
S.Diag(Entity.getDecl()->getLocation(),
9159+
diag::note_default_constructed_field)
9160+
<< Entity.getDecl();
9161+
}
91519162
S.NoteDeletedFunction(Best->Function);
91529163
break;
91539164
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
namespace test_deleted_ctor_note {
4+
struct A {
5+
int a;
6+
A() = delete; // expected-note {{'A' has been explicitly marked deleted here}}
7+
A(int a_) : a(a_) { }
8+
};
9+
10+
struct B {
11+
A a1, a2, a3; // expected-note {{default constructed field 'a2' declared here}}
12+
B(int a_) : a1(a_), a3(a_) { } // expected-error{{call to deleted constructor of 'A'}}
13+
};
14+
}

clang/test/CXX/dcl.decl/dcl.init/p14-0x.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Friend {
2727

2828

2929
class S {
30-
NoDefault nd1;
30+
NoDefault nd1; // expected-note {{default constructed field 'nd1' declared here}}
3131
NoDefault nd2 = 42;
3232
Explicit e1; // expected-note {{here}}
3333
Explicit e2 = 42; // expected-error {{no viable conversion}}

clang/test/SemaCUDA/inherited-ctor.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace DefaultCtorInvalid {
8181
};
8282

8383
struct C {
84-
struct B b;
84+
struct B b; // expected-note{{default constructed field 'b' declared here}}
8585
C() {} // expected-error{{call to implicitly-deleted default constructor of 'struct B'}}
8686
// expected-note@-6{{default constructor of 'B' is implicitly deleted because field 's' has a deleted default constructor}}
8787
// expected-note@-15{{'S' has been explicitly marked deleted here}}

0 commit comments

Comments
 (0)