Skip to content

Allow FunctionTypes to work in place of InterfaceType in applicability check #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/src/element_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ class TypeParameterElementType extends DefinedElementType {
String get nameWithGenerics => name;

@override
ClassElement get _boundClassElement => interfaceType.element;
ClassElement get _boundClassElement => type.element;

@override
InterfaceType get interfaceType => (type as TypeParameterType).bound;
// TODO(jcollins-g): This is wrong; bound is not always an InterfaceType.
InterfaceType get _interfaceType => (type as TypeParameterType).bound;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bound of a type parameter is not always InterfaceType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is just all messed up. Added a todo; will fix if it causes a real world crash.

}

/// An [ElementType] associated with an [Element].
Expand Down Expand Up @@ -271,18 +272,19 @@ abstract class DefinedElementType extends ElementType {
ClassElement get _boundClassElement => (element.element as ClassElement);
Class get boundClass =>
ModelElement.fromElement(_boundClassElement, packageGraph);
InterfaceType get interfaceType => type;

InterfaceType get _interfaceType => type;

InterfaceType _instantiatedType;

/// Return this type, instantiated to bounds if it isn't already.
DartType get instantiatedType {
if (_instantiatedType == null) {
if (!interfaceType.typeArguments.every((t) => t is InterfaceType)) {
if (!_interfaceType.typeArguments.every((t) => t is InterfaceType)) {
var typeSystem = library.element.typeSystem as TypeSystemImpl;
_instantiatedType = typeSystem.instantiateToBounds(interfaceType);
_instantiatedType = typeSystem.instantiateToBounds(_interfaceType);
} else {
_instantiatedType = interfaceType;
_instantiatedType = _interfaceType;
}
}
return _instantiatedType;
Expand Down Expand Up @@ -413,4 +415,7 @@ class CallableGenericTypeAliasElementType extends ParameterizedElementType
}
return _returnType;
}

@override
DartType get instantiatedType => type;
}
20 changes: 3 additions & 17 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/extension_target.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
import 'package:quiver/iterables.dart' as quiver;

class Class extends Container
with TypeParameters, Categorization
with TypeParameters, Categorization, ExtensionTarget
implements EnclosedElement {
List<DefinedElementType> mixins;
DefinedElementType supertype;
Expand Down Expand Up @@ -51,22 +52,6 @@ class Class extends Container
return _defaultConstructor;
}

bool get hasPotentiallyApplicableExtensions =>
potentiallyApplicableExtensions.isNotEmpty;

List<Extension> _potentiallyApplicableExtensions;

Iterable<Extension> get potentiallyApplicableExtensions {
if (_potentiallyApplicableExtensions == null) {
_potentiallyApplicableExtensions = model_utils
.filterNonDocumented(packageGraph.extensions)
.where((e) => e.couldApplyTo(this))
.toList(growable: false)
..sort(byName);
}
return _potentiallyApplicableExtensions;
}

Iterable<Method> get allInstanceMethods =>
quiver.concat([instanceMethods, inheritedMethods]);

Expand Down Expand Up @@ -223,6 +208,7 @@ class Class extends Container

bool get hasPublicMixins => publicMixins.isNotEmpty;

@override
bool get hasModifiers =>
hasPublicMixins ||
hasAnnotations ||
Expand Down
11 changes: 7 additions & 4 deletions lib/src/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/extension_target.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:quiver/iterables.dart' as quiver;

Expand All @@ -24,7 +25,8 @@ class Extension extends Container
ElementType.from(_extension.extendedType, library, packageGraph);
}

bool couldApplyTo(Class c) => _couldApplyTo(c.modelType);
bool couldApplyTo<T extends ExtensionTarget>(T c) =>
_couldApplyTo(c.modelType);

/// Return true if this extension could apply to [t].
bool _couldApplyTo(DefinedElementType t) {
Expand All @@ -40,13 +42,14 @@ class Extension extends Container
.isSubtypeOf(extendedType.instantiatedType, t.instantiatedType);

bool isBoundSupertypeTo(DefinedElementType t) =>
_isBoundSupertypeTo(t.type, HashSet());
_isBoundSupertypeTo(t.instantiatedType, HashSet());

/// Returns true if at least one supertype (including via mixins and
/// interfaces) is equivalent to or a subtype of [extendedType] when
/// instantiated to bounds.
bool _isBoundSupertypeTo(
InterfaceType superType, HashSet<InterfaceType> visited) {
bool _isBoundSupertypeTo(DartType superType, HashSet<DartType> visited) {
// Only InterfaceTypes can have superTypes.
if (superType is! InterfaceType) return false;
ClassElement superClass = superType?.element;
if (visited.contains(superType)) return false;
visited.add(superType);
Expand Down
26 changes: 26 additions & 0 deletions lib/src/model/extension_target.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:dartdoc/src/model/model.dart';

// TODO(jcollins-g): Mix-in ExtensionTarget on Method, ModelFunction, Typedef,
// and other possible documented symbols that could be extended (#2701).
mixin ExtensionTarget on ModelElement {
bool get hasModifiers;

bool get hasPotentiallyApplicableExtensions =>
potentiallyApplicableExtensions.isNotEmpty;

List<Extension> _potentiallyApplicableExtensions;

Iterable<Extension> get potentiallyApplicableExtensions {
if (_potentiallyApplicableExtensions == null) {
_potentiallyApplicableExtensions = packageGraph.documentedExtensions
.where((e) => e.couldApplyTo(this))
.toList(growable: false)
..sort(byName);
}
return _potentiallyApplicableExtensions;
}
}
10 changes: 9 additions & 1 deletion lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,21 @@ class PackageGraph {
return _implementors;
}

List<Extension> _documentedExtensions;
Iterable<Extension> get documentedExtensions {
if (_documentedExtensions == null) {
_documentedExtensions =
utils.filterNonDocumented(extensions).toList(growable: false);
}
return _documentedExtensions;
}

Iterable<Extension> get extensions {
assert(allExtensionsAdded);
return _extensions;
}

Map<String, Set<ModelElement>> _findRefElementCache;

Map<String, Set<ModelElement>> get findRefElementCache {
if (_findRefElementCache == null) {
assert(packageGraph.allLibrariesAdded);
Expand Down
15 changes: 15 additions & 0 deletions testing/test_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,18 @@ abstract class CanonicalPrivateInheritedToolUser
print('hello, tool world');
}
}

/*
* Complex extension methods case.
*
* TODO(jcollins-g): add unit tests around behavior when #2701 is implemented.
* Until #2701 is fixed we mostly are testing that we don't crash because
* DoSomething2X is declared.
*/

typedef R Function1<A, R>(A a);
typedef R Function2<A, B, R>(A a, B b);

extension DoSomething2X<A, B, R> on Function1<A, Function1<B, R>> {
Function2<A, B, R> something() => (A first, B second) => this(first)(second);
}