Skip to content

Commit ca86634

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Share checkForInvalidAssignment betwen resolver and ErrorVerifier.
In order to implement "why not promoted" functionality for variable declaration initializers, we will need to call checkForInvalidAssignment from the resolver, so that we can pass it failed promotion information. So move it into the ErrorDetectionHelpers mixin, along with _checkForAssignableExpression (which it calls). _checkForAssignableExpressionAtType can now be private, since all of its callers are now in ErrorDetectionHelpers. Bug: #44898 Change-Id: I6655e3a655d330d61459692804854c89c0dcff70 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192612 Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 20d9c6e commit ca86634

File tree

3 files changed

+85
-85
lines changed

3 files changed

+85
-85
lines changed

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

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mixin ErrorDetectionHelpers {
3939
return;
4040
}
4141

42-
checkForAssignableExpressionAtType(
42+
_checkForAssignableExpressionAtType(
4343
expression, actualStaticType, expectedStaticType, errorCode,
4444
whyNotPromotedInfo: whyNotPromotedInfo);
4545
}
@@ -81,33 +81,41 @@ mixin ErrorDetectionHelpers {
8181
whyNotPromotedInfo);
8282
}
8383

84-
bool checkForAssignableExpressionAtType(
85-
Expression expression,
86-
DartType actualStaticType,
87-
DartType expectedStaticType,
88-
ErrorCode errorCode,
89-
{Map<DartType, NonPromotionReason> Function()? whyNotPromotedInfo}) {
90-
if (!typeSystem.isAssignableTo(actualStaticType, expectedStaticType)) {
91-
AstNode getErrorNode(AstNode node) {
92-
if (node is CascadeExpression) {
93-
return getErrorNode(node.target);
94-
}
95-
if (node is ParenthesizedExpression) {
96-
return getErrorNode(node.expression);
97-
}
98-
return node;
99-
}
84+
/// Verify that the given left hand side ([lhs]) and right hand side ([rhs])
85+
/// represent a valid assignment.
86+
///
87+
/// See [CompileTimeErrorCode.INVALID_ASSIGNMENT].
88+
void checkForInvalidAssignment(Expression? lhs, Expression? rhs) {
89+
if (lhs == null || rhs == null) {
90+
return;
91+
}
10092

101-
errorReporter.reportErrorForNode(
102-
errorCode,
103-
getErrorNode(expression),
104-
[actualStaticType, expectedStaticType],
105-
computeWhyNotPromotedMessages(
106-
expression, expression, whyNotPromotedInfo?.call()),
107-
);
108-
return false;
93+
if (lhs is IndexExpression &&
94+
identical(lhs.realTarget.staticType, NeverTypeImpl.instance) ||
95+
lhs is PrefixedIdentifier &&
96+
identical(lhs.prefix.staticType, NeverTypeImpl.instance) ||
97+
lhs is PropertyAccess &&
98+
identical(lhs.realTarget.staticType, NeverTypeImpl.instance)) {
99+
return;
109100
}
110-
return true;
101+
102+
DartType leftType;
103+
var parent = lhs.parent;
104+
if (parent is AssignmentExpression && parent.leftHandSide == lhs) {
105+
leftType = parent.writeType!;
106+
} else {
107+
var leftVariableElement = getVariableElement(lhs);
108+
leftType = (leftVariableElement == null)
109+
? lhs.typeOrThrow
110+
: leftVariableElement.type;
111+
}
112+
113+
if (!leftType.isVoid && checkForUseOfVoidResult(rhs)) {
114+
return;
115+
}
116+
117+
_checkForAssignableExpression(
118+
rhs, leftType, CompileTimeErrorCode.INVALID_ASSIGNMENT);
111119
}
112120

113121
/// Check for situations where the result of a method or function is used,
@@ -148,6 +156,18 @@ mixin ErrorDetectionHelpers {
148156
SyntacticEntity errorEntity,
149157
Map<DartType, NonPromotionReason>? whyNotPromoted);
150158

159+
/// Return the variable element represented by the given [expression], or
160+
/// `null` if there is no such element.
161+
VariableElement? getVariableElement(Expression? expression) {
162+
if (expression is Identifier) {
163+
var element = expression.staticElement;
164+
if (element is VariableElement) {
165+
return element;
166+
}
167+
}
168+
return null;
169+
}
170+
151171
/// Verify that the given [expression] can be assigned to its corresponding
152172
/// parameters.
153173
///
@@ -167,4 +187,40 @@ mixin ErrorDetectionHelpers {
167187
expression, expectedStaticType, expression.typeOrThrow, errorCode,
168188
whyNotPromotedInfo: whyNotPromotedInfo);
169189
}
190+
191+
bool _checkForAssignableExpression(
192+
Expression expression, DartType expectedStaticType, ErrorCode errorCode) {
193+
DartType actualStaticType = expression.typeOrThrow;
194+
return _checkForAssignableExpressionAtType(
195+
expression, actualStaticType, expectedStaticType, errorCode);
196+
}
197+
198+
bool _checkForAssignableExpressionAtType(
199+
Expression expression,
200+
DartType actualStaticType,
201+
DartType expectedStaticType,
202+
ErrorCode errorCode,
203+
{Map<DartType, NonPromotionReason> Function()? whyNotPromotedInfo}) {
204+
if (!typeSystem.isAssignableTo(actualStaticType, expectedStaticType)) {
205+
AstNode getErrorNode(AstNode node) {
206+
if (node is CascadeExpression) {
207+
return getErrorNode(node.target);
208+
}
209+
if (node is ParenthesizedExpression) {
210+
return getErrorNode(node.expression);
211+
}
212+
return node;
213+
}
214+
215+
errorReporter.reportErrorForNode(
216+
errorCode,
217+
getErrorNode(expression),
218+
[actualStaticType, expectedStaticType],
219+
computeWhyNotPromotedMessages(
220+
expression, expression, whyNotPromotedInfo?.call()),
221+
);
222+
return false;
223+
}
224+
return true;
225+
}
170226
}

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

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
350350
operatorType == TokenType.QUESTION_QUESTION_EQ) {
351351
// Already handled in the assignment resolver.
352352
if (lhs is! SimpleIdentifier) {
353-
_checkForInvalidAssignment(lhs, rhs);
353+
checkForInvalidAssignment(lhs, rhs);
354354
}
355355
} else {
356356
checkForArgumentTypeNotAssignableForArgument(rhs);
@@ -572,7 +572,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
572572

573573
@override
574574
void visitDefaultFormalParameter(DefaultFormalParameter node) {
575-
_checkForInvalidAssignment(node.identifier, node.defaultValue);
575+
checkForInvalidAssignment(node.identifier, node.defaultValue);
576576
super.visitDefaultFormalParameter(node);
577577
}
578578

@@ -1249,7 +1249,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
12491249
SimpleIdentifier nameNode = node.name;
12501250
var initializerNode = node.initializer;
12511251
// do checks
1252-
_checkForInvalidAssignment(nameNode, initializerNode);
12531252
_checkForImplicitDynamicIdentifier(node, nameNode);
12541253
_checkForAbstractOrExternalVariableInitializer(node);
12551254
// visit name
@@ -1525,13 +1524,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
15251524
}
15261525
}
15271526

