Skip to content

Commit 37ea254

Browse files
authored
Merge pull request #40220 from DougGregor/predates-concurrency
2 parents da7474e + 7012a05 commit 37ea254

22 files changed

+336
-165
lines changed

include/swift/AST/Attr.def

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,8 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(nonisolated, Nonisolated,
635635
APIBreakingToAdd | APIStableToRemove,
636636
112)
637637

638-
CONTEXTUAL_SIMPLE_DECL_ATTR(_unsafeSendable, UnsafeSendable,
639-
OnParam | UserInaccessible |
640-
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
641-
113)
642-
643-
CONTEXTUAL_SIMPLE_DECL_ATTR(_unsafeMainActor, UnsafeMainActor,
644-
OnParam | UserInaccessible |
645-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIBreakingToRemove,
646-
114)
638+
// 113 was experimental _unsafeSendable and is now unused
639+
// 114 was experimental _unsafeMainActor and is now unused
647640

648641
SIMPLE_DECL_ATTR(_implicitSelfCapture, ImplicitSelfCapture,
649642
OnParam | UserInaccessible |
@@ -695,6 +688,12 @@ SIMPLE_DECL_ATTR(_noAllocation, NoAllocation,
695688
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
696689
124)
697690

691+
SIMPLE_DECL_ATTR(_predatesConcurrency, PredatesConcurrency,
692+
OnFunc | OnConstructor | OnProtocol | OnGenericType | OnVar | OnSubscript |
693+
OnEnumElement | UserInaccessible |
694+
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
695+
125)
696+
698697
// If you're adding a new underscored attribute here, please document it in
699698
// docs/ReferenceGuides/UnderscoredAttributes.md.
700699

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
883883
/// but should behave like a top-level declaration. This is used by lldb.
884884
void setHoisted(bool hoisted = true) { Bits.Decl.Hoisted = hoisted; }
885885

886+
/// Whether this declaration predates the introduction of concurrency.
887+
bool predatesConcurrency() const;
888+
886889
public:
887890
bool escapedFromIfConfig() const {
888891
return Bits.Decl.EscapedFromIfConfig;

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,10 @@ ERROR(sil_inst_autodiff_invalid_witness_generic_signature,PointsToFirstBadToken,
17191719
"parameters as original function generic signature '%1'",
17201720
(StringRef, StringRef))
17211721

1722+
WARNING(warn_attr_unsafe_removed,none,
1723+
"'%0' attribute has been removed in favor of @_predatesConcurrency",
1724+
(StringRef))
1725+
17221726
//------------------------------------------------------------------------------
17231727
// MARK: Generics parsing diagnostics
17241728
//------------------------------------------------------------------------------

lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ Optional<CustomAttrNominalPair> Decl::getGlobalActorAttr() const {
718718
None);
719719
}
720720

721+
bool Decl::predatesConcurrency() const {
722+
return getAttrs().hasAttribute<PredatesConcurrencyAttr>();
723+
}
724+
725+
721726
Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
722727
assert(hasSingleExpressionBody() && "Not a single-expression body");
723728
auto braceStmt = getBody();

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,8 +1707,7 @@ static Type applyToFunctionType(
17071707
}
17081708

17091709
Type ClangImporter::Implementation::applyParamAttributes(
1710-
const clang::ParmVarDecl *param, Type type, bool &isUnsafeSendable,
1711-
bool &isUnsafeMainActor) {
1710+
const clang::ParmVarDecl *param, Type type) {
17121711
if (!param->hasAttrs())
17131712
return type;
17141713

@@ -1745,18 +1744,6 @@ Type ClangImporter::Implementation::applyParamAttributes(
17451744

17461745
continue;
17471746
}
1748-
1749-
// Map @_unsafeSendable.
1750-
if (swiftAttr->getAttribute() == "@_unsafeSendable") {
1751-
isUnsafeSendable = true;
1752-
continue;
1753-
}
1754-
1755-
// Map @_unsafeMainActor.
1756-
if (swiftAttr->getAttribute() == "@_unsafeMainActor") {
1757-
isUnsafeMainActor = true;
1758-
continue;
1759-
}
17601747
}
17611748

17621749
return type;
@@ -1953,10 +1940,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19531940
}
19541941

