Skip to content

Commit ab4fc52

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Use LocatableDiagnostic in constant evaluation.
Changes the error reporting logic in analyzer constant evaluation so that diagnostic messages are converted as early as possible into `LocatableDiagnostic` objects by using the statically-checked `.withArguments` methods. Thereafter, the `LocatableDiagnostic` objects are passed around but not modified further (except to add context messages when appropriate). This should make it impossible for bugs like #61761 to occur during constant evaluation, by preventing the diagnostic code for a constant evaluation error from getting out of sync with the corresponding arguments. Change-Id: I6a6a69646130b5a19cbacba8e964fcdebf20d10f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/459941 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent c30b974 commit ab4fc52

File tree

4 files changed

+257
-288
lines changed

4 files changed

+257
-288
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import 'package:analyzer/src/dart/element/type_provider.dart';
3131
import 'package:analyzer/src/dart/element/type_system.dart';
3232
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
3333
import 'package:analyzer/src/error/codes.dart';
34+
import 'package:analyzer/src/error/listener.dart';
3435
import 'package:analyzer/src/generated/exhaustiveness.dart';
3536
import 'package:analyzer/src/utilities/extensions/ast.dart';
3637

@@ -672,7 +673,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
672673
//
673674
// These error codes are more specific than the [defaultErrorCode] so they
674675
// will overwrite and replace the default when we report the error.
675-
DiagnosticCode diagnosticCode = error.diagnosticCode;
676+
DiagnosticCode diagnosticCode = error.locatableDiagnostic.code;
676677
if (identical(
677678
diagnosticCode,
678679
CompileTimeErrorCode.constEvalExtensionMethod,
@@ -803,14 +804,10 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
803804
diagnosticCode,
804805
CompileTimeErrorCode.wrongNumberOfTypeArgumentsAnonymousFunction,
805806
)) {
806-
_diagnosticReporter.reportError(
807-
Diagnostic.tmp(
808-
source: _diagnosticReporter.source,
807+
_diagnosticReporter.report(
808+
error.locatableDiagnostic.atOffset(
809809
offset: error.offset,
810810
length: error.length,
811-
diagnosticCode: error.diagnosticCode,
812-
arguments: error.arguments,
813-
contextMessages: error.contextMessages,
814811
),
815812
);
816813
} else if (defaultDiagnosticCode != null) {
@@ -911,12 +908,11 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
911908
switch (result) {
912909
case InvalidConstant():
913910
if (!result.avoidReporting) {
914-
_diagnosticReporter.atOffset(
915-
offset: result.offset,
916-
length: result.length,
917-
diagnosticCode: result.diagnosticCode,
918-
arguments: result.arguments,
919-
contextMessages: result.contextMessages,
911+
_diagnosticReporter.report(
912+
result.locatableDiagnostic.atOffset(
913+
offset: result.offset,
914+
length: result.length,
915+
),
920916
);
921917
}
922918
case DartObjectImpl():

0 commit comments

Comments
 (0)