Don't report ASSIGNMENT_TO_FINAL or ASSIGNMENT_TO_FINAL_LOCAL for late variables.

R=brianwilkerson@google.com

Change-Id: I72feef1b1e78ab787fa538d7ff87e20c04a644fc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115269
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2019-09-03 19:38:16 +00:00 committed by commit-bot@chromium.org
parent cb0abfef47
commit 67ac9cf52c
3 changed files with 61 additions and 20 deletions

View file

@ -2087,6 +2087,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
_errorReporter.reportErrorForNode(
StaticWarningCode.ASSIGNMENT_TO_CONST, expression);
} else if (element.isFinal) {
if (element is PropertyInducingElement && element.isLate ||
element is LocalVariableElement && element.isLate) {
return;
}
if (element is FieldElementImpl) {
if (element.setter == null && element.isSynthetic) {
_errorReporter.reportErrorForNode(

View file

@ -2,8 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
@ -11,6 +13,7 @@ import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(AssignmentToFinalLocalTest);
defineReflectiveTests(AssignmentToFinalLocalWithNnbdTest);
});
}
@ -27,6 +30,18 @@ f() {
]);
}
test_localVariable_inForEach() async {
await assertErrorsInCode('''
f() {
final x = 0;
for (x in <int>[1, 2]) {
print(x);
}
}''', [
error(StaticWarningCode.ASSIGNMENT_TO_FINAL_LOCAL, 28, 1),
]);
}
test_localVariable_plusEq() async {
await assertErrorsInCode('''
f() {
@ -113,18 +128,6 @@ f() {
]);
}
test_localVariable_inForEach() async {
await assertErrorsInCode('''
f() {
final x = 0;
for (x in <int>[1, 2]) {
print(x);
}
}''', [
error(StaticWarningCode.ASSIGNMENT_TO_FINAL_LOCAL, 28, 1),
]);
}
test_topLevelVariable() async {
await assertErrorsInCode('''
final x = 0;
@ -133,3 +136,33 @@ f() { x = 1; }''', [
]);
}
}
@reflectiveTest
class AssignmentToFinalLocalWithNnbdTest extends DriverResolutionTest {
@override
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.non_nullable]);
test_localVariable_late() async {
await assertNoErrorsInCode('''
void f() {
late final int a;
late final int b = 0;
a = 1;
b = 1;
}
''');
}
test_topLevelVariable_late() async {
await assertNoErrorsInCode('''
late final int a;
late final int b = 0;
void f() {
a = 1;
b = 1;
}
''');
}
}

View file

@ -3,11 +3,11 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
import 'package:analyzer/src/error/codes.dart';
main() {
defineReflectiveSuite(() {
@ -52,24 +52,28 @@ class AssignmentToFinalWithNnbdTest extends AssignmentToFinalTest {
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.non_nullable]);
@failingTest
test_field_late() async {
await assertNoErrorsInCode('''
class A {
final a;
late final int a;
late final int b = 0;
void m() {
a = 1;
b = 1;
}
}
''');
}
@failingTest
test_localVariable_late() async {
test_field_static_late() async {
await assertNoErrorsInCode('''
void f() {
final a;
a = 1;
class A {
static late final int a;
static late final int b = 0;
void m() {
a = 1;
b = 1;
}
}
''');
}