Skip to content

Commit 218fd4a

Browse files
authored
[pigeon] adds support for non nullable types in collections (#7547)
[pigeon] adds support for non nullable types in collections fixes flutter/flutter#97848
1 parent 8f47459 commit 218fd4a

File tree

38 files changed

+29880
-10084
lines changed

38 files changed

+29880
-10084
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+
## 22.4.0
2+
3+
* Adds support for non-nullable types in collections.
4+
15
## 22.3.0
26

37
* Adds support for enums and classes in collections.

packages/pigeon/lib/dart_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,8 +2159,8 @@ String _getMethodParameterSignature(Iterable<Parameter> parameters) {
21592159
String _flattenTypeArguments(List<TypeDeclaration> args) {
21602160
return args
21612161
.map<String>((TypeDeclaration arg) => arg.typeArguments.isEmpty
2162-
? '${arg.baseName}?'
2163-
: '${arg.baseName}<${_flattenTypeArguments(arg.typeArguments)}>?')
2162+
? '${arg.baseName}${arg.isNullable ? '?' : ''}'
2163+
: '${arg.baseName}<${_flattenTypeArguments(arg.typeArguments)}>${arg.isNullable ? '?' : ''}')
21642164
.join(', ');
21652165
}
21662166

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'ast.dart';
1414
/// The current version of pigeon.
1515
///
1616
/// This must match the version in pubspec.yaml.
17-
const String pigeonVersion = '22.3.0';
17+
const String pigeonVersion = '22.4.0';
1818

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

packages/pigeon/lib/objc_generator.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
297297
final _ObjcType returnType = _objcTypeForDartType(
298298
generatorOptions.prefix, func.returnType,
299299
// Nullability is required since the return must be nil if NSError is set.
300-
forceNullability: true,
300+
forceBox: true,
301301
);
302302
final String callbackType =
303303
_callbackForType(func.returnType, returnType, generatorOptions);
@@ -336,7 +336,7 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
336336
func.returnType,
337337
// Nullability is required since the return must be nil if NSError is
338338
// set.
339-
forceNullability: true,
339+
forceBox: true,
340340
);
341341

342342
String? lastArgName;
@@ -1072,7 +1072,7 @@ static FlutterError *createConnectionError(NSString *channelName) {
10721072
final _ObjcType returnType = _objcTypeForDartType(
10731073
generatorOptions.prefix, func.returnType,
10741074
// Nullability is required since the return must be nil if NSError is set.
1075-
forceNullability: true,
1075+
forceBox: true,
10761076
);
10771077
final Iterable<String> selectorComponents =
10781078
_getSelectorComponents(func, lastSelectorComponent);
@@ -1187,7 +1187,7 @@ void _writeMethod(
11871187
languageOptions.prefix,
11881188
func.returnType,
11891189
// Nullability is required since the return must be nil if NSError is set.
1190-
forceNullability: true,
1190+
forceBox: true,
11911191
);
11921192
final String callbackType =
11931193
_callbackForType(func.returnType, returnType, languageOptions);
@@ -1444,23 +1444,23 @@ String _flattenTypeArguments(String? classPrefix, List<TypeDeclaration> args) {
14441444
return _enumName(e.baseName,
14451445
prefix: classPrefix, box: true, suffix: ' *');
14461446
}
1447-
return _objcTypeForDartType(classPrefix, e).toString();
1447+
return _objcTypeForDartType(classPrefix, e, forceBox: true).toString();
14481448
}).join(', ');
14491449
return result;
14501450
}
14511451

14521452
_ObjcType? _objcTypeForPrimitiveDartType(TypeDeclaration type,
1453-
{bool forceNullability = false}) {
1454-
return forceNullability || type.isNullable
1453+
{bool forceBox = false}) {
1454+
return forceBox || type.isNullable
14551455
? _objcTypeForNullableDartTypeMap[type.baseName]
14561456
: _objcTypeForNonNullableDartTypeMap[type.baseName];
14571457
}
14581458

