Skip to content

Commit 6df2491

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 38551. Fix for reading references to PropertyAccessorElement(s) defined in extensions.
[email protected] Bug: #38551 Change-Id: Id93714a2db97ff1ab448fe408f4e86df9988a147 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125520 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 7c1f58f commit 6df2491

3 files changed

Lines changed: 78 additions & 17 deletions

File tree

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,16 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
12601260
SimpleIdentifier prefixNode = node.prefix;
12611261
Element prefixElement = prefixNode.staticElement;
12621262
// String.length
1263-
if (prefixElement is! PrefixElement && prefixElement is! ClassElement) {
1263+
if (prefixElement is! PrefixElement &&
1264+
prefixElement is! ClassElement &&
1265+
prefixElement is! ExtensionElement) {
12641266
DartObjectImpl prefixResult = prefixNode.accept(this);
12651267
if (_isStringLength(prefixResult, node.identifier)) {
12661268
return prefixResult.stringLength(_typeProvider);
12671269
}
12681270
}
12691271
// importPrefix.CONST
1270-
if (prefixElement is! PrefixElement) {
1272+
if (prefixElement is! PrefixElement && prefixElement is! ExtensionElement) {
12711273
DartObjectImpl prefixResult = prefixNode.accept(this);
12721274
if (prefixResult == null) {
12731275
// The error has already been reported.

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,18 @@ class _ElementRequest {
240240
ElementImpl enclosing, Reference reference) {
241241
if (enclosing is ClassElementImpl) {
242242
enclosing.accessors;
243-
// Requesting accessors sets elements for accessors and fields.
244-
assert(reference.element != null);
245-
return reference.element;
246-
}
247-
if (enclosing is CompilationUnitElementImpl) {
243+
} else if (enclosing is CompilationUnitElementImpl) {
248244
enclosing.accessors;
249-
// Requesting accessors sets elements for accessors and variables.
250-
assert(reference.element != null);
251-
return reference.element;
252-
}
253-
if (enclosing is EnumElementImpl) {
245+
} else if (enclosing is EnumElementImpl) {
254246
enclosing.accessors;
255-
// Requesting accessors sets elements for accessors and variables.
256-
assert(reference.element != null);
257-
return reference.element;
247+
} else if (enclosing is ExtensionElementImpl) {
248+
enclosing.accessors;
249+
} else {
250+
throw StateError('${enclosing.runtimeType}');
258251
}
259-
// Only classes and units have accessors.
260-
throw StateError('${enclosing.runtimeType}');
252+
// Requesting accessors sets elements for accessors and variables.
253+
assert(reference.element != null);
254+
return reference.element;
261255
}
262256

263257
ClassElementImpl _class(

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,69 @@ const v = a;
116116
expect(typeArgument.element.enclosingElement, elementF);
117117
assertElementTypeStrings(typeArgument.typeArguments, ['double']);
118118
}
119+
120+
test_imported_prefixedIdentifier_staticField_class() async {
121+
newFile('/test/lib/a.dart', content: r'''
122+
const a = C.f;
123+
124+
class C {
125+
static const int f = 42;
126+
}
127+
''');
128+
await resolveTestCode(r'''
129+
import 'a.dart';
130+
''');
131+
132+
var import_ = findElement.importFind('package:test/a.dart');
133+
var a = import_.topVar('a') as ConstVariableElement;
134+
expect(a.computeConstantValue().toIntValue(), 42);
135+
}
136+
137+
test_imported_prefixedIdentifier_staticField_extension() async {
138+
newFile('/test/lib/a.dart', content: r'''
139+
const a = E.f;
140+
141+
extension E on int {
142+
static const int f = 42;
143+
}
144+
''');
145+
await resolveTestCode(r'''
146+
import 'a.dart';
147+
''');
148+
149+
var import_ = findElement.importFind('package:test/a.dart');
150+
var a = import_.topVar('a') as ConstVariableElement;
151+
expect(a.computeConstantValue().toIntValue(), 42);
152+
}
153+
154+
test_imported_prefixedIdentifier_staticField_mixin() async {
155+
newFile('/test/lib/a.dart', content: r'''
156+
const a = M.f;
157+
158+
class C {}
159+
160+
mixin M on C {
161+
static const int f = 42;
162+
}
163+
''');
164+
await resolveTestCode(r'''
165+
import 'a.dart';
166+
''');
167+
168+
var import_ = findElement.importFind('package:test/a.dart');
169+
var a = import_.topVar('a') as ConstVariableElement;
170+
expect(a.computeConstantValue().toIntValue(), 42);
171+
}
172+
173+
test_local_prefixedIdentifier_staticField_extension() async {
174+
await assertNoErrorsInCode(r'''
175+
const a = E.f;
176+
177+
extension E on int {
178+
static const int f = 42;
179+
}
180+
''');
181+
var a = findElement.topVar('a') as ConstVariableElement;
182+
expect(a.computeConstantValue().toIntValue(), 42);
183+
}
119184
}

0 commit comments

Comments
 (0)