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
4 changes: 4 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4546,6 +4546,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
15 changes: 11 additions & 4 deletions lib/IRGen/StructLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,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
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)