Issue 46758. LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR.

Does not pass https://dart-review.googlesource.com/c/sdk/+/211140
yet, because requires one more fix, not reporting
CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD for const redirect constructors.

Bug: https://github.com/dart-lang/sdk/issues/46985
Bug: https://github.com/dart-lang/sdk/issues/46758
Change-Id: If6289b595545216d2d4e247214c5176a6a9d4a31
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/209222
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-08-25 15:30:47 +00:00 committed by commit-bot@chromium.org
parent 8d5c80ce83
commit 965af148f3
4 changed files with 25 additions and 5 deletions

View file

@ -7542,8 +7542,10 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
// }
// ```
static const CompileTimeErrorCode LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR =
CompileTimeErrorCode('LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR',
"Can't have a late final field in a class with a const constructor.",
CompileTimeErrorCode(
'LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR',
"Can't have a late final field in a class with a generative "
"const constructor.",
correction: "Try removing the 'late' modifier, or don't declare "
"'const' constructors.",
hasPublishedDocs: true);

View file

@ -2817,8 +2817,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
// The field is in an extension and should be handled elsewhere.
return;
}
var hasConstConstructor = enclosingClass.constructors.any((c) => c.isConst);
if (!hasConstConstructor) return;
var hasGenerativeConstConstructor =
_enclosingClass!.constructors.any((c) => c.isConst && !c.isFactory);
if (!hasGenerativeConstConstructor) return;
errorReporter.reportErrorForToken(
CompileTimeErrorCode.LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR,

View file

@ -62,6 +62,22 @@ class A {
''');
}
test_class_hasConstFactoryConstructor() async {
await assertNoErrorsInCode('''
class Base {
Base();
const factory Base.empty() = _Empty;
late final int property;
}
class _Empty implements Base {
const _Empty();
int get property => 0;
set property(_) {}
}
''');
}
test_class_noConstConstructor() async {
await assertNoErrorsInCode('''
class A {

View file

@ -7438,7 +7438,7 @@ void f() {
### late_final_field_with_const_constructor
_Can't have a late final field in a class with a const constructor._
_Can't have a late final field in a class with a generative const constructor._
#### Description