@@ -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+
100112abstract 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+
173200extension 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
248275extension 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