Skip to content

Commit c6edb59

Browse files
committed
[Clang] Mark declarators invalid in the presence of ill-formed explicit parameters.
To avoid crashes later in sema. Fixes #69962 Fixes #69838
1 parent 3af0ff9 commit c6edb59

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11352,12 +11352,14 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
1135211352
Diag(ExplicitObjectParam->getBeginLoc(),
1135311353
diag::err_explicit_object_parameter_nonmember)
1135411354
<< D.getSourceRange() << /*static=*/0 << IsLambda;
11355+
D.setInvalidType();
1135511356
}
1135611357

1135711358
if (D.getDeclSpec().isVirtualSpecified()) {
1135811359
Diag(ExplicitObjectParam->getBeginLoc(),
1135911360
diag::err_explicit_object_parameter_nonmember)
1136011361
<< D.getSourceRange() << /*virtual=*/1 << IsLambda;
11362+
D.setInvalidType();
1136111363
}
1136211364

1136311365
if (IsLambda && FTI.hasMutableQualifier()) {
@@ -11373,16 +11375,19 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
1137311375
Diag(ExplicitObjectParam->getLocation(),
1137411376
diag::err_explicit_object_parameter_nonmember)
1137511377
<< D.getSourceRange() << /*non-member=*/2 << IsLambda;
11378+
D.setInvalidType();
1137611379
return;
1137711380
}
1137811381

1137911382
// CWG2674: constructors and destructors cannot have explicit parameters.
1138011383
if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
11381-
Name.getNameKind() == DeclarationName::CXXDestructorName)
11384+
Name.getNameKind() == DeclarationName::CXXDestructorName) {
1138211385
Diag(ExplicitObjectParam->getBeginLoc(),
1138311386
diag::err_explicit_object_parameter_constructor)
1138411387
<< (Name.getNameKind() == DeclarationName::CXXDestructorName)
1138511388
<< D.getSourceRange();
11389+
D.setInvalidType();
11390+
}
1138611391
}
1138711392

1138811393
namespace {

clang/test/CXX/drs/dr25xx.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,14 @@ using ::dr2521::operator""_div;
8585
#if __cplusplus >= 202302L
8686
namespace dr2553 { // dr2553: 18
8787
struct B {
88-
virtual void f(this B&); // expected-error {{an explicit object parameter cannot appear in a virtual function}} \
89-
// expected-note {{here}}
88+
virtual void f(this B&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
9089
static void f(this B&); // expected-error {{an explicit object parameter cannot appear in a static function}}
9190
virtual void g(); // expected-note {{here}}
9291
};
9392
struct D : B {
9493
void g(this D&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
9594
};
9695

97-
struct D2 : B {
98-
void f(this B&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
99-
};
100-
10196
}
10297
#endif
10398

clang/test/SemaCXX/cxx2b-deducing-this.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,46 @@ void foo(C c) {
542542
}
543543

544544
}
545+
546+
547+
namespace GH69838 {
548+
struct S {
549+
S(this auto &self) {} // expected-error {{an explicit object parameter cannot appear in a constructor}}
550+
virtual void f(this S self) {} // expected-error {{an explicit object parameter cannot appear in a virtual function}}
551+
void g(this auto &self) const {} // expected-error {{explicit object member function cannot have 'const' qualifier}}
552+
void h(this S self = S{}) {} // expected-error {{the explicit object parameter cannot have a default argument}}
553+
void i(int i, this S self = S{}) {} // expected-error {{an explicit object parameter can only appear as the first parameter of the function}}
554+
~S(this S &&self); // expected-error {{an explicit object parameter cannot appear in a destructor}} \
555+
// expected-error {{destructor cannot have any parameters}}
556+
557+
static void j(this S s); // expected-error {{an explicit object parameter cannot appear in a static function}}
558+
};
559+
560+
void nonmember(this S s); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
561+
562+
int test() {
563+
S s;
564+
s.f();
565+
s.g();
566+
s.h();
567+
s.i(0);
568+
s.j({});
569+
nonmember(S{});
570+
}
571+
572+
}
573+
574+
namespace GH69962 {
575+
struct S {
576+
S(const S&);
577+
};
578+
579+
struct Thing {
580+
template<typename Self, typename ... Args>
581+
Thing(this Self&& self, Args&& ... args) { } // expected-error {{an explicit object parameter cannot appear in a constructor}}
582+
};
583+
584+
class Server : public Thing {
585+
S name_;
586+
};
587+
}

0 commit comments

Comments
 (0)