|
21 | 21 | #include "swift/AST/DiagnosticsSema.h"
|
22 | 22 | #include "swift/AST/Effects.h"
|
23 | 23 | #include "swift/AST/Initializer.h"
|
| 24 | +#include "swift/AST/ParameterList.h" |
24 | 25 | #include "swift/AST/Pattern.h"
|
25 | 26 | #include "swift/AST/PrettyStackTrace.h"
|
26 | 27 | #include "swift/AST/ProtocolConformance.h"
|
| 28 | +#include "swift/AST/TypeCheckRequests.h" |
27 | 29 |
|
28 | 30 | using namespace swift;
|
29 | 31 |
|
| 32 | +static bool hasThrowingFunctionClosureParameter(CanType type) { |
| 33 | + // Only consider throwing function types. |
| 34 | + if (auto fnType = dyn_cast<AnyFunctionType>(type)) { |
| 35 | + return fnType->getExtInfo().isThrowing(); |
| 36 | + } |
| 37 | + |
| 38 | + // Look through tuples. |
| 39 | + if (auto tuple = dyn_cast<TupleType>(type)) { |
| 40 | + for (auto eltType : tuple.getElementTypes()) { |
| 41 | + auto elt = eltType->lookThroughAllOptionalTypes()->getCanonicalType(); |
| 42 | + if (hasThrowingFunctionClosureParameter(elt)) |
| 43 | + return true; |
| 44 | + } |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + // Suppress diagnostics in the presence of errors. |
| 49 | + if (type->hasError()) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +static FunctionRethrowingKind |
| 57 | +getTypeThrowingKind(Type interfaceTy, GenericSignature genericSig) { |
| 58 | + if (interfaceTy->isTypeParameter()) { |
| 59 | + for (auto proto : genericSig->getRequiredProtocols(interfaceTy)) { |
| 60 | + if (proto->isRethrowingProtocol()) { |
| 61 | + return FunctionRethrowingKind::ByConformance; |
| 62 | + } |
| 63 | + } |
| 64 | + } else if (auto NTD = interfaceTy->getNominalOrBoundGenericNominal()) { |
| 65 | + if (auto genericSig = NTD->getGenericSignature()) { |
| 66 | + for (auto req : genericSig->getRequirements()) { |
| 67 | + if (req.getKind() == RequirementKind::Conformance) { |
| 68 | + if (req.getSecondType()->castTo<ProtocolType>() |
| 69 | + ->getDecl() |
| 70 | + ->isRethrowingProtocol()) { |
| 71 | + return FunctionRethrowingKind::ByConformance; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return FunctionRethrowingKind::Invalid; |
| 78 | +} |
| 79 | + |
| 80 | +static FunctionRethrowingKind |
| 81 | +getParameterThrowingKind(AbstractFunctionDecl *decl, |
| 82 | + GenericSignature genericSig) { |
| 83 | + FunctionRethrowingKind kind = FunctionRethrowingKind::Invalid; |
| 84 | + // check all parameters to determine if any are closures that throw |
| 85 | + bool foundThrowingClosure = false; |
| 86 | + for (auto param : *decl->getParameters()) { |
| 87 | + auto interfaceTy = param->getInterfaceType(); |
| 88 | + if (hasThrowingFunctionClosureParameter(interfaceTy |
| 89 | + ->lookThroughAllOptionalTypes() |
| 90 | + ->getCanonicalType())) { |
| 91 | + foundThrowingClosure = true; |
| 92 | + } |
| 93 | + |
| 94 | + if (kind == FunctionRethrowingKind::Invalid) { |
| 95 | + kind = getTypeThrowingKind(interfaceTy, genericSig); |
| 96 | + } |
| 97 | + } |
| 98 | + if (kind == FunctionRethrowingKind::Invalid && |
| 99 | + foundThrowingClosure) { |
| 100 | + return FunctionRethrowingKind::ByClosure; |
| 101 | + } |
| 102 | + return kind; |
| 103 | +} |
| 104 | + |
| 105 | +ProtocolRethrowsRequirementList |
| 106 | +ProtocolRethrowsRequirementsRequest::evaluate(Evaluator &evaluator, |
| 107 | + ProtocolDecl *decl) const { |
| 108 | + SmallVector<std::pair<Type, ValueDecl*>, 2> found; |
| 109 | + llvm::DenseSet<ProtocolDecl*> checkedProtocols; |
| 110 | + |
| 111 | + ASTContext &ctx = decl->getASTContext(); |
| 112 | + |
| 113 | + // only allow rethrowing requirements to be determined from marked protocols |
| 114 | + if (!decl->getAttrs().hasAttribute<swift::AtRethrowsAttr>()) { |
| 115 | + return ProtocolRethrowsRequirementList(ctx.AllocateCopy(found)); |
| 116 | + } |
| 117 | + |
| 118 | + // check if immediate members of protocol are 'rethrows' |
| 119 | + for (auto member : decl->getMembers()) { |
| 120 | + auto fnDecl = dyn_cast<AbstractFunctionDecl>(member); |
| 121 | + // it must be a function |
| 122 | + // it must have a rethrows attribute |
| 123 | + // it must not have any parameters that are closures that cause rethrowing |
| 124 | + if (!fnDecl || |
| 125 | + !fnDecl->hasThrows()) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + GenericSignature genericSig = fnDecl->getGenericSignature(); |
| 130 | + auto kind = getParameterThrowingKind(fnDecl, genericSig); |
| 131 | + // skip closure based rethrowing cases |
| 132 | + if (kind == FunctionRethrowingKind::ByClosure) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + // we now have a protocol member that has a rethrows and no closure |
| 136 | + // parameters contributing to it's rethrowing-ness |
| 137 | + found.push_back( |
| 138 | + std::pair<Type, ValueDecl*>(decl->getSelfInterfaceType(), fnDecl)); |
| 139 | + } |
| 140 | + checkedProtocols.insert(decl); |
| 141 | + |
| 142 | + // check associated conformances of associated types or inheritance |
| 143 | + for (auto requirement : decl->getRequirementSignature()) { |
| 144 | + if (requirement.getKind() != RequirementKind::Conformance) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + auto protoTy = requirement.getSecondType()->castTo<ProtocolType>(); |
| 148 | + auto proto = protoTy->getDecl(); |
| 149 | + if (checkedProtocols.count(proto) != 0) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + checkedProtocols.insert(proto); |
| 153 | + for (auto entry : proto->getRethrowingRequirements()) { |
| 154 | + found.emplace_back(requirement.getFirstType(), entry.second); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return ProtocolRethrowsRequirementList(ctx.AllocateCopy(found)); |
| 159 | +} |
| 160 | + |
| 161 | +FunctionRethrowingKind |
| 162 | +FunctionRethrowingKindRequest::evaluate(Evaluator &evaluator, |
| 163 | + AbstractFunctionDecl *decl) const { |
| 164 | + if (decl->hasThrows()) { |
| 165 | + auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext()); |
| 166 | + bool fromRethrow = proto != nullptr ? proto->isRethrowingProtocol() : false; |
| 167 | + bool markedRethrows = decl->getAttrs().hasAttribute<RethrowsAttr>(); |
| 168 | + if (fromRethrow && !markedRethrows) { |
| 169 | + return FunctionRethrowingKind::ByConformance; |
| 170 | + } |
| 171 | + if (markedRethrows) { |
| 172 | + GenericSignature genericSig = decl->getGenericSignature(); |
| 173 | + FunctionRethrowingKind kind = getParameterThrowingKind(decl, genericSig); |
| 174 | + // since we have checked all arguments, if we still havent found anything |
| 175 | + // check the self parameter |
| 176 | + if (kind == FunctionRethrowingKind::Invalid && |
| 177 | + decl->hasImplicitSelfDecl()) { |
| 178 | + auto selfParam = decl->getImplicitSelfDecl(); |
| 179 | + if (selfParam) { |
| 180 | + auto interfaceTy = selfParam->getInterfaceType(); |
| 181 | + kind = getTypeThrowingKind(interfaceTy, genericSig); |
| 182 | + } |
| 183 | + } |
| 184 | + return kind; |
| 185 | + } |
| 186 | + return FunctionRethrowingKind::Throws; |
| 187 | + } |
| 188 | + return FunctionRethrowingKind::None; |
| 189 | +} |
| 190 | + |
30 | 191 | namespace {
|
31 | 192 |
|
32 | 193 | /// A function reference.
|
|
0 commit comments