[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 <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
This commit is contained in:
Kallen Tu 2023-05-19 16:27:58 +00:00 committed by Commit Queue
parent 027660f5b9
commit 345d0f3a47
9 changed files with 31 additions and 37 deletions

View file

@ -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.

View file

@ -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();

View file

@ -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),
]);
}

View file

@ -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

View file

@ -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

View file

@ -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);

View file

@ -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;
}
}

View file

@ -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

View file

@ -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);