From patchwork Thu Jan 9 14:31:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 104416 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8019E3858D38 for ; Thu, 9 Jan 2025 14:32:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8019E3858D38 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=h4bsyTcw X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 2E5DA3858D1E for ; Thu, 9 Jan 2025 14:31:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E5DA3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2E5DA3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::635 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736433119; cv=none; b=f7NWESGWuytIWGbK3R2fop8NffLjNxvi5GDNjYP6rQKhGjLwOHzx7Nou0dVrEDSWUhCWPLQMwjKwFAO9Z8FckZFxnfLzXStL5jvpD9xv0EiXROx5e46Yv/hdL5n4HnGtbP1UH5oSgnIyWBInaSmu6rVfZCcUKomSHBkp32SUDkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736433119; c=relaxed/simple; bh=mbLN6XLh8WS+EIiwM6gNw3+FkpZYtYEpUIPZP3kfFQA=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=nq1ZL/s2mXlFsZSJE25ZwIQQRSBl072/kJc24ig44geitn937rJff9km+VyrvkfE8KftpDX8q6cdxSSHf5nV9bR/94aITzoRGYore2o60zHblxR7Cwp5CFGPefHhtBeLAVxQc+YdyPV4MBqdujmToNV0OwjK3KmSCX3ESvESClY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E5DA3858D1E Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-215464b0432so1906725ad.0 for ; Thu, 09 Jan 2025 06:31:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736433118; x=1737037918; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=U/3gJ/UfZuHc4lcOfAupbr0Syk74xYrE4orNnIwz89E=; b=h4bsyTcwYnCehUdDl7xXOxexo9vSmAyJk2Cj8+Se0Y2iEPe1wKGmfL+EvBD9cqXxL6 wTFtTziZJf4KLn+k7/PfaeEy0exf/XeHfwE0bFp85hhakM2igsTKlXkKaH1swglHbjfd QSHe7cpZWyrZgcN3rpEW7S0eMLOkFgXHit9sGk8+vKdr6YEZXFDTDbVMJmhX4mnPpz81 Y/MK9fUX5Nlc0KJaeY+NMvrFaeXvn07rT8wT/dL2wtSQGGLAOEoXVEnDvdCVfHAfKRhW +G/1oLKPG0ZvZCcQW7N7leWMEnre/2NNyOk4IjxXnGZvkIRIi3BTNLRuxwOlEwym1/vC /BDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736433118; x=1737037918; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=U/3gJ/UfZuHc4lcOfAupbr0Syk74xYrE4orNnIwz89E=; b=hEaQ5JOkiZzB+i9XT9oheWhKvUUQcGYNZm9QoMshAI5T/QQtQaDet5Vu7ZryHdbA+l OAdtOu6ofB64QKrlTYbY1iSInG0p3Nuumxgy9pQzLiUh5exSHPZZUTgWWq2GDkhG4lkY 7DM+G9Tdb6TDIvuyoRCOQpz1coWDv2A+Zqxpc4i9mrdnvLoGd4kAC3ekc2+63A0rsjY0 pdwM0LQQSXk+F7uDFMuNk6SLdi23wdEUJqvINyQVhfN39pM+nWrKn9/kXuQOkP1JJC1G XVEJ5nfTjHddlWbt1ntIHxKSfOugVZG6nAfzH7s+aCsvwHYB8hda+dFF25gEV/rzilkX RwhQ== X-Gm-Message-State: AOJu0YzatawXc53+wbx+AWurl8aTMDpCQgcKZqszM3PrpW3NCY3CoCej D4lFp3V26m+idSvpWzAkny7xVPE78rWWjkr+9spkFy/ilK9MMOoxIIUxDg== X-Gm-Gg: ASbGncuq55Otk/+vutKYKJwXSrAhOmKvyDrKHKYNTyKWqPVdyoyaDhHO3VEVNooxfcx usHzY6kCllk9kqkZywIYES8THyQngRhfNS/j2Da8jNld4I29b09utYtutWWVH0BWueRBTdt1g7O 9bLiK0wbAD3HFOHZYShigDHLRB3jeCCFZtxbKlrB4dEWMLswFiTSJUPvsgKpZbZ2hH2rkvKvJJU WGcMuE1vAe9c0xcjwgOBn+GIoxMHm+5XGVp4TUHH9RIg/UlJXO9PMAY6jdE36slYPk8wsXe9cFg PyTi3iIla6EMDgFyFYW8eA== X-Google-Smtp-Source: AGHT+IE2f6j9v0wI2Lg0UQ7JDGM9fhh1jxr9RSeS19AZhvm9KM6+PZuO7PSNOZgnm8gWVWzClY9GwQ== X-Received: by 2002:a05:6300:41:b0:1cf:2be2:5e5a with SMTP id adf61e73a8af0-1e88d0edfedmr4496692637.10.1736433117862; Thu, 09 Jan 2025 06:31:57 -0800 (PST) Received: from Thaum. (163-47-68-2.ipv4.originbroadband.com.au. [163.47.68.2]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-a31d5047df6sm1359986a12.59.2025.01.09.06.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 06:31:57 -0800 (PST) Message-ID: <677fdddd.630a0220.1c6bbe.87e5@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 10 Jan 2025 01:31:52 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Patrick Palka , Nathan Sidwell Subject: [PATCH] c++/modules: Handle chaining already-imported local types [PR114630] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk/14? -- >8 -- In the linked testcase, an ICE occurs because when reading the (duplicate) function definition for _M_do_parse from module Y, the local type definitions have already been streamed from module X and setup as regular backreferences, rather than being found with find_duplicate, causing issues with managing DECL_CHAIN. It is tempting to just skip setting up the DECL_CHAIN for this case. However, for the future it would be best to ensure that the block vars for the duplicate definition are accurate, so that we could implement ODR checking on function definitions at some point. So to solve this, this patch creates a copy of the streamed-in local type and chains that; it will be discarded along with the rest of the duplicate function after we've finished processing. A couple of suggested implementations from the discussion on the PR that don't work: - Replacing the `DECL_CHAIN` assertion with `(*chain && *chain != decl)` doesn't handle the case where type definitions are followed by regular local variables, since those won't have been imported as separate backreferences and so the chains will diverge. - Correcting the purviewness of GMF template instantiations to force Y to emit copies of the local types rather than backreferences into X is insufficient, as it's still possible that the local types got streamed in a separate cluster to the function definition, and so will be again referred to via regular backreferences when importing. - Likewise, preventing the emission of function definitions where an import has already provided that same definition also is insufficient, for much the same reason. PR c++/114630 gcc/cp/ChangeLog: * module.cc (trees_in::core_vals) : Chain a new node if DECL_CHAIN already is set. gcc/testsuite/ChangeLog: * g++.dg/modules/pr114630.h: New test. * g++.dg/modules/pr114630_a.C: New test. * g++.dg/modules/pr114630_b.C: New test. * g++.dg/modules/pr114630_c.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 14 +++++++++++++- gcc/testsuite/g++.dg/modules/pr114630.h | 11 +++++++++++ gcc/testsuite/g++.dg/modules/pr114630_a.C | 7 +++++++ gcc/testsuite/g++.dg/modules/pr114630_b.C | 8 ++++++++ gcc/testsuite/g++.dg/modules/pr114630_c.C | 4 ++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/modules/pr114630.h create mode 100644 gcc/testsuite/g++.dg/modules/pr114630_a.C create mode 100644 gcc/testsuite/g++.dg/modules/pr114630_b.C create mode 100644 gcc/testsuite/g++.dg/modules/pr114630_c.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 5350e6c4bad..ff2683de73e 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -6928,11 +6928,23 @@ trees_in::core_vals (tree t) body) anyway. */ decl = maybe_duplicate (decl); - if (!DECL_P (decl) || DECL_CHAIN (decl)) + if (!DECL_P (decl)) { set_overrun (); break; } + + /* If DECL_CHAIN is already set then this was a backreference to a + local type or enumerator from a previous read (PR c++/114630). + Let's copy the node so we can keep building the chain for ODR + checking later. */ + if (DECL_CHAIN (decl)) + { + gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL + && find_duplicate (DECL_CONTEXT (decl))); + decl = copy_node (decl); + } + *chain = decl; chain = &DECL_CHAIN (decl); } diff --git a/gcc/testsuite/g++.dg/modules/pr114630.h b/gcc/testsuite/g++.dg/modules/pr114630.h new file mode 100644 index 00000000000..8730007f59f --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr114630.h @@ -0,0 +1,11 @@ +template +void _M_do_parse() { + struct A {}; + struct B {}; + int x; +} + +template struct formatter; +template <> struct formatter { + void parse() { _M_do_parse(); } +}; diff --git a/gcc/testsuite/g++.dg/modules/pr114630_a.C b/gcc/testsuite/g++.dg/modules/pr114630_a.C new file mode 100644 index 00000000000..ecfd7ca0b28 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr114630_a.C @@ -0,0 +1,7 @@ +// { dg-additional-options "-fmodules" } +// { dg-module-cmi X } + +module; +#include "pr114630.h" +export module X; +formatter a; diff --git a/gcc/testsuite/g++.dg/modules/pr114630_b.C b/gcc/testsuite/g++.dg/modules/pr114630_b.C new file mode 100644 index 00000000000..52fe04e2ce0 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr114630_b.C @@ -0,0 +1,8 @@ +// { dg-additional-options "-fmodules" } +// { dg-module-cmi Y } + +module; +#include "pr114630.h" +export module Y; +import X; +formatter b; diff --git a/gcc/testsuite/g++.dg/modules/pr114630_c.C b/gcc/testsuite/g++.dg/modules/pr114630_c.C new file mode 100644 index 00000000000..54a21f08057 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr114630_c.C @@ -0,0 +1,4 @@ +// { dg-additional-options "-fmodules" } + +#include "pr114630.h" +import Y;