Skip to content

Commit 60a5780

Browse files
authored
Merge pull request swiftlang#33445 from slavapestov/objc-protocols-have-a-layout-constraint
GSB: Add an inferred AnyObject constraint to @objc protocol requirement signatures
2 parents bd5d0c3 + b07a17e commit 60a5780

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,9 +1297,9 @@ bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req) {
12971297
bool inWhereClause;
12981298

12991299
switch (req.getKind()) {
1300+
case RequirementKind::Layout:
13001301
case RequirementKind::Conformance:
1301-
case RequirementKind::Superclass:
1302-
case RequirementKind::Layout: {
1302+
case RequirementKind::Superclass: {
13031303
auto subject = req.getFirstType();
13041304
auto result = findRelevantDeclAndDirectUse(subject);
13051305

@@ -1351,6 +1351,15 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
13511351
proto->getRequirementSignature()),
13521352
PrintInherited,
13531353
[&](const Requirement &req) {
1354+
// Skip the inferred 'Self : AnyObject' constraint if this is an
1355+
// @objc protocol.
1356+
if (req.getKind() == RequirementKind::Layout &&
1357+
req.getFirstType()->isEqual(proto->getProtocolSelfType()) &&
1358+
req.getLayoutConstraint()->getKind() == LayoutConstraintKind::Class &&
1359+
proto->isObjC()) {
1360+
return false;
1361+
}
1362+
13541363
auto location = bestRequirementPrintLocation(proto, req);
13551364
return location.AttachedTo == attachingTo && !location.InWhereClause;
13561365
});

lib/AST/GenericSignature.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,6 @@ bool GenericSignatureImpl::requiresClass(Type type) const {
404404
// If there is a layout constraint, it might be a class.
405405
if (equivClass->layout && equivClass->layout->isClass()) return true;
406406

407-
// If there is a superclass bound, then obviously it must be a class.
408-
// FIXME: We shouldn't need this?
409-
if (equivClass->superclass) return true;
410-
411-
// If any of the protocols are class-bound, then it must be a class.
412-
// FIXME: We shouldn't need this?
413-
for (const auto &conforms : equivClass->conformsTo) {
414-
if (conforms.first->requiresClass()) return true;
415-
}
416-
417407
return false;
418408
}
419409

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3871,9 +3871,21 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
38713871
return false;
38723872
});
38733873

3874-
// Remaining logic is not relevant in ObjC protocol cases.
3875-
if (proto->isObjC())
3874+
if (proto->isObjC()) {
3875+
// @objc implies an inferred AnyObject constraint.
3876+
auto innerSource =
3877+
FloatingRequirementSource::viaProtocolRequirement(source, proto,
3878+
/*inferred=*/true);
3879+
addLayoutRequirementDirect(selfType,
3880+
LayoutConstraint::getLayoutConstraint(
3881+
LayoutConstraintKind::Class,
3882+
getASTContext()),
3883+
innerSource);
3884+
38763885
return ConstraintResult::Resolved;
3886+
}
3887+
3888+
// Remaining logic is not relevant in ObjC protocol cases.
38773889

38783890
// Collect all of the inherited associated types and typealiases in the
38793891
// inherited protocols (recursively).

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,6 @@ class SubstFunctionTypeCollector {
12141214
substRequirements.push_back(
12151215
Requirement(RequirementKind::Layout, param, layout));
12161216
}
1217-
} else {
1218-
(void)0;
12191217
}
12201218

12211219
for (unsigned i : indices(upperBoundConformances)) {
@@ -1313,12 +1311,7 @@ class SubstFunctionTypeCollector {
13131311
CanType binding,
13141312
ArchetypeType *upperBound,
13151313
ArrayRef<ProtocolConformanceRef> bindingConformances) -> CanType {
1316-
// TODO: ArchetypeType::getLayoutConstraint sometimes misses out on
1317-
// implied layout constraints. For now AnyObject is the only one we
1318-
// care about.
1319-
return addSubstitution(archetype->requiresClass()
1320-
? LayoutConstraint::getLayoutConstraint(LayoutConstraintKind::Class)
1321-
: LayoutConstraint(),
1314+
return addSubstitution(archetype->getLayoutConstraint(),
13221315
binding,
13231316
upperBound,
13241317
bindingConformances);

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t SWIFTMODULE_VERSION_MINOR = 570; // isUserAccessible
58+
const uint16_t SWIFTMODULE_VERSION_MINOR = 571; // @objc protocols get AnyObject constraint
5959

6060
/// A standard hash seed used for all string hashes in a serialized module.
6161
///

test/api-digester/Outputs/clang-module-dump.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"declKind": "Protocol",
4444
"usr": "c:objc(pl)AnotherObjcProt",
4545
"moduleName": "Foo",
46+
"genericSig": "<Self : AnyObject>",
4647
"objc_name": "AnotherObjcProt",
4748
"declAttributes": [
4849
"ObjC",
@@ -196,6 +197,7 @@
196197
"declKind": "Protocol",
197198
"usr": "c:objc(pl)ObjcProt",
198199
"moduleName": "Foo",
200+
"genericSig": "<Self : AnyObject>",
199201
"objc_name": "ObjcProt",
200202
"declAttributes": [
201203
"ObjC",

0 commit comments

Comments
 (0)