19551942
// Apply attributes to the type.
1956-
bool isUnsafeSendable = false;
1957-
bool isUnsafeMainActor = false;
19581943
swiftParamTy = applyParamAttributes(
1959-
param, swiftParamTy, isUnsafeSendable, isUnsafeMainActor);
1944+
param, swiftParamTy);
19601945

19611946
// Figure out the name for this parameter.
19621947
Identifier bodyName = importFullName(param, CurrentVersion)
@@ -1978,8 +1963,6 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19781963
: ParamSpecifier::Default);
19791964
paramInfo->setInterfaceType(swiftParamTy);
19801965
recordImplicitUnwrapForDecl(paramInfo, isParamTypeImplicitlyUnwrapped);
1981-
recordUnsafeConcurrencyForDecl(
1982-
paramInfo, isUnsafeSendable, isUnsafeMainActor);
19831966
parameters.push_back(paramInfo);
19841967
++index;
19851968
}
@@ -2544,10 +2527,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
25442527
}
25452528

25462529
// Apply Clang attributes to the parameter type.
2547-
bool isUnsafeSendable = false;
2548-
bool isUnsafeMainActor = false;
2549-
swiftParamTy = applyParamAttributes(
2550-
param, swiftParamTy, isUnsafeSendable, isUnsafeMainActor);
2530+
swiftParamTy = applyParamAttributes(param, swiftParamTy);
25512531

25522532
// Figure out the name for this parameter.
25532533
Identifier bodyName = importFullName(param, CurrentVersion)
@@ -2571,8 +2551,6 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
25712551
paramInfo->setSpecifier(ParamSpecifier::Default);
25722552
paramInfo->setInterfaceType(swiftParamTy);
25732553
recordImplicitUnwrapForDecl(paramInfo, paramIsIUO);
2574-
recordUnsafeConcurrencyForDecl(
2575-
paramInfo, isUnsafeSendable, isUnsafeMainActor);
25762554

25772555
// Determine whether we have a default argument.
25782556
if (kind == SpecialMethodKind::Regular ||

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -685,19 +685,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
685685
decl->setImplicitlyUnwrappedOptional(true);
686686
}
687687

