Skip to content

Commit 3bfdce6

Browse files
committed
Merge branch 'main' into class_templates
2 parents c082a8c + f55f290 commit 3bfdce6

Some content is hidden

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

48 files changed

+1432
-114
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ class ASTContext final {
522522
/// Get Sequence.makeIterator().
523523
FuncDecl *getSequenceMakeIterator() const;
524524

525+
/// Get AsyncSequence.makeAsyncIterator().
526+
FuncDecl *getAsyncSequenceMakeAsyncIterator() const;
527+
525528
/// Check whether the standard library provides all the correct
526529
/// intrinsic support for Optional<T>.
527530
///

include/swift/AST/ASTTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ SWIFT_TYPEID(PropertyWrapperTypeInfo)
3131
SWIFT_TYPEID(Requirement)
3232
SWIFT_TYPEID(ResilienceExpansion)
3333
SWIFT_TYPEID(FragileFunctionKind)
34+
SWIFT_TYPEID(FunctionRethrowingKind)
35+
SWIFT_TYPEID(ProtocolRethrowsRequirementList)
3436
SWIFT_TYPEID(TangentPropertyInfo)
3537
SWIFT_TYPEID(SymbolSourceMap)
3638
SWIFT_TYPEID(Type)

include/swift/AST/ASTTypeIDs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ProtocolDecl;
6464
class Requirement;
6565
enum class ResilienceExpansion : unsigned;
6666
struct FragileFunctionKind;
67+
enum class FunctionRethrowingKind : uint8_t;
68+
class ProtocolRethrowsRequirementList;
6769
class SourceFile;
6870
class SymbolSourceMap;
6971
struct TangentPropertyInfo;

include/swift/AST/Attr.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ SIMPLE_DECL_ATTR(rethrows, Rethrows,
359359
RejectByParser |
360360
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
361361
57)
362+
SIMPLE_DECL_ATTR(rethrows, AtRethrows,
363+
OnProtocol |
364+
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
365+
58)
362366
DECL_ATTR(_swift_native_objc_runtime_base, SwiftNativeObjCRuntimeBase,
363367
OnClass |
364368
UserInaccessible |

include/swift/AST/Decl.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,66 @@ enum class KnownDerivableProtocolKind : uint8_t {
38913891
Actor,
38923892
};
38933893

3894+
class ProtocolRethrowsRequirementList {
3895+
public:
3896+
typedef std::pair<Type, ValueDecl *> Entry;
3897+
3898+
private:
3899+
ArrayRef<Entry> entries;
3900+
3901+
public:
3902+
ProtocolRethrowsRequirementList(ArrayRef<Entry> entries) : entries(entries) {}
3903+
ProtocolRethrowsRequirementList() : entries() {}
3904+
3905+
typedef const Entry *const_iterator;
3906+
typedef const_iterator iterator;
3907+
3908+
const_iterator begin() const { return entries.begin(); }
3909+
const_iterator end() const { return entries.end(); }
3910+
3911+
size_t size() const { return entries.size(); }
3912+
3913+
void print(raw_ostream &OS) const;
3914+
3915+
SWIFT_DEBUG_DUMP;
3916+
3917+
friend bool operator==(const ProtocolRethrowsRequirementList &lhs,
3918+
const ProtocolRethrowsRequirementList &rhs) {
3919+
if (lhs.size() != rhs.size()) {
3920+
return false;
3921+
}
3922+
auto lhsIter = lhs.begin();
3923+
auto rhsIter = rhs.begin();
3924+
while (lhsIter != lhs.end() && rhsIter != rhs.end()) {
3925+
if (lhsIter->first->isEqual(rhsIter->first)) {
3926+
return false;
3927+
}
3928+
if (lhsIter->second != rhsIter->second) {
3929+
return false;
3930+
}
3931+
}
3932+
return true;
3933+
}
3934+
3935+
friend bool operator!=(const ProtocolRethrowsRequirementList &lhs,
3936+
const ProtocolRethrowsRequirementList &rhs) {
3937+
return !(lhs == rhs);
3938+
}
3939+
3940+
friend llvm::hash_code hash_value(
3941+
const ProtocolRethrowsRequirementList &list) {
3942+
return llvm::hash_combine(list.size()); // it is good enought for
3943+
// llvm::hash_code hash;
3944+
// for (auto entry : list) {
3945+
// hash = llvm::hash_combine(hash, entry.first->getCanonicalType());
3946+
// hash = llvm::hash_combine(hash, entry.second);
3947+
// }
3948+
// return hash;
3949+
}
3950+
};
3951+
3952+
void simple_display(raw_ostream &out, const ProtocolRethrowsRequirementList reqs);
3953+
38943954
/// ProtocolDecl - A declaration of a protocol, for example:
38953955
///
38963956
/// protocol Drawable {
@@ -4072,6 +4132,9 @@ class ProtocolDecl final : public NominalTypeDecl {
40724132
/// contain 'Self' in 'parameter' or 'other' position.
40734133
bool existentialTypeSupported() const;
40744134

4135+
ProtocolRethrowsRequirementList getRethrowingRequirements() const;
4136+
bool isRethrowingProtocol() const;
4137+
40754138
private:
40764139
void computeKnownProtocolKind() const;
40774140

@@ -5481,6 +5544,23 @@ class ImportAsMemberStatus {
54815544
}
54825545
};
54835546

5547+
enum class FunctionRethrowingKind : uint8_t {
5548+
/// The function is not throwing
5549+
None,
5550+
5551+
/// The function rethrows by closure
5552+
ByClosure,
5553+
5554+
/// The function rethrows by conformance
5555+
ByConformance,
5556+
5557+
/// The function throws
5558+
Throws,
5559+
5560+
/// The function throwing determinate is invalid
5561+
Invalid
5562+
};
5563+
54845564
/// Base class for function-like declarations.
54855565
class AbstractFunctionDecl : public GenericContext, public ValueDecl {
54865566
friend class NeedsNewVTableEntryRequest;
@@ -5683,6 +5763,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
56835763
/// Returns true if the function body throws.
56845764
bool hasThrows() const { return Bits.AbstractFunctionDecl.Throws; }
56855765

5766+
FunctionRethrowingKind getRethrowingKind() const;
5767+
56865768
// FIXME: Hack that provides names with keyword arguments for accessors.
56875769
DeclName getEffectiveFullName() const;
56885770

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ ERROR(protocol_method_argument_init,none,
791791
"default argument not permitted in a protocol method", ())
792792
ERROR(protocol_init_argument_init,none,
793793
"default argument not permitted in a protocol initializer", ())
794+
ERROR(protocol_subscript_argument_init,none,
795+
"default argument not permitted in a protocol subscript", ())
794796
ERROR(tuple_type_multiple_labels,none,
795797
"tuple element cannot have two labels", ())
796798

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,6 +2987,8 @@ ERROR(override_rethrows_with_non_rethrows,none,
29872987
"be 'rethrows'", (bool))
29882988
ERROR(rethrows_without_throwing_parameter,none,
29892989
"'rethrows' function must take a throwing function argument", ())
2990+
ERROR(rethrows_attr_on_non_protocol,none,
2991+
"@rethrows may only be used on 'protocol' declarations", ())
29902992

29912993
ERROR(autoclosure_function_type,none,
29922994
"@autoclosure attribute only applies to function types",
@@ -4063,6 +4065,8 @@ NOTE(because_rethrows_argument_throws,none,
40634065
NOTE(because_rethrows_default_argument_throws,none,
40644066
"call is to 'rethrows' function, but a defaulted argument function"
40654067
" can throw", ())
4068+
NOTE(because_rethrows_default_conformance_throws,none,
4069+
"call is to 'rethrows' function, but a conformance has a throwing witness", ())
40664070

40674071
ERROR(throwing_call_in_nonthrowing_autoclosure,none,
40684072
"call can throw, but it is executed in a non-throwing "

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ IDENTIFIER(KeyedEncodingContainer)
9393
IDENTIFIER(keyedBy)
9494
IDENTIFIER(keyPath)
9595
IDENTIFIER(makeIterator)
96+
IDENTIFIER(makeAsyncIterator)
9697
IDENTIFIER(Iterator)
98+
IDENTIFIER(AsyncIterator)
9799
IDENTIFIER(load)
98100
IDENTIFIER(main)
99101
IDENTIFIER_WITH_NAME(MainEntryPoint, "$main")

include/swift/AST/KnownProtocols.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ PROTOCOL(StringInterpolationProtocol)
8888
PROTOCOL(AdditiveArithmetic)
8989
PROTOCOL(Differentiable)
9090

91+
PROTOCOL(AsyncSequence)
92+
PROTOCOL(AsyncIteratorProtocol)
93+
9194
PROTOCOL(FloatingPoint)
9295

9396
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral, "Array", false)

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ class ProtocolConformanceRef {
170170
/// Get any additional requirements that are required for this conformance to
171171
/// be satisfied.
172172
ArrayRef<Requirement> getConditionalRequirements() const;
173+
174+
bool classifyAsThrows() const;
173175
};
174176

177+
void simple_display(llvm::raw_ostream &out, ProtocolConformanceRef conformanceRef);
178+
SourceLoc extractNearestSourceLoc(const ProtocolConformanceRef conformanceRef);
179+
175180
} // end namespace swift
176181

177182
#endif // LLVM_SWIFT_AST_PROTOCOLCONFORMANCEREF_H

include/swift/AST/Stmt.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ class RepeatWhileStmt : public LabeledStmt {
726726
/// \endcode
727727
class ForEachStmt : public LabeledStmt {
728728
SourceLoc ForLoc;
729+
SourceLoc TryLoc;
730+
SourceLoc AwaitLoc;
729731
Pattern *Pat;
730732
SourceLoc InLoc;
731733
Expr *Sequence;
@@ -741,12 +743,12 @@ class ForEachStmt : public LabeledStmt {
741743
Expr *convertElementExpr = nullptr;
742744

743745
public:
744-
ForEachStmt(LabeledStmtInfo LabelInfo, SourceLoc ForLoc, Pattern *Pat,
745-
SourceLoc InLoc, Expr *Sequence, SourceLoc WhereLoc,
746+
ForEachStmt(LabeledStmtInfo LabelInfo, SourceLoc ForLoc, SourceLoc TryLoc, SourceLoc AwaitLoc,
747+
Pattern *Pat, SourceLoc InLoc, Expr *Sequence, SourceLoc WhereLoc,
746748
Expr *WhereExpr, BraceStmt *Body, Optional<bool> implicit = None)
747749
: LabeledStmt(StmtKind::ForEach, getDefaultImplicitFlag(implicit, ForLoc),
748750
LabelInfo),
749-
ForLoc(ForLoc), Pat(nullptr), InLoc(InLoc), Sequence(Sequence),
751+
ForLoc(ForLoc), TryLoc(TryLoc), AwaitLoc(AwaitLoc), Pat(nullptr), InLoc(InLoc), Sequence(Sequence),
750752
WhereLoc(WhereLoc), WhereExpr(WhereExpr), Body(Body) {
751753
setPattern(Pat);
752754
}
@@ -778,6 +780,9 @@ class ForEachStmt : public LabeledStmt {
778780

779781
/// getWhereLoc - Retrieve the location of the 'where' keyword.
780782
SourceLoc getWhereLoc() const { return WhereLoc; }
783+
784+
SourceLoc getAwaitLoc() const { return AwaitLoc; }
785+
SourceLoc getTryLoc() const { return TryLoc; }
781786

782787
/// getPattern - Retrieve the pattern describing the iteration variables.
783788
/// These variables will only be visible within the body of the loop.

include/swift/AST/TypeCheckRequests.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,44 @@ class ExistentialTypeSupportedRequest :
311311
void cacheResult(bool value) const;
312312
};
313313

314+
class ProtocolRethrowsRequirementsRequest :
315+
public SimpleRequest<ProtocolRethrowsRequirementsRequest,
316+
ProtocolRethrowsRequirementList(ProtocolDecl *),
317+
RequestFlags::Cached> {
318+
public:
319+
using SimpleRequest::SimpleRequest;
320+
321+
private:
322+
friend SimpleRequest;
323+
324+
// Evaluation.
325+
ProtocolRethrowsRequirementList
326+
evaluate(Evaluator &evaluator, ProtocolDecl *decl) const;
327+
328+
public:
329+
// Caching.
330+
bool isCached() const { return true; }
331+
};
332+
333+
class ProtocolConformanceRefClassifyAsThrowsRequest :
334+
public SimpleRequest<ProtocolConformanceRefClassifyAsThrowsRequest,
335+
bool(ProtocolConformanceRef),
336+
RequestFlags::Cached> {
337+
public:
338+
using SimpleRequest::SimpleRequest;
339+
340+
private:
341+
friend SimpleRequest;
342+
343+
// Evaluation.
344+
bool
345+
evaluate(Evaluator &evaluator, ProtocolConformanceRef conformanceRef) const;
346+
347+
public:
348+
// Caching.
349+
bool isCached() const { return true; }
350+
};
351+
314352
/// Determine whether the given declaration is 'final'.
315353
class IsFinalRequest :
316354
public SimpleRequest<IsFinalRequest,
@@ -734,6 +772,26 @@ void simple_display(llvm::raw_ostream &out, FragileFunctionKind value);
734772

735773
void simple_display(llvm::raw_ostream &out, ResilienceExpansion value);
736774

775+
class FunctionRethrowingKindRequest :
776+
public SimpleRequest<FunctionRethrowingKindRequest,
777+
FunctionRethrowingKind(AbstractFunctionDecl*),
778+
RequestFlags::Cached> {
779+
public:
780+
using SimpleRequest::SimpleRequest;
781+
782+
private:
783+
friend SimpleRequest;
784+
785+
// Evaluation.
786+
FunctionRethrowingKind evaluate(Evaluator &evaluator, AbstractFunctionDecl *decl) const;
787+
788+
public:
789+
// Caching.
790+
bool isCached() const { return true; }
791+
};
792+
793+
void simple_display(llvm::raw_ostream &out, FunctionRethrowingKind value);
794+
737795
/// Request the custom attribute which attaches a result builder to the
738796
/// given declaration.
739797
class AttachedResultBuilderRequest :

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ SWIFT_REQUEST(TypeChecker, RequiresOpaqueModifyCoroutineRequest,
211211
bool(AbstractStorageDecl *), SeparatelyCached, NoLocationInfo)
212212
SWIFT_REQUEST(TypeChecker, FragileFunctionKindRequest,
213213
FragileFunctionKind(DeclContext *), Cached, NoLocationInfo)
214+
SWIFT_REQUEST(TypeChecker, FunctionRethrowingKindRequest,
215+
FunctionRethrowingKind(AbstractFunctionDecl *), Cached, NoLocationInfo)
214216
SWIFT_REQUEST(TypeChecker, SelfAccessKindRequest, SelfAccessKind(FuncDecl *),
215217
SeparatelyCached, NoLocationInfo)
216218
SWIFT_REQUEST(TypeChecker, StorageImplInfoRequest,
@@ -265,6 +267,12 @@ SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
265267
SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,
266268
Type(ProtocolDecl *, TypeEraserAttr *),
267269
SeparatelyCached, NoLocationInfo)
270+
SWIFT_REQUEST(TypeChecker, ProtocolRethrowsRequirementsRequest,
271+
ProtocolRethrowsRequirementList(ProtocolDecl *),
272+
Cached, NoLocationInfo)
273+
SWIFT_REQUEST(TypeChecker, ProtocolConformanceRefClassifyAsThrowsRequest,
274+
bool(ProtocolConformanceRef),
275+
Cached, NoLocationInfo)
268276
SWIFT_REQUEST(TypeChecker, ResolveTypeRequest,
269277
Type (const TypeResolution *, TypeRepr *, GenericParamList *),
270278
Uncached, NoLocationInfo)

lib/AST/ASTContext.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ struct ASTContext::Implementation {
206206
/// The declaration of 'Sequence.makeIterator()'.
207207
FuncDecl *MakeIterator = nullptr;
208208

209+
/// The declaration of 'AsyncSequence.makeAsyncIterator()'.
210+
FuncDecl *MakeAsyncIterator = nullptr;
211+
209212
/// The declaration of Swift.Optional<T>.Some.
210213
EnumElementDecl *OptionalSomeDecl = nullptr;
211214

@@ -772,6 +775,31 @@ FuncDecl *ASTContext::getSequenceMakeIterator() const {
772775
return nullptr;
773776
}
774777

778+
FuncDecl *ASTContext::getAsyncSequenceMakeAsyncIterator() const {
779+
if (getImpl().MakeAsyncIterator) {
780+
return getImpl().MakeAsyncIterator;
781+
}
782+
783+
auto proto = getProtocol(KnownProtocolKind::AsyncSequence);
784+
if (!proto)
785+
return nullptr;
786+
787+
for (auto result : proto->lookupDirect(Id_makeAsyncIterator)) {
788+
if (result->getDeclContext() != proto)
789+
continue;
790+
791+
if (auto func = dyn_cast<FuncDecl>(result)) {
792+
if (func->getParameters()->size() != 0)
793+
continue;
794+
795+
getImpl().MakeAsyncIterator = func;
796+
return func;
797+
}
798+
}
799+
800+
return nullptr;
801+
}
802+
775803
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
776804
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
777805
if (getImpl().NAME##Decl) \
@@ -943,6 +971,8 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
943971
M = getLoadedModule(Id_Differentiation);
944972
break;
945973
case KnownProtocolKind::Actor:
974+
case KnownProtocolKind::AsyncSequence:
975+
case KnownProtocolKind::AsyncIteratorProtocol:
946976
M = getLoadedModule(Id_Concurrency);
947977
break;
948978
default:

0 commit comments

Comments
 (0)