Skip to content

Commit 447e13b

Browse files
authored
Merge pull request #78989 from beccadax/rdar142693093-6.1
🌸 [6.1] Work around Foundation NS_TYPED_ENUM bug
2 parents 707ea39 + 822e1a9 commit 447e13b

File tree

11 files changed

+60
-1
lines changed

11 files changed

+60
-1
lines changed

include/swift/SIL/PrettyStackTrace.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/SIL/SILLocation.h"
2222
#include "swift/SIL/SILNode.h"
23+
#include "swift/SIL/SILDeclRef.h"
2324
#include "llvm/ADT/SmallString.h"
2425
#include "llvm/ADT/Twine.h"
2526
#include "llvm/Support/PrettyStackTrace.h"
@@ -82,6 +83,18 @@ class PrettyStackTraceSILNode : public llvm::PrettyStackTraceEntry {
8283
virtual void print(llvm::raw_ostream &OS) const override;
8384
};
8485

86+
/// Observe that we are processing a reference to a SIL decl.
87+
class PrettyStackTraceSILDeclRef : public llvm::PrettyStackTraceEntry {
88+
SILDeclRef declRef;
89+
StringRef action;
90+
91+
public:
92+
PrettyStackTraceSILDeclRef(const char *action, SILDeclRef declRef)
93+
: declRef(declRef), action(action) {}
94+
95+
virtual void print(llvm::raw_ostream &os) const override;
96+
};
97+
8598
} // end namespace swift
8699

