Skip to content

Commit 0833395

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
analyzer: Catch errors on identifier in function references
Fixes #54091 Change-Id: Ie8dbe3f5200a2d5e510287168d852e7de25d21ca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/337823 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 28ca02d commit 0833395

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
873873
@override
874874
void visitFunctionReference(FunctionReference node) {
875875
_typeArgumentsVerifier.checkFunctionReference(node);
876+
super.visitFunctionReference(node);
876877
}
877878

878879
@override

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,63 @@ import '../dart/resolution/context_collection_resolution.dart';
1010
main() {
1111
defineReflectiveSuite(() {
1212
defineReflectiveTests(ImplicitThisReferenceInInitializerTest);
13-
defineReflectiveTests(
14-
ImplicitThisReferenceInInitializerWithoutNullSafetyTest);
1513
});
1614
}
1715

1816
@reflectiveTest
19-
class ImplicitThisReferenceInInitializerTest extends PubPackageResolutionTest
20-
with ImplicitThisReferenceInInitializerTestCases {
21-
test_class_field_late_invokeInstanceMethod() async {
17+
class ImplicitThisReferenceInInitializerTest extends PubPackageResolutionTest {
18+
test_class_fieldInitializer_commentReference_prefixedIdentifier() async {
2219
await assertNoErrorsInCode(r'''
2320
class A {
24-
late int x = foo();
25-
int foo() => 0;
26-
}
27-
''');
28-
}
29-
30-
test_class_field_late_invokeStaticMethod() async {
31-
await assertNoErrorsInCode(r'''
32-
class A {
33-
late int x = foo();
34-
static int foo() => 0;
21+
int a = 0;
22+
/// foo [a.isEven] bar
23+
int x = 1;
3524
}
3625
''');
3726
}
3827

39-
test_class_field_late_readInstanceField() async {
28+
test_class_fieldInitializer_commentReference_simpleIdentifier() async {
4029
await assertNoErrorsInCode(r'''
4130
class A {
4231
int a = 0;
43-
late int x = a;
32+
/// foo [a] bar
33+
int x = 1;
4434
}
4535
''');
4636
}
4737

48-
test_class_field_late_readStaticField() async {
38+
test_class_fieldInitializer_late_invokeInstanceMethod() async {
4939
await assertNoErrorsInCode(r'''
5040
class A {
51-
static int a = 0;
52-
late int x = a;
41+
late int x = foo();
42+
int foo() => 0;
5343
}
5444
''');
5545
}
5646

57-
test_mixin_field_late_readInstanceField() async {
47+
test_class_fieldInitializer_late_invokeStaticMethod() async {
5848
await assertNoErrorsInCode(r'''
59-
mixin M {
60-
int a = 0;
61-
late int x = a;
49+
class A {
50+
late int x = foo();
51+
static int foo() => 0;
6252
}
6353
''');
6454
}
65-
}
6655

67-
mixin ImplicitThisReferenceInInitializerTestCases on PubPackageResolutionTest {
68-
test_class_field_commentReference_prefixedIdentifier() async {
56+
test_class_fieldInitializer_late_readInstanceField() async {
6957
await assertNoErrorsInCode(r'''
7058
class A {
7159
int a = 0;
72-
/// foo [a.isEven] bar
73-
int x = 1;
60+
late int x = a;
7461
}
7562
''');
7663
}
7764

78-
test_class_field_commentReference_simpleIdentifier() async {
65+
test_class_fieldInitializer_late_readStaticField() async {
7966
await assertNoErrorsInCode(r'''
8067
class A {
81-
int a = 0;
82-
/// foo [a] bar
83-
int x = 1;
68+
static int a = 0;
69+
late int x = a;
8470
}
8571
''');
8672
}
@@ -134,7 +120,7 @@ class B {
134120
''');
135121
}
136122

137-
test_field2() async {
123+
test_fieldInitializer() async {
138124
await assertErrorsInCode(r'''
139125
class A {
140126
final x = 0;
@@ -145,7 +131,18 @@ class A {
145131
]);
146132
}
147133

148-
test_instanceVariableInitializer_nestedLocal() async {
134+
test_fieldInitializer_functionReference() async {
135+
await assertErrorsInCode(r'''
136+
class A {
137+
void x<T>() {}
138+
final y = x<int>;
139+
}
140+
''', [
141+
error(CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER, 39, 1),
142+
]);
143+
}
144+
145+
test_fieldInitializer_nestedLocal() async {
149146
// Test that (1) does not prevent reporting an error at (2).
150147
await assertErrorsInCode(r'''
151148
class A {
@@ -187,6 +184,15 @@ class A {
187184
]);
188185
}
189186

187+
test_mixin_field_late_readInstanceField() async {
188+
await assertNoErrorsInCode(r'''
189+
mixin M {
190+
int a = 0;
191+
late int x = a;
192+
}
193+
''');
194+
}
195+
190196
test_prefixedIdentifier() async {
191197
await assertNoErrorsInCode(r'''
192198
class A {
@@ -318,8 +324,3 @@ class A<T> {
318324
''');
319325
}
320326
}
321-
322-
@reflectiveTest
323-
class ImplicitThisReferenceInInitializerWithoutNullSafetyTest
324-
extends PubPackageResolutionTest
325-
with ImplicitThisReferenceInInitializerTestCases, WithoutNullSafetyMixin {}

0 commit comments

Comments
 (0)