diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 5a123b0b86dda..777c51da590eb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -742,6 +742,8 @@ Bug Fixes to C++ Support - Fix a bug with checking constrained non-type template parameters for equivalence. Fixes (#GH77377). - Fix a bug where the last argument was not considered when considering the most viable function for explicit object argument member functions. Fixes (#GH92188). +- Clang now diagnoses explicit object parameters in member pointers and other contexts where they should not appear. + Fixes (#GH85992). Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index e3b4186f1b06f..91e3c4113d668 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7525,6 +7525,8 @@ def err_explicit_object_parameter_mutable: Error< def err_invalid_explicit_object_type_in_lambda: Error< "invalid explicit object parameter type %0 in lambda with capture; " "the type must be the same as, or derived from, the lambda">; +def err_explicit_object_parameter_invalid: Error< + "an explicit object parameter is not allowed here">; def err_ref_qualifier_overload : Error< "cannot overload a member function %select{without a ref-qualifier|with " diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 8225381985052..3f9b2d91f91ff 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -11463,6 +11463,23 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D, D.setInvalidType(); } + // Handle the following case: + // + // struct S { + // struct T { + // int f(this T); + // }; + // + // friend int T::f(this T); // Allow this. + // friend int f(this S); // But disallow this. + // }; + if (D.getDeclSpec().isFriendSpecified() && D.getCXXScopeSpec().isEmpty()) { + Diag(ExplicitObjectParam->getBeginLoc(), + diag::err_explicit_object_parameter_nonmember) + << D.getSourceRange() << /*non-member=*/2 << IsLambda; + D.setInvalidType(); + } + if (IsLambda && FTI.hasMutableQualifier()) { Diag(ExplicitObjectParam->getBeginLoc(), diag::err_explicit_object_parameter_mutable) @@ -11473,10 +11490,8 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D, return; if (!DC || !DC->isRecord()) { - Diag(ExplicitObjectParam->getLocation(), - diag::err_explicit_object_parameter_nonmember) - << D.getSourceRange() << /*non-member=*/2 << IsLambda; - D.setInvalidType(); + assert(D.isInvalidType() && "Explicit object parameter in non-member " + "should have been diagnosed already"); return; } diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index c19c8cc34dd3b..27e39ec4e195d 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -4845,6 +4845,55 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, // 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(S.computeDeclContext(SS)); + }; + + // [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 either: + // + // - a member-declarator that declares a member function [class.mem], or + // - a lambda-declarator [expr.prim.lambda]. + DeclaratorContext C = D.getContext(); + ParmVarDecl *First = + FTI.NumParams + ? dyn_cast_if_present(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(); + + 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 cdb9d1324b974..39f2c65de59eb 100644 --- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp +++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp @@ -893,3 +893,33 @@ void g() { a * lval; } } + +namespace GH85992 { +struct S { + int (S::*x)(this int); // expected-error {{an explicit object parameter is not allowed here}} + int (*y)(this int); // expected-error {{an explicit object parameter is not allowed here}} + int (***z)(this int); // expected-error {{an explicit object parameter is not allowed here}} + + int f(this S); + int ((g))(this S); + friend int h(this S); // expected-error {{an explicit object parameter cannot appear in a non-member function}} + int h(int x, int (*)(this S)); // expected-error {{an explicit object parameter is not allowed here}} + + struct T { + int f(this T); + }; + + friend int T::f(this T); +}; + +using T = int (*)(this int); // expected-error {{an explicit object parameter is not allowed here}} +using U = int (S::*)(this int); // expected-error {{an explicit object parameter is not allowed here}} +int h(this int); // expected-error {{an explicit object parameter cannot appear in a non-member function}} + +int S::f(this S) { return 1; } + +namespace a { +void f(); +}; +void a::f(this auto) {} // expected-error {{an explicit object parameter cannot appear in a non-member function}} +}