Use AnalysisDriver.useSummary2 in FileSystemState.

R=brianwilkerson@google.com

Change-Id: Ie64f05a522d08c1fe779ccc78fbfe25ad52ec422
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106302
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart
index 2159da6..71c167c 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -1575,7 +1575,6 @@
       _unlinkedSalt,
       _linkedSalt,
       externalSummaries: _externalSummaries,
-      useSummary2: useSummary2,
     );
     _fileTracker = new FileTracker(_logger, _fsState, _changeHook);
   }
diff --git a/pkg/analyzer/lib/src/dart/analysis/file_state.dart b/pkg/analyzer/lib/src/dart/analysis/file_state.dart
index 252c1e4..f716911 100644
--- a/pkg/analyzer/lib/src/dart/analysis/file_state.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/file_state.dart
@@ -13,6 +13,7 @@
 import 'package:analyzer/file_system/file_system.dart';
 import 'package:analyzer/src/dart/analysis/byte_store.dart';
 import 'package:analyzer/src/dart/analysis/defined_names.dart';
+import 'package:analyzer/src/dart/analysis/driver.dart';
 import 'package:analyzer/src/dart/analysis/library_graph.dart';
 import 'package:analyzer/src/dart/analysis/performance_logger.dart';
 import 'package:analyzer/src/dart/analysis/referenced_names.dart';
