Hi!
I've implemented the non-aggregate part of is_implicit_lifetime
paper according to the paper's comment how it can be implemented, i.e.
the std::conjunction from
template<typename T>
struct is_implicit_lifetime : std::disjunction<
std::is_scalar<T>,
std::is_array<T>,
std::is_aggregate<T>,
std::conjunction<
std::is_trivially_destructible<T>,
std::disjunction<
std::is_trivially_default_constructible<T>,
std::is_trivially_copy_constructible<T>,
std::is_trivially_move_constructible<T>>>> {};
in the paper. But as reported in PR122690, the actual wording in the
paper is different from that, the
https://eel.is/c++draft/class.prop#16.2 part of it:
"it has at least one trivial eligible constructor and a trivial,
non-deleted destructor" doesn't talk anything about accessibility
of those ctors or dtors, only triviality, not being deleted and eligibility.
My understanding is that GCC handles the last 2 bullets of
https://eel.is/c++draft/special#6 by not adding ctors ineligible because
of those into the overload at all, and for testing deleted cdtors
I need to lazily declare them in case such synthetization makes them
deleted.
So, this patch first checks for the easy cases (where the flags on the
type say the dtor is non-trivial or all the 3 special member ctors are
non-trivial) and if not, lazily declares them if needed and checks if they
are trivial and non-deleted.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2025-11-19 Jakub Jelinek <[email protected]>
PR c++/122690
* tree.cc (implicit_lifetime_type_p): Don't test is_trivially_xible,
instead try to lazily declare dtor and default, copy and move ctors
if needed and check for their triviality and whether they are
deleted.
* g++.dg/ext/is_implicit_lifetime2.C: New test.
--- gcc/cp/tree.cc.jj 2025-11-15 16:03:45.588512360 +0100
+++ gcc/cp/tree.cc 2025-11-18 11:07:13.996087236 +0100
@@ -4829,18 +4829,24 @@ implicit_lifetime_type_p (tree t)
&& (!CLASSTYPE_DESTRUCTOR (t)
|| !user_provided_p (CLASSTYPE_DESTRUCTOR (t))))
return true;
- if (is_trivially_xible (BIT_NOT_EXPR, t, NULL_TREE))
+ if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
+ return false;
+ if (TYPE_HAS_COMPLEX_DFLT (t)
+ && TYPE_HAS_COMPLEX_COPY_CTOR (t)
+ && TYPE_HAS_COMPLEX_MOVE_CTOR (t))
+ return false;
+ if (CLASSTYPE_LAZY_DESTRUCTOR (t))
+ lazily_declare_fn (sfk_destructor, t);
+ tree fn = CLASSTYPE_DESTRUCTOR (t);
+ if (!fn || DECL_DELETED_FN (fn))
+ return false;
+ for (ovl_iterator iter (get_class_binding (t, complete_ctor_identifier));
+ iter; ++iter)
{
- if (is_trivially_xible (INIT_EXPR, t, make_tree_vec (0)))
- return true;
- tree arg = make_tree_vec (1);
- tree ct
- = cp_build_qualified_type (t, (cp_type_quals (t) | TYPE_QUAL_CONST));
- TREE_VEC_ELT (arg, 0) = cp_build_reference_type (ct, /*rval=*/false);
- if (is_trivially_xible (INIT_EXPR, t, arg))
- return true;
- TREE_VEC_ELT (arg, 0) = t;
- if (is_trivially_xible (INIT_EXPR, t, arg))
+ fn = *iter;
+ if ((default_ctor_p (fn) || copy_fn_p (fn) || move_fn_p (fn))
+ && trivial_fn_p (fn)
+ && !DECL_DELETED_FN (fn))
return true;
}
return false;
--- gcc/testsuite/g++.dg/ext/is_implicit_lifetime2.C.jj 2025-11-18
11:13:27.119778979 +0100
+++ gcc/testsuite/g++.dg/ext/is_implicit_lifetime2.C 2025-11-18
11:14:35.852801144 +0100
@@ -0,0 +1,91 @@
+// PR c++/122690
+// { dg-do compile { target c++11 } }
+
+class A {
+ int i;
+public:
+ A () = default;
+ A (int i) : i(i) { }
+ A (A const &x) : i(x.i) {}
+ A (A &&x) : i(x.i) {}
+};
+class B {
+ int i;
+protected:
+ B () = default;
+public:
+ B (int i) : i(i) { }
+ B (B const &x) : i(x.i) {}
+ B (B &&x) : i(x.i) {}
+};
+class C {
+ int i;
+private:
+ C () = default;
+public:
+ C (int i) : i(i) { }
+ C (C const &x) : i(x.i) {}
+ C (C &&x) : i(x.i) {}
+};
+class D {
+ int i;
+public:
+ D (D const &) = default;
+ D () : i(0) {}
+ D (int i) : i(i) { }
+ D (D &&x) : i(x.i) {}
+};
+class E {
+ int i;
+protected:
+ E (E const &) = default;
+public:
+ E () : i(0) {}
+ E (int i) : i(i) { }
+ E (E &&x) : i(x.i) {}
+};
+class F {
+ int i;
+private:
+ F (F const &) = default;
+public:
+ F () : i(0) {}
+ F (int i) : i(i) { }
+ F (F &&x) : i(x.i) {}
+};
+class G {
+ int i;
+public:
+ G (G &&) = default;
+ G () : i(0) {}
+ G (int i) : i(i) { }
+ G (const G &x) : i(x.i) {}
+};
+class H {
+ int i;
+protected:
+ H (H &&) = default;
+public:
+ H () : i(0) {}
+ H (int i) : i(i) { }
+ H (const H &x) : i(x.i) {}
+};
+class I {
+ int i;
+private:
+ I (I &&) = default;
+public:
+ I () : i(0) {}
+ I (int i) : i(i) { }
+ I (const I &x) : i(x.i) {}
+};
+
+static_assert (__builtin_is_implicit_lifetime (A), "");
+static_assert (__builtin_is_implicit_lifetime (B), "");
+static_assert (__builtin_is_implicit_lifetime (C), "");
+static_assert (__builtin_is_implicit_lifetime (D), "");
+static_assert (__builtin_is_implicit_lifetime (E), "");
+static_assert (__builtin_is_implicit_lifetime (F), "");
+static_assert (__builtin_is_implicit_lifetime (G), "");
+static_assert (__builtin_is_implicit_lifetime (H), "");
+static_assert (__builtin_is_implicit_lifetime (I), "");
Jakub