Skip to content

Commit 69cd0ee

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Deprecate "errorCode" parameters in Diagnostic constructors
Work towards #60635 Change-Id: Ifc173ec6e5924057fee3c420c8916f0abb257428 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434141 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 3cc8940 commit 69cd0ee

File tree

18 files changed

+76
-58
lines changed

18 files changed

+76
-58
lines changed

pkg/analyzer/api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4655,8 +4655,8 @@ package:analyzer/dart/sdk/build_sdk_summary.dart:
46554655
buildSdkSummary (function: Future<Uint8List> Function({String? embedderYamlPath, required ResourceProvider resourceProvider, required String sdkPath}))
46564656
package:analyzer/diagnostic/diagnostic.dart:
46574657
Diagnostic (class extends Object):
4658-
forValues (constructor: Diagnostic Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, Object? data, required DiagnosticCode errorCode, required int length, required String message, required int offset, required Source source}))
4659-
tmp (constructor: Diagnostic Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, Object? data, required DiagnosticCode errorCode, required int length, required int offset, required Source source}))
4658+
forValues (constructor: Diagnostic Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, Object? data, DiagnosticCode? diagnosticCode, DiagnosticCode? errorCode, required int length, required String message, required int offset, required Source source}))
4659+
tmp (constructor: Diagnostic Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, Object? data, DiagnosticCode? diagnosticCode, DiagnosticCode? errorCode, required int length, required int offset, required Source source}))
46604660
contextMessages (getter: List<DiagnosticMessage>)
46614661
correction (getter: String?, deprecated)
46624662
correctionMessage (getter: String?)

pkg/analyzer/lib/analysis_rule/analysis_rule.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ sealed class AbstractAnalysisRule {
119119
source: node.source,
120120
offset: node.span.start.offset,
121121
length: node.span.length,
122-
errorCode: diagnosticCode,
122+
diagnosticCode: diagnosticCode,
123123
arguments: arguments,
124124
contextMessages: contextMessages,
125125
);
@@ -261,7 +261,7 @@ abstract class MultiAnalysisRule extends AbstractAnalysisRule {
261261
source: (node as PubspecNodeImpl).source,
262262
offset: node.span.start.offset,
263263
length: node.span.length,
264-
errorCode: diagnosticCode,
264+
diagnosticCode: diagnosticCode,
265265
arguments: arguments,
266266
contextMessages: contextMessages,
267267
);

