@@ -1000,6 +1000,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1000
1000
_checkTypeArgumentCount (typeArguments, 1 ,
1001
1001
StaticTypeWarningCode .EXPECTED_ONE_LIST_TYPE_ARGUMENTS );
1002
1002
}
1003
+ _checkForRawTypedLiteral (node);
1003
1004
_checkForImplicitDynamicTypedLiteral (node);
1004
1005
_checkForListElementTypeNotAssignable (node);
1005
1006
@@ -1020,6 +1021,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1020
1021
_checkTypeArgumentCount (typeArguments, 2 ,
1021
1022
StaticTypeWarningCode .EXPECTED_TWO_MAP_TYPE_ARGUMENTS );
1022
1023
}
1024
+ _checkForRawTypedLiteral (node);
1023
1025
_checkForImplicitDynamicTypedLiteral (node);
1024
1026
_checkForMapTypeNotAssignable (node);
1025
1027
_checkForNonConstMapAsExpressionStatement (node);
@@ -1225,6 +1227,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1225
1227
_checkTypeArgumentCount (typeArguments, 1 ,
1226
1228
StaticTypeWarningCode .EXPECTED_ONE_SET_TYPE_ARGUMENTS );
1227
1229
}
1230
+ _checkForRawTypedLiteral (node);
1228
1231
_checkForImplicitDynamicTypedLiteral (node);
1229
1232
_checkForSetElementTypeNotAssignable (node);
1230
1233
@@ -1246,6 +1249,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1246
1249
_checkTypeArgumentCount (typeArguments, 2 ,
1247
1250
StaticTypeWarningCode .EXPECTED_TWO_MAP_TYPE_ARGUMENTS );
1248
1251
}
1252
+ _checkForRawTypedLiteral (node);
1249
1253
_checkForImplicitDynamicTypedLiteral (node);
1250
1254
_checkForMapTypeNotAssignable3 (node);
1251
1255
_checkForNonConstMapAsExpressionStatement3 (node);
@@ -1261,6 +1265,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1261
1265
_checkTypeArgumentCount (typeArguments, 1 ,
1262
1266
StaticTypeWarningCode .EXPECTED_ONE_SET_TYPE_ARGUMENTS );
1263
1267
}
1268
+ _checkForRawTypedLiteral (node);
1264
1269
_checkForImplicitDynamicTypedLiteral (node);
1265
1270
_checkForSetElementTypeNotAssignable3 (node);
1266
1271
}
@@ -1358,6 +1363,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
1358
1363
@override
1359
1364
void visitTypeName (TypeName node) {
1360
1365
_checkForTypeArgumentNotMatchingBounds (node);
1366
+ _checkForRawTypeName (node);
1361
1367
super .visitTypeName (node);
1362
1368
}
1363
1369
@@ -4506,11 +4512,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
4506
4512
Map <LibraryElement , Map <String , String >> mixedInNames =
4507
4513
< LibraryElement , Map <String , String >> {};
4508
4514
4509
- /**
4510
- * Report an error and return `true` if the given [name] is a private name
4511
- * (which is defined in the given [library] ) and it conflicts with another
4512
- * definition of that name inherited from the superclass.
4513
- */
4515
+ /// Report an error and return `true` if the given [name] is a private name
4516
+ /// (which is defined in the given [library] ) and it conflicts with another
4517
+ /// definition of that name inherited from the superclass.
4514
4518
bool isConflictingName (
4515
4519
String name, LibraryElement library, TypeName typeName) {
4516
4520
if (Identifier .isPrivateName (name)) {
@@ -4998,6 +5002,93 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
4998
5002
CompileTimeErrorCode .PRIVATE_OPTIONAL_PARAMETER , parameter);
4999
5003
}
5000
5004
5005
+ /// Similar to [_checkForRawTypeName] but for list/map/set literals.
5006
+ void _checkForRawTypedLiteral (TypedLiteral node) {
5007
+ if (! _options.strictRawTypes || node == null ) return ;
5008
+ if (node.typeArguments != null ) {
5009
+ // Type has explicit type arguments.
5010
+ return ;
5011
+ }
5012
+ var type = node.staticType;
5013
+ return _checkForRawTypeErrors (node, type, type.element, node);
5014
+ }
5015
+
5016
+ /// Given a [node] without type arguments that refers to [element] , issues
5017
+ /// an error if [type] is a generic type, and the type arguments were not
5018
+ /// supplied from inference or a non-dynamic default instantiation.
5019
+ ///
5020
+ /// This function is used by other node-specific raw type checking functions
5021
+ /// (for example [_checkForRawTypeName] ), and should only be called when
5022
+ /// we already know [AnalysisOptionsImpl.strictRawTypes] is true and [node]
5023
+ /// has no explicit `typeArguments` .
5024
+ ///
5025
+ /// [inferenceContextNode] is the node that has the downwards context type,
5026
+ /// if any. For example an [InstanceCreationExpression] .
5027
+ ///
5028
+ /// The raw type error [HintCode.STRICT_RAW_TYPE] will *not* be reported when
5029
+ /// any of the following are true:
5030
+ ///
5031
+ /// - [inferenceContextNode] has an inference context type that does not
5032
+ /// contain `?`
5033
+ /// - [type] does not have any `dynamic` type arguments.
5034
+ /// - the element is marked with `@optionalTypeArgs` from "package:meta".
5035
+ void _checkForRawTypeErrors (AstNode node, DartType type, Element element,
5036
+ Expression inferenceContextNode) {
5037
+ assert (_options.strictRawTypes);
5038
+ // Check if this type has type arguments and at least one is dynamic.
5039
+ // If so, we may need to issue a strict-raw-types error.
5040
+ if (type is ParameterizedType &&
5041
+ type.typeArguments.any ((t) => t.isDynamic)) {
5042
+ // If we have an inference context node, check if the type was inferred
5043
+ // from it. Some cases will not have a context type, such as the type
5044
+ // annotation `List` in `List list;`
5045
+ if (inferenceContextNode != null ) {
5046
+ var contextType = InferenceContext .getContext (inferenceContextNode);
5047
+ if (contextType != null && UnknownInferredType .isKnown (contextType)) {
5048
+ // Type was inferred from downwards context: not an error.
5049
+ return ;
5050
+ }
5051
+ }
5052
+ if (element.metadata.isNotEmpty) {
5053
+ for (var annotation in element.metadata) {
5054
+ var e = annotation.element;
5055
+ // TODO(jmesserly): similar "package:meta" annotations are added to
5056
+ // the element as boolean getters, that may be worth considering.
5057
+ if (e? .name == 'optionalTypeArgs' &&
5058
+ e.librarySource.uri.toString () == 'package:meta/meta.dart' ) {
5059
+ // Type is marked with `@optionalTypeArgs`: not an error.
5060
+ return ;
5061
+ }
5062
+ }
5063
+ }
5064
+ _errorReporter.reportErrorForNode (HintCode .STRICT_RAW_TYPE , node, [type]);
5065
+ }
5066
+ }
5067
+
5068
+ /// Checks a type annotation for a raw generic type, and reports the
5069
+ /// appropriate error if [AnalysisOptionsImpl.strictRawTypes] is set.
5070
+ ///
5071
+ /// This checks if [node] refers to a generic type and does not have explicit
5072
+ /// or inferred type arguments. When that happens, it reports error code
5073
+ /// [StrongModeCode.STRICT_RAW_TYPE] .
5074
+ void _checkForRawTypeName (TypeName node) {
5075
+ if (! _options.strictRawTypes || node == null ) return ;
5076
+ if (node.typeArguments != null ) {
5077
+ // Type has explicit type arguments.
5078
+ return ;
5079
+ }
5080
+ var parent = node.parent;
5081
+ InstanceCreationExpression inferenceContextNode;
5082
+ if (parent is ConstructorName ) {
5083
+ var grandparent = parent.parent;
5084
+ if (grandparent is InstanceCreationExpression ) {
5085
+ inferenceContextNode = grandparent;
5086
+ }
5087
+ }
5088
+ return _checkForRawTypeErrors (
5089
+ node, node.type, node.name.staticElement, inferenceContextNode);
5090
+ }
5091
+
5001
5092
/**
5002
5093
* Check whether the given constructor [declaration] is the redirecting
5003
5094
* generative constructor and references itself directly or indirectly. The
0 commit comments