Index: lib/src/compiler/code_generator.dart |
diff --git a/lib/src/compiler/code_generator.dart b/lib/src/compiler/code_generator.dart |
index d0bdf2043190b9a91a642c7f84ea0751b1be688b..a331523ce063c60cb003c69df08c72b03b5f7e44 100644 |
--- a/lib/src/compiler/code_generator.dart |
+++ b/lib/src/compiler/code_generator.dart |
@@ -255,6 +255,10 @@ class CodeGenerator extends GeneralizingAstVisitor |
_collectElements(unit, nodes); |
} |
_loader = new ElementLoader(nodes); |
+ if (compilationUnits.isNotEmpty) { |
+ _constField = new ConstFieldVisitor(types, |
+ dummySource: compilationUnits.first.element.source); |
+ } |
// Add implicit dart:core dependency so it is first. |
emitLibraryName(dartCoreLibrary); |
@@ -267,7 +271,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
// NOTE: declarations are not necessarily emitted in this order. |
// Order will be changed as needed so the resulting code can execute. |
// This is done by forward declaring items. |
- compilationUnits.forEach(visitCompilationUnit); |
+ compilationUnits.forEach(_finishDeclarationsInUnit); |
// Declare imports |
_finishImports(items); |
@@ -439,10 +443,12 @@ class CodeGenerator extends GeneralizingAstVisitor |
_loader.declareBeforeUse(e, _emitDeclaration); |
} |
- @override |
- void visitCompilationUnit(CompilationUnit unit) { |
- _constField = new ConstFieldVisitor(types, unit.element.source); |
- |
+ void _finishDeclarationsInUnit(CompilationUnit unit) { |
+ // NOTE: this method isn't the right place to initialize |
+ // per-compilation-unit state. Declarations can be visited out of order, |
+ // this is only to catch things that haven't been emitted yet. |
+ // |
+ // See _emitDeclaration. |
for (var declaration in unit.declarations) { |
var element = declaration.element; |
if (element != null) { |
@@ -4242,7 +4248,9 @@ class CodeGenerator extends GeneralizingAstVisitor |
// Check if the target could be `null`, is dynamic, or may be an extension |
// native type. In all of those cases we need defensive code generation. |
var type = getStaticType(target); |
+ |
return isNullable(target) || |
+ type is FunctionType || |
type.isDynamic || |
(_extensionTypes.hasNativeSubtype(type) && target is! SuperExpression); |
} |