Skip to content

Commit 15839ff

Browse files
Merge pull request #42049 from AnthonyLatsis/dyn-self-inherit
findGenericParameterReferences: Do better at honoring 'treatNonResultCovarianceAsInvariant' and 'hasCovariantSelfResult'
2 parents b99df9e + def23f4 commit 15839ff

File tree

4 files changed

+184
-133
lines changed

4 files changed

+184
-133
lines changed

include/swift/AST/Decl.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,9 @@ class GenericParameterReferenceInfo final {
21712171
using OptionalTypePosition = OptionalEnum<decltype(TypePosition::Covariant)>;
21722172

21732173
public:
2174+
/// Whether the uncurried interface type of the declaration, stipped of any
2175+
/// optionality, is a direct reference to the generic parameter at hand. For
2176+
/// example, "func foo(x: Int) -> () -> Self?" has a covariant 'Self' result.
21742177
bool hasCovariantSelfResult;
21752178

21762179
OptionalTypePosition selfRef;
@@ -2181,6 +2184,12 @@ class GenericParameterReferenceInfo final {
21812184
return GenericParameterReferenceInfo(false, position, llvm::None);
21822185
}
21832186

2187+
/// A reference to the generic parameter in covariant result position.
2188+
static GenericParameterReferenceInfo forCovariantResult() {
2189+
return GenericParameterReferenceInfo(true, TypePosition::Covariant,
2190+
llvm::None);
2191+
}
2192+
21842193
/// A reference to 'Self' through an associated type.
21852194
static GenericParameterReferenceInfo forAssocTypeRef(TypePosition position) {
21862195
return GenericParameterReferenceInfo(false, llvm::None, position);
@@ -2681,13 +2690,14 @@ class ValueDecl : public Decl {
26812690
/// that this declaration dynamically replaces.
26822691
ValueDecl *getDynamicallyReplacedDecl() const;
26832692

2684-
/// Report 'Self' references within the type of this declaration as a
2685-
/// member of the given existential base type.
2693+
/// Find references to 'Self' in the type signature of this declaration in the
2694+
/// context of the given existential base type.
26862695
///
2687-
/// \param treatNonResultCovariantSelfAsInvariant If true, 'Self' or 'Self?'
2688-
/// is considered covariant only when it appears as the immediate type of a
2689-
/// property, or the uncurried result type of a method/subscript, e.g.
2690-
/// '() -> () -> Self'.
2696+
/// \param treatNonResultCovariantSelfAsInvariant When set, covariant 'Self'
2697+
/// references that are not in covariant result type position are considered
2698+
/// invariant. This position is the uncurried interface type of a declaration,
2699+
/// stipped of any optionality. For example, this is true for 'Self' in
2700+
/// 'func foo(Int) -> () -> Self?'.
26912701
GenericParameterReferenceInfo findExistentialSelfReferences(
26922702
Type baseTy, bool treatNonResultCovariantSelfAsInvariant) const;
26932703
};
@@ -7948,8 +7958,11 @@ class MissingMemberDecl : public Decl {
79487958
}
79497959
};
79507960

7951-
/// Find references to the given generic paramaeter in the generic signature
7952-
/// and the type of the given value.
7961+
/// Find references to the given generic paramater in the type signature of the
7962+
/// given declaration using the given generic signature.
7963+
///
7964+
/// \param skipParamIndex If the value is a function or subscript declaration,
7965+
/// specifies the index of the parameter that shall be skipped.
79537966
GenericParameterReferenceInfo findGenericParameterReferences(
79547967
const ValueDecl *value,
79557968
CanGenericSignature sig, GenericTypeParamType *genericParam,

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
689689
bool canDynamicallyBeOptionalType(bool includeExistential);
690690

691691
/// Determine whether this type contains a type parameter somewhere in it.
692-
bool hasTypeParameter() {
692+
bool hasTypeParameter() const {
693693
return getRecursiveProperties().hasTypeParameter();
694694
}
695695

0 commit comments

Comments
 (0)