Fix prefixed imports in augmentations.

Change-Id: Idf9093b2a90df2a3a7b3516f296a7efbb0a28b8f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252002
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index 9467514..b4242a8 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -559,6 +559,7 @@
       nameOffset: -1, // TODO(scheglov) implement, test
     );
     augmentation.definingCompilationUnit = definingUnit;
+    augmentation.reference = definingUnit.reference!;
 
     final resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
     _readLibraryOrAugmentationElement(augmentation);
@@ -988,7 +989,9 @@
     final uri = _readDirectiveUri(
       container: container,
     );
-    final prefix = _readImportElementPrefix();
+    final prefix = _readImportElementPrefix(
+      container: container,
+    );
     final combinators = _reader.readTypedList(_readNamespaceCombinator);
 
     final element = LibraryImportElementImpl(
@@ -1000,9 +1003,13 @@
     return element;
   }
 
-  ImportElementPrefixImpl? _readImportElementPrefix() {
+  ImportElementPrefixImpl? _readImportElementPrefix({
+    required LibraryOrAugmentationElementImpl container,
+  }) {
     PrefixElementImpl buildElement(String name) {
-      final reference = _reference.getChild('@prefix').getChild(name);
+      // TODO(scheglov) Make reference required.
+      final containerRef = container.reference!;
+      final reference = containerRef.getChild('@prefix').getChild(name);
       final existing = reference.element;
       if (existing is PrefixElementImpl) {
         return existing;
diff --git a/pkg/analyzer/lib/src/summary2/library_builder.dart b/pkg/analyzer/lib/src/summary2/library_builder.dart
index 690daa8..b18aafb 100644
--- a/pkg/analyzer/lib/src/summary2/library_builder.dart
+++ b/pkg/analyzer/lib/src/summary2/library_builder.dart
@@ -159,7 +159,7 @@
   void buildElements() {
     _buildDirectives(
       kind: kind,
-      element: element,
+      container: element,
     );
 
     for (var linkingUnit in units) {
@@ -467,10 +467,11 @@
           nameOffset: importedAugmentation.unlinked.libraryKeywordOffset,
         );
         augmentation.definingCompilationUnit = unitElement;
+        augmentation.reference = unitElement.reference!;
 
         _buildDirectives(
           kind: importedAugmentation,
-          element: augmentation,
+          container: augmentation,
         );
 
         uri = DirectiveUriWithAugmentationImpl(
@@ -537,13 +538,18 @@
   /// augmentations.
   void _buildDirectives({
     required LibraryOrAugmentationFileKind kind,
-    required LibraryOrAugmentationElementImpl element,
+    required LibraryOrAugmentationElementImpl container,
   }) {
-    element.libraryExports = kind.libraryExports.map(_buildExport).toList();
-    element.libraryImports = kind.libraryImports.map(_buildImport).toList();
+    container.libraryExports = kind.libraryExports.map(_buildExport).toList();
+    container.libraryImports = kind.libraryImports.map((state) {
+      return _buildImport(
+        container: container,
+        state: state,
+      );
+    }).toList();
 
-    element.augmentationImports = kind.augmentationImports.map((state) {
-      return _buildAugmentationImport(element, state);
+    container.augmentationImports = kind.augmentationImports.map((state) {
+      return _buildAugmentationImport(container, state);
     }).toList();
   }
 
@@ -614,21 +620,23 @@
     )..combinators = combinators;
   }
 
-  LibraryImportElementImpl _buildImport(LibraryImportState state) {
+  LibraryImportElementImpl _buildImport({
+    required LibraryOrAugmentationElementImpl container,
+    required LibraryImportState state,
+  }) {
     final importPrefix = state.unlinked.prefix.mapOrNull((unlinked) {
+      final prefix = _buildPrefix(
+        name: unlinked.name,
+        nameOffset: unlinked.nameOffset,
+        container: container,
+      );
       if (unlinked.deferredOffset != null) {
         return DeferredImportElementPrefixImpl(
-          element: _buildPrefix(
-            name: unlinked.name,
-            nameOffset: unlinked.nameOffset,
-          ),
+          element: prefix,
         );
       } else {
         return ImportElementPrefixImpl(
-          element: _buildPrefix(
-            name: unlinked.name,
-            nameOffset: unlinked.nameOffset,
-          ),
+          element: prefix,
         );
       }
     });
@@ -705,8 +713,11 @@
   PrefixElementImpl _buildPrefix({
     required String name,
     required int nameOffset,
+    required LibraryOrAugmentationElementImpl container,
   }) {
-    final reference = this.reference.getChild('@prefix').getChild(name);
+    // TODO(scheglov) Make reference required.
+    final containerRef = container.reference!;
+    final reference = containerRef.getChild('@prefix').getChild(name);
     final existing = reference.element;
     if (existing is PrefixElementImpl) {
       return existing;
@@ -716,7 +727,7 @@
         nameOffset,
         reference: reference,
       );
-      element.encloseElement(result);
+      container.encloseElement(result);
       return result;
     }
   }
diff --git a/pkg/analyzer/test/src/summary/elements_test.dart b/pkg/analyzer/test/src/summary/elements_test.dart
index c80647b..3d18ac2 100644
--- a/pkg/analyzer/test/src/summary/elements_test.dart
+++ b/pkg/analyzer/test/src/summary/elements_test.dart
@@ -300,7 +300,7 @@
 ''');
   }
 
-  test_augmentation_importScope_constant_field() async {
+  test_augmentation_importScope_constant_class_field() async {
     newFile('$testPackageLibPath/a.dart', r'''
 class A {
   static const a = 0;
@@ -395,6 +395,89 @@
 ''');
   }
 
+  test_augmentation_importScope_constant_prefix_class_field() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+class A {
+  static const a = 0;
+}
+''');
+
+    newFile('$testPackageLibPath/b.dart', r'''
+library augment 'test.dart';
+import 'a.dart' as prefix;
+const b = prefix.A.a;
+''');
+
+    final library = await buildLibrary(r'''
+import augment 'b.dart';
+''');
+
+    checkElementText(library, r'''
+library
+  augmentationImports
+    package:test/b.dart
+      imports
+        package:test/a.dart as prefix @48
+      definingUnit
+        topLevelVariables
+          static const b @62
+            type: int
+            constantInitializer
+              PropertyAccess
+                target: PrefixedIdentifier
+                  prefix: SimpleIdentifier
+                    token: prefix @66
+                    staticElement: self::@augmentation::package:test/b.dart::@prefix::prefix
+                    staticType: null
+                  period: . @72
+                  identifier: SimpleIdentifier
+                    token: A @73
+                    staticElement: package:test/a.dart::@class::A
+                    staticType: null
+                  staticElement: package:test/a.dart::@class::A
+                  staticType: null
+                operator: . @74
+                propertyName: SimpleIdentifier
+                  token: a @75
+                  staticElement: package:test/a.dart::@class::A::@getter::a
+                  staticType: int
+                staticType: int
+        accessors
+          synthetic static get b @-1
+            returnType: int
+  definingUnit
+''');
+  }
+
+  test_augmentation_importScope_prefixed() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+class A {}
+''');
+
+    newFile('$testPackageLibPath/b.dart', r'''
+library augment 'test.dart';
+import 'a.dart' as prefix;
+prefix.A f() {}
+''');
+
+    final library = await buildLibrary(r'''
+import augment 'b.dart';
+''');
+
+    checkElementText(library, r'''
+library
+  augmentationImports
+    package:test/b.dart
+      imports
+        package:test/a.dart as prefix @48
+      definingUnit
+        functions
+          f @65
+            returnType: A
+  definingUnit
+''');
+  }
+
   test_augmentation_importScope_topInference() async {
     newFile('$testPackageLibPath/a.dart', r'''
 final a = 0;