Augment. Use the latest augmentation getter/setter/funciton  for top-level scope.

Change-Id: Iff9d93edc891ad461c05dcac78ebd45caad3f1c8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356880
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/element/scope.dart b/pkg/analyzer/lib/src/dart/element/scope.dart
index d505ed3..3e4ccf7 100644
--- a/pkg/analyzer/lib/src/dart/element/scope.dart
+++ b/pkg/analyzer/lib/src/dart/element/scope.dart
@@ -147,11 +147,21 @@
   }
 
   void _addUnitElements(CompilationUnitElement compilationUnit) {
-    compilationUnit.accessors.forEach(_addPropertyAccessor);
+    for (var element in compilationUnit.accessors) {
+      if (element.augmentation == null) {
+        _addPropertyAccessor(element);
+      }
+    }
+
+    for (var element in compilationUnit.functions) {
+      if (element.augmentation == null) {
+        _addGetter(element);
+      }
+    }
+
     compilationUnit.enums.forEach(_addGetter);
     compilationUnit.extensions.forEach(_addExtension);
     compilationUnit.extensionTypes.forEach(_addGetter);
-    compilationUnit.functions.forEach(_addGetter);
     compilationUnit.typeAliases.forEach(_addGetter);
     compilationUnit.mixins.forEach(_addGetter);
     compilationUnit.classes.forEach(_addGetter);
diff --git a/pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart b/pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart
index 728f6f8..f0e09f0 100644
--- a/pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart
@@ -15,6 +15,148 @@
 
 @reflectiveTest
 class SimpleIdentifierResolutionTest extends PubPackageResolutionTest {
+  test_augment_topLevel_function_with_function() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+library augment 'test.dart';
+
+augment void foo() {}
+''');
+
+    await assertNoErrorsInCode('''
+import augment 'a.dart';
+
+void foo() {}
+
+void f() {
+  foo;
+}
+''');
+
+    final node = findNode.simple('foo;');
+    assertResolvedNodeText(node, r'''
+SimpleIdentifier
+  token: foo
+  staticElement: self::@augmentation::package:test/a.dart::@functionAugmentation::foo
+  staticType: void Function()
+''');
+  }
+
+  test_augment_topLevel_getter_with_getter() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+library augment 'test.dart';
+
+augment int get foo => 1;
+''');
+
+    await assertNoErrorsInCode('''
+import augment 'a.dart';
+
+int get foo => 0;
+
+void f() {
+  foo;
+}
+''');
+
+    final node = findNode.simple('foo;');
+    assertResolvedNodeText(node, r'''
+SimpleIdentifier
+  token: foo
+  staticElement: self::@augmentation::package:test/a.dart::@getterAugmentation::foo
+  staticType: int
+''');
+  }
+
+  test_augment_topLevel_setter_with_setter() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+library augment 'test.dart';
+
+augment set foo(int _) {}
+''');
+
+    await assertNoErrorsInCode('''
+import augment 'a.dart';
+
+set foo(int _) {}
+
+void f() {
+  foo = 0;
+}
+''');
+
+    final node = findNode.singleAssignmentExpression;
+    assertResolvedNodeText(node, r'''
+AssignmentExpression
+  leftHandSide: SimpleIdentifier
+    token: foo
+    staticElement: <null>
+    staticType: null
+  operator: =
+  rightHandSide: IntegerLiteral
+    literal: 0
+    parameter: self::@augmentation::package:test/a.dart::@setterAugmentation::foo::@parameter::_
+    staticType: int
+  readElement: <null>
+  readType: null
+  writeElement: self::@augmentation::package:test/a.dart::@setterAugmentation::foo
+  writeType: int
+  staticElement: <null>
+  staticType: int
+''');
+  }
+
+  test_augment_topLevel_variable_with_getter() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+library augment 'test.dart';
+
+augment int get foo() => 1;
+''');
+
+    await assertNoErrorsInCode('''
+import augment 'a.dart';
+
+int foo = 0;
+
+void f() {
+  foo;
+}
+''');
+
+    final node = findNode.simple('foo;');
+    assertResolvedNodeText(node, r'''
+SimpleIdentifier
+  token: foo
+  staticElement: self::@augmentation::package:test/a.dart::@getterAugmentation::foo
+  staticType: int
+''');
+  }
+
+  test_augment_topLevel_variable_with_variable() async {
+    newFile('$testPackageLibPath/a.dart', r'''
+library augment 'test.dart';
+
+augment int foo = 1;
+''');
+
+    await assertNoErrorsInCode('''
+import augment 'a.dart';
+
+int foo = 0;
+
+void f() {
+  foo;
+}
+''');
+
+    final node = findNode.simple('foo;');
+    assertResolvedNodeText(node, r'''
+SimpleIdentifier
+  token: foo
+  staticElement: self::@getter::foo
+  staticType: int
+''');
+  }
+
   test_dynamic_explicitCore() async {
     await assertNoErrorsInCode(r'''
 import 'dart:core';