Skip to content

Commit 2aa4dad

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate error_verifier (EnclosingExecutable)
Change-Id: I919fc8b7e4ad76d39c6dfdb163bc17370c259423 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411060 Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 7ff66f4 commit 2aa4dad

File tree

5 files changed

+133
-126
lines changed

5 files changed

+133
-126
lines changed

pkg/analyzer/api.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3862,7 +3862,6 @@ package:analyzer/dart/element/element2.dart:
38623862
GenericFunctionTypeFragment (class extends Object implements FunctionTypedFragment):
38633863
new (constructor: GenericFunctionTypeFragment Function())
38643864
element (getter: GenericFunctionTypeElement2)
3865-
enclosingFragment (getter: LibraryFragment?)
38663865
nextFragment (getter: GenericFunctionTypeFragment?)
38673866
previousFragment (getter: GenericFunctionTypeFragment?)
38683867
GetterElement (class extends Object implements PropertyAccessorElement2):

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,6 @@ abstract class GenericFunctionTypeFragment implements FunctionTypedFragment {
11471147
@override
11481148
GenericFunctionTypeElement2 get element;
11491149

1150-
@override
1151-
LibraryFragment? get enclosingFragment;
1152-
11531150
@override
11541151
GenericFunctionTypeFragment? get nextFragment;
11551152

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,6 +5316,8 @@ class FunctionElementImpl extends ExecutableElementImpl
53165316
return executableFragment;
53175317
case LocalVariableFragment variableFragment:
53185318
return variableFragment;
5319+
case ParameterElementImpl parameterFragment:
5320+
return parameterFragment;
53195321
case TopLevelVariableFragment variableFragment:
53205322
return variableFragment;
53215323
case FieldFragment fieldFragment:
@@ -5427,8 +5429,7 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
54275429
GenericFunctionTypeElement2 get element => _element2;
54285430

54295431
@override
5430-
LibraryFragment? get enclosingFragment =>
5431-
enclosingElement3 as LibraryFragment;
5432+
Fragment? get enclosingFragment => enclosingElement3 as Fragment;
54325433

54335434
@override
54345435
List<FormalParameterFragment> get formalParameters =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ class ErrorHandlerVerifier {
226226
var callbackType = callback.staticType as FunctionType;
227227
_checkErrorHandlerFunctionType(
228228
callback, callbackType, expectedReturnType);
229-
var catchErrorOnErrorExecutable = EnclosingExecutableContext.tmp(
229+
var catchErrorOnErrorExecutable = EnclosingExecutableContext(
230230
callback.declaredFragment!.element,
231231
isAsynchronous: true,
232+
isGenerator: false,
232233
catchErrorOnErrorReturnType: expectedReturnType);
233234
var returnStatementVerifier =
234235
_ReturnStatementVerifier(_returnTypeVerifier);

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

Lines changed: 128 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import 'package:analyzer/src/utilities/extensions/string.dart';
6161
import 'package:collection/collection.dart';
6262

6363
class EnclosingExecutableContext {
64-
final ExecutableElement? element;
64+
final ExecutableElement2? element;
6565
final bool isAsynchronous;
6666
final bool isConstConstructor;
6767
final bool isGenerativeConstructor;
@@ -89,76 +89,66 @@ class EnclosingExecutableContext {
8989
int catchClauseLevel = 0;
9090

9191
EnclosingExecutableContext(this.element,
92-
{bool? isAsynchronous, this.catchErrorOnErrorReturnType})
93-
: isAsynchronous =
94-
isAsynchronous ?? (element != null && element.isAsynchronous),
95-
isConstConstructor = element is ConstructorElement && element.isConst,
92+
{required this.isAsynchronous,
93+
required this.isGenerator,
94+
this.catchErrorOnErrorReturnType})
95+
: isConstConstructor = element is ConstructorElement2 && element.isConst,
9696
isGenerativeConstructor =
97-
element is ConstructorElement && !element.isFactory,
98-
isGenerator = element != null && element.isGenerator,
97+
element is ConstructorElement2 && !element.isFactory,
9998
inFactoryConstructor = _inFactoryConstructor(element),
10099
inStaticMethod = _inStaticMethod(element);
101100

102-
EnclosingExecutableContext.empty() : this(null);
103-
104-
factory EnclosingExecutableContext.tmp(ExecutableElement2? element,
105-
{bool? isAsynchronous, InterfaceType? catchErrorOnErrorReturnType}) {
106-
return EnclosingExecutableContext(
107-
element.asElement,
108-
isAsynchronous: isAsynchronous,
109-
catchErrorOnErrorReturnType: catchErrorOnErrorReturnType,
110-
);
111-
}
101+
EnclosingExecutableContext.empty()
102+
: this(null, isAsynchronous: false, isGenerator: false);
112103

113104
String? get displayName {
114-
return element2?.displayName;
105+
return element?.displayName;
115106
}
116107

117-
ExecutableElement2? get element2 => element.asElement2;
118-
119-
bool get isClosure {
120-
return element is FunctionElement && element!.displayName.isEmpty;
121-
}
108+
bool get isClosure => switch (element) {
109+
LocalFunctionElement(:var displayName) => displayName.isEmpty,
110+
_ => false,
111+
};
122112

123-
bool get isConstructor => element2 is ConstructorElement2;
113+
bool get isConstructor => element is ConstructorElement2;
124114

125-
bool get isFunction {
126-
if (element is FunctionElement) {
127-
return element!.displayName.isNotEmpty;
128-
}
129-
return element is PropertyAccessorElement;
130-
}
115+
bool get isFunction => switch (element) {
116+
LocalFunctionElement(:var displayName) => displayName.isNotEmpty,
117+
TopLevelFunctionElement(:var displayName) => displayName.isNotEmpty,
118+
PropertyAccessorElement2() => true,
119+
_ => false,
120+
};
131121

132-
bool get isMethod => element2 is MethodElement2;
122+
bool get isMethod => element is MethodElement2;
133123

134124
bool get isSynchronous => !isAsynchronous;
135125

136126
DartType get returnType {
137-
return catchErrorOnErrorReturnType ?? element2!.returnType;
127+
return catchErrorOnErrorReturnType ?? element!.returnType;
138128
}
139129

140-
static bool _inFactoryConstructor(Element? element) {
141-
var enclosing = element?.enclosingElement3;
130+
static bool _inFactoryConstructor(Element2? element) {
131+
var enclosing = element?.firstFragment.enclosingFragment;
142132
if (enclosing == null) {
143133
return false;
144134
}
145-
if (element is ConstructorElement) {
135+
if (element is ConstructorElement2) {
146136
return element.isFactory;
147137
}
148-
return _inFactoryConstructor(enclosing);
138+
return _inFactoryConstructor(enclosing.element);
149139
}
150140

151-
static bool _inStaticMethod(Element? element) {
152-
var enclosing = element?.enclosingElement3;
141+
static bool _inStaticMethod(Element2? element) {
142+
var enclosing = element?.firstFragment.enclosingFragment;
153143
if (enclosing == null) {
154144
return false;
155145
}
156-
if (enclosing is InterfaceElement || enclosing is ExtensionElement) {
157-
if (element is ExecutableElement) {
146+
if (enclosing is InterfaceFragment || enclosing is ExtensionFragment) {
147+
if (element is ExecutableElement2) {
158148
return element.isStatic;
159149
}
160150
}
161-
return _inStaticMethod(enclosing);
151+
return _inStaticMethod(enclosing.element);
162152
}
163153
}
164154

@@ -613,27 +603,32 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
613603
) {
614604
var fragment = node.declaredFragment!;
615605
var element = fragment.element;
616-
_withEnclosingExecutable2(element, () {
617-
_checkForNonConstGenerativeEnumConstructor(node);
618-
_checkForInvalidModifierOnBody(
619-
node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR);
620-
if (!_checkForConstConstructorWithNonConstSuper(node)) {
621-
_checkForConstConstructorWithNonFinalField(node, element);
622-
}
623-
_checkForRedirectingConstructorErrorCodes(node);
624-
_checkForConflictingInitializerErrorCodes(node);
625-
_checkForRecursiveConstructorRedirect(node, element);
626-
if (!_checkForRecursiveFactoryRedirect(node, element)) {
627-
_checkForAllRedirectConstructorErrorCodes(node);
628-
}
629-
_checkForUndefinedConstructorInInitializerImplicit(node);
630-
_checkForReturnInGenerativeConstructor(node);
631-
_checkAugmentations(
632-
augmentKeyword: node.augmentKeyword,
633-
element: fragment,
634-
);
635-
super.visitConstructorDeclaration(node);
636-
});
606+
_withEnclosingExecutable(
607+
element,
608+
() {
609+
_checkForNonConstGenerativeEnumConstructor(node);
610+
_checkForInvalidModifierOnBody(
611+
node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR);
612+
if (!_checkForConstConstructorWithNonConstSuper(node)) {
613+
_checkForConstConstructorWithNonFinalField(node, element);
614+
}
615+
_checkForRedirectingConstructorErrorCodes(node);
616+
_checkForConflictingInitializerErrorCodes(node);
617+
_checkForRecursiveConstructorRedirect(node, element);
618+
if (!_checkForRecursiveFactoryRedirect(node, element)) {
619+
_checkForAllRedirectConstructorErrorCodes(node);
620+
}
621+
_checkForUndefinedConstructorInInitializerImplicit(node);
622+
_checkForReturnInGenerativeConstructor(node);
623+
_checkAugmentations(
624+
augmentKeyword: node.augmentKeyword,
625+
element: fragment,
626+
);
627+
super.visitConstructorDeclaration(node);
628+
},
629+
isAsynchronous: fragment.isAsynchronous,
630+
isGenerator: fragment.isGenerator,
631+
);
637632
}
638633

639634
@override
@@ -976,24 +971,30 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
976971
_hiddenElements!.declare(element);
977972
}
978973

979-
_withEnclosingExecutable(element, () {
980-
TypeAnnotation? returnType = node.returnType;
981-
if (node.isSetter) {
982-
FunctionExpression functionExpression = node.functionExpression;
983-
_checkForWrongNumberOfParametersForSetter(
984-
node.name, functionExpression.parameters);
985-
_checkForNonVoidReturnTypeForSetter(returnType);
986-
}
987-
_checkForTypeAnnotationDeferredClass(returnType);
988-
_returnTypeVerifier.verifyReturnType(returnType);
989-
_checkForMainFunction1(node.name, node.declaredElement!);
990-
_checkForMainFunction2(node);
991-
_checkAugmentations(
992-
augmentKeyword: node.augmentKeyword,
993-
element: element,
994-
);
995-
super.visitFunctionDeclaration(node);
996-
});
974+
_withEnclosingExecutable(
975+
element.asElement2,
976+
() {
977+
TypeAnnotation? returnType = node.returnType;
978+
if (node.isSetter) {
979+
FunctionExpression functionExpression = node.functionExpression;
980+
_checkForWrongNumberOfParametersForSetter(
981+
node.name, functionExpression.parameters);
982+
_checkForNonVoidReturnTypeForSetter(returnType);
983+
}
984+
_checkForTypeAnnotationDeferredClass(returnType);
985+
_returnTypeVerifier.verifyReturnType(returnType);
986+
_checkForMainFunction1(node.name, node.declaredElement!);
987+
_checkForMainFunction2(node);
988+
_checkAugmentations(
989+
augmentKeyword: node.augmentKeyword,
990+
element: element,
991+
);
992+
super.visitFunctionDeclaration(node);
993+
},
994+
// TODO(pq): store fragment above.
995+
isAsynchronous: node.declaredFragment!.isAsynchronous,
996+
isGenerator: node.declaredFragment!.isGenerator,
997+
);
997998
}
998999

9991000
@override
@@ -1003,9 +1004,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
10031004
if (node.parent is FunctionDeclaration) {
10041005
super.visitFunctionExpression(node);
10051006
} else {
1006-
_withEnclosingExecutable2(node.declaredFragment!.element, () {
1007-
super.visitFunctionExpression(node);
1008-
});
1007+
var fragment = node.declaredFragment!;
1008+
_withEnclosingExecutable(
1009+
fragment.element,
1010+
() {
1011+
super.visitFunctionExpression(node);
1012+
},
1013+
isAsynchronous: fragment.isAsynchronous,
1014+
isGenerator: fragment.isGenerator,
1015+
);
10091016
}
10101017

10111018
_isInLateLocalVariable.removeLast();
@@ -1185,31 +1192,37 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
11851192
@override
11861193
void visitMethodDeclaration(covariant MethodDeclarationImpl node) {
11871194
var element = node.declaredElement!;
1188-
_withEnclosingExecutable(element, () {
1189-
var returnType = node.returnType;
1190-
if (node.isSetter) {
1191-
_checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
1192-
_checkForNonVoidReturnTypeForSetter(returnType);
1193-
} else if (node.isOperator) {
1194-
var hasWrongNumberOfParameters =
1195-
_checkForWrongNumberOfParametersForOperator(node);
1196-
if (!hasWrongNumberOfParameters) {
1197-
// If the operator has too many parameters including one or more
1198-
// optional parameters, only report one error.
1199-
_checkForOptionalParameterInOperator(node);
1195+
_withEnclosingExecutable(
1196+
element.asElement2,
1197+
() {
1198+
var returnType = node.returnType;
1199+
if (node.isSetter) {
1200+
_checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
1201+
_checkForNonVoidReturnTypeForSetter(returnType);
1202+
} else if (node.isOperator) {
1203+
var hasWrongNumberOfParameters =
1204+
_checkForWrongNumberOfParametersForOperator(node);
1205+
if (!hasWrongNumberOfParameters) {
1206+
// If the operator has too many parameters including one or more
1207+
// optional parameters, only report one error.
1208+
_checkForOptionalParameterInOperator(node);
1209+
}
1210+
_checkForNonVoidReturnTypeForOperator(node);
12001211
}
1201-
_checkForNonVoidReturnTypeForOperator(node);
1202-
}
1203-
_checkForExtensionDeclaresMemberOfObject(node);
1204-
_checkForTypeAnnotationDeferredClass(returnType);
1205-
_returnTypeVerifier.verifyReturnType(returnType);
1206-
_checkForWrongTypeParameterVarianceInMethod(node);
1207-
_checkAugmentations(
1208-
augmentKeyword: node.augmentKeyword,
1209-
element: element,
1210-
);
1211-
super.visitMethodDeclaration(node);
1212-
});
1212+
_checkForExtensionDeclaresMemberOfObject(node);
1213+
_checkForTypeAnnotationDeferredClass(returnType);
1214+
_returnTypeVerifier.verifyReturnType(returnType);
1215+
_checkForWrongTypeParameterVarianceInMethod(node);
1216+
_checkAugmentations(
1217+
augmentKeyword: node.augmentKeyword,
1218+
element: element,
1219+
);
1220+
super.visitMethodDeclaration(node);
1221+
},
1222+
// TODO(pq): store fragment above.
1223+
isAsynchronous: node.declaredFragment!.isAsynchronous,
1224+
isGenerator: node.declaredFragment!.isGenerator,
1225+
);
12131226
}
12141227

12151228
@override
@@ -1474,7 +1487,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
14741487
void visitSuperConstructorInvocation(SuperConstructorInvocation node) {
14751488
_requiredParametersVerifier.visitSuperConstructorInvocation(
14761489
node,
1477-
enclosingConstructor: _enclosingExecutable.element2.ifTypeOrNull(),
1490+
enclosingConstructor: _enclosingExecutable.element.ifTypeOrNull(),
14781491
);
14791492
_constArgumentsVerifier.visitSuperConstructorInvocation(node);
14801493
_isInConstructorInitializer = true;
@@ -6492,12 +6505,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
64926505
}
64936506

64946507
void _withEnclosingExecutable(
6495-
ExecutableElement element,
6496-
void Function() operation,
6497-
) {
6508+
ExecutableElement2 element,
6509+
void Function() operation, {
6510+
required bool isAsynchronous,
6511+
required bool isGenerator,
6512+
}) {
64986513
var current = _enclosingExecutable;
64996514
try {
6500-
_enclosingExecutable = EnclosingExecutableContext(element);
6515+
_enclosingExecutable = EnclosingExecutableContext(element,
6516+
isAsynchronous: isAsynchronous, isGenerator: isGenerator);
65016517
_returnTypeVerifier.enclosingExecutable = _enclosingExecutable;
65026518
operation();
65036519
} finally {
@@ -6506,13 +6522,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
65066522
}
65076523
}
65086524

6509-
void _withEnclosingExecutable2(
6510-
ExecutableElement2 element,
6511-
void Function() operation,
6512-
) {
6513-
_withEnclosingExecutable(element.asElement, operation);
6514-
}
6515-
65166525
void _withHiddenElements(List<Statement> statements, void Function() f) {
65176526
_hiddenElements = HiddenElements(_hiddenElements, statements);
65186527
try {

0 commit comments

Comments
 (0)