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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of resolution; 5 part of resolution;
6 6
7 abstract class TreeElements { 7 abstract class TreeElements {
8 Element get currentElement; 8 Element get currentElement;
9 Set<Node> get superUses; 9 Set<Node> get superUses;
10 10
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 TreeElements resolveField(VariableElement element) { 442 TreeElements resolveField(VariableElement element) {
443 Node tree = element.parseNode(compiler); 443 Node tree = element.parseNode(compiler);
444 if(element.modifiers.isStatic() && element.variables.isTopLevel()) { 444 if(element.modifiers.isStatic() && element.variables.isTopLevel()) {
445 error(element.modifiers.getStatic(), 445 error(element.modifiers.getStatic(),
446 MessageKind.TOP_LEVEL_VARIABLE_DECLARED_STATIC); 446 MessageKind.TOP_LEVEL_VARIABLE_DECLARED_STATIC);
447 } 447 }
448 ResolverVisitor visitor = visitorFor(element); 448 ResolverVisitor visitor = visitorFor(element);
449 visitor.useElement(tree, element); 449 visitor.useElement(tree, element);
450 450
451 SendSet send = tree.asSendSet(); 451 SendSet send = tree.asSendSet();
452 Modifiers modifiers = element.modifiers;
452 if (send != null) { 453 if (send != null) {
453 // TODO(johnniwinther): Avoid analyzing initializers if 454 // TODO(johnniwinther): Avoid analyzing initializers if
454 // [Compiler.analyzeSignaturesOnly] is set. 455 // [Compiler.analyzeSignaturesOnly] is set.
455 visitor.visit(send.arguments.head); 456 visitor.visit(send.arguments.head);
456 } else if (element.modifiers.isConst()) { 457 } else if (modifiers.isConst()) {
457 compiler.reportError(element, MessageKind.CONST_WITHOUT_INITIALIZER); 458 compiler.reportError(element, MessageKind.CONST_WITHOUT_INITIALIZER);
459 } else if (modifiers.isFinal() && !element.isInstanceMember()) {
460 compiler.reportError(element, MessageKind.FINAL_WITHOUT_INITIALIZER);
458 } 461 }
459 462
460 if (Elements.isStaticOrTopLevelField(element)) { 463 if (Elements.isStaticOrTopLevelField(element)) {
461 if (tree.asSendSet() != null) { 464 if (tree.asSendSet() != null) {
462 // TODO(13429): We could do better here by using the 465 // TODO(13429): We could do better here by using the
463 // constant handler to figure out if it's a lazy field or not. 466 // constant handler to figure out if it's a lazy field or not.
464 compiler.backend.registerLazyField(visitor.mapping); 467 compiler.backend.registerLazyField(visitor.mapping);
465 } else { 468 } else {
466 compiler.enqueuer.resolution.registerInstantiatedClass( 469 compiler.enqueuer.resolution.registerInstantiatedClass(
467 compiler.nullClass, visitor.mapping); 470 compiler.nullClass, visitor.mapping);
(...skipping 1331 matching lines...) Expand 10 before | Expand all | Expand 10 after
1799 bool inCheckContext; 1802 bool inCheckContext;
1800 bool inCatchBlock; 1803 bool inCatchBlock;
1801 Scope scope; 1804 Scope scope;
1802 ClassElement currentClass; 1805 ClassElement currentClass;
1803 ExpressionStatement currentExpressionStatement; 1806 ExpressionStatement currentExpressionStatement;
1804 bool sendIsMemberAccess = false; 1807 bool sendIsMemberAccess = false;
1805 StatementScope statementScope; 1808 StatementScope statementScope;
1806 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION 1809 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION
1807 | ElementCategory.IMPLIES_TYPE; 1810 | ElementCategory.IMPLIES_TYPE;
1808 1811
1812 /// When visiting the type declaration of the variable in a [ForIn] loop,
1813 /// the initializer of the variable is implicit and we should not emit an
1814 /// error when verifying that all final variables are initialized.
1815 bool allowFinalWithoutInitializer = false;
1816
1809 // TODO(ahe): Find a way to share this with runtime implementation. 1817 // TODO(ahe): Find a way to share this with runtime implementation.
1810 static final RegExp symbolValidationPattern = 1818 static final RegExp symbolValidationPattern =
1811 new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|' 1819 new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|'
1812 r'-|' 1820 r'-|'
1813 r'unary-|' 1821 r'unary-|'
1814 r'\[\]=|' 1822 r'\[\]=|'
1815 r'~|' 1823 r'~|'
1816 r'==|' 1824 r'==|'
1817 r'\[\]|' 1825 r'\[\]|'
1818 r'\*|' 1826 r'\*|'
(...skipping 1200 matching lines...) Expand 10 before | Expand all | Expand 10 after
3019 mapping.setIteratorSelector(node, compiler.iteratorSelector); 3027 mapping.setIteratorSelector(node, compiler.iteratorSelector);
3020 world.registerDynamicGetter(compiler.iteratorSelector); 3028 world.registerDynamicGetter(compiler.iteratorSelector);
3021 mapping.setCurrentSelector(node, compiler.currentSelector); 3029 mapping.setCurrentSelector(node, compiler.currentSelector);
3022 world.registerDynamicGetter(compiler.currentSelector); 3030 world.registerDynamicGetter(compiler.currentSelector);
3023 mapping.setMoveNextSelector(node, compiler.moveNextSelector); 3031 mapping.setMoveNextSelector(node, compiler.moveNextSelector);
3024 world.registerDynamicInvocation(compiler.moveNextSelector); 3032 world.registerDynamicInvocation(compiler.moveNextSelector);
3025 3033
3026 visit(node.expression); 3034 visit(node.expression);
3027 Scope blockScope = new BlockScope(scope); 3035 Scope blockScope = new BlockScope(scope);
3028 Node declaration = node.declaredIdentifier; 3036 Node declaration = node.declaredIdentifier;
3037
3038 bool oldAllowFinalWithoutInitializer = allowFinalWithoutInitializer;
3039 allowFinalWithoutInitializer = true;
3029 visitIn(declaration, blockScope); 3040 visitIn(declaration, blockScope);
3041 allowFinalWithoutInitializer = oldAllowFinalWithoutInitializer;
3030 3042
3031 Send send = declaration.asSend(); 3043 Send send = declaration.asSend();
3032 VariableDefinitions variableDefinitions = 3044 VariableDefinitions variableDefinitions =
3033 declaration.asVariableDefinitions(); 3045 declaration.asVariableDefinitions();
3034 Element loopVariable; 3046 Element loopVariable;
3035 Selector loopVariableSelector; 3047 Selector loopVariableSelector;
3036 if (send != null) { 3048 if (send != null) {
3037 loopVariable = mapping[send]; 3049 loopVariable = mapping[send];
3038 Identifier identifier = send.selector.asIdentifier(); 3050 Identifier identifier = send.selector.asIdentifier();
3039 if (identifier == null) { 3051 if (identifier == null) {
(...skipping 943 matching lines...) Expand 10 before | Expand all | Expand 10 after
3983 return name; 3995 return name;
3984 } 3996 }
3985 3997
3986 SourceString visitIdentifier(Identifier node) { 3998 SourceString visitIdentifier(Identifier node) {
3987 // The variable is initialized to null. 3999 // The variable is initialized to null.
3988 resolver.world.registerInstantiatedClass(compiler.nullClass, 4000 resolver.world.registerInstantiatedClass(compiler.nullClass,
3989 resolver.mapping); 4001 resolver.mapping);
3990 if (definitions.modifiers.isConst()) { 4002 if (definitions.modifiers.isConst()) {
3991 compiler.reportError(node, MessageKind.CONST_WITHOUT_INITIALIZER); 4003 compiler.reportError(node, MessageKind.CONST_WITHOUT_INITIALIZER);
3992 } 4004 }
4005 if (definitions.modifiers.isFinal() &&
4006 !resolver.allowFinalWithoutInitializer) {
4007 compiler.reportError(node, MessageKind.FINAL_WITHOUT_INITIALIZER);
4008 }
3993 return node.source; 4009 return node.source;
3994 } 4010 }
3995 4011
3996 visitNodeList(NodeList node) { 4012 visitNodeList(NodeList node) {
3997 for (Link<Node> link = node.nodes; !link.isEmpty; link = link.tail) { 4013 for (Link<Node> link = node.nodes; !link.isEmpty; link = link.tail) {
3998 SourceString name = visit(link.head); 4014 SourceString name = visit(link.head);
3999 VariableElement element = 4015 VariableElement element =
4000 new VariableElementX(name, variables, kind, link.head); 4016 new VariableElementX(name, variables, kind, link.head);
4001 resolver.defineElement(link.head, element); 4017 resolver.defineElement(link.head, element);
4002 } 4018 }
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
4420 return e; 4436 return e;
4421 } 4437 }
4422 4438
4423 /// Assumed to be called by [resolveRedirectingFactory]. 4439 /// Assumed to be called by [resolveRedirectingFactory].
4424 Element visitReturn(Return node) { 4440 Element visitReturn(Return node) {
4425 Node expression = node.expression; 4441 Node expression = node.expression;
4426 return finishConstructorReference(visit(expression), 4442 return finishConstructorReference(visit(expression),
4427 expression, expression); 4443 expression, expression);
4428 } 4444 }
4429 } 4445 }
OLDNEW
« 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