public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Hans-Peter Nilsson <hp@axis.com>
Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org, cassio.neri@gmail.com
Subject: Re: [PATCH/RFA] libstdc++: provide conversion from day, month to unsigned long, PR99301
Date: Sat, 27 Feb 2021 12:54:46 +0000	[thread overview]
Message-ID: <20210227125446.GJ3008@redhat.com> (raw)
In-Reply-To: <20210227114206.C3F46203B5@pchp3.se.axis.com>

[-- Attachment #1: Type: text/plain, Size: 1496 bytes --]

On 27/02/21 12:42 +0100, Hans-Peter Nilsson via Libstdc++ wrote:
>Since 97d6161f6a7fa712 / r11-7370 "libstdc++: More efficient
>days from date" I see an additional 81 testsuite-errors for
>cris-elf, with this in g++.log for one randomly picked
>regressing test:
>
>FAIL: g++.dg/cpp1y/pr57640.C  -std=c++2a (test for excess errors)
>Excess errors:
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2483:25: error: invalid 'static_cast' from type 'const std::chrono::month' to type 'uint32_t' {aka 'long unsigned int'}
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2484:25: error: invalid 'static_cast' from type 'const std::chrono::day' to type 'uint32_t' {aka 'long unsigned int'}
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2496:69: error: no matching function for call to 'std::chrono::duration<long long int, std::ratio<86400> >::duration(<brace-enclosed initializer list>)'
>
>The commit shows conversions to uint32_t, which for
>e.g. x86_64-linux is "unsigned int", and there are explicit
>conversions to unsigned int for month and day (see patch
>context).
>
>But, "newlib ILP32 targets" have an uint32_t that is
>effectively typedef'd "long unsigned int" (see
>newlib-stdint.h UINT32_TYPE).
>
>Better provide an unsigned long conversion aside the
>unsigned ones.

No, the allowed conversions are specific by the standard. The right
fix is to convert to unsigned explicitly.

I've pushed the attached patch after testing on x86_64-linux. This
should work for newlib ILP32 targets too.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1369 bytes --]

commit 699672d4dccfb5579dbe48977bda86f6836225a0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat Feb 27 12:50:53 2021

    libstdc++: Fix conversions from date types to integers [PR 99301]
    
    The conversions to integer types are explicit, so need to use the
    correct type. Converting to uint32_t only works if that is the same type
    as unsigned.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/99301
            * include/std/chrono (year_month_day::_M_days_since_epoch()):
            Convert chrono::month and chrono::day to unsigned before
            converting to uint32_t.

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index fcdaee7328e..11729aae708 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -2496,8 +2496,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
 
       const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
-      const auto __m1 = static_cast<uint32_t>(_M_m);
-      const auto __d1 = static_cast<uint32_t>(_M_d);
+      const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
+      const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
 
       const auto __j  = static_cast<uint32_t>(__m1 < 3);
       const auto __y0 = __y1 - __j;

      reply	other threads:[~2021-02-27 12:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-27 11:42 Hans-Peter Nilsson
2021-02-27 12:54 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210227125446.GJ3008@redhat.com \
    --to=jwakely@redhat.com \
    --cc=cassio.neri@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hp@axis.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).