Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix kotlin
  • Loading branch information
bparrishMines committed Feb 6, 2025
commit 4c06e7c597320b63a4046fccda24ee73767f7f2c
6 changes: 5 additions & 1 deletion packages/pigeon/lib/src/kotlin/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,11 @@ if (wrapped == null) {
'if (pigeonRegistrar.instanceManager.containsInstance(${classMemberNamePrefix}instanceArg)) {',
'}',
() {
indent.writeln('Result.success(Unit)');
indent.format(
'''
callback(Result.success(Unit))
return''',
);
indent.writeln('return');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is corresponding generator line?

},
);
Expand Down
4 changes: 2 additions & 2 deletions packages/pigeon/lib/src/swift/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
}
indent.addScoped('else {', '}', () {
if (returnType.isVoid) {
indent.writeln('completion(.success(Void()))');
indent.writeln('completion(.success(()))');
} else {
final String fieldType = _swiftTypeForDartType(returnType);
_writeGenericCasting(
Expand Down Expand Up @@ -2316,7 +2316,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
'if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {',
'}',
() {
indent.writeln('completion(.success(Void()))');
indent.writeln('completion(.success(()))');
indent.writeln('return');
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3057,7 +3057,8 @@ abstract class PigeonApiProxyApiTestClass(
return
}
if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
Result.success(Unit)
callback(Result.success(Unit))
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return shouldn't be generated any more; with the change to the if/else if/else structure it's not needed, and it's inconsistent with the other branches.

return
}
callback(
Expand Down Expand Up @@ -4049,7 +4050,8 @@ abstract class PigeonApiProxyApiSuperClass(
return
}
if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
Result.success(Unit)
callback(Result.success(Unit))
return
return
}
val pigeon_identifierArg =
Expand Down Expand Up @@ -4089,7 +4091,8 @@ open class PigeonApiProxyApiInterface(
return
}
if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
Result.success(Unit)
callback(Result.success(Unit))
return
return
}
val pigeon_identifierArg =
Expand Down Expand Up @@ -4259,7 +4262,8 @@ abstract class PigeonApiClassWithApiRequirement(
return
}
if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
Result.success(Unit)
callback(Result.success(Unit))
return
return
}
val pigeon_identifierArg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4299,7 +4299,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4342,7 +4342,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -5659,7 +5659,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private class ProxyApiTestsPigeonInstanceManagerApi {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -2817,7 +2817,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same structural question for this whole method (I can't comment on the method declaration since it's outside of the change context): could this have a simple return instead of a completion, reducing the complexity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code to use a if...else if...else, so it no longer needs a return.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue isn't the return, it's that (as the Kotlin bug demonstrated) having a function use an async callback is inherently more error-prone, because the compiler will enforce that you return exactly once, but callbacks don't. Since we don't need any async behavior here, the method would be safer if it used a return value instead of a callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but technically the method is making a async call when it makes the MethodCall to create the Dart instance. However, the case that the MethodCall is replaced by the error, the method probably could just return a Result. Were you suggesting that the method should return a Result when its not actually making a method call?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following where there's an async step in this method, looking at the code. The structure of pigeonNewInstance is:

func pigeonNewInstance(
    pigeonInstance: ProxyApiTestClass,
    completion: @escaping (Result<Void, ProxyApiTestsError>) -> Void
  ) {
    if pigeonRegistrar.ignoreCallsToDart {
      completion(...)
    } else if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
      completion(...)
    } else {
      completion(...)
    }
  }

In every branch, it's just synchronously calling completion. I don't see any reason (other than the logistics of the generator logic helper function discussed in the other thread) it couldn't be

pigeonNewInstance(pigeonInstance: ProxyApiTestClass) -> Result<Void, ProxyApiTestsError>

with every completion(...) replaced with a return ....

Copy link
Contributor Author

@bparrishMines bparrishMines Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most classes, the final completion is done inside of a MethodCall:

  } else {
      let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
        pigeonInstance as AnyObject)
      let binaryMessenger = pigeonRegistrar.binaryMessenger
      let codec = pigeonRegistrar.codec
      let channelName: String =
        "dev.flutter.pigeon.pigeon_integration_tests.ProxyApiSuperClass.pigeon_newInstance"
      let channel = FlutterBasicMessageChannel(
        name: channelName, binaryMessenger: binaryMessenger, codec: codec)
      channel.sendMessage([pigeonIdentifierArg] as [Any?]) { response in
        guard let listResponse = response as? [Any?] else {
          completion(.failure(createConnectionError(withChannelName: channelName)))
          return
        }
        if listResponse.count > 1 {
          let code: String = listResponse[0] as! String
          let message: String? = nilOrValue(listResponse[1])
          let details: String? = nilOrValue(listResponse[2])
          completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
        } else {
          completion(.success(()))
        }
      }
    }

Its based on whether the Dart class has required callback methods to be implemented. If yes, this final completion synchronously returns an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now. Sorry about that; I missed that there were two different forms of it.

completion(
Expand Down Expand Up @@ -2859,7 +2859,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -2930,7 +2930,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3756,7 +3756,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3885,7 +3885,7 @@ final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -3907,7 +3907,7 @@ final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3945,7 +3945,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -3967,7 +3967,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4000,7 +4000,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4131,7 +4131,7 @@ final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequi
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -4153,7 +4153,7 @@ final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequi
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4299,7 +4299,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4342,7 +4342,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -5659,7 +5659,7 @@ class FlutterIntegrationCoreApi: FlutterIntegrationCoreApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private class ProxyApiTestsPigeonInstanceManagerApi {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -2817,7 +2817,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
completion(
Expand Down Expand Up @@ -2859,7 +2859,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -2930,7 +2930,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3756,7 +3756,7 @@ final class PigeonApiProxyApiTestClass: PigeonApiProtocolProxyApiTestClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3885,7 +3885,7 @@ final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -3907,7 +3907,7 @@ final class PigeonApiProxyApiSuperClass: PigeonApiProtocolProxyApiSuperClass {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -3945,7 +3945,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -3967,7 +3967,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4000,7 +4000,7 @@ final class PigeonApiProxyApiInterface: PigeonApiProtocolProxyApiInterface {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down Expand Up @@ -4131,7 +4131,7 @@ final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequi
return
}
if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {
completion(.success(Void()))
completion(.success(()))
return
}
let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(
Expand All @@ -4153,7 +4153,7 @@ final class PigeonApiClassWithApiRequirement: PigeonApiProtocolClassWithApiRequi
let details: String? = nilOrValue(listResponse[2])
completion(.failure(ProxyApiTestsError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down