Skip to content

Commit

Permalink
[analyzer] Use AstNodeImpl when interfacing with shared code
Browse files Browse the repository at this point in the history
Change the analyzer's use of the following generic types so that it
supplies the type parameter `AstNodeImpl` instead of `AstNode` as the
type it uses to represent AST nodes:

- `AssignedVariables`
- `AssignedVariablesForTesting`
- `CaseHeadOrDefaultInfo`
- `FlowAnalysis`
- `MatchContext`
- `SwitchExpressionMemberInfo`
- `SwitchStatementMemberInfo`
- `TypeAnalyzer`
- `TypeAnalyzerErrors`

In some places I was able to avoid adding casts by changing method
parameters to accept concrete AST node types instead of abstract
public interface types. This required adding the `covariant` keyword
to one method (`ResolverVisitor.visitTryStatement`).

This is part of a larger arc of work to change the analyzer's use of
the shared code so that the type parameters it supplies are not part
of the analyzer public API. See
#59763.

Change-Id: I98a6df704b64ff3efd3c73d05fb47409828fbf04
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403281
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 6, 2025
1 parent f023483 commit 03fd2aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
20 changes: 10 additions & 10 deletions pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FlowAnalysisHelper {
final TypeSystemOperations typeOperations;

/// Precomputed sets of potentially assigned variables.
AssignedVariables<AstNode, PromotableElementImpl2>? assignedVariables;
AssignedVariables<AstNodeImpl, PromotableElementImpl2>? assignedVariables;

/// The result for post-resolution stages of analysis, for testing only.
final FlowAnalysisDataForTesting? dataForTesting;
Expand All @@ -96,8 +96,8 @@ class FlowAnalysisHelper {
final bool inferenceUpdate4Enabled;

/// The current flow, when resolving a function body, or `null` otherwise.
FlowAnalysis<AstNode, StatementImpl, ExpressionImpl, PromotableElementImpl2,
SharedTypeView<DartType>>? flow;
FlowAnalysis<AstNodeImpl, StatementImpl, ExpressionImpl,
PromotableElementImpl2, SharedTypeView<DartType>>? flow;

FlowAnalysisHelper(bool retainDataForTesting, FeatureSet featureSet,
{required TypeSystemOperations typeSystemOperations})
Expand Down Expand Up @@ -174,10 +174,10 @@ class FlowAnalysisHelper {
retainDataForTesting: dataForTesting != null, visit: visit);
if (dataForTesting != null) {
dataForTesting!.assignedVariables[node] = assignedVariables
as AssignedVariablesForTesting<AstNode, PromotableElementImpl2>;
as AssignedVariablesForTesting<AstNodeImpl, PromotableElementImpl2>;
}
flow = isNonNullableByDefault
? FlowAnalysis<AstNode, StatementImpl, ExpressionImpl,
? FlowAnalysis<AstNodeImpl, StatementImpl, ExpressionImpl,
PromotableElementImpl2, SharedTypeView<DartType>>(
typeOperations,
assignedVariables!,
Expand All @@ -186,7 +186,7 @@ class FlowAnalysisHelper {
fieldPromotionEnabled: fieldPromotionEnabled,
inferenceUpdate4Enabled: inferenceUpdate4Enabled,
)
: FlowAnalysis<AstNode, StatementImpl, ExpressionImpl,
: FlowAnalysis<AstNodeImpl, StatementImpl, ExpressionImpl,
PromotableElementImpl2, SharedTypeView<DartType>>.legacy(
typeOperations, assignedVariables!);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ class FlowAnalysisHelper {
}

void executableDeclaration_enter(
AstNode node, FormalParameterList? parameters,
AstNodeImpl node, FormalParameterList? parameters,
{required bool isClosure}) {
if (isClosure) {
flow!.functionExpression_begin(node);
Expand Down Expand Up @@ -261,7 +261,7 @@ class FlowAnalysisHelper {
flow?.for_bodyBegin(node is StatementImpl ? node : null, condition);
}

void for_conditionBegin(AstNode node) {
void for_conditionBegin(AstNodeImpl node) {
flow?.for_conditionBegin(node);
}

Expand Down Expand Up @@ -348,11 +348,11 @@ class FlowAnalysisHelper {
}

/// Computes the [AssignedVariables] map for the given [node].
static AssignedVariables<AstNode, PromotableElementImpl2>
static AssignedVariables<AstNodeImpl, PromotableElementImpl2>
computeAssignedVariables(AstNode node, FormalParameterList? parameters,
{bool retainDataForTesting = false,
void Function(AstVisitor<Object?> visitor)? visit}) {
AssignedVariables<AstNode, PromotableElementImpl2> assignedVariables =
AssignedVariables<AstNodeImpl, PromotableElementImpl2> assignedVariables =
retainDataForTesting
? AssignedVariablesForTesting()
: AssignedVariables();
Expand Down
6 changes: 3 additions & 3 deletions pkg/analyzer/lib/src/dart/resolver/for_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class ForResolver {
return iteratedType.typeArguments.single;
}

void _forEachParts(AstNode node, bool isAsync, ForEachPartsImpl forEachParts,
void Function() visitBody) {
void _forEachParts(AstNodeImpl node, bool isAsync,
ForEachPartsImpl forEachParts, void Function() visitBody) {
ExpressionImpl iterable = forEachParts.iterable;
DeclaredIdentifierImpl? loopVariable;
SimpleIdentifierImpl? identifier;
Expand Down Expand Up @@ -200,7 +200,7 @@ class ForResolver {
}

void _forParts(
AstNode node, ForPartsImpl forParts, void Function() visitBody) {
AstNodeImpl node, ForPartsImpl forParts, void Function() visitBody) {
if (forParts is ForPartsWithDeclarationsImpl) {
forParts.variables.accept(_resolver);
} else if (forParts is ForPartsWithExpressionImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef SharedPatternField
class SharedTypeAnalyzerErrors
implements
shared.TypeAnalyzerErrors<
AstNode,
AstNodeImpl,
StatementImpl,
ExpressionImpl,
PromotableElementImpl2,
Expand Down
22 changes: 13 additions & 9 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import 'package:analyzer/src/utilities/extensions/object.dart';
/// By default, no files have inference logging enabled.
bool Function(Source) inferenceLoggingPredicate = (_) => false;

typedef SharedMatchContext = shared.MatchContext<AstNode, ExpressionImpl,
typedef SharedMatchContext = shared.MatchContext<AstNodeImpl, ExpressionImpl,
DartPatternImpl, SharedTypeView<DartType>, PromotableElementImpl2>;

typedef SharedPatternField
Expand All @@ -122,7 +122,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
ErrorDetectionHelpers,
TypeAnalyzer<
DartType,
AstNode,
AstNodeImpl,
StatementImpl,
ExpressionImpl,
PromotableElementImpl2,
Expand Down Expand Up @@ -437,7 +437,11 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
ExecutableElement? get enclosingFunction => _enclosingFunction;

@override
FlowAnalysis<AstNode, StatementImpl, ExpressionImpl, PromotableElementImpl2,
FlowAnalysis<
AstNodeImpl,
StatementImpl,
ExpressionImpl,
PromotableElementImpl2,
SharedTypeView<DartType>> get flow => flowAnalysis.flow!;

bool get isConstructorTearoffsEnabled =>
Expand Down Expand Up @@ -810,7 +814,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>

@override
PatternResult<DartType> dispatchPattern(
SharedMatchContext context, AstNode node) {
SharedMatchContext context, AstNodeImpl node) {
shared.PatternResult<DartType> analysisResult;
if (node is DartPatternImpl) {
analysisResult = node.resolvePattern(this, context);
Expand Down Expand Up @@ -977,8 +981,8 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
}

@override
SwitchExpressionMemberInfo<AstNode, ExpressionImpl, PromotableElementImpl2>
getSwitchExpressionMemberInfo(
SwitchExpressionMemberInfo<AstNodeImpl, ExpressionImpl,
PromotableElementImpl2> getSwitchExpressionMemberInfo(
covariant SwitchExpressionImpl node,
int index,
) {
Expand All @@ -995,12 +999,12 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
}

@override
SwitchStatementMemberInfo<AstNode, StatementImpl, ExpressionImpl,
SwitchStatementMemberInfo<AstNodeImpl, StatementImpl, ExpressionImpl,
PromotableElementImpl2> getSwitchStatementMemberInfo(
covariant SwitchStatementImpl node,
int index,
) {
CaseHeadOrDefaultInfo<AstNode, ExpressionImpl, PromotableElementImpl2>
CaseHeadOrDefaultInfo<AstNodeImpl, ExpressionImpl, PromotableElementImpl2>
ofMember(
SwitchMemberImpl member,
) {
Expand Down Expand Up @@ -3792,7 +3796,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
}

@override
void visitTryStatement(TryStatement node) {
void visitTryStatement(covariant TryStatementImpl node) {
inferenceLogWriter?.enterStatement(node);
checkUnreachableNode(node);
var flow = flowAnalysis.flow!;
Expand Down

0 comments on commit 03fd2aa

Please sign in to comment.