Fix error reporting if a for-in statement attempts to assign to a prefix.

This was regressed by https://dart-review.googlesource.com/c/sdk/+/66402.

Change-Id: Icad3d2d33cb4db0321534a6246c8cc5f50444edf
Reviewed-on: https://dart-review.googlesource.com/66416
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Paul Berry 2018-07-25 14:12:13 +00:00 committed by commit-bot@chromium.org
parent 79ed0553e9
commit 432e59e589
8 changed files with 34 additions and 48 deletions

View file

@ -256,7 +256,7 @@ class ResolutionStorer
_store(writeLocation,
declaration: writeVariableBinder.fileOffset,
inferredType: writeType);
} else {
} else if (writeLocation != null) {
_store(writeLocation,
reference: writeTarget,
isWriteReference: true,

View file

@ -6368,13 +6368,7 @@ class C {
''');
await computeAnalysisResult(source);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
CompileTimeErrorCode.NOT_AN_LVALUE
]
: [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}
@ -6388,13 +6382,7 @@ f() {
''');
await computeAnalysisResult(source);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
CompileTimeErrorCode.NOT_AN_LVALUE
]
: [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}
@ -6410,13 +6398,7 @@ class C {
''');
await computeAnalysisResult(source);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
CompileTimeErrorCode.NOT_AN_LVALUE
]
: [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}
@ -6430,13 +6412,7 @@ f() {
''');
await computeAnalysisResult(source);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
CompileTimeErrorCode.NOT_AN_LVALUE
]
: [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}
@ -6649,13 +6625,7 @@ f() {
''');
await computeAnalysisResult(source);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
CompileTimeErrorCode.NOT_AN_LVALUE
]
: [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
verify([source]);
}

View file

@ -2615,6 +2615,17 @@ class C<T> {
expect(fElement.type, typeProvider.listType.instantiate([tElement.type]));
}
test_for_in_assign_to_prefix() async {
addTestFile('''
import "dart:core" as prefix;
main() {
for (prefix in []) {}
}
''');
await resolveTestFile();
}
test_formalParameter_functionTyped() async {
addTestFile(r'''
class A {

View file

@ -1233,7 +1233,9 @@ abstract class PrefixUseGenerator implements Generator {
String get debugName => "PrefixUseGenerator";
@override
Generator asLvalue() => makeNonLValueGenerator();
Generator asLvalue() {
return new NonLvalueGenerator(helper, token, makeError(), null);
}
@override
Expression buildSimpleRead() => makeInvalidRead();
@ -1299,10 +1301,12 @@ abstract class PrefixUseGenerator implements Generator {
@override
Expression makeInvalidRead() {
return new SyntheticExpressionJudgment(helper.buildCompileTimeError(
messageCantUsePrefixAsExpression,
offsetForToken(token),
lengthForToken(token)));
return new SyntheticExpressionJudgment(makeError());
}
Expression makeError() {
return helper.buildCompileTimeError(messageCantUsePrefixAsExpression,
offsetForToken(token), lengthForToken(token));
}
@override

View file

@ -1588,7 +1588,7 @@ class IllegalAssignmentJudgment extends SyntheticExpressionJudgment {
// inference currently requires expressions to have a parent (so that it can
// replace them with their desugared equivalents), so create placeholder
// parents if needed.
if (lhs.parent == null) {
if (lhs != null && lhs.parent == null) {
new ExpressionStatement(lhs);
}
if (rhs != null && rhs.parent == null) {
@ -1605,7 +1605,9 @@ class IllegalAssignmentJudgment extends SyntheticExpressionJudgment {
ShadowTypeInferrer inferrer,
Factory<Expression, Statement, Initializer, Type> factory,
DartType typeContext) {
inferrer.inferExpression(factory, lhs, const UnknownType(), false);
if (lhs != null) {
inferrer.inferExpression(factory, lhs, const UnknownType(), false);
}
if (assignmentOffset != -1) {
inferrer.listener.invalidAssignment(this, assignmentOffset);
}

View file

@ -34,7 +34,6 @@ CantInferPackagesFromPackageUri/example: Fail
CantInferTypeDueToCircularity/dart2jsCode: Fail
CantInferTypeDueToCircularity/example: Fail
CantInferTypeDueToInconsistentOverrides/example: Fail
CantUsePrefixAsExpression/script: Fail
CantUseSuperBoundedTypeForInstanceCreation/analyzerCode: Fail
CantUseSuperBoundedTypeForInstanceCreation/example: Fail
ColonInPlaceOfIn/example: Fail

View file

@ -29,7 +29,7 @@ class Fisk extends core::Object {
core::print(self::Fisk);
}
for (final dynamic #t3 in x) {
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:17:10: Error: Can't assign to this.
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:17:10: Error: A prefix can't be used as an expression.
for (collection in x) {
^^^^^^^^^^";
core::print(invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:18:13: Error: A prefix can't be used as an expression.
@ -62,7 +62,7 @@ static method main(dynamic arguments) → dynamic {
core::print(self::Fisk);
}
for (final dynamic #t8 in arguments) {
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:37:8: Error: Can't assign to this.
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:37:8: Error: A prefix can't be used as an expression.
for (collection in arguments) {
^^^^^^^^^^";
core::print(invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:38:11: Error: A prefix can't be used as an expression.

View file

@ -29,7 +29,7 @@ class Fisk extends core::Object {
core::print(self::Fisk);
}
for (final dynamic #t3 in x) {
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:17:10: Error: Can't assign to this.
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:17:10: Error: A prefix can't be used as an expression.
for (collection in x) {
^^^^^^^^^^";
core::print(invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:18:13: Error: A prefix can't be used as an expression.
@ -62,7 +62,7 @@ static method main(dynamic arguments) → dynamic {
core::print(self::Fisk);
}
for (final dynamic #t8 in arguments) {
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:37:8: Error: Can't assign to this.
invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:37:8: Error: A prefix can't be used as an expression.
for (collection in arguments) {
^^^^^^^^^^";
core::print(invalid-expression "pkg/front_end/testcases/rasta/unresolved_for_in.dart:38:11: Error: A prefix can't be used as an expression.