extern char const *module_name (unsigned, bool header_ok);
extern bitmap get_import_bitmap ();
extern bitmap visible_instantiation_path (bitmap *);
+extern bitmap visible_from_instantiation_origination (unsigned *);
extern void module_begin_main_file (cpp_reader *, line_maps *,
const line_map_ordinary *);
extern void module_preprocess_options (cpp_reader *);
return this_module ()->imports;
}
+/* Get the original decl for an instantiation at TINST, or NULL_TREE
+ if we're not an instantiation. */
+
+static tree
+orig_decl_for_instantiation (tinst_level *tinst)
+{
+ if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
+ return NULL_TREE;
+
+ tree decl = tinst->tldcl;
+ if (TREE_CODE (decl) == TREE_LIST)
+ decl = TREE_PURPOSE (decl);
+ if (TYPE_P (decl))
+ decl = TYPE_NAME (decl);
+ return decl;
+}
+
/* Return the visible imports and path of instantiation for an
instantiation at TINST. If TINST is nullptr, we're not in an
instantiation, and thus will return the visible imports of the
the tinst level itself. */
static bitmap
-path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
+path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
{
gcc_checking_assert (modules_p ());
- if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
+ tree decl = orig_decl_for_instantiation (tinst);
+ if (!decl)
{
gcc_assert (!tinst || !tinst->next);
/* Not inside an instantiation, just the regular case. */
bitmap_set_bit (path_map, 0);
}
- tree decl = tinst->tldcl;
- if (TREE_CODE (decl) == TREE_LIST)
- decl = TREE_PURPOSE (decl);
- if (TYPE_P (decl))
- decl = TYPE_NAME (decl);
-
if (unsigned mod = get_originating_module (decl))
if (!bitmap_bit_p (path_map, mod))
{
return path_of_instantiation (current_instantiation (), path_map_p);
}
+/* Returns the bitmap describing what modules were visible from the
+ module that the current instantiation originated from. If we're
+ not an instantiation, returns NULL. *MODULE_P is filled in with
+ the originating module of the definition for this instantiation. */
+
+bitmap
+visible_from_instantiation_origination (unsigned *module_p)
+{
+ if (!modules_p ())
+ return NULL;
+
+ tree decl = orig_decl_for_instantiation (current_instantiation ());
+ if (!decl)
+ return NULL;
+
+ *module_p = get_originating_module (decl);
+ return (*modules)[*module_p]->imports;
+}
+
/* We've just directly imported IMPORT. Update our import/export
bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
the import bitmap. Hence iterate over the former
checking for bits set in the bitmap. */
bitmap imports = get_import_bitmap ();
+ /* FIXME: For instantiations, we also want to include any
+ declarations visible at the point it was defined, even
+ if not visible from the current TU; we approximate
+ this here, but a proper solution would involve caching
+ phase 1 lookup results (PR c++/122609). */
+ unsigned orig_mod = 0;
+ bitmap orig_imp = visible_from_instantiation_origination (&orig_mod);
+
binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
int marker = 0;
int dup_detect = 0;
if (unsigned span = cluster->indices[jx].span)
do
if (bool (want & LOOK_want::ANY_REACHABLE)
- || bitmap_bit_p (imports, base))
+ || bitmap_bit_p (imports, base)
+ || (orig_imp && bitmap_bit_p (orig_imp, base)))
goto found;
while (++base, --span);
continue;
found:;
/* Is it loaded? */
+ unsigned mod = cluster->indices[jx].base;
if (cluster->slots[jx].is_lazy ())
{
gcc_assert (cluster->indices[jx].span == 1);
- lazy_load_binding (cluster->indices[jx].base,
- scope, name, &cluster->slots[jx]);
+ lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
}
tree bind = cluster->slots[jx];
if (!bind)
dup_detect |= dup;
}
- if (bool (want & LOOK_want::ANY_REACHABLE))
+ if (bool (want & LOOK_want::ANY_REACHABLE)
+ || mod == orig_mod)
{
type = STAT_TYPE (bind);
bind = STAT_DECL (bind);
#if __cpp_impl_three_way_comparison >= 201907L
rewrite_ops(0); // OK
-
- // This should error, but lookup_qualified_name in add_candidates
- // doesn't look in the instantiation context of this call, so
- // we don't see the operator!= and think we can validly rewrite.
- rewrite_ops_error(0); // { dg-error "required from here" "PR122609" { xfail *-*-* } }
+ rewrite_ops_error(0); // { dg-message "required from here" "" { target c++20 } }
+ // { dg-prune-output "no match for" }
#endif
}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi K }
+
+export module K;
+export template <typename T> void use_without_import(T t) {
+ auto [x] = t;
+}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi U }
+
+export module U;
+import K;
+
+namespace std {
+ template <typename T> struct tuple_size { static constexpr int value = 1; };
+ template <int I, typename T> struct tuple_element { using type = int; };
+};
+
+export struct S {};
+template <int I> int get(S) { return 123; };
+
+export template <typename T> void call_use_without_import(T t) {
+ use_without_import(t);
+}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+
+import U;
+
+int main() {
+ // std::tuple_size and std::tuple_element aren't visible from
+ // use_without_import, and also aren't visible from here,
+ // so despite being visible on the instantiation path this is an error.
+
+ call_use_without_import(S{}); // { dg-message "required from here" }
+ // { dg-error {cannot decompose class type} "" { target *-*-* } 0 }
+}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi std }
+
+module;
+#include <coroutine>
+#include <tuple>
+#include <typeinfo>
+export module std;
+
+// PR c++/101140
+export using ::operator new;
+export using ::operator delete;
+
+export namespace std {
+ using std::coroutine_handle;
+ using std::coroutine_traits;
+ using std::suspend_always;
+
+ using std::get;
+ using std::tuple;
+ using std::tuple_size;
+ using std::tuple_element;
+
+ using std::type_info;
+}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi M }
+
+export module M;
+import std; // not exported
+
+export template <typename T> void structured_binding() {
+ std::tuple<T> t;
+ auto [x] = t;
+}
+
+export template <typename T> void operator_new() {
+ // PR c++/101140
+ T x;
+ new (&x) T;
+}
+
+export template <typename T> void use_typeid() {
+ const auto& id = typeid(T);
+}
+
+struct simple_promise;
+struct simple_coroutine : std::coroutine_handle<simple_promise> {
+ using promise_type = ::simple_promise;
+};
+struct simple_promise {
+ simple_coroutine get_return_object() { return { simple_coroutine::from_promise(*this) }; }
+ std::suspend_always initial_suspend() noexcept { return {}; }
+ std::suspend_always final_suspend() noexcept { return {}; }
+ void return_void() {}
+ void unhandled_exception() {}
+};
+template <typename T> simple_coroutine coroutine_impl() {
+ co_return;
+}
+export template <typename T> void coroutine() {
+ simple_coroutine sc = coroutine_impl<T>();
+ sc.resume();
+ sc.destroy();
+}
--- /dev/null
+// PR c++/122609
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fmodules" }
+
+import M;
+
+int main() {
+ structured_binding<int>();
+ operator_new<int>();
+ use_typeid<int>();
+ coroutine<int>();
+}
--- /dev/null
+// PR c++/122609
+// PR c++/101140
+// { dg-additional-options "-fmodules -Wno-global-module -fdump-lang-module" }
+// { dg-module-cmi M }
+
+module;
+
+using size_t = decltype(sizeof(0));
+
+namespace std {
+ template <typename T> struct tuple_size;
+ template <size_t I, typename T> struct tuple_element;
+}
+
+struct G {};
+template <> struct std::tuple_size<G> { static constexpr size_t value = 1; };
+template <> struct std::tuple_element<0, G> { using type = int; };
+template <size_t I> int get(G) { return 123; }
+
+export module M;
+
+export using ::G;
+export template <typename T> void use_gmf(T t) {
+ // This should make std::tuple_size and std::tuple_element decl-reachable;
+ // additionally, this should make ::get reachable via ADL.
+ auto [x] = t;
+ // { dg-final { scan-lang-dump "Bindings '::std::tuple_size'" module { xfail *-*-* } } }
+ // { dg-final { scan-lang-dump "Bindings '::std::tuple_element'" module { xfail *-*-* } } }
+ // { dg-final { scan-lang-dump "Bindings '::get'" module { xfail *-*-* } } }
+}
+
+export template <typename T> void use_future_decl(T t) {
+ ::new (t) int;
+}
+
+export struct F {};
+void* operator new(size_t, F); // not exported
--- /dev/null
+// PR c++/122609
+// PR c++/101140
+// { dg-additional-options "-fmodules" }
+
+import M;
+
+int main() {
+ use_gmf(G{}); // { dg-bogus "" "PR122609" { xfail *-*-* } }
+ // { dg-prune-output "cannot decompose" }
+
+ // operator new is not visible from point of definition,
+ // and is also not visible from this TU (point of instantiation),
+ // so the call below should fail.
+ use_future_decl(F{}); // { dg-error "" "PR122609" { xfail *-*-* } }
+}