Skip to content

Commit c84586d

Browse files
authored
Merge pull request swiftlang#35841 from slavapestov/rethrows-cleanup
Rethrows checking cleanup
2 parents bdcb2a4 + a70ef07 commit c84586d

14 files changed

+509
-446
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ namespace swift {
8686
struct PropertyWrapperTypeInfo;
8787
struct PropertyWrapperMutability;
8888
class ProtocolDecl;
89+
class ProtocolRethrowsRequirementList;
8990
class ProtocolType;
9091
struct RawComment;
9192
enum class ResilienceExpansion : unsigned;
93+
enum class FunctionRethrowingKind : uint8_t;
9294
class TrailingWhereClause;
9395
class TypeAliasDecl;
9496
class Stmt;
@@ -3918,66 +3920,6 @@ enum class KnownDerivableProtocolKind : uint8_t {
39183920
Actor,
39193921
};
39203922

3921-
class ProtocolRethrowsRequirementList {
3922-
public:
3923-
typedef std::pair<Type, ValueDecl *> Entry;
3924-
3925-
private:
3926-
ArrayRef<Entry> entries;
3927-
3928-
public:
3929-
ProtocolRethrowsRequirementList(ArrayRef<Entry> entries) : entries(entries) {}
3930-
ProtocolRethrowsRequirementList() : entries() {}
3931-
3932-
typedef const Entry *const_iterator;
3933-
typedef const_iterator iterator;
3934-
3935-
const_iterator begin() const { return entries.begin(); }
3936-
const_iterator end() const { return entries.end(); }
3937-
3938-
size_t size() const { return entries.size(); }
3939-
3940-
void print(raw_ostream &OS) const;
3941-
3942-
SWIFT_DEBUG_DUMP;
3943-
3944-
friend bool operator==(const ProtocolRethrowsRequirementList &lhs,
3945-
const ProtocolRethrowsRequirementList &rhs) {
3946-
if (lhs.size() != rhs.size()) {
3947-
return false;
3948-
}
3949-
auto lhsIter = lhs.begin();
3950-
auto rhsIter = rhs.begin();
3951-
while (lhsIter != lhs.end() && rhsIter != rhs.end()) {
3952-
if (lhsIter->first->isEqual(rhsIter->first)) {
3953-
return false;
3954-
}
3955-
if (lhsIter->second != rhsIter->second) {
3956-
return false;
3957-
}
3958-
}
3959-
return true;
3960-
}
3961-
3962-
friend bool operator!=(const ProtocolRethrowsRequirementList &lhs,
3963-
const ProtocolRethrowsRequirementList &rhs) {
3964-
return !(lhs == rhs);
3965-
}
3966-
3967-
friend llvm::hash_code hash_value(
3968-
const ProtocolRethrowsRequirementList &list) {
3969-
return llvm::hash_combine(list.size()); // it is good enought for
3970-
// llvm::hash_code hash;
3971-
// for (auto entry : list) {
3972-
// hash = llvm::hash_combine(hash, entry.first->getCanonicalType());
3973-
// hash = llvm::hash_combine(hash, entry.second);
3974-
// }
3975-
// return hash;
3976-
}
3977-
};
3978-
3979-
void simple_display(raw_ostream &out, const ProtocolRethrowsRequirementList reqs);
3980-
39813923
/// ProtocolDecl - A declaration of a protocol, for example:
39823924
///
39833925
/// protocol Drawable {
@@ -5575,23 +5517,6 @@ class ImportAsMemberStatus {
55755517
}
55765518
};
55775519

