c++: Fix ICE with type aliases in inherited CTAD [PR122070]

Message ID 20251207224715.1051833-3-egas.g.ribeiro@tecnico.ulisboa.pt
State New
Headers
Series c++: Fix ICE with type aliases in inherited CTAD [PR122070] |

Commit Message

Egas Ribeiro Dec. 7, 2025, 10:47 p.m. UTC
  Regtested on x86_64-pc-linux-gnu, OK for trunk?

-- >8 --

When processing inherited CTAD in C++23, type_targs_deducible_from can
be called with a synthetic alias template whose TREE_VALUE is a type
alias like std::string_view.  Since type aliases don't have their own
TEMPLATE_INFO, calling TI_TEMPLATE on a NULL 
TYPE_TEMPLATE_INFO_MAYBE_ALIAS result caused a segfault.

This fix adds a NULL check and returns NULL_TREE when the type lacks 
template info, indicating it can't be deduced.

	PR c++/122070

gcc/cp/ChangeLog:

	* pt.cc (type_targs_deducible_from): Add NULL check for
	TYPE_TEMPLATE_INFO_MAYBE_ALIAS to prevent ICE with type aliases.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp23/class-deduction-inherited-ice.C: New test.

Signed-off-by: Egas Ribeiro <egas.g.ribeiro@tecnico.ulisboa.pt>
---
 gcc/cp/pt.cc                                  |  5 ++-
 .../cpp23/class-deduction-inherited-ice.C     | 34 +++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
  

Comments

Patrick Palka Dec. 12, 2025, 8:41 p.m. UTC | #1
On Sun, 7 Dec 2025, Egas Ribeiro wrote:

> Regtested on x86_64-pc-linux-gnu, OK for trunk?
> 
> -- >8 --
> 
> When processing inherited CTAD in C++23, type_targs_deducible_from can
> be called with a synthetic alias template whose TREE_VALUE is a type
> alias like std::string_view.  Since type aliases don't have their own
> TEMPLATE_INFO, calling TI_TEMPLATE on a NULL 
> TYPE_TEMPLATE_INFO_MAYBE_ALIAS result caused a segfault.
> 
> This fix adds a NULL check and returns NULL_TREE when the type lacks 
> template info, indicating it can't be deduced.

Does this mean CTAD will fail for

    template<class T> struct A { A(T); };

    using B = A<int>;

    template<class T=void>
    struct C : B { using B::B; };

    C c = 0;

It's a bit of a contrived testcase but I think this should be valid and
behave as if we used A<int> instead of B in the definition of C.

To achieve that we could fall back to using TYPE_TEMPLATE_INFO if
TYPE_TEMPLATE_INFO_MAYBE_ALIAS is NULL.

