Skip to content

Commit 016cb69

Browse files
authored
Merge pull request swiftlang#23730 from slavapestov/assorted-bug-fixes
Assorted bug fixes
2 parents 52e8340 + 0aadaf5 commit 016cb69

30 files changed

+262
-268
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ class ASTWalker {
211211
/// However, ASTWalker does not walk into LazyInitializerExprs on its own.
212212
virtual bool shouldWalkIntoLazyInitializers() { return true; }
213213

214+
/// This method configures whether the walker should visit the body of a
215+
/// non-single expression closure.
216+
///
217+
/// For work that is performed for every top-level expression, this should
218+
/// be overridden to return false, to avoid duplicating work or visiting
219+
/// bodies of closures that have not yet been type checked.
220+
virtual bool shouldWalkIntoNonSingleExpressionClosure() { return true; }
221+
214222
/// walkToParameterListPre - This method is called when first visiting a
215223
/// ParameterList, before walking into its parameters. If it returns false,
216224
/// the subtree is skipped.

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,14 +2268,10 @@ ERROR(duplicate_inheritance,none,
22682268
"duplicate inheritance from %0", (Type))
22692269
WARNING(duplicate_anyobject_class_inheritance,none,
22702270
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
2271-
ERROR(objc_protocol_with_superclass,none,
2272-
"protocol %0 is '@objc' and cannot have a superclass constraint", (Identifier))
2271+
ERROR(inheritance_from_protocol_with_superclass,none,
2272+
"inheritance from class-constrained protocol composition type %0", (Type))
22732273
ERROR(multiple_inheritance,none,
22742274
"multiple inheritance from classes %0 and %1", (Type, Type))
2275-
ERROR(non_class_inheritance,none,
2276-
"non-class type %0 cannot inherit from class %1", (Type, Type))
2277-
ERROR(extension_class_inheritance,none,
2278-
"extension of type %0 cannot inherit from class %1", (Type, Type))
22792275
ERROR(inheritance_from_non_protocol_or_class,none,
22802276
"inheritance from non-protocol, non-class type %0", (Type))
22812277
ERROR(inheritance_from_non_protocol,none,

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
755755
return nullptr;
756756
}
757757

758+
if (!Walker.shouldWalkIntoNonSingleExpressionClosure())
759+
return expr;
760+
758761
// Handle other closures.
759762
if (BraceStmt *body = cast_or_null<BraceStmt>(doIt(expr->getBody()))) {
760763
expr->setBody(body, false);
@@ -1065,12 +1068,14 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
10651068
if (auto oldSubExpr = E->getSubExpr()) {
10661069
if (auto subExpr = doIt(oldSubExpr)) {
10671070
E->setSubExpr(subExpr);
1068-
}
1069-
else {
1071+
} else {
10701072
return nullptr;
10711073
}
10721074
}
10731075

1076+
if (!Walker.shouldWalkIntoNonSingleExpressionClosure())
1077+
return E;
1078+
10741079
if (auto oldBody = E->getBody()) {
10751080
if (auto body = doIt(oldBody)) {
10761081
E->setBody(dyn_cast<BraceStmt>(body));

lib/IRGen/GenProto.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,8 @@ class AccessorConformanceInfo : public ConformanceInfo {
11001100
llvm::Value *getTable(IRGenFunction &IGF,
11011101
llvm::Value **typeMetadataCache) const override {
11021102
// If we're looking up a dependent type, we can't cache the result.
1103-
if (Conformance->getType()->hasArchetype()) {
1103+
if (Conformance->getType()->hasArchetype() ||
1104+
Conformance->getType()->hasDynamicSelfType()) {
11041105
return emitWitnessTableAccessorCall(IGF, Conformance,
11051106
typeMetadataCache);
11061107
}

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
152152
if (!field->isStatic() && field->isLet() &&
153153
field->getParentInitializer()) {
154154
#ifndef NDEBUG
155-
auto fieldTy = decl->getDeclContext()->mapTypeIntoContext(
156-
field->getInterfaceType());
157-
assert(fieldTy->isEqual(field->getParentInitializer()->getType())
155+
assert(field->getType()->isEqual(field->getParentInitializer()->getType())
158156
&& "Checked by sema");
159157
#endif
160158

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7315,8 +7315,7 @@ namespace {
73157315
// Coerce the pattern, in case we resolved something.
73167316
auto fnType = cs.getType(closure)->castTo<FunctionType>();
73177317
auto *params = closure->getParameters();
7318-
if (tc.coerceParameterListToType(params, closure, fnType))
7319-
return { false, nullptr };
7318+
tc.coerceParameterListToType(params, closure, fnType);
73207319

73217320
// Require layout of dependent types that could be used to materialize
73227321
// metadata types/conformances during IRGen.

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,8 +5741,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
57415741
}
57425742

57435743
// Coerce parameter types here only if there are no unresolved
5744-
if (CS.TC.coerceParameterListToType(params, CE, fnType))
5745-
return true;
5744+
CS.TC.coerceParameterListToType(params, CE, fnType);
57465745
expectedResultType = fnType->getResult();
57475746
}
57485747

lib/Sema/CodeSynthesis.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,25 @@ static void maybeAddMemberwiseDefaultArg(ParamDecl *arg, VarDecl *var,
16901690
arg->setDefaultArgumentKind(DefaultArgumentKind::StoredProperty);
16911691
}
16921692

1693+
bool swift::isMemberwiseInitialized(VarDecl *var) {
1694+
// Implicit, computed, and static properties are not initialized.
1695+
// The exception is lazy properties, which due to batch mode we may or
1696+
// may not have yet finalized, so they may currently be "stored" or
1697+
// "computed" in the current AST state.
1698+
if (var->isImplicit() || var->isStatic())
1699+
return false;
1700+
1701+
if (!var->hasStorage() && !var->getAttrs().hasAttribute<LazyAttr>())
1702+
return false;
1703+
1704+
// Initialized 'let' properties have storage, but don't get an argument
1705+
// to the memberwise initializer since they already have an initial
1706+
// value that cannot be overridden.
1707+
if (var->isLet() && var->getParentInitializer())
1708+
return false;
1709+
1710+
return true;
1711+
}
16931712
/// Create an implicit struct or class constructor.
16941713
///
16951714
/// \param decl The struct or class for which a constructor will be created.
@@ -1716,23 +1735,10 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
17161735
auto var = dyn_cast<VarDecl>(member);
17171736
if (!var)
17181737
continue;
1719-
1720-
// Implicit, computed, and static properties are not initialized.
1721-
// The exception is lazy properties, which due to batch mode we may or
1722-
// may not have yet finalized, so they may currently be "stored" or
1723-
// "computed" in the current AST state.
1724-
if (var->isImplicit() || var->isStatic())
1725-
continue;
17261738

1727-
if (!var->hasStorage() && !var->getAttrs().hasAttribute<LazyAttr>())
1739+
if (!isMemberwiseInitialized(var))
17281740
continue;
17291741

1730-
// Initialized 'let' properties have storage, but don't get an argument
1731-
// to the memberwise initializer since they already have an initial
1732-
// value that cannot be overridden.
1733-
if (var->isLet() && var->getParentInitializer())
1734-
continue;
1735-
17361742
accessLevel = std::min(accessLevel, var->getFormalAccess());
17371743

17381744
tc.validateDecl(var);
@@ -1765,9 +1771,9 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
17651771
DeclName name(ctx, DeclBaseName::createConstructor(), paramList);
17661772
auto *ctor =
17671773
new (ctx) ConstructorDecl(name, Loc,
1768-
OTK_None, /*FailabilityLoc=*/SourceLoc(),
1769-
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
1770-
paramList, /*GenericParams=*/nullptr, decl);
1774+
OTK_None, /*FailabilityLoc=*/SourceLoc(),
1775+
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
1776+
paramList, /*GenericParams=*/nullptr, decl);
17711777

17721778
// Mark implicit.
17731779
ctor->setImplicit();

lib/Sema/CodeSynthesis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ enum class ImplicitConstructorKind {
6969
Memberwise
7070
};
7171

72+
bool isMemberwiseInitialized(VarDecl *var);
73+
7274
/// Create an implicit struct or class constructor.
7375
///
7476
/// \param decl The struct or class for which a constructor will be created.

lib/Sema/MiscDiagnostics.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
111111
bool walkToDeclPre(Decl *D) override { return false; }
112112
bool walkToTypeReprPre(TypeRepr *T) override { return true; }
113113

114+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
115+
114116
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
115117
// See through implicit conversions of the expression. We want to be able
116118
// to associate the parent of this expression with the ultimate callee.
@@ -582,7 +584,7 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
582584
void checkNoEscapeParameterUse(DeclRefExpr *DRE, Expr *parent,
583585
OperandKind useKind) {
584586
// This only cares about declarations of noescape function type.
585-
auto AFT = DRE->getDecl()->getInterfaceType()->getAs<AnyFunctionType>();
587+
auto AFT = DRE->getType()->getAs<FunctionType>();
586588
if (!AFT || !AFT->isNoEscape())
587589
return;
588590

@@ -1399,6 +1401,8 @@ static void diagRecursivePropertyAccess(TypeChecker &TC, const Expr *E,
13991401
cast<VarDecl>(DRE->getDecl())->isSelfParameter();
14001402
}
14011403

1404+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
1405+
14021406
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
14031407
Expr *subExpr;
14041408
bool isStore = false;
@@ -1547,11 +1551,10 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
15471551
return false;
15481552
}
15491553

1554+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
1555+
15501556
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
15511557
if (auto *CE = dyn_cast<AbstractClosureExpr>(E)) {
1552-
if (!CE->hasSingleExpressionBody())
1553-
return { false, E };
1554-
15551558
// If this is a potentially-escaping closure expression, start looking
15561559
// for references to self if we aren't already.
15571560
if (isClosureRequiringSelfQualification(CE))
@@ -2348,7 +2351,7 @@ class VarDeclUsageChecker : public ASTWalker {
23482351
// other things going on in the initializer expressions.
23492352
return true;
23502353
}
2351-
2354+
23522355
/// The heavy lifting happens when visiting expressions.
23532356
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;
23542357

@@ -2733,8 +2736,6 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
27332736
E->walk(*this);
27342737
}
27352738

2736-
2737-
27382739
/// The heavy lifting happens when visiting expressions.
27392740
std::pair<bool, Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
27402741
// Sema leaves some subexpressions null, which seems really unfortunate. It
@@ -2975,6 +2976,8 @@ static void checkStmtConditionTrailingClosure(TypeChecker &TC, const Expr *E) {
29752976
public:
29762977
DiagnoseWalker(TypeChecker &tc) : TC(tc) { }
29772978

2979+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
2980+
29782981
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
29792982
// Dig into implicit expression.
29802983
if (E->isImplicit()) return { true, E };
@@ -3106,6 +3109,8 @@ class ObjCSelectorWalker : public ASTWalker {
31063109
ObjCSelectorWalker(TypeChecker &tc, const DeclContext *dc, Type selectorTy)
31073110
: TC(tc), DC(dc), SelectorTy(selectorTy) { }
31083111

3112+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3113+
31093114
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
31103115
auto *stringLiteral = dyn_cast<StringLiteralExpr>(expr);
31113116
bool fromStringLiteral = false;
@@ -3777,14 +3782,12 @@ static void diagnoseUnintendedOptionalBehavior(TypeChecker &TC, const Expr *E,
37773782
}
37783783
}
37793784

3785+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3786+
37803787
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
37813788
if (!E || isa<ErrorExpr>(E) || !E->getType())
37823789
return { false, E };
37833790

3784-
if (auto *CE = dyn_cast<AbstractClosureExpr>(E))
3785-
if (!CE->hasSingleExpressionBody())
3786-
return { false, E };
3787-
37883791
if (IgnoredExprs.count(E))
37893792
return { true, E };
37903793

@@ -3851,6 +3854,8 @@ static void diagnoseDeprecatedWritableKeyPath(TypeChecker &TC, const Expr *E,
38513854
}
38523855
}
38533856

3857+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3858+
38543859
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
38553860
if (!E || isa<ErrorExpr>(E) || !E->getType())
38563861
return {false, E};

0 commit comments

Comments
 (0)