5578-
enum class FunctionRethrowingKind : uint8_t {
5579-
/// The function is not throwing
5580-
None,
5581-
5582-
/// The function rethrows by closure
5583-
ByClosure,
5584-
5585-
/// The function rethrows by conformance
5586-
ByConformance,
5587-
5588-
/// The function throws
5589-
Throws,
5590-
5591-
/// The function throwing determinate is invalid
5592-
Invalid
5593-
};
5594-
55955520
/// Base class for function-like declarations.
55965521
class AbstractFunctionDecl : public GenericContext, public ValueDecl {
55975522
friend class NeedsNewVTableEntryRequest;

include/swift/AST/Effects.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===--- Decl.h - Swift Language Declaration ASTs ---------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines some data types for rethrows and reasync effects.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_EFFECTS_H
18+
#define SWIFT_EFFECTS_H
19+
20+
#include "swift/AST/Type.h"
21+
22+
#include <utility>
23+
24+
namespace llvm {
25+
class raw_ostream;
26+
}
27+
28+
namespace swift {
29+
30+
class ValueDecl;
31+
32+
class ProtocolRethrowsRequirementList {
33+
public:
34+
typedef std::pair<Type, ValueDecl *> Entry;
35+
36+
private:
37+
ArrayRef<Entry> entries;
38+
39+
public:
40+
ProtocolRethrowsRequirementList(ArrayRef<Entry> entries) : entries(entries) {}
41+
ProtocolRethrowsRequirementList() : entries() {}
42+
43+
typedef const Entry *const_iterator;
44+
typedef const_iterator iterator;
45+
46+
const_iterator begin() const { return entries.begin(); }
47+
const_iterator end() const { return entries.end(); }
48+
49+
size_t size() const { return entries.size(); }
50+
51+
void print(raw_ostream &OS) const;
52+
53+
SWIFT_DEBUG_DUMP;
54+
};
55+
56+
void simple_display(llvm::raw_ostream &out, const ProtocolRethrowsRequirementList reqs);
57+
58+
enum class FunctionRethrowingKind : uint8_t {
59+
/// The function is not throwing
60+
None,
61+
62+
/// The function rethrows by closure
63+
ByClosure,
64+
65+
/// The function rethrows by conformance
66+
ByConformance,
67+
68+
/// The function throws
69+
Throws,
70+
71+
/// The function throwing determinate is invalid
72+
Invalid
73+
};
74+
75+
void simple_display(llvm::raw_ostream &out, FunctionRethrowingKind value);
76+
77+
} // end namespace swift
78+
79+
#endif

include/swift/AST/TypeCheckRequests.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ActorIsolation.h"
2020
#include "swift/AST/AnyFunctionRef.h"
2121
#include "swift/AST/ASTTypeIDs.h"
22+
#include "swift/AST/Effects.h"
2223
#include "swift/AST/GenericParamList.h"
2324
#include "swift/AST/GenericSignature.h"
2425
#include "swift/AST/Type.h"
@@ -790,8 +791,6 @@ class FunctionRethrowingKindRequest :
790791
bool isCached() const { return true; }
791792
};
792793

793-
void simple_display(llvm::raw_ostream &out, FunctionRethrowingKind value);
794-
795794
/// Request the custom attribute which attaches a result builder to the
796795
/// given declaration.
797796
class AttachedResultBuilderRequest :

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/ASTWalker.h"
1919
#include "swift/AST/AccessScope.h"
2020
#include "swift/AST/Decl.h"
21+
#include "swift/AST/Effects.h"
2122
#include "swift/AST/ExistentialLayout.h"
2223
#include "swift/AST/Expr.h"
2324
#include "swift/AST/ForeignAsyncConvention.h"

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_swift_host_library(swiftAST STATIC
4444
DiagnosticEngine.cpp
4545
DiagnosticList.cpp
4646
DocComment.cpp
47+
Effects.cpp
4748
Evaluator.cpp
4849
Expr.cpp
4950
ExtInfo.cpp

lib/AST/Decl.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4934,26 +4934,6 @@ bool ProtocolDecl::existentialTypeSupported() const {
49344934
ExistentialTypeSupportedRequest{const_cast<ProtocolDecl *>(this)}, true);
49354935
}
49364936

