From patchwork Fri Mar 1 14:44:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 208887 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp1141992dyb; Fri, 1 Mar 2024 07:09:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXboege/pBxPs3jqfCN+YZ4YRx7BYS9aRXTg5g3SsLlEN7TtK7RWT+YO1Zeyve9kY1N2Q1kgMjAVWQpAyR1x35rcA+weA== X-Google-Smtp-Source: AGHT+IHxqEP5xcQZZAUsk/rN4CTnPUPd/8I5BtgrL89H16f8iE6mpxTvW2EaINjl1rqSoC2SDcgF X-Received: by 2002:a0c:ed46:0:b0:68f:cbdd:aaf2 with SMTP id v6-20020a0ced46000000b0068fcbddaaf2mr1950938qvq.33.1709305747349; Fri, 01 Mar 2024 07:09:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709305747; cv=pass; d=google.com; s=arc-20160816; b=p4dWtZN4ojyNGRDs+vewcNjkib60QnC4qGxlzxkTcGsD/L4w3DHaTZeys3j1L0ydc0 8mjLQY68kAPu7FDIfNy4xo+S8KZ/OMvW4zGB4BkB46xDamjgkn49FRB9ePfgJoL56vSy zJYUqrrl2ucoujFsZ1/UXkeTNn6DQJx33VALo3AU83WSxcPuMQzEos0h94Rxsmc2wyFJ dh6YYQ8YEpHBcS1iHr4Fqjd0qWOtOEE7b2MLcMGo/spVA2lOF45Sd/Ce22ULuy1j73Z3 4QHKNwOoO57e2T5MRoes0/pJgZ+GbheMgGsw9Y7poIXAXGJ6s17eUi1IABVDU8RdyuKa Dpfw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=JRNJMSwa5L6hKsysSP+X9YFhYAk9l+7vvGv2AEKYepM=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=DP3o8mHiLAnyF6I6pdjg2oIGiZg9bG3iqhKDw0FmoPOgQjQc4MOTJ/0nWJl7uqbb4+ V99mUZLZqil7F+L6Kyn0dKyk1KxNgRdgwhtLMNyWFuB8Fy0vVAKwOdz1jZ6vH6aNi9wB Le/UIzAwm0Yh4GddTbPyyIUZzgifyq8Scc75fMwqZLtmvjnhR4pIgbXl4kWj/jCi9wDF 0UwlJ7mZ/qc36dw44KMOblgx+4GSgFFlmKmKh/kZG06W/p0JabhZDLXIJchVpgZU7cqU U5HxcJvctV3w8xjEFsxWB9z46jO1ZGyVvkmDlRb25XKYN2ThL6y8QpRNVbRzsyzzvHj6 aPZw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zg9pCOFR; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b4-20020ac87544000000b0042e49a73daesi3355770qtr.385.2024.03.01.07.09.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 07:09:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zg9pCOFR; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 113463858C66 for ; Fri, 1 Mar 2024 15:09:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8F8D23858409 for ; Fri, 1 Mar 2024 15:08:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8F8D23858409 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8F8D23858409 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709305699; cv=none; b=GC7p0Yxecf/oxWMlDK5zMjMIAXNVG9rNn4cUInIf1JUIIkd2dL1GcFLEz6a5qXijFupkkTEty31w333HhA42bCDY1YpV0L1SK04h1P+xmUEs6eiC/C43P4JEUG/PeAjnFiRxb38mcblWps9HiGsSWw3Oig/qz+afrS4B4C/QuXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709305699; c=relaxed/simple; bh=K257Oc5y6U4K9yjLqErR5KfjX0AC8m275OSxbyZKHEY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dIg2NC0LoSr0wU0BEODSQ61VwThtudyHDnGTaqbX9ho48gtt+3+B1Zv/0YYvO3c7vfcms2PvrFU3Tu68fHQ8ZAGlK3x4DzK0IrmgltfF6Y8ReNkV0sD0d7RLvGYf+iWFwgpsbqzc2UwpcSv9kM6pya8PcOawzZMNcPjZq6jiuTQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709305696; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=JRNJMSwa5L6hKsysSP+X9YFhYAk9l+7vvGv2AEKYepM=; b=Zg9pCOFRlU8n4c/LChDOUvVtpoi29wYN+/82MJ6pU8cFP6iGqUhyZ3a6J6Js1AQGuaijX7 vlHUhfSVuceKShLaOTJHDmVYDsqqjVPWED5f/WZOAGA+rQy3wPZ6IXtpNISk6UoejxfA47 HDjn03V2fdsiYx0H62Mdrw9bHj+nWlQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-622-7HgALrUhPHCszxcgBEDgRA-1; Fri, 01 Mar 2024 10:08:14 -0500 X-MC-Unique: 7HgALrUhPHCszxcgBEDgRA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 00C7D280640F; Fri, 1 Mar 2024 15:08:14 +0000 (UTC) Received: from localhost (unknown [10.42.28.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id 921671121312; Fri, 1 Mar 2024 15:08:13 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Better diagnostics for std::format errors Date: Fri, 1 Mar 2024 14:44:30 +0000 Message-ID: <20240301150812.95213-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792336983383364279 X-GMAIL-MSGID: 1792336983383364279 Does the text of these new diagnostics look good? There are of course other ways for a type to be not-formattable (e.g. the formatter::format member doesn't return the right type or has some other kind of incorrect signature, or the formatter::parse member isn't constexpr) but we can't predict/detect them all reliably. This just attempts to give a user-friendly explanation for a couple of common mistakes. It should not have any false positives, because the basic_format_arg constructor requires __formattable_with<_Tp, _Context> so if either of these assertions fails, constructing __arg will fail too. The static_assert only adds a more readable error for a compilation that's going to fail anyway. Tested x86_64-linux. -- >8 -- This adds two new static_assert messages to the internals of std::make_format_args to give better diagnostics for invalid format args. Rather than just getting an error saying that basic_format_arg cannot be constructed, we get more specific errors for the cases where std::formatter isn't specialized for the type at all, and where it's specialized but only meets the BasicFormatter requirements and so can only format non-const arguments. Also add a test for the existing static_assert when constructing a format_string for non-formattable args. libstdc++-v3/ChangeLog: * include/std/format (_Arg_store::_S_make_elt): Add two static_assert checks to give more user-friendly error messages. * testsuite/lib/prune.exp (libstdc++-dg-prune): Prune another form of "in requirements with" note. * testsuite/std/format/arguments/args_neg.cc: Check for user-friendly diagnostics for non-formattable types. * testsuite/std/format/string_neg.cc: Likewise. --- libstdc++-v3/include/std/format | 13 +++++++ libstdc++-v3/testsuite/lib/prune.exp | 1 + .../std/format/arguments/args_neg.cc | 34 ++++++++++++++++++- .../testsuite/std/format/string_neg.cc | 4 +++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index ee189f9086c..1e839e88db4 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3704,6 +3704,19 @@ namespace __format static _Element_t _S_make_elt(_Tp& __v) { + using _Tq = remove_const_t<_Tp>; + using _CharT = typename _Context::char_type; + static_assert(is_default_constructible_v>, + "std::formatter must be specialized for the type " + "of each format arg"); + using __format::__formattable_with; + if constexpr (is_const_v<_Tp>) + if constexpr (!__formattable_with<_Tp, _Context>) + if constexpr (__formattable_with<_Tq, _Context>) + static_assert(__formattable_with<_Tp, _Context>, + "format arg must be non-const because its " + "std::formatter specialization has a " + "non-const reference parameter"); basic_format_arg<_Context> __arg(__v); if constexpr (_S_values_only) return __arg._M_val; diff --git a/libstdc++-v3/testsuite/lib/prune.exp b/libstdc++-v3/testsuite/lib/prune.exp index 24a15ccad22..071dcf34c1e 100644 --- a/libstdc++-v3/testsuite/lib/prune.exp +++ b/libstdc++-v3/testsuite/lib/prune.exp @@ -54,6 +54,7 @@ proc libstdc++-dg-prune { system text } { regsub -all "(^|\n)\[^\n\]*: . skipping \[0-9\]* instantiation contexts \[^\n\]*" $text "" text regsub -all "(^|\n)\[^\n\]*: in .constexpr. expansion \[^\n\]*" $text "" text regsub -all "(^|\n)\[^\n\]*: in requirements .with\[^\n\]*" $text "" text + regsub -all "(^|\n)\[^\n\]*: in requirements with\[^\n\]*" $text "" text regsub -all "(^|\n) inlined from \[^\n\]*" $text "" text # Why doesn't GCC need these to strip header context? regsub -all "(^|\n)In file included from \[^\n\]*" $text "" text diff --git a/libstdc++-v3/testsuite/std/format/arguments/args_neg.cc b/libstdc++-v3/testsuite/std/format/arguments/args_neg.cc index 16ac3040146..ded56fe63ab 100644 --- a/libstdc++-v3/testsuite/std/format/arguments/args_neg.cc +++ b/libstdc++-v3/testsuite/std/format/arguments/args_neg.cc @@ -6,7 +6,39 @@ std::string rval() { return "path/etic/experience"; } -void f() +void test_rval() { (void)std::make_format_args(rval()); // { dg-error "cannot bind non-const lvalue reference" } } + +void test_missing_specialization() +{ + struct X { }; + X x; + (void)std::make_format_args(x); // { dg-error "here" } +// { dg-error "std::formatter must be specialized" "" { target *-*-* } 0 } +} + +struct Y { }; +template<> class std::formatter { +public: + constexpr typename format_parse_context::iterator + parse(format_parse_context& c) + { return c.begin(); } + + template + typename C::iterator format(Y&, C&) const; +}; + +void test(std::formatter& f, std::format_parse_context& pc) { + f.parse(pc); +} + +void test_const_arg() +{ + const Y y; + (void)std::make_format_args(y); // { dg-error "here" } +// { dg-error "format arg must be non-const" "" { target *-*-* } 0 } +} + +// { dg-prune-output "no matching function for call to .*::basic_format_arg<" } diff --git a/libstdc++-v3/testsuite/std/format/string_neg.cc b/libstdc++-v3/testsuite/std/format/string_neg.cc index 69bcc736cff..d5963145b18 100644 --- a/libstdc++-v3/testsuite/std/format/string_neg.cc +++ b/libstdc++-v3/testsuite/std/format/string_neg.cc @@ -4,3 +4,7 @@ auto s = std::format(" {9} "); // { dg-error "call to consteval function" } // { dg-error "invalid.arg.id" "" { target *-*-* } 0 } + +struct X { }; +std::format_string str(""); // dg-error "here" } +// { dg-error "std::formatter must be specialized" "" { target *-*-* } 0 }