Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] Enable warnings as errors for remaining languages #3317

Merged
merged 5 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 9.0.3

* [kotlin] Fixes compiler warnings in generated output.
* [swift] Fixes compiler warnings in generated output.

## 9.0.2

* [swift] Removes safe casting from decode process.
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '9.0.2';
const String pigeonVersion = '9.0.3';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
17 changes: 12 additions & 5 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
'$returnType $output = channelReply == null ? null : ((Number) channelReply).longValue();');
} else {
indent.writeln(
'$returnType $output = ($returnType) channelReply;');
'$returnType $output = ${_cast('channelReply', javaType: returnType)};');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cast changes in Java and Kotlin fix uncessary casts (foo as Any?, foo as? Any in Kotlin, (Object) foo in Java).

}
indent.writeln('callback.reply($output);');
});
Expand Down Expand Up @@ -627,8 +627,8 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
String accessor = 'args.get($index)';
if (isEnum(root, arg.type)) {
accessor = _intToEnum(accessor, arg.type.baseName);
} else if (argType != 'Object') {
accessor = '($argType) $accessor';
} else {
accessor = _cast(accessor, javaType: argType);
}
indent.writeln('$argType $argName = $accessor;');
if (!arg.type.isNullable) {
Expand All @@ -646,7 +646,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
method.returnType.isVoid ? 'null' : 'result';
const String resultName = 'resultCallback';
indent.format('''
Result<$returnType> $resultName =
Result<$returnType> $resultName =
\t\tnew Result<$returnType>() {
\t\t\tpublic void success($returnType result) {
\t\t\t\twrapped.add(0, $resultValue);
Expand Down Expand Up @@ -871,6 +871,13 @@ String _nullsafeJavaTypeForDartType(TypeDeclaration type) {
return '$nullSafe${_javaTypeForDartType(type)}';
}

/// Returns an expression to cast [variable] to [javaType].
String _cast(String variable, {required String javaType}) {
// Special-case Object, since casting to Object doesn't do anything, and
// causes a warning.
return javaType == 'Object' ? variable : '($javaType) $variable';
}

/// Casts variable named [varName] to the correct host datatype for [field].
/// This is for use in codecs where we may have a map representation of an
/// object.
Expand All @@ -884,6 +891,6 @@ String _castObject(
classes.map((Class x) => x.name).contains(field.type.baseName)) {
return '($varName == null) ? null : ${hostDatatype.datatype}.fromList((ArrayList<Object>) $varName)';
} else {
return '(${hostDatatype.datatype}) $varName';
return _cast(varName, javaType: hostDatatype.datatype);
}
}
35 changes: 25 additions & 10 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
final HostDatatype hostDatatype = _getHostDatatype(root, field);
String toWriteValue = '';
final String fieldName = field.name;
final String safeCall = field.type.isNullable ? '?' : '';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixes warnings about unnecessary safe calls on non-nullable values.

if (!hostDatatype.isBuiltin &&
customClassNames.contains(field.type.baseName)) {
toWriteValue = '$fieldName?.toList()';
toWriteValue = '$fieldName$safeCall.toList()';
} else if (!hostDatatype.isBuiltin &&
customEnumNames.contains(field.type.baseName)) {
toWriteValue = '$fieldName?.raw';
toWriteValue = '$fieldName$safeCall.raw';
} else {
toWriteValue = fieldName;
}
Expand Down Expand Up @@ -244,7 +245,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.addln(
'.let { if (it is Int) it.toLong() else it as Long? }');
} else {
indent.writeln('val ${field.name} = $listValue as $fieldType?');
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: '$fieldType?')}');
}
} else {
if (!hostDatatype.isBuiltin &&
Expand All @@ -260,7 +262,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent
.addln('.let { if (it is Int) it.toLong() else it as Long }');
} else {
indent.writeln('val ${field.name} = $listValue as $fieldType');
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType)}');
}
}
});
Expand Down Expand Up @@ -380,13 +383,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeln('callback()');
});
} else {
final String forceUnwrap = func.returnType.isNullable ? '?' : '';
indent.addScoped('{', '}', () {
if (func.returnType.baseName == 'int') {
final String forceUnwrap =
func.returnType.isNullable ? '?' : '';
indent.writeln(
'val result = if (it is Int) it.toLong() else it as$forceUnwrap Long');
} else {
indent.writeln('val result = it as$forceUnwrap $returnType');
indent.writeln(
'val result = ${_cast('it', kotlinType: returnType, safeCast: func.returnType.isNullable)}');
}
indent.writeln('callback(result)');
});
Expand Down Expand Up @@ -507,7 +512,6 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {

indent.write('channel.setMessageHandler ');
indent.addScoped('{ $messageVarName, reply ->', '}', () {
indent.writeln('var wrapped = listOf<Any?>()');
final List<String> methodArguments = <String>[];
if (method.arguments.isNotEmpty) {
indent.writeln('val args = message as List<Any?>');
Expand Down Expand Up @@ -543,6 +547,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
});
} else {
indent.writeln('var wrapped: List<Any?>');
tarrinneal marked this conversation as resolved.
Show resolved Hide resolved
indent.write('try ');
indent.addScoped('{', '}', () {
if (method.returnType.isVoid) {
Expand Down Expand Up @@ -669,15 +674,15 @@ String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
type.isNullable ? '$value == null ? null : ' : '';
return '$nullableConditionPrefix${_kotlinTypeForDartType(type)}.ofRaw($value as Int)$forceUnwrap';
} else {
final String castUnwrap = type.isNullable ? '?' : '';

// The StandardMessageCodec can give us [Integer, Long] for
// a Dart 'int'. To keep things simple we just use 64bit
// longs in Pigeon with Kotlin.
if (type.baseName == 'int') {
final String castUnwrap = type.isNullable ? '?' : '';
return '$value.let { if (it is Int) it.toLong() else it as$castUnwrap Long }';
} else {
return '$value as$castUnwrap ${_kotlinTypeForDartType(type)}';
return _cast(value,
kotlinType: _kotlinTypeForDartType(type), safeCast: type.isNullable);
}
}
}
Expand Down Expand Up @@ -741,3 +746,13 @@ String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
final String nullSafe = type.isNullable ? '?' : '';
return '${_kotlinTypeForDartType(type)}$nullSafe';
}

/// Returns an expression to cast [variable] to [kotlinType].
String _cast(String variable,
{required String kotlinType, bool safeCast = false}) {
// Special-case Any, since no-op casts cause warnings.
if (kotlinType == 'Any?' || (safeCast && kotlinType == 'Any')) {
return variable;
}
return '$variable as${safeCast ? '?' : ''} $kotlinType';
}
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
// ignore_for_file: avoid_relative_lib_imports
Expand Down
30 changes: 0 additions & 30 deletions packages/pigeon/pigeons/android_unittests.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ android {
testImplementation "org.mockito:mockito-core:5.+"
}
}

project.getTasks().withType(JavaCompile){
options.compilerArgs.addAll(["-Xlint:all", "-Werror"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package com.example.alternate_language_test_plugin;
Expand Down Expand Up @@ -3239,7 +3239,7 @@ public void throwError(Reply<Object> callback) {
null,
channelReply -> {
@SuppressWarnings("ConstantConditions")
Object output = (Object) channelReply;
Object output = channelReply;
callback.reply(output);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void nullValues() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
ByteBuffer replyData =
Expand Down Expand Up @@ -101,6 +102,7 @@ public void hasValues() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
ByteBuffer replyData =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void asyncSuccess() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
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 had to fix a bunch of raw type warnings in our tests, and suppress a bunch of unchecked casts for most of the same lines.

ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() == 1);
didCall[0] = true;
});
Expand All @@ -88,7 +88,7 @@ public void asyncError() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() > 1);
assertEquals("java.lang.Exception: error", (String) wrapped.get(0));
didCall[0] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public void listInList() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args = (ArrayList) FlutterSmallApi.getCodec().decodeMessage(message);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterSmallApi.getCodec().decodeMessage(message);
ByteBuffer replyData = FlutterSmallApi.getCodec().encodeMessage(args.get(0));
replyData.position(0);
reply.reply(replyData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void subtract() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) MultipleArityFlutterApi.getCodec().decodeMessage(message);
Long arg0 = (Long) args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void nullArgHostApi() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() == 1);
});
}
Expand All @@ -52,8 +52,9 @@ public void nullArgFlutterApi() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args =
(ArrayList)
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>)
NullableReturns.NullableArgFlutterApi.getCodec().decodeMessage(message);
assertNull(args.get(0));
ByteBuffer replyData =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void errorMessage() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList error = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> error = (ArrayList<Object>) codec.decodeMessage(bytes);
assertNotNull(error.get(0));
assertNotNull(error.get(1));
String details = (String) error.get(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ private static BinaryMessenger makeMockBinaryMessenger() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args = (ArrayList) PrimitiveFlutterApi.getCodec().decodeMessage(message);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) PrimitiveFlutterApi.getCodec().decodeMessage(message);
Object arg = args.get(0);
if (arg instanceof Long) {
Long longArg = (Long) arg;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void primitiveIntHostApi() {
.setMessageHandler(eq("dev.flutter.pigeon.PrimitiveHostApi.anInt"), handler.capture());
MessageCodec<Object> codec = PrimitiveHostApi.getCodec();
@SuppressWarnings("unchecked")
ByteBuffer message = codec.encodeMessage(new ArrayList(Arrays.asList((Integer) 1)));
ByteBuffer message = codec.encodeMessage(new ArrayList<Object>(Arrays.asList((Integer) 1)));
message.rewind();
handler
.getValue()
Expand All @@ -97,7 +99,7 @@ public void primitiveIntHostApi() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() > 0);
assertEquals(1L, ((Long) wrapped.get(0)).longValue());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)

target.build_configurations.each do |config|
config.build_settings['GCC_TREAT_WARNINGS_AS_ERRORS'] = 'YES'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import <Foundation/Foundation.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import "CoreTests.gen.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import

Expand Down
Loading