Skip to content

Commit e1e9f04

Browse files
authored
Merge pull request swiftlang#81863 from xedin/using-for-default-isolation-in-file-context
[AST/Sema] SE-0478: Implement `using` declaration under an experimental flag
2 parents db47bfd + bc61bfb commit e1e9f04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+590
-9
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ final public class TopLevelCodeDecl: Decl {}
124124

125125
final public class ImportDecl: Decl {}
126126

127+
final public class UsingDecl: Decl {}
128+
127129
final public class PrecedenceGroupDecl: Decl {}
128130

129131
final public class MissingDecl: Decl {}

SwiftCompilerSources/Sources/AST/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public func registerAST() {
3636
registerDecl(ExtensionDecl.self)
3737
registerDecl(TopLevelCodeDecl.self)
3838
registerDecl(ImportDecl.self)
39+
registerDecl(UsingDecl.self)
3940
registerDecl(PrecedenceGroupDecl.self)
4041
registerDecl(MissingDecl.self)
4142
registerDecl(MissingMemberDecl.self)

include/swift/AST/ASTBridging.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,19 @@ BridgedImportDecl BridgedImportDecl_createParsed(
17441744
BridgedSourceLoc cImportKeywordLoc, BridgedImportKind cImportKind,
17451745
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements);
17461746

1747+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedUsingSpecifier {
1748+
BridgedUsingSpecifierMainActor,
1749+
BridgedUsingSpecifierNonisolated,
1750+
};
1751+
1752+
SWIFT_NAME("BridgedUsingDecl.createParsed(_:declContext:usingKeywordLoc:"
1753+
"specifierLoc:specifier:)")
1754+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
1755+
BridgedDeclContext cDeclContext,
1756+
BridgedSourceLoc usingKeywordLoc,
1757+
BridgedSourceLoc specifierLoc,
1758+
BridgedUsingSpecifier specifier);
1759+
17471760
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
17481761
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
17491762
"arrowLoc:returnType:genericWhereClause:)")

include/swift/AST/Decl.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ enum class DescriptiveDeclKind : uint8_t {
213213
OpaqueResultType,
214214
OpaqueVarType,
215215
Macro,
216-
MacroExpansion
216+
MacroExpansion,
217+
Using
217218
};
218219

219220
/// Describes which spelling was used in the source for the 'static' or 'class'
@@ -267,6 +268,16 @@ static_assert(uint8_t(SelfAccessKind::LastSelfAccessKind) <
267268
"Self Access Kind is too small to fit in SelfAccess kind bits. "
268269
"Please expand ");
269270

271+
enum class UsingSpecifier : uint8_t {
272+
MainActor,
273+
Nonisolated,
274+
LastSpecifier = Nonisolated,
275+
};
276+
enum : unsigned {
277+
NumUsingSpecifierBits =
278+
countBitsUsed(static_cast<unsigned>(UsingSpecifier::LastSpecifier))
279+
};
280+
270281
/// Diagnostic printing of \c SelfAccessKind.
271282
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SelfAccessKind SAK);
272283

@@ -827,6 +838,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
827838
NumPathElements : 8
828839
);
829840

841+
SWIFT_INLINE_BITFIELD(UsingDecl, Decl, NumUsingSpecifierBits,
842+
Specifier : NumUsingSpecifierBits
843+
);
844+
830845
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1,
831846
/// An encoding of the default and maximum access level for this extension.
832847
/// The value 4 corresponds to AccessLevel::Public
@@ -9737,6 +9752,34 @@ class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {
97379752
}
97389753
};
97399754

