Skip to content

Commit 1a829bb

Browse files
committed
Analyzer: Recategorize COULD_NOT_INFER as CompileTimeErrorCode
Change-Id: I2aec1fd6499fcbcc35e7cf9b765dd43559d3ef67 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155846 Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 70aa3ac commit 1a829bb

File tree

8 files changed

+36
-32
lines changed

8 files changed

+36
-32
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const List<ErrorCode> errorCodeValues = [
139139
CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR,
140140
CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT,
141141
CompileTimeErrorCode.CONTINUE_LABEL_ON_SWITCH,
142+
CompileTimeErrorCode.COULD_NOT_INFER,
142143
CompileTimeErrorCode.DEFAULT_LIST_CONSTRUCTOR,
143144
CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPED_PARAMETER,
144145
CompileTimeErrorCode.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR,
@@ -790,7 +791,6 @@ const List<ErrorCode> errorCodeValues = [
790791
StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_POSITIONAL,
791792
StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH,
792793
StaticWarningCode.UNNECESSARY_NON_NULL_ASSERTION,
793-
StrongModeCode.COULD_NOT_INFER,
794794
StrongModeCode.IMPLICIT_DYNAMIC_FIELD,
795795
StrongModeCode.IMPLICIT_DYNAMIC_FUNCTION,
796796
StrongModeCode.IMPLICIT_DYNAMIC_INVOKE,

pkg/analyzer/lib/src/dart/element/generic_inferrer.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
1717
import 'package:analyzer/src/dart/element/type_demotion.dart';
1818
import 'package:analyzer/src/dart/element/type_schema.dart';
1919
import 'package:analyzer/src/dart/element/type_system.dart';
20-
import 'package:analyzer/src/error/codes.dart' show HintCode, StrongModeCode;
20+
import 'package:analyzer/src/error/codes.dart'
21+
show CompileTimeErrorCode, HintCode;
2122
import 'package:meta/meta.dart';
2223

2324
/// Tracks upper and lower type bounds for a set of type parameters.
@@ -187,7 +188,7 @@ class GenericInferrer {
187188
if (!success) {
188189
if (failAtError) return null;
189190
errorReporter?.reportErrorForNode(
190-
StrongModeCode.COULD_NOT_INFER,
191+
CompileTimeErrorCode.COULD_NOT_INFER,
191192
errorNode,
192193
[typeParam.name, _formatError(typeParam, inferred, constraints)]);
193194

@@ -201,8 +202,8 @@ class GenericInferrer {
201202
if (failAtError) return null;
202203
var typeFormals = (inferred as FunctionType).typeFormals;
203204
var typeFormalsStr = typeFormals.map(_elementStr).join(', ');
204-
errorReporter
205-
?.reportErrorForNode(StrongModeCode.COULD_NOT_INFER, errorNode, [
205+
errorReporter?.reportErrorForNode(
206+
CompileTimeErrorCode.COULD_NOT_INFER, errorNode, [
206207
typeParam.name,
207208
' Inferred candidate type ${_typeStr(inferred)} has type parameters'
208209
' [$typeFormalsStr], but a function with'
@@ -248,8 +249,8 @@ class GenericInferrer {
248249
var typeParamBound = Substitution.fromPairs(typeFormals, inferredTypes)
249250
.substituteType(typeParam.bound ?? typeProvider.objectType);
250251
// TODO(jmesserly): improve this error message.
251-
errorReporter
252-
?.reportErrorForNode(StrongModeCode.COULD_NOT_INFER, errorNode, [
252+
errorReporter?.reportErrorForNode(
253+
CompileTimeErrorCode.COULD_NOT_INFER, errorNode, [
253254
typeParam.name,
254255
"\nRecursive bound cannot be instantiated: '$typeParamBound'."
255256
"\nConsider passing explicit type argument(s) "

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
13211321
correction: "Try converting the getter to a method, or "
13221322
"renaming the field to a name that doesn't conflict.");
13231323

1324+
/**
1325+
* Parameters:
1326+
* 0: the name of the type parameter
1327+
* 1: detail text explaining why the type could not be inferred
1328+
*/
1329+
static const CompileTimeErrorCode COULD_NOT_INFER = CompileTimeErrorCode(
1330+
'COULD_NOT_INFER', "Couldn't infer type parameter '{0}'.{1}");
1331+
13241332
/**
13251333
* 10.10 Superinterfaces: It is a compile-time error if a class `C` has two
13261334
* superinterfaces that are different instantiations of the same generic
@@ -1330,7 +1338,7 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
13301338
* Parameters:
13311339
* 0: the name of the class implementing the conflicting interface
13321340
* 1: the first conflicting type
1333-
* 1: the second conflicting type
1341+
* 2: the second conflicting type
13341342
*/
13351343
static const CompileTimeErrorCode CONFLICTING_GENERIC_INTERFACES =
13361344
CompileTimeErrorCode(
@@ -10690,11 +10698,6 @@ class StrongModeCode extends ErrorCode {
1069010698
"Try adding an explicit type like 'dynamic', or "
1069110699
"enable implicit-dynamic in your analysis options file.";
1069210700

10693-
static const StrongModeCode COULD_NOT_INFER = StrongModeCode(
10694-
ErrorType.COMPILE_TIME_ERROR,
10695-
'COULD_NOT_INFER',
10696-
"Couldn't infer type parameter '{0}'.{1}");
10697-
1069810701
static const StrongModeCode IMPLICIT_DYNAMIC_FIELD = StrongModeCode(
1069910702
ErrorType.COMPILE_TIME_ERROR,
1070010703
'IMPLICIT_DYNAMIC_FIELD',

pkg/analyzer/test/generated/compile_time_error_code.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ var b = const bool.fromEnvironment('x', defaultValue: 1);
335335
T f<T>(T t) => null;
336336
main() { f(<S>(S s) => s); }
337337
''', [
338-
error(StrongModeCode.COULD_NOT_INFER, 30, 1),
338+
error(CompileTimeErrorCode.COULD_NOT_INFER, 30, 1),
339339
]);
340340
}
341341

@@ -344,7 +344,7 @@ main() { f(<S>(S s) => s); }
344344
T Function<T>(T) f;
345345
main() { f(<S>(S s) => s); }
346346
''', [
347-
error(StrongModeCode.COULD_NOT_INFER, 29, 1),
347+
error(CompileTimeErrorCode.COULD_NOT_INFER, 29, 1),
348348
]);
349349
}
350350

@@ -355,7 +355,7 @@ class C {
355355
}
356356
main() { new C().f(<S>(S s) => s); }
357357
''', [
358-
error(StrongModeCode.COULD_NOT_INFER, 52, 1),
358+
error(CompileTimeErrorCode.COULD_NOT_INFER, 52, 1),
359359
]);
360360
}
361361

pkg/analyzer/test/generated/strong_mode_test.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ class StrongModeLocalInferenceTest extends ResolverTestCase {
335335

336336
test_constrainedByBounds5() async {
337337
// Test that upwards inference with two type variables does not
338-
// propogate from the constrained variable to the unconstrained
338+
// propagate from the constrained variable to the unconstrained
339339
// variable if they are ordered right to left, when the variable
340-
// appears co and contra variantly, and that an error is issued
340+
// appears co- and contra-variantly, and that an error is issued
341341
// for the non-matching bound.
342342
String code = r'''
343343
typedef To Func1<From, To>(From x);
@@ -346,7 +346,7 @@ class StrongModeLocalInferenceTest extends ResolverTestCase {
346346
''';
347347
Source source = addSource(code);
348348
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
349-
assertErrors(source, [StrongModeCode.COULD_NOT_INFER]);
349+
assertErrors(source, [CompileTimeErrorCode.COULD_NOT_INFER]);
350350
verify([source]);
351351
CompilationUnit unit = analysisResult.unit;
352352
List<Statement> statements =
@@ -981,7 +981,7 @@ class StrongModeLocalInferenceTest extends ResolverTestCase {
981981
MethodInvocation invoke = await _testFutureOr(r'''
982982
Future<T> mk<T extends Future<Object>>(FutureOr<T> x) => null;
983983
dynamic test() => mk(new Future<int>.value(42));
984-
''', errors: [StrongModeCode.COULD_NOT_INFER]);
984+
''', errors: [CompileTimeErrorCode.COULD_NOT_INFER]);
985985
_isFutureOfInt(invoke.staticType);
986986
}
987987

@@ -1093,7 +1093,7 @@ test() {
10931093
''');
10941094
await computeAnalysisResult(source);
10951095
_expectInferenceError(source, [
1096-
StrongModeCode.COULD_NOT_INFER,
1096+
CompileTimeErrorCode.COULD_NOT_INFER,
10971097
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
10981098
], r'''
10991099
Couldn't infer type parameter 'T'.
@@ -1119,7 +1119,7 @@ test() {
11191119
''');
11201120
await computeAnalysisResult(source);
11211121
_expectInferenceError(source, [
1122-
StrongModeCode.COULD_NOT_INFER,
1122+
CompileTimeErrorCode.COULD_NOT_INFER,
11231123
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
11241124
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
11251125
], r'''
@@ -1170,7 +1170,7 @@ test() {
11701170
''');
11711171
await computeAnalysisResult(source);
11721172
_expectInferenceError(source, [
1173-
StrongModeCode.COULD_NOT_INFER,
1173+
CompileTimeErrorCode.COULD_NOT_INFER,
11741174
], r'''
11751175
Couldn't infer type parameter 'T'.
11761176
@@ -1198,7 +1198,7 @@ test(Iterable values) {
11981198
''');
11991199
await computeAnalysisResult(source);
12001200
_expectInferenceError(source, [
1201-
StrongModeCode.COULD_NOT_INFER,
1201+
CompileTimeErrorCode.COULD_NOT_INFER,
12021202
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
12031203
], r'''
12041204
Couldn't infer type parameter 'T'.
@@ -1223,7 +1223,7 @@ test() {
12231223
}
12241224
''');
12251225
await computeAnalysisResult(source);
1226-
_expectInferenceError(source, [StrongModeCode.COULD_NOT_INFER], r'''
1226+
_expectInferenceError(source, [CompileTimeErrorCode.COULD_NOT_INFER], r'''
12271227
Couldn't infer type parameter 'T'.
12281228
12291229
Tried to infer 'num' for 'T' which doesn't work:
@@ -1364,7 +1364,7 @@ num test(Iterable values) => values.fold(values.first as num, max);
13641364
''');
13651365
var analysisResult = await computeAnalysisResult(source);
13661366
assertErrors(source, [
1367-
StrongModeCode.COULD_NOT_INFER,
1367+
CompileTimeErrorCode.COULD_NOT_INFER,
13681368
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
13691369
]);
13701370
verify([source]);
@@ -2375,7 +2375,7 @@ class B<T2, U2> {
23752375
assertErrors(source, errorCodes);
23762376
var errors = analysisResults[source]
23772377
.errors
2378-
.where((e) => e.errorCode == StrongModeCode.COULD_NOT_INFER)
2378+
.where((e) => e.errorCode == CompileTimeErrorCode.COULD_NOT_INFER)
23792379
.map((e) => e.message)
23802380
.toList();
23812381
expect(errors.length, 1);
@@ -3341,7 +3341,7 @@ void test() {
33413341
''', [
33423342
error(HintCode.UNUSED_LOCAL_VARIABLE, 73, 1),
33433343
error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 81, 1),
3344-
error(StrongModeCode.COULD_NOT_INFER, 81, 1),
3344+
error(CompileTimeErrorCode.COULD_NOT_INFER, 81, 1),
33453345
]);
33463346
_assertLocalVarType('c', 'C<List<dynamic>, List<List<dynamic>>>');
33473347
}
@@ -3456,7 +3456,7 @@ void g() {
34563456
''', [
34573457
error(HintCode.MISSING_RETURN, 3, 1),
34583458
error(HintCode.UNUSED_LOCAL_VARIABLE, 69, 1),
3459-
error(StrongModeCode.COULD_NOT_INFER, 73, 1),
3459+
error(CompileTimeErrorCode.COULD_NOT_INFER, 73, 1),
34603460
]);
34613461
_assertLocalVarType('c', 'List<dynamic>');
34623462
}

pkg/analyzer/test/src/dart/element/generic_inferrer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class GenericFunctionInferenceTest extends AbstractTypeSystemNullSafetyTest {
616616

617617
if (expectError) {
618618
expect(listener.errors.map((e) => e.errorCode).toList(),
619-
[StrongModeCode.COULD_NOT_INFER],
619+
[CompileTimeErrorCode.COULD_NOT_INFER],
620620
reason: 'expected exactly 1 could not infer error.');
621621
} else {
622622
expect(listener.errors, isEmpty, reason: 'did not expect any errors.');

pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class A {
127127
@A([min])
128128
main() {}
129129
''', [
130-
error(StrongModeCode.COULD_NOT_INFER, 66, 5),
130+
error(CompileTimeErrorCode.COULD_NOT_INFER, 66, 5),
131131
]);
132132

133133
var identifier = findNode.simple('min]');

pkg/analyzer/test/src/dart/resolution/type_inference/extension_methods_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ f(String s) {
306306
E(s).foo();
307307
}
308308
''', [
309-
error(StrongModeCode.COULD_NOT_INFER, 69, 1),
309+
error(CompileTimeErrorCode.COULD_NOT_INFER, 69, 1),
310310
]);
311311
var override = findNode.extensionOverride('E(s)');
312312
assertElementTypeStrings(override.typeArgumentTypes, ['String']);

0 commit comments

Comments
 (0)