Skip to content

Commit 25718c7

Browse files
committed
RequirementMachine: Preliminary support for minimizing layout, superclass and concrete type requirements
1 parent 5643972 commit 25718c7

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ RequirementMachine::buildRequirementSignature(ArrayRef<unsigned> rules,
5050
llvm::SmallDenseMap<TypeBase *, llvm::SmallVector<Type, 2>> sameTypeReqs;
5151

5252
auto genericParams = proto->getGenericSignature().getGenericParams();
53+
const auto &protos = System.getProtocols();
5354

5455
// Convert a rewrite rule into a requirement.
5556
auto createRequirementFromRule = [&](const Rule &rule) {
5657
if (auto prop = rule.isPropertyRule()) {
5758
auto subjectType = Context.getTypeForTerm(rule.getRHS(), genericParams,
58-
System.getProtocols());
59+
protos);
5960

6061
switch (prop->getKind()) {
6162
case Symbol::Kind::Protocol:
@@ -65,9 +66,29 @@ RequirementMachine::buildRequirementSignature(ArrayRef<unsigned> rules,
6566
return;
6667

6768
case Symbol::Kind::Layout:
68-
case Symbol::Kind::ConcreteType:
69+
reqs.emplace_back(RequirementKind::Layout,
70+
subjectType,
71+
prop->getLayoutConstraint());
72+
return;
73+
6974
case Symbol::Kind::Superclass:
70-
// FIXME
75+
reqs.emplace_back(RequirementKind::Superclass,
76+
subjectType,
77+
Context.getTypeFromSubstitutionSchema(
78+
prop->getSuperclass(),
79+
prop->getSubstitutions(),
80+
genericParams, MutableTerm(),
81+
protos));
82+
return;
83+
84+
case Symbol::Kind::ConcreteType:
85+
reqs.emplace_back(RequirementKind::SameType,
86+
subjectType,
87+
Context.getTypeFromSubstitutionSchema(
88+
prop->getConcreteType(),
89+
prop->getSubstitutions(),
90+
genericParams, MutableTerm(),
91+
protos));
7192
return;
7293

7394
case Symbol::Kind::Name:
@@ -79,9 +100,9 @@ RequirementMachine::buildRequirementSignature(ArrayRef<unsigned> rules,
79100
llvm_unreachable("Invalid symbol kind");
80101
} else if (rule.getLHS().back().getKind() != Symbol::Kind::Protocol) {
81102
auto constraintType = Context.getTypeForTerm(rule.getLHS(), genericParams,
82-
System.getProtocols());
103+
protos);
83104
auto subjectType = Context.getTypeForTerm(rule.getRHS(), genericParams,
84-
System.getProtocols());
105+
protos);
85106

86107
sameTypeReqs[subjectType.getPointer()].push_back(constraintType);
87108
}

test/Generics/sr14580.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift
2-
// RUN: %target-swift-frontend -typecheck -debug-generic-signatures %s 2>&1 | %FileCheck %s
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on
2+
// RUN: %target-swift-frontend -typecheck -debug-generic-signatures -requirement-machine-protocol-signatures=on %s 2>&1 | %FileCheck %s
33

44
public protocol ScalarProtocol: ScalarMultiplicative where Self == Scalar {
55
}
@@ -13,10 +13,8 @@ public protocol MapReduceArithmetic: ScalarMultiplicative, Collection where Elem
1313
public protocol Tensor: MapReduceArithmetic where Element == Scalar {
1414
}
1515

16-
// FIXME: The same-type requirement canonicalization still produces bugus results here, but
17-
// at least we don't crash.
18-
19-
// CHECK-LABEL: Requirement signature: <Self where Self : Tensor, Self == Self.Float16Components.Model, Self.Element == Double, Self.Float16Components : ColorComponents, Self.Float32Components : ColorComponents, Self.Float16Components.Element == Double, Self.Float16Components.Model == Self.Float32Components.Model, Self.Float32Components.Element == Double, Self.Float32Components.Indices == Self.Float32Components.Indices.SubSequence, Self.Float32Components.SubSequence == Self.Float32Components.SubSequence.SubSequence, Self.Float32Components.Indices.Indices == Self.Float32Components.Indices.Indices.SubSequence, Self.Float32Components.SubSequence.Indices == Self.Float32Components.SubSequence.Indices.SubSequence, Self.Float32Components.SubSequence.Indices.Indices == Self.Float32Components.SubSequence.Indices.Indices.SubSequence>
16+
// CHECK-LABEL: sr14580.(file).ColorModel@
17+
// CHECK-LABEL: Requirement signature: <Self where Self : Tensor, Self == Self.Float16Components.Model, Self.Element == Double, Self.Float16Components : ColorComponents, Self.Float32Components : ColorComponents, Self.Float16Components.Element == Double, Self.Float16Components.Model == Self.Float32Components.Model, Self.Float32Components.Element == Double>
2018
public protocol ColorModel: Tensor where Scalar == Double {
2119
associatedtype Float16Components: ColorComponents where Float16Components.Model == Self, Float16Components.Scalar == Double
2220
associatedtype Float32Components: ColorComponents where Float32Components.Model == Self, Float32Components.Scalar == Double

0 commit comments

Comments
 (0)