@@ -994,7 +994,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
994
994
_checkTypeArgumentCount (typeArguments, 1 ,
995
995
StaticTypeWarningCode .EXPECTED_ONE_LIST_TYPE_ARGUMENTS );
996
996
}
997
- _checkForRawTypedLiteral (node);
997
+ _checkForInferenceFailureOnCollectionLiteral (node);
998
998
_checkForImplicitDynamicTypedLiteral (node);
999
999
_checkForListElementTypeNotAssignable (node);
1000
1000
@@ -1201,7 +1201,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1201
1201
_checkTypeArgumentCount (typeArguments, 2 ,
1202
1202
StaticTypeWarningCode .EXPECTED_TWO_MAP_TYPE_ARGUMENTS );
1203
1203
}
1204
- _checkForRawTypedLiteral (node);
1204
+ _checkForInferenceFailureOnCollectionLiteral (node);
1205
1205
_checkForImplicitDynamicTypedLiteral (node);
1206
1206
_checkForMapTypeNotAssignable (node);
1207
1207
_checkForNonConstMapAsExpressionStatement3 (node);
@@ -1217,7 +1217,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1217
1217
_checkTypeArgumentCount (typeArguments, 1 ,
1218
1218
StaticTypeWarningCode .EXPECTED_ONE_SET_TYPE_ARGUMENTS );
1219
1219
}
1220
- _checkForRawTypedLiteral (node);
1220
+ _checkForInferenceFailureOnCollectionLiteral (node);
1221
1221
_checkForImplicitDynamicTypedLiteral (node);
1222
1222
_checkForSetElementTypeNotAssignable3 (node);
1223
1223
}
@@ -3666,6 +3666,25 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
3666
3666
[directive.uri.stringValue]);
3667
3667
}
3668
3668
3669
+ /// Checks a collection literal for an inference failure, and reports the
3670
+ /// appropriate error if [AnalysisOptionsImpl.strictInference] is set.
3671
+ ///
3672
+ /// This checks if [node] does not have explicit or inferred type arguments.
3673
+ /// When that happens, it reports a
3674
+ /// HintCode.INFERENCE_FAILURE_ON_COLLECTION_LITERAL error.
3675
+ void _checkForInferenceFailureOnCollectionLiteral (TypedLiteral node) {
3676
+ if (! _options.strictInference || node == null ) return ;
3677
+ if (node.typeArguments != null ) {
3678
+ // Type has explicit type arguments.
3679
+ return ;
3680
+ }
3681
+ var type = node.staticType;
3682
+ if (_isMissingTypeArguments (node, type, type.element, node)) {
3683
+ _errorReporter.reportErrorForNode (
3684
+ HintCode .INFERENCE_FAILURE_ON_COLLECTION_LITERAL , node, [type.name]);
3685
+ }
3686
+ }
3687
+
3669
3688
/**
3670
3689
* Check that the given [typeReference] is not a type reference and that then
3671
3690
* the [name] is reference to an instance member.
@@ -4692,60 +4711,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
4692
4711
CompileTimeErrorCode .PRIVATE_OPTIONAL_PARAMETER , parameter);
4693
4712
}
4694
4713
4695
- /// Similar to [_checkForRawTypeName] but for list/map/set literals.
4696
- void _checkForRawTypedLiteral (TypedLiteral node) {
4697
- if (! _options.strictRawTypes || node == null ) return ;
4698
- if (node.typeArguments != null ) {
4699
- // Type has explicit type arguments.
4700
- return ;
4701
- }
4702
- var type = node.staticType;
4703
- return _checkForRawTypeErrors (node, type, type.element, node);
4704
- }
4705
-
4706
- /// Given a [node] without type arguments that refers to [element] , issues
4707
- /// an error if [type] is a generic type, and the type arguments were not
4708
- /// supplied from inference or a non-dynamic default instantiation.
4709
- ///
4710
- /// This function is used by other node-specific raw type checking functions
4711
- /// (for example [_checkForRawTypeName] ), and should only be called when
4712
- /// we already know [AnalysisOptionsImpl.strictRawTypes] is true and [node]
4713
- /// has no explicit `typeArguments` .
4714
- ///
4715
- /// [inferenceContextNode] is the node that has the downwards context type,
4716
- /// if any. For example an [InstanceCreationExpression] .
4717
- ///
4718
- /// The raw type error [HintCode.STRICT_RAW_TYPE] will *not* be reported when
4719
- /// any of the following are true:
4720
- ///
4721
- /// - [inferenceContextNode] has an inference context type that does not
4722
- /// contain `?`
4723
- /// - [type] does not have any `dynamic` type arguments.
4724
- /// - the element is marked with `@optionalTypeArgs` from "package:meta".
4725
- void _checkForRawTypeErrors (AstNode node, DartType type, Element element,
4726
- Expression inferenceContextNode) {
4727
- assert (_options.strictRawTypes);
4728
- // Check if this type has type arguments and at least one is dynamic.
4729
- // If so, we may need to issue a strict-raw-types error.
4730
- if (type is ParameterizedType &&
4731
- type.typeArguments.any ((t) => t.isDynamic)) {
4732
- // If we have an inference context node, check if the type was inferred
4733
- // from it. Some cases will not have a context type, such as the type
4734
- // annotation `List` in `List list;`
4735
- if (inferenceContextNode != null ) {
4736
- var contextType = InferenceContext .getContext (inferenceContextNode);
4737
- if (contextType != null && UnknownInferredType .isKnown (contextType)) {
4738
- // Type was inferred from downwards context: not an error.
4739
- return ;
4740
- }
4741
- }
4742
- if (element.hasOptionalTypeArgs) {
4743
- return ;
4744
- }
4745
- _errorReporter.reportErrorForNode (HintCode .STRICT_RAW_TYPE , node, [type]);
4746
- }
4747
- }
4748
-
4749
4714
/// Checks a type annotation for a raw generic type, and reports the
4750
4715
/// appropriate error if [AnalysisOptionsImpl.strictRawTypes] is set.
4751
4716
///
@@ -4766,8 +4731,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
4766
4731
inferenceContextNode = grandparent;
4767
4732
}
4768
4733
}
4769
- return _checkForRawTypeErrors (
4770
- node, node.type, node.name.staticElement, inferenceContextNode);
4734
+ if (_isMissingTypeArguments (
4735
+ node, node.type, node.name.staticElement, inferenceContextNode)) {
4736
+ _errorReporter
4737
+ .reportErrorForNode (HintCode .STRICT_RAW_TYPE , node, [node.type]);
4738
+ }
4771
4739
}
4772
4740
4773
4741
/**
@@ -6111,6 +6079,46 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
6111
6079
return false ;
6112
6080
}
6113
6081
6082
+ /// Given a [node] without type arguments that refers to [element] , issues
6083
+ /// an error if [type] is a generic type, and the type arguments were not
6084
+ /// supplied from inference or a non-dynamic default instantiation.
6085
+ ///
6086
+ /// This function is used by other node-specific type checking functions, and
6087
+ /// should only be called when [node] has no explicit `typeArguments` .
6088
+ ///
6089
+ /// [inferenceContextNode] is the node that has the downwards context type,
6090
+ /// if any. For example an [InstanceCreationExpression] .
6091
+ ///
6092
+ /// This function will return false if any of the following are true:
6093
+ ///
6094
+ /// - [inferenceContextNode] has an inference context type that does not
6095
+ /// contain `?`
6096
+ /// - [type] does not have any `dynamic` type arguments.
6097
+ /// - the element is marked with `@optionalTypeArgs` from "package:meta".
6098
+ bool _isMissingTypeArguments (AstNode node, DartType type, Element element,
6099
+ Expression inferenceContextNode) {
6100
+ // Check if this type has type arguments and at least one is dynamic.
6101
+ // If so, we may need to issue a strict-raw-types error.
6102
+ if (type is ParameterizedType &&
6103
+ type.typeArguments.any ((t) => t.isDynamic)) {
6104
+ // If we have an inference context node, check if the type was inferred
6105
+ // from it. Some cases will not have a context type, such as the type
6106
+ // annotation `List` in `List list;`
6107
+ if (inferenceContextNode != null ) {
6108
+ var contextType = InferenceContext .getContext (inferenceContextNode);
6109
+ if (contextType != null && UnknownInferredType .isKnown (contextType)) {
6110
+ // Type was inferred from downwards context: not an error.
6111
+ return false ;
6112
+ }
6113
+ }
6114
+ if (element.hasOptionalTypeArgs) {
6115
+ return false ;
6116
+ }
6117
+ return true ;
6118
+ }
6119
+ return false ;
6120
+ }
6121
+
6114
6122
/**
6115
6123
* Return `true` if the given 'this' [expression] is in a valid context.
6116
6124
*/
0 commit comments