Skip to content

Commit 5658dea

Browse files
committed
Add initial support for _resultDependsOnSelf
This is used to establish lifetime dependence between self and the result. Add under NonEscapableTypes experimental feature
1 parent 17c30f1 commit 5658dea

23 files changed

+140
-10
lines changed

include/swift/AST/Attr.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ DECL_ATTR(storageRestrictions, StorageRestrictions,
527527
CONTEXTUAL_SIMPLE_DECL_ATTR(actor, Actor,
528528
DeclModifier | OnClass | ConcurrencyOnly | ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
529529
102)
530+
CONTEXTUAL_SIMPLE_DECL_ATTR(_resultDependsOnSelf, ResultDependsOnSelf,
531+
OnFunc | DeclModifier | UserInaccessible | ABIBreakingToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
532+
150)
530533

531534
#undef TYPE_ATTR
532535
#undef DECL_ATTR_ALIAS

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ enum class SelfAccessKind : uint8_t {
241241
LegacyConsuming,
242242
Consuming,
243243
Borrowing,
244-
LastSelfAccessKind = Borrowing,
244+
ResultDependsOnSelf,
245+
LastSelfAccessKind = ResultDependsOnSelf,
245246
};
246247
enum : unsigned {
247248
NumSelfAccessKindBits =

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,5 +2166,10 @@ ERROR(duplicate_storage_restrictions_attr_label,none,
21662166
ERROR(storage_restrictions_attr_expected_name,none,
21672167
"expected property name in @storageRestrictions list", ())
21682168

2169+
ERROR(requires_non_escapable_types, none,
2170+
"'%0' %select{attribute|parameter specifier}1 is only valid when experimental "
2171+
"NonEscapableTypes is enabled",
2172+
(StringRef, bool))
2173+
21692174
#define UNDEFINE_DIAGNOSTIC_MACROS
21702175
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7701,5 +7701,12 @@ ERROR(referencebindings_binding_must_have_initial_value,none,
77017701
ERROR(referencebindings_binding_must_be_to_lvalue,none,
77027702
"%0 bindings must be bound to an lvalue", (StringRef))
77037703

7704+
//------------------------------------------------------------------------------
7705+
// MARK: resultDependsOn Errors
7706+
//------------------------------------------------------------------------------
7707+
7708+
ERROR(result_depends_on_no_result,none,
7709+
"Incorrect use of %0 with no result", (StringRef))
7710+
77047711
#define UNDEFINE_DIAGNOSTIC_MACROS
77057712
#include "DefineDiagnosticMacros.h"

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,9 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
15591559
case SelfAccessKind::Borrowing:
15601560
FuncSelfKind = "Borrowing";
15611561
break;
1562+
case SelfAccessKind::ResultDependsOnSelf:
1563+
FuncSelfKind = "ResultDependsOnSelf";
1564+
break;
15621565
}
15631566
}
15641567

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,8 @@ AnyFunctionType::Param swift::computeSelfParam(AbstractFunctionDecl *AFD,
34553455
case SelfAccessKind::NonMutating:
34563456
// The default flagless state.
34573457
break;
3458+
case SelfAccessKind::ResultDependsOnSelf:
3459+
break;
34583460
}
34593461

34603462
return AnyFunctionType::Param(selfTy, Identifier(), flags);

lib/AST/ASTPrinter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,10 @@ void PrintAST::printSelfAccessKindModifiersIfNeeded(const FuncDecl *FD) {
21192119
if (!Options.excludeAttrKind(DAK_Borrowing))
21202120
Printer.printKeyword("borrowing", Options, " ");
21212121
break;
2122+
case SelfAccessKind::ResultDependsOnSelf:
2123+
if (!Options.excludeAttrKind(DAK_ResultDependsOnSelf))
2124+
Printer.printKeyword("_resultDependsOnSelf", Options, " ");
2125+
break;
21222126
}
21232127
}
21242128

@@ -3516,8 +3520,15 @@ static bool usesFeatureStructLetDestructuring(Decl *decl) {
35163520
}
35173521

35183522
static bool usesFeatureNonEscapableTypes(Decl *decl) {
3519-
return decl->getAttrs().hasAttribute<NonEscapableAttr>()
3520-
|| decl->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>();
3523+
if (decl->getAttrs().hasAttribute<NonEscapableAttr>() ||
3524+
decl->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
3525+
return true;
3526+
}
3527+
auto *fd = dyn_cast<FuncDecl>(decl);
3528+
if (fd && fd->getAttrs().getAttribute(DAK_ResultDependsOnSelf)) {
3529+
return true;
3530+
}
3531+
return false;
35213532
}
35223533

35233534
static bool hasParameterPacks(Decl *decl) {

lib/AST/Decl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS,
604604
case SelfAccessKind::LegacyConsuming: return OS << "'__consuming'";
605605
case SelfAccessKind::Consuming: return OS << "'consuming'";
606606
case SelfAccessKind::Borrowing: return OS << "'borrowing'";
607+
case SelfAccessKind::ResultDependsOnSelf:
608+
return OS << "'resultDependsOnSelf'";
607609
}
608610
llvm_unreachable("Unknown SelfAccessKind");
609611
}

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extension Parser.ExperimentalFeatures {
3434
mapFeature(.ThenStatements, to: .thenStatements)
3535
mapFeature(.TypedThrows, to: .typedThrows)
3636
mapFeature(.DoExpressions, to: .doExpressions)
37+
mapFeature(.NonEscapableTypes, to: .nonEscapableTypes)
3738
}
3839
}
3940

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extension ASTGenVisitor {
217217
}
218218
}
219219

220-
// MARK: - SpecifierTypeRepr/AttrubutedTypeRepr
220+
// MARK: - SpecifierTypeRepr/AttributedTypeRepr
221221

222222
extension BridgedAttributedTypeSpecifier {
223223
fileprivate init?(from tokenKind: TokenKind) {

0 commit comments

Comments
 (0)