Skip to content

Commit 4475a23

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate AnnotationVerifier.
Change-Id: I73d58d75a4f1e54dc681f32db0ec5393035b5e86 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395341 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent c5a53ed commit 4475a23

File tree

9 files changed

+79
-44
lines changed

9 files changed

+79
-44
lines changed

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ lib/src/dart/resolver/typed_literal_resolver.dart
9898
lib/src/dart/resolver/variable_declaration_resolver.dart
9999
lib/src/dart/resolver/yield_statement_resolver.dart
100100
lib/src/diagnostic/diagnostic_factory.dart
101-
lib/src/error/annotation_verifier.dart
102101
lib/src/error/assignment_verifier.dart
103102
lib/src/error/base_or_final_type_verifier.dart
104103
lib/src/error/best_practices_verifier.dart

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4230,7 +4230,7 @@ class FieldElementImpl2 extends PropertyInducingElementImpl2
42304230
ElementKind get kind => ElementKind.FIELD;
42314231

42324232
@override
4233-
String get name3 => firstFragment.name;
4233+
String? get name3 => firstFragment.name2;
42344234

42354235
@override
42364236
SetterElement? get setter2 => firstFragment.setter?.element as SetterElement?;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ extension DartTypeExtension on DartType {
1818
}
1919

