Skip to content

Commit f2effca

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Simplify the GenericInferrer constructor
* DO a TODO on the `typeParameters` type. * An assert can be simplified and moved into the constructor initializers. * `_typeParameters` and `_constraints` can be initialized in the initializers. * `genericMetadata` can be made private. Change-Id: I03d40683064debe5906b9518b768b04b1aaaa090 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/439540 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Auto-Submit: Samuel Rawlins <[email protected]>
1 parent 84f22e8 commit f2effca

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import 'package:analyzer/src/dart/element/element.dart';
1616
import 'package:analyzer/src/dart/element/type.dart';
1717
import 'package:analyzer/src/dart/element/type_algebra.dart';
1818
import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
19-
import 'package:analyzer/src/dart/element/type_provider.dart';
2019
import 'package:analyzer/src/dart/element/type_schema.dart';
2120
import 'package:analyzer/src/dart/element/type_system.dart';
2221
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
@@ -50,9 +49,8 @@ import 'package:collection/collection.dart';
5049
/// infer a single call and discarded immediately afterwards.
5150
class GenericInferrer {
5251
final TypeSystemImpl _typeSystem;
53-
final Set<TypeParameterElementImpl> _typeParameters = Set.identity();
54-
final Map<TypeParameterElementImpl, List<MergedTypeConstraint>> _constraints =
55-
{};
52+
final Set<TypeParameterElementImpl> _typeParameters;
53+
final Map<TypeParameterElementImpl, List<MergedTypeConstraint>> _constraints;
5654

5755
/// The list of type parameters being inferred.
5856
final List<TypeParameterElementImpl> _typeFormals;
@@ -68,7 +66,7 @@ class GenericInferrer {
6866

6967
/// Indicates whether the "generic metadata" feature is enabled. When it is,
7068
/// type arguments are allowed to be instantiated with generic function types.
71-
final bool genericMetadataIsEnabled;
69+
final bool _genericMetadataIsEnabled;
7270

7371
/// Indicates whether the "inference using bounds" feature is enabled. When it
7472
/// is, the bounds of type parameters will be used more extensively when
@@ -107,30 +105,22 @@ class GenericInferrer {
107105

108106
GenericInferrer(
109107
this._typeSystem,
110-
List<TypeParameterElement> typeFormals, {
108+
List<TypeParameterElementImpl> typeFormals, {
111109
DiagnosticReporter? diagnosticReporter,
112110
this.errorEntity,
113-
required this.genericMetadataIsEnabled,
111+
required bool genericMetadataIsEnabled,
114112
required this.inferenceUsingBoundsIsEnabled,
115113
required bool strictInference,
116114
required TypeSystemOperations typeSystemOperations,
117115
required this.dataForTesting,
118-
}) : _diagnosticReporter = diagnosticReporter,
119-
// TODO(paulberry): make this cast unnecessary by changing `typeFormals`
120-
// to `List<TypeParameterElementImpl2>`.
121-
_typeFormals = typeFormals.cast(),
116+
}) : assert(diagnosticReporter == null || errorEntity != null),
117+
_typeParameters = typeFormals.toSet(),
118+
_constraints = {for (var formal in typeFormals) formal: []},
119+
_diagnosticReporter = diagnosticReporter,
120+
_typeFormals = typeFormals,
121+
_genericMetadataIsEnabled = genericMetadataIsEnabled,
122122
_strictInference = strictInference,
123-
_typeSystemOperations = typeSystemOperations {
124-
if (_diagnosticReporter != null) {
125-
assert(errorEntity != null);
126-
}
127-
_typeParameters.addAll(_typeFormals);
128-
for (var formal in _typeFormals) {
129-
_constraints[formal] = [];
130-
}
131-
}
132-
133-
TypeProviderImpl get typeProvider => _typeSystem.typeProvider;
123+
_typeSystemOperations = typeSystemOperations;
134124

135125
/// Performs upwards inference, producing a final set of inferred types that
136126
/// does not contain references to the "unknown type".
@@ -344,7 +334,7 @@ class GenericInferrer {
344334

345335
if (inferred is FunctionTypeImpl &&
346336
inferred.typeParameters.isNotEmpty &&
347-
!genericMetadataIsEnabled &&
337+
!_genericMetadataIsEnabled &&
348338
_diagnosticReporter != null) {
349339
if (failAtError) {
350340
inferenceLogWriter?.exitGenericInference(failed: true);
@@ -381,7 +371,7 @@ class GenericInferrer {
381371
_reportInferenceFailure(
382372
diagnosticReporter: _diagnosticReporter,
383373
errorEntity: errorEntity,
384-
genericMetadataIsEnabled: genericMetadataIsEnabled,
374+
genericMetadataIsEnabled: _genericMetadataIsEnabled,
385375
);
386376
}
387377
}
@@ -412,7 +402,9 @@ class GenericInferrer {
412402
var typeParamBound = Substitution.fromPairs2(
413403
_typeFormals,
414404
inferredTypes,
415-
).substituteType(typeParam.bound ?? typeProvider.objectType);
405+
).substituteType(
406+
typeParam.bound ?? _typeSystem.typeProvider.objectType,
407+
);
416408
// TODO(jmesserly): improve this error message.
417409
_diagnosticReporter?.atEntity(
418410
errorEntity!,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,8 +1715,7 @@ class TypeSystemImpl implements TypeSystem {
17151715
/// list/map literal, initializing a [GenericInferrer] using the downward
17161716
/// context type.
17171717
GenericInferrer setupGenericTypeInference({
1718-
// TODO(paulberry): change this to a list of `TypeParameterElementImpl`.
1719-
required List<TypeParameterElement> typeParameters,
1718+
required List<TypeParameterElementImpl> typeParameters,
17201719
required TypeImpl declaredReturnType,
17211720
required TypeImpl contextReturnType,
17221721
DiagnosticReporter? diagnosticReporter,

0 commit comments

Comments
 (0)