inline bool named_module_attach_p ()
{ return named_module_p () && module_attach_p (); }
+/* We don't know if this TU will have a CMI while parsing the GMF,
+ so tentatively assume that it might, for the purpose of determining
+ whether no-linkage decls could be used by an importer. */
+inline bool module_maybe_has_cmi_p ()
+{ return module_has_cmi_p () || (named_module_p () && !module_purview_p ()); }
+
/* We're currently exporting declarations. */
inline bool module_exporting_p ()
{ return module_kind & MK_EXPORTING; }
/* Members of anonymous types and local classes have no linkage; make
them internal. If a typedef is made later, this will be changed. */
- if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
- || decl_function_context (TYPE_MAIN_DECL (ctype))))
+ if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
publicp = 0;
+ else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
+ /* But members of local classes in a module CMI should have their
+ definitions exported, in case they are (directly or indirectly)
+ used by an importer. We don't just use module_has_cmi_p here
+ because for entities in the GMF we don't yet know whether this
+ module will have a CMI, so we'll conservatively assume it might. */
+ publicp = module_maybe_has_cmi_p ();
if (publicp && cxx_dialect == cxx98)
{
bool d = false;
auto_diagnostic_group grp;
if (cxx_dialect >= cxx11)
- d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
- "unnamed type, is used but never defined", decl);
+ {
+ /* If t is declared in a module CMI, then decl could actually
+ be defined in a different TU, so don't warn since C++20. */
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using an unnamed type, "
+ "is used but never defined", decl);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using an unnamed type, "
+ "is used but not defined", decl);
+ }
else if (DECL_EXTERN_C_P (decl))
/* Allow this; it's pretty common in C. */;
else if (VAR_P (decl))
inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
"to the unqualified type, so it is not used for linkage",
TYPE_NAME (t));
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
}
else if (cxx_dialect >= cxx11)
{
if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
- permerror (DECL_SOURCE_LOCATION (decl),
- "%q#D, declared using local type "
- "%qT, is used but never defined", decl, t);
+ {
+ /* Similarly for local types in a function with vague linkage or
+ defined in a module CMI, then decl could actually be defined
+ in a different TU, so don't warn since C++20. */
+ bool d = false;
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using local type "
+ "%qT, is used but never defined", decl, t);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using local type "
+ "%qT, is used but not defined here", decl, t);
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
+ }
}
else if (VAR_P (decl))
warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
/* Check if the type T depends on a type with no linkage and if so,
return it. If RELAXED_P then do not consider a class type declared
- within a vague-linkage function to have no linkage. Remember:
+ within a vague-linkage function or in a module CMI to have no linkage,
+ since it can still be accessed within a different TU. Remember:
no-linkage is not the same as internal-linkage. */
tree
/* Only treat unnamed types as having no linkage if they're at
namespace scope. This is core issue 966. */
if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
- return t;
+ {
+ if (relaxed_p
+ && TREE_PUBLIC (CP_TYPE_CONTEXT (t))
+ && module_maybe_has_cmi_p ())
+ /* This type could possibly be accessed outside this TU. */
+ return NULL_TREE;
+ else
+ return t;
+ }
for (r = CP_TYPE_CONTEXT (t); ; )
{
return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
else if (TREE_CODE (r) == FUNCTION_DECL)
{
- if (!relaxed_p || !vague_linkage_p (r))
- return t;
- else
+ if (relaxed_p
+ && (vague_linkage_p (r)
+ || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
r = CP_DECL_CONTEXT (r);
+ else
+ return t;
}
else
break;
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+inline auto f() {
+ struct A {};
+ return A{};
+}
+decltype(f()) a(); // { dg-error "used but not defined" "" { target c++17_down } }
+
+auto g() {
+ struct A {};
+ return A{};
+}
+decltype(g()) b(); // { dg-error "used but never defined" }
+
+int main() {
+ a();
+ b();
+}
--- /dev/null
+// GMF
+
+// Non-inline function definitions in headers are a recipe for ODR violations,
+// but we should probably support that anyway as its not inherently wrong
+// if only ever included into the GMF of a single module.
+
+auto gmf_n_i() {
+ struct X { void f() {} };
+ return X{};
+}
+
+inline auto gmf_i_i() {
+ struct X { void f() {} };
+ return X{};
+}
+
+auto gmf_n_i_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+inline auto gmf_i_i_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
--- /dev/null
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi mod }
+
+// Test that we can link various forms of local class functions.
+// Function names use i=inline, n=non-inline, for each nesting.
+
+module;
+#include "block-decl-3.h"
+
+export module mod;
+
+namespace {
+ void internal() {}
+}
+
+// singly-nested
+
+export auto n_n() {
+ internal();
+ struct X { void f() { internal(); } };
+ return X{};
+}
+
+export auto n_i() {
+ internal();
+ struct X { inline void f() {} };
+ return X{};
+}
+
+export inline auto i_n() {
+ // `f` is not inline here, so this is OK
+ struct X { void f() { internal(); } };
+ return X{};
+}
+
+export inline auto i_i() {
+ struct X { inline void f() {} };
+ return X{};
+}
+
+
+// doubly nested
+
+export auto n_n_n() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() { internal(); }
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+export auto n_i_n() {
+ struct X {
+ inline auto f() {
+ struct Y {
+ void g() { internal(); }
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+export inline auto i_n_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ inline void g() {}
+ };
+ return Y {};
+ }
+ };
+ return X{};
+}
+
+export inline auto i_i_i() {
+ struct X {
+ inline auto f() {
+ struct Y {
+ inline void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+
+// multiple types
+
+export auto multi_n_n() {
+ struct X {
+ void f() { internal(); }
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+}
+
+export auto multi_n_i() {
+ struct X {
+ inline void f() {}
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+}
+
+export inline auto multi_i_i() {
+ struct X {
+ inline void f() {}
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+};
+
+
+// extern "C++"
+
+export extern "C++" auto extern_n_i() {
+ struct X {
+ void f() {} // implicitly inline
+ };
+ return X{};
+};
+
+export extern "C++" inline auto extern_i_i() {
+ struct X {
+ void f() {}
+ };
+ return X{};
+};
+
+
+// GMF
+
+export using ::gmf_n_i;
+export using ::gmf_i_i;
+export using ::gmf_n_i_i;
+export using ::gmf_i_i_i;
+
+
+// can access from implementation unit
+
+auto only_used_in_impl() {
+ struct X { void f() {} };
+ return X{};
+}
+export void test_from_impl_unit();
--- /dev/null
+// { dg-additional-options "-fmodules-ts" }
+
+module mod;
+
+// Test that we can access (and link) to declarations from the interface
+void test_from_impl_unit() {
+ only_used_in_impl().f();
+}
--- /dev/null
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import mod;
+
+int main() {
+ n_n().f();
+ n_i().f();
+ i_n().f();
+ i_i().f();
+
+ n_n_n().f().g();
+ n_i_n().f().g();
+ i_n_i().f().g();
+ i_i_i().f().g();
+
+ multi_n_n().x.f();
+ multi_n_i().x.f();
+ multi_i_i().x.f();
+
+ extern_n_i().f();
+ extern_i_i().f();
+
+ gmf_n_i().f();
+ gmf_i_i().f();
+ gmf_n_i_i().f().g();
+ gmf_i_i_i().f().g();
+
+ test_from_impl_unit();
+}
--- /dev/null
+// { dg-additional-options "-fmodules-ts -Wno-error=c++20-extensions" }
+// { dg-module-cmi M }
+
+export module M;
+
+auto f() {
+ struct A {};
+ return A{};
+}
+decltype(f()) g(); // { dg-warning "used but not defined" "" { target c++17_down } }
+export auto x = g();
+
+struct {} s;
+decltype(s) h(); // { dg-warning "used but not defined" "" { target c++17_down } }
+export auto y = h();
--- /dev/null
+// { dg-additional-options "-fmodules-ts" }
+
+module M;
+
+decltype(f()) g() { return {}; }
+decltype(s) h() { return {}; }
--- /dev/null
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int main() {
+ auto a = x;
+ auto b = y;
+}
--- /dev/null
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi !M }
+
+export module M;
+
+// Same as a linkage-1 except within an anonymous namespace;
+// now these declarations cannot possibly be defined outside this TU,
+// so we should error.
+
+namespace {
+ auto f() {
+ struct A {};
+ return A{};
+ }
+ decltype(f()) g(); // { dg-error "used but never defined" }
+
+ struct {} s;
+ decltype(s) h(); // { dg-error "used but never defined" }
+}
+
+export void use() {
+ g();
+ h();
+}
+
+// { dg-prune-output "not writing module" }