Improve set/map/list literal type checking.
We now exclusively use StaticWarningCode.*_TYPE_NOT_ASSIGNABLE to
report static errors.
CheckedModeCompileTimeErrorCode.*_TYPE_NOT_ASSIGNABLE is reserved for
future use when #21119 is fixed.
Change-Id: I681b1253417a135272361306cb3468dd9590b17b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96200
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index a4feb02..d5e0a30 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -3942,21 +3942,9 @@
DartType listElementType = typeArguments[0];
// Check every list element.
- bool isConst = literal.isConst;
for (CollectionElement element in literal.elements2) {
- if (isConst) {
- // TODO(paulberry): this error should be based on the actual type of the
- // list element, not the static type. See dartbug.com/21119.
- _checkForCollectionElementTypeNotAssignableWithElementType(
- element,
- listElementType,
- CheckedModeCompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE);
- } else {
- _checkForCollectionElementTypeNotAssignableWithElementType(
- element,
- listElementType,
- StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE);
- }
+ _checkForCollectionElementTypeNotAssignableWithElementType(element,
+ listElementType, StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE);
}
}
@@ -4031,26 +4019,14 @@
DartType keyType = typeArguments[0];
DartType valueType = typeArguments[1];
- bool isConst = literal.isConst;
NodeList<CollectionElement> entries = literal.elements2;
for (CollectionElement entry in entries) {
- if (isConst) {
- // TODO(paulberry): this error should be based on the actual type of
- // the map entries, not the static type. See dartbug.com/21119.
- _checkForMapElementTypeNotAssignableWithKeyOrValueType(
- entry,
- keyType,
- valueType,
- CheckedModeCompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
- CheckedModeCompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE);
- } else {
- _checkForMapElementTypeNotAssignableWithKeyOrValueType(
- entry,
- keyType,
- valueType,
- StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
- StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE);
- }
+ _checkForMapElementTypeNotAssignableWithKeyOrValueType(
+ entry,
+ keyType,
+ valueType,
+ StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
+ StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE);
}
}
}
@@ -5263,21 +5239,9 @@
DartType setElementType = typeArguments[0];
// Check every set element.
- bool isConst = literal.isConst;
for (CollectionElement element in literal.elements2) {
- if (isConst) {
- // TODO(paulberry): this error should be based on the actual type of
- // the element, not the static type. See dartbug.com/21119.
- _checkForCollectionElementTypeNotAssignableWithElementType(
- element,
- setElementType,
- CheckedModeCompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE);
- } else {
- _checkForCollectionElementTypeNotAssignableWithElementType(
- element,
- setElementType,
- StaticWarningCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE);
- }
+ _checkForCollectionElementTypeNotAssignableWithElementType(element,
+ setElementType, StaticWarningCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE);
}
}
}
diff --git a/pkg/analyzer/test/generated/checked_mode_compile_time_error_code.dart b/pkg/analyzer/test/generated/checked_mode_compile_time_error_code.dart
index cd217b8..c5011c3 100644
--- a/pkg/analyzer/test/generated/checked_mode_compile_time_error_code.dart
+++ b/pkg/analyzer/test/generated/checked_mode_compile_time_error_code.dart
@@ -479,8 +479,7 @@
test_listElementTypeNotAssignable() async {
Source source = addSource("var v = const <String> [42];");
await computeAnalysisResult(source);
- assertErrors(source,
- [CheckedModeCompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
+ assertErrors(source, [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
verify([source]);
}
diff --git a/pkg/analyzer/test/generated/compile_time_error_code_driver_test.dart b/pkg/analyzer/test/generated/compile_time_error_code_driver_test.dart
index 20e0728..a399b73 100644
--- a/pkg/analyzer/test/generated/compile_time_error_code_driver_test.dart
+++ b/pkg/analyzer/test/generated/compile_time_error_code_driver_test.dart
@@ -415,24 +415,21 @@
test_listElementTypeNotAssignable_const() async {
Source source = addSource("var v = const <String>[42];");
await computeAnalysisResult(source);
- assertErrors(source,
- [CheckedModeCompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
+ assertErrors(source, [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
verify([source]);
}
test_mapKeyTypeNotAssignable_const() async {
Source source = addSource("var v = const <String, int >{1 : 2};");
await computeAnalysisResult(source);
- assertErrors(
- source, [CheckedModeCompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE]);
+ assertErrors(source, [StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE]);
verify([source]);
}
test_mapValueTypeNotAssignable_const() async {
Source source = addSource("var v = const <String, String>{'a' : 2};");
await computeAnalysisResult(source);
- assertErrors(source,
- [CheckedModeCompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE]);
+ assertErrors(source, [StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE]);
verify([source]);
}
@@ -512,8 +509,7 @@
test_setElementTypeNotAssignable_const() async {
Source source = addSource("var v = const <String>{42};");
await computeAnalysisResult(source);
- assertErrors(source,
- [CheckedModeCompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE]);
+ assertErrors(source, [StaticWarningCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE]);
verify([source]);
}
}
diff --git a/pkg/analyzer/test/src/diagnostics/list_element_type_not_assignable_test.dart b/pkg/analyzer/test/src/diagnostics/list_element_type_not_assignable_test.dart
index 417d121..148844c 100644
--- a/pkg/analyzer/test/src/diagnostics/list_element_type_not_assignable_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/list_element_type_not_assignable_test.dart
@@ -20,6 +20,21 @@
test_explicitTypeArgs_const() async {
await assertErrorsInCode('''
var v = const <String>[42];
+''', [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
+ }
+
+ test_explicitTypeArgs_const_actualTypeMatch() async {
+ await assertNoErrorsInCode('''
+const dynamic x = null;
+var v = const <String>[x];
+''');
+ }
+
+ @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/21119')
+ test_explicitTypeArgs_const_actualTypeMismatch() async {
+ await assertErrorsInCode('''
+const dynamic x = 42;
+var v = const <String>[x];
''', [CheckedModeCompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
}
diff --git a/pkg/analyzer/test/src/diagnostics/map_key_type_not_assignable_test.dart b/pkg/analyzer/test/src/diagnostics/map_key_type_not_assignable_test.dart
index 5584706..1f17622 100644
--- a/pkg/analyzer/test/src/diagnostics/map_key_type_not_assignable_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/map_key_type_not_assignable_test.dart
@@ -17,15 +17,25 @@
@reflectiveTest
class MapKeyTypeNotAssignableTest extends DriverResolutionTest {
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/35569')
test_explicitTypeArgs_const() async {
- // TODO(brianwilkerson) Fix this so that only one error is produced.
await assertErrorsInCode('''
var v = const <String, int>{42 : 1};
-''', [
- CheckedModeCompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
- StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE
- ]);
+''', [StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE]);
+ }
+
+ test_explicitTypeArgs_const_actualTypeMatch() async {
+ await assertNoErrorsInCode('''
+const dynamic x = null;
+var v = const <String, int>{x : 1};
+''');
+ }
+
+ @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/21119')
+ test_explicitTypeArgs_const_actualTypeMismatch() async {
+ await assertErrorsInCode('''
+const dynamic x = 42;
+var v = const <String, int>{x : 1};
+''', [CheckedModeCompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE]);
}
test_explicitTypeArgs_notConst() async {
@@ -42,12 +52,6 @@
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..enabledExperiments = ['control-flow-collections', 'spread-collections'];
- @failingTest
- @override
- test_explicitTypeArgs_const() async {
- await super.test_explicitTypeArgs_const();
- }
-
test_spread_valid_const() async {
await assertNoErrorsInCode('''
var v = const <String, int>{...{'a' : 1, 'b' : 2}};
diff --git a/pkg/analyzer/test/src/diagnostics/map_value_type_not_assignable_test.dart b/pkg/analyzer/test/src/diagnostics/map_value_type_not_assignable_test.dart
index 60b18a8..d5a111d 100644
--- a/pkg/analyzer/test/src/diagnostics/map_value_type_not_assignable_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/map_value_type_not_assignable_test.dart
@@ -17,15 +17,25 @@
@reflectiveTest
class MapValueTypeNotAssignableTest extends DriverResolutionTest {
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/35569')
test_explicitTypeArgs_const() async {
- // TODO(brianwilkerson) Fix this so that only one error is produced.
await assertErrorsInCode('''
var v = const <String, String>{'a' : 1};
-''', [
- CheckedModeCompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE,
- StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE
- ]);
+''', [StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE]);
+ }
+
+ test_explicitTypeArgs_const_actualTypeMatch() async {
+ await assertNoErrorsInCode('''
+const dynamic x = null;
+var v = const <String, String>{'a' : x};
+''');
+ }
+
+ @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/21119')
+ test_explicitTypeArgs_const_actualTypeMismatch() async {
+ await assertErrorsInCode('''
+const dynamic x = 1;
+var v = const <String, String>{'a' : x};
+''', [CheckedModeCompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE]);
}
test_explicitTypeArgs_notConst() async {
@@ -42,12 +52,6 @@
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..enabledExperiments = ['control-flow-collections', 'spread-collections'];
- @failingTest
- @override
- test_explicitTypeArgs_const() async {
- await super.test_explicitTypeArgs_const();
- }
-
test_spread_valid_const() async {
await assertNoErrorsInCode('''
var v = const <String, int>{...{'a' : 1, 'b' : 2}};
diff --git a/pkg/analyzer/test/src/diagnostics/set_element_type_not_assignable_test.dart b/pkg/analyzer/test/src/diagnostics/set_element_type_not_assignable_test.dart
index 16f26ad..61ebabe 100644
--- a/pkg/analyzer/test/src/diagnostics/set_element_type_not_assignable_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/set_element_type_not_assignable_test.dart
@@ -17,15 +17,25 @@
@reflectiveTest
class SetElementTypeNotAssignableTest extends DriverResolutionTest {
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/35569')
test_explicitTypeArgs_const() async {
- // TODO(brianwilkerson) Fix this so that only one error is produced.
await assertErrorsInCode('''
var v = const <String>{42};
-''', [
- CheckedModeCompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE,
- StaticWarningCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE
- ]);
+''', [StaticWarningCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE]);
+ }
+
+ test_explicitTypeArgs_const_actualTypeMatch() async {
+ await assertNoErrorsInCode('''
+const dynamic x = null;
+var v = const <String>{x};
+''');
+ }
+
+ @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/21119')
+ test_explicitTypeArgs_const_actualTypeMismatch() async {
+ await assertErrorsInCode('''
+const dynamic x = 42;
+var v = const <String>{x};
+''', [CheckedModeCompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE]);
}
test_explicitTypeArgs_notConst() async {
@@ -42,14 +52,6 @@
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..enabledExperiments = ['control-flow-collections', 'spread-collections'];
- @override
- test_explicitTypeArgs_const() async {
- // In the newer code we're correctly only producing one error.
- await assertErrorsInCode('''
-var v = const <String>{42};
-''', [CheckedModeCompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE]);
- }
-
test_spread_valid_const() async {
await assertNoErrorsInCode('''
var v = const <String>{...['a', 'b']};