Skip to content

[Sema] Note member decl when initializer list default constructs member #121854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Attribute Changes in Clang
Improvements to Clang's diagnostics
-----------------------------------

- Improve the diagnostics for deleted default constructor errors for C++ class
initializer lists that don't explicitly list a class member and thus attempt
to implicitly default construct that member.

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -6113,6 +6113,8 @@ def note_deleted_special_member_class_subobject : Note<
"destructor}5"
"%select{||s||}4"
"|is an ObjC pointer}6">;
def note_default_constructed_field
: Note<"default constructed field %0 declared here">;
def note_deleted_default_ctor_uninit_field : Note<
"%select{default constructor of|constructor inherited by}0 "
"%1 is implicitly deleted because field %2 of "
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9148,6 +9148,17 @@ bool InitializationSequence::Diagnose(Sema &S,
<< (Msg ? Msg->getString() : StringRef()) << ArgsRange;
}

// If it's a default constructed member, but it's not in the
// constructor's initializer list, explicitly note where the member is
// declared so the user can see which member is erroneously initialized
// with a deleted default constructor.
if (Kind.getKind() == InitializationKind::IK_Default &&
(Entity.getKind() == InitializedEntity::EK_Member ||
Entity.getKind() == InitializedEntity::EK_ParenAggInitMember)) {
S.Diag(Entity.getDecl()->getLocation(),
diag::note_default_constructed_field)
<< Entity.getDecl();
}
S.NoteDeletedFunction(Best->Function);
break;
}
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CXX/class/class.init/p1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

namespace test_deleted_ctor_note {
struct A {
int a;
A() = delete; // expected-note {{'A' has been explicitly marked deleted here}}
A(int a_) : a(a_) { }
};

struct B {
A a1, a2, a3; // expected-note {{default constructed field 'a2' declared here}}
B(int a_) : a1(a_), a3(a_) { } // expected-error{{call to deleted constructor of 'A'}}
};
}
2 changes: 1 addition & 1 deletion clang/test/CXX/dcl.decl/dcl.init/p14-0x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Friend {


class S {
NoDefault nd1;
NoDefault nd1; // expected-note {{default constructed field 'nd1' declared here}}
NoDefault nd2 = 42;
Explicit e1; // expected-note {{here}}
Explicit e2 = 42; // expected-error {{no viable conversion}}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaCUDA/inherited-ctor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace DefaultCtorInvalid {
};

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