Skip to content

Commit 6fc6c47

Browse files
committed
Updates for typed literals resolution tests.
Fixes for comments on https://dart-review.googlesource.com/c/sdk/+/155680 Change-Id: I7667fe0f323bce1503a15425a654af641ee2a6ce Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155820 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f4053e3 commit 6fc6c47

File tree

3 files changed

+249
-130
lines changed

3 files changed

+249
-130
lines changed

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

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/error/codes.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../driver_resolution.dart';
@@ -17,9 +18,11 @@ main() {
1718
@reflectiveTest
1819
class ListLiteralTest extends DriverResolutionTest {
1920
test_context_noTypeArgs_expression_conflict() async {
20-
await resolveTestCode('''
21+
await assertErrorsInCode('''
2122
List<int> a = ['a'];
22-
''');
23+
''', [
24+
error(StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE, 15, 3),
25+
]);
2326
assertType(findNode.listLiteral('['), 'List<int>');
2427
}
2528

@@ -38,34 +41,54 @@ List<String> a = [];
3841
}
3942

4043
test_context_noTypeArgs_noElements_typeParameter() async {
41-
await resolveTestCode('''
44+
var expectedErrors = expectedErrorsByNullability(
45+
nullable: [
46+
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 39, 2),
47+
],
48+
legacy: [
49+
error(CompileTimeErrorCode.INVALID_CAST_LITERAL_LIST, 39, 2),
50+
],
51+
);
52+
await assertErrorsInCode('''
4253
class A<E extends List<int>> {
4354
E a = [];
4455
}
45-
''');
56+
''', expectedErrors);
4657
assertType(findNode.listLiteral('['), 'List<dynamic>');
4758
}
4859

4960
test_context_noTypeArgs_noElements_typeParameter_dynamic() async {
50-
await resolveTestCode('''
61+
var expectedErrors = expectedErrorsByNullability(
62+
nullable: [
63+
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 43, 2),
64+
],
65+
legacy: [
66+
error(CompileTimeErrorCode.INVALID_CAST_LITERAL_LIST, 43, 2),
67+
],
68+
);
69+
await assertErrorsInCode('''
5170
class A<E extends List<dynamic>> {
5271
E a = [];
5372
}
54-
''');
73+
''', expectedErrors);
5574
assertType(findNode.listLiteral('['), 'List<dynamic>');
5675
}
5776

5877
test_context_typeArgs_expression_conflictingContext() async {
59-
await resolveTestCode('''
78+
await assertErrorsInCode('''
6079
List<String> a = <int>[0];
61-
''');
80+
''', [
81+
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 17, 8),
82+
]);
6283
assertType(findNode.listLiteral('['), 'List<int>');
6384
}
6485

6586
test_context_typeArgs_expression_conflictingExpression() async {
66-
await resolveTestCode('''
87+
await assertErrorsInCode('''
6788
List<String> a = <String>[0];
68-
''');
89+
''', [
90+
error(StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE, 26, 1),
91+
]);
6992
assertType(findNode.listLiteral('['), 'List<String>');
7093
}
7194

@@ -87,9 +110,11 @@ List<String> a = <String>['a'];
87110
}
88111

89112
test_context_typeArgs_noElements_conflict() async {
90-
await resolveTestCode('''
113+
await assertErrorsInCode('''
91114
List<String> a = <int>[];
92-
''');
115+
''', [
116+
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 17, 7),
117+
]);
93118
assertType(findNode.listLiteral('['), 'List<int>');
94119
}
95120

@@ -122,16 +147,20 @@ var a = [1, '2', 3];
122147
}
123148

124149
test_noContext_noTypeArgs_expressions_unresolved() async {
125-
await resolveTestCode('''
150+
await assertErrorsInCode('''
126151
var a = [x];
127-
''');
152+
''', [
153+
error(StaticWarningCode.UNDEFINED_IDENTIFIER, 9, 1),
154+
]);
128155
assertType(findNode.listLiteral('['), 'List<dynamic>');
129156
}
130157

131158
test_noContext_noTypeArgs_expressions_unresolved_multiple() async {
132-
await resolveTestCode('''
159+
await assertErrorsInCode('''
133160
var a = [0, x, 2];
134-
''');
161+
''', [
162+
error(StaticWarningCode.UNDEFINED_IDENTIFIER, 12, 1),
163+
]);
135164
assertType(findNode.listLiteral('['), 'List<dynamic>');
136165
}
137166

@@ -254,7 +283,7 @@ main(L l1) {
254283
}
255284

