Skip to content

Commit 3199e65

Browse files
stereotype441Commit Bot
authored and
Commit Bot
committed
Fix logic error causing all unnamed parameters have index 0.
In the definition of `_computeExplicitlyTypedParameterSet`, I accidentally nested the declaration of `unnamedParameterIndex` inside the `for` loop, defeating the increment and causing all parameters to be considered to have index 0. I've included a test case that would have caught the mistake. Bug: dart-lang/language#731 Change-Id: I0cd0e1e5b481313150e495d370af2477253d6637 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242741 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 08c6045 commit 3199e65

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

pkg/analyzer/lib/src/dart/resolver/invocation_inferrer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Set<Object> _computeExplicitlyTypedParameterSet(
2323
List<FormalParameter> parameters =
2424
functionExpression.parameters?.parameters ?? const [];
2525
Set<Object> result = {};
26+
int unnamedParameterIndex = 0;
2627
for (var formalParameter in parameters) {
27-
int unnamedParameterIndex = 0;
2828
var key = formalParameter.isNamed
2929
? formalParameter.identifier?.name ?? ''
3030
: unnamedParameterIndex++;

pkg/analyzer/test/src/dart/resolution/type_inference/inference_update_1_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ test() {
110110
_isEnabled ? 'List<int>' : 'List<dynamic>');
111111
}
112112

113+
test_horizontal_inference_necessary_due_to_wrong_explicit_parameter_type() async {
114+
// In this example, horizontal type inference is needed because although the
115+
// type of `y` is explicit, it's actually `x` that would have needed to be
116+
// explicit.
117+
await assertErrorsInCode('''
118+
test(List<int> list) {
119+
var a = list.fold(0, (x, int y) => x + y);
120+
}
121+
''', [
122+
error(HintCode.UNUSED_LOCAL_VARIABLE, 29, 1),
123+
if (!_isEnabled)
124+
error(
125+
CompileTimeErrorCode
126+
.UNCHECKED_OPERATOR_INVOCATION_OF_NULLABLE_VALUE,
127+
62,
128+
1),
129+
]);
130+
assertType(findElement.localVar('a').type, _isEnabled ? 'int' : 'dynamic');
131+
assertType(findElement.parameter('x').type, _isEnabled ? 'int' : 'Object?');
132+
assertType(findElement.parameter('y').type, 'int');
133+
expect(findNode.binary('+ y').staticElement?.enclosingElement.name,
134+
_isEnabled ? 'num' : null);
135+
}
136+
113137
test_horizontal_inference_propagate_to_earlier_closure() async {
114138
await assertErrorsInCode('''
115139
U f<T, U>(U Function(T) g, T Function() h) => throw '';

tests/language/inference_update_1/horizontal_inference_enabled_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ testDependencyCycle(Map<T, U> Function<T, U>(T Function(U), U Function(T)) f) {
8484
.expectStaticType<Exactly<Map<List<Object?>, Set<Object?>>>>();
8585
}
8686

87+
testNecessaryDueToWrongExplicitParameterType(List<int> list) {
88+
var a = list.fold(
89+
0,
90+
(x, int y) =>
91+
(x..expectStaticType<Exactly<int>>()) +
92+
(y..expectStaticType<Exactly<int>>()));
93+
a.expectStaticType<Exactly<int>>();
94+
}
95+
8796
testPropagateFromContravariantReturnType(
8897
U Function<T, U>(void Function(T) Function(), U Function(T)) f) {
8998
f(() => (int i) {}, (x) => [x]..expectStaticType<Exactly<List<int>>>())

0 commit comments

Comments
 (0)