Skip to content

Commit 77b8f88

Browse files
authored
Merge pull request #73002 from meg-gupta/cp1
[6.0] Updates on lifetime dependence
2 parents fbf62a6 + bbaa90d commit 77b8f88

File tree

7 files changed

+87
-52
lines changed

7 files changed

+87
-52
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7877,37 +7877,31 @@ ERROR(lifetime_dependence_only_on_function_method_init_result, none,
78777877
"functions, methods, initializers", ())
78787878
ERROR(lifetime_dependence_invalid_return_type, none,
78797879
"lifetime dependence can only be specified on ~Escapable results", ())
7880-
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
7881-
"cannot infer lifetime dependence, multiple ~Escapable or ~Copyable "
7882-
"parameters with ownership modifiers, specify explicit lifetime "
7883-
"dependence", ())
7884-
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
7885-
"cannot infer lifetime dependence, no parameters with ownership "
7886-
"modifiers present", ())
7887-
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
7888-
"expected nil or self as return values in an initializer with "
7889-
"lifetime dependent specifiers", ())
7890-
ERROR(lifetime_dependence_on_bitwise_copyable, none,
7891-
"invalid lifetime dependence on bitwise copyable type", ())
7892-
ERROR(lifetime_dependence_cannot_infer_implicit_init, none,
7893-
"cannot infer lifetime dependence on implicit initializer of ~Escapable"
7894-
" type, define an initializer with explicit lifetime dependence"
7895-
" specifiers", ())
7896-
ERROR(lifetime_dependence_cannot_be_applied_to_tuple_elt, none,
7897-
"lifetime dependence specifiers cannot be applied to tuple elements", ())
7898-
7899-
//===----------------------------------------------------------------------===//
7900-
// MARK: Transferring
7901-
//===----------------------------------------------------------------------===//
7902-
7903-
ERROR(transferring_unsupported_param_specifier, none,
7904-
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7905-
7906-
ERROR(transferring_only_on_parameters_and_results, none,
7907-
"'transferring' may only be used on parameters and results", ())
7908-
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7909-
"'transferring' cannot be applied to tuple elements", ())
7910-
7880+
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
7881+
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
7882+
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
7883+
"cannot infer lifetime dependence %0, no parameters found that are "
7884+
"~Escapable or Escapable with a borrowing ownership", (StringRef))
7885+
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
7886+
"expected nil or self as return values in an initializer with "
7887+
"lifetime dependent specifiers",
7888+
())
7889+
ERROR(lifetime_dependence_on_bitwise_copyable, none,
7890+
"invalid lifetime dependence on bitwise copyable type", ())
7891+
ERROR(lifetime_dependence_cannot_be_applied_to_tuple_elt, none,
7892+
"lifetime dependence specifiers cannot be applied to tuple elements", ())
7893+
7894+
//===----------------------------------------------------------------------===//
7895+
// MARK: Transferring
7896+
//===----------------------------------------------------------------------===//
7897+
7898+
ERROR(transferring_unsupported_param_specifier, none,
7899+
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7900+
7901+
ERROR(transferring_only_on_parameters_and_results, none,
7902+
"'transferring' may only be used on parameters and results", ())
7903+
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7904+
"'transferring' cannot be applied to tuple elements", ())
79117905

79127906
#define UNDEFINE_DIAGNOSTIC_MACROS
79137907
#include "DefineDiagnosticMacros.h"

lib/AST/Builtins.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,10 @@ static ValueDecl *getWithUnsafeContinuation(ASTContext &ctx,
20532053
auto *fnTy = FunctionType::get(params, voidTy, extInfo);
20542054

20552055
builder.addParameter(makeConcrete(fnTy));
2056-
builder.setResult(makeGenericParam());
2056+
2057+
auto resultTy = makeGenericParam();
2058+
builder.addConformanceRequirement(resultTy, KnownProtocolKind::Escapable);
2059+
builder.setResult(resultTy);
20572060

20582061
builder.setAsync();
20592062
if (throws)

lib/Sema/LifetimeDependence.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,6 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
425425
if (cd && cd->isImplicit()) {
426426
if (cd->getParameters()->size() == 0) {
427427
return std::nullopt;
428-
} else {
429-
diags.diagnose(cd->getLoc(),
430-
diag::lifetime_dependence_cannot_infer_implicit_init);
431-
return std::nullopt;
432428
}
433429
}
434430

@@ -470,9 +466,16 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
470466
continue;
471467
}
472468
if (candidateParam) {
473-
diags.diagnose(
474-
returnLoc,
475-
diag::lifetime_dependence_cannot_infer_ambiguous_candidate);
469+
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
470+
diags.diagnose(
471+
returnLoc,
472+
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
473+
"on implicit initializer");
474+
return std::nullopt;
475+
}
476+
diags.diagnose(returnLoc,
477+
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
478+
"");
476479
return std::nullopt;
477480
}
478481
candidateParam = param;
@@ -481,17 +484,15 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
481484
}
482485

