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"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,7 @@ SWIFT_REQUEST(TypeChecker, SemanticAvailabilitySpecRequest,
630630
std::optional<SemanticAvailabilitySpec>
631631
(const AvailabilitySpec *, const DeclContext *),
632632
SeparatelyCached, NoLocationInfo)
633+
634+
SWIFT_REQUEST(TypeChecker, DefaultIsolationInSourceFileRequest,
635+
std::optional<DefaultIsolation>(const SourceFile *),
636+
Cached, NoLocationInfo)

include/swift/AST/TypeMemberVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
4141
BAD_MEMBER(Operator)
4242
BAD_MEMBER(PrecedenceGroup)
4343
BAD_MEMBER(Macro)
44+
BAD_MEMBER(Using)
4445

4546
RetTy visitMacroExpansionDecl(MacroExpansionDecl *D) {
4647
// Expansion already visited as auxiliary decls.

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)
522522
/// Optimize copies of ObjectiveC blocks.
523523
EXPERIMENTAL_FEATURE(CopyBlockOptimization, true)
524524

525+
/// Allow use of `using` declaration that control default isolation
526+
/// in a file scope.
527+
EXPERIMENTAL_FEATURE(DefaultIsolationPerFile, false)
528+
525529
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
526530
#undef EXPERIMENTAL_FEATURE
527531
#undef UPCOMING_FEATURE

include/swift/IDE/CodeCompletionResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ enum class CodeCompletionKeywordKind : uint8_t {
190190
enum class CompletionKind : uint8_t {
191191
None,
192192
Import,
193+
Using,
193194
UnresolvedMember,
194195
DotExpr,
195196
StmtOrExpr,

include/swift/IDE/CompletionLookup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
325325

326326
void addImportModuleNames();
327327

328+
void addUsingSpecifiers();
329+
328330
SemanticContextKind getSemanticContext(const Decl *D,
329331
DeclVisibilityKind Reason,
330332
DynamicLookupInfo dynamicLookupInfo);

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ class CodeCompletionCallbacks {
255255
virtual void
256256
completeImportDecl(ImportPath::Builder &Path) {};
257257

258+
/// Complete the 'using' decl with supported specifiers.
259+
virtual void
260+
completeUsingDecl() {};
261+
258262
/// Complete unresolved members after dot.
259263
virtual void completeUnresolvedMember(CodeCompletionExpr *E,
260264
SourceLoc DotLoc) {};

include/swift/Parse/Parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,9 @@ class Parser {
12231223
ParserResult<ImportDecl> parseDeclImport(ParseDeclOptions Flags,
12241224
DeclAttributes &Attributes);
12251225

1226+
ParserResult<UsingDecl> parseDeclUsing(ParseDeclOptions Flags,
1227+
DeclAttributes &Attributes);
1228+
12261229
/// Parse an inheritance clause into a vector of InheritedEntry's.
12271230
///
12281231
/// \param allowClassRequirement whether to permit parsing of 'class'

include/swift/Parse/Token.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ class Token {
196196
#define CONTEXTUAL_SIMPLE_DECL_ATTR(KW, ...) CONTEXTUAL_CASE(KW)
197197
#include "swift/AST/DeclAttr.def"
198198
#undef CONTEXTUAL_CASE
199-
.Case("macro", true)
200-
.Default(false);
199+
.Case("macro", true)
200+
.Case("using", true)
201+
.Default(false);
201202
}
202203

203204
bool isContextualPunctuator(StringRef ContextPunc) const {

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,11 @@ namespace {
21262126
printFoot();
21272127
}
21282128

2129+
void visitUsingDecl(UsingDecl *UD, Label label) {
2130+
printCommon(UD, "using_decl", label);
2131+
printFieldQuoted(UD->getSpecifierName(), Label::always("specifier"));
2132+
}
2133+
21292134
void visitExtensionDecl(ExtensionDecl *ED, Label label) {
21302135
printCommon(ED, "extension_decl", label, ExtensionColor);
21312136
printFlag(!ED->hasBeenBound(), "unbound");

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5429,6 +5429,7 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
54295429
case DeclKind::PrefixOperator:
54305430
case DeclKind::PostfixOperator:
54315431
case DeclKind::MacroExpansion:
5432+
case DeclKind::Using:
54325433
break;
54335434
};
54345435
}

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
383383
}
384384
}
385385

386+
// The `using` declarations are private to the file at the moment
387+
// and shouldn't appear in swift interfaces.
388+
if (isa<UsingDecl>(D))
389+
return false;
390+
386391
return ShouldPrintChecker::shouldPrint(D, options);
387392
}
388393
};
@@ -3058,6 +3063,11 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
30583063
[&] { Printer << "."; });
30593064
}
30603065

3066+
void PrintAST::visitUsingDecl(UsingDecl *decl) {
3067+
Printer.printIntroducerKeyword("using", Options, " ");
3068+
Printer << decl->getSpecifierName();
3069+
}
3070+
30613071
void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
30623072
bool OldFullyQualifiedTypesIfAmbiguous =
30633073
Options.FullyQualifiedTypesIfAmbiguous;

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class NodeAdder
404404
VISIT_AND_IGNORE(ParamDecl)
405405
VISIT_AND_IGNORE(MissingDecl)
406406
VISIT_AND_IGNORE(MissingMemberDecl)
407+
VISIT_AND_IGNORE(UsingDecl)
407408

408409
// This declaration is handled from the PatternBindingDecl
409410
VISIT_AND_IGNORE(VarDecl)

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
201201
return false;
202202
}
203203

204+
bool visitUsingDecl(UsingDecl *UD) {
205+
return false;
206+
}
207+
204208
bool visitExtensionDecl(ExtensionDecl *ED) {
205209
if (auto *typeRepr = ED->getExtendedTypeRepr())
206210
if (doIt(typeRepr))

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ BridgedImportDecl BridgedImportDecl_createParsed(
619619
std::move(builder).get());
620620
}
621621

622+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
623+
BridgedDeclContext cDeclContext,
624+
BridgedSourceLoc usingKeywordLoc,
625+
BridgedSourceLoc specifierLoc,
626+
BridgedUsingSpecifier specifier) {
627+
ASTContext &ctx = cContext.unbridged();
628+
return UsingDecl::create(
629+
ctx, usingKeywordLoc.unbridged(), specifierLoc.unbridged(),
630+
static_cast<UsingSpecifier>(specifier), cDeclContext.unbridged());
631+
}
632+
622633
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
623634
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
624635
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,

0 commit comments

Comments
 (0)