9755+
/// UsingDecl - This represents a single `using` declaration, e.g.:
9756+
/// using @MainActor
9757+
class UsingDecl : public Decl {
9758+
friend class Decl;
9759+
9760+
private:
9761+
SourceLoc UsingLoc, SpecifierLoc;
9762+
9763+
UsingDecl(SourceLoc usingLoc, SourceLoc specifierLoc,
9764+
UsingSpecifier specifier, DeclContext *parent);
9765+
9766+
public:
9767+
UsingSpecifier getSpecifier() const {
9768+
return static_cast<UsingSpecifier>(Bits.UsingDecl.Specifier);
9769+
}
9770+
9771+
std::string getSpecifierName() const;
9772+
9773+
SourceLoc getLocFromSource() const { return UsingLoc; }
9774+
SourceRange getSourceRange() const { return {UsingLoc, SpecifierLoc}; }
9775+
9776+
static UsingDecl *create(ASTContext &ctx, SourceLoc usingLoc,
9777+
SourceLoc specifierLoc, UsingSpecifier specifier,
9778+
DeclContext *parent);
9779+
9780+
static bool classof(const Decl *D) { return D->getKind() == DeclKind::Using; }
9781+
};
9782+
97409783
inline void
97419784
AbstractStorageDecl::overwriteSetterAccess(AccessLevel accessLevel) {
97429785
Accessors.setInt(accessLevel);

include/swift/AST/DeclExportabilityVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class DeclExportabilityVisitor
158158
UNREACHABLE(MissingMember);
159159
UNREACHABLE(GenericTypeParam);
160160
UNREACHABLE(Param);
161+
UNREACHABLE(Using);
161162

162163
#undef UNREACHABLE
163164

include/swift/AST/DeclNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ DECL(Missing, Decl)
190190
DECL(MissingMember, Decl)
191191
DECL(PatternBinding, Decl)
192192
DECL(EnumCase, Decl)
193+
DECL(Using, Decl)
193194

194195
ABSTRACT_DECL(Operator, Decl)
195196
OPERATOR_DECL(InfixOperator, OperatorDecl)

include/swift/AST/DiagnosticsParse.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,5 +2187,14 @@ ERROR(nonisolated_nonsending_expected_rparen,PointsToFirstBadToken,
21872187
ERROR(nonisolated_nonsending_repeated,none,
21882188
"parameter may have at most one 'nonisolated(nonsending)' specifier", ())
21892189

2190+
//------------------------------------------------------------------------------
2191+
// MARK: using @<attribute> or using <identifier>
2192+
//------------------------------------------------------------------------------
2193+
ERROR(using_decl_invalid_specifier,PointsToFirstBadToken,
2194+
"default isolation can only be set to '@MainActor' or 'nonisolated'",
2195+
())
2196+
ERROR(experimental_using_decl_disabled,PointsToFirstBadToken,
2197+
"'using' is an experimental feature that is currently disabled", ())
2198+
21902199
#define UNDEFINE_DIAGNOSTIC_MACROS
21912200
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8782,5 +8782,13 @@ ERROR(extensible_attr_on_internal_type,none,
87828782
ERROR(pre_enum_extensibility_without_extensible,none,
87838783
"%0 can only be used together with '@extensible' attribute", (DeclAttribute))
87848784

8785+
//===----------------------------------------------------------------------===//
8786+
// MARK: `using` declaration
8787+
//===----------------------------------------------------------------------===//
8788+
ERROR(invalid_redecl_of_file_isolation,none,
8789+
"invalid redeclaration of file-level default actor isolation", ())
8790+
NOTE(invalid_redecl_of_file_isolation_prev,none,
8791+
"default isolation was previously declared here", ())
8792+
87858793
#define UNDEFINE_DIAGNOSTIC_MACROS
87868794
#include "DefineDiagnosticMacros.h"

include/swift/AST/SourceFile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GeneratedSourceInfo;
3232
class PersistentParserState;
3333
struct SourceFileExtras;
3434
class Token;
35+
enum class DefaultIsolation : uint8_t;
3536

3637
/// Kind of import affecting how a decl can be reexported.
3738
///
@@ -690,6 +691,11 @@ class SourceFile final : public FileUnit {
690691
DelayedParserState = std::move(state);
691692
}
692693

694+
/// Retrieve default action isolation to be used for this source file.
695+
/// It's determine based on on top-level `using <<isolation>>` declaration
696+
/// found in the file.
697+
std::optional<DefaultIsolation> getDefaultIsolation() const;
698+
693699
SWIFT_DEBUG_DUMP;
694700
void
695701
dump(raw_ostream &os,

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5332,6 +5332,23 @@ class SemanticAvailabilitySpecRequest
53325332
void cacheResult(std::optional<SemanticAvailabilitySpec> value) const;
53335333
};
53345334

5335+
class DefaultIsolationInSourceFileRequest
5336+
: public SimpleRequest<DefaultIsolationInSourceFileRequest,
5337+
std::optional<DefaultIsolation>(const SourceFile *),
5338+
RequestFlags::Cached> {
5339+
public:
5340+
using SimpleRequest::SimpleRequest;
5341+
5342+
private:
5343+
friend SimpleRequest;
5344+
5345+
std::optional<DefaultIsolation> evaluate(Evaluator &evaluator,
5346+
const SourceFile *file) const;
5347+
5348+
public:
5349+
bool isCached() const { return true; }
5350+
};
5351+
53355352
#define SWIFT_TYPEID_ZONE TypeChecker
53365353
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
53375354
#include "swift/Basic/DefineTypeIDZone.h"

0 commit comments

Comments
 (0)