Skip to content

Commit 3bf3e0f

Browse files
committed
Delete dependsOn support
Lifetime dependencies will now be represented with @Lifetime attribute in the language. dependsOn is a type modifier and was represented as a LifetimeDependentTypeRepr in the AST. I am deleting dependsOn syntax parsing support and retaining LifetimeDependentTypeRepr support. We may want to represent lifetime dependencies in a function type with a type attribute in the future. If we use a decl attribute instead, then support for LifetimeDependentTypeRepr can be deleted.
1 parent cdd8fe9 commit 3bf3e0f

File tree

9 files changed

+7
-157
lines changed

9 files changed

+7
-157
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8805,8 +8805,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
88058805
Bits.ConstructorDecl.HasStubImplementation = stub;
88068806
}
88078807

8808-
bool hasLifetimeDependentReturn() const;
8809-
88108808
ConstructorDecl *getOverriddenDecl() const {
88118809
return cast_or_null<ConstructorDecl>(
88128810
AbstractFunctionDecl::getOverriddenDecl());

include/swift/AST/LifetimeDependence.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ class LifetimeDependenceInfo {
157157
static std::optional<ArrayRef<LifetimeDependenceInfo>>
158158
fromLifetimeAttribute(AbstractFunctionDecl *afd);
159159

160-
/// Builds LifetimeDependenceInfo from dependsOn type modifier
161-
static std::optional<LifetimeDependenceInfo>
162-
fromDependsOn(AbstractFunctionDecl *afd, TypeRepr *targetRepr,
163-
Type targetType, unsigned targetIndex);
164-
165160
/// Infer LifetimeDependenceInfo on result
166161
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd);
167162

include/swift/Parse/Parser.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,11 +1222,8 @@ class Parser {
12221222
}
12231223

12241224
bool isLifetimeDependenceToken() {
1225-
if (!isInSILMode()) {
1226-
return Tok.isContextualKeyword("dependsOn");
1227-
}
1228-
return Tok.isContextualKeyword("_inherit") ||
1229-
Tok.isContextualKeyword("_scope");
1225+
return isInSILMode() && (Tok.isContextualKeyword("_inherit") ||
1226+
Tok.isContextualKeyword("_scope"));
12301227
}
12311228

12321229
bool canHaveParameterSpecifierContextualKeyword() {

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,16 +4246,6 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
42464246

42474247
Printer.printDeclResultTypePre(decl, ResultTyLoc);
42484248
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
4249-
{
4250-
if (!Options.SuppressNonEscapableTypes) {
4251-
if (auto *typeRepr = dyn_cast_or_null<LifetimeDependentTypeRepr>(
4252-
decl->getResultTypeRepr())) {
4253-
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4254-
Printer << " " << dep.getDependsOnString() << " ";
4255-
}
4256-
}
4257-
}
4258-
}
42594249

42604250
if (!Options.SuppressSendingArgsAndResults) {
42614251
if (decl->hasSendingResult()) {
@@ -4499,18 +4489,6 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
44994489

45004490
printGenericDeclGenericParams(decl);
45014491
printFunctionParameters(decl);
4502-
if (!Options.SuppressNonEscapableTypes) {
4503-
if (decl->hasLifetimeDependentReturn()) {
4504-
Printer << " -> ";
4505-
auto *typeRepr =
4506-
cast<LifetimeDependentTypeRepr>(decl->getResultTypeRepr());
4507-
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4508-
Printer << dep.getDependsOnString() << " ";
4509-
}
4510-
// TODO: Handle failable initializers with lifetime dependent returns
4511-
Printer << "Self";
4512-
}
4513-
}
45144492
});
45154493

45164494
printDeclGenericRequirements(decl);

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,16 +1109,6 @@ class Verifier : public ASTWalker {
11091109
}
11101110

