You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d8d9e97
Browse filesBrowse the repository at this point in the historyBrowse files
[pigeon] Reject unsupported native interop features and fix generated code that doesn't compile (#13153)
Pigeon accepted several combinations of `swift_use_ffi` or `kotlin_use_jni` with other features and options that generated code that doesn't compile. One combination crashed pigeon. flutter/flutter#193881 reported two of them: event channels and ProxyApis with Swift FFI.
To find the rest, I generated every repo pigeon input and a set of edge-case inputs with each interop option. I then type-checked the Swift, compiled the Kotlin and analyzed the Dart. That found 19 broken combinations, and fixing them found a few more.
This PR adds a validation error where native interop has no implementation yet, and fixes the generators for everything else. It also adds a test API to the test plugin that uses the names JNIgen renames, so the integration tests check pigeon's copy of JNIgen's renaming rules at runtime.
**New validation errors.** Apart from event channels and ProxyApis with Kotlin JNI, none of these combinations ever produced code that compiled.
- **Swift FFI:**
- Event channels, ProxyApis and sealed classes. The error says to move them to a separate pigeon file that doesn't use `swift_use_ffi`.
- Data class fields that conflict with `NSObject` members, because FFI bridge classes extend `NSObject`. Examples are `hash`, `isProxy` and `superclass`, plus names that ffigen renames, such as `alloc` or `isEqual`.
- HostApi parameters named `wrappedError`. ffigen would generate two `wrappedError:` named parameters.
- `includeErrorClass: false`. swiftgen compiles each Swift file on its own, so the file must declare its error class. Give each file a different `errorClassName` instead.
- **Kotlin JNI:** Event channels, ProxyApis and sealed classes, the same as Swift FFI. The error says to move them to a separate pigeon file that doesn't use `kotlin_use_jni`.
- Sealed classes never worked. The Kotlin compiles, but the generated Dart has no JNI conversion for them.
- Event channels and ProxyApis did compile, because the Kotlin file falls back to channel code for them. They're rejected so that both interop options support the same features until these work over native interop. A file that generated before can now fail validation, but native interop is still marked experimental.
- **Dart:** API method names that conflict with generated native interop members:
- HostApi: `getInstance` and `createWithNativeInteropApi`.
- FlutterApi: `implement`, and with JNI also `dartApi` and `register`.
**Generator fixes**
- **Swift (FFI):**
- The bridge for a class without fields didn't compile because its `init()` was missing `override`.
- `@asyncCallback` host methods: the bridge `await`ed a callback-style method. It now wraps the call in `withCheckedThrowingContinuation`.
- `@SwiftFunction` overloads that differ only in nullability were ambiguous. The bridge now declares the result type.
- An API method named `type` broke error handling in every method of that API, because `type(of:)` resolved to the method. The bridge now uses `Swift.type(of:)`.
- Parameters that shared a name with one of the bridge's own parameters, such as a FlutterApi parameter named `error`, collided with it. Internal parameter names now end in `Arg`. Selectors don't change.
- Two FFI files in the same module both declared `_PigeonFfiCodec`. It's now `private`.
- **Dart:**
- Pigeon crashed with `Bad state: No element` when a class without fields was used with Swift FFI.
- JNI calls used the Kotlin member names, but JNIgen changes some of them:
- It renames members that clash with its own, such as `type`, `release`, `use` and `isNull`.
- It turns `getX()`, `isX()` and `setX()` into properties.
- It turns `is`-prefixed data class properties that aren't `Boolean` into methods.
A new `_JniBindingNamer` mirrors the JNIgen 1.0.1 renamer for data class fields, HostApi methods and FlutterApi overrides. It replaces `_getJniFieldName`, which handled only a few field names.
- FFI calls assumed that Swift's Objective-C selectors always join the method name and the first argument label with `With`. Swift leaves out `With` when the method name ends with a preposition or the label starts with one. For example, `signIn(value:)` becomes `signInValue:` and `init(forKey:)` becomes `initForKey:`. Dart now applies the same rule to host API calls, FlutterApi callbacks and data class bridge initializers. It uses the preposition list from Swift's [`PartsOfSpeech.def`](https://github.com/swiftlang/swift/blob/main/lib/Basic/PartsOfSpeech.def) and Swift's camel-case word splitting.
- Generated local variables shadowed parameters with the same name, such as `res`, `error`, `completer` and `response`. Generated locals now use the `pigeonVar_` prefix, and callback parameters use `arg_`.
- Lists and maps of ProxyApis caused the interop codecs to contain the literal text `There is something wrong, a type is not classified`. The codecs now skip those types.
- The class-name prefix for FFI types didn't match the Swift one in three cases:
- The Swift file name wasn't UpperCamelCase.
- `fileSpecificClassNameComponent` wasn't UpperCamelCase.
- `SwiftOptions.fileSpecificClassNameComponent` was set.
Dart now uses the same precedence and casing as the Swift generator.
- Test files (`dartTestOut`) contained native interop code that referenced bindings they don't import. They're now generated with method channels only, which is all their mock handlers use.
- **Docs:** The `includeErrorClass` docs on `SwiftOptions` and `KotlinOptions` explain the native interop requirements.
**Test plugin**
`native_interop_tests.dart` adds `NativeInteropReservedNames`, `NativeInteropReservedNamesHostApi` and `NativeInteropReservedNamesFlutterApi`. Their names cover each JNIgen renaming rule:
- members that clash with JNIgen's own, such as `release`, `use`, `type`, `isNull`, `reference` and `jClass`;
- getters, setters and `is` methods that JNIgen turns into properties, including `getDefault`, which becomes a Dart keyword;
- `is` fields that aren't `bool`, which JNIgen turns into methods;
- an `@async` method, which JNIgen keeps as a method because it's a Kotlin `suspend` function;
- a FlutterApi method named `implementIn`, which clashes with JNIgen's `implementIn`.
The Kotlin and Swift test plugins implement both APIs, and new integration tests call every method and round-trip the data class. The tests use `true` for `isNull` and `isReleased`, so they fail if the generated code calls `JObject`'s own members instead, which return `false`. This API is also how I found the Swift selector bug above: `implementIn` ends with a preposition.
The test plugin and the native interop example are regenerated. Apart from the new test API, their diffs contain only:
- the renames above;
- `private` on `_PigeonFfiCodec`;
- `Swift.type(of:)`;
- the matching ffigen bindings and Objective-C header.
Selectors and public APIs don't change.
**Testing**
- New unit tests: 12 for validation, 6 for the Dart generator and 4 for the Swift generator. All 581 pigeon unit tests pass.
- The Android (emulator) and macOS integration suites pass, including the new reserved-name tests: 674 tests on Android, and 673 on macOS with 1 existing skip. I didn't run iOS locally.
- I compiled Kotlin that uses every JNIgen-reserved name and ran JNIgen 1.0.1 on it. The generated Dart analyzes against those bindings with no errors.
- I checked Swift's selector rule with `swiftc`, for every preposition and for names with underscores, digits and acronyms.
- I re-ran the full probe. Validation rejects exactly the unsupported combinations, and the generated Swift for every other case type-checks with `swiftc`.
Fixesflutter/flutter#193881
## Pre-Review Checklist
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.
[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
"Move them to a separate pigeon file that doesn't use Swift FFI (`SwiftOptions.useFfi` or `--swift_use_ffi`).";
404
+
405
+
void_errorOnSwiftFfiUnsupportedFeatures(
406
+
List<Error> errors,
407
+
InternalSwiftOptions swiftOptions,
408
+
Root root,
409
+
) {
410
+
for (finalApi api in root.apis) {
411
+
if (api isAstEventChannelApi) {
412
+
errors.add(
413
+
Error(
414
+
message:
415
+
'Swift FFI does not support event channels yet (in API "${api.name}"). $_moveToNonFfiFileSuggestion',
416
+
),
417
+
);
418
+
} elseif (api isAstProxyApi) {
419
+
errors.add(
420
+
Error(
421
+
message:
422
+
'Swift FFI does not support ProxyApis yet (in API "${api.name}"). $_moveToNonFfiFileSuggestion',
423
+
),
424
+
);
425
+
} elseif (api isAstHostApi) {
426
+
for (finalMethod method in api.methods) {
427
+
if (method.parameters.any((Parameter parameter) => parameter.name =='wrappedError')) {
428
+
errors.add(
429
+
Error(
430
+
message:
431
+
'Parameter "wrappedError" in method "${method.name}" in API "${api.name}" conflicts with the error parameter that Swift FFI adds to host API methods. Rename the parameter.',
432
+
),
433
+
);
434
+
}
435
+
}
436
+
}
437
+
}
438
+
for (finalClass classDefinition in root.classes) {
439
+
if (classDefinition.isSealed) {
440
+
errors.add(
441
+
Error(
442
+
message:
443
+
'Swift FFI does not support sealed classes yet (class "${classDefinition.name}"). $_moveToNonFfiFileSuggestion',
444
+
),
445
+
);
446
+
}
447
+
for (finalNamedType field in classDefinition.fields) {
448
+
// `description` has its own error for all Swift generation.
449
+
if (field.name !='description'&& _swiftFfiReservedFieldNames.contains(field.name)) {
450
+
errors.add(
451
+
Error(
452
+
message:
453
+
'Field "${field.name}" is not allowed in class "${classDefinition.name}" with Swift FFI because it conflicts with a member of NSObject, which FFI bridge classes extend. Rename the field.',
454
+
),
455
+
);
456
+
}
457
+
}
458
+
}
459
+
if (!swiftOptions.includeErrorClass) {
460
+
errors.add(
461
+
Error(
462
+
message:
463
+
'Swift FFI requires `includeErrorClass: true`, because the FFI bindings are generated from this file alone, so the error class "${swiftOptions.errorClassName ?? 'PigeonError'}" must be declared in it. To use Swift FFI in more than one pigeon file in the same module, give each file a different `errorClassName` instead.',
464
+
),
465
+
);
466
+
}
467
+
}
468
+
469
+
constString _moveToNonJniFileSuggestion =
470
+
"Move them to a separate pigeon file that doesn't use Kotlin JNI (`KotlinOptions.useJni` or `--kotlin_use_jni`).";
// The JNI registrar implements the JNIgen interface for the API, so it
564
+
// shares a namespace with the API's methods.
565
+
AstFlutterApi() =><String>{
566
+
'implement',
567
+
if (useJni) ...<String>{'dartApi', 'register'},
568
+
},
569
+
_ =>const<String>{},
570
+
};
571
+
for (finalMethod method in api.methods) {
572
+
if (reservedNames.contains(method.name)) {
573
+
errors.add(
574
+
Error(
575
+
message:
576
+
'Method name "${method.name}" in API "${api.name}" conflicts with a member of the Dart code that native interop generates for the API. Rename the method.',
577
+
),
578
+
);
579
+
}
580
+
}
581
+
}
582
+
return errors;
376
583
}
377
584
378
585
/// A [GeneratorAdapter] that generates Dart test source code.
@@ -555,6 +762,7 @@ class SwiftGeneratorAdapter implements GeneratorAdapter {
0 commit comments