Skip to content

Commit 0677dc1

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Replace remaining CFE analyzerCode values.
Replaces the remaining `analyzerCode: FOO` entries in `pkg/_fe_analyzer_shared/messages.yaml` with `psuedoSharedCode: FOO` entries. A `pseudoSharedCode` entry carries two pieces of information: it indicates that the error code is "pseudo-shared" (meaning that there is manually maintained logic that translates it from a CFE diagnostic to an analyzer diagnostic), and it provides an enum value that can be used by the manually maintained logic to identify which analyzer error to translate to. In the few cases where diagnostic had a `pseudoShared: true` annotation but no `analyzerCode`, a fresh `pseudoSharedCode` is introduced. The diagnostic code generation logic is updated accordingly. The logic that translates CFE diagnostics to analyzer diagnostics is also updated. This change carries two benefits: - It more accurately reflects reality; calling these codes `analyzerCode` was inaccurate because there was no automated process to verify that they corresponded to actual analyzer error codes, and in fact in many cases they didn't. - It avoids confusion with the `analyzerCode` values in `pkg/_fe_analyzer_shared/messages.yaml`, which _do_ in fact correspond to actual analyzer error codes. Change-Id: I6a6a69640fe4b04da0296086d052be46126a1ebd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453102 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent dfebf91 commit 0677dc1

File tree

11 files changed

+304
-344
lines changed

11 files changed

+304
-344
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
/// @docImport 'package:_fe_analyzer_shared/src/scanner/errors.dart';
6+
/// @docImport 'package:analyzer/src/fasta/ast_builder.dart';
7+
/// @docImport 'package:analyzer/src/fasta/error_converter.dart';
58
library _fe_analyzer_shared.messages.codes;
69

