From patchwork Mon Jun 11 21:00:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 927925 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-479496-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="oukdVv+F"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 414QRT5p0Rz9rxs for ; Tue, 12 Jun 2018 07:01:23 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=d9m5UPJGsJV2E472mxJXlAHojvrYq31OFagnoFUdRCV3jT y4cb98w5btrRwkIInT5ssoNRwwIzjOxP5Je4e6Q5R4sFWftK1/mBsikbEQ7Tk2RL bWQ113nUs3QZvGwjrVZLua4FcJN4Veaw+jQxoJ11ux3TADfoD6N7O9myEFQbw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=ovRV7INGHkxZQ13mxqJuD5/0Ja8=; b=oukdVv+Fvy44PLiqF/Hq 6L3/LcUokuW85IBSDtWEebVJIgt0V7x3Sp55Hl2Q8E70S/QgfklvwE/p2wIy+wMK nrivmud19uBBVJbP0ifNkZL6TvmFH0WfJ8qRxn4dknSWsQG8lgjqOmQNCVQBDqiC WfzXc6ta5+3TyvlfnHjpivg= Received: (qmail 97700 invoked by alias); 11 Jun 2018 21:01:15 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 97649 invoked by uid 89); 11 Jun 2018 21:01:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-23.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 spammy=meeting X-HELO: mail-oi0-f48.google.com Received: from mail-oi0-f48.google.com (HELO mail-oi0-f48.google.com) (209.85.218.48) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 11 Jun 2018 21:01:08 +0000 Received: by mail-oi0-f48.google.com with SMTP id c6-v6so18070255oiy.0 for ; Mon, 11 Jun 2018 14:01:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=EcGsa3OdlGn6v6SZdoBgJLKhof3tT8XxhiCAtWdwDxQ=; b=BLoKwVPHaQrCFRGD8iyLvYGCv/+0t23yFwtEwCQlSZ51xA6Y5KkVNWgnyDgztgKwOF v1VjV6XXYzxhSPZM8eyPg+wU8bTTPaABJvyUdLUsjSuyUEbXS7eGcR015xx1bUZlrk67 nhFxTs7QDZ6kujOwE0znGtjyMAgJzdVKTqCrGIzmpNSyVF6aTvQ8cnRz5I8mzWi8uw5Q d/tySoK2ZaE/81Yhnysn4EUsAhNhiNAtXEDkoMQ7e+TvMtA/yH1F4+OFx+M/macg5i/G MjdsXnujZ8cdAKE9YrMIPt6/xDELy3Qvsjn8sgedekMdgTAP0coO3sDZXqyc9Da3S0kN 3D1g== X-Gm-Message-State: APt69E1qv3SytqmjuASoVOrRnfgnOieJ6bA1/tFQzoqDamDq0xbhrcxH 0RcFF17rTFILrif4EOfHI22LQgEBfR0jGpwAhJbyea/k X-Google-Smtp-Source: ADUXVKJUl0su7fhHKQOIGWB9SgyJ7Zmx5+j2jfmZ5q/2K5Hr0vXaxDxNu9FoyFrP2hC5m5H5a4xV3Op2XzzvOHA2/WM= X-Received: by 2002:aca:b58a:: with SMTP id e132-v6mr450155oif.8.1528750865513; Mon, 11 Jun 2018 14:01:05 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac9:dd0:0:0:0:0:0 with HTTP; Mon, 11 Jun 2018 14:00:44 -0700 (PDT) From: Jason Merrill Date: Mon, 11 Jun 2018 17:00:44 -0400 Message-ID: Subject: C++ PATCH for comparison of pointers to union members To: gcc-patches List X-IsSubscribed: yes At the C++ meeting last week it came up that pointers to different members of the same union are specified to compare as equal, which we were getting wrong. Tested x86_64-pc-linux-gnu, applying to trunk. commit bdaca661a7dd74d703f089af7b4484e098884510 Author: Jason Merrill Date: Tue Jun 5 15:57:06 2018 +0200 Fix ptrmem comparison for unions. * constexpr.c (cxx_eval_binary_expression): Special case comparison of pointers to members of the same union. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 944c1cdf11e..97a338535db 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -2051,8 +2051,22 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, if (TREE_CODE (lhs) == PTRMEM_CST && TREE_CODE (rhs) == PTRMEM_CST) - r = constant_boolean_node (cp_tree_equal (lhs, rhs) == is_code_eq, - type); + { + tree lmem = PTRMEM_CST_MEMBER (lhs); + tree rmem = PTRMEM_CST_MEMBER (rhs); + bool eq; + if (TREE_CODE (lmem) == TREE_CODE (rmem) + && TREE_CODE (lmem) == FIELD_DECL + && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE + && same_type_p (DECL_CONTEXT (lmem), + DECL_CONTEXT (rmem))) + /* If both refer to (possibly different) members of the same union + (12.3), they compare equal. */ + eq = true; + else + eq = cp_tree_equal (lhs, rhs); + r = constant_boolean_node (eq == is_code_eq, type); + } else if ((TREE_CODE (lhs) == PTRMEM_CST || TREE_CODE (rhs) == PTRMEM_CST) && (null_member_pointer_value_p (lhs) diff --git a/gcc/testsuite/g++.dg/expr/ptrmem10.C b/gcc/testsuite/g++.dg/expr/ptrmem10.C new file mode 100644 index 00000000000..71d2df85860 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/ptrmem10.C @@ -0,0 +1,28 @@ +/* [expr.eq] If both refer to (possibly different) members of the same union + (12.3), they compare equal. */ +// { dg-do run { target c++11 } } +// { dg-additional-options -O } + +union U +{ + int i; + int j; +}; + +#define SA(X) static_assert ((X),#X) +SA (&U::i == &U::j); +SA (!(&U::i != &U::j)); + +#define assert(X) do { if (!(X)) __builtin_abort(); } while(0) + +void f (int U::*p, int U::*q) +{ + assert (p==q); + assert (!(p!=q)); +} + +int main() +{ + assert (&U::i == &U::j); + assert (!(&U::i != &U::j)); +}