Skip to content

Commit b835c14

Browse files
committed
LifetimeDependence inference, dont use BitwiseCopyable type as candidate
1 parent a05c34c commit b835c14

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ static LifetimeDependenceKind getLifetimeDependenceKindFromDecl(
172172
: LifetimeDependenceKind::Inherit;
173173
}
174174

175+
static bool isBitwiseCopyable(Type type, ModuleDecl *mod, ASTContext &ctx) {
176+
if (!ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
177+
return false;
178+
}
179+
auto *bitwiseCopyableProtocol =
180+
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
181+
if (!bitwiseCopyableProtocol) {
182+
return false;
183+
}
184+
return (bool)(mod->checkConformance(type, bitwiseCopyableProtocol));
185+
}
186+
175187
std::optional<LifetimeDependenceInfo>
176188
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
177189
auto *dc = afd->getDeclContext();
@@ -205,14 +217,9 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
205217
// error.
206218
// TODO: Diagnose ~Escapable types are always non-trivial in SIL.
207219
if (paramType->isEscapable()) {
208-
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
209-
auto *bitwiseCopyableProtocol =
210-
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
211-
if (bitwiseCopyableProtocol &&
212-
mod->checkConformance(paramType, bitwiseCopyableProtocol)) {
213-
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
214-
return true;
215-
}
220+
if (isBitwiseCopyable(paramType, mod, ctx)) {
221+
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
222+
return true;
216223
}
217224
}
218225

@@ -426,16 +433,11 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
426433
if (!cd && afd->hasImplicitSelfDecl()) {
427434
Type selfTypeInContext = dc->getSelfTypeInContext();
428435
if (selfTypeInContext->isEscapable()) {
429-
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
430-
auto *bitwiseCopyableProtocol =
431-
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
432-
if (bitwiseCopyableProtocol &&
433-
mod->checkConformance(selfTypeInContext, bitwiseCopyableProtocol)) {
436+
if (isBitwiseCopyable(selfTypeInContext, mod, ctx)) {
434437
diags.diagnose(
435438
returnLoc,
436439
diag::lifetime_dependence_method_escapable_bitwisecopyable_self);
437440
return std::nullopt;
438-
}
439441
}
440442
}
441443
auto kind = getLifetimeDependenceKindFromType(selfTypeInContext);
@@ -463,9 +465,13 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
463465
continue;
464466
}
465467
auto paramOwnership = param->getValueOwnership();
466-
if (paramTypeInContext->isEscapable() &&
467-
paramOwnership == ValueOwnership::Default) {
468-
continue;
468+
if (paramTypeInContext->isEscapable()) {
469+
if (isBitwiseCopyable(paramTypeInContext, mod, ctx)) {
470+
continue;
471+
}
472+
if (paramOwnership == ValueOwnership::Default) {
473+
continue;
474+
}
469475
}
470476

471477
auto lifetimeKind = getLifetimeDependenceKindFromType(paramTypeInContext);

test/Sema/implicit_lifetime_dependence.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature BitwiseCopyable
22
// REQUIRES: asserts
33

44
struct BufferView : ~Escapable, ~Copyable {
@@ -24,7 +24,15 @@ struct ImplicitInit3 : ~Escapable, ~Copyable { // expected-error{{cannot infer l
2424
let mbv2: BufferView
2525
}
2626

27-
func foo() -> BufferView { // expected-error{{cannot infer lifetime dependence , no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
27+
func foo1() -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
28+
return BufferView(nil, 0)
29+
}
30+
31+
func foo2(_ i: borrowing Int) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
32+
return BufferView(nil, 0)
33+
}
34+
35+
func foo3<T: _BitwiseCopyable>(arg: borrowing T) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
2836
return BufferView(nil, 0)
2937
}
3038

0 commit comments

Comments
 (0)