Skip to content

Commit d53c015

Browse files
authored
Merge pull request #28231 from CodaFi/security-types
2 parents 95c716c + 4f9f6b9 commit d53c015

File tree

4 files changed

+29
-41
lines changed

4 files changed

+29
-41
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ ConstraintSystem::SolverState::SolverState(
362362

363363
// If we're supposed to debug a specific constraint solver attempt,
364364
// turn on debugging now.
365-
ASTContext &ctx = CS.getTypeChecker().Context;
365+
ASTContext &ctx = CS.getASTContext();
366366
auto &tyOpts = ctx.TypeCheckerOpts;
367367
OldDebugConstraintSolver = tyOpts.DebugConstraintSolver;
368368
if (tyOpts.DebugConstraintSolverAttempt &&
@@ -409,7 +409,7 @@ ConstraintSystem::SolverState::~SolverState() {
409409
}
410410

411411
// Restore debugging state.
412-
TypeCheckerOptions &tyOpts = CS.getTypeChecker().Context.TypeCheckerOpts;
412+
TypeCheckerOptions &tyOpts = CS.getASTContext().TypeCheckerOpts;
413413
tyOpts.DebugConstraintSolver = OldDebugConstraintSolver;
414414

415415
// Write our local statistics back to the overall statistics.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,7 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
21722172
TypeCheckExprOptions options,
21732173
ExprTypeCheckListener &listener,
21742174
ConstraintSystem *baseCS) {
2175+
auto &Context = dc->getASTContext();
21752176
FrontendStatsTracer StatsTracer(Context.Stats, "typecheck-expr", expr);
21762177
PrettyStackTraceExpr stackTrace(Context, "type-checking", expr);
21772178

@@ -3717,22 +3718,22 @@ void ConstraintSystem::print(raw_ostream &out) const {
37173718
out << "\nActive Constraints:\n";
37183719
for (auto &constraint : ActiveConstraints) {
37193720
out.indent(2);
3720-
constraint.print(out, &getTypeChecker().Context.SourceMgr);
3721+
constraint.print(out, &getASTContext().SourceMgr);
37213722
out << "\n";
37223723
}
37233724

37243725
out << "\nInactive Constraints:\n";
37253726
for (auto &constraint : InactiveConstraints) {
37263727
out.indent(2);
3727-
constraint.print(out, &getTypeChecker().Context.SourceMgr);
3728+
constraint.print(out, &getASTContext().SourceMgr);
37283729
out << "\n";
37293730
}
37303731

37313732
if (solverState && !solverState->hasRetiredConstraints()) {
37323733
out << "\nRetired Constraints:\n";
37333734
solverState->forEachRetired([&](Constraint &constraint) {
37343735
out.indent(2);
3735-
constraint.print(out, &getTypeChecker().Context.SourceMgr);
3736+
constraint.print(out, &getASTContext().SourceMgr);
37363737
out << "\n";
37373738
});
37383739
}
@@ -3786,7 +3787,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
37863787
out << "\nDisjunction choices:\n";
37873788
for (auto &choice : DisjunctionChoices) {
37883789
out.indent(2);
3789-
choice.first->dump(&getTypeChecker().Context.SourceMgr, out);
3790+
choice.first->dump(&getASTContext().SourceMgr, out);
37903791
out << " is #" << choice.second << "\n";
37913792
}
37923793
}
@@ -3795,7 +3796,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
37953796
out << "\nOpened types:\n";
37963797
for (const auto &opened : OpenedTypes) {
37973798
out.indent(2);
3798-
opened.first->dump(&getTypeChecker().Context.SourceMgr, out);
3799+
opened.first->dump(&getASTContext().SourceMgr, out);
37993800
out << " opens ";
38003801
interleave(opened.second.begin(), opened.second.end(),
38013802
[&](OpenedType opened) {
@@ -3814,7 +3815,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
38143815
out << "\nOpened existential types:\n";
38153816
for (const auto &openedExistential : OpenedExistentialTypes) {
38163817
out.indent(2);
3817-
openedExistential.first->dump(&getTypeChecker().Context.SourceMgr, out);
3818+
openedExistential.first->dump(&getASTContext().SourceMgr, out);
38183819
out << " opens to " << openedExistential.second->getString(PO);
38193820
out << "\n";
38203821
}
@@ -3823,7 +3824,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
38233824
if (!DefaultedConstraints.empty()) {
38243825
out << "\nDefaulted constraints: ";
38253826
interleave(DefaultedConstraints, [&](ConstraintLocator *locator) {
3826-
locator->dump(&getTypeChecker().Context.SourceMgr, out);
3827+
locator->dump(&getASTContext().SourceMgr, out);
38273828
}, [&] {
38283829
out << ", ";
38293830
});
@@ -3832,7 +3833,7 @@ void ConstraintSystem::print(raw_ostream &out) const {
38323833
if (failedConstraint) {
38333834
out << "\nFailed constraint:\n";
38343835
out.indent(2);
3835-
failedConstraint->print(out, &getTypeChecker().Context.SourceMgr);
3836+
failedConstraint->print(out, &getASTContext().SourceMgr);
38363837
out << "\n";
38373838
}
38383839

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ using namespace swift;
5555
TypeChecker &TypeChecker::createForContext(ASTContext &ctx) {
5656
assert(!ctx.getLegacyGlobalTypeChecker() &&
5757
"Cannot install more than one instance of the global type checker!");
58-
auto *TC = new TypeChecker(ctx);
58+
auto *TC = new TypeChecker();
5959
ctx.installGlobalTypeChecker(TC);
6060
ctx.addCleanup([=](){ delete TC; });
6161
return *ctx.getLegacyGlobalTypeChecker();
6262
}
6363

64-
TypeChecker::TypeChecker(ASTContext &Ctx)
65-
: Context(Ctx) {}
66-
67-
TypeChecker::~TypeChecker() {}
68-
6964
ProtocolDecl *TypeChecker::getProtocol(ASTContext &Context, SourceLoc loc,
7065
KnownProtocolKind kind) {
7166
auto protocol = Context.getProtocol(kind);

lib/Sema/TypeChecker.h

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,10 @@ class TypeChecker final {
542542
std::vector<AbstractClosureExpr *> ClosuresWithUncomputedCaptures;
543543

544544
private:
545-
ASTContext &Context;
545+
TypeChecker() = default;
546+
~TypeChecker() = default;
546547

547-
TypeChecker(ASTContext &Ctx);
548548
friend class ASTContext;
549-
friend class constraints::ConstraintSystem;
550-
friend class TypeCheckFunctionBodyUntilRequest;
551549

552550
public:
553551
/// Create a new type checker instance for the given ASTContext, if it
@@ -559,7 +557,6 @@ class TypeChecker final {
559557
public:
560558
TypeChecker(const TypeChecker&) = delete;
561559
TypeChecker& operator=(const TypeChecker&) = delete;
562-
~TypeChecker();
563560

564561
static Type getArraySliceType(SourceLoc loc, Type elementType);
565562
static Type getDictionaryType(SourceLoc loc, Type keyType, Type valueType);
@@ -928,24 +925,17 @@ class TypeChecker final {
928925
/// struct or class.
929926
static void addImplicitConstructors(NominalTypeDecl *typeDecl);
930927

931-
/// \name Name lookup
932-
///
933-
/// Routines that perform name lookup.
934-
///
935-
/// During type checking, these routines should be used instead of
936-
/// \c MemberLookup and \c UnqualifiedLookup, because these routines will
937-
/// lazily introduce declarations and (FIXME: eventually) perform recursive
938-
/// type-checking that the AST-level lookup routines don't.
939-
///
940-
/// @{
941-
private:
942-
Optional<Type> boolType;
943-
944928
public:
945929
/// Fold the given sequence expression into an (unchecked) expression
946930
/// tree.
947931
static Expr *foldSequence(SequenceExpr *expr, DeclContext *dc);
948932

933+
private:
934+
/// Given an pre-folded expression, find LHS from the expression if a binary
935+
/// operator \c name appended to the expression.
936+
static Expr *findLHS(DeclContext *DC, Expr *E, Identifier name);
937+
938+
public:
949939
/// Type check the given expression.
950940
///
951941
/// \param expr The expression to type-check, which will be modified in
@@ -1338,6 +1328,12 @@ class TypeChecker final {
13381328
static Type deriveTypeWitness(DeclContext *DC, NominalTypeDecl *nominal,
13391329
AssociatedTypeDecl *assocType);
13401330

1331+
/// \name Name lookup
1332+
///
1333+
/// Routines that perform name lookup.
1334+
///
1335+
/// @{
1336+
13411337
/// Perform unqualified name lookup at the given source location
13421338
/// within a particular declaration context.
13431339
///
@@ -1374,10 +1370,6 @@ class TypeChecker final {
13741370
NameLookupOptions options
13751371
= defaultMemberLookupOptions);
13761372

1377-
/// Check whether the given declaration can be written as a
1378-
/// member of the given base type.
1379-
static bool isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl);
1380-
13811373
/// Look up a member type within the given type.
13821374
///
13831375
/// This routine looks for member types with the given name within the
@@ -1414,9 +1406,9 @@ class TypeChecker final {
14141406
Identifier name,
14151407
SourceLoc nameLoc);
14161408

1417-
/// Given an pre-folded expression, find LHS from the expression if a binary
1418-
/// operator \c name appended to the expression.
1419-
static Expr *findLHS(DeclContext *DC, Expr *E, Identifier name);
1409+
/// Check whether the given declaration can be written as a
1410+
/// member of the given base type.
1411+
static bool isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl);
14201412

14211413
/// @}
14221414

0 commit comments

Comments
 (0)