Skip to content

Commit ca709d0

Browse files
scheglovCommit Queue
authored andcommitted
Resolve RelationalPattern.
Change-Id: Ic390c0c760fbcfb91384bf4c9d5a0981483b0f27 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262844 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent b704bba commit ca709d0

File tree

7 files changed

+423
-26
lines changed

7 files changed

+423
-26
lines changed

pkg/analyzer/lib/dart/ast/ast.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,6 +4467,9 @@ abstract class RedirectingConstructorInvocation
44674467
/// Clients may not extend, implement or mix-in this class.
44684468
@experimental
44694469
abstract class RelationalPattern implements DartPattern {
4470+
/// The element of the [operator] for the matched type.
4471+
MethodElement? get element;
4472+
44704473
/// Return the expression used to compute the operand.
44714474
Expression get operand;
44724475

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10813,13 +10813,18 @@ class RedirectingConstructorInvocationImpl extends ConstructorInitializerImpl
1081310813
@experimental
1081410814
class RelationalPatternImpl extends DartPatternImpl
1081510815
implements RelationalPattern {
10816-
@override
10817-
final ExpressionImpl operand;
10816+
ExpressionImpl _operand;
1081810817

1081910818
@override
1082010819
final Token operator;
1082110820

10822-
RelationalPatternImpl({required this.operator, required this.operand}) {
10821+
@override
10822+
MethodElement? element;
10823+
10824+
RelationalPatternImpl({
10825+
required this.operator,
10826+
required ExpressionImpl operand,
10827+
}) : _operand = operand {
1082310828
_becomeParentOf(operand);
1082410829
}
1082510830

@@ -10829,6 +10834,13 @@ class RelationalPatternImpl extends DartPatternImpl
1082910834
@override
1083010835
Token get endToken => operand.endToken;
1083110836

10837+
@override
10838+
ExpressionImpl get operand => _operand;
10839+
10840+
set operand(ExpressionImpl operand) {
10841+
_operand = _becomeParentOf(operand);
10842+
}
10843+
1083210844
@override
1083310845
ChildEntities get _childEntities => super._childEntities
1083410846
..addToken('operator', operator)
@@ -10847,7 +10859,10 @@ class RelationalPatternImpl extends DartPatternImpl
1084710859
DartType matchedType,
1084810860
Map<PromotableElement, VariableTypeInfo<AstNode, DartType>> typeInfos,
1084910861
MatchContext<AstNode, Expression> context) {
10850-
// TODO(scheglov) https://github.com/dart-lang/sdk/issues/50066
10862+
resolverVisitor.resolveRelationalPattern(
10863+
node: this,
10864+
matchedType: matchedType,
10865+
);
1085110866
}
1085210867

1085310868
@override

pkg/analyzer/lib/src/dart/ast/utilities.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,15 @@ class NodeReplacer extends ThrowingAstVisitor<bool> {
31503150
return visitNode(node);
31513151
}
31523152

3153+
@override
3154+
bool? visitRelationalPattern(covariant RelationalPatternImpl node) {
3155+
if (identical(node.operand, _oldNode)) {
3156+
node.operand = _newNode as ExpressionImpl;
3157+
return true;
3158+
}
3159+
return visitNode(node);
3160+
}
3161+
31533162
@override
31543163
bool visitRethrowExpression(RethrowExpression node) => visitNode(node);
31553164

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,38 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
11011101
}
11021102
}
11031103

1104+
void resolveRelationalPattern({
1105+
required RelationalPatternImpl node,
1106+
required DartType matchedType,
1107+
}) {
1108+
final String methodName;
1109+
if (node.operator.lexeme == '!=') {
1110+
methodName = '==';
1111+
} else {
1112+
methodName = node.operator.lexeme;
1113+
}
1114+
1115+
analyzeExpression(node.operand, matchedType);
1116+
node.operand = popRewrite()!;
1117+
1118+
final result = typePropertyResolver.resolve(
1119+
receiver: null,
1120+
receiverType: matchedType,
1121+
name: methodName,
1122+
propertyErrorEntity: node.operator,
1123+
nameErrorEntity: node,
1124+
);
1125+
1126+
node.element = result.getter as MethodElement?;
1127+
if (result.needsGetterError) {
1128+
errorReporter.reportErrorForToken(
1129+
CompileTimeErrorCode.UNDEFINED_OPERATOR,
1130+
node.operator,
1131+
[methodName, matchedType],
1132+
);
1133+
}
1134+
}
1135+
11041136
void setReadElement(Expression node, Element? element) {
11051137
DartType readType = DynamicTypeImpl.instance;
11061138
if (node is IndexExpression) {

pkg/analyzer/test/generated/utilities_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,22 @@ class A {
15131513
);
15141514
}
15151515

1516+
void test_relationalPattern() {
1517+
var findNode = _parseStringToFindNode(r'''
1518+
void f(x) {
1519+
if (x case > 0) {}
1520+
if (x case > 1) {}
1521+
}
1522+
''');
1523+
_assertReplacementForChildren<RelationalPattern>(
1524+
destination: findNode.relationalPattern('> 0'),
1525+
source: findNode.relationalPattern('> 1'),
1526+
childAccessors: [
1527+
(node) => node.operand,
1528+
],
1529+
);
1530+
}
1531+
15161532
void test_returnStatement() {
15171533
var findNode = _parseStringToFindNode(r'''
15181534
void f() {

0 commit comments

Comments
 (0)