Skip to content

Commit 4993f4b

Browse files
authored
Merge pull request #74956 from meg-gupta/lifetimedepfunctiontype
Add some diagnostics related to lifetime dependence and function types
2 parents 28795ef + 13cfbca commit 4993f4b

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8000,6 +8000,9 @@ ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
80008000
())
80018001
ERROR(lifetime_dependence_immortal_conflict_name, none,
80028002
"conflict between the parameter name and immortal keyword", ())
8003+
ERROR(lifetime_dependence_function_type, none,
8004+
"lifetime dependencies on function types are not supported",
8005+
())
80038006

80048007
//===----------------------------------------------------------------------===//
80058008
// MARK: Sending

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,6 +3248,10 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
32483248
if (!matchFunctionIsolations(func1, func2, kind, flags, locator))
32493249
return getTypeMatchFailure(locator);
32503250

3251+
if (func1->getLifetimeDependenceInfo() != func2->getLifetimeDependenceInfo()) {
3252+
return getTypeMatchFailure(locator);
3253+
}
3254+
32513255
// To contextual type increase the score to avoid ambiguity when solver can
32523256
// find more than one viable binding different only in representation e.g.
32533257
// let _: (@convention(block) () -> Void)? = Bool.random() ? nil : {}

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3961,6 +3961,12 @@ NeverNullType TypeResolver::resolveASTFunctionType(
39613961
}
39623962
}
39633963

3964+
if (auto *lifetimeRepr = dyn_cast_or_null<LifetimeDependentReturnTypeRepr>(
3965+
repr->getResultTypeRepr())) {
3966+
diagnoseInvalid(lifetimeRepr, lifetimeRepr->getLoc(),
3967+
diag::lifetime_dependence_function_type);
3968+
}
3969+
39643970
auto resultOptions = options.withoutContext();
39653971
resultOptions.setContext(TypeResolverContext::FunctionResult);
39663972
auto outputTy = resolveType(repr->getResultTypeRepr(), resultOptions);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes
2+
// REQUIRES: asserts
3+
4+
struct NC: ~Copyable {
5+
var ne: NE {
6+
NE()
7+
}
8+
}
9+
10+
struct NE: ~Escapable {
11+
@_unsafeNonescapableResult
12+
init() {}
13+
}
14+
15+
func transfer(_ ne: NE) -> NE {
16+
ne
17+
}
18+
19+
func applyAnnotatedTransfer(ne: NE, transfer: (NE) -> dependsOn(0) NE) -> NE { // expected-error{{lifetime dependencies on function types are not supported}}
20+
transfer(ne)
21+
}
22+
23+
func applyTransfer(ne: NE, transfer: (NE) -> NE) -> NE {
24+
transfer(ne)
25+
}
26+
27+
func testTransfer(nc: consuming NC) {
28+
let transferred = applyTransfer(ne: nc.ne, transfer: transfer) // expected-error{{cannot convert value of type '(NE) -> _inherit(0) NE' to expected argument type '(NE) -> NE'}}
29+
30+
_ = consume nc
31+
_ = transfer(transferred)
32+
}
33+
34+
func borrow(_ nc: borrowing NC) -> NE {
35+
nc.ne
36+
}
37+
38+
func applyBorrow(nc: borrowing NC, borrow: (borrowing NC) -> NE) -> NE {
39+
borrow(nc)
40+
}
41+
42+
func testBorrow(nc: consuming NC) {
43+
let borrowed = applyBorrow(nc: nc, borrow: borrow) // expected-error{{cannot convert value of type '(borrowing NC) -> _scope(0) NE' to expected argument type '(borrowing NC) -> NE}}
44+
_ = consume nc
45+
_ = transfer(borrowed)
46+
}
47+

0 commit comments

Comments
 (0)