Skip to content

Commit af16a4e

Browse files
NoumanAmir657AaronBallman
authored andcommitted
Improve error message for constexpr constructors of virtual base classes
The changes are for better diagnostic/error-messages. The error message of Clang, MSVC, and GCC were compared and MSVC gives more detailed error message so that is used now. Fixes llvm#64843 Differential Revision: https://reviews.llvm.org/D158540
1 parent 980e024 commit af16a4e

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Improvements to Clang's diagnostics
216216
- The fix-it emitted by ``-Wformat`` for scoped enumerations now take the
217217
enumeration's underlying type into account instead of suggesting a type just
218218
based on the format string specifier being used.
219+
- Clang now displays an improved diagnostic and a note when a defaulted special
220+
member is marked ``constexpr`` in a class with a virtual base class
221+
(`#64843: <https://github.com/llvm/llvm-project/issues/64843>`_).
219222

220223
Bug Fixes in This Version
221224
-------------------------

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9454,6 +9454,8 @@ def err_defaulted_copy_assign_not_ref : Error<
94549454
def err_incorrect_defaulted_constexpr : Error<
94559455
"defaulted definition of %sub{select_special_member_kind}0 "
94569456
"is not constexpr">;
9457+
def err_incorrect_defaulted_constexpr_with_vb: Error<
9458+
"%sub{select_special_member_kind}0 cannot be 'constexpr' in a class with virtual base class">;
94579459
def err_incorrect_defaulted_consteval : Error<
94589460
"defaulted declaration of %sub{select_special_member_kind}0 "
94599461
"cannot be consteval because implicit definition is not constexpr">;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7819,10 +7819,17 @@ bool Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD,
78197819
: isa<CXXConstructorDecl>(MD))) &&
78207820
MD->isConstexpr() && !Constexpr &&
78217821
MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) {
7822-
Diag(MD->getBeginLoc(), MD->isConsteval()
7823-
? diag::err_incorrect_defaulted_consteval
7824-
: diag::err_incorrect_defaulted_constexpr)
7825-
<< CSM;
7822+
if (!MD->isConsteval() && RD->getNumVBases()) {
7823+
Diag(MD->getBeginLoc(), diag::err_incorrect_defaulted_constexpr_with_vb)
7824+
<< CSM;
7825+
for (const auto &I : RD->vbases())
7826+
Diag(I.getBeginLoc(), diag::note_constexpr_virtual_base_here);
7827+
} else {
7828+
Diag(MD->getBeginLoc(), MD->isConsteval()
7829+
? diag::err_incorrect_defaulted_consteval
7830+
: diag::err_incorrect_defaulted_constexpr)
7831+
<< CSM;
7832+
}
78267833
// FIXME: Explain why the special member can't be constexpr.
78277834
HadError = true;
78287835
}

clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,12 @@ namespace std_example {
283283
return r;
284284
}
285285
}
286+
287+
struct Base {
288+
constexpr Base() = default;
289+
};
290+
struct Derived : virtual Base { // expected-note 3{{virtual base class declared here}}
291+
constexpr Derived() = default; // expected-error {{default constructor cannot be 'constexpr' in a class with virtual base class}}
292+
constexpr Derived(const Derived&) = default; // expected-error {{copy constructor cannot be 'constexpr' in a class with virtual base class}}
293+
constexpr Derived(Derived&&) = default; // expected-error {{move constructor cannot be 'constexpr' in a class with virtual base class}}
294+
};

0 commit comments

Comments
 (0)