Skip to content

Commit

Permalink
[flow analysis] Remove _typeContains method.
Browse files Browse the repository at this point in the history
It wasn't necessary; `List.contains` does the same thing.

Also, remove the plumbing for `typeOperations`, a parameter of
`_typeContains` that was not used.

Change-Id: I688835512e58cb7a24336318b2006c9913c77888
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398300
Auto-Submit: Paul Berry <[email protected]>
Reviewed-by: Kallen Tu <[email protected]>
Commit-Queue: Kallen Tu <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Dec 2, 2024
1 parent 56b6e5b commit 0197bea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
48 changes: 17 additions & 31 deletions pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2400,8 +2400,8 @@ class FlowModel<Type extends Object> {
PromotionModel<Type>? otherPromotionModel = right?.model;
PromotionModel<Type> newPromotionModel = otherPromotionModel == null
? promotionModel
: PromotionModel.inheritTested(helper.typeOperations, promotionModel,
otherPromotionModel.tested);
: PromotionModel.inheritTested(
promotionModel, otherPromotionModel.tested);
if (!identical(newPromotionModel, promotionModel)) {
result =
result.updatePromotionInfo(helper, promotionKey, newPromotionModel);
Expand Down Expand Up @@ -2496,8 +2496,8 @@ class FlowModel<Type extends Object> {
}
// Tests are kept regardless of whether they are in `this` model or the
// new base model.
List<Type> newTested = PromotionModel.joinTested(
thisModel.tested, baseModel.tested, helper.typeOperations);
List<Type> newTested =
PromotionModel.joinTested(thisModel.tested, baseModel.tested);
// The variable is definitely assigned if it was definitely assigned
// either in `this` model or the new base model.
bool newAssigned = thisModel.assigned || baseModel.assigned;
Expand Down Expand Up @@ -2720,8 +2720,7 @@ class FlowModel<Type extends Object> {
Type? promotedType) {
List<Type> newTested = info.tested;
if (testedType != null) {
newTested = PromotionModel._addTypeToUniqueList(
info.tested, testedType, helper.typeOperations);
newTested = PromotionModel._addTypeToUniqueList(info.tested, testedType);
}

List<Type>? newPromotedTypes = info.promotedTypes;
Expand Down Expand Up @@ -3321,7 +3320,7 @@ class PromotionModel<Type extends Object> {
}

// Add only unique candidates.
if (!_typeListContains(typeOperations, candidates!, type)) {
if (!candidates!.contains(type)) {
candidates!.add(type);
return;
}
Expand Down Expand Up @@ -3390,10 +3389,8 @@ class PromotionModel<Type extends Object> {
/// regardless of the type of loop.
@visibleForTesting
static PromotionModel<Type> inheritTested<Type extends Object>(
FlowAnalysisTypeOperations<Type> typeOperations,
PromotionModel<Type> model,
List<Type> tested) {
List<Type> newTested = joinTested(tested, model.tested, typeOperations);
PromotionModel<Type> model, List<Type> tested) {
List<Type> newTested = joinTested(tested, model.tested);
if (identical(newTested, model.tested)) return model;
return new PromotionModel<Type>(
promotedTypes: model.promotedTypes,
Expand Down Expand Up @@ -3429,9 +3426,8 @@ class PromotionModel<Type extends Object> {
bool newAssigned = first.assigned && second.assigned;
bool newUnassigned = first.unassigned && second.unassigned;
bool newWriteCaptured = first.writeCaptured || second.writeCaptured;
List<Type> newTested = newWriteCaptured
? const []
: joinTested(first.tested, second.tested, typeOperations);
List<Type> newTested =
newWriteCaptured ? const [] : joinTested(first.tested, second.tested);
SsaNode<Type>? newSsaNode = propertySsaNode;
if (newSsaNode == null && !newWriteCaptured) {
(newSsaNode, newFlowModel) = SsaNode._join(
Expand Down Expand Up @@ -3500,8 +3496,8 @@ class PromotionModel<Type extends Object> {
/// small in real-world cases)
/// - The sense of equality for the union operation is determined by `==`.
/// - The types of interests lists are considered immutable.
static List<Type> joinTested<Type extends Object>(List<Type> types1,
List<Type> types2, FlowAnalysisTypeOperations<Type> typeOperations) {
static List<Type> joinTested<Type extends Object>(
List<Type> types1, List<Type> types2) {
// Ensure that types1 is the shorter list.
if (types1.length > types2.length) {
List<Type> tmp = types1;
Expand All @@ -3517,11 +3513,11 @@ class PromotionModel<Type extends Object> {
// not present in it.
for (int i = shared; i < types1.length; i++) {
Type typeToAdd = types1[i];
if (_typeListContains(typeOperations, types2, typeToAdd)) continue;
if (types2.contains(typeToAdd)) continue;
List<Type> result = types2.toList()..add(typeToAdd);
for (i++; i < types1.length; i++) {
typeToAdd = types1[i];
if (_typeListContains(typeOperations, types2, typeToAdd)) continue;
if (types2.contains(typeToAdd)) continue;
result.add(typeToAdd);
}
return result;
Expand Down Expand Up @@ -3575,9 +3571,9 @@ class PromotionModel<Type extends Object> {
? [promoted]
: (promotedTypes.toList()..add(promoted));

static List<Type> _addTypeToUniqueList<Type extends Object>(List<Type> types,
Type newType, FlowAnalysisTypeOperations<Type> typeOperations) {
if (_typeListContains(typeOperations, types, newType)) return types;
static List<Type> _addTypeToUniqueList<Type extends Object>(
List<Type> types, Type newType) {
if (types.contains(newType)) return types;
return new List<Type>.of(types)..add(newType);
}

Expand Down Expand Up @@ -3612,16 +3608,6 @@ class PromotionModel<Type extends Object> {
ssaNode: newSsaNode);
}
}

static bool _typeListContains<Type extends Object>(
FlowAnalysisTypeOperations<Type> typeOperations,
List<Type> list,
Type searchType) {
for (Type type in list) {
if (type == searchType) return true;
}
return false;
}
}

/// Non-promotion reason describing the situation where an expression was not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4846,33 +4846,33 @@ main() {
var s1 = _makeTypes(['double', 'int']);
var s2 = _makeTypes(['double', 'int', 'bool']);
var expected = _matchOfInterestSet(['double', 'int', 'bool']);
expect(PromotionModel.joinTested(s1, s2, h.typeOperations), expected);
expect(PromotionModel.joinTested(s2, s1, h.typeOperations), expected);
expect(PromotionModel.joinTested(s1, s2), expected);
expect(PromotionModel.joinTested(s2, s1), expected);
});

test('common prefix', () {
var s1 = _makeTypes(['double', 'int', 'String']);
var s2 = _makeTypes(['double', 'int', 'bool']);
var expected = _matchOfInterestSet(['double', 'int', 'String', 'bool']);
expect(PromotionModel.joinTested(s1, s2, h.typeOperations), expected);
expect(PromotionModel.joinTested(s2, s1, h.typeOperations), expected);
expect(PromotionModel.joinTested(s1, s2), expected);
expect(PromotionModel.joinTested(s2, s1), expected);
});

test('order mismatch', () {
var s1 = _makeTypes(['double', 'int']);
var s2 = _makeTypes(['int', 'double']);
var expected = _matchOfInterestSet(['double', 'int']);
expect(PromotionModel.joinTested(s1, s2, h.typeOperations), expected);
expect(PromotionModel.joinTested(s2, s1, h.typeOperations), expected);
expect(PromotionModel.joinTested(s1, s2), expected);
expect(PromotionModel.joinTested(s2, s1), expected);
});

test('small common prefix', () {
var s1 = _makeTypes(['int', 'double', 'String', 'bool']);
var s2 = _makeTypes(['int', 'List', 'bool', 'Future']);
var expected = _matchOfInterestSet(
['int', 'double', 'String', 'bool', 'List', 'Future']);
expect(PromotionModel.joinTested(s1, s2, h.typeOperations), expected);
expect(PromotionModel.joinTested(s2, s1, h.typeOperations), expected);
expect(PromotionModel.joinTested(s1, s2), expected);
expect(PromotionModel.joinTested(s2, s1), expected);
});
});

Expand Down

0 comments on commit 0197bea

Please sign in to comment.