11111111
if (S->hasResult()) {
1112-
if (auto *CD = dyn_cast<ConstructorDecl>(func)) {
1113-
if (!CD->hasLifetimeDependentReturn()) {
1114-
Out << "Expected ReturnStmt not to have a result. A constructor "
1115-
"should not return a result. Returned expression: ";
1116-
S->getResult()->dump(Out);
1117-
Out << "\n";
1118-
abort();
1119-
}
1120-
}
1121-
11221112
auto result = S->getResult();
11231113
auto returnType = result->getType();
11241114
// Make sure that the return has the same type as the function.

lib/AST/Decl.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10611,10 +10611,6 @@ bool ConstructorDecl::isObjCZeroParameterWithLongSelector() const {
1061110611
return params->get(0)->getInterfaceType()->isVoid();
1061210612
}
1061310613

10614-
bool ConstructorDecl::hasLifetimeDependentReturn() const {
10615-
return isa_and_nonnull<LifetimeDependentTypeRepr>(getResultTypeRepr());
10616-
}
10617-
1061810614
DestructorDecl::DestructorDecl(SourceLoc DestructorLoc, DeclContext *Parent)
1061910615
: AbstractFunctionDecl(DeclKind::Destructor, Parent,
1062010616
DeclBaseName::createDestructor(), DestructorLoc,

lib/AST/LifetimeDependence.cpp

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -414,48 +414,6 @@ LifetimeDependenceInfo::fromLifetimeAttribute(AbstractFunctionDecl *afd) {
414414
}
415415

416416
std::optional<LifetimeDependenceInfo>
417-
LifetimeDependenceInfo::fromDependsOn(AbstractFunctionDecl *afd,
418-
TypeRepr *targetTypeRepr, Type targetType,
419-
unsigned targetIndex) {
420-
auto *dc = afd->getDeclContext();
421-
auto &ctx = dc->getASTContext();
422-
auto &diags = ctx.Diags;
423-
424-
auto *lifetimeDependentRepr =
425-
dyn_cast_or_null<LifetimeDependentTypeRepr>(targetTypeRepr);
426-
if (!lifetimeDependentRepr) {
427-
return std::nullopt;
428-
}
429-
430-
if (targetType->isEscapable()) {
431-
diags.diagnose(targetTypeRepr->getLoc(),
432-
diag::lifetime_dependence_invalid_type);
433-
return std::nullopt;
434-
}
435-
436-
auto capacity = afd->hasImplicitSelfDecl()
437-
? (afd->getParameters()->size() + 1)
438-
: afd->getParameters()->size();
439-
440-
SmallBitVector inheritIndices(capacity);
441-
SmallBitVector scopeIndices(capacity);
442-
bool isImmortal = false;
443-
bool hasError = false;
444-
445-
for (auto entry : lifetimeDependentRepr->getLifetimeDependencies()) {
446-
hasError |= populateLifetimeDependence(afd, entry, inheritIndices,
447-
scopeIndices, isImmortal);
448-
}
449-
450-
if (hasError) {
451-
return std::nullopt;
452-
}
453-
return LifetimeDependenceInfo(
454-
inheritIndices.any() ? IndexSubset::get(ctx, inheritIndices) : nullptr,
455-
scopeIndices.any() ? IndexSubset::get(ctx, scopeIndices) : nullptr,
456-
targetIndex, isImmortal);
457-
}
458-
459417
// This utility is similar to its overloaded version that builds the
460418
// LifetimeDependenceInfo from the swift decl. Reason for duplicated code is
461419
// the apis on type and ownership is different in SIL compared to Sema.
@@ -735,38 +693,12 @@ LifetimeDependenceInfo::get(AbstractFunctionDecl *afd) {
735693
return LifetimeDependenceInfo::fromLifetimeAttribute(afd);
736694
}
737695

738-
SmallVector<LifetimeDependenceInfo> lifetimeDependencies;
739-
740-
for (unsigned targetIndex : indices(*afd->getParameters())) {
741-
auto *param = (*afd->getParameters())[targetIndex];
742-
auto paramType =
743-
afd->mapTypeIntoContext(param->toFunctionParam().getParameterType());
744-
if (auto result = LifetimeDependenceInfo::fromDependsOn(
745-
afd, param->getTypeRepr(), paramType, targetIndex)) {
746-
lifetimeDependencies.push_back(*result);
747-
}
748-
}
749-
750-
std::optional<LifetimeDependenceInfo> resultDependence;
751-
752-
if (auto *lifetimeTypeRepr = dyn_cast_or_null<LifetimeDependentTypeRepr>(
753-
afd->getResultTypeRepr())) {
754-
resultDependence = LifetimeDependenceInfo::fromDependsOn(
755-
afd, lifetimeTypeRepr, getResultOrYield(afd),
756-
afd->hasImplicitSelfDecl() ? afd->getParameters()->size() + 1
757-
: afd->getParameters()->size());
758-
} else {
759-
resultDependence = LifetimeDependenceInfo::infer(afd);
760-
}
761-
762-
if (resultDependence.has_value()) {
763-
lifetimeDependencies.push_back(*resultDependence);
764-
}
765-
766-
if (lifetimeDependencies.empty()) {
696+
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
697+
auto resultDependence = LifetimeDependenceInfo::infer(afd);
698+
if (!resultDependence.has_value()) {
767699
return std::nullopt;
768700
}
769-
701+
lifetimeDependencies.push_back(*resultDependence);
770702
return afd->getASTContext().AllocateCopy(lifetimeDependencies);
771703
}
772704

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10074,26 +10074,7 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
1007410074
isAsync = true;
1007510075
}
1007610076

10077-
if (auto *lifetimeTyR =
10078-
dyn_cast_or_null<LifetimeDependentTypeRepr>(FuncRetTy)) {
10079-
auto *base = lifetimeTyR->getBase();
10080-
10081-
auto isOptionalSimpleUnqualifiedIdentifier = [](TypeRepr *typeRepr,
10082-
Identifier str) {
10083-
if (auto *optionalTR = dyn_cast<OptionalTypeRepr>(typeRepr)) {
10084-
return optionalTR->getBase()->isSimpleUnqualifiedIdentifier(str);
10085-
}
10086-
return false;
10087-
};
10088-
10089-
// Diagnose if return type is not Self or Self?
10090-
if (!base->isSimpleUnqualifiedIdentifier(Context.Id_Self) &&
10091-
!isOptionalSimpleUnqualifiedIdentifier(base, Context.Id_Self)) {
10092-
diagnose(FuncRetTy->getStartLoc(),
10093-
diag::lifetime_dependence_invalid_init_return);
10094-
return nullptr;
10095-
}
10096-
} else if (FuncRetTy) {
10077+
if (FuncRetTy) {
1009710078
diagnose(FuncRetTy->getStartLoc(), diag::initializer_result_type)
1009810079
.fixItRemove(FuncRetTy->getSourceRange());
1009910080
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,23 +1726,6 @@ Stmt *PreCheckReturnStmtRequest::evaluate(Evaluator &evaluator, ReturnStmt *RS,
17261726
// 'nil'.
17271727
auto *nilExpr = dyn_cast<NilLiteralExpr>(E->getSemanticsProvidingExpr());
17281728
if (!nilExpr) {
1729-
if (ctor->hasLifetimeDependentReturn()) {
1730-
bool isSelf = false;
1731-
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E)) {
1732-
isSelf = UDRE->getName().isSimpleName(ctx.Id_self);
1733-
// Result the result expression so that rest of the compilation
1734-
// pipeline handles initializers with lifetime dependence specifiers
1735-
// in the same way as other initializers.
1736-
RS->setResult(nullptr);
1737-
}
1738-
if (!isSelf) {
1739-
ctx.Diags.diagnose(
1740-
RS->getStartLoc(),
1741-
diag::lifetime_dependence_ctor_non_self_or_nil_return);
1742-
RS->setResult(nullptr);
1743-
}
1744-
return RS;
1745-
}
17461729
ctx.Diags.diagnose(RS->getReturnLoc(), diag::return_init_non_nil)
17471730
.highlight(E->getSourceRange());
17481731
RS->setResult(nullptr);

0 commit comments

Comments
 (0)