Skip to content

Commit 7019f1c

Browse files
srawlinsCommit Queue
authored andcommitted
Simplify InheritanceManager3 after many migrations
* `combineSignatures` can be private. * `getInherited` is no longer used in the repo; inline it into `getInherited3` (which is marked experimental). * `getInheritedConcreteMap2` is no longer used in the repo; inline it into `getInheritedConcreteMap` (which is marked experimental). * `getOverridden` is no longer used; remove it. * Migrate a few uses of `getMember` to `getMember3`. Change-Id: I38951b320684a80206b6d9a7706f60e5d21b3c0e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433263 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 3f6db11 commit 7019f1c

File tree

4 files changed

+68
-93
lines changed

4 files changed

+68
-93
lines changed

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

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,6 @@ class InheritanceManager3 {
8989
/// self-referencing cycles.
9090
final Set<InterfaceFragmentImpl> _processingClasses = {};
9191

92-
/// Combine [candidates] into a single signature in the [targetClass].
93-
///
94-
/// If such signature does not exist, return `null`, and if [conflicts] is
95-
/// not `null`, add a new [Conflict] to it.
96-
ExecutableElementOrMember? combineSignatures({
97-
required InterfaceFragmentImpl targetClass,
98-
required List<ExecutableElementOrMember> candidates,
99-
required Name name,
100-
List<Conflict>? conflicts,
101-
}) {
102-
// If just one candidate, it is always valid.
103-
if (candidates.length == 1) {
104-
return candidates[0];
105-
}
106-
107-
var targetLibrary = targetClass.library;
108-
var typeSystem = targetLibrary.typeSystem;
109-
110-
var validOverrides = _getValidOverrides(
111-
candidates: candidates,
112-
typeSystem: typeSystem,
113-
);
114-
115-
if (validOverrides.isEmpty) {
116-
conflicts?.add(CandidatesConflict(name: name, candidates: candidates));
117-
return null;
118-
}
119-
120-
(_combinedSignatures[targetClass] ??= {})[name] =
121-
candidates.map((e) => e.asElement2).toList();
122-
123-
return _topMerge(typeSystem, targetClass, validOverrides);
124-
}
125-
12692
/// Combine types of [candidates] into a single most specific type.
12793
///
12894
/// If such signature does not exist, return `null`, and if [conflicts] is
@@ -164,26 +130,12 @@ class InheritanceManager3 {
164130
);
165131
}
166132

