{"id":2232019,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2232019/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/96dcb22e519ae15f28523be5c1dffe48265827c8.camel@tugraz.at/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<96dcb22e519ae15f28523be5c1dffe48265827c8.camel@tugraz.at>","date":"2026-05-02T08:03:29","name":"[C] Fix use of variably-modified structure/union types in nested context [PR124985]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b7d07abe017fc43ece7c86c8a405014d99b9097a","submitter":{"id":85465,"url":"http://patchwork.ozlabs.org/api/1.1/people/85465/?format=json","name":"Martin Uecker","email":"uecker@tugraz.at"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/96dcb22e519ae15f28523be5c1dffe48265827c8.camel@tugraz.at/mbox/","series":[{"id":502516,"url":"http://patchwork.ozlabs.org/api/1.1/series/502516/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502516","date":"2026-05-02T08:03:29","name":"[C] Fix use of variably-modified structure/union types in nested context [PR124985]","version":1,"mbox":"http://patchwork.ozlabs.org/series/502516/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2232019/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2232019/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=tugraz.at header.i=@tugraz.at header.a=rsa-sha256\n header.s=mailrelay header.b=lgYYQwY/;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=tugraz.at header.i=@tugraz.at header.a=rsa-sha256\n header.s=mailrelay header.b=lgYYQwY/","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=tugraz.at","sourceware.org; spf=pass smtp.mailfrom=tugraz.at","server2.sourceware.org;\n arc=none smtp.remote-ip=129.27.2.202"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g70mb0WGvz1yGq\n\tfor <incoming@patchwork.ozlabs.org>; Sat, 02 May 2026 18:04:13 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id B2260469D687\n\tfor <incoming@patchwork.ozlabs.org>; Sat,  2 May 2026 08:04:11 +0000 (GMT)","from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202])\n by sourceware.org (Postfix) with ESMTPS id DC9794A99304\n for <gcc-patches@gcc.gnu.org>; Sat,  2 May 2026 08:03:42 +0000 (GMT)","from vra-168-195.tugraz.at (vra-168-195.tugraz.at [129.27.168.195])\n by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4g70lj3NfBz2xHG;\n Sat,  2 May 2026 10:03:29 +0200 (CEST)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org B2260469D687","OpenDKIM Filter v2.11.0 sourceware.org DC9794A99304"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org DC9794A99304","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org DC9794A99304","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777709024; cv=none;\n b=Gx5dHlAAAKLA3KDmXtsjOaYgCYSSIlVRSVXW7NHV0HueEdiD7RAsvQpWkz37HXn5EsIAIvMrw7P2hju45ZSxKWIIfWpniMFFTUwyv/UiL62mP+LzjHZVhgy9YyFcUpHa3WVN5qEmaNbKJD1pKx2XiUDlHHAXAneUorLMU8d7nxk=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777709024; c=relaxed/simple;\n bh=bqY4P3kmCHGJrh/kK3T2JdkN2zw8zLSz0FfDJWKrffE=;\n h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version;\n b=i0Mg7DzBLLPGqAdErpHFX0yjgFqoff/+S6ZMEQQE3gZc8xuVrHMZeJwgjWd25ujkVT034YTy/kxExwZMVgQplVgL9pufAwS5sf5bLoRnww7ooh0JXgnHfjnh7NEjgkksvTLtY2GTVLhfpVZwLfOPNR8NJXf8E/Wdr8LhE/Nq8a0=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at;\n s=mailrelay; t=1777709009;\n bh=ys3dZKCEU5VFUpviHWJrVd3wZA6fhiWPBGPc6myoaGE=;\n h=Subject:From:To:Cc:Date;\n b=lgYYQwY/D7SpSl01cqCxqYo5LVMJ/ADBRYIv4xqf7w1wnMZFBVoRU+vTQh7xOfQZH\n EsLKGVXtq/1Y632AZwAjMUX9Uzt0E1b7yiOhfh0HwQEXu2lfHAhJwBEKDU51C2KXTs\n XHorOdvL9mp934BtHoWLTRXQDhGg0yWA4n5XGqfg=","Message-ID":"<96dcb22e519ae15f28523be5c1dffe48265827c8.camel@tugraz.at>","Subject":"[C PATCH] Fix use of variably-modified structure/union types in\n nested context [PR124985]","From":"Martin Uecker <uecker@tugraz.at>","To":"gcc-patches@gcc.gnu.org","Cc":"Joseph Myers <josmyers@redhat.com>","Date":"Sat, 02 May 2026 10:03:29 +0200","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","User-Agent":"Evolution 3.56.2-0+deb13u1 ","MIME-Version":"1.0","X-TUG-Backscatter-control":"G/VXY7/6zeyuAY/PU2/0qw","X-Scanned-By":"MIMEDefang 2.74 on 129.27.10.117","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"There are two cases I missed regarding the need for a static chain.\nOne is fixed by this patch and related to structure/union types\n\nThe other one is the (hopefully rare) case of using a variable of the\nenclosing context in a variably modified type of a parameter (which\ndoes not work correctly with the existing code, because we parse\nthe function declaration in the enclosing context, so the existing\nlogic does not recognize this as needing special treatment).\n\nWe probably also want to have the a1 case below require a static chain,\nalthough it seems it is not required for existing ABIs. For now the test\nis included to make sure we do not crash.\n\nI will address the other issues in a future patch.\n\n\nBootstrapped and regression tested on x86_64.\n\n\n    c: Fix use of variably-modified structure/union types in nested context [PR124985]\n    \n    If a nested function uses a variably-modified structure or union type\n    that depends on the enclosing context, it needs the static chain.\n    My recent change missed this, because in this case no decl was used.\n    To address this, we now call mark_decl_used also on TYPE_STUB_DECLs.\n    \n            PR c/124985\n    \n    gcc/c/ChangeLog:\n            * c-decl.cc (pushtag): Update comment.\n            (declspecs_add_type): Mark TYPE_STUB_DECL as used.\n            * c-typeck.cc (function_to_pointer_conversion): Fix grammar in comment.\n    \n    gcc/testsuite/ChangeLog:\n            * gcc.dg/Wreturn-nested-3.c: New test.\n            * gcc.dg/pr124985.c: New test.","diff":"diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc\nindex cc07e05e336..683780ab654 100644\n--- a/gcc/c/c-decl.cc\n+++ b/gcc/c/c-decl.cc\n@@ -1709,7 +1709,8 @@ pushtag (location_t loc, tree name, tree type)\n      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs\n      to output a representation of a tagged type, and it also gives\n      us a convenient place to record the \"scope start\" address for the\n-     tagged type.  */\n+     tagged type, and it is used to track whether the type is used\n+     in a non-local context via mark_decl_used.  */\n \n   TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,\n \t\t\t\t\t\tTYPE_DECL, NULL_TREE, type));\n@@ -13134,6 +13135,11 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,\n \t      specs->typedef_p = true;\n \t      specs->locations[cdw_typedef] = loc;\n \t    }\n+\n+\t  if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE\n+\t      || TREE_CODE (type) == ENUMERAL_TYPE)\n+\t    mark_decl_used (TYPE_STUB_DECL (type), false);\n+\n \t  if (spec.expr)\n \t    {\n \t      tree expr = save_expr (fold_convert (void_type_node, spec.expr));\ndiff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc\nindex aa368a6e95a..6195d179543 100644\n--- a/gcc/c/c-typeck.cc\n+++ b/gcc/c/c-typeck.cc\n@@ -2373,7 +2373,7 @@ function_to_pointer_conversion (location_t loc, tree exp)\n \n   tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);\n \n-  /* If the function is defined and known to not to require a non-local\n+  /* If the function is defined and known to not require a non-local\n      context, make sure no trampoline is generated.  */\n   if (TREE_CODE (exp) == FUNCTION_DECL\n       && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))\ndiff --git a/gcc/testsuite/gcc.dg/Wreturn-nested-3.c b/gcc/testsuite/gcc.dg/Wreturn-nested-3.c\nnew file mode 100644\nindex 00000000000..c31685f28f3\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/Wreturn-nested-3.c\n@@ -0,0 +1,42 @@\n+/* { dg-do compile } */\n+/* { dg-require-effective-target trampolines } */\n+/* { dg-options \"\" } */\n+\n+\n+void *a0() {\n+  int b = 1;\n+  struct bar { char c[b]; };\n+  struct bar e() { struct bar f; return f; }\n+  return &e;\t/* { dg-warning \"referencing local context\" } */\n+}\n+\n+void *a1() {\n+  int b = 1;\n+  struct bar { char c[b]; };\n+  struct bar e() { }\n+  return &e;\n+}\n+\n+void *a2() {\n+  int b = 1;\n+  struct bar { char c[b]; };\n+  struct bar e() { typeof(struct bar) f; return f; }\n+  return &e;\t/* { dg-warning \"referencing local context\" } */\n+}\n+\n+void *a3() {\n+  int b = 1;\n+  struct bar { char c[b]; };\n+  struct bar (*d)();\n+  struct bar e() { typeof((*d)()) f; return f; }\n+  return &e;\t/* { dg-warning \"referencing local context\" } */\n+}\n+\n+void *a4() {\n+  int b = 1;\n+  struct bar { char c[b]; };\n+  struct bar e() { struct bar f; return f; }\n+  return &e;\t/* { dg-warning \"referencing local context\" } */\n+}\n+\n+\ndiff --git a/gcc/testsuite/gcc.dg/pr124985.c b/gcc/testsuite/gcc.dg/pr124985.c\nnew file mode 100644\nindex 00000000000..8ccc282586f\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/pr124985.c\n@@ -0,0 +1,13 @@\n+/* { dg-do compile } */\n+/* { dg-require-effective-target trampolines } */\n+/* { dg-options \"-O1 -fno-tree-dse\" } */\n+\n+void a() {\n+  int b = 0;\n+  struct bar { char c[b]; };\n+  struct bar (*d)();\n+  struct bar e() { struct bar f; return f; }\n+  d = e;\n+  d();\n+}\n+\n","prefixes":["C"]}