Skip to content

Commit aff1786

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Issue 56917. Fix for thisOrAncestorOfType2() for InstanceElement2.
Bug: #56917 Change-Id: Ideda3596a7503ff98154e27353d1337b2055df02 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390811 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent fef2bef commit aff1786

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,8 +2441,10 @@ abstract class ElementImpl implements Element, Element2 {
24412441
@override
24422442
Element2? get enclosingElement2 {
24432443
var candidate = _enclosingElement3;
2444-
if (candidate is CompilationUnitElementImpl ||
2445-
candidate is AugmentableElement) {
2444+
if (candidate is CompilationUnitElementImpl) {
2445+
throw UnsupportedError('Cannot get an enclosingElement2 for a fragment');
2446+
}
2447+
if (candidate is AugmentableElement) {
24462448
throw UnsupportedError('Cannot get an enclosingElement2 for a fragment');
24472449
}
24482450
return candidate as Element2?;
@@ -7138,12 +7140,20 @@ mixin MaybeAugmentedInstanceElementMixin
71387140
@override
71397141
E? thisOrAncestorMatching2<E extends Element2>(
71407142
bool Function(Element2) predicate,
7141-
) =>
7142-
declaration.thisOrAncestorMatching2(predicate);
7143+
) {
7144+
if (predicate(this)) {
7145+
return this as E;
7146+
}
7147+
return library2.thisOrAncestorMatching2(predicate);
7148+
}
71437149

71447150
@override
7145-
E? thisOrAncestorOfType2<E extends Element2>() =>
7146-
declaration.thisOrAncestorOfType2<E>();
7151+
E? thisOrAncestorOfType2<E extends Element2>() {
7152+
if (this case E result) {
7153+
return result;
7154+
}
7155+
return library2.thisOrAncestorOfType2<E>();
7156+
}
71477157

71487158
@override
71497159
void visitChildren2<T>(ElementVisitor2<T> visitor) {

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,11 @@ class _Element2Writer extends _AbstractElementWriter {
950950
}
951951

952952
void _writeInstanceElement(InstanceElement2 e) {
953+
expect(e.thisOrAncestorOfType2<InstanceElement2>(), same(e));
954+
expect(e.thisOrAncestorOfType2<GetterElement>(), isNull);
955+
expect(e.thisOrAncestorMatching2((_) => true), same(e));
956+
expect(e.thisOrAncestorMatching2((_) => false), isNull);
957+
953958
_sink.writeIndentedLine(() {
954959
switch (e) {
955960
case ClassElement2():

0 commit comments

Comments
 (0)