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

[Clang] accepts ambiguous inline namespace redeclaration

Open YuchangSu opened this issue 9 months ago • 4 comments

The code creates an inline namespace ::X0 containing another inline namespace ::X0::X0. Because inline namespaces inject members into the enclosing namespace for lookup, ::X0::X0 also becomes visible as ::X0 at global scope, making the later inline namespace X0 {} ambiguous. GCC rejects it; Clang accepts it.

Code:

inline namespace X0 { inline namespace X0 { } } inline namespace X0 { }

See Compiler Explorer: https://godbolt.org/z/crcEhreqP

The test case was generated by a fuzzer.

YuchangSu avatar Feb 26 '26 08:02 YuchangSu

MSVC also accepts this.

frederick-vs-ja avatar Feb 26 '26 12:02 frederick-vs-ja

@llvm/issue-subscribers-clang-frontend

Author: None (YuchangSu)

The code creates an inline namespace `::X0` containing another inline namespace `::X0::X0`. Because inline namespaces inject members into the enclosing namespace for lookup, `::X0::X0` also becomes visible as `::X0` at global scope, making the later `inline namespace X0 {}` ambiguous. GCC rejects it; Clang accepts it.

Code:

inline namespace X0 { inline namespace X0 { } } inline namespace X0 { }

See Compiler Explorer: https://godbolt.org/z/crcEhreqP

The test case was generated by a fuzzer.

llvmbot avatar Feb 26 '26 12:02 llvmbot

I think clang is wrong here: https://godbolt.org/z/jKhnMz956

inline namespace X0 { 
    inline namespace X0 { int x ; } 
} 

inline namespace X0 { }

void f() {
    X0::x; // All reject this
}

shafik avatar Feb 27 '26 19:02 shafik

If we want the first example to be ill-formed, we need to file a Core issue, because it passes [basic.scope.scope]/6:

Two declarations potentially conflict if they correspond and cause their shared name to denote different entities ([basic.link]). The program is ill-formed if, in any scope, a name is bound to two declarations A and B that potentially conflict and A precedes B ([basic.lookup]), unless B is name-independent.

because there is no scope in which X0 is bound to two declarations that potentially conflict (each namespace-definition, including those with inline keyword, introduces a namespace scope ([basic.scope.namespace]/1)).

While this first example is certainly error-prone, I'm not entirely sure that it is ought to be ill-formed, because it's usable as long as you don't spell the name X0, instead relying on unqualified lookup, including ADL. I wonder if there are users of this (probably libraries).

Shafik's example is ill-formed because of [basic.lookup.general]/1:

Otherwise, if the declarations found by name lookup do not all denote the same entity, they are ambiguous and the program is ill-formed.

Endilll avatar Feb 27 '26 19:02 Endilll