Skip to content

Commit 11f03cd

Browse files
committed
Sema: Don't try bridged classes as default literal types.
One last bit of SE-0072. We shouldn't fall back to bridged classes in the absence of type context for literals anymore. By itself, this kind of hoses the use of literals with NS types, but I think we can get most of the QoI back with overlay changes I plan to propose following this.
1 parent 2c85b5d commit 11f03cd

27 files changed

+83
-125
lines changed

lib/Sema/ConstraintSystem.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,6 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
256256

257257
SmallVector<Type, 4> types;
258258

259-
// If the default literal type is bridged to a class type, add the class type.
260-
if (auto proto = TC.Context.getProtocol(kind)) {
261-
if (auto defaultType = TC.getDefaultType(proto, DC)) {
262-
if (auto bridgedClassType = TC.getBridgedToObjC(DC, defaultType)) {
263-
types.push_back(bridgedClassType);
264-
}
265-
}
266-
}
267-
268259
// Some literal kinds are related.
269260
switch (kind) {
270261
#define PROTOCOL_WITH_NAME(Id, Name) \

stdlib/public/SDK/XCTest/XCTest.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ func _XCTRunThrowableBlock(_ block: @noescape () throws -> Void) -> _XCTThrowabl
7070
if let blockError = blockErrorOptional {
7171
return .failedWithError(error: blockError)
7272
} else if d.count > 0 {
73-
let t: String = d["type"] as! String
73+
let t: String = d["type" as NSString] as! String
7474

7575
if t == "objc" {
7676
return .failedWithException(
77-
className: d["className"] as! String,
78-
name: d["name"] as! String,
79-
reason: d["reason"] as! String)
77+
className: d["className" as NSString] as! String,
78+
name: d["name" as NSString] as! String,
79+
reason: d["reason" as NSString] as! String)
8080
} else {
8181
return .failedWithUnknownException
8282
}

test/1_stdlib/ArrayTrapsObjC.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ArrayTraps.test("downcast2")
3737
{ _isFastAssertConfiguration() },
3838
reason: "this trap is not guaranteed to happen in -Ounchecked"))
3939
.code {
40-
let a: [AnyObject] = ["String", 1]
40+
let a: [AnyObject] = ["String" as NSString, 1 as NSNumber]
4141
let sa = a as! [NSString]
4242
let s0 = sa[0]
4343
expectCrashLater()

test/1_stdlib/DictionaryLiteral.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ let anNSString = "Foo" as NSString
5151
var stringNSStringLet: DictionaryLiteral = [ "a": aString as NSString, "b": anNSString]
5252
expectType(DictionaryLiteral<String, NSString>.self, &stringNSStringLet)
5353

54-
var hetero1: DictionaryLiteral = ["a": 1, "b": "Foo" as NSString]
54+
var hetero1: DictionaryLiteral = ["a": 1 as NSNumber, "b": "Foo" as NSString]
5555
expectType(DictionaryLiteral<String, NSObject>.self, &hetero1)
5656

57-
var hetero2: DictionaryLiteral = ["a": 1, "b": "Foo"]
57+
var hetero2: DictionaryLiteral = ["a": 1 as NSNumber, "b": "Foo" as NSString]
5858
expectType(DictionaryLiteral<String, NSObject>.self, &hetero2)

test/1_stdlib/NSArrayAPI.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var NSArrayAPI = TestSuite("NSArrayAPI")
1212

1313
NSArrayAPI.test("mixed types with AnyObject") {
1414
do {
15-
let result: AnyObject = [1, "two"]
16-
let expect: NSArray = [1, "two"]
15+
let result: AnyObject = [1 as NSNumber, "two" as NSString] as NSArray
16+
let expect: NSArray = [1 as NSNumber, "two" as NSString]
1717
expectEqual(expect, result as! NSArray)
1818
}
1919
do {
20-
let result: AnyObject = [1, 2]
21-
let expect: NSArray = [1, 2]
20+
let result: AnyObject = [1 as NSNumber, 2 as NSNumber] as NSArray
21+
let expect: NSArray = [1 as NSNumber, 2 as NSNumber]
2222
expectEqual(expect, result as! NSArray)
2323
}
2424
}

test/1_stdlib/NSObject.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ let o1 = NSObject.init()
7171
let o2 = NSObject.init()
7272
printIdentity(o1, o2, "o1", "o2")
7373
printEquality(o1, o2, "o1", "o2")
74-
printIdentity(o1, 10, "o1", "10")
75-
printEquality(o1, 10, "o1", "10")
76-
printIdentity(10, o1, "10", "o1")
77-
printEquality(10, o1, "10", "o1")
74+
printIdentity(o1, 10 as NSNumber, "o1", "10")
75+
printEquality(o1, 10 as NSNumber, "o1", "10")
76+
printIdentity(10 as NSNumber, o1, "10", "o1")
77+
printEquality(10 as NSNumber, o1, "10", "o1")
7878
print("done NSObject ==")
7979
// CHECK: NSObject ==
8080
// CHECK-NEXT: o1 === o1

test/1_stdlib/NSStringAPI.swift

-39
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,6 @@ NSStringAPIs.test("init(format:locale:arguments:)") {
841841
locale: nil, arguments: args))
842842
}
843843

