[{"id":3684284,"web_url":"http://patchwork.ozlabs.org/comment/3684284/","msgid":"<b62e5130-b908-4e00-a70e-c76919444473@redhat.com>","list_archive_url":null,"date":"2026-04-29T21:36:58","subject":"Re: [PATCH v5] c++, contracts: fix testsuite basic.contract.eval.p8\n failed","submitter":{"id":4337,"url":"http://patchwork.ozlabs.org/api/people/4337/","name":"Jason Merrill","email":"jason@redhat.com"},"content":"On 4/29/26 3:22 PM, yxj-github-437 wrote:\n>>>>> If in a context that is manifestly constant-evaluated ([expr.const]),\n>>>>> a contract violation occurs and the evaluation semantic is terminating\n>>>>> or contract stmt isnot constant-evaluated, the non_constant_p should be\n>>>>> set true.\n>>>>\n>>>> Please add a testcase?  If this is supposed to improve the test in\n>>>> [basic.contract.eval]/8, then I don't see how, since with this patch\n>>>> we emit the error twice:\n>>>>\n>>>> q.C:3:3: error: contract condition is not constant\n>>>>       3 |   contract_assert((++const_cast<int&>(i), true));\n>>>>         |   ^~~~~~~~~~~~~~~\n>>>> q.C:3:3: error: contract condition is not constant\n>>>>\n>>>> instead of just once as before.  That doesn't seem like an improvement.\n>>>\n>>> Yes, the patch was wrong.The reason for the test case failure may be that\n>>> constexpr_call did not cache contract violation information.\n>>> The new patch as following.\n>>>\n>>> ----------------8<-----------------\n>>>\n>>> I noticed that the following code passed.\n>>>\n>>> 1 | consteval void foo( auto x ) pre( false ) { return x; }\n>>> 2 |\n>>> 3 | static_assert (foo( 1 ) == 1, \"\");\n>>> 4 |\n>>> 5 | int main() {\n>>> 6 |   foo( 1 );\n>>> 7 | }\n>>>\n>>> However, the code has a contract violation.\n>>> In constexpr_call, the result will be cached, but not cache the\n>>> existence of contract violations.\n>>>\n>>> gcc/cp/ChangeLog:\n>>>   * constexpr.cc (cxx_eval_constant_expression): cache failed or\n>>>   no constant contract statement\n>>\n>> I think it would be simpler to set cacheable to false if there are\n>> contract violations.\n> \n> It's great! The change is very small.\n> And I notice that:\n> \n> basic.contract.eval.p8.C: In function 'int main()':\n> basic.contract.eval.p8.C:8:30: error: contract predicate is false in constant expression\n>      8 | consteval void foo( auto x ) pre( false ) {}\n>        |                              ^~~~~~~~~~~~\n> basic.contract.eval.p8.C:11:6: error: call to consteval function 'foo<int>(42)' is not a constant expression\n>     11 |   foo( 42 ); // { dg-error {call to consteval function 'foo<int>\\(42\\)' is not a constant expression} }\n>        |   ~~~^~~~~~\n> basic.contract.eval.p8.C:8:30: error: contract predicate is false in constant expression\n>      8 | consteval void foo( auto x ) pre( false ) {}\n>        |                              ^~~~~~~~~~~~\n> \n> The error report abort contract violations appeared twice. It's expected?\n> If it only needs to be reported once, consider adding the following code in the function `check_for_failed_contracts`.\n> \n>    if (!ctx->quiet)\n>      return error;\n> \n> How abort it?\n\nUnfortunately that would risk never getting an error in some other \nsituations, like\n\nconstexpr int f() pre(false) { return 42; }\ntemplate <int I> struct A { };\ntemplate <class T = A<f()>> void f() { }\nint main() { f(); }\n\nwould just give a strange \"could not convert\" error.  So let's leave \nfixing the redundant errors for a later patch.\n\nThis change looks good, just some formatting issues:\n\n> ----------------8<-----------------\n> \n> I noticed that the following code passed.\n> \n> 1 | consteval void foo( auto x ) pre( false ) { return x; }\n> 2 |\n> 3 | static_assert (foo( 1 ) == 1, \"\");\n> 4 |\n> 5 | int main() {\n> 6 |   foo( 1 );\n> 7 | }\n> \n> However, the code has contract violations.\n> In constexpr_call, a result with contract violations should\n> not be cached.\n> \n> gcc/cp/ChangeLog:\n> \t* constexpr.cc (cxx_eval_constant_expression): donot cache\n> \tresult with contract violation.\n\nChange descriptions should start with a capital letter.  You're also \nmissing a space in \"Do not\".\n\n> gcc/testsuite/ChangeLog:\n> \t* g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C: New\n\nChange descriptions should also end in a period.\n\n> ---\n>   gcc/cp/constexpr.cc                               |  4 ++++\n>   .../contracts/cpp26/basic.contract.eval.p8-3.C    | 15 +++++++++++++++\n>   2 files changed, 19 insertions(+)\n>   create mode 100644 gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\n> \n> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc\n> index a593b66dd41..efb7a42014b 100644\n> --- a/gcc/cp/constexpr.cc\n> +++ b/gcc/cp/constexpr.cc\n> @@ -4639,6 +4639,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,\n>   \t  /* Only cache a permitted result of a constant expression.  */\n>   \t  if (cacheable && !reduced_constant_expression_p (result))\n>   \t    cacheable = false;\n> +\n> +\t  /* Only cache a result without contract violations */\n\nAnd comments end in a period and two spaces before */\n\n> +\t  if (cacheable && ctx->global->contract_statement)\n> +\t    cacheable = false;\n>   \t}\n>         else\n>   \t/* Couldn't get a function copy to evaluate.  */\n> diff --git a/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C b/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\n> new file mode 100644\n> index 00000000000..869d7dc35e6\n> --- /dev/null\n> +++ b/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\n> @@ -0,0 +1,15 @@\n> +// basic.contract.eval/p8\n> +// If a contract violation occurs in a context that is manifestly\n> +// constant-evaluated ([expr.const]), and the evaluation semantic is a\n> +// terminating semantic, the program is ill-formed.\n> +// { dg-do compile { target c++23 } }\n> +// { dg-additional-options \"-fcontracts  \" }\n\nWhy spaces after the option?\n\n> +\n> +consteval auto foo( auto x ) pre( false ) { return x; }\n> +// { dg-error {contract predicate is false in constant expression} \"\" { target *-*-* } .-1 }\n> +\n> +static_assert(foo( 42 ) == 42, \"\");\n> +\n> +int main() {\n> +  foo( 42 ); // { dg-error {call to consteval function 'foo<int>\\(42\\)' is not a constant expression} }\n> +}\n> --\n> 2.43.0\n>","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=JaFca0mI;\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=JaFca0mI","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.133.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 4g5Vyh60mgz1yHv\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 07:37:48 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id D3CCE4BBC0BD\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 29 Apr 2026 21:37:46 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.124])\n by sourceware.org (Postfix) with ESMTP id 4FB944BB3BC4\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 21:37:08 +0000 (GMT)","from mail-qt1-f199.google.com (mail-qt1-f199.google.com\n [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-90-OE8E40JTNbqrxL8qDACZZA-1; Wed, 29 Apr 2026 17:37:02 -0400","by mail-qt1-f199.google.com with SMTP id\n d75a77b69052e-50f817c3240so1533591cf.1\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 14:37:02 -0700 (PDT)","from [192.168.50.130]\n (130-44-146-247.s12789.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com.\n [130.44.146.247]) by smtp.gmail.com with ESMTPSA id\n 6a1803df08f44-8b3ff482682sm213526d6.48.2026.04.29.14.36.58\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Wed, 29 Apr 2026 14:36:59 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org D3CCE4BBC0BD","OpenDKIM Filter v2.11.0 sourceware.org 4FB944BB3BC4"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 4FB944BB3BC4","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 4FB944BB3BC4","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777498628; cv=none;\n b=TV/41c/UZoTMjMtPYOuHWuK1dl1zJO+/7fT5jtmLDWHgT4GZkfBqC/o9J3RGDoCOKPcN6kuYKT6hCqV7PapEDLZYicTL2plnWBKGGaN99PfgKVPaSbi9ooAiinHXkSywbwpqE4FBqlOiwYF346LDW1j4t8ojtP7tD/I8FC/hswk=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777498628; c=relaxed/simple;\n bh=RSPJZ+EDCeM6IwxMWis0Bot6GOmFq/0463Whcm6EVvU=;\n h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From;\n b=cZqsVzXAhA1I+m5ngXZLPN8PfVVVUYTV4lPkGyB0th19f5DCTozIONVPe/2DPo1+7wJjve3TuzJkDQLPTb1uvsxWMkeFMwj9A0qJngZMtMOg/Oj2439De8LERMGC8WfrYj8PWzrlKNacx6H/V5bAA+ht2k5o5HQKBWRLmupOpCI=","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=1777498628;\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 content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=DVK9jGpmIMQcOgkf9dFjqf5SfEyzIowxBjqrf69uk+Q=;\n b=JaFca0mI1l8u0Qwd7gGFIWwbnaASDL4zRSD1wU09uhKYasmn50SBqHzEz6pY5MxLrSENFu\n 50OkmJ5krHt/59Z6g0ePZf9J5YBrF2jg8SrgjdYhYnZVZCAxd643VUtZTD1jjhC+6KLtQb\n sEg21dtBv6B/2gHfSLQIYYOBzi8qUP8=","X-MC-Unique":"OE8E40JTNbqrxL8qDACZZA-1","X-Mimecast-MFC-AGG-ID":"OE8E40JTNbqrxL8qDACZZA_1777498621","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777498621; x=1778103421;\n h=content-transfer-encoding:in-reply-to:from:content-language\n :references:cc:to:subject:user-agent:mime-version:date:message-id\n :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id\n :reply-to;\n bh=DVK9jGpmIMQcOgkf9dFjqf5SfEyzIowxBjqrf69uk+Q=;\n b=O0dNfC+dqrCcEpfxNzDsL/l2AUihL4U3dnyLWU6EEs740+cehSliw93R+GFs4mhnDw\n 19coAbp9TF5qDUHLMeLKKmOYoeIoyYnlxnP6h7kEDmHBuOgXoj3v7bM4jwoTXLpoVN3D\n q4vOcZKMTOH5aEv8Fb2RLV4660xSbbpXKLkILtL93SNtf83x5PiNv0EwU3vOTYyP2uhQ\n prCS0Y+3Px10Ee4EtoTA9o7VkOXe41+Yk02XJ+BiOl/lhobOeGMVIVzLBhFE//rJVuaQ\n NMCs+2zrELVHWHITUYNtHoho52j70MqWaYtiMe/Tvo951+6wjTJ0yjC3oUfD4NYwm4f1\n wBbg==","X-Gm-Message-State":"AOJu0Yyqp4a/sXnDBRDkOPSu+p9kYkqXtcO8RXUX3ueCmW5MslHEdzJc\n 3uxxxLbzeIA/MzVVnItrrCL2nxwIK/z1w2fL1x9bbcxoc42WzYQ2HYsMk9ShcHVkZV+ktM2kb2E\n SRs/P6Tm5ac08z9DOgI0HeaIy91zJBj+PTwggE6etVulOlsZ35z7cgv1ku8E=","X-Gm-Gg":"AeBDievjgNZRzUBX14plkf09aiy63EXzQ1M3rSnKJb6Eg9AgdOXWwe0agXOvsCrVsYF\n 6gau+GtpkA0qh9geOu8hXOsZmKdoYbC42NRLPCDvu6s9ncS7AVD+2wm0W9/au12lxvA85flEB6e\n KfFrPyAIJUyxYSSNRaxpPvSwdDoBWzdSc8ETBcIUbYuCFbD9xIyn6dMjr24PdghhWd7PFjbbAhI\n i+Z8+ATjfcenC/iJLkVH+mUiCStpN20hDaqXqZHhhKiESA80DZ6h8hsYsQu7GoihlgGte2329KD\n Y6TH2vb8fWCgL4wa1O6qH94j/qv0YzKgetUx7W7gDRfQegrkrwyGBObjuCdU8rav6M/lh4ym7ts\n 17YLPuTWAIR6sosO7foMqgQ3OYbCD9WPdQln0Ou25Lbnv9r79gQaGID9oDd7KRwAsn+XvfoXOvK\n 0yjREfn9VvpCDqxuJpGgy6YjIDV8y29LFxAN8u3Sbw8A==","X-Received":["by 2002:a05:622a:16:b0:50d:8e2c:6a68 with SMTP id\n d75a77b69052e-5102ae4a7f7mr2441621cf.51.1777498621384;\n Wed, 29 Apr 2026 14:37:01 -0700 (PDT)","by 2002:a05:622a:16:b0:50d:8e2c:6a68 with SMTP id\n d75a77b69052e-5102ae4a7f7mr2441231cf.51.1777498620866;\n Wed, 29 Apr 2026 14:37:00 -0700 (PDT)"],"Message-ID":"<b62e5130-b908-4e00-a70e-c76919444473@redhat.com>","Date":"Wed, 29 Apr 2026 17:36:58 -0400","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v5] c++, contracts: fix testsuite basic.contract.eval.p8\n failed","To":"yxj-github-437 <2457369732@qq.com>","Cc":"gcc-patches@gcc.gnu.org, nathan@acm.org, ppalka@gcc.gnu.org","References":"<a49890f7-d8b9-4b40-bf35-de461b42fb3d@redhat.com>\n <tencent_6643D30414E51F9CE20CC76E6C2C65AB3D05@qq.com>","From":"Jason Merrill <jason@redhat.com>","In-Reply-To":"<tencent_6643D30414E51F9CE20CC76E6C2C65AB3D05@qq.com>","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"Xxk_5v7FXtUmuyM01vf5npolqM5AXxXTcwhsot9oup0_1777498621","X-Mimecast-Originator":"redhat.com","Content-Language":"en-US","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","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"}},{"id":3684809,"web_url":"http://patchwork.ozlabs.org/comment/3684809/","msgid":"<tencent_B317ED339AACA003FF2014EB381D63042908@qq.com>","list_archive_url":null,"date":"2026-04-30T15:52:18","subject":"Re: [PATCH v5] c++,\n contracts: fix testsuite basic.contract.eval.p8 failed","submitter":{"id":89506,"url":"http://patchwork.ozlabs.org/api/people/89506/","name":"yxj-github-437","email":"2457369732@qq.com"},"content":"From: 2457369732@qq.com (yxj-github-437)\n\n>>>>>> If in a context that is manifestly constant-evaluated ([expr.const]),\n>>>>>> a contract violation occurs and the evaluation semantic is terminating\n>>>>>> or contract stmt isnot constant-evaluated, the non_constant_p should be\n>>>>>> set true.\n>>>>>\n>>>>> Please add a testcase?  If this is supposed to improve the test in\n>>>>> [basic.contract.eval]/8, then I don't see how, since with this patch\n>>>>> we emit the error twice:\n>>>>>\n>>>>> q.C:3:3: error: contract condition is not constant\n>>>>>       3 |   contract_assert((++const_cast<int&>(i), true));\n>>>>>         |   ^~~~~~~~~~~~~~~\n>>>>> q.C:3:3: error: contract condition is not constant\n>>>>>\n>>>>> instead of just once as before.  That doesn't seem like an improvement.\n>>>>\n>>>> Yes, the patch was wrong.The reason for the test case failure may be that\n>>>> constexpr_call did not cache contract violation information.\n>>>> The new patch as following.\n>>>>\n>>>> ----------------8<-----------------\n>>>>\n>>>> I noticed that the following code passed.\n>>>>\n>>>> 1 | consteval void foo( auto x ) pre( false ) { return x; }\n>>>> 2 |\n>>>> 3 | static_assert (foo( 1 ) == 1, \"\");\n>>>> 4 |\n>>>> 5 | int main() {\n>>>> 6 |   foo( 1 );\n>>>> 7 | }\n>>>>\n>>>> However, the code has a contract violation.\n>>>> In constexpr_call, the result will be cached, but not cache the\n>>>> existence of contract violations.\n>>>>\n>>>> gcc/cp/ChangeLog:\n>>>>   * constexpr.cc (cxx_eval_constant_expression): cache failed or\n>>>>   no constant contract statement\n>>>\n>>> I think it would be simpler to set cacheable to false if there are\n>>> contract violations.\n>> \n>> It's great! The change is very small.\n>> And I notice that:\n>> \n>> basic.contract.eval.p8.C: In function 'int main()':\n>> basic.contract.eval.p8.C:8:30: error: contract predicate is false in constant expression\n>>      8 | consteval void foo( auto x ) pre( false ) {}\n>>        |                              ^~~~~~~~~~~~\n>> basic.contract.eval.p8.C:11:6: error: call to consteval function 'foo<int>(42)' is not a constant expression\n>>     11 |   foo( 42 ); // { dg-error {call to consteval function 'foo<int>\\(42\\)' is not a constant expression} }\n>>        |   ~~~^~~~~~\n>> basic.contract.eval.p8.C:8:30: error: contract predicate is false in constant expression\n>>      8 | consteval void foo( auto x ) pre( false ) {}\n>>        |                              ^~~~~~~~~~~~\n>> \n>> The error report abort contract violations appeared twice. It's expected?\n>> If it only needs to be reported once, consider adding the following code in the function `check_for_failed_contracts`.\n>> \n>>    if (!ctx->quiet)\n>>      return error;\n>> \n>> How abort it?\n>\n> Unfortunately that would risk never getting an error in some other \n> situations, like\n>\n> constexpr int f() pre(false) { return 42; }\n> template <int I> struct A { };\n> template <class T = A<f()>> void f() { }\n>.int main() { f(); }\n>\n> would just give a strange \"could not convert\" error.  So let's leave \n>.fixing the redundant errors for a later patch.\n>\n> This change looks good, just some formatting issues:\n\nOK, the formatting issues are fixed.\n\n----------------8<-----------------\n\nI noticed that the following code passed.\n\n1 | consteval void foo( auto x ) pre( false ) { return x; }\n2 |\n3 | static_assert (foo( 1 ) == 1, \"\");\n4 |\n5 | int main() {\n6 |   foo( 1 );\n7 | }\n\nHowever, the code has contract violations.\nIn constexpr_call, a result with contract violations should\nnot be cached.\n\ngcc/cp/ChangeLog:\n\t* constexpr.cc (cxx_eval_constant_expression): Do not cache\n\tresult with contract violation.\ngcc/testsuite/ChangeLog:\n\t* g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C: New test.\n---\n gcc/cp/constexpr.cc                               |  4 ++++\n .../contracts/cpp26/basic.contract.eval.p8-3.C    | 15 +++++++++++++++\n 2 files changed, 19 insertions(+)\n create mode 100644 gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\n\ndiff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc\nindex a593b66dd41..efb7a42014b 100644\n--- a/gcc/cp/constexpr.cc\n+++ b/gcc/cp/constexpr.cc\n@@ -4639,6 +4639,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,\n \t  /* Only cache a permitted result of a constant expression.  */\n \t  if (cacheable && !reduced_constant_expression_p (result))\n \t    cacheable = false;\n+\n+\t  /* Only cache a result without contract violations.  */\n+\t  if (cacheable && ctx->global->contract_statement)\n+\t    cacheable = false;\n \t}\n       else\n \t/* Couldn't get a function copy to evaluate.  */\ndiff --git a/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C b/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\nnew file mode 100644\nindex 00000000000..869d7dc35e6\n--- /dev/null\n+++ b/gcc/testsuite/g++.dg/contracts/cpp26/basic.contract.eval.p8-3.C\n@@ -0,0 +1,15 @@\n+// basic.contract.eval/p8\n+// If a contract violation occurs in a context that is manifestly\n+// constant-evaluated ([expr.const]), and the evaluation semantic is a\n+// terminating semantic, the program is ill-formed.\n+// { dg-do compile { target c++23 } }\n+// { dg-additional-options \"-fcontracts\" }\n+\n+consteval auto foo( auto x ) pre( false ) { return x; }\n+// { dg-error {contract predicate is false in constant expression} \"\" { target *-*-* } .-1 }\n+\n+static_assert(foo( 42 ) == 42, \"\");\n+\n+int main() {\n+  foo( 42 ); // { dg-error {call to consteval function 'foo<int>\\(42\\)' is not a constant expression} }\n+}\n--\n2.43.0","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=qq.com header.i=@qq.com header.a=rsa-sha256\n header.s=s201512 header.b=R0lV7krk;\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=qq.com header.i=@qq.com header.a=rsa-sha256\n header.s=s201512 header.b=R0lV7krk","sourceware.org;\n dmarc=pass (p=quarantine dis=none) header.from=qq.com","sourceware.org; spf=pass smtp.mailfrom=qq.com","server2.sourceware.org;\n arc=none smtp.remote-ip=203.205.221.149"],"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 4g5zGd1QZ8z1xqf\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 01 May 2026 01:53:11 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id C4D8A43B5533\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 15:53:09 +0000 (GMT)","from out203-205-221-149.mail.qq.com (out203-205-221-149.mail.qq.com\n [203.205.221.149])\n by sourceware.org (Postfix) with UTF8SMTPS id 0A66D43B5533;\n Thu, 30 Apr 2026 15:52:32 +0000 (GMT)","from localhost ([2409:8962:d44:f8:9cae:88ff:fe7d:fc65])\n by newxmesmtplogicsvrsza53-0.qq.com (NewEsmtp) with SMTP\n id D14AE497; Thu, 30 Apr 2026 23:52:20 +0800"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org C4D8A43B5533","OpenDKIM Filter v2.11.0 sourceware.org 0A66D43B5533"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 0A66D43B5533","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 0A66D43B5533","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777564354; cv=none;\n b=vUdw4o2DLs6LuT6zYrzaVEX8rQvmtv49ACi2eNPro1CKEanQTAkWmW/bDqdF4QFg036BmPmUyPudCbMF6dZFj1ZNt+Bu/w88BK05h+Xa/96LFO+ZBX2A1VpxVMFpRsj3bOiUxq8k2avfFtsXXI8K+C+8yYTmYp7uZcQLdjUdtl8=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777564354; c=relaxed/simple;\n bh=Pknxr6qEuLA6VJp7fNK1q5BSzFShLvw/wRiBKYcghdA=;\n h=DKIM-Signature:Message-ID:From:To:Subject:Date:MIME-Version;\n b=itI7AFL9xorkvK4ka2He3C5h09GK6JTAmOn94PV5mfX3TC+oodv9ujg9voIYpOkHnAL9VNMBoMveJ2X0ARCiwm/WqwPfX8HyTGtwDvPoQst0/1rg6VvK92Yr3LGtdcIOK6qTgMy1MC5pMggLidwA+7BfbRWonc5gllUKZ6O4n8Q=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512;\n t=1777564342; bh=T4mj6GYtPMo4cMtXMI3J9FfEd87xUv1/LghNkWTmy54=;\n h=From:To:Cc:Subject:Date:In-Reply-To:References;\n b=R0lV7krk4tghkgmVzTs/pGr1+j7qxDHJ3svItwP/oQs8hGzi8LN6vouAfJcLIJgY7\n h6xyqyyKV0OkP+oaKfSgUJWWiogCmQX0qxb6Ou03Nd8Qz4EkUm5C4eEXxiavnflqWQ\n JKFBIbCMr8lIadpUiLyFf0XX5yBohG8LeHE0Z7E0=","X-QQ-mid":"xmsmtpt1777564340tas4clago","Message-ID":"<tencent_B317ED339AACA003FF2014EB381D63042908@qq.com>","X-QQ-XMAILINFO":"NMx5osnRqFlcKSiHjcYnbcfewFd5fZOdYKOv0OeV24pr8OSflgyw7K3ZYK/WmS\n 56X0eZb5CgWOhs6SeQJz2GC9FeGN6ujk8Emqd72sPh42zYNnefmAnK1R/u5BJ/zUqwpVHWF1t6OB\n QUndPF1cb1mizw75v835fwYnsAQsAFlQT40cOhe94W0T5CLjdY9K5Q2A7yxaeYl/P2tj+3qm/YxS\n aFy+taZu+kE5rzzEgRsIYD1T6lM46VbEk8h5Vowuy+X00k2/V18bN1NqywzPPDeUrTTNniSAqL/F\n YpVtJ58bAY2yyAuJwfLp0h/0ONri3iamQxr6VInOjTZZIx/ORLRemkM90lCNH2bi82wULh5tXfcY\n QkUmSBUGnfEO1zLufQS1MxsPsmtd1fXd1tZ8XiSYAs7IKCV7ckUKZEpl4ktFoC96bJtSDYlIZZE5\n ZNHBKIuuck0tIGbzsn47I9gGP0VJTMOxNIUG0d1oS47Wh4VsH2hF9js0fAw+e/tIQCROaA4ZQ622\n dnLQ6GDhieQtAOEE98fJqoOMWp7jk1ER4GstZ/xG8f1Y5ehTX8i+3faqSBhhq+AAx2zhAKkn4CCy\n sa34QI5Ll1aW/fT3io74mrl6aP/ZOPZc9jpvlUCTDh+HokLVjUCdr5oWBa3PypjnIL8JCjn6YwIv\n BnNi5B5I/Vvb6W8q97V84pXmI3ktSmuckrSb+dytYxz+Nvtw6Hmn0Notb/KknvC6y7ktjoSDtRqD\n kRvw/q9Xgmq6ErLAipt/h9pH3VKyfYG8f49+VGfmGR8OwpkR8fUajyAywwer0n8iZeDUpURSR+XN\n z2G7WeXa8lDyz0Zf0Z/1nyM+7+izmj5vaBncY/kOSM+mNE/atwHzHh1xvAhnpIaE72j6yZkfvx06\n +EQnfoCeDbsfqB+IOOgjsuFIlsVbLLvH0jgctN/O/bBANip1w9TAJny0dn6QTPD5C9qN0qYs3xMt\n ji6kNHTxifOyiGYqjNWipsGugQiXD/S3NdKBTMbvlaxByx3BCDxtnLaU9l7jLUVKzICdar9CfkNp\n ZRaCexg3uKXdXb81SQ8UZUtr4/0NuhKzhtcnQzDw==","X-QQ-XMRINFO":"Mp0Kj//9VHAxzExpfF+O8yhSrljjwrznVg==","From":"yxj-github-437 <2457369732@qq.com>","To":"jason@redhat.com","Cc":"gcc-patches@gcc.gnu.org, nathan@acm.org, ppalka@gcc.gnu.org,\n 2457369732@qq.com (yxj-github-437)","Subject":"Re: [PATCH v5] c++,\n contracts: fix testsuite basic.contract.eval.p8 failed","Date":"Thu, 30 Apr 2026 23:52:18 +0800","X-OQ-MSGID":"<20260430155218.9086-1-2457369732@qq.com>","X-Mailer":"git-send-email 2.54.0","In-Reply-To":"<b62e5130-b908-4e00-a70e-c76919444473@redhat.com>","References":"<b62e5130-b908-4e00-a70e-c76919444473@redhat.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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"}}]