Skip to content

Commit 838e1da

Browse files
scheglovCommit Queue
authored andcommitted
Fine. Don't lookup private fields, methods from other libraries.
Prevent the resolver from considering private identifiers when collecting applicable extensions for a different library. Previously we probed members by simple name and could end up touching private fields/methods like `_foo` across library boundaries, which led to unnecessary dependency tracking and occasional cache churn (e.g., ID mismatches) and extra analysis work. Add an early accessibility guard: - Skip an extension if the requested `baseName` is not accessible from the extension’s library URI. With this guard in place, the subsequent per-member name equality checks are redundant and are removed, simplifying the lookup. The result is correct privacy behavior, fewer spurious lookups, and more stable analysis results. Change-Id: I063823f29d820377897a406663fe4d612c493f72 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445343 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4c4a1d3 commit 838e1da

File tree

2 files changed

+22
-63
lines changed

2 files changed

+22
-63
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ extension ExtensionsExtensions on Iterable<ExtensionElement> {
133133
) {
134134
var result = <_NotInstantiatedExtensionWithMember>[];
135135
for (var extension in this) {
136+
if (!baseName.isAccessibleFor(extension.library.uri)) {
137+
continue;
138+
}
139+
136140
if (baseName.name == '[]') {
137141
var getter = extension.getMethod('[]');
138142
var setter = extension.getMethod('[]=');
@@ -150,31 +154,27 @@ extension ExtensionsExtensions on Iterable<ExtensionElement> {
150154
} else {
151155
var field = extension.getField(baseName.name);
152156
if (field != null && !field.isStatic) {
153-
if (Name.forElement(field) == baseName) {
154-
result.add(
155-
_NotInstantiatedExtensionWithMember(
156-
// TODO(paulberry): eliminate this cast by changing the
157-
// extension to apply only to `Iterable<ExtensionElementImpl>`.
158-
extension as ExtensionElementImpl,
159-
getter: field.getter,
160-
setter: field.setter,
161-
),
162-
);
163-
}
157+
result.add(
158+
_NotInstantiatedExtensionWithMember(
159+
// TODO(paulberry): eliminate this cast by changing the
160+
// extension to apply only to `Iterable<ExtensionElementImpl>`.
161+
extension as ExtensionElementImpl,
162+
getter: field.getter,
163+
setter: field.setter,
164+
),
165+
);
164166
}
165167

166168
var method = extension.getMethod(baseName.name);
167169
if (method != null && !method.isStatic) {
168-
if (Name.forElement(method) == baseName) {
169-
result.add(
170-
_NotInstantiatedExtensionWithMember(
171-
// TODO(paulberry): eliminate this cast by changing the
172-
// extension to apply only to `Iterable<ExtensionElementImpl>`.
173-
extension as ExtensionElementImpl,
174-
getter: method,
175-
),
176-
);
177-
}
170+
result.add(
171+
_NotInstantiatedExtensionWithMember(
172+
// TODO(paulberry): eliminate this cast by changing the
173+
// extension to apply only to `Iterable<ExtensionElementImpl>`.
174+
extension as ExtensionElementImpl,
175+
getter: method,
176+
),
177+
);
178178
}
179179
}
180180
}

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20519,13 +20519,6 @@ void f() {
2051920519
package:test/a.dart
2052020520
Object: <null>
2052120521
int: <null>
20522-
instances
20523-
package:test/a.dart
20524-
A
20525-
requestedFields
20526-
_foo: #M1
20527-
requestedMethods
20528-
_foo: <null>
2052920522
exportedExtensions
2053020523
dart:core: #M9
2053120524
package:test/a.dart: #M0
@@ -20561,43 +20554,9 @@ extension A on Object {
2056120554
7 +8 UNUSED_IMPORT
2056220555
[operation] readLibraryCycleBundle
2056320556
package:test/test.dart
20564-
[operation] getErrorsCannotReuse
20565-
instanceFieldIdMismatch
20566-
libraryUri: package:test/a.dart
20567-
interfaceName: A
20568-
fieldName: _foo
20569-
expectedId: #M1
20570-
actualId: #M10
20571-
[operation] analyzeFile
20557+
[operation] getErrorsFromBytes
2057220558
file: /home/test/lib/test.dart
2057320559
library: /home/test/lib/test.dart
20574-
[stream]
20575-
ResolvedUnitResult #3
20576-
path: /home/test/lib/test.dart
20577-
uri: package:test/test.dart
20578-
flags: exists isLibrary
20579-
errors
20580-
7 +8 UNUSED_IMPORT
20581-
[operation] analyzedLibrary
20582-
file: /home/test/lib/test.dart
20583-
requirements
20584-
topLevels
20585-
dart:core
20586-
Object: #M3
20587-
int: #M4
20588-
package:test/a.dart
20589-
Object: <null>
20590-
int: <null>
20591-
instances
20592-
package:test/a.dart
20593-
A
20594-
requestedFields
20595-
_foo: #M10
20596-
requestedMethods
20597-
_foo: <null>
20598-
exportedExtensions
20599-
dart:core: #M9
20600-
package:test/a.dart: #M0
2060120560
[status] idle
2060220561
''',
2060320562
);

0 commit comments

Comments
 (0)