Skip to content

Revert "SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel." #4820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2016
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
27 changes: 9 additions & 18 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3879,22 +3879,18 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
known->second.getGeneration() < CurrentGeneration)) {
Optional<ForeignRepresentationInfo> result;

// Look for a conformance to _ObjectiveCBridgeable (other than Optional's--
// we don't want to allow exposing APIs with double-optional types like
// NSObject??, even though Optional is bridged to its underlying type).
// Look for a conformance to _ObjectiveCBridgeable.
//
// FIXME: We're implicitly depending on the fact that lookupConformance
// is global, ignoring the module we provide for it.
if (nominal != dc->getASTContext().getOptionalDecl()) {
if (auto objcBridgeable
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
if (auto conformance
= dc->getParentModule()->lookupConformance(
nominal->getDeclaredType(), objcBridgeable,
getLazyResolver())) {
result =
ForeignRepresentationInfo::forBridged(conformance->getConcrete());
}
if (auto objcBridgeable
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
if (auto conformance
= dc->getParentModule()->lookupConformance(
nominal->getDeclaredType(), objcBridgeable,
getLazyResolver())) {
result =
ForeignRepresentationInfo::forBridged(conformance->getConcrete());
}
}

Expand Down Expand Up @@ -3990,11 +3986,6 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
// Try to find a conformance that will enable bridging.
auto findConformance =
[&](KnownProtocolKind known) -> Optional<ProtocolConformanceRef> {
// Don't ascribe any behavior to Optional other than what we explicitly
// give it. We don't want things like AnyObject?? to work.
if (type->getAnyNominal() == getOptionalDecl())
return None;

// Find the protocol.
auto proto = getProtocol(known);
if (!proto) return None;
Expand Down
1 change: 0 additions & 1 deletion lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ static ValueDecl *getIsOptionalOperation(ASTContext &Context, Identifier Id) {
return builder.build(Id);
}


static ValueDecl *getAllocOperation(ASTContext &Context, Identifier Id) {
Type PtrSizeTy = BuiltinIntegerType::getWordType(Context);
Type ResultTy = Context.TheRawPointerType;
Expand Down
9 changes: 0 additions & 9 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ emitBridgeNativeToObjectiveC(SILGenFunction &gen,
witnessFnTy = witnessFnTy.substGenericArgs(gen.SGM.M, substitutions);
}

// The witness may be more abstract than the concrete value we're bridging,
// for instance, if the value is a concrete instantiation of a generic type.
if (witnessFnTy.castTo<SILFunctionType>()->getParameters()[0].isIndirect()
&& !swiftValue.getType().isAddress()) {
auto tmp = gen.emitTemporaryAllocation(loc, swiftValue.getType());
gen.B.createStore(loc, swiftValue.getValue(), tmp);
swiftValue = ManagedValue::forUnmanaged(tmp);
}

// Call the witness.
SILType resultTy = gen.getLoweredType(objcType);
SILValue bridgedValue = gen.B.createApply(loc, witnessRef, witnessFnTy,
Expand Down
7 changes: 1 addition & 6 deletions lib/SILOptimizer/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,11 +1662,6 @@ optimizeBridgedSwiftToObjCCast(SILInstruction *Inst,

auto SILFnTy = SILType::getPrimitiveObjectType(
M.Types.getConstantFunctionType(BridgeFuncDeclRef));

// TODO: Handle indirect argument to or return from witness function.
if (ParamTypes[0].isIndirect()
|| BridgedFunc->getLoweredFunctionType()->getSingleResult().isIndirect())
return nullptr;

// Get substitutions, if source is a bound generic type.
ArrayRef<Substitution> Subs =
Expand Down Expand Up @@ -1733,9 +1728,9 @@ optimizeBridgedSwiftToObjCCast(SILInstruction *Inst,
case ParameterConvention::Direct_Unowned:
break;
case ParameterConvention::Indirect_In:
case ParameterConvention::Indirect_In_Guaranteed:
case ParameterConvention::Indirect_Inout:
case ParameterConvention::Indirect_InoutAliasable:
case ParameterConvention::Indirect_In_Guaranteed:
case ParameterConvention::Direct_Deallocating:
llvm_unreachable("unsupported convention for bridging conversion");
}
Expand Down
75 changes: 0 additions & 75 deletions stdlib/public/core/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,78 +487,3 @@ extension Optional {
}

}

//===----------------------------------------------------------------------===//
// Bridging
//===----------------------------------------------------------------------===//

#if _runtime(_ObjC)
extension Optional : _ObjectiveCBridgeable {
// The object that represents `none` for an Optional of this type.
internal static var _nilSentinel : AnyObject {
@_silgen_name("_swift_Foundation_getOptionalNilSentinelObject")
get
}

public func _bridgeToObjectiveC() -> AnyObject {
// Bridge a wrapped value by unwrapping.
if let value = self {
return _bridgeAnythingToObjectiveC(value)
}
// Bridge nil using a sentinel.
return type(of: self)._nilSentinel
}

public static func _forceBridgeFromObjectiveC(_ source: AnyObject,
result: inout Optional<Wrapped>?) {
// Map the nil sentinel back to .none.
// NB that the signature of _forceBridgeFromObjectiveC adds another level
// of optionality, so we need to wrap the immediate result of the conversion
// in `.some`.
if source === _nilSentinel {
result = .some(.none)
return
}
// Otherwise, force-bridge the underlying value.
let unwrappedResult = source as! Wrapped
result = .some(.some(unwrappedResult))
}

public static func _conditionallyBridgeFromObjectiveC(_ source: AnyObject,
result: inout Optional<Wrapped>?)
-> Bool {
// Map the nil sentinel back to .none.
// NB that the signature of _forceBridgeFromObjectiveC adds another level
// of optionality, so we need to wrap the immediate result of the conversion
// in `.some` to indicate success of the bridging operation, with a nil
// result.
if source === _nilSentinel {
result = .some(.none)
return true
}
// Otherwise, try to bridge the underlying value.
if let unwrappedResult = source as? Wrapped {
result = .some(.some(unwrappedResult))
return true
} else {
result = .none
return false
}
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: AnyObject?)
-> Optional<Wrapped> {
if let nonnullSource = source {
// Map the nil sentinel back to none.
if nonnullSource === _nilSentinel {
return .none
} else {
return .some(nonnullSource as! Wrapped)
}
} else {
// If we unexpectedly got nil, just map it to `none` too.
return .none
}
}
}
#endif
12 changes: 0 additions & 12 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,18 +2513,6 @@ bool swift::swift_dynamicCast(OpaqueValue *dest,
// unwrapping the target. This handles an optional source wrapped within an
// existential that Optional conforms to (Any).
if (auto srcExistentialType = dyn_cast<ExistentialTypeMetadata>(srcType)) {
#if SWIFT_OBJC_INTEROP
// If coming from AnyObject, we may want to bridge.
if (srcExistentialType->Flags.getSpecialProtocol()
== SpecialProtocol::AnyObject) {
if (auto targetBridgeWitness = findBridgeWitness(targetType)) {
return _dynamicCastClassToValueViaObjCBridgeable(dest, src, srcType,
targetType,
targetBridgeWitness,
flags);
}
}
#endif
return _dynamicCastFromExistential(dest, src, srcExistentialType,
targetType, flags);
}
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
Availability.mm
DispatchShims.mm
FoundationHelpers.mm
OptionalBridgingHelper.mm
Reflection.mm
SwiftNativeNSXXXBase.mm.gyb)
set(LLVM_OPTIONAL_SOURCES
Expand Down
100 changes: 0 additions & 100 deletions stdlib/public/stubs/OptionalBridgingHelper.mm

This file was deleted.

14 changes: 6 additions & 8 deletions test/SILGen/objc_bridging_any.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,27 @@ func passingToId<T: CP, U>(receiver: NSIdLover,
// CHECK: apply [[METHOD]]([[ANYOBJECT]], [[SELF]])
receiver.takesId(knownUnbridged)

// These cases bridge using Optional's _ObjectiveCBridgeable conformance.

// CHECK: [[METHOD:%.*]] = class_method [volatile] [[SELF]] : $NSIdLover,
// CHECK: [[BRIDGE_OPTIONAL:%.*]] = function_ref @_TFSq19_bridgeToObjectiveCfT_Ps9AnyObject_
// CHECK: [[TMP:%.*]] = alloc_stack $Optional<String>
// CHECK: store [[OPT_STRING]] to [[TMP]]
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_OPTIONAL]]<String>([[TMP]])
// CHECK: [[BRIDGE_ANYTHING:%.*]] = function_ref @_TFs27_bridgeAnythingToObjectiveC
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_ANYTHING]]<Optional<String>>([[TMP]])
// CHECK: apply [[METHOD]]([[ANYOBJECT]], [[SELF]])
receiver.takesId(optionalA)

// CHECK: [[METHOD:%.*]] = class_method [volatile] [[SELF]] : $NSIdLover,
// CHECK: [[BRIDGE_OPTIONAL:%.*]] = function_ref @_TFSq19_bridgeToObjectiveCfT_Ps9AnyObject_
// CHECK: [[TMP:%.*]] = alloc_stack $Optional<NSString>
// CHECK: store [[OPT_NSSTRING]] to [[TMP]]
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_OPTIONAL]]<NSString>([[TMP]])
// CHECK: [[BRIDGE_ANYTHING:%.*]] = function_ref @_TFs27_bridgeAnythingToObjectiveC
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_ANYTHING]]<Optional<NSString>>([[TMP]])
// CHECK: apply [[METHOD]]([[ANYOBJECT]], [[SELF]])
receiver.takesId(optionalB)

// CHECK: [[METHOD:%.*]] = class_method [volatile] [[SELF]] : $NSIdLover,
// CHECK: [[TMP:%.*]] = alloc_stack $Optional<Any>
// CHECK: copy_addr [[OPT_ANY]] to [initialization] [[TMP]]
// CHECK: [[BRIDGE_OPTIONAL:%.*]] = function_ref @_TFSq19_bridgeToObjectiveCfT_Ps9AnyObject_
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_OPTIONAL]]<Any>([[TMP]])
// CHECK: [[BRIDGE_ANYTHING:%.*]] = function_ref @_TFs27_bridgeAnythingToObjectiveC
// CHECK: [[ANYOBJECT:%.*]] = apply [[BRIDGE_ANYTHING]]<Optional<Any>>([[TMP]])
// CHECK: apply [[METHOD]]([[ANYOBJECT]], [[SELF]])
receiver.takesId(optionalC)

Expand Down
Loading