Skip to content

[clang] Reject incomplete types in __is_layout_compatible() #87869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6026,6 +6026,11 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
return false;
}
case BTT_IsLayoutCompatible: {
if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType())
Self.RequireCompleteType(KeyLoc, LhsT, diag::err_incomplete_type);
if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType())
Self.RequireCompleteType(KeyLoc, RhsT, diag::err_incomplete_type);

if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
Self.Diag(KeyLoc, diag::err_vla_unsupported)
<< 1 << tok::kw___is_layout_compatible;
Expand Down
39 changes: 33 additions & 6 deletions clang/test/SemaCXX/type-traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ enum class EnumClassLayout {};
enum EnumForward : int;
enum class EnumClassForward;

struct CStructIncomplete;
struct CStructIncomplete; // #CStructIncomplete

struct CStructNested {
int a;
Expand Down Expand Up @@ -1719,6 +1719,20 @@ struct StructWithAnonUnion3 {
} u;
};

struct CStructWithArrayAtTheEnd {
int a;
int b[4];
};

struct CStructWithFMA {
int c;
int d[];
};

struct CStructWithFMA2 {
int e;
int f[];
};

void is_layout_compatible(int n)
{
Expand Down Expand Up @@ -1800,15 +1814,28 @@ void is_layout_compatible(int n)
static_assert(__is_layout_compatible(EnumLayout, EnumClassLayout));
static_assert(__is_layout_compatible(EnumForward, EnumForward));
static_assert(__is_layout_compatible(EnumForward, EnumClassForward));
// Layout compatibility for enums might be relaxed in the future. See https://github.com/cplusplus/CWG/issues/39#issuecomment-1184791364
static_assert(__is_layout_compatible(CStructIncomplete, CStructIncomplete));
// expected-error@-1 {{incomplete type 'CStructIncomplete' where a complete type is required}}
// expected-note@#CStructIncomplete {{forward declaration of 'CStructIncomplete'}}
// expected-error@-3 {{incomplete type 'CStructIncomplete' where a complete type is required}}
// expected-note@#CStructIncomplete {{forward declaration of 'CStructIncomplete'}}
static_assert(!__is_layout_compatible(CStruct, CStructIncomplete));
// expected-error@-1 {{incomplete type 'CStructIncomplete' where a complete type is required}}
// expected-note@#CStructIncomplete {{forward declaration of 'CStructIncomplete'}}
static_assert(__is_layout_compatible(CStructIncomplete[2], CStructIncomplete[2]));
// expected-error@-1 {{incomplete type 'CStructIncomplete[2]' where a complete type is required}}
// expected-note@#CStructIncomplete {{forward declaration of 'CStructIncomplete'}}
// expected-error@-3 {{incomplete type 'CStructIncomplete[2]' where a complete type is required}}
// expected-note@#CStructIncomplete {{forward declaration of 'CStructIncomplete'}}
static_assert(__is_layout_compatible(CStructIncomplete[], CStructIncomplete[]));
static_assert(!__is_layout_compatible(CStructWithArrayAtTheEnd, CStructWithFMA));
static_assert(__is_layout_compatible(CStructWithFMA, CStructWithFMA));
static_assert(__is_layout_compatible(CStructWithFMA, CStructWithFMA2));
// Layout compatibility rules for enums might be relaxed in the future. See https://github.com/cplusplus/CWG/issues/39#issuecomment-1184791364
static_assert(!__is_layout_compatible(EnumLayout, int));
static_assert(!__is_layout_compatible(EnumClassLayout, int));
static_assert(!__is_layout_compatible(EnumForward, int));
static_assert(!__is_layout_compatible(EnumClassForward, int));
// FIXME: the following should be rejected (array of unknown bound and void are the only allowed incomplete types)
static_assert(__is_layout_compatible(CStructIncomplete, CStructIncomplete));
static_assert(!__is_layout_compatible(CStruct, CStructIncomplete));
static_assert(__is_layout_compatible(CStructIncomplete[2], CStructIncomplete[2]));
}

void is_signed()
Expand Down