Skip to content

Commit 95b2e0d

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Generalize runtime types of analyzer message constants.
Changes the runtime types of the diagnostic message constants for the analyzer from derived classes like `ParserErrorWithoutArguments`, `ParserErrorTemplate`, or `ParserErrorCode` to the corresponding base classes (`DiagnosticWithoutArgumentsImpl`, `DiagnosticWithArguments`, or `DiagnosticCodeWithExpectedTypes`). The static types of the constants are unchanged. This allows the generated `...Template` classes (which derived from `DiagnosticWithArguments`) and `...WithoutArguments` classes (which used `DiagnosticWithoutArguments` as a mixin) to be removed. Classes such as `ParserErrorCode`, `WarningCode`, etc. remain, but they are now used solely as namespaces for the generated constants. They will be removed in a later CL. The runtime types of the diagnostic message constants for lints are not changed. They remain `LinterLintWithoutArguments`, `LinterLintTemplate`, and `LinterLintCode`, so that clients of the analyzer public API can still identify lint codes by checking whether they are subtypes of the public API class `LintCode`. Change-Id: I6a6a69640886d560ba53a1b4351f634778d70db5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/459500 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent e3bdaae commit 95b2e0d

File tree

13 files changed

+2700
-1945
lines changed

13 files changed

+2700
-1945
lines changed

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ abstract class DiagnosticCodeImpl extends DiagnosticCode {
337337

338338
/// Private subtype of [DiagnosticCode] that supports runtime checking of
339339
/// parameter types.
340-
abstract class DiagnosticCodeWithExpectedTypes extends DiagnosticCodeImpl {
340+
class DiagnosticCodeWithExpectedTypes extends DiagnosticCodeImpl {
341341
final List<ExpectedType>? expectedTypes;
342342

343343
const DiagnosticCodeWithExpectedTypes({
@@ -568,15 +568,27 @@ class DiagnosticType implements Comparable<DiagnosticType> {
568568
///
569569
/// This class provides a [withArguments] getter, which can be used to supply
570570
/// arguments and produce a [LocatableDiagnostic].
571-
abstract class DiagnosticWithArguments<T extends Function>
572-
implements DiagnosticCode {
571+
class DiagnosticWithArguments<T extends Function>
572+
extends DiagnosticCodeWithExpectedTypes {
573573
/// Function accepting named arguments and returning [LocatableDiagnostic].
574574
///
575575
/// The value returned by this function can
576576
/// be associated with a location in the source code using the
577577
/// [LocatableDiagnostic.at] method, and then the result can be passed to
578578
/// [DiagnosticReporter.reportError].
579-
T get withArguments;
579+
final T withArguments;
580+
581+
const DiagnosticWithArguments({
582+
required super.name,
583+
required super.problemMessage,
584+
super.correctionMessage,
585+
super.hasPublishedDocs = false,
586+
super.isUnresolvedIdentifier = false,
587+
required super.type,
588+
required super.uniqueName,
589+
required super.expectedTypes,
590+
required this.withArguments,
591+
});
580592
}
581593

582594
/// Common functionality for [DiagnosticCode]-derived classes that represent
@@ -609,6 +621,26 @@ base mixin DiagnosticWithoutArguments on DiagnosticCodeImpl
609621
new LocatableDiagnosticImpl(code, arguments, contextMessages: messages);
610622
}
611623

624+
/// Concrete implementation of [DiagnosticWithoutArguments], used for diagnostic
625+
/// messages that don't take any arguments.
626+
///
627+
/// This needs to be a separate class from [DiagnosticWithoutArguments] because
628+
/// [DiagnosticWithoutArguments] is a mixin.
629+
final class DiagnosticWithoutArgumentsImpl
630+
extends DiagnosticCodeWithExpectedTypes
631+
with DiagnosticWithoutArguments {
632+
const DiagnosticWithoutArgumentsImpl({
633+
required super.name,
634+
required super.problemMessage,
635+
super.correctionMessage,
636+
super.hasPublishedDocs = false,
637+
super.isUnresolvedIdentifier = false,
638+
required super.type,
639+
required super.uniqueName,
640+
super.expectedTypes,
641+
});
642+
}
643+
612644
/// Expected type of a diagnostic code's parameter.
613645
enum ExpectedType { element, int, name, object, string, token, type, uri }
614646

pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_error_code.g.dart

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
2626
static const DiagnosticWithArguments<
2727
LocatableDiagnostic Function({required Object p0, required Object p1})
2828
>
29-
conflictingKey = TransformSetErrorTemplate(
29+
conflictingKey = DiagnosticWithArguments(
3030
name: 'conflicting_key',
3131
problemMessage: "The key '{0}' can't be used when '{1}' is also used.",
32+
type: DiagnosticType.COMPILE_TIME_ERROR,
3233
uniqueName: 'TransformSetErrorCode.conflicting_key',
3334
withArguments: _withArgumentsConflictingKey,
3435
expectedTypes: [ExpectedType.object, ExpectedType.object],
3536
);
3637

3738
/// No parameters.
3839
static const DiagnosticWithoutArguments expectedPrimary =
39-
TransformSetErrorWithoutArguments(
40+
DiagnosticWithoutArgumentsImpl(
4041
name: 'expected_primary',
4142
problemMessage: "Expected either an identifier or a string literal.",
43+
type: DiagnosticType.COMPILE_TIME_ERROR,
4244
uniqueName: 'TransformSetErrorCode.expected_primary',
4345
expectedTypes: [],
4446
);
@@ -49,10 +51,11 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
4951
static const DiagnosticWithArguments<
5052
LocatableDiagnostic Function({required Object p0, required Object p1})
5153
>
52-
incompatibleElementKind = TransformSetErrorTemplate(
54+
incompatibleElementKind = DiagnosticWithArguments(
5355
name: 'incompatible_element_kind',
5456
problemMessage:
5557
"An element of kind '{0}' can't be replaced by an element of kind '{1}'.",
58+
type: DiagnosticType.COMPILE_TIME_ERROR,
5659
uniqueName: 'TransformSetErrorCode.incompatible_element_kind',
5760
withArguments: _withArgumentsIncompatibleElementKind,
5861
expectedTypes: [ExpectedType.object, ExpectedType.object],
@@ -64,10 +67,11 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
6467
static const DiagnosticWithArguments<
6568
LocatableDiagnostic Function({required Object p0, required Object p1})
6669
>
67-
invalidChangeForKind = TransformSetErrorTemplate(
70+
invalidChangeForKind = DiagnosticWithArguments(
6871
name: 'invalid_change_for_kind',
6972
problemMessage:
7073
"A change of type '{0}' can't be used for an element of kind '{1}'.",
74+
type: DiagnosticType.COMPILE_TIME_ERROR,
7175
uniqueName: 'TransformSetErrorCode.invalid_change_for_kind',
7276
withArguments: _withArgumentsInvalidChangeForKind,
7377
expectedTypes: [ExpectedType.object, ExpectedType.object],
@@ -78,9 +82,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
7882
static const DiagnosticWithArguments<
7983
LocatableDiagnostic Function({required Object p0})
8084
>
81-
invalidCharacter = TransformSetErrorTemplate(
85+
invalidCharacter = DiagnosticWithArguments(
8286
name: 'invalid_character',
8387
problemMessage: "Invalid character '{0}'.",
88+
type: DiagnosticType.COMPILE_TIME_ERROR,
8489
uniqueName: 'TransformSetErrorCode.invalid_character',
8590
withArguments: _withArgumentsInvalidCharacter,
8691
expectedTypes: [ExpectedType.object],
@@ -91,9 +96,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
9196
static const DiagnosticWithArguments<
9297
LocatableDiagnostic Function({required Object p0})
9398
>
94-
invalidKey = TransformSetErrorTemplate(
99+
invalidKey = DiagnosticWithArguments(
95100
name: 'invalid_key',
96101
problemMessage: "Keys must be of type 'String' but found the type '{0}'.",
102+
type: DiagnosticType.COMPILE_TIME_ERROR,
97103
uniqueName: 'TransformSetErrorCode.invalid_key',
98104
withArguments: _withArgumentsInvalidKey,
99105
expectedTypes: [ExpectedType.object],
@@ -104,20 +110,22 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
104110
static const DiagnosticWithArguments<
105111
LocatableDiagnostic Function({required Object p0})
106112
>
107-
invalidParameterStyle = TransformSetErrorTemplate(
113+
invalidParameterStyle = DiagnosticWithArguments(
108114
name: 'invalid_parameter_style',
109115
problemMessage: "The parameter style must be one of the following: {0}.",
116+
type: DiagnosticType.COMPILE_TIME_ERROR,
110117
uniqueName: 'TransformSetErrorCode.invalid_parameter_style',
111118
withArguments: _withArgumentsInvalidParameterStyle,
112119
expectedTypes: [ExpectedType.object],
113120
);
114121

115122
/// No parameters.
116123
static const DiagnosticWithoutArguments
117-
invalidRequiredIf = TransformSetErrorWithoutArguments(
124+
invalidRequiredIf = DiagnosticWithoutArgumentsImpl(
118125
name: 'invalid_required_if',
119126
problemMessage:
120127
"The key 'requiredIf' can only be used with optional named parameters.",
128+
type: DiagnosticType.COMPILE_TIME_ERROR,
121129
uniqueName: 'TransformSetErrorCode.invalid_required_if',
122130
expectedTypes: [],
123131
);
@@ -133,10 +141,11 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
133141
required Object p2,
134142
})
135143
>
136-
invalidValue = TransformSetErrorTemplate(
144+
invalidValue = DiagnosticWithArguments(
137145
name: 'invalid_value',
138146
problemMessage:
139147
"The value of '{0}' should be of type '{1}' but is of type '{2}'.",
148+
type: DiagnosticType.COMPILE_TIME_ERROR,
140149
uniqueName: 'TransformSetErrorCode.invalid_value',
141150
withArguments: _withArgumentsInvalidValue,
142151
expectedTypes: [
@@ -152,9 +161,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
152161
static const DiagnosticWithArguments<
153162
LocatableDiagnostic Function({required Object p0, required Object p1})
154163
>
155-
invalidValueOneOf = TransformSetErrorTemplate(
164+
invalidValueOneOf = DiagnosticWithArguments(
156165
name: 'invalid_value_one_of',
157166
problemMessage: "The value of '{0}' must be one of the following: '{1}'.",
167+
type: DiagnosticType.COMPILE_TIME_ERROR,
158168
uniqueName: 'TransformSetErrorCode.invalid_value_one_of',
159169
withArguments: _withArgumentsInvalidValueOneOf,
160170
expectedTypes: [ExpectedType.object, ExpectedType.object],
@@ -165,9 +175,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
165175
static const DiagnosticWithArguments<
166176
LocatableDiagnostic Function({required Object p0})
167177
>
168-
missingKey = TransformSetErrorTemplate(
178+
missingKey = DiagnosticWithArguments(
169179
name: 'missing_key',
170180
problemMessage: "Missing the required key '{0}'.",
181+
type: DiagnosticType.COMPILE_TIME_ERROR,
171182
uniqueName: 'TransformSetErrorCode.missing_key',
172183
withArguments: _withArgumentsMissingKey,
173184
expectedTypes: [ExpectedType.object],
@@ -178,19 +189,21 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
178189
static const DiagnosticWithArguments<
179190
LocatableDiagnostic Function({required Object p0})
180191
>
181-
missingOneOfMultipleKeys = TransformSetErrorTemplate(
192+
missingOneOfMultipleKeys = DiagnosticWithArguments(
182193
name: 'missing_one_of_multiple_keys',
183194
problemMessage: "Exactly one of the following keys must be provided: {0}.",
195+
type: DiagnosticType.COMPILE_TIME_ERROR,
184196
uniqueName: 'TransformSetErrorCode.missing_one_of_multiple_keys',
185197
withArguments: _withArgumentsMissingOneOfMultipleKeys,
186198
expectedTypes: [ExpectedType.object],
187199
);
188200

189201
/// No parameters.
190202
static const DiagnosticWithoutArguments missingTemplateEnd =
191-
TransformSetErrorWithoutArguments(
203+
DiagnosticWithoutArgumentsImpl(
192204
name: 'missing_template_end',
193205
problemMessage: "Missing the end brace for the template.",
206+
type: DiagnosticType.COMPILE_TIME_ERROR,
194207
uniqueName: 'TransformSetErrorCode.missing_template_end',
195208
expectedTypes: [],
196209
);
@@ -200,19 +213,21 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
200213
static const DiagnosticWithArguments<
201214
LocatableDiagnostic Function({required Object p0})
202215
>
203-
missingToken = TransformSetErrorTemplate(
216+
missingToken = DiagnosticWithArguments(
204217
name: 'missing_token',
205218
problemMessage: "Expected to find {0}.",
219+
type: DiagnosticType.COMPILE_TIME_ERROR,
206220
uniqueName: 'TransformSetErrorCode.missing_token',
207221
withArguments: _withArgumentsMissingToken,
208222
expectedTypes: [ExpectedType.object],
209223
);
210224

211225
/// No parameters.
212226
static const DiagnosticWithoutArguments missingUri =
213-
TransformSetErrorWithoutArguments(
227+
DiagnosticWithoutArgumentsImpl(
214228
name: 'missing_uri',
215229
problemMessage: "At least one URI must be provided.",
230+
type: DiagnosticType.COMPILE_TIME_ERROR,
216231
uniqueName: 'TransformSetErrorCode.missing_uri',
217232
expectedTypes: [],
218233
);
@@ -222,9 +237,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
222237
static const DiagnosticWithArguments<
223238
LocatableDiagnostic Function({required Object p0})
224239
>
225-
undefinedVariable = TransformSetErrorTemplate(
240+
undefinedVariable = DiagnosticWithArguments(
226241
name: 'undefined_variable',
227242
problemMessage: "The variable '{0}' isn't defined.",
243+
type: DiagnosticType.COMPILE_TIME_ERROR,
228244
uniqueName: 'TransformSetErrorCode.undefined_variable',
229245
withArguments: _withArgumentsUndefinedVariable,
230246
expectedTypes: [ExpectedType.object],
@@ -235,9 +251,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
235251
static const DiagnosticWithArguments<
236252
LocatableDiagnostic Function({required Object p0})
237253
>
238-
unexpectedTransformSetToken = TransformSetErrorTemplate(
254+
unexpectedTransformSetToken = DiagnosticWithArguments(
239255
name: 'unexpected_transform_set_token',
240256
problemMessage: "Didn't expect to find {0}.",
257+
type: DiagnosticType.COMPILE_TIME_ERROR,
241258
uniqueName: 'TransformSetErrorCode.unexpected_transform_set_token',
242259
withArguments: _withArgumentsUnexpectedTransformSetToken,
243260
expectedTypes: [ExpectedType.object],
@@ -248,9 +265,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
248265
static const DiagnosticWithArguments<
249266
LocatableDiagnostic Function({required Object p0})
250267
>
251-
unknownAccessor = TransformSetErrorTemplate(
268+
unknownAccessor = DiagnosticWithArguments(
252269
name: 'unknown_accessor',
253270
problemMessage: "The accessor '{0}' is invalid.",
271+
type: DiagnosticType.COMPILE_TIME_ERROR,
254272
uniqueName: 'TransformSetErrorCode.unknown_accessor',
255273
withArguments: _withArgumentsUnknownAccessor,
256274
expectedTypes: [ExpectedType.object],
@@ -261,30 +279,33 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
261279
static const DiagnosticWithArguments<
262280
LocatableDiagnostic Function({required Object p0})
263281
>
264-
unsupportedKey = TransformSetErrorTemplate(
282+
unsupportedKey = DiagnosticWithArguments(
265283
name: 'unsupported_key',
266284
problemMessage: "The key '{0}' isn't supported.",
285+
type: DiagnosticType.COMPILE_TIME_ERROR,
267286
uniqueName: 'TransformSetErrorCode.unsupported_key',
268287
withArguments: _withArgumentsUnsupportedKey,
269288
expectedTypes: [ExpectedType.object],
270289
);
271290

272291
/// No parameters.
273292
static const DiagnosticWithoutArguments unsupportedStatic =
274-
TransformSetErrorWithoutArguments(
293+
DiagnosticWithoutArgumentsImpl(
275294
name: 'unsupported_static',
276295
problemMessage:
277296
"The key 'static' is only supported for elements in a class, enum, "
278297
"extension, or mixin.",
298+
type: DiagnosticType.COMPILE_TIME_ERROR,
279299
uniqueName: 'TransformSetErrorCode.unsupported_static',
280300
expectedTypes: [],
281301
);
282302

283303
/// No parameters.
284304
static const DiagnosticWithoutArguments unsupportedVersion =
285-
TransformSetErrorWithoutArguments(
305+
DiagnosticWithoutArgumentsImpl(
286306
name: 'unsupported_version',
287307
problemMessage: "Only version '1' is supported at this time.",
308+
type: DiagnosticType.COMPILE_TIME_ERROR,
288309
uniqueName: 'TransformSetErrorCode.unsupported_version',
289310
expectedTypes: [],
290311
);
@@ -295,9 +316,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
295316
static const DiagnosticWithArguments<
296317
LocatableDiagnostic Function({required Object p0, required Object p1})
297318
>
298-
wrongToken = TransformSetErrorTemplate(
319+
wrongToken = DiagnosticWithArguments(
299320
name: 'wrong_token',
300321
problemMessage: "Expected to find {0}, but found {1}.",
322+
type: DiagnosticType.COMPILE_TIME_ERROR,
301323
uniqueName: 'TransformSetErrorCode.wrong_token',
302324
withArguments: _withArgumentsWrongToken,
303325
expectedTypes: [ExpectedType.object, ExpectedType.object],
@@ -308,9 +330,10 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
308330
static const DiagnosticWithArguments<
309331
LocatableDiagnostic Function({required Object p0})
310332
>
311-
yamlSyntaxError = TransformSetErrorTemplate(
333+
yamlSyntaxError = DiagnosticWithArguments(
312334
name: 'yaml_syntax_error',
313335
problemMessage: "Parse error: {0}",
336+
type: DiagnosticType.COMPILE_TIME_ERROR,
314337
uniqueName: 'TransformSetErrorCode.yaml_syntax_error',
315338
withArguments: _withArgumentsYamlSyntaxError,
316339
expectedTypes: [ExpectedType.object],
@@ -459,36 +482,3 @@ class TransformSetErrorCode extends DiagnosticCodeWithExpectedTypes {
459482
return LocatableDiagnosticImpl(TransformSetErrorCode.yamlSyntaxError, [p0]);
460483
}
461484
}
462-
463-
final class TransformSetErrorTemplate<T extends Function>
464-
extends TransformSetErrorCode
465-
implements DiagnosticWithArguments<T> {
466-
@override
467-
final T withArguments;
468-
469-
/// Initialize a newly created error code to have the given [name].
470-
const TransformSetErrorTemplate({
471-
required super.name,
472-
required super.problemMessage,
473-
super.correctionMessage,
474-
super.hasPublishedDocs = false,
475-
super.isUnresolvedIdentifier = false,
476-
required super.uniqueName,
477-
required super.expectedTypes,
478-
required this.withArguments,
479-
});
480-
}
481-
482-
final class TransformSetErrorWithoutArguments extends TransformSetErrorCode
483-
with DiagnosticWithoutArguments {
484-
/// Initialize a newly created error code to have the given [name].
485-
const TransformSetErrorWithoutArguments({
486-
required super.name,
487-
required super.problemMessage,
488-
super.correctionMessage,
489-
super.hasPublishedDocs = false,
490-
super.isUnresolvedIdentifier = false,
491-
required super.uniqueName,
492-
required super.expectedTypes,
493-
});
494-
}

0 commit comments

Comments
 (0)