2020
extension Element2Extension on Element2 {
21+
/// Return `true` if this element is an instance member of a class or mixin.
22+
///
23+
/// Only [MethodElement2]s, [GetterElement]s, and [SetterElement]s are
24+
/// supported.
25+
///
26+
/// We intentionally exclude [ConstructorElement2]s - they can only be
27+
/// invoked in instance creation expressions, and [FieldElement2]s - they
28+
/// cannot be invoked directly and are always accessed using corresponding
29+
/// [GetterElement]s or [SetterElement]s.
30+
bool get isInstanceMember {
31+
assert(this is! PropertyInducingElement2,
32+
'Check the GetterElement or SetterElement instead');
33+
var this_ = this;
34+
var enclosing = this_.enclosingElement2;
35+
if (enclosing is InterfaceElement2) {
36+
return this_ is MethodElement2 && !this_.isStatic ||
37+
this_ is GetterElement && !this_.isStatic ||
38+
this_ is SetterElement && !this_.isStatic;
39+
}
40+
return false;
41+
}
42+
2143
/// Whether this element is a wildcard variable.
2244
bool get isWildcardVariable {
2345
return name3 == '_' &&

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

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/dart/element/element2.dart';
66
import 'package:analyzer/dart/element/type.dart';
77
import 'package:analyzer/error/listener.dart';
88
import 'package:analyzer/src/dart/ast/ast.dart';
99
import 'package:analyzer/src/dart/element/extensions.dart';
1010
import 'package:analyzer/src/error/codes.dart';
11+
import 'package:analyzer/src/utilities/extensions/ast.dart';
12+
import 'package:analyzer/src/utilities/extensions/object.dart';
1113
import 'package:analyzer/src/utilities/extensions/string.dart';
1214
import 'package:analyzer/src/workspace/workspace.dart';
1315
import 'package:meta/meta_meta.dart';
@@ -17,14 +19,15 @@ class AnnotationVerifier {
1719
final ErrorReporter _errorReporter;
1820

1921
/// The current library.
20-
final LibraryElement _currentLibrary;
22+
final LibraryElement2 _currentLibrary;
2123

2224
/// The [WorkspacePackage] in which [_currentLibrary] is declared.
2325
final WorkspacePackage? _workspacePackage;
2426

2527
/// Whether [_currentLibrary] is part of its containing package's public API.
2628
late final bool _inPackagePublicApi = _workspacePackage != null &&
27-
_workspacePackage.sourceIsInPublicApi(_currentLibrary.source);
29+
_workspacePackage
30+
.sourceIsInPublicApi(_currentLibrary.firstFragment.source);
2831

2932
AnnotationVerifier(
3033
this._errorReporter,
@@ -115,12 +118,13 @@ class AnnotationVerifier {
115118
/// `@internal` annotation.
116119
void _checkInternal(Annotation node) {
117120
var parent = node.parent;
118-
var parentElement = parent is Declaration ? parent.declaredElement : null;
121+
var parentElement =
122+
parent.ifTypeOrNull<Declaration>()?.declaredFragment?.element;
119123
var parentElementIsPrivate = parentElement?.isPrivate ?? false;
120124
if (parent is TopLevelVariableDeclaration) {
121125
for (var variable in parent.variables.variables) {
122-
var element = variable.declaredElement as TopLevelVariableElement;
123-
if (Identifier.isPrivateName(element.name)) {
126+
var element = variable.declaredTopLevelVariableElement;
127+
if (element.isPrivate) {
124128
_errorReporter.atNode(
125129
variable,
126130
WarningCode.INVALID_INTERNAL_ANNOTATION,
@@ -129,16 +133,17 @@ class AnnotationVerifier {
129133
}
130134
} else if (parent is FieldDeclaration) {
131135
for (var variable in parent.fields.variables) {
132-
var element = variable.declaredElement as FieldElement;
133-
if (Identifier.isPrivateName(element.name)) {
136+
var element = variable.declaredFieldElement;
137+
if (element.isPrivate) {
134138
_errorReporter.atNode(
135139
variable,
136140
WarningCode.INVALID_INTERNAL_ANNOTATION,
137141
);
138142
}
139143
}
140144
} else if (parent is ConstructorDeclaration) {
141-
var class_ = parent.declaredElement!.enclosingElement3;
145+
var element = parent.declaredFragment!.element;
146+
var class_ = element.enclosingElement2;
142147
if (class_.isPrivate || parentElementIsPrivate) {
143148
_errorReporter.atNode(
144149
node.name,
@@ -162,10 +167,10 @@ class AnnotationVerifier {
162167
var kinds = element.targetKinds;
163168
if (kinds.isNotEmpty) {
164169
if (!_isValidTarget(parent, kinds)) {
165-
var invokedElement = element.element!;
166-
var name = invokedElement.name;
167-
if (invokedElement is ConstructorElement) {
168-
var className = invokedElement.enclosingElement3.name;
170+
var invokedElement = element.element2!;
171+
var name = invokedElement.name3;
172+
if (invokedElement is ConstructorElement2) {
173+
var className = invokedElement.enclosingElement2.name3;
169174
if (name!.isEmpty) {
170175
name = className;
171176
} else {
@@ -243,16 +248,16 @@ class AnnotationVerifier {
243248
/// Reports a warning at [node] if its parent is not a valid target for a
244249
/// `@reopen` annotation.
245250
void _checkReopen(Annotation node) {
246-
ClassElement? classElement;
247-
InterfaceElement? superElement;
251+
ClassElement2? classElement;
252+
InterfaceElement2? superElement;
248253

249254
var parent = node.parent;
250255
if (parent is ClassDeclaration) {
251-
classElement = parent.declaredElement;
252-
superElement = classElement?.supertype?.element;
256+
classElement = parent.declaredFragment?.element;
257+
superElement = classElement?.supertype?.element3;
253258
} else if (parent is ClassTypeAlias) {
254-
classElement = parent.declaredElement;
255-
superElement = classElement?.supertype?.element;
259+
classElement = parent.declaredFragment?.element;
260+
superElement = classElement?.supertype?.element3;
256261
} else {
257262
// If `parent` is neither of the above types, then `_checkKinds` will
258263
// report a warning.
@@ -262,7 +267,7 @@ class AnnotationVerifier {
262267
if (classElement == null) {
263268
return;
264269
}
265-
if (superElement is! ClassElement) {
270+
if (superElement is! ClassElement2) {
266271
return;
267272
}
268273
if (classElement.isFinal ||
@@ -274,7 +279,7 @@ class AnnotationVerifier {
274279
);
275280
return;
276281
}
277-
if (classElement.library != superElement.library) {
282+
if (classElement.library2 != superElement.library2) {
278283
_errorReporter.atNode(
279284
node.name,
280285
WarningCode.INVALID_REOPEN_ANNOTATION,
@@ -319,7 +324,7 @@ class AnnotationVerifier {
319324
if (name != null) {
320325
var parameterName = undefinedParameter is SimpleStringLiteral
321326
? undefinedParameter.value
322-
: undefinedParameter.staticParameterElement?.name;
327+
: undefinedParameter.correspondingParameter?.name3;
323328
_errorReporter.atNode(
324329
undefinedParameter,
325330
WarningCode.UNDEFINED_REFERENCED_PARAMETER,
@@ -354,11 +359,10 @@ class AnnotationVerifier {
354359

355360
if (parent is TopLevelVariableDeclaration) {
356361
for (VariableDeclaration variable in parent.variables.variables) {
357-
var variableElement =
358-
variable.declaredElement as TopLevelVariableElement;
362+
var variableElement = variable.declaredTopLevelVariableElement;
359363

360-
var variableName = variableElement.name;
361-
if (Identifier.isPrivateName(variableName)) {
364+
var variableName = variableElement.name3;
365+
if (variableName != null && Identifier.isPrivateName(variableName)) {
362366
reportInvalidAnnotation(variableName);
363367
}
364368

@@ -369,25 +373,24 @@ class AnnotationVerifier {
369373
}
370374
} else if (parent is FieldDeclaration) {
371375
for (VariableDeclaration variable in parent.fields.variables) {
372-
var fieldElement = variable.declaredElement as FieldElement;
376+
var fieldElement = variable.declaredFieldElement;
373377
if (parent.isStatic && element.isVisibleForOverriding) {
374378
reportInvalidVisibleForOverriding();
375379
}
376380

377-
var fieldName = fieldElement.name;
378-
if (Identifier.isPrivateName(fieldName)) {
381+
var fieldName = fieldElement.name3;
382+
if (fieldName != null && Identifier.isPrivateName(fieldName)) {
379383
reportInvalidAnnotation(fieldName);
380384
}
381385
}
382-
} else if (parent.declaredElement != null) {
383-
var declaredElement = parent.declaredElement!;
386+
} else if (parent.declaredFragment?.element case var declaredElement?) {
384387
if (element.isVisibleForOverriding &&
385388
(!declaredElement.isInstanceMember ||
386-
declaredElement.enclosingElement3 is ExtensionTypeElement)) {
389+
declaredElement.enclosingElement2 is ExtensionTypeElement2)) {
387390
reportInvalidVisibleForOverriding();
388391
}
389392

390-
var name = declaredElement.name;
393+
var name = declaredElement.name3;
391394
if (name != null && Identifier.isPrivateName(name)) {
392395
reportInvalidAnnotation(name);
393396
}
@@ -425,14 +428,14 @@ class AnnotationVerifier {
425428
return;
426429
}
427430

428-
InterfaceElement? declaredElement;
431+
InterfaceElement2? declaredElement;
429432
switch (containedDeclaration.parent) {
430433
case ClassDeclaration classDeclaration:
431-
declaredElement = classDeclaration.declaredElement;
434+
declaredElement = classDeclaration.declaredFragment?.element;
432435
case EnumDeclaration enumDeclaration:
433-
declaredElement = enumDeclaration.declaredElement;
436+
declaredElement = enumDeclaration.declaredFragment?.element;
434437
case MixinDeclaration mixinDeclaration:
435-
declaredElement = mixinDeclaration.declaredElement;
438+
declaredElement = mixinDeclaration.declaredFragment?.element;
436439
default:
437440
reportError();
438441
return;
@@ -443,7 +446,7 @@ class AnnotationVerifier {
443446
return;
444447
}
445448

446-
for (var annotation in declaredElement.metadata) {
449+
for (var annotation in declaredElement.metadata2.annotations) {
447450
if (annotation.isVisibleForTemplate) {
448451
return;
449452
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
6464
final InheritanceManager3 _inheritanceManager;
6565

6666
/// The current library.
67-
final LibraryElement _currentLibrary;
67+
final LibraryElementImpl _currentLibrary;
6868

6969
final AnnotationVerifier _annotationVerifier;
7070

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/src/dart/element/element.dart';
89

910
extension AstNodeExtension on AstNode {
1011
/// Returns all tokens, from [beginToken] to [endToken] including.
@@ -179,3 +180,13 @@ extension CompilationUnitExtension on CompilationUnit {
179180
};
180181
}
181182
}
183+
184+
extension VariableDeclarationExtension on VariableDeclaration {
185+
FieldElementImpl2 get declaredFieldElement {
186+
return declaredFragment!.element as FieldElementImpl2;
187+
}
188+
189+
TopLevelVariableElementImpl2 get declaredTopLevelVariableElement {
190+
return declaredFragment!.element as TopLevelVariableElementImpl2;
191+
}
192+
}

pkg/analyzer/test/src/summary/elements/class_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14903,7 +14903,7 @@ library
1490314903
type: Object
1490414904
getter: <testLibraryFragment>::@class::C::@getter::a#element
1490514905
setter: <testLibraryFragment>::@class::C::@setter::a#element
14906-
14906+
<null-name>
1490714907
firstFragment: <testLibraryFragment>::@class::C::@field::0
1490814908
type: Object
1490914909
getter: <testLibraryFragment>::@class::C::@getter::0#element

pkg/analyzer/test/src/summary/elements/enum_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6358,7 +6358,7 @@ library
63586358
firstFragment: <testLibraryFragment>::@enum::E::@field::v
63596359
type: E
63606360
getter: <testLibraryFragment>::@enum::E::@getter::v#element
6361-
static const
6361+
static const <null-name>
63626362
firstFragment: <testLibraryFragment>::@enum::E::@field::0
63636363
type: E
63646364
getter: <testLibraryFragment>::@enum::E::@getter::0#element

pkg/analyzer/test/src/summary/elements/extension_type_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ library
25242524
primaryConstructor: <testLibraryFragment>::@extensionType::A::@constructor::new#element
25252525
typeErasure: InvalidType
25262526
fields
2527-
final <empty>
2527+
final <null-name>
25282528
firstFragment: <testLibraryFragment>::@extensionType::A::@field::<empty>
25292529
type: InvalidType
25302530
getter: <testLibraryFragment>::@extensionType::A::@getter::<empty>#element

0 commit comments

Comments
 (0)