Skip to content

Commit d48e7dc

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Consume types from messages.yaml files.
Now that all diagnostic codes in analyzer-style `messages.yaml` files have a `type:` entry, it can safely be used to determine the diagnostic's type. This replaces the old logic that inferred diagnostic types from the diagnostic's class name (diagnostics under the heading `CompileTimeErrorCode` have type `compileTimeError`, those under `StaticWarningCode` have type `staticWarning`, etc). This paves the way for removing the notion of diagnostic code class entirely. Change-Id: I6a6a69646fdcaccaef6f8c32a1ebf303a54548d8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464283 Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent c8ab18f commit d48e7dc

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

pkg/analyzer/test/verify_diagnostics_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class DocumentationValidator {
371371
}
372372
for (int i = 0; i < exampleSnippets.length; i++) {
373373
_SnippetData snippet = exampleSnippets[i];
374-
if (message.analyzerCode.diagnosticClass == linterLintCodeInfo) {
374+
if (message.type == AnalyzerDiagnosticType.lint) {
375375
snippet.lintCode = codeName;
376376
}
377377
await _validateSnippet('example', i, snippet);
@@ -386,7 +386,7 @@ class DocumentationValidator {
386386
if (firstExample != null) {
387387
snippet.auxiliaryFiles.addAll(firstExample.auxiliaryFiles);
388388
}
389-
if (message.analyzerCode.diagnosticClass == linterLintCodeInfo) {
389+
if (message.type == AnalyzerDiagnosticType.lint) {
390390
snippet.lintCode = codeName;
391391
}
392392
await _validateSnippet('fixes', i, snippet);

pkg/analyzer_utilities/lib/analyzer_messages.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,24 @@ enum AnalyzerDiagnosticPackage {
311311
analyzer(
312312
diagnosticPathPart: 'src/diagnostic/diagnostic',
313313
dirName: 'analyzer',
314+
permittedTypes: {
315+
.compileTimeError,
316+
.hint,
317+
.staticWarning,
318+
.syntacticError,
319+
.todo,
320+
},
314321
),
315322
analysisServer(
316323
diagnosticPathPart: 'src/diagnostic',
317324
dirName: 'analysis_server',
325+
permittedTypes: {.compileTimeError},
318326
shouldIgnorePreferSingleQuotes: true,
319327
),
320328
linter(
321329
diagnosticPathPart: 'src/diagnostic',
322330
dirName: 'linter',
331+
permittedTypes: {.lint},
323332
shouldIgnorePreferExpressionFunctionBodies: true,
324333
shouldIgnorePreferSingleQuotes: true,
325334
);
@@ -335,6 +344,9 @@ enum AnalyzerDiagnosticPackage {
335344
/// file will be `pkg/linter/lib/src/diagnostic/diagnostic.g.dart`.
336345
final String diagnosticPathPart;
337346

347+
/// The set of [AnalyzerDiagnosticType]s that may be used in this package.
348+
final Set<AnalyzerDiagnosticType> permittedTypes;
349+
338350
/// Whether code generated in this package needs an "ignore" comment to ignore
339351
/// the `prefer_expression_function_bodies` lint.
340352
final bool shouldIgnorePreferExpressionFunctionBodies;
@@ -346,6 +358,7 @@ enum AnalyzerDiagnosticPackage {
346358
const AnalyzerDiagnosticPackage({
347359
required this.diagnosticPathPart,
348360
required this.dirName,
361+
required this.permittedTypes,
349362
this.shouldIgnorePreferExpressionFunctionBodies = false,
350363
this.shouldIgnorePreferSingleQuotes = false,
351364
});
@@ -415,7 +428,7 @@ class AnalyzerMessage extends Message with MessageWithAnalyzerCode {
415428
final AnalyzerDiagnosticPackage package;
416429

417430
@override
418-
final AnalyzerDiagnosticType? type;
431+
final AnalyzerDiagnosticType type;
419432

420433
factory AnalyzerMessage(
421434
MessageYaml messageYaml, {
@@ -450,7 +463,6 @@ class AnalyzerMessage extends Message with MessageWithAnalyzerCode {
450463
type = messageYaml.get(
451464
'type',
452465
decode: MessageWithAnalyzerCode.decodeType,
453-
ifAbsent: () => null,
454466
),
455467
super(messageYaml) {
456468
// Ignore extra keys related to analyzer example-based tests.
@@ -545,7 +557,7 @@ mixin MessageWithAnalyzerCode on Message {
545557
(value) =>
546558
value != null && value.any((part) => part is TemplateParameterPart),
547559
);
548-
var baseClasses = analyzerCode.diagnosticClass.type.baseClasses;
560+
var baseClasses = type.baseClasses;
549561
if (parameters.isNotEmpty && !usesParameters) {
550562
throw 'Error code declares parameters using a `parameters` entry, but '
551563
"doesn't use them";
@@ -595,9 +607,8 @@ mixin MessageWithAnalyzerCode on Message {
595607
/// The package into which this error code will be generated.
596608
AnalyzerDiagnosticPackage get package;
597609

598-
/// The type of this diagnostic, if present in `messages.yaml`. Otherwise
599-
/// `null`.
600-
AnalyzerDiagnosticType? get type;
610+
/// The type of this diagnostic.
611+
AnalyzerDiagnosticType get type;
601612

602613
void outputConstantHeader(StringSink out) {
603614
out.write(toAnalyzerComments(indent: ' '));
@@ -617,7 +628,7 @@ mixin MessageWithAnalyzerCode on Message {
617628
var diagnosticCode = analyzerCode.snakeCaseName;
618629
var correctionMessage = this.correctionMessage;
619630
String? withArgumentsName;
620-
var baseClasses = analyzerCode.diagnosticClass.type.baseClasses;
631+
var baseClasses = type.baseClasses;
621632
var ConstantStyle(:concreteClassName, :staticType) = constantStyle;
622633
if (constantStyle case WithArgumentsConstantStyle(
623634
:var withArgumentsParams,
@@ -662,7 +673,7 @@ LocatableDiagnostic $withArgumentsName({$withArgumentsParams}) {
662673
constant.writeln('isUnresolvedIdentifier:true,');
663674
}
664675
if (baseClasses.requiresTypeArgument) {
665-
constant.writeln('type: ${diagnosticClassInfo.type.code},');
676+
constant.writeln('type: ${type.code},');
666677
}
667678
String uniqueName = analyzerCode.toString().replaceFirst(
668679
'LinterLintCode.',

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,21 @@ class DiagnosticTables {
451451
.add(message);
452452
var package = message.package;
453453
var type = message.type;
454-
if (type != null && analyzerCode.diagnosticClass.type != type) {
454+
if (analyzerCode.diagnosticClass.type != type) {
455455
throw LocatedError(
456456
'Diagnostic type is ${type.name}, but its diagnostic class '
457457
'(${analyzerCode.diagnosticClass.name}) implies a type of '
458458
'${analyzerCode.diagnosticClass.type.name}',
459459
span: message.keySpan,
460460
);
461461
}
462+
if (!package.permittedTypes.contains(type)) {
463+
throw LocatedError(
464+
'Diagnostic type is ${type.name}, which may not be used in '
465+
'package:${package.dirName}',
466+
span: message.keySpan,
467+
);
468+
}
462469
if (!message.isRemoved && message is! AliasMessage) {
463470
(activeMessagesByPackage[package] ??= []).add(message);
464471
}
@@ -935,7 +942,7 @@ class SharedMessage extends CfeStyleMessage with MessageWithAnalyzerCode {
935942
final bool hasPublishedDocs;
936943

937944
@override
938-
final AnalyzerDiagnosticType? type;
945+
final AnalyzerDiagnosticType type;
939946

940947
SharedMessage(super.messageYaml)
941948
: analyzerCode = messageYaml.get(
@@ -946,7 +953,6 @@ class SharedMessage extends CfeStyleMessage with MessageWithAnalyzerCode {
946953
type = messageYaml.get(
947954
'type',
948955
decode: MessageWithAnalyzerCode.decodeType,
949-
ifAbsent: () => null,
950956
);
951957

952958
@override

0 commit comments

Comments
 (0)