|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/element/element.dart'; |
| 6 | +import 'package:analyzer/dart/element/type.dart'; |
| 7 | +import 'package:analyzer/src/dart/element/type_algebra.dart'; |
| 8 | +import 'package:analyzer/src/dart/element/type_system.dart'; |
| 9 | + |
| 10 | +class NotWellBoundedTypeResult implements TypeBoundedResult { |
| 11 | + final String elementName; |
| 12 | + final List<TypeArgumentIssue> issues; |
| 13 | + |
| 14 | + NotWellBoundedTypeResult._({ |
| 15 | + required this.elementName, |
| 16 | + required this.issues, |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +class RegularBoundedTypeResult implements WellBoundedTypeResult { |
| 21 | + const RegularBoundedTypeResult._(); |
| 22 | +} |
| 23 | + |
| 24 | +class SuperBoundedTypeResult implements WellBoundedTypeResult { |
| 25 | + const SuperBoundedTypeResult._(); |
| 26 | +} |
| 27 | + |
| 28 | +class TypeArgumentIssue { |
| 29 | + /// The index for type argument within the passed type arguments. |
| 30 | + final int index; |
| 31 | + |
| 32 | + /// The type parameter with the bound that was violated. |
| 33 | + final TypeParameterElement parameter; |
| 34 | + |
| 35 | + /// The substituted bound of the [parameter]. |
| 36 | + final DartType parameterBound; |
| 37 | + |
| 38 | + /// The type argument that violated the [parameterBound]. |
| 39 | + final DartType argument; |
| 40 | + |
| 41 | + TypeArgumentIssue( |
| 42 | + this.index, |
| 43 | + this.parameter, |
| 44 | + this.parameterBound, |
| 45 | + this.argument, |
| 46 | + ); |
| 47 | + |
| 48 | + @override |
| 49 | + String toString() { |
| 50 | + return 'TypeArgumentIssue(index=$index, parameter=$parameter, ' |
| 51 | + 'parameterBound=$parameterBound, argument=$argument)'; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Helper for checking whether a type if well-bounded. |
| 56 | +/// |
| 57 | +/// See `15.2 Super-bounded types` in the language specification. |
| 58 | +class TypeBoundedHelper { |
| 59 | + final TypeSystemImpl typeSystem; |
| 60 | + |
| 61 | + TypeBoundedHelper(this.typeSystem); |
| 62 | + |
| 63 | + TypeBoundedResult isWellBounded( |
| 64 | + DartType type, { |
| 65 | + required bool allowSuperBounded, |
| 66 | + }) { |
| 67 | + var result = _isRegularBounded(type); |
| 68 | + if (!allowSuperBounded) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + return _isSuperBounded(type); |
| 73 | + } |
| 74 | + |
| 75 | + TypeBoundedResult _isRegularBounded(DartType type) { |
| 76 | + List<TypeArgumentIssue>? issues; |
| 77 | + |
| 78 | + final String elementName; |
| 79 | + final List<TypeParameterElement> typeParameters; |
| 80 | + final List<DartType> typeArguments; |
| 81 | + final alias = type.alias; |
| 82 | + if (alias != null) { |
| 83 | + elementName = alias.element.name; |
| 84 | + typeParameters = alias.element.typeParameters; |
| 85 | + typeArguments = alias.typeArguments; |
| 86 | + } else if (type is InterfaceType) { |
| 87 | + elementName = type.element.name; |
| 88 | + typeParameters = type.element.typeParameters; |
| 89 | + typeArguments = type.typeArguments; |
| 90 | + } else { |
| 91 | + return const RegularBoundedTypeResult._(); |
| 92 | + } |
| 93 | + |
| 94 | + final substitution = Substitution.fromPairs(typeParameters, typeArguments); |
| 95 | + for (var i = 0; i < typeParameters.length; i++) { |
| 96 | + var typeParameter = typeParameters[i]; |
| 97 | + var typeArgument = typeArguments[i]; |
| 98 | + |
| 99 | + var bound = typeParameter.bound; |
| 100 | + if (bound == null) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + bound = typeSystem.toLegacyType(bound); |
| 105 | + bound = substitution.substituteType(bound); |
| 106 | + |
| 107 | + if (!typeSystem.isSubtypeOf(typeArgument, bound)) { |
| 108 | + issues ??= <TypeArgumentIssue>[]; |
| 109 | + issues.add( |
| 110 | + TypeArgumentIssue(i, typeParameter, bound, typeArgument), |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (issues == null) { |
| 116 | + return const RegularBoundedTypeResult._(); |
| 117 | + } else { |
| 118 | + return NotWellBoundedTypeResult._( |
| 119 | + elementName: elementName, |
| 120 | + issues: issues, |
| 121 | + ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + TypeBoundedResult _isSuperBounded(DartType type) { |
| 126 | + final invertedType = typeSystem.replaceTopAndBottom(type); |
| 127 | + var result = _isRegularBounded(invertedType); |
| 128 | + if (result is RegularBoundedTypeResult) { |
| 129 | + return const SuperBoundedTypeResult._(); |
| 130 | + } else { |
| 131 | + return result; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +abstract class TypeBoundedResult {} |
| 137 | + |
| 138 | +class WellBoundedTypeResult implements TypeBoundedResult {} |
0 commit comments