From 1092514f12ca6f6e4bda60bb1bbf6fd97acdf2cf Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 17 Feb 2023 18:08:14 -0500 Subject: [PATCH] Make sure we handle subclasses properly when we use instancetype. (#25169) We should not be claiming to return instancetype if we actually always return an instance of our class. Fixes https://github.com/project-chip/connectedhomeip/issues/23384 --- src/darwin/Framework/CHIP/MTRBaseDevice.h | 26 ++++++++++--------- src/darwin/Framework/CHIP/MTRBaseDevice.mm | 14 +++++----- src/darwin/Framework/CHIP/MTRDevice.h | 8 +++--- src/darwin/Framework/CHIP/MTRDevice.mm | 8 +++--- .../CHIP/MTRDeviceControllerFactory.h | 4 +-- .../CHIP/MTRDeviceControllerFactory.mm | 4 +-- .../CHIP/MTRDeviceControllerXPCConnection.h | 3 ++- .../CHIP/MTRDeviceControllerXPCConnection.mm | 3 ++- 8 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.h b/src/darwin/Framework/CHIP/MTRBaseDevice.h index c7d5c364691eca..5317d1e3e15e59 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.h @@ -141,8 +141,9 @@ typedef NS_ENUM(uint8_t, MTRTransportType) { * fabric, but attempts to actually use the MTRBaseDevice will fail * (asynchronously) in that case. */ -+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID - controller:(MTRDeviceController *)controller API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); ++ (MTRBaseDevice *)deviceWithNodeID:(NSNumber *)nodeID + controller:(MTRDeviceController *)controller + API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); /** * The transport used by the current session with this device, or @@ -377,7 +378,7 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)) @property (nonatomic, readonly, copy) NSNumber * endpoint; @property (nonatomic, readonly, copy) NSNumber * cluster; -+ (instancetype)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID; ++ (MTRClusterPath *)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID; - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; @@ -390,9 +391,10 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)) @interface MTRAttributePath : MTRClusterPath @property (nonatomic, readonly, copy) NSNumber * attribute; -+ (instancetype)attributePathWithEndpointID:(NSNumber *)endpointID - clusterID:(NSNumber *)clusterID - attributeID:(NSNumber *)attributeID API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); ++ (MTRAttributePath *)attributePathWithEndpointID:(NSNumber *)endpointID + clusterID:(NSNumber *)clusterID + attributeID:(NSNumber *)attributeID + API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); @end /** @@ -403,9 +405,9 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)) @interface MTREventPath : MTRClusterPath @property (nonatomic, readonly, copy) NSNumber * event; -+ (instancetype)eventPathWithEndpointID:(NSNumber *)endpointID - clusterID:(NSNumber *)clusterID - eventID:(NSNumber *)eventID API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); ++ (MTREventPath *)eventPathWithEndpointID:(NSNumber *)endpointID + clusterID:(NSNumber *)clusterID + eventID:(NSNumber *)eventID API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); @end /** @@ -415,9 +417,9 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)) @interface MTRCommandPath : MTRClusterPath @property (nonatomic, readonly, copy) NSNumber * command; -+ (instancetype)commandPathWithEndpointID:(NSNumber *)endpointID - clusterID:(NSNumber *)clusterID - commandID:(NSNumber *)commandID API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); ++ (MTRCommandPath *)commandPathWithEndpointID:(NSNumber *)endpointID + clusterID:(NSNumber *)clusterID + commandID:(NSNumber *)commandID API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); @end @interface MTRAttributeReport : NSObject diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index 98760e8c4fff86..1234edb4e2b1e8 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -264,7 +264,7 @@ - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControlle return self; } -+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller ++ (MTRBaseDevice *)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller { // Indirect through the controller to give it a chance to create an // MTRBaseDeviceOverXPC instead of just an MTRBaseDevice. @@ -1870,7 +1870,7 @@ - (NSString *)description (uint32_t) _cluster.unsignedLongValue]; } -+ (instancetype)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID ++ (MTRClusterPath *)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID { ConcreteClusterPath path(static_cast([endpointID unsignedShortValue]), static_cast([clusterID unsignedLongValue])); @@ -1919,9 +1919,9 @@ - (NSString *)description (uint32_t) _attribute.unsignedLongValue]; } -+ (instancetype)attributePathWithEndpointID:(NSNumber *)endpointID - clusterID:(NSNumber *)clusterID - attributeID:(NSNumber *)attributeID ++ (MTRAttributePath *)attributePathWithEndpointID:(NSNumber *)endpointID + clusterID:(NSNumber *)clusterID + attributeID:(NSNumber *)attributeID { ConcreteDataAttributePath path(static_cast([endpointID unsignedShortValue]), static_cast([clusterID unsignedLongValue]), @@ -1979,7 +1979,7 @@ - (NSString *)description (uint32_t) self.cluster.unsignedLongValue, (uint32_t) _event.unsignedLongValue]; } -+ (instancetype)eventPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID eventID:(NSNumber *)eventID ++ (MTREventPath *)eventPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID eventID:(NSNumber *)eventID { ConcreteEventPath path(static_cast([endpointID unsignedShortValue]), static_cast([clusterID unsignedLongValue]), static_cast([eventID unsignedLongValue])); @@ -2009,7 +2009,7 @@ - (instancetype)initWithPath:(const ConcreteCommandPath &)path return self; } -+ (instancetype)commandPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID commandID:(NSNumber *)commandID ++ (MTRCommandPath *)commandPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID commandID:(NSNumber *)commandID { ConcreteCommandPath path(static_cast([endpointID unsignedShortValue]), static_cast([clusterID unsignedLongValue]), static_cast([commandID unsignedLongValue])); diff --git a/src/darwin/Framework/CHIP/MTRDevice.h b/src/darwin/Framework/CHIP/MTRDevice.h index e3cd859b6f816d..4ae66999f52f64 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.h +++ b/src/darwin/Framework/CHIP/MTRDevice.h @@ -44,8 +44,8 @@ typedef NS_ENUM(NSUInteger, MTRDeviceState) { * retrieved when performing actions using a combination of MTRBaseDevice * and MTRAsyncCallbackQueue. */ -+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID - controller:(MTRDeviceController *)controller API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); ++ (MTRDevice *)deviceWithNodeID:(NSNumber *)nodeID + controller:(MTRDeviceController *)controller API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)); /** * The current state of the device. @@ -238,8 +238,8 @@ extern NSString * const MTREventTimestampDateKey MTR_NEWLY_AVAILABLE; /** * Deprecated MTRDevice APIs. */ -+ (instancetype)deviceWithNodeID:(uint64_t)nodeID - deviceController:(MTRDeviceController *)deviceController ++ (MTRDevice *)deviceWithNodeID:(uint64_t)nodeID + deviceController:(MTRDeviceController *)deviceController API_DEPRECATED( "Please use deviceWithNodeID:controller:", ios(16.1, 16.4), macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4)); diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index cf949a46e5e795..642a45ae622993 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -65,7 +65,7 @@ - (instancetype)initWithFirst:(id)first second:(id)second } + (instancetype)pairWithFirst:(id)first second:(id)second { - return [[MTRPair alloc] initWithFirst:first second:second]; + return [[self alloc] initWithFirst:first second:second]; } @end @@ -91,7 +91,7 @@ - (instancetype)initWithObject:(id)object } + (instancetype)weakReferenceWithObject:(id)object { - return [[MTRWeakReference alloc] initWithObject:object]; + return [[self alloc] initWithObject:object]; } - (id)strongObject { @@ -221,7 +221,7 @@ - (NSString *)description return [NSString stringWithFormat:@"[fabric: %u, nodeID: %@]", self, _fabricIndex, _nodeID]; } -+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller ++ (MTRDevice *)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller { return [controller deviceForNodeID:nodeID]; } @@ -982,7 +982,7 @@ - (MTRBaseDevice *)newBaseDevice @implementation MTRDevice (Deprecated) -+ (instancetype)deviceWithNodeID:(uint64_t)nodeID deviceController:(MTRDeviceController *)deviceController ++ (MTRDevice *)deviceWithNodeID:(uint64_t)nodeID deviceController:(MTRDeviceController *)deviceController { return [self deviceWithNodeID:@(nodeID) controller:deviceController]; } diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h index 25039c5a8b6c83..7f6fab792c07eb 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h @@ -96,7 +96,7 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4)) * Return the single MTRDeviceControllerFactory we support existing. It starts off * in a "not started" state. */ -+ (instancetype)sharedInstance; ++ (MTRDeviceControllerFactory *)sharedInstance; /** * If true, the factory is in a state where it can create controllers: @@ -167,7 +167,7 @@ API_DEPRECATED( API_DEPRECATED("Please use MTRDeviceControllerFactory", ios(16.1, 16.4), macos(13.0, 13.3), watchos(9.1, 9.4), tvos(16.1, 16.4)) @interface MTRControllerFactory : NSObject @property (readonly, nonatomic) BOOL isRunning; -+ (instancetype)sharedInstance; ++ (MTRControllerFactory *)sharedInstance; - (BOOL)startup:(MTRControllerFactoryParams *)startupParams; - (void)shutdown; - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams; diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index b47f192520bc61..9c96f7e98ad308 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -97,7 +97,7 @@ + (void)initialize MTRFrameworkInit(); } -+ (instancetype)sharedInstance ++ (MTRDeviceControllerFactory *)sharedInstance { static MTRDeviceControllerFactory * factory = nil; static dispatch_once_t onceToken; @@ -815,7 +815,7 @@ - (BOOL)isRunning return [[MTRDeviceControllerFactory sharedInstance] isRunning]; } -+ (instancetype)sharedInstance ++ (MTRControllerFactory *)sharedInstance { // We could try to delegate to MTRDeviceControllerFactory's sharedInstance // here, but then we would have to add the backwards-compar selectors to diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.h b/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.h index 34a4041144675d..3c6a82c1235ec0 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.h @@ -45,7 +45,8 @@ typedef void (^MTRGetProxyHandleHandler)(dispatch_queue_t queue, MTRDeviceContro /** * This method is just for test purpsoe. */ -+ (instancetype)connectionWithWorkQueue:(dispatch_queue_t)workQueue connectBlock:(MTRXPCConnectBlock)connectBlock; ++ (MTRDeviceControllerXPCConnection *)connectionWithWorkQueue:(dispatch_queue_t)workQueue + connectBlock:(MTRXPCConnectBlock)connectBlock; - (void)getProxyHandleWithCompletion:(MTRGetProxyHandleHandler)completion; - (void)registerReportHandlerWithController:(id)controller diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.mm index dc5cec1081dbf7..6428e148af7f69 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerXPCConnection.mm @@ -78,7 +78,8 @@ - (instancetype)initWithWorkQueue:(dispatch_queue_t)workQueue connectBlock:(NSXP } // This class method is for unit testing -+ (instancetype)connectionWithWorkQueue:(dispatch_queue_t)workQueue connectBlock:(NSXPCConnection * (^)(void) )connectBlock ++ (MTRDeviceControllerXPCConnection *)connectionWithWorkQueue:(dispatch_queue_t)workQueue + connectBlock:(NSXPCConnection * (^)(void) )connectBlock { return [[MTRDeviceControllerXPCConnection alloc] initWithWorkQueue:workQueue connectBlock:connectBlock]; }