From 878cd3a3f8a6c94571bbc3a2cac7afeeb4bc274e Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Wed, 4 Aug 2021 17:29:47 +0000 Subject: [PATCH] Improve the messaging around conflicting constructors Fixes: https://github.com/dart-lang/sdk/issues/46803 Change-Id: I0435ea15cfb4c57dfb865f66d251afbe1d9459a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208921 Reviewed-by: Konstantin Shcheglov Commit-Queue: Brian Wilkerson --- pkg/analyzer/lib/error/error.dart | 2 ++ pkg/analyzer/lib/src/error/codes.dart | 28 ++++++++++++++++++- .../error/duplicate_definition_verifier.dart | 17 +++++++---- .../test/src/dart/resolution/class_test.dart | 8 +++--- pkg/analyzer/tool/diagnostics/diagnostics.md | 6 ++++ 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart index 7d50e7e4767..a8fcbf3426a 100644 --- a/pkg/analyzer/lib/error/error.dart +++ b/pkg/analyzer/lib/error/error.dart @@ -95,7 +95,9 @@ const List errorCodeValues = [ CompileTimeErrorCode.CAST_TO_NON_TYPE, CompileTimeErrorCode.CONCRETE_CLASS_WITH_ABSTRACT_MEMBER, CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, + CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER, CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD, + CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER, CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD, CompileTimeErrorCode.CONFLICTING_GENERIC_INTERFACES, CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, diff --git a/pkg/analyzer/lib/src/error/codes.dart b/pkg/analyzer/lib/src/error/codes.dart index fb2881f934a..29f4fe29c5e 100644 --- a/pkg/analyzer/lib/src/error/codes.dart +++ b/pkg/analyzer/lib/src/error/codes.dart @@ -1565,7 +1565,7 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { /** * Parameters: - * 0: the name of the field + * 0: the name of the constructor and field */ // #### Description // @@ -1608,6 +1608,19 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { hasPublishedDocs: true, uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD'); + /** + * Parameters: + * 0: the name of the constructor and getter + */ + static const CompileTimeErrorCode CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER = + CompileTimeErrorCode( + 'CONFLICTING_CONSTRUCTOR_AND_STATIC_MEMBER', + "'{0}' can't be used to name both a constructor and a static getter " + "in this class.", + correction: "Try renaming either the constructor or the getter.", + hasPublishedDocs: true, + uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER'); + /** * Parameters: * 0: the name of the constructor @@ -1621,6 +1634,19 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { hasPublishedDocs: true, uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD'); + /** + * Parameters: + * 0: the name of the constructor and setter + */ + static const CompileTimeErrorCode CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER = + CompileTimeErrorCode( + 'CONFLICTING_CONSTRUCTOR_AND_STATIC_MEMBER', + "'{0}' can't be used to name both a constructor and a static setter " + "in this class.", + correction: "Try renaming either the constructor or the setter.", + hasPublishedDocs: true, + uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER'); + /** * 10.11 Class Member Conflicts: Let `C` be a class. It is a compile-time * error if `C` declares a getter or a setter with basename `n`, and has a diff --git a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart index 0947fbe6e0a..7ad7ac9ecc1 100644 --- a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart +++ b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart @@ -302,11 +302,18 @@ class DuplicateDefinitionVerifier { var staticMember = staticGetters[name] ?? staticSetters[name]; if (staticMember != null) { if (staticMember is PropertyAccessorElement) { - _errorReporter.reportErrorForNode( - CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, - nameNode, - [name], - ); + CompileTimeErrorCode errorCode; + if (staticMember.isSynthetic) { + errorCode = CompileTimeErrorCode + .CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD; + } else if (staticMember.isGetter) { + errorCode = CompileTimeErrorCode + .CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER; + } else { + errorCode = CompileTimeErrorCode + .CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER; + } + _errorReporter.reportErrorForNode(errorCode, nameNode, [name]); } else { _errorReporter.reportErrorForNode( CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD, diff --git a/pkg/analyzer/test/src/dart/resolution/class_test.dart b/pkg/analyzer/test/src/dart/resolution/class_test.dart index d5bb61e2271..c59ee4f165e 100644 --- a/pkg/analyzer/test/src/dart/resolution/class_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/class_test.dart @@ -180,8 +180,8 @@ class C { static int get foo => 0; } ''', [ - error( - CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, 14, 3), + error(CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER, 14, + 3), ]); } @@ -212,8 +212,8 @@ class C { static void set foo(_) {} } ''', [ - error( - CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, 14, 3), + error(CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER, 14, + 3), ]); } diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md index f6857fc3c7f..0cdb816d882 100644 --- a/pkg/analyzer/tool/diagnostics/diagnostics.md +++ b/pkg/analyzer/tool/diagnostics/diagnostics.md @@ -1845,9 +1845,15 @@ abstract class C { _'{0}' can't be used to name both a constructor and a static field in this class._ +_'{0}' can't be used to name both a constructor and a static getter in this +class._ + _'{0}' can't be used to name both a constructor and a static method in this class._ +_'{0}' can't be used to name both a constructor and a static setter in this +class._ + #### Description The analyzer produces this diagnostic when a named constructor and either a