Revert "Revert "Create new AnalysisSession instance on changes.""

This reverts commit 1caf7b7ecdc28e8e8135f04eea987fd0e100103f.

R=brianwilkerson@google.com

Change-Id: I52c9628afe5d3df94801dc793217c9adf4ea82fe
Reviewed-on: https://dart-review.googlesource.com/48085
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 5c6e63d..43a1d47 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -326,9 +326,6 @@
 
   /**
    * The current analysis session.
-   *
-   * TODO(brianwilkerson) Create a new session when the current session might
-   * produce inconsistent results.
    */
   AnalysisSessionImpl _currentSession;
 
@@ -356,7 +353,7 @@
         _sourceFactory = sourceFactory.clone(),
         _sdkBundle = sdkBundle,
         _externalSummaries = externalSummaries {
-    _currentSession = new AnalysisSessionImpl(this);
+    _createNewSession();
     _onResults = _resultController.stream.asBroadcastStream();
     _testView = new AnalysisDriverTestView(this);
     _createFileTracker();
@@ -1122,6 +1119,7 @@
    * of state.
    */
   void _changeHook() {
+    _createNewSession();
     _priorityResults.clear();
     _scheduler.notify(this);
   }
@@ -1387,6 +1385,13 @@
   }
 
   /**
+   * Create a new analysis session, so invalidating the current one.
+   */
+  void _createNewSession() {
+    _currentSession = new AnalysisSessionImpl(this);
+  }
+
+  /**
    * Fill [_salt] with data.
    */
   void _fillSalt() {
diff --git a/pkg/analyzer/test/src/dart/analysis/driver_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_test.dart
index 6d12d04..32df76a 100644
--- a/pkg/analyzer/test/src/dart/analysis/driver_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/driver_test.dart
@@ -6725,6 +6725,26 @@
     expect(x.constantValue.toIntValue(), 1);
   }
 
+  test_currentSession() async {
+    var a = _p('/a.dart');
+
+    provider.newFile(a, 'var V = 1;');
+    await driver.getResult(a);
+
+    var session1 = driver.currentSession;
+    expect(session1, isNotNull);
+
+    provider.updateFile(a, 'var V = 2;');
+    driver.changeFile(a);
+    await driver.getResult(a);
+
+    var session2 = driver.currentSession;
+    expect(session2, isNotNull);
+
+    // We get a new session.
+    expect(session2, isNot(session1));
+  }
+
   test_errors_uriDoesNotExist_export() async {
     addTestFile(r'''
 export 'foo.dart';