Skip to content

Commit 9f89ca1

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Deprecate LocalVariableElement and subclasses.
Change-Id: Iba07661be40d1d05ddcd656413322111db3e15d4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/414822 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 18c0848 commit 9f89ca1

File tree

7 files changed

+52
-63
lines changed

7 files changed

+52
-63
lines changed

pkg/analyzer/api.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,7 +2992,7 @@ package:analyzer/dart/constant/value.dart:
29922992
toSymbolValue (method: String? Function())
29932993
toTypeValue (method: DartType? Function())
29942994
package:analyzer/dart/element/element.dart:
2995-
BindPatternVariableElement (class extends Object implements PatternVariableElement):
2995+
BindPatternVariableElement (class extends Object implements PatternVariableElement, deprecated):
29962996
new (constructor: BindPatternVariableElement Function())
29972997
ClassElement (class extends Object implements InterfaceElement):
29982998
new (constructor: ClassElement Function())
@@ -3342,7 +3342,7 @@ package:analyzer/dart/element/element.dart:
33423342
lookUpInheritedMethod (method: MethodElement? Function(String, LibraryElement))
33433343
lookUpMethod (method: MethodElement? Function(String, LibraryElement), deprecated)
33443344
lookUpSetter (method: PropertyAccessorElement? Function(String, LibraryElement), deprecated)
3345-
JoinPatternVariableElement (class extends Object implements PatternVariableElement):
3345+
JoinPatternVariableElement (class extends Object implements PatternVariableElement, deprecated):
33463346
new (constructor: JoinPatternVariableElement Function())
33473347
isConsistent (getter: bool)
33483348
variables (getter: List<PatternVariableElement>)
@@ -3397,7 +3397,7 @@ package:analyzer/dart/element/element.dart:
33973397
package (getter: Version)
33983398
LocalElement (class extends Object implements Element):
33993399
new (constructor: LocalElement Function())
3400-
LocalVariableElement (class extends Object implements PromotableElement):
3400+
LocalVariableElement (class extends Object implements PromotableElement, deprecated):
34013401
new (constructor: LocalVariableElement Function())
34023402
hasInitializer (getter: bool)
34033403
name (getter: String)
@@ -3438,7 +3438,7 @@ package:analyzer/dart/element/element.dart:
34383438
PartElement (class extends Object implements _ExistingElement):
34393439
new (constructor: PartElement Function())
34403440
uri (getter: DirectiveUri)
3441-
PatternVariableElement (class extends Object implements LocalVariableElement):
3441+
PatternVariableElement (class extends Object implements LocalVariableElement, deprecated):
34423442
new (constructor: PatternVariableElement Function())
34433443
join (getter: JoinPatternVariableElement?)
34443444
PrefixElement (class extends Object implements _ExistingElement):

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import 'package:pub_semver/pub_semver.dart';
6262
/// A pattern variable that is explicitly declared.
6363
///
6464
/// Clients may not extend, implement or mix-in this class.
65+
@Deprecated(
66+
'Use BindPatternVariableFragment and BindPatternVariableElement2 instead')
6567
abstract class BindPatternVariableElement implements PatternVariableElement {}
6668