> 
> 	PR c++/122070
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (type_targs_deducible_from): Add NULL check for
> 	TYPE_TEMPLATE_INFO_MAYBE_ALIAS to prevent ICE with type aliases.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp23/class-deduction-inherited-ice.C: New test.
> 
> Signed-off-by: Egas Ribeiro <egas.g.ribeiro@tecnico.ulisboa.pt>
> ---
>  gcc/cp/pt.cc                                  |  5 ++-
>  .../cpp23/class-deduction-inherited-ice.C     | 34 +++++++++++++++++++
>  2 files changed, 38 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 8498730b6e4..c99df255dd7 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -31630,7 +31630,10 @@ type_targs_deducible_from (tree tmpl, tree type)
>  	 per alias_ctad_tweaks.  */
>        tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
>        ttype = TREE_VALUE (tmpl);
> -      tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype));
> +      tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
> +      if (!ti)
> +	return NULL_TREE;
> +      tmpl = TI_TEMPLATE (ti);
>      }
>  
>    int len = TREE_VEC_LENGTH (tparms);
> diff --git a/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C b/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
> new file mode 100644
> index 00000000000..6724ce303d8
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
> @@ -0,0 +1,34 @@
> +// { dg-do compile { target c++23 } }
> +// PR c++/122070
> +
> +namespace std {
> +    template<typename T, T v> struct integral_constant { static constexpr T value = v; };
> +    using size_t = decltype(sizeof(0));
> +
> +    template<typename CharT>
> +    struct basic_string_view {
> +        const CharT* ptr;
> +        size_t len;
> +        constexpr basic_string_view(const CharT* p) : ptr(p), len(0) { while (p && p[len]) ++len; }
> +    };
> +    using string_view = basic_string_view<char>;
> +}
> +
> +template<std::size_t N>
> +struct sized_string_view: std::string_view {
> +    using std::string_view::string_view;
> +};
> +template<std::size_t N>
> +sized_string_view(const char (&str)[N]) -> sized_string_view<N - 1>;
> +
> +
> +constexpr auto string_builder(auto first, auto second, auto... trailing) {
> +    constexpr auto is_last = sizeof...(trailing) == 0;
> +    auto buffer = 1;
> +    if constexpr (is_last) {
> +        return buffer;
> +    } else
> +        return string_builder(buffer, trailing...);
> +}
> +
> +constexpr auto copyright = string_builder(sized_string_view("a"), sized_string_view("b"), sized_string_view("c"));
> -- 
> 2.52.0
> 
>
  
Egas Ribeiro Dec. 13, 2025, 9:15 a.m. UTC | #2
On Fri Dec 12, 2025 at 8:41 PM WET, Patrick Palka wrote:
>> This fix adds a NULL check and returns NULL_TREE when the type lacks 
>> template info, indicating it can't be deduced.
>
> Does this mean CTAD will fail for
>
>     template<class T> struct A { A(T); };
>
>     using B = A<int>;
>
>     template<class T=void>
>     struct C : B { using B::B; };
>
>     C c = 0;
>
> It's a bit of a contrived testcase but I think this should be valid and
> behave as if we used A<int> instead of B in the definition of C.
>
> To achieve that we could fall back to using TYPE_TEMPLATE_INFO if
> TYPE_TEMPLATE_INFO_MAYBE_ALIAS is NULL.

You're right! CTAD does fail for this. I'll try submitting a v2 later.
  

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8498730b6e4..c99df255dd7 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -31630,7 +31630,10 @@  type_targs_deducible_from (tree tmpl, tree type)
 	 per alias_ctad_tweaks.  */
       tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
       ttype = TREE_VALUE (tmpl);
-      tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype));
+      tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
+      if (!ti)
+	return NULL_TREE;
+      tmpl = TI_TEMPLATE (ti);
     }
 
   int len = TREE_VEC_LENGTH (tparms);
diff --git a/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C b/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
new file mode 100644
index 00000000000..6724ce303d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/class-deduction-inherited-ice.C
@@ -0,0 +1,34 @@ 
+// { dg-do compile { target c++23 } }
+// PR c++/122070
+
+namespace std {
+    template<typename T, T v> struct integral_constant { static constexpr T value = v; };
+    using size_t = decltype(sizeof(0));
+
+    template<typename CharT>
+    struct basic_string_view {
+        const CharT* ptr;
+        size_t len;
+        constexpr basic_string_view(const CharT* p) : ptr(p), len(0) { while (p && p[len]) ++len; }
+    };
+    using string_view = basic_string_view<char>;
+}
+
+template<std::size_t N>
+struct sized_string_view: std::string_view {
+    using std::string_view::string_view;
+};
+template<std::size_t N>
+sized_string_view(const char (&str)[N]) -> sized_string_view<N - 1>;
+
+
+constexpr auto string_builder(auto first, auto second, auto... trailing) {
+    constexpr auto is_last = sizeof...(trailing) == 0;
+    auto buffer = 1;
+    if constexpr (is_last) {
+        return buffer;
+    } else
+        return string_builder(buffer, trailing...);
+}
+
+constexpr auto copyright = string_builder(sized_string_view("a"), sized_string_view("b"), sized_string_view("c"));