710
import 'dart:convert' show JsonEncoder, json;
@@ -28,14 +31,26 @@ class Code {
2831
/// this error to its corresponding Analyzer error.
2932
final int index;
3033

31-
final List<AnalyzerCode>? analyzerCodes;
34+
/// Enumerated value that can be used to map this [Code] to a corresponding
35+
/// analyzer code.
36+
///
37+
/// If this value is non-null, then manually maintained logic (such as
38+
/// that in [translateErrorToken], [AstBuilder.addProblem],
39+
/// [AstBuilder.handleRecoverableError], or
40+
/// [FastaErrorReporter.reportMessage]) can use it to translate to a
41+
/// corresponding analyzer error code.
42+
///
43+
/// Note that error codes that require translation in this way are not truly
44+
/// shared (hence the name "pseudoSharedCode"). Truly shared error codes are
45+
/// mapped to corresponding analyzer error codes using [index].
46+
final PseudoSharedCode? pseudoSharedCode;
3247

3348
final CfeSeverity severity;
3449

3550
const Code(
3651
this.name, {
3752
this.index = -1,
38-
this.analyzerCodes,
53+
this.pseudoSharedCode,
3954
this.severity = CfeSeverity.error,
4055
});
4156

@@ -83,7 +98,7 @@ class MessageCode extends Code implements Message {
8398
const MessageCode(
8499
super.name, {
85100
super.index,
86-
super.analyzerCodes,
101+
super.pseudoSharedCode,
87102
super.severity,
88103
required this.problemMessage,
89104
this.correctionMessage,
@@ -124,7 +139,7 @@ class Template<TOld extends Function, T extends Function> extends Code {
124139
required this.withArgumentsOld,
125140
required this.withArguments,
126141
super.index = -1,
127-
super.analyzerCodes,
142+
super.pseudoSharedCode,
128143
super.severity = CfeSeverity.error,
129144
});
130145

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 78 additions & 68 deletions
Large diffs are not rendered by default.

pkg/_fe_analyzer_shared/lib/src/scanner/errors.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
3030
}
3131

3232
Code errorCode = token.errorCode;
33-
switch (errorCode.analyzerCodes?.first) {
34-
case AnalyzerCode.encoding:
33+
switch (errorCode.pseudoSharedCode) {
34+
case PseudoSharedCode.encoding:
3535
reportError(ScannerErrorCode.encoding, charOffset, null);
3636
return;
3737

38-
case AnalyzerCode.unterminatedStringLiteral:
38+
case PseudoSharedCode.unterminatedStringLiteral:
3939
// TODO(paulberry,ahe): Fasta reports the error location as the entire
4040
// string; analyzer expects the end of the string.
4141
reportError(
@@ -45,7 +45,7 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
4545
);
4646
return;
4747

48-
case AnalyzerCode.unterminatedMultiLineComment:
48+
case PseudoSharedCode.unterminatedMultiLineComment:
4949
// TODO(paulberry,ahe): Fasta reports the error location as the entire
5050
// comment; analyzer expects the end of the comment.
5151
reportError(
@@ -55,27 +55,27 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
5555
);
5656
return;
5757

58-
case AnalyzerCode.missingDigit:
58+
case PseudoSharedCode.missingDigit:
5959
// TODO(paulberry,ahe): Fasta reports the error location as the entire
6060
// number; analyzer expects the end of the number.
6161
charOffset = endOffset - 1;
6262
return _makeError(ScannerErrorCode.missingDigit, null);
6363

64-
case AnalyzerCode.missingHexDigit:
64+
case PseudoSharedCode.missingHexDigit:
6565
// TODO(paulberry,ahe): Fasta reports the error location as the entire
6666
// number; analyzer expects the end of the number.
6767
charOffset = endOffset - 1;
6868
return _makeError(ScannerErrorCode.missingHexDigit, null);
6969

70-
case AnalyzerCode.illegalCharacter:
70+
case PseudoSharedCode.illegalCharacter:
7171
// We can safely assume `token.character` is non-`null` because this error
7272
// is only reported when there is a character associated with the token.
7373
return _makeError(ScannerErrorCode.illegalCharacter, [token.character!]);
7474

75-
case AnalyzerCode.unexpectedSeparatorInNumber:
75+
case PseudoSharedCode.unexpectedSeparatorInNumber:
7676
return _makeError(ScannerErrorCode.unexpectedSeparatorInNumber, null);
7777

78-
case AnalyzerCode.unsupportedOperator:
78+
case PseudoSharedCode.unsupportedOperator:
7979
return _makeError(ScannerErrorCode.unsupportedOperator, [
8080
(token as UnsupportedOperator).token.lexeme,
8181
]);
@@ -101,7 +101,7 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
101101
return _makeError(ScannerErrorCode.missingIdentifier, null);
102102
}
103103
throw new UnimplementedError(
104-
'$errorCode "${errorCode.analyzerCodes?.first}"',
104+
'$errorCode "${errorCode.pseudoSharedCode}"',
105105
);
106106
}
107107
}

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
3131
codeExperimentNotEnabled,
3232
codeExtraneousModifier,
3333
codeInternalProblemUnhandled,
34-
AnalyzerCode;
34+
PseudoSharedCode;
3535
import 'package:_fe_analyzer_shared/src/parser/parser.dart'
3636
show
3737
Assert,
@@ -223,10 +223,8 @@ class AstBuilder extends StackListener {
223223
List<LocatedMessage>? context,
224224
}) {
225225
if (directives.isEmpty &&
226-
(message.code.analyzerCodes?.contains(
227-
AnalyzerCode.nonPartOfDirectiveInPart,
228-
) ??
229-
false)) {
226+
message.code.pseudoSharedCode ==
227+
PseudoSharedCode.nonPartOfDirectiveInPart) {
230228
message = codeDirectiveAfterDeclaration;
231229
}
232230
diagnosticReporter.reportMessage(message, charOffset, length);
@@ -5632,7 +5630,7 @@ class AstBuilder extends StackListener {
56325630
if (isDartLibrary) return;
56335631
}
56345632
debugEvent("Error: ${message.problemMessage}");
5635-
if (message.code.analyzerCodes == null && startToken is ErrorToken) {
5633+
if (message.code.pseudoSharedCode == null && startToken is ErrorToken) {
56365634
translateErrorToken(startToken, diagnosticReporter.reportScannerError);
56375635
} else {
56385636
int offset = startToken.offset;

0 commit comments

Comments
 (0)