6769
/// An element that represents a class or a mixin. The class can be defined by
@@ -1580,6 +1582,8 @@ abstract class InterfaceElement implements InstanceElement {
15801582
/// for a logical-or patterns, or shared `case` bodies in `switch` statements.
15811583
///
15821584
/// Clients may not extend, implement or mix-in this class.
1585+
@Deprecated(
1586+
'Use JoinPatternVariableFragment and JoinPatternVariableElement2 instead')
15831587
abstract class JoinPatternVariableElement implements PatternVariableElement {
15841588
/// Whether the [variables] are consistent, present in all branches,
15851589
/// and have the same type and finality.
@@ -1770,6 +1774,7 @@ abstract class LocalElement implements Element {}
17701774
/// A local variable.
17711775
///
17721776
/// Clients may not extend, implement or mix-in this class.
1777+
@Deprecated('Use LocalVariableFragment and LocalVariableElement2 instead')
17731778
abstract class LocalVariableElement implements PromotableElement {
17741779
/// Whether the variable has an initializer at declaration.
17751780
bool get hasInitializer;
@@ -1946,6 +1951,7 @@ abstract class PartElement implements _ExistingElement {
19461951
/// A pattern variable.
19471952
///
19481953
/// Clients may not extend, implement or mix-in this class.
1954+
@Deprecated('Use PatternVariableFragment and PatternVariableElement2 instead')
19491955
abstract class PatternVariableElement implements LocalVariableElement {
19501956
/// The variable in which this variable joins with other pattern variables
19511957
/// with the same name, in a logical-or pattern, or shared case scope.

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ mixin AugmentableFragment on ElementImpl {
7878
}
7979

8080
class BindPatternVariableElementImpl extends PatternVariableElementImpl
81-
implements BindPatternVariableElement, BindPatternVariableFragment {
81+
implements
82+
// ignore: deprecated_member_use_from_same_package
83+
BindPatternVariableElement,
84+
BindPatternVariableFragment {
8285
final DeclaredVariablePatternImpl node;
8386

8487
/// This flag is set to `true` if this variable clashes with another
@@ -1357,7 +1360,7 @@ class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement {
13571360
}
13581361
}
13591362

1360-
/// A [LocalVariableElement] for a local 'const' variable that has an
1363+
/// A `LocalVariableElement` for a local 'const' variable that has an
13611364
/// initializer.
13621365
class ConstLocalVariableElementImpl extends LocalVariableElementImpl
13631366
with ConstVariableElement {
@@ -6738,7 +6741,10 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
67386741
}
67396742

67406743
class JoinPatternVariableElementImpl extends PatternVariableElementImpl
6741-
implements JoinPatternVariableElement, JoinPatternVariableFragment {
6744+
implements
6745+
// ignore: deprecated_member_use_from_same_package
6746+
JoinPatternVariableElement,
6747+
JoinPatternVariableFragment {
67426748
@override
67436749
final List<PatternVariableElementImpl> variables;
67446750

@@ -7611,7 +7617,7 @@ class LibraryImportElementImpl extends _ExistingElementImpl
76117617
LibraryElementImpl get library2 => super.library2 as LibraryElementImpl;
76127618

76137619
@override
7614-
LibraryFragment get libraryFragment => enclosingElement3;
7620+
CompilationUnitElementImpl get libraryFragment => enclosingElement3;
76157621

76167622
@override
76177623
Namespace get namespace {
@@ -7786,9 +7792,9 @@ class LocalFunctionFragmentImpl extends FunctionElementImpl
77867792
}
77877793
}
77887794

7789-
/// A concrete implementation of a [LocalVariableElement].
77907795
class LocalVariableElementImpl extends NonParameterVariableElementImpl
77917796
implements
7797+
// ignore: deprecated_member_use_from_same_package
77927798
LocalVariableElement,
77937799
LocalVariableFragment,
77947800
VariableElementOrMember {
@@ -9677,7 +9683,10 @@ class PartElementImpl extends _ExistingElementImpl
96779683
}
96789684

96799685
class PatternVariableElementImpl extends LocalVariableElementImpl
9680-
implements PatternVariableElement, PatternVariableFragment {
9686+
implements
9687+
// ignore: deprecated_member_use_from_same_package
9688+
PatternVariableElement,
9689+
PatternVariableFragment {
96819690
@override
96829691
JoinPatternVariableElementImpl? join;
96839692

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ extension Element2Extension on Element2 {
8484
}
8585
}
8686

87+
extension Element2OrNullExtension on Element2? {
88+
/// Return true if this element is a wildcard variable.
89+
bool get isWildcardVariable {
90+
return this?.isWildcardVariable ?? false;
91+
}
92+
}
93+
8794
extension ElementAnnotationExtensions on ElementAnnotation {
8895
static final Map<String, TargetKind> _targetKindsByName = {
8996
for (var kind in TargetKind.values) kind.name: kind,
@@ -175,32 +182,6 @@ extension ElementExtension on Element {
175182
}
176183
return false;
177184
}
178-
179-
/// Return true if this element is a wildcard variable.
180-
bool get isWildcardVariable {
181-
return name == '_' &&
182-
(this is LocalVariableElement ||
183-
this is PrefixElement ||
184-
this is TypeParameterElement ||
185-
(this is ParameterElement &&
186-
this is! FieldFormalParameterElement &&
187-
this is! SuperFormalParameterElement)) &&
188-
library.asElement2.hasWildcardVariablesFeatureEnabled;
189-
}
190-
}
191-
192-
extension ElementImplExtension on ElementImpl {
193-
/// Return true if this element is a wildcard variable.
194-
bool get isWildcardVariable {
195-
return name == '_' &&
196-
(this is LocalVariableElement ||
197-
this is PrefixElement ||
198-
this is TypeParameterElement ||
199-
(this is ParameterElement &&
200-
this is! FieldFormalParameterElement &&
201-
this is! SuperFormalParameterElement)) &&
202-
library.asElement2.hasWildcardVariablesFeatureEnabled;
203-
}
204185
}
205186

206187
extension ExecutableElement2OrMemberQuestionExtension

pkg/analyzer/lib/src/dart/micro/resolve_file.dart

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'dart:typed_data';
99
import 'package:analyzer/dart/analysis/declared_variables.dart';
1010
import 'package:analyzer/dart/analysis/features.dart';
1111
import 'package:analyzer/dart/analysis/results.dart';
12-
import 'package:analyzer/dart/element/element.dart';
1312
import 'package:analyzer/dart/element/element2.dart';
1413
import 'package:analyzer/error/error.dart';
1514
import 'package:analyzer/file_system/file_system.dart';
@@ -259,24 +258,14 @@ class FileResolver {
259258
/// cached by the resolver are searched, generated files are ignored.
260259
Future<List<CiderSearchMatch>> findReferences(Element2 element,
261260
{OperationPerformanceImpl? performance}) {
262-
return findReferences2(
263-
element.asElement!,
264-
performance: performance,
265-
);
266-
}
267-
268-
/// Looks for references to the given Element. All the files currently
269-
/// cached by the resolver are searched, generated files are ignored.
270-
Future<List<CiderSearchMatch>> findReferences2(Element element,
271-
{OperationPerformanceImpl? performance}) {
272-
return logger.runAsync('findReferences for ${element.name}', () async {
261+
return logger.runAsync('findReferences for ${element.name3}', () async {
273262
var references = <CiderSearchMatch>[];
274263

275264
Future<void> collectReferences2(
276265
String path, OperationPerformanceImpl performance) async {
277266
await performance.runAsync('collectReferences', (_) async {
278267
var resolved = await resolve(path: path);
279-
var collector = ReferencesCollector(element);
268+
var collector = ReferencesCollector(element.asElement!);
280269
resolved.unit.accept(collector);
281270
var matches = collector.references;
282271
if (matches.isNotEmpty) {
@@ -295,10 +284,13 @@ class FileResolver {
295284

296285
performance ??= OperationPerformanceImpl('<default>');
297286
// TODO(keertip): check if element is named constructor.
298-
if (element is LocalVariableElement ||
299-
(element is ParameterElement && !element.isNamed)) {
300-
await collectReferences2(element.source!.fullName, performance!);
301-
} else if (element is LibraryImportElementImpl) {
287+
if (element is LocalVariableElement2 ||
288+
(element is FormalParameterElement && !element.isNamed)) {
289+
await collectReferences2(
290+
element.firstFragment.libraryFragment!.source.fullName,
291+
performance!,
292+
);
293+
} else if (element is MockLibraryImportElement) {
302294
return await _searchReferences_Import(element);
303295
} else {
304296
var result = performance!.run('getFilesContaining', (performance) {
@@ -892,13 +884,14 @@ class FileResolver {
892884
}
893885

894886
Future<List<CiderSearchMatch>> _searchReferences_Import(
895-
LibraryImportElementImpl element) async {
887+
MockLibraryImportElement element) async {
896888
var results = <CiderSearchMatch>[];
897-
var libraryElement = element.library;
898-
for (var unitElement in libraryElement.units) {
899-
String unitPath = unitElement.source.fullName;
889+
var libraryElement = element.library2;
890+
for (var libraryFragment in libraryElement.fragments) {
891+
String unitPath = libraryFragment.source.fullName;
900892
var unitResult = await resolve(path: unitPath);
901-
var visitor = ImportElementReferencesVisitor(element, unitElement);
893+
var visitor =
894+
ImportElementReferencesVisitor(element.import, libraryFragment);
902895
unitResult.unit.accept(visitor);
903896
var lineInfo = unitResult.lineInfo;
904897
var infos = visitor.results

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class DuplicateDefinitionVerifier {
214214
void _checkDuplicateIdentifier(
215215
Map<String, Element> getterScope, Token identifier,
216216
{required ElementImpl element, Map<String, Element>? setterScope}) {
217-
if (identifier.isSynthetic || element.isWildcardVariable) {
217+
if (identifier.isSynthetic || element.asElement2.isWildcardVariable) {
218218
return;
219219
}
220220

@@ -506,7 +506,7 @@ class MemberDuplicateDefinitionVerifier {
506506
void _checkDuplicateIdentifier(
507507
Map<String, Element> getterScope, Token identifier,
508508
{required Element element, Map<String, Element>? setterScope}) {
509-
if (identifier.isSynthetic || element.isWildcardVariable) {
509+
if (identifier.isSynthetic || element.asElement2.isWildcardVariable) {
510510
return;
511511
}
512512

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ class MockLibraryImportElement implements Element2, PrefixFragment {
2020
: import = import as LibraryImportElementImpl;
2121

2222
@override
23-
Element2? get enclosingElement2 => library2;
23+
LibraryElement2 get enclosingElement2 => library2;
2424

2525
@override
2626
ElementKind get kind => ElementKind.IMPORT;
2727

2828
@override
29-
LibraryElement2? get library2 => libraryFragment.element;
29+
LibraryElementImpl get library2 => libraryFragment.element;
3030

3131
@override
32-
LibraryFragment get libraryFragment => import.libraryFragment;
32+
CompilationUnitElementImpl get libraryFragment => import.libraryFragment;
3333

3434
@override
3535
String? get name3 => import.prefix2?.name2;

0 commit comments

Comments
 (0)