On 11/27/24 1:43 AM, Nathaniel Shead wrote:
On Wed, Nov 27, 2024 at 12:03:23AM -0500, Jason Merrill wrote:
Tested x86_64-pc-linux-gnu.

Does this approach make sense to you?  Any other ideas?

-- 8< --

We weren't representing 'using namespace' at all in modules, which broke
some of the <chrono> literals tests.

I experimented with various approaches to representing them, and ended up
with emitting them as a pseudo-binding for "using", which as a keyword can't
have any real bindings.  Then reading this pseudo-binding adds it to
using_directives instead of the usual handling.

+       /* ??? should we try to distinguish whether the using-directive
+          is purview/exported?  */
+       add_binding_entity (used, WMB_Flags(WMB_Using|WMB_Purview), &data);

I don't think the standard is entirely clear about how using-directives
should interact with modules; they don't declare names, and before P2615
were in fact forbidden from being explicitly exported, which implies to
me that the intention was for them to not be considered outside of the
declaring module.

P2615 is certainly clear about allowing them. Given that, I think the general rules of [module.interface] apply, so it should be found by name lookup in an importing TU.

That said, if we were to do this I would think the logic should match
what we do for any other name, in terms of requiring it to be explicitly
exported/purview as required; in particular, I would hope that something
like this doesn't happen:

   // m.cpp
   export module M;
   using namespace std;

   // test.cpp
   #include <iostream>
   import M;
   int main() {
     cout << "hello\n";  // using-directive "inherited" from M?
   }

Good point, I have more work to do.

I think that since ADL doesn't consider using-directives, we only need to represent the exported ones?

  name_lookup::search_namespace_only (tree scope)
  {
    bool found = false;
+  if (modules_p () && name && !id_equal (name, "using"))
+    {
+      name_lookup u (get_identifier ("using"));
+      u.search_namespace_only (scope);
+    }

Could we just add to the list of using-directives within read_namespaces
perhaps?  Probably as a second pass after all namespaces have been
created so that we don't run into issues with circular directives.
That would mean we wouldn't need to do this in every lookup.

That was my first thought, but I had trouble figuring out how. Perhaps I'll try again.

Jason

Reply via email to