[{"id":3678169,"web_url":"http://patchwork.ozlabs.org/comment/3678169/","msgid":"<aeDj7wrH60HjsnPU@redhat.com>","list_archive_url":null,"date":"2026-04-16T13:28:15","subject":"Re: [PATCH] c++: Reject splicing of function parameters [PR123783]","submitter":{"id":14370,"url":"http://patchwork.ozlabs.org/api/people/14370/","name":"Marek Polacek","email":"polacek@redhat.com"},"content":"On Thu, Apr 16, 2026 at 12:28:07PM +0200, Jakub Jelinek wrote:\n> Hi!\n> \n> When discussing the PR last night, I've realized that we don't reject\n> splicing of function parameters, even when\n> http://eel.is/c++draft/expr.prim.splice#2\n> doesn't list function parameters anywhere and so\n> http://eel.is/c++draft/expr.prim.splice#2.7 should apply.\n> Note, this is something that can't be tested in check_splice_expr,\n> because REFLECT_EXPR_KIND isn't propagated to it and whether some\n> reflection is a reflection of a function parameter or variable_of thereof\n> only differentiates in that kind, otherwise it is PARM_DECL in both\n> cases.  So, the following patch diagnoses it in splice function.\n> And for the case where using [:variable_of(e):] could make it valid\n> a message suggesting that is emitted too.\n> In mangle3.C I had to drop one test, it is now rejected and the\n> test tests mangling, so better shouldn't have errors in it.\n> For the dep14.C case, I've applied variable_of on it so that we get\n> the previous behavior.  And in func2 there it was actually (I think\n> mistakenly) asking for paramete of func rather than func2, fixed that too.\n> \n> Tested on x86_64-linux, ok for trunk if it passes full bootstrap/regtest?\n> \n> 2026-04-16  Jakub Jelinek  <jakub@redhat.com>\n> \n> \tPR c++/123783\n> \t* reflect.cc (splice): Reject splicing of function parameters.\n> \n> \t* g++.dg/reflect/mangle3.C: Remove f5<parameters_of (^^g)[0]>()\n> \tcase.\n> \t* g++.dg/reflect/dep14.C: Include <ranges>.\n> \t(func): Apply variable_of to each vector member.\n> \t(func2): Likewise.  Use ^^func2 instead of ^^func.\n> \t* g++.dg/reflect/splice12.C: New test.\n> \n> --- gcc/cp/reflect.cc.jj\t2026-04-15 08:52:04.297374316 +0200\n> +++ gcc/cp/reflect.cc\t2026-04-16 11:39:30.763707538 +0200\n> @@ -8510,6 +8510,18 @@ splice (tree refl)\n>        return error_mark_node;\n>      }\n>  \n> +  if (REFLECT_EXPR_KIND (refl) == REFLECT_PARM)\n> +    {\n> +      auto_diagnostic_group d;\n> +      error_at (cp_expr_loc_or_input_loc (refl),\n> +\t\t\"cannot use a function parameter reflection in a splice \"\n> +\t\t\"expression\");\n> +      if (DECL_CONTEXT (REFLECT_EXPR_HANDLE (refl)) == current_function_decl)\n> +\tinform (cp_expr_loc_or_input_loc (refl),\n> +\t\t\"apply %<std::meta::variable_of%> on it before splicing\");\n> +      return error_mark_node;\n> +    }\n> +\n\nLooks ok.  Optionally: \n- add a comment saying why this isn't in check_splice_expr\n- we could also have a location_t for the cp_expr_loc_or_input_loc\n  since it's used 3x in the function now\n- could add %qD in the error message since that's what check_splice_expr\n  does\n\n>    /* We are bringing some entity from the unevaluated expressions world\n>       to possibly outside of that, mark it used.  */\n>    if (!mark_used (REFLECT_EXPR_HANDLE (refl)))\n> --- gcc/testsuite/g++.dg/reflect/mangle3.C.jj\t2026-04-15 09:57:58.778962876 +0200\n> +++ gcc/testsuite/g++.dg/reflect/mangle3.C\t2026-04-16 11:47:13.739937787 +0200\n> @@ -175,8 +175,6 @@ g (int p)\n>  // { dg-final { scan-assembler \"_Z2f5ILDmvlLi42EEEDtDST_EEv\" } }\n>    f5<std::meta::reflect_object (arr[1])>();\n>  // { dg-final { scan-assembler \"_Z2f5ILDmobixL_Z3arrEL\\[ilx]1EEEDtDST_EEv\" } }\n> -  f5<parameters_of (^^g)[0]>();\n> -// { dg-final { scan-assembler \"_Z2f5ILDmpa_1giEEDtDST_EEv\" } }\n>    f5<bases_of (^^S, ctx)[0]>();\n>  // { dg-final { scan-assembler \"_Z2f5ILDmba_1SEEDtDST_EEv\" } }\n>    f6<Y>(Y{42});\n> --- gcc/testsuite/g++.dg/reflect/dep14.C.jj\t2026-04-15 21:28:31.118011956 +0200\n> +++ gcc/testsuite/g++.dg/reflect/dep14.C\t2026-04-16 12:10:13.009767880 +0200\n> @@ -3,6 +3,7 @@\n>  // { dg-additional-options \"-freflection\" }\n>  \n>  #include <meta>\n> +#include <ranges>\n>  \n>  namespace __impl {\n>    template<auto... vals>\n> @@ -30,7 +31,7 @@ int func(int counter, float factor) {\n>      std::array<void *, parameters_of(^^func).size()> args;\n>  \n>      std::size_t i = 0;\n> -    [:expand(parameters_of(^^func)):] >> [&i, &counter, &factor, &args]<auto e>\n> +    [:expand(parameters_of(^^func) | std::views::transform(std::meta::variable_of)):] >> [&i, &counter, &factor, &args]<auto e>\n>      {\n>          args[i++] = &[:e:]; // { dg-error \"use of local variable\" }\n>      };\n> @@ -38,10 +39,10 @@ int func(int counter, float factor) {\n>  }\n>  \n>  int func2(int counter, float factor) {\n> -    std::array<void *, parameters_of(^^func).size()> args;\n> +    std::array<void *, parameters_of(^^func2).size()> args;\n>  \n>      std::size_t i = 0;\n> -    [:expand(parameters_of(^^func)):] >> [&]<auto e>\n> +    [:expand(parameters_of(^^func2) | std::views::transform(std::meta::variable_of)):] >> [&]<auto e>\n>      {\n>          args[i++] = &[:e:]; // { dg-error \"use of local variable|trying to capture \\[^\\n\\r]* in instantiation of generic lambda\" }\n>      };\n> --- gcc/testsuite/g++.dg/reflect/splice12.C.jj\t2026-04-16 11:53:29.075638834 +0200\n> +++ gcc/testsuite/g++.dg/reflect/splice12.C\t2026-04-16 12:05:05.248940576 +0200\n> @@ -0,0 +1,21 @@\n> +// PR c++/123783\n> +// { dg-do compile { target c++26 } }\n> +// { dg-additional-options \"-freflection\" }\n> +\n> +#include <meta>\n> +\n> +void\n> +bar (int a)\n> +{\n> +}\n> +\n> +void\n> +foo (int a)\n> +{\n> +  auto b = &[: ^^a :];\n> +  auto c = &[: variable_of (parameters_of (^^foo)[0]) :];\n> +  auto d = &[: parameters_of (^^foo)[0] :];\t\t\t// { dg-error \"cannot use a function parameter reflection in a splice expression\" }\n> +\t\t\t\t\t\t\t\t// { dg-message \"apply 'std::meta::variable_of' on it before splicing\" \"\" { target *-*-* } .-1 }\n> +  auto e = &[: parameters_of (^^bar)[0] :];\t\t\t// { dg-error \"cannot use a function parameter reflection in a splice expression\" }\n> +  auto f = [] { auto g = &[: parameters_of (^^foo)[0] :]; };\t// { dg-error \"cannot use a function parameter reflection in a splice expression\" }\n> +}\n> \n> \tJakub\n> \n\nMarek","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=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=QWDwOLbf;\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=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=QWDwOLbf","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.129.124"],"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 4fxJkY043sz1yHP\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 16 Apr 2026 23:28:51 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id A185B4BA23D9\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 16 Apr 2026 13:28:49 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 8B5204BA2E35\n for <gcc-patches@gcc.gnu.org>; Thu, 16 Apr 2026 13:28:21 +0000 (GMT)","from mail-qt1-f198.google.com (mail-qt1-f198.google.com\n [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-515-R-2Hv2m4NW62NQXbJvSIPQ-1; Thu, 16 Apr 2026 09:28:20 -0400","by mail-qt1-f198.google.com with SMTP id\n d75a77b69052e-50d58bed44aso237765401cf.3\n for <gcc-patches@gcc.gnu.org>; Thu, 16 Apr 2026 06:28:19 -0700 (PDT)","from redhat.com ([2603:7000:9500:10::1db4])\n by smtp.gmail.com with ESMTPSA id\n d75a77b69052e-50e1afbcdd5sm47847191cf.20.2026.04.16.06.28.17\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Thu, 16 Apr 2026 06:28:17 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org A185B4BA23D9","OpenDKIM Filter v2.11.0 sourceware.org 8B5204BA2E35"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 8B5204BA2E35","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 8B5204BA2E35","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776346101; cv=none;\n b=Mp4ekJBKJMBCh1km76oQLivDclVSY7ngYMxhyaCbk/wuSTKVAxaxj6oxlJYXIeuTqbf7Jj6KcJ2jE1NjfG5IEq/hSxo5Y3J9/sLdZQly+wbONYPeUzGTRzYexQlq7CDRzRfa7gtIQynRbuetWdHMNL0wv1zpdWdNU7YdocMbUeA=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776346101; c=relaxed/simple;\n bh=DOW3UUiIR0qpPyC1RtKz8eRY3HN1eaNZme8sbwTa0eI=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=rglUGpuX4Nttg4VR0mk6RFQBFErFXZgEoHez2XqPyp2jAxC5k/Cq9qNlQ0qPvr/A9gtmZcG/yXnJPWUfkSt/3Juo3LTC/5mPyk/MzCn7dEwRxYVQClc7cnpS3D7o1W2Q3SVPwLg/tNkOPrJPL0tfNDzzfkTjHJcJAuMsEHEdgCg=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1776346101;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n in-reply-to:in-reply-to:references:references;\n bh=x0/jLJ5GzMbTtmsFQ8RZDgt2SOvJW7TV9QfBpz1msoQ=;\n b=QWDwOLbfg0EZC2gPv/+AFdUoJ/NFHlmfM2jwRp+9VLU1myibJsvLQtyicd38vaa1iAAdy1\n hmWm9FSLa+xwjbq6eYXItCOl1DLeWb2OyyQ0bySToYRcqpyL2CO4oW8evh2RvgLnOf3Nf6\n Byl6EmY/QBNeQ6C3S4tQVtTMXALjHz0=","X-MC-Unique":"R-2Hv2m4NW62NQXbJvSIPQ-1","X-Mimecast-MFC-AGG-ID":"R-2Hv2m4NW62NQXbJvSIPQ_1776346099","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776346099; x=1776950899;\n h=user-agent:in-reply-to:content-disposition:mime-version:references\n :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=x0/jLJ5GzMbTtmsFQ8RZDgt2SOvJW7TV9QfBpz1msoQ=;\n b=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==","X-Received":["by 2002:a05:622a:1209:b0:50d:81c4:4c79 with SMTP id\n d75a77b69052e-50dd5cbfcfbmr380900671cf.35.1776346098669;\n Thu, 16 Apr 2026 06:28:18 -0700 (PDT)","by 2002:a05:622a:1209:b0:50d:81c4:4c79 with SMTP id\n d75a77b69052e-50dd5cbfcfbmr380899921cf.35.1776346097936;\n Thu, 16 Apr 2026 06:28:17 -0700 (PDT)"],"Date":"Thu, 16 Apr 2026 09:28:15 -0400","From":"Marek Polacek <polacek@redhat.com>","To":"Jakub Jelinek <jakub@redhat.com>","Cc":"Jason Merrill <jason@redhat.com>, gcc-patches@gcc.gnu.org","Subject":"Re: [PATCH] c++: Reject splicing of function parameters [PR123783]","Message-ID":"<aeDj7wrH60HjsnPU@redhat.com>","References":"<aeC5t2bY4NkoD4nm@tucnak>","MIME-Version":"1.0","In-Reply-To":"<aeC5t2bY4NkoD4nm@tucnak>","User-Agent":"Mutt/2.3.1 (2026-03-20)","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"XtGcBIP3TIkXJqHbeBkl5uAxl13YrLQrRXxtVuU-Z9c_1776346099","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","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"}}]