On Mon, Aug 25, 2025 at 04:00:37PM -0400, Jason Merrill wrote:
> On 8/25/25 12:02 PM, Jakub Jelinek wrote:
> > On Mon, Aug 25, 2025 at 11:55:24AM -0400, Patrick Palka wrote:
> > > > The following patch fixes that by testing DECL_TEMPLATE_INFO, dunno
> > > > what else would be more appropriate for this function is a template,
> > > > maybe decl_dependent_p?
> > > 
> > > I was going to suggest just replacing the !processing_template_decl check
> > > with !in_template_context to distinguish a true template context but that
> > > wouldn't work here because we also push a dummy template parameter list
> > > when parsing an expansion statement... so failing that, your current
> > > approach makes sense to me.
> > 
> > Based on Jason's comment on the PARM_DECL tsubst_expr patch, another
> > possibility is !uses_template_params (fco), either always, or
> > in
> >    if (!processing_template_decl
> >        || (in_expansion_stmt && !uses_template_parms (fco)))
> > form.
> 
> I think changing !processing_template_decl to !uses_template_parms (fco)
> makes sense.

The following passed bootstrap/regtest on x86_64-linux and i686-linux, ok
for trunk?

2025-08-27  Jakub Jelinek  <ja...@redhat.com>

        PR c++/121583
        * semantics.cc (apply_deduced_return_type): Adjust
        fun->returns*_struct when !uses_template_parms (fco) instead of
        when !processing_template_decl.

        * g++.dg/cpp26/expansion-stmt23.C: New test.
        * g++.dg/cpp26/expansion-stmt24.C: New test.

--- gcc/cp/semantics.cc.jj      2025-08-15 22:38:28.907872033 +0200
+++ gcc/cp/semantics.cc 2025-08-23 18:02:09.130079505 +0200
@@ -14213,7 +14213,7 @@ apply_deduced_return_type (tree fco, tre
                                result);
   DECL_RESULT (fco) = result;
 
-  if (!processing_template_decl)
+  if (!uses_template_parms (fco))
     if (function *fun = DECL_STRUCT_FUNCTION (fco))
       {
        bool aggr = aggregate_value_p (result, fco);
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt23.C.jj    2025-08-23 
17:49:13.816155626 +0200
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt23.C       2025-08-23 
18:08:32.125064559 +0200
@@ -0,0 +1,17 @@
+// PR c++/121583
+// { dg-do compile { target c++14 } }
+// { dg-options "" }
+
+auto
+foo ()
+{
+  template for (int i : { 0 })         // { dg-warning "'template for' only 
available with" "" { target c++23_down } }
+    return i;
+}
+
+auto
+bar ()
+{
+  template for (auto i : { 0 })                // { dg-warning "'template for' 
only available with" "" { target c++23_down } }
+    return i;
+}
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt24.C.jj    2025-08-23 
18:08:46.988868108 +0200
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt24.C       2025-08-23 
18:12:46.856697841 +0200
@@ -0,0 +1,10 @@
+// PR c++/121583
+// { dg-do compile { target c++14 } }
+// { dg-options "" }
+
+auto
+foo ()
+{
+  template for (auto i : { 0, 1, 2LL })                // { dg-warning 
"'template for' only available with" "" { target c++23_down } }
+    return i;                                  // { dg-error "inconsistent 
deduction for auto return type: 'int' and then 'long long int'" }
+}


        Jakub

Reply via email to