Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ def note_using_decl_class_member_workaround : Note<
"a const variable|a constexpr variable}0 instead">;
def err_using_decl_can_not_refer_to_namespace : Error<
"using declaration cannot refer to a namespace">;
def note_namespace_using_decl : Note<
"did you mean 'using namespace'?">;
def warn_cxx17_compat_using_decl_scoped_enumerator: Warning<
"using declaration naming a scoped enumerator is incompatible with "
"C++ standards before C++20">, InGroup<CXXPre20Compat>, DefaultIgnore;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13080,6 +13080,9 @@ NamedDecl *Sema::BuildUsingDeclaration(
if (R.getAsSingle<NamespaceDecl>()) {
Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace)
<< SS.getRange();
// Suggest using 'using namespace ...' instead.
Diag(SS.getBeginLoc(), diag::note_namespace_using_decl)
<< FixItHint::CreateInsertion(SS.getBeginLoc(), "namespace ");
return BuildInvalid();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose another exercise for our future selves could be to recover as if the user typed "using namespace"

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s

namespace A {
namespace B { }
}

// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:7-[[@LINE+1]]:7}:"namespace "
using A::B; // expected-error{{using declaration cannot refer to a namespace}}
// expected-note@-1 {{did you mean 'using namespace'?}}
2 changes: 2 additions & 0 deletions clang/test/CXX/drs/cwg4xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,10 @@ namespace cwg460 { // cwg460: yes
// expected-error@-1 {{using declaration requires a qualified name}}
using cwg460::X;
// expected-error@-1 {{using declaration cannot refer to a namespace}}
// expected-note@-2 {{did you mean 'using namespace'?}}
using X::Q;
// expected-error@-1 {{using declaration cannot refer to a namespace}}
// expected-note@-2 {{did you mean 'using namespace'?}}
}
}

Expand Down