From patchwork Fri Feb 10 18:49:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 726672 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3vKkXP5wNyz9s06 for ; Sat, 11 Feb 2017 05:50:13 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="aR1W+gcu"; dkim-atps=neutral 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=xpDTrKLTZnEJ7kZjJsV+k78tROu6CAXApUpYISwFf0tFjx ICFCsapnQeCQoij2l8DK0eIThmJX+G0Lm5f6grB8w5oi4dJPDcz2ddvOyMeHEEIc veU8qaEbNRPfBGcfHHdRlZsu2Eebug7pWwXrbcG5Lkwd44KjvCw7dugGCsyq4= 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=bOzVLkjymqQFBzt2RYXQm6JbuYA=; b=aR1W+gcu0ox7oQfQT0zc 1+Id5mieNreaukq19gaJqDws9oryZFdIQvR3dkI7usjAmp9GwDE/+Itjz/nARlqW WgVyEuy2RhSvyw6YeFUEh+kJU5XBEwlnublcMjUij7EFwheDwL36+tEco4ewNYE/ CIbALtTJJ4vtxf0/iReLVNk= Received: (qmail 86185 invoked by alias); 10 Feb 2017 18:50:04 -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 86172 invoked by uid 89); 10 Feb 2017 18:50:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=no version=3.3.2 spammy=cep, *valp, decl_context, DECL_CONTEXT X-HELO: mail-ot0-f175.google.com Received: from mail-ot0-f175.google.com (HELO mail-ot0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 10 Feb 2017 18:50:02 +0000 Received: by mail-ot0-f175.google.com with SMTP id 65so35024336otq.2 for ; Fri, 10 Feb 2017 10:50:02 -0800 (PST) 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=Km988J/F3alMxSRvin34StOyKaZg780EPk6MTnIx3MM=; b=RZsRrN4JiKn73/P1m1fCpKKGRqL1lUnyAq9Swhm9xghCie9zDMU9Qynh0kIJ9xGXg8 HwIjZbnbjosAfSN/aRpmZCNcg4QMgedbW+TgLs1AhVMrmxHQ/ppedwPUiYGVfCmG8n8O QaQbSeaiKsAQQnMkUU34Y8pjksJEByOrpbXvwtpdfPwUIBD6nwxIv8Qn8j957ZuQT7fp Z6LFJ+dSxc+p89QRJfiP3sl6p5ETdBzWVKKi8EnenRZSaSLoK/sRmmor+CU2zh0/q5Q8 ou+m87nSrDTk1UEPoR2+wQXse3o4Eo6gHIV4ADtJz3/45asvhxNfCeAxoecivrnQMUGU 0UcQ== X-Gm-Message-State: AMke39nVgNGLDaB+x1Tfqso0XqySodQpiZ7TDnjIALSRVow/BZJIl2n0dFpHA6NAWwp3bs6Ur8pICRzDvMn21wgl X-Received: by 10.157.19.43 with SMTP id f40mr6127163ote.189.1486752600358; Fri, 10 Feb 2017 10:50:00 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.128.198 with HTTP; Fri, 10 Feb 2017 10:49:39 -0800 (PST) From: Jason Merrill Date: Fri, 10 Feb 2017 13:49:39 -0500 Message-ID: Subject: C++ PATCH for c++/78897 (ICE with constexpr and union) To: gcc-patches List X-IsSubscribed: yes When we process a store that changes the active member of a union, we need to forget about other members. Tested x86_64-pc-linux-gnu, applying to trunk. commit 23c02701a1f2d115b9b88ccd88e1d3a26674ab14 Author: Jason Merrill Date: Fri Feb 10 12:35:49 2017 -0500 PR c++/78897 - constexpr union * constexpr.c (cxx_eval_store_expression): A store to a union member erases a previous store to another member. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index bb45f1e..bfdde9e 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -3466,6 +3466,11 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); unsigned HOST_WIDE_INT idx; + if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) + && CONSTRUCTOR_ELT (*valp, 0)->index != index) + /* Changing active member. */ + vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0); + for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep); idx++, fields = DECL_CHAIN (fields)) diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-union1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-union1.C new file mode 100644 index 0000000..8aed6d9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-union1.C @@ -0,0 +1,11 @@ +// PR c++/78897 +// { dg-do compile { target c++14 } } + +struct Optional { + constexpr Optional() : _dummy{} { _value = 1; } + union { + int _dummy; + int _value; + }; +}; +Optional opt{};