@@ -39,6 +39,7 @@ import 'package:analyzer/src/generated/engine.dart';
39
39
import 'package:analyzer/src/generated/java_engine.dart' ;
40
40
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
41
41
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
42
+ import 'package:analyzer/src/generated/this_access_tracker.dart' ;
42
43
import 'package:analyzer/src/task/strong/checker.dart' ;
43
44
import 'package:meta/meta.dart' ;
44
45
@@ -194,6 +195,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
194
195
/// in the scope of an extension.
195
196
ExtensionElement _enclosingExtension;
196
197
198
+ /// The helper for tracking if the current location has access to `this` .
199
+ final ThisAccessTracker _thisAccessTracker = ThisAccessTracker .unit ();
200
+
197
201
/// The context of the method or function that we are currently visiting, or
198
202
/// `null` if we are not inside a method or function.
199
203
EnclosingExecutableContext _enclosingExecutable =
@@ -365,6 +369,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
365
369
});
366
370
}
367
371
372
+ @override
373
+ void visitBlockFunctionBody (BlockFunctionBody node) {
374
+ _thisAccessTracker.enterFunctionBody (node);
375
+ try {
376
+ super .visitBlockFunctionBody (node);
377
+ } finally {
378
+ _thisAccessTracker.exitFunctionBody (node);
379
+ }
380
+ }
381
+
368
382
@override
369
383
void visitBreakStatement (BreakStatement node) {
370
384
SimpleIdentifier labelNode = node.label;
@@ -546,8 +560,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
546
560
547
561
@override
548
562
void visitExpressionFunctionBody (ExpressionFunctionBody node) {
549
- _returnTypeVerifier.verifyExpressionFunctionBody (node);
550
- super .visitExpressionFunctionBody (node);
563
+ _thisAccessTracker.enterFunctionBody (node);
564
+ try {
565
+ _returnTypeVerifier.verifyExpressionFunctionBody (node);
566
+ super .visitExpressionFunctionBody (node);
567
+ } finally {
568
+ _thisAccessTracker.exitFunctionBody (node);
569
+ }
551
570
}
552
571
553
572
@override
@@ -574,6 +593,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
574
593
@override
575
594
void visitFieldDeclaration (FieldDeclaration node) {
576
595
var fields = node.fields;
596
+ _thisAccessTracker.enterFieldDeclaration (node);
577
597
_isInStaticVariableDeclaration = node.isStatic;
578
598
_isInInstanceNotLateVariableDeclaration =
579
599
! node.isStatic && ! node.fields.isLate;
@@ -591,6 +611,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
591
611
} finally {
592
612
_isInStaticVariableDeclaration = false ;
593
613
_isInInstanceNotLateVariableDeclaration = false ;
614
+ _thisAccessTracker.exitFieldDeclaration (node);
594
615
}
595
616
}
596
617
@@ -2976,7 +2997,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
2976
2997
///
2977
2998
/// See [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS] .
2978
2999
void _checkForInvalidReferenceToThis (ThisExpression expression) {
2979
- if (! _isThisInValidContext (expression) ) {
3000
+ if (! _thisAccessTracker.hasAccess ) {
2980
3001
_errorReporter.reportErrorForNode (
2981
3002
CompileTimeErrorCode .INVALID_REFERENCE_TO_THIS , expression);
2982
3003
}
@@ -5161,29 +5182,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
5161
5182
return false ;
5162
5183
}
5163
5184
5164
- /// Return `true` if the given 'this' [expression] is in a valid context.
5165
- bool _isThisInValidContext (ThisExpression expression) {
5166
- for (AstNode node = expression.parent; node != null ; node = node.parent) {
5167
- if (node is CompilationUnit ) {
5168
- return false ;
5169
- } else if (node is ConstructorDeclaration ) {
5170
- return node.factoryKeyword == null ;
5171
- } else if (node is ConstructorInitializer ) {
5172
- return false ;
5173
- } else if (node is MethodDeclaration ) {
5174
- return ! node.isStatic;
5175
- } else if (node is FieldDeclaration ) {
5176
- if (node.fields.isLate &&
5177
- (node.parent is ClassDeclaration ||
5178
- node.parent is MixinDeclaration )) {
5179
- return true ;
5180
- }
5181
- // Continue; a non-late variable may still occur in a valid context.
5182
- }
5183
- }
5184
- return false ;
5185
- }
5186
-
5187
5185
/// Return `true` if the given [identifier] is in a location where it is
5188
5186
/// allowed to resolve to a static member of a supertype.
5189
5187
bool _isUnqualifiedReferenceToNonLocalStaticMemberAllowed (
0 commit comments