Skip to content

Commit ce36fba

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate RedeclareVerifier.
Change-Id: I7e8b6d4b22b1e657a1517932cc27265690f7b0cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395142 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent fab7e4b commit ce36fba

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ lib/src/error/literal_element_verifier.dart
116116
lib/src/error/must_call_super_verifier.dart
117117
lib/src/error/nullable_dereference_verifier.dart
118118
lib/src/error/override_verifier.dart
119-
lib/src/error/redeclare_verifier.dart
120119
lib/src/error/required_parameters_verifier.dart
121120
lib/src/error/super_formal_parameters_verifier.dart
122121
lib/src/error/type_arguments_verifier.dart

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,12 @@ class LibraryAnalyzer {
497497
errorReporter,
498498
));
499499

500-
unit.accept(RedeclareVerifier(
501-
_inheritance,
502-
_libraryElement,
503-
errorReporter,
504-
));
500+
unit.accept(
501+
RedeclareVerifier(
502+
_inheritance,
503+
errorReporter,
504+
),
505+
);
505506

506507
TodoFinder(errorReporter).findIn(unit);
507508
LanguageVersionOverrideVerifier(errorReporter).verify(unit);

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:analyzer/error/listener.dart';
99
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1010
import 'package:analyzer/src/error/codes.dart';
@@ -16,22 +16,17 @@ class RedeclareVerifier extends RecursiveAstVisitor<void> {
1616
/// The inheritance manager used to find redeclared members.
1717
final InheritanceManager3 _inheritance;
1818

19-
/// The URI of the library being verified.
20-
final Uri _libraryUri;
21-
2219
/// The error reporter used to report errors.
2320
final ErrorReporter _errorReporter;
2421

2522
/// The current extension type.
26-
InterfaceElement? _currentExtensionType;
23+
InterfaceElement2? _currentExtensionType;
2724

28-
RedeclareVerifier(
29-
this._inheritance, LibraryElement library, this._errorReporter)
30-
: _libraryUri = library.source.uri;
25+
RedeclareVerifier(this._inheritance, this._errorReporter);
3126

3227
@override
3328
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
34-
_currentExtensionType = node.declaredElement;
29+
_currentExtensionType = node.declaredFragment!.element;
3530
super.visitExtensionTypeDeclaration(node);
3631
_currentExtensionType = null;
3732
}
@@ -41,49 +36,48 @@ class RedeclareVerifier extends RecursiveAstVisitor<void> {
4136
// Only check if we're in an extension type declaration.
4237
if (_currentExtensionType == null) return;
4338

44-
var element = node.declaredElement!;
39+
var element = node.declaredFragment!.element;
4540

4641
// Static members can't redeclare.
4742
if (element.isStatic) return;
4843

49-
if (element.hasRedeclare && !_redeclaresMember(element)) {
50-
if (element is MethodElement) {
51-
_errorReporter.atToken(
52-
node.name,
53-
WarningCode.REDECLARE_ON_NON_REDECLARING_MEMBER,
54-
arguments: [
55-
'method',
56-
],
57-
);
58-
} else if (element is PropertyAccessorElement) {
59-
if (element.isGetter) {
44+
if (element.metadata2.hasRedeclare && !_redeclaresMember(element)) {
45+
switch (element) {
46+
case MethodElement2():
47+
_errorReporter.atToken(
48+
node.name,
49+
WarningCode.REDECLARE_ON_NON_REDECLARING_MEMBER,
50+
arguments: [
51+
'method',
52+
],
53+
);
54+
case GetterElement():
6055
_errorReporter.atToken(
6156
node.name,
6257
WarningCode.REDECLARE_ON_NON_REDECLARING_MEMBER,
6358
arguments: [
6459
'getter',
6560
],
6661
);
67-
} else {
62+
case SetterElement():
6863
_errorReporter.atToken(
6964
node.name,
7065
WarningCode.REDECLARE_ON_NON_REDECLARING_MEMBER,
7166
arguments: [
7267
'setter',
7368
],
7469
);
75-
}
7670
}
7771
}
7872
}
7973

8074
/// Return `true` if the [member] redeclares a member from a superinterface.
81-
bool _redeclaresMember(ExecutableElement member) {
75+
bool _redeclaresMember(ExecutableElement2 member) {
8276
var currentType = _currentExtensionType;
8377
if (currentType != null) {
84-
var interface = _inheritance.getInterface(currentType);
85-
var redeclared = interface.redeclared;
86-
var name = Name(_libraryUri, member.name);
78+
var interface = _inheritance.getInterface2(currentType);
79+
var redeclared = interface.redeclared2;
80+
var name = Name.forElement(member);
8781
return redeclared.containsKey(name);
8882
}
8983

pkg/analyzer/test/src/diagnostics/redeclare_on_non_redeclaring_member_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ extension type E(C c) implements C {
100100
''');
101101
}
102102

103+
test_method_redeclared_private() async {
104+
await assertErrorsInCode(r'''
105+
import 'package:meta/meta.dart';
106+
107+
class A {
108+
void _foo() {}
109+
}
110+
111+
extension type E(A it) implements A {
112+
@redeclare
113+
void _foo() {}
114+
}
115+
''', [
116+
error(WarningCode.UNUSED_ELEMENT, 51, 4),
117+
error(WarningCode.UNUSED_ELEMENT, 122, 4),
118+
]);
119+
}
120+
103121
test_method_static() async {
104122
await assertErrorsInCode(r'''
105123
import 'package:meta/meta.dart';

0 commit comments

Comments
 (0)