From patchwork Thu Oct 13 19:04:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 2343 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp434625wrs; Thu, 13 Oct 2022 12:05:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5pUYZ9IzE6X/Y8CrVkuAr+0sOkWGwMWl4h8yPkelYPs23MxjRe+umJ4cRt204Mqw5tTh1v X-Received: by 2002:a05:6402:11ce:b0:45c:a2a2:4207 with SMTP id j14-20020a05640211ce00b0045ca2a24207mr1061565edw.3.1665687934829; Thu, 13 Oct 2022 12:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665687934; cv=none; d=google.com; s=arc-20160816; b=g1k2Wu15YsWsaLChMMjMaWIRYwv0aAcrfFaRfvowgNhb/sZ7PeJ8iwrOMueo9iruHk /XmqNiJPpgEue/jEhjkXovIy/Xn5eCKMCiYXved/TMrDykRfGdUx7l5Ujxi8tnQMoLjQ E1zquhtEeStlMXBfrbpYPHcujFNHrYmXMEKqSgUYSKBtAhc+vsitqsuBDYwaSVbsfed+ matAGlBWqd+NSu2AtITGVi0opII9TjvF4g4mgmO8KvxodLKBAd5ZYKSqCCDmbMFpX8MN OIeNEBNIu8pkAOqggGBffeMNb6G65kyALDVjzfu0qB1kdu2ctqSOPmMkGYfbWQBr5r0C bKoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/Je0aAmjR19Ne7Z8zp8cqOUel7zkjaA9qseLj8UssMc=; b=Y+QQ5V7vUbGZVTf4W56QNJvJOdODofIAoxSRZPb8Hm6fHAARv9BYMY7k3Ie1Bk6ver P4yaRdHjJxEp0SwbJpFIXX9/bHfOaBDyWATOGJ3ylaAZWW1HnJkLwHcJQFVvgCqvIkrB tDtEsXuJW0Spx+fmAlUMrwBSTe63oPVLsiszBi7eIQF0p611RXpsanlLC1C3kbNHlmmk Q9c3GmRhgHIGSLLzvK/6w0r3ngbMKzy1FPDf1ax6XQ1bUL5lnk0hjBJa3S73KzmZ34Re erAtfeXJzkKwcKdgWJkCl0a/A1jj2Ak80RsmQcKbRXu3TWhN2ZAF8hecEejnOx+/Rc+o tA5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AxdwK8PY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id jg16-20020a170907971000b00782686da1d1si387376ejc.761.2022.10.13.12.05.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 12:05:34 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AxdwK8PY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C867C3857C4D for ; Thu, 13 Oct 2022 19:05:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C867C3857C4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665687933; bh=/Je0aAmjR19Ne7Z8zp8cqOUel7zkjaA9qseLj8UssMc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=AxdwK8PYPvhSC39Hr+9m+3SJ49Ix9jccvnt2Ra9eBy91EKCLNZ/1/vxC8m+CRMRSj 78/G7sza4a6eoSU4NaMZA9fid23+uOwNoPTibxZY5FZT3AHcC8NpZlvpb4fIoUhI9P fvjxRK2MNxpnP4n9k6BKSEgFTWRkFPVSiwlE8fcI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 C68603858C55 for ; Thu, 13 Oct 2022 19:04:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C68603858C55 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-158-icIeCDImM3aNUDG0FBjB4g-1; Thu, 13 Oct 2022 15:04:31 -0400 X-MC-Unique: icIeCDImM3aNUDG0FBjB4g-1 Received: by mail-qv1-f70.google.com with SMTP id ma6-20020a0562145b0600b004b49a5037aeso1417658qvb.18 for ; Thu, 13 Oct 2022 12:04:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/Je0aAmjR19Ne7Z8zp8cqOUel7zkjaA9qseLj8UssMc=; b=wEqb8yx52XAryffB3p3Z6x6WUdfL6z8uqW7Mg4TNswESRqpQl5Z+OBVkCXndOfHv+1 QkXZwxrfPjN5UrGow2v443+pU/lq5ojvkgCnYzn05TWyPe6PMXlvrc4oYpS2i//jF4ml DLtWWmswgSzRNfGfHwCi+vBEvtle+suQ1UccmtyhbBKWs+r83VweXyduTQSxBKPHyzaa bGER0EnIB2SeQx8BOJlPmvESV7Tytw6e2rZ4zOgoaOxL3/foxx0Wtm0mqXxdbnVOm30l jC8uu+Z8Ec39Kw31xmYhsMdVKUFiAS7iegGB+y2Xyv6p4pngOstfw3vCaxcFBoiZkOHh 62aQ== X-Gm-Message-State: ACrzQf0h46URWWyOoCgbDG2bGmowXTZ3AR2ZgsIUsc84F9UtoEO9ad2V XFwyAsGXVUumTe5W6mcoK5qcJVBgyL7Dqg+wU+++hd7myz/S3M7BLPBJeD1jcuLo28JZEnVmN+K FO5xfYmWr65IzrzVgy4RooN6jW/mcJxlG3Yvx0kpMsdq/zbx0RXmRj1POtCTcIMFGWNI= X-Received: by 2002:a05:6214:d08:b0:4b1:7127:f615 with SMTP id 8-20020a0562140d0800b004b17127f615mr1060874qvh.92.1665687870628; Thu, 13 Oct 2022 12:04:30 -0700 (PDT) X-Received: by 2002:a05:6214:d08:b0:4b1:7127:f615 with SMTP id 8-20020a0562140d0800b004b17127f615mr1060846qvh.92.1665687870387; Thu, 13 Oct 2022 12:04:30 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bs15-20020ac86f0f000000b003972790deb9sm482492qtb.84.2022.10.13.12.04.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 12:04:29 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++ modules: ICE with dynamic_cast [PR106304] Date: Thu, 13 Oct 2022 15:04:27 -0400 Message-Id: <20221013190427.181432-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.0.68.ge85701b4af MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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 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: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: nathan@acm.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746600391643481541?= X-GMAIL-MSGID: =?utf-8?q?1746600391643481541?= The FUNCTION_DECL we build for __dynamic_cast has an empty DECL_CONTEXT, but trees_out::tree_node expects all FUNCTION_DECLs to have non-empty DECL_CONTEXT thus we crash when streaming out the dynamic_cast in the below testcase. This patch naively fixes this by setting DECL_CONTEXT for __dynamic_cast appropriately. Like for __cxa_atexit which is similarly lazily declared, I suppose we should push it into the namespace too. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/106304 gcc/cp/ChangeLog: * constexpr.cc (cxx_dynamic_cast_fn_p): Check for abi_node instead of global_namespace. * rtti.cc (build_dynamic_cast_1): Set DECL_CONTEXT and DECL_SOURCE_LOCATION on dynamic_cast_node, and push it into the namespace. gcc/testsuite/ChangeLog: * g++.dg/modules/pr106304_a.C: New test. * g++.dg/modules/pr106304_b.C: New test. --- gcc/cp/constexpr.cc | 2 +- gcc/cp/rtti.cc | 4 ++++ gcc/testsuite/g++.dg/modules/pr106304_a.C | 12 ++++++++++++ gcc/testsuite/g++.dg/modules/pr106304_b.C | 8 ++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/modules/pr106304_a.C create mode 100644 gcc/testsuite/g++.dg/modules/pr106304_b.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 06dcd71c926..5939d2882f8 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -2108,7 +2108,7 @@ cxx_dynamic_cast_fn_p (tree fndecl) { return (cxx_dialect >= cxx20 && id_equal (DECL_NAME (fndecl), "__dynamic_cast") - && CP_DECL_CONTEXT (fndecl) == global_namespace); + && CP_DECL_CONTEXT (fndecl) == abi_node); } /* Often, we have an expression in the form of address + offset, e.g. diff --git a/gcc/cp/rtti.cc b/gcc/cp/rtti.cc index f5b43ec0fb2..a85c7b56409 100644 --- a/gcc/cp/rtti.cc +++ b/gcc/cp/rtti.cc @@ -787,6 +787,10 @@ build_dynamic_cast_1 (location_t loc, tree type, tree expr, NULL_TREE)); dcast_fn = (build_library_fn_ptr (fn_name, fn_type, ECF_LEAF | ECF_PURE | ECF_NOTHROW)); + /* As with __cxa_atexit in get_atexit_node. */ + DECL_CONTEXT (dcast_fn) = FROB_CONTEXT (current_namespace); + DECL_SOURCE_LOCATION (dcast_fn) = BUILTINS_LOCATION; + dcast_fn = pushdecl (dcast_fn, /*hiding=*/true); pop_abi_namespace (flags); dynamic_cast_node = dcast_fn; } diff --git a/gcc/testsuite/g++.dg/modules/pr106304_a.C b/gcc/testsuite/g++.dg/modules/pr106304_a.C new file mode 100644 index 00000000000..b999eeccf4a --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr106304_a.C @@ -0,0 +1,12 @@ +// PR c++/106304 +// { dg-additional-options -fmodules-ts } +// { dg-module-cmi pr106304 } + +export module pr106304; + +struct A { virtual ~A() = default; }; +struct B : A { }; + +inline const B* as_b(const A& a) { + return dynamic_cast(&a); +} diff --git a/gcc/testsuite/g++.dg/modules/pr106304_b.C b/gcc/testsuite/g++.dg/modules/pr106304_b.C new file mode 100644 index 00000000000..e8333909c8d --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr106304_b.C @@ -0,0 +1,8 @@ +// PR c++/106304 +// { dg-additional-options -fmodules-ts } + +module pr106304; + +void f(A& a) { + as_b(a); +}