Skip to content

Commit 896d72a

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Analyzer: introduce more non-function typedef test cases
Bug: #44078 Change-Id: I9b4f299c61034301b5a9c403f128a614384236ff Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184680 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 875af7c commit 896d72a

7 files changed

+134
-23
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ class B implements a.A {}
2929
]);
3030
}
3131

32+
test_implements_interfaceTypeTypedef() async {
33+
newFile('$testPackageLibPath/lib1.dart', content: '''
34+
library lib1;
35+
class A {}
36+
typedef B = A;
37+
''');
38+
await assertErrorsInCode('''
39+
library root;
40+
import 'lib1.dart' deferred as a;
41+
class C implements a.B {}
42+
''', [
43+
error(CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS, 67, 3),
44+
]);
45+
}
46+
3247
test_mixinApplication() async {
3348
newFile('$testPackageLibPath/lib1.dart', content: '''
3449
library lib1;

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

Lines changed: 14 additions & 0 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/dart/analysis/experiments.dart';
56
import 'package:analyzer/src/error/codes.dart';
67
import 'package:test/test.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -21,6 +22,7 @@ class InferenceFailureOnInstanceCreationTest extends PubPackageResolutionTest {
2122
super.setUp();
2223
writeTestPackageAnalysisOptionsFile(
2324
AnalysisOptionsFileConfig(
25+
experiments: [EnableString.nonfunction_type_aliases],
2426
strictInference: true,
2527
),
2628
);
@@ -68,6 +70,18 @@ HashMap<int, int> f() {
6870
''');
6971
}
7072

