From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4A42B385DC05 for ; Thu, 7 Mar 2024 20:59:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A42B385DC05 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 4A42B385DC05 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709845193; cv=none; b=OJdIYHRpC2QGLUi5UrbGlWkYUPED7Hn/DwOnGvqOvkY+VyPyW6UvlmQupL6NcvDR2SXpEmzSoWbmzod1L0UEJBZCsbCnveRjenYCvEPjTwpUWi4fsSVqFhZXxVhUO8FCQrTO9mIQ6upc652ns2LmrOdxCgGsyOM1uqiULQALrIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709845193; c=relaxed/simple; bh=RI/DZ44UznaHom8+SoD2B08kZ9YI/2WzuA2vDD/KHFU=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=qITRVm56dvqjyDwFjMA1Q4nhn3ohs3adKUIdL4t4AHVaTsZOK3MhTFqJS8zKQm9DC4ax+b+lTVN2XJZ1Z8hCU5jNZKQF+06mzmNhKN9OwpvY5PD/tBQERZsbjSBQ3lbLSgdOzXy+fQTewz/KySBcPxKCeMqrgb9+QQNrc1QOXig= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709845189; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AeQk4+qdEB9KFqw6HbMMiT93CCXoKxGkwuF486Otyd4=; b=UQpmFAKk0bj7sTPcHuk/WpGzkQAkGtbtruJ8XaKsIr7z+dheBZC8bIFPCl/Dabmm9C8yJp 922VokYAiNl5QZ6ADzZz5nhsqnsZRa2GaENvQHAKHGN7IIb+o6wxm7rEsgUGSx6n3QRz9v Rer0ZQsYsxGfBwutEXT+NdTV9Jz4Lw4= Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-539-bsaCDxAVPm6oEbjUzdGw6A-1; Thu, 07 Mar 2024 15:59:47 -0500 X-MC-Unique: bsaCDxAVPm6oEbjUzdGw6A-1 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-609ff5727f9so7002907b3.0 for ; Thu, 07 Mar 2024 12:59:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709845186; x=1710449986; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AeQk4+qdEB9KFqw6HbMMiT93CCXoKxGkwuF486Otyd4=; b=KLnJNdFkrnxBoyRyABrySkYqNHfpsnZ1WlR3SHNyW/YbA+xiCSBNDhkuAwCqA5HT8Q C7OsMOx6hwzRCNtzalBCBvzX+cWGoEYg0SUX+Nml8b+LIXgXI/LfBz8H4b7sU40gaye+ UhUskQtZY9p96M7zJlPhnjhs8eOCVSr6misocYfKPkQ6Y7F08ds/FUQvCBf+wVZED2Fy RVsN4VIP5X3KduYf3tEB5MpAqySOvZjcwbdQaZn6BoFD6+WYqDW2Ixv/GRMh+CVwfG1A IcRsVld8xk+r8gmOLz4+c4NOX2G6z7GrXcTO4BNj+0p0kcZxVLkND8ms3Ohm2TJFZm2l IiZA== X-Gm-Message-State: AOJu0Yw1r8i190HKP8FRkhN7m9jUiSEeGjyeHnQVTpYSEyK6esslPgPT dcGReU6JVfqrEobCLzlDh9OmZA+VzWos3SrWAHdBb0s17lxvq5ZHWT5jjY7FUwrZg8B0C99Lfns pvoVIAkXlrGHM+3PeiHnk1Jik8mEsyVR9u5YiXrC/i/FhPOyxouG0fSaYt4WqXPO8vHFMa+zReW cR6I8BEfDW/dPfwa+tHJvtuZeA4RclEPFNico= X-Received: by 2002:a81:5293:0:b0:609:8cec:36a4 with SMTP id g141-20020a815293000000b006098cec36a4mr2066585ywb.19.1709845186412; Thu, 07 Mar 2024 12:59:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFQqpBO/YmCO4Y6SgF7JcwZCJHIMee9bREWUbhlvTD4+Qu5xui7xo5o8+KAa+708WMrGONfyCGdzkSQmKcHaXM= X-Received: by 2002:a81:5293:0:b0:609:8cec:36a4 with SMTP id g141-20020a815293000000b006098cec36a4mr2066572ywb.19.1709845186128; Thu, 07 Mar 2024 12:59:46 -0800 (PST) MIME-Version: 1.0 References: <20240301150812.95213-1-jwakely@redhat.com> In-Reply-To: <20240301150812.95213-1-jwakely@redhat.com> From: Jonathan Wakely Date: Thu, 7 Mar 2024 20:59:30 +0000 Message-ID: Subject: Re: [PATCH] libstdc++: Better diagnostics for std::format errors To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000f0cd7a0613185a52" X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,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 List-Id: --000000000000f0cd7a0613185a52 Content-Type: text/plain; charset="UTF-8" Pushed to trunk. On Fri, 1 Mar 2024 at 15:09, Jonathan Wakely wrote: > 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 } > -- > 2.43.2 > > --000000000000f0cd7a0613185a52--