[clang] af16a4e - Improve error message for constexpr constructors of virtual base classes
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 3 12:06:20 PDT 2023
Author: Nouman Amir
Date: 2023-10-03T15:06:08-04:00
New Revision: af16a4e131c1383c1b023f9384f0d73a3d207a2f
URL: https://github.com/llvm/llvm-project/commit/af16a4e131c1383c1b023f9384f0d73a3d207a2f
DIFF: https://github.com/llvm/llvm-project/commit/af16a4e131c1383c1b023f9384f0d73a3d207a2f.diff
LOG: 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 https://github.com/llvm/llvm-project/issues/64843
Differential Revision: https://reviews.llvm.org/D158540
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5825e15c76f1da..f81fb27ec57320f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -216,6 +216,9 @@ Improvements to Clang's diagnostics
- The fix-it emitted by ``-Wformat`` for scoped enumerations now take the
enumeration's underlying type into account instead of suggesting a type just
based on the format string specifier being used.
+- Clang now displays an improved diagnostic and a note when a defaulted special
+ member is marked ``constexpr`` in a class with a virtual base class
+ (`#64843: <https://github.com/llvm/llvm-project/issues/64843>`_).
Bug Fixes in This Version
-------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index ac854a86a2fce23..e2eaeb885e2ea77 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9454,6 +9454,8 @@ def err_defaulted_copy_assign_not_ref : Error<
def err_incorrect_defaulted_constexpr : Error<
"defaulted definition of %sub{select_special_member_kind}0 "
"is not constexpr">;
+def err_incorrect_defaulted_constexpr_with_vb: Error<
+ "%sub{select_special_member_kind}0 cannot be 'constexpr' in a class with virtual base class">;
def err_incorrect_defaulted_consteval : Error<
"defaulted declaration of %sub{select_special_member_kind}0 "
"cannot be consteval because implicit definition is not constexpr">;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 0a48d3311d5156c..7ebb38ec2ec0c49 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7819,10 +7819,17 @@ bool Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD,
: isa<CXXConstructorDecl>(MD))) &&
MD->isConstexpr() && !Constexpr &&
MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) {
- Diag(MD->getBeginLoc(), MD->isConsteval()
- ? diag::err_incorrect_defaulted_consteval
- : diag::err_incorrect_defaulted_constexpr)
- << CSM;
+ if (!MD->isConsteval() && RD->getNumVBases()) {
+ Diag(MD->getBeginLoc(), diag::err_incorrect_defaulted_constexpr_with_vb)
+ << CSM;
+ for (const auto &I : RD->vbases())
+ Diag(I.getBeginLoc(), diag::note_constexpr_virtual_base_here);
+ } else {
+ Diag(MD->getBeginLoc(), MD->isConsteval()
+ ? diag::err_incorrect_defaulted_consteval
+ : diag::err_incorrect_defaulted_constexpr)
+ << CSM;
+ }
// FIXME: Explain why the special member can't be constexpr.
HadError = true;
}
diff --git a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
index d545f49fa5db3d6..6214ff8006d67f3 100644
--- a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
+++ b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
@@ -283,3 +283,12 @@ namespace std_example {
return r;
}
}
+
+struct Base {
+ constexpr Base() = default;
+};
+struct Derived : virtual Base { // expected-note 3{{virtual base class declared here}}
+ constexpr Derived() = default; // expected-error {{default constructor cannot be 'constexpr' in a class with virtual base class}}
+ constexpr Derived(const Derived&) = default; // expected-error {{copy constructor cannot be 'constexpr' in a class with virtual base class}}
+ constexpr Derived(Derived&&) = default; // expected-error {{move constructor cannot be 'constexpr' in a class with virtual base class}}
+};
More information about the cfe-commits
mailing list