Skip to content

Commit 01d42a9

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[model] Only infer first required positional parameter in setters
This CL makes sure that only the first required positional parameter is inferred in setters. Optional positional and named parameters, which are erroneous in the case of setters, aren't inferred. That prevents reporting of some cascading errors. Change-Id: If89628adf542a325aa7320557c131abb7987a687 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426040 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 695cf90 commit 01d42a9

File tree

5 files changed

+20
-47
lines changed

5 files changed

+20
-47
lines changed

pkg/front_end/lib/src/kernel/hierarchy/members_node.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,20 @@ class ClassMembersNodeBuilder extends MembersNodeBuilder {
392392
// Erroneous case.
393393
return;
394394
}
395-
FormalParameterBuilder parameter = formals.first;
395+
396+
// Only infer the parameter type if it's the first required positional.
397+
FormalParameterBuilder? parameter;
398+
for (FormalParameterBuilder formal in formals) {
399+
if (formal.isRequiredPositional) {
400+
parameter = formal;
401+
break;
402+
}
403+
}
404+
if (parameter == null) {
405+
// Erroneous case.
406+
return;
407+
}
408+
396409
if (parameter.type is InferableTypeBuilder) {
397410
DartType? inferredType;
398411

pkg/front_end/testcases/general/issue54006.dart.strong.expect

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ library;
5959
// get field(a) => 0;
6060
// ^
6161
//
62-
// pkg/front_end/testcases/general/issue54006.dart:27:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
63-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
64-
// set field([a]) {}
65-
// ^
66-
//
67-
// pkg/front_end/testcases/general/issue54006.dart:32:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
68-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
69-
// set field({a}) {}
70-
// ^
71-
//
7262
// pkg/front_end/testcases/general/issue54006.dart:12:13: Error: Expected a function body or '=>'.
7363
// Try adding {}.
7464
// set field = 0;
@@ -161,7 +151,7 @@ class E extends core::Object implements self::A {
161151
set field([a]) {}
162152
^";
163153
{
164-
core::int a = null;
154+
dynamic a = null;
165155
{}
166156
}
167157
}
@@ -177,7 +167,7 @@ class F extends core::Object implements self::A {
177167
set field({a}) {}
178168
^";
179169
{
180-
core::int a = null;
170+
dynamic a = null;
181171
{}
182172
}
183173
}

pkg/front_end/testcases/general/issue54006.dart.strong.modular.expect

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ library;
5959
// get field(a) => 0;
6060
// ^
6161
//
62-
// pkg/front_end/testcases/general/issue54006.dart:27:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
63-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
64-
// set field([a]) {}
65-
// ^
66-
//
67-
// pkg/front_end/testcases/general/issue54006.dart:32:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
68-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
69-
// set field({a}) {}
70-
// ^
71-
//
7262
// pkg/front_end/testcases/general/issue54006.dart:12:13: Error: Expected a function body or '=>'.
7363
// Try adding {}.
7464
// set field = 0;
@@ -161,7 +151,7 @@ class E extends core::Object implements self::A {
161151
set field([a]) {}
162152
^";
163153
{
164-
core::int a = null;
154+
dynamic a = null;
165155
{}
166156
}
167157
}
@@ -177,7 +167,7 @@ class F extends core::Object implements self::A {
177167
set field({a}) {}
178168
^";
179169
{
180-
core::int a = null;
170+
dynamic a = null;
181171
{}
182172
}
183173
}

pkg/front_end/testcases/general/issue54006.dart.strong.outline.expect

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ library;
5959
// get field(a) => 0;
6060
// ^
6161
//
62-
// pkg/front_end/testcases/general/issue54006.dart:27:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
63-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
64-
// set field([a]) {}
65-
// ^
66-
//
67-
// pkg/front_end/testcases/general/issue54006.dart:32:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
68-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
69-
// set field({a}) {}
70-
// ^
71-
//
7262
import self as self;
7363
import "dart:core" as core;
7464

pkg/front_end/testcases/general/issue54006.dart.strong.transformed.expect

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ library;
5959
// get field(a) => 0;
6060
// ^
6161
//
62-
// pkg/front_end/testcases/general/issue54006.dart:27:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
63-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
64-
// set field([a]) {}
65-
// ^
66-
//
67-
// pkg/front_end/testcases/general/issue54006.dart:32:14: Error: The parameter 'a' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
68-
// Try adding either an explicit non-'null' default value or the 'required' modifier.
69-
// set field({a}) {}
70-
// ^
71-
//
7262
// pkg/front_end/testcases/general/issue54006.dart:12:13: Error: Expected a function body or '=>'.
7363
// Try adding {}.
7464
// set field = 0;
@@ -161,7 +151,7 @@ class E extends core::Object implements self::A {
161151
set field([a]) {}
162152
^";
163153
{
164-
core::int a = null;
154+
dynamic a = null;
165155
{}
166156
}
167157
}
@@ -177,7 +167,7 @@ class F extends core::Object implements self::A {
177167
set field({a}) {}
178168
^";
179169
{
180-
core::int a = null;
170+
dynamic a = null;
181171
{}
182172
}
183173
}

0 commit comments

Comments
 (0)