C ObjC Var(warn_declaration_missing_parameter) Warning Init(1)
Warn for missing parameter types in function declarations.
+Wdefaulted-function-deleted
+C++ ObjC++ Var(warn_defaulted_fn_deleted) Init(1) Warning
+Warn when an explicitly defaulted function is deleted.
+
Wdelete-incomplete
C++ ObjC++ Var(warn_delete_incomplete) Init(1) Warning
Warn when deleting a pointer to incomplete type.
for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
if (DECL_DECLARES_FUNCTION_P (fn)
&& !DECL_ARTIFICIAL (fn)
- && DECL_DEFAULTED_IN_CLASS_P (fn))
- {
+ && DECL_DEFAULTED_IN_CLASS_P (fn)
/* ...except handle comparisons later, in finish_struct_1. */
- if (special_function_p (fn) == sfk_comparison)
- continue;
-
- int copy = copy_fn_p (fn);
- if (copy > 0)
- {
- bool imp_const_p
- = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
- : !no_const_asn_ref);
- bool fn_const_p = (copy == 2);
-
- if (fn_const_p && !imp_const_p)
- /* If the function is defaulted outside the class, we just
- give the synthesis error. Core Issue #1331 says this is
- no longer ill-formed, it is defined as deleted instead. */
- DECL_DELETED_FN (fn) = true;
- }
- defaulted_late_check (fn);
+ && special_function_p (fn) != sfk_comparison)
+ {
+ bool imp_const_p
+ = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
+ : !no_const_asn_ref);
+ defaulted_late_check (fn, imp_const_p);
}
if (LAMBDA_TYPE_P (t))
extern bool type_build_dtor_call (tree);
extern void explain_non_literal_class (tree);
extern void inherit_targ_abi_tags (tree);
-extern void defaulted_late_check (tree);
+extern void maybe_delete_defaulted_fn (tree, tree);
+extern void defaulted_late_check (tree, tristate = tristate::unknown ());
extern bool defaultable_fn_check (tree);
extern void check_abi_tags (tree);
extern tree missing_abi_tags (tree);
return fn;
}
+/* Mark an explicitly defaulted function FN as =deleted and warn.
+ IMPLICIT_FN is the corresponding special member function that
+ would have been implicitly declared. */
+
+void
+maybe_delete_defaulted_fn (tree fn, tree implicit_fn)
+{
+ if (DECL_ARTIFICIAL (fn) || !DECL_DEFAULTED_IN_CLASS_P (fn))
+ return;
+
+ DECL_DELETED_FN (fn) = true;
+
+ auto_diagnostic_group d;
+ const special_function_kind kind = special_function_p (fn);
+ tree parmtype
+ = TREE_VALUE (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
+ ? TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))
+ : FUNCTION_FIRST_USER_PARMTYPE (fn));
+ const bool illformed_p
+ /* [dcl.fct.def.default] "if F1 is an assignment operator"... */
+ = (SFK_ASSIGN_P (kind)
+ /* "and the return type of F1 differs from the return type of F2" */
+ && (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
+ TREE_TYPE (TREE_TYPE (implicit_fn)))
+ /* "or F1's non-object parameter type is not a reference,
+ the program is ill-formed" */
+ || !TYPE_REF_P (parmtype)));
+ /* Decide if we want to emit a pedwarn, error, or a warning. */
+ diagnostic_t diag_kind;
+ int opt;
+ if (illformed_p)
+ {
+ diag_kind = DK_ERROR;
+ opt = 0;
+ }
+ else
+ {
+ diag_kind = cxx_dialect >= cxx20 ? DK_WARNING : DK_PEDWARN;
+ opt = OPT_Wdefaulted_function_deleted;
+ }
+
+ /* Don't warn for template instantiations. */
+ if (DECL_TEMPLATE_INSTANTIATION (fn) && diag_kind == DK_WARNING)
+ return;
+
+ const char *wmsg;
+ switch (kind)
+ {
+ case sfk_copy_constructor:
+ wmsg = G_("explicitly defaulted copy constructor is implicitly deleted "
+ "because its declared type does not match the type of an "
+ "implicit copy constructor");
+ break;
+ case sfk_move_constructor:
+ wmsg = G_("explicitly defaulted move constructor is implicitly deleted "
+ "because its declared type does not match the type of an "
+ "implicit move constructor");
+ break;
+ case sfk_copy_assignment:
+ wmsg = G_("explicitly defaulted copy assignment operator is implicitly "
+ "deleted because its declared type does not match the type "
+ "of an implicit copy assignment operator");
+ break;
+ case sfk_move_assignment:
+ wmsg = G_("explicitly defaulted move assignment operator is implicitly "
+ "deleted because its declared type does not match the type "
+ "of an implicit move assignment operator");
+ break;
+ default:
+ gcc_unreachable ();
+ }
+ if (emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), opt, wmsg))
+ inform (DECL_SOURCE_LOCATION (fn),
+ "expected signature: %qD", implicit_fn);
+}
+
/* Gives any errors about defaulted functions which need to be deferred
- until the containing class is complete. */
+ until the containing class is complete. IMP_CONST is false or true
+ if we are called from check_bases_and_members and signals whether
+ the implicit function has a non-object parameter of type const C&. */
void
-defaulted_late_check (tree fn)
+defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
{
/* Complain about invalid signature for defaulted fn. */
tree ctx = DECL_CONTEXT (fn);
}
bool fn_const_p = (copy_fn_p (fn) == 2);
+ /* "if F2 has a non-object parameter of type const C&, the corresponding
+ non-object parameter of F1 may be of type C&." But not the other way
+ around. */
+ if (fn_const_p && imp_const.is_false ())
+ fn_const_p = false;
tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
- NULL, NULL);
+ /*pattern_fn=*/NULL_TREE,
+ /*inherited_parms=*/NULL_TREE);
tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
/* Includes special handling for a default xobj operator. */
if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
TREE_TYPE (TREE_TYPE (implicit_fn)))
|| !compare_fn_params (fn, implicit_fn))
- {
- auto_diagnostic_group d;
- error ("defaulted declaration %q+D does not match the "
- "expected signature", fn);
- inform (DECL_SOURCE_LOCATION (fn),
- "expected signature: %qD", implicit_fn);
- }
+ maybe_delete_defaulted_fn (fn, implicit_fn);
if (DECL_DELETED_FN (implicit_fn))
{
-Wcomma-subscript -Wconditionally-supported
-Wno-conversion-null -Wctad-maybe-unsupported
-Wctor-dtor-privacy -Wdangling-reference
+-Wno-defaulted-function-deleted
-Wno-delete-incomplete
-Wdelete-non-virtual-dtor -Wno-deprecated-array-compare
-Wdeprecated-copy -Wdeprecated-copy-dtor
@item -Wconditionally-supported @r{(C++ and Objective-C++ only)}
Warn for conditionally-supported (C++11 [intro.defs]) constructs.
+@opindex Wdefaulted-function-deleted
+@opindex Wno-defaulted-function-deleted
+@item -Wno-defaulted-function-deleted @r{(C++ and Objective-C++ only)}
+Warn when an explicitly defaulted function is deleted by the compiler.
+That can occur when the function's declared type does not match the type
+of the function that would have been implicitly declared. This warning
+is enabled by default.
+
@opindex Wdelete-incomplete
@opindex Wno-delete-incomplete
@item -Wno-delete-incomplete @r{(C++ and Objective-C++ only)}
struct G: public F
{
- G(const G&) = default;
+ G(const G&) = default; // { dg-error "implicitly deleted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
struct H
template<int> struct A
{
A();
- A(volatile A&) = default; // { dg-error "defaulted" }
+ A(volatile A&) = default; // { dg-error "defaulted" "" { target c++17_down } }
};
struct B
{
W();
// This should now compile and be =deleted.
- W(const W&) = default;
+ W(const W&) = default; // { dg-error "implicitly deleted" "" { target c++17_down } }
T t;
};
struct S
{
- S& operator=(const S&) = default;
+ S& operator=(const S&) = default; // { dg-error "implicitly deleted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
M m;
};
{
W();
W(const W&) = default; // { dg-error "binding" }
+// { dg-error "implicitly deleted" "" { target c++17_down } .-1 }
T t;
};
struct T
{
- constexpr T(volatile T &) = default; // { dg-error "defaulted" }
+ constexpr T(volatile T &) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
struct U
{
- constexpr U(const volatile U &) = default; // { dg-error "defaulted" }
+ constexpr U(const volatile U &) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
struct V
struct T
{
- T& operator=(volatile T &) = default; // { dg-error "defaulted" }
+ T& operator=(volatile T &) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
struct U
{
- U& operator=(const volatile U &) = default; // { dg-error "defaulted" }
+ U& operator=(const volatile U &) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
struct V
{
W() = default;
W& operator=(const W&) = default; // { dg-error "binding" }
+// { dg-error "implicitly deleted" "" { target c++17_down } .-1 }
T t;
};
struct W : public M
{
- W(const W&) = default;
+ W(const W&) = default; // { dg-error "implicitly deleted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+
+struct C0 {
+ C0(C0&) = default;
+};
+
+struct C1 {
+ C1(volatile C1&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+};
+
+struct C2 {
+ C2(const C2&) = default;
+};
+
+struct C3 {
+ C3(const volatile C3&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+};
+
+struct M0 {
+ M0(M0&&) = default;
+};
+
+struct M1 {
+ M1(const M1&&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+};
+
+struct M2 {
+ M2(volatile M2&&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+};
+
+struct M3 {
+ M3(const volatile M3&&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+
+struct M
+{
+ M& operator=(M&&);
+};
+
+struct R
+{
+ R& operator=(R&&) = default;
+ M m;
+};
+
+struct S
+{
+ S& operator=(const S&&) = default; // { dg-warning "implicitly deleted" "" { target c++20 } }
+ // { dg-error "does not match" "" { target c++17_down } .-1 }
+
+ M m;
+};
+
+struct T
+{
+ T operator=(T&&) = default; // { dg-error "defaulted" }
+ M m;
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+
+struct S
+{
+ S& operator=(S &&) = default;
+};
+
+struct T
+{
+ T& operator=(volatile T &&) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
+};
+
+struct U
+{
+ U& operator=(const volatile U &&) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
+};
+
+struct V
+{
+ V& operator=(const V &&) = default; // { dg-error "defaulted" "" { target c++17_down } }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+// Check that there is no -Wdefaulted-function-deleted for template
+// instantiations.
+
+template<typename>
+struct C {
+ C();
+ C(const C&&) = default; // { dg-error "implicitly deleted" "" { target c++17_down} }
+};
+
+struct D {
+ D(const D&&) = default; // { dg-error "implicitly deleted" "" { target c++17_down} }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
+};
+
+struct M {
+ M();
+ // So that W wouldn't have had "const W&" copy ctor if it were
+ // implicitly declared.
+ M(M&);
+};
+
+struct W {
+ W();
+ W(const W&) = default; // { dg-error "implicitly deleted" "" { target c++17_down} }
+ // { dg-warning "implicitly deleted" "" { target c++20 } .-1 }
+ M m;
+};
+
+void
+g ()
+{
+ C<int> c;
+}
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-defaulted-function-deleted" }
+
+struct S
+{
+ S& operator=(S &&) = default;
+};
+
+struct T
+{
+ T& operator=(volatile T &&) = default;
+};
+
+struct U
+{
+ U& operator=(const volatile U &&) = default;
+};
+
+struct V
+{
+ V& operator=(const V &&) = default;
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-defaulted-function-deleted" }
+
+struct C0 {
+ C0(C0&) = default;
+};
+
+struct C1 {
+ C1(volatile C1&) = default;
+};
+
+struct C2 {
+ C2(const C2&) = default;
+};
+
+struct C3 {
+ C3(const volatile C3&) = default;
+};
+
+struct M0 {
+ M0(M0&&) = default;
+};
+
+struct M1 {
+ M1(const M1&&) = default;
+};
+
+struct M2 {
+ M2(volatile M2&&) = default;
+};
+
+struct M3 {
+ M3(const volatile M3&&) = default;
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-defaulted-function-deleted" }
+
+struct M
+{
+ M& operator=(M&);
+};
+
+struct T
+{
+ // if F1 is an assignment operator, and the return type of F1 differs
+ // from the return type, the program is ill-formed.
+ T operator=(T&) = default; // { dg-error "defaulted" }
+ M m;
+};
+
+struct U
+{
+ // if F1's non-object parameter type is not a reference, the program
+ // is ill-formed.
+ U& operator=(U) = default; // { dg-error "defaulted" }
+ M m;
+};
--- /dev/null
+// PR c++/116162
+// { dg-do compile { target c++23 } }
+
+struct M
+{
+ M& operator=(M&);
+};
+
+struct T
+{
+ // if F1 is an assignment operator, and the return type of F1 differs
+ // from the return type, the program is ill-formed.
+ T operator=(this T&, T&) = default; // { dg-error "defaulted" }
+ M m;
+};
+
+struct U
+{
+ // if F1's non-object parameter type is not a reference, the program
+ // is ill-formed.
+ U& operator=(this U&, U) = default; // { dg-error "defaulted" }
+ M m;
+};