4937-
void swift::simple_display(llvm::raw_ostream &out, const ProtocolRethrowsRequirementList list) {
4938-
for (auto entry : list) {
4939-
simple_display(out, entry.first);
4940-
simple_display(out, entry.second);
4941-
}
4942-
}
4943-
4944-
4945-
ProtocolRethrowsRequirementList
4946-
ProtocolDecl::getRethrowingRequirements() const {
4947-
return evaluateOrDefault(getASTContext().evaluator,
4948-
ProtocolRethrowsRequirementsRequest{const_cast<ProtocolDecl *>(this)},
4949-
ProtocolRethrowsRequirementList());
4950-
}
4951-
4952-
bool
4953-
ProtocolDecl::isRethrowingProtocol() const {
4954-
return getRethrowingRequirements().size() > 0;
4955-
}
4956-
49574937
StringRef ProtocolDecl::getObjCRuntimeName(
49584938
llvm::SmallVectorImpl<char> &buffer) const {
49594939
// If there is an 'objc' attribute with a name, use that name.
@@ -6800,12 +6780,6 @@ bool AbstractFunctionDecl::canBeAsyncHandler() const {
68006780
false);
68016781
}
68026782

6803-
FunctionRethrowingKind AbstractFunctionDecl::getRethrowingKind() const {
6804-
return evaluateOrDefault(getASTContext().evaluator,
6805-
FunctionRethrowingKindRequest{const_cast<AbstractFunctionDecl *>(this)},
6806-
FunctionRethrowingKind::Invalid);
6807-
}
6808-
68096783
BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
68106784
if ((getBodyKind() == BodyKind::Synthesize ||
68116785
getBodyKind() == BodyKind::Unparsed) &&

lib/AST/Effects.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===--- Effects.cpp - Effect Checking ASTs -------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file implements some logic for rethrows and reasync checking.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "swift/AST/ASTContext.h"
18+
#include "swift/AST/Effects.h"
19+
#include "swift/AST/Evaluator.h"
20+
#include "swift/AST/Decl.h"
21+
#include "swift/AST/ProtocolConformanceRef.h"
22+
#include "swift/AST/Type.h"
23+
#include "swift/AST/TypeCheckRequests.h"
24+
#include "llvm/ADT/ArrayRef.h"
25+
#include "llvm/Support/raw_ostream.h"
26+
27+
using namespace swift;
28+
29+
void swift::simple_display(llvm::raw_ostream &out,
30+
const ProtocolRethrowsRequirementList list) {
31+
for (auto entry : list) {
32+
simple_display(out, entry.first);
33+
simple_display(out, entry.second);
34+
}
35+
}
36+
37+
ProtocolRethrowsRequirementList
38+
ProtocolDecl::getRethrowingRequirements() const {
39+
return evaluateOrDefault(getASTContext().evaluator,
40+
ProtocolRethrowsRequirementsRequest{const_cast<ProtocolDecl *>(this)},
41+
ProtocolRethrowsRequirementList());
42+
}
43+
44+
bool ProtocolDecl::isRethrowingProtocol() const {
45+
return getAttrs().hasAttribute<swift::AtRethrowsAttr>();
46+
}
47+
48+
FunctionRethrowingKind AbstractFunctionDecl::getRethrowingKind() const {
49+
return evaluateOrDefault(getASTContext().evaluator,
50+
FunctionRethrowingKindRequest{const_cast<AbstractFunctionDecl *>(this)},
51+
FunctionRethrowingKind::Invalid);
52+
}
53+
54+
void swift::simple_display(llvm::raw_ostream &out,
55+
FunctionRethrowingKind kind) {
56+
switch (kind) {
57+
case FunctionRethrowingKind::None:
58+
out << "non-throwing";
59+
break;
60+
case FunctionRethrowingKind::ByClosure:
61+
out << "by closure";
62+
break;
63+
case FunctionRethrowingKind::ByConformance:
64+
out << "by conformance";
65+
break;
66+
case FunctionRethrowingKind::Throws:
67+
out << "throws";
68+
break;
69+
case FunctionRethrowingKind::Invalid:
70+
out << "invalid";
71+
break;
72+
}
73+
}
74+
75+
bool ProtocolConformanceRef::classifyAsThrows() const {
76+
if (!isConcrete()) { return true; }
77+
return evaluateOrDefault(getRequirement()->getASTContext().evaluator,
78+
ProtocolConformanceRefClassifyAsThrowsRequest{ *this },
79+
true);
80+
}

0 commit comments

Comments
 (0)