pkg/analyzer/lib/diagnostic/diagnostic.dart

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class Diagnostic {
4040
required this.source,
4141
required int offset,
4242
required int length,
43-
required DiagnosticCode errorCode,
43+
DiagnosticCode? diagnosticCode,
44+
@Deprecated("Pass a value for 'diagnosticCode' instead")
45+
DiagnosticCode? errorCode,
4446
required String message,
4547
this.correctionMessage,
4648
this.contextMessages = const [],
4749
this.data,
48-
}) : diagnosticCode = errorCode,
50+
}) : diagnosticCode = _useNonNullCodeBetween(diagnosticCode, errorCode),
4951
problemMessage = DiagnosticMessageImpl(
5052
filePath: source.fullName,
5153
length: length,
@@ -65,22 +67,24 @@ class Diagnostic {
6567
required Source source,
6668
required int offset,
6769
required int length,
68-
// TODO(srawlins): Rename to `diagnosticCode`.
69-
required DiagnosticCode errorCode,
70+
DiagnosticCode? diagnosticCode,
71+
@Deprecated("Pass a value for 'diagnosticCode' instead")
72+
DiagnosticCode? errorCode,
7073
List<Object?> arguments = const [],
7174
List<DiagnosticMessage> contextMessages = const [],
7275
Object? data,
7376
}) {
77+
var code = _useNonNullCodeBetween(diagnosticCode, errorCode);
7478
assert(
75-
arguments.length == errorCode.numParameters,
76-
'Message $errorCode requires ${errorCode.numParameters} '
77-
'argument${errorCode.numParameters == 1 ? '' : 's'}, but '
79+
arguments.length == code.numParameters,
80+
'Message $code requires ${code.numParameters} '
81+
'argument${code.numParameters == 1 ? '' : 's'}, but '
7882
'${arguments.length} '
7983
'argument${arguments.length == 1 ? ' was' : 's were'} '
8084
'provided',
8185
);
82-
String message = formatList(errorCode.problemMessage, arguments);
83-
String? correctionTemplate = errorCode.correctionMessage;
86+
String message = formatList(code.problemMessage, arguments);
87+
String? correctionTemplate = code.correctionMessage;
8488
String? correctionMessage;
8589
if (correctionTemplate != null) {
8690
correctionMessage = formatList(correctionTemplate, arguments);
@@ -90,7 +94,7 @@ class Diagnostic {
9094
source: source,
9195
offset: offset,
9296
length: length,
93-
errorCode: errorCode,
97+
diagnosticCode: code,
9498
message: message,
9599
correctionMessage: correctionMessage,
96100
contextMessages: contextMessages,
@@ -180,6 +184,19 @@ class Diagnostic {
180184
buffer.write(message);
181185
return buffer.toString();
182186
}
187+
188+
/// The non-`null` [DiagnosticCode] value between the two parameters.
189+
static DiagnosticCode _useNonNullCodeBetween(
190+
DiagnosticCode? diagnosticCode,
191+
DiagnosticCode? errorCode,
192+
) {
193+
if ((diagnosticCode == null) == (errorCode == null)) {
194+
throw ArgumentError(
195+
"Exactly one of 'diagnosticCode' and 'errorCode' may be passed",
196+
);
197+
}
198+
return diagnosticCode ?? errorCode!;
199+
}
183200
}
184201

185202
/// A single message associated with a [Diagnostic], consisting of the text of

pkg/analyzer/lib/error/listener.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class DiagnosticReporter {
216216
source: _source,
217217
offset: offset,
218218
length: length,
219-
errorCode: diagnosticCode,
219+
diagnosticCode: diagnosticCode,
220220
arguments: arguments ?? const [],
221221
contextMessages: contextMessages,
222222
data: data,

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ class AnalysisDriver {
19761976
source: file.source,
19771977
offset: 0,
19781978
length: 0,
1979-
errorCode: CompileTimeErrorCode.MISSING_DART_LIBRARY,
1979+
diagnosticCode: CompileTimeErrorCode.MISSING_DART_LIBRARY,
19801980
arguments: [missingUri],
19811981
),
19821982
],
@@ -2709,7 +2709,7 @@ class ErrorEncoding {
27092709
source: source,
27102710
offset: error.offset,
27112711
length: error.length,
2712-
errorCode: diagnosticCode,
2712+
diagnosticCode: diagnosticCode,
27132713
message: error.message,
27142714
correctionMessage: error.correction.isEmpty ? null : error.correction,
27152715
contextMessages: contextMessages,

pkg/analyzer/lib/src/dart/constant/constant_verifier.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
844844
source: _diagnosticReporter.source,
845845
offset: error.offset,
846846
length: error.length,
847-
errorCode: error.diagnosticCode,
847+
diagnosticCode: error.diagnosticCode,
848848
arguments: error.arguments,
849849
contextMessages: error.contextMessages,
850850
),
@@ -855,7 +855,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
855855
source: _diagnosticReporter.source,
856856
offset: error.offset,
857857
length: error.length,
858-
errorCode: defaultDiagnosticCode,
858+
diagnosticCode: defaultDiagnosticCode,
859859
),
860860
);
861861
}

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ class ElementAnnotationImpl implements ElementAnnotation {
17711771
source: source,
17721772
offset: evaluationResult.offset,
17731773
length: evaluationResult.length,
1774-
errorCode: evaluationResult.diagnosticCode,
1774+
diagnosticCode: evaluationResult.diagnosticCode,
17751775
arguments: evaluationResult.arguments,
17761776
contextMessages: evaluationResult.contextMessages,
17771777
),

pkg/analyzer/lib/src/dart/scanner/scanner.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Scanner {
133133
source: source,
134134
offset: offset,
135135
length: 1,
136-
errorCode: errorCode,
136+
diagnosticCode: errorCode,
137137
arguments: arguments ?? const [],
138138
),
139139
);
@@ -206,7 +206,7 @@ class Scanner {
206206
source: source,
207207
offset: versionToken.offset,
208208
length: versionToken.length,
209-
errorCode: WarningCode.INVALID_LANGUAGE_VERSION_OVERRIDE_GREATER,
209+
diagnosticCode: WarningCode.INVALID_LANGUAGE_VERSION_OVERRIDE_GREATER,
210210
arguments: [latestVersion.major, latestVersion.minor],
211211
),
212212
);

