Message ID | 20231024161843.20031-1-polacek@redhat.com |
---|---|
State | Accepted |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2056409vqx; Tue, 24 Oct 2023 09:19:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9RHzAArz1le6kExy9idwJN+zpZkhBdpJCI35+bY5FcnXKsfN9TWHHBv5wsh6pyIPHahxG X-Received: by 2002:ac8:5fc7:0:b0:415:1752:1be5 with SMTP id k7-20020ac85fc7000000b0041517521be5mr13834759qta.31.1698164356020; Tue, 24 Oct 2023 09:19:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698164356; cv=pass; d=google.com; s=arc-20160816; b=IYWqzkkSgr9WxiNw5hWJftS23/kHyI8/Zx4mRFGrymhSRk6sC7ZnX/AisL7i1/nCYa PEIWIT4seneKJ5luYGKFs67a5e1m6MgpaF7zYT1zoV6w1OlJkUwltAVZ2UPVDgfh6Htl tA5AOksbpazOUnZlpMAC4MsFQRa5l88218Rp9/dRtvo6e7mHew01hC47tKQ8MP2C3t+g 7YiAFtcUGAFOGEZCJWFu4QaaJpqhbInWfqU7RHR2MQfF/V2ifECCm/lcb922AvAu4qSn iO+LNctGFZiY/mT/+o71MIX66lnzlH0XB0O6f3W3HOKwDatFneuiQm8NtgsonlXDUY+k PZdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=AEs8/X/NRTdOmKpbB/wWKS/gzi1YbG3YAcrSKsUmYzM=; fh=fpCc4VkYXmQrurtjjuGMl3oKQaOAjGwXgTAjHxumZ4s=; b=esrI4f2urH6GftKQRcZWEqmNmnlXWZO/lI7KRQxbdO9Hj6NTycKiV2r32W1i21Bk9F GBcpkyIPrEWggUmBIKpfLJ6xyHecJd5n86UcW8Qm7fK87zD/orNU9ocUFBykPfDxYcee pb0L7eadGPlDKDetMNWlybb0FWGFjM3yMrwx0ZtC1D/6pdLBiJpuvC/t3P9cRfa7Sd8b Mwl90Kxf0iWKOj6T614uYdo7QASi01MFSURNH1/DtEz7MlENIWosQMfEDP3B52wEMLHR lAHniuQusV8/9zApfcW8CBz9SqH2NC7zRNAqeyJqiBPHaEmOSTTx3Q66lY11bEp2/eBz V6Vg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="b+Rhl/B7"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h22-20020ac87d56000000b0041cbcce90c1si7587817qtb.531.2023.10.24.09.19.15 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 09:19:16 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="b+Rhl/B7"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADBA83857713 for <ouuuleilei@gmail.com>; Tue, 24 Oct 2023 16:19:15 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 438533858C2B for <gcc-patches@gcc.gnu.org>; Tue, 24 Oct 2023 16:18:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 438533858C2B 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 438533858C2B 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=1698164333; cv=none; b=KhMHQa+6E+7p9s/0SzvRRetpwWYI0tpuKbBbBQkatsvt8sUkY4yX4thmRkdUq9vYoMAMQKPyZ0hj3Y/sU+I+MjS12IVCj0tr2+aMb2y1hFBdlcp0PT+ZGLh500Wv7AmwekYGw4A//J309nBTK6Dp8xmxmpjGG0aZj0n5BoldqmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698164333; c=relaxed/simple; bh=TMRmgjyOr/jvsrbezan64CzDO7jnFZOV81f2fg1IbaM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=O7rKUCEPAXLMgYJBOfG3e0zVNbq5xIeEs1CpkTw8jPu7CruknBcAgwCl3xMDLQUu0MIIkzAZ4L3Y+Szllmc3gSTH8347XdYOCwivCh7qkpVjIdpL0qqEimgJBF9nOnJMfgtpKOxyC43AkORt+bBtiR/l8rlMaa43Ng38prHTJ+U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698164330; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AEs8/X/NRTdOmKpbB/wWKS/gzi1YbG3YAcrSKsUmYzM=; b=b+Rhl/B7abo3daqm71Z4nKB1uKL6ng/3iknjieO/HTB1mfhycj8nOwj/EN67xsXdx5+pS6 2PHKuVrjGNOTRid9j6Qwaz/papTrS+8HirZQijdu5PmMr7lXoEDUOvY4KcD9uiPyDxAAyM OZe8wth7rksJCvsKt7n67+pp9TqCyLQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-425-rcUASASbOgKweu-3EEhpTA-1; Tue, 24 Oct 2023 12:18:49 -0400 X-MC-Unique: rcUASASbOgKweu-3EEhpTA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4364F383DC5E for <gcc-patches@gcc.gnu.org>; Tue, 24 Oct 2023 16:18:49 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.33.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1EB9D40C6F79; Tue, 24 Oct 2023 16:18:49 +0000 (UTC) From: Marek Polacek <polacek@redhat.com> To: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org> Subject: [PATCH] c++: error with bit-fields and scoped enums [PR111895] Date: Tue, 24 Oct 2023 12:18:43 -0400 Message-ID: <20231024161843.20031-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.3 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780654387668933212 X-GMAIL-MSGID: 1780654387668933212 |
Series |
c++: error with bit-fields and scoped enums [PR111895]
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patch-check | success | Github commit url |
Commit Message
Marek Polacek
Oct. 24, 2023, 4:18 p.m. UTC
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we issue a bogus error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' when casting a bit-field of scoped enum type to bool. In build_static_cast_1, perform_direct_initialization_if_possible returns NULL_TREE, because the invented declaration T t(e) fails, which is correct. So we go down to ocp_convert, which has code to deal with this case: /* We can't implicitly convert a scoped enum to bool, so convert to the underlying type first. */ if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); but the SCOPED_ENUM_P is false since intype is <unnamed-unsigned:2>. This could be fixed by using unlowered_expr_type. But then c_common_truthvalue_conversion/CASE_CONVERT has a similar problem, and unlowered_expr_type is a C++-only function. Rather than adding a dummy unlowered_expr_type to C, I think we should follow [expr.static.cast]p3: "the lvalue-to-rvalue conversion is applied to the bit-field and the resulting prvalue is used as the operand of the static_cast." There are no prvalue bit-fields, so the l-to-r conversion will get us an expression whose type is the enum. (I thought we didn't need decay_conversion because that does a whole lot more but using it would make sense to me too.) PR c++/111895 gcc/cp/ChangeLog: * typeck.cc (build_static_cast_1): Call convert_bitfield_to_declared_type. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/scoped_enum12.C: New test. --- gcc/cp/typeck.cc | 9 +++++++++ gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C base-commit: 99a6c1065de2db04d0f56f4b2cc89acecf21b72e
Comments
On 10/24/23 12:18, Marek Polacek wrote: > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > Here we issue a bogus error: invalid operands of types 'unsigned char:2' > and 'int' to binary 'operator!=' when casting a bit-field of scoped enum > type to bool. > > In build_static_cast_1, perform_direct_initialization_if_possible returns > NULL_TREE, because the invented declaration T t(e) fails, which is > correct. So we go down to ocp_convert, which has code to deal with this > case: > /* We can't implicitly convert a scoped enum to bool, so convert > to the underlying type first. */ > if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) > e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); > but the SCOPED_ENUM_P is false since intype is <unnamed-unsigned:2>. > This could be fixed by using unlowered_expr_type. But then > c_common_truthvalue_conversion/CASE_CONVERT has a similar problem, and > unlowered_expr_type is a C++-only function. > > Rather than adding a dummy unlowered_expr_type to C, I think we should > follow [expr.static.cast]p3: "the lvalue-to-rvalue conversion is applied > to the bit-field and the resulting prvalue is used as the operand of the > static_cast." There are no prvalue bit-fields, so the l-to-r conversion > will get us an expression whose type is the enum. (I thought we didn't > need decay_conversion because that does a whole lot more but using it > would make sense to me too.) It's possible that we might want some of that more, particularly mark_rvalue_use; decay_conversion seems like the right answer. OK with that change. rvalue() would also make sense, though that seems to be missing a call to unlowered_expr_type at the moment. In fact, after "otherwise, it's the lvalue-to-rvalue conversion" in decay_conv should probably just be a call to rvalue, with missing bits added to the latter function. Jason
On Tue, Oct 24, 2023 at 04:46:02PM -0400, Jason Merrill wrote: > On 10/24/23 12:18, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Here we issue a bogus error: invalid operands of types 'unsigned char:2' > > and 'int' to binary 'operator!=' when casting a bit-field of scoped enum > > type to bool. > > > > In build_static_cast_1, perform_direct_initialization_if_possible returns > > NULL_TREE, because the invented declaration T t(e) fails, which is > > correct. So we go down to ocp_convert, which has code to deal with this > > case: > > /* We can't implicitly convert a scoped enum to bool, so convert > > to the underlying type first. */ > > if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) > > e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); > > but the SCOPED_ENUM_P is false since intype is <unnamed-unsigned:2>. > > This could be fixed by using unlowered_expr_type. But then > > c_common_truthvalue_conversion/CASE_CONVERT has a similar problem, and > > unlowered_expr_type is a C++-only function. > > > > Rather than adding a dummy unlowered_expr_type to C, I think we should > > follow [expr.static.cast]p3: "the lvalue-to-rvalue conversion is applied > > to the bit-field and the resulting prvalue is used as the operand of the > > static_cast." There are no prvalue bit-fields, so the l-to-r conversion > > will get us an expression whose type is the enum. (I thought we didn't > > need decay_conversion because that does a whole lot more but using it > > would make sense to me too.) > > It's possible that we might want some of that more, particularly > mark_rvalue_use; decay_conversion seems like the right answer. OK with that > change. Makes total sense, thank you. (I'd tested the version with decay_conversion and it worked fine.) > rvalue() would also make sense, though that seems to be missing a call to > unlowered_expr_type at the moment. In fact, after "otherwise, it's the > lvalue-to-rvalue conversion" in decay_conv should probably just be a call to > rvalue, with missing bits added to the latter function. Sounds good; I hope I'll get to it next week. I'm not going to make it part of this patch so that I can backport this one to 13 and leave the cleanup for trunk only. Marek
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index f3dc80c40cf..50427090e5d 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8405,6 +8405,15 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, return expr; if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) expr = TREE_OPERAND (expr, 0); + /* [expr.static.cast]: "If the value is not a bit-field, the result + refers to the object or the specified base class subobject thereof; + otherwise, the lvalue-to-rvalue conversion is applied to the + bit-field and the resulting prvalue is used as the operand of the + static_cast." There are no prvalue bit-fields; the l-to-r conversion + will give us an object of the underlying type of the bit-field. We + can let convert_bitfield_to_declared_type convert EXPR to the desired + type. */ + expr = convert_bitfield_to_declared_type (expr); return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); } diff --git a/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C new file mode 100644 index 00000000000..1d10431e6dc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C @@ -0,0 +1,8 @@ +// PR c++/111895 +// { dg-do compile { target c++11 } } + +enum class o_field : unsigned char { no, yes, different_from_s }; +struct fields { + o_field o : 2; +}; +bool func(fields f) { return static_cast<bool>(f.o); }