Skip to content

Commit 396f0ca

Browse files
authored
Merge pull request #23754 from slavapestov/assorted-bug-fixes-5.1
Assorted bug fixes [5.1]
2 parents 6ec5e66 + 3c2d6a6 commit 396f0ca

24 files changed

+206
-145
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
@@ -2273,14 +2273,10 @@ ERROR(duplicate_inheritance,none,
22732273
"duplicate inheritance from %0", (Type))
22742274
WARNING(duplicate_anyobject_class_inheritance,none,
22752275
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
2276-
ERROR(objc_protocol_with_superclass,none,
2277-
"protocol %0 is '@objc' and cannot have a superclass constraint", (Identifier))
2276+
ERROR(inheritance_from_protocol_with_superclass,none,
2277+
"inheritance from class-constrained protocol composition type %0", (Type))
22782278
ERROR(multiple_inheritance,none,
22792279
"multiple inheritance from classes %0 and %1", (Type, Type))
2280-
ERROR(non_class_inheritance,none,
2281-
"non-class type %0 cannot inherit from class %1", (Type, Type))
2282-
ERROR(extension_class_inheritance,none,
2283-
"extension of type %0 cannot inherit from class %1", (Type, Type))
22842280
ERROR(inheritance_from_non_protocol_or_class,none,
22852281
"inheritance from non-protocol, non-class type %0", (Type))
22862282
ERROR(inheritance_from_non_protocol,none,

lib/AST/ASTMangler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,10 +1595,10 @@ void ASTMangler::appendContext(const DeclContext *ctx) {
15951595
return appendEntity(eed);
15961596
}
15971597

1598-
case DeclContextKind::SubscriptDecl:
1599-
// FIXME: We may need to do something here if subscripts contain any symbols
1600-
// exposed with linkage names, or if/when they get generic parameters.
1601-
return appendContext(ctx->getParent());
1598+
case DeclContextKind::SubscriptDecl: {
1599+
auto sd = cast<SubscriptDecl>(ctx);
1600+
return appendEntity(sd);
1601+
}
16021602

16031603
case DeclContextKind::Initializer:
16041604
switch (cast<Initializer>(ctx)->getInitializerKind()) {

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
761761
return nullptr;
762762
}
763763

764+
if (!Walker.shouldWalkIntoNonSingleExpressionClosure())
765+
return expr;
766+
764767
// Handle other closures.
765768
if (BraceStmt *body = cast_or_null<BraceStmt>(doIt(expr->getBody()))) {
766769
expr->setBody(body, false);
@@ -1071,12 +1074,14 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
10711074
if (auto oldSubExpr = E->getSubExpr()) {
10721075
if (auto subExpr = doIt(oldSubExpr)) {
10731076
E->setSubExpr(subExpr);
1074-
}
1075-
else {
1077+
} else {
10761078
return nullptr;
10771079
}
10781080
}
10791081

1082+
if (!Walker.shouldWalkIntoNonSingleExpressionClosure())
1083+
return E;
1084+
10801085
if (auto oldBody = E->getBody()) {
10811086
if (auto body = doIt(oldBody)) {
10821087
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/MiscDiagnostics.cpp

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

113+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
114+
113115
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
114116
// See through implicit conversions of the expression. We want to be able
115117
// to associate the parent of this expression with the ultimate callee.
@@ -1408,6 +1410,8 @@ static void diagRecursivePropertyAccess(TypeChecker &TC, const Expr *E,
14081410
cast<VarDecl>(DRE->getDecl())->isSelfParameter();
14091411
}
14101412

1413+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
1414+
14111415
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
14121416
Expr *subExpr;
14131417
bool isStore = false;
@@ -1556,11 +1560,10 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
15561560
return false;
15571561
}
15581562

1563+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
1564+
15591565
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
15601566
if (auto *CE = dyn_cast<AbstractClosureExpr>(E)) {
1561-
if (!CE->hasSingleExpressionBody())
1562-
return { false, E };
1563-
15641567
// If this is a potentially-escaping closure expression, start looking
15651568
// for references to self if we aren't already.
15661569
if (isClosureRequiringSelfQualification(CE))
@@ -2387,7 +2390,7 @@ class VarDeclUsageChecker : public ASTWalker {
23872390
// other things going on in the initializer expressions.
23882391
return true;
23892392
}
2390-
2393+
23912394
/// The heavy lifting happens when visiting expressions.
23922395
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;
23932396

@@ -2772,8 +2775,6 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
27722775
E->walk(*this);
27732776
}
27742777

2775-
2776-
27772778
/// The heavy lifting happens when visiting expressions.
27782779
std::pair<bool, Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
27792780
// Sema leaves some subexpressions null, which seems really unfortunate. It
@@ -3020,6 +3021,8 @@ static void checkStmtConditionTrailingClosure(TypeChecker &TC, const Expr *E) {
30203021
public:
30213022
DiagnoseWalker(TypeChecker &tc) : TC(tc) { }
30223023

3024+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3025+
30233026
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
30243027
// Dig into implicit expression.
30253028
if (E->isImplicit()) return { true, E };
@@ -3151,6 +3154,8 @@ class ObjCSelectorWalker : public ASTWalker {
31513154
ObjCSelectorWalker(TypeChecker &tc, const DeclContext *dc, Type selectorTy)
31523155
: TC(tc), DC(dc), SelectorTy(selectorTy) { }
31533156

3157+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3158+
31543159
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
31553160
auto *stringLiteral = dyn_cast<StringLiteralExpr>(expr);
31563161
bool fromStringLiteral = false;
@@ -3822,14 +3827,12 @@ static void diagnoseUnintendedOptionalBehavior(TypeChecker &TC, const Expr *E,
38223827
}
38233828
}
38243829

3830+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3831+
38253832
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
38263833
if (!E || isa<ErrorExpr>(E) || !E->getType())
38273834
return { false, E };
38283835

3829-
if (auto *CE = dyn_cast<AbstractClosureExpr>(E))
3830-
if (!CE->hasSingleExpressionBody())
3831-
return { false, E };
3832-
38333836
if (IgnoredExprs.count(E))
38343837
return { true, E };
38353838

@@ -3896,6 +3899,8 @@ static void diagnoseDeprecatedWritableKeyPath(TypeChecker &TC, const Expr *E,
38963899
}
38973900
}
38983901

3902+
bool shouldWalkIntoNonSingleExpressionClosure() override { return false; }
3903+
38993904
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
39003905
if (!E || isa<ErrorExpr>(E) || !E->getType())
39013906
return {false, E};

lib/Sema/TypeCheckDecl.cpp

Lines changed: 43 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ static void validateAttributes(TypeChecker &TC, Decl *D);
215215
/// file.
216216
static void checkInheritanceClause(
217217
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> declUnion) {
218-
TypeResolutionOptions options = None;
219218
DeclContext *DC;
220219
MutableArrayRef<TypeLoc> inheritedClause;
221220
ExtensionDecl *ext = nullptr;
@@ -224,7 +223,6 @@ static void checkInheritanceClause(
224223
if ((ext = declUnion.dyn_cast<ExtensionDecl *>())) {
225224
decl = ext;
226225
DC = ext;
227-
options |= TypeResolutionFlags::AllowUnavailableProtocol;
228226

229227
inheritedClause = ext->getInherited();
230228

@@ -243,14 +241,17 @@ static void checkInheritanceClause(
243241
decl = typeDecl;
244242
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
245243
DC = nominal;
246-
options |= TypeResolutionFlags::AllowUnavailableProtocol;
247244
} else {
248245
DC = typeDecl->getDeclContext();
249246
}
250247

251248
inheritedClause = typeDecl->getInherited();
252249
}
253250

251+
bool canHaveSuperclass = (isa<ClassDecl>(decl) ||
252+
(isa<ProtocolDecl>(decl) &&
253+
!cast<ProtocolDecl>(decl)->isObjC()));
254+
254255
ASTContext &ctx = decl->getASTContext();
255256
auto &diags = ctx.Diags;
256257

@@ -259,13 +260,6 @@ static void checkInheritanceClause(
259260
if (ext)
260261
return ext->getSourceRange().End;
261262

262-
if (auto genericTypeDecl = dyn_cast<GenericTypeDecl>(typeDecl)) {
263-
if (auto genericParams = genericTypeDecl->getGenericParams())
264-
return genericParams->getSourceRange().End;
265-
266-
return genericTypeDecl->getNameLoc();
267-
}
268-
269263
return typeDecl->getNameLoc();
270264
};
271265

@@ -316,6 +310,12 @@ static void checkInheritanceClause(
316310
if (!inheritedTy || inheritedTy->hasError())
317311
continue;
318312

313+
// For generic parameters and associated types, the GSB checks constraints;
314+
// however, we still want to fire off the requests to produce diagnostics
315+
// in some circular validation cases.
316+
if (isa<AbstractTypeParamDecl>(decl))
317+
continue;
318+
319319
// Check whether we inherited from 'AnyObject' twice.
320320
// Other redundant-inheritance scenarios are checked below, the
321321
// GenericSignatureBuilder (for protocol inheritance) or the
@@ -351,41 +351,36 @@ static void checkInheritanceClause(
351351
if (inheritedTy->isExistentialType()) {
352352
auto layout = inheritedTy->getExistentialLayout();
353353

354-
// @objc protocols cannot have superclass constraints.
355-
if (layout.explicitSuperclass) {
356-
if (auto *protoDecl = dyn_cast<ProtocolDecl>(decl)) {
357-
if (protoDecl->isObjC()) {
358-
protoDecl->diagnose(diag::objc_protocol_with_superclass,
359-
protoDecl->getName());
360-
continue;
361-
}
362-
}
363-
}
354+
// Inheritance from protocol compositions that do not contain classes
355+
// or AnyObject is always OK.
356+
if (!layout.hasExplicitAnyObject &&
357+
!layout.explicitSuperclass)
358+
continue;
364359

365-
// Protocols, generic parameters and associated types can inherit
366-
// from subclass existentials, which are "exploded" into their
367-
// corresponding requirements.
368-
//
369-
// Extensions, structs and enums can only inherit from protocol
370-
// compositions that do not contain AnyObject or class members.
371-
if (isa<ProtocolDecl>(decl) ||
372-
isa<AbstractTypeParamDecl>(decl) ||
373-
(!layout.hasExplicitAnyObject &&
374-
!layout.explicitSuperclass)) {
360+
// Protocols can inherit from AnyObject.
361+
if (layout.hasExplicitAnyObject &&
362+
isa<ProtocolDecl>(decl))
375363
continue;
376-
}
377364

378-
// Classes can inherit from subclass existentials as long as they
379-
// do not contain an explicit AnyObject member.
380-
if (isa<ClassDecl>(decl) &&
381-
!layout.hasExplicitAnyObject) {
382-
// Superclass inheritance is handled below.
383-
inheritedTy = layout.explicitSuperclass;
384-
if (!inheritedTy)
365+
// Class-constrained protocol compositions are not allowed except in
366+
// special cases.
367+
if (layout.explicitSuperclass) {
368+
if (!canHaveSuperclass) {
369+
decl->diagnose(diag::inheritance_from_protocol_with_superclass,
370+
inheritedTy);
385371
continue;
372+
}
373+
374+
// Classes can inherit from protocol compositions that contain a
375+
// superclass, but not AnyObject.
376+
if (isa<ClassDecl>(decl) &&
377+
!layout.hasExplicitAnyObject) {
378+
// Superclass inheritance is handled below.
379+
inheritedTy = layout.explicitSuperclass;
380+
}
386381
}
387382
}
388-
383+
389384
// If this is an enum inheritance clause, check for a raw type.
390385
if (isa<EnumDecl>(decl)) {
391386
// Check if we already had a raw type.
@@ -445,29 +440,6 @@ static void checkInheritanceClause(
445440
continue;
446441
}
447442

448-
// @objc protocols cannot have superclass constraints.
449-
if (auto *protoDecl = dyn_cast<ProtocolDecl>(decl)) {
450-
if (protoDecl->isObjC()) {
451-
protoDecl->diagnose(diag::objc_protocol_with_superclass,
452-
protoDecl->getName());
453-
continue;
454-
}
455-
}
456-
457-
// If the declaration we're looking at doesn't allow a superclass,
458-
// complain.
459-
if (isa<StructDecl>(decl) || isa<ExtensionDecl>(decl)) {
460-
decl->diagnose(isa<ExtensionDecl>(decl)
461-
? diag::extension_class_inheritance
462-
: diag::non_class_inheritance,
463-
isa<ExtensionDecl>(decl)
464-
? cast<ExtensionDecl>(decl)->getDeclaredInterfaceType()
465-
: cast<TypeDecl>(decl)->getDeclaredInterfaceType(),
466-
inheritedTy)
467-
.highlight(inherited.getSourceRange());
468-
continue;
469-
}
470-
471443
// If this is not the first entry in the inheritance clause, complain.
472444
if (isa<ClassDecl>(decl) && i > 0) {
473445
auto removeRange = getRemovalRange(i);
@@ -480,21 +452,18 @@ static void checkInheritanceClause(
480452
// Fall through to record the superclass.
481453
}
482454

483-
// Record the superclass.
484-
superclassTy = inheritedTy;
485-
superclassRange = inherited.getSourceRange();
486-
continue;
455+
if (canHaveSuperclass) {
456+
// Record the superclass.
457+
superclassTy = inheritedTy;
458+
superclassRange = inherited.getSourceRange();
459+
continue;
460+
}
487461
}
488462

489-
// The GenericSignatureBuilder diagnoses problems with generic type
490-
// parameters.
491-
if (isa<GenericTypeParamDecl>(decl))
492-
continue;
493-
494463
// We can't inherit from a non-class, non-protocol type.
495-
decl->diagnose((isa<StructDecl>(decl) || isa<ExtensionDecl>(decl))
496-
? diag::inheritance_from_non_protocol
497-
: diag::inheritance_from_non_protocol_or_class,
464+
decl->diagnose(canHaveSuperclass
465+
? diag::inheritance_from_non_protocol_or_class
466+
: diag::inheritance_from_non_protocol,
498467
inheritedTy);
499468
// FIXME: Note pointing to the declaration 'inheritedTy' references?
500469
}

0 commit comments

Comments
 (0)