llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Non-inline unnamed namespaces can be redeclared as inline in some cases

Open miyuki opened this issue 7 years ago • 2 comments

Bugzilla Link 42523
Version trunk
OS Linux
CC @DougGregor,@K-ballo,@zygoloid

Extended Description

The following code is correctly rejected:

inline namespace I {
    namespace N {}
    using namespace N;
    namespace N { int i; }
}
inline namespace N { }

Clang produces the following diagnostic:

<source>:6:18: error: non-inline namespace cannot be reopened as inline
inline namespace N { }
                 ^
<source>:4:15: note: previous definition is here
    namespace N { int i; }
              ^
1 error generated.
Compiler returned: 1

But this code is accepted:

inline namespace I {
    namespace { int i; }
}
inline namespace { }

According to the standard ([namespace.unnamed]/1), An unnamed-namespace-definition behaves as if it were replaced by

    inline_opt namespace unique { /* empty body */ }
    using namespace unique ;
    namespace unique { namespace-body }

where inline appears if and only if it appears in the unnamed-namespace-definition and all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the translation unit.

I.e. the second example should also be rejected.

miyuki avatar Jul 05 '19 16:07 miyuki

@llvm/issue-subscribers-clang-frontend

Author: Mikhail Maltsev (miyuki)

| | | | --- | --- | | Bugzilla Link | [42523](https://llvm.org/bz42523) | | Version | trunk | | OS | Linux | | CC | @DougGregor,@K-ballo,@zygoloid |

Extended Description

The following code is correctly rejected:

inline namespace I {
    namespace N {}
    using namespace N;
    namespace N { int i; }
}
inline namespace N { }

Clang produces the following diagnostic:

&lt;source&gt;:6:18: error: non-inline namespace cannot be reopened as inline
inline namespace N { }
                 ^
&lt;source&gt;:4:15: note: previous definition is here
    namespace N { int i; }
              ^
1 error generated.
Compiler returned: 1

But this code is accepted:

inline namespace I {
    namespace { int i; }
}
inline namespace { }

According to the standard ([namespace.unnamed]/1), An unnamed-namespace-definition behaves as if it were replaced by

    inline_opt namespace unique { /* empty body */ }
    using namespace unique ;
    namespace unique { namespace-body }

where inline appears if and only if it appears in the unnamed-namespace-definition and all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the translation unit.

I.e. the second example should also be rejected.

llvmbot avatar Mar 10 '25 21:03 llvmbot

I don't think your interpretation is correct, no implementation reject: https://godbolt.org/z/PWqqcYeGK

We have I::unique and unique.

shafik avatar Mar 12 '25 02:03 shafik