-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[Clang] Disallow explicit object parameters irrespective of whether return type is valid #174603
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
Conversation
|
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 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. |
|
@llvm/pr-subscribers-clang Author: Kartik (hax0kartik) ChangesThis fixes issue: #173943 Full diff: https://github.com/llvm/llvm-project/pull/174603.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 411cc348d4caf..b95b4ee65282f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -594,6 +594,7 @@ Bug Fixes to C++ Support
"initializing multiple members of union" coincide (#GH149985).
- Fix a crash when using ``explicit(bool)`` in pre-C++11 language modes. (#GH152729)
- Fix the parsing of variadic member functions when the ellipis immediately follows a default argument.(#GH153445)
+- Fix a crash when using an explicit object parameter in a non-member function with an invalid return type.(#GH173943)
- Fixed a bug that caused ``this`` captured by value in a lambda with a dependent explicit object parameter to not be
instantiated properly. (#GH154054)
- Fixed a bug where our ``member-like constrained friend`` checking caused an incorrect analysis of lambda captures. (#GH156225)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 7ef83433326ed..46b668b7e0c0a 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -4834,66 +4834,67 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
IsQualifiedFunction =
FTI.hasMethodTypeQualifiers() || FTI.hasRefQualifier();
+ auto IsClassType = [&](CXXScopeSpec &SS) {
+ // If there already was an problem with the scope, don’t issue another
+ // error about the explicit object parameter.
+ return SS.isInvalid() ||
+ isa_and_present<CXXRecordDecl>(S.computeDeclContext(SS));
+ };
+
+ // C++23 [dcl.fct]p6:
+ //
+ // An explicit-object-parameter-declaration is a parameter-declaration
+ // with a this specifier. An explicit-object-parameter-declaration shall
+ // appear only as the first parameter-declaration of a
+ // parameter-declaration-list of one of:
+ //
+ // - a declaration of a member function or member function template
+ // ([class.mem]), or
+ //
+ // - an explicit instantiation ([temp.explicit]) or explicit
+ // specialization ([temp.expl.spec]) of a templated member function,
+ // or
+ //
+ // - a lambda-declarator [expr.prim.lambda].
+ DeclaratorContext C = D.getContext();
+ ParmVarDecl *First =
+ FTI.NumParams
+ ? dyn_cast_if_present<ParmVarDecl>(FTI.Params[0].Param)
+ : nullptr;
+
+ bool IsFunctionDecl = D.getInnermostNonParenChunk() == &DeclType;
+ if (First && First->isExplicitObjectParameter() &&
+ C != DeclaratorContext::LambdaExpr &&
+
+ // Either not a member or nested declarator in a member.
+ //
+ // Note that e.g. 'static' or 'friend' declarations are accepted
+ // here; we diagnose them later when we build the member function
+ // because it's easier that way.
+ (C != DeclaratorContext::Member || !IsFunctionDecl) &&
+
+ // Allow out-of-line definitions of member functions.
+ !IsClassType(D.getCXXScopeSpec())) {
+ if (IsFunctionDecl)
+ S.Diag(First->getBeginLoc(),
+ diag::err_explicit_object_parameter_nonmember)
+ << /*non-member*/ 2 << /*function*/ 0
+ << First->getSourceRange();
+ else
+ S.Diag(First->getBeginLoc(),
+ diag::err_explicit_object_parameter_invalid)
+ << First->getSourceRange();
+
+ // Do let non-member function have explicit parameters
+ // to not break assumptions elsewhere in the code.
+ First->setExplicitObjectParameterLoc(SourceLocation());
+ D.setInvalidType();
+ AreDeclaratorChunksValid = false;
+ }
+
// Check for auto functions and trailing return type and adjust the
// return type accordingly.
if (!D.isInvalidType()) {
- auto IsClassType = [&](CXXScopeSpec &SS) {
- // If there already was an problem with the scope, don’t issue another
- // error about the explicit object parameter.
- return SS.isInvalid() ||
- isa_and_present<CXXRecordDecl>(S.computeDeclContext(SS));
- };
-
- // C++23 [dcl.fct]p6:
- //
- // An explicit-object-parameter-declaration is a parameter-declaration
- // with a this specifier. An explicit-object-parameter-declaration shall
- // appear only as the first parameter-declaration of a
- // parameter-declaration-list of one of:
- //
- // - a declaration of a member function or member function template
- // ([class.mem]), or
- //
- // - an explicit instantiation ([temp.explicit]) or explicit
- // specialization ([temp.expl.spec]) of a templated member function,
- // or
- //
- // - a lambda-declarator [expr.prim.lambda].
- DeclaratorContext C = D.getContext();
- ParmVarDecl *First =
- FTI.NumParams
- ? dyn_cast_if_present<ParmVarDecl>(FTI.Params[0].Param)
- : nullptr;
-
- bool IsFunctionDecl = D.getInnermostNonParenChunk() == &DeclType;
- if (First && First->isExplicitObjectParameter() &&
- C != DeclaratorContext::LambdaExpr &&
-
- // Either not a member or nested declarator in a member.
- //
- // Note that e.g. 'static' or 'friend' declarations are accepted
- // here; we diagnose them later when we build the member function
- // because it's easier that way.
- (C != DeclaratorContext::Member || !IsFunctionDecl) &&
-
- // Allow out-of-line definitions of member functions.
- !IsClassType(D.getCXXScopeSpec())) {
- if (IsFunctionDecl)
- S.Diag(First->getBeginLoc(),
- diag::err_explicit_object_parameter_nonmember)
- << /*non-member*/ 2 << /*function*/ 0
- << First->getSourceRange();
- else
- S.Diag(First->getBeginLoc(),
- diag::err_explicit_object_parameter_invalid)
- << First->getSourceRange();
- // Do let non-member function have explicit parameters
- // to not break assumptions elsewhere in the code.
- First->setExplicitObjectParameterLoc(SourceLocation());
- D.setInvalidType();
- AreDeclaratorChunksValid = false;
- }
-
// trailing-return-type is only required if we're declaring a function,
// and not, for instance, a pointer to a function.
if (D.getDeclSpec().hasAutoTypeSpec() &&
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 6777dc23c44a6..a9e31c3d06676 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -1389,3 +1389,14 @@ void f() {
}
}
+
+namespace GH173943 {
+
+a void Bar(this int) { // expected-note {{candidate function}}
+ // expected-error@-1 {{unknown type name 'a'}}
+ // expected-error@-2 {{an explicit object parameter cannot appear in a non-member function}}
+ Bar(0);
+ Bar(); // expected-error {{no matching function for call to 'Bar'}}
+}
+
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Sirraide
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the reproducer from the issue as a test case as well? Otherwise, this lgtm.
Thanks for the quick review! The testcase in that issue only works when |
Yes, it should be added regardless to demonstrate that this patch actually fixes that issue; if it requires C++98 you’ll also have to put it in a separate file |
a62a0e2 to
5740fe4
Compare
…eturn type is valid
5740fe4 to
1076362
Compare
Added, please check again. Thanks! |
|
@hax0kartik 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 receive 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! |
Fixes: #173943