Skip to content

Commit 16ef3ee

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
Fix LSP equality checks for lists/maps within unions
Change-Id: I7ec8b591375dfb4d75f802839b89c1288cd4cfef Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151236 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 3b243a0 commit 16ef3ee

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/analysis_server/lib/lsp_protocol/protocol_special.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66

77
import 'package:analysis_server/lsp_protocol/protocol_generated.dart';
88
import 'package:analysis_server/src/lsp/json_parsing.dart';
9+
import 'package:analysis_server/src/protocol/protocol_internal.dart';
910
import 'package:analyzer/src/generated/utilities_general.dart';
1011

1112
const jsonRpcVersion = '2.0';
@@ -20,6 +21,18 @@ ErrorOr<R> error<R>(ErrorCodes code, String message, [String data]) =>
2021

2122
ErrorOr<R> failure<R>(ErrorOr<dynamic> error) => ErrorOr<R>.error(error.error);
2223

24+
/// Returns if two objects are equal, recursively checking items in
25+
/// Maps/Lists.
26+
bool lspEquals(dynamic obj1, dynamic obj2) {
27+
if (obj1 is List && obj2 is List) {
28+
return listEqual(obj1, obj2, lspEquals);
29+
} else if (obj1 is Map && obj2 is Map) {
30+
return mapEqual(obj1, obj2, lspEquals);
31+
} else {
32+
return obj1.runtimeType == obj2.runtimeType && obj1 == obj2;
33+
}
34+
}
35+
2336
/// Returns an objects hash code, recursively combining hashes for items in
2437
/// Maps/Lists.
2538
int lspHashCode(dynamic obj) {
@@ -69,7 +82,8 @@ class Either2<T1, T2> {
6982
int get hashCode => map(lspHashCode, lspHashCode);
7083

7184
@override
72-
bool operator ==(o) => o is Either2<T1, T2> && o._t1 == _t1 && o._t2 == _t2;
85+
bool operator ==(o) =>
86+
o is Either2<T1, T2> && lspEquals(o._t1, _t1) && lspEquals(o._t2, _t2);
7387

7488
T map<T>(T Function(T1) f1, T Function(T2) f2) {
7589
return _which == 1 ? f1(_t1) : f2(_t2);
@@ -108,7 +122,10 @@ class Either3<T1, T2, T3> {
108122

109123
@override
110124
bool operator ==(o) =>
111-
o is Either3<T1, T2, T3> && o._t1 == _t1 && o._t2 == _t2 && o._t3 == _t3;
125+
o is Either3<T1, T2, T3> &&
126+
lspEquals(o._t1, _t1) &&
127+
lspEquals(o._t2, _t2) &&
128+
lspEquals(o._t3, _t3);
112129

113130
T map<T>(T Function(T1) f1, T Function(T2) f2, T Function(T3) f3) {
114131
switch (_which) {
@@ -170,10 +187,10 @@ class Either4<T1, T2, T3, T4> {
170187
@override
171188
bool operator ==(o) =>
172189
o is Either4<T1, T2, T3, T4> &&
173-
o._t1 == _t1 &&
174-
o._t2 == _t2 &&
175-
o._t3 == _t3 &&
176-
o._t4 == _t4;
190+
lspEquals(o._t1, _t1) &&
191+
lspEquals(o._t2, _t2) &&
192+
lspEquals(o._t3, _t3) &&
193+
lspEquals(o._t4, _t4);
177194

178195
T map<T>(T Function(T1) f1, T Function(T2) f2, T Function(T3) f3,
179196
T Function(T4) f4) {

pkg/analysis_server/test/tool/lsp_spec/generated_classes_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ void main() {
4848
expect(a.hashCode, equals(b.hashCode));
4949
});
5050

51+
test('with unions of lists can be checked for equality', () {
52+
final a = Either2<List<String>, List<int>>.t1(['test']);
53+
final b = Either2<List<String>, List<int>>.t1(['test']);
54+
55+
expect(a, equals(b));
56+
expect(a.hashCode, equals(b.hashCode));
57+
});
58+
5159
test('with union fields can be checked for equality', () {
5260
final a =
5361
SignatureInformation('a', Either2<String, MarkupContent>.t1('a'), []);

0 commit comments

Comments
 (0)