]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++/modules: Forbid exposures of TU-local entities in inline variables [PR119551]
authorNathaniel Shead <nathanieloshead@gmail.com>
Tue, 1 Apr 2025 05:36:30 +0000 (16:36 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 1 Apr 2025 20:20:36 +0000 (07:20 +1100)
commit0210bedf481a9fd248ce29650b824bcd84c3723c
tree75993b058b0de1185be3854fc993a043f1bc8252
parent9aa78d771222cd9aec463ee28f9039d306ab5834
c++/modules: Forbid exposures of TU-local entities in inline variables [PR119551]

An inline variable has vague linkage, and needs to be conditionally
emitted in TUs that reference it.  Unfortunately this clashes with
[basic.link] p14.2, which says that we ignore the initialisers of all
variables (including inline ones), since importers will not have access
to the referenced TU-local entities to write the definition.

This patch makes such exposures be ill-formed.  One case that continues
to work is if the exposure is part of the dynamic initialiser of an
inline variable; in such cases, the definition has been built as part of
the module interface unit anyway, and importers don't need to write it
out again, so such exposures are "harmless".

PR c++/119551

gcc/cp/ChangeLog:

* module.cc (trees_out::write_var_def): Only ignore non-inline
variable initializers.

gcc/testsuite/ChangeLog:

* g++.dg/modules/internal-5_a.C: Add cases that should be
ignored.
* g++.dg/modules/internal-5_b.C: Test these new cases, and make
the testcase more robust.
* g++.dg/modules/internal-11.C: New test.
* g++.dg/modules/internal-12_a.C: New test.
* g++.dg/modules/internal-12_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/internal-11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/internal-12_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/internal-12_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/internal-5_a.C
gcc/testsuite/g++.dg/modules/internal-5_b.C