Skip to content

Commit 3d5238a

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Resolve static members of an extension within the members of the extension
Change-Id: I60687da4353ad056ff6312b07e9818b9dcfb9813 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110202 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 187c5b5 commit 3d5238a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

pkg/analyzer/lib/src/dart/resolver/scope.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,36 @@ class EnclosedScope extends Scope {
146146
}
147147
}
148148

149+
/// The scope defined by an extension.
150+
class ExtensionScope extends EnclosedScope {
151+
/// Initialize a newly created scope, enclosed within the [enclosingScope],
152+
/// that represents the given [_extensionElement].
153+
ExtensionScope(Scope enclosingScope, ExtensionElement extensionElement)
154+
: super(enclosingScope) {
155+
_defineMembers(extensionElement);
156+
}
157+
158+
/// Define the static members defined by the given [extensionElement]. The
159+
/// instance members should only be found if they would be found by normal
160+
/// lookup on `this`.
161+
void _defineMembers(ExtensionElement extensionElement) {
162+
List<PropertyAccessorElement> accessors = extensionElement.accessors;
163+
int accessorLength = accessors.length;
164+
for (int i = 0; i < accessorLength; i++) {
165+
if (accessors[i].isStatic) {
166+
define(accessors[i]);
167+
}
168+
}
169+
List<MethodElement> methods = extensionElement.methods;
170+
int methodLength = methods.length;
171+
for (int i = 0; i < methodLength; i++) {
172+
if (methods[i].isStatic) {
173+
define(methods[i]);
174+
}
175+
}
176+
}
177+
}
178+
149179
/**
150180
* The scope defined by a function.
151181
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5993,6 +5993,7 @@ abstract class ScopedVisitor extends UnifyingAstVisitor<void> {
59935993
// TODO(brianwilkerson) Figure out what, if anything, to do here.
59945994
throw UnsupportedError('Extension of function type');
59955995
}
5996+
nameScope = ExtensionScope(nameScope, extensionElement);
59965997
visitExtensionMembersInScope(node);
59975998
}
59985999
} finally {

pkg/analyzer/test/src/dart/resolution/extension_method_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ f() {
7878
expect(invocation.identifier.staticElement, declaration.declaredElement);
7979
}
8080

81+
test_accessStaticWithinInstance() async {
82+
await assertNoErrorsInCode('''
83+
class A {}
84+
extension E on A {
85+
static void a() {}
86+
void b() { a(); }
87+
}
88+
''');
89+
var invocation = findNode.methodInvocation('a();');
90+
assertElement(invocation, findElement.method('a'));
91+
}
92+
8193
test_method_moreSpecificThanPlatform() async {
8294
//
8395
// An extension with on type clause T1 is more specific than another

0 commit comments

Comments
 (0)