Skip to content

Commit c1e04fc

Browse files
keertipCommit Queue
authored andcommitted
[Elements.migrate] Migrate create_extension_member.dart
Change-Id: I2b6bc9c0c6b1e8b1e2b8345d06b207b039fd70c2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398583 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 584c80f commit c1e04fc

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ lib/src/services/completion/dart/declaration_helper.dart
3232
lib/src/services/completion/dart/identifier_helper.dart
3333
lib/src/services/completion/dart/in_scope_completion_pass.dart
3434
lib/src/services/completion/dart/visibility_tracker.dart
35-
lib/src/services/correction/dart/create_extension_member.dart
3635
lib/src/services/correction/dart/import_library.dart
3736
lib/src/services/correction/dart/use_different_division_operator.dart
3837
lib/src/services/correction/namespace.dart

pkg/analysis_server/lib/src/services/correction/dart/create_extension_member.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ abstract class _CreateExtensionMember extends ResolvedCorrectionProducer {
280280
ExtensionDeclaration? _existingExtension(DartType targetType) {
281281
for (var existingExtension in unitResult.unit.declarations) {
282282
if (existingExtension is ExtensionDeclaration) {
283-
var element = existingExtension.declaredElement!;
283+
var element = existingExtension.declaredFragment!.element;
284284
var instantiated = [element].applicableTo(
285-
targetLibrary: libraryElement,
285+
targetLibrary: libraryElement2,
286286
targetType: targetType,
287287
strictCasts: true,
288288
);

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ class InstantiatedExtensionWithoutMember {
9797
);
9898
}
9999

100+
class InstantiatedExtensionWithoutMember2 {
101+
final ExtensionElement2 extension;
102+
final MapSubstitution substitution;
103+
final DartType extendedType;
104+
105+
InstantiatedExtensionWithoutMember2(
106+
this.extension,
107+
this.substitution,
108+
this.extendedType,
109+
);
110+
}
111+
100112
abstract class _NotInstantiatedExtension<R> {
101113
final ExtensionElement extension;
102114

@@ -170,6 +182,21 @@ class _NotInstantiatedExtensionWithoutMember
170182
}
171183
}
172184

185+
/// [_NotInstantiatedExtension2] for any [ExtensionElement2].
186+
class _NotInstantiatedExtensionWithoutMember2
187+
extends _NotInstantiatedExtension2<InstantiatedExtensionWithoutMember2> {
188+
_NotInstantiatedExtensionWithoutMember2(super.extension);
189+
190+
@override
191+
InstantiatedExtensionWithoutMember2 instantiate({
192+
required MapSubstitution substitution,
193+
required DartType extendedType,
194+
}) {
195+
return InstantiatedExtensionWithoutMember2(
196+
extension, substitution, extendedType);
197+
}
198+
}
199+
173200
extension ExtensionsExtensions on Iterable<ExtensionElement> {
174201
/// Extensions that can be applied, within [targetLibrary], to [targetType].
175202
List<InstantiatedExtensionWithoutMember> applicableTo({
@@ -246,6 +273,16 @@ extension ExtensionsExtensions on Iterable<ExtensionElement> {
246273
}
247274

248275
extension ExtensionsExtensions2 on Iterable<ExtensionElement2> {
276+
/// Extensions that can be applied, within [targetLibrary], to [targetType].
277+
List<InstantiatedExtensionWithoutMember2> applicableTo({
278+
required LibraryElement2 targetLibrary,
279+
required DartType targetType,
280+
required bool strictCasts,
281+
}) {
282+
return map((e) => _NotInstantiatedExtensionWithoutMember2(e))
283+
.applicableTo(targetLibrary: targetLibrary, targetType: targetType);
284+
}
285+
249286
/// Returns the sublist of [ExtensionElement2]s that have an instance member
250287
/// named [baseName].
251288
List<_NotInstantiatedExtensionWithMember2> havingMemberWithBaseName(
@@ -390,3 +427,81 @@ extension NotInstantiatedExtensionsExtensions<R>
390427
return instantiated;
391428
}
392429
}
430+
431+
extension NotInstantiatedExtensionsExtensions2<R>
432+
on Iterable<_NotInstantiatedExtension2<R>> {
433+
/// Extensions that can be applied, within [targetLibrary], to [targetType].
434+
List<R> applicableTo({
435+
required LibraryElement2 targetLibrary,
436+
required DartType targetType,
437+
}) {
438+
if (identical(targetType, NeverTypeImpl.instance)) {
439+
return <R>[];
440+
}
441+
442+
targetLibrary as LibraryElementImpl;
443+
var typeSystem = targetLibrary.typeSystem;
444+
var genericMetadataIsEnabled = targetLibrary.featureSet.isEnabled(
445+
Feature.generic_metadata,
446+
);
447+
var inferenceUsingBoundsIsEnabled = targetLibrary.featureSet.isEnabled(
448+
Feature.inference_using_bounds,
449+
);
450+
451+
var instantiated = <R>[];
452+
453+
for (var notInstantiated in this) {
454+
var extension = notInstantiated.extension.asElement as ExtensionElement;
455+
456+
var freshTypes = getFreshTypeParameters(extension.typeParameters);
457+
var freshTypeParameters = freshTypes.freshTypeParameters;
458+
var rawExtendedType = freshTypes.substitute(extension.extendedType);
459+
// Casts aren't relevant in extension applicability.
460+
var typeSystemOperations =
461+
TypeSystemOperations(typeSystem, strictCasts: false);
462+
463+
inferenceLogWriter?.enterGenericInference(
464+
freshTypeParameters, rawExtendedType);
465+
var inferrer = GenericInferrer(
466+
typeSystem,
467+
freshTypeParameters,
468+
genericMetadataIsEnabled: genericMetadataIsEnabled,
469+
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled,
470+
strictInference: false,
471+
typeSystemOperations: typeSystemOperations,
472+
dataForTesting: null,
473+
);
474+
inferrer.constrainArgument(
475+
targetType,
476+
rawExtendedType,
477+
'extendedType',
478+
nodeForTesting: null,
479+
);
480+
var inferredTypes = inferrer.tryChooseFinalTypes();
481+
if (inferredTypes == null) {
482+
continue;
483+
}
484+
485+
var substitution = Substitution.fromPairs(
486+
extension.typeParameters,
487+
inferredTypes,
488+
);
489+
var extendedType = substitution.substituteType(
490+
extension.extendedType,
491+
);
492+
493+
if (!typeSystem.isSubtypeOf(targetType, extendedType)) {
494+
continue;
495+
}
496+
497+
instantiated.add(
498+
notInstantiated.instantiate(
499+
substitution: substitution,
500+
extendedType: extendedType,
501+
),
502+
);
503+
}
504+
505+
return instantiated;
506+
}
507+
}

0 commit comments

Comments
 (0)