pkg/analyzer/lib/src/diagnostic/diagnostic_factory.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class DiagnosticFactory {
2929
source: source,
3030
offset: duplicate.offset,
3131
length: duplicate.length,
32-
errorCode: CompileTimeErrorCode.DUPLICATE_PATTERN_ASSIGNMENT_VARIABLE,
32+
diagnosticCode:
33+
CompileTimeErrorCode.DUPLICATE_PATTERN_ASSIGNMENT_VARIABLE,
3334
arguments: [variable.name3!],
3435
contextMessages: [
3536
DiagnosticMessageImpl(
@@ -59,7 +60,7 @@ class DiagnosticFactory {
5960
source: duplicateFragment.libraryFragment!.source,
6061
offset: duplicateFragment.nameOffset2 ?? -1,
6162
length: duplicate.name3!.length,
62-
errorCode: code,
63+
diagnosticCode: code,
6364
arguments: arguments,
6465
contextMessages: [
6566
DiagnosticMessageImpl(
@@ -86,7 +87,7 @@ class DiagnosticFactory {
8687
source: source,
8788
offset: duplicateNode.offset,
8889
length: duplicateNode.length,
89-
errorCode: code,
90+
diagnosticCode: code,
9091
arguments: arguments,
9192
contextMessages: [
9293
DiagnosticMessageImpl(
@@ -113,7 +114,7 @@ class DiagnosticFactory {
113114
source: source,
114115
offset: duplicateNode.offset,
115116
length: duplicateNode.length,
116-
errorCode: CompileTimeErrorCode.DUPLICATE_FIELD_NAME,
117+
diagnosticCode: CompileTimeErrorCode.DUPLICATE_FIELD_NAME,
117118
arguments: [duplicateName],
118119
contextMessages: [
119120
DiagnosticMessageImpl(
@@ -143,7 +144,7 @@ class DiagnosticFactory {
143144
source: source,
144145
offset: duplicateNode.offset,
145146
length: duplicateNode.length,
146-
errorCode: CompileTimeErrorCode.DUPLICATE_FIELD_NAME,
147+
diagnosticCode: CompileTimeErrorCode.DUPLICATE_FIELD_NAME,
147148
arguments: [duplicateName],
148149
contextMessages: [
149150
DiagnosticMessageImpl(
@@ -173,7 +174,7 @@ class DiagnosticFactory {
173174
source: source,
174175
offset: duplicateTarget.offset,
175176
length: duplicateTarget.length,
176-
errorCode: CompileTimeErrorCode.DUPLICATE_PATTERN_FIELD,
177+
diagnosticCode: CompileTimeErrorCode.DUPLICATE_PATTERN_FIELD,
177178
arguments: [name],
178179
contextMessages: [
179180
DiagnosticMessageImpl(
@@ -198,7 +199,7 @@ class DiagnosticFactory {
198199
source: source,
199200
offset: duplicateElement.offset,
200201
length: duplicateElement.length,
201-
errorCode: CompileTimeErrorCode.DUPLICATE_REST_ELEMENT_IN_PATTERN,
202+
diagnosticCode: CompileTimeErrorCode.DUPLICATE_REST_ELEMENT_IN_PATTERN,
202203
contextMessages: [
203204
DiagnosticMessageImpl(
204205
filePath: source.fullName,
@@ -222,7 +223,7 @@ class DiagnosticFactory {
222223
source: source,
223224
offset: duplicateElement.offset,
224225
length: duplicateElement.length,
225-
errorCode: CompileTimeErrorCode.EQUAL_ELEMENTS_IN_CONST_SET,
226+
diagnosticCode: CompileTimeErrorCode.EQUAL_ELEMENTS_IN_CONST_SET,
226227
contextMessages: [
227228
DiagnosticMessageImpl(
228229
filePath: source.fullName,
@@ -246,7 +247,7 @@ class DiagnosticFactory {
246247
source: source,
247248
offset: duplicateKey.offset,
248249
length: duplicateKey.length,
249-
errorCode: CompileTimeErrorCode.EQUAL_KEYS_IN_CONST_MAP,
250+
diagnosticCode: CompileTimeErrorCode.EQUAL_KEYS_IN_CONST_MAP,
250251
contextMessages: [
251252
DiagnosticMessageImpl(
252253
filePath: source.fullName,
@@ -270,7 +271,7 @@ class DiagnosticFactory {
270271
source: source,
271272
offset: duplicateKey.offset,
272273
length: duplicateKey.length,
273-
errorCode: CompileTimeErrorCode.EQUAL_KEYS_IN_MAP_PATTERN,
274+
diagnosticCode: CompileTimeErrorCode.EQUAL_KEYS_IN_MAP_PATTERN,
274275
contextMessages: [
275276
DiagnosticMessageImpl(
276277
filePath: source.fullName,
@@ -295,7 +296,7 @@ class DiagnosticFactory {
295296
source: source,
296297
offset: offset,
297298
length: length,
298-
errorCode:
299+
diagnosticCode:
299300
StaticWarningCode.INVALID_NULL_AWARE_OPERATOR_AFTER_SHORT_CIRCUIT,
300301
arguments: arguments,
301302
contextMessages: [
@@ -328,7 +329,7 @@ class DiagnosticFactory {
328329
source: source,
329330
offset: errorNode.offset,
330331
length: errorNode.length,
331-
errorCode: code,
332+
diagnosticCode: code,
332333
arguments: [
333334
memberName,
334335
member.enclosingElement!.name3,
@@ -387,7 +388,7 @@ class DiagnosticFactory {
387388
source: source,
388389
offset: nameToken.offset,
389390
length: nameToken.length,
390-
errorCode: CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION,
391+
diagnosticCode: CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION,
391392
arguments: [name],
392393
contextMessages: contextMessages ?? const [],
393394
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
10191019
fragment.enclosingFragment?.nameOffset2 ??
10201020
0,
10211021
length: fragment.name2?.length ?? 0,
1022-
errorCode: code,
1022+
diagnosticCode: code,
10231023
arguments: arguments,
10241024
),
10251025
);

0 commit comments

Comments
 (0)