<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/73632>73632</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang-repl doesn't execute top-level code located inside a namespace
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          p4vook
      </td>
    </tr>
</table>

<pre>
    Consider the following piece of code:
```cpp
#include <stdlib.h>
namespace ns { exit(1); }
```
Currently `clang-repl` does not execute the code inside `ns`.
However, executing top-level code inside namespaces not only seems the logical thing to do, but it could also prove to be useful when implementing proper notebook integration: consider this example:
```cpp
#include <iostream>
int n = 1; std::cout << n; // user executes notebook cell the first time
// user edits the cell and wants to re-execute it.
int n = 2; std::cout << n; // error: redefinition of 'n'
```
This code is, indeed, incorrect: C++ does not allow variable or function redefinition.
However, if rewritten using nested (anonymous) namespaces this works:
```cpp
#include <iostream>
{ int n = 1; std::cout << n; { int n = 2; std::cout << n; } } // outputs 12, as expected
```
However, this layout is quite hard to implement, as it requires manually inserting namespaces into existing code. 
However, it could potentially be rewritten using C++17 syntax for nested namespaces:
```cpp
#include <iostream>
namespace n1 { int n = 1; std::cout << n; } // currently nothing
namespace n1::n2 { int n = 2; std::cout << n; } // still nothing
```
This does not require any messing with compiler internals, so should behave in a way that's more predictable.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclVtu6zgMhlejvBANbDmNkwc_tOkUs4DZgCzRsaay6CPRSbP7geTcelpgegq0SGKLPyl-vKgY7d4jNuLxWTy-LNTEPYVmXB2I3hYtmVOzIx-twQDcI3TkHB2t38NoUSNQB5oMiupJFC-ieBLrYv7T43h-IivrtZsMgqh2kY2z7bIX1V_za68GjKPSCD6CqJ8B3y0LuSmF3IrqGUT98pvy_HM3hYCe3QmSM6f8_iHg6MS6AEMYwRMDvqOe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