From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4F3E73858C52 for ; Thu, 19 Jan 2023 20:13:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F3E73858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674159190; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=9Z8v62J4bnC1SwgehlM7PjOEW1d/BabE+zK8RzSd8ms=; b=J5HfyYUAg9LNH06D/MxNccrYb/n9unt38Ll44e387CnJXJp96VyTfdlEL0+sZBQK50dT2Q wNlLy/Rok1r+Frk0YYROkkm9IamjMbQ70ZyJpEHPh0NZl6zz8EmUjRASok1UAkKuWyE9Qj Fmkqb9wbvt/n9DjbjBQ7GDqiTC3DNg0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-648-2AVtXlLcPUCTYjr7T_B2iw-1; Thu, 19 Jan 2023 15:13:08 -0500 X-MC-Unique: 2AVtXlLcPUCTYjr7T_B2iw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3EB1B8533E1 for ; Thu, 19 Jan 2023 20:13:08 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EE028492C3C; Thu, 19 Jan 2023 20:13:07 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 30JKD5NV3366286 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 19 Jan 2023 21:13:05 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 30JKD48x3366284; Thu, 19 Jan 2023 21:13:04 +0100 Date: Thu, 19 Jan 2023 21:13:04 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up handling of references to anon union members in initializers [PR53932] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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 List-Id: Hi! For anonymous union members we create artificial VAR_DECLs which have DECL_VALUE_EXPR for the actual COMPONENT_REF. That works just fine inside of functions (including global dynamic constructors), because during gimplification such VAR_DECLs are gimplified as their DECL_VALUE_EXPR. This is also done during regimplification. But references to these artificial vars in DECL_INITIAL expressions aren't ever replaced by the DECL_VALUE_EXPRs, so we end up either with link failures like on the testcase below, or worse ICEs with LTO. The following patch fixes those during cp_fully_fold_init where we already walk all the trees (!data->genericize means that function rather than cp_fold_function). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-01-19 Jakub Jelinek PR c++/53932 * cp-gimplify.cc (cp_fold_r): During cp_fully_fold_init replace DECL_ANON_UNION_VAR_P VAR_DECLs with their corresponding DECL_VALUE_EXPR. * g++.dg/init/pr53932.C: New test. --- gcc/cp/cp-gimplify.cc.jj 2023-01-16 11:52:16.065734330 +0100 +++ gcc/cp/cp-gimplify.cc 2023-01-19 18:13:54.592661735 +0100 @@ -1010,6 +1010,16 @@ cp_fold_r (tree *stmt_p, int *walk_subtr } break; + case VAR_DECL: + /* In initializers replace anon union artificial VAR_DECLs + with their DECL_VALUE_EXPRs, as nothing will do it later. */ + if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize) + { + *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt)); + break; + } + break; + default: break; } --- gcc/testsuite/g++.dg/init/pr53932.C.jj 2023-01-19 18:22:24.837231192 +0100 +++ gcc/testsuite/g++.dg/init/pr53932.C 2023-01-19 18:20:51.776586408 +0100 @@ -0,0 +1,25 @@ +// PR c++/53932 +// { dg-do link } + +static union { int i; }; +int &r = i; +int s = i; +int *t = &i; + +void +foo (int **p, int *q) +{ + static int &u = i; + static int v = i; + static int *w = &i; + int &x = i; + int y = i; + int *z = &i; + *p = &i; + *q = i; +} + +int +main () +{ +} Jakub