Skip to content

Commit 04bb69f

Browse files
authored
Merge pull request #79727 from gottesmm/pr-025c1133954b74e7b4eda77e1a6f3bcafd4eb6cf
[concurrency] Add initial support for SwiftSettings to control defaultIsolation at the file level.
2 parents 6e725f2 + f64dd5a commit 04bb69f

29 files changed

+534
-28
lines changed

Runtimes/Core/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ add_library(swiftCore
185185
StringWordBreaking.swift
186186
Substring.swift
187187
SwiftNativeNSArray.swift
188+
SwiftSettings.swift
188189
TemporaryAllocation.swift
189190
ThreadLocalStorage.swift
190191
UIntBuffer.swift

include/swift/AST/ASTBridging.h

+2
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,8 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : size_t {
29902990
BridgedBuiltinExternalMacro,
29912991
/// The builtin definition for the "isolation" macro.
29922992
BridgedBuiltinIsolationMacro,
2993+
/// The builtin definition for the "SwiftSetting" macro.
2994+
BridgedBuiltinSwiftSettingsMacro,
29932995
};
29942996

29952997
struct BridgedASTType {

include/swift/AST/DiagnosticsSema.def

+9
Original file line numberDiff line numberDiff line change
@@ -8368,6 +8368,15 @@ ERROR(invalid_function_conversion_with_non_sendable,none,
83688368
NOTE(type_does_not_conform_to_Sendable,none,
83698369
"type %0 does not conform to 'Sendable' protocol", (Type))
83708370

8371+
//===----------------------------------------------------------------------===//
8372+
// MARK: SwiftSettings
8373+
//===----------------------------------------------------------------------===//
8374+
8375+
ERROR(swift_settings_invalid_setting, none,
8376+
"Unrecognized setting passed to #SwiftSettings", ())
8377+
8378+
ERROR(swift_settings_must_be_top_level, none,
8379+
"#SwiftSettings can only be invoked as a top level declaration", ())
83718380

83728381
#define UNDEFINE_DIAGNOSTIC_MACROS
83738382
#include "DefineDiagnosticMacros.h"

include/swift/AST/FileUnit.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
3939
friend class DirectOperatorLookupRequest;
4040
friend class DirectPrecedenceGroupLookupRequest;
4141

42-
// The pointer is FileUnit insted of SynthesizedFileUnit to break circularity.
42+
/// The pointer is FileUnit insted of SynthesizedFileUnit to break
43+
/// circularity.
4344
llvm::PointerIntPair<FileUnit *, 3, FileUnitKind> SynthesizedFileAndKind;
4445

4546
protected:

include/swift/AST/KnownStdlibTypes.def

+2
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,6 @@ KNOWN_STDLIB_TYPE_DECL(Result, NominalTypeDecl, 2)
101101

102102
KNOWN_STDLIB_TYPE_DECL(InlineArray, NominalTypeDecl, 2)
103103

104+
KNOWN_STDLIB_TYPE_DECL(SwiftSetting, NominalTypeDecl, 0)
105+
104106
#undef KNOWN_STDLIB_TYPE_DECL

include/swift/AST/MacroDefinition.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#ifndef SWIFT_AST_MACRO_DEFINITION_H
1919
#define SWIFT_AST_MACRO_DEFINITION_H
2020

21+
#include "swift/AST/Identifier.h"
2122
#include "swift/Basic/StringExtras.h"
23+
24+
#include "llvm/ADT/ArrayRef.h"
2225
#include "llvm/ADT/PointerUnion.h"
2326

2427
namespace swift {
@@ -69,11 +72,14 @@ struct ExternalMacroReference {
6972
};
7073

7174
/// Describes the known kinds of builtin macros.
72-
enum class BuiltinMacroKind: uint8_t {
75+
enum class BuiltinMacroKind : uint8_t {
7376
/// #externalMacro, which references an external macro.
7477
ExternalMacro,
7578
/// #isolation, which produces the isolation of the current context
7679
IsolationMacro,
80+
/// #SwiftSettings, which allows for the user to set a compiler setting at
81+
/// the file level
82+
SwiftSettingsMacro,
7783
};
7884

7985
/// A single replacement

include/swift/AST/SourceFile.h

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ enum class RestrictedImportKind {
5353
/// Import that limits the access level of imported entities.
5454
using ImportAccessLevel = std::optional<AttributedImport<ImportedModule>>;
5555

56+
/// Language options only for use with a specific SourceFile.
57+
///
58+
/// Vended by SourceFile::getLanguageOptions().
59+
struct SourceFileLangOptions {
60+
/// If unset, no value was provided. If a Type, that type is the type of the
61+
/// isolation. If set to an empty type, nil was specified explicitly.
62+
std::optional<Type> defaultIsolation;
63+
};
64+
5665
/// A file containing Swift source code.
5766
///
5867
/// This is a .swift or .sil file (or a virtual file, such as the contents of
@@ -562,6 +571,9 @@ class SourceFile final : public FileUnit {
562571
ObjCSelector selector,
563572
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
564573

574+
/// File level language options.
575+
SourceFileLangOptions getLanguageOptions() const;
576+
565577
protected:
566578
virtual void
567579
lookupOperatorDirect(Identifier name, OperatorFixity fixity,

include/swift/AST/TypeCheckRequests.h

+20
Original file line numberDiff line numberDiff line change
@@ -5251,6 +5251,26 @@ class SemanticAvailabilitySpecRequest
52515251
void cacheResult(std::optional<SemanticAvailabilitySpec> value) const;
52525252
};
52535253

5254+
class SourceFileLangOptionsRequest
5255+
: public SimpleRequest<SourceFileLangOptionsRequest,
5256+
SourceFileLangOptions(SourceFile *),
5257+
RequestFlags::Cached> {
5258+
5259+
public:
5260+
using SimpleRequest::SimpleRequest;
5261+
5262+
private:
5263+
friend SimpleRequest;
5264+
5265+
SourceFileLangOptions evaluate(Evaluator &evaluator,
5266+
SourceFile *sourceFile) const;
5267+
5268+
public:
5269+
bool isCached() const { return true; }
5270+
std::optional<SourceFileLangOptions> getCachedResult() const;
5271+
void cacheResult(SourceFileLangOptions value) const;
5272+
};
5273+
52545274
#define SWIFT_TYPEID_ZONE TypeChecker
52555275
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
52565276
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

+3
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,6 @@ SWIFT_REQUEST(TypeChecker, SemanticAvailabilitySpecRequest,
620620
std::optional<SemanticAvailabilitySpec>
621621
(const AvailabilitySpec *, const DeclContext *),
622622
SeparatelyCached, NoLocationInfo)
623+
624+
SWIFT_REQUEST(TypeChecker, SourceFileLangOptionsRequest,
625+
SourceFileLangOptions (SourceFile *), Cached, NoLocationInfo)

include/swift/Basic/Features.def

+3
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ EXPERIMENTAL_FEATURE(ExtensibleEnums, true)
508508
/// Allow isolated conformances.
509509
EXPERIMENTAL_FEATURE(IsolatedConformances, true)
510510

511+
/// Allow SwiftSettings
512+
EXPERIMENTAL_FEATURE(SwiftSettings, false)
513+
511514
/// Syntax sugar features for concurrency.
512515
EXPERIMENTAL_FEATURE(ConcurrencySyntaxSugar, true)
513516

lib/AST/ASTPrinter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -4710,6 +4710,9 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
47104710
case BuiltinMacroKind::IsolationMacro:
47114711
Printer << "IsolationMacro";
47124712
break;
4713+
case BuiltinMacroKind::SwiftSettingsMacro:
4714+
Printer << "SwiftSettingsMacro";
4715+
break;
47134716
}
47144717
break;
47154718

lib/AST/FeatureSet.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ using namespace swift;
2626

2727
/// Does the interface of this declaration use a type for which the
2828
/// given predicate returns true?
29-
static bool usesTypeMatching(Decl *decl, llvm::function_ref<bool(Type)> fn) {
29+
static bool usesTypeMatching(const Decl *decl,
30+
llvm::function_ref<bool(Type)> fn) {
3031
if (auto value = dyn_cast<ValueDecl>(decl)) {
3132
if (Type type = value->getInterfaceType()) {
3233
return type.findIf(fn);
@@ -393,6 +394,14 @@ UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
393394
UNINTERESTING_FEATURE(ImportNonPublicCxxMembers)
394395
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)
395396

397+
static bool usesFeatureSwiftSettings(const Decl *decl) {
398+
// We just need to guard `#SwiftSettings`.
399+
auto *macro = dyn_cast<MacroDecl>(decl);
400+
return macro && macro->isStdlibDecl() &&
401+
macro->getMacroRoles().contains(MacroRole::Declaration) &&
402+
macro->getBaseIdentifier().is("SwiftSettings");
403+
}
404+
396405
bool swift::usesFeatureIsolatedDeinit(const Decl *decl) {
397406
if (auto cd = dyn_cast<ClassDecl>(decl)) {
398407
return cd->getFormalAccess() == AccessLevel::Open &&

0 commit comments

Comments
 (0)