diff mbox series

c++: double non-dep folding from finish_compound_literal [PR104565]

Message ID 20220216202642.2063582-1-ppalka@redhat.com
State New
Headers show
Series c++: double non-dep folding from finish_compound_literal [PR104565] | expand

Commit Message

Patrick Palka Feb. 16, 2022, 8:26 p.m. UTC
In finish_compound_literal, we perform non-dependent expr folding before
calling check_narrowing (ever since r9-5973).  But ever since r10-7096,
check_narrowing also performs non-dependent expr folding of its own.
This double folding cause tsubst to see non-templated trees during the
second folding, which causes a spurious error in the below testcase.

This patch removes this first folding operation; it now seems obviated
by the second one.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/10/11?

	PR c++/104565

gcc/cp/ChangeLog:

	* semantics.cc (finish_compound_literal): Don't perform
	non-dependent expr folding before calling check_narrowing.

gcc/testsuite/ChangeLog:

	* g++.dg/template/non-dependent22.C: New test.
---
 gcc/cp/semantics.cc                             | 10 +++-------
 gcc/testsuite/g++.dg/template/non-dependent22.C | 12 ++++++++++++
 2 files changed, 15 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/non-dependent22.C

Comments

Jason Merrill Feb. 17, 2022, 12:51 a.m. UTC | #1
On 2/16/22 15:26, Patrick Palka wrote:
> In finish_compound_literal, we perform non-dependent expr folding before
> calling check_narrowing (ever since r9-5973).  But ever since r10-7096,
> check_narrowing also performs non-dependent expr folding of its own.
> This double folding cause tsubst to see non-templated trees during the
> second folding, which causes a spurious error in the below testcase.
> 
> This patch removes this first folding operation; it now seems obviated
> by the second one.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/10/11?

OK for trunk now, release branches in about a month.

> 	PR c++/104565
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (finish_compound_literal): Don't perform
> 	non-dependent expr folding before calling check_narrowing.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/non-dependent22.C: New test.
> ---
>   gcc/cp/semantics.cc                             | 10 +++-------
>   gcc/testsuite/g++.dg/template/non-dependent22.C | 12 ++++++++++++
>   2 files changed, 15 insertions(+), 7 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/non-dependent22.C
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 0cb17a6a8ab..114baa48710 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -3203,13 +3203,9 @@ finish_compound_literal (tree type, tree compound_literal,
>       return error_mark_node;
>     compound_literal = reshape_init (type, compound_literal, complain);
>     if (SCALAR_TYPE_P (type)
> -      && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
> -    {
> -      tree t = instantiate_non_dependent_expr_sfinae (compound_literal,
> -						      complain);
> -      if (!check_narrowing (type, t, complain))
> -	return error_mark_node;
> -    }
> +      && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
> +      && !check_narrowing (type, compound_literal, complain))
> +    return error_mark_node;
>     if (TREE_CODE (type) == ARRAY_TYPE
>         && TYPE_DOMAIN (type) == NULL_TREE)
>       {
> diff --git a/gcc/testsuite/g++.dg/template/non-dependent22.C b/gcc/testsuite/g++.dg/template/non-dependent22.C
> new file mode 100644
> index 00000000000..83a6a13f15b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/non-dependent22.C
> @@ -0,0 +1,12 @@
> +// PR c++/104565
> +// { dg-do compile { target c++11 } }
> +
> +struct apa {
> +  constexpr int n() const { return 3; }
> +};
> +
> +template<class>
> +int f() {
> +  apa foo;
> +  return int{foo.n()};  // no matching function for call to 'apa::n(apa*)'
> +}
diff mbox series

Patch

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0cb17a6a8ab..114baa48710 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -3203,13 +3203,9 @@  finish_compound_literal (tree type, tree compound_literal,
     return error_mark_node;
   compound_literal = reshape_init (type, compound_literal, complain);
   if (SCALAR_TYPE_P (type)
-      && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
-    {
-      tree t = instantiate_non_dependent_expr_sfinae (compound_literal,
-						      complain);
-      if (!check_narrowing (type, t, complain))
-	return error_mark_node;
-    }
+      && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
+      && !check_narrowing (type, compound_literal, complain))
+    return error_mark_node;
   if (TREE_CODE (type) == ARRAY_TYPE
       && TYPE_DOMAIN (type) == NULL_TREE)
     {
diff --git a/gcc/testsuite/g++.dg/template/non-dependent22.C b/gcc/testsuite/g++.dg/template/non-dependent22.C
new file mode 100644
index 00000000000..83a6a13f15b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent22.C
@@ -0,0 +1,12 @@ 
+// PR c++/104565
+// { dg-do compile { target c++11 } }
+
+struct apa {
+  constexpr int n() const { return 3; }
+};
+
+template<class>
+int f() {
+  apa foo;
+  return int{foo.n()};  // no matching function for call to 'apa::n(apa*)'
+}