688-
void recordUnsafeConcurrencyForDecl(
689-
ValueDecl *decl, bool isUnsafeSendable, bool isUnsafeMainActor) {
690-
if (isUnsafeSendable) {
691-
decl->getAttrs().add(
692-
new (SwiftContext) UnsafeSendableAttr(/*implicit=*/true));
693-
}
694-
695-
if (isUnsafeMainActor) {
696-
decl->getAttrs().add(
697-
new (SwiftContext) UnsafeMainActorAttr(/*implicit=*/true));
698-
}
699-
}
700-
701688
/// Retrieve the Clang AST context.
702689
clang::ASTContext &getClangASTContext() const {
703690
return Instance->getASTContext();
@@ -848,8 +835,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
848835
void importAttributes(const clang::NamedDecl *ClangDecl, Decl *MappedDecl,
849836
const clang::ObjCContainerDecl *NewContext = nullptr);
850837

851-
Type applyParamAttributes(const clang::ParmVarDecl *param, Type type,
852-
bool &isUnsafeSendable, bool &isUnsafeMainActor);
838+
Type applyParamAttributes(const clang::ParmVarDecl *param, Type type);
853839

854840
/// If we already imported a given decl, return the corresponding Swift decl.
855841
/// Otherwise, return nullptr.

lib/Parse/ParseDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,17 @@ ParserStatus Parser::parseDeclAttribute(
30803080
return makeParserSuccess();
30813081
}
30823082

3083+
// @_unsafeSendable and @_unsafeMainActor have been removed; warn about them.
3084+
if (DK == DAK_Count &&
3085+
(Tok.getText() == "_unsafeSendable" ||
3086+
Tok.getText() == "_unsafeMainActor")) {
3087+
StringRef attrName = Tok.getText();
3088+
SourceLoc attrLoc = consumeToken();
3089+
diagnose(AtLoc, diag::warn_attr_unsafe_removed, attrName)
3090+
.fixItRemove(SourceRange(AtLoc, attrLoc));
3091+
return makeParserSuccess();
3092+
}
3093+
30833094
if (DK != DAK_Count && !DeclAttribute::shouldBeRejectedByParser(DK)) {
30843095
parseNewDeclAttribute(Attributes, AtLoc, DK, isFromClangAttribute);
30853096
return makeParserSuccess();

lib/Sema/ConstraintSystem.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,9 @@ Type ConstraintSystem::getUnopenedTypeOfReference(
11071107
Type requestedType =
11081108
getType(value)->getWithoutSpecifierType()->getReferenceStorageReferent();
11091109

1110+
// Adjust the type for concurrency.
1111+
requestedType = adjustVarTypeForConcurrency(requestedType, value, UseDC);
1112+
11101113
// If we're dealing with contextual types, and we referenced this type from
11111114
// a different context, map the type.
11121115
if (!wantInterfaceType && requestedType->hasArchetype()) {
@@ -1757,7 +1760,11 @@ ConstraintSystem::getTypeOfMemberReference(
17571760
->castTo<AnyFunctionType>()->getParams();
17581761
// FIXME: Verify ExtInfo state is correct, not working by accident.
17591762
FunctionType::ExtInfo info;
1760-
refType = FunctionType::get(indices, elementTy, info);
1763+
auto *refFnType = FunctionType::get(indices, elementTy, info);
1764+
1765+
refType = adjustFunctionTypeForConcurrency(
1766+
refFnType, subscript, useDC, /*numApplies=*/1,
1767+
/*isMainDispatchQueue=*/false);
17611768
} else {
17621769
refType = getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy, useDC,
17631770
locator,
@@ -2051,14 +2058,17 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
20512058
->castTo<AnyFunctionType>()->getParams();
20522059
// FIXME: Verify ExtInfo state is correct, not working by accident.
20532060
FunctionType::ExtInfo info;
2054-
type = FunctionType::get(indices, elementTy, info);
2061+
type = adjustFunctionTypeForConcurrency(
2062+
FunctionType::get(indices, elementTy, info),
2063+
subscript, useDC, /*numApplies=*/1, /*isMainDispatchQueue=*/false);
20552064
} else if (auto var = dyn_cast<VarDecl>(decl)) {
20562065
type = var->getValueInterfaceType();
20572066
if (doesStorageProduceLValue(var, overload.getBaseType(), useDC)) {
20582067
type = LValueType::get(type);
20592068
} else if (type->hasDynamicSelfType()) {
20602069
type = withDynamicSelfResultReplaced(type, /*uncurryLevel=*/0);
20612070
}
2071+
type = adjustVarTypeForConcurrency(type, var, useDC);
20622072
} else if (isa<AbstractFunctionDecl>(decl) || isa<EnumElementDecl>(decl)) {
20632073
if (decl->isInstanceMember() &&
20642074
(!overload.getBaseType() ||
@@ -2087,7 +2097,10 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
20872097
}
20882098
}
20892099

2090-
type = type->castTo<FunctionType>()->getResult();
2100+
type = adjustFunctionTypeForConcurrency(
2101+
type->castTo<FunctionType>(),
2102+
subscript, useDC, /*numApplies=*/2, /*isMainDispatchQueue=*/false)
2103+
->getResult();
20912104
}
20922105
}
20932106

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,10 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
114114
IGNORED_ATTR(NonSendable)
115115
IGNORED_ATTR(AtRethrows)
116116
IGNORED_ATTR(AtReasync)
117-
IGNORED_ATTR(UnsafeSendable)
118-
IGNORED_ATTR(UnsafeMainActor)
119117
IGNORED_ATTR(ImplicitSelfCapture)
120118
IGNORED_ATTR(InheritActorContext)
121119
IGNORED_ATTR(Isolated)
120+
IGNORED_ATTR(PredatesConcurrency)
122121
#undef IGNORED_ATTR
123122

124123
void visitAlignmentAttr(AlignmentAttr *attr) {

0 commit comments

Comments
 (0)