From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id C85BD385702B; Tue, 2 May 2023 20:25:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C85BD385702B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683059150; bh=A59jZz5bscIGgabZmx20qTXQLTDM96YSg6uhe7f1Qxs=; h=From:To:Subject:Date:From; b=AZeV+pSI2BwDb+WT9NS+jjGWibfuNyedow5dJoW0/zl/j7JCrdiAXj9UBECMcDskg 72Gtc2buk0VL52NCx/Lj0YoijzBd6pX/jWa1fMIinSdsjxClnqd6iMgqBN9TyJZI7X RJ/n020PTN7I49rOcF3uTyzxUQ4gNBo9m4VTEPWc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-7277] c++: fix 'unsigned typedef-name' extension [PR108099] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 6b927b1297e66e26e62e722bf15c921dcbbd25b9 X-Git-Newrev: a713aa4f47ac1efbfe2338790b0b5e94a233fb99 Message-Id: <20230502202550.C85BD385702B@sourceware.org> Date: Tue, 2 May 2023 20:25:50 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a713aa4f47ac1efbfe2338790b0b5e94a233fb99 commit r13-7277-ga713aa4f47ac1efbfe2338790b0b5e94a233fb99 Author: Jason Merrill Date: Tue Apr 18 21:32:07 2023 -0400 c++: fix 'unsigned typedef-name' extension [PR108099] In the comments for PR108099 Jakub provided some testcases that demonstrated that even before the regression noted in the patch we were getting the semantics of this extension wrong: in the unsigned case we weren't producing the corresponding standard unsigned type but another distinct one of the same size, and in the signed case we were just dropping it on the floor and not actually returning a signed type at all. The former issue is fixed by using c_common_signed_or_unsigned_type instead of unsigned_type_for, and the latter issue by adding a (signed_p && typedef_decl) case. This patch introduces a failure on std/ranges/iota/max_size_type.cc due to the latter issue, since the testcase expects 'signed rep_t' to do something sensible, and previously we didn't. Now that we do, it exposes a bug in the __max_diff_type::operator>>= handling of sign extension: when we evaluate -1000 >> 2 in __max_diff_type we keep the MSB set, but leave the second-most-significant bit cleared. PR c++/108099 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Don't clear typedef_decl after 'unsigned typedef' pedwarn. Use c_common_signed_or_unsigned_type. Also handle 'signed typedef'. gcc/testsuite/ChangeLog: * g++.dg/ext/int128-8.C: New test. * g++.dg/ext/unsigned-typedef2.C: New test. * g++.dg/ext/unsigned-typedef3.C: New test. Diff: --- gcc/cp/decl.cc | 14 +++++++------- gcc/testsuite/g++.dg/ext/int128-8.C | 24 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/ext/unsigned-typedef2.C | 25 +++++++++++++++++++++++++ gcc/testsuite/g++.dg/ext/unsigned-typedef3.C | 25 +++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 772c059dc2c..71d33d2b7a4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -12478,16 +12478,14 @@ grokdeclarator (const cp_declarator *declarator, { if (typedef_decl) { - pedwarn (loc, OPT_Wpedantic, "%qs specified with %qD", + pedwarn (loc, OPT_Wpedantic, + "%qs specified with typedef-name %qD", key, typedef_decl); ok = !flag_pedantic_errors; + /* PR108099: __int128_t comes from c_common_nodes_and_builtins, + and is not built as a typedef. */ if (is_typedef_decl (typedef_decl)) type = DECL_ORIGINAL_TYPE (typedef_decl); - else - /* PR108099: __int128_t comes from c_common_nodes_and_builtins, - and is not built as a typedef. */ - type = TREE_TYPE (typedef_decl); - typedef_decl = NULL_TREE; } else if (declspecs->decltype_p) error_at (loc, "%qs specified with %", key); @@ -12540,7 +12538,7 @@ grokdeclarator (const cp_declarator *declarator, else if (type == char_type_node) type = unsigned_char_type_node; else if (typedef_decl) - type = unsigned_type_for (type); + type = c_common_unsigned_type (type); else type = unsigned_type_node; } @@ -12554,6 +12552,8 @@ grokdeclarator (const cp_declarator *declarator, type = long_integer_type_node; else if (short_p) type = short_integer_type_node; + else if (signed_p && typedef_decl) + type = c_common_signed_type (type); if (decl_spec_seq_has_spec_p (declspecs, ds_complex)) { diff --git a/gcc/testsuite/g++.dg/ext/int128-8.C b/gcc/testsuite/g++.dg/ext/int128-8.C new file mode 100644 index 00000000000..07535a9820e --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/int128-8.C @@ -0,0 +1,24 @@ +// PR c++/108099 +// { dg-do compile { target { c++11 && int128 } } } +// { dg-options "" } + +using u128 = unsigned __int128_t; +using s128 = signed __int128_t; +template struct integral_constant { + static constexpr T value = v; +}; +typedef integral_constant false_type; +typedef integral_constant true_type; +template +struct is_same : false_type {}; +template +struct is_same : true_type {}; +static_assert (is_same <__int128, s128>::value, ""); +static_assert (is_same ::value, ""); +static_assert (is_same <__int128_t, s128>::value, ""); +static_assert (is_same ::value, ""); +static_assert (is_same <__uint128_t, u128>::value, ""); +static_assert (sizeof (s128) == sizeof (__int128), ""); +static_assert (sizeof (u128) == sizeof (unsigned __int128), ""); +static_assert (s128(-1) < 0, ""); +static_assert (u128(-1) > 0, ""); diff --git a/gcc/testsuite/g++.dg/ext/unsigned-typedef2.C b/gcc/testsuite/g++.dg/ext/unsigned-typedef2.C new file mode 100644 index 00000000000..936c0ccb748 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/unsigned-typedef2.C @@ -0,0 +1,25 @@ +// PR c++/108099 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +typedef long long t64; +template struct integral_constant { + static constexpr T value = v; +}; +typedef integral_constant false_type; +typedef integral_constant true_type; +template +struct is_same : false_type {}; +template +struct is_same : true_type {}; + +using s64 = signed t64; +static_assert (is_same ::value, ""); +static_assert (is_same ::value, ""); +static_assert (sizeof (s64) == sizeof (long long), ""); +static_assert (s64(-1) < 0, ""); + +using u64 = unsigned t64; +static_assert (is_same ::value, ""); +static_assert (sizeof (u64) == sizeof (unsigned long long), ""); +static_assert (u64(-1) > 0, ""); diff --git a/gcc/testsuite/g++.dg/ext/unsigned-typedef3.C b/gcc/testsuite/g++.dg/ext/unsigned-typedef3.C new file mode 100644 index 00000000000..bb99ca0ccc9 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/unsigned-typedef3.C @@ -0,0 +1,25 @@ +// PR c++/108099 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +typedef unsigned long long t64; +template struct integral_constant { + static constexpr T value = v; +}; +typedef integral_constant false_type; +typedef integral_constant true_type; +template +struct is_same : false_type {}; +template +struct is_same : true_type {}; + +using s64 = signed t64; +static_assert (is_same ::value, ""); +static_assert (is_same ::value, ""); +static_assert (sizeof (s64) == sizeof (long long), ""); +static_assert (s64(-1) < 0, ""); + +using u64 = unsigned t64; +static_assert (is_same ::value, ""); +static_assert (sizeof (u64) == sizeof (unsigned long long), ""); +static_assert (u64(-1) > 0, "");