Skip to content

Commit 51529ae

Browse files
committed
Eliminate the -enable-id-as-any flag; it's always on now anyway.
Simplify e.g., ASTContext::getBridgedToObjC(), which no longer needs the optional return. Eliminate the now-unused constraint kind for checking bridging to Objective-C.
1 parent f9d1094 commit 51529ae

33 files changed

+87
-267
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,10 @@ class ASTContext {
522522
///
523523
/// \param dc The context in which bridging is occurring.
524524
/// \param type The Swift for which we are querying bridging behavior.
525-
/// \param resolver The lazy resolver.
526525
/// \param bridgedValueType The specific value type that is bridged,
527526
/// which will usually by the same as \c type.
528-
Optional<Type> getBridgedToObjC(const DeclContext *dc,
529-
Type type,
530-
LazyResolver *resolver,
531-
Type *bridgedValueType = nullptr) const;
527+
Type getBridgedToObjC(const DeclContext *dc, Type type,
528+
Type *bridgedValueType = nullptr) const;
532529

533530
/// Determine whether the given Swift type is representable in a
534531
/// given foreign language.

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ namespace swift {
155155
/// and methods.
156156
bool InferImportAsMember = false;
157157

158-
/// Should 'id' in Objective-C be imported as 'Any' in Swift?
159-
bool EnableIdAsAny = true;
160-
161158
/// Sets the target we are building for and updates platform conditions
162159
/// to match.
163160
///

include/swift/Option/FrontendOptions.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,4 @@ def enable_resilience : Flag<["-"], "enable-resilience">,
337337
def group_info_path : Separate<["-"], "group-info-path">,
338338
HelpText<"The path to collect the group information of the compiled module">;
339339

340-
def enable_id_as_any: Flag<["-"], "enable-id-as-any">,
341-
HelpText<"Enable importing ObjC 'id' as Swift 'Any' type">;
342-
343340
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,46 +3998,14 @@ bool ASTContext::isStandardLibraryTypeBridgedInFoundation(
39983998
nominal->getName() == Id_CGFloat);
39993999
}
40004000

4001-
Optional<Type>
4002-
ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
4003-
LazyResolver *resolver,
4004-
Type *bridgedValueType) const {
4001+
Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
4002+
Type *bridgedValueType) const {
40054003
if (type->isBridgeableObjectType()) {
40064004
if (bridgedValueType) *bridgedValueType = type;
40074005

40084006
return type;
40094007
}
40104008

4011-
// Whitelist certain types even if Foundation is not imported, to ensure
4012-
// that casts from AnyObject to one of these types are not optimized away.
4013-
//
4014-
// Outside of these standard library types to which Foundation
4015-
// bridges, an _ObjectiveCBridgeable conformance can only be added
4016-
// in the same module where the Swift type itself is defined, so the
4017-
// optimizer will be guaranteed to see the conformance if it exists.
4018-
bool knownBridgedToObjC = false;
4019-
if (auto ntd = type->getAnyNominal())
4020-
knownBridgedToObjC = isStandardLibraryTypeBridgedInFoundation(ntd);
4021-
4022-
// TODO: Under id-as-any, container bridging is unconstrained. This check can
4023-
// go away.
4024-
if (!LangOpts.EnableIdAsAny) {
4025-
// If the type is generic, check whether its generic arguments are also
4026-
// bridged to Objective-C.
4027-
if (auto bgt = type->getAs<BoundGenericType>()) {
4028-
for (auto arg : bgt->getGenericArgs()) {
4029-
if (arg->hasTypeVariable())
4030-
continue;
4031-
4032-
if (!getBridgedToObjC(dc, arg, resolver))
4033-
return None;
4034-
}
4035-
}
4036-
} else {
4037-
// Under id-as-any, anything is bridged to objective c.
4038-
knownBridgedToObjC = true;
4039-
}
4040-
40414009
if (auto metaTy = type->getAs<MetatypeType>())
40424010
if (metaTy->getInstanceType()->mayHaveSuperclass())
40434011
return type;
@@ -4065,7 +4033,8 @@ ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
40654033
auto proto = getProtocol(known);
40664034
if (!proto) return None;
40674035

4068-
return dc->getParentModule()->lookupConformance(type, proto, resolver);
4036+
return dc->getParentModule()->lookupConformance(type, proto,
4037+
getLazyResolver());
40694038
};
40704039

40714040
// Do we conform to _ObjectiveCBridgeable?
@@ -4079,7 +4048,7 @@ ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
40794048
if (conformance->isConcrete()) {
40804049
return ProtocolConformance::getTypeWitnessByName(
40814050
type, conformance->getConcrete(), Id_ObjectiveCType,
4082-
resolver);
4051+
getLazyResolver());
40834052
} else {
40844053
return type->castTo<ArchetypeType>()->getNestedType(Id_ObjectiveCType)
40854054
.getValue();
@@ -4097,12 +4066,8 @@ ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
40974066
return nsErrorDecl->getDeclaredInterfaceType();
40984067
}
40994068

