Skip to content

Commit 62c9b0c

Browse files
authored
[Clang] Fix a crash when diagnosing a non relocatable with no copy ctr (#143350)
If lookup did not find a copy constructor, it would return nulll, leading to an assert in `cast`. Fixes #143325
1 parent f3ffee6 commit 62c9b0c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

clang/lib/Sema/SemaTypeTraits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ static void DiagnoseNonTriviallyRelocatableReason(Sema &SemaRef,
20522052
}
20532053

20542054
if (!D->hasSimpleMoveConstructor() && !D->hasSimpleCopyConstructor()) {
2055-
const auto *Decl = cast<CXXConstructorDecl>(
2055+
const auto *Decl = cast_or_null<CXXConstructorDecl>(
20562056
LookupSpecialMemberFromXValue(SemaRef, D, /*Assign=*/false));
20572057
if (Decl && Decl->isUserProvided())
20582058
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)

clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,50 @@ static_assert(__builtin_is_cpp_trivially_relocatable(U2));
145145

146146
}
147147

148+
149+
namespace GH143325 {
150+
struct Foo { // expected-note {{previous definition is here}}
151+
Foo(const Foo&);
152+
~Foo();
153+
};
154+
155+
struct Foo { // expected-error {{redefinition of 'Foo'}}
156+
Foo();
157+
int;
158+
};
159+
struct Wrapper { // #GH143325-Wrapper
160+
union {
161+
Foo p;
162+
} u;
163+
};
164+
165+
static_assert(__builtin_is_cpp_trivially_relocatable(Wrapper));
166+
// expected-error@-1 {{static assertion failed due to requirement '__builtin_is_cpp_trivially_relocatable(GH143325::Wrapper)'}} \
167+
// expected-note@-1 {{'Wrapper' is not trivially relocatable}} \
168+
// expected-note@-1 {{because it has a non-trivially-relocatable member 'u' of type 'union}} \
169+
// expected-note@-1 {{because it has a deleted destructor}}
170+
// expected-note@#GH143325-Wrapper {{'Wrapper' defined here}}
171+
172+
struct Polymorphic {
173+
virtual ~Polymorphic();
174+
};
175+
176+
struct UnionOfPolymorphic { // #GH143325-UnionOfPolymorphic
177+
union {
178+
Polymorphic p;
179+
int i;
180+
} u;
181+
};
182+
183+
static_assert(__builtin_is_cpp_trivially_relocatable(UnionOfPolymorphic));
184+
// expected-error@-1 {{static assertion failed due to requirement '__builtin_is_cpp_trivially_relocatable(GH143325::UnionOfPolymorphic)'}} \
185+
// expected-note@-1 {{'UnionOfPolymorphic' is not trivially relocatable}} \
186+
// expected-note@-1 {{because it has a non-trivially-relocatable member 'u' of type 'union}} \
187+
// expected-note@-1 {{because it has a deleted destructor}} \
188+
// expected-note@#GH143325-UnionOfPolymorphic {{'UnionOfPolymorphic' defined here}}
189+
190+
}
191+
148192
namespace trivially_copyable {
149193
struct B {
150194
virtual ~B();

0 commit comments

Comments
 (0)