844-
NSStringAPIs.test("lastPathComponent") {
845-
expectEqual("bar", "/foo/bar".lastPathComponent)
846-
expectEqual("абв", "/foo/абв".lastPathComponent)
847-
}
848-
849844
NSStringAPIs.test("utf16Count") {
850845
expectEqual(1, "a".utf16.count)
851846
expectEqual(2, "\u{0001F60A}".utf16.count)
@@ -1035,11 +1030,6 @@ NSStringAPIs.test("pathComponents") {
10351030
expectEqual([ "/", "абв", "где" ] as [NSString], ("/абв/где" as NSString).pathComponents as [NSString])
10361031
}
10371032

1038-
NSStringAPIs.test("pathExtension") {
1039-
expectEqual("", "/foo/bar".pathExtension)
1040-
expectEqual("txt", "/foo/bar.txt".pathExtension)
1041-
}
1042-
10431033
NSStringAPIs.test("precomposedStringWithCanonicalMapping") {
10441034
expectEqual("abc", "abc".precomposedStringWithCanonicalMapping)
10451035
expectEqual("だくてん",
@@ -1359,26 +1349,13 @@ NSStringAPIs.test("appendingFormat(_:_:...)") {
13591349
.appendingFormat("def %@ %ld", formatArg, 42))
13601350
}
13611351

1362-
NSStringAPIs.test("appendingPathComponent(_:)") {
1363-
expectEqual("", "".appendingPathComponent(""))
1364-
expectEqual("a.txt", "".appendingPathComponent("a.txt"))
1365-
expectEqual("/tmp/a.txt", "/tmp".appendingPathComponent("a.txt"))
1366-
}
1367-
13681352
NSStringAPIs.test("appending(_:)") {
13691353
expectEqual("", "".appending(""))
13701354
expectEqual("a", "a".appending(""))
13711355
expectEqual("a", "".appending("a"))
13721356
expectEqual("\u{3099}", "".appending("\u{3099}"))
13731357
}
13741358

1375-
NSStringAPIs.test("deletingLastPathComponent") {
1376-
expectEqual("", "".deletingLastPathComponent)
1377-
expectEqual("/", "/".deletingLastPathComponent)
1378-
expectEqual("/", "/tmp".deletingLastPathComponent)
1379-
expectEqual("/tmp", "/tmp/a.txt".deletingLastPathComponent)
1380-
}
1381-
13821359
NSStringAPIs.test("folding(options:locale:)") {
13831360

13841361
func fwo(
@@ -1597,22 +1574,6 @@ NSStringAPIs.test("replacingPercentEscapes(using:)/rdar18029471")
15971574
using: .ascii))
15981575
}
15991576

1600-
NSStringAPIs.test("resolvingSymlinksInPath") {
1601-
// <rdar://problem/18030188> Difference between
1602-
// resolvingSymlinksInPath and stringByStandardizingPath is unclear
1603-
expectEqual("", "".resolvingSymlinksInPath)
1604-
expectEqual(
1605-
"/var", "/private/var/tmp////..//".resolvingSymlinksInPath)
1606-
}
1607-
1608-
NSStringAPIs.test("standardizingPath") {
1609-
// <rdar://problem/18030188> Difference between
1610-
// resolvingSymlinksInPath and standardizingPath is unclear
1611-
expectEqual("", "".standardizingPath)
1612-
expectEqual(
1613-
"/var", "/private/var/tmp////..//".standardizingPath)
1614-
}
1615-
16161577
NSStringAPIs.test("trimmingCharacters(in:)") {
16171578
expectEqual("", "".trimmingCharacters(
16181579
in: CharacterSet.decimalDigits))

test/1_stdlib/RuntimeObjC.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ Reflection.test("Unmanaged/nil") {
747747
Reflection.test("Unmanaged/not-nil") {
748748
var output = ""
749749
var optionalURL: Unmanaged<CFURL>? =
750-
Unmanaged.passRetained(CFURLCreateWithString(nil, "http://llvm.org/", nil))
750+
Unmanaged.passRetained(CFURLCreateWithString(nil, "http://llvm.org/" as CFString, nil))
751751
dump(optionalURL, to: &output)
752752

753753
let expected =

test/Interpreter/SDK/Foundation_NSExpression.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
// Test overlain variadic methods.
11-
let expression = NSExpression(format: "(3 + 2)**2", "LLLL", "BBBB")
11+
let expression = NSExpression(format: "(3 + 2)**2", "LLLL" as NSString, "BBBB" as NSString)
1212
let result = expression.expressionValue(with: expression, context:nil) as! NSNumber
1313
let number = result.stringValue
1414
print(number)

test/Interpreter/SDK/Foundation_NSLog.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ testNSLog()
1919
// CHECK: 1 is the loneliest number that you'll ever do
2020
NSLog(
2121
"%@ is the loneliest number that you'll ever %@",
22-
NSNumber(value: 1), "do"
22+
NSNumber(value: 1), "do" as NSString
2323
)

test/Interpreter/SDK/Foundation_NSPredicate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import Foundation
88

99
// Test overlain variadic methods.
10-
let s = NSPredicate(format: "(lastName like[cd] %@) AND (birthday > %@)", "LLLL", "BBBB")
10+
let s = NSPredicate(format: "(lastName like[cd] %@) AND (birthday > %@)", "LLLL" as NSString, "BBBB" as NSString)
1111
print(s.predicateFormat)
1212

1313
// CHECK: lastName LIKE[cd] "LLLL" AND birthday > "BBBB"

test/Interpreter/SDK/Foundation_NSString.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,21 @@ testComparisons()
136136
// CHECK-LABEL: Variadic methods:
137137
print("Variadic methods:")
138138
// CHECK-NEXT: x y
139-
print(NSString(format: "%@ %@", "x", "y"))
139+
print(NSString(format: "%@ %@", "x" as NSString, "y" as NSString))
140140
// CHECK-NEXT: 1{{.*}}024,25
141141
print(NSString(
142142
format: "%g",
143143
locale: Locale(identifier: "fr_FR"),
144144
1024.25
145145
))
146146
// CHECK-NEXT: x y z
147-
print(("x " as NSString).appendingFormat("%@ %@", "y", "z"))
147+
print(("x " as NSString).appendingFormat("%@ %@", "y" as NSString, "z" as NSString))
148148
// CHECK-NEXT: a b c
149149
let s = NSMutableString(string: "a ")
150-
s.appendFormat("%@ %@", "b", "c")
150+
s.appendFormat("%@ %@", "b" as NSString, "c" as NSString)
151151
print(s)
152152

153-
let m = NSMutableString.localizedStringWithFormat("<%@ %@>", "q", "r")
153+
let m = NSMutableString.localizedStringWithFormat("<%@ %@>", "q" as NSString, "r" as NSString)
154154
// CHECK-NEXT: <q r>
155155
print(m)
156156
m.append(" lever")

test/Interpreter/SDK/Foundation_test.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ FoundationTestSuite.test("arrayConversions") {
122122
//===----------------------------------------------------------------------===//
123123

124124
FoundationTestSuite.test("NSDictionary") {
125-
var nsDict : NSDictionary = [1 : "Hello", 2 : "World"]
126-
assert((nsDict[1]! as! NSString).isEqual("Hello"))
127-
assert((nsDict[2]! as! NSString).isEqual("World"))
125+
var nsDict : NSDictionary = [1 as NSNumber : "Hello" as NSString, 2 as NSNumber : "World" as NSString]
126+
assert((nsDict[1 as NSNumber]! as! NSString).isEqual("Hello"))
127+
assert((nsDict[2 as NSNumber]! as! NSString).isEqual("World"))
128128

129-
let nsMutableDict: NSMutableDictionary = ["Hello" : 1, "World" : 2]
130-
assert((nsMutableDict["Hello"]! as AnyObject).isEqual(1))
131-
assert((nsMutableDict["World"]! as AnyObject).isEqual(2))
129+
let nsMutableDict: NSMutableDictionary = ["Hello" as NSString : 1 as NSNumber, "World" as NSString : 2 as NSNumber]
130+
assert((nsMutableDict["Hello" as NSString]! as AnyObject).isEqual(1))
131+
assert((nsMutableDict["World" as NSString]! as AnyObject).isEqual(2))
132132
}
133133

134134
//===----------------------------------------------------------------------===//
@@ -189,9 +189,9 @@ class ClassWithDtor : NSObject {
189189
FoundationTestSuite.test("rdar://17584531") {
190190
// <rdar://problem/17584531>
191191
// Type checker used to be confused by this.
192-
var dict: NSDictionary = ["status": 200, "people": [["id": 255, "name": ["first": "John", "last": "Appleseed"]]]]
193-
var dict2 = dict["people"].map { $0 as AnyObject }?[0] as! NSDictionary
194-
expectEqual("Optional(255)", String(describing: dict2["id"]))
192+
var dict: NSDictionary = ["status" as NSString: 200 as NSNumber, "people" as NSString: [["id" as NSString: 255 as NSNumber, "name" as NSString: ["first" as NSString: "John" as NSString, "last" as NSString: "Appleseed" as NSString] as NSDictionary] as NSDictionary] as NSArray] as NSDictionary
193+
var dict2 = dict["people" as NSString].map { $0 as AnyObject }?[0] as! NSDictionary
194+
expectEqual("Optional(255)", String(describing: dict2["id" as NSString]))
195195
}
196196

197197
FoundationTestSuite.test("DarwinBoolean smoke test") {
@@ -384,7 +384,7 @@ if #available(OSX 10.11, iOS 9.0, *) {
384384
}
385385

386386
FoundationTestSuite.test("NotificationCenter/addObserver(_:selector:name:object:)") {
387-
let obj: AnyObject = "Hello"
387+
let obj: AnyObject = "Hello" as NSString
388388
NotificationCenter.default.addObserver(obj, selector: Selector("blah:"),
389389
name: nil, object: nil)
390390
let name = "hello"

test/Interpreter/SDK/dictionary_pattern_matching.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct State {
1111
let abbrev: String
1212
}
1313

14-
func stateFromPlistLame(_ plist: Dictionary<String, AnyObject>) -> State? {
14+
func stateFromPlistLame(_ plist: Dictionary<String, Any>) -> State? {
1515
if let name = plist["name"] as? NSString {
1616
if let population = plist["population"] as? NSNumber {
1717
if let abbrev = plist["abbrev"] as? NSString {
@@ -26,7 +26,7 @@ func stateFromPlistLame(_ plist: Dictionary<String, AnyObject>) -> State? {
2626
return nil
2727
}
2828

29-
func stateFromPlistCool(_ plist: Dictionary<String, AnyObject>) -> State? {
29+
func stateFromPlistCool(_ plist: Dictionary<String, Any>) -> State? {
3030
switch (plist["name"], plist["population"], plist["abbrev"]) {
3131
case let (name as String, pop as Int, abbr as String)
3232
where abbr.characters.count == 2:
@@ -38,22 +38,22 @@ func stateFromPlistCool(_ plist: Dictionary<String, AnyObject>) -> State? {
3838
}
3939
}
4040

41-
let goodStatePlist: Dictionary<String, AnyObject> = [
42-
"name": "California",
43-
"population": 38_040_000,
44-
"abbrev": "CA",
41+
let goodStatePlist: Dictionary<String, Any> = [
42+
"name" as String: "California",
43+
"population" as String: 38_040_000,
44+
"abbrev" as String: "CA",
4545
]
46-
let invalidStatePlist1: Dictionary<String, AnyObject> = [
46+
let invalidStatePlist1: Dictionary<String, Any> = [
4747
"name": "California",
4848
"population": "hella",
4949
"abbrev": "CA",
5050
]
51-
let invalidStatePlist2: Dictionary<String, AnyObject> = [
51+
let invalidStatePlist2: Dictionary<String, Any> = [
5252
"name": "California",
5353
"population": 38_040_000,
5454
"abbrev": "Cali",
5555
]
56-
let invalidStatePlist3: Dictionary<String, AnyObject> = [
56+
let invalidStatePlist3: Dictionary<String, Any> = [
5757
"name": "California",
5858
"population": 38_040_000,
5959
]
@@ -102,7 +102,7 @@ enum Statistic : CustomReflectable {
102102
}
103103
}
104104

105-
func statisticFromPlist(_ plist: Dictionary<String, AnyObject>) -> Statistic? {
105+
func statisticFromPlist(_ plist: Dictionary<String, Any>) -> Statistic? {
106106
switch (plist["kind"], plist["name"], plist["population"], plist["abbrev"]) {
107107
case let ("state" as String, name as String, population as Int, abbrev as String)
108108
where abbrev.characters.count == 2:
@@ -117,29 +117,29 @@ func statisticFromPlist(_ plist: Dictionary<String, AnyObject>) -> Statistic? {
117117
}
118118
}
119119

120-
let goodStatePlist2: Dictionary<String, AnyObject> = [
120+
let goodStatePlist2: Dictionary<String, Any> = [
121121
"kind": "state",
122122
"name": "California",
123123
"population": 38_040_000,
124124
"abbrev": "CA"
125125
]
126-
let goodCountryPlist: Dictionary<String, AnyObject> = [
126+
let goodCountryPlist: Dictionary<String, Any> = [
127127
"kind": "country",
128128
"name": "India",
129129
"population": 1_23_70_00_000,
130130
]
131-
let invalidCountryPlist1: Dictionary<String, AnyObject> = [
131+
let invalidCountryPlist1: Dictionary<String, Any> = [
132132
"kind": "country",
133133
"name": "India",
134134
"population": 1_23_70_00_000,
135135
"abbrev": "IN"
136136
]
137-
let invalidCountryPlist2: Dictionary<String, AnyObject> = [
137+
let invalidCountryPlist2: Dictionary<String, Any> = [
138138
"kind": "country",
139139
"name": "India",
140140
"population": "123 crore",
141141
]
142-
let invalidKindPlist: Dictionary<String, AnyObject> = [
142+
let invalidKindPlist: Dictionary<String, Any> = [
143143
"kind": "planet",
144144
"name": "Mercury",
145145
"population": 0

test/Interpreter/SDK/objc_cast.swift

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-run-simple-swift | FileCheck %s
22
// REQUIRES: executable_test
33

4+
// rdar://problem/27616753
5+
// XFAIL: *
6+
47
// REQUIRES: objc_interop
58

69
import Foundation

test/Interpreter/SDK/objc_dynamic_lookup.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import Foundation
77

88
// Dynamic subscripting of NSArray, dynamic method dispatch
99
// CHECK: {{^3$}}
10-
var array : AnyObject = [1, 2, 3, 4, 5]
10+
var array : AnyObject = [1 as NSNumber, 2 as NSNumber, 3 as NSNumber, 4 as NSNumber, 5 as NSNumber] as NSArray
1111
print((array[2] as AnyObject).description)
1212

1313
// Dynamic subscripting on an array using an object (fails)
1414
// CHECK: NSArray subscript with an object fails
15-
var optVal1 = array["Hello"]
15+
var optVal1 = array["Hello" as NSString]
1616
if optVal1 != nil {
1717
print(((optVal1!)! as AnyObject).description)
1818
} else {
@@ -21,9 +21,9 @@ if optVal1 != nil {
2121

2222
// Dynamic subscripting of NSDictionary, dynamic method dispatch
2323
// CHECK: {{^2$}}
24-
var nsdict : NSDictionary = ["Hello" : 1, "World" : 2]
24+
var nsdict : NSDictionary = ["Hello" as NSString : 1 as NSNumber, "World" as NSString : 2 as NSNumber]
2525
var dict : AnyObject = nsdict
26-
print(((dict["World"]!)! as AnyObject).description)
26+
print(((dict["World" as NSString]!)! as AnyObject).description)
2727

2828
// Dynamic subscripting on a dictionary using an index (fails)
2929
// CHECK: NSDictionary subscript with an index fails

0 commit comments

Comments
 (0)