73+
test_missingTypeArgument_interfaceTypeTypedef_noInference() async {
74+
await assertErrorsInCode(r'''
75+
import 'dart:collection';
76+
typedef A = HashMap;
77+
void f() {
78+
A();
79+
}
80+
''', [
81+
error(HintCode.INFERENCE_FAILURE_ON_INSTANCE_CREATION, 60, 1),
82+
]);
83+
}
84+
7185
test_missingTypeArgument_noInference() async {
7286
await assertErrorsInCode(r'''
7387
import 'dart:collection';

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ void f() {
5656
assertType(findNode.instanceCreation('new A<int>'), 'A<int>');
5757
}
5858

59+
test_new_interfaceTypeTypedef() async {
60+
await assertErrorsInCode('''
61+
abstract class A {}
62+
typedef B = A;
63+
void f() {
64+
new B();
65+
}
66+
''', [
67+
error(CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS, 52, 1),
68+
]);
69+
}
70+
5971
test_new_nonGeneric() async {
6072
await assertErrorsInCode('''
6173
abstract class A {}
@@ -80,6 +92,18 @@ void f() {
8092
assertType(findNode.instanceCreation('A<int>'), 'A<int>');
8193
}
8294

95+
test_noKeyword_interfaceTypeTypedef() async {
96+
await assertErrorsInCode('''
97+
abstract class A {}
98+
typedef B = A;
99+
void f() {
100+
B();
101+
}
102+
''', [
103+
error(CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS, 48, 1),
104+
]);
105+
}
106+
83107
test_noKeyword_nonGeneric() async {
84108
await assertErrorsInCode('''
85109
abstract class A {}

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,77 @@ main() {
1616
@reflectiveTest
1717
class NonTypeInCatchClauseTest extends PubPackageResolutionTest {
1818
test_isClass() async {
19-
await assertErrorsInCode(r'''
19+
await assertNoErrorsInCode(r'''
2020
f() {
2121
try {
2222
} on String catch (e) {
23+
e;
2324
}
2425
}
25-
''', [
26-
error(HintCode.UNUSED_CATCH_CLAUSE, 35, 1),
27-
]);
26+
''');
2827
}
2928

3029
test_isFunctionTypeAlias() async {
31-
await assertErrorsInCode(r'''
30+
await assertNoErrorsInCode(r'''
3231
typedef F();
3332
f() {
3433
try {
3534
} on F catch (e) {
35+
e;
3636
}
3737
}
38-
''', [
39-
error(HintCode.UNUSED_CATCH_CLAUSE, 43, 1),
40-
]);
38+
''');
39+
}
40+
41+
test_isGenericFunctionTypeAlias() async {
42+
await assertNoErrorsInCode(r'''
43+
typedef F<T> = void Function(T);
44+
f() {
45+
try {
46+
} on F catch (e) {
47+
e;
48+
}
49+
}
50+
''');
51+
}
52+
53+
test_isInterfaceTypeTypeAlias() async {
54+
await assertNoErrorsInCode(r'''
55+
typedef F = String;
56+
f() {
57+
try {
58+
} on F catch (e) {
59+
e;
60+
}
61+
}
62+
''');
4163
}
4264

4365
test_isTypeParameter() async {
44-
await assertErrorsInCode(r'''
66+
await assertNoErrorsInCode(r'''
4567
class A<T extends Object> {
4668
f() {
4769
try {
4870
} on T catch (e) {
71+
e;
4972
}
5073
}
5174
}
52-
''', [
53-
error(HintCode.UNUSED_CATCH_CLAUSE, 64, 1),
54-
]);
75+
''');
5576
}
5677

5778
test_notDefined() async {
5879
await assertErrorsInCode('''
5980
f() {
6081
try {
6182
} on T catch (e) {
83+
e;
6284
}
6385
}
6486
''', [
6587
// TODO(srawlins): Ideally the first error should not be reported.
6688
error(HintCode.NULLABLE_TYPE_IN_CATCH_CLAUSE, 21, 1),
6789
error(CompileTimeErrorCode.NON_TYPE_IN_CATCH_CLAUSE, 21, 1),
68-
error(HintCode.UNUSED_CATCH_CLAUSE, 30, 1),
6990
]);
7091
}
7192

@@ -75,13 +96,13 @@ var T = 0;
7596
f() {
7697
try {
7798
} on T catch (e) {
99+
e;
78100
}
79101
}
80102
''', [
81103
// TODO(srawlins): Ideally the first error should not be reported.
82104
error(HintCode.NULLABLE_TYPE_IN_CATCH_CLAUSE, 32, 1),
83105
error(CompileTimeErrorCode.NON_TYPE_IN_CATCH_CLAUSE, 32, 1),
84-
error(HintCode.UNUSED_CATCH_CLAUSE, 41, 1),
85106
]);
86107
}
87108

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ import '../dart/resolution/context_collection_resolution.dart';
1010
main() {
1111
defineReflectiveSuite(() {
1212
defineReflectiveTests(NotIterableSpreadTest);
13-
defineReflectiveTests(NotIterableSpreadNullSafetyTest);
13+
defineReflectiveTests(NotIterableSpreadWithoutNullSafetyTest);
1414
});
1515
}
1616

1717
@reflectiveTest
18-
class NotIterableSpreadNullSafetyTest extends NotIterableSpreadTest
19-
with WithNullSafetyMixin {
18+
class NotIterableSpreadTest extends PubPackageResolutionTest
19+
with NotIterableSpreadTestCases {
20+
test_iterable_interfaceTypeTypedef() async {
21+
await assertNoErrorsInCode('''
22+
typedef A = List<int>;
23+
f(A a) {
24+
var v = [...a];
25+
v;
26+
}
27+
''');
28+
}
29+
2030
test_iterable_typeParameter_bound_listQuestion() async {
2131
await assertNoErrorsInCode('''
2232
void f<T extends List<int>?>(T a) {
@@ -27,9 +37,7 @@ void f<T extends List<int>?>(T a) {
2737
}
2838
}
2939

30-
@reflectiveTest
31-
class NotIterableSpreadTest extends PubPackageResolutionTest
32-
with WithoutNullSafetyMixin {
40+
mixin NotIterableSpreadTestCases on PubPackageResolutionTest {
3341
test_iterable_list() async {
3442
await assertNoErrorsInCode('''
3543
var a = [0];
@@ -100,3 +108,7 @@ void f<T extends num>(T a) {
100108
]);
101109
}
102110
}
111+
112+
@reflectiveTest
113+
class NotIterableSpreadWithoutNullSafetyTest extends PubPackageResolutionTest
114+
with WithoutNullSafetyMixin, NotIterableSpreadTestCases {}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ class A {
4747
}
4848
4949
class B extends A {}
50+
''');
51+
}
52+
53+
test_redirectsToSubclass_asTypedef() async {
54+
await assertNoErrorsInCode(r'''
55+
class A {
56+
factory A.named() = C;
57+
A();
58+
}
59+
60+
class B extends A {}
61+
typedef C = B;
5062
''');
5163
}
5264
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ main() {
1616

1717
@reflectiveTest
1818
class UnnecessaryCastTest extends PubPackageResolutionTest
19-
with WithoutNullSafetyMixin {
19+
with UnnecessaryCastTestCases, WithoutNullSafetyMixin {}
20+
21+
mixin UnnecessaryCastTestCases on PubPackageResolutionTest {
2022
test_conditionalExpression_changesResultType_left() async {
2123
await assertNoErrorsInCode(r'''
2224
class A {}
@@ -216,8 +218,8 @@ void f<T>(T a) {
216218
}
217219

218220
@reflectiveTest
219-
class UnnecessaryCastTestWithNullSafety extends UnnecessaryCastTest
220-
with WithNullSafetyMixin {
221+
class UnnecessaryCastTestWithNullSafety extends PubPackageResolutionTest
222+
with UnnecessaryCastTestCases {
221223
test_interfaceType_star_toNone() async {
222224
newFile('$testPackageLibPath/a.dart', content: r'''
223225
// @dart = 2.7
@@ -254,4 +256,15 @@ void f() {
254256
error(HintCode.IMPORT_OF_LEGACY_LIBRARY_INTO_NULL_SAFE, 7, 8),
255257
]);
256258
}
259+
260+
test_type_type_asInterfaceTypeTypedef() async {
261+
await assertErrorsInCode(r'''
262+
typedef N = num;
263+
void f(num a) {
264+
a as N;
265+
}
266+
''', [
267+
error(HintCode.UNNECESSARY_CAST, 35, 6),
268+
]);
269+
}
257270
}

0 commit comments

Comments
 (0)