Skip to content
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

[Clang] Mark declarators invalid in the presence of ill-formed explicit parameters. #70018

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

cor3ntin
Copy link
Contributor

To avoid crashes later in sema.

Fixes #69962
Fixes #69838

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 24, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 24, 2023

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

To avoid crashes later in sema.

Fixes #69962
Fixes #69838


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+6-1)
  • (modified) clang/test/SemaCXX/cxx2b-deducing-this.cpp (+43)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 0193e476b3a781b..898758502e51bde 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11352,12 +11352,14 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
     Diag(ExplicitObjectParam->getBeginLoc(),
          diag::err_explicit_object_parameter_nonmember)
         << D.getSourceRange() << /*static=*/0 << IsLambda;
+    D.setInvalidType();
   }
 
   if (D.getDeclSpec().isVirtualSpecified()) {
     Diag(ExplicitObjectParam->getBeginLoc(),
          diag::err_explicit_object_parameter_nonmember)
         << D.getSourceRange() << /*virtual=*/1 << IsLambda;
+    D.setInvalidType();
   }
 
   if (IsLambda && FTI.hasMutableQualifier()) {
@@ -11373,16 +11375,19 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
     Diag(ExplicitObjectParam->getLocation(),
          diag::err_explicit_object_parameter_nonmember)
         << D.getSourceRange() << /*non-member=*/2 << IsLambda;
+    D.setInvalidType();
     return;
   }
 
   // CWG2674: constructors and destructors cannot have explicit parameters.
   if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
-      Name.getNameKind() == DeclarationName::CXXDestructorName)
+      Name.getNameKind() == DeclarationName::CXXDestructorName) {
     Diag(ExplicitObjectParam->getBeginLoc(),
          diag::err_explicit_object_parameter_constructor)
         << (Name.getNameKind() == DeclarationName::CXXDestructorName)
         << D.getSourceRange();
+    D.setInvalidType();
+  }
 }
 
 namespace {
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index cb83270752443ad..535381e876da9c7 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -542,3 +542,46 @@ void foo(C c) {
 }
 
 }
+
+
+namespace GH69838 {
+struct S {
+  S(this auto &self) {} // expected-error {{an explicit object parameter cannot appear in a constructor}}
+  virtual void f(this S self) {} // expected-error {{an explicit object parameter cannot appear in a virtual function}}
+  void g(this auto &self) const {} // expected-error {{explicit object member function cannot have 'const' qualifier}}
+  void h(this S self = S{}) {} // expected-error {{the explicit object parameter cannot have a default argument}}
+  void i(int i, this S self = S{}) {} // expected-error {{an explicit object parameter can only appear as the first parameter of the function}}
+  ~S(this S &&self); // expected-error {{an explicit object parameter cannot appear in a destructor}} \
+                     // expected-error {{destructor cannot have any parameters}}
+
+  static void j(this S s); // expected-error {{an explicit object parameter cannot appear in a static function}}
+};
+
+void nonmember(this S s); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
+
+int test() {
+  S s;
+  s.f();
+  s.g();
+  s.h();
+  s.i(0);
+  s.j({});
+  nonmember(S{});
+}
+
+}
+
+namespace GH69962 {
+struct S {
+    S(const S&);
+};
+
+struct Thing {
+    template<typename Self, typename ... Args>
+    Thing(this Self&& self, Args&& ... args) { } // expected-error {{an explicit object parameter cannot appear in a constructor}}
+};
+
+class Server : public Thing {
+    S name_;
+};
+}

Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

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

Is there no release note because there is no release that supports explicit object parameters yet?

@cor3ntin
Copy link
Contributor Author

Is there no release note because there is no release that supports explicit object parameters yet?

Exactly, yes!

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

…it parameters.

To avoid crashes later in sema.

Fixes llvm#69962
Fixes llvm#69838
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

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

DR test part of this PR looks fine to me.

@cor3ntin cor3ntin merged commit 20d97ad into llvm:main Oct 24, 2023
3 checks passed
static void j(this S s); // expected-error {{an explicit object parameter cannot appear in a static function}}
};

void nonmember(this S s); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could add static here for extra fun.

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
6 participants