Skip to content

Commit 20d97ad

Browse files
authored
[Clang] Mark declarators invalid in the presence of ill-formed explicit parameters. (#70018)
To avoid crashes later in sema. Fixes #69962 Fixes #69838
1 parent 570c168 commit 20d97ad

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
@@ -11353,12 +11353,14 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
1135311353
Diag(ExplicitObjectParam->getBeginLoc(),
1135411354
diag::err_explicit_object_parameter_nonmember)
1135511355
<< D.getSourceRange() << /*static=*/0 << IsLambda;
11356+
D.setInvalidType();
1135611357
}
1135711358

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

1136411366
if (IsLambda && FTI.hasMutableQualifier()) {
@@ -11374,16 +11376,19 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
1137411376
Diag(ExplicitObjectParam->getLocation(),
1137511377
diag::err_explicit_object_parameter_nonmember)
1137611378
<< D.getSourceRange() << /*non-member=*/2 << IsLambda;
11379+
D.setInvalidType();
1137711380
return;
1137811381
}
1137911382

1138011383
// CWG2674: constructors and destructors cannot have explicit parameters.
1138111384
if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
11382-
Name.getNameKind() == DeclarationName::CXXDestructorName)
11385+
Name.getNameKind() == DeclarationName::CXXDestructorName) {
1138311386
Diag(ExplicitObjectParam->getBeginLoc(),
1138411387
diag::err_explicit_object_parameter_constructor)
1138511388
<< (Name.getNameKind() == DeclarationName::CXXDestructorName)
1138611389
<< D.getSourceRange();
11390+
D.setInvalidType();
11391+
}
1138711392
}
1138811393

1138911394
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)