1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/NameLookup.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeCheckingRequests.h"
#include "swift/Subsystems.h"
#include "TypeChecker.h"
using namespace swift;
namespace swift {
// Implement the IDE type zone.
#define SWIFT_TYPEID_ZONE IDETypeChecking
#define SWIFT_TYPEID_HEADER "swift/Sema/IDETypeCheckingRequestIDZone.def"
#include "swift/Basic/ImplementTypeIDZone.h"
#undef SWIFT_TYPEID_ZONE
#undef SWIFT_TYPEID_HEADER
}
// Define request evaluation functions for each of the IDE type check requests.
static AbstractRequestFunction *ideTypeCheckRequestFunctions[] = {
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
#include "swift/Sema/IDETypeCheckingRequestIDZone.def"
#undef SWIFT_REQUEST
};
void swift::registerIDETypeCheckRequestFunctions(Evaluator &evaluator) {
evaluator.registerRequestFunctions(Zone::IDETypeChecking,
ideTypeCheckRequestFunctions);
}
/// Consider the following example
///
/// \code
/// protocol FontStyle {}
/// struct FontStyleOne: FontStyle {}
/// extension FontStyle where Self == FontStyleOne {
/// static var one: FontStyleOne { FontStyleOne() }
/// }
/// func foo<T: FontStyle>(x: T) {}
///
/// func case1() {
/// foo(x: .#^COMPLETE^#) // extension should be considered applied here
/// }
/// func case2<T: FontStyle>(x: T) {
/// x.#^COMPLETE_2^# // extension should not be considered applied here
/// }
/// \endcode
/// We want to consider the extension applied in the first case but not the
/// second case. In the first case the constraint `T: FontStyle` from the
/// definition of `foo` should be considered an 'at-least' constraint and any
/// additional constraints on `T` (like `T == FontStyleOne`) can be
/// fulfilled by picking a more specialized version of `T`.
/// However, in the second case, `T: FontStyle` should be considered an
/// 'at-most' constraint and we can't make the assumption that `x` has a more
/// specialized type.
///
/// After type-checking we cannot easily differentiate the two cases. In both
/// we have a unresolved dot completion on a primary archetype that
/// conforms to `FontStyle`.
///
/// To tell them apart, we apply the following heuristic: If the primary
/// archetype refers to a generic parameter that is not visible in the current
/// decl context (i.e. the current decl context is not a child context of the
/// parameter's decl context), it is not the type of a variable visible
/// in the current decl context. Hence, we must be in the first case and
/// consider all extensions applied, otherwise we should only consider those
/// extensions applied whose requirements are fulfilled.
class ContainsSpecializableArchetype : public TypeWalker {
const DeclContext *DC;
bool Result = false;
ContainsSpecializableArchetype(const DeclContext *DC) : DC(DC) {}
Action walkToTypePre(Type T) override {
if (auto *Archetype = T->getAs<ArchetypeType>()) {
if (auto *GenericTypeParam =
Archetype->mapTypeOutOfContext()->getAs<GenericTypeParamType>()) {
if (auto GenericTypeParamDecl = GenericTypeParam->getDecl()) {
bool ParamMaybeVisibleInCurrentContext =
(DC == GenericTypeParamDecl->getDeclContext() ||
DC->isChildContextOf(GenericTypeParamDecl->getDeclContext()));
if (!ParamMaybeVisibleInCurrentContext) {
Result = true;
return Action::Stop;
}
}
}
}
return Action::Continue;
}
public:
static bool check(const DeclContext *DC, Type T) {
if (!T->hasArchetype()) {
// Fast path, we don't have an archetype to check.
return false;
}
ContainsSpecializableArchetype Checker(DC);
T.walk(Checker);
return Checker.Result;
}
};
/// Returns `true` if `ED` is an extension that binds `Self` to a
/// concrete type, like `extension MyProto where Self == MyStruct {}`. The
/// protocol being extended must either be `PD`, or `Self` must be a type
/// that conforms to `PD`.
///
/// In these cases, it is possible to access static members defined in the
/// extension when perfoming unresolved member lookup in a type context of
/// `PD`.
static bool isExtensionWithSelfBound(const ExtensionDecl *ED,
ProtocolDecl *PD) {
if (!ED || !PD)
return false;
GenericSignature genericSig = ED->getGenericSignature();
Type selfType = genericSig->getConcreteType(ED->getSelfInterfaceType());
if (!selfType)
return false;
if (selfType->is<ExistentialType>())
return false;
auto *M = ED->getParentModule();
return ED->getExtendedNominal() == PD || M->checkConformance(selfType, PD);
}
static bool isExtensionAppliedInternal(const DeclContext *DC, Type BaseTy,
const ExtensionDecl *ED) {
// We can't do anything if the base type has unbound generic parameters.
// We can't leak type variables into another constraint system.
// For check on specializable archetype see comment on
// ContainsSpecializableArchetype.
if (BaseTy->hasTypeVariable() || BaseTy->hasUnboundGenericType() ||
BaseTy->hasUnresolvedType() || BaseTy->hasError() ||
ContainsSpecializableArchetype::check(DC, BaseTy))
return true;
if (!ED->isConstrainedExtension())
return true;
ProtocolDecl *BaseTypeProtocolDecl = nullptr;
if (auto opaqueType = dyn_cast<OpaqueTypeArchetypeType>(BaseTy)) {
if (opaqueType->getConformsTo().size() == 1) {
BaseTypeProtocolDecl = opaqueType->getConformsTo().front();
}
} else {
BaseTypeProtocolDecl = dyn_cast_or_null<ProtocolDecl>(BaseTy->getAnyNominal());
}
if (isExtensionWithSelfBound(ED, BaseTypeProtocolDecl)) {
return true;
}
auto *module = DC->getParentModule();
GenericSignature genericSig = ED->getGenericSignature();
SubstitutionMap substMap = BaseTy->getContextSubstitutionMap(
module, ED->getExtendedNominal());
return checkRequirements(module,
genericSig.getRequirements(),
QuerySubstitutionMap{substMap}) ==
CheckRequirementsResult::Success;
}
static bool isMemberDeclAppliedInternal(const DeclContext *DC, Type BaseTy,
const ValueDecl *VD) {
if (BaseTy->isExistentialType() && VD->isStatic() &&
!isExtensionWithSelfBound(
dyn_cast<ExtensionDecl>(VD->getDeclContext()),
dyn_cast_or_null<ProtocolDecl>(BaseTy->getAnyNominal())))
return false;
// We can't leak type variables into another constraint system.
// We can't do anything if the base type has unbound generic parameters.
if (BaseTy->hasTypeVariable() || BaseTy->hasUnboundGenericType()||
BaseTy->hasUnresolvedType() || BaseTy->hasError())
return true;
if (isa<TypeAliasDecl>(VD) && BaseTy->is<ProtocolType>()) {
// The protocol doesn't satisfy its own generic signature (static members
// of the protocol are not visible on the protocol itself) but we can still
// access typealias declarations on it.
return true;
}
const GenericContext *genericDecl = VD->getAsGenericContext();
if (!genericDecl)
return true;
// The declaration may introduce inner generic parameters and requirements,
// or it may be nested in an outer generic context.
GenericSignature genericSig = genericDecl->getGenericSignature();
if (!genericSig)
return true;
// The context substitution map for the base type fixes the declaration's
// outer generic parameters.
auto *module = DC->getParentModule();
auto substMap = BaseTy->getContextSubstitutionMap(
module, VD->getDeclContext(), genericDecl->getGenericEnvironment());
// The innermost generic parameters are mapped to error types.
unsigned innerDepth = genericSig.getGenericParams().back()->getDepth();
if (!genericDecl->isGeneric())
++innerDepth;
// We treat substitution failure as success, to ignore requirements
// that involve innermost generic parameters.
return checkRequirements(module,
genericSig.getRequirements(),
[&](SubstitutableType *type) -> Type {
auto *paramTy = cast<GenericTypeParamType>(type);
if (paramTy->getDepth() == innerDepth)
return ErrorType::get(DC->getASTContext());
return Type(paramTy).subst(substMap);
}) != CheckRequirementsResult::RequirementFailure;
}
bool
IsDeclApplicableRequest::evaluate(Evaluator &evaluator,
DeclApplicabilityOwner Owner) const {
if (auto *VD = dyn_cast<ValueDecl>(Owner.ExtensionOrMember)) {
return isMemberDeclAppliedInternal(Owner.DC, Owner.Ty, VD);
} else if (auto *ED = dyn_cast<ExtensionDecl>(Owner.ExtensionOrMember)) {
return isExtensionAppliedInternal(Owner.DC, Owner.Ty, ED);
} else {
llvm_unreachable("unhandled decl kind");
}
}
bool
TypeRelationCheckRequest::evaluate(Evaluator &evaluator,
TypeRelationCheckInput Owner) const {
using namespace constraints;
std::optional<ConstraintKind> CKind;
switch (Owner.Relation) {
case TypeRelation::ConvertTo:
CKind = ConstraintKind::Conversion;
break;
case TypeRelation::SubtypeOf:
CKind = ConstraintKind::Subtype;
break;
}
assert(CKind.has_value());
return TypeChecker::typesSatisfyConstraint(Owner.Pair.FirstTy,
Owner.Pair.SecondTy,
Owner.OpenArchetypes,
*CKind, Owner.DC);
}
TypePair
RootAndResultTypeOfKeypathDynamicMemberRequest::evaluate(Evaluator &evaluator,
SubscriptDecl *subscript) const {
if (!isValidKeyPathDynamicMemberLookup(subscript))
return TypePair();
const auto *param = subscript->getIndices()->get(0);
auto keyPathType = param->getTypeInContext()->getAs<BoundGenericType>();
if (!keyPathType)
return TypePair();
auto genericArgs = keyPathType->getGenericArgs();
assert(!genericArgs.empty() && genericArgs.size() == 2 &&
"invalid keypath dynamic member");
return TypePair(genericArgs[0], genericArgs[1]);
}
|