256285
test_noContext_noTypeArgs_spread_nestedInIf_oneAmbiguous() async {
257-
await resolveTestCode('''
286+
await assertNoErrorsInCode('''
258287
List<int> c = [];
259288
dynamic d;
260289
var a = [if (0 < 1) ...c else ...d];
@@ -264,7 +293,7 @@ var a = [if (0 < 1) ...c else ...d];
264293

265294
test_noContext_noTypeArgs_spread_nullAware_null() async {
266295
await assertNoErrorsInCode('''
267-
void f(Null a) async {
296+
void f(Null a) {
268297
// ignore:unused_local_variable
269298
var v = [...?a];
270299
}
@@ -280,27 +309,27 @@ void f(Null a) async {
280309

281310
test_noContext_noTypeArgs_spread_nullAware_null2() async {
282311
await assertNoErrorsInCode('''
283-
void f(Null a) async {
312+
void f(Null a) {
284313
// ignore:unused_local_variable
285314
var v = [1, ...?a, 2];
286315
}
287316
''');
288-
assertType(
289-
findNode.listLiteral('['),
290-
typeStringByNullability(
291-
nullable: 'List<int>',
292-
legacy: 'List<int>',
293-
),
294-
);
317+
assertType(findNode.listLiteral('['), 'List<int>');
295318
}
296319

297320
test_noContext_noTypeArgs_spread_nullAware_typeParameter_implementsNull() async {
298-
await resolveTestCode('''
321+
var expectedErrors = expectedErrorsByNullability(
322+
nullable: [],
323+
legacy: [
324+
error(CompileTimeErrorCode.NOT_ITERABLE_SPREAD, 85, 1),
325+
],
326+
);
327+
await assertErrorsInCode('''
299328
void f<T extends Null>(T a) async {
300329
// ignore:unused_local_variable
301330
var v = [...?a];
302331
}
303-
''');
332+
''', expectedErrors);
304333
assertType(
305334
findNode.listLiteral('['),
306335
typeStringByNullability(
@@ -311,7 +340,7 @@ void f<T extends Null>(T a) async {
311340
}
312341

313342
test_noContext_noTypeArgs_spread_typeParameter_implementsIterable() async {
314-
await resolveTestCode('''
343+
await assertNoErrorsInCode('''
315344
void f<T extends List<int>>(T a) {
316345
// ignore:unused_local_variable
317346
var v = [...a];
@@ -321,29 +350,35 @@ void f<T extends List<int>>(T a) {
321350
}
322351

323352
test_noContext_noTypeArgs_spread_typeParameter_notImplementsIterable() async {
324-
await resolveTestCode('''
353+
await assertErrorsInCode('''
325354
void f<T extends num>(T a) {
326355
// ignore:unused_local_variable
327356
var v = [...a];
328357
}
329-
''');
358+
''', [
359+
error(CompileTimeErrorCode.NOT_ITERABLE_SPREAD, 77, 1),
360+
]);
330361
assertType(findNode.listLiteral('[...'), 'List<dynamic>');
331362
}
332363

333364
test_noContext_noTypeArgs_spread_typeParameter_notImplementsIterable2() async {
334-
await resolveTestCode('''
365+
await assertErrorsInCode('''
335366
void f<T extends num>(T a) {
336367
// ignore:unused_local_variable
337368
var v = [...a, 0];
338369
}
339-
''');
370+
''', [
371+
error(CompileTimeErrorCode.NOT_ITERABLE_SPREAD, 77, 1),
372+
]);
340373
assertType(findNode.listLiteral('[...'), 'List<dynamic>');
341374
}
342375

343376
test_noContext_typeArgs_expression_conflict() async {
344-
await resolveTestCode('''
377+
await assertErrorsInCode('''
345378
var a = <String>[1];
346-
''');
379+
''', [
380+
error(StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE, 17, 1),
381+
]);
347382
assertType(findNode.listLiteral('['), 'List<String>');
348383
}
349384

@@ -423,22 +458,26 @@ void f(Never a) async {
423458
}
424459

425460
test_noContext_noTypeArgs_spread_nullAware_never() async {
426-
await assertNoErrorsInCode('''
461+
await assertErrorsInCode('''
427462
void f(Never a) async {
428463
// ignore:unused_local_variable
429-
var v = [...a];
464+
var v = [...?a];
430465
}
431-
''');
466+
''', [
467+
error(StaticWarningCode.INVALID_NULL_AWARE_OPERATOR, 69, 4),
468+
]);
432469
assertType(findNode.listLiteral('['), 'List<Never>');
433470
}
434471

435472
test_noContext_noTypeArgs_spread_nullAware_typeParameter_implementsNever() async {
436-
await resolveTestCode('''
473+
await assertErrorsInCode('''
437474
void f<T extends Never>(T a) async {
438475
// ignore:unused_local_variable
439476
var v = [...?a];
440477
}
441-
''');
478+
''', [
479+
error(StaticWarningCode.INVALID_NULL_AWARE_OPERATOR, 82, 4),
480+
]);
442481
assertType(findNode.listLiteral('['), 'List<Never>');
443482
}
444483

0 commit comments

Comments
 (0)