Skip to content

Commit 0707019

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Convert a few errors to literate diagnostic reporting.
This change modifies the following four errors to use the analyzer's new literate diagnostic reporting API: - `CompileTimeErrorCode.abstractFieldConstructorInitializer` - `CompileTimeErrorCode.abstractSuperMemberReference` - `CompileTimeErrorCode.baseClassImplementedOutsideOfLibrary` - `CompileTimeErrorCode.baseMixinImplementedOutsideOfLibrary` Also, the parameter names used by these messages are changed from the old placeholder names (`p0`, `p1`, etc.) to names that appropriately discribe their functions. This should be enough to validate the overall approach. After this, I will move over to the CFE side, to give its `withArguments` methods the ability to accept named parameters rather than positional ones. Change-Id: I6a6a6964fca29633c18dc7945cdd7def0bbabdf3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447060 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 818b2a0 commit 0707019

File tree

6 files changed

+68
-53
lines changed

6 files changed

+68
-53
lines changed

pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
77
import 'package:analyzer/dart/ast/token.dart' show Token;
88
import 'package:analyzer/dart/element/element.dart';
99
import 'package:analyzer/dart/element/type.dart';
10-
import 'package:analyzer/error/listener.dart';
1110
import 'package:analyzer/src/dart/ast/ast.dart';
1211
import 'package:analyzer/src/dart/ast/extensions.dart';
1312
import 'package:analyzer/src/dart/element/element.dart';
@@ -18,6 +17,7 @@ import 'package:analyzer/src/dart/resolver/extension_member_resolver.dart';
1817
import 'package:analyzer/src/dart/resolver/invocation_inference_helper.dart';
1918
import 'package:analyzer/src/dart/resolver/invocation_inferrer.dart';
2019
import 'package:analyzer/src/error/codes.dart';
20+
import 'package:analyzer/src/error/listener.dart';
2121
import 'package:analyzer/src/generated/inference_log.dart';
2222
import 'package:analyzer/src/generated/resolver.dart';
2323
import 'package:analyzer/src/generated/scope_helpers.dart';
@@ -1024,10 +1024,10 @@ class MethodInvocationResolver with ScopeHelpers {
10241024
contextType: contextType,
10251025
);
10261026

1027-
_resolver.diagnosticReporter.atNode(
1028-
nameNode,
1029-
CompileTimeErrorCode.abstractSuperMemberReference,
1030-
arguments: [target.kind.displayName, name],
1027+
_resolver.diagnosticReporter.report(
1028+
CompileTimeErrorCode.abstractSuperMemberReference
1029+
.withArguments(kind: target.kind.displayName, name: name)
1030+
.at(nameNode),
10311031
);
10321032
return null;
10331033
}

pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/type.dart';
99
import 'package:analyzer/error/error.dart';
10-
import 'package:analyzer/error/listener.dart';
1110
import 'package:analyzer/src/dart/ast/ast.dart';
1211
import 'package:analyzer/src/dart/ast/extensions.dart';
1312
import 'package:analyzer/src/dart/element/element.dart';
@@ -21,6 +20,7 @@ import 'package:analyzer/src/dart/resolver/lexical_lookup.dart';
2120
import 'package:analyzer/src/dart/resolver/resolution_result.dart';
2221
import 'package:analyzer/src/error/assignment_verifier.dart';
2322
import 'package:analyzer/src/error/codes.dart';
23+
import 'package:analyzer/src/error/listener.dart';
2424
import 'package:analyzer/src/generated/resolver.dart';
2525
import 'package:analyzer/src/generated/scope_helpers.dart';
2626
import 'package:analyzer/src/generated/super_context.dart';
@@ -942,10 +942,13 @@ class PropertyElementResolver with ScopeHelpers {
942942
name,
943943
);
944944
if (readElement != null) {
945-
diagnosticReporter.atNode(
946-
propertyName,
947-
CompileTimeErrorCode.abstractSuperMemberReference,
948-
arguments: [readElement.kind.displayName, propertyName.name],
945+
diagnosticReporter.report(
946+
CompileTimeErrorCode.abstractSuperMemberReference
947+
.withArguments(
948+
kind: readElement.kind.displayName,
949+
name: propertyName.name,
950+
)
951+
.at(propertyName),
949952
);
950953
} else {
951954
diagnosticReporter.atNode(
@@ -990,10 +993,13 @@ class PropertyElementResolver with ScopeHelpers {
990993
inherited: true,
991994
);
992995
if (writeElement != null) {
993-
diagnosticReporter.atNode(
994-
propertyName,
995-
CompileTimeErrorCode.abstractSuperMemberReference,
996-
arguments: [writeElement.kind.displayName, propertyName.name],
996+
diagnosticReporter.report(
997+
CompileTimeErrorCode.abstractSuperMemberReference
998+
.withArguments(
999+
kind: writeElement.kind.displayName,
1000+
name: propertyName.name,
1001+
)
1002+
.at(propertyName),
9971003
);
9981004
} else {
9991005
diagnosticReporter.atNode(

pkg/analyzer/lib/src/error/base_or_final_type_verifier.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/type.dart';
99
import 'package:analyzer/diagnostic/diagnostic.dart';
10-
import 'package:analyzer/error/listener.dart';
1110
import 'package:analyzer/src/dart/element/element.dart';
1211
import 'package:analyzer/src/diagnostic/diagnostic.dart';
1312
import 'package:analyzer/src/error/codes.dart';
13+
import 'package:analyzer/src/error/listener.dart';
1414

1515
/// Helper for verifying that subelements of a base or final element must be
1616
/// base, final, or sealed.
@@ -243,11 +243,13 @@ class BaseOrFinalTypeVerifier {
243243
var errorCode = baseOrFinalSuperElement is MixinElement
244244
? CompileTimeErrorCode.baseMixinImplementedOutsideOfLibrary
245245
: CompileTimeErrorCode.baseClassImplementedOutsideOfLibrary;
246-
_diagnosticReporter.atNode(
247-
implementsNamedType,
248-
errorCode,
249-
arguments: [baseOrFinalSuperElement.displayName],
250-
contextMessages: contextMessages,
246+
_diagnosticReporter.report(
247+
errorCode
248+
.withArguments(
249+
superElementName: baseOrFinalSuperElement.displayName,
250+
)
251+
.withContextMessages(contextMessages)
252+
.at(implementsNamedType),
251253
);
252254
return true;
253255
}

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class CompileTimeErrorCode extends DiagnosticCodeWithExpectedTypes {
4242
);
4343

4444
/// Parameters:
45-
/// String p0: the display name for the kind of the found abstract member
46-
/// String p1: the name of the member
45+
/// String kind: the display name for the kind of the found abstract member
46+
/// String name: the name of the member
4747
static const CompileTimeErrorTemplate<
48-
LocatableDiagnostic Function({required String p0, required String p1})
48+
LocatableDiagnostic Function({required String kind, required String name})
4949
>
5050
abstractSuperMemberReference = CompileTimeErrorTemplate(
5151
'ABSTRACT_SUPER_MEMBER_REFERENCE',
@@ -470,9 +470,9 @@ class CompileTimeErrorCode extends DiagnosticCodeWithExpectedTypes {
470470
);
471471

472472
/// Parameters:
473-
/// String p0: the name of the base class being implemented
473+
/// String superElementName: the name of the base class being implemented
474474
static const CompileTimeErrorTemplate<
475-
LocatableDiagnostic Function({required String p0})
475+
LocatableDiagnostic Function({required String superElementName})
476476
>
477477
baseClassImplementedOutsideOfLibrary = CompileTimeErrorTemplate(
478478
'INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY',
@@ -485,9 +485,9 @@ class CompileTimeErrorCode extends DiagnosticCodeWithExpectedTypes {
485485
);
486486

487487
/// Parameters:
488-
/// String p0: the name of the base mixin being implemented
488+
/// String superElementName: the name of the base mixin being implemented
489489
static const CompileTimeErrorTemplate<
490-
LocatableDiagnostic Function({required String p0})
490+
LocatableDiagnostic Function({required String superElementName})
491491
>
492492
baseMixinImplementedOutsideOfLibrary = CompileTimeErrorTemplate(
493493
'INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY',
@@ -8065,10 +8065,10 @@ class CompileTimeErrorCode extends DiagnosticCodeWithExpectedTypes {
80658065
DiagnosticType get type => DiagnosticType.COMPILE_TIME_ERROR;
80668066

80678067
static LocatableDiagnostic _withArgumentsAbstractSuperMemberReference({
8068-
required String p0,
8069-
required String p1,
8068+
required String kind,
8069+
required String name,
80708070
}) {
8071-
return LocatableDiagnosticImpl(abstractSuperMemberReference, [p0, p1]);
8071+
return LocatableDiagnosticImpl(abstractSuperMemberReference, [kind, name]);
80728072
}
80738073

80748074
static LocatableDiagnostic _withArgumentsAmbiguousExport({
@@ -8166,13 +8166,21 @@ class CompileTimeErrorCode extends DiagnosticCodeWithExpectedTypes {
81668166
}
81678167

81688168
static LocatableDiagnostic
8169-
_withArgumentsBaseClassImplementedOutsideOfLibrary({required String p0}) {
8170-
return LocatableDiagnosticImpl(baseClassImplementedOutsideOfLibrary, [p0]);
8169+
_withArgumentsBaseClassImplementedOutsideOfLibrary({
8170+
required String superElementName,
8171+
}) {
8172+
return LocatableDiagnosticImpl(baseClassImplementedOutsideOfLibrary, [
8173+
superElementName,
8174+
]);
81718175
}
81728176

81738177
static LocatableDiagnostic
8174-
_withArgumentsBaseMixinImplementedOutsideOfLibrary({required String p0}) {
8175-
return LocatableDiagnosticImpl(baseMixinImplementedOutsideOfLibrary, [p0]);
8178+
_withArgumentsBaseMixinImplementedOutsideOfLibrary({
8179+
required String superElementName,
8180+
}) {
8181+
return LocatableDiagnosticImpl(baseMixinImplementedOutsideOfLibrary, [
8182+
superElementName,
8183+
]);
81768184
}
81778185

81788186
static LocatableDiagnostic _withArgumentsBodyMightCompleteNormally({

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import 'package:analyzer/dart/element/element.dart';
1616
import 'package:analyzer/dart/element/type.dart';
1717
import 'package:analyzer/diagnostic/diagnostic.dart';
1818
import 'package:analyzer/error/error.dart';
19-
import 'package:analyzer/error/listener.dart';
2019
import 'package:analyzer/src/dart/analysis/analysis_options.dart';
2120
import 'package:analyzer/src/dart/analysis/file_state.dart';
2221
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -40,6 +39,7 @@ import 'package:analyzer/src/error/constructor_fields_verifier.dart';
4039
import 'package:analyzer/src/error/correct_override.dart';
4140
import 'package:analyzer/src/error/duplicate_definition_verifier.dart';
4241
import 'package:analyzer/src/error/getter_setter_types_verifier.dart';
42+
import 'package:analyzer/src/error/listener.dart';
4343
import 'package:analyzer/src/error/literal_element_verifier.dart';
4444
import 'package:analyzer/src/error/required_parameters_verifier.dart';
4545
import 'package:analyzer/src/error/return_type_verifier.dart';
@@ -1766,9 +1766,8 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
17661766
FieldElement fieldElement,
17671767
) {
17681768
if (fieldElement.isAbstract) {
1769-
diagnosticReporter.atToken(
1770-
identifier,
1771-
CompileTimeErrorCode.abstractFieldConstructorInitializer,
1769+
diagnosticReporter.report(
1770+
CompileTimeErrorCode.abstractFieldConstructorInitializer.at(identifier),
17721771
);
17731772
}
17741773
if (fieldElement.isExternal) {
@@ -2151,16 +2150,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
21512150
// to avoid double errors if implementing `int`.
21522151
if (interfaceElement is ClassElementImpl &&
21532152
!interfaceElement.isSealed) {
2154-
diagnosticReporter.atNode(
2155-
interface,
2156-
CompileTimeErrorCode.baseClassImplementedOutsideOfLibrary,
2157-
arguments: [interfaceElement.name!],
2153+
diagnosticReporter.report(
2154+
CompileTimeErrorCode.baseClassImplementedOutsideOfLibrary
2155+
.withArguments(superElementName: interfaceElement.name!)
2156+
.at(interface),
21582157
);
21592158
} else if (interfaceElement is MixinElement) {
2160-
diagnosticReporter.atNode(
2161-
interface,
2162-
CompileTimeErrorCode.baseMixinImplementedOutsideOfLibrary,
2163-
arguments: [interfaceElement.name!],
2159+
diagnosticReporter.report(
2160+
CompileTimeErrorCode.baseMixinImplementedOutsideOfLibrary
2161+
.withArguments(superElementName: interfaceElement.name!)
2162+
.at(interface),
21642163
);
21652164
}
21662165
break;

pkg/analyzer/messages.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ CompileTimeErrorCode:
340340
hasPublishedDocs: true
341341
ABSTRACT_SUPER_MEMBER_REFERENCE:
342342
parameters:
343-
String p0: the display name for the kind of the found abstract member
344-
String p1: the name of the member
345-
problemMessage: "The #p0 '#p1' is always abstract in the supertype."
343+
String kind: the display name for the kind of the found abstract member
344+
String name: the name of the member
345+
problemMessage: "The #kind '#name' is always abstract in the supertype."
346346
hasPublishedDocs: true
347347
documentation: |-
348348
#### Description
@@ -1331,9 +1331,9 @@ CompileTimeErrorCode:
13311331
```
13321332
BASE_CLASS_IMPLEMENTED_OUTSIDE_OF_LIBRARY:
13331333
parameters:
1334-
String p0: the name of the base class being implemented
1334+
String superElementName: the name of the base class being implemented
13351335
sharedName: INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY
1336-
problemMessage: "The class '#p0' can't be implemented outside of its library because it's a base class."
1336+
problemMessage: "The class '#superElementName' can't be implemented outside of its library because it's a base class."
13371337
hasPublishedDocs: true
13381338
documentation: |-
13391339
#### Description
@@ -1383,9 +1383,9 @@ CompileTimeErrorCode:
13831383
```
13841384
BASE_MIXIN_IMPLEMENTED_OUTSIDE_OF_LIBRARY:
13851385
parameters:
1386-
String p0: the name of the base mixin being implemented
1386+
String superElementName: the name of the base mixin being implemented
13871387
sharedName: INVALID_USE_OF_TYPE_OUTSIDE_LIBRARY
1388-
problemMessage: "The mixin '#p0' can't be implemented outside of its library because it's a base mixin."
1388+
problemMessage: "The mixin '#superElementName' can't be implemented outside of its library because it's a base mixin."
13891389
hasPublishedDocs: true
13901390
BODY_MIGHT_COMPLETE_NORMALLY:
13911391
parameters:

0 commit comments

Comments
 (0)