From patchwork Thu Dec 30 19:55:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ed Catmur X-Patchwork-Id: 49415 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 952A23858414 for ; Thu, 30 Dec 2021 19:56:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from forward4-smtp.messagingengine.com (forward4-smtp.messagingengine.com [66.111.4.238]) by sourceware.org (Postfix) with ESMTPS id 109363858D28 for ; Thu, 30 Dec 2021 19:56:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 109363858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=catmur.uk Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=catmur.uk Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailforward.nyi.internal (Postfix) with ESMTP id 26F4919408C1 for ; Thu, 30 Dec 2021 14:56:36 -0500 (EST) Received: from imap51 ([10.202.2.101]) by compute5.internal (MEProxy); Thu, 30 Dec 2021 14:56:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=aPZsgjMok7zWC8xXaySfOxf3ilsS1 2pH2e3cMgMAis8=; b=gWKiIYRk4reqF72sWXhQtjZpFO8GiPC5vgZgrtNoAVh6W tHTzFngWa0AZZ9jOgWhXKGBU0XnkbBIyzHfWVN75lF6Wfef5I0cY/zPL5hfWsphy ClLxlUEMxF0wjRmHN9yYOd8iFnaWNwPA4w3V47T+fy2Dq0KTUBeBZ93ox86GSZ8y Ve4WZG/wmuk6GqhXo8LkQXFx6WGCYKvdMnuofjwh/j/QLoGk0IvFvY8Tvpd9DZ7t oeqW6QQHNpcCqTSg36ot7ghEtdkWqe/uZedRC46lEOD8w/yE3NVdUFxwLYE7cbil aa8ZsVG/OY3YFlwGlYtAbKSOmFVONXtAqXZJJaJtQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddruddvfedgudeffecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkfffhvffutgesthdtre dtreertdenucfhrhhomhepfdfgugcuvegrthhmuhhrfdcuoegvugestggrthhmuhhrrdhu kheqnecuggftrfgrthhtvghrnhepudehtdefffdtffeujedviefggfetffegleeffeffle eihffgffeiueekgedtheehnecuffhomhgrihhnpehophgvnhdqshhtugdrohhrghdpghhn uhdrohhrghdpshhtrggtkhhovhgvrhhflhhofidrtghomhenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegvugestggrthhmuhhrrdhukh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id DEF7A1BC006D; Thu, 30 Dec 2021 14:56:35 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-4525-g8883000b21-fm-20211221.001-g8883000b Mime-Version: 1.0 Message-Id: Date: Thu, 30 Dec 2021 19:55:41 +0000 From: "Ed Catmur" To: gcc-patches@gcc.gnu.org Subject: [PATCH 0/3] c++: expr-cast - C-style cast conformance [PR77465] X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, SPF_NEUTRAL, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch series improves conformance to the C++ standard for C-style casts (explicit cast notation, [expr.cast]) in two scenarios. First, as discussed in CWG 909 [1], a C-style cast may invoke a user-defined conversion (as a static_cast) followed by a const_cast; currently this is erroneously rejected, tracked in PR77465 [2]. Second, a C-style cast to non-const reference to arithmetic type can and thus should be interpreted as a static_cast reference binding to temporary followed by a const_cast; currently it is interpreted as a reinterpret_cast (possibly followed by const_cast). To the best of my knowledge this has not been reported as a defect; credit to Turtlefight on Stack Overflow [3] for analyzing the situation, drawing my attention to the above issues, and convincing me that my interpretation was correct. Note that C-style cast to /const/ arithmetic reference will already introduce a temporary; this patch only changes the behavior where the reference is to non-const. Clearly there is the potential for miscompilation of existing code that assumes an incorrect interpretation (reinterpret_cast); a number of existing test cases are amended in this patch series. In consideration of this, I have checked bootstrap (on x86_64) with no new errors, and add a warning (to Wconversion) where a C-style cast to arithmetic reference introduces a temporary in a situation where the user may expect a reinterpret_cast (type-punning); this affects both the const (existing) and non-const case. Note that this change will not affect C code compiled as C++, since that code cannot contain user-defined conversions or references. In this patch series, * 1/3 - allow C-style cast to invoke user-defined conversion followed by const_cast, resolving PR77465; adds a test case and fixes existing test cases which expect this to be rejected * 2/3 - allow C-style cast to non-const reference to arithmetic type to invoke static_cast reference binding to temporary followed by const_cast; adds a test case and fixes existing test cases which expect reinterpret_cast semantics * 3/3 - add comprehensive test suite covering expr.cast scenarios, positive and negative. 1. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#909 2. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77465 3. https://stackoverflow.com/a/70400230/567292