Skip to content

Commit 965af14

Browse files
scheglovcommit-bot@chromium.org
authored and
commit-bot@chromium.org
committed
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: #46985 Bug: #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>
1 parent 8d5c80c commit 965af14

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7542,8 +7542,10 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
75427542
// }
75437543
// ```
75447544
static const CompileTimeErrorCode LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR =
7545-
CompileTimeErrorCode('LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR',
7546-
"Can't have a late final field in a class with a const constructor.",
7545+
CompileTimeErrorCode(
7546+
'LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR',
7547+
"Can't have a late final field in a class with a generative "
7548+
"const constructor.",
75477549
correction: "Try removing the 'late' modifier, or don't declare "
75487550
"'const' constructors.",
75497551
hasPublishedDocs: true);

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,8 +2817,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
28172817
// The field is in an extension and should be handled elsewhere.
28182818
return;
28192819
}
2820-
var hasConstConstructor = enclosingClass.constructors.any((c) => c.isConst);
2821-
if (!hasConstConstructor) return;
2820+
2821+
var hasGenerativeConstConstructor =
2822+
_enclosingClass!.constructors.any((c) => c.isConst && !c.isFactory);
2823+
if (!hasGenerativeConstConstructor) return;
28222824

28232825
errorReporter.reportErrorForToken(
28242826
CompileTimeErrorCode.LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR,

pkg/analyzer/test/src/diagnostics/late_final_field_with_const_constructor_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ class A {
6262
''');
6363
}
6464

65+
test_class_hasConstFactoryConstructor() async {
66+
await assertNoErrorsInCode('''
67+
class Base {
68+
Base();
69+
const factory Base.empty() = _Empty;
70+
late final int property;
71+
}
72+
73+
class _Empty implements Base {
74+
const _Empty();
75+
int get property => 0;
76+
set property(_) {}
77+
}
78+
''');
79+
}
80+
6581
test_class_noConstConstructor() async {
6682
await assertNoErrorsInCode('''
6783
class A {

pkg/analyzer/tool/diagnostics/diagnostics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7438,7 +7438,7 @@ void f() {
74387438

74397439
### late_final_field_with_const_constructor
74407440

7441-
_Can't have a late final field in a class with a const constructor._
7441+
_Can't have a late final field in a class with a generative const constructor._
74427442

74437443
#### Description
74447444

0 commit comments

Comments
 (0)