diff --git a/built_value_generator/lib/src/value_source_class.dart b/built_value_generator/lib/src/value_source_class.dart index 6fa1fa4c..8f6fba6e 100644 --- a/built_value_generator/lib/src/value_source_class.dart +++ b/built_value_generator/lib/src/value_source_class.dart @@ -1354,7 +1354,10 @@ abstract class ValueSourceClass result.writeln('bool operator==(Object other) {'); result.writeln(' if (identical(other, this)) return true;'); - if (comparedFunctionFields.isNotEmpty) { + var needsDynamic = + comparedFunctionFields.isNotEmpty && genericParameters.isNotEmpty; + + if (needsDynamic) { result.writeln(' final dynamic _\$dynamicOther = other;'); } result.writeln(' return other is $name${forBuilder ? 'Builder' : ''}'); @@ -1364,7 +1367,7 @@ abstract class ValueSourceClass comparedFields.map((field) { var nameOrThisDotName = field.name == 'other' ? 'this.other' : field.name; - return field.isFunctionType + return needsDynamic && field.isFunctionType ? '$nameOrThisDotName == _\$dynamicOther.${field.name}' : '$nameOrThisDotName == other.${field.name}'; }).join('&&'), diff --git a/built_value_generator/test/built_value_generator_test.dart b/built_value_generator/test/built_value_generator_test.dart index bc40aa81..74d379d3 100644 --- a/built_value_generator/test/built_value_generator_test.dart +++ b/built_value_generator/test/built_value_generator_test.dart @@ -880,6 +880,37 @@ abstract class NestedValue implements Built { '1. Make builder field nestedValue have `nestedBuilder: false` in order to use `comparableBuilders: true`.')); }); + test('uses dynamic for equality check on function fields with generics', + () async { + expect( + await generate('''library value; +import 'package:built_value/built_value.dart'; +part 'value.g.dart'; +abstract class Value implements Built, ValueBuilder> { + Value._(); + factory Value([void Function(ValueBuilder) updates]) = _\$Value; + void Function(T) get callback; +} +'''), + contains(r'final dynamic _$dynamicOther = other;')); + }); + + test( + 'does not use dynamic for equality check on function fields without generics', + () async { + expect( + await generate('''library value; +import 'package:built_value/built_value.dart'; +part 'value.g.dart'; +abstract class Value implements Built { + Value._(); + factory Value([void Function(ValueBuilder) updates]) = _\$Value; + void Function(int) get callback; +} +'''), + isNot(contains(r'dynamicOther'))); + }); + test('cleans generated class names for private classes', () async { expect( await generate('''library value; diff --git a/end_to_end_test/lib/mixins.g.dart b/end_to_end_test/lib/mixins.g.dart index 80d0b04b..dba363e9 100644 --- a/end_to_end_test/lib/mixins.g.dart +++ b/end_to_end_test/lib/mixins.g.dart @@ -46,8 +46,7 @@ class _$UsesMixin extends UsesMixin { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; - return other is UsesMixin && typeDef == _$dynamicOther.typeDef; + return other is UsesMixin && typeDef == other.typeDef; } @override diff --git a/end_to_end_test/lib/records.g.dart b/end_to_end_test/lib/records.g.dart index 7fd1cb9d..4d2740cc 100644 --- a/end_to_end_test/lib/records.g.dart +++ b/end_to_end_test/lib/records.g.dart @@ -97,8 +97,7 @@ class _$SimpleRecordValue extends SimpleRecordValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; - return other is SimpleRecordValue && record == _$dynamicOther.record; + return other is SimpleRecordValue && record == other.record; } @override @@ -225,20 +224,19 @@ class _$ComplexRecordValue extends ComplexRecordValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; return other is ComplexRecordValue && - record2 == _$dynamicOther.record2 && - record2p == _$dynamicOther.record2p && - record2n == _$dynamicOther.record2n && - record3 == _$dynamicOther.record3 && - record3p == _$dynamicOther.record3p && - record3n == _$dynamicOther.record3n && - record4 == _$dynamicOther.record4 && - record4p == _$dynamicOther.record4p && - record4n == _$dynamicOther.record4n && - record5 == _$dynamicOther.record5 && - record5p == _$dynamicOther.record5p && - record5n == _$dynamicOther.record5n; + record2 == other.record2 && + record2p == other.record2p && + record2n == other.record2n && + record3 == other.record3 && + record3p == other.record3p && + record3n == other.record3n && + record4 == other.record4 && + record4p == other.record4p && + record4n == other.record4n && + record5 == other.record5 && + record5p == other.record5p && + record5n == other.record5n; } @override @@ -456,11 +454,10 @@ class _$SerializableRecordValue extends SerializableRecordValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; return other is SerializableRecordValue && value == other.value && - record == _$dynamicOther.record && - intOrList == _$dynamicOther.intOrList; + record == other.record && + intOrList == other.intOrList; } @override diff --git a/end_to_end_test/lib/values.g.dart b/end_to_end_test/lib/values.g.dart index a7c391d1..6f82ab9b 100644 --- a/end_to_end_test/lib/values.g.dart +++ b/end_to_end_test/lib/values.g.dart @@ -4316,8 +4316,7 @@ class _$FunctionValue extends FunctionValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; - return other is FunctionValue && function == _$dynamicOther.function; + return other is FunctionValue && function == other.function; } @override @@ -4400,9 +4399,7 @@ class _$ListOfFunctionValue extends ListOfFunctionValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; - return other is ListOfFunctionValue && - functions == _$dynamicOther.functions; + return other is ListOfFunctionValue && functions == other.functions; } @override @@ -7035,21 +7032,19 @@ class _$VariousFunctionsValue extends VariousFunctionsValue { @override bool operator ==(Object other) { if (identical(other, this)) return true; - final dynamic _$dynamicOther = other; return other is VariousFunctionsValue && bareFunction == other.bareFunction && - positionalFunction == _$dynamicOther.positionalFunction && - optionalFunction == _$dynamicOther.optionalFunction && - positionalNamedFunction == _$dynamicOther.positionalNamedFunction && - namedFunction == _$dynamicOther.namedFunction && - requiredNamedFunction == _$dynamicOther.requiredNamedFunction && + positionalFunction == other.positionalFunction && + optionalFunction == other.optionalFunction && + positionalNamedFunction == other.positionalNamedFunction && + namedFunction == other.namedFunction && + requiredNamedFunction == other.requiredNamedFunction && mixinBareFunction == other.mixinBareFunction && - mixinPositionalFunction == _$dynamicOther.mixinPositionalFunction && - mixinOptionalFunction == _$dynamicOther.mixinOptionalFunction && - mixinPositionalNamedFunction == - _$dynamicOther.mixinPositionalNamedFunction && - mixinNamedFunction == _$dynamicOther.mixinNamedFunction && - mixinRequiredNamedFunction == _$dynamicOther.mixinRequiredNamedFunction; + mixinPositionalFunction == other.mixinPositionalFunction && + mixinOptionalFunction == other.mixinOptionalFunction && + mixinPositionalNamedFunction == other.mixinPositionalNamedFunction && + mixinNamedFunction == other.mixinNamedFunction && + mixinRequiredNamedFunction == other.mixinRequiredNamedFunction; } @override