From patchwork Fri Apr 21 19:32:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1772108 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=E7gRyqAK; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q34Sg2Bsmz23tD for ; Sat, 22 Apr 2023 05:33:23 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 445FA3857732 for ; Fri, 21 Apr 2023 19:33:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 445FA3857732 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682105601; bh=GfxKQk5IRHWHsvZ3Do/pGzERvUcmuDwriVj+WLhXBq0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=E7gRyqAKcpHeCNG4pyEQQ684jko4iCJ6DjQg5wDb7ZK+ABuex6yeH0X8zKt1CbxeV 8h8uqiIYTZ015FVv/gs7OWetxYpzFKggjz6o9Xu1ReDb2ygLzT0yYVkiQisNTYOVq2 TAk+MOr4r9bI9LEfEoMyPCiBV15+fEB1yZjwlrDU= 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 BA1BD3857354 for ; Fri, 21 Apr 2023 19:32:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA1BD3857354 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-86-5gtbesRVP0uAwR4VhX4Flg-1; Fri, 21 Apr 2023 15:32:31 -0400 X-MC-Unique: 5gtbesRVP0uAwR4VhX4Flg-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-5ef40d2af43so13887416d6.3 for ; Fri, 21 Apr 2023 12:32:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682105550; x=1684697550; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GfxKQk5IRHWHsvZ3Do/pGzERvUcmuDwriVj+WLhXBq0=; b=QbgW089lmxmlChitQCO/lrLY00mb+9Sp3X195dQMl+5prbWoFmiCGvzv8mgKpPnO2M Jos0cHcjrjVaKNWp6MhfcO6N/ZDBMu+Y9Ad3Lo6fC9CAwNU/jWGVC/nCEChwRzmtw7ul hjQB6BhszhIz4uO3nuseqXgXAuUt1bg/0iDBWkU2WglnNFuvOwHkztoG/qhURF7fNRhb Vz7ezpcEptuBVvtFpKGkQwEi68pY9n01DgXJUlqOrlxVfqa3fYw28dN13mzPWlJGwEEk tmXkH/C3FFSuiS7jvEyqEcdftUd+rISImTfRjmKgEQ4mYIPWL8xZoHPYPKkc7Nlirw7F 0lVw== X-Gm-Message-State: AAQBX9eUAoxEYbRu+YJkvNDfbOpNCxTZPIVhohvXmayGqTGJpDlfu6B1 Z5KDOW9hS6JP4TMxCHx0tBYVy/Z/XOuaGKEloOyaH4aeVKyFZR5i9OGW/ByDV5hOn2OLvc3ZVzD sipwmhjCoO0UbaCbkTkOgyxCSoE+G1wTXmHKtxqA5cBpCtZhdwqKGaRIZMvkNZ14FNTeuqH/Idw == X-Received: by 2002:ad4:594b:0:b0:56a:b623:9b09 with SMTP id eo11-20020ad4594b000000b0056ab6239b09mr9268677qvb.14.1682105550433; Fri, 21 Apr 2023 12:32:30 -0700 (PDT) X-Google-Smtp-Source: AKy350anvGmjRKYDWxXRemouUzoriPVtd+p3mLyXlqh0C+Wv1u89M5AtclOT1PJR55dI0HMCgqIxrA== X-Received: by 2002:ad4:594b:0:b0:56a:b623:9b09 with SMTP id eo11-20020ad4594b000000b0056ab6239b09mr9268628qvb.14.1682105549816; Fri, 21 Apr 2023 12:32:29 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id r7-20020a0ce287000000b00606750abaf9sm591397qvl.136.2023.04.21.12.32.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Apr 2023 12:32:29 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: fix 'unsigned typedef-name' extension [PR108099] Date: Fri, 21 Apr 2023 15:32:27 -0400 Message-Id: <20230421193227.1044332-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- 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: Remove xfailed dg-bogus markers. * g++.dg/ext/unsigned-typedef2.C: New test. * g++.dg/ext/unsigned-typedef3.C: New test. --- gcc/cp/decl.cc | 18 +++++++------- gcc/testsuite/g++.dg/ext/int128-8.C | 4 ++-- gcc/testsuite/g++.dg/ext/unsigned-typedef2.C | 25 ++++++++++++++++++++ gcc/testsuite/g++.dg/ext/unsigned-typedef3.C | 25 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/unsigned-typedef2.C create mode 100644 gcc/testsuite/g++.dg/ext/unsigned-typedef3.C base-commit: 1d2aa9a8cb8fd078af930347392cfdfc14e1551c diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index ab5cb69b2ae..71d33d2b7a4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -12478,18 +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); - typedef_decl = NULL_TREE; - } - else - /* PR108099: __int128_t comes from c_common_nodes_and_builtins, - and is not built as a typedef. */ - type = TREE_TYPE (typedef_decl); + type = DECL_ORIGINAL_TYPE (typedef_decl); } else if (declspecs->decltype_p) error_at (loc, "%qs specified with %", key); @@ -12542,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; } @@ -12556,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 index 7e909d50873..07535a9820e 100644 --- a/gcc/testsuite/g++.dg/ext/int128-8.C +++ b/gcc/testsuite/g++.dg/ext/int128-8.C @@ -16,8 +16,8 @@ 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, ""); // { dg-bogus "" "" { xfail *-*-* } } -static_assert (is_same <__uint128_t, u128>::value, ""); // { dg-bogus "" "" { xfail *-*-* } } +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, ""); 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, "");