Skip to content

Commit c8ff99d

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Omit '=' at the end of SetterFragment / SetterElement name.
Also fixes names where they were previously missing. Change-Id: I2d16059e3871283de7c9373fb040d939bd5357ba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394900 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 8fa66a0 commit c8ff99d

40 files changed

+3138
-3075
lines changed

pkg/analysis_server/lib/src/computer/computer_overrides.dart

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analysis_server/src/protocol_server.dart' as proto;
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/token.dart';
99
import 'package:analyzer/dart/element/element2.dart';
10-
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1110
import 'package:collection/collection.dart';
1211

1312
/// Return the elements that the given [element] overrides.
@@ -211,37 +210,32 @@ class _OverriddenElementsFinder {
211210
}
212211

213212
Element2? _lookupMember(InterfaceElement2 classElement) {
214-
var name = Name.forLibrary(_library, _name);
215-
216-
/// Helper to find an element in [elements] that matches [targetName].
217-
Element2? findMatchingElement(
218-
Iterable<Element2> elements,
219-
Name targetName,
220-
) {
213+
Element2? findMatchingElement(Iterable<Element2> elements) {
221214
return elements.firstWhereOrNull((Element2 element) {
222-
var elementName = element.name3;
223-
return elementName != null &&
224-
Name.forLibrary(element.library2, elementName) == targetName;
215+
if (!identical(element.library2, _library) && _name.startsWith('_')) {
216+
return false;
217+
}
218+
return element.name3 == _name;
225219
});
226220
}
227221

228222
// method
229223
if (_kinds.contains(ElementKind.METHOD)) {
230-
var member = findMatchingElement(classElement.methods2, name);
224+
var member = findMatchingElement(classElement.methods2);
231225
if (member != null) {
232226
return member;
233227
}
234228
}
235229
// getter
236230
if (_kinds.contains(ElementKind.GETTER)) {
237-
var member = findMatchingElement(classElement.getters2, name.forGetter);
231+
var member = findMatchingElement(classElement.getters2);
238232
if (member != null) {
239233
return member;
240234
}
241235
}
242236
// setter
243237
if (_kinds.contains(ElementKind.SETTER)) {
244-
var member = findMatchingElement(classElement.setters2, name.forSetter);
238+
var member = findMatchingElement(classElement.setters2);
245239
if (member != null) {
246240
return member;
247241
}

pkg/analysis_server/lib/src/lsp/handlers/custom/handler_super.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,22 @@ class _SuperComputer {
9696
}
9797

9898
var inheritanceManager = session.inheritanceManager;
99-
var elementName = element.name3;
100-
var interfaceElement = element.thisOrAncestorOfType2<InterfaceElement2>();
10199

102-
if (elementName == null || interfaceElement == null) {
100+
if (element is! ExecutableElement2) {
103101
return null;
104102
}
105103

106-
var name = Name(
107-
interfaceElement.library2.firstFragment.source.uri,
108-
elementName,
109-
);
110-
var member = inheritanceManager.getInherited4(interfaceElement, name);
104+
var name = Name.forElement(element);
105+
if (name == null) {
106+
return null;
107+
}
111108

109+
var interfaceElement = element.thisOrAncestorOfType2<InterfaceElement2>();
110+
if (interfaceElement == null) {
111+
return null;
112+
}
113+
114+
var member = inheritanceManager.getInherited4(interfaceElement, name);
112115
return _lastFragment(member);
113116
}
114117

pkg/analysis_server/lib/src/services/correction/dart/import_add_show.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ class _ReferenceFinder extends RecursiveAstVisitor<void> {
144144
void _addName(Token nameToken, Element2? element) {
145145
if (element != null) {
146146
var name = nameToken.lexeme;
147-
if (namespace[name] == element ||
148-
(name != element.name3 && namespace[element.name3] == element)) {
147+
if (namespace[name] == element || namespace['$name='] == element) {
149148
referencedNames.add(element.displayName);
150149
}
151150
}

pkg/analysis_server/lib/src/utilities/import_analyzer.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,20 @@ class _ReferenceFinder extends RecursiveAstVisitor<void> {
320320
/// in [node].
321321
LibraryImport? _getImportForElement(AstNode? node, Element2 element) {
322322
var prefix = _getPrefixFromExpression(node)?.name3;
323-
var elementName = element.name3;
323+
324+
var lookupName = () {
325+
var name = element.name3;
326+
if (name == null) {
327+
return null;
328+
}
329+
if (element is SetterElement) {
330+
return '$name=';
331+
}
332+
return name;
333+
}();
334+
324335
// We cannot locate imports for unnamed elements.
325-
if (elementName == null) {
336+
if (lookupName == null) {
326337
return null;
327338
}
328339

@@ -332,8 +343,8 @@ class _ReferenceFinder extends RecursiveAstVisitor<void> {
332343
// prefix/name.
333344
var exportedElement =
334345
prefix != null
335-
? import.namespace.getPrefixed2(prefix, elementName)
336-
: import.namespace.get2(elementName);
346+
? import.namespace.getPrefixed2(prefix, lookupName)
347+
: import.namespace.get2(lookupName);
337348
return exportedElement == element;
338349
}).firstOrNull;
339350

@@ -347,7 +358,7 @@ class _ReferenceFinder extends RecursiveAstVisitor<void> {
347358
// Because we don't know what prefix we're looking for (any is
348359
// allowed), use the imports own prefix when checking for the
349360
// element.
350-
import.namespace.getPrefixed2(import.prefix2?.element.name3 ?? '', elementName) ==
361+
import.namespace.getPrefixed2(import.prefix2?.element.name3 ?? '', lookupName) ==
351362
element,
352363
)
353364
.firstOrNull;

pkg/analysis_server/test/src/utilities/import_analyzer_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int v2 = 0;
211211
element<ExtensionElement2>(name: 'X1'),
212212
element<TopLevelFunctionElement>(name: 'f1'),
213213
element<GetterElement>(name: 'g1'),
214-
element<SetterElement>(name: 's1='),
214+
element<SetterElement>(name: 's1'),
215215
element<TopLevelVariableElement2>(name: 'v1'),
216216
],
217217
expectedStayingDeclarations: [
@@ -225,7 +225,7 @@ int v2 = 0;
225225
element<ExtensionElement2>(name: 'X2'),
226226
element<TopLevelFunctionElement>(name: 'f2'),
227227
element<GetterElement>(name: 'g2'),
228-
element<SetterElement>(name: 's2='),
228+
element<SetterElement>(name: 's2'),
229229
element<TopLevelVariableElement2>(name: 'v2'),
230230
],
231231
);
@@ -675,7 +675,7 @@ int set s(x) {}
675675
!]
676676
''',
677677
expectedStayingReferences: {
678-
element<SetterElement>(name: 's='): [''],
678+
element<SetterElement>(name: 's'): [''],
679679
},
680680
);
681681
}
@@ -691,7 +691,7 @@ void f() {
691691
!]
692692
''',
693693
expectedMovingReferences: {
694-
element<SetterElement>(name: 's='): [''],
694+
element<SetterElement>(name: 's'): [''],
695695
},
696696
);
697697
}
@@ -707,7 +707,7 @@ set v(num _) {}
707707
!]
708708
''',
709709
expectedStayingReferences: {
710-
element<SetterElement>(name: 'v='): [''],
710+
element<SetterElement>(name: 'v'): [''],
711711
element<GetterElement>(name: 'v'): [''],
712712
},
713713
);
@@ -724,7 +724,7 @@ void f() => v += 1;
724724
!]
725725
''',
726726
expectedMovingReferences: {
727-
element<SetterElement>(name: 'v='): [''],
727+
element<SetterElement>(name: 'v'): [''],
728728
element<GetterElement>(name: 'v'): [''],
729729
},
730730
);
@@ -799,7 +799,7 @@ set v(num _) {}
799799
!]
800800
''',
801801
expectedStayingReferences: {
802-
element<SetterElement>(name: 'v='): [''],
802+
element<SetterElement>(name: 'v'): [''],
803803
element<GetterElement>(name: 'v'): [''],
804804
},
805805
);
@@ -816,7 +816,7 @@ void f() => v++;
816816
!]
817817
''',
818818
expectedMovingReferences: {
819-
element<SetterElement>(name: 'v='): [''],
819+
element<SetterElement>(name: 'v'): [''],
820820
element<GetterElement>(name: 'v'): [''],
821821
},
822822
);
@@ -863,7 +863,7 @@ set v(num _) {}
863863
!]
864864
''',
865865
expectedStayingReferences: {
866-
element<SetterElement>(name: 'v='): [''],
866+
element<SetterElement>(name: 'v'): [''],
867867
element<GetterElement>(name: 'v'): [''],
868868
},
869869
);
@@ -880,7 +880,7 @@ void f() => ++v;
880880
!]
881881
''',
882882
expectedMovingReferences: {
883-
element<SetterElement>(name: 'v='): [''],
883+
element<SetterElement>(name: 'v'): [''],
884884
element<GetterElement>(name: 'v'): [''],
885885
},
886886
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import 'package:meta/meta.dart';
9999
// TODO(scheglov): Clean up the list of implicitly analyzed files.
100100
class AnalysisDriver {
101101
/// The version of data format, should be incremented on every format change.
102-
static const int DATA_VERSION = 418;
102+
static const int DATA_VERSION = 419;
103103

104104
/// The number of exception contexts allowed to write. Once this field is
105105
/// zero, we stop writing any new exception contexts in this process.

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,7 +3237,7 @@ abstract class ElementImpl2 implements Element2 {
32373237
List<Element2> get children2 => const [];
32383238

32393239
@override
3240-
String get displayName => name3 ?? '';
3240+
String get displayName => name3 ?? '<unnamed>';
32413241

32423242
@override
32433243
// TODO(augmentations): implement enclosingElement2
@@ -5180,7 +5180,7 @@ class GetterElementImpl extends ExecutableElementImpl2
51805180
ElementKind get kind => ElementKind.GETTER;
51815181

51825182
@override
5183-
String? get name3 => firstFragment.name;
5183+
String? get name3 => firstFragment.name2;
51845184

51855185
@override
51865186
Element2 get nonSynthetic2 {
@@ -9409,6 +9409,9 @@ class PropertyAccessorElementImpl_ImplicitGetter
94099409
@override
94109410
bool get isGetter => true;
94119411

9412+
@override
9413+
String? get name2 => variable2.name2;
9414+
94129415
@override
94139416
Element get nonSynthetic {
94149417
if (!variable2.isSynthetic) {
@@ -9467,6 +9470,9 @@ class PropertyAccessorElementImpl_ImplicitSetter
94679470
@override
94689471
bool get isSetter => true;
94699472

9473+
@override
9474+
String? get name2 => variable2.name2;
9475+
94709476
@override
94719477
Element get nonSynthetic => variable2;
94729478

@@ -9726,15 +9732,6 @@ class SetterElementImpl extends ExecutableElementImpl2
97269732
GetterElement? get correspondingGetter2 =>
97279733
firstFragment.correspondingGetter2?.element as GetterElement?;
97289734

9729-
@override
9730-
String get displayName {
9731-
if (name3 case var name?) {
9732-
return name.substring(0, name.length - 1);
9733-
} else {
9734-
return '<null-name>';
9735-
}
9736-
}
9737-
97389735
@override
97399736
Element2? get enclosingElement2 => firstFragment.enclosingFragment?.element;
97409737

@@ -9745,7 +9742,7 @@ class SetterElementImpl extends ExecutableElementImpl2
97459742
ElementKind get kind => ElementKind.SETTER;
97469743

97479744
@override
9748-
String? get name3 => firstFragment.name;
9745+
String? get name3 => firstFragment.name2;
97499746

97509747
@override
97519748
Element2 get nonSynthetic2 {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,10 @@ class InheritanceManager3 {
11321132

11331133
if (first is MethodElement) {
11341134
var firstMethod = first;
1135+
var fragmentName = first.asElement2.firstFragment.name2;
11351136
var result = MethodElementImpl(firstMethod.name, -1);
11361137
result.enclosingElement3 = targetClass;
1138+
result.name2 = fragmentName;
11371139
result.typeParameters = resultType.typeFormals;
11381140
result.returnType = resultType.returnType;
11391141
result.parameters = resultType.parameters;
@@ -1145,17 +1147,20 @@ class InheritanceManager3 {
11451147
return result;
11461148
} else {
11471149
var firstAccessor = first as PropertyAccessorElement;
1150+
var fragmentName = first.asElement2.firstFragment.name2;
11481151
var variableName = firstAccessor.displayName;
11491152

11501153
var result = PropertyAccessorElementImpl(variableName, -1);
11511154
result.enclosingElement3 = targetClass;
1155+
result.name2 = fragmentName;
11521156
result.isGetter = firstAccessor.isGetter;
11531157
result.isSetter = firstAccessor.isSetter;
11541158
result.returnType = resultType.returnType;
11551159
result.parameters = resultType.parameters;
11561160

11571161
var field = FieldElementImpl(variableName, -1);
11581162
field.enclosingElement3 = targetClass;
1163+
field.name2 = fragmentName;
11591164
if (firstAccessor.isGetter) {
11601165
field.getter = result;
11611166
field.type = result.returnType;
@@ -1350,6 +1355,29 @@ class Name {
13501355

13511356
@override
13521357
String toString() => libraryUri != null ? '$libraryUri::$name' : name;
1358+
1359+
/// Returns the name that corresponds to [element].
1360+
///
1361+
/// If the element is private, the name includes the library URI.
1362+
///
1363+
/// If the name is a setter, the name ends with `=`.
1364+
static Name? forElement(ExecutableElement2 element) {
1365+
var name = element.name3;
1366+
if (name == null) {
1367+
return null;
1368+
}
1369+
1370+
if (element is SetterElement) {
1371+
name = '$name=';
1372+
}
1373+
1374+
if (name.startsWith('_')) {
1375+
var libraryUri = element.firstFragment.libraryFragment.source.uri;
1376+
return Name(libraryUri, name);
1377+
} else {
1378+
return Name(null, name);
1379+
}
1380+
}
13531381
}
13541382

13551383
/// Failure because of not unique extension type member.

pkg/analyzer/lib/src/summary2/bundle_reader.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,8 +1584,8 @@ class LibraryReader {
15841584
var variableFragment = TopLevelVariableElementImpl(name, -1)
15851585
..enclosingElement3 = enclosingElement
15861586
..reference = propertyFragmentReference
1587-
..isSynthetic = true;
1588-
variableFragment.name2 = accessor.name2;
1587+
..isSynthetic = true
1588+
..name2 = accessor.name2;
15891589
propertyFragment = variableFragment;
15901590
propertyFragmentReference.element ??= propertyFragment;
15911591
propertyFragments.add(variableFragment);
@@ -1606,7 +1606,8 @@ class LibraryReader {
16061606
..enclosingElement3 = enclosingElement
16071607
..reference = propertyFragmentReference
16081608
..isStatic = accessor.isStatic
1609-
..isSynthetic = true;
1609+
..isSynthetic = true
1610+
..name2 = accessor.name2;
16101611
propertyFragmentReference.element ??= propertyFragment;
16111612
propertyFragments.add(propertyFragment);
16121613
}

0 commit comments

Comments
 (0)