Skip to content

[Clang] Implement CWG3005 Function parameters should never be name-independent #138245

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 3 commits into from
May 2, 2025

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented May 2, 2025

We already attempted to implement this (it was the intent of the paper), in that parameters were never considered name-independent. However, we failed to check that any previously found parameter declaration was also name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to CWG with better wording), but there is some consensus on the design outcome.

Fixes #136373

@cor3ntin cor3ntin requested a review from erichkeane May 2, 2025 09:30
@cor3ntin cor3ntin requested a review from Endilll as a code owner May 2, 2025 09:30
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

We already attempted to implement this (it was the intent of the paper), in that parameters were never considered name-independent. However, we failed to check that any previously found parameter declaration was also name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to CWG with better wording), but there is some consensus on the design outcome.

Fixes #136373


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

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+7-3)
  • (added) clang/test/CXX/drs/cwg30xx.cpp (+19)
  • (modified) clang/www/cxx_dr_status.html (+5-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5cd1fbeabcfe..3d83aeb46ad72 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -134,6 +134,8 @@ Resolutions to C++ Defect Reports
 - Bumped the ``__cpp_constexpr`` feature-test macro to ``202002L`` in C++20 mode as indicated in
   `P2493R0 <https://wg21.link/P2493R0>`_.
 
+- Implemented `CWG3005 Function parameters should never be name-independent <https://wg21.link/CWG3005>`_.
+
 C Language Changes
 ------------------
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 46933c5c43168..eb1f2c172c427 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7532,16 +7532,20 @@ NamedDecl *Sema::ActOnVariableDeclarator(
 
   DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();
   StorageClass SC = StorageClassSpecToVarDeclStorageClass(D.getDeclSpec());
-
   if (LangOpts.CPlusPlus && (DC->isClosure() || DC->isFunctionOrMethod()) &&
       SC != SC_Static && SC != SC_Extern && II && II->isPlaceholder()) {
+
     IsPlaceholderVariable = true;
+
     if (!Previous.empty()) {
       NamedDecl *PrevDecl = *Previous.begin();
       bool SameDC = PrevDecl->getDeclContext()->getRedeclContext()->Equals(
           DC->getRedeclContext());
-      if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false))
-        DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
+      if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false)) {
+        IsPlaceholderVariable = !isa<ParmVarDecl>(PrevDecl);
+        if (IsPlaceholderVariable)
+          DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
+      }
     }
   }
 
diff --git a/clang/test/CXX/drs/cwg30xx.cpp b/clang/test/CXX/drs/cwg30xx.cpp
new file mode 100644
index 0000000000000..2f65e8b9bebbe
--- /dev/null
+++ b/clang/test/CXX/drs/cwg30xx.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++98 -pedantic-errors -verify=expected,cxx98 %s
+// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected,since-cxx11,cxx11-23 %s
+// RUN: %clang_cc1 -std=c++14 -pedantic-errors -verify=expected,since-cxx11,cxx11-23 %s
+// RUN: %clang_cc1 -std=c++17 -pedantic-errors -verify=expected,since-cxx11,cxx11-23 %s
+// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify=expected,since-cxx11,cxx11-23,since-cxx20 %s
+// RUN: %clang_cc1 -std=c++23 -pedantic-errors -verify=expected,since-cxx11,cxx11-23,since-cxx20,since-cxx23 %s
+// RUN: %clang_cc1 -std=c++2c -pedantic-errors -verify=expected,since-cxx11,since-cxx20,since-cxx23,since-cxx26 %s
+
+
+namespace cwg3005 { // cwg3005: 21 open 2025-03-10
+
+void f(int _, // expected-note  {{previous declaration is here}} \
+              // expected-note  {{previous definition is here}}
+       int _) // expected-error {{redefinition of parameter '_'}}
+{
+    int _; // expected-error {{redefinition of '_'}}
+}
+
+}
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 8fe53ad46aca9..f8783c6858de1 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -17890,7 +17890,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
     <td><a href="https://cplusplus.github.io/CWG/issues/3005.html">3005</a></td>
     <td>open</td>
     <td>Function parameters should never be name-independent</td>
-    <td align="center">Not resolved</td>
+    <td align="center">
+      <details>
+        <summary>Not resolved</summary>
+        Clang 21 implements 2025-03-10 resolution
+      </details></td>
   </tr>
   <tr class="open" id="3006">
     <td><a href="https://cplusplus.github.io/CWG/issues/3006.html">3006</a></td>

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not going to bother you with the description of what I want to see in this file. Here you go:

// RUN: %clang_cc1 -std=c++98 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++14 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++17 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++23 -pedantic-errors -verify=expected %s
// RUN: %clang_cc1 -std=c++2c -pedantic-errors -verify=expected %s


namespace cwg3005 { // cwg3005: 21 open 2025-03-10

void f(
  int _, // #cwg3005-first-param
  int _)
  // expected-error@-1 {{redefinition of parameter '_'}}
  //   expected-note@#cwg3005-first-param {{previous declaration is here}}
{
  int _;
  // expected-error@-1 {{redefinition of '_'}}
  //   expected-note@#cwg3005-first-param {{previous declaration is here}}
}

} // namespace cwg3005

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

THIS example should also be ill-formed by CWG3005:


struct S3 {
   union {
     int _;
     };
    int _; // error, the one in the union isn't type independent
};

@cor3ntin
Copy link
Contributor Author

cor3ntin commented May 2, 2025

THIS example should also be ill-formed by CWG3005:


struct S3 {
   union {
     int _;
     };
    int _; // error, the one in the union isn't type independent
};

This is CWG2764 - I'll do that in a separate PR (and I need to open another issue because I think the wording fails to implement the intent)

@erichkeane
Copy link
Collaborator

THIS example should also be ill-formed by CWG3005:


struct S3 {
   union {
     int _;
     };
    int _; // error, the one in the union isn't type independent
};

This is CWG2764 - I'll do that in a separate PR (and I need to open another issue because I think the wording fails to implement the intent)

Ah! SGTM!

cor3ntin added 3 commits May 2, 2025 18:48
…dependent.

We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered nmame independent.
However, we failed to check that any previously found parameter
declaration was also name independent.

Note that as worded the current resolution is insufficient
(I wrote to CWG with better wording), but there is some
consensus on the design outcome.

Fixes llvm#136373
@cor3ntin cor3ntin merged commit 1101b76 into llvm:main May 2, 2025
10 of 11 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…dependent (llvm#138245)

We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered name-independent. However, we
failed to check that any previously found parameter declaration was also
name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to
CWG with better wording), but there is some consensus on the design
outcome.

Fixes llvm#136373
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…dependent (llvm#138245)

We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered name-independent. However, we
failed to check that any previously found parameter declaration was also
name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to
CWG with better wording), but there is some consensus on the design
outcome.

Fixes llvm#136373
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…dependent (llvm#138245)

We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered name-independent. However, we
failed to check that any previously found parameter declaration was also
name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to
CWG with better wording), but there is some consensus on the design
outcome.

Fixes llvm#136373
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…dependent (llvm#138245)

We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered name-independent. However, we
failed to check that any previously found parameter declaration was also
name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to
CWG with better wording), but there is some consensus on the design
outcome.

Fixes llvm#136373
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.

Name used in parameter should conflict with placeholder used for automatic variable
4 participants