-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathLifetimeDependence.cpp
444 lines (411 loc) · 15.6 KB
/
LifetimeDependence.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//===--- LifetimeDependence.cpp -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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 "TypeChecker.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/LifetimeDependence.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Defer.h"
namespace swift {
std::string LifetimeDependenceInfo::getString() const {
std::string lifetimeDependenceString;
auto getOnIndices = [](IndexSubset *bitvector) {
std::string result;
bool isFirstSetBit = true;
for (unsigned i = 0; i < bitvector->getCapacity(); i++) {
if (bitvector->contains(i)) {
if (!isFirstSetBit) {
result += ", ";
}
result += std::to_string(i);
isFirstSetBit = false;
}
}
return result;
};
if (inheritLifetimeParamIndices && !inheritLifetimeParamIndices->isEmpty()) {
lifetimeDependenceString =
"_inherit(" + getOnIndices(inheritLifetimeParamIndices) + ")";
}
if (scopeLifetimeParamIndices && !scopeLifetimeParamIndices->isEmpty()) {
lifetimeDependenceString +=
"_scope(" + getOnIndices(scopeLifetimeParamIndices) + ")";
}
return lifetimeDependenceString;
}
void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
if (inheritLifetimeParamIndices) {
// Copy and Consume are the same, can be unified if we converge on dependsOn
// syntax
ID.AddInteger((uint8_t)LifetimeDependenceKind::Copy);
inheritLifetimeParamIndices->Profile(ID);
}
if (scopeLifetimeParamIndices) {
// Borrow and Mutate are the same, can be unified if we converge on
// dependsOn syntax
ID.AddInteger((uint8_t)LifetimeDependenceKind::Borrow);
scopeLifetimeParamIndices->Profile(ID);
}
}
LifetimeDependenceInfo LifetimeDependenceInfo::getForParamIndex(
AbstractFunctionDecl *afd, unsigned index, ValueOwnership ownership) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
unsigned capacity = afd->getParameters()->size() + 1;
auto indexSubset = IndexSubset::get(ctx, capacity, {index});
if (ownership == ValueOwnership::Owned) {
return LifetimeDependenceInfo{/*inheritLifetimeParamIndices*/ indexSubset,
/*scopeLifetimeParamIndices*/ nullptr};
}
assert(ownership == ValueOwnership::Shared ||
ownership == ValueOwnership::InOut);
return LifetimeDependenceInfo{/*inheritLifetimeParamIndices*/ nullptr,
/*scopeLifetimeParamIndices*/ indexSubset};
}
void LifetimeDependenceInfo::getConcatenatedData(
SmallVectorImpl<bool> &concatenatedData) const {
auto pushData = [&](IndexSubset *paramIndices) {
if (paramIndices == nullptr) {
return;
}
assert(!paramIndices->isEmpty());
for (unsigned i = 0; i < paramIndices->getCapacity(); i++) {
if (paramIndices->contains(i)) {
concatenatedData.push_back(true);
continue;
}
concatenatedData.push_back(false);
}
};
if (hasInheritLifetimeParamIndices()) {
pushData(inheritLifetimeParamIndices);
}
if (hasScopeLifetimeParamIndices()) {
pushData(scopeLifetimeParamIndices);
}
}
static bool hasEscapableResultOrYield(AbstractFunctionDecl *afd,
Type resultType) {
Type resultTypeInContext = afd->mapTypeIntoContext(resultType);
std::optional<Type> yieldTyInContext;
if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
if (accessor->isCoroutine()) {
yieldTyInContext = accessor->getStorage()->getValueInterfaceType();
yieldTyInContext = accessor->mapTypeIntoContext(*yieldTyInContext);
}
}
if (resultTypeInContext->isEscapable() &&
(!yieldTyInContext.has_value() || (*yieldTyInContext)->isEscapable())) {
return true;
}
return false;
}
std::optional<LifetimeDependenceInfo>
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
bool allowIndex) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
auto *mod = afd->getModuleContext();
auto &diags = ctx.Diags;
auto capacity = afd->getParameters()->size() + 1;
auto lifetimeDependentRepr =
cast<LifetimeDependentReturnTypeRepr>(afd->getResultTypeRepr());
if (hasEscapableResultOrYield(afd, resultType)) {
diags.diagnose(lifetimeDependentRepr->getLoc(),
diag::lifetime_dependence_invalid_return_type);
return std::nullopt;
}
SmallBitVector inheritLifetimeParamIndices(capacity);
SmallBitVector scopeLifetimeParamIndices(capacity);
auto updateLifetimeDependenceInfo = [&](LifetimeDependenceSpecifier specifier,
unsigned paramIndexToSet, Type type,
ValueOwnership ownership) {
auto loc = specifier.getLoc();
auto kind = specifier.getLifetimeDependenceKind();
if (ownership == ValueOwnership::Default) {
diags.diagnose(loc, diag::lifetime_dependence_missing_ownership_modifier);
return true;
}
if (kind == LifetimeDependenceKind::Borrow &&
ownership != ValueOwnership::Shared) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "borrow",
getOwnershipSpelling(ownership));
return true;
}
if (kind == LifetimeDependenceKind::Mutate &&
ownership != ValueOwnership::InOut) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "mutate",
getOwnershipSpelling(ownership));
return true;
}
if (kind == LifetimeDependenceKind::Consume &&
ownership != ValueOwnership::Owned) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "consume",
getOwnershipSpelling(ownership));
return true;
}
if (kind == LifetimeDependenceKind::Consume ||
kind == LifetimeDependenceKind::Copy) {
if (type->isEscapable()) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_infer,
specifier.getLifetimeDependenceKindString());
return true;
}
}
// Diagnose when we have lifetime dependence on a type that is
// BitwiseCopyable & Escapable.
// ~Escapable types are non-trivial in SIL and we should not raise this
// error.
// TODO: Diagnose ~Escapable types are always non-trivial in SIL.
if (type->isEscapable()) {
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
auto *bitwiseCopyableProtocol =
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
if (bitwiseCopyableProtocol &&
mod->checkConformance(type, bitwiseCopyableProtocol)) {
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
return true;
}
}
}
if (inheritLifetimeParamIndices.test(paramIndexToSet) ||
scopeLifetimeParamIndices.test(paramIndexToSet)) {
diags.diagnose(loc, diag::lifetime_dependence_duplicate_param_id);
return true;
}
if (kind == LifetimeDependenceKind::Copy ||
kind == LifetimeDependenceKind::Consume) {
inheritLifetimeParamIndices.set(paramIndexToSet);
} else {
assert(kind == LifetimeDependenceKind::Borrow ||
kind == LifetimeDependenceKind::Mutate);
scopeLifetimeParamIndices.set(paramIndexToSet);
}
return false;
};
for (auto specifier : lifetimeDependentRepr->getLifetimeDependencies()) {
switch (specifier.getSpecifierKind()) {
case LifetimeDependenceSpecifier::SpecifierKind::Named: {
bool foundParamName = false;
unsigned paramIndex = 0;
for (auto *param : *afd->getParameters()) {
if (param->getParameterName() == specifier.getName()) {
foundParamName = true;
if (updateLifetimeDependenceInfo(
specifier, paramIndex + 1,
afd->mapTypeIntoContext(
param->toFunctionParam().getParameterType()),
param->getValueOwnership())) {
return std::nullopt;
}
break;
}
paramIndex++;
}
if (!foundParamName) {
diags.diagnose(specifier.getLoc(),
diag::lifetime_dependence_invalid_param_name,
specifier.getName());
return std::nullopt;
}
break;
}
case LifetimeDependenceSpecifier::SpecifierKind::Ordered: {
auto index = specifier.getIndex();
if (index > afd->getParameters()->size()) {
diags.diagnose(specifier.getLoc(),
diag::lifetime_dependence_invalid_param_index, index);
return std::nullopt;
}
if (index != 0) {
auto param = afd->getParameters()->get(index - 1);
auto ownership = param->getValueOwnership();
auto type = afd->mapTypeIntoContext(
param->toFunctionParam().getParameterType());
if (updateLifetimeDependenceInfo(specifier, index, type, ownership)) {
return std::nullopt;
}
break;
}
LLVM_FALLTHROUGH;
}
case LifetimeDependenceSpecifier::SpecifierKind::Self: {
if (!afd->hasImplicitSelfDecl()) {
diags.diagnose(specifier.getLoc(),
diag::lifetime_dependence_invalid_self);
return std::nullopt;
}
if (updateLifetimeDependenceInfo(
specifier, /*selfIndex*/ 0,
afd->getImplicitSelfDecl()->getTypeInContext(),
afd->getImplicitSelfDecl()->getValueOwnership())) {
return std::nullopt;
}
break;
}
}
}
return LifetimeDependenceInfo(
inheritLifetimeParamIndices.any()
? IndexSubset::get(ctx, inheritLifetimeParamIndices)
: nullptr,
scopeLifetimeParamIndices.any()
? IndexSubset::get(ctx, scopeLifetimeParamIndices)
: nullptr,
/*isExplicit*/ true);
}
std::optional<LifetimeDependenceInfo>
LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
if (!ctx.LangOpts.hasFeature(Feature::NonescapableTypes)) {
return std::nullopt;
}
// Perform lifetime dependence inference under a flag only. Currently all
// stdlib types can appear is ~Escapable and ~Copyable.
if (!ctx.LangOpts.EnableExperimentalLifetimeDependenceInference) {
return std::nullopt;
}
auto &diags = ctx.Diags;
auto returnTypeRepr = afd->getResultTypeRepr();
auto returnLoc = returnTypeRepr ? returnTypeRepr->getLoc() : afd->getLoc();
if (hasEscapableResultOrYield(afd, resultType)) {
return std::nullopt;
}
if (afd->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
return std::nullopt;
}
if (afd->getKind() != DeclKind::Constructor && afd->hasImplicitSelfDecl()) {
ValueOwnership ownership = ValueOwnership::Default;
if (auto *AD = dyn_cast<AccessorDecl>(afd)) {
if (AD->getAccessorKind() == AccessorKind::Get ||
AD->getAccessorKind() == AccessorKind::Read) {
// We don't support "borrowing/consuming" ownership modifiers on
// getters/_read accessors, they are guaranteed by default for now.
ownership = ValueOwnership::Shared;
}
if (AD->getAccessorKind() == AccessorKind::Modify) {
ownership = ValueOwnership::InOut;
}
} else {
assert(afd->getKind() == DeclKind::Func);
ownership = afd->getImplicitSelfDecl()->getValueOwnership();
if (ownership == ValueOwnership::Default) {
diags.diagnose(
returnLoc,
diag::
lifetime_dependence_cannot_infer_wo_ownership_modifier_on_method);
return std::nullopt;
}
}
return LifetimeDependenceInfo::getForParamIndex(afd, /*selfIndex*/ 0,
ownership);
}
auto *cd = dyn_cast<ConstructorDecl>(afd);
if (cd && cd->isImplicit() && cd->getParameters()->size() == 0) {
return std::nullopt;
}
LifetimeDependenceInfo lifetimeDependenceInfo;
ParamDecl *candidateParam = nullptr;
unsigned paramIndex = 0;
bool hasParamError = false;
for (auto *param : *afd->getParameters()) {
SWIFT_DEFER {
paramIndex++;
};
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
hasParamError = true;
continue;
}
if (param->getValueOwnership() == ValueOwnership::Default) {
continue;
}
if (param->getValueOwnership() == ValueOwnership::Owned &&
paramTypeInContext->isEscapable()) {
continue;
}
if (candidateParam) {
diags.diagnose(
returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate);
return std::nullopt;
}
candidateParam = param;
lifetimeDependenceInfo = LifetimeDependenceInfo::getForParamIndex(
afd, paramIndex + 1, param->getValueOwnership());
}
if (cd && cd->isImplicit()) {
diags.diagnose(cd->getLoc(),
diag::lifetime_dependence_cannot_infer_implicit_init);
return std::nullopt;
}
if (!candidateParam && !hasParamError) {
// Explicitly turn off error messages for builtins, since some of are
// ~Escapable currently.
// TODO: rdar://123555720: Remove this check after another round of
// surveying builtins
if (auto *fd = dyn_cast<FuncDecl>(afd)) {
if (fd->isImplicit() && fd->getModuleContext()->isBuiltinModule()) {
return std::nullopt;
}
}
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates);
return std::nullopt;
}
return lifetimeDependenceInfo;
}
std::optional<LifetimeDependenceInfo>
LifetimeDependenceInfo::get(AbstractFunctionDecl *afd, Type resultType,
bool allowIndex) {
auto *returnTypeRepr = afd->getResultTypeRepr();
if (isa_and_nonnull<LifetimeDependentReturnTypeRepr>(returnTypeRepr)) {
return LifetimeDependenceInfo::fromTypeRepr(afd, resultType, allowIndex);
}
return LifetimeDependenceInfo::infer(afd, resultType);
}
LifetimeDependenceInfo
LifetimeDependenceInfo::get(ASTContext &ctx,
const SmallBitVector &inheritLifetimeIndices,
const SmallBitVector &scopeLifetimeIndices) {
return LifetimeDependenceInfo{
inheritLifetimeIndices.any()
? IndexSubset::get(ctx, inheritLifetimeIndices)
: nullptr,
scopeLifetimeIndices.any() ? IndexSubset::get(ctx, scopeLifetimeIndices)
: nullptr};
}
std::optional<LifetimeDependenceKind>
LifetimeDependenceInfo::getLifetimeDependenceOnParam(unsigned paramIndex) {
if (inheritLifetimeParamIndices) {
if (inheritLifetimeParamIndices->contains(paramIndex)) {
// Can arbitarily return copy or consume here.
// If we converge on dependsOn(borrowed: paramName)/dependsOn(borrowed:
// paramName) syntax, this can be a single case value.
return LifetimeDependenceKind::Copy;
}
}
if (scopeLifetimeParamIndices) {
if (scopeLifetimeParamIndices->contains(paramIndex)) {
// Can arbitarily return borrow or mutate here.
// If we converge on dependsOn(borrowed: paramName)/dependsOn(borrowed:
// paramName) syntax, this can be a single case value.
return LifetimeDependenceKind::Borrow;
}
}
return {};
}
} // namespace swift