diff mbox series

c++: direct enum init from type-dep elt [PR112515]

Message ID 20231114163536.1208039-1-ppalka@redhat.com
State New
Headers show
Series c++: direct enum init from type-dep elt [PR112515] | expand

Commit Message

Patrick Palka Nov. 14, 2023, 4:35 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
OK for trunk?

-- >8 --

The NON_DEPENDENT_EXPR removal exposed that is_direct_enum_init may end
up checking convertibility of a type-dependent argument.

	PR c++/112515

gcc/cp/ChangeLog:

	* decl.cc (is_direct_enum_init): Return false if the single
	element is type-dependent.

gcc/testsuite/ChangeLog:

	* g++.dg/template/non-dependent30.C: New test.
---
 gcc/cp/decl.cc                                  | 1 +
 gcc/testsuite/g++.dg/template/non-dependent30.C | 9 +++++++++
 2 files changed, 10 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/non-dependent30.C

Comments

Jason Merrill Nov. 14, 2023, 10:32 p.m. UTC | #1
On 11/14/23 11:35, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
> OK for trunk?

OK.

> -- >8 --
> 
> The NON_DEPENDENT_EXPR removal exposed that is_direct_enum_init may end
> up checking convertibility of a type-dependent argument.
> 
> 	PR c++/112515
> 
> gcc/cp/ChangeLog:
> 
> 	* decl.cc (is_direct_enum_init): Return false if the single
> 	element is type-dependent.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/non-dependent30.C: New test.
> ---
>   gcc/cp/decl.cc                                  | 1 +
>   gcc/testsuite/g++.dg/template/non-dependent30.C | 9 +++++++++
>   2 files changed, 10 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/template/non-dependent30.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 4a07c7e879b..4be4847810a 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -6614,6 +6614,7 @@ is_direct_enum_init (tree type, tree init)
>         && CONSTRUCTOR_NELTS (init) == 1
>         /* DR 2374: The single element needs to be implicitly
>   	 convertible to the underlying type of the enum.  */
> +      && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
>         && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
>   			  TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
>   			  CONSTRUCTOR_ELT (init, 0)->value,
> diff --git a/gcc/testsuite/g++.dg/template/non-dependent30.C b/gcc/testsuite/g++.dg/template/non-dependent30.C
> new file mode 100644
> index 00000000000..32d48e44c09
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/non-dependent30.C
> @@ -0,0 +1,9 @@
> +// PR c++/112515
> +// { dg-do compile { target c++11 } }
> +
> +enum E : int { };
> +
> +template<class T>
> +E f(T t) {
> +  return E{t.e};
> +}
diff mbox series

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 4a07c7e879b..4be4847810a 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -6614,6 +6614,7 @@  is_direct_enum_init (tree type, tree init)
       && CONSTRUCTOR_NELTS (init) == 1
       /* DR 2374: The single element needs to be implicitly
 	 convertible to the underlying type of the enum.  */
+      && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
       && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
 			  TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
 			  CONSTRUCTOR_ELT (init, 0)->value,
diff --git a/gcc/testsuite/g++.dg/template/non-dependent30.C b/gcc/testsuite/g++.dg/template/non-dependent30.C
new file mode 100644
index 00000000000..32d48e44c09
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent30.C
@@ -0,0 +1,9 @@ 
+// PR c++/112515
+// { dg-do compile { target c++11 } }
+
+enum E : int { };
+
+template<class T>
+E f(T t) {
+  return E{t.e};
+}