Skip to content

Commit 5b62705

Browse files
committed
[Sema] TypeWrappers: If type wrapper comes from .swiftinterface use synthesized decls
Swift interfaces have both attribute and all publicly accessible synthesized members, which means that the synthesis needs to be taught to lookup existing synthesized declarations for `$storage`, `$Storage`, and `init(storageWrapper:)` if request comes for declaration that belongs to a Swift interface file.
1 parent 0013b19 commit 5b62705

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,14 +1657,29 @@ ConstructorDecl *SynthesizeTypeWrappedTypeStorageWrapperInitializer::evaluate(
16571657
if (!wrappedType->hasTypeWrapper())
16581658
return nullptr;
16591659

1660+
auto &ctx = wrappedType->getASTContext();
1661+
1662+
// .swiftinterfaces have both attribute and a synthesized member
1663+
// (if it's public), so in this case we need use existing declaration
1664+
// if available.
1665+
{
1666+
auto parentSF = wrappedType->getDeclContext()->getParentSourceFile();
1667+
if (parentSF && parentSF->Kind == SourceFileKind::Interface) {
1668+
DeclName initName(ctx, DeclBaseName::createConstructor(),
1669+
/*labels=*/{ctx.Id_storageWrapper});
1670+
auto results = wrappedType->lookupDirect(initName);
1671+
if (results.size() == 1)
1672+
return cast<ConstructorDecl>(results.front());
1673+
}
1674+
}
1675+
16601676
// `@typeWrapperIgnored` properties suppress this initializer.
16611677
if (llvm::any_of(wrappedType->getMembers(), [&](Decl *member) {
16621678
return member->getAttrs().hasAttribute<TypeWrapperIgnoredAttr>();
16631679
}))
16641680
return nullptr;
16651681

16661682
// Create the implicit type wrapper storage constructor.
1667-
auto &ctx = wrappedType->getASTContext();
16681683
auto ctor = createImplicitConstructor(
16691684
wrappedType, ImplicitConstructorKind::TypeWrapperStorage, ctx);
16701685
wrappedType->addMember(ctor);

lib/Sema/TypeCheckTypeWrapper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626

2727
using namespace swift;
2828

29+
/// Check whether given declaration comes from the .swiftinterface file.
30+
static bool inSwiftInterfaceContext(NominalTypeDecl *typeDecl) {
31+
auto *SF = typeDecl->getDeclContext()->getParentSourceFile();
32+
return SF && SF->Kind == SourceFileKind::Interface;
33+
}
34+
35+
static ValueDecl *findMember(NominalTypeDecl *typeDecl, Identifier memberName) {
36+
auto members = typeDecl->lookupDirect(memberName);
37+
return members.size() == 1 ? members.front() : nullptr;
38+
}
39+
2940
static PatternBindingDecl *injectVariable(DeclContext *DC, Identifier name,
3041
Type type,
3142
VarDecl::Introducer introducer,
@@ -284,6 +295,15 @@ TypeDecl *GetTypeWrapperStorage::evaluate(Evaluator &evaluator,
284295

285296
auto &ctx = parent->getASTContext();
286297

298+
// .swiftinterfaces have both attribute and a synthesized member
299+
// (if it's public), so in this case we need use existing declaration
300+
// if available.
301+
if (inSwiftInterfaceContext(parent)) {
302+
if (auto *storage = dyn_cast_or_null<TypeDecl>(
303+
findMember(parent, ctx.Id_TypeWrapperStorage)))
304+
return storage;
305+
}
306+
287307
TypeDecl *storage = nullptr;
288308
if (isa<ProtocolDecl>(parent)) {
289309
// If type wrapper is associated with a protocol, we need to
@@ -320,6 +340,15 @@ GetTypeWrapperProperty::evaluate(Evaluator &evaluator,
320340
if (!typeWrapper)
321341
return nullptr;
322342

343+
// .swiftinterfaces have both attribute and a synthesized member
344+
// (if it's public), so in this case we need use existing declaration
345+
// if available.
346+
if (inSwiftInterfaceContext(parent)) {
347+
if (auto *storage = dyn_cast_or_null<VarDecl>(
348+
findMember(parent, ctx.Id_TypeWrapperProperty)))
349+
return storage;
350+
}
351+
323352
auto *storage = parent->getTypeWrapperStorageDecl();
324353
assert(storage);
325354

0 commit comments

Comments
 (0)