Skip to content

Commit ebea19d

Browse files
authored
Merge pull request swiftlang#78697 from beccadax/rdar142693093
Work around Foundation NS_TYPED_ENUM bug
2 parents 5066dd9 + 0ce403d commit ebea19d

File tree

11 files changed

+78
-36
lines changed

11 files changed

+78
-36
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: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4883,18 +4883,34 @@ namespace {
48834883
printCommon(#Name, label); \
48844884
\
48854885
printFieldQuoted(T->getDecl()->printRef(), "decl"); \
4886+
printFlag(T->getDecl()->hasClangNode(), "foreign"); \
48864887
\
48874888
if (T->getParent()) \
48884889
printRec(T->getParent(), "parent"); \
48894890
\
48904891
printFoot(); \
48914892
}
48924893

4893-
VISIT_NOMINAL_TYPE(EnumType, enum_type)
4894-
VISIT_NOMINAL_TYPE(StructType, struct_type)
4895-
VISIT_NOMINAL_TYPE(ClassType, class_type)
4894+
#define VISIT_BINDABLE_NOMINAL_TYPE(TypeClass, Name) \
4895+
VISIT_NOMINAL_TYPE(TypeClass, Name) \
4896+
void visitBoundGeneric##TypeClass( \
4897+
BoundGeneric##TypeClass *T, StringRef label) { \
4898+
printCommon("bound_generic_" #Name, label); \
4899+
printFieldQuoted(T->getDecl()->printRef(), "decl"); \
4900+
printFlag(T->getDecl()->hasClangNode(), "foreign"); \
4901+
if (T->getParent()) \
4902+
printRec(T->getParent(), "parent"); \
4903+
for (auto arg : T->getGenericArgs()) \
4904+
printRec(arg); \
4905+
printFoot(); \
4906+
}
4907+
4908+
VISIT_BINDABLE_NOMINAL_TYPE(EnumType, enum_type)
4909+
VISIT_BINDABLE_NOMINAL_TYPE(StructType, struct_type)
4910+
VISIT_BINDABLE_NOMINAL_TYPE(ClassType, class_type)
48964911
VISIT_NOMINAL_TYPE(ProtocolType, protocol_type)
48974912

4913+
#undef VISIT_BINDABLE_NOMINAL_TYPE
48984914
#undef VISIT_NOMINAL_TYPE
48994915

49004916
void visitBuiltinTupleType(BuiltinTupleType *T, StringRef label) {
@@ -4929,6 +4945,7 @@ namespace {
49294945
void visitModuleType(ModuleType *T, StringRef label) {
49304946
printCommon("module_type", label);
49314947
printDeclNameField(T->getModule(), "module");
4948+
printFlag(T->getModule()->isNonSwiftModule(), "foreign");
49324949
printFoot();
49334950
}
49344951

@@ -5046,7 +5063,7 @@ namespace {
50465063
void printAnyFunctionParamsRec(ArrayRef<AnyFunctionType::Param> params,
50475064
StringRef label) {
50485065
printRecArbitrary([&](StringRef label) {
5049-
printCommon("function_params", label);
5066+
printHead("function_params", FieldLabelColor, label);
50505067

50515068
printField(params.size(), "num_params");
50525069
for (const auto &param : params) {
@@ -5257,37 +5274,6 @@ namespace {
52575274
printFoot();
52585275
}
52595276

5260-
void visitBoundGenericClassType(BoundGenericClassType *T, StringRef label) {
5261-
printCommon("bound_generic_class_type", label);
5262-
printFieldQuoted(T->getDecl()->printRef(), "decl");
5263-
if (T->getParent())
5264-
printRec(T->getParent(), "parent");
5265-
for (auto arg : T->getGenericArgs())
5266-
printRec(arg);
5267-
printFoot();
5268-
}
5269-
5270-
void visitBoundGenericStructType(BoundGenericStructType *T,
5271-
StringRef label) {
5272-
printCommon("bound_generic_struct_type", label);
5273-
printFieldQuoted(T->getDecl()->printRef(), "decl");
5274-
if (T->getParent())
5275-
printRec(T->getParent(), "parent");
5276-
for (auto arg : T->getGenericArgs())
5277-
printRec(arg);
5278-
printFoot();
5279-
}
5280-
5281-
void visitBoundGenericEnumType(BoundGenericEnumType *T, StringRef label) {
5282-
printCommon("bound_generic_enum_type", label);
5283-
printFieldQuoted(T->getDecl()->printRef(), "decl");
5284-
if (T->getParent())
5285-
printRec(T->getParent(), "parent");
5286-
for (auto arg : T->getGenericArgs())
5287-
printRec(arg);
5288-
printFoot();
5289-
}
5290-
52915277
void visitTypeVariableType(TypeVariableType *T, StringRef label) {
52925278
printCommon("type_variable_type", label);
52935279
printField(T->getID(), "id");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,6 +6369,19 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
63696369
synthesizedProtocols.push_back(kind);
63706370
return true;
63716371
}
6372+
// HACK: This method may be called before all extensions have been bound.
6373+
// This is a problem for newtypes in Foundation, which is what provides the
6374+
// `String: _ObjectiveCBridgeable` conformance; it can cause us to create
6375+
// `String`-backed newtypes which aren't bridgeable, causing typecheck
6376+
// failures and crashes down the line (rdar://142693093). Hardcode knowledge
6377+
// that this conformance will exist.
6378+
// FIXME: Defer adding conformances to newtypes instead of this. (#78731)
6379+
if (structDecl->getModuleContext()->isFoundationModule()
6380+
&& kind == KnownProtocolKind::ObjectiveCBridgeable
6381+
&& computedNominal == ctx.getStringDecl()) {
6382+
synthesizedProtocols.push_back(kind);
6383+
return true;
6384+
}
63726385

63736386
return false;
63746387
};

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"
@@ -3515,6 +3516,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
35153516
assert(forDefinition || !isDynamicallyReplaceableImplementation);
35163517
assert(!forDefinition || !shouldCallPreviousImplementation);
35173518

3519+
PrettyStackTraceSILFunction entry("lowering address of", f);
35183520
LinkEntity entity =
35193521
LinkEntity::forSILFunction(f, shouldCallPreviousImplementation);
35203522

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
@@ -3117,6 +3117,8 @@ static bool mayDirectlyCallAsync(SILFunction *fn) {
31173117

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

31223124
auto fpKind = irgen::classifyFunctionPointerKind(fn);
@@ -8098,6 +8100,8 @@ void IRGenSILFunction::visitWitnessMethodInst(swift::WitnessMethodInst *i) {
80988100
CanType baseTy = i->getLookupType();
80998101
ProtocolConformanceRef conformance = i->getConformance();
81008102
SILDeclRef member = i->getMember();
8103+
PrettyStackTraceSILDeclRef entry("lowering use of witness method", member);
8104+
81018105
auto fnType = IGM.getSILTypes().getConstantFunctionType(
81028106
IGM.getMaximalTypeExpansionContext(), member);
81038107

@@ -8282,6 +8286,7 @@ void IRGenSILFunction::visitSuperMethodInst(swift::SuperMethodInst *i) {
82828286
llvm::Value *baseValue = base.claimNext();
82838287

82848288
auto method = i->getMember().getOverriddenVTableEntry();
8289+
PrettyStackTraceSILDeclRef entry("lowering super call to", method);
82858290
auto methodType = i->getType().castTo<SILFunctionType>();
82868291

82878292
auto *classDecl = cast<ClassDecl>(method.getDecl()->getDeclContext());
@@ -8378,6 +8383,8 @@ void IRGenSILFunction::visitClassMethodInst(swift::ClassMethodInst *i) {
83788383
llvm::Value *baseValue = base.claimNext();
83798384

83808385
SILDeclRef method = i->getMember().getOverriddenVTableEntry();
8386+
PrettyStackTraceSILDeclRef entry("lowering class method call to", method);
8387+
83818388
auto methodType = i->getType().castTo<SILFunctionType>();
83828389

83838390
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()

0 commit comments

Comments
 (0)