public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned>
@ 2024-06-26 17:23 redi at gcc dot gnu.org
  2024-06-26 17:30 ` [Bug libstdc++/115668] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-26 17:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

            Bug ID: 115668
           Summary: Cannot format chrono::duration<unsigned>
           Product: gcc
           Version: 13.3.1
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

std::format("", std::chrono::duration<unsigned>{}) fails with:

/home/jwakely/gcc/15/include/c++/15.0.0/bits/chrono_io.h:1699:44: error: no
matching function for call to ‘abs(const std::chrono::duration<unsigned int>&)’
 1699 |           return _M_f._M_format(chrono::abs(__d), __fc, __d <
__d.zero());
      |                                 ~~~~~~~~~~~^~~~~

chrono::abs is only defined for durations with signed rep types.


This fixes it:

--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -1696,10 +1696,10 @@ namespace __format
        format(const chrono::duration<_Rep, _Period>& __d,
               basic_format_context<_Out, _CharT>& __fc) const
        {
-         const bool __is_neg = __d < __d.zero();
-         if (__is_neg)
-           __d = -__d;
-         return _M_f._M_format(__d, __fc, __is_neg);
+         if (__d < __d.zero())
+           return _M_f._M_format(-__d, __fc, true);
+         else
+           return _M_f._M_format(__d, __fc, false);
        }

     private:

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
@ 2024-06-26 17:30 ` redi at gcc dot gnu.org
  2024-06-26 17:31 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-26 17:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
   Last reconfirmed|                            |2024-06-26
             Status|UNCONFIRMED                 |ASSIGNED
   Target Milestone|---                         |13.4
     Ever confirmed|0                           |1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
  2024-06-26 17:30 ` [Bug libstdc++/115668] " redi at gcc dot gnu.org
@ 2024-06-26 17:31 ` redi at gcc dot gnu.org
  2024-06-27  8:48 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-26 17:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Oops, the diff above is against an incomplete fix I already had in my git
index, this is the proper fix:

--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -1696,7 +1696,10 @@ namespace __format
        format(const chrono::duration<_Rep, _Period>& __d,
               basic_format_context<_Out, _CharT>& __fc) const
        {
-         return _M_f._M_format(chrono::abs(__d), __fc, __d < __d.zero());
+         if (__d < __d.zero())
+           return _M_f._M_format(-__d, __fc, true);
+         else
+           return _M_f._M_format(__d, __fc, false);
        }

     private:

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
  2024-06-26 17:30 ` [Bug libstdc++/115668] " redi at gcc dot gnu.org
  2024-06-26 17:31 ` redi at gcc dot gnu.org
@ 2024-06-27  8:48 ` cvs-commit at gcc dot gnu.org
  2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-27  8:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:dafa750c8a6f0a088677871bfaad054881737ab1

commit r15-1690-gdafa750c8a6f0a088677871bfaad054881737ab1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jun 26 20:22:54 2024 +0100

    libstdc++: Fix std::format for chrono::duration with unsigned rep
[PR115668]

    Using std::chrono::abs is only valid if numeric_limits<rep>::is_signed
    is true, so using it unconditionally made it ill-formed to format a
    duration with an unsigned rep.

    The duration formatter might as negate the duration itself instead of
    using chrono::abs, because it already needs to check for a negative
    value.

    libstdc++-v3/ChangeLog:

            PR libstdc++/115668
            * include/bits/chrono_io.h (formatter<duration<R,P, C>::format):
            Do not use chrono::abs.
            * testsuite/20_util/duration/io.cc: Check formatting a duration
            with unsigned rep.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-06-27  8:48 ` cvs-commit at gcc dot gnu.org
@ 2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
  2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
  2024-06-28 19:40 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-28 19:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-14 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:d5e352addf4c17d28bcada42409881089c5d8088

commit r14-10356-gd5e352addf4c17d28bcada42409881089c5d8088
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jun 26 20:22:54 2024 +0100

    libstdc++: Fix std::format for chrono::duration with unsigned rep
[PR115668]

    Using std::chrono::abs is only valid if numeric_limits<rep>::is_signed
    is true, so using it unconditionally made it ill-formed to format a
    duration with an unsigned rep.

    The duration formatter might as well negate the duration itself instead
    of using chrono::abs, because it already needs to check for a negative
    value.

    libstdc++-v3/ChangeLog:

            PR libstdc++/115668
            * include/bits/chrono_io.h (formatter<duration<R,P, C>::format):
            Do not use chrono::abs.
            * testsuite/20_util/duration/io.cc: Check formatting a duration
            with unsigned rep.

    (cherry picked from commit dafa750c8a6f0a088677871bfaad054881737ab1)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
@ 2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
  2024-06-28 19:40 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-28 19:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:4b64a1051a927a65a9acefbbb5714a8118c320bc

commit r13-8877-g4b64a1051a927a65a9acefbbb5714a8118c320bc
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jun 26 20:22:54 2024 +0100

    libstdc++: Fix std::format for chrono::duration with unsigned rep
[PR115668]

    Using std::chrono::abs is only valid if numeric_limits<rep>::is_signed
    is true, so using it unconditionally made it ill-formed to format a
    duration with an unsigned rep.

    The duration formatter might as well negate the duration itself instead
    of using chrono::abs, because it already needs to check for a negative
    value.

    libstdc++-v3/ChangeLog:

            PR libstdc++/115668
            * include/bits/chrono_io.h (formatter<duration<R,P, C>::format):
            Do not use chrono::abs.
            * testsuite/20_util/duration/io.cc: Check formatting a duration
            with unsigned rep.

    (cherry picked from commit dafa750c8a6f0a088677871bfaad054881737ab1)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/115668] Cannot format chrono::duration<unsigned>
  2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
@ 2024-06-28 19:40 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-28 19:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 13.4 and 14.2

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-06-28 19:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 17:23 [Bug libstdc++/115668] New: Cannot format chrono::duration<unsigned> redi at gcc dot gnu.org
2024-06-26 17:30 ` [Bug libstdc++/115668] " redi at gcc dot gnu.org
2024-06-26 17:31 ` redi at gcc dot gnu.org
2024-06-27  8:48 ` cvs-commit at gcc dot gnu.org
2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
2024-06-28 19:39 ` cvs-commit at gcc dot gnu.org
2024-06-28 19:40 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).