Skip to content

[6.0] Updates on lifetime dependence #73002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7873,37 +7873,31 @@ ERROR(lifetime_dependence_only_on_function_method_init_result, none,
"functions, methods, initializers", ())
ERROR(lifetime_dependence_invalid_return_type, none,
"lifetime dependence can only be specified on ~Escapable results", ())
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
"cannot infer lifetime dependence, multiple ~Escapable or ~Copyable "
"parameters with ownership modifiers, specify explicit lifetime "
"dependence", ())
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
"cannot infer lifetime dependence, no parameters with ownership "
"modifiers present", ())
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
"expected nil or self as return values in an initializer with "
"lifetime dependent specifiers", ())
ERROR(lifetime_dependence_on_bitwise_copyable, none,
"invalid lifetime dependence on bitwise copyable type", ())
ERROR(lifetime_dependence_cannot_infer_implicit_init, none,
"cannot infer lifetime dependence on implicit initializer of ~Escapable"
" type, define an initializer with explicit lifetime dependence"
" specifiers", ())
ERROR(lifetime_dependence_cannot_be_applied_to_tuple_elt, none,
"lifetime dependence specifiers cannot be applied to tuple elements", ())

//===----------------------------------------------------------------------===//
// MARK: Transferring
//===----------------------------------------------------------------------===//

ERROR(transferring_unsupported_param_specifier, none,
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))

ERROR(transferring_only_on_parameters_and_results, none,
"'transferring' may only be used on parameters and results", ())
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
"'transferring' cannot be applied to tuple elements", ())

ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
"cannot infer lifetime dependence %0, no parameters found that are "
"~Escapable or Escapable with a borrowing ownership", (StringRef))
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
"expected nil or self as return values in an initializer with "
"lifetime dependent specifiers",
())
ERROR(lifetime_dependence_on_bitwise_copyable, none,
"invalid lifetime dependence on bitwise copyable type", ())
ERROR(lifetime_dependence_cannot_be_applied_to_tuple_elt, none,
"lifetime dependence specifiers cannot be applied to tuple elements", ())

//===----------------------------------------------------------------------===//
// MARK: Transferring
//===----------------------------------------------------------------------===//

ERROR(transferring_unsupported_param_specifier, none,
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))

ERROR(transferring_only_on_parameters_and_results, none,
"'transferring' may only be used on parameters and results", ())
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
"'transferring' cannot be applied to tuple elements", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
5 changes: 4 additions & 1 deletion lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,10 @@ static ValueDecl *getWithUnsafeContinuation(ASTContext &ctx,
auto *fnTy = FunctionType::get(params, voidTy, extInfo);

builder.addParameter(makeConcrete(fnTy));
builder.setResult(makeGenericParam());

auto resultTy = makeGenericParam();
builder.addConformanceRequirement(resultTy, KnownProtocolKind::Escapable);
builder.setResult(resultTy);

builder.setAsync();
if (throws)
Expand Down
33 changes: 17 additions & 16 deletions lib/Sema/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,6 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
if (cd && cd->isImplicit()) {
if (cd->getParameters()->size() == 0) {
return std::nullopt;
} else {
diags.diagnose(cd->getLoc(),
diag::lifetime_dependence_cannot_infer_implicit_init);
return std::nullopt;
}
}

Expand Down Expand Up @@ -470,9 +466,16 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
continue;
}
if (candidateParam) {
diags.diagnose(
returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate);
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
diags.diagnose(
returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
"on implicit initializer");
return std::nullopt;
}
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
"");
return std::nullopt;
}
candidateParam = param;
Expand All @@ -481,17 +484,15 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
}

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;
}
if (afd->getKind() == DeclKind::Constructor && afd->isImplicit()) {
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates,
"on implicit initializer");
return std::nullopt;
}
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates);
diag::lifetime_dependence_cannot_infer_no_candidates,
"");
return std::nullopt;
}
return lifetimeDependenceInfo;
Expand Down
3 changes: 0 additions & 3 deletions test/SIL/type_lowering_unit.sil
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ extension GSNC: Copyable where T: Copyable {}

struct GSNE<T: ~Escapable>: ~Escapable {
var x: T

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

extension GSNE: Escapable where T: Escapable {}
Expand Down
1 change: 0 additions & 1 deletion test/SILGen/typelowering_inverses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func check(_ t: inout any NoEscapeP & ~Escapable) {}
struct MyStruct<T: ~Copyable & ~Escapable>: ~Copyable & ~Escapable {
var x: T

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

Expand Down
16 changes: 16 additions & 0 deletions test/Sema/explicit_lifetime_dependence_specifiers1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ struct BufferView : ~Escapable {
consuming func consume() -> dependsOn(scoped self) BufferView { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
return BufferView(self.ptr)
}

func get() -> dependsOn(self) Self { // expected-note{{'get()' previously declared here}}
return self
}

func get() -> dependsOn(scoped self) Self { // expected-error{{invalid redeclaration of 'get()'}}
return self
}
}

struct MutableBufferView : ~Escapable, ~Copyable {
Expand Down Expand Up @@ -202,3 +210,11 @@ public struct GenericBufferView<Element> : ~Escapable {
self.count = count
}
}

func derive(_ x: BufferView) -> dependsOn(x) BufferView { // expected-note{{'derive' previously declared here}}
return BufferView(x.ptr)
}

func derive(_ x: BufferView) -> dependsOn(scoped x) BufferView { // expected-error{{invalid redeclaration of 'derive'}}
return BufferView(x.ptr)
}
25 changes: 25 additions & 0 deletions test/Sema/implicit_lifetime_dependence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics
// REQUIRES: asserts

struct BufferView : ~Escapable, ~Copyable {
let ptr: UnsafeRawBufferPointer
let c: Int
@_unsafeNonescapableResult
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
self.ptr = ptr
self.c = c
}
}

struct ImplicitInit1 : ~Escapable { // expected-error{{cannot infer lifetime dependence on implicit initializer, no parameters found that are ~Escapable or Escapable with a borrowing ownership}}
let ptr: UnsafeRawBufferPointer
}

struct ImplicitInit2 : ~Escapable, ~Copyable {
let mbv: BufferView
}

struct ImplicitInit3 : ~Escapable, ~Copyable { // expected-error{{cannot infer lifetime dependence on implicit initializer, multiple parameters qualifiy as a candidate}}
let mbv1: BufferView
let mbv2: BufferView
}