@@ -396,7 +397,7 @@
   bool refresh({bool allowCached: false}) {
     counterFileStateRefresh++;
 
-    if (_fsState.useSummary2) {
+    if (AnalysisDriver.useSummary2) {
       return _refresh2(allowCached: allowCached);
     }
 
@@ -748,8 +749,9 @@
 
   static UnlinkedUnit2Builder serializeAstUnlinked2(CompilationUnit unit) {
     var exports = <String>[];
-    var imports = <String>['dart:core'];
+    var imports = <String>[];
     var parts = <String>[];
+    var hasDartCoreImport = false;
     var hasLibraryDirective = false;
     var hasPartOfDirective = false;
     for (var directive in unit.directives) {
@@ -759,6 +761,9 @@
       } else if (directive is ImportDirective) {
         var uriStr = directive.uri.stringValue;
         imports.add(uriStr ?? '');
+        if (uriStr == 'dart:core') {
+          hasDartCoreImport = true;
+        }
       } else if (directive is LibraryDirective) {
         hasLibraryDirective = true;
       } else if (directive is PartDirective) {
@@ -768,6 +773,9 @@
         hasPartOfDirective = true;
       }
     }
+    if (!hasDartCoreImport) {
+      imports.add('dart:core');
+    }
     var informativeData = createInformativeData(unit);
     return UnlinkedUnit2Builder(
       apiSignature: computeUnlinkedApiSignature(unit),
@@ -823,7 +831,6 @@
   final AnalysisOptions _analysisOptions;
   final Uint32List _unlinkedSalt;
   final Uint32List _linkedSalt;
-  final bool useSummary2;
 
   /**
    * The optional store with externally provided unlinked and corresponding
@@ -904,7 +911,6 @@
     this._unlinkedSalt,
     this._linkedSalt, {
     this.externalSummaries,
-    this.useSummary2 = false,
   }) {
     _fileContentCache = _FileContentCache.getInstance(
       _resourceProvider,
diff --git a/pkg/analyzer/test/src/dart/analysis/file_state_test.dart b/pkg/analyzer/test/src/dart/analysis/file_state_test.dart
index 24ec015..0eb6a6e 100644
--- a/pkg/analyzer/test/src/dart/analysis/file_state_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/file_state_test.dart
@@ -7,6 +7,7 @@
 
 import 'package:analyzer/file_system/file_system.dart';
 import 'package:analyzer/src/dart/analysis/byte_store.dart';
+import 'package:analyzer/src/dart/analysis/driver.dart';
 import 'package:analyzer/src/dart/analysis/file_state.dart';
 import 'package:analyzer/src/dart/analysis/library_graph.dart';
 import 'package:analyzer/src/dart/analysis/performance_logger.dart';
@@ -117,8 +118,13 @@
     expect(_excludeSdk(file.directReferencedFiles), isEmpty);
     expect(file.isPart, isFalse);
     expect(file.library, isNull);
-    expect(file.unlinked, isNotNull);
-    expect(file.unlinked.classes, isEmpty);
+    if (AnalysisDriver.useSummary2) {
+      expect(file.unlinked2, isNotNull);
+      expect(file.unlinked2.exports, isEmpty);
+    } else {
+      expect(file.unlinked, isNotNull);
+      expect(file.unlinked.classes, isEmpty);
+    }
   }
 
   test_getFileForPath_emptyUri() {
@@ -208,9 +214,13 @@
 
     expect(file.isPart, isFalse);
     expect(file.library, isNull);
-    expect(file.unlinked, isNotNull);
-    expect(file.unlinked.classes, hasLength(1));
-    expect(file.unlinked.classes[0].name, 'A1');
+    if (AnalysisDriver.useSummary2) {
+      expect(file.unlinked2, isNotNull);
+    } else {
+      expect(file.unlinked, isNotNull);
+      expect(file.unlinked.classes, hasLength(1));
+      expect(file.unlinked.classes[0].name, 'A1');
+    }
 
     expect(_excludeSdk(file.importedFiles), hasLength(2));
     expect(file.importedFiles[0].path, a2);
@@ -281,9 +291,13 @@
     expect(file_a2.path, a2);
     expect(file_a2.uri, Uri.parse('package:aaa/a2.dart'));
 
-    expect(file_a2.unlinked, isNotNull);
-    expect(file_a2.unlinked.classes, hasLength(1));
-    expect(file_a2.unlinked.classes[0].name, 'A2');
+    if (AnalysisDriver.useSummary2) {
+      expect(file_a2.unlinked2, isNotNull);
+    } else {
+      expect(file_a2.unlinked, isNotNull);
+      expect(file_a2.unlinked.classes, hasLength(1));
+      expect(file_a2.unlinked.classes[0].name, 'A2');
+    }
 
     expect(_excludeSdk(file_a2.importedFiles), isEmpty);
     expect(file_a2.exportedFiles, isEmpty);
@@ -500,7 +514,11 @@
 class A {}
 ''');
     FileState file = fileSystemState.getFileForPath(path);
-    expect(file.unlinked.classes[0].name, 'A');
+    if (AnalysisDriver.useSummary2) {
+      expect(file.definedTopLevelNames, contains('A'));
+    } else {
+      expect(file.unlinked.classes[0].name, 'A');
+    }
     List<int> signature = file.apiSignature;
 
     // Update the resource and refresh the file state.
@@ -510,7 +528,11 @@
     bool apiSignatureChanged = file.refresh();
     expect(apiSignatureChanged, isTrue);
 
-    expect(file.unlinked.classes[0].name, 'B');
+    if (AnalysisDriver.useSummary2) {
+      expect(file.definedTopLevelNames, contains('B'));
+    } else {
+      expect(file.unlinked.classes[0].name, 'B');
+    }
     expect(file.apiSignature, isNot(signature));
   }
 
@@ -546,14 +568,22 @@
 
     // Get the file, prepare unlinked.
     FileState file = fileSystemState.getFileForPath(path);
-    expect(file.unlinked, isNotNull);
+    if (AnalysisDriver.useSummary2) {
+      expect(file.unlinked2, isNotNull);
+    } else {
+      expect(file.unlinked, isNotNull);
+    }
 
     // Make the unlinked unit in the byte store zero-length, damaged.
     byteStore.put(file.test.unlinkedKey, <int>[]);
 
     // Refresh should not fail, zero bytes in the store are ignored.
     file.refresh();
-    expect(file.unlinked, isNotNull);
+    if (AnalysisDriver.useSummary2) {
+      expect(file.unlinked2, isNotNull);
+    } else {
+      expect(file.unlinked, isNotNull);
+    }
   }
 
   test_subtypedNames() {