Skip to content

Commit 1101b76

Browse files
authored
[Clang] Implement CWG3005 Function parameters should never be name-independent (#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 #136373
1 parent cce6de8 commit 1101b76

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Resolutions to C++ Defect Reports
134134
- Bumped the ``__cpp_constexpr`` feature-test macro to ``202002L`` in C++20 mode as indicated in
135135
`P2493R0 <https://wg21.link/P2493R0>`_.
136136

137+
- Implemented `CWG3005 Function parameters should never be name-independent <https://wg21.link/CWG3005>`_.
138+
137139
C Language Changes
138140
------------------
139141

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7542,16 +7542,20 @@ NamedDecl *Sema::ActOnVariableDeclarator(
75427542

75437543
DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();
75447544
StorageClass SC = StorageClassSpecToVarDeclStorageClass(D.getDeclSpec());
7545-
75467545
if (LangOpts.CPlusPlus && (DC->isClosure() || DC->isFunctionOrMethod()) &&
75477546
SC != SC_Static && SC != SC_Extern && II && II->isPlaceholder()) {
7547+
75487548
IsPlaceholderVariable = true;
7549+
75497550
if (!Previous.empty()) {
75507551
NamedDecl *PrevDecl = *Previous.begin();
75517552
bool SameDC = PrevDecl->getDeclContext()->getRedeclContext()->Equals(
75527553
DC->getRedeclContext());
7553-
if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false))
7554-
DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
7554+
if (SameDC && isDeclInScope(PrevDecl, CurContext, S, false)) {
7555+
IsPlaceholderVariable = !isa<ParmVarDecl>(PrevDecl);
7556+
if (IsPlaceholderVariable)
7557+
DiagPlaceholderVariableDefinition(D.getIdentifierLoc());
7558+
}
75557559
}
75567560
}
75577561

clang/test/CXX/drs/cwg30xx.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -std=c++98 -pedantic-errors -verify=expected %s
2+
// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected %s
3+
// RUN: %clang_cc1 -std=c++14 -pedantic-errors -verify=expected %s
4+
// RUN: %clang_cc1 -std=c++17 -pedantic-errors -verify=expected %s
5+
// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify=expected %s
6+
// RUN: %clang_cc1 -std=c++23 -pedantic-errors -verify=expected %s
7+
// RUN: %clang_cc1 -std=c++2c -pedantic-errors -verify=expected %s
8+
9+
10+
namespace cwg3005 { // cwg3005: 21 open 2025-03-10
11+
12+
void f(
13+
int _, // #cwg3005-first-param
14+
int _)
15+
// expected-error@-1 {{redefinition of parameter '_'}}
16+
// expected-note@#cwg3005-first-param {{previous definition is here}}
17+
{
18+
int _;
19+
// expected-error@-1 {{redefinition of '_'}}
20+
// expected-note@#cwg3005-first-param {{previous declaration is here}}
21+
}
22+
23+
} // namespace cwg3005

clang/www/cxx_dr_status.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17890,7 +17890,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
1789017890
<td><a href="https://cplusplus.github.io/CWG/issues/3005.html">3005</a></td>
1789117891
<td>open</td>
1789217892
<td>Function parameters should never be name-independent</td>
17893-
<td align="center">Not resolved</td>
17893+
<td align="center">
17894+
<details>
17895+
<summary>Not resolved</summary>
17896+
Clang 21 implements 2025-03-10 resolution
17897+
</details></td>
1789417898
</tr>
1789517899
<tr class="open" id="3006">
1789617900
<td><a href="https://cplusplus.github.io/CWG/issues/3006.html">3006</a></td>

0 commit comments

Comments
 (0)