Implement LibraryElementForLink.session and return (empty) AnalysisSessionForLink.

R=brianwilkerson@google.com, paulberry@google.com

Bug: https://buganizer.corp.google.com/issues/127956822
Change-Id: Ifc93882d9ae7ae6079497fb10f3feea7ac6fb9e2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96864
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/summary/link.dart b/pkg/analyzer/lib/src/summary/link.dart
index 05b9467..c7e24ab 100644
--- a/pkg/analyzer/lib/src/summary/link.dart
+++ b/pkg/analyzer/lib/src/summary/link.dart
@@ -2,8 +2,6 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-import 'package:analyzer/dart/analysis/session.dart';
-
 /// This library is capable of producing linked summaries from unlinked
 /// ones (or prelinked ones).  It functions by building a miniature
 /// element model to represent the contents of the summaries, and then
@@ -57,6 +55,7 @@
 ///
 /// - Where possible, we favor method dispatch instead of "is" and "as"
 ///   checks.  E.g. see [ReferenceableElementForLink.asConstructor].
+import 'package:analyzer/dart/analysis/session.dart';
 import 'package:analyzer/dart/ast/ast.dart';
 import 'package:analyzer/dart/ast/standard_ast_factory.dart';
 import 'package:analyzer/dart/element/element.dart';
@@ -413,6 +412,11 @@
 /// [UnlinkedUnit] objects.
 typedef UnlinkedUnit GetUnitCallback(String absoluteUri);
 
+class AnalysisSessionForLink implements AnalysisSession {
+  @override
+  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
 /// Element representing a class or enum resynthesized from a summary
 /// during linking.
 abstract class ClassElementForLink
@@ -3639,6 +3643,9 @@
   LibraryResynthesizerContext get resynthesizerContext => this;
 
   @override
+  AnalysisSession get session => _linker.session;
+
+  @override
   Source get source => definingCompilationUnit.source;
 
   @override
@@ -3936,6 +3943,7 @@
   SpecialTypeElementForLink _bottomElement;
   InheritanceManager2 _inheritanceManager;
   ContextForLink _context;
+  AnalysisSessionForLink _session;
 
   /// Gets an instance of [AnalysisOptions] for use during linking.
   final AnalysisOptions analysisOptions;
@@ -3976,6 +3984,10 @@
   InheritanceManager2 get inheritanceManager =>
       _inheritanceManager ??= new InheritanceManager2(typeSystem);
 
+  /// Get a stub implementation of [AnalysisContext] which can be used during
+  /// linking.
+  get session => _session ??= new AnalysisSessionForLink();
+
   /// Indicates whether type inference should use strong mode rules.
   @deprecated
   bool get strongMode => true;
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart b/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
index 1859e5e..187dc7b 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
@@ -1009,11 +1009,16 @@
 
   @override
   LinkedNodeBuilder visitSimpleIdentifier(SimpleIdentifier node) {
-    var isDeclared = node.inDeclarationContext();
+    Element element;
+    if (!node.inDeclarationContext()) {
+      element = node.staticElement;
+      if (element is MultiplyDefinedElement) {
+        element = null;
+      }
+    }
 
     return LinkedNodeBuilder.simpleIdentifier(
-      simpleIdentifier_element:
-          isDeclared ? null : _getReferenceIndex(node.staticElement),
+      simpleIdentifier_element: _getReferenceIndex(element),
       simpleIdentifier_token: _getToken(node.token),
       expression_type: _writeType(node.staticType),
     );
diff --git a/pkg/analyzer/test/src/summary/resynthesize_common.dart b/pkg/analyzer/test/src/summary/resynthesize_common.dart
index 00cd2c8..139ad19 100644
--- a/pkg/analyzer/test/src/summary/resynthesize_common.dart
+++ b/pkg/analyzer/test/src/summary/resynthesize_common.dart
@@ -7800,6 +7800,21 @@
 ''');
   }
 
+  test_type_inference_multiplyDefinedElement() async {
+    addLibrarySource('/a.dart', 'class C {}');
+    addLibrarySource('/b.dart', 'class C {}');
+    var library = await checkLibrary('''
+import 'a.dart';
+import 'b.dart';
+var v = C;
+''');
+    checkElementText(library, r'''
+import 'a.dart';
+import 'b.dart';
+dynamic v;
+''');
+  }
+
   test_type_inference_nested_function() async {
     var library = await checkLibrary('''
 var x = (t) => (u) => t + u;