Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f2a7851

Browse files
author
Dart CI
committed
Version 2.12.0-273.0.dev
Merge commit '3eb117c24e770187a078412b419c636570670865' into 'dev'
2 parents 21b7f86 + 3eb117c commit f2a7851

File tree

16 files changed

+393
-30
lines changed

16 files changed

+393
-30
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ const List<ErrorCode> errorCodeValues = [
481481
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_EXTENDS,
482482
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS,
483483
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH,
484+
HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR,
484485
HintCode.ASSIGNMENT_OF_DO_NOT_STORE,
485486
HintCode.CAN_BE_NULL_AFTER_NULL_AWARE,
486487
HintCode.DEAD_CODE,

pkg/analyzer/lib/src/dart/error/hint_codes.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ import 'package:analyzer/src/error/analyzer_error_code.dart';
1414
* mentioned in the Dart Language Specification.
1515
*/
1616
class HintCode extends AnalyzerErrorCode {
17+
/**
18+
* Parameters:
19+
* 0: the name of the actual argument type
20+
* 1: the name of the expected function return type
21+
*/
22+
static const HintCode ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR =
23+
HintCode(
24+
'ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR',
25+
"The argument type '{0}' can't be assigned to the parameter type "
26+
"'{1} Function(Object)' or '{1} Function(Object, StackTrace)'.");
27+
1728
/**
1829
* Users should not assign values marked `@doNotStore`.
1930
*/

pkg/analyzer/lib/src/error/catch_error_verifier.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class CatchErrorVerifier {
5252
var targetFutureType = targetType.typeArguments.first;
5353
var expectedReturnType = _typeProvider.futureOrType2(targetFutureType);
5454
if (callback is FunctionExpression) {
55+
_checkOnErrorFunctionType(
56+
callback, callback.staticType, expectedReturnType);
5557
var catchErrorOnErrorExecutable = EnclosingExecutableContext(
5658
callback.declaredElement,
5759
isAsynchronous: true,
@@ -64,10 +66,56 @@ class CatchErrorVerifier {
6466
var callbackType = callback.staticType;
6567
if (callbackType is FunctionType) {
6668
_checkReturnType(expectedReturnType, callbackType.returnType, callback);
69+
_checkOnErrorFunctionType(callback, callbackType, expectedReturnType);
70+
} else {
71+
// If [callback] is not even a Function, then ErrorVerifier will have
72+
// reported this.
6773
}
6874
}
6975
}
7076

77+
void _checkOnErrorFunctionType(Expression expression,
78+
FunctionType expressionType, DartType expectedFunctionReturnType) {
79+
void report() {
80+
_errorReporter.reportErrorForNode(
81+
HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR,
82+
expression,
83+
[expressionType, expectedFunctionReturnType],
84+
);
85+
}
86+
87+
var parameters = expressionType.parameters;
88+
if (parameters == null || parameters.isEmpty) {
89+
return report();
90+
}
91+
var firstParameter = parameters.first;
92+
if (firstParameter.isNamed) {
93+
return report();
94+
} else if (firstParameter.isOptionalPositional) {
95+
return report();
96+
} else {
97+
if (!_typeSystem.isSubtypeOf(
98+
_typeProvider.objectType, firstParameter.type)) {
99+
return report();
100+
}
101+
}
102+
if (parameters.length == 2) {
103+
var secondParameter = parameters[1];
104+
if (secondParameter.isNamed) {
105+
return report();
106+
} else if (firstParameter.isOptionalPositional) {
107+
return report();
108+
} else {
109+
if (!_typeSystem.isSubtypeOf(
110+
_typeProvider.stackTraceType, secondParameter.type)) {
111+
return report();
112+
}
113+
}
114+
} else if (parameters.length > 2) {
115+
return report();
116+
}
117+
}
118+
71119
void _checkReturnType(
72120
DartType expectedType, DartType functionReturnType, Expression callback) {
73121
if (!_typeSystem.isAssignableTo(functionReturnType, expectedType)) {

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,10 +1605,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
16051605
}
16061606

16071607
/// Verify that the given [expression] can be assigned to its corresponding
1608-
/// parameters. The [expectedStaticType] is the expected static type.
1609-
///
1610-
/// This method corresponds to
1611-
/// [BestPracticesVerifier.checkForArgumentTypeNotAssignableWithExpectedTypes].
1608+
/// parameters.
16121609
///
16131610
/// See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE],
16141611
/// [CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE],
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(ArgumentTypeNotAssignableCatchErrorOnErrorTest);
13+
defineReflectiveTests(
14+
ArgumentTypeNotAssignableCatchErrorOnErrorWithNullSafetyTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class ArgumentTypeNotAssignableCatchErrorOnErrorTest
20+
extends PubPackageResolutionTest {
21+
void test_firstParameterIsDynamic() async {
22+
await assertNoErrorsInCode('''
23+
void f(Future<int> future, Future<int> Function(dynamic a) callback) {
24+
future.catchError(callback);
25+
}
26+
''');
27+
}
28+
29+
void test_firstParameterIsNamed() async {
30+
await assertErrorsInCode('''
31+
void f(Future<int> future, Future<int> Function({Object a}) callback) {
32+
future.catchError(callback);
33+
}
34+
''', [
35+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 92, 8),
36+
]);
37+
}
38+
39+
void test_firstParameterIsOptional() async {
40+
await assertErrorsInCode('''
41+
void f(Future<int> future, Future<int> Function([Object a]) callback) {
42+
future.catchError(callback);
43+
}
44+
''', [
45+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 92, 8),
46+
]);
47+
}
48+
49+
void test_functionExpression_firstParameterIsDynamic() async {
50+
await assertNoErrorsInCode('''
51+
void f(Future<void> future) {
52+
future.catchError((dynamic a) {});
53+
}
54+
''');
55+
}
56+
57+
void test_functionExpression_firstParameterIsImplicit() async {
58+
await assertNoErrorsInCode('''
59+
void f(Future<void> future) {
60+
future.catchError((a) {});
61+
}
62+
''');
63+
}
64+
65+
void test_functionExpression_firstParameterIsNamed() async {
66+
await assertErrorsInCode('''
67+
void f(Future<void> future) {
68+
future.catchError(({Object a = 1}) {});
69+
}
70+
''', [
71+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 19),
72+
]);
73+
}
74+
75+
void test_functionExpression_firstParameterIsOptional() async {
76+
await assertErrorsInCode('''
77+
void f(Future<void> future) {
78+
future.catchError(([Object a = 1]) {});
79+
}
80+
''', [
81+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 19),
82+
]);
83+
}
84+
85+
void test_functionExpression_firstParameterIsVar() async {
86+
await assertNoErrorsInCode('''
87+
void f(Future<void> future) {
88+
future.catchError((var a) {});
89+
}
90+
''');
91+
}
92+
93+
void test_functionExpression_noParameters() async {
94+
await assertErrorsInCode('''
95+
void f(Future<void> future) {
96+
future.catchError(() {});
97+
}
98+
''', [
99+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 5),
100+
]);
101+
}
102+
103+
void test_functionExpression_secondParameterIsDynamic() async {
104+
await assertNoErrorsInCode('''
105+
void f(Future<void> future) {
106+
future.catchError((Object a, dynamic b) {});
107+
}
108+
''');
109+
}
110+
111+
void test_functionExpression_secondParameterIsImplicit() async {
112+
await assertNoErrorsInCode('''
113+
void f(Future<void> future) {
114+
future.catchError((Object a, b) {});
115+
}
116+
''');
117+
}
118+
119+
void test_functionExpression_secondParameterIsNamed() async {
120+
await assertErrorsInCode('''
121+
void f(Future<void> future) {
122+
future.catchError((Object a, {StackTrace b}) {});
123+
}
124+
''', [
125+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 29),
126+
]);
127+
}
128+
129+
void test_functionExpression_secondParameterIsVar() async {
130+
await assertNoErrorsInCode('''
131+
void f(Future<void> future) {
132+
future.catchError((Object a, var b) {});
133+
}
134+
''');
135+
}
136+
137+
void test_functionExpression_tooManyParameters() async {
138+
await assertErrorsInCode('''
139+
void f(Future<void> future) {
140+
future.catchError((a, b, c) {});
141+
}
142+
''', [
143+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 12),
144+
]);
145+
}
146+
147+
void test_functionExpression_wrongFirstParameterType() async {
148+
await assertErrorsInCode('''
149+
void f(Future<void> future) {
150+
future.catchError((String a) {});
151+
}
152+
''', [
153+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 13),
154+
]);
155+
}
156+
157+
void test_functionExpression_wrongSecondParameterType() async {
158+
await assertErrorsInCode('''
159+
void f(Future<void> future) {
160+
future.catchError((Object a, String b) {});
161+
}
162+
''', [
163+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 23),
164+
]);
165+
}
166+
167+
void test_noParameters() async {
168+
await assertErrorsInCode('''
169+
void f(Future<int> future, Future<int> Function() callback) {
170+
future.catchError(callback);
171+
}
172+
''', [
173+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 82, 8),
174+
]);
175+
}
176+
177+
void test_okType() async {
178+
await assertNoErrorsInCode('''
179+
void f(Future<int> future, Future<int> Function(Object, StackTrace) callback) {
180+
future.catchError(callback);
181+
}
182+
''');
183+
}
184+
185+
void test_secondParameterIsDynamic() async {
186+
await assertNoErrorsInCode('''
187+
void f(Future<int> future, Future<int> Function(Object a, dynamic b) callback) {
188+
future.catchError(callback);
189+
}
190+
''');
191+
}
192+
193+
void test_secondParameterIsNamed() async {
194+
await assertErrorsInCode('''
195+
void f(Future<int> future, Future<int> Function(Object a, {StackTrace b}) callback) {
196+
future.catchError(callback);
197+
}
198+
''', [
199+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 106, 8),
200+
]);
201+
}
202+
203+
void test_tooManyParameters() async {
204+
await assertErrorsInCode('''
205+
void f(Future<int> future, Future<int> Function(int, int, int) callback) {
206+
future.catchError(callback);
207+
}
208+
''', [
209+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 95, 8),
210+
]);
211+
}
212+
213+
void test_wrongSecondParameterType() async {
214+
await assertErrorsInCode('''
215+
void f(Future<int> future, Future<int> Function(Object, String) callback) {
216+
future.catchError(callback);
217+
}
218+
''', [
219+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 96, 8),
220+
]);
221+
}
222+
223+
voidtest_wrongFirstParameterType() async {
224+
await assertErrorsInCode('''
225+
void f(Future<int> future, Future<int> Function(String) callback) {
226+
future.catchError(callback);
227+
}
228+
''', [
229+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 88, 8),
230+
]);
231+
}
232+
}
233+
234+
@reflectiveTest
235+
class ArgumentTypeNotAssignableCatchErrorOnErrorWithNullSafetyTest
236+
extends ArgumentTypeNotAssignableCatchErrorOnErrorTest
237+
with WithNullSafetyMixin {
238+
void test_functionExpression_firstParameterIsNullableObject() async {
239+
await assertNoErrorsInCode('''
240+
void f(Future<void> future) {
241+
future.catchError((Object? a) {});
242+
}
243+
''');
244+
}
245+
246+
@override
247+
void test_functionExpression_secondParameterIsNamed() async {
248+
await assertErrorsInCode('''
249+
void f(Future<void> future) {
250+
future.catchError((Object a, {required StackTrace b}) {});
251+
}
252+
''', [
253+
error(HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE_CATCH_ERROR_ON_ERROR, 50, 38),
254+
]);
255+
}
256+
257+
void test_functionExpression_secondParameterIsNullableStackTrace() async {
258+
await assertNoErrorsInCode('''
259+
void f(Future<void> future) {
260+
future.catchError((Object a, StackTrace? b) {});
261+
}
262+
''');
263+
}
264+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'ambiguous_import_test.dart' as ambiguous_import;
1717
import 'ambiguous_set_or_map_literal_test.dart' as ambiguous_set_or_map_literal;
1818
import 'annotation_on_pointer_field_test.dart' as annotation_on_pointer_field;
1919
import 'annotation_syntax_test.dart' as annotation_syntax;
20+
import 'argument_type_not_assignable_catch_error_on_error_test.dart'
21+
as argument_type_not_assignable_catch_error_on_error;
2022
import 'argument_type_not_assignable_test.dart' as argument_type_not_assignable;
2123
import 'assert_in_redirecting_constructor_test.dart'
2224
as assert_in_redirecting_constructor;
@@ -677,6 +679,7 @@ main() {
677679
annotation_on_pointer_field.main();
678680
annotation_syntax.main();
679681
argument_type_not_assignable.main();
682+
argument_type_not_assignable_catch_error_on_error.main();
680683
assert_in_redirecting_constructor.main();
681684
assignment_of_do_not_store.main();
682685
assignment_to_const.main();

pkg/front_end/testcases/incremental_initialize_from_dill/no_outline_change_22.yaml.world.1.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ library from "org-dartlang-test:///main.dart" as main {
188188
return new dart._internal::SkipWhileIterable::•<dart.core::int*>(this, test);
189189
}
190190
method /*isNonNullableByDefault, from org-dartlang-sdk:///sdk/lib/collection/list.dart */ take(dart.core::int count) → dart.core::Iterable<dart.core::int*>
191-
return new dart._internal::SubListIterable::•<dart.core::int*>(this, 0, count);
191+
return new dart._internal::SubListIterable::•<dart.core::int*>(this, 0, dart._internal::checkNotNullable<dart.core::int>(count, "count"));
192192
method /*isNonNullableByDefault, from org-dartlang-sdk:///sdk/lib/collection/list.dart */ takeWhile((dart.core::int*) → dart.core::bool test) → dart.core::Iterable<dart.core::int*> {
193193
return new dart._internal::TakeWhileIterable::•<dart.core::int*>(this, test);
194194
}

0 commit comments

Comments
 (0)