167-
/// Return the result of [getInherited2] with [type] substitution.
168-
ExecutableElementOrMember? getInherited(InterfaceType type, Name name) {
169-
type as InterfaceTypeImpl;
170-
var rawElement = getInherited2(type.element3.asElement, name);
171-
if (rawElement == null) {
172-
return null;
173-
}
174-
175-
return ExecutableMember.from2(
176-
rawElement,
177-
Substitution.fromInterfaceType(type),
178-
);
179-
}
180-
181133
/// Return the most specific signature of the member with the given [name]
182134
/// that [element] inherits from the mixins, superclasses, or interfaces;
183135
/// or `null` if no member is inherited because the member is not declared
184136
/// at all, or because there is no the most specific signature.
185137
///
186-
/// This is equivalent to `getInheritedMap2(type)[name]`.
138+
/// This is equivalent to `getInheritedMap2(element)[name]`.
187139
ExecutableElementOrMember? getInherited2(
188140
InterfaceFragmentImpl element,
189141
Name name,
@@ -192,11 +144,20 @@ class InheritanceManager3 {
192144
}
193145

194146
/// Returns the result of [getInherited2] with [type] substitution.
195-
// This is a replacement for `getInherited`.
196147
@experimental
197148
ExecutableElement? getInherited3(InterfaceType type, Name name) {
198-
var element = getInherited(type, name);
199-
return element?.asElement2;
149+
type as InterfaceTypeImpl;
150+
var rawElement = getInherited2(type.element3.asElement, name);
151+
if (rawElement == null) {
152+
return null;
153+
}
154+
155+
var element = ExecutableMember.from2(
156+
rawElement,
157+
Substitution.fromInterfaceType(type),
158+
);
159+
160+
return element.asElement2;
200161
}
201162

202163
/// Returns the most specific signature of the member with the given [name]
@@ -224,26 +185,20 @@ class InheritanceManager3 {
224185
InterfaceElement element,
225186
) {
226187
element as InterfaceElementImpl2; // TODO(scheglov): remove cast
227-
var map = getInheritedConcreteMap2(element.asElement);
228-
return map.mapValue((element) => element.asElement2);
229-
}
188+
var fragment = element.asElement;
230189

231-
/// Return signatures of all concrete members that the given [element] inherits
232-
/// from the superclasses and mixins.
233-
Map<Name, ExecutableElementOrMember> getInheritedConcreteMap2(
234-
InterfaceFragmentImpl element,
235-
) {
236-
if (element is ExtensionTypeFragmentImpl) {
190+
if (fragment is ExtensionTypeFragmentImpl) {
237191
return const {};
238192
}
239193

240-
var interface = getInterface(element);
241-
if (interface.superImplemented.isNotEmpty) {
242-
return interface.superImplemented.last;
243-
} else {
244-
assert(element.name2 == 'Object');
194+
var interface = getInterface(fragment);
195+
if (interface.superImplemented.isEmpty) {
196+
assert(fragment.name2 == 'Object');
245197
return const {};
246198
}
199+
200+
var map = interface.superImplemented.last;
201+
return map.mapValue((e) => e.asElement2);
247202
}
248203

249204
/// Returns the mapping from names to most specific signatures of members
@@ -443,20 +398,6 @@ class InheritanceManager3 {
443398
return oldElement?.asElement2;
444399
}
445400

446-
/// Returns all members of mixins, superclasses, and interfaces that a member
447-
/// with the given [name], defined in the [element], would override.
448-
///
449-
/// Returns `null` if no members would be overridden.
450-
@experimental
451-
List<ExecutableElement>? getOverridden(InterfaceElement element, Name name) {
452-
element as InterfaceElementImpl2; // TODO(scheglov): remove cast
453-
var elements = getOverridden2(element.asElement, name);
454-
if (elements == null) {
455-
return null;
456-
}
457-
return elements.map((fragment) => fragment.asElement2).toList();
458-
}
459-
460401
/// Return all members of mixins, superclasses, and interfaces that a member
461402
/// with the given [name], defined in the [element], would override; or `null`
462403
/// if no members would be overridden.
@@ -578,6 +519,40 @@ class InheritanceManager3 {
578519
}
579520
}
580521

522+
/// Combine [candidates] into a single signature in the [targetClass].
523+
///
524+
/// If such signature does not exist, return `null`, and if [conflicts] is
525+
/// not `null`, add a new [Conflict] to it.
526+
ExecutableElementOrMember? _combineSignatures({
527+
required InterfaceFragmentImpl targetClass,
528+
required List<ExecutableElementOrMember> candidates,
529+
required Name name,
530+
List<Conflict>? conflicts,
531+
}) {
532+
// If just one candidate, it is always valid.
533+
if (candidates.length == 1) {
534+
return candidates[0];
535+
}
536+
537+
var targetLibrary = targetClass.library;
538+
var typeSystem = targetLibrary.typeSystem;
539+
540+
var validOverrides = _getValidOverrides(
541+
candidates: candidates,
542+
typeSystem: typeSystem,
543+
);
544+
545+
if (validOverrides.isEmpty) {
546+
conflicts?.add(CandidatesConflict(name: name, candidates: candidates));
547+
return null;
548+
}
549+
550+
(_combinedSignatures[targetClass] ??= {})[name] =
551+
candidates.map((e) => e.asElement2).toList();
552+
553+
return _topMerge(typeSystem, targetClass, validOverrides);
554+
}
555+
581556
/// The given [namedCandidates] maps names to candidates from direct
582557
/// superinterfaces. Find the most specific signature, and put it into the
583558
/// [map], if there is no one yet (from the class itself). If there is no
@@ -607,7 +582,7 @@ class InheritanceManager3 {
607582
continue;
608583
}
609584

610-
var combinedSignature = combineSignatures(
585+
var combinedSignature = _combineSignatures(
611586
targetClass: targetClass,
612587
candidates: candidates,
613588
name: name,
@@ -965,7 +940,7 @@ class InheritanceManager3 {
965940
continue;
966941
}
967942

968-
var combinedSignature = combineSignatures(
943+
var combinedSignature = _combineSignatures(
969944
targetClass: fragment,
970945
candidates: notPrecluded,
971946
name: name,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ class CorrectOverrideHelper {
3434
}
3535

3636
/// Return `true` if [_thisMember] is a correct override of [superMember].
37-
bool isCorrectOverrideOf({required ExecutableElement2OrMember superMember}) {
38-
var superType = superMember.type;
37+
bool isCorrectOverrideOf({required ExecutableElement superMember}) {
38+
var superType = superMember.type as TypeImpl;
3939
return _typeSystem.isSubtypeOf(_thisTypeForSubtype!, superType);
4040
}
4141

4242
/// If [_thisMember] is not a correct override of [superMember], report the
4343
/// error.
4444
void verify({
45-
required ExecutableElement2OrMember superMember,
45+
required ExecutableElement superMember,
4646
required DiagnosticReporter diagnosticReporter,
4747
required SyntacticEntity errorNode,
4848
required DiagnosticCode diagnosticCode,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class _ClassVerifier {
389389
);
390390

391391
for (var superType in directSuperInterfaces) {
392-
var superMember = inheritance.getMember(
392+
var superMember = inheritance.getMember3(
393393
superType,
394394
name,
395395
forMixinIndex: mixinIndex,
@@ -405,7 +405,7 @@ class _ClassVerifier {
405405
}
406406

407407
correctOverrideHelper.verify(
408-
superMember: superMember.asElement2,
408+
superMember: superMember,
409409
diagnosticReporter: reporter,
410410
errorNode: node,
411411
diagnosticCode:
@@ -667,15 +667,15 @@ class _ClassVerifier {
667667
}
668668

669669
if (implementsDartCoreEnum) {
670-
var concreteMap = inheritance.getInheritedConcreteMap2(classElement);
671-
672670
void checkSingle(
673671
String memberName,
674672
bool Function(ClassElement enclosingClass) filter,
675673
) {
676-
var member = concreteMap[Name(libraryUri, memberName)];
674+
var member = classElement.element.getInheritedConcreteMember(
675+
Name(libraryUri, memberName),
676+
);
677677
if (member != null) {
678-
var enclosingClass = member.asElement2.enclosingElement;
678+
var enclosingClass = member.enclosingElement;
679679
if (enclosingClass != null) {
680680
if (enclosingClass is! ClassElement || filter(enclosingClass)) {
681681
reporter.atToken(

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4421,7 +4421,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
44214421
return true;
44224422
}
44234423

4424-
var mixinMember = _inheritanceManager.getMember(
4424+
var mixinMember = _inheritanceManager.getMember3(
44254425
mixinType,
44264426
nameObject,
44274427
forSuper: true,
@@ -4431,7 +4431,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
44314431
var isCorrect = CorrectOverrideHelper(
44324432
typeSystem: typeSystem,
44334433
thisMember: superMember.asElement2,
4434-
).isCorrectOverrideOf(superMember: mixinMember.asElement2);
4434+
).isCorrectOverrideOf(superMember: mixinMember);
44354435
if (!isCorrect) {
44364436
diagnosticReporter.atNode(
44374437
mixinName,

0 commit comments

Comments
 (0)