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

Unified Diff: sdk/lib/_internal/compiler/implementation/resolution/members.dart

Issue 25504002: Emit a compile-time error for uninitialized final variables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: dart2dart status file. Created 7 years, 2 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 | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/resolution/members.dart
diff --git a/sdk/lib/_internal/compiler/implementation/resolution/members.dart b/sdk/lib/_internal/compiler/implementation/resolution/members.dart
index 2595674855ccde764ab95078df4959167a7e795f..980b87d5a78814f315e17832f0748a29582d006e 100644
--- a/sdk/lib/_internal/compiler/implementation/resolution/members.dart
+++ b/sdk/lib/_internal/compiler/implementation/resolution/members.dart
@@ -449,12 +449,15 @@ class ResolverTask extends CompilerTask {
visitor.useElement(tree, element);
SendSet send = tree.asSendSet();
+ Modifiers modifiers = element.modifiers;
if (send != null) {
// TODO(johnniwinther): Avoid analyzing initializers if
// [Compiler.analyzeSignaturesOnly] is set.
visitor.visit(send.arguments.head);
- } else if (element.modifiers.isConst()) {
+ } else if (modifiers.isConst()) {
compiler.reportError(element, MessageKind.CONST_WITHOUT_INITIALIZER);
+ } else if (modifiers.isFinal() && !element.isInstanceMember()) {
+ compiler.reportError(element, MessageKind.FINAL_WITHOUT_INITIALIZER);
}
if (Elements.isStaticOrTopLevelField(element)) {
@@ -1806,6 +1809,11 @@ class ResolverVisitor extends MappingVisitor<Element> {
int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION
| ElementCategory.IMPLIES_TYPE;
+ /// When visiting the type declaration of the variable in a [ForIn] loop,
+ /// the initializer of the variable is implicit and we should not emit an
+ /// error when verifying that all final variables are initialized.
+ bool allowFinalWithoutInitializer = false;
+
// TODO(ahe): Find a way to share this with runtime implementation.
static final RegExp symbolValidationPattern =
new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|'
@@ -3026,7 +3034,11 @@ class ResolverVisitor extends MappingVisitor<Element> {
visit(node.expression);
Scope blockScope = new BlockScope(scope);
Node declaration = node.declaredIdentifier;
+
+ bool oldAllowFinalWithoutInitializer = allowFinalWithoutInitializer;
+ allowFinalWithoutInitializer = true;
visitIn(declaration, blockScope);
+ allowFinalWithoutInitializer = oldAllowFinalWithoutInitializer;
Send send = declaration.asSend();
VariableDefinitions variableDefinitions =
@@ -3990,6 +4002,10 @@ class VariableDefinitionsVisitor extends CommonResolverVisitor<SourceString> {
if (definitions.modifiers.isConst()) {
compiler.reportError(node, MessageKind.CONST_WITHOUT_INITIALIZER);
}
+ if (definitions.modifiers.isFinal() &&
+ !resolver.allowFinalWithoutInitializer) {
+ compiler.reportError(node, MessageKind.FINAL_WITHOUT_INITIALIZER);
+ }
return node.source;
}
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698