Fix assertion when generating diagnostic for inline namespaces

When calculating the name to display for inline namespaces, we have
custom logic to try to hide redundant inline namespaces from the
diagnostic. Calculating these redundancies requires performing a lookup
in the parent declaration context, but that lookup should not try to
look through transparent declaration contexts, like linkage
specifications. Instead, loop up the declaration context chain until we
find a non-transparent context and use that instead.

This fixes PR49954.
This commit is contained in:
Aaron Ballman 2021-08-20 09:49:07 -04:00
parent 9efda541bf
commit 48f73ee666
4 changed files with 27 additions and 1 deletions

View File

@ -614,7 +614,9 @@ public:
if (!isInline())
return false;
auto X = lookup(Name);
auto Y = getParent()->lookup(Name);
// We should not perform a lookup within a transparent context, so find a
// non-transparent parent context.
auto Y = getParent()->getNonTransparentContext()->lookup(Name);
return std::distance(X.begin(), X.end()) ==
std::distance(Y.begin(), Y.end());
}

View File

@ -1997,6 +1997,12 @@ public:
return const_cast<DeclContext*>(this)->getNonClosureAncestor();
}
// Retrieve the nearest context that is not a transparent context.
DeclContext *getNonTransparentContext();
const DeclContext *getNonTransparentContext() const {
return const_cast<DeclContext *>(this)->getNonTransparentContext();
}
/// getPrimaryContext - There may be many different
/// declarations of the same entity (including forward declarations
/// of classes, multiple definitions of namespaces, etc.), each with

View File

@ -1217,6 +1217,13 @@ bool DeclContext::Encloses(const DeclContext *DC) const {
return false;
}
DeclContext *DeclContext::getNonTransparentContext() {
DeclContext *DC = this;
while (DC && DC->isTransparentContext())
DC = DC->getParent();
return DC;
}
DeclContext *DeclContext::getPrimaryContext() {
switch (getDeclKind()) {
case Decl::ExternCContext:

View File

@ -48,3 +48,14 @@ namespace N {
T<struct A::B::C::i> t4; // expected-error {{implicit instantiation of undefined template 'N::T<N::A::i>'}}
T<struct A::B::C::j> t5; // expected-error {{implicit instantiation of undefined template 'N::T<N::B::C::j>'}}
}
namespace dont_crash {
// A malformed lookup involving inline namespaces in a linkage specification
// would previous cause an assertion due to the way diagnostics are emitted.
extern "C++" inline namespace {
namespace a {
a : b // expected-error {{unexpected ':' in nested name specifier; did you mean '::'?}} \
// expected-error {{no type named 'b' in namespace 'dont_crash::a'}}
} // expected-error {{expected unqualified-id}}
} // inline namespace
} // dont_crash