Skip to content

Raise an error on namespace aliases with qualified names. #86122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2024

Conversation

katzdm
Copy link
Contributor

@katzdm katzdm commented Mar 21, 2024

Clang's current behavior is to ignore the trailing qualifiers, but the grammar for namespace-alias-definition requires an identifier without qualification.

https://godbolt.org/z/1zvW5q4f8

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-testing-tools

@llvm/pr-subscribers-clang

Author: Daniel M. Katz (katzdm)

Changes

Clang's current behavior is to ignore the trailing qualifiers, but the grammar for namespace-alias-definition requires an identifier without qualification.

https://godbolt.org/z/1zvW5q4f8


Full diff: https://github.com/llvm/llvm-project/pull/86122.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+2)
  • (modified) clang/lib/Parse/ParseDeclCXX.cpp (+5)
  • (modified) clang/test/SemaCXX/namespace-alias.cpp (+2)
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 816c3ff5f8b2aa..d45a1f0b6ad1c0 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -268,6 +268,8 @@ def err_expected_semi_after_namespace_name : Error<
   "expected ';' after namespace name">;
 def err_unexpected_namespace_attributes_alias : Error<
   "attributes cannot be specified on namespace alias">;
+def err_unexpected_qualified_namespace_alias : Error<
+  "unexpected nested name specifier in namespace alias definition">;
 def err_unexpected_nested_namespace_attribute : Error<
   "attributes cannot be specified on a nested namespace definition">;
 def err_inline_namespace_alias : Error<"namespace alias cannot be inline">;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 77d2382ea6d907..0ef2f2fad2a9d1 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -140,6 +140,11 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
       SkipUntil(tok::semi);
       return nullptr;
     }
+    if (!ExtraNSs.empty()) {
+      Diag(IdentLoc, diag::err_unexpected_qualified_namespace_alias);
+      SkipUntil(tok::semi);
+      return nullptr;
+    }
     if (attrLoc.isValid())
       Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
     if (InlineLoc.isValid())
diff --git a/clang/test/SemaCXX/namespace-alias.cpp b/clang/test/SemaCXX/namespace-alias.cpp
index 281ee9962e8b52..10d2e8beeccaa0 100644
--- a/clang/test/SemaCXX/namespace-alias.cpp
+++ b/clang/test/SemaCXX/namespace-alias.cpp
@@ -47,6 +47,8 @@ namespace I {
   namespace A1 { int i; }
   
   namespace A2 = A1;
+
+  namespace A3::extra::specifiers = A2;  // expected-error {{unexpected nested name specifier}}
 }
 
 int f() {

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

Thanks for contributing! This needs a release note (in clang/docs/ReleaseNotes.rst) and some changes to the diagnostics, but other than that it looks fine to me.

@katzdm
Copy link
Contributor Author

katzdm commented Mar 21, 2024

@Sirraide Thanks for the quick review! I've applied your feedback. Here is an example error message with the patch applied:

test.cpp:4:20: error: namespace alias must be a single identifier
    4 |     namespace alias::extra::qualifiers = ::myns;
      |                    ^~~~~~~~~~~~~~~~~~~
1 error generated.

Oops, still need the release note.

@Sirraide
Copy link
Member

test.cpp:4:20: error: namespace alias must be a single identifier
    4 |     namespace alias::extra::qualifiers = ::myns;
      |                    ^~~~~~~~~~~~~~~~~~~
1 error generated.

Nice, that is pretty much exactly what I had in mind.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

LGTM except for a minor comment wrt the release note.

@Sirraide
Copy link
Member

You can ignore the merge conflicts in the release notes; it’s better to let whoever ends up merging this (probably me) take care of that when it comes to the release notes (because they get updated constantly).

katzdm added 2 commits March 21, 2024 12:36
Current behavior is to ignore the trailing qualifiers, but the grammar
for `namespace-alias-definition` requires an `identifier` without
qualification.

https://godbolt.org/z/1zvW5q4f8

https://eel.is/c++draft/namespace.alias#nt:namespace-alias-definition
Copy link

github-actions bot commented Mar 21, 2024

✅ With the latest revision this PR passed the Python code formatter.

@katzdm katzdm force-pushed the disallow-qualified-ns-alises branch from c5997ee to 3c6632e Compare March 21, 2024 16:40
@katzdm
Copy link
Contributor Author

katzdm commented Mar 21, 2024

You can ignore the merge conflicts in the release notes; it’s better to let whoever ends up merging this (probably me) take care of that when it comes to the release notes (because they get updated constantly).

Ah, missed this - already fixed it. Will ignore if it happens again.

@Sirraide
Copy link
Member

You can ignore the merge conflicts in the release notes; it’s better to let whoever ends up merging this (probably me) take care of that when it comes to the release notes (because they get updated constantly).

Ah, missed this - already fixed it. Will ignore if it happens again.

I mean, no-one’s gonna be mad at you for fixing that yourself because it means less work for them; just saying that merging release notes is fairly trivial, so whoever merges it can just do that when they merge the pr—at least that’s my opinion on this. Otherwise, you’d have to keep resolving merge conflicts because they might come back time and time again if a pr stays open for a while.

@Sirraide Sirraide merged commit 0024875 into llvm:main Mar 22, 2024
Copy link

@katzdm Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@katzdm katzdm deleted the disallow-qualified-ns-alises branch March 22, 2024 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants