Skip to content

Commit a932576

Browse files
committed
Ban explicit lifetime dependence on bitwise copyable type
1 parent 7ec4ed4 commit a932576

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7835,6 +7835,8 @@ ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
78357835
ERROR(lifetime_dependence_cannot_use_infer, none,
78367836
"invalid use of %0 lifetime dependence on Escapable type",
78377837
(StringRef))
7838+
ERROR(lifetime_dependence_on_bitwise_copyable, none,
7839+
"invalid lifetime dependence on bitwise copyable type", ())
78387840

78397841
//===----------------------------------------------------------------------===//
78407842
// MARK: Transferring

lib/Sema/LifetimeDependence.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
107107
bool allowIndex) {
108108
auto *dc = afd->getDeclContext();
109109
auto &ctx = dc->getASTContext();
110+
auto *mod = afd->getModuleContext();
110111
auto &diags = ctx.Diags;
111112
auto capacity = afd->getParameters()->size() + 1;
112113
auto lifetimeDependentRepr =
@@ -156,6 +157,16 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
156157
return true;
157158
}
158159
}
160+
161+
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
162+
auto *bitwiseCopyableProtocol =
163+
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
164+
if (bitwiseCopyableProtocol && mod->checkConformance(type, bitwiseCopyableProtocol)) {
165+
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
166+
return true;
167+
}
168+
}
169+
159170
if (inheritLifetimeParamIndices.test(paramIndexToSet) ||
160171
scopeLifetimeParamIndices.test(paramIndexToSet)) {
161172
diags.diagnose(loc, diag::lifetime_dependence_duplicate_param_id);

test/Sema/explicit_lifetime_dependence_specifiers.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -disable-experimental-parser-round-trip -enable-experimental-feature NoncopyableGenerics -enable-builtin-module
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -disable-experimental-parser-round-trip -enable-experimental-feature NoncopyableGenerics -enable-builtin-module -enable-experimental-feature BitwiseCopyable
22
// REQUIRES: noncopyable_generics
33
import Builtin
44

5+
struct Container {
6+
let ptr: UnsafeRawBufferPointer
7+
}
8+
59
struct BufferView : ~Escapable {
610
let ptr: UnsafeRawBufferPointer
711
init(_ ptr: UnsafeRawBufferPointer) {
812
self.ptr = ptr
913
}
14+
init(_ c: borrowing Container) -> _borrow(c) Self { // expected-error{{invalid lifetime dependence on bitwise copyable type}}
15+
self.ptr = c.ptr
16+
return self
17+
}
1018
}
1119

1220
struct MutableBufferView : ~Escapable, ~Copyable {

0 commit comments

Comments
 (0)