Skip to content

Commit 5d445b7

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Migrate shared_type_analyzer.dart to literate diagnostic reporting.
For all the diagnostic messages used by `shared_type_analyzer.dart`, replaces the placeholder parameter names `p0`, `p1`, `p2`, etc. with descriptive parameter names, and updates the call sites that use those diagnostic messages to use the new literate diagnostic reporting API. The changes to `dart` code had to be done manually, since the types substituted into the diagnostic messages needed to be converted from `SharedTypeView` to `TypeImpl` by calling `.unwrapTypeView<TypeImpl>()`. Change-Id: I6a6a6964147dbb304cb21b3fed427ab0d5ed1e6d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/467740 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 65f16e3 commit 5d445b7

File tree

4 files changed

+113
-77
lines changed

4 files changed

+113
-77
lines changed

pkg/analyzer/lib/src/dart/resolver/shared_type_analyzer.dart

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer.dart'
88
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
99
import 'package:analyzer/src/dart/ast/ast.dart';
1010
import 'package:analyzer/src/dart/element/element.dart';
11+
import 'package:analyzer/src/dart/element/type.dart';
1112
import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
1213
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
1314
import 'package:analyzer/src/error/listener.dart';
@@ -42,10 +43,13 @@ class SharedTypeAnalyzerErrors
4243
required SharedTypeView scrutineeType,
4344
required SharedTypeView caseExpressionType,
4445
}) {
45-
_diagnosticReporter.atNode(
46-
caseExpression,
47-
diag.caseExpressionTypeIsNotSwitchExpressionSubtype,
48-
arguments: [caseExpressionType, scrutineeType],
46+
_diagnosticReporter.report(
47+
diag.caseExpressionTypeIsNotSwitchExpressionSubtype
48+
.withArguments(
49+
caseExpressionType: caseExpressionType.unwrapTypeView<TypeImpl>(),
50+
scrutineeType: scrutineeType.unwrapTypeView<TypeImpl>(),
51+
)
52+
.at(caseExpression),
4953
);
5054
}
5155

@@ -110,10 +114,14 @@ class SharedTypeAnalyzerErrors
110114
required PromotableElementImpl variable,
111115
required PromotableElementImpl component,
112116
}) {
113-
_diagnosticReporter.atElement2(
114-
component,
115-
diag.inconsistentPatternVariableLogicalOr,
116-
arguments: [variable.name!],
117+
// Local variables are never synthetic.
118+
assert(identical(component.nonSynthetic, component));
119+
var offset = component.firstFragment.nameOffset ?? 0;
120+
var length = component.name?.length ?? 1;
121+
_diagnosticReporter.report(
122+
diag.inconsistentPatternVariableLogicalOr
123+
.withArguments(name: variable.name!)
124+
.atOffset(offset: offset, length: length),
117125
);
118126
}
119127

@@ -155,10 +163,13 @@ class SharedTypeAnalyzerErrors
155163
required Expression expression,
156164
required SharedTypeView expressionType,
157165
}) {
158-
_diagnosticReporter.atNode(
159-
expression,
160-
diag.forInOfInvalidType,
161-
arguments: [expressionType, 'Iterable'],
166+
_diagnosticReporter.report(
167+
diag.forInOfInvalidType
168+
.withArguments(
169+
expressionType: expressionType.unwrapTypeView<TypeImpl>(),
170+
expectedType: 'Iterable',
171+
)
172+
.at(expression),
162173
);
163174
}
164175

@@ -169,10 +180,13 @@ class SharedTypeAnalyzerErrors
169180
required SharedTypeView matchedType,
170181
required SharedTypeView requiredType,
171182
}) {
172-
_diagnosticReporter.atNode(
173-
pattern,
174-
diag.patternTypeMismatchInIrrefutableContext,
175-
arguments: [matchedType, requiredType],
183+
_diagnosticReporter.report(
184+
diag.patternTypeMismatchInIrrefutableContext
185+
.withArguments(
186+
matchedType: matchedType.unwrapTypeView<TypeImpl>(),
187+
requiredType: requiredType.unwrapTypeView<TypeImpl>(),
188+
)
189+
.at(pattern),
176190
);
177191
}
178192

@@ -192,10 +206,14 @@ class SharedTypeAnalyzerErrors
192206
required SharedTypeView operandType,
193207
required SharedTypeView parameterType,
194208
}) {
195-
_diagnosticReporter.atNode(
196-
pattern.operand,
197-
diag.relationalPatternOperandTypeNotAssignable,
198-
arguments: [operandType, parameterType, pattern.operator.lexeme],
209+
_diagnosticReporter.report(
210+
diag.relationalPatternOperandTypeNotAssignable
211+
.withArguments(
212+
operandType: operandType.unwrapTypeView<TypeImpl>(),
213+
parameterType: parameterType.unwrapTypeView<TypeImpl>(),
214+
operator: pattern.operator.lexeme,
215+
)
216+
.at(pattern.operand),
199217
);
200218
}
201219

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

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,10 +1338,13 @@ caseExpressionTypeImplementsEquals = DiagnosticWithArguments(
13381338
);
13391339

13401340
/// Parameters:
1341-
/// Type p0: the type of the case expression
1342-
/// Type p1: the type of the switch expression
1341+
/// Type caseExpressionType: the type of the case expression
1342+
/// Type scrutineeType: the type of the switch expression
13431343
const DiagnosticWithArguments<
1344-
LocatableDiagnostic Function({required DartType p0, required DartType p1})
1344+
LocatableDiagnostic Function({
1345+
required DartType caseExpressionType,
1346+
required DartType scrutineeType,
1347+
})
13451348
>
13461349
caseExpressionTypeIsNotSwitchExpressionSubtype = DiagnosticWithArguments(
13471350
name: 'case_expression_type_is_not_switch_expression_subtype',
@@ -6148,11 +6151,14 @@ forInOfInvalidElementType = DiagnosticWithArguments(
61486151
);
61496152

61506153
/// Parameters:
6151-
/// Type p0: the type of the iterable expression.
6152-
/// String p1: the sequence type -- Iterable for `for` or Stream for `await
6153-
/// for`.
6154+
/// Type expressionType: the type of the iterable expression.
6155+
/// String expectedType: the sequence type -- Iterable for `for` or Stream for
6156+
/// `await for`.
61546157
const DiagnosticWithArguments<
6155-
LocatableDiagnostic Function({required DartType p0, required String p1})
6158+
LocatableDiagnostic Function({
6159+
required DartType expressionType,
6160+
required String expectedType,
6161+
})
61566162
>
61576163
forInOfInvalidType = DiagnosticWithArguments(
61586164
name: 'for_in_of_invalid_type',
@@ -7087,9 +7093,9 @@ const DiagnosticWithoutArguments inconsistentLanguageVersionOverride =
70877093
);
70887094

70897095
/// Parameters:
7090-
/// String p0: the name of the pattern variable
7096+
/// String name: the name of the pattern variable
70917097
const DiagnosticWithArguments<
7092-
LocatableDiagnostic Function({required String p0})
7098+
LocatableDiagnostic Function({required String name})
70937099
>
70947100
inconsistentPatternVariableLogicalOr = DiagnosticWithArguments(
70957101
name: 'inconsistent_pattern_variable_logical_or',
@@ -13190,10 +13196,13 @@ patternNeverMatchesValueType = DiagnosticWithArguments(
1319013196
);
1319113197

1319213198
/// Parameters:
13193-
/// Type p0: the matched type
13194-
/// Type p1: the required type
13199+
/// Type matchedType: the matched type
13200+
/// Type requiredType: the required type
1319513201
const DiagnosticWithArguments<
13196-
LocatableDiagnostic Function({required DartType p0, required DartType p1})
13202+
LocatableDiagnostic Function({
13203+
required DartType matchedType,
13204+
required DartType requiredType,
13205+
})
1319713206
>
1319813207
patternTypeMismatchInIrrefutableContext = DiagnosticWithArguments(
1319913208
name: 'pattern_type_mismatch_in_irrefutable_context',
@@ -14090,14 +14099,14 @@ refutablePatternInIrrefutableContext = DiagnosticWithoutArgumentsImpl(
1409014099
);
1409114100

1409214101
/// Parameters:
14093-
/// Type p0: the operand type
14094-
/// Type p1: the parameter type of the invoked operator
14095-
/// String p2: the name of the invoked operator
14102+
/// Type operandType: the operand type
14103+
/// Type parameterType: the parameter type of the invoked operator
14104+
/// String operator: the name of the invoked operator
1409614105
const DiagnosticWithArguments<
1409714106
LocatableDiagnostic Function({
14098-
required DartType p0,
14099-
required DartType p1,
14100-
required String p2,
14107+
required DartType operandType,
14108+
required DartType parameterType,
14109+
required String operator,
1410114110
})
1410214111
>
1410314112
relationalPatternOperandTypeNotAssignable = DiagnosticWithArguments(
@@ -18007,12 +18016,12 @@ LocatableDiagnostic _withArgumentsCaseExpressionTypeImplementsEquals({
1800718016

1800818017
LocatableDiagnostic
1800918018
_withArgumentsCaseExpressionTypeIsNotSwitchExpressionSubtype({
18010-
required DartType p0,
18011-
required DartType p1,
18019+
required DartType caseExpressionType,
18020+
required DartType scrutineeType,
1801218021
}) {
1801318022
return LocatableDiagnosticImpl(
1801418023
diag.caseExpressionTypeIsNotSwitchExpressionSubtype,
18015-
[p0, p1],
18024+
[caseExpressionType, scrutineeType],
1801618025
);
1801718026
}
1801818027

@@ -18921,10 +18930,13 @@ LocatableDiagnostic _withArgumentsForInOfInvalidElementType({
1892118930
}
1892218931

1892318932
LocatableDiagnostic _withArgumentsForInOfInvalidType({
18924-
required DartType p0,
18925-
required String p1,
18933+
required DartType expressionType,
18934+
required String expectedType,
1892618935
}) {
18927-
return LocatableDiagnosticImpl(diag.forInOfInvalidType, [p0, p1]);
18936+
return LocatableDiagnosticImpl(diag.forInOfInvalidType, [
18937+
expressionType,
18938+
expectedType,
18939+
]);
1892818940
}
1892918941

1893018942
LocatableDiagnostic _withArgumentsGenericStructSubclass({
@@ -19140,10 +19152,10 @@ LocatableDiagnostic _withArgumentsInconsistentInheritanceGetterAndMethod({
1914019152
}
1914119153

1914219154
LocatableDiagnostic _withArgumentsInconsistentPatternVariableLogicalOr({
19143-
required String p0,
19155+
required String name,
1914419156
}) {
1914519157
return LocatableDiagnosticImpl(diag.inconsistentPatternVariableLogicalOr, [
19146-
p0,
19158+
name,
1914719159
]);
1914819160
}
1914919161

@@ -20299,12 +20311,12 @@ LocatableDiagnostic _withArgumentsPatternNeverMatchesValueType({
2029920311
}
2030020312

2030120313
LocatableDiagnostic _withArgumentsPatternTypeMismatchInIrrefutableContext({
20302-
required DartType p0,
20303-
required DartType p1,
20314+
required DartType matchedType,
20315+
required DartType requiredType,
2030420316
}) {
2030520317
return LocatableDiagnosticImpl(diag.patternTypeMismatchInIrrefutableContext, [
20306-
p0,
20307-
p1,
20318+
matchedType,
20319+
requiredType,
2030820320
]);
2030920321
}
2031020322

@@ -20501,13 +20513,13 @@ LocatableDiagnostic _withArgumentsReferencedBeforeDeclaration({
2050120513
}
2050220514

2050320515
LocatableDiagnostic _withArgumentsRelationalPatternOperandTypeNotAssignable({
20504-
required DartType p0,
20505-
required DartType p1,
20506-
required String p2,
20516+
required DartType operandType,
20517+
required DartType parameterType,
20518+
required String operator,
2050720519
}) {
2050820520
return LocatableDiagnosticImpl(
2050920521
diag.relationalPatternOperandTypeNotAssignable,
20510-
[p0, p1, p2],
20522+
[operandType, parameterType, operator],
2051120523
);
2051220524
}
2051320525

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,10 +2958,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
29582958
String loopNamedType = awaitKeyword != null ? 'Stream' : 'Iterable';
29592959

29602960
if (iterableType is DynamicType && strictCasts) {
2961-
diagnosticReporter.atNode(
2962-
node.iterable,
2963-
diag.forInOfInvalidType,
2964-
arguments: [iterableType, loopNamedType],
2961+
diagnosticReporter.report(
2962+
diag.forInOfInvalidType
2963+
.withArguments(
2964+
expressionType: iterableType,
2965+
expectedType: loopNamedType,
2966+
)
2967+
.at(node.iterable),
29652968
);
29662969
return false;
29672970
}
@@ -2997,10 +3000,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
29973000
requiredSequenceType,
29983001
strictCasts: strictCasts,
29993002
)) {
3000-
diagnosticReporter.atNode(
3001-
node.iterable,
3002-
diag.forInOfInvalidType,
3003-
arguments: [iterableType, loopNamedType],
3003+
diagnosticReporter.report(
3004+
diag.forInOfInvalidType
3005+
.withArguments(
3006+
expressionType: iterableType,
3007+
expectedType: loopNamedType,
3008+
)
3009+
.at(node.iterable),
30043010
);
30053011
return false;
30063012
}

pkg/analyzer/messages.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,9 +2432,9 @@ CompileTimeErrorCode:
24322432
caseExpressionTypeIsNotSwitchExpressionSubtype:
24332433
type: compileTimeError
24342434
parameters:
2435-
Type p0: the type of the case expression
2436-
Type p1: the type of the switch expression
2437-
problemMessage: "The switch case expression type '#p0' must be a subtype of the switch expression type '#p1'."
2435+
Type caseExpressionType: the type of the case expression
2436+
Type scrutineeType: the type of the switch expression
2437+
problemMessage: "The switch case expression type '#caseExpressionType' must be a subtype of the switch expression type '#scrutineeType'."
24382438
hasPublishedDocs: true
24392439
documentation: |-
24402440
#### Description
@@ -7601,9 +7601,9 @@ CompileTimeErrorCode:
76017601
forInOfInvalidType:
76027602
type: compileTimeError
76037603
parameters:
7604-
Type p0: the type of the iterable expression.
7605-
String p1: the sequence type -- Iterable for `for` or Stream for `await for`.
7606-
problemMessage: "The type '#p0' used in the 'for' loop must implement '#p1'."
7604+
Type expressionType: the type of the iterable expression.
7605+
String expectedType: the sequence type -- Iterable for `for` or Stream for `await for`.
7606+
problemMessage: "The type '#expressionType' used in the 'for' loop must implement '#expectedType'."
76077607
hasPublishedDocs: true
76087608
documentation: |-
76097609
#### Description
@@ -8510,8 +8510,8 @@ CompileTimeErrorCode:
85108510
inconsistentPatternVariableLogicalOr:
85118511
type: compileTimeError
85128512
parameters:
8513-
String p0: the name of the pattern variable
8514-
problemMessage: "The variable '#p0' has a different type and/or finality in this branch of the logical-or pattern."
8513+
String name: the name of the pattern variable
8514+
problemMessage: "The variable '#name' has a different type and/or finality in this branch of the logical-or pattern."
85158515
correctionMessage: Try declaring the variable pattern with the same type and finality in both branches.
85168516
hasPublishedDocs: true
85178517
documentation: |-
@@ -14402,9 +14402,9 @@ CompileTimeErrorCode:
1440214402
patternTypeMismatchInIrrefutableContext:
1440314403
type: compileTimeError
1440414404
parameters:
14405-
Type p0: the matched type
14406-
Type p1: the required type
14407-
problemMessage: "The matched value of type '#p0' isn't assignable to the required type '#p1'."
14405+
Type matchedType: the matched type
14406+
Type requiredType: the required type
14407+
problemMessage: "The matched value of type '#matchedType' isn't assignable to the required type '#requiredType'."
1440814408
correctionMessage: "Try changing the required type of the pattern, or the matched value type."
1440914409
hasPublishedDocs: true
1441014410
documentation: |-
@@ -15870,10 +15870,10 @@ CompileTimeErrorCode:
1587015870
relationalPatternOperandTypeNotAssignable:
1587115871
type: compileTimeError
1587215872
parameters:
15873-
Type p0: the operand type
15874-
Type p1: the parameter type of the invoked operator
15875-
String p2: the name of the invoked operator
15876-
problemMessage: "The constant expression type '#p0' is not assignable to the parameter type '#p1' of the '#p2' operator."
15873+
Type operandType: the operand type
15874+
Type parameterType: the parameter type of the invoked operator
15875+
String operator: the name of the invoked operator
15876+
problemMessage: "The constant expression type '#operandType' is not assignable to the parameter type '#parameterType' of the '#operator' operator."
1587715877
hasPublishedDocs: true
1587815878
documentation: |-
1587915879
#### Description

0 commit comments

Comments
 (0)