Skip to content

Commit 3b17e59

Browse files
MitalAshokyuxuanchen1997
authored andcommitted
[Clang] Require base element type of __has_unique_object_representations to be complete (#95432)
Summary: Fixes #95311 Previous behaviour was that `false` was silently returned, templated classes were not instantiated and incomplete classes did not issue an error. --------- Co-authored-by: cor3ntin <[email protected]> Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250880
1 parent 47c93c6 commit 3b17e59

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ Bug Fixes in This Version
835835

836836
- Fixed Clang from generating dangling StringRefs when deserializing Exprs & Stmts (#GH98667)
837837

838+
- ``__has_unique_object_representations`` correctly handles arrays of unknown bounds of
839+
types by ensuring they are complete and instantiating them if needed. Fixes (#GH95311).
840+
838841
Bug Fixes to Compiler Builtins
839842
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
840843

clang/lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,10 @@ bool ASTContext::hasUniqueObjectRepresentations(
28312831
return hasUniqueObjectRepresentations(getBaseElementType(Ty),
28322832
CheckIfTriviallyCopyable);
28332833

2834+
assert((Ty->isVoidType() || !Ty->isIncompleteType()) &&
2835+
"hasUniqueObjectRepresentations should not be called with an "
2836+
"incomplete type");
2837+
28342838
// (9.1) - T is trivially copyable...
28352839
if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))
28362840
return false;

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,10 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
50695069
case UTT_HasTrivialCopy:
50705070
case UTT_HasTrivialDestructor:
50715071
case UTT_HasVirtualDestructor:
5072+
// has_unique_object_representations<T> when T is an array is defined in terms
5073+
// of has_unique_object_representations<remove_all_extents_t<T>>, so the base
5074+
// type needs to be complete even if the type is an incomplete array type.
5075+
case UTT_HasUniqueObjectRepresentations:
50725076
ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0);
50735077
[[fallthrough]];
50745078

@@ -5077,7 +5081,6 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
50775081
case UTT_IsDestructible:
50785082
case UTT_IsNothrowDestructible:
50795083
case UTT_IsTriviallyDestructible:
5080-
case UTT_HasUniqueObjectRepresentations:
50815084
if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
50825085
return true;
50835086

clang/test/SemaCXX/type-traits.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,17 @@ static_assert(__has_unique_object_representations(_BitInt(8)), "BitInt:");
35053505
static_assert(!__has_unique_object_representations(_BitInt(127)), "BitInt:");
35063506
static_assert(__has_unique_object_representations(_BitInt(128)), "BitInt:");
35073507

3508+
namespace GH95311 {
3509+
3510+
template <int>
3511+
class Foo {
3512+
int x;
3513+
};
3514+
static_assert(__has_unique_object_representations(Foo<0>[]));
3515+
class Bar; // expected-note {{forward declaration of 'GH95311::Bar'}}
3516+
static_assert(__has_unique_object_representations(Bar[])); // expected-error {{incomplete type}}
3517+
3518+
}
35083519

35093520
namespace PR46209 {
35103521
// Foo has both a trivial assignment operator and a non-trivial one.

0 commit comments

Comments
 (0)