Skip to content

Commit e1e9f04

Browse files
authored
Merge pull request #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"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,7 @@ SWIFT_REQUEST(TypeChecker, SemanticAvailabilitySpecRequest,
629629
std::optional<SemanticAvailabilitySpec>
630630
(const AvailabilitySpec *, const DeclContext *),
631631
SeparatelyCached, NoLocationInfo)
632+
633+
SWIFT_REQUEST(TypeChecker, DefaultIsolationInSourceFileRequest,
634+
std::optional<DefaultIsolation>(const SourceFile *),
635+
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
@@ -521,6 +521,10 @@ SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)
521521
/// Allow use of `Module::name` syntax
522522
EXPERIMENTAL_FEATURE(ModuleSelector, false)
523523

524+
/// Allow use of `using` declaration that control default isolation
525+
/// in a file scope.
526+
EXPERIMENTAL_FEATURE(DefaultIsolationPerFile, false)
527+
524528
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
525529
#undef EXPERIMENTAL_FEATURE
526530
#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
@@ -2114,6 +2114,11 @@ namespace {
21142114
printFoot();
21152115
}
21162116

2117+
void visitUsingDecl(UsingDecl *UD, Label label) {
2118+
printCommon(UD, "using_decl", label);
2119+
printFieldQuoted(UD->getSpecifierName(), Label::always("specifier"));
2120+
}
2121+
21172122
void visitExtensionDecl(ExtensionDecl *ED, Label label) {
21182123
printCommon(ED, "extension_decl", label, ExtensionColor);
21192124
printFlag(!ED->hasBeenBound(), "unbound");

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5426,6 +5426,7 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
54265426
case DeclKind::PrefixOperator:
54275427
case DeclKind::PostfixOperator:
54285428
case DeclKind::MacroExpansion:
5429+
case DeclKind::Using:
54295430
break;
54305431
};
54315432
}

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
};
@@ -3052,6 +3057,11 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
30523057
[&] { Printer << "."; });
30533058
}
30543059

3060+
void PrintAST::visitUsingDecl(UsingDecl *decl) {
3061+
Printer.printIntroducerKeyword("using", Options, " ");
3062+
Printer << decl->getSpecifierName();
3063+
}
3064+
30553065
void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
30563066
bool OldFullyQualifiedTypesIfAmbiguous =
30573067
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
@@ -637,6 +637,17 @@ BridgedImportDecl BridgedImportDecl_createParsed(
637637
std::move(builder).get());
638638
}
639639

640+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
641+
BridgedDeclContext cDeclContext,
642+
BridgedSourceLoc usingKeywordLoc,
643+
BridgedSourceLoc specifierLoc,
644+
BridgedUsingSpecifier specifier) {
645+
ASTContext &ctx = cContext.unbridged();
646+
return UsingDecl::create(
647+
ctx, usingKeywordLoc.unbridged(), specifierLoc.unbridged(),
648+
static_cast<UsingSpecifier>(specifier), cDeclContext.unbridged());
649+
}
650+
640651
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
641652
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
642653
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,

0 commit comments

Comments
 (0)