Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(707)

Unified Diff: pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Issue 2775633004: Report errors on non-final fields in const classes. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/co19/co19-kernel.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
index a14190a032e685fe2dc6eb303795ebc7f4a86ca4..8dc08064a014a78b6cfbeb3885d751ee76ee2be5 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
@@ -114,6 +114,13 @@ class KernelTarget extends TargetImplementation {
loader = createLoader();
}
+ void addError(file, int charOffset, String message) {
+ Uri uri = file is String ? Uri.parse(file) : file;
+ InputError error = new InputError(uri, charOffset, message);
+ print(error.format());
+ errors.add(error);
+ }
+
SourceLoader<Library> createLoader() => new SourceLoader<Library>(this);
void addSourceInformation(
@@ -551,7 +558,11 @@ class KernelTarget extends TargetImplementation {
/// Edition](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-408.pdf):
Constructor superTarget;
List<Field> uninitializedFields = <Field>[];
+ List<Field> nonFinalFields = <Field>[];
for (Field field in cls.fields) {
+ if (field.isInstanceMember && !field.isFinal) {
+ nonFinalFields.add(field);
+ }
if (field.initializer == null) {
uninitializedFields.add(field);
}
@@ -567,16 +578,11 @@ class KernelTarget extends TargetImplementation {
superTarget ??= defaultSuperConstructor(cls);
Initializer initializer;
if (superTarget == null) {
- Uri uri = constructor.enclosingClass.fileUri == null
- ? null
- : Uri.parse(constructor.enclosingClass.fileUri);
- InputError error = new InputError(
- uri,
+ addError(
+ constructor.enclosingClass.fileUri,
constructor.fileOffset,
"${cls.superclass.name} has no constructor that takes zero"
" arguments.");
- print(error.format());
- errors.add(error);
initializer = new InvalidInitializer();
} else {
initializer =
@@ -599,6 +605,15 @@ class KernelTarget extends TargetImplementation {
}
}
fieldInitializers[constructor] = myFieldInitializers;
+ if (constructor.isConst && nonFinalFields.isNotEmpty) {
+ addError(constructor.enclosingClass.fileUri, constructor.fileOffset,
+ "Constructor is marked 'const' so all fields must be final.");
+ for (Field field in nonFinalFields) {
+ addError(constructor.enclosingClass.fileUri, field.fileOffset,
+ "Field isn't final, but constructor is 'const'.");
+ }
+ nonFinalFields.clear();
+ }
}
}
Set<Field> initializedFields;
« no previous file with comments | « no previous file | tests/co19/co19-kernel.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698