Skip to content
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
8 changes: 6 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3064,11 +3064,11 @@ namespace {
}
}
}
if (copyCtor) {
if (copyCtor && !decl->isAnonymousStructOrUnion()) {
clangSema.DefineImplicitCopyConstructor(clang::SourceLocation(),
copyCtor);
}
if (moveCtor) {
if (moveCtor && !decl->isAnonymousStructOrUnion()) {
clangSema.DefineImplicitMoveConstructor(clang::SourceLocation(),
moveCtor);
}
Expand Down Expand Up @@ -4598,6 +4598,10 @@ namespace {
// FIXME: Temporarily unreachable because of check above.
markAsVariant(result, *correctSwiftName);

if (decl->isAnonymousStructOrUnion())
Impl.markUnavailable(
result, "refer to the members of the anonymous type instead");

return result;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6935,7 +6935,13 @@ namespace {
}

void addValueWitnessTable() {
auto vwtPointer = emitValueWitnessTable(/*relative*/ false).getValue();
llvm::Constant* vwtPointer = nullptr;
if (auto cd = Target->getClangDecl())
if (auto rd = dyn_cast<clang::RecordDecl>(cd))
if (rd->isAnonymousStructOrUnion())
vwtPointer = llvm::Constant::getNullValue(IGM.WitnessTablePtrTy);
if (!vwtPointer)
vwtPointer = emitValueWitnessTable(/*relative*/ false).getValue();
B.addSignedPointer(vwtPointer,
IGM.getOptions().PointerAuth.ValueWitnessTable,
PointerAuthEntity());
Expand Down
15 changes: 11 additions & 4 deletions lib/IRGen/StructLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,17 @@ unsigned irgen::getNumFields(const NominalTypeDecl *target) {
}

bool irgen::isExportableField(Field field) {
if (field.getKind() == Field::Kind::Var &&
field.getVarDecl()->getClangDecl() &&
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
return false;
if (field.getKind() == Field::Kind::Var) {
if (field.getVarDecl()->getClangDecl() &&
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
return false;
// We should not be able to refer to anonymous types.
if (const auto *vd = dyn_cast_or_null<clang::ValueDecl>(
field.getVarDecl()->getClangDecl()))
if (const auto *rd = vd->getType()->getAsRecordDecl())
if (rd->isAnonymousStructOrUnion())
return false;
}
// All other fields are exportable
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions test/Interop/Cxx/class/Inputs/simple-structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ struct HasPublicFieldsOnly {
HasPublicFieldsOnly(int i1, int i2) : publ1(i1), publ2(i2) {}
};

struct HasAnonymousType {
HasAnonymousType(int a, int b, int c) : a(a), b(b), c(c) {}

struct {
int a, b;
};
int c;
};

struct HasPrivatePublicProtectedFields {
private:
int priv1;
Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/class/access-anonymous-field.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-typecheck-verify-swift -I %S/Inputs -swift-version 6 -cxx-interoperability-mode=upcoming-swift

// CHECK: Foobar

import SimpleStructs

let s = HasAnonymousType(1, 2, 3)
let _ = s.__Anonymous_field0 // expected-error {{'__Anonymous_field0' is unavailable: refer to the members of the anonymous type instead}}
// Referring to the members of the anonymous type directly.
let _ = s.a
let _ = s.b
let _ = s.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ struct NonCopyableHolderDerivedDerived: NonCopyableHolderDerived {
inline NonCopyable *getNonCopyablePtr() { return nullptr; }
inline NonCopyableDerived *getNonCopyableDerivedPtr() { return nullptr; }

template <typename T>
struct FieldInAnonStruct {
FieldInAnonStruct() : field(5) {}
FieldInAnonStruct(const FieldInAnonStruct &) = delete;
FieldInAnonStruct(FieldInAnonStruct &&) = default;
struct {
T field;
};
};

using FieldInAnonStructNC = FieldInAnonStruct<NonCopyable>;

#endif // TEST_INTEROP_CXX_CLASS_MOVE_ONLY_VT_H
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ MoveOnlyCxxValueType.test("Test move only field access in derived holder") {
}
#endif

MoveOnlyCxxValueType.test("Test move only field in anonymous struct") {
let a = FieldInAnonStructNC()
let b = a
}
runAllTests()
8 changes: 8 additions & 0 deletions test/Interop/Cxx/class/print-simple-structs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func printCxxStructNested() {
print(s)
}

func printCxxStructWithAnonType() {
let s = HasAnonymousType(1, 2, 3)
print(s)
}

printCxxStructPrivateFields()
// CHECK: HasPrivateFieldsOnly()

Expand All @@ -35,3 +40,6 @@ printCxxStructPrivatePublicProtectedFields()

printCxxStructNested()
// CHECK: Outer(publStruct: {{.*}}.HasPrivatePublicProtectedFields(publ1: 8, publ2: 12))

printCxxStructWithAnonType()
// CHECK: HasAnonymousType(c: 3)