From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 1AC9D3858D32 for ; Tue, 2 Jan 2024 22:41:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1AC9D3858D32 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 1AC9D3858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704235267; cv=none; b=XrU2zBkjHksyrfv3MPC+sEEQGFzNE9fHc+XfMrjTnqH4QJzjBYMRlo9x5r2zt3eMUyvvoTPVcLz15Qqetly4WPHaPGz2OifKOI3w/952bQJYfj8LPK8Y0TM6TxEDNFnqLGQIe4z14sJs4P92roLuBZ18qPZytzHMMhde2EblEDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704235267; c=relaxed/simple; bh=ybFsBpVABquUB8jD4ZwwOSS/ePQ9mxT03neZ0jE3Vx8=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=NVdRQQC9f+axzZd9T2ts5ATMnG01atkBwk3ML8yNp5D2oNKgt6bhCz41reqhShIGhhjTbpuLq3AfBbj7JLUtzZag89Ee1WwQBqs8kqNlPA/l7F4tkaYlNMq+G6ftrfaGiUtdRMdcM/FKiDFvMkJ8mwov3DvL93cnEtTd6zGDU9E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1d480c6342dso36797535ad.2 for ; Tue, 02 Jan 2024 14:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704235262; x=1704840062; 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=8aGUhRz4KHxwG8COYvtQ8yu+VKRVCzS00jNF7D+xlC8=; b=YRvmcRi44pGRfEt7NvRTeM5pgcVGmWu97ILs9d/tt24VGtZQhq8JwjYwuMI9fwyeze UQblxZWtBwc5kC2K1fpG9epG7Mayb+ocmYIgBv+EfTNwsYauo3KILjiq88A63UwTIL16 FL/5ke7wN2SCtfR82hzohpZ7npt4vqqGwrnr+8I1e4K46sL0aZBf9QBcmQi1PRpgnvrQ NH1JyK7pifzrbZaDMYrd8ga5CTLkWuZU2Ew6o6Tsr7NSNUm567/safrTULsyvlWwpQ+e IQThqXcvy3QiItHaHKba/Wv1VMwfWuG5Fp/0Coz37k3uTT71kinLE6mKW1Uip++R0eWH Ls3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704235262; x=1704840062; 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=8aGUhRz4KHxwG8COYvtQ8yu+VKRVCzS00jNF7D+xlC8=; b=xNTkps+81iFFQLNOH0AK0Ax6lQIP7f5ntYbPSVP9g6vuqz4EbLwE4eOHXNHqQsYO9y nbsTQT7FGUqEOEF00sMnkLiQOrKQi2if+vckJIkiOIHtdwWxb7UQ0aI62EddjWkAq3SB NxfXwxPx4t77REH7d70w5l2nQyaFEvTrOfy0nSGjJZ77XXt3572dfjlk4UNL6naAm132 8m1BcnVdzXPn7IlgA0U9xq93QpGp1BsiEGLnA7b1VeDqbJGoWEAARq7+kE6BEvBCkXSQ tD0xtEd/IJDPudUzjS1hkG6JEPg6l8oTevHM9WjzvIprZtBOGx4AXu44aX9EzCOy7vT5 CsFQ== X-Gm-Message-State: AOJu0Yz3Q6R7f1hJ2cG66saiJptYP8Kcpmav58NqVV4kV+VzgLu3cn4u IVIJ01jJ1BaN8PfpTvXNbGPLt1h4lgU= X-Google-Smtp-Source: AGHT+IERnlpb8eTYNBSb5AxtgCxFnsG55J+8Vv5Nm0854GuzWa6vED1bq7K01eDgb9O+EFqpByVcBg== X-Received: by 2002:a17:902:c211:b0:1d4:a31d:1de0 with SMTP id 17-20020a170902c21100b001d4a31d1de0mr6438990pll.54.1704235261500; Tue, 02 Jan 2024 14:41:01 -0800 (PST) Received: from Thaum. ([203.166.236.30]) by smtp.gmail.com with ESMTPSA id e9-20020a170902ed8900b001d4c3918167sm2428818plj.185.2024.01.02.14.40.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 14:41:01 -0800 (PST) Message-ID: <659490fd.170a0220.1ce2e.503a@mx.google.com> X-Google-Original-Message-ID: Date: Wed, 3 Jan 2024 09:40:55 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell , Patrick Palka Subject: c++/modules: Emit definitions of ODR-used static members imported from modules [PR112899] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-11.3 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,T_SCC_BODY_TEXT_LINE,URIBL_BLACK 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: Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? -- >8 -- Static data members marked 'inline' should be emitted in TUs where they are ODR-used. We need to make sure that statics imported from modules are correctly added to the 'pending_statics' map so that they get emitted if needed, otherwise the attached testcase fails to link. PR c++/112899 gcc/cp/ChangeLog: * cp-tree.h (note_variable_template_instantiation): Rename to... (note_static_storage_variable): ...this. * decl2.cc (note_variable_template_instantiation): Rename to... (note_static_storage_variable): ...this. * pt.cc (instantiate_decl): Rename usage of above function. * module.cc (trees_in::read_var_def): Remember pending statics that we stream in. gcc/testsuite/ChangeLog: * g++.dg/modules/init-4_a.C: New test. * g++.dg/modules/init-4_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/cp-tree.h | 2 +- gcc/cp/decl2.cc | 4 ++-- gcc/cp/module.cc | 4 ++++ gcc/cp/pt.cc | 2 +- gcc/testsuite/g++.dg/modules/init-4_a.C | 9 +++++++++ gcc/testsuite/g++.dg/modules/init-4_b.C | 11 +++++++++++ 6 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/init-4_a.C create mode 100644 gcc/testsuite/g++.dg/modules/init-4_b.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1979572c365..ebd2850599a 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7113,7 +7113,7 @@ extern tree maybe_get_tls_wrapper_call (tree); extern void mark_needed (tree); extern bool decl_needed_p (tree); extern void note_vague_linkage_fn (tree); -extern void note_variable_template_instantiation (tree); +extern void note_static_storage_variable (tree); extern tree build_artificial_parm (tree, tree, tree); extern bool possibly_inlined_p (tree); extern int parm_index (tree); diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 0850d3f5bce..241216b0dfe 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -910,10 +910,10 @@ note_vague_linkage_fn (tree decl) vec_safe_push (deferred_fns, decl); } -/* As above, but for variable template instantiations. */ +/* As above, but for variables with static storage duration. */ void -note_variable_template_instantiation (tree decl) +note_static_storage_variable (tree decl) { vec_safe_push (pending_statics, decl); } diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 0bd46414da9..14818131a70 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -11752,6 +11752,10 @@ trees_in::read_var_def (tree decl, tree maybe_template) DECL_INITIALIZED_P (decl) = true; if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup)) DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; + if (DECL_CONTEXT (decl) + && RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (decl)) + && !DECL_TEMPLATE_INFO (decl)) + note_static_storage_variable (decl); } DECL_INITIAL (decl) = init; if (!dyn_init) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index f7063e09581..ce498750758 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -27150,7 +27150,7 @@ instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p) { set_instantiating_module (d); if (variable_template_p (gen_tmpl)) - note_variable_template_instantiation (d); + note_static_storage_variable (d); instantiate_body (td, args, d, false); } diff --git a/gcc/testsuite/g++.dg/modules/init-4_a.C b/gcc/testsuite/g++.dg/modules/init-4_a.C new file mode 100644 index 00000000000..e0eb97b474e --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/init-4_a.C @@ -0,0 +1,9 @@ +// PR c++/112899 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi M } + +export module M; + +export struct A { + static constexpr int x = -1; +}; diff --git a/gcc/testsuite/g++.dg/modules/init-4_b.C b/gcc/testsuite/g++.dg/modules/init-4_b.C new file mode 100644 index 00000000000..d28017a1d14 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/init-4_b.C @@ -0,0 +1,11 @@ +// PR c++/112899 +// { dg-module-do run } +// { dg-additional-options "-fmodules-ts" } + +import M; + +int main() { + const int& x = A::x; + if (x != -1) + __builtin_abort(); +} -- 2.43.0