[clang] [Clang][C++2d] Implement defaulted postfix increment and decrement operators (P3668R4) (PR #229560)
Muhammad Bassiouni via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 7 20:52:14 PDT 2026
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/229560
>From 4559ad78ffc2ea4b580de598468239211978c59c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 6 Oct 2026 23:50:26 +0300
Subject: [PATCH 01/18] [Clang][C++2d] Implement P3668R4: defaulted postfix
increment and decrement operators
Allow the postfix `operator++(int)` and `operator--(int)` to be explicitly
defaulted, as specified by P3668R4 ([over.inc.default]). The implicit
definition of a defaulted postfix operator for a type C is equivalent to
C tmp(c);
++c; // or --c
return tmp;
where c denotes `*this` for an implicit object member function and the first
parameter otherwise. The copy is direct-initialized and `tmp` is a candidate
for named return value optimization.
The declaration requirements of [over.inc.default]p1 are checked: the first
parameter (counting the implicit object parameter) must be a reference to
C or to volatile C, the declared return type must be C, C must be complete,
and a definition as defaulted that appears in a class must be the first
declaration. Function templates and the prefix forms cannot be defaulted.
The function is defined as deleted per [over.inc.default]p2 when C has no
usable copy constructor, a deleted or inaccessible destructor, or no usable
prefix operator; the reason is explained with notes, and using such an
operator produces a dedicated error. The exception specification is computed
from the implicit definition ([except.spec]p10). A defaulted postfix operator
is implicitly inline and, following the rules for other defaulted functions,
implicitly constexpr when it is constexpr-suitable (C++23 and later) or
constexpr-compatible (C++14 through C++20).
Name lookup of the prefix operator is performed from a context equivalent to
the function body, also for `= default` at namespace scope: the parser now
passes the current scope to SetFunctionBodyKind/SetDeclDefaulted so that the
unqualified lookup results can be stored with the function, in the same way
as for defaulted comparison operators.
Like other recent C++ features, this is available in all C++ language modes
as an extension, diagnosed with -Wc++2d-extensions and -Wpre-c++2d-compat.
Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
---
clang/docs/LanguageExtensions.md | 1 +
clang/docs/ReleaseNotes.md | 7 +
clang/include/clang/AST/Decl.h | 39 +-
.../clang/Basic/DiagnosticCommonKinds.td | 3 +-
.../clang/Basic/DiagnosticSemaKinds.td | 67 ++
clang/include/clang/Sema/Sema.h | 26 +-
clang/lib/AST/Decl.cpp | 18 +-
clang/lib/Parse/ParseDecl.cpp | 2 +-
clang/lib/Parse/ParseDeclCXX.cpp | 2 +-
clang/lib/Parse/Parser.cpp | 3 +-
clang/lib/Sema/SemaDeclCXX.cpp | 685 +++++++++++++++++-
clang/lib/Sema/SemaExpr.cpp | 9 +-
clang/lib/Sema/SemaOverload.cpp | 13 +
clang/lib/Sema/SemaTemplateInstantiate.cpp | 7 +
.../ast-dump-defaulted-postfix-operators.cpp | 92 +++
.../over.inc/over.inc.default/p1.cpp | 205 ++++++
.../over.inc/over.inc.default/p2.cpp | 192 +++++
.../over.inc/over.inc.default/p3.cpp | 263 +++++++
.../cxx2d-defaulted-postfix-operators.cpp | 53 ++
.../PCH/cxx2d-defaulted-postfix-operators.cpp | 59 ++
.../cxx2d-defaulted-postfix-operators.cpp | 108 +++
clang/www/cxx_status.html | 2 +-
22 files changed, 1830 insertions(+), 26 deletions(-)
create mode 100644 clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
create mode 100644 clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
create mode 100644 clang/test/CXX/over/over.oper/over.inc/over.inc.default/p2.cpp
create mode 100644 clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
create mode 100644 clang/test/CodeGenCXX/cxx2d-defaulted-postfix-operators.cpp
create mode 100644 clang/test/PCH/cxx2d-defaulted-postfix-operators.cpp
create mode 100644 clang/test/SemaCXX/cxx2d-defaulted-postfix-operators.cpp
diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index 7ec585d8362927..4e8047e596cbc0 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -1784,6 +1784,7 @@ More information can be found [here](Modules.md).
| Trivial Relocatability | \_\_cpp_trivial_relocatability | C++26 | C++03 |
|``auto()`` cast | \_\_cpp_auto_cast | C++26 | C++03 |
| Pack Indexing for Template Names | \_\_cpp_pack_indexing >= 202606L | C++2d | C++03 |
+| Defaulted postfix `++` and `--` operators | | C++2d | C++03 |
| Designated initializers (N494) | | C99 | C89 |
| `_Complex` (N693) | | C99 | C89, C++ |
| `_Bool` (N815) | | C99 | C89 |
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index bf190df9769ddf..bcc9662c59ecf5 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -165,6 +165,13 @@ features cannot lower the translation-unit ABI level;
`-Wc++2d-extensions` and `-Wpre-c++2d-compat`, and `__cpp_pack_indexing` is
bumped to `202606L`.
+- Clang now supports [P3668R4](https://wg21.link/p3668r4) (Defaulting postfix
+ increment and decrement operations), which allows the postfix `operator++(int)`
+ and `operator--(int)` to be explicitly defaulted. The implicit definition
+ copies the object, applies the corresponding prefix operator, and returns the
+ copy. This is available in all C++ language modes as an extension, controlled
+ by `-Wc++2d-extensions` and `-Wpre-c++2d-compat`.
+
#### C++2c Feature Support
- Added `__builtin_type_order` for compatibility with GCC as part of the
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index c64dc8c02d40e0..34e7f4ea701cdd 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -2026,6 +2026,17 @@ enum class CXXSpecialMemberKind {
Invalid
};
+/// Kinds of defaulted postfix increment and decrement operator functions
+/// (C++2d [over.inc.default]).
+enum class PostfixOperatorKind : unsigned char {
+ /// This is not a defaultable postfix increment or decrement operator.
+ None,
+ /// This is a postfix increment operator, 'operator++(int)'.
+ Increment,
+ /// This is a postfix decrement operator, 'operator--(int)'.
+ Decrement,
+};
+
/// Kinds of defaulted comparison operator functions.
enum class DefaultedComparisonKind : unsigned char {
/// This is not a defaultable comparison operator.
@@ -2124,17 +2135,26 @@ class FunctionDecl : public DeclaratorDecl,
LLVM_PREFERRED_TYPE(CXXSpecialMemberKind)
unsigned SpecialMember : 8;
unsigned Comparison : 8;
+ LLVM_PREFERRED_TYPE(PostfixOperatorKind)
+ unsigned PostfixOperator : 2;
public:
DefaultedFunctionKind()
: SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
- Comparison(llvm::to_underlying(DefaultedComparisonKind::None)) {}
+ Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
+ PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
DefaultedFunctionKind(CXXSpecialMemberKind CSM)
: SpecialMember(llvm::to_underlying(CSM)),
- Comparison(llvm::to_underlying(DefaultedComparisonKind::None)) {}
+ Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
+ PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
DefaultedFunctionKind(DefaultedComparisonKind Comp)
: SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
- Comparison(llvm::to_underlying(Comp)) {}
+ Comparison(llvm::to_underlying(Comp)),
+ PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
+ DefaultedFunctionKind(PostfixOperatorKind PO)
+ : SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
+ Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
+ PostfixOperator(llvm::to_underlying(PO)) {}
bool isSpecialMember() const {
return static_cast<CXXSpecialMemberKind>(SpecialMember) !=
@@ -2144,9 +2164,13 @@ class FunctionDecl : public DeclaratorDecl,
return static_cast<DefaultedComparisonKind>(Comparison) !=
DefaultedComparisonKind::None;
}
+ bool isPostfixOperator() const {
+ return static_cast<PostfixOperatorKind>(PostfixOperator) !=
+ PostfixOperatorKind::None;
+ }
explicit operator bool() const {
- return isSpecialMember() || isComparison();
+ return isSpecialMember() || isComparison() || isPostfixOperator();
}
CXXSpecialMemberKind asSpecialMember() const {
@@ -2155,6 +2179,9 @@ class FunctionDecl : public DeclaratorDecl,
DefaultedComparisonKind asComparison() const {
return static_cast<DefaultedComparisonKind>(Comparison);
}
+ PostfixOperatorKind asPostfixOperator() const {
+ return static_cast<PostfixOperatorKind>(PostfixOperator);
+ }
/// Get the index of this function kind for use in diagnostics.
unsigned getDiagnosticIndex() const {
@@ -2163,7 +2190,9 @@ class FunctionDecl : public DeclaratorDecl,
"invalid should have highest index");
static_assert((unsigned)DefaultedComparisonKind::None == 0,
"none should be equal to zero");
- return SpecialMember + Comparison;
+ static_assert((unsigned)PostfixOperatorKind::None == 0,
+ "none should be equal to zero");
+ return SpecialMember + Comparison + PostfixOperator;
}
};
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index d8dfb3e306ed51..2a503cd76e0eba 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -97,7 +97,8 @@ def err_invalid_storage_class_in_func_decl : Error<
"invalid storage class specifier in function declarator">;
def err_expected_namespace_name : Error<"expected namespace name">;
def err_default_special_members : Error<
- "only special member functions %select{|and comparison operators }0"
+ "only special member functions%select{| and comparison operators|, "
+ "comparison operators, and postfix increment and decrement operators}0 "
"may be defaulted">;
def err_deleted_non_function : Error<
"only functions can have deleted definitions">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d293a9798da6a3..b2f72cab83243d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -88,6 +88,8 @@ defm equals_this_lambda_capture : CXX20Compat<
"explicit capture of 'this' with a capture default of '=' is">;
defm capture_binding : CXX20Compat<"captured structured bindings are">;
defm defaulted_comparison : CXX20Compat<"defaulted comparison operators are">;
+defm defaulted_postfix_operator : CXX29Compat<
+ "defaulted postfix increment and decrement operators are">;
// C++23 compatibility with C++20 and earlier.
defm constexpr_static_var : CXX23Compat<
@@ -10675,6 +10677,71 @@ def err_volatile_comparison_operator : Error<
def err_ref_qualifier_comparison_operator : Error<
"ref-qualifier '&&' is not allowed on a defaulted comparison operator">;
+// C++2d defaulted postfix increment and decrement operators
+// This corresponds to values of PostfixOperatorKind.
+def select_defaulted_postfix_operator_kind : TextSubstitution<
+ "%select{<ERROR>|postfix increment|postfix decrement}0 operator">;
+def err_defaulted_prefix_operator : Error<
+ "only the postfix form of 'operator%select{++|--}0' can be defaulted">;
+def err_defaulted_postfix_operator_template : Error<
+ "%sub{select_defaulted_postfix_operator_kind}0 template cannot be "
+ "defaulted">;
+def err_defaulted_postfix_operator_const : Error<
+ "defaulted member %sub{select_defaulted_postfix_operator_kind}0 cannot be "
+ "const-qualified">;
+def err_defaulted_postfix_operator_param : Error<
+ "invalid %select{explicit object|first}1 parameter type for defaulted "
+ "%sub{select_defaulted_postfix_operator_kind}0; found %2, expected %3 "
+ "or %4">;
+def err_defaulted_postfix_operator_param_unknown : Error<
+ "invalid first parameter type for defaulted non-member "
+ "%sub{select_defaulted_postfix_operator_kind}0; found %1, expected "
+ "reference to a non-const class or enumeration type">;
+def err_defaulted_postfix_operator_return_type : Error<
+ "return type for defaulted %sub{select_defaulted_postfix_operator_kind}0 "
+ "must be %1, not %2">;
+def err_defaulted_postfix_operator_incomplete : Error<
+ "cannot default %sub{select_defaulted_postfix_operator_kind}0 for "
+ "incomplete type %1">;
+def err_non_first_default_postfix_operator_deletes : Error<
+ "defaulting this %sub{select_defaulted_postfix_operator_kind}0 "
+ "would delete it after its first declaration">;
+def err_non_first_default_postfix_operator_in_class : Error<
+ "defaulting this %sub{select_defaulted_postfix_operator_kind}0 "
+ "is not allowed because it was already declared outside the class">;
+def warn_defaulted_postfix_operator_deleted : Warning<
+ "explicitly defaulted %sub{select_defaulted_postfix_operator_kind}0 is "
+ "implicitly deleted">, InGroup<DefaultedFunctionDeleted>;
+def note_defaulted_postfix_operator_no_viable : Note<
+ "defaulted %0 is implicitly deleted because there is no viable "
+ "%select{constructor to copy|prefix 'operator++' for|prefix 'operator--' "
+ "for}1 an lvalue of type %2">;
+def note_defaulted_postfix_operator_ambiguous : Note<
+ "defaulted %0 is implicitly deleted because %select{copy construction of|"
+ "prefix 'operator++' for|prefix 'operator--' for}1 an lvalue of type %2 "
+ "is ambiguous">;
+def note_defaulted_postfix_operator_calls_deleted : Note<
+ "defaulted %0 is implicitly deleted because it would invoke a deleted "
+ "%select{constructor|destructor|prefix 'operator++'|prefix 'operator--'}1">;
+def note_defaulted_postfix_operator_inaccessible : Note<
+ "defaulted %0 is implicitly deleted because it would invoke a "
+ "%select{private|protected}2 %select{constructor|destructor|"
+ "prefix 'operator++'|prefix 'operator--'}1 of %5">;
+def err_defaulted_postfix_operator_constexpr_mismatch : Error<
+ "defaulted definition of %sub{select_defaulted_postfix_operator_kind}0 "
+ "cannot be declared %select{constexpr|consteval}1 because it invokes a "
+ "non-constexpr function">;
+def note_defaulted_postfix_operator_not_constexpr_here : Note<
+ "non-constexpr %select{constructor|destructor|prefix 'operator++'|"
+ "prefix 'operator--'|conversion function}0 declared here">;
+def err_ovl_deleted_postfix_operator : Error<
+ "object of type %0 cannot be %select{<ERROR>|incremented|decremented}1 "
+ "because its defaulted %sub{select_defaulted_postfix_operator_kind}1 is "
+ "implicitly deleted">;
+def note_postfix_operator_synthesized_at : Note<
+ "in defaulted %sub{select_defaulted_postfix_operator_kind}0 for %1 "
+ "first required here">;
+
def ext_implicit_exception_spec_mismatch : ExtWarn<
"function previously declared with an %select{explicit|implicit}0 exception "
"specification redeclared with an %select{implicit|explicit}0 exception "
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 3edf6f6c295002..6c615b482c7fc6 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6214,6 +6214,21 @@ class Sema final : public SemaBase {
void DefineDefaultedComparison(SourceLocation Loc, FunctionDecl *FD,
DefaultedComparisonKind DCK);
+ /// Check a defaulted postfix increment or decrement operator function
+ /// (C++2d [over.inc.default]), defining it as deleted if necessary.
+ ///
+ /// \param S The scope in which the function is defaulted, used to perform
+ /// the unqualified name lookups needed by the implicit definition, or
+ /// \c nullptr if those lookups have already been performed.
+ ///
+ /// \returns \c true if the function is ill-formed.
+ bool CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
+ PostfixOperatorKind Kind);
+ /// Synthesize the body of a defaulted postfix increment or decrement
+ /// operator function (C++2d [over.inc.default]p3).
+ void DefineDefaultedPostfixOperator(SourceLocation Loc, FunctionDecl *FD,
+ PostfixOperatorKind Kind);
+
void CheckExplicitObjectMemberFunction(Declarator &D, DeclarationName Name,
QualType R, bool IsLambda,
DeclContext *DC = nullptr);
@@ -6531,10 +6546,17 @@ class Sema final : public SemaBase {
void ActOnPureSpecifier(Decl *D, SourceLocation PureSpecLoc);
void SetDeclDeleted(Decl *dcl, SourceLocation DelLoc,
StringLiteral *Message = nullptr);
- void SetDeclDefaulted(Decl *dcl, SourceLocation DefaultLoc);
+ /// Mark the given declaration as explicitly defaulted.
+ ///
+ /// \param S The scope in which the function is defaulted, if it is
+ /// available. It is used to perform the unqualified name lookups needed by
+ /// the implicit definition of an out-of-class defaulted function.
+ void SetDeclDefaulted(Decl *dcl, SourceLocation DefaultLoc,
+ Scope *S = nullptr);
void SetFunctionBodyKind(Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
- StringLiteral *DeletedMessage = nullptr);
+ StringLiteral *DeletedMessage = nullptr,
+ Scope *S = nullptr);
void ActOnStartTrailingRequiresClause(Scope *S, Declarator &D);
ExprResult ActOnFinishTrailingRequiresClause(ExprResult ConstraintExpr);
ExprResult ActOnRequiresClause(ExprResult ConstraintExpr);
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index b145560508e956..aa0f37aa1a782e 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3306,7 +3306,23 @@ FunctionDecl::getDefaultedFunctionKind() const {
return CXXSpecialMemberKind::Destructor;
}
- switch (getDeclName().getCXXOverloadedOperator()) {
+ switch (OverloadedOperatorKind OO =
+ getDeclName().getCXXOverloadedOperator()) {
+ case OO_PlusPlus:
+ case OO_MinusMinus: {
+ // Only the postfix forms can be defaulted. They are distinguished from the
+ // prefix forms by their trailing 'int' parameter, so they have exactly two
+ // parameters once the implicit object parameter (if any) is counted.
+ unsigned NumParams = getNumParams();
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(this);
+ MD && MD->isImplicitObjectMemberFunction())
+ ++NumParams;
+ if (NumParams != 2)
+ break;
+ return OO == OO_PlusPlus ? PostfixOperatorKind::Increment
+ : PostfixOperatorKind::Decrement;
+ }
+
case OO_EqualEqual:
return DefaultedComparisonKind::Equal;
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index fbecae7b80fb97..c3da1f50ba23fe 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -2628,7 +2628,7 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
<< 0 /* default */;
else
Diag(ConsumeToken(), diag::err_default_special_members)
- << getLangOpts().CPlusPlus20;
+ << (getLangOpts().CPlusPlus29 ? 2 : getLangOpts().CPlusPlus20);
} else {
InitializerScopeRAII InitScope(*this, D, ThisDecl);
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 1eeb0d21b68d7b..644b2e1f180dc3 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -3377,7 +3377,7 @@ ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
<< 0 /* default */;
else
Diag(ConsumeToken(), diag::err_default_special_members)
- << getLangOpts().CPlusPlus20;
+ << (getLangOpts().CPlusPlus29 ? 2 : getLangOpts().CPlusPlus20);
return ExprError();
}
}
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index c86ed6b2ea3f98..94681cb758c588 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1378,7 +1378,8 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
D.getMutableDeclSpec().abort();
if (BodyKind != Sema::FnBodyKind::Other) {
- Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage);
+ Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage,
+ getCurScope());
Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
return Res;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 5345966419d40b..97a3114ca99a3a 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -6944,6 +6944,9 @@ static void DefineDefaultedFunction(Sema &S, FunctionDecl *FD,
FunctionDecl::DefaultedFunctionKind DFK = FD->getDefaultedFunctionKind();
if (DFK.isComparison())
return S.DefineDefaultedComparison(DefaultLoc, FD, DFK.asComparison());
+ if (DFK.isPostfixOperator())
+ return S.DefineDefaultedPostfixOperator(DefaultLoc, FD,
+ DFK.asPostfixOperator());
switch (DFK.asSpecialMember()) {
case CXXSpecialMemberKind::DefaultConstructor:
@@ -7820,6 +7823,11 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
FunctionDecl *FD,
DefaultedComparisonKind DCK);
+static Sema::ImplicitExceptionSpecification
+ComputeDefaultedPostfixOperatorExceptionSpec(Sema &S, SourceLocation Loc,
+ FunctionDecl *FD,
+ PostfixOperatorKind Kind);
+
static Sema::ImplicitExceptionSpecification
computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, FunctionDecl *FD) {
auto DFK = FD->getDefaultedFunctionKind();
@@ -7829,6 +7837,9 @@ computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, FunctionDecl *FD) {
if (DFK.isComparison())
return ComputeDefaultedComparisonExceptionSpec(S, Loc, FD,
DFK.asComparison());
+ if (DFK.isPostfixOperator())
+ return ComputeDefaultedPostfixOperatorExceptionSpec(
+ S, Loc, FD, DFK.asPostfixOperator());
auto *CD = cast<CXXConstructorDecl>(FD);
assert(CD->getInheritedConstructor() &&
@@ -7887,6 +7898,13 @@ void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) {
}
}
+ if (DefKind.isPostfixOperator()) {
+ if (CheckExplicitlyDefaultedPostfixOperator(S, FD,
+ DefKind.asPostfixOperator()))
+ FD->setInvalidDecl();
+ return;
+ }
+
if (DefKind.isSpecialMember()
? CheckExplicitlyDefaultedSpecialMember(cast<CXXMethodDecl>(FD),
DefKind.asSpecialMember(),
@@ -9481,6 +9499,620 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
return ExceptSpec;
}
+//===----------------------------------------------------------------------===//
+// C++2d [over.inc.default]: defaulted postfix increment and decrement operator
+// functions (P3668R4).
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// The operations performed by the implicit definition of a defaulted postfix
+/// increment or decrement operator function. The values correspond to %select
+/// indices in the diagnostics explaining why such a function is deleted or is
+/// not constexpr.
+enum class PostfixOperatorStep {
+ Constructor,
+ Destructor,
+ PrefixIncrement,
+ PrefixDecrement,
+ ConversionFunction
+};
+
+/// Which diagnostics to produce while analyzing a defaulted postfix increment
+/// or decrement operator function.
+enum class PostfixOperatorDiagnoseKind {
+ NoDiagnostics,
+ ExplainDeleted,
+ ExplainConstexpr
+};
+
+/// Information about a defaulted postfix increment or decrement operator
+/// function, as determined by analyzeDefaultedPostfixOperator.
+struct DefaultedPostfixOperatorInfo {
+ /// Whether the function should be defined as deleted.
+ bool Deleted = false;
+ /// Whether every function invoked by the implicit definition is constexpr.
+ bool Constexpr = true;
+};
+} // namespace
+
+/// Get the type of the first parameter of a postfix increment or decrement
+/// operator function, where the implicit object parameter (if any) is
+/// considered to be the first parameter.
+static QualType getPostfixOperatorFirstParamType(const FunctionDecl *FD) {
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
+ return MD->getFunctionObjectParameterReferenceType();
+ return FD->getParamDecl(0)->getType();
+}
+
+/// Get the type C for which a well-formed defaulted postfix increment or
+/// decrement operator function is defaulted.
+static QualType getDefaultedPostfixOperatorType(const FunctionDecl *FD) {
+ return getPostfixOperatorFirstParamType(FD)
+ .getNonReferenceType()
+ .getUnqualifiedType();
+}
+
+/// Determine whether a defaulted postfix increment or decrement operator
+/// function for type \p C should be defined as deleted, as specified in C++2d
+/// [over.inc.default]p2, and whether every function it would invoke is
+/// constexpr.
+static DefaultedPostfixOperatorInfo
+analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
+ PostfixOperatorKind Kind, QualType C,
+ PostfixOperatorDiagnoseKind Diagnose) {
+ using DiagnoseKind = PostfixOperatorDiagnoseKind;
+ DefaultedPostfixOperatorInfo Info;
+ SourceLocation Loc = FD->getLocation();
+
+ // C++2d [over.inc.default]p1:
+ // Name lookups and access checks in the implicit definition of a defaulted
+ // postfix increment or decrement operator function are performed from a
+ // context equivalent to its function-body.
+ Sema::ContextRAII SavedContext(S, FD);
+
+ // Let c be an lvalue of type C.
+ OpaqueValueExpr Obj(Loc, C, VK_LValue);
+ Expr *Args[] = {&Obj};
+
+ auto Deleted = [&] {
+ Info.Deleted = true;
+ return Info;
+ };
+ auto InaccessibleDiag = [&](PostfixOperatorStep Step) {
+ return Diagnose == DiagnoseKind::ExplainDeleted
+ ? S.PDiag(diag::note_defaulted_postfix_operator_inaccessible)
+ << FD << (int)Step
+ : S.PDiag();
+ };
+ auto NoteCallsDeleted = [&](PostfixOperatorStep Step, FunctionDecl *Callee) {
+ if (Diagnose != DiagnoseKind::ExplainDeleted)
+ return;
+ S.Diag(Loc, diag::note_defaulted_postfix_operator_calls_deleted)
+ << FD << (int)Step;
+ if (Callee)
+ S.NoteDeletedFunction(Callee);
+ };
+ auto CheckConstexpr = [&](const FunctionDecl *Callee,
+ PostfixOperatorStep Step) {
+ if (Callee->isConstexpr())
+ return;
+ Info.Constexpr = false;
+ if (Diagnose == DiagnoseKind::ExplainConstexpr)
+ S.Diag(Callee->getLocation(),
+ diag::note_defaulted_postfix_operator_not_constexpr_here)
+ << (int)Step;
+ };
+
+ // C++2d [over.inc.default]p2:
+ // A defaulted postfix increment or decrement operator function for a type
+ // C is defined as deleted if
+ CXXRecordDecl *RD = C->getAsCXXRecordDecl();
+ if (RD) {
+ // -- C is a class type for which overload resolution, as applied to
+ // direct-initialization of a variable of type C from an lvalue of
+ // type C, does not result in a usable candidate,
+ InitializedEntity Entity = InitializedEntity::InitializeTemporary(C);
+ InitializationKind InitKind =
+ InitializationKind::CreateDirect(Loc, Loc, Loc);
+ InitializationSequence Seq(S, Entity, InitKind, Args);
+ if (Seq.Failed()) {
+ if (Diagnose == DiagnoseKind::ExplainDeleted) {
+ OverloadCandidateSet &Cands = Seq.getFailedCandidateSet();
+ OverloadingResult OR =
+ Seq.getFailureKind() ==
+ InitializationSequence::FK_ConstructorOverloadFailed
+ ? Seq.getFailedOverloadResult()
+ : OR_No_Viable_Function;
+ OverloadCandidateSet::iterator Best;
+ switch (OR) {
+ case OR_Deleted:
+ NoteCallsDeleted(PostfixOperatorStep::Constructor,
+ Cands.BestViableFunction(S, Loc, Best) == OR_Deleted
+ ? Best->Function
+ : nullptr);
+ break;
+ case OR_Ambiguous:
+ Cands.NoteCandidates(
+ PartialDiagnosticAt(
+ Loc, S.PDiag(diag::note_defaulted_postfix_operator_ambiguous)
+ << FD << 0 << C),
+ S, OCD_AmbiguousCandidates, Args, /*Opc=*/"", Loc);
+ break;
+ case OR_Success:
+ case OR_No_Viable_Function:
+ Cands.NoteCandidates(
+ PartialDiagnosticAt(
+ Loc, S.PDiag(diag::note_defaulted_postfix_operator_no_viable)
+ << FD << 0 << C),
+ S, OCD_AllCandidates, Args, /*Opc=*/"", Loc);
+ break;
+ }
+ }
+ return Deleted();
+ }
+ for (const InitializationSequence::Step &Step : Seq.steps()) {
+ if (Step.Kind != InitializationSequence::SK_ConstructorInitialization)
+ continue;
+ if (!S.isMemberAccessibleForDeletion(
+ RD, Step.Function.FoundDecl, C, Loc,
+ InaccessibleDiag(PostfixOperatorStep::Constructor)))
+ return Deleted();
+ CheckConstexpr(Step.Function.Function, PostfixOperatorStep::Constructor);
+ }
+
+ // -- C has a destructor that is deleted or inaccessible from the context
+ // of the function-body, or
+ if (CXXDestructorDecl *Dtor = S.LookupDestructor(RD)) {
+ if (Dtor->isDeleted()) {
+ NoteCallsDeleted(PostfixOperatorStep::Destructor, Dtor);
+ return Deleted();
+ }
+ if (!S.isMemberAccessibleForDeletion(
+ RD, DeclAccessPair::make(Dtor, Dtor->getAccess()), C, Loc,
+ InaccessibleDiag(PostfixOperatorStep::Destructor)))
+ return Deleted();
+ }
+ }
+
+ // -- for an lvalue c of type C, overload resolution as applied to ++c for a
+ // postfix increment operator function or --c for a postfix decrement
+ // operator function does not result in a usable candidate.
+ bool IsIncrement = Kind == PostfixOperatorKind::Increment;
+ PostfixOperatorStep PrefixStep = IsIncrement
+ ? PostfixOperatorStep::PrefixIncrement
+ : PostfixOperatorStep::PrefixDecrement;
+ UnresolvedSet<16> Fns;
+ if (auto *DFI = FD->getDefaultedOrDeletedInfo())
+ Fns.assign(DFI->getUnqualifiedLookups().begin(),
+ DFI->getUnqualifiedLookups().end());
+ OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Operator);
+ S.LookupOverloadedUnaryOp(
+ CandidateSet, IsIncrement ? OO_PlusPlus : OO_MinusMinus, Fns, Args);
+
+ OverloadCandidateSet::iterator Best;
+ switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
+ case OR_Success:
+ if (FunctionDecl *Prefix = Best->Function) {
+ // Only a member of C can be inaccessible here; the non-member
+ // candidates were found by unqualified lookup and ADL.
+ if (RD && Best->FoundDecl.getDecl() &&
+ Best->FoundDecl.getDecl()->isCXXClassMember() &&
+ !S.isMemberAccessibleForDeletion(RD, Best->FoundDecl, C, Loc,
+ InaccessibleDiag(PrefixStep)))
+ return Deleted();
+ CheckConstexpr(Prefix, PrefixStep);
+ } else if (!Best->Conversions.empty() &&
+ Best->Conversions[0].isUserDefined()) {
+ // A built-in candidate, reached through a conversion function.
+ CheckConstexpr(Best->Conversions[0].UserDefined.ConversionFunction,
+ PostfixOperatorStep::ConversionFunction);
+ }
+ break;
+
+ case OR_Ambiguous:
+ if (Diagnose == DiagnoseKind::ExplainDeleted)
+ CandidateSet.NoteCandidates(
+ PartialDiagnosticAt(
+ Loc, S.PDiag(diag::note_defaulted_postfix_operator_ambiguous)
+ << FD << (int)Kind << C),
+ S, OCD_AmbiguousCandidates, Args, IsIncrement ? "++" : "--", Loc);
+ return Deleted();
+
+ case OR_Deleted:
+ NoteCallsDeleted(PrefixStep, Best->Function);
+ return Deleted();
+
+ case OR_No_Viable_Function:
+ // Don't list the non-viable candidates: like for any other unary operator,
+ // the candidate notes can't sensibly describe an arity mismatch for a
+ // member postfix operator (whose object argument is not a parameter).
+ if (Diagnose == DiagnoseKind::ExplainDeleted)
+ S.Diag(Loc, diag::note_defaulted_postfix_operator_no_viable)
+ << FD << (int)Kind << C;
+ return Deleted();
+ }
+
+ return Info;
+}
+
+/// Build the body of a defaulted postfix increment or decrement operator
+/// function. C++2d [over.inc.default]p3:
+/// The implicit definition of a defaulted postfix increment or decrement
+/// operator function F that is not defined as deleted for a type C is
+/// equivalent to:
+/// C tmp(c);
+/// ++c; // or --c
+/// return tmp;
+/// where tmp is a variable defined for exposition only, and c is an lvalue
+/// that denotes *this if F is an implicit object member function, or the
+/// first parameter of F otherwise.
+static StmtResult buildDefaultedPostfixOperatorBody(Sema &S, FunctionDecl *FD,
+ PostfixOperatorKind Kind,
+ SourceLocation Loc) {
+ Sema::CompoundScopeRAII CompoundScope(S);
+
+ // Build an expression denoting 'c'.
+ auto BuildObject = [&]() -> ExprResult {
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ MD && MD->isImplicitObjectMemberFunction()) {
+ ExprResult This = S.ActOnCXXThis(Loc);
+ if (This.isInvalid())
+ return ExprError();
+ return S.CreateBuiltinUnaryOp(Loc, UO_Deref, This.get());
+ }
+ ParmVarDecl *Param = FD->getParamDecl(0);
+ return S.BuildDeclarationNameExpr(
+ CXXScopeSpec(), DeclarationNameInfo(Param->getDeclName(), Loc), Param);
+ };
+
+ // C tmp(c);
+ ExprResult Init = BuildObject();
+ if (Init.isInvalid())
+ return StmtError();
+ QualType C = FD->getReturnType();
+ IdentifierInfo *TmpName = &S.Context.Idents.get("tmp");
+ VarDecl *Tmp =
+ VarDecl::Create(S.Context, S.CurContext, Loc, Loc, TmpName, C,
+ S.Context.getTrivialTypeSourceInfo(C, Loc), SC_None);
+ Expr *InitArgs[] = {Init.get()};
+ S.AddInitializerToDecl(Tmp,
+ ParenListExpr::Create(S.Context, Loc, InitArgs, Loc),
+ /*DirectInit=*/true);
+ if (Tmp->isInvalidDecl())
+ return StmtError();
+ StmtResult TmpStmt = S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Tmp), Loc, Loc);
+ if (TmpStmt.isInvalid())
+ return StmtError();
+
+ // ++c; or --c;
+ ExprResult Obj = BuildObject();
+ if (Obj.isInvalid())
+ return StmtError();
+ UnaryOperatorKind Opc =
+ Kind == PostfixOperatorKind::Increment ? UO_PreInc : UO_PreDec;
+ ExprResult Prefix;
+ if (Obj.get()->getType()->isOverloadableType()) {
+ UnresolvedSet<16> Fns;
+ if (auto *DFI = FD->getDefaultedOrDeletedInfo())
+ Fns.assign(DFI->getUnqualifiedLookups().begin(),
+ DFI->getUnqualifiedLookups().end());
+ Prefix = S.CreateOverloadedUnaryOp(Loc, Opc, Fns, Obj.get(),
+ /*RequiresADL=*/true);
+ } else {
+ Prefix = S.CreateBuiltinUnaryOp(Loc, Opc, Obj.get());
+ }
+ if (Prefix.isInvalid())
+ return StmtError();
+ StmtResult PrefixStmt = S.ActOnExprStmt(Prefix, /*DiscardedValue=*/true);
+ if (PrefixStmt.isInvalid())
+ return StmtError();
+
+ // return tmp;
+ ExprResult TmpRef = S.BuildDeclarationNameExpr(
+ CXXScopeSpec(), DeclarationNameInfo(TmpName, Loc), Tmp);
+ if (TmpRef.isInvalid())
+ return StmtError();
+ StmtResult Return = S.BuildReturnStmt(Loc, TmpRef.get());
+ if (Return.isInvalid())
+ return StmtError();
+ // 'tmp' is the only variable returned by this function, so it qualifies for
+ // the named return value optimization.
+ if (cast<ReturnStmt>(Return.get())->getNRVOCandidate() == Tmp)
+ Tmp->setNRVOVariable(true);
+
+ Stmt *Stmts[] = {TmpStmt.get(), PrefixStmt.get(), Return.get()};
+ return S.ActOnCompoundStmt(Loc, Loc, Stmts, /*IsStmtExpr=*/false);
+}
+
+bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
+ PostfixOperatorKind Kind) {
+ assert(Kind != PostfixOperatorKind::None &&
+ "not a defaulted postfix operator");
+ bool IsIncrement = Kind == PostfixOperatorKind::Increment;
+
+ // Perform the unqualified lookup of the prefix operator that the implicit
+ // definition needs. C++2d [over.inc.default]p1:
+ // Name lookups and access checks in the implicit definition of a defaulted
+ // postfix increment or decrement operator function are performed from a
+ // context equivalent to its function-body.
+ if (S) {
+ UnresolvedSet<16> Operators;
+ LookupOverloadedOperatorName(IsIncrement ? OO_PlusPlus : OO_MinusMinus, S,
+ Operators);
+ FD->setDefaultedOrDeletedInfo(
+ FunctionDecl::DefaultedOrDeletedFunctionInfo::Create(
+ Context, Operators.pairs(), CurFPFeatureOverrides()));
+ }
+
+ // C++2d [over.inc.default]p1:
+ // A defaulted postfix increment or decrement operator function for a type
+ // C shall be a non-template function that
+ // -- has a first parameter of type "reference to C" or a first parameter
+ // of type "reference to volatile C", where the implicit object
+ // parameter (if any) is considered to be the first parameter,
+ // -- is defined as defaulted in C or in a context where C is complete,
+ // and
+ // -- has a declared return type of C.
+ auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ ParmVarDecl *FirstParam = FD->getParamDecl(0);
+ QualType FirstParamType = getPostfixOperatorFirstParamType(FD);
+ QualType ReturnType = FD->getDeclaredReturnType();
+
+ // Determine the type C. For a member function, this is the enclosing class.
+ // For a non-member function, infer it from the first parameter, falling back
+ // to the return type if the first parameter is not a reference.
+ QualType C;
+ if (MD)
+ C = Context.getCanonicalTagType(MD->getParent());
+ else if (FirstParamType->isReferenceType())
+ C = FirstParamType.getNonReferenceType().getUnqualifiedType();
+ else if (!ReturnType->isReferenceType())
+ C = ReturnType.getUnqualifiedType();
+ if (C.isNull() || (!MD && !C->isDependentType() && !C->isRecordType() &&
+ !C->isEnumeralType())) {
+ Diag(FirstParam->getBeginLoc(),
+ diag::err_defaulted_postfix_operator_param_unknown)
+ << (int)Kind << FirstParamType << FirstParam->getSourceRange();
+ return true;
+ }
+
+ // Check the type of the first parameter.
+ if (!FirstParamType->isDependentType() && !C->isDependentType()) {
+ QualType Referent = FirstParamType->isReferenceType()
+ ? FirstParamType.getNonReferenceType()
+ : QualType();
+ if (Referent.isNull() || Referent.isConstQualified() ||
+ !Context.hasSameType(Referent.getUnqualifiedType(), C)) {
+ if (MD && MD->isImplicitObjectMemberFunction()) {
+ // The implicit object parameter is always a reference to the class,
+ // so the only possible problem is a const qualifier.
+ Diag(MD->getLocation(), diag::err_defaulted_postfix_operator_const)
+ << (int)Kind;
+ } else {
+ Diag(FirstParam->getBeginLoc(),
+ diag::err_defaulted_postfix_operator_param)
+ << (int)Kind << !MD << FirstParamType
+ << Context.getLValueReferenceType(C)
+ << Context.getLValueReferenceType(C.withVolatile())
+ << FirstParam->getSourceRange();
+ }
+ return true;
+ }
+ }
+
+ // Check the declared return type. A placeholder type is not permitted; this
+ // would need P2952R2 (auto& operator=(X&&) = default), which is not yet
+ // part of the standard.
+ if (ReturnType->getContainedDeducedType() ||
+ (!ReturnType->isDependentType() && !C->isDependentType() &&
+ !Context.hasSameType(ReturnType, C))) {
+ Diag(FD->getLocation(), diag::err_defaulted_postfix_operator_return_type)
+ << (int)Kind << C << ReturnType << FD->getReturnTypeSourceRange();
+ return true;
+ }
+
+ // For a defaulted function in a dependent context, defer all remaining
+ // checks until instantiation.
+ if (C->isDependentType() || FD->isDependentContext())
+ return false;
+
+ // -- is defined as defaulted in C or in a context where C is complete
+ if (RequireCompleteType(FD->getLocation(), C,
+ diag::err_defaulted_postfix_operator_incomplete,
+ (int)Kind))
+ return true;
+
+ // Determine whether the function should be defined as deleted.
+ DefaultedPostfixOperatorInfo Info = analyzeDefaultedPostfixOperator(
+ *this, FD, Kind, C, PostfixOperatorDiagnoseKind::NoDiagnostics);
+
+ bool First = FD == FD->getCanonicalDecl();
+
+ if (!First) {
+ if (Info.Deleted) {
+ // C++11 [dcl.fct.def.default]p4:
+ // [For a] user-provided explicitly-defaulted function [...] if such a
+ // function is implicitly defined as deleted, the program is ill-formed.
+ //
+ // This is really just a consequence of the general rule that you can
+ // only delete a function on its first declaration.
+ Diag(FD->getLocation(),
+ diag::err_non_first_default_postfix_operator_deletes)
+ << (int)Kind;
+ analyzeDefaultedPostfixOperator(
+ *this, FD, Kind, C, PostfixOperatorDiagnoseKind::ExplainDeleted);
+ return true;
+ }
+ if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
+ // C++2d [over.inc.default]p1:
+ // A definition of a postfix increment or decrement operator as
+ // defaulted that appears in a class shall be the first declaration of
+ // that function.
+ Diag(FD->getLocation(),
+ diag::err_non_first_default_postfix_operator_in_class)
+ << (int)Kind;
+ Diag(FD->getCanonicalDecl()->getLocation(),
+ diag::note_previous_declaration);
+ return true;
+ }
+ }
+
+ // If we want to delete the function, then do so; there's nothing else to
+ // check in that case.
+ if (Info.Deleted) {
+ SetDeclDeleted(FD, FD->getLocation());
+ if (!inTemplateInstantiation() && !FD->isImplicit()) {
+ Diag(FD->getLocation(), diag::warn_defaulted_postfix_operator_deleted)
+ << (int)Kind;
+ analyzeDefaultedPostfixOperator(
+ *this, FD, Kind, C, PostfixOperatorDiagnoseKind::ExplainDeleted);
+ if (FD->getDefaultLoc().isValid())
+ Diag(FD->getDefaultLoc(), diag::note_replace_equals_default_to_delete)
+ << FixItHint::CreateReplacement(FD->getDefaultLoc(), "delete");
+ }
+ return false;
+ }
+
+ // C++2a [dcl.fct.def.default]p3 [P2002R0]:
+ // An explicitly-defaulted function that is not defined as deleted may be
+ // declared constexpr or consteval only if it is constexpr-compatible.
+ // By analogy with C++2a [class.compare.default]p3, we consider a defaulted
+ // postfix operator to be constexpr-compatible if its parameter and return
+ // types are literal types and no overload resolution performed when
+ // determining whether to delete it results in a non-constexpr function.
+ //
+ // C++23 [dcl.fct.def.default]p3 [P2448R2] removed this restriction: a
+ // defaulted function may always be declared constexpr.
+ if (FD->isConstexpr() && !getLangOpts().CPlusPlus23 &&
+ !FD->isTemplateInstantiation() &&
+ CheckConstexprReturnType(*this, FD, CheckConstexprKind::Diagnose) &&
+ CheckConstexprParameterTypes(*this, FD, CheckConstexprKind::Diagnose) &&
+ !Info.Constexpr) {
+ Diag(FD->getBeginLoc(),
+ diag::err_defaulted_postfix_operator_constexpr_mismatch)
+ << (int)Kind << FD->isConsteval();
+ analyzeDefaultedPostfixOperator(
+ *this, FD, Kind, C, PostfixOperatorDiagnoseKind::ExplainConstexpr);
+ return true;
+ }
+
+ if (First) {
+ // C++11 [dcl.fct.def.default]p5:
+ // A function explicitly defaulted on its first declaration is implicitly
+ // inline.
+ FD->setImplicitlyInline();
+
+ // C++2a [dcl.fct.def.default]p3 [P2002R0]:
+ // If a constexpr-compatible function is explicitly defaulted on its
+ // first declaration, it is implicitly considered to be constexpr.
+ // C++23 [dcl.fct.def.default]p3 [P2448R2]:
+ // A function explicitly defaulted on its first declaration is [...]
+ // implicitly constexpr if it is constexpr-suitable.
+ // A postfix operator function is always constexpr-suitable. The implicit
+ // definition declares a variable, so it can only be constexpr in C++14
+ // and later, and a virtual function can only be constexpr in C++20 and
+ // later.
+ if (!FD->isConstexpr() && getLangOpts().CPlusPlus14 &&
+ (getLangOpts().CPlusPlus20 || !MD || !MD->isVirtual()) &&
+ (getLangOpts().CPlusPlus23 ||
+ (Info.Constexpr &&
+ CheckConstexprReturnType(*this, FD, CheckConstexprKind::CheckValid) &&
+ CheckConstexprParameterTypes(*this, FD,
+ CheckConstexprKind::CheckValid))))
+ FD->setConstexprKind(ConstexprSpecKind::Constexpr);
+
+ // C++2d [except.spec]p10:
+ // The exception specification for [...] a postfix increment or decrement
+ // operator function without a noexcept-specifier that is defaulted on
+ // its first declaration is potentially-throwing if and only if any
+ // expression in the implicit definition is potentially-throwing.
+ if (FD->getExceptionSpecType() == EST_None) {
+ auto *FPT = FD->getType()->castAs<FunctionProtoType>();
+ FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
+ EPI.ExceptionSpec.Type = EST_Unevaluated;
+ EPI.ExceptionSpec.SourceDecl = FD;
+ FD->setType(Context.getFunctionType(FPT->getReturnType(),
+ FPT->getParamTypes(), EPI));
+ }
+ }
+
+ return false;
+}
+
+void Sema::DefineDefaultedPostfixOperator(SourceLocation UseLoc,
+ FunctionDecl *FD,
+ PostfixOperatorKind Kind) {
+ assert(FD->isDefaulted() && !FD->isDeleted() &&
+ !FD->doesThisDeclarationHaveABody());
+ if (FD->willHaveBody() || FD->isInvalidDecl())
+ return;
+
+ SynthesizedFunctionScope Scope(*this, FD);
+
+ // Add a context note for diagnostics produced after this point.
+ Scope.addContextNote(UseLoc);
+
+ DefaultedFunctionFPFeaturesRAII RestoreFP(*this, FD);
+
+ {
+ // Build and set up the function body.
+ SourceLocation BodyLoc =
+ FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
+ StmtResult Body =
+ buildDefaultedPostfixOperatorBody(*this, FD, Kind, BodyLoc);
+ if (Body.isInvalid()) {
+ FD->setInvalidDecl();
+ return;
+ }
+ FD->setBody(Body.get());
+ FD->markUsed(Context);
+ }
+
+ // The exception specification is needed because we are defining the
+ // function. Note that this will reuse the body we just built.
+ ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
+
+ if (ASTMutationListener *L = getASTMutationListener())
+ L->CompletedImplicitDefinition(FD);
+}
+
+static Sema::ImplicitExceptionSpecification
+ComputeDefaultedPostfixOperatorExceptionSpec(Sema &S, SourceLocation Loc,
+ FunctionDecl *FD,
+ PostfixOperatorKind Kind) {
+ ComputingExceptionSpec CES(S, FD, Loc);
+ Sema::ImplicitExceptionSpecification ExceptSpec(S);
+
+ if (FD->isInvalidDecl())
+ return ExceptSpec;
+
+ // The common case is that we just defined the function. In that case, just
+ // look at whether the body can throw.
+ if (Stmt *FunctionBody = FD->getBody()) {
+ ExceptSpec.CalledStmt(FunctionBody);
+ } else {
+ // Otherwise, build a body so we can check it. Pretend to synthesize the
+ // function body in an unevaluated context: we are not permitted to mark
+ // its callees as referenced.
+ Sema::SynthesizedFunctionScope Scope(S, FD);
+ EnterExpressionEvaluationContext Context(
+ S, Sema::ExpressionEvaluationContext::Unevaluated);
+
+ SourceLocation BodyLoc =
+ FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
+ StmtResult Body = buildDefaultedPostfixOperatorBody(S, FD, Kind, BodyLoc);
+ if (!Body.isInvalid())
+ ExceptSpec.CalledStmt(Body.get());
+ }
+
+ // The implicit definition also destroys the temporary copy.
+ if (CXXRecordDecl *RD = FD->getReturnType()->getAsCXXRecordDecl())
+ if (CXXDestructorDecl *Dtor = S.LookupDestructor(RD))
+ ExceptSpec.CalledDecl(Loc, Dtor);
+
+ return ExceptSpec;
+}
+
void Sema::CheckDelayedMemberExceptionSpecs() {
decltype(DelayedOverridingExceptionSpecChecks) Overriding;
decltype(DelayedEquivalentExceptionSpecChecks) Equivalent;
@@ -10160,6 +10792,10 @@ void Sema::DiagnoseDeletedDefaultedFunction(FunctionDecl *FD) {
if (DFK.isSpecialMember()) {
ShouldDeleteSpecialMember(cast<CXXMethodDecl>(FD), DFK.asSpecialMember(),
nullptr, /*Diagnose=*/true);
+ } else if (DFK.isPostfixOperator()) {
+ analyzeDefaultedPostfixOperator(
+ *this, FD, DFK.asPostfixOperator(), getDefaultedPostfixOperatorType(FD),
+ PostfixOperatorDiagnoseKind::ExplainDeleted);
} else {
DefaultedComparisonAnalyzer(
*this, cast<CXXRecordDecl>(FD->getLexicalDeclContext()), FD,
@@ -18952,21 +19588,35 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc,
Fn->setDeletedAsWritten(true, Message);
}
-void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
+void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) {
if (!Dcl || Dcl->isInvalidDecl())
return;
+ // The %select index for err_default_special_members: which kinds of
+ // functions can be defaulted in the current language mode.
+ unsigned DefaultableKinds = 0;
+ if (getLangOpts().CPlusPlus29)
+ DefaultableKinds = 2;
+ else if (getLangOpts().CPlusPlus20)
+ DefaultableKinds = 1;
+
auto *FD = dyn_cast<FunctionDecl>(Dcl);
if (!FD) {
if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Dcl)) {
- if (FTD->getTemplatedDecl()->getDefaultedFunctionKind().isComparison()) {
+ FunctionDecl::DefaultedFunctionKind DFK =
+ FTD->getTemplatedDecl()->getDefaultedFunctionKind();
+ if (DFK.isComparison()) {
Diag(DefaultLoc, diag::err_defaulted_comparison_template);
return;
}
+ if (DFK.isPostfixOperator()) {
+ Diag(DefaultLoc, diag::err_defaulted_postfix_operator_template)
+ << (int)DFK.asPostfixOperator();
+ return;
+ }
}
- Diag(DefaultLoc, diag::err_default_special_members)
- << getLangOpts().CPlusPlus20;
+ Diag(DefaultLoc, diag::err_default_special_members) << DefaultableKinds;
return;
}
@@ -18979,8 +19629,12 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
(!FD->isDependentContext() ||
(!isa<CXXConstructorDecl>(FD) &&
FD->getDeclName().getCXXOverloadedOperator() != OO_Equal))) {
- Diag(DefaultLoc, diag::err_default_special_members)
- << getLangOpts().CPlusPlus20;
+ OverloadedOperatorKind OO = FD->getDeclName().getCXXOverloadedOperator();
+ if (OO == OO_PlusPlus || OO == OO_MinusMinus)
+ Diag(DefaultLoc, diag::err_defaulted_prefix_operator)
+ << (OO == OO_MinusMinus);
+ else
+ Diag(DefaultLoc, diag::err_default_special_members) << DefaultableKinds;
return;
}
@@ -18990,6 +19644,10 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
DefKind.asComparison() != DefaultedComparisonKind::ThreeWay) {
DiagCompat(DefaultLoc, diag_compat::defaulted_comparison);
}
+ // Defaulted postfix increment and decrement operators (P3668R4) are a C++2d
+ // feature that we support in all C++ language modes as an extension.
+ if (DefKind.isPostfixOperator() && !inTemplateInstantiation())
+ DiagCompat(DefaultLoc, diag_compat::defaulted_postfix_operator);
FD->setDefaulted();
FD->setExplicitlyDefaulted();
@@ -19004,8 +19662,8 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
// that we've marked it as defaulted.
FD->setWillHaveBody(false);
- if (DefKind.isComparison()) {
- // If this comparison's defaulting occurs within the definition of its
+ if (DefKind.isComparison() || DefKind.isPostfixOperator()) {
+ // If this function's defaulting occurs within the definition of its
// lexical class context, we have to do the checking when complete.
if (auto const *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalDeclContext()))
if (!RD->isCompleteDefinition())
@@ -19040,6 +19698,13 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
FD->setInvalidDecl();
else
DefineDefaultedComparison(DefaultLoc, FD, DefKind.asComparison());
+ } else if (DefKind.isPostfixOperator()) {
+ if (CheckExplicitlyDefaultedPostfixOperator(S, FD,
+ DefKind.asPostfixOperator()))
+ FD->setInvalidDecl();
+ else if (!FD->isDeleted())
+ DefineDefaultedPostfixOperator(DefaultLoc, FD,
+ DefKind.asPostfixOperator());
} else {
auto *MD = cast<CXXMethodDecl>(FD);
@@ -19071,13 +19736,13 @@ void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) {
}
void Sema::SetFunctionBodyKind(Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
- StringLiteral *DeletedMessage) {
+ StringLiteral *DeletedMessage, Scope *S) {
switch (BodyKind) {
case FnBodyKind::Delete:
SetDeclDeleted(D, Loc, DeletedMessage);
break;
case FnBodyKind::Default:
- SetDeclDefaulted(D, Loc);
+ SetDeclDefaulted(D, Loc, S);
break;
case FnBodyKind::Other:
llvm_unreachable(
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 3258abbc4d24f0..0eac93e34d1ba4 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19321,9 +19321,12 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
}
if (Func->isDefaulted() && !Func->isDeleted()) {
- DefaultedComparisonKind DCK = Func->getDefaultedComparisonKind();
- if (DCK != DefaultedComparisonKind::None)
- DefineDefaultedComparison(Loc, Func, DCK);
+ FunctionDecl::DefaultedFunctionKind DFK =
+ Func->getDefaultedFunctionKind();
+ if (DFK.isComparison())
+ DefineDefaultedComparison(Loc, Func, DFK.asComparison());
+ else if (DFK.isPostfixOperator())
+ DefineDefaultedPostfixOperator(Loc, Func, DFK.asPostfixOperator());
}
// Implicit instantiation of function templates and member functions of
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index c19a47c35639c5..819e8566c67818 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -15502,6 +15502,19 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc,
return ExprError();
case OR_Deleted: {
+ // A defaulted postfix increment or decrement operator that was implicitly
+ // defined as deleted. Explain why it was deleted.
+ if (FunctionDecl *DeletedFD = Best->Function;
+ DeletedFD->isDefaulted() &&
+ DeletedFD->getDefaultedFunctionKind().isPostfixOperator()) {
+ Diag(OpLoc, diag::err_ovl_deleted_postfix_operator)
+ << Input->getType()
+ << (int)DeletedFD->getDefaultedFunctionKind().asPostfixOperator()
+ << Input->getSourceRange();
+ NoteDeletedFunction(DeletedFD);
+ return ExprError();
+ }
+
// CreateOverloadedUnaryOp fills the first element of ArgsArray with the
// object whose method was called. Later in NoteCandidates size of ArgsArray
// is passed further and it eventually ends up compared to number of
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 06ea12ca7bd73b..fb51c8a95c40da 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1168,6 +1168,13 @@ void Sema::PrintInstantiationStack(InstantiationContextDiagFuncRef DiagFunc) {
DiagFunc(Active->PointOfInstantiation,
PDiag(diag::note_comparison_synthesized_at)
<< (int)DFK.asComparison() << RecordType);
+ } else if (DFK.isPostfixOperator()) {
+ // The declared return type of a defaulted postfix operator is the
+ // type for which it is defaulted.
+ DiagFunc(Active->PointOfInstantiation,
+ PDiag(diag::note_postfix_operator_synthesized_at)
+ << (int)DFK.asPostfixOperator()
+ << FD->getReturnType().getUnqualifiedType());
}
break;
}
diff --git a/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp b/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
new file mode 100644
index 00000000000000..3b9bec55202137
--- /dev/null
+++ b/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
@@ -0,0 +1,92 @@
+// RUN: %clang_cc1 -std=c++2d -ast-dump %s | FileCheck %s
+
+// The implicit definition of a defaulted postfix increment or decrement
+// operator for a type C is equivalent to:
+// C tmp(c);
+// ++c;
+// return tmp;
+
+struct S {
+ int v;
+ S &operator++();
+ S operator++(int) = default;
+};
+void use_member(S s) { s++; }
+
+// CHECK: CXXMethodDecl {{.*}} used constexpr operator++ 'S (int) noexcept(false)' default implicit-inline
+// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
+// CHECK-NEXT: CompoundStmt
+// CHECK-NEXT: DeclStmt
+// CHECK-NEXT: VarDecl {{.*}} used tmp 'S' nrvo callinit
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'S' 'void (const S &) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const S' lvalue <NoOp>
+// CHECK-NEXT: UnaryOperator {{.*}} 'S' lvalue prefix '*' cannot overflow
+// CHECK-NEXT: CXXThisExpr {{.*}} 'S *' this
+// CHECK-NEXT: CXXOperatorCallExpr {{.*}} 'S' lvalue '++'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'S &(*)()' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'S &()' lvalue CXXMethod {{.*}} 'operator++' 'S &()'
+// CHECK-NEXT: UnaryOperator {{.*}} 'S' lvalue prefix '*' cannot overflow
+// CHECK-NEXT: CXXThisExpr {{.*}} 'S *' this
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'S')
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'S' 'void (S &&) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'S' xvalue <NoOp>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'S' lvalue Var {{.*}} 'tmp' 'S'
+
+struct T {
+ int v;
+ T &operator--();
+};
+T operator--(T &self, int) = default;
+void use_non_member(T t) { t--; }
+
+// CHECK: FunctionDecl {{.*}} used constexpr operator-- 'T (T &, int) noexcept(false)' default implicit-inline
+// CHECK-NEXT: ParmVarDecl {{.*}} used self 'T &'
+// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
+// CHECK-NEXT: CompoundStmt
+// CHECK-NEXT: DeclStmt
+// CHECK-NEXT: VarDecl {{.*}} used tmp 'T' nrvo callinit
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'T' 'void (const T &) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const T' lvalue <NoOp>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 'self' 'T &'
+// CHECK-NEXT: CXXOperatorCallExpr {{.*}} 'T' lvalue '--'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T &(*)()' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'T &()' lvalue CXXMethod {{.*}} 'operator--' 'T &()'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 'self' 'T &'
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'T')
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'T' 'void (T &&) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T' xvalue <NoOp>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue Var {{.*}} 'tmp' 'T'
+
+struct U {
+ int v;
+ U &operator++();
+ U operator++(this U &self, int) = default;
+};
+void use_explicit_object(U u) { u++; }
+
+// CHECK: CXXMethodDecl {{.*}} used constexpr operator++ 'U (U &, int) noexcept(false)' default implicit-inline
+// CHECK-NEXT: ParmVarDecl {{.*}} used self this 'U &'
+// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
+// CHECK-NEXT: CompoundStmt
+// CHECK-NEXT: DeclStmt
+// CHECK-NEXT: VarDecl {{.*}} used tmp 'U' nrvo callinit
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'U' 'void (const U &) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const U' lvalue <NoOp>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue ParmVar {{.*}} 'self' 'U &'
+// CHECK-NEXT: CXXOperatorCallExpr {{.*}} 'U' lvalue '++'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'U &(*)()' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'U &()' lvalue CXXMethod {{.*}} 'operator++' 'U &()'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue ParmVar {{.*}} 'self' 'U &'
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'U')
+// CHECK-NEXT: CXXConstructExpr {{.*}} 'U' 'void (U &&) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'U' xvalue <NoOp>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue Var {{.*}} 'tmp' 'U'
+
+// A deleted defaulted postfix operator has no body.
+struct Deleted {
+ Deleted operator++(int) = default;
+};
+// CHECK: CXXRecordDecl {{.*}} struct Deleted definition
+// CHECK: CXXMethodDecl {{.*}} operator++ 'Deleted (int)' default_delete
+// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
+// CHECK-NOT: CompoundStmt
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
new file mode 100644
index 00000000000000..86a2620fbd7944
--- /dev/null
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
@@ -0,0 +1,205 @@
+// RUN: %clang_cc1 -std=c++2d -verify %s
+
+// C++2d [over.inc.default]p1:
+// A defaulted postfix increment or decrement operator function for a type C
+// shall be a non-template function that
+// -- has a first parameter of type "reference to C" or a first parameter of
+// type "reference to volatile C", where the implicit object parameter
+// (if any) is considered to be the first parameter,
+// -- is defined as defaulted in C or in a context where C is complete, and
+// -- has a declared return type of C.
+// [...] A definition of a postfix increment or decrement operator as
+// defaulted that appears in a class shall be the first declaration of that
+// function.
+
+struct S {
+ S &operator++();
+ S &operator--();
+
+ S operator++(int) = default;
+ S operator--(int) = default;
+};
+
+// The implicit object parameter is the first parameter.
+struct Const {
+ Const &operator++();
+ Const operator++(int) const = default; // expected-error {{defaulted member postfix increment operator cannot be const-qualified}}
+};
+struct ConstDecrement {
+ ConstDecrement &operator--();
+ ConstDecrement operator--(int) const = default; // expected-error {{defaulted member postfix decrement operator cannot be const-qualified}}
+};
+struct Volatile {
+ Volatile &operator++() volatile;
+ Volatile operator++(int) volatile = default; // OK, "reference to volatile C"
+};
+struct RefQualified {
+ RefQualified &operator++();
+ RefQualified operator++(int) & = default; // OK
+ RefQualified operator++(int) && = default; // OK, an rvalue reference is a "reference to C"
+};
+
+// Explicit object parameters.
+struct Explicit {
+ Explicit &operator++();
+ Explicit operator++(this Explicit &, int) = default; // OK
+};
+struct ExplicitVolatile {
+ ExplicitVolatile &operator++() volatile;
+ ExplicitVolatile operator++(this volatile ExplicitVolatile &, int) = default; // OK
+};
+struct ExplicitRvalue {
+ ExplicitRvalue &operator++();
+ ExplicitRvalue operator++(this ExplicitRvalue &&, int) = default; // OK
+};
+struct ExplicitConst {
+ ExplicitConst &operator++();
+ ExplicitConst operator++(this const ExplicitConst &, int) = default; // expected-error {{invalid explicit object parameter type for defaulted postfix increment operator; found 'const ExplicitConst &', expected 'ExplicitConst &' or 'volatile ExplicitConst &'}}
+};
+struct ExplicitOther {
+ ExplicitOther &operator++();
+ ExplicitOther operator++(this S &, int) = default; // expected-error {{invalid explicit object parameter type for defaulted postfix increment operator; found 'S &', expected 'ExplicitOther &' or 'volatile ExplicitOther &'}}
+};
+struct ExplicitByValue {
+ ExplicitByValue &operator--();
+ ExplicitByValue operator--(this ExplicitByValue, int) = default; // expected-error {{invalid explicit object parameter type for defaulted postfix decrement operator; found 'ExplicitByValue', expected 'ExplicitByValue &' or 'volatile ExplicitByValue &'}}
+};
+
+// Return type.
+struct ReturnsRef {
+ ReturnsRef &operator++();
+ ReturnsRef &operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'ReturnsRef', not 'ReturnsRef &'}}
+};
+struct ReturnsConst {
+ ReturnsConst &operator++();
+ const ReturnsConst operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'ReturnsConst', not 'const ReturnsConst'}}
+};
+struct ReturnsAuto {
+ ReturnsAuto &operator++();
+ auto operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'ReturnsAuto', not 'auto'}}
+};
+struct ReturnsDecltypeAuto {
+ ReturnsDecltypeAuto &operator--();
+ decltype(auto) operator--(int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'ReturnsDecltypeAuto', not 'decltype(auto)'}}
+};
+struct ReturnsInt {
+ ReturnsInt &operator++();
+ int operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'ReturnsInt', not 'int'}}
+};
+struct ReturnsAlias {
+ using Self = ReturnsAlias;
+ Self &operator++();
+ Self operator++(int) = default; // OK
+};
+
+// Non-member functions.
+struct N {
+ N &operator++();
+ N &operator--();
+};
+N operator++(N &, int) = default; // OK
+N operator--(N &&, int) = default; // OK, an rvalue reference is a "reference to C"
+
+struct NV {
+ NV &operator++() volatile;
+ NV(const volatile NV &);
+ NV(NV &&);
+};
+NV operator++(volatile NV &, int) = default; // OK
+
+struct NConst { NConst &operator++(); };
+NConst operator++(const NConst &, int) = default; // expected-error {{invalid first parameter type for defaulted postfix increment operator; found 'const NConst &', expected 'NConst &' or 'volatile NConst &'}}
+struct NValue { NValue &operator++(); };
+NValue operator++(NValue, int) = default; // expected-error {{invalid first parameter type for defaulted postfix increment operator; found 'NValue', expected 'NValue &' or 'volatile NValue &'}}
+struct NRet { NRet &operator--(); };
+int operator--(NRet &, int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'NRet', not 'int'}}
+struct NMismatch { NMismatch &operator++(); };
+S operator++(NMismatch &, int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'NMismatch', not 'S'}}
+struct NUnknown { NUnknown &operator++(); };
+S &operator++(NUnknown, int) = default; // expected-error {{invalid first parameter type for defaulted non-member postfix increment operator; found 'NUnknown', expected reference to a non-const class or enumeration type}}
+
+// Enumerations.
+enum E { e };
+E &operator++(E &);
+E operator++(E &, int) = default; // OK
+enum class EC { ec };
+EC operator++(EC &, int) = default; // OK, defined as deleted (see p2.cpp)
+// expected-warning at -1 {{explicitly defaulted postfix increment operator is implicitly deleted}}
+// expected-note at -2 {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'EC'}}
+// expected-note at -3 {{replace 'default' with 'delete'}}
+
+// C must be complete.
+struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}
+Incomplete operator++(Incomplete &, int) = default; // expected-error {{incomplete result type 'Incomplete' in function definition}}
+struct IncompleteFriend; // expected-note {{forward declaration of 'IncompleteFriend'}}
+struct Befriender {
+ friend IncompleteFriend operator++(IncompleteFriend &, int) = default; // expected-error {{cannot default postfix increment operator for incomplete type 'IncompleteFriend'}}
+};
+struct DefinedLater {
+ friend DefinedLater operator++(DefinedLater &, int) = default; // OK, defaulted in C
+ DefinedLater &operator++();
+};
+
+// A definition as defaulted that appears in a class shall be the first
+// declaration of that function.
+struct First;
+First operator++(First &, int); // expected-note {{previous declaration is here}}
+struct First {
+ First &operator++();
+ friend First operator++(First &, int) = default; // expected-error {{defaulting this postfix increment operator is not allowed because it was already declared outside the class}}
+};
+struct OutOfLine {
+ OutOfLine &operator++();
+ OutOfLine operator++(int);
+};
+OutOfLine OutOfLine::operator++(int) = default; // OK
+struct OutOfLineFriend {
+ OutOfLineFriend &operator--();
+ friend OutOfLineFriend operator--(OutOfLineFriend &, int);
+};
+OutOfLineFriend operator--(OutOfLineFriend &, int) = default; // OK
+struct Redefined {
+ Redefined &operator++();
+ Redefined operator++(int) = default;
+};
+Redefined Redefined::operator++(int) { return *this; } // expected-error {{definition of explicitly defaulted function}}
+
+// Templates cannot be defaulted.
+struct Template {
+ Template &operator++();
+ template <typename T = void> Template operator++(int) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+};
+template <typename T = void> S operator--(S &, int) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+
+// Only the postfix forms can be defaulted.
+struct Prefix {
+ Prefix &operator++() = default; // expected-error {{only the postfix form of 'operator++' can be defaulted}}
+ Prefix &operator--() = default; // expected-error {{only the postfix form of 'operator--' can be defaulted}}
+};
+struct PrefixNonMember {};
+PrefixNonMember &operator++(PrefixNonMember &) = default; // expected-error {{only the postfix form of 'operator++' can be defaulted}}
+
+// Other operators still cannot be defaulted.
+struct Other {
+ Other operator+(int) = default; // expected-error {{only special member functions, comparison operators, and postfix increment and decrement operators may be defaulted}}
+};
+
+// Dependent declarations are checked when instantiated.
+template <typename T> struct DependentReturn {
+ DependentReturn &operator++();
+ T operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'DependentReturn<int>', not 'int'}}
+};
+DependentReturn<int> dr1; // expected-note {{in instantiation of template class 'DependentReturn<int>' requested here}}
+
+template <typename T> struct DependentObject {
+ DependentObject &operator++();
+ DependentObject operator++(this T &, int) = default; // expected-error {{invalid explicit object parameter type for defaulted postfix increment operator; found 'int &', expected 'DependentObject<int> &' or 'volatile DependentObject<int> &'}}
+};
+DependentObject<int> do1; // expected-note {{in instantiation of template class 'DependentObject<int>' requested here}}
+
+template <typename T> struct DependentFriend {
+ DependentFriend &operator++();
+ friend DependentFriend operator++(DependentFriend &, int) = default; // OK
+ friend int operator--(T &, int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'S', not 'int'}}
+};
+DependentFriend<S> df1; // expected-note {{in instantiation of template class 'DependentFriend<S>' requested here}}
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p2.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p2.cpp
new file mode 100644
index 00000000000000..b7794fc8d064ff
--- /dev/null
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p2.cpp
@@ -0,0 +1,192 @@
+// RUN: %clang_cc1 -std=c++2d -verify %s
+
+// C++2d [over.inc.default]p2:
+// A defaulted postfix increment or decrement operator function for a type C
+// is defined as deleted if
+// -- C is a class type for which overload resolution, as applied to
+// direct-initialization of a variable of type C from an lvalue of type
+// C, does not result in a usable candidate,
+// -- C has a destructor that is deleted or inaccessible from the context of
+// the function-body, or
+// -- for an lvalue c of type C, overload resolution as applied to ++c for a
+// postfix increment operator function or --c for a postfix decrement
+// operator function does not result in a usable candidate.
+
+struct NoPrefix {
+ NoPrefix operator++(int) = default; // #NoPrefix
+ // expected-warning@#NoPrefix {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#NoPrefix 2 {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'NoPrefix'}}
+ // expected-note@#NoPrefix {{replace 'default' with 'delete'}}
+ // expected-note@#NoPrefix {{explicitly defaulted function was implicitly deleted here}}
+};
+void use_no_prefix(NoPrefix n) {
+ n++; // expected-error {{object of type 'NoPrefix' cannot be incremented because its defaulted postfix increment operator is implicitly deleted}}
+}
+
+struct NoPrefixDecrement {
+ NoPrefixDecrement &operator++();
+ NoPrefixDecrement operator--(int) = default; // #NoPrefixDecrement
+ // expected-warning@#NoPrefixDecrement {{explicitly defaulted postfix decrement operator is implicitly deleted}}
+ // expected-note@#NoPrefixDecrement 2 {{defaulted 'operator--' is implicitly deleted because there is no viable prefix 'operator--' for an lvalue of type 'NoPrefixDecrement'}}
+ // expected-note@#NoPrefixDecrement {{replace 'default' with 'delete'}}
+ // expected-note@#NoPrefixDecrement {{explicitly defaulted function was implicitly deleted here}}
+};
+void use_no_prefix_decrement(NoPrefixDecrement n) {
+ n--; // expected-error {{object of type 'NoPrefixDecrement' cannot be decremented because its defaulted postfix decrement operator is implicitly deleted}}
+}
+
+// There is no built-in increment operator for enumeration types.
+enum class E {};
+E operator++(E &, int) = default; // #E
+// expected-warning@#E {{explicitly defaulted postfix increment operator is implicitly deleted}}
+// expected-note@#E 2 {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'E'}}
+// expected-note@#E {{replace 'default' with 'delete'}}
+// expected-note@#E {{explicitly defaulted function was implicitly deleted here}}
+void use_e(E e) {
+ e++; // expected-error {{object of type 'E' cannot be incremented because its defaulted postfix increment operator is implicitly deleted}}
+}
+
+// A conversion to a built-in type provides a usable candidate.
+struct ConvertsToInt {
+ operator int &();
+ ConvertsToInt operator++(int) = default; // OK
+};
+struct AmbiguousBuiltin {
+ operator int &();
+ operator long &();
+ AmbiguousBuiltin operator++(int) = default; // #AmbiguousBuiltin
+ // expected-warning@#AmbiguousBuiltin {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#AmbiguousBuiltin {{defaulted 'operator++' is implicitly deleted because prefix 'operator++' for an lvalue of type 'AmbiguousBuiltin' is ambiguous}}
+ // expected-note@#AmbiguousBuiltin {{built-in candidate operator++(int &)}}
+ // expected-note@#AmbiguousBuiltin {{built-in candidate operator++(long &)}}
+ // expected-note@#AmbiguousBuiltin {{replace 'default' with 'delete'}}
+};
+
+struct DeletedPrefix {
+ DeletedPrefix &operator++() = delete; // expected-note {{'operator++' has been explicitly marked deleted here}}
+ DeletedPrefix operator++(int) = default; // #DeletedPrefix
+ // expected-warning@#DeletedPrefix {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#DeletedPrefix {{defaulted 'operator++' is implicitly deleted because it would invoke a deleted prefix 'operator++'}}
+ // expected-note@#DeletedPrefix {{replace 'default' with 'delete'}}
+};
+
+struct RvaluePrefix {
+ RvaluePrefix &operator++() &&;
+ RvaluePrefix operator++(int) = default; // #RvaluePrefix
+ // expected-warning@#RvaluePrefix {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#RvaluePrefix {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'RvaluePrefix'}}
+ // expected-note@#RvaluePrefix {{replace 'default' with 'delete'}}
+};
+
+class PrivatePrefix {
+ PrivatePrefix &operator++(); // expected-note {{declared private here}}
+
+public:
+ PrivatePrefix operator--(int) = default; // #PrivatePrefixDecrement
+ // expected-warning@#PrivatePrefixDecrement {{explicitly defaulted postfix decrement operator is implicitly deleted}}
+ // expected-note@#PrivatePrefixDecrement {{defaulted 'operator--' is implicitly deleted because there is no viable prefix 'operator--' for an lvalue of type 'PrivatePrefix'}}
+ // expected-note@#PrivatePrefixDecrement {{replace 'default' with 'delete'}}
+};
+PrivatePrefix operator++(PrivatePrefix &, int) = default; // #PrivatePrefix
+// expected-warning@#PrivatePrefix {{explicitly defaulted postfix increment operator is implicitly deleted}}
+// expected-note@#PrivatePrefix {{defaulted 'operator++' is implicitly deleted because it would invoke a private prefix 'operator++' of 'PrivatePrefix'}}
+// expected-note@#PrivatePrefix {{replace 'default' with 'delete'}}
+struct FriendOfPrivatePrefix {
+ // A friend has access to the prefix operator.
+ friend PrivatePrefix operator--(PrivatePrefix &, int) = default; // #FriendOfPrivatePrefix
+ // expected-warning@#FriendOfPrivatePrefix {{explicitly defaulted postfix decrement operator is implicitly deleted}}
+ // expected-note@#FriendOfPrivatePrefix {{defaulted 'operator--' is implicitly deleted because there is no viable prefix 'operator--' for an lvalue of type 'PrivatePrefix'}}
+ // expected-note@#FriendOfPrivatePrefix {{replace 'default' with 'delete'}}
+};
+
+struct DeletedCopy {
+ DeletedCopy &operator++();
+ DeletedCopy(const DeletedCopy &) = delete; // expected-note {{'DeletedCopy' has been explicitly marked deleted here}}
+ DeletedCopy operator++(int) = default; // #DeletedCopy
+ // expected-warning@#DeletedCopy {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#DeletedCopy {{defaulted 'operator++' is implicitly deleted because it would invoke a deleted constructor}}
+ // expected-note@#DeletedCopy {{replace 'default' with 'delete'}}
+};
+
+struct MoveOnly {
+ MoveOnly &operator++();
+ MoveOnly(MoveOnly &&); // expected-note {{copy constructor is implicitly deleted because 'MoveOnly' has a user-declared move constructor}}
+ MoveOnly operator++(int) = default; // #MoveOnly
+ // expected-warning@#MoveOnly {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#MoveOnly {{defaulted 'operator++' is implicitly deleted because it would invoke a deleted constructor}}
+ // expected-note@#MoveOnly {{replace 'default' with 'delete'}}
+};
+
+struct AmbiguousCopy {
+ AmbiguousCopy &operator++();
+ AmbiguousCopy(AmbiguousCopy &, int = 0); // expected-note {{candidate constructor}}
+ AmbiguousCopy(AmbiguousCopy &, long = 0); // expected-note {{candidate constructor}}
+ AmbiguousCopy operator++(int) = default; // #AmbiguousCopy
+ // expected-warning@#AmbiguousCopy {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#AmbiguousCopy {{defaulted 'operator++' is implicitly deleted because copy construction of an lvalue of type 'AmbiguousCopy' is ambiguous}}
+ // expected-note@#AmbiguousCopy {{replace 'default' with 'delete'}}
+};
+
+class PrivateCopy {
+ PrivateCopy(const PrivateCopy &); // expected-note {{declared private here}}
+
+public:
+ PrivateCopy &operator++();
+ PrivateCopy &operator--();
+ PrivateCopy operator++(int) = default; // OK, a member has access to the copy constructor
+};
+PrivateCopy operator--(PrivateCopy &, int) = default; // #PrivateCopy
+// expected-warning@#PrivateCopy {{explicitly defaulted postfix decrement operator is implicitly deleted}}
+// expected-note@#PrivateCopy {{defaulted 'operator--' is implicitly deleted because it would invoke a private constructor of 'PrivateCopy'}}
+// expected-note@#PrivateCopy {{replace 'default' with 'delete'}}
+
+struct DeletedDestructor {
+ DeletedDestructor &operator++();
+ ~DeletedDestructor() = delete; // expected-note {{'~DeletedDestructor' has been explicitly marked deleted here}}
+ DeletedDestructor operator++(int) = default; // #DeletedDestructor
+ // expected-warning@#DeletedDestructor {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#DeletedDestructor {{defaulted 'operator++' is implicitly deleted because it would invoke a deleted destructor}}
+ // expected-note@#DeletedDestructor {{replace 'default' with 'delete'}}
+};
+
+class PrivateDestructor {
+ ~PrivateDestructor(); // expected-note {{declared private here}}
+
+public:
+ PrivateDestructor &operator++();
+ PrivateDestructor(const PrivateDestructor &);
+ PrivateDestructor operator++(int) = default; // OK, a member has access to the destructor
+};
+PrivateDestructor operator++(PrivateDestructor &, int) = default; // #PrivateDestructor
+// expected-warning@#PrivateDestructor {{explicitly defaulted postfix increment operator is implicitly deleted}}
+// expected-note@#PrivateDestructor {{defaulted 'operator++' is implicitly deleted because it would invoke a private destructor of 'PrivateDestructor'}}
+// expected-note@#PrivateDestructor {{replace 'default' with 'delete'}}
+
+// A function that is not defaulted on its first declaration cannot be deleted.
+struct NotFirst {
+ NotFirst &operator++();
+ NotFirst(const NotFirst &) = delete; // expected-note {{'NotFirst' has been explicitly marked deleted here}}
+ NotFirst operator++(int);
+};
+NotFirst NotFirst::operator++(int) = default; // expected-error {{defaulting this postfix increment operator would delete it after its first declaration}}
+// expected-note at -1 {{defaulted 'operator++' is implicitly deleted because it would invoke a deleted constructor}}
+
+// No warning is produced for a template instantiation, but the function is
+// still deleted.
+template <typename T> struct Wrapper {
+ T v;
+ Wrapper operator++(int) = default; // #Wrapper
+ // expected-note@#Wrapper {{explicitly defaulted function was implicitly deleted here}}
+ // expected-note@#Wrapper {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'Wrapper<int>'}}
+};
+void use_wrapper(Wrapper<int> w) {
+ w++; // expected-error {{object of type 'Wrapper<int>' cannot be incremented because its defaulted postfix increment operator is implicitly deleted}}
+}
+
+// -Wdefaulted-function-deleted can be disabled.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdefaulted-function-deleted"
+struct Quiet {
+ Quiet operator++(int) = default;
+};
+#pragma clang diagnostic pop
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
new file mode 100644
index 00000000000000..2f8d1d9788c672
--- /dev/null
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
@@ -0,0 +1,263 @@
+// RUN: %clang_cc1 -std=c++2d -verify %s
+
+// C++2d [over.inc.default]p3:
+// The implicit definition of a defaulted postfix increment or decrement
+// operator function F that is not defined as deleted for a type C is
+// equivalent to:
+// C tmp(c);
+// ++c;
+// return tmp;
+// for a postfix increment operator function, or
+// C tmp(c);
+// --c;
+// return tmp;
+// for a postfix decrement operator function, where tmp is a variable defined
+// for exposition only, and c is an lvalue that denotes *this if F is an
+// implicit object member function, or the first parameter of F otherwise.
+
+struct Counter {
+ int v;
+ constexpr Counter &operator++() { ++v; return *this; }
+ constexpr Counter &operator--() { --v; return *this; }
+ Counter operator++(int) = default;
+ Counter operator--(int) = default;
+};
+constexpr int counter() {
+ Counter c{1};
+ Counter a = c++;
+ Counter b = c--;
+ return a.v * 100 + b.v * 10 + c.v;
+}
+static_assert(counter() == 121);
+
+struct NonMember {
+ int v;
+ constexpr NonMember &operator++() { ++v; return *this; }
+};
+constexpr NonMember operator++(NonMember &, int) = default;
+constexpr int non_member() {
+ NonMember n{1};
+ NonMember a = n++;
+ return a.v * 10 + n.v;
+}
+static_assert(non_member() == 12);
+
+struct ExplicitObject {
+ int v;
+ constexpr ExplicitObject &operator++() { ++v; return *this; }
+ ExplicitObject operator++(this ExplicitObject &self, int) = default;
+};
+constexpr int explicit_object() {
+ ExplicitObject e{1};
+ ExplicitObject a = e++;
+ return a.v * 10 + e.v;
+}
+static_assert(explicit_object() == 12);
+
+struct RvalueObject {
+ int v;
+ constexpr RvalueObject &operator++() { ++v; return *this; }
+ RvalueObject operator++(int) && = default;
+};
+constexpr int rvalue_object() {
+ RvalueObject r{1};
+ RvalueObject a = static_cast<RvalueObject &&>(r)++;
+ return a.v * 10 + r.v;
+}
+static_assert(rvalue_object() == 12);
+
+// The result of the prefix operator is discarded, so its type is irrelevant.
+struct ReturnsVoid {
+ int v;
+ constexpr void operator++() { ++v; }
+ ReturnsVoid operator++(int) = default;
+};
+constexpr int returns_void() {
+ ReturnsVoid r{1};
+ ReturnsVoid a = r++;
+ return a.v * 10 + r.v;
+}
+static_assert(returns_void() == 12);
+
+// Enumerations.
+enum E { e0, e1, e2 };
+constexpr E &operator++(E &e) { return e = E(e + 1); }
+constexpr E operator++(E &, int) = default;
+constexpr int enumeration() {
+ E e = e0;
+ E a = e++;
+ return a * 10 + e;
+}
+static_assert(enumeration() == 1);
+
+// The prefix operator may be a built-in operator reached through a conversion
+// function.
+struct ConvertsToInt {
+ int v;
+ constexpr operator int &() { return v; }
+ ConvertsToInt operator++(int) = default;
+};
+constexpr int converts_to_int() {
+ ConvertsToInt c{1};
+ ConvertsToInt a = c++;
+ return a.v * 10 + c.v;
+}
+static_assert(converts_to_int() == 12);
+
+// Class templates.
+template <typename T> struct Wrapper {
+ T v;
+ constexpr Wrapper &operator++() { ++v; return *this; }
+ constexpr Wrapper &operator--() { --v; return *this; }
+ Wrapper operator++(int) = default;
+ friend Wrapper operator--(Wrapper &, int) = default;
+};
+constexpr int wrapper() {
+ Wrapper<int> w{5};
+ Wrapper<int> a = w++;
+ Wrapper<int> b = w--;
+ return a.v * 100 + b.v * 10 + w.v;
+}
+static_assert(wrapper() == 565);
+
+// Direct-initialization of the copy permits an explicit copy constructor, but
+// the return statement needs a usable move or copy constructor.
+struct ExplicitCopy {
+ explicit ExplicitCopy(const ExplicitCopy &);
+ ExplicitCopy(ExplicitCopy &&);
+ ExplicitCopy &operator++();
+ ExplicitCopy operator++(int) = default; // OK
+};
+void explicit_copy(ExplicitCopy e) { e++; }
+
+struct ExplicitCopyOnly {
+ explicit ExplicitCopyOnly(const ExplicitCopyOnly &); // expected-note {{explicit constructor is not a candidate}}
+ ExplicitCopyOnly &operator++();
+ ExplicitCopyOnly operator++(int) = default; // expected-error {{no matching constructor for initialization of 'ExplicitCopyOnly'}}
+};
+void explicit_copy_only(ExplicitCopyOnly e) {
+ e++; // expected-note {{in defaulted postfix increment operator for 'ExplicitCopyOnly' first required here}}
+}
+
+struct DeletedMove {
+ DeletedMove(const DeletedMove &);
+ DeletedMove(DeletedMove &&) = delete; // expected-note {{'DeletedMove' has been explicitly marked deleted here}}
+ DeletedMove &operator++();
+ DeletedMove operator++(int) = default; // expected-error {{call to deleted constructor of 'DeletedMove'}}
+};
+void deleted_move(DeletedMove d) {
+ d++; // expected-note {{in defaulted postfix increment operator for 'DeletedMove' first required here}}
+}
+
+// Attributes on the prefix operator are honored, as for any other call.
+struct Nodiscard {
+ [[nodiscard]] Nodiscard &operator++();
+ Nodiscard operator++(int) = default; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+};
+void nodiscard(Nodiscard n) {
+ n++; // expected-note {{in defaulted postfix increment operator for 'Nodiscard' first required here}}
+}
+
+// Volatile objects.
+struct Volatile {
+ Volatile();
+ Volatile(const Volatile &);
+ Volatile(const volatile Volatile &);
+ Volatile(Volatile &&);
+ Volatile &operator++() volatile;
+ Volatile operator++(int) volatile = default;
+};
+void use_volatile() {
+ volatile Volatile v;
+ v++;
+}
+
+// Name lookup for the prefix operator is performed from a context equivalent
+// to the function body, even if the body is synthesized elsewhere.
+namespace Lookup {
+namespace A {
+struct X { int v; };
+struct Y;
+} // namespace A
+namespace B {
+constexpr A::Y &operator++(A::Y &y);
+} // namespace B
+namespace C {
+namespace Nested {
+constexpr A::X &operator++(A::X &x) { ++x.v; return x; }
+} // namespace Nested
+using namespace Nested;
+// Found by unqualified lookup through the using-directive, not by ADL.
+constexpr A::X operator++(A::X &, int) = default;
+} // namespace C
+constexpr int non_member() {
+ A::X x{1};
+ A::X a = C::operator++(x, 0);
+ return a.v * 10 + x.v;
+}
+static_assert(non_member() == 12);
+
+namespace A {
+using namespace B;
+struct Y {
+ int v;
+ Y operator++(int) = default;
+};
+} // namespace A
+constexpr A::Y &B::operator++(A::Y &y) { ++y.v; return y; }
+namespace D {
+constexpr int member() {
+ A::Y y{1};
+ A::Y a = y++; // The body is synthesized here, where B::operator++ is not visible.
+ return a.v * 10 + y.v;
+}
+static_assert(member() == 12);
+} // namespace D
+} // namespace Lookup
+
+// C++2d [except.spec]p10:
+// The exception specification for [...] a postfix increment or decrement
+// operator function without a noexcept-specifier that is defaulted on its
+// first declaration is potentially-throwing if and only if any expression in
+// the implicit definition is potentially-throwing.
+struct Noexcept {
+ Noexcept &operator++() noexcept;
+ Noexcept operator++(int) = default;
+};
+static_assert(noexcept(Noexcept{}++));
+struct ThrowingPrefix {
+ ThrowingPrefix &operator++();
+ ThrowingPrefix operator++(int) = default;
+};
+static_assert(!noexcept(ThrowingPrefix{}++));
+struct ThrowingCopy {
+ ThrowingCopy();
+ ThrowingCopy(const ThrowingCopy &) noexcept(false);
+ ThrowingCopy &operator++() noexcept;
+ ThrowingCopy operator++(int) = default;
+};
+static_assert(!noexcept(ThrowingCopy{}++));
+struct ThrowingDestructor {
+ ThrowingDestructor();
+ ~ThrowingDestructor() noexcept(false);
+ ThrowingDestructor &operator++() noexcept;
+ ThrowingDestructor operator++(int) = default;
+};
+static_assert(!noexcept(ThrowingDestructor{}++));
+struct ExplicitNoexcept {
+ ExplicitNoexcept &operator++();
+ ExplicitNoexcept operator++(int) noexcept = default;
+};
+static_assert(noexcept(ExplicitNoexcept{}++));
+struct ThrowingNonMember {
+ ThrowingNonMember &operator++();
+};
+ThrowingNonMember operator++(ThrowingNonMember &, int) = default;
+extern ThrowingNonMember throwing_non_member;
+static_assert(!noexcept(throwing_non_member++));
+struct NoexceptNonMember {
+ NoexceptNonMember &operator++() noexcept;
+};
+NoexceptNonMember operator++(NoexceptNonMember &, int) = default;
+extern NoexceptNonMember noexcept_non_member;
+static_assert(noexcept(noexcept_non_member++));
diff --git a/clang/test/CodeGenCXX/cxx2d-defaulted-postfix-operators.cpp b/clang/test/CodeGenCXX/cxx2d-defaulted-postfix-operators.cpp
new file mode 100644
index 00000000000000..e84f07710802a4
--- /dev/null
+++ b/clang/test/CodeGenCXX/cxx2d-defaulted-postfix-operators.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -std=c++2d -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+// A defaulted postfix increment or decrement operator copies the object,
+// applies the prefix operator, and returns the copy (with NRVO).
+
+struct Trivial {
+ int v;
+ Trivial &operator++();
+ Trivial operator++(int) = default;
+};
+Trivial use_trivial(Trivial t) { return t++; }
+
+// CHECK-LABEL: define {{.*}} @_Z11use_trivial7Trivial(
+// CHECK: call i32 @_ZN7TrivialppEi(ptr {{.*}} %t, i32 noundef 0)
+
+// A function defaulted on its first declaration is implicitly inline.
+// CHECK-LABEL: define linkonce_odr i32 @_ZN7TrivialppEi(ptr {{.*}} %this, i32 noundef %0)
+// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %retval, ptr align 4 %this1, i64 4, i1 false)
+// CHECK-NEXT: call {{.*}} @_ZN7TrivialppEv(ptr {{.*}} %this1)
+// CHECK: ret i32
+
+struct NonTrivial {
+ int *p;
+ NonTrivial(const NonTrivial &);
+ NonTrivial(NonTrivial &&);
+ ~NonTrivial();
+ NonTrivial &operator--();
+};
+NonTrivial operator--(NonTrivial &, int) = default;
+NonTrivial use_non_trivial(NonTrivial n) { return n--; }
+
+// CHECK-LABEL: define {{.*}} @_Z15use_non_trivial10NonTrivial(
+// CHECK: call void @_ZmmR10NonTriviali(ptr {{.*}} %agg.result, ptr {{.*}} %n, i32 noundef 0)
+
+// The copy is constructed directly in the return slot; no move constructor is
+// called.
+// CHECK-LABEL: define linkonce_odr void @_ZmmR10NonTriviali(ptr {{.*}} %agg.result, ptr {{.*}} %0, i32 noundef %1)
+// CHECK: call void @_ZN10NonTrivialC1ERKS_(ptr {{.*}} %agg.result, ptr {{.*}})
+// CHECK: call {{.*}} @_ZN10NonTrivialmmEv(
+// CHECK-NOT: call void @_ZN10NonTrivialC1EOS_
+// CHECK: ret void
+
+struct Explicit {
+ int v;
+ Explicit &operator++();
+ Explicit operator++(this Explicit &self, int) = default;
+};
+Explicit use_explicit(Explicit e) { return e++; }
+
+// CHECK-LABEL: define linkonce_odr i32 @_ZNH8ExplicitppERS_i(ptr {{.*}} %self, i32 noundef %0)
+// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %retval, ptr align 4 %{{.*}}, i64 4, i1 false)
+// CHECK: call {{.*}} @_ZN8ExplicitppEv(
+// CHECK: ret i32
diff --git a/clang/test/PCH/cxx2d-defaulted-postfix-operators.cpp b/clang/test/PCH/cxx2d-defaulted-postfix-operators.cpp
new file mode 100644
index 00000000000000..9d4c22cd00ee69
--- /dev/null
+++ b/clang/test/PCH/cxx2d-defaulted-postfix-operators.cpp
@@ -0,0 +1,59 @@
+// RUN: %clang_cc1 -std=c++2d -verify -include %s %s
+//
+// RUN: %clang_cc1 -std=c++2d -emit-pch %s -o %t.pch
+// RUN: %clang_cc1 -std=c++2d -include-pch %t.pch %s -verify
+//
+// RUN: %clang_cc1 -std=c++2d -emit-pch -fpch-instantiate-templates %s -o %t.pch
+// RUN: %clang_cc1 -std=c++2d -include-pch %t.pch %s -verify
+
+// expected-no-diagnostics
+
+#ifndef INCLUDED
+#define INCLUDED
+
+struct Counter {
+ int v;
+ constexpr Counter &operator++() { ++v; return *this; }
+ constexpr Counter &operator--() { --v; return *this; }
+ Counter operator++(int) = default;
+ friend Counter operator--(Counter &, int) = default;
+};
+
+// Ensure that we can round-trip DefaultedOrDeletedInfo through an AST file.
+namespace LookupContext {
+namespace A {
+struct X { int v; };
+}
+namespace B {
+constexpr A::X &operator++(A::X &x) { ++x.v; return x; }
+}
+namespace A {
+using namespace B;
+template <typename T> struct Y {
+ T x;
+ constexpr Y &operator++() { ++x; return *this; }
+ Y operator++(int) = default;
+};
+} // namespace A
+} // namespace LookupContext
+
+#else
+
+constexpr int counter() {
+ Counter c{1};
+ Counter a = c++;
+ Counter b = c--;
+ return a.v * 100 + b.v * 10 + c.v;
+}
+static_assert(counter() == 121);
+
+namespace Other {
+constexpr int lookup() {
+ LookupContext::A::Y<LookupContext::A::X> y{{1}};
+ LookupContext::A::Y<LookupContext::A::X> a = y++;
+ return a.x.v * 10 + y.x.v;
+}
+static_assert(lookup() == 12);
+} // namespace Other
+
+#endif
diff --git a/clang/test/SemaCXX/cxx2d-defaulted-postfix-operators.cpp b/clang/test/SemaCXX/cxx2d-defaulted-postfix-operators.cpp
new file mode 100644
index 00000000000000..da75d03dac96d9
--- /dev/null
+++ b/clang/test/SemaCXX/cxx2d-defaulted-postfix-operators.cpp
@@ -0,0 +1,108 @@
+// RUN: %clang_cc1 -std=c++03 -verify=expected,cxx03,pre14,pre20,pre23,pre2d %s
+// RUN: %clang_cc1 -std=c++11 -verify=expected,pre14,pre20,pre23,pre2d %s
+// RUN: %clang_cc1 -std=c++14 -verify=expected,pre20,pre23,pre2d %s
+// RUN: %clang_cc1 -std=c++17 -verify=expected,pre20,pre23,pre2d %s
+// RUN: %clang_cc1 -std=c++20 -verify=expected,cxx20,pre23,pre2d %s
+// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20,pre2d %s
+// RUN: %clang_cc1 -std=c++26 -verify=expected,cxx20,pre2d %s
+// RUN: %clang_cc1 -std=c++2d -verify=expected,cxx2d %s
+// RUN: %clang_cc1 -std=c++2d -Wpre-c++2d-compat -verify=expected,cxx2d,compat %s
+
+// Defaulted postfix increment and decrement operators (P3668R4) are a C++2d
+// feature that is available in all language modes as an extension.
+
+struct S {
+ int v;
+ S &operator++();
+ S &operator--();
+ S operator++(int) = default;
+ // pre2d-warning at -1 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning at -2 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+ // cxx03-warning at -3 {{defaulted function definitions are a C++11 extension}}
+ S operator--(int) = default;
+ // pre2d-warning at -1 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning at -2 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+ // cxx03-warning at -3 {{defaulted function definitions are a C++11 extension}}
+};
+void use_s(S s) {
+ s++;
+ s--;
+}
+
+struct N {
+ N &operator++();
+};
+N operator++(N &, int) = default;
+// pre2d-warning at -1 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+// compat-warning at -2 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+// cxx03-warning at -3 {{defaulted function definitions are a C++11 extension}}
+void use_n(N n) { n++; }
+
+// The warning is produced once for a class template, not again for each
+// instantiation.
+template <typename T> struct W {
+ T v;
+ W &operator++();
+ W operator++(int) = default;
+ // pre2d-warning at -1 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning at -2 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+ // cxx03-warning at -3 {{defaulted function definitions are a C++11 extension}}
+};
+void use_w(W<int> w, W<long> x) {
+ w++;
+ x++;
+}
+
+// Other functions still cannot be defaulted.
+struct Other {
+ void f() = default;
+ // pre20-error at -1 {{only special member functions may be defaulted}}
+ // cxx20-error at -2 {{only special member functions and comparison operators may be defaulted}}
+ // cxx2d-error at -3 {{only special member functions, comparison operators, and postfix increment and decrement operators may be defaulted}}
+ // cxx03-warning at -4 {{defaulted function definitions are a C++11 extension}}
+ Other &operator++() = default;
+ // expected-error at -1 {{only the postfix form of 'operator++' can be defaulted}}
+ // cxx03-warning at -2 {{defaulted function definitions are a C++11 extension}}
+};
+
+// An implicitly deleted defaulted postfix operator.
+struct Deleted {
+ Deleted operator++(int) = default; // #Deleted
+ // expected-warning@#Deleted {{explicitly defaulted postfix increment operator is implicitly deleted}}
+ // expected-note@#Deleted 2 {{defaulted 'operator++' is implicitly deleted because there is no viable prefix 'operator++' for an lvalue of type 'Deleted'}}
+ // expected-note@#Deleted {{replace 'default' with 'delete'}}
+ // expected-note@#Deleted {{explicitly defaulted function was implicitly deleted here}}
+ // pre2d-warning@#Deleted {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning@#Deleted {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+ // cxx03-warning@#Deleted {{defaulted function definitions are a C++11 extension}}
+};
+void use_deleted(Deleted d) {
+ d++; // expected-error {{object of type 'Deleted' cannot be incremented because its defaulted postfix increment operator is implicitly deleted}}
+}
+
+#if __cplusplus >= 201402L
+// Before C++23, an explicitly-defaulted function may only be declared
+// constexpr if it is constexpr-compatible.
+struct NotConstexprCompatible {
+ NotConstexprCompatible &operator++(); // pre23-note {{non-constexpr prefix 'operator++' declared here}}
+ constexpr NotConstexprCompatible operator++(int) = default;
+ // pre23-error at -1 {{defaulted definition of postfix increment operator cannot be declared constexpr because it invokes a non-constexpr function}}
+ // pre2d-warning at -2 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning at -3 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+};
+
+// A defaulted postfix operator is implicitly constexpr when it can be.
+struct Constexpr {
+ int v;
+ constexpr Constexpr &operator++() { ++v; return *this; }
+ Constexpr operator++(int) = default;
+ // pre2d-warning at -1 {{defaulted postfix increment and decrement operators are a C++2d extension}}
+ // compat-warning at -2 {{defaulted postfix increment and decrement operators are incompatible with C++ standards before C++2d}}
+};
+constexpr int use_constexpr() {
+ Constexpr c{1};
+ Constexpr a = c++;
+ return a.v * 10 + c.v;
+}
+static_assert(use_constexpr() == 12, "");
+#endif
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index 61d0f38bce87a5..c9ae0696461722 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -117,7 +117,7 @@ <h2 id="cxx29">C++2d implementation status</h2>
<tr>
<td>Defaulting postfix increment and decrement operations</td>
<td><a href="https://wg21.link/P3668R4">P3668R4</a></td>
- <td class="none" align="center">No</td>
+ <td class="unreleased" align="center">Clang 24</td>
</tr>
<tr>
<td>Adding restrictions to defaulted assignment operator functions</td>
>From ce09ad196e5bc6af60bc775bebc4b336048c45cf Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 03:58:43 +0300
Subject: [PATCH 02/18] cover more test cases
---
.../over.inc/over.inc.default/p3.cpp | 73 +++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
index 2f8d1d9788c672..d0532d3c648e2f 100644
--- a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
@@ -1,4 +1,16 @@
// RUN: %clang_cc1 -std=c++2d -verify %s
+// RUN: %clang_cc1 -std=c++2d -funknown-anytype -DUNKNOWN_ANYTYPE -verify=anytype %s
+
+#ifdef UNKNOWN_ANYTYPE
+// The discarded-value prefix call must have a known type.
+struct UnknownPrefixResult {
+ __unknown_anytype operator++();
+ UnknownPrefixResult operator++(int) = default; // anytype-error {{'operator++' has unknown return type; cast the call to its declared return type}}
+};
+void unknown_prefix_result(UnknownPrefixResult u) {
+ u++; // anytype-note {{in defaulted postfix increment operator for 'UnknownPrefixResult' first required here}}
+}
+#else
// C++2d [over.inc.default]p3:
// The implicit definition of a defaulted postfix increment or decrement
@@ -149,6 +161,66 @@ void deleted_move(DeletedMove d) {
d++; // expected-note {{in defaulted postfix increment operator for 'DeletedMove' first required here}}
}
+// [over.inc.default]p2 checks the copy from an lvalue of type C, but in the
+// implicit definition of a volatile member, c is an lvalue of type volatile C.
+struct VolatileAggregate {
+ volatile VolatileAggregate &operator++() volatile;
+ VolatileAggregate operator++(int) volatile = default; // expected-error {{excess elements in struct initializer}}
+};
+void volatile_aggregate(VolatileAggregate v) {
+ v++; // expected-note {{in defaulted postfix increment operator for 'VolatileAggregate' first required here}}
+}
+
+// Errors that overload resolution does not detect make the implicit definition
+// ill-formed rather than deleted.
+struct BadDefaultArgument {
+ BadDefaultArgument();
+ template <class T> BadDefaultArgument(T &, int = T::missing); // expected-error 2 {{no member named 'missing' in 'BadDefaultArgument'}}
+ BadDefaultArgument &operator++();
+ BadDefaultArgument operator++(int) = default; // expected-note 2 {{in instantiation of default function argument expression for 'BadDefaultArgument<BadDefaultArgument>' required here}}
+};
+void bad_default_argument(BadDefaultArgument b) {
+ b++; // expected-note 2 {{in defaulted postfix increment operator for 'BadDefaultArgument' first required here}}
+}
+
+struct Incomplete; // expected-note 2 {{forward declaration of 'Incomplete'}}
+struct IncompletePrefixResult {
+ Incomplete operator++(); // expected-note {{'operator++' declared here}}
+ IncompletePrefixResult operator++(int) = default; // expected-error {{calling 'operator++' with incomplete return type 'Incomplete'}}
+};
+void incomplete_prefix_result(IncompletePrefixResult i) {
+ i++; // expected-note {{in defaulted postfix increment operator for 'IncompletePrefixResult' first required here}}
+}
+
+struct DeletedDestructor {
+ ~DeletedDestructor() = delete; // expected-note {{'~DeletedDestructor' has been explicitly marked deleted here}}
+};
+struct DeletedDestructorPrefixResult {
+ DeletedDestructor operator--();
+ DeletedDestructorPrefixResult operator--(int) = default; // expected-error {{attempt to use a deleted function}}
+};
+void deleted_destructor_prefix_result(DeletedDestructorPrefixResult d) {
+ d--; // expected-note {{in defaulted postfix decrement operator for 'DeletedDestructorPrefixResult' first required here}}
+}
+
+// Uses of the first parameter in the implicit definition are checked as usual.
+struct UnavailableParam {
+ UnavailableParam &operator++();
+};
+UnavailableParam operator++(UnavailableParam &u __attribute__((unavailable)), int) = default; // expected-note 2 {{'u' has been explicitly marked unavailable here}}
+// expected-error at -1 2 {{'u' is unavailable}}
+// expected-note at -2 {{in defaulted postfix increment operator for 'UnavailableParam' first required here}}
+void unavailable_param(UnavailableParam u) { u++; }
+
+// The same errors are diagnosed when the body is only built to compute the
+// exception specification.
+struct IncompletePrefixResultNoexcept {
+ Incomplete operator++(); // expected-note {{'operator++' declared here}}
+ IncompletePrefixResultNoexcept operator++(int) = default; // expected-error {{calling 'operator++' with incomplete return type 'Incomplete'}}
+};
+extern IncompletePrefixResultNoexcept incomplete_prefix_result_noexcept;
+bool incomplete_noexcept = noexcept(incomplete_prefix_result_noexcept++); // expected-note {{in evaluation of exception specification for 'IncompletePrefixResultNoexcept::operator++' needed here}}
+
// Attributes on the prefix operator are honored, as for any other call.
struct Nodiscard {
[[nodiscard]] Nodiscard &operator++();
@@ -261,3 +333,4 @@ struct NoexceptNonMember {
NoexceptNonMember operator++(NoexceptNonMember &, int) = default;
extern NoexceptNonMember noexcept_non_member;
static_assert(noexcept(noexcept_non_member++));
+#endif
>From f4a3307628022302b4cdf0568bcfaea725408af7 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:00:57 +0300
Subject: [PATCH 03/18] Use `"__ugly"` identifiers.
---
clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
.../ast-dump-defaulted-postfix-operators.cpp | 18 +++++++++---------
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 97a3114ca99a3a..c19a25ca20ebaf 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9770,7 +9770,7 @@ static StmtResult buildDefaultedPostfixOperatorBody(Sema &S, FunctionDecl *FD,
if (Init.isInvalid())
return StmtError();
QualType C = FD->getReturnType();
- IdentifierInfo *TmpName = &S.Context.Idents.get("tmp");
+ IdentifierInfo *TmpName = &S.Context.Idents.get("__tmp");
VarDecl *Tmp =
VarDecl::Create(S.Context, S.CurContext, Loc, Loc, TmpName, C,
S.Context.getTrivialTypeSourceInfo(C, Loc), SC_None);
diff --git a/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp b/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
index 3b9bec55202137..ced3d5d2ce539c 100644
--- a/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
+++ b/clang/test/AST/ast-dump-defaulted-postfix-operators.cpp
@@ -17,7 +17,7 @@ void use_member(S s) { s++; }
// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
// CHECK-NEXT: CompoundStmt
// CHECK-NEXT: DeclStmt
-// CHECK-NEXT: VarDecl {{.*}} used tmp 'S' nrvo callinit
+// CHECK-NEXT: VarDecl {{.*}} used __tmp 'S' nrvo callinit
// CHECK-NEXT: CXXConstructExpr {{.*}} 'S' 'void (const S &) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const S' lvalue <NoOp>
// CHECK-NEXT: UnaryOperator {{.*}} 'S' lvalue prefix '*' cannot overflow
@@ -27,10 +27,10 @@ void use_member(S s) { s++; }
// CHECK-NEXT: DeclRefExpr {{.*}} 'S &()' lvalue CXXMethod {{.*}} 'operator++' 'S &()'
// CHECK-NEXT: UnaryOperator {{.*}} 'S' lvalue prefix '*' cannot overflow
// CHECK-NEXT: CXXThisExpr {{.*}} 'S *' this
-// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'S')
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} '__tmp' 'S')
// CHECK-NEXT: CXXConstructExpr {{.*}} 'S' 'void (S &&) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'S' xvalue <NoOp>
-// CHECK-NEXT: DeclRefExpr {{.*}} 'S' lvalue Var {{.*}} 'tmp' 'S'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'S' lvalue Var {{.*}} '__tmp' 'S'
struct T {
int v;
@@ -44,7 +44,7 @@ void use_non_member(T t) { t--; }
// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
// CHECK-NEXT: CompoundStmt
// CHECK-NEXT: DeclStmt
-// CHECK-NEXT: VarDecl {{.*}} used tmp 'T' nrvo callinit
+// CHECK-NEXT: VarDecl {{.*}} used __tmp 'T' nrvo callinit
// CHECK-NEXT: CXXConstructExpr {{.*}} 'T' 'void (const T &) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const T' lvalue <NoOp>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 'self' 'T &'
@@ -52,10 +52,10 @@ void use_non_member(T t) { t--; }
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T &(*)()' <FunctionToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'T &()' lvalue CXXMethod {{.*}} 'operator--' 'T &()'
// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue ParmVar {{.*}} 'self' 'T &'
-// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'T')
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} '__tmp' 'T')
// CHECK-NEXT: CXXConstructExpr {{.*}} 'T' 'void (T &&) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'T' xvalue <NoOp>
-// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue Var {{.*}} 'tmp' 'T'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'T' lvalue Var {{.*}} '__tmp' 'T'
struct U {
int v;
@@ -69,7 +69,7 @@ void use_explicit_object(U u) { u++; }
// CHECK-NEXT: ParmVarDecl {{.*}} 'int'
// CHECK-NEXT: CompoundStmt
// CHECK-NEXT: DeclStmt
-// CHECK-NEXT: VarDecl {{.*}} used tmp 'U' nrvo callinit
+// CHECK-NEXT: VarDecl {{.*}} used __tmp 'U' nrvo callinit
// CHECK-NEXT: CXXConstructExpr {{.*}} 'U' 'void (const U &) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'const U' lvalue <NoOp>
// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue ParmVar {{.*}} 'self' 'U &'
@@ -77,10 +77,10 @@ void use_explicit_object(U u) { u++; }
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'U &(*)()' <FunctionToPointerDecay>
// CHECK-NEXT: DeclRefExpr {{.*}} 'U &()' lvalue CXXMethod {{.*}} 'operator++' 'U &()'
// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue ParmVar {{.*}} 'self' 'U &'
-// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} 'tmp' 'U')
+// CHECK-NEXT: ReturnStmt {{.*}} nrvo_candidate(Var {{.*}} '__tmp' 'U')
// CHECK-NEXT: CXXConstructExpr {{.*}} 'U' 'void (U &&) noexcept'
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'U' xvalue <NoOp>
-// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue Var {{.*}} 'tmp' 'U'
+// CHECK-NEXT: DeclRefExpr {{.*}} 'U' lvalue Var {{.*}} '__tmp' 'U'
// A deleted defaulted postfix operator has no body.
struct Deleted {
>From 2e10a137a2ebe92b3cd30d0c7f78885924717add Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:06:04 +0300
Subject: [PATCH 04/18] Refactor exception specification handling for defaulted
postfix operators.
---
clang/lib/Sema/SemaDeclCXX.cpp | 5 -----
.../CXX/over/over.oper/over.inc/over.inc.default/p3.cpp | 8 ++++++++
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c19a25ca20ebaf..9e248247a9a7ce 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -10105,11 +10105,6 @@ ComputeDefaultedPostfixOperatorExceptionSpec(Sema &S, SourceLocation Loc,
ExceptSpec.CalledStmt(Body.get());
}
- // The implicit definition also destroys the temporary copy.
- if (CXXRecordDecl *RD = FD->getReturnType()->getAsCXXRecordDecl())
- if (CXXDestructorDecl *Dtor = S.LookupDestructor(RD))
- ExceptSpec.CalledDecl(Loc, Dtor);
-
return ExceptSpec;
}
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
index d0532d3c648e2f..64d3d6ad8638b6 100644
--- a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p3.cpp
@@ -316,6 +316,14 @@ struct ThrowingDestructor {
ThrowingDestructor operator++(int) = default;
};
static_assert(!noexcept(ThrowingDestructor{}++));
+struct ThrowingDestructorDefined {
+ ThrowingDestructorDefined();
+ ~ThrowingDestructorDefined() noexcept(false);
+ ThrowingDestructorDefined &operator++() noexcept;
+ ThrowingDestructorDefined operator++(int) = default;
+};
+void throwing_destructor_defined(ThrowingDestructorDefined t) { t++; }
+static_assert(!noexcept(ThrowingDestructorDefined{}++));
struct ExplicitNoexcept {
ExplicitNoexcept &operator++();
ExplicitNoexcept operator++(int) noexcept = default;
>From 9a9cefb08b93869cacf56f3902262645ee85a0db Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:36:03 +0300
Subject: [PATCH 05/18] compact 1 byte
---
clang/include/clang/AST/Decl.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 34e7f4ea701cdd..3a270d916f1623 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -2133,8 +2133,9 @@ class FunctionDecl : public DeclaratorDecl,
/// For a defaulted function, the kind of defaulted function that it is.
class DefaultedFunctionKind {
LLVM_PREFERRED_TYPE(CXXSpecialMemberKind)
- unsigned SpecialMember : 8;
- unsigned Comparison : 8;
+ unsigned SpecialMember : 3;
+ LLVM_PREFERRED_TYPE(DefaultedComparisonKind)
+ unsigned Comparison : 3;
LLVM_PREFERRED_TYPE(PostfixOperatorKind)
unsigned PostfixOperator : 2;
>From ff9fa36709cd35ad48994299be3711872d9f1230 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:46:54 +0300
Subject: [PATCH 06/18] delegate constructors to the default constructor
---
clang/include/clang/AST/Decl.h | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 3a270d916f1623..b3d218bc0af7a8 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -2144,18 +2144,16 @@ class FunctionDecl : public DeclaratorDecl,
: SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
- DefaultedFunctionKind(CXXSpecialMemberKind CSM)
- : SpecialMember(llvm::to_underlying(CSM)),
- Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
- PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
+ DefaultedFunctionKind(CXXSpecialMemberKind CSM) : DefaultedFunctionKind() {
+ SpecialMember = llvm::to_underlying(CSM);
+ }
DefaultedFunctionKind(DefaultedComparisonKind Comp)
- : SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
- Comparison(llvm::to_underlying(Comp)),
- PostfixOperator(llvm::to_underlying(PostfixOperatorKind::None)) {}
- DefaultedFunctionKind(PostfixOperatorKind PO)
- : SpecialMember(llvm::to_underlying(CXXSpecialMemberKind::Invalid)),
- Comparison(llvm::to_underlying(DefaultedComparisonKind::None)),
- PostfixOperator(llvm::to_underlying(PO)) {}
+ : DefaultedFunctionKind() {
+ Comparison = llvm::to_underlying(Comp);
+ }
+ DefaultedFunctionKind(PostfixOperatorKind PO) : DefaultedFunctionKind() {
+ PostfixOperator = llvm::to_underlying(PO);
+ }
bool isSpecialMember() const {
return static_cast<CXXSpecialMemberKind>(SpecialMember) !=
>From 67d6959d81cf52684948b4e9dc86acec46ae31a5 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:48:46 +0300
Subject: [PATCH 07/18] reorganize compatibility warnings
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index b2f72cab83243d..ffa178f6fe2098 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -88,8 +88,6 @@ defm equals_this_lambda_capture : CXX20Compat<
"explicit capture of 'this' with a capture default of '=' is">;
defm capture_binding : CXX20Compat<"captured structured bindings are">;
defm defaulted_comparison : CXX20Compat<"defaulted comparison operators are">;
-defm defaulted_postfix_operator : CXX29Compat<
- "defaulted postfix increment and decrement operators are">;
// C++23 compatibility with C++20 and earlier.
defm constexpr_static_var : CXX23Compat<
@@ -108,6 +106,8 @@ defm placeholder_var_definition : CXX26Compat<"placeholder variables are">;
// C++29 compatibility with C++26 and earlier.
defm pack_indexing_template : CXX29Compat<"pack indexing for template names is">;
+defm defaulted_postfix_operator : CXX29Compat<
+ "defaulted postfix increment and decrement operators are">;
// Compatibility warnings duplicated across multiple language versions.
foreach std = [14, 20, 23] in {
>From 45e5b75c1ab80f0e0131c82ebf9e47a0d8a06c1c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 04:51:16 +0300
Subject: [PATCH 08/18] rephrase comment
---
clang/lib/AST/Decl.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index aa0f37aa1a782e..e93507140ac6b7 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3312,7 +3312,7 @@ FunctionDecl::getDefaultedFunctionKind() const {
case OO_MinusMinus: {
// Only the postfix forms can be defaulted. They are distinguished from the
// prefix forms by their trailing 'int' parameter, so they have exactly two
- // parameters once the implicit object parameter (if any) is counted.
+ // parameters including implicit or explicit 'this'.
unsigned NumParams = getNumParams();
if (const auto *MD = dyn_cast<CXXMethodDecl>(this);
MD && MD->isImplicitObjectMemberFunction())
>From d0a41a0ba6a2d2d938c43e7f5e68c173b7b6d060 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 05:16:59 +0300
Subject: [PATCH 09/18] use `enum_select`
---
.../clang/Basic/DiagnosticCommonKinds.td | 8 +--
.../clang/Basic/DiagnosticSemaKinds.td | 8 ++-
clang/lib/Parse/ParseDecl.cpp | 6 ++-
clang/lib/Parse/ParseDeclCXX.cpp | 6 ++-
clang/lib/Sema/SemaDeclCXX.cpp | 54 ++++++++-----------
5 files changed, 42 insertions(+), 40 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index 2a503cd76e0eba..d1b62ac13fd23d 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -97,9 +97,11 @@ def err_invalid_storage_class_in_func_decl : Error<
"invalid storage class specifier in function declarator">;
def err_expected_namespace_name : Error<"expected namespace name">;
def err_default_special_members : Error<
- "only special member functions%select{| and comparison operators|, "
- "comparison operators, and postfix increment and decrement operators}0 "
- "may be defaulted">;
+ "only special member functions"
+ "%enum_select<DefaultableFunctionKinds>{%SpecialMembers{}|"
+ "%Comparisons{ and comparison operators}|"
+ "%PostfixOperators{, comparison operators, and postfix increment and "
+ "decrement operators}}0 may be defaulted">;
def err_deleted_non_function : Error<
"only functions can have deleted definitions">;
def err_module_not_found : Error<"module '%0' not found">, DefaultFatal;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index ffa178f6fe2098..d5917d12857da7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10720,6 +10720,8 @@ def note_defaulted_postfix_operator_ambiguous : Note<
"defaulted %0 is implicitly deleted because %select{copy construction of|"
"prefix 'operator++' for|prefix 'operator--' for}1 an lvalue of type %2 "
"is ambiguous">;
+// The %select indices of the operations in the following notes are given by
+// diag::DefaultedPostfixOperatorStep.
def note_defaulted_postfix_operator_calls_deleted : Note<
"defaulted %0 is implicitly deleted because it would invoke a deleted "
"%select{constructor|destructor|prefix 'operator++'|prefix 'operator--'}1">;
@@ -10732,8 +10734,10 @@ def err_defaulted_postfix_operator_constexpr_mismatch : Error<
"cannot be declared %select{constexpr|consteval}1 because it invokes a "
"non-constexpr function">;
def note_defaulted_postfix_operator_not_constexpr_here : Note<
- "non-constexpr %select{constructor|destructor|prefix 'operator++'|"
- "prefix 'operator--'|conversion function}0 declared here">;
+ "non-constexpr %enum_select<DefaultedPostfixOperatorStep>{"
+ "%Constructor{constructor}|%Destructor{destructor}|"
+ "%PrefixIncrement{prefix 'operator++'}|%PrefixDecrement{prefix 'operator--'}|"
+ "%ConversionFunction{conversion function}}0 declared here">;
def err_ovl_deleted_postfix_operator : Error<
"object of type %0 cannot be %select{<ERROR>|incremented|decremented}1 "
"because its defaulted %sub{select_defaulted_postfix_operator_kind}1 is "
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index c3da1f50ba23fe..691fb814d370c2 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -2628,7 +2628,11 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
<< 0 /* default */;
else
Diag(ConsumeToken(), diag::err_default_special_members)
- << (getLangOpts().CPlusPlus29 ? 2 : getLangOpts().CPlusPlus20);
+ << (getLangOpts().CPlusPlus29
+ ? diag::DefaultableFunctionKinds::PostfixOperators
+ : getLangOpts().CPlusPlus20
+ ? diag::DefaultableFunctionKinds::Comparisons
+ : diag::DefaultableFunctionKinds::SpecialMembers);
} else {
InitializerScopeRAII InitScope(*this, D, ThisDecl);
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 644b2e1f180dc3..d34051caa925f6 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -3377,7 +3377,11 @@ ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
<< 0 /* default */;
else
Diag(ConsumeToken(), diag::err_default_special_members)
- << (getLangOpts().CPlusPlus29 ? 2 : getLangOpts().CPlusPlus20);
+ << (getLangOpts().CPlusPlus29
+ ? diag::DefaultableFunctionKinds::PostfixOperators
+ : getLangOpts().CPlusPlus20
+ ? diag::DefaultableFunctionKinds::Comparisons
+ : diag::DefaultableFunctionKinds::SpecialMembers);
return ExprError();
}
}
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9e248247a9a7ce..9b60451d0de3a0 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9505,18 +9505,6 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
//===----------------------------------------------------------------------===//
namespace {
-/// The operations performed by the implicit definition of a defaulted postfix
-/// increment or decrement operator function. The values correspond to %select
-/// indices in the diagnostics explaining why such a function is deleted or is
-/// not constexpr.
-enum class PostfixOperatorStep {
- Constructor,
- Destructor,
- PrefixIncrement,
- PrefixDecrement,
- ConversionFunction
-};
-
/// Which diagnostics to produce while analyzing a defaulted postfix increment
/// or decrement operator function.
enum class PostfixOperatorDiagnoseKind {
@@ -9578,29 +9566,28 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
Info.Deleted = true;
return Info;
};
- auto InaccessibleDiag = [&](PostfixOperatorStep Step) {
+ auto InaccessibleDiag = [&](unsigned Step) {
return Diagnose == DiagnoseKind::ExplainDeleted
? S.PDiag(diag::note_defaulted_postfix_operator_inaccessible)
- << FD << (int)Step
+ << FD << Step
: S.PDiag();
};
- auto NoteCallsDeleted = [&](PostfixOperatorStep Step, FunctionDecl *Callee) {
+ auto NoteCallsDeleted = [&](unsigned Step, FunctionDecl *Callee) {
if (Diagnose != DiagnoseKind::ExplainDeleted)
return;
S.Diag(Loc, diag::note_defaulted_postfix_operator_calls_deleted)
- << FD << (int)Step;
+ << FD << Step;
if (Callee)
S.NoteDeletedFunction(Callee);
};
- auto CheckConstexpr = [&](const FunctionDecl *Callee,
- PostfixOperatorStep Step) {
+ auto CheckConstexpr = [&](const FunctionDecl *Callee, unsigned Step) {
if (Callee->isConstexpr())
return;
Info.Constexpr = false;
if (Diagnose == DiagnoseKind::ExplainConstexpr)
S.Diag(Callee->getLocation(),
diag::note_defaulted_postfix_operator_not_constexpr_here)
- << (int)Step;
+ << Step;
};
// C++2d [over.inc.default]p2:
@@ -9626,7 +9613,7 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
OverloadCandidateSet::iterator Best;
switch (OR) {
case OR_Deleted:
- NoteCallsDeleted(PostfixOperatorStep::Constructor,
+ NoteCallsDeleted(diag::DefaultedPostfixOperatorStep::Constructor,
Cands.BestViableFunction(S, Loc, Best) == OR_Deleted
? Best->Function
: nullptr);
@@ -9655,21 +9642,23 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
continue;
if (!S.isMemberAccessibleForDeletion(
RD, Step.Function.FoundDecl, C, Loc,
- InaccessibleDiag(PostfixOperatorStep::Constructor)))
+ InaccessibleDiag(
+ diag::DefaultedPostfixOperatorStep::Constructor)))
return Deleted();
- CheckConstexpr(Step.Function.Function, PostfixOperatorStep::Constructor);
+ CheckConstexpr(Step.Function.Function,
+ diag::DefaultedPostfixOperatorStep::Constructor);
}
// -- C has a destructor that is deleted or inaccessible from the context
// of the function-body, or
if (CXXDestructorDecl *Dtor = S.LookupDestructor(RD)) {
if (Dtor->isDeleted()) {
- NoteCallsDeleted(PostfixOperatorStep::Destructor, Dtor);
+ NoteCallsDeleted(diag::DefaultedPostfixOperatorStep::Destructor, Dtor);
return Deleted();
}
if (!S.isMemberAccessibleForDeletion(
RD, DeclAccessPair::make(Dtor, Dtor->getAccess()), C, Loc,
- InaccessibleDiag(PostfixOperatorStep::Destructor)))
+ InaccessibleDiag(diag::DefaultedPostfixOperatorStep::Destructor)))
return Deleted();
}
}
@@ -9678,9 +9667,9 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
// postfix increment operator function or --c for a postfix decrement
// operator function does not result in a usable candidate.
bool IsIncrement = Kind == PostfixOperatorKind::Increment;
- PostfixOperatorStep PrefixStep = IsIncrement
- ? PostfixOperatorStep::PrefixIncrement
- : PostfixOperatorStep::PrefixDecrement;
+ unsigned PrefixStep =
+ IsIncrement ? diag::DefaultedPostfixOperatorStep::PrefixIncrement
+ : diag::DefaultedPostfixOperatorStep::PrefixDecrement;
UnresolvedSet<16> Fns;
if (auto *DFI = FD->getDefaultedOrDeletedInfo())
Fns.assign(DFI->getUnqualifiedLookups().begin(),
@@ -9705,7 +9694,7 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
Best->Conversions[0].isUserDefined()) {
// A built-in candidate, reached through a conversion function.
CheckConstexpr(Best->Conversions[0].UserDefined.ConversionFunction,
- PostfixOperatorStep::ConversionFunction);
+ diag::DefaultedPostfixOperatorStep::ConversionFunction);
}
break;
@@ -19587,13 +19576,12 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) {
if (!Dcl || Dcl->isInvalidDecl())
return;
- // The %select index for err_default_special_members: which kinds of
- // functions can be defaulted in the current language mode.
- unsigned DefaultableKinds = 0;
+ // Which kinds of functions can be defaulted in the current language mode.
+ unsigned DefaultableKinds = diag::DefaultableFunctionKinds::SpecialMembers;
if (getLangOpts().CPlusPlus29)
- DefaultableKinds = 2;
+ DefaultableKinds = diag::DefaultableFunctionKinds::PostfixOperators;
else if (getLangOpts().CPlusPlus20)
- DefaultableKinds = 1;
+ DefaultableKinds = diag::DefaultableFunctionKinds::Comparisons;
auto *FD = dyn_cast<FunctionDecl>(Dcl);
if (!FD) {
>From 0a8d6e5780db3522ef42d2a526c484edf23b6a82 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 05:24:49 +0300
Subject: [PATCH 10/18] clarify lvalue comment
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9b60451d0de3a0..9b144de09734f9 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9558,7 +9558,9 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
// context equivalent to its function-body.
Sema::ContextRAII SavedContext(S, FD);
- // Let c be an lvalue of type C.
+ // Let c be an lvalue of type C. c stands for the first parameter (or the
+ // implicit object), which is always an lvalue when named in the function
+ // body, even if it is declared as an rvalue reference.
OpaqueValueExpr Obj(Loc, C, VK_LValue);
Expr *Args[] = {&Obj};
>From a48b32fe5ce22048fc50c35d1ffb64446ed7365e Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 05:26:28 +0300
Subject: [PATCH 11/18] clarify 0
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9b144de09734f9..82a930f1280596 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9624,7 +9624,7 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
Cands.NoteCandidates(
PartialDiagnosticAt(
Loc, S.PDiag(diag::note_defaulted_postfix_operator_ambiguous)
- << FD << 0 << C),
+ << FD << /*copy construction*/ 0 << C),
S, OCD_AmbiguousCandidates, Args, /*Opc=*/"", Loc);
break;
case OR_Success:
@@ -9632,7 +9632,7 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
Cands.NoteCandidates(
PartialDiagnosticAt(
Loc, S.PDiag(diag::note_defaulted_postfix_operator_no_viable)
- << FD << 0 << C),
+ << FD << /*copy construction*/ 0 << C),
S, OCD_AllCandidates, Args, /*Opc=*/"", Loc);
break;
}
>From f4a3ec1e917dc4aec1486e6f8c91f44be26b84e1 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 05:29:34 +0300
Subject: [PATCH 12/18] Reintroduce OR_Success case in
analyzeDefaultedPostfixOperator with llvm_unreachable for initialization
sequence validation.
---
clang/lib/Sema/SemaDeclCXX.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 82a930f1280596..cb940edebdf29d 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9627,7 +9627,6 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
<< FD << /*copy construction*/ 0 << C),
S, OCD_AmbiguousCandidates, Args, /*Opc=*/"", Loc);
break;
- case OR_Success:
case OR_No_Viable_Function:
Cands.NoteCandidates(
PartialDiagnosticAt(
@@ -9635,6 +9634,8 @@ analyzeDefaultedPostfixOperator(Sema &S, FunctionDecl *FD,
<< FD << /*copy construction*/ 0 << C),
S, OCD_AllCandidates, Args, /*Opc=*/"", Loc);
break;
+ case OR_Success:
+ llvm_unreachable("initialization sequence did not fail");
}
}
return Deleted();
>From 89dfa402f700016b13eab54c58adfcbe8440c303 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 05:45:47 +0300
Subject: [PATCH 13/18] Enhance comment clarity in
CheckExplicitlyDefaultedPostfixOperator regarding lookup scope and its
equivalence to function-body context.
---
clang/lib/Sema/SemaDeclCXX.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index cb940edebdf29d..e3fb032b091a31 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9827,7 +9827,14 @@ bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
// Name lookups and access checks in the implicit definition of a defaulted
// postfix increment or decrement operator function are performed from a
// context equivalent to its function-body.
+ // The lookup is performed from S, not from CurContext. S is either the body
+ // scope of an out-of-line definition, or the scope of the class in which the
+ // function is defaulted on its first declaration; for a lookup that ignores
+ // member functions, both are equivalent to the function-body.
if (S) {
+ assert((S->getEntity() == FD ||
+ S->getEntity() == FD->getLexicalDeclContext()) &&
+ "lookup scope is not equivalent to the function-body");
UnresolvedSet<16> Operators;
LookupOverloadedOperatorName(IsIncrement ? OO_PlusPlus : OO_MinusMinus, S,
Operators);
>From ba61f4df39516a3c70bdb2d7556eae16f143ba67 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 06:25:44 +0300
Subject: [PATCH 14/18] Rewrite logic for determining C type
---
.../clang/Basic/DiagnosticSemaKinds.td | 3 +
clang/lib/Sema/SemaDeclCXX.cpp | 107 +++++++++++-------
.../over.inc/over.inc.default/p1.cpp | 13 ++-
3 files changed, 76 insertions(+), 47 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d5917d12857da7..4d06e8cd763eba 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10700,6 +10700,9 @@ def err_defaulted_postfix_operator_param_unknown : Error<
def err_defaulted_postfix_operator_return_type : Error<
"return type for defaulted %sub{select_defaulted_postfix_operator_kind}0 "
"must be %1, not %2">;
+def err_defaulted_postfix_operator_deduced_return_type : Error<
+ "defaulted %sub{select_defaulted_postfix_operator_kind}0 cannot have a "
+ "deduced return type">;
def err_defaulted_postfix_operator_incomplete : Error<
"cannot default %sub{select_defaulted_postfix_operator_kind}0 for "
"incomplete type %1">;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e3fb032b091a31..5a3cec3094d43c 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9857,54 +9857,73 @@ bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
QualType FirstParamType = getPostfixOperatorFirstParamType(FD);
QualType ReturnType = FD->getDeclaredReturnType();
- // Determine the type C. For a member function, this is the enclosing class.
- // For a non-member function, infer it from the first parameter, falling back
- // to the return type if the first parameter is not a reference.
- QualType C;
- if (MD)
- C = Context.getCanonicalTagType(MD->getParent());
- else if (FirstParamType->isReferenceType())
- C = FirstParamType.getNonReferenceType().getUnqualifiedType();
- else if (!ReturnType->isReferenceType())
- C = ReturnType.getUnqualifiedType();
- if (C.isNull() || (!MD && !C->isDependentType() && !C->isRecordType() &&
- !C->isEnumeralType())) {
- Diag(FirstParam->getBeginLoc(),
- diag::err_defaulted_postfix_operator_param_unknown)
- << (int)Kind << FirstParamType << FirstParam->getSourceRange();
+ // Diagnose an invalid first parameter. ExpectedC is the type that the
+ // parameter should be a reference to, if that type is known.
+ auto DiagnoseFirstParam = [&](QualType ExpectedC) {
+ if (MD && MD->isImplicitObjectMemberFunction()) {
+ // The implicit object parameter is always a reference to the enclosing
+ // class, so the only possible problem is a const qualifier.
+ Diag(MD->getLocation(), diag::err_defaulted_postfix_operator_const)
+ << (int)Kind;
+ } else if (ExpectedC.isNull()) {
+ Diag(FirstParam->getBeginLoc(),
+ diag::err_defaulted_postfix_operator_param_unknown)
+ << (int)Kind << FirstParamType << FirstParam->getSourceRange();
+ } else {
+ Diag(FirstParam->getBeginLoc(),
+ diag::err_defaulted_postfix_operator_param)
+ << (int)Kind << !MD << FirstParamType
+ << Context.getLValueReferenceType(ExpectedC)
+ << Context.getLValueReferenceType(ExpectedC.withVolatile())
+ << FirstParam->getSourceRange();
+ }
return true;
- }
+ };
- // Check the type of the first parameter.
- if (!FirstParamType->isDependentType() && !C->isDependentType()) {
- QualType Referent = FirstParamType->isReferenceType()
- ? FirstParamType.getNonReferenceType()
- : QualType();
- if (Referent.isNull() || Referent.isConstQualified() ||
- !Context.hasSameType(Referent.getUnqualifiedType(), C)) {
- if (MD && MD->isImplicitObjectMemberFunction()) {
- // The implicit object parameter is always a reference to the class,
- // so the only possible problem is a const qualifier.
- Diag(MD->getLocation(), diag::err_defaulted_postfix_operator_const)
- << (int)Kind;
- } else {
- Diag(FirstParam->getBeginLoc(),
- diag::err_defaulted_postfix_operator_param)
- << (int)Kind << !MD << FirstParamType
- << Context.getLValueReferenceType(C)
- << Context.getLValueReferenceType(C.withVolatile())
- << FirstParam->getSourceRange();
- }
- return true;
+ // Check the type of the first parameter and determine the type C from it.
+ // If the parameter type is dependent, C is not known until instantiation.
+ QualType C;
+ if (!FirstParamType->isDependentType()) {
+ QualType EnclosingClass =
+ MD ? Context.getCanonicalTagType(MD->getParent()) : QualType();
+
+ // The first parameter must be a reference to C.
+ if (!FirstParamType->isReferenceType())
+ return DiagnoseFirstParam(EnclosingClass);
+ QualType Referent = FirstParamType.getNonReferenceType();
+ C = Referent.getUnqualifiedType();
+
+ // For a member function, C is the enclosing class. For a non-member
+ // function, C must be a class or enumeration type.
+ if (MD) {
+ if (!Context.hasSameType(C, EnclosingClass))
+ return DiagnoseFirstParam(EnclosingClass);
+ } else if (!C->isRecordType() && !C->isEnumeralType()) {
+ return DiagnoseFirstParam(QualType());
}
+
+ // The referent may be volatile-qualified, but not const-qualified.
+ if (Referent.isConstQualified())
+ return DiagnoseFirstParam(C);
+ }
+
+ // A deduced return type is never permitted, so diagnose it even if C is not
+ // known yet. Allowing it would need P2952R2 (auto& operator=(X&&) = default),
+ // which is not part of the standard.
+ if (ReturnType->getContainedDeducedType()) {
+ Diag(FD->getLocation(),
+ diag::err_defaulted_postfix_operator_deduced_return_type)
+ << (int)Kind << FD->getReturnTypeSourceRange();
+ return true;
}
- // Check the declared return type. A placeholder type is not permitted; this
- // would need P2952R2 (auto& operator=(X&&) = default), which is not yet
- // part of the standard.
- if (ReturnType->getContainedDeducedType() ||
- (!ReturnType->isDependentType() && !C->isDependentType() &&
- !Context.hasSameType(ReturnType, C))) {
+ // If C is not known or the return type is dependent, defer the remaining
+ // checks until instantiation.
+ if (C.isNull() || ReturnType->isDependentType())
+ return false;
+
+ // -- has a declared return type of C.
+ if (!Context.hasSameType(ReturnType, C)) {
Diag(FD->getLocation(), diag::err_defaulted_postfix_operator_return_type)
<< (int)Kind << C << ReturnType << FD->getReturnTypeSourceRange();
return true;
@@ -9912,7 +9931,7 @@ bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
// For a defaulted function in a dependent context, defer all remaining
// checks until instantiation.
- if (C->isDependentType() || FD->isDependentContext())
+ if (FD->isDependentContext())
return false;
// -- is defined as defaulted in C or in a context where C is complete
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
index 86a2620fbd7944..c970eb122da502 100644
--- a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
@@ -76,11 +76,11 @@ struct ReturnsConst {
};
struct ReturnsAuto {
ReturnsAuto &operator++();
- auto operator++(int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'ReturnsAuto', not 'auto'}}
+ auto operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
};
struct ReturnsDecltypeAuto {
ReturnsDecltypeAuto &operator--();
- decltype(auto) operator--(int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'ReturnsDecltypeAuto', not 'decltype(auto)'}}
+ decltype(auto) operator--(int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
};
struct ReturnsInt {
ReturnsInt &operator++();
@@ -110,7 +110,7 @@ NV operator++(volatile NV &, int) = default; // OK
struct NConst { NConst &operator++(); };
NConst operator++(const NConst &, int) = default; // expected-error {{invalid first parameter type for defaulted postfix increment operator; found 'const NConst &', expected 'NConst &' or 'volatile NConst &'}}
struct NValue { NValue &operator++(); };
-NValue operator++(NValue, int) = default; // expected-error {{invalid first parameter type for defaulted postfix increment operator; found 'NValue', expected 'NValue &' or 'volatile NValue &'}}
+NValue operator++(NValue, int) = default; // expected-error {{invalid first parameter type for defaulted non-member postfix increment operator; found 'NValue', expected reference to a non-const class or enumeration type}}
struct NRet { NRet &operator--(); };
int operator--(NRet &, int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'NRet', not 'int'}}
struct NMismatch { NMismatch &operator++(); };
@@ -203,3 +203,10 @@ template <typename T> struct DependentFriend {
friend int operator--(T &, int) = default; // expected-error {{return type for defaulted postfix decrement operator must be 'S', not 'int'}}
};
DependentFriend<S> df1; // expected-note {{in instantiation of template class 'DependentFriend<S>' requested here}}
+
+// A deduced return type is never valid, so it is diagnosed even if C is not
+// known yet.
+template <typename T> struct DependentAuto {
+ friend auto operator++(T &, int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+ auto operator--(this T &, int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
+};
>From 209f61da98b930d1b91ef6959c9103fba1d6d56b Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 06:27:06 +0300
Subject: [PATCH 15/18] Fix logic for determining the first declaration in
CheckExplicitlyDefaultedPostfixOperator by using getFirstDecl instead of
getCanonicalDecl.
---
clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 5a3cec3094d43c..f768fceb7dc998 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9944,7 +9944,7 @@ bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
DefaultedPostfixOperatorInfo Info = analyzeDefaultedPostfixOperator(
*this, FD, Kind, C, PostfixOperatorDiagnoseKind::NoDiagnostics);
- bool First = FD == FD->getCanonicalDecl();
+ bool First = FD == FD->getFirstDecl();
if (!First) {
if (Info.Deleted) {
>From a7a446917ac31fd7cac9dfcb90944e2f75072e1c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 06:37:42 +0300
Subject: [PATCH 16/18] Refactor defaulted function handling by introducing a
helper to define functions with synthesized bodies. Update exception
specification computation to utilize this new helper for better code clarity
and maintainability.
---
clang/lib/Sema/SemaDeclCXX.cpp | 140 ++++++++++++++-------------------
1 file changed, 57 insertions(+), 83 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f768fceb7dc998..c5844d3fdfd9a6 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9412,65 +9412,80 @@ void Sema::DeclareImplicitEqualityComparison(CXXRecordDecl *RD,
popCodeSynthesisContext();
}
-void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
- DefaultedComparisonKind DCK) {
+/// Define a defaulted function whose body is synthesized by \p BuildBody: a
+/// defaulted comparison operator function or a defaulted postfix increment or
+/// decrement operator function.
+static void defineDefaultedFunctionWithSynthesizedBody(
+ Sema &S, SourceLocation UseLoc, FunctionDecl *FD,
+ llvm::function_ref<StmtResult(SourceLocation BodyLoc)> BuildBody) {
assert(FD->isDefaulted() && !FD->isDeleted() &&
!FD->doesThisDeclarationHaveABody());
if (FD->willHaveBody() || FD->isInvalidDecl())
return;
- SynthesizedFunctionScope Scope(*this, FD);
+ Sema::SynthesizedFunctionScope Scope(S, FD);
// Add a context note for diagnostics produced after this point.
Scope.addContextNote(UseLoc);
- DefaultedFunctionFPFeaturesRAII RestoreFP(*this, FD);
+ DefaultedFunctionFPFeaturesRAII RestoreFP(S, FD);
{
// Build and set up the function body.
- // The first parameter has type maybe-ref-to maybe-const T, use that to get
- // the type of the class being compared.
- auto PT = FD->getParamDecl(0)->getType();
- CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
SourceLocation BodyLoc =
FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
- StmtResult Body =
- DefaultedComparisonSynthesizer(*this, RD, FD, DCK, BodyLoc).build();
+ StmtResult Body = BuildBody(BodyLoc);
if (Body.isInvalid()) {
FD->setInvalidDecl();
return;
}
FD->setBody(Body.get());
- FD->markUsed(Context);
+ FD->markUsed(S.Context);
}
// The exception specification is needed because we are defining the
// function. Note that this will reuse the body we just built.
- ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
+ S.ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
- if (ASTMutationListener *L = getASTMutationListener())
+ if (ASTMutationListener *L = S.getASTMutationListener())
L->CompletedImplicitDefinition(FD);
}
+void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
+ DefaultedComparisonKind DCK) {
+ defineDefaultedFunctionWithSynthesizedBody(
+ *this, UseLoc, FD, [&](SourceLocation BodyLoc) {
+ // The first parameter has type maybe-ref-to maybe-const T, use that to
+ // get the type of the class being compared.
+ auto PT = FD->getParamDecl(0)->getType();
+ CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
+ return DefaultedComparisonSynthesizer(*this, RD, FD, DCK, BodyLoc)
+ .build();
+ });
+}
+
+/// Compute the exception specification of a defaulted function whose body is
+/// synthesized by \p BuildBody: a defaulted comparison operator function or a
+/// defaulted postfix increment or decrement operator function.
static Sema::ImplicitExceptionSpecification
-ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
- FunctionDecl *FD,
- DefaultedComparisonKind DCK) {
+computeExceptionSpecFromSynthesizedBody(
+ Sema &S, SourceLocation Loc, FunctionDecl *FD,
+ llvm::function_ref<StmtResult(SourceLocation BodyLoc)> BuildBody) {
ComputingExceptionSpec CES(S, FD, Loc);
Sema::ImplicitExceptionSpecification ExceptSpec(S);
if (FD->isInvalidDecl())
return ExceptSpec;
- // The common case is that we just defined the comparison function. In that
- // case, just look at whether the body can throw.
+ // The common case is that we just defined the function. In that case, just
+ // look at whether the body can throw.
if (Stmt *FunctionBody = FD->getBody()) {
ExceptSpec.CalledStmt(FunctionBody);
} else {
// Otherwise, build a body so we can check it. This should ideally only
// happen when we're not actually marking the function referenced. (This is
// only really important for efficiency: we don't want to build and throw
- // away bodies for comparison functions more than we strictly need to.)
+ // away bodies for defaulted functions more than we strictly need to.)
// Pretend to synthesize the function body in an unevaluated context.
// Note that we can't actually just go ahead and define the function here:
@@ -9479,14 +9494,9 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
EnterExpressionEvaluationContext Context(
S, Sema::ExpressionEvaluationContext::Unevaluated);
- CXXRecordDecl *RD =
- cast<CXXRecordDecl>(FD->getFriendObjectKind() == Decl::FOK_None
- ? FD->getDeclContext()
- : FD->getLexicalDeclContext());
SourceLocation BodyLoc =
FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
- StmtResult Body =
- DefaultedComparisonSynthesizer(S, RD, FD, DCK, BodyLoc).build();
+ StmtResult Body = BuildBody(BodyLoc);
if (!Body.isInvalid())
ExceptSpec.CalledStmt(Body.get());
@@ -9499,6 +9509,20 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
return ExceptSpec;
}
+static Sema::ImplicitExceptionSpecification
+ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
+ FunctionDecl *FD,
+ DefaultedComparisonKind DCK) {
+ return computeExceptionSpecFromSynthesizedBody(
+ S, Loc, FD, [&](SourceLocation BodyLoc) {
+ CXXRecordDecl *RD =
+ cast<CXXRecordDecl>(FD->getFriendObjectKind() == Decl::FOK_None
+ ? FD->getDeclContext()
+ : FD->getLexicalDeclContext());
+ return DefaultedComparisonSynthesizer(S, RD, FD, DCK, BodyLoc).build();
+ });
+}
+
//===----------------------------------------------------------------------===//
// C++2d [over.inc.default]: defaulted postfix increment and decrement operator
// functions (P3668R4).
@@ -10060,70 +10084,20 @@ bool Sema::CheckExplicitlyDefaultedPostfixOperator(Scope *S, FunctionDecl *FD,
void Sema::DefineDefaultedPostfixOperator(SourceLocation UseLoc,
FunctionDecl *FD,
PostfixOperatorKind Kind) {
- assert(FD->isDefaulted() && !FD->isDeleted() &&
- !FD->doesThisDeclarationHaveABody());
- if (FD->willHaveBody() || FD->isInvalidDecl())
- return;
-
- SynthesizedFunctionScope Scope(*this, FD);
-
- // Add a context note for diagnostics produced after this point.
- Scope.addContextNote(UseLoc);
-
- DefaultedFunctionFPFeaturesRAII RestoreFP(*this, FD);
-
- {
- // Build and set up the function body.
- SourceLocation BodyLoc =
- FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
- StmtResult Body =
- buildDefaultedPostfixOperatorBody(*this, FD, Kind, BodyLoc);
- if (Body.isInvalid()) {
- FD->setInvalidDecl();
- return;
- }
- FD->setBody(Body.get());
- FD->markUsed(Context);
- }
-
- // The exception specification is needed because we are defining the
- // function. Note that this will reuse the body we just built.
- ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
-
- if (ASTMutationListener *L = getASTMutationListener())
- L->CompletedImplicitDefinition(FD);
+ defineDefaultedFunctionWithSynthesizedBody(
+ *this, UseLoc, FD, [&](SourceLocation BodyLoc) {
+ return buildDefaultedPostfixOperatorBody(*this, FD, Kind, BodyLoc);
+ });
}
static Sema::ImplicitExceptionSpecification
ComputeDefaultedPostfixOperatorExceptionSpec(Sema &S, SourceLocation Loc,
FunctionDecl *FD,
PostfixOperatorKind Kind) {
- ComputingExceptionSpec CES(S, FD, Loc);
- Sema::ImplicitExceptionSpecification ExceptSpec(S);
-
- if (FD->isInvalidDecl())
- return ExceptSpec;
-
- // The common case is that we just defined the function. In that case, just
- // look at whether the body can throw.
- if (Stmt *FunctionBody = FD->getBody()) {
- ExceptSpec.CalledStmt(FunctionBody);
- } else {
- // Otherwise, build a body so we can check it. Pretend to synthesize the
- // function body in an unevaluated context: we are not permitted to mark
- // its callees as referenced.
- Sema::SynthesizedFunctionScope Scope(S, FD);
- EnterExpressionEvaluationContext Context(
- S, Sema::ExpressionEvaluationContext::Unevaluated);
-
- SourceLocation BodyLoc =
- FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
- StmtResult Body = buildDefaultedPostfixOperatorBody(S, FD, Kind, BodyLoc);
- if (!Body.isInvalid())
- ExceptSpec.CalledStmt(Body.get());
- }
-
- return ExceptSpec;
+ return computeExceptionSpecFromSynthesizedBody(
+ S, Loc, FD, [&](SourceLocation BodyLoc) {
+ return buildDefaultedPostfixOperatorBody(S, FD, Kind, BodyLoc);
+ });
}
void Sema::CheckDelayedMemberExceptionSpecs() {
>From 9ceae7c9d5efa73793e2a3b9c645e203a471c33f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 06:45:20 +0300
Subject: [PATCH 17/18] Merge `err_defaulted_comparison_template` and
`err_defaulted_postfix_operator_template`
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 8 +++-----
clang/lib/Sema/SemaDeclCXX.cpp | 10 +++-------
2 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 4d06e8cd763eba..67b6051163c534 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10577,8 +10577,9 @@ def note_vbase_moved_here : Note<
def select_defaulted_comparison_kind : TextSubstitution<
"%select{<ERROR>|equality|three-way|equality|relational}0 comparison "
"operator">;
-def err_defaulted_comparison_template : Error<
- "comparison operator template cannot be defaulted">;
+def err_defaulted_operator_template : Error<
+ "%select{comparison operator|%sub{select_defaulted_postfix_operator_kind}1}0 "
+ "template cannot be defaulted">;
def err_defaulted_comparison_num_args : Error<
"%select{non-member|member}0 %sub{select_defaulted_comparison_kind}1"
" must have %select{2|1}0 parameters">;
@@ -10683,9 +10684,6 @@ def select_defaulted_postfix_operator_kind : TextSubstitution<
"%select{<ERROR>|postfix increment|postfix decrement}0 operator">;
def err_defaulted_prefix_operator : Error<
"only the postfix form of 'operator%select{++|--}0' can be defaulted">;
-def err_defaulted_postfix_operator_template : Error<
- "%sub{select_defaulted_postfix_operator_kind}0 template cannot be "
- "defaulted">;
def err_defaulted_postfix_operator_const : Error<
"defaulted member %sub{select_defaulted_postfix_operator_kind}0 cannot be "
"const-qualified">;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c5844d3fdfd9a6..6341d2690ca917 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -19591,13 +19591,9 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) {
if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Dcl)) {
FunctionDecl::DefaultedFunctionKind DFK =
FTD->getTemplatedDecl()->getDefaultedFunctionKind();
- if (DFK.isComparison()) {
- Diag(DefaultLoc, diag::err_defaulted_comparison_template);
- return;
- }
- if (DFK.isPostfixOperator()) {
- Diag(DefaultLoc, diag::err_defaulted_postfix_operator_template)
- << (int)DFK.asPostfixOperator();
+ if (DFK.isComparison() || DFK.isPostfixOperator()) {
+ Diag(DefaultLoc, diag::err_defaulted_operator_template)
+ << DFK.isPostfixOperator() << (int)DFK.asPostfixOperator();
return;
}
}
>From 5bd54fd9b1747929713c9e57dca07e0578eded1f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 8 Oct 2026 06:51:21 +0300
Subject: [PATCH 18/18] Add various defaulted postfix operator overloads with
restrictions and error checks
Introduce new structures demonstrating defaulted postfix increment and decrement operators with various qualifiers such as `__restrict__`, `volatile`, and `const`. Include error checks for invalid return types and parameter types in defaulted operators. Enhance the handling of placeholder types and clarify the rules around defaulted operators in the context of C++ concepts and templates.
---
.../over.inc/over.inc.default/p1.cpp | 157 ++++++++++++++++++
1 file changed, 157 insertions(+)
diff --git a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
index c970eb122da502..f7d78556883b11 100644
--- a/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
+++ b/clang/test/CXX/over/over.oper/over.inc/over.inc.default/p1.cpp
@@ -39,6 +39,34 @@ struct RefQualified {
RefQualified operator++(int) && = default; // OK, an rvalue reference is a "reference to C"
};
+// __restrict__ is not part of the type of the implicit object parameter.
+struct Restrict {
+ Restrict &operator++();
+ Restrict operator++(int) __restrict__ = default; // OK
+};
+struct RestrictBoth {
+ RestrictBoth &operator--() __restrict__;
+ RestrictBoth operator--(int) __restrict__ = default; // OK
+};
+struct RestrictRvalue {
+ RestrictRvalue &operator++();
+ RestrictRvalue operator++(int) __restrict__ && = default; // OK
+};
+struct RestrictVolatile {
+ RestrictVolatile &operator++() volatile;
+ RestrictVolatile(const volatile RestrictVolatile &);
+ RestrictVolatile(RestrictVolatile &&);
+ RestrictVolatile operator++(int) volatile __restrict__ = default; // OK
+};
+struct RestrictConst {
+ RestrictConst &operator++();
+ RestrictConst operator++(int) const __restrict__ = default; // expected-error {{defaulted member postfix increment operator cannot be const-qualified}}
+};
+struct ExplicitRestrict {
+ ExplicitRestrict &operator++();
+ ExplicitRestrict operator++(this ExplicitRestrict &__restrict__, int) = default; // OK
+};
+
// Explicit object parameters.
struct Explicit {
Explicit &operator++();
@@ -91,6 +119,33 @@ struct ReturnsAlias {
Self &operator++();
Self operator++(int) = default; // OK
};
+struct ReturnsTrailing {
+ ReturnsTrailing &operator++();
+ auto operator++(int) -> ReturnsTrailing = default; // OK, not a deduced return type
+};
+
+// A placeholder type is not C, even if deduction would produce C.
+template <typename T> concept Any = true;
+struct ReturnsConstrainedAuto {
+ ReturnsConstrainedAuto &operator++();
+ Any auto operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+};
+struct ReturnsAutoPointer {
+ ReturnsAutoPointer &operator++();
+ auto *operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+};
+struct ReturnsAutoRef {
+ ReturnsAutoRef &operator--();
+ auto &operator--(int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
+};
+struct ReturnsConstAuto {
+ ReturnsConstAuto &operator++();
+ const auto operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+};
+struct ReturnsConstrainedAutoRef {
+ ReturnsConstrainedAutoRef &operator++();
+ const Any auto &operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+};
// Non-member functions.
struct N {
@@ -117,6 +172,16 @@ struct NMismatch { NMismatch &operator++(); };
S operator++(NMismatch &, int) = default; // expected-error {{return type for defaulted postfix increment operator must be 'NMismatch', not 'S'}}
struct NUnknown { NUnknown &operator++(); };
S &operator++(NUnknown, int) = default; // expected-error {{invalid first parameter type for defaulted non-member postfix increment operator; found 'NUnknown', expected reference to a non-const class or enumeration type}}
+struct NAuto {
+ NAuto &operator++();
+ NAuto &operator--();
+};
+auto operator++(NAuto &, int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+Any auto operator--(NAuto &, int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
+decltype(auto) operator++(NAuto &&, int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+auto operator--(NAuto &&, int) -> NAuto = default; // OK
+struct NRestrict { NRestrict &operator++(); };
+NRestrict operator++(NRestrict &__restrict__, int) = default; // OK
// Enumerations.
enum E { e };
@@ -171,6 +236,44 @@ struct Template {
};
template <typename T = void> S operator--(S &, int) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+// A parameter declared with a placeholder type makes the function an
+// abbreviated function template.
+struct AutoFirst {
+ AutoFirst &operator++();
+ AutoFirst &operator--();
+ AutoFirst operator++(this auto &, int) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+ friend AutoFirst operator--(auto &, int) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+};
+AutoFirst operator++(Any auto &, int) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+AutoFirst operator--(auto &&, int) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+
+// The second parameter of a postfix operator always has type int.
+struct AutoSecond {
+ AutoSecond &operator++();
+ AutoSecond &operator--();
+ AutoSecond operator++(auto) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+ AutoSecond operator--(this AutoSecond &, auto) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+};
+AutoSecond operator++(AutoSecond &&, Any auto) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+struct NotInt {
+ NotInt &operator++();
+ NotInt &operator--();
+ NotInt operator++(long) = default; // expected-error {{parameter of overloaded post-increment operator must have type 'int' (not 'long')}}
+ NotInt operator--(this NotInt &, unsigned) = default; // expected-error {{parameter of overloaded post-decrement operator must have type 'int' (not 'unsigned int')}}
+};
+NotInt operator++(NotInt &&, char) = default; // expected-error {{parameter of overloaded post-increment operator must have type 'int' (not 'char')}}
+struct IntSpellings {
+ using Int = int;
+ IntSpellings &operator++();
+ IntSpellings &operator--();
+ IntSpellings operator++(const int) = default; // OK
+ IntSpellings operator--(Int) = default; // OK
+};
+struct DefaultArgument {
+ DefaultArgument &operator++();
+ DefaultArgument operator++(int = 0) = default; // expected-error {{parameter of overloaded 'operator++' cannot have a default argument}}
+};
+
// Only the postfix forms can be defaulted.
struct Prefix {
Prefix &operator++() = default; // expected-error {{only the postfix form of 'operator++' can be defaulted}}
@@ -210,3 +313,57 @@ template <typename T> struct DependentAuto {
friend auto operator++(T &, int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
auto operator--(this T &, int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
};
+
+template <typename T> struct DependentDeducedReturn {
+ DependentDeducedReturn &operator++();
+ DependentDeducedReturn &operator--();
+ auto operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+ Any auto operator--(int) = default; // expected-error {{defaulted postfix decrement operator cannot have a deduced return type}}
+};
+template <typename T> struct DependentDeducedReturn2 {
+ DependentDeducedReturn2 &operator++();
+ DependentDeducedReturn2 &operator--();
+ decltype(auto) operator++(int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+ friend T *operator--(DependentDeducedReturn2 &, int) = default; // OK, checked when instantiated
+};
+template <typename T> struct DependentConstrainedFriend {
+ DependentConstrainedFriend &operator++();
+ friend Any auto operator++(DependentConstrainedFriend &, int) = default; // expected-error {{defaulted postfix increment operator cannot have a deduced return type}}
+};
+
+// A trailing return type that names C is not deduced.
+template <typename T> struct DependentTrailing {
+ DependentTrailing &operator++();
+ auto operator++(int) -> T = default; // expected-error {{return type for defaulted postfix increment operator must be 'DependentTrailing<int>', not 'int'}}
+};
+DependentTrailing<int> dt1; // expected-note {{in instantiation of template class 'DependentTrailing<int>' requested here}}
+template <typename T> struct DependentTrailingOK {
+ DependentTrailingOK &operator++();
+ auto operator++(int) -> DependentTrailingOK = default; // OK
+};
+DependentTrailingOK<int> dt2;
+
+// A placeholder parameter makes the function a template even in a class
+// template.
+template <typename T> struct DependentAutoParam {
+ DependentAutoParam &operator++();
+ DependentAutoParam &operator--();
+ DependentAutoParam operator++(this auto &, int) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+ friend DependentAutoParam operator--(auto &, int) = default; // expected-error {{postfix decrement operator template cannot be defaulted}}
+ DependentAutoParam operator++(auto) = default; // expected-error {{postfix increment operator template cannot be defaulted}}
+};
+
+// A dependent second parameter is checked when instantiated.
+template <typename T> struct DependentSecond {
+ DependentSecond &operator++();
+ DependentSecond operator++(T) = default; // expected-error {{parameter of overloaded post-increment operator must have type 'int' (not 'long')}}
+};
+DependentSecond<int> ds1; // OK
+DependentSecond<long> ds2; // expected-note {{in instantiation of template class 'DependentSecond<long>' requested here}}
+
+template <typename T> struct DependentRestrict {
+ DependentRestrict &operator++();
+ DependentRestrict operator++(int) __restrict__ = default; // OK
+ DependentRestrict operator--(int) const __restrict__ = default; // expected-error {{defaulted member postfix decrement operator cannot be const-qualified}}
+};
+DependentRestrict<int> dres1; // expected-note {{in instantiation of template class 'DependentRestrict<int>' requested here}}
More information about the cfe-commits
mailing list