14591459
String? _objcTypeStringForPrimitiveDartType(
14601460
String? classPrefix, TypeDeclaration type,
1461-
{required bool beforeString, bool forceNullability = false}) {
1461+
{required bool beforeString, bool forceBox = false}) {
14621462
final _ObjcType? objcType;
1463-
if (forceNullability || type.isNullable) {
1463+
if (forceBox || type.isNullable) {
14641464
objcType = _objcTypeForNullableDartTypeMap.containsKey(type.baseName)
14651465
? _objcTypeForDartType(classPrefix, type)
14661466
: null;
@@ -1475,14 +1475,14 @@ String? _objcTypeStringForPrimitiveDartType(
14751475
/// Returns the Objective-C type for a Dart [field], prepending the
14761476
/// [classPrefix] for generated classes.
14771477
_ObjcType _objcTypeForDartType(String? classPrefix, TypeDeclaration field,
1478-
{bool forceNullability = false}) {
1478+
{bool forceBox = false}) {
14791479
final _ObjcType? primitiveType =
1480-
_objcTypeForPrimitiveDartType(field, forceNullability: forceNullability);
1480+
_objcTypeForPrimitiveDartType(field, forceBox: forceBox);
14811481
return primitiveType == null
14821482
? _ObjcType(
14831483
baseName: _className(classPrefix, field.baseName),
14841484
// Non-nullable enums are non-pointer types.
1485-
isPointer: !field.isEnum || (field.isNullable || forceNullability))
1485+
isPointer: !field.isEnum || (field.isNullable || forceBox))
14861486
: field.typeArguments.isEmpty
14871487
? primitiveType
14881488
: _ObjcType(

packages/pigeon/lib/pigeon_lib.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,6 @@ List<Error> _validateAst(Root root, String source) {
971971
lineNumber: _calculateLineNumberNullable(source, field.offset),
972972
));
973973
}
974-
for (final TypeDeclaration typeArgument in field.type.typeArguments) {
975-
if (!typeArgument.isNullable) {
976-
result.add(Error(
977-
message:
978-
'Generic type parameters must be nullable in field "${field.name}" in class "${classDefinition.name}".',
979-
lineNumber: _calculateLineNumberNullable(source, field.offset),
980-
));
981-
}
982-
}
983974
if (!(validTypes.contains(field.type.baseName) ||
984975
customClasses.contains(field.type.baseName) ||
985976
customEnums.contains(field.type.baseName))) {

packages/pigeon/lib/swift_generator.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,7 @@ String _flattenTypeArguments(List<TypeDeclaration> args) {
10311031
}
10321032

10331033
String _swiftTypeForBuiltinGenericDartType(TypeDeclaration type) {
1034-
if (type.typeArguments.isEmpty ||
1035-
(type.typeArguments.first.baseName == 'Object')) {
1034+
if (type.typeArguments.isEmpty) {
10361035
if (type.baseName == 'List') {
10371036
return '[Any?]';
10381037
} else if (type.baseName == 'Map') {
@@ -1044,14 +1043,17 @@ String _swiftTypeForBuiltinGenericDartType(TypeDeclaration type) {
10441043
if (type.baseName == 'List') {
10451044
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first)}]';
10461045
} else if (type.baseName == 'Map') {
1047-
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first)}: ${_nullSafeSwiftTypeForDartType(type.typeArguments.last)}]';
1046+
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first, mapKey: true)}: ${_nullSafeSwiftTypeForDartType(type.typeArguments.last)}]';
10481047
} else {
10491048
return '${type.baseName}<${_flattenTypeArguments(type.typeArguments)}>';
10501049
}
10511050
}
10521051
}
10531052

1054-
String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
1053+
String? _swiftTypeForBuiltinDartType(
1054+
TypeDeclaration type, {
1055+
bool mapKey = false,
1056+
}) {
10551057
const Map<String, String> swiftTypeForDartTypeMap = <String, String>{
10561058
'void': 'Void',
10571059
'bool': 'Bool',
@@ -1065,7 +1067,9 @@ String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
10651067
'Float64List': 'FlutterStandardTypedData',
10661068
'Object': 'Any',
10671069
};
1068-
if (swiftTypeForDartTypeMap.containsKey(type.baseName)) {
1070+
if (mapKey && type.baseName == 'Object') {
1071+
return 'AnyHashable';
1072+
} else if (swiftTypeForDartTypeMap.containsKey(type.baseName)) {
10691073
return swiftTypeForDartTypeMap[type.baseName];
10701074
} else if (type.baseName == 'List' || type.baseName == 'Map') {
10711075
return _swiftTypeForBuiltinGenericDartType(type);
@@ -1074,13 +1078,16 @@ String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
10741078
}
10751079
}
10761080

1077-
String _swiftTypeForDartType(TypeDeclaration type) {
1078-
return _swiftTypeForBuiltinDartType(type) ?? type.baseName;
1081+
String _swiftTypeForDartType(TypeDeclaration type, {bool mapKey = false}) {
1082+
return _swiftTypeForBuiltinDartType(type, mapKey: mapKey) ?? type.baseName;
10791083
}
10801084

1081-
String _nullSafeSwiftTypeForDartType(TypeDeclaration type) {
1085+
String _nullSafeSwiftTypeForDartType(
1086+
TypeDeclaration type, {
1087+
bool mapKey = false,
1088+
}) {
10821089
final String nullSafe = type.isNullable ? '?' : '';
1083-
return '${_swiftTypeForDartType(type)}$nullSafe';
1090+
return '${_swiftTypeForDartType(type, mapKey: mapKey)}$nullSafe';
10841091
}
10851092

10861093
String _getMethodSignature({

0 commit comments

Comments
 (0)