From patchwork Mon Dec 12 19:25:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 32514 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2424940wrr; Mon, 12 Dec 2022 11:26:54 -0800 (PST) X-Google-Smtp-Source: AA0mqf5wZRuFNk8+hLNlr0SAEJUz76A5msKljdk8LCCnuVwSasPoH3R/tx66XClrkd/X2vY4/Z+/ X-Received: by 2002:a05:6402:5303:b0:45c:834b:f2ad with SMTP id eo3-20020a056402530300b0045c834bf2admr14242998edb.42.1670873214801; Mon, 12 Dec 2022 11:26:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670873214; cv=none; d=google.com; s=arc-20160816; b=Ss8LKexzLd5KZHFw0JYI/J0W9DyBEtDO2HLh6fzCeMkL8uosr8XCeaMQD1Uxc+AkMB yusJvhs+EqvBd4MFbdyn6A/BR1X14dvK7/v70qXAvJPqlC4YKdTtv3qglaCB835KSdG+ IQXgjowv/6bU+W8Pr3VoVxnKjvz0NzK/0s7wmGk/xSjLNtarLfzgpoUptLjX7XN74nBL YUqjJpVgTfUuYsLlKdNIRXUYGYjqQ9Xrr/8QokdDEbpjhdzezbI01Y8s8Id7YBaxzPeC bryLx87AemUpSShhiTxayXdlVkklL+1RIzTe4avJyIdHhcRJoj3SFHRdUgQymt+Rak6b hHvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=x3gyURHIJbRZdi7ufCBVBH2GvCrDRWmsIapC+AXirk8=; b=MIWKD6mKeSZqxFZWBW2rMUVAOMaYgj4xFylNCU+x2V6u1WE7Sjg4Raq292/xhUIfCP xk0UCAigrTT2gT9RNH8Wo30uVC1dr++erdKCiKp6IH2x4IfzfdpCyYnRaptP4jU/aIgT vU7v5sq6nnL1+vS+VdZzy/Ju0pzuw7YKJnCd78K5/ZfYYI3DOVQ3bObARfR5sEKX1tgy hZakuNJCaZaIC4k3Wpd3UCbeLk40cdchOzBlVer5EfTYJmt5dH6D5gw19VNULDT6iR28 Lud0TPY7SEwAjYcFd6kRLxJNPCqroHmHmYpYWlazGe7jcML0TMcE9hs7YEbCTAZ5rfvO SHYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M+hPOeS5; 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 z9-20020a056402274900b00469acead38esi9231761edd.1.2022.12.12.11.26.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 11:26:54 -0800 (PST) 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=M+hPOeS5; 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 8A5D4384F95B for ; Mon, 12 Dec 2022 19:26:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A5D4384F95B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670873213; bh=x3gyURHIJbRZdi7ufCBVBH2GvCrDRWmsIapC+AXirk8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=M+hPOeS5drZ8TFZ60u/u/WaCmXyA8TvHoiNaCoiqfwcXEZKY5/FIOhVEbCC0CW0Je KLSrBo/5bpREALFfQ6YP3aBXYuQBGOiqk+YVlx3H6PPMLPp0oAvN7QCRCb/1C/qvCB iJQtf54emL9hRgKVH97zX4RuwTp6551Cc0iXWCE8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [IPv6:2001:67c:2050:0:465::202]) by sourceware.org (Postfix) with ESMTPS id 7ED66384F945 for ; Mon, 12 Dec 2022 19:26:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7ED66384F945 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4NWBSC6k4lz9sTB; Mon, 12 Dec 2022 20:26:03 +0100 (CET) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix undefined reference to nested lambda in template (PR108055) Date: Mon, 12 Dec 2022 20:25:57 +0100 Message-Id: <20221212192557.52896-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4NWBSC6k4lz9sTB X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, 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: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw 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?1752037552178023473?= X-GMAIL-MSGID: =?utf-8?q?1752037552178023473?= Hi, This patch fixes a linker error caused by gdc not emitting all symbols. Sometimes, nested lambdas of templated functions get no code generation due to them being marked as instantianted outside of all modules being compiled in the current compilation unit. This despite enclosing template instances being marked as instantiated inside the current compilation unit. To fix, all enclosing templates are now checked in `function_defined_in_root_p'. Because of this change, `function_needs_inline_definition_p' has also been fixed up to only check whether the regular function definition itself is to be emitted in the current compilation unit. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, committed to mainline and backported to the releases/gcc-12 branch. Regards, Iain. --- PR d/108055 gcc/d/ChangeLog: * decl.cc (function_defined_in_root_p): Check all enclosing template instances for definition in a root module. (function_needs_inline_definition_p): Replace call to function_defined_in_root_p with test for outer module `isRoot'. gcc/testsuite/ChangeLog: * gdc.dg/torture/imports/pr108055conv.d: New. * gdc.dg/torture/imports/pr108055spec.d: New. * gdc.dg/torture/imports/pr108055write.d: New. * gdc.dg/torture/pr108055.d: New test. --- gcc/d/decl.cc | 14 ++++++---- .../gdc.dg/torture/imports/pr108055conv.d | 26 +++++++++++++++++++ .../gdc.dg/torture/imports/pr108055spec.d | 18 +++++++++++++ .../gdc.dg/torture/imports/pr108055write.d | 19 ++++++++++++++ gcc/testsuite/gdc.dg/torture/pr108055.d | 12 +++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/torture/imports/pr108055conv.d create mode 100644 gcc/testsuite/gdc.dg/torture/imports/pr108055spec.d create mode 100644 gcc/testsuite/gdc.dg/torture/imports/pr108055write.d create mode 100644 gcc/testsuite/gdc.dg/torture/pr108055.d diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index bed16323fec..35081083cd6 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1028,7 +1028,8 @@ build_decl_tree (Dsymbol *d) input_location = saved_location; } -/* Returns true if function FD is defined or instantiated in a root module. */ +/* Returns true if function FD, or any lexically enclosing scope function of FD + is defined or instantiated in a root module. */ static bool function_defined_in_root_p (FuncDeclaration *fd) @@ -1037,9 +1038,11 @@ function_defined_in_root_p (FuncDeclaration *fd) if (md && md->isRoot ()) return true; - TemplateInstance *ti = fd->isInstantiated (); - if (ti && ti->minst && ti->minst->isRoot ()) - return true; + for (TemplateInstance *ti = fd->isInstantiated (); ti != NULL; ti = ti->tinst) + { + if (ti->minst && ti->minst->isRoot ()) + return true; + } return false; } @@ -1067,7 +1070,8 @@ function_needs_inline_definition_p (FuncDeclaration *fd) /* Check whether function will be regularly defined later in the current translation unit. */ - if (function_defined_in_root_p (fd)) + Module *md = fd->getModule (); + if (md && md->isRoot ()) return false; /* Non-inlineable functions are always external. */ diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr108055conv.d b/gcc/testsuite/gdc.dg/torture/imports/pr108055conv.d new file mode 100644 index 00000000000..93ebba747b1 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/imports/pr108055conv.d @@ -0,0 +1,26 @@ +module imports.pr108055conv; + +T toStr(T, S)(S src) +{ + static if (is(typeof(T.init[0]) E)) + { + struct Appender + { + inout(E)[] data; + } + + import imports.pr108055spec; + import imports.pr108055write; + + auto w = Appender(); + FormatSpec!E f; + formatValue(w, src, f); + return w.data; + } +} + +T to(T, A)(A args) +{ + return toStr!T(args); +} + diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr108055spec.d b/gcc/testsuite/gdc.dg/torture/imports/pr108055spec.d new file mode 100644 index 00000000000..801c5810516 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/imports/pr108055spec.d @@ -0,0 +1,18 @@ +module imports.pr108055spec; + +template Unqual(T : const U, U) +{ + alias Unqual = U; +} + +template FormatSpec(Char) +if (!is(Unqual!Char == Char)) +{ + alias FormatSpec = FormatSpec!(Unqual!Char); +} + +struct FormatSpec(Char) +if (is(Unqual!Char == Char)) +{ + const(Char)[] nested; +} diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr108055write.d b/gcc/testsuite/gdc.dg/torture/imports/pr108055write.d new file mode 100644 index 00000000000..fe41d7baa7c --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/imports/pr108055write.d @@ -0,0 +1,19 @@ +module imports.pr108055write; +import imports.pr108055spec; + +void formatValueImpl(Writer, T, Char)(ref Writer , const(T) , + scope const ref FormatSpec!Char ) +{ + T val; + char spec; + + (ref val) @trusted { + return (cast(const char*) &val)[0 .. val.sizeof]; + }(val); + +} + +void formatValue(Writer, T, Char)(Writer w, T val, Char f) +{ + formatValueImpl(w, val, f); +} diff --git a/gcc/testsuite/gdc.dg/torture/pr108055.d b/gcc/testsuite/gdc.dg/torture/pr108055.d new file mode 100644 index 00000000000..c4ffad26d1e --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr108055.d @@ -0,0 +1,12 @@ +// { dg-do link } +// { dg-additional-files "imports/pr108055conv.d imports/pr108055spec.d imports/pr108055write.d" } +// { dg-additional-options "-I[srcdir] -fno-druntime" } +import imports.pr108055conv; + +extern(C) int main() +{ + float zis; + static if (is(typeof(to!string(&zis)))) + to!string(&zis); + return 0; +}