Skip to content

Commit a94e43a

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Fix analyzer/CFE integration of some more obscure invalid assignment cases.
This CL fixes handling of the following erroneous assignments: - Compound assignment to postfix increment (e.g. "x++ += y;") - Null-aware assignment to postfix increment (e.g. "x++ ??= y;") There is some code duplication in ContextAwareGenerator now; I will clean that up in a follow-up CL. Change-Id: If68bc267b67904562bf8c2e705123d04fdf0b69a Reviewed-on: https://dart-review.googlesource.com/71321 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent a69844c commit a94e43a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,38 @@ void f(num x, int y) {
972972
assertElement(yRef, findElement.parameter('y'));
973973
}
974974

975+
test_assign_to_postfix_increment_compound() async {
976+
addTestFile('''
977+
void f(num x, int y) {
978+
x++ += y;
979+
}
980+
''');
981+
await resolveTestFile();
982+
983+
var xRef = findNode.simple('x++');
984+
assertType(xRef, 'num');
985+
assertElement(xRef, findElement.parameter('x'));
986+
var yRef = findNode.simple('y;');
987+
assertType(yRef, 'int');
988+
assertElement(yRef, findElement.parameter('y'));
989+
}
990+
991+
test_assign_to_postfix_increment_null_aware() async {
992+
addTestFile('''
993+
void f(num x, int y) {
994+
x++ ??= y;
995+
}
996+
''');
997+
await resolveTestFile();
998+
999+
var xRef = findNode.simple('x++');
1000+
assertType(xRef, 'num');
1001+
assertElement(xRef, findElement.parameter('x'));
1002+
var yRef = findNode.simple('y;');
1003+
assertType(yRef, 'int');
1004+
assertElement(yRef, findElement.parameter('y'));
1005+
}
1006+
9751007
test_assign_to_prefix_increment() async {
9761008
addTestFile('''
9771009
void f(num x, int y) {

pkg/front_end/lib/src/fasta/kernel/expression_generator.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,16 @@ abstract class ContextAwareGenerator implements Generator {
958958
Expression buildNullAwareAssignment(
959959
Expression value, DartType type, int offset,
960960
{bool voidContext: false}) {
961-
return makeInvalidWrite(value);
961+
var lhs = buildSimpleRead();
962+
// The lhs expression needs to have a parent so that type inference can be
963+
// applied to it, but it doesn't matter what the parent is because the
964+
// lhs expression won't appear in the tree. So just give it a quick and
965+
// dirty parent.
966+
new VariableDeclaration.forValue(lhs);
967+
968+
return new IllegalAssignmentJudgment(value,
969+
assignmentOffset: offset, desugared: makeInvalidWrite(value))
970+
..write = lhs;
962971
}
963972

964973
@override
@@ -968,7 +977,16 @@ abstract class ContextAwareGenerator implements Generator {
968977
Procedure interfaceTarget,
969978
bool isPreIncDec: false,
970979
bool isPostIncDec: false}) {
971-
return makeInvalidWrite(value);
980+
var lhs = buildSimpleRead();
981+
// The lhs expression needs to have a parent so that type inference can be
982+
// applied to it, but it doesn't matter what the parent is because the
983+
// lhs expression won't appear in the tree. So just give it a quick and
984+
// dirty parent.
985+
new VariableDeclaration.forValue(lhs);
986+
987+
return new IllegalAssignmentJudgment(value,
988+
assignmentOffset: offset, desugared: makeInvalidWrite(value))
989+
..write = lhs;
972990
}
973991

974992
@override

0 commit comments

Comments
 (0)