Skip to content

Commit 608a37a

Browse files
authored
Merge pull request #81997 from xedin/using-for-default-isolation-in-file-context-6.2
[6.2][AST/Sema] SE-0478: Implement using declaration under an experimental flag
2 parents 916e96d + 6478305 commit 608a37a

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

+592
-11
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

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

118118
final public class ImportDecl: Decl {}
119119

120+
final public class UsingDecl: Decl {}
121+
120122
final public class PrecedenceGroupDecl: Decl {}
121123

122124
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
@@ -1714,6 +1714,19 @@ BridgedImportDecl BridgedImportDecl_createParsed(
17141714
BridgedSourceLoc cImportKeywordLoc, BridgedImportKind cImportKind,
17151715
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements);
17161716

1717+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedUsingSpecifier {
1718+
BridgedUsingSpecifierMainActor,
1719+
BridgedUsingSpecifierNonisolated,
1720+
};
1721+
1722+
SWIFT_NAME("BridgedUsingDecl.createParsed(_:declContext:usingKeywordLoc:"
1723+
"specifierLoc:specifier:)")
1724+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
1725+
BridgedDeclContext cDeclContext,
1726+
BridgedSourceLoc usingKeywordLoc,
1727+
BridgedSourceLoc specifierLoc,
1728+
BridgedUsingSpecifier specifier);
1729+
17171730
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
17181731
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
17191732
"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

@@ -830,6 +841,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
830841
NumPathElements : 8
831842
);
832843

844+
SWIFT_INLINE_BITFIELD(UsingDecl, Decl, NumUsingSpecifierBits,
845+
Specifier : NumUsingSpecifierBits
846+
);
847+
833848
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1,
834849
/// An encoding of the default and maximum access level for this extension.
835850
/// The value 4 corresponds to AccessLevel::Public
@@ -9738,6 +9753,34 @@ class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {
97389753
}
97399754
};
97409755

9756+
/// UsingDecl - This represents a single `using` declaration, e.g.:
9757+
/// using @MainActor
9758+
class UsingDecl : public Decl {
9759+
friend class Decl;
9760+
9761+
private:
9762+
SourceLoc UsingLoc, SpecifierLoc;
9763+
9764+
UsingDecl(SourceLoc usingLoc, SourceLoc specifierLoc,
9765+
UsingSpecifier specifier, DeclContext *parent);
9766+
9767+
public:
9768+
UsingSpecifier getSpecifier() const {
9769+
return static_cast<UsingSpecifier>(Bits.UsingDecl.Specifier);
9770+
}
9771+
9772+
std::string getSpecifierName() const;
9773+
9774+
SourceLoc getLocFromSource() const { return UsingLoc; }
9775+
SourceRange getSourceRange() const { return {UsingLoc, SpecifierLoc}; }
9776+
9777+
static UsingDecl *create(ASTContext &ctx, SourceLoc usingLoc,
9778+
SourceLoc specifierLoc, UsingSpecifier specifier,
9779+
DeclContext *parent);
9780+
9781+
static bool classof(const Decl *D) { return D->getKind() == DeclKind::Using; }
9782+
};
9783+
97419784
inline void
97429785
AbstractStorageDecl::overwriteSetterAccess(AccessLevel accessLevel) {
97439786
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
@@ -2180,5 +2180,14 @@ ERROR(nonisolated_nonsending_expected_rparen,PointsToFirstBadToken,
21802180
ERROR(nonisolated_nonsending_repeated,none,
21812181
"parameter may have at most one 'nonisolated(nonsending)' specifier", ())
21822182

2183+
//------------------------------------------------------------------------------
2184+
// MARK: using @<attribute> or using <identifier>
2185+
//------------------------------------------------------------------------------
2186+
ERROR(using_decl_invalid_specifier,PointsToFirstBadToken,
2187+
"default isolation can only be set to '@MainActor' or 'nonisolated'",
2188+
())
2189+
ERROR(experimental_using_decl_disabled,PointsToFirstBadToken,
2190+
"'using' is an experimental feature that is currently disabled", ())
2191+
21832192
#define UNDEFINE_DIAGNOSTIC_MACROS
21842193
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8676,5 +8676,14 @@ GROUPED_WARNING(
86768676
"behavior",
86778677
(StringRef))
86788678

8679+
//===----------------------------------------------------------------------===//
8680+
// MARK: `using` declaration
8681+
//===----------------------------------------------------------------------===//
8682+
ERROR(invalid_redecl_of_file_isolation,none,
8683+
"invalid redeclaration of file-level default actor isolation", ())
8684+
NOTE(invalid_redecl_of_file_isolation_prev,none,
8685+
"default isolation was previously declared here", ())
8686+
8687+
86798688
#define UNDEFINE_DIAGNOSTIC_MACROS
86808689
#include "DefineDiagnosticMacros.h"

include/swift/AST/SourceFile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AvailabilityScope;
3131
class PersistentParserState;
3232
struct SourceFileExtras;
3333
class Token;
34+
enum class DefaultIsolation : uint8_t;
3435

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

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

include/swift/AST/TypeCheckRequests.h

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

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

0 commit comments

Comments
 (0)