Skip to content

Commit

Permalink
[Clang] Require base element type of `__has_unique_object_representat…
Browse files Browse the repository at this point in the history
…ions` to be complete (llvm#95432)

Fixes llvm#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]>
  • Loading branch information
MitalAshok and cor3ntin authored Jul 17, 2024
1 parent 396a5ba commit 6451806
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ Bug Fixes in This Version

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

- ``__has_unique_object_representations`` correctly handles arrays of unknown bounds of
types by ensuring they are complete and instantiating them if needed. Fixes (#GH95311).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,10 @@ bool ASTContext::hasUniqueObjectRepresentations(
return hasUniqueObjectRepresentations(getBaseElementType(Ty),
CheckIfTriviallyCopyable);

assert((Ty->isVoidType() || !Ty->isIncompleteType()) &&
"hasUniqueObjectRepresentations should not be called with an "
"incomplete type");

// (9.1) - T is trivially copyable...
if (CheckIfTriviallyCopyable && !Ty.isTriviallyCopyableType(*this))
return false;
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5069,6 +5069,10 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
case UTT_HasTrivialCopy:
case UTT_HasTrivialDestructor:
case UTT_HasVirtualDestructor:
// has_unique_object_representations<T> when T is an array is defined in terms
// of has_unique_object_representations<remove_all_extents_t<T>>, so the base
// type needs to be complete even if the type is an incomplete array type.
case UTT_HasUniqueObjectRepresentations:
ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0);
[[fallthrough]];

Expand All @@ -5077,7 +5081,6 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
case UTT_IsDestructible:
case UTT_IsNothrowDestructible:
case UTT_IsTriviallyDestructible:
case UTT_HasUniqueObjectRepresentations:
if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
return true;

Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/type-traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,17 @@ static_assert(__has_unique_object_representations(_BitInt(8)), "BitInt:");
static_assert(!__has_unique_object_representations(_BitInt(127)), "BitInt:");
static_assert(__has_unique_object_representations(_BitInt(128)), "BitInt:");

namespace GH95311 {

template <int>
class Foo {
int x;
};
static_assert(__has_unique_object_representations(Foo<0>[]));
class Bar; // expected-note {{forward declaration of 'GH95311::Bar'}}
static_assert(__has_unique_object_representations(Bar[])); // expected-error {{incomplete type}}

}

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

0 comments on commit 6451806

Please sign in to comment.