Skip to content

[3.0] Fix casting to Measurement and introduce custom AnyHashable support #4801

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
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
4 changes: 2 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4176,7 +4176,7 @@ ConstraintSystem::simplifyRestrictedConstraint(ConversionRestrictionKind restric
}

// If the bridged value type is generic, the generic arguments
// must match the
// must either match or be bridged.
// FIXME: This should be an associated type of the protocol.
if (auto bgt1 = type2->getAs<BoundGenericType>()) {
if (bgt1->getDecl() == TC.Context.getArrayDecl()) {
Expand Down Expand Up @@ -4219,7 +4219,7 @@ ConstraintSystem::simplifyRestrictedConstraint(ConversionRestrictionKind restric
locator.withPathElement(
LocatorPathElt::getGenericArgument(0))));
} else {
llvm_unreachable("unhandled generic bridged type");
// Nothing special to do; matchTypes will match generic arguments.
}
}

Expand Down
5 changes: 0 additions & 5 deletions stdlib/public/SDK/Foundation/Measurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ extension Measurement : _ObjectiveCBridgeable {
}
}

/*
FIXME(id-as-any): can't write this code because of:
<rdar://problem/27539951> "unhandled generic bridged type" when bridging NSMeasurement

@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
extension NSMeasurement : _HasCustomAnyHashableRepresentation {
// Must be @nonobjc to avoid infinite recursion during bridging.
Expand All @@ -239,7 +235,6 @@ extension NSMeasurement : _HasCustomAnyHashableRepresentation {
return AnyHashable(self as Measurement)
}
}
*/

// This workaround is required for the time being, because Swift doesn't support covariance for Measurement (26607639)
@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
Expand Down
30 changes: 30 additions & 0 deletions test/1_stdlib/TestMeasurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ class TestMeasurement : TestMeasurementSuper {
expectTrue(fiveKM < sevenThousandM)
expectTrue(fiveKM <= fiveThousandM)
}

func test_AnyHashableContainingMeasurement() {
let values: [Measurement<UnitLength>] = [
Measurement(value: 100, unit: UnitLength.meters),
Measurement(value: 100, unit: UnitLength.kilometers),
Measurement(value: 100, unit: UnitLength.kilometers),
]
let anyHashables = values.map(AnyHashable.init)
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[0].base))
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[1].base))
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[2].base))
expectNotEqual(anyHashables[0], anyHashables[1])
expectEqual(anyHashables[1], anyHashables[2])
}

func test_AnyHashableCreatedFromNSMeasurement() {
let values: [NSMeasurement] = [
NSMeasurement(doubleValue: 100, unit: UnitLength.meters),
NSMeasurement(doubleValue: 100, unit: UnitLength.kilometers),
NSMeasurement(doubleValue: 100, unit: UnitLength.kilometers),
]
let anyHashables = values.map(AnyHashable.init)
expectEqual(Measurement<Unit>.self, type(of: anyHashables[0].base))
expectEqual(Measurement<Unit>.self, type(of: anyHashables[1].base))
expectEqual(Measurement<Unit>.self, type(of: anyHashables[2].base))
expectNotEqual(anyHashables[0], anyHashables[1])
expectEqual(anyHashables[1], anyHashables[2])
}
}

#if !FOUNDATION_XCTEST
Expand All @@ -159,6 +187,8 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
MeasurementTests.test("testMeasurementFormatter") { TestMeasurement().testMeasurementFormatter() }
MeasurementTests.test("testEquality") { TestMeasurement().testEquality() }
MeasurementTests.test("testComparison") { TestMeasurement().testComparison() }
MeasurementTests.test("test_AnyHashableContainingMeasurement") { TestMeasurement().test_AnyHashableContainingMeasurement() }
MeasurementTests.test("test_AnyHashableCreatedFromNSMeasurement") { TestMeasurement().test_AnyHashableCreatedFromNSMeasurement() }
runAllTests()
}
#endif
15 changes: 15 additions & 0 deletions test/ClangModules/objc_bridging_custom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,18 @@ class TestObjCProtoImpl : NSObject, TestObjCProto {
return nil
}
}

// Check explicit conversions for bridged generic types.
// rdar://problem/27539951
func testExplicitConversion(objc: APPManufacturerInfo<NSString>,
swift: ManufacturerInfo<NSString>) {
// Bridging to Swift
let _ = objc as ManufacturerInfo<NSString>
let _ = objc as ManufacturerInfo<NSNumber> // expected-error{{cannot convert value of type 'APPManufacturerInfo<NSString>' to type 'ManufacturerInfo<NSNumber>' in coercion}}
let _ = objc as ManufacturerInfo<NSObject> // expected-error{{cannot convert value of type 'APPManufacturerInfo<NSString>' to type 'ManufacturerInfo<NSObject>' in coercion}}

// Bridging to Objective-C
let _ = swift as APPManufacturerInfo<NSString>
let _ = swift as APPManufacturerInfo<NSNumber> // expected-error{{cannot convert value of type 'ManufacturerInfo<NSString>' to type 'APPManufacturerInfo<NSNumber>' in coercion}}
let _ = swift as APPManufacturerInfo<NSObject> // expected-error{{cannot convert value of type 'ManufacturerInfo<NSString>' to type 'APPManufacturerInfo<NSObject>' in coercion}}
}