From patchwork Mon Feb 24 16:44:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1243309 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-520016-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.a=rsa-sha1 header.s=default header.b=fpbyf4kT; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YcWK4DZl; 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 48R7GG27W8z9sP7 for ; Tue, 25 Feb 2020 03:45:07 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=AzVsQ3OUv6k+VnPK 0OP2A8ylFdDdnEchkY/YxQYX6aTq9dH9/1WcXNj+wM8IxAYwdAs+39A/tm70fAbR hYrY30XZwkzIOy69Swiv6ZPTbB83vIz86qo8GUA60El1k+3cXG8wa0wXzVf4TAdh Y3R5iB+YH2apLISpcu3eP7+Kd8A= 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=AOjlpb6mz0RzENg6+K/BHb tFZ1Y=; b=fpbyf4kT4+wPPP32edS9eAaWsqU+pGO//ACPLqJjyL/yPLiM2JAVoI vI+icA9YXEl9IBxyLoZp89ukbJ5QTHyFMhY8Mf1t3PP7sx5Yd8r2+FbBoOuzmlzO hU5l1qgh/+3qC+yGsBDDYPZ6+p/KJIHrx8sZR8vbCzs1DdwSEi/Ak= Received: (qmail 75466 invoked by alias); 24 Feb 2020 16:44:59 -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 75429 invoked by uid 89); 24 Feb 2020 16:44:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 24 Feb 2020 16:44:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582562696; 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=R6ivZ2FAuu30CFxUZ8KciuvvcE83cMq03oMTexf5rtQ=; b=YcWK4DZl8Vu04Q3NLpscuHasHsJtUMujRbHwAO2mw063NfyJ1OJU3B6SmunS5fsooxKlHJ QjR7m06jXVrq60zDe4jWb05Q0TwKAA3VdefZ+hJAGcw8QoePe+ywaNu+r7oJBAoF9VlLpJ 4m5HLt8nMlBDfPwOYotS1DjlWFTUijw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-93-W6Lkm4IaPBitPQ4JZWgxcw-1; Mon, 24 Feb 2020 11:44:52 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1F3BB8017DF for ; Mon, 24 Feb 2020 16:44:51 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-127-158.rdu2.redhat.com [10.10.127.158]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88D6F60BF7; Mon, 24 Feb 2020 16:44:50 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Fix ICE with constexpr init and [[no_unique_address]] [PR93803] Date: Mon, 24 Feb 2020 11:44:40 -0500 Message-Id: <20200224164440.390576-1-polacek@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Here we crash when constexpr-initializing a class member of empty class type with [[no_unique_address]]. Without the attribute we would have a ctor (that initializes bar) of the form { .D.2173 = { .x = {} } } but with the attribute reduced_constant_expression_p gets { .x = {} } That means that "idx != field" is true for the latter and we see that foo, the base class of bar, is an empty class, so we want to look at the next initializable field (since empty class fields may not have an initializer). But in this case there are no more, therefore accessing DECL_CHAIN (field) crashes. Long story short, we need to avoid a crash on a null field when we're initializing a class that only contains an empty base class. While poking into this I discovered c++/93898, but that's a different problem. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2020-02-24 Marek Polacek PR c++/93803 - ICE with constexpr init and [[no_unique_address]]. * constexpr.c (reduced_constant_expression_p): Don't crash on a null field. * g++.dg/cpp2a/constexpr-init16.C: New test. * g++.dg/cpp2a/constexpr-init17.C: New test. --- gcc/cp/constexpr.c | 16 +++++++--------- gcc/testsuite/g++.dg/cpp2a/constexpr-init16.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/cpp2a/constexpr-init17.C | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-init16.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-init17.C base-commit: 004f2c07b6d3fa543f0fe86c55a7b3c227de2bb6 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 128f760778b..3da1609cdaa 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -2603,16 +2603,14 @@ reduced_constant_expression_p (tree t) element. */ if (!reduced_constant_expression_p (val)) return false; + /* Empty class field may or may not have an initializer. */ + for (; field && idx != field; + field = next_initializable_field (DECL_CHAIN (field))) + if (!is_really_empty_class (TREE_TYPE (field), + /*ignore_vptr*/false)) + return false; if (field) - { - /* Empty class field may or may not have an initializer. */ - for (; idx != field; - field = next_initializable_field (DECL_CHAIN (field))) - if (!is_really_empty_class (TREE_TYPE (field), - /*ignore_vptr*/false)) - return false; - field = next_initializable_field (DECL_CHAIN (field)); - } + field = next_initializable_field (DECL_CHAIN (field)); } /* There could be a non-empty field at the end. */ for (; field; field = next_initializable_field (DECL_CHAIN (field))) diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-init16.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-init16.C new file mode 100644 index 00000000000..16db2974f2a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-init16.C @@ -0,0 +1,15 @@ +// PR c++/93803 - ICE with constexpr init and [[no_unique_address]]. +// { dg-do compile { target c++2a } } + +struct empty { }; + +struct foo { + [[no_unique_address]] empty x; + constexpr foo() : x{} { } +}; + +struct bar : foo { + using foo::foo; +}; + +constexpr bar a{}; diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-init17.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-init17.C new file mode 100644 index 00000000000..34d9fe58ecb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-init17.C @@ -0,0 +1,15 @@ +// PR c++/93803 - ICE with constexpr init and [[no_unique_address]]. +// { dg-do compile { target c++2a } } + +struct empty { }; + +struct foo { + [[no_unique_address]] empty x, x2, x3; + constexpr foo() : x{}, x2{} { } +}; + +struct bar : foo { + using foo::foo; +}; + +constexpr bar a{};