Skip to content

Commit 8928283

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Stop using Diagnostic.data and deprecate it.
The only two uses of `Diagnostic.data` have been replaced with expandos keyed off the `Diagnostic` object. To make this possible, the `at...` methods of `DiagnosticReporter` have been changed to return the `Diagnostic` object they create. This paves the way for a future CL series, in which I plan to change the API for reporting errors to a more literate API in which the caller will have access to the `Diagnostic` object. Change-Id: I40820387abcd53daf934974c93106803d706fe07 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444461 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 1bcb9fa commit 8928283

File tree

12 files changed

+112
-62
lines changed

12 files changed

+112
-62
lines changed

pkg/analysis_server/lib/src/services/correction/dart/add_missing_switch_cases.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class AddMissingSwitchCases extends ResolvedCorrectionProducer {
3232
return;
3333
}
3434

35-
var patternPartsList = diagnostic.data;
36-
if (patternPartsList is! List<List<MissingPatternPart>>) {
35+
var patternPartsList = MissingPatternPart.byDiagnostic[diagnostic];
36+
if (patternPartsList == null) {
3737
return;
3838
}
3939

pkg/analysis_server/lib/src/services/correction/fix/pubspec/fix_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class PubspecFixGenerator {
154154
defaultEol: endOfLine,
155155
);
156156

157-
var data = diagnostic.data as MissingDependencyData;
157+
var data = MissingDependencyData.byDiagnostic[diagnostic]!;
158158
var addDeps = data.addDeps;
159159
var addDevDeps = data.addDevDeps;
160160
var removeDevDeps = data.removeDevDeps;

pkg/analyzer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* Deprecate `ClassElement.isImplementableIn`; use `isImplementableOutside` instead.
2323
* Deprecate `ClassElement.isMixableIn`; use `isMixableOutside` instead.
2424
* Deprecate `MixinElement.isImplementableIn`; use `isImplementableOutside` instead.
25+
* Deprecate `Diagnostic.data`. Instead, please use an expando to attach data to
26+
the `Diagnostic` object.
2527

2628
## 8.0.0
2729
* Remove deprecated element model V1.

pkg/analyzer/api.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,8 +4763,8 @@ package:analyzer/dart/sdk/build_sdk_summary.dart:
47634763
buildSdkSummary (function: Future<Uint8List> Function({String? embedderYamlPath, required ResourceProvider resourceProvider, required String sdkPath}))
47644764
package:analyzer/diagnostic/diagnostic.dart:
47654765
Diagnostic (class extends Object):
4766-
forValues (constructor: Diagnostic Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required String message, required int offset, required Source source}))
4767-
tmp (constructor: Diagnostic Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required int offset, required Source source}))
4766+
forValues (constructor: Diagnostic Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, deprecated Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required String message, required int offset, required Source source}))
4767+
tmp (constructor: Diagnostic Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, deprecated Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required int offset, required Source source}))
47684768
contextMessages (getter: List<DiagnosticMessage>)
47694769
correction (getter: String?, deprecated)
47704770
correctionMessage (getter: String?)
@@ -4874,13 +4874,13 @@ package:analyzer/error/listener.dart:
48744874
lockLevel (getter: int)
48754875
lockLevel= (setter: int)
48764876
source (getter: Source)
4877-
atConstructorDeclaration (method: void Function(ConstructorDeclaration, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4878-
atElement2 (method: void Function(Element, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}), experimental)
4879-
atEntity (method: void Function(SyntacticEntity, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4880-
atNode (method: void Function(AstNode, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4881-
atOffset (method: void Function({List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required int offset}))
4882-
atSourceSpan (method: void Function(SourceSpan, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4883-
atToken (method: void Function(Token, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4877+
atConstructorDeclaration (method: Diagnostic Function(ConstructorDeclaration, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}))
4878+
atElement2 (method: Diagnostic Function(Element, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}), experimental)
4879+
atEntity (method: Diagnostic Function(SyntacticEntity, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}))
4880+
atNode (method: Diagnostic Function(AstNode, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}))
4881+
atOffset (method: Diagnostic Function({List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data, DiagnosticCode? diagnosticCode, deprecated DiagnosticCode? errorCode, required int length, required int offset}))
4882+
atSourceSpan (method: Diagnostic Function(SourceSpan, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}))
4883+
atToken (method: Diagnostic Function(Token, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, deprecated Object? data}))
48844884
reportError (method: void Function(Diagnostic))
48854885
RecordingDiagnosticListener (class extends Object implements AnalysisErrorListener, DiagnosticListener):
48864886
new (constructor: RecordingDiagnosticListener Function())

pkg/analyzer/lib/diagnostic/diagnostic.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Diagnostic {
2424
final List<DiagnosticMessage> contextMessages;
2525

2626
/// Data associated with this diagnostic, specific for [diagnosticCode].
27+
@Deprecated('Use an expando instead')
2728
final Object? data;
2829

2930
/// A description of how to fix the problem, or `null` if there is no such
@@ -46,7 +47,7 @@ class Diagnostic {
4647
required String message,
4748
this.correctionMessage,
4849
this.contextMessages = const [],
49-
this.data,
50+
@Deprecated('Use an expando instead') this.data,
5051
}) : diagnosticCode = _useNonNullCodeBetween(diagnosticCode, errorCode),
5152
problemMessage = DiagnosticMessageImpl(
5253
filePath: source.fullName,
@@ -72,7 +73,7 @@ class Diagnostic {
7273
DiagnosticCode? errorCode,
7374
List<Object?> arguments = const [],
7475
List<DiagnosticMessage> contextMessages = const [],
75-
Object? data,
76+
@Deprecated('Use an expando instead') Object? data,
7677
}) {
7778
var code = _useNonNullCodeBetween(diagnosticCode, errorCode);
7879
assert(
@@ -98,6 +99,7 @@ class Diagnostic {
9899
message: message,
99100
correctionMessage: correctionMessage,
100101
contextMessages: contextMessages,
102+
// ignore: deprecated_member_use_from_same_package
101103
data: data,
102104
);
103105
}

pkg/analyzer/lib/error/listener.dart

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,87 +96,102 @@ class DiagnosticReporter {
9696
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
9797
///
9898
/// The location of the diagnostic will be the name of the [node].
99-
void atConstructorDeclaration(
99+
///
100+
/// The reported [Diagnostic] is returned so that the caller may attach
101+
/// additional information to it (for example, using an expando).
102+
Diagnostic atConstructorDeclaration(
100103
ConstructorDeclaration node,
101104
DiagnosticCode diagnosticCode, {
102105
List<Object>? arguments,
103106
List<DiagnosticMessage>? contextMessages,
104-
Object? data,
107+
@Deprecated('Use an expando instead') Object? data,
105108
}) {
106109
// TODO(brianwilkerson): Consider extending this method to take any
107110
// declaration and compute the correct range for the name of that
108111
// declaration. This might make it easier to be consistent.
109112
if (node.name case var nameToken?) {
110113
var offset = node.returnType.offset;
111-
atOffset(
114+
return atOffset(
112115
offset: offset,
113116
length: nameToken.end - offset,
114117
diagnosticCode: diagnosticCode,
115118
arguments: arguments,
116119
);
117120
} else {
118-
atNode(node.returnType, diagnosticCode, arguments: arguments);
121+
return atNode(node.returnType, diagnosticCode, arguments: arguments);
119122
}
120123
}
121124

122125
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
123126
///
124127
/// The [element] is used to compute the location of the diagnostic.
128+
///
129+
/// The reported [Diagnostic] is returned so that the caller may attach
130+
/// additional information to it (for example, using an expando).
125131
@experimental
126-
void atElement2(
132+
Diagnostic atElement2(
127133
Element element,
128134
DiagnosticCode diagnosticCode, {
129135
List<Object>? arguments,
130136
List<DiagnosticMessage>? contextMessages,
131-
Object? data,
137+
@Deprecated('Use an expando instead') Object? data,
132138
}) {
133139
var nonSynthetic = element.nonSynthetic;
134-
atOffset(
140+
return atOffset(
135141
diagnosticCode: diagnosticCode,
136142
offset: nonSynthetic.firstFragment.nameOffset ?? -1,
137143
length: nonSynthetic.name?.length ?? 0,
138144
arguments: arguments,
139145
contextMessages: contextMessages,
146+
// ignore: deprecated_member_use_from_same_package
140147
data: data,
141148
);
142149
}
143150

144151
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
145152
///
146153
/// The [entity] is used to compute the location of the diagnostic.
147-
void atEntity(
154+
///
155+
/// The reported [Diagnostic] is returned so that the caller may attach
156+
/// additional information to it (for example, using an expando).
157+
Diagnostic atEntity(
148158
SyntacticEntity entity,
149159
DiagnosticCode diagnosticCode, {
150160
List<Object>? arguments,
151161
List<DiagnosticMessage>? contextMessages,
152-
Object? data,
162+
@Deprecated('Use an expando instead') Object? data,
153163
}) {
154-
atOffset(
164+
return atOffset(
155165
diagnosticCode: diagnosticCode,
156166
offset: entity.offset,
157167
length: entity.length,
158168
arguments: arguments,
159169
contextMessages: contextMessages,
170+
// ignore: deprecated_member_use_from_same_package
160171
data: data,
161172
);
162173
}
163174

164175
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
165176
///
166177
/// The [node] is used to compute the location of the diagnostic.
167-
void atNode(
178+
///
179+
/// The reported [Diagnostic] is returned so that the caller may attach
180+
/// additional information to it (for example, using an expando).
181+
Diagnostic atNode(
168182
AstNode node,
169183
DiagnosticCode diagnosticCode, {
170184
List<Object>? arguments,
171185
List<DiagnosticMessage>? contextMessages,
172-
Object? data,
186+
@Deprecated('Use an expando instead') Object? data,
173187
}) {
174-
atOffset(
188+
return atOffset(
175189
diagnosticCode: diagnosticCode,
176190
offset: node.offset,
177191
length: node.length,
178192
arguments: arguments,
179193
contextMessages: contextMessages,
194+
// ignore: deprecated_member_use_from_same_package
180195
data: data,
181196
);
182197
}
@@ -186,18 +201,18 @@ class DiagnosticReporter {
186201
///
187202
/// The location of the diagnostic is specified by the given [offset] and
188203
/// [length].
189-
void atOffset({
204+
///
205+
/// The reported [Diagnostic] is returned so that the caller may attach
206+
/// additional information to it (for example, using an expando).
207+
Diagnostic atOffset({
190208
required int offset,
191209
required int length,
192210
@Deprecated("Use 'diagnosticCode' instead") DiagnosticCode? errorCode,
193211
DiagnosticCode? diagnosticCode,
194212
List<Object>? arguments,
195213
List<DiagnosticMessage>? contextMessages,
196-
Object? data,
214+
@Deprecated('Use an expando instead') Object? data,
197215
}) {
198-
if (lockLevel != 0) {
199-
return;
200-
}
201216
if ((errorCode == null && diagnosticCode == null) ||
202217
(errorCode != null && diagnosticCode != null)) {
203218
throw ArgumentError(
@@ -225,61 +240,73 @@ class DiagnosticReporter {
225240

226241
contextMessages ??= [];
227242
contextMessages.addAll(convertTypeNames(arguments));
228-
_diagnosticListener.onDiagnostic(
229-
Diagnostic.tmp(
230-
source: _source,
231-
offset: offset,
232-
length: length,
233-
diagnosticCode: diagnosticCode,
234-
arguments: arguments ?? const [],
235-
contextMessages: contextMessages,
236-
data: data,
237-
),
243+
var diagnostic = Diagnostic.tmp(
244+
source: _source,
245+
offset: offset,
246+
length: length,
247+
diagnosticCode: diagnosticCode,
248+
arguments: arguments ?? const [],
249+
contextMessages: contextMessages,
250+
// ignore: deprecated_member_use_from_same_package
251+
data: data,
238252
);
253+
reportError(diagnostic);
254+
return diagnostic;
239255
}
240256

241257
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
242258
///
243259
/// The [span] is used to compute the location of the diagnostic.
244-
void atSourceSpan(
260+
///
261+
/// The reported [Diagnostic] is returned so that the caller may attach
262+
/// additional information to it (for example, using an expando).
263+
Diagnostic atSourceSpan(
245264
SourceSpan span,
246265
DiagnosticCode diagnosticCode, {
247266
List<Object>? arguments,
248267
List<DiagnosticMessage>? contextMessages,
249-
Object? data,
268+
@Deprecated('Use an expando instead') Object? data,
250269
}) {
251-
atOffset(
270+
return atOffset(
252271
diagnosticCode: diagnosticCode,
253272
offset: span.start.offset,
254273
length: span.length,
255274
arguments: arguments,
256275
contextMessages: contextMessages,
276+
// ignore: deprecated_member_use_from_same_package
257277
data: data,
258278
);
259279
}
260280

261281
/// Reports a diagnostic with the given [diagnosticCode] and [arguments].
262282
///
263283
/// The [token] is used to compute the location of the diagnostic.
264-
void atToken(
284+
///
285+
/// The reported [Diagnostic] is returned so that the caller may attach
286+
/// additional information to it (for example, using an expando).
287+
Diagnostic atToken(
265288
Token token,
266289
DiagnosticCode diagnosticCode, {
267290
List<Object>? arguments,
268291
List<DiagnosticMessage>? contextMessages,
269-
Object? data,
292+
@Deprecated('Use an expando instead') Object? data,
270293
}) {
271-
atOffset(
294+
return atOffset(
272295
diagnosticCode: diagnosticCode,
273296
offset: token.offset,
274297
length: token.length,
275298
arguments: arguments,
276299
contextMessages: contextMessages,
300+
// ignore: deprecated_member_use_from_same_package
277301
data: data,
278302
);
279303
}
280304

281305
/// Report the given [diagnostic].
282306
void reportError(Diagnostic diagnostic) {
307+
if (lockLevel != 0) {
308+
return;
309+
}
283310
_diagnosticListener.onDiagnostic(diagnostic);
284311
}
285312
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
11151115
correctionData.add(correctionDataBuffer.parts);
11161116
}
11171117
}
1118-
_diagnosticReporter.atToken(
1118+
var diagnostic = _diagnosticReporter.atToken(
11191119
switchKeyword,
11201120
isSwitchExpression
11211121
? CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_EXPRESSION
@@ -1125,8 +1125,10 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
11251125
errorBuffer.toString(),
11261126
correctionTextBuffer.toString(),
11271127
],
1128-
data: correctionData.isNotEmpty ? correctionData : null,
11291128
);
1129+
if (correctionData.isNotEmpty) {
1130+
MissingPatternPart.byDiagnostic[diagnostic] = correctionData;
1131+
}
11301132
}
11311133
} else {
11321134
if (defaultNode != null && mustBeExhaustive) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,11 @@ class LiteralElementVerifier {
257257
);
258258

259259
if (iterableType == null) {
260-
return _diagnosticReporter.atNode(
260+
_diagnosticReporter.atNode(
261261
expression,
262262
CompileTimeErrorCode.NOT_ITERABLE_SPREAD,
263263
);
264+
return;
264265
}
265266

266267
var iterableElementType = iterableType.typeArguments[0];
@@ -357,10 +358,11 @@ class LiteralElementVerifier {
357358
var mapType = expressionType.asInstanceOf(typeProvider.mapElement);
358359

359360
if (mapType == null) {
360-
return _diagnosticReporter.atNode(
361+
_diagnosticReporter.atNode(
361362
expression,
362363
CompileTimeErrorCode.NOT_MAP_SPREAD,
363364
);
365+
return;
364366
}
365367

366368
var keyType = mapType.typeArguments[0];

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,14 @@ class MissingPatternEnumValuePart extends MissingPatternPart {
476476
String toString() => value2.name!;
477477
}
478478

479-
abstract class MissingPatternPart {}
479+
abstract class MissingPatternPart {
480+
/// Expando associating each
481+
/// [CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_EXPRESSION] or
482+
/// [CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_STATEMENT] diagnostic with a
483+
/// list of missing patterns; this data is used by the analysis server to add
484+
/// missing switch cases.
485+
static final byDiagnostic = Expando<List<List<MissingPatternPart>>>();
486+
}
480487

481488
class MissingPatternTextPart extends MissingPatternPart {
482489
final String text;

0 commit comments

Comments
 (0)