Skip to content

Commit f5f57d1

Browse files
authored
Merge pull request #71665 from meg-gupta/fixgeninit
Fix generic initializers with lifetime dependence
2 parents c5e7a60 + dc4b89f commit f5f57d1

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class AnyFunctionRef {
118118
return FD->mapTypeIntoContext(FD->getResultInterfaceType());
119119
if (auto *CD = dyn_cast<ConstructorDecl>(AFD)) {
120120
if (CD->hasLifetimeDependentReturn()) {
121-
return CD->getResultInterfaceType();
121+
return CD->mapTypeIntoContext(CD->getResultInterfaceType());
122122
}
123123
}
124124
return TupleType::getEmpty(AFD->getASTContext());

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ class Verifier : public ASTWalker {
10781078
} else if (auto *CD = dyn_cast<ConstructorDecl>(func)) {
10791079
if (CD->hasLifetimeDependentReturn()) {
10801080
resultType = CD->getResultInterfaceType();
1081+
resultType = CD->mapTypeIntoContext(resultType);
10811082
} else {
10821083
resultType = TupleType::getEmpty(Ctx);
10831084
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,16 +1719,10 @@ Stmt *PreCheckReturnStmtRequest::evaluate(Evaluator &evaluator, ReturnStmt *RS,
17191719
auto *nilExpr = dyn_cast<NilLiteralExpr>(E->getSemanticsProvidingExpr());
17201720
if (!nilExpr) {
17211721
if (ctor->hasLifetimeDependentReturn()) {
1722-
// Typecheck the expression unconditionally.
1723-
TypeChecker::typeCheckExpression(E, DC, {});
1724-
1725-
auto *checkE = E;
1726-
if (auto *load = dyn_cast<LoadExpr>(checkE))
1727-
checkE = load->getSubExpr();
17281722
bool isSelf = false;
1729-
if (auto DRE = dyn_cast<DeclRefExpr>(checkE))
1730-
isSelf = DRE->getDecl() == ctor->getImplicitSelfDecl();
1731-
1723+
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E)) {
1724+
isSelf = UDRE->getName().isSimpleName(ctx.Id_self);
1725+
}
17321726
if (!isSelf) {
17331727
ctx.Diags.diagnose(
17341728
RS->getStartLoc(),

test/Sema/explicit_lifetime_dependence_specifiers.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,27 @@ struct Wrapper : ~Escapable {
137137
return view
138138
}
139139
}
140+
141+
public struct GenericBufferView<Element> : ~Escapable {
142+
public typealias Index = Int
143+
public typealias Pointer = UnsafePointer<Element>
144+
145+
public let baseAddress: Pointer
146+
public let count: Int
147+
148+
public init<Storage>(unsafeBuffer: UnsafeBufferPointer<Element>,
149+
storage: borrowing Storage)
150+
-> _borrow(storage) Self {
151+
let baseAddress = unsafeBuffer.baseAddress!
152+
self = GenericBufferView<Element>(baseAddress: baseAddress,
153+
count: unsafeBuffer.count)
154+
return self
155+
}
156+
// unsafe private API
157+
@_unsafeNonescapableResult
158+
init(baseAddress: Pointer, count: Int) {
159+
precondition(count >= 0, "Count must not be negative")
160+
self.baseAddress = baseAddress
161+
self.count = count
162+
}
163+
}

0 commit comments

Comments
 (0)