87100
#endif

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5777,6 +5777,7 @@ namespace {
57775777
printCommon(#Name, label); \
57785778
\
57795779
printFieldQuoted(T->getDecl()->printRef(), Label::always("decl")); \
5780+
printFlag(T->getDecl()->hasClangNode(), "foreign"); \
57805781
\
57815782
if (T->getParent()) \
57825783
printRec(T->getParent(), Label::always("parent")); \
@@ -5790,6 +5791,7 @@ namespace {
57905791
BoundGeneric##TypeClass *T, Label label) { \
57915792
printCommon("bound_generic_" #Name, label); \
57925793
printFieldQuoted(T->getDecl()->printRef(), Label::always("decl")); \
5794+
printFlag(T->getDecl()->hasClangNode(), "foreign"); \
57935795
if (T->getParent()) \
57945796
printRec(T->getParent(), Label::always("parent")); \
57955797
printList(T->getGenericArgs(), [&](auto arg, Label label) { \
@@ -5838,6 +5840,7 @@ namespace {
58385840
void visitModuleType(ModuleType *T, Label label) {
58395841
printCommon("module_type", label);
58405842
printDeclName(T->getModule(), Label::always("module"));
5843+
printFlag(T->getModule()->isNonSwiftModule(), "foreign");
58415844
printFoot();
58425845
}
58435846

lib/ClangImporter/ImportDecl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6277,6 +6277,19 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
62776277
synthesizedProtocols.push_back(kind);
62786278
return true;
62796279
}
6280+
// HACK: This method may be called before all extensions have been bound.
6281+
// This is a problem for newtypes in Foundation, which is what provides the
6282+
// `String: _ObjectiveCBridgeable` conformance; it can cause us to create
6283+
// `String`-backed newtypes which aren't bridgeable, causing typecheck
6284+
// failures and crashes down the line (rdar://142693093). Hardcode knowledge
6285+
// that this conformance will exist.
6286+
// FIXME: Defer adding conformances to newtypes instead of this. (#78731)
6287+
if (structDecl->getModuleContext()->isFoundationModule()
6288+
&& kind == KnownProtocolKind::ObjectiveCBridgeable
6289+
&& computedNominal == ctx.getStringDecl()) {
6290+
synthesizedProtocols.push_back(kind);
6291+
return true;
6292+
}
62806293

62816294
return false;
62826295
};

lib/IRGen/GenCall.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/ClangModuleLoader.h"
2222
#include "swift/AST/GenericEnvironment.h"
23+
#include "swift/AST/PrettyStackTrace.h"
2324
#include "swift/Basic/Assertions.h"
2425
#include "swift/IRGen/Linking.h"
2526
#include "swift/Runtime/Config.h"
@@ -1473,6 +1474,8 @@ static bool doesClangExpansionMatchSchema(IRGenModule &IGM,
14731474
/// Expand the result and parameter types to the appropriate LLVM IR
14741475
/// types for C, C++ and Objective-C signatures.
14751476
void SignatureExpansion::expandExternalSignatureTypes() {
1477+
PrettyStackTraceType entry(IGM.Context, "using clang to expand signature for",
1478+
FnType);
14761479
assert(FnType->getLanguage() == SILFunctionLanguage::C);
14771480

14781481
auto SILResultTy = [&]() {

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "swift/IRGen/Linking.h"
3535
#include "swift/Runtime/HeapObject.h"
3636
#include "swift/SIL/FormalLinkage.h"
37+
#include "swift/SIL/PrettyStackTrace.h"
3738
#include "swift/SIL/SILDebugScope.h"
3839
#include "swift/SIL/SILModule.h"
3940
#include "swift/Subsystems.h"
@@ -3512,6 +3513,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
35123513
assert(forDefinition || !isDynamicallyReplaceableImplementation);
35133514
assert(!forDefinition || !shouldCallPreviousImplementation);
35143515

3516+
PrettyStackTraceSILFunction entry("lowering address of", f);
35153517
LinkEntity entity =
35163518
LinkEntity::forSILFunction(f, shouldCallPreviousImplementation);
35173519

lib/IRGen/GenObjC.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/ClangImporter/ClangImporter.h"
3434
#include "swift/Demangling/ManglingMacros.h"
3535
#include "swift/IRGen/Linking.h"
36+
#include "swift/SIL/PrettyStackTrace.h"
3637
#include "swift/SIL/SILModule.h"
3738
#include "clang/AST/Attr.h"
3839
#include "clang/AST/DeclObjC.h"
@@ -779,6 +780,8 @@ Callee irgen::getObjCMethodCallee(IRGenFunction &IGF,
779780
llvm::Value *selfValue,
780781
CalleeInfo &&info) {
781782
SILDeclRef method = methodInfo.getMethod();
783+
PrettyStackTraceSILDeclRef entry("lowering reference to ObjC method", method);
784+
782785
// Note that isolated deallocator is never called directly, only from regular
783786
// deallocator
784787
assert((method.kind == SILDeclRef::Kind::Initializer

lib/IRGen/IRGenSIL.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,8 @@ static bool mayDirectlyCallAsync(SILFunction *fn) {
31183118

31193119
void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {
31203120
auto fn = i->getInitiallyReferencedFunction();
3121+
PrettyStackTraceSILFunction entry("lowering reference to", fn);
3122+
31213123
auto fnType = fn->getLoweredFunctionType();
31223124

31233125
auto fpKind = irgen::classifyFunctionPointerKind(fn);
@@ -8101,6 +8103,8 @@ void IRGenSILFunction::visitWitnessMethodInst(swift::WitnessMethodInst *i) {
81018103
CanType baseTy = i->getLookupType();
81028104
ProtocolConformanceRef conformance = i->getConformance();
81038105
SILDeclRef member = i->getMember();
8106+
PrettyStackTraceSILDeclRef entry("lowering use of witness method", member);
8107+
81048108
auto fnType = IGM.getSILTypes().getConstantFunctionType(
81058109
IGM.getMaximalTypeExpansionContext(), member);
81068110

@@ -8285,6 +8289,7 @@ void IRGenSILFunction::visitSuperMethodInst(swift::SuperMethodInst *i) {
82858289
llvm::Value *baseValue = base.claimNext();
82868290

82878291
auto method = i->getMember().getOverriddenVTableEntry();
8292+
PrettyStackTraceSILDeclRef entry("lowering super call to", method);
82888293
auto methodType = i->getType().castTo<SILFunctionType>();
82898294

82908295
auto *classDecl = cast<ClassDecl>(method.getDecl()->getDeclContext());
@@ -8381,6 +8386,8 @@ void IRGenSILFunction::visitClassMethodInst(swift::ClassMethodInst *i) {
83818386
llvm::Value *baseValue = base.claimNext();
83828387

83838388
SILDeclRef method = i->getMember().getOverriddenVTableEntry();
8389+
PrettyStackTraceSILDeclRef entry("lowering class method call to", method);
8390+
83848391
auto methodType = i->getType().castTo<SILFunctionType>();
83858392

83868393
auto *classDecl = cast<ClassDecl>(method.getDecl()->getDeclContext());

lib/SIL/Utils/PrettyStackTrace.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ void PrettyStackTraceSILNode::print(llvm::raw_ostream &out) const {
104104
if (Node)
105105
out << *Node;
106106
}
107+
108+
void PrettyStackTraceSILDeclRef::print(llvm::raw_ostream &out) const {
109+
out << "While " << action << " SIL decl '";
110+
declRef.print(out);
111+
out << "'\n";
112+
}

test/ClangImporter/newtype_conformance.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func acceptHashable<T: Hashable>(_: T) {}
1818
func acceptComparable<T: Comparable>(_: T) {}
1919
// expected-note@-1 {{where 'T' = 'NSNotification.Name'}}
2020

21-
func testNewTypeWrapper(x: NSNotification.Name, y: NSNotification.Name) {
21+
func testNewTypeWrapper(x: NSNotification.Name, y: NSNotification.Name, z: NSFileAttributeKey) {
2222
acceptEquatable(x)
2323
acceptHashable(x)
2424
acceptComparable(x) // expected-error {{global function 'acceptComparable' requires that 'NSNotification.Name' conform to 'Comparable'}}
@@ -28,6 +28,8 @@ func testNewTypeWrapper(x: NSNotification.Name, y: NSNotification.Name) {
2828
_ = x != y
2929
_ = x.hashValue
3030
_ = x as NSString
31+
32+
_ = z as NSString
3133
}
3234

3335

test/Inputs/clang-importer-sdk/swift-modules/Foundation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
public let NSUTF8StringEncoding: UInt = 8
66

7+
// This extension will cause ClangImporter/newtype_conformance.swift to fail
8+
// unless rdar://142693093 is fixed. To reproduce, it's important that this
9+
// extension come *before* the _ObjectiveCBridgeable extension for String.
10+
extension NSFileAttributeKey { }
11+
712
extension AnyHashable : _ObjectiveCBridgeable {
813
public func _bridgeToObjectiveC() -> NSObject {
914
return NSObject()

test/Inputs/clang-importer-sdk/usr/include/Foundation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ extern void CGColorRelease(CGColorRef color) __attribute__((availability(macosx,
867867

868868
typedef NSString *_Nonnull NSNotificationName
869869
__attribute((swift_newtype(struct)));
870+
typedef NSString *_Nonnull NSFileAttributeKey
871+
__attribute((swift_newtype(struct)));
870872

871873
NS_SWIFT_UNAVAILABLE("Use NSXPCConnection instead")
872874
extern NSString * const NSConnectionReplyMode;

0 commit comments

Comments
 (0)