Skip to content

Commit 8918543

Browse files
[pigeon] Fixes compilation error with unbounded type parameter for InstanceManager (#10483)
`IdentityWeakReference` requires a nonnull value so the input to type parameter to `getInstance` has to be nonnull. Its hard to write a meaningful test since this only causes a compilation error on older Kotlin versions, but I added a unit test to make sure `getInstance` specifies nonnull. This fixes flutter/flutter#178756 once plugins update generated code. ## 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.
1 parent ad21d8d commit 8918543

File tree

7 files changed

+39
-5
lines changed

7 files changed

+39
-5
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 26.1.2
2+
3+
* [kotlin] Fixes compilation error with unbounded type parameter for InstanceManager.
4+
15
## 26.1.1
26

37
* Updates supported `analyzer` versions to 8.x or 9.x.

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
1515
/// The current version of pigeon.
1616
///
1717
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '26.1.1';
18+
const String pigeonVersion = '26.1.2';
1919

2020
/// Read all the content from [stdin] to a String.
2121
String readStdin() {

packages/pigeon/lib/src/kotlin/templates.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ${kotlinInstanceManagerClassName(options)}(private val finalizationListene
180180
}
181181
182182
/** Retrieves the instance associated with identifier, if present, otherwise `null`. */
183-
fun <T> getInstance(identifier: Long): T? {
183+
fun <T : Any> getInstance(identifier: Long): T? {
184184
logWarningIfFinalizationListenerHasStopped()
185185
val instance = weakInstances[identifier] as IdentityWeakReference<T>?
186186
return instance?.get()

packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/ProxyApiTests.gen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class ProxyApiTestsPigeonInstanceManager(
214214
}
215215

216216
/** Retrieves the instance associated with identifier, if present, otherwise `null`. */
217-
fun <T> getInstance(identifier: Long): T? {
217+
fun <T : Any> getInstance(identifier: Long): T? {
218218
logWarningIfFinalizationListenerHasStopped()
219219
val instance = weakInstances[identifier] as IdentityWeakReference<T>?
220220
return instance?.get()

packages/pigeon/platform_tests/test_plugin/android/src/test/kotlin/com/example/test_plugin/InstanceManagerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class InstanceManagerTest {
145145
instanceManager.clearFinalizedWeakReferencesInterval = 1000
146146
instanceManager.stopFinalizationListener()
147147

148-
assertNull(instanceManager.getInstance<Any?>(0))
148+
assertNull(instanceManager.getInstance(0))
149149
assertFalse(finalizerRan)
150150
}
151151

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 26.1.1 # This must match the version in lib/src/generator_tools.dart
5+
version: 26.1.2 # This must match the version in lib/src/generator_tools.dart
66

77
environment:
88
sdk: ^3.8.0

packages/pigeon/test/kotlin/proxy_api_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,36 @@ void main() {
10701070
);
10711071
},
10721072
);
1073+
1074+
test('InstanceManager.getInstance specifies nonnull type', () {
1075+
final Root root = Root(
1076+
apis: <Api>[
1077+
AstProxyApi(
1078+
name: 'Api',
1079+
constructors: <Constructor>[],
1080+
fields: <ApiField>[],
1081+
methods: <Method>[],
1082+
),
1083+
],
1084+
classes: <Class>[],
1085+
enums: <Enum>[],
1086+
);
1087+
final StringBuffer sink = StringBuffer();
1088+
const KotlinGenerator generator = KotlinGenerator();
1089+
generator.generate(
1090+
const InternalKotlinOptions(kotlinOut: ''),
1091+
root,
1092+
sink,
1093+
dartPackageName: DEFAULT_PACKAGE_NAME,
1094+
);
1095+
final String code = sink.toString();
1096+
final String collapsedCode = _collapseNewlineAndIndentation(code);
1097+
1098+
expect(
1099+
collapsedCode,
1100+
contains('fun <T : Any> getInstance(identifier: Long): T?'),
1101+
);
1102+
});
10731103
});
10741104
});
10751105
}

0 commit comments

Comments
 (0)