Skip to content

Commit 951d589

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Refactor visitPrefixExpression, visitNamedExpression, and visitParenthesizedExpression.
Change-Id: I5a5fa0897d25aab3092fc526342c5af12a67e0a5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310770 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 1837435 commit 951d589

File tree

3 files changed

+30
-46
lines changed

3 files changed

+30
-46
lines changed

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,8 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
985985
}
986986

987987
@override
988-
Constant? visitNamedExpression(NamedExpression node) =>
989-
node.expression.accept(this);
988+
Constant visitNamedExpression(NamedExpression node) =>
989+
_getConstant(node.expression);
990990

991991
@override
992992
Constant? visitNamedType(NamedType node) {
@@ -1037,8 +1037,8 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
10371037
}
10381038

10391039
@override
1040-
Constant? visitParenthesizedExpression(ParenthesizedExpression node) =>
1041-
node.expression.accept(this);
1040+
Constant visitParenthesizedExpression(ParenthesizedExpression node) =>
1041+
_getConstant(node.expression);
10421042

10431043
@override
10441044
Constant? visitPrefixedIdentifier(PrefixedIdentifier node) {
@@ -1078,13 +1078,10 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
10781078
}
10791079

10801080
@override
1081-
Constant? visitPrefixExpression(PrefixExpression node) {
1082-
// TODO(kallentu): Remove unwrapping of Constant.
1083-
var operandConstant = node.operand.accept(this);
1084-
var operand = operandConstant is DartObjectImpl ? operandConstant : null;
1085-
if (operand != null && operand.isNull) {
1086-
_error(node, CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION);
1087-
return null;
1081+
Constant visitPrefixExpression(PrefixExpression node) {
1082+
var operand = _getConstant(node.operand);
1083+
if (operand is! DartObjectImpl) {
1084+
return operand;
10881085
}
10891086
if (node.staticElement?.enclosingElement is ExtensionElement) {
10901087
// TODO(kallentu): Don't report error here.
@@ -1103,7 +1100,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
11031100
// TODO(https://github.com/dart-lang/sdk/issues/47061): Use a specific
11041101
// error code.
11051102
_error(node, null);
1106-
return null;
1103+
return InvalidConstant(node, CompileTimeErrorCode.INVALID_CONSTANT);
11071104
}
11081105
}
11091106

@@ -1800,15 +1797,14 @@ class DartObjectComputer {
18001797
}
18011798
}
18021799

1803-
DartObjectImpl? bitNot(Expression node, DartObjectImpl? evaluationResult) {
1804-
if (evaluationResult != null) {
1805-
try {
1806-
return evaluationResult.bitNot(_typeSystem);
1807-
} on EvaluationException catch (exception) {
1808-
_errorReporter.reportErrorForNode(exception.errorCode, node);
1809-
}
1800+
Constant bitNot(Expression node, DartObjectImpl evaluationResult) {
1801+
try {
1802+
return evaluationResult.bitNot(_typeSystem);
1803+
} on EvaluationException catch (exception) {
1804+
// TODO(kallentu): Don't report error here.
1805+
_errorReporter.reportErrorForNode(exception.errorCode, node);
1806+
return InvalidConstant(node, exception.errorCode);
18101807
}
1811-
return null;
18121808
}
18131809

18141810
Constant castToType(
@@ -2013,16 +2009,14 @@ class DartObjectComputer {
20132009
return null;
20142010
}
20152011

2016-
DartObjectImpl? logicalNot(
2017-
Expression node, DartObjectImpl? evaluationResult) {
2018-
if (evaluationResult != null) {
2019-
try {
2020-
return evaluationResult.logicalNot(_typeSystem);
2021-
} on EvaluationException catch (exception) {
2022-
_errorReporter.reportErrorForNode(exception.errorCode, node);
2023-
}
2012+
Constant logicalNot(Expression node, DartObjectImpl evaluationResult) {
2013+
try {
2014+
return evaluationResult.logicalNot(_typeSystem);
2015+
} on EvaluationException catch (exception) {
2016+
// TODO(kallentu): Don't report error here.
2017+
_errorReporter.reportErrorForNode(exception.errorCode, node);
2018+
return InvalidConstant(node, exception.errorCode);
20242019
}
2025-
return null;
20262020
}
20272021

20282022
DartObjectImpl? logicalShiftRight(BinaryExpression node,
@@ -2049,15 +2043,14 @@ class DartObjectComputer {
20492043
return null;
20502044
}
20512045

2052-
DartObjectImpl? negated(Expression node, DartObjectImpl? evaluationResult) {
2053-
if (evaluationResult != null) {
2054-
try {
2055-
return evaluationResult.negated(_typeSystem);
2056-
} on EvaluationException catch (exception) {
2057-
_errorReporter.reportErrorForNode(exception.errorCode, node);
2058-
}
2046+
Constant negated(Expression node, DartObjectImpl evaluationResult) {
2047+
try {
2048+
return evaluationResult.negated(_typeSystem);
2049+
} on EvaluationException catch (exception) {
2050+
// TODO(kallentu): Don't report error here.
2051+
_errorReporter.reportErrorForNode(exception.errorCode, node);
2052+
return InvalidConstant(node, exception.errorCode);
20592053
}
2060-
return null;
20612054
}
20622055

20632056
DartObjectImpl? notEqual(BinaryExpression node, DartObjectImpl? leftOperand,

tests/language/patterns/invalid_const_pattern_binary_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,13 @@ method<T>(o) {
428428

429429
switch (o) {
430430
case ++o: // Error
431-
// ^^^
432-
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
433431
// ^
434432
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
435433
// [cfe] Not a constant expression.
436434
}
437435

438436
switch (o) {
439437
case --o: // Error
440-
// ^^^
441-
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
442438
// ^
443439
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
444440
// [cfe] Not a constant expression.

tests/language/patterns/invalid_const_pattern_test.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class Class {
4444
case super(): // Error
4545
// ^^^^^
4646
// [analyzer] COMPILE_TIME_ERROR.INVOCATION_OF_NON_FUNCTION_EXPRESSION
47-
// ^^^^^^^
4847
// [cfe] Method invocation is not a constant expression.
4948
}
5049

@@ -198,8 +197,6 @@ class Class {
198197

199198
switch (o) {
200199
case ++variable: // Error
201-
// ^^^^^^^^^^
202-
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
203200
// ^^^^^^^^
204201
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
205202
// [cfe] Not a constant expression.
@@ -355,8 +352,6 @@ class Class {
355352

356353
switch (o) {
357354
case const ++variable: // Error
358-
// ^^^^^^^^^^
359-
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
360355
// ^^^^^^^^
361356
// [analyzer] COMPILE_TIME_ERROR.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION
362357
// [cfe] Not a constant expression.

0 commit comments

Comments
 (0)