Skip to content

Commit 1ebf3df

Browse files
johnniwintherCommit Queue
authored andcommitted
[kernel] Add tool for checking AST equivalence
Change-Id: Ie06776203080e91346582534af2d56c24581bd54 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413200 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 0060b0f commit 1ebf3df

File tree

5 files changed

+818
-28
lines changed

5 files changed

+818
-28
lines changed

pkg/front_end/tool/generate_ast_equivalence.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ class EquivalenceVisitorStrategy extends Visitor1Strategy {
150150
registerAstClassEquivalence(utilityFieldType.astClass);
151151
sb.writeln('''($thisName, $otherName, _) {
152152
if (identical($thisName, $otherName)) return true;
153-
if ($thisName is! ${utilityFieldType.astClass.name}) return false;
154-
if ($otherName is! ${utilityFieldType.astClass.name}) return false;
155153
return ${classCheckName(utilityFieldType.astClass)}(
156154
visitor,
157155
$thisName,
@@ -209,8 +207,6 @@ class EquivalenceVisitorStrategy extends Visitor1Strategy {
209207
registerAstClassEquivalence(utilityFieldType.astClass);
210208
sb.writeln('''($thisName, $otherName, _) {
211209
if (identical($thisName, $otherName)) return true;
212-
if ($thisName is! ${utilityFieldType.astClass.name}) return false;
213-
if ($otherName is! ${utilityFieldType.astClass.name}) return false;
214210
return ${classCheckName(utilityFieldType.astClass)}(
215211
visitor,
216212
$thisName,
@@ -625,7 +621,7 @@ class $visitorName$visitorTypeParameters
625621
bool $checkLists<E>(
626622
List<E>? a,
627623
List<E>? b,
628-
bool Function(E?, E?, String) equivalentValues,
624+
bool Function(E, E, String) equivalentValues,
629625
[String propertyName = '']) {
630626
if (identical(a, b)) return true;
631627
if (a == null || b == null) return false;
@@ -651,8 +647,8 @@ class $visitorName$visitorTypeParameters
651647
bool $checkSets<E>(
652648
Set<E>? a,
653649
Set<E>? b,
654-
bool Function(E?, E?) matchingValues,
655-
bool Function(E?, E?, String) equivalentValues,
650+
bool Function(E, E) matchingValues,
651+
bool Function(E, E, String) equivalentValues,
656652
[String propertyName = '']) {
657653
if (identical(a, b)) return true;
658654
if (a == null || b == null) return false;
@@ -702,9 +698,9 @@ class $visitorName$visitorTypeParameters
702698
bool $checkMaps<K, V>(
703699
Map<K, V>? a,
704700
Map<K, V>? b,
705-
bool Function(K?, K?) matchingKeys,
706-
bool Function(K?, K?, String) equivalentKeys,
707-
bool Function(V?, V?, String) equivalentValues,
701+
bool Function(K, K) matchingKeys,
702+
bool Function(K, K, String) equivalentKeys,
703+
bool Function(V, V, String) equivalentValues,
708704
[String propertyName = '']) {
709705
if (identical(a, b)) return true;
710706
if (a == null || b == null) return false;
@@ -733,7 +729,7 @@ class $visitorName$visitorTypeParameters
733729
}
734730
if (hasFoundKey) {
735731
bKeys.remove(foundKey);
736-
if (!equivalentValues(a[aKey], b[foundKey],
732+
if (!equivalentValues(a[aKey]!, b[foundKey]!,
737733
'\${propertyName}[\${aKey}]')) {
738734
return false;
739735
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env dart
2+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
3+
// for details. All rights reserved. Use of this source code is governed by a
4+
// BSD-style license that can be found in the LICENSE file.
5+
6+
export 'package:kernel/src/tool/check_equivalence.dart';

pkg/kernel/lib/src/equivalence.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ class EquivalenceVisitor implements Visitor1<bool, Node> {
12531253
/// If run in a checking state, the [propertyName] is used for registering
12541254
/// inequivalences.
12551255
bool checkLists<E>(
1256-
List<E>? a, List<E>? b, bool Function(E?, E?, String) equivalentValues,
1256+
List<E>? a, List<E>? b, bool Function(E, E, String) equivalentValues,
12571257
[String propertyName = '']) {
12581258
if (identical(a, b)) return true;
12591259
if (a == null || b == null) return false;
@@ -1276,8 +1276,8 @@ class EquivalenceVisitor implements Visitor1<bool, Node> {
12761276
///
12771277
/// If run in a checking state, the [propertyName] is used for registering
12781278
/// inequivalences.
1279-
bool checkSets<E>(Set<E>? a, Set<E>? b, bool Function(E?, E?) matchingValues,
1280-
bool Function(E?, E?, String) equivalentValues,
1279+
bool checkSets<E>(Set<E>? a, Set<E>? b, bool Function(E, E) matchingValues,
1280+
bool Function(E, E, String) equivalentValues,
12811281
[String propertyName = '']) {
12821282
if (identical(a, b)) return true;
12831283
if (a == null || b == null) return false;
@@ -1325,9 +1325,9 @@ class EquivalenceVisitor implements Visitor1<bool, Node> {
13251325
bool checkMaps<K, V>(
13261326
Map<K, V>? a,
13271327
Map<K, V>? b,
1328-
bool Function(K?, K?) matchingKeys,
1329-
bool Function(K?, K?, String) equivalentKeys,
1330-
bool Function(V?, V?, String) equivalentValues,
1328+
bool Function(K, K) matchingKeys,
1329+
bool Function(K, K, String) equivalentKeys,
1330+
bool Function(V, V, String) equivalentValues,
13311331
[String propertyName = '']) {
13321332
if (identical(a, b)) return true;
13331333
if (a == null || b == null) return false;
@@ -1355,7 +1355,7 @@ class EquivalenceVisitor implements Visitor1<bool, Node> {
13551355
if (hasFoundKey) {
13561356
bKeys.remove(foundKey);
13571357
if (!equivalentValues(
1358-
a[aKey], b[foundKey], '${propertyName}[${aKey}]')) {
1358+
a[aKey]!, b[foundKey]!, '${propertyName}[${aKey}]')) {
13591359
return false;
13601360
}
13611361
} else {
@@ -5716,8 +5716,6 @@ class EquivalenceStrategy {
57165716
EquivalenceVisitor visitor, MapConstant node, MapConstant other) {
57175717
return visitor.checkLists(node.entries, other.entries, (a, b, _) {
57185718
if (identical(a, b)) return true;
5719-
if (a is! ConstantMapEntry) return false;
5720-
if (b is! ConstantMapEntry) return false;
57215719
return checkConstantMapEntry(visitor, a, b);
57225720
}, 'entries');
57235721
}
@@ -5902,8 +5900,6 @@ class EquivalenceStrategy {
59025900
return visitor.checkMaps(node.uriToSource, other.uriToSource,
59035901
visitor.matchValues, visitor.checkValues, (a, b, _) {
59045902
if (identical(a, b)) return true;
5905-
if (a is! Source) return false;
5906-
if (b is! Source) return false;
59075903
return checkSource(visitor, a, b);
59085904
}, 'uriToSource');
59095905
}
@@ -5914,8 +5910,6 @@ class EquivalenceStrategy {
59145910
node.metadata, other.metadata, visitor.matchValues, visitor.checkValues,
59155911
(a, b, _) {
59165912
if (identical(a, b)) return true;
5917-
if (a is! MetadataRepository) return false;
5918-
if (b is! MetadataRepository) return false;
59195913
return checkMetadataRepository(visitor, a, b);
59205914
}, 'metadata');
59215915
}
@@ -6089,8 +6083,6 @@ class EquivalenceStrategy {
60896083
return visitor.checkLists(node.memberDescriptors, other.memberDescriptors,
60906084
(a, b, _) {
60916085
if (identical(a, b)) return true;
6092-
if (a is! ExtensionMemberDescriptor) return false;
6093-
if (b is! ExtensionMemberDescriptor) return false;
60946086
return checkExtensionMemberDescriptor(visitor, a, b);
60956087
}, 'memberDescriptors');
60966088
}
@@ -6192,8 +6184,6 @@ class EquivalenceStrategy {
61926184
return visitor.checkLists(node.memberDescriptors, other.memberDescriptors,
61936185
(a, b, _) {
61946186
if (identical(a, b)) return true;
6195-
if (a is! ExtensionTypeMemberDescriptor) return false;
6196-
if (b is! ExtensionTypeMemberDescriptor) return false;
61976187
return checkExtensionTypeMemberDescriptor(visitor, a, b);
61986188
}, 'memberDescriptors');
61996189
}

0 commit comments

Comments
 (0)