1528-
bool _checkForAssignableExpression(
1529-
Expression expression, DartType expectedStaticType, ErrorCode errorCode) {
1530-
DartType actualStaticType = expression.typeOrThrow;
1531-
return checkForAssignableExpressionAtType(
1532-
expression, actualStaticType, expectedStaticType, errorCode);
1533-
}
1534-
15351527
/// Verify that the given [expression] is not final.
15361528
///
15371529
/// See [StaticWarningCode.ASSIGNMENT_TO_CONST],
@@ -2753,43 +2745,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
27532745
}
27542746
}
27552747

2756-
/// Verify that the given left hand side ([lhs]) and right hand side ([rhs])
2757-
/// represent a valid assignment.
2758-
///
2759-
/// See [CompileTimeErrorCode.INVALID_ASSIGNMENT].
2760-
void _checkForInvalidAssignment(Expression? lhs, Expression? rhs) {
2761-
if (lhs == null || rhs == null) {
2762-
return;
2763-
}
2764-
2765-
if (lhs is IndexExpression &&
2766-
identical(lhs.realTarget.staticType, NeverTypeImpl.instance) ||
2767-
lhs is PrefixedIdentifier &&
2768-
identical(lhs.prefix.staticType, NeverTypeImpl.instance) ||
2769-
lhs is PropertyAccess &&
2770-
identical(lhs.realTarget.staticType, NeverTypeImpl.instance)) {
2771-
return;
2772-
}
2773-
2774-
DartType leftType;
2775-
var parent = lhs.parent;
2776-
if (parent is AssignmentExpression && parent.leftHandSide == lhs) {
2777-
leftType = parent.writeType!;
2778-
} else {
2779-
var leftVariableElement = getVariableElement(lhs);
2780-
leftType = (leftVariableElement == null)
2781-
? lhs.typeOrThrow
2782-
: leftVariableElement.type;
2783-
}
2784-
2785-
if (!leftType.isVoid && checkForUseOfVoidResult(rhs)) {
2786-
return;
2787-
}
2788-
2789-
_checkForAssignableExpression(
2790-
rhs, leftType, CompileTimeErrorCode.INVALID_ASSIGNMENT);
2791-
}
2792-
27932748
/// Check the given [initializer] to ensure that the field being initialized
27942749
/// is a valid field. The [fieldName] is the field name from the
27952750
/// [ConstructorFieldInitializer]. The [staticElement] is the static element
@@ -5167,18 +5122,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
51675122

51685123
return fields.toList();
51695124
}
5170-
5171-
/// Return the variable element represented by the given [expression], or
5172-
/// `null` if there is no such element.
5173-
static VariableElement? getVariableElement(Expression? expression) {
5174-
if (expression is Identifier) {
5175-
var element = expression.staticElement;
5176-
if (element is VariableElement) {
5177-
return element;
5178-
}
5179-
}
5180-
return null;
5181-
}
51825125
}
51835126

51845127
/// A record of the elements that will be declared in some scope (block), but

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,7 @@ class ResolverVisitor extends ScopedVisitor with ErrorDetectionHelpers {
21552155
isFinal: parent.isFinal, isLate: parent.isLate);
21562156
}
21572157
}
2158+
checkForInvalidAssignment(node.name, node.initializer);
21582159
}
21592160

21602161
@override

0 commit comments

Comments
 (0)