Skip to content

clang-repl doesn't execute top-level code located inside a namespace #73632

Open
@p4vook

Description

@p4vook

Consider the following piece of code:

#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:

#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, it could potentially be rewritten into working code using C++17 syntax for nested namespaces:

#include <iostream>
namespace n1 { int n = 1; std::cout << n; } // currently nothing
namespace n1::n2 { int n = 2; std::cout << n; } // still nothing, but this doesn't trigger redefinition, and n1::n2::n = 2

This does not require any messing with compiler internals, so should behave in a way that's more predictable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions