Augment. Report CONFLICTING_METHOD_AND_FIELD only in the fragment.

Change-Id: Icb57cda60c3d21421884aec581e8bedef8ddbe01
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373901
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
index c20d74e..c2715bf 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
@@ -429,6 +429,7 @@
     ErrorVerifier errorVerifier = ErrorVerifier(
       errorReporter,
       _libraryElement,
+      unit.declaredElement!,
       _typeProvider,
       _inheritance,
       _libraryVerificationContext,
diff --git a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
index 4556adb..8db67d7 100644
--- a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
+++ b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
@@ -9,6 +9,7 @@
 import 'package:analyzer/error/error.dart';
 import 'package:analyzer/error/listener.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/element/element.dart';
 import 'package:analyzer/src/dart/element/extensions.dart';
 import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
 import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
@@ -17,6 +18,7 @@
 class DuplicateDefinitionVerifier {
   final InheritanceManager3 _inheritanceManager;
   final LibraryElement _currentLibrary;
+  final CompilationUnitElementImpl _currentUnit;
   final ErrorReporter _errorReporter;
   final DuplicationDefinitionContext context;
 
@@ -25,6 +27,7 @@
   DuplicateDefinitionVerifier(
     this._inheritanceManager,
     this._currentLibrary,
+    this._currentUnit,
     this._errorReporter,
     this.context,
   );
@@ -55,8 +58,8 @@
 
   /// Check that there are no members with the same name.
   void checkEnum(EnumDeclaration node) {
-    var element = node.declaredElement!;
-    var augmented = element.augmented;
+    var fragment = node.declaredElement!;
+    var augmented = fragment.augmented;
     var declarationElement = augmented.declaration;
     var declarationName = declarationElement.name;
 
@@ -137,7 +140,7 @@
       staticSetters: staticSetters,
     );
 
-    for (var accessor in element.accessors) {
+    for (var accessor in fragment.accessors) {
       var baseName = accessor.displayName;
       if (accessor.isStatic) {
         var instance = _getInterfaceMember(declarationElement, baseName);
@@ -164,7 +167,10 @@
       }
     }
 
-    for (var method in declarationElement.methods) {
+    for (var method in fragment.methods) {
+      if (method.source != _currentUnit.source) {
+        continue;
+      }
       var baseName = method.displayName;
       if (method.isStatic) {
         var instance = _getInterfaceMember(declarationElement, baseName);
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index dd68abf..31a4307 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -162,6 +162,9 @@
   /// The current library that is being analyzed.
   final LibraryElementImpl _currentLibrary;
 
+  /// The current unit that is being analyzed.
+  final CompilationUnitElementImpl _currentUnit;
+
   /// The type representing the type 'int'.
   late final InterfaceType _intType;
 
@@ -255,6 +258,7 @@
   ErrorVerifier(
     this.errorReporter,
     this._currentLibrary,
+    this._currentUnit,
     this._typeProvider,
     this._inheritanceManager,
     this.libraryContext,
@@ -268,6 +272,7 @@
         _duplicateDefinitionVerifier = DuplicateDefinitionVerifier(
           _inheritanceManager,
           _currentLibrary,
+          _currentUnit,
           errorReporter,
           libraryContext.duplicationDefinitionContext,
         ) {
@@ -2374,7 +2379,11 @@
     var conflictingDeclaredNames = <String>{};
 
     // method declared in the enclosing class vs. inherited getter/setter
-    for (MethodElement method in enclosingClass.methods) {
+    for (MethodElement method in fragment.methods) {
+      if (method.source != _currentUnit.source) {
+        continue;
+      }
+
       String name = method.name;
 
       // find inherited property accessors
diff --git a/pkg/analyzer/test/src/diagnostics/conflicting_method_and_field_test.dart b/pkg/analyzer/test/src/diagnostics/conflicting_method_and_field_test.dart
index 44c0fe2..1589f13 100644
--- a/pkg/analyzer/test/src/diagnostics/conflicting_method_and_field_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/conflicting_method_and_field_test.dart
@@ -41,6 +41,58 @@
     ]);
   }
 
+  test_class_inSuper_getter_hasAugmentation_inAugmentation() async {
+    var a = newFile('$testPackageLibPath/a.dart', r'''
+import augment 'b.dart';
+
+class A {
+  int get foo => 0;
+}
+
+class B extends A {}
+''');
+
+    var b = newFile('$testPackageLibPath/b.dart', r'''
+augment library 'a.dart';
+
+augment class B {
+  void foo() {}
+}
+''');
+
+    await assertErrorsInFile2(a, []);
+
+    await assertErrorsInFile2(b, [
+      error(CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, 52, 3),
+    ]);
+  }
+
+  test_class_inSuper_getter_hasAugmentation_inDeclaration() async {
+    var a = newFile('$testPackageLibPath/a.dart', r'''
+import augment 'b.dart';
+
+class A {
+  int get foo => 0;
+}
+
+class B extends A {
+  void foo() {}
+}
+''');
+
+    var b = newFile('$testPackageLibPath/b.dart', r'''
+augment library 'a.dart';
+
+augment class B {}
+''');
+
+    await assertErrorsInFile2(a, [
+      error(CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, 86, 3),
+    ]);
+
+    await assertErrorsInFile2(b, []);
+  }
+
   test_class_inSuper_setter() async {
     await assertErrorsInCode(r'''
 class A {
@@ -69,6 +121,59 @@
     ]);
   }
 
+  test_enum_inMixin_getter_hasAugmentation_inAugmentation() async {
+    var a = newFile('$testPackageLibPath/a.dart', r'''
+import augment 'b.dart';
+
+mixin M {
+  int get foo => 0;
+}
+
+enum E with M {v}
+''');
+
+    var b = newFile('$testPackageLibPath/b.dart', r'''
+augment library 'a.dart';
+
+augment enum E {;
+  void foo() {}
+}
+''');
+
+    await assertErrorsInFile2(a, []);
+
+    await assertErrorsInFile2(b, [
+      error(CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, 52, 3),
+    ]);
+  }
+
+  test_enum_inMixin_getter_hasAugmentation_inDeclaration() async {
+    var a = newFile('$testPackageLibPath/a.dart', r'''
+import augment 'b.dart';
+
+mixin M {
+  int get foo => 0;
+}
+
+enum E with M {
+  v;
+  void foo() {}
+}
+''');
+
+    var b = newFile('$testPackageLibPath/b.dart', r'''
+augment library 'a.dart';
+
+augment enum E {}
+''');
+
+    await assertErrorsInFile2(a, [
+      error(CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, 87, 3),
+    ]);
+
+    await assertErrorsInFile2(b, []);
+  }
+
   test_enum_inMixin_setter() async {
     await assertErrorsInCode(r'''
 mixin M {