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

Unified Diff: runtime/vm/parser.cc

Issue 2953843003: Reintroducing parser changes to cause compile-time errors when trying to reinitialize a final field… (Closed)
Patch Set: Created 3 years, 6 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 | « runtime/vm/parser.h ('k') | tests/co19/co19-dartium.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/parser.cc
diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc
index 48718b88ec666376e80bd1e0cef991294274be36..bad94b2f98c82040d5863d738ccced35e1ff2c59 100644
--- a/runtime/vm/parser.cc
+++ b/runtime/vm/parser.cc
@@ -3022,6 +3022,8 @@ AstNode* Parser::CheckDuplicateFieldInit(
AstNode* init_value) {
ASSERT(!field->is_static());
AstNode* result = NULL;
+ const String& field_name = String::Handle(field->name());
+ String& initialized_name = String::Handle(Z);
// The initializer_list is divided into two sections. The sections
// are separated by a NULL entry: [f0, ... fn, NULL, fn+1, ...]
@@ -3036,6 +3038,13 @@ AstNode* Parser::CheckDuplicateFieldInit(
if (initialized_field == NULL) {
break;
}
+
+ initialized_name ^= initialized_field->name();
+ if (initialized_name.Equals(field_name) && field->has_initializer()) {
+ ReportError(init_pos, "final field '%s' is already initialized.",
+ field_name.ToCString());
+ }
+
if (initialized_field->raw() == field->raw()) {
// This final field has been initialized by an inlined
// initializer expression. This is a runtime error.
@@ -5256,12 +5265,52 @@ void Parser::AddImplicitConstructor(const Class& cls) {
}
+void Parser::CheckFinalInitializationConflicts(const ClassDesc* class_desc,
+ const MemberDesc* member) {
+ const ParamList* params = &member->params;
+ if (!params->has_field_initializer) {
+ return;
+ }
+
+ const ZoneGrowableArray<ParamDesc>& parameters = *params->parameters;
+ const GrowableArray<const Field*>& fields = class_desc->fields();
+ String& field_name = String::Handle(Z);
+
+ for (intptr_t p = 0; p < parameters.length(); p++) {
+ const ParamDesc& current_param = parameters[p];
+ if (!current_param.is_field_initializer) {
+ continue;
+ }
+
+ const String& param_name = *current_param.name;
+ for (intptr_t i = 0; i < fields.length(); i++) {
+ const Field* current_field = fields.At(i);
+ if (!current_field->is_final() || !current_field->has_initializer()) {
+ continue;
+ }
+
+ field_name ^= current_field->name();
+ if (param_name.Equals(field_name)) {
+ ReportError(current_param.name_pos,
+ "final field '%s' is already initialized.",
+ param_name.ToCString());
+ }
+ }
+ }
+}
+
+
// Check for cycles in constructor redirection.
void Parser::CheckConstructors(ClassDesc* class_desc) {
// Check for cycles in constructor redirection.
const GrowableArray<MemberDesc>& members = class_desc->members();
for (int i = 0; i < members.length(); i++) {
MemberDesc* member = &members[i];
+ if (member->IsConstructor()) {
+ // Check that our constructors don't try and reinitialize an initialized
+ // final variable.
+ CheckFinalInitializationConflicts(class_desc, member);
+ }
if (member->redirect_name == NULL) {
continue;
}
« no previous file with comments | « runtime/vm/parser.h ('k') | tests/co19/co19-dartium.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698