From 345d0f3a478e601ad6657ec13a0370694bd261dd Mon Sep 17 00:00:00 2001 From: Kallen Tu Date: Fri, 19 May 2023 16:27:58 +0000 Subject: [PATCH] [analyzer] Issue 37238: Multiple errors for type mismatch assignments. Avoid reporting an error if the static types are mismatched because CompileTimeErrorCode.INVALID_ASSIGNMENT would have already been reported by the ErrorVerifier. Closes #37238 Bug: https://github.com/dart-lang/sdk/issues/37238 Change-Id: I2248d054ed6bf6a546e998afb9b731a36e197780 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304362 Reviewed-by: Konstantin Shcheglov Commit-Queue: Kallen Tu --- .../lib/src/dart/constant/evaluation.dart | 16 +++++++------ .../src/dart/constant/evaluation_test.dart | 8 +++++++ .../variable_type_mismatch_test.dart | 1 - tests/language/assign/static_type_test.dart | 4 +--- .../compile_time_constant/static_test.dart | 4 +--- tests/language/const/init2_test.dart | 4 +--- tests/language_2/assign/static_type_test.dart | 23 ++++++++----------- .../compile_time_constant/static_test.dart | 4 +--- tests/language_2/const/init2_test.dart | 4 +--- 9 files changed, 31 insertions(+), 37 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart index 80976496e4f..fdc78c4de65 100644 --- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart +++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart @@ -126,13 +126,15 @@ class ConstantEvaluationEngine { // invoked). if (dartObject != null && constant.isConst) { if (!library.typeSystem.runtimeTypeMatch(dartObject, constant.type)) { - // TODO(brianwilkerson) This should not be reported if - // CompileTimeErrorCode.INVALID_ASSIGNMENT has already been - // reported (that is, if the static types are also wrong). - errorReporter.reportErrorForNode( - CompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, - constantInitializer, - [dartObject.type, constant.type]); + // If the static types are mismatched, an error would have already + // been reported. + if (library.typeSystem.isAssignableTo( + constantInitializer.typeOrThrow, constant.type)) { + errorReporter.reportErrorForNode( + CompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, + constantInitializer, + [dartObject.type, constant.type]); + } } // Associate with the variable. diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart index f1812a96671..384c2e48ae0 100644 --- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart +++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart @@ -28,6 +28,14 @@ main() { @reflectiveTest class ConstantVisitorTest extends ConstantVisitorTestSupport with ConstantVisitorTestCases { + test_declaration_staticError_notAssignable() async { + await assertErrorsInCode(''' +const int x = 'foo'; +''', [ + error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 14, 5), + ]); + } + test_equalEqual_double_object() async { await assertNoErrorsInCode(''' const v = 1.2 == Object(); diff --git a/pkg/analyzer/test/src/diagnostics/variable_type_mismatch_test.dart b/pkg/analyzer/test/src/diagnostics/variable_type_mismatch_test.dart index 0944903676c..d9d71c24497 100644 --- a/pkg/analyzer/test/src/diagnostics/variable_type_mismatch_test.dart +++ b/pkg/analyzer/test/src/diagnostics/variable_type_mismatch_test.dart @@ -34,7 +34,6 @@ const Unresolved x = null; await assertErrorsInCode(''' const int x = 'foo'; ''', [ - error(CompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, 14, 5), error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 14, 5), ]); } diff --git a/tests/language/assign/static_type_test.dart b/tests/language/assign/static_type_test.dart index 2362a6f0cf2..d0a654c2990 100644 --- a/tests/language/assign/static_type_test.dart +++ b/tests/language/assign/static_type_test.dart @@ -13,10 +13,8 @@ int a = "String"; class A { static const int c = "String"; // ^^^^^^^^ - // [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH - // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. - // ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT + // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. final int d = "String"; // ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT diff --git a/tests/language/compile_time_constant/static_test.dart b/tests/language/compile_time_constant/static_test.dart index d5ebd130f48..550e2d8fbda 100644 --- a/tests/language/compile_time_constant/static_test.dart +++ b/tests/language/compile_time_constant/static_test.dart @@ -8,10 +8,8 @@ final int x = 'foo'; // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. const int y = 'foo'; // ^^^^^ -// [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH -// [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. -// ^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT +// [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. int z = 'foo'; // ^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT diff --git a/tests/language/const/init2_test.dart b/tests/language/const/init2_test.dart index 65f0aa2481c..4bd2fc593ed 100644 --- a/tests/language/const/init2_test.dart +++ b/tests/language/const/init2_test.dart @@ -6,10 +6,8 @@ const intValue = 0; const double c = 0.0; const double d = intValue; // ^^^^^^^^ -// [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH -// [cfe] A value of type 'int' can't be assigned to a variable of type 'double'. -// ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT +// [cfe] A value of type 'int' can't be assigned to a variable of type 'double'. main() { print(c); diff --git a/tests/language_2/assign/static_type_test.dart b/tests/language_2/assign/static_type_test.dart index e5d475c0247..f3c949411df 100644 --- a/tests/language_2/assign/static_type_test.dart +++ b/tests/language_2/assign/static_type_test.dart @@ -15,10 +15,8 @@ int a = "String"; class A { static const int c = "String"; // ^^^^^^^^ - // [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH - // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. - // ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT + // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. final int d = "String"; // ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT @@ -28,18 +26,15 @@ class A { // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. A() { - int f = "String"; - // ^^^^^^^^ - // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT - // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. + int f = "String"; + // ^^^^^^^^ + // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT + // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. } - method( - [ - int - g = "String"]) { - // ^^^^^^^^ - // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT - // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. + method([int g = "String"]) { + // ^^^^^^^^ + // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT + // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. return g; } } diff --git a/tests/language_2/compile_time_constant/static_test.dart b/tests/language_2/compile_time_constant/static_test.dart index 5b404489140..77740dd2fad 100644 --- a/tests/language_2/compile_time_constant/static_test.dart +++ b/tests/language_2/compile_time_constant/static_test.dart @@ -10,10 +10,8 @@ final int x = 'foo'; // [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. const int y = 'foo'; // ^^^^^ -// [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH -// [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. -// ^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT +// [cfe] A value of type 'String' can't be assigned to a variable of type 'int'. int z = 'foo'; // ^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT diff --git a/tests/language_2/const/init2_test.dart b/tests/language_2/const/init2_test.dart index f367c29f711..d10e38873fd 100644 --- a/tests/language_2/const/init2_test.dart +++ b/tests/language_2/const/init2_test.dart @@ -8,10 +8,8 @@ const intValue = 0; const double c = 0.0; const double d = intValue; // ^^^^^^^^ -// [analyzer] COMPILE_TIME_ERROR.VARIABLE_TYPE_MISMATCH -// [cfe] A value of type 'int' can't be assigned to a variable of type 'double'. -// ^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT +// [cfe] A value of type 'int' can't be assigned to a variable of type 'double'. main() { print(c);