|
| 1 | +//===--- TypeCheckConcurrency.cpp - Concurrency ---------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file implements type checking support for Swift's concurrency model. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +#include "TypeChecker.h" |
| 17 | +#include "swift/AST/ParameterList.h" |
| 18 | +#include "swift/AST/ProtocolConformance.h" |
| 19 | +#include "swift/AST/TypeCheckRequests.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | + |
| 23 | +/// Check whether the @asyncHandler attribute can be applied to the given |
| 24 | +/// function declaration. |
| 25 | +/// |
| 26 | +/// \param diagnose Whether to emit a diagnostic when a problem is encountered. |
| 27 | +/// |
| 28 | +/// \returns \c true if there was a problem with adding the attribute, \c false |
| 29 | +/// otherwise. |
| 30 | +static bool checkAsyncHandler(FuncDecl *func, bool diagnose) { |
| 31 | + if (!func->getResultInterfaceType()->isVoid()) { |
| 32 | + if (diagnose) { |
| 33 | + func->diagnose(diag::asynchandler_returns_value) |
| 34 | + .highlight(func->getBodyResultTypeLoc().getSourceRange()); |
| 35 | + } |
| 36 | + |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + if (func->hasThrows()) { |
| 41 | + if (diagnose) { |
| 42 | + func->diagnose(diag::asynchandler_throws) |
| 43 | + .fixItRemove(func->getThrowsLoc()); |
| 44 | + } |
| 45 | + |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + if (func->hasAsync()) { |
| 50 | + if (diagnose) { |
| 51 | + func->diagnose(diag::asynchandler_async) |
| 52 | + .fixItRemove(func->getAsyncLoc()); |
| 53 | + } |
| 54 | + |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + for (auto param : *func->getParameters()) { |
| 59 | + if (param->isInOut()) { |
| 60 | + if (diagnose) { |
| 61 | + param->diagnose(diag::asynchandler_inout_parameter) |
| 62 | + .fixItRemove(param->getSpecifierLoc()); |
| 63 | + } |
| 64 | + |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (func->isMutating()) { |
| 70 | + if (diagnose) { |
| 71 | + auto diag = func->diagnose(diag::asynchandler_mutating); |
| 72 | + if (auto mutatingAttr = func->getAttrs().getAttribute<MutatingAttr>()) { |
| 73 | + diag.fixItRemove(mutatingAttr->getRange()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +void swift::addAsyncNotes(FuncDecl *func) { |
| 84 | + func->diagnose(diag::note_add_async_to_function, func->getName()); |
| 85 | + |
| 86 | + if (!checkAsyncHandler(func, /*diagnose=*/false)) { |
| 87 | + func->diagnose( |
| 88 | + diag::note_add_asynchandler_to_function, func->getName()) |
| 89 | + .fixItInsert(func->getAttributeInsertionLoc(false), "@asyncHandler "); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +bool IsAsyncHandlerRequest::evaluate( |
| 94 | + Evaluator &evaluator, FuncDecl *func) const { |
| 95 | + // Check whether the attribute was explicitly specified. |
| 96 | + if (auto attr = func->getAttrs().getAttribute<AsyncHandlerAttr>()) { |
| 97 | + // Check for well-formedness. |
| 98 | + if (checkAsyncHandler(func, /*diagnose=*/true)) { |
| 99 | + attr->setInvalid(); |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + // Are we in a context where inference is possible? |
| 107 | + auto dc = func->getDeclContext(); |
| 108 | + if (!dc->isTypeContext() || !dc->getParentSourceFile() || |
| 109 | + isa<ProtocolDecl>(dc) || !func->hasBody()) |
| 110 | + return false; |
| 111 | + |
| 112 | + // Is it possible to infer @asyncHandler for this function at all? |
| 113 | + if (checkAsyncHandler(func, /*diagnose=*/false)) |
| 114 | + return false; |
| 115 | + |
| 116 | + // Add an implicit @asyncHandler attribute and return true. We're done. |
| 117 | + auto addImplicitAsyncHandlerAttr = [&] { |
| 118 | + func->getAttrs().add(new (func->getASTContext()) AsyncHandlerAttr(true)); |
| 119 | + return true; |
| 120 | + }; |
| 121 | + |
| 122 | + // Check whether any of the conformances in the context of the function |
| 123 | + // implies @asyncHandler. |
| 124 | + { |
| 125 | + auto idc = cast<IterableDeclContext>(dc->getAsDecl()); |
| 126 | + auto conformances = evaluateOrDefault( |
| 127 | + dc->getASTContext().evaluator, |
| 128 | + LookupAllConformancesInContextRequest{idc}, { }); |
| 129 | + |
| 130 | + for (auto conformance : conformances) { |
| 131 | + auto protocol = conformance->getProtocol(); |
| 132 | + for (auto found : protocol->lookupDirect(func->getName())) { |
| 133 | + if (!isa<ProtocolDecl>(found->getDeclContext())) |
| 134 | + continue; |
| 135 | + |
| 136 | + auto requirement = dyn_cast<FuncDecl>(found); |
| 137 | + if (!requirement) |
| 138 | + continue; |
| 139 | + |
| 140 | + if (!requirement->isAsyncHandler()) |
| 141 | + continue; |
| 142 | + |
| 143 | + auto witness = conformance->getWitnessDecl(requirement); |
| 144 | + if (witness != func) |
| 145 | + continue; |
| 146 | + |
| 147 | + return addImplicitAsyncHandlerAttr(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Look through dynamic replacements. |
| 153 | + if (auto replaced = func->getDynamicallyReplacedDecl()) { |
| 154 | + if (auto replacedFunc = dyn_cast<FuncDecl>(replaced)) |
| 155 | + if (replacedFunc->isAsyncHandler()) |
| 156 | + return addImplicitAsyncHandlerAttr(); |
| 157 | + } |
| 158 | + |
| 159 | + return false; |
| 160 | +} |
0 commit comments