4100-
4101-
// If we haven't imported Foundation but this is a whitelisted type,
4102-
// behave as above.
4103-
if (knownBridgedToObjC)
4104-
return Type();
4105-
return None;
4069+
// No special bridging to Objective-C, but this can become an 'Any'.
4070+
return Type();
41064071
}
41074072

41084073
std::pair<ArchetypeBuilder *, ArchetypeBuilder::PotentialArchetype *>

lib/AST/Decl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,10 +3970,7 @@ ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind(
39703970
if (Type objectTy = indexTy->getAnyOptionalObjectType())
39713971
indexTy = objectTy;
39723972

3973-
if (getASTContext().getBridgedToObjC(getDeclContext(), indexTy, resolver))
3974-
return ObjCSubscriptKind::Keyed;
3975-
3976-
return ObjCSubscriptKind::None;
3973+
return ObjCSubscriptKind::Keyed;
39773974
}
39783975

39793976
SourceRange SubscriptDecl::getSourceRange() const {

lib/AST/Type.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,12 +2067,10 @@ getObjCObjectRepresentable(Type type, const DeclContext *dc) {
20672067
return ForeignRepresentableKind::Object;
20682068

20692069
// Any can be bridged to id.
2070-
if (type->getASTContext().LangOpts.EnableIdAsAny) {
2071-
if (type->isAny()) {
2072-
return ForeignRepresentableKind::Bridged;
2073-
}
2070+
if (type->isAny()) {
2071+
return ForeignRepresentableKind::Bridged;
20742072
}
2075-
2073+
20762074
// Class-constrained generic parameters, from ObjC generic classes.
20772075
if (auto tyContext = dc->getInnermostTypeContext())
20782076
if (auto clas = tyContext->getAsClassOrClassExtensionContext())

lib/ClangImporter/ImportDecl.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,19 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
10371037
return addErrorDomain(swiftDecl, clangNamedDecl, importer);
10381038
}
10391039

1040+
/// Determine whether this is the declaration of Objective-C's 'id' type.
1041+
static bool isObjCId(ASTContext &ctx, const clang::Decl *decl) {
1042+
if (!ctx.LangOpts.EnableObjCInterop) return false;
1043+
1044+
auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(decl);
1045+
if (!typedefDecl) return false;
1046+
1047+
if (!typedefDecl->getDeclContext()->getRedeclContext()->isTranslationUnit())
1048+
return false;
1049+
1050+
return typedefDecl->getName() == "id";
1051+
}
1052+
10401053
namespace {
10411054
/// \brief Convert Clang declarations into the corresponding Swift
10421055
/// declarations.
@@ -1537,36 +1550,6 @@ namespace {
15371550

15381551
Type SwiftType;
15391552
if (Decl->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
1540-
// Ignore the 'id' typedef. We want to bridge the underlying
1541-
// ObjCId type.
1542-
//
1543-
// When we remove the EnableIdAsAny staging flag, the 'id' entry
1544-
// should be removed from MappedTypes.def, and this conditional should
1545-
// become unnecessary.
1546-
if (Name.str() == "id" && Impl.SwiftContext.LangOpts.EnableIdAsAny) {
1547-
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
1548-
MappedTypeNameKind::DoNothing;
1549-
1550-
auto DC = Impl.importDeclContextOf(Decl, importedName.EffectiveContext);
1551-
if (!DC) return nullptr;
1552-
1553-
auto loc = Impl.importSourceLoc(Decl->getLocStart());
1554-
1555-
// Leave behind a typealias for 'id' marked unavailable, to instruct
1556-
// people to use Any instead.
1557-
auto unavailableAlias = Impl.createDeclWithClangNode<TypeAliasDecl>(
1558-
Decl, Accessibility::Public, loc, Name, loc,
1559-
TypeLoc::withoutLoc(Impl.SwiftContext.TheAnyType),
1560-
/*genericparams*/nullptr, DC);
1561-
1562-
auto attr = AvailableAttr::createUnconditional(
1563-
Impl.SwiftContext, "'id' is not available in Swift; use 'Any'",
1564-
"", UnconditionalAvailabilityKind::UnavailableInSwift);
1565-
1566-
unavailableAlias->getAttrs().add(attr);
1567-
return nullptr;
1568-
}
1569-
15701553
bool IsError;
15711554
StringRef StdlibTypeName;
15721555
MappedTypeNameKind NameMapping;
@@ -1719,6 +1702,17 @@ namespace {
17191702
TypeLoc::withoutLoc(SwiftType),
17201703
/*genericparams*/nullptr, DC);
17211704
Result->computeType();
1705+
1706+
// Make Objective-C's 'id' unavailable.
1707+
ASTContext &ctx = DC->getASTContext();
1708+
if (isObjCId(ctx, Decl)) {
1709+
auto attr = AvailableAttr::createUnconditional(
1710+
ctx,
1711+
"'id' is not available in Swift; use 'Any'", "",
1712+
UnconditionalAvailabilityKind::UnavailableInSwift);
1713+
Result->getAttrs().add(attr);
1714+
}
1715+
17221716
return Result;
17231717
}
17241718

@@ -6508,6 +6502,9 @@ void ClangImporter::Implementation::importAttributes(
65086502

65096503
bool ClangImporter::Implementation::isUnavailableInSwift(
65106504
const clang::Decl *decl) {
6505+
// 'id' is always unavailable in Swift.
6506+
if (isObjCId(SwiftContext, decl)) return true;
6507+
65116508
// FIXME: Somewhat duplicated from importAttributes(), but this is a
65126509
// more direct path.
65136510
if (decl->getAvailability() == clang::AR_Unavailable) return true;

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -985,11 +985,9 @@ namespace {
985985

986986
// id maps to Any in bridgeable contexts, AnyObject otherwise.
987987
if (type->isObjCIdType()) {
988-
if (Impl.SwiftContext.LangOpts.EnableIdAsAny)
989-
return {proto->getDeclaredType(),
990-
ImportHint(ImportHint::ObjCBridged,
991-
Impl.SwiftContext.TheAnyType)};
992-
return {proto->getDeclaredType(), ImportHint::ObjCPointer};
988+
return {proto->getDeclaredType(),
989+
ImportHint(ImportHint::ObjCBridged,
990+
Impl.SwiftContext.TheAnyType)};
993991
}
994992

995993
// Class maps to AnyObject.Type.

lib/ClangImporter/MappedTypes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ MAP_TYPE("__swift_shims_dispatch_data_t", ObjCId, 0, "Dispatch", "dispatch_data_
144144
// Objective-C types.
145145
MAP_TYPE("BOOL", ObjCBool, 8, "ObjectiveC", "ObjCBool", false, DoNothing)
146146
MAP_TYPE("SEL", ObjCSel, 0, "ObjectiveC", "Selector", false, DoNothing)
147-
MAP_STDLIB_TYPE("id", ObjCId, 0, "AnyObject", false, DoNothing)
148147
MAP_STDLIB_TYPE("Class", ObjCClass, 0, "AnyClass", false, DoNothing)
149148
MAP_STDLIB_TYPE(
150149
Impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSInteger),

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
775775
Opts.InferImportAsMember |= Args.hasArg(OPT_enable_infer_import_as_member);
776776

777777
Opts.EnableThrowWithoutTry |= Args.hasArg(OPT_enable_throw_without_try);
778-
Opts.EnableIdAsAny |= Args.hasArg(OPT_enable_id_as_any);
779778

780779
if (auto A = Args.getLastArg(OPT_enable_objc_attr_requires_foundation_module,
781780
OPT_disable_objc_attr_requires_foundation_module)) {

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
11931193
SD != ctx.getDictionaryDecl() &&
11941194
SD != ctx.getSetDecl() &&
11951195
!isSwiftNewtype(SD)) {
1196-
ty = *ctx.getBridgedToObjC(&M, ty, /*resolver*/nullptr);
1196+
ty = ctx.getBridgedToObjC(&M, ty);
11971197
}
11981198

11991199
assert(ty && "unknown bridged type");

lib/SIL/Bridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
171171
}
172172

173173
// `Any` can bridge to `AnyObject` (`id` in ObjC).
174-
if (Context.LangOpts.EnableIdAsAny && t->isAny()) {
174+
if (t->isAny()) {
175175
return Context.getProtocol(KnownProtocolKind::AnyObject)->getDeclaredType();
176176
}
177177

lib/SIL/DynamicCasts.cpp

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,8 @@ static unsigned getAnyMetatypeDepth(CanType type) {
3030

3131
static bool
3232
mayBridgeToObjectiveC(Module *M, CanType T) {
33-
// If the target type is either an unknown dynamic type, or statically
34-
// known to bridge, the cast may succeed.
35-
// TODO: We could be more precise with the bridged-to type.
36-
if (T->hasArchetype())
37-
return true;
38-
39-
if (T->isAnyExistentialType())
40-
return true;
41-
42-
if (M->getASTContext().getBridgedToObjC(M, T, nullptr))
43-
return true;
44-
45-
return false;
33+
// FIXME: Disable when we don't support Objective-C interoperability?
34+
return true;
4635
}
4736

4837
static bool
@@ -61,14 +50,7 @@ mustBridgeToSwiftValueBox(Module *M, CanType T) {
6150
if (M->getASTContext().isStandardLibraryTypeBridgedInFoundation(N))
6251
return false;
6352

64-
auto bridgeTy = M->getASTContext().getBridgedToObjC(M, T, nullptr);
65-
if (!bridgeTy.hasValue())
66-
return false;
67-
68-
if (bridgeTy->isNull())
69-
return true;
70-
71-
return false;
53+
return !M->getASTContext().getBridgedToObjC(M, T);
7254
}
7355

7456
static bool canClassOrSuperclassesHaveExtensions(ClassDecl *CD,
@@ -532,13 +514,11 @@ swift::classifyDynamicCast(Module *M,
532514
if (source->isBridgeableObjectType() && mayBridgeToObjectiveC(M, target)) {
533515
// Try to get the ObjC type which is bridged to target type.
534516
assert(!target.isAnyExistentialType());
535-
Optional<Type> ObjCTy = M->getASTContext().getBridgedToObjC(
536-
M, target, nullptr);
537-
if (ObjCTy && ObjCTy.getValue()) {
517+
if (Type ObjCTy = M->getASTContext().getBridgedToObjC(M, target)) {
538518
// If the bridged ObjC type is known, check if
539519
// source type can be cast into it.
540520
return classifyDynamicCast(M, source,
541-
ObjCTy.getValue().getCanonicalTypeOrNull(),
521+
ObjCTy.getCanonicalTypeOrNull(),
542522
/* isSourceTypeExact */ false, isWholeModuleOpts);
543523
}
544524
return DynamicCastFeasibility::MaySucceed;
@@ -547,13 +527,11 @@ swift::classifyDynamicCast(Module *M,
547527
if (target->isBridgeableObjectType() && mayBridgeToObjectiveC(M, source)) {
548528
// Try to get the ObjC type which is bridged to source type.
549529
assert(!source.isAnyExistentialType());
550-
Optional<Type> ObjCTy = M->getASTContext().getBridgedToObjC(
551-
M, source, nullptr);
552-
if (ObjCTy && ObjCTy.getValue()) {
530+
if (Type ObjCTy = M->getASTContext().getBridgedToObjC(M, source)) {
553531
// If the bridged ObjC type is known, check if
554532
// this type can be cast into target type.
555533
return classifyDynamicCast(M,
556-
ObjCTy.getValue().getCanonicalTypeOrNull(),
534+
ObjCTy.getCanonicalTypeOrNull(),
557535
target,
558536
/* isSourceTypeExact */ false, isWholeModuleOpts);
559537
}

lib/SILGen/SILGenBridging.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,9 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &gen,
451451

452452
// Fall back to dynamic Any-to-id bridging.
453453
// The destination type should be AnyObject in this case.
454-
//
455-
// TODO: Should only get here if -enable-id-as-any is active.
456-
assert(gen.getASTContext().LangOpts.EnableIdAsAny
457-
&& loweredBridgedTy->isEqual(
458-
gen.getASTContext().getProtocol(KnownProtocolKind::AnyObject)
459-
->getDeclaredType()));
454+
assert(loweredBridgedTy->isEqual(
455+
gen.getASTContext().getProtocol(KnownProtocolKind::AnyObject)
456+
->getDeclaredType()));
460457

461458
// If the input argument is known to be an existential, save the runtime
462459
// some work by opening it.

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,13 +1038,6 @@ static void emitMatchingRCAdjustmentsForCall(ApplyInst *Call, SILValue OnX) {
10381038
Builder.createReleaseValue(Call->getLoc(), OnX, Atomicity::Atomic);
10391039
}
10401040

1041-
static bool isCastTypeKnownToSucceed(SILType Type, SILModule &Mod) {
1042-
auto *M = Mod.getSwiftModule();
1043-
return M->getASTContext()
1044-
.getBridgedToObjC(M, Type.getSwiftRValueType(), nullptr)
1045-
.hasValue();
1046-
}
1047-
10481041
/// Replace an application of a cast composition f_inverse(f(x)) by x.
10491042
bool SILCombiner::optimizeIdentityCastComposition(ApplyInst *FInverse,
10501043
StringRef FInverseName,
@@ -1057,12 +1050,6 @@ bool SILCombiner::optimizeIdentityCastComposition(ApplyInst *FInverse,
10571050
if (!knowHowToEmitReferenceCountInsts(FInverse))
10581051
return false;
10591052

1060-
// We need to know that the cast will succeed.
1061-
if (!isCastTypeKnownToSucceed(FInverse->getArgument(0)->getType(),
1062-
FInverse->getModule()) ||
1063-
!isCastTypeKnownToSucceed(FInverse->getType(), FInverse->getModule()))
1064-
return false;
1065-
10661053
// Need to have a matching 'f'.
10671054
auto *F = dyn_cast<ApplyInst>(FInverse->getArgument(0));
10681055
if (!F)

0 commit comments

Comments
 (0)