Skip to content

Commit 455aa05

Browse files
committed
Pending QOI improvements, reinstate implicit bridging conversions.
1 parent 3e20817 commit 455aa05

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,27 +1709,33 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
17091709
// Bridging from a value type to an Objective-C class type.
17101710
// FIXME: Banned for operator parameters, like user conversions are.
17111711

1712-
if (kind == TypeMatchKind::ExplicitConversion) {
17131712

1714-
if (type1->isPotentiallyBridgedValueType() &&
1715-
type1->getAnyNominal()
1716-
!= TC.Context.getImplicitlyUnwrappedOptionalDecl() &&
1717-
!(flags & TMF_ApplyingOperatorParameter)) {
1718-
1719-
auto isBridgeableTargetType = type2->isBridgeableObjectType();
1720-
1721-
// Allow bridged conversions to CVarArg through NSObject.
1722-
if (!isBridgeableTargetType && type2->isExistentialType()) {
1723-
if (auto nominalType = type2->getAs<NominalType>())
1724-
isBridgeableTargetType = nominalType->getDecl()->getName() ==
1725-
TC.Context.Id_CVarArg;
1726-
}
1727-
1728-
if (isBridgeableTargetType && TC.getBridgedToObjC(DC, type1)) {
1729-
conversionsOrFixes.push_back(ConversionRestrictionKind::BridgeToObjC);
1730-
}
1713+
// NOTE: The plan for <rdar://problem/18311362> was to make such bridging
1714+
// conversions illegal except when explicitly converting with the 'as'
1715+
// operator. But using a String to subscript an [NSObject : AnyObject] is
1716+
// sufficiently common due to bridging that disallowing such conversions is
1717+
// not yet feasible, and a more targeted fix in the type checker is hard to
1718+
// justify.
1719+
if (type1->isPotentiallyBridgedValueType() &&
1720+
type1->getAnyNominal()
1721+
!= TC.Context.getImplicitlyUnwrappedOptionalDecl() &&
1722+
!(flags & TMF_ApplyingOperatorParameter)) {
1723+
1724+
auto isBridgeableTargetType = type2->isBridgeableObjectType();
1725+
1726+
// Allow bridged conversions to CVarArg through NSObject.
1727+
if (!isBridgeableTargetType && type2->isExistentialType()) {
1728+
if (auto nominalType = type2->getAs<NominalType>())
1729+
isBridgeableTargetType = nominalType->getDecl()->getName() ==
1730+
TC.Context.Id_CVarArg;
1731+
}
1732+
1733+
if (isBridgeableTargetType && TC.getBridgedToObjC(DC, type1)) {
1734+
conversionsOrFixes.push_back(ConversionRestrictionKind::BridgeToObjC);
17311735
}
1736+
}
17321737

1738+
if (kind == TypeMatchKind::ExplicitConversion) {
17331739
// Bridging from an Objective-C class type to a value type.
17341740
// Note that specifically require a class or class-constrained archetype
17351741
// here, because archetypes cannot be bridged.

test/Constraints/bridging.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ func ==(x: OtherClass, y: OtherClass) -> Bool { return true }
8989

9090
// Basic bridging
9191
func bridgeToObjC(_ s: BridgedStruct) -> BridgedClass {
92-
return s // expected-error{{cannot convert return expression of type 'BridgedStruct' to return type 'BridgedClass'}}
92+
return s
9393
return s as BridgedClass
9494
}
9595

9696
func bridgeToAnyObject(_ s: BridgedStruct) -> AnyObject {
97-
return s // expected-error{{return expression of type 'BridgedStruct' does not conform to 'AnyObject'}}
97+
return s
9898
return s as AnyObject
9999
}
100100

@@ -112,10 +112,10 @@ func bridgeFromObjCDerived(_ s: BridgedClassSub) -> BridgedStruct {
112112
func arrayToNSArray() {
113113
var nsa: NSArray
114114

115-
nsa = [AnyObject]() // expected-error {{cannot assign value of type '[AnyObject]' to type 'NSArray'}}
116-
nsa = [BridgedClass]() // expected-error {{cannot assign value of type '[BridgedClass]' to type 'NSArray'}}
117-
nsa = [OtherClass]() // expected-error {{cannot assign value of type '[OtherClass]' to type 'NSArray'}}
118-
nsa = [BridgedStruct]() // expected-error {{cannot assign value of type '[BridgedStruct]' to type 'NSArray'}}
115+
nsa = [AnyObject]()
116+
nsa = [BridgedClass]()
117+
nsa = [OtherClass]()
118+
nsa = [BridgedStruct]()
119119
nsa = [NotBridgedStruct]() // expected-error{{cannot assign value of type '[NotBridgedStruct]' to type 'NSArray'}}
120120

121121
nsa = [AnyObject]() as NSArray
@@ -150,13 +150,13 @@ func dictionaryToNSDictionary() {
150150

151151
var nsd: NSDictionary
152152

153-
nsd = [NSObject : AnyObject]() // expected-error {{cannot assign value of type '[NSObject : AnyObject]' to type 'NSDictionary'}}
153+
nsd = [NSObject : AnyObject]()
154154
nsd = [NSObject : AnyObject]() as NSDictionary
155-
nsd = [NSObject : BridgedClass]() // expected-error {{cannot assign value of type '[NSObject : BridgedClass]' to type 'NSDictionary'}}
155+
nsd = [NSObject : BridgedClass]()
156156
nsd = [NSObject : BridgedClass]() as NSDictionary
157-
nsd = [NSObject : OtherClass]() // expected-error {{cannot assign value of type '[NSObject : OtherClass]' to type 'NSDictionary'}}
157+
nsd = [NSObject : OtherClass]()
158158
nsd = [NSObject : OtherClass]() as NSDictionary
159-
nsd = [NSObject : BridgedStruct]() // expected-error {{cannot assign value of type '[NSObject : BridgedStruct]' to type 'NSDictionary'}}
159+
nsd = [NSObject : BridgedStruct]()
160160
nsd = [NSObject : BridgedStruct]() as NSDictionary
161161
nsd = [NSObject : NotBridgedStruct]() // expected-error{{cannot assign value of type '[NSObject : NotBridgedStruct]' to type 'NSDictionary'}}
162162
nsd = [NSObject : NotBridgedStruct]() as NSDictionary // expected-error{{cannot convert value of type '[NSObject : NotBridgedStruct]' to type 'NSDictionary' in coercion}}
@@ -166,18 +166,18 @@ func dictionaryToNSDictionary() {
166166
nsd = [NSObject : BridgedStruct?]() // expected-error{{cannot assign value of type '[NSObject : BridgedStruct?]' to type 'NSDictionary'}}
167167
nsd = [NSObject : BridgedStruct?]() as NSDictionary //expected-error{{cannot convert value of type '[NSObject : BridgedStruct?]' to type 'NSDictionary' in coercion}}
168168

169-
nsd = [BridgedClass : AnyObject]() // expected-error {{cannot assign value of type '[BridgedClass : AnyObject]' to type 'NSDictionary'}}
169+
nsd = [BridgedClass : AnyObject]()
170170
nsd = [BridgedClass : AnyObject]() as NSDictionary
171-
nsd = [OtherClass : AnyObject]() // expected-error {{cannot assign value of type '[OtherClass : AnyObject]' to type 'NSDictionary'}}
171+
nsd = [OtherClass : AnyObject]()
172172
nsd = [OtherClass : AnyObject]() as NSDictionary
173-
nsd = [BridgedStruct : AnyObject]() // expected-error {{cannot assign value of type '[BridgedStruct : AnyObject]' to type 'NSDictionary'}}
173+
nsd = [BridgedStruct : AnyObject]()
174174
nsd = [BridgedStruct : AnyObject]() as NSDictionary
175175
nsd = [NotBridgedStruct : AnyObject]() // expected-error{{cannot assign value of type '[NotBridgedStruct : AnyObject]' to type 'NSDictionary'}}
176176
nsd = [NotBridgedStruct : AnyObject]() as NSDictionary // expected-error{{cannot convert value of type '[NotBridgedStruct : AnyObject]' to type 'NSDictionary' in coercion}}
177177

178178
// <rdar://problem/17134986>
179179
var bcOpt: BridgedClass?
180-
nsd = [BridgedStruct() : bcOpt] // expected-error{{value of type 'BridgedStruct' does not conform to expected dictionary key type 'NSCopying'}}
180+
nsd = [BridgedStruct() : bcOpt] // expected-error{{value of optional type 'BridgedClass?' not unwrapped; did you mean to use '!' or '?'?}}
181181
bcOpt = nil
182182
_ = nsd
183183
}
@@ -233,7 +233,7 @@ func rdar19695671() {
233233
// This failed at one point while fixing rdar://problem/19600325.
234234
func getArrayOfAnyObject(_: AnyObject) -> [AnyObject] { return [] }
235235
func testCallback(_ f: (AnyObject) -> AnyObject?) {}
236-
testCallback { return getArrayOfAnyObject($0) } // expected-error {{cannot convert value of type '[AnyObject]' to closure result type 'AnyObject?'}}
236+
testCallback { return getArrayOfAnyObject($0) }
237237

238238
// <rdar://problem/19724719> Type checker thinks "(optionalNSString ?? nonoptionalNSString) as String" is a forced cast
239239
func rdar19724719(_ f: (String) -> (), s1: NSString?, s2: NSString) {
@@ -292,10 +292,10 @@ func rdar19836341(_ ns: NSString?, vns: NSString?) {
292292

293293
// <rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!"
294294
func rdar20029786(_ ns: NSString?) {
295-
var s: String = ns ?? "str" as String as String // expected-error{{cannot convert value of type 'NSString?' to expected argument type 'String?'}}
296-
var s2 = ns ?? "str" as String as String // expected-error {{binary operator '??' cannot be applied to operands of type 'NSString?' and 'String'}} expected-note{{}}
295+
var s: String = ns ?? "str" as String as String // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}}
296+
var s2 = ns ?? "str" as String as String
297297

298-
let s3: NSString? = "str" as String? // expected-error {{cannot convert value of type 'String?' to specified type 'NSString?'}}
298+
let s3: NSString? = "str" as String?
299299

300300
var s4: String = ns ?? "str" // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}}{{20-20=(}}{{31-31=) as String}}
301301
var s5: String = (ns ?? "str") as String // fixed version

0 commit comments

Comments
 (0)