483486
if (!candidateParam && !hasParamError) {
484-
// Explicitly turn off error messages for builtins, since some of are
485-
// ~Escapable currently.
486-
// TODO: rdar://123555720: Remove this check after another round of
487-
// surveying builtins
488-
if (auto *fd = dyn_cast<FuncDecl>(afd)) {
489-
if (fd->isImplicit() && fd->getModuleContext()->isBuiltinModule()) {
490-
return std::nullopt;
491-
}
487+
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
488+
diags.diagnose(returnLoc,
489+
diag::lifetime_dependence_cannot_infer_no_candidates,
490+
"on implicit initializer");
491+
return std::nullopt;
492492
}
493493
diags.diagnose(returnLoc,
494-
diag::lifetime_dependence_cannot_infer_no_candidates);
494+
diag::lifetime_dependence_cannot_infer_no_candidates,
495+
"");
495496
return std::nullopt;
496497
}
497498
return lifetimeDependenceInfo;

test/SIL/type_lowering_unit.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ extension GSNC: Copyable where T: Copyable {}
120120

121121
struct GSNE<T: ~Escapable>: ~Escapable {
122122
var x: T
123-
124-
// 60_MERGE: an explicit initializer is temporarily required until initializer inferrence is merged.
125-
init(x: consuming T) { self.x = x }
126123
}
127124

128125
extension GSNE: Escapable where T: Escapable {}

test/SILGen/typelowering_inverses.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ func check(_ t: inout any NoEscapeP & ~Escapable) {}
141141
struct MyStruct<T: ~Copyable & ~Escapable>: ~Copyable & ~Escapable {
142142
var x: T
143143

144-
// 60_MERGE: an explicit initializer is temporarily required until initializer inferrence is merged.
145144
init(x: consuming T) { self.x = x }
146145
}
147146

test/Sema/explicit_lifetime_dependence_specifiers1.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ struct BufferView : ~Escapable {
5656
consuming func consume() -> dependsOn(scoped self) BufferView { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
5757
return BufferView(self.ptr)
5858
}
59+
60+
func get() -> dependsOn(self) Self { // expected-note{{'get()' previously declared here}}
61+
return self
62+
}
63+
64+
func get() -> dependsOn(scoped self) Self { // expected-error{{invalid redeclaration of 'get()'}}
65+
return self
66+
}
5967
}
6068

6169
struct MutableBufferView : ~Escapable, ~Copyable {
@@ -202,3 +210,11 @@ public struct GenericBufferView<Element> : ~Escapable {
202210
self.count = count
203211
}
204212
}
213+
214+
func derive(_ x: BufferView) -> dependsOn(x) BufferView { // expected-note{{'derive' previously declared here}}
215+
return BufferView(x.ptr)
216+
}
217+
218+
func derive(_ x: BufferView) -> dependsOn(scoped x) BufferView { // expected-error{{invalid redeclaration of 'derive'}}
219+
return BufferView(x.ptr)
220+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics
2+
// REQUIRES: asserts
3+
4+
struct BufferView : ~Escapable, ~Copyable {
5+
let ptr: UnsafeRawBufferPointer
6+
let c: Int
7+
@_unsafeNonescapableResult
8+
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
9+
self.ptr = ptr
10+
self.c = c
11+
}
12+
}
13+
14+
struct ImplicitInit1 : ~Escapable { // expected-error{{cannot infer lifetime dependence on implicit initializer, no parameters found that are ~Escapable or Escapable with a borrowing ownership}}
15+
let ptr: UnsafeRawBufferPointer
16+
}
17+
18+
struct ImplicitInit2 : ~Escapable, ~Copyable {
19+
let mbv: BufferView
20+
}
21+
22+
struct ImplicitInit3 : ~Escapable, ~Copyable { // expected-error{{cannot infer lifetime dependence on implicit initializer, multiple parameters qualifiy as a candidate}}
23+
let mbv1: BufferView
24+
let mbv2: BufferView
25+
}

0 commit comments

Comments
 (0)