Skip to content

rdar://problem/27651717 Make bridged String and collection types conform to CVarArg. #4000

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
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
11 changes: 8 additions & 3 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4073,9 +4073,14 @@ ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
*bridgedValueType = type;

// Find the Objective-C class type we bridge to.
return ProtocolConformance::getTypeWitnessByName(
type, conformance->getConcrete(), Id_ObjectiveCType,
resolver);
if (conformance->isConcrete()) {
return ProtocolConformance::getTypeWitnessByName(
type, conformance->getConcrete(), Id_ObjectiveCType,
resolver);
} else {
return type->castTo<ArchetypeType>()->getNestedType(Id_ObjectiveCType)
.getValue();
}
}

// Do we conform to Error?
Expand Down
17 changes: 17 additions & 0 deletions stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1550,3 +1550,20 @@ extension AnyHashable : _ObjectiveCBridgeable {
}
}

//===----------------------------------------------------------------------===//
// CVarArg for bridged types
//===----------------------------------------------------------------------===//

extension CVarArg where Self: _ObjectiveCBridgeable {
/// Default implementation for bridgeable types.
public var _cVarArgEncoding: [Int] {
let object = self._bridgeToObjectiveC()
_autorelease(object)
return _encodeBitsAsWords(object)
}
}

extension String: CVarArg {}
extension Array: CVarArg {}
extension Dictionary: CVarArg {}
extension Set: CVarArg {}
2 changes: 1 addition & 1 deletion stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public func getVaList(_ args: [CVarArg]) -> CVaListPointer {
}
#endif

public func _encodeBitsAsWords<T : CVarArg>(_ x: T) -> [Int] {
public func _encodeBitsAsWords<T>(_ x: T) -> [Int] {
let result = [Int](
repeating: 0,
count: (MemoryLayout<T>.size + MemoryLayout<Int>.size - 1) / MemoryLayout<Int>.size)
Expand Down
2 changes: 1 addition & 1 deletion test/Interpreter/SDK/Foundation_NSExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

// Test overlain variadic methods.
let expression = NSExpression(format: "(3 + 2)**2", "LLLL" as NSString, "BBBB" as NSString)
let expression = NSExpression(format: "(3 + 2)**2", "LLLL", "BBBB")
let result = expression.expressionValue(with: expression, context:nil) as! NSNumber
let number = result.stringValue
print(number)
Expand Down
2 changes: 1 addition & 1 deletion test/Interpreter/SDK/Foundation_NSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ testNSLog()
// CHECK: 1 is the loneliest number that you'll ever do
NSLog(
"%@ is the loneliest number that you'll ever %@",
NSNumber(value: 1), "do" as NSString
NSNumber(value: 1), "do"
)
2 changes: 1 addition & 1 deletion test/Interpreter/SDK/Foundation_NSPredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

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

// CHECK: lastName LIKE[cd] "LLLL" AND birthday > "BBBB"
16 changes: 12 additions & 4 deletions test/Interpreter/SDK/Foundation_NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,30 @@ testComparisons()
// Test overlain variadic methods.
// CHECK-LABEL: Variadic methods:
print("Variadic methods:")
// Check that it works with bridged Strings.
// CHECK-NEXT: x y
print(NSString(format: "%@ %@", "x" as NSString, "y" as NSString))
print(NSString(format: "%@ %@", "x", "y"))
// Check that it works with bridged Arrays and Dictionaries.
// CHECK-NEXT: (
// CHECK-NEXT: x
// CHECK-NEXT: ) {
// CHECK-NEXT: y = z;
// CHECK-NEXT: }
print(NSString(format: "%@ %@", ["x"], ["y": "z"]))
// CHECK-NEXT: 1{{.*}}024,25
print(NSString(
format: "%g",
locale: Locale(identifier: "fr_FR"),
1024.25
))
// CHECK-NEXT: x y z
print(("x " as NSString).appendingFormat("%@ %@", "y" as NSString, "z" as NSString))
print(("x " as NSString).appendingFormat("%@ %@", "y", "z"))
// CHECK-NEXT: a b c
let s = NSMutableString(string: "a ")
s.appendFormat("%@ %@", "b" as NSString, "c" as NSString)
s.appendFormat("%@ %@", "b", "c")
print(s)

let m = NSMutableString.localizedStringWithFormat("<%@ %@>", "q" as NSString, "r" as NSString)
let m = NSMutableString.localizedStringWithFormat("<%@ %@>", "q", "r")
// CHECK-NEXT: <q r>
print(m)
m.append(" lever")
Expand Down