Skip to content

Commit fcb5e06

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] use PromotableElement2 to interface to shared analysis code.
Change the analyzer's use of the following shared classes and mixins to supply `PromotableElement2` instead of `PromotableElement` as the type parameter that represents promotable variables: - `AssignedVariables` - `AssignedVariablesForTesting` - `CaseHeadOrDefaultInfo` - `DemoteViaExplicitWrite` - `FlowAnalysis` - `GeneratedTypeConstraint` - `MatchContext` - `MergedTypeConstraint` - `NonPromotionReasonVisitor` - `SwitchExpressionMemberInfo` - `SwitchStatementMemberInfo` - `TypeAnalyzer` - `TypeAnalyzerErrors` - `TypeAnalyzerOperations` - `TypeAnalyzerOperationsMixin` - `TypeConstraintFromArgument` - `TypeConstraintFromExtendsClause` - `TypeConstraintFromFunctionContext` - `TypeConstraintFromReturnType` - `TypeConstraintGenerator` - `TypeConstraintGeneratorMixin` - `TypeConstraintOrigin` - `UnknownTypeConstraintOrigin` - `VariableBinder` - `VariableBinderErrors` This ensures that all references to variables within the shared code are using the new analyzer element model. As a result of this type change, a lot of analyzer code that interfaces with shared logic needs to change to use the new element model. Also, a few additional members need to be added to `BindPatternVariableElementImpl2`, `JoinPatternVariableElementImpl2`, `LocalVariableElementImpl2`, and `PatternVariableElementImpl2` to allow members of the underlying `_wrappedElement` to be accessed. Change-Id: Ie925eeb82523c769c4d869a37551718f75d334ca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400660 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f1394aa commit fcb5e06

23 files changed

+264
-152
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4429,7 +4429,10 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
44294429

44304430
/// For debugging only: the set of [Variable]s that have been passed to
44314431
/// [declare] so far. This is used to detect unnecessary calls to [declare].
4432-
final Set<Variable> _debugDeclaredVariables = {};
4432+
final Set<Variable> _debugDeclaredVariables =
4433+
// TODO(paulberry): consider changing back to `{}` once
4434+
// https://github.com/dart-lang/sdk/issues/59753 is fixed.
4435+
new Set.identity();
44334436

44344437
@override
44354438
late final SsaNode<Type> _superSsaNode = new SsaNode<Type>(null);

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ lib/src/dart/resolver/property_element_resolver.dart
8585
lib/src/dart/resolver/resolution_result.dart
8686
lib/src/dart/resolver/resolution_visitor.dart
8787
lib/src/dart/resolver/scope.dart
88-
lib/src/dart/resolver/shared_type_analyzer.dart
8988
lib/src/dart/resolver/simple_identifier_resolver.dart
9089
lib/src/dart/resolver/this_lookup.dart
9190
lib/src/dart/resolver/type_property_resolver.dart

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,8 @@ final class AssignedVariablePatternImpl extends VariablePatternImpl
820820

821821
@override
822822
DartType computePatternSchema(ResolverVisitor resolverVisitor) {
823-
var element = this.element;
824-
if (element is PromotableElement) {
823+
var element = element2;
824+
if (element is PromotableElement2) {
825825
return resolverVisitor
826826
.analyzeAssignedVariablePatternSchema(element)
827827
.unwrapTypeSchemaView();
@@ -5029,7 +5029,7 @@ final class DeclaredVariablePatternImpl extends VariablePatternImpl
50295029
var result = resolverVisitor.analyzeDeclaredVariablePattern(
50305030
context,
50315031
this,
5032-
declaredElement!,
5032+
declaredElement2!,
50335033
declaredElement!.name,
50345034
type?.typeOrThrow.wrapSharedTypeView());
50355035
declaredElement!.type = result.staticType.unwrapTypeView();
@@ -9210,7 +9210,7 @@ final class GuardedPatternImpl extends AstNodeImpl implements GuardedPattern {
92109210

92119211
/// Variables declared in [pattern], available in [whenClause] guard, and
92129212
/// to the `ifTrue` node.
9213-
late Map<String, PatternVariableElementImpl> variables;
9213+
late Map<String, PatternVariableElementImpl2> variables;
92149214

92159215
@override
92169216
final WhenClauseImpl? whenClause;
@@ -17274,7 +17274,7 @@ class SwitchStatementCaseGroup {
1727417274
final bool hasLabels;
1727517275

1727617276
/// Joined variables declared in [members], available in [statements].
17277-
late Map<String, PromotableElement> variables;
17277+
late Map<String, PromotableElement2> variables;
1727817278

1727917279
SwitchStatementCaseGroup(this.members, this.hasLabels);
1728017280

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class BindPatternVariableElementImpl2 extends PatternVariableElementImpl2
140140
BindPatternVariableFragment get firstFragment =>
141141
super.firstFragment as BindPatternVariableFragment;
142142

143+
/// This flag is set to `true` if this variable clashes with another
144+
/// pattern variable with the same name within the same pattern.
145+
set isDuplicate(bool value) => _wrappedElement.isDuplicate = value;
146+
147+
DeclaredVariablePatternImpl get node => _wrappedElement.node;
148+
143149
@override
144150
BindPatternVariableElementImpl get _wrappedElement =>
145151
super._wrappedElement as BindPatternVariableElementImpl;
@@ -6596,9 +6602,23 @@ class JoinPatternVariableElementImpl2 extends PatternVariableElementImpl2
65966602
JoinPatternVariableFragment get firstFragment =>
65976603
super.firstFragment as JoinPatternVariableFragment;
65986604

6605+
shared.JoinedPatternVariableInconsistency get inconsistency =>
6606+
_wrappedElement.inconsistency;
6607+
6608+
set inconsistency(shared.JoinedPatternVariableInconsistency value) =>
6609+
_wrappedElement.inconsistency = value;
6610+
65996611
@override
66006612
bool get isConsistent => _wrappedElement.isConsistent;
66016613

6614+
set isFinal(bool value) => _wrappedElement.isFinal = value;
6615+
6616+
/// The identifiers that reference this element.
6617+
List<SimpleIdentifier> get references => _wrappedElement.references;
6618+
6619+
/// The variables that join into this variable.
6620+
List<PatternVariableElementImpl> get variables => _wrappedElement.variables;
6621+
66026622
@override
66036623
List<PatternVariableElement2> get variables2 =>
66046624
_wrappedElement.variables.map((fragment) => fragment.element).toList();
@@ -7598,6 +7618,8 @@ class LocalVariableElementImpl2 extends PromotableElementImpl2
75987618
@override
75997619
DartType get type => _wrappedElement.type;
76007620

7621+
set type(DartType type) => _wrappedElement.type = type;
7622+
76017623
LocalVariableElementImpl get wrappedElement {
76027624
return _wrappedElement;
76037625
}
@@ -8964,7 +8986,11 @@ abstract class NonParameterVariableElementImpl extends VariableElementImpl
89648986
NonParameterVariableElementImpl(String super.name, super.offset);
89658987

89668988
@override
8967-
Element get enclosingElement3 => super.enclosingElement3!;
8989+
Element get enclosingElement3 =>
8990+
// TODO(paulberry): `!` is not appropriate here because variable elements
8991+
// aren't guaranteed to have enclosing elements. See
8992+
// https://github.com/dart-lang/sdk/issues/59750.
8993+
super.enclosingElement3!;
89688994

89698995
bool get hasInitializer {
89708996
return hasModifier(Modifier.HAS_INITIALIZER);
@@ -9345,6 +9371,11 @@ class PatternVariableElementImpl2 extends LocalVariableElementImpl2
93459371
PatternVariableFragment get firstFragment =>
93469372
super.firstFragment as PatternVariableFragment;
93479373

9374+
/// This flag is set to `true` while we are visiting the [WhenClause] of
9375+
/// the [GuardedPattern] that declares this variable.
9376+
set isVisitingWhenClause(bool value) =>
9377+
_wrappedElement.isVisitingWhenClause = value;
9378+
93489379
@override
93499380
JoinPatternVariableElement2? get join2 =>
93509381
JoinPatternVariableElementImpl2(_wrappedElement.join!);

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'package:_fe_analyzer_shared/src/type_inference/type_constraint.dart'
2121
UnknownTypeConstraintOrigin;
2222
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
2323
import 'package:analyzer/dart/element/element.dart';
24+
import 'package:analyzer/dart/element/element2.dart';
2425
import 'package:analyzer/dart/element/type.dart';
2526
import 'package:analyzer/src/dart/ast/ast.dart';
2627
import 'package:analyzer/src/dart/element/element.dart';
@@ -29,20 +30,20 @@ import 'package:analyzer/src/dart/element/type_schema.dart';
2930
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
3031

3132
/// Instance of [shared.GeneratedTypeConstraint] specific to the Analyzer.
32-
typedef GeneratedTypeConstraint = shared
33-
.GeneratedTypeConstraint<DartType, TypeParameterElement, PromotableElement>;
33+
typedef GeneratedTypeConstraint = shared.GeneratedTypeConstraint<DartType,
34+
TypeParameterElement, PromotableElement2>;
3435

3536
/// Instance of [shared.MergedTypeConstraint] specific to the Analyzer.
3637
typedef MergedTypeConstraint = shared.MergedTypeConstraint<DartType,
37-
TypeParameterElement, PromotableElement, InterfaceType, InterfaceElement>;
38+
TypeParameterElement, PromotableElement2, InterfaceType, InterfaceElement>;
3839

3940
/// Instance of [shared.TypeConstraintFromArgument] specific to the Analyzer.
4041
typedef TypeConstraintFromArgument = shared.TypeConstraintFromArgument<DartType,
41-
PromotableElement, TypeParameterElement, InterfaceType, InterfaceElement>;
42+
PromotableElement2, TypeParameterElement, InterfaceType, InterfaceElement>;
4243

4344
/// Instance of [shared.TypeConstraintFromExtendsClause] specific to the Analyzer.
4445
typedef TypeConstraintFromExtendsClause
45-
= shared.TypeConstraintFromExtendsClause<DartType, PromotableElement,
46+
= shared.TypeConstraintFromExtendsClause<DartType, PromotableElement2,
4647
TypeParameterElement, InterfaceType, InterfaceElement>;
4748

4849
/// Instance of [shared.TypeConstraintFromFunctionContext] specific to the Analyzer.
@@ -51,7 +52,7 @@ typedef TypeConstraintFromFunctionContext
5152
DartType,
5253
DartType,
5354
DartType,
54-
PromotableElement,
55+
PromotableElement2,
5556
TypeParameterElement,
5657
InterfaceType,
5758
InterfaceElement>;
@@ -61,19 +62,19 @@ typedef TypeConstraintFromReturnType = shared.TypeConstraintFromReturnType<
6162
DartType,
6263
DartType,
6364
DartType,
64-
PromotableElement,
65+
PromotableElement2,
6566
TypeParameterElement,
6667
InterfaceType,
6768
InterfaceElement>;
6869

6970
/// Instance of [shared.TypeConstraintOrigin] specific to the Analyzer.
7071
typedef TypeConstraintOrigin = shared.TypeConstraintOrigin<DartType,
71-
PromotableElement, TypeParameterElement, InterfaceType, InterfaceElement>;
72+
PromotableElement2, TypeParameterElement, InterfaceType, InterfaceElement>;
7273

7374
/// Instance of [shared.UnknownTypeConstraintOrigin] specific to the Analyzer.
7475
typedef UnknownTypeConstraintOrigin = shared.UnknownTypeConstraintOrigin<
7576
DartType,
76-
PromotableElement,
77+
PromotableElement2,
7778
TypeParameterElement,
7879
InterfaceType,
7980
InterfaceElement>;
@@ -83,7 +84,7 @@ typedef UnknownTypeConstraintOrigin = shared.UnknownTypeConstraintOrigin<
8384
class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
8485
DartType,
8586
ParameterElement,
86-
PromotableElement,
87+
PromotableElement2,
8788
TypeParameterElement,
8889
InterfaceType,
8990
InterfaceElement,
@@ -92,7 +93,7 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
9293
shared.TypeConstraintGeneratorMixin<
9394
DartType,
9495
ParameterElement,
95-
PromotableElement,
96+
PromotableElement2,
9697
TypeParameterElement,
9798
InterfaceType,
9899
InterfaceElement,

pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
77
import 'package:analyzer/dart/analysis/features.dart';
88
import 'package:analyzer/dart/ast/token.dart';
99
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1011
import 'package:analyzer/dart/element/type.dart';
1112
import 'package:analyzer/dart/element/type_provider.dart';
1213
import 'package:analyzer/error/listener.dart';
@@ -54,6 +55,7 @@ class AssignmentExpressionResolver {
5455

5556
var readElement = leftResolution.readElement;
5657
var writeElement = leftResolution.writeElement;
58+
var writeElement2 = leftResolution.writeElement2;
5759

5860
if (hasRead) {
5961
_resolver.setReadElement(
@@ -101,8 +103,8 @@ class AssignmentExpressionResolver {
101103
whyNotPromoted: whyNotPromoted, contextType: contextType);
102104

103105
if (flow != null) {
104-
if (writeElement is PromotableElement) {
105-
flow.write(node, writeElement, SharedTypeView(node.typeOrThrow),
106+
if (writeElement2 is PromotableElement2) {
107+
flow.write(node, writeElement2, SharedTypeView(node.typeOrThrow),
106108
hasRead ? null : right);
107109
}
108110
if (isIfNull) {
@@ -359,8 +361,8 @@ class AssignmentExpressionShared {
359361
if (flow == null) return;
360362

361363
if (left is SimpleIdentifier) {
362-
var element = left.staticElement;
363-
if (element is PromotableElement) {
364+
var element = left.element;
365+
if (element is PromotableElement2) {
364366
var assigned = flowAnalysis.isDefinitelyAssigned(left, element);
365367
var unassigned = flowAnalysis.isDefinitelyUnassigned(left, element);
366368

@@ -377,7 +379,7 @@ class AssignmentExpressionShared {
377379
_errorReporter.atNode(
378380
left,
379381
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_LOCAL,
380-
arguments: [element.name],
382+
arguments: [element.name3!],
381383
);
382384
}
383385
}

pkg/analyzer/lib/src/dart/resolver/binary_expression_resolver.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/analysis/features.dart';
88
import 'package:analyzer/dart/ast/syntactic_entity.dart';
99
import 'package:analyzer/dart/ast/token.dart';
1010
import 'package:analyzer/dart/element/element.dart';
11+
import 'package:analyzer/dart/element/element2.dart';
1112
import 'package:analyzer/dart/element/type.dart';
1213
import 'package:analyzer/dart/element/type_provider.dart';
1314
import 'package:analyzer/error/listener.dart';
@@ -139,14 +140,14 @@ class BinaryExpressionResolver {
139140
}
140141

141142
if (left is SimpleIdentifierImpl && right is NullLiteralImpl) {
142-
var element = left.staticElement;
143-
if (element is PromotableElement &&
143+
var element = left.element;
144+
if (element is PromotableElement2 &&
144145
flowAnalysis.isDefinitelyUnassigned(left, element)) {
145146
reportNullComparison(left, node.operator);
146147
}
147148
} else if (right is SimpleIdentifierImpl && left is NullLiteralImpl) {
148-
var element = right.staticElement;
149-
if (element is PromotableElement &&
149+
var element = right.element;
150+
if (element is PromotableElement2 &&
150151
flowAnalysis.isDefinitelyUnassigned(right, element)) {
151152
reportNullComparison(node.operator, right);
152153
}

0 commit comments

Comments
 (0)