Skip to content

Commit 175d586

Browse files
committed
RequirementMachine: Don't substitute unsatisfied layout requirements in concrete contraction
1 parent 06b181a commit 175d586

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
381381

382382
case RequirementKind::Layout: {
383383
auto substFirstType = substTypeParameter(firstType);
384+
if (!substFirstType->isTypeParameter() &&
385+
!substFirstType->satisfiesClassConstraint() &&
386+
req.getLayoutConstraint()->isClass()) {
387+
// If the concrete type doesn't satisfy the layout constraint,
388+
// leave it unsubstituted so that we produce a better diagnostic.
389+
return req;
390+
}
384391

385392
return Requirement(req.getKind(),
386393
substFirstType,

test/Generics/concrete_same_type_versus_anyobject.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
// RUN: %target-typecheck-verify-swift
2-
// RUN: not %target-swift-frontend -typecheck -debug-generic-signatures -requirement-machine-inferred-signatures=verify %s 2>&1 | %FileCheck %s
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-inferred-signatures=on
2+
// RUN: not %target-swift-frontend -typecheck -debug-generic-signatures -requirement-machine-inferred-signatures=on %s 2>&1 | %FileCheck %s
3+
34
struct S {}
45
class C {}
56

67
struct G1<T : AnyObject> {}
78

8-
// CHECK-LABEL: Generic signature: <T where T == S>
9+
// CHECK: ExtensionDecl line={{.*}} base=G1
10+
// CHECK-NEXT: Generic signature: <T>
911
extension G1 where T == S {}
10-
// expected-error@-1 {{'T' requires that 'S' be a class type}}
11-
// expected-note@-2 {{same-type constraint 'T' == 'S' implied here}}
12+
// expected-error@-1 {{no type for 'T' can satisfy both 'T : AnyObject' and 'T == S'}}
1213

13-
// CHECK-LABEL: Generic signature: <T where T == C>
14+
// CHECK: ExtensionDecl line={{.*}} base=G1
15+
// CHECK-NEXT: Generic signature: <T where T == C>
1416
extension G1 where T == C {}
1517

1618
struct G2<U> {}
1719

18-
// CHECK-LABEL: Generic signature: <U where U == S>
20+
// CHECK: ExtensionDecl line={{.*}} base=G2
21+
// CHECK-NEXT: Generic signature: <U>
1922
extension G2 where U == S, U : AnyObject {}
20-
// expected-error@-1 {{'U' requires that 'S' be a class type}}
21-
// expected-note@-2 {{same-type constraint 'U' == 'S' implied here}}
22-
// expected-note@-3 {{constraint 'U' : 'AnyObject' implied here}}
23+
// expected-error@-1 {{no type for 'U' can satisfy both 'U : AnyObject' and 'U == S'}}
2324

24-
// CHECK-LABEL: Generic signature: <U where U == C>
25+
// CHECK: ExtensionDecl line={{.*}} base=G2
26+
// CHECK-NEXT: Generic signature: <U where U == C>
2527
extension G2 where U == C, U : AnyObject {}
26-
// expected-warning@-1 {{redundant constraint 'U' : 'AnyObject'}}
27-
// expected-note@-2 {{constraint 'U' : 'AnyObject' implied here}}
28+
// expected-warning@-1 {{redundant constraint 'C' : 'AnyObject'}}
2829

29-
// CHECK-LABEL: Generic signature: <U where U : C>
30+
// CHECK: ExtensionDecl line={{.*}} base=G2
31+
// CHECK-NEXT: Generic signature: <U where U : C>
3032
extension G2 where U : C, U : AnyObject {}
3133
// expected-warning@-1 {{redundant constraint 'U' : 'AnyObject'}}
32-
// expected-note@-2 {{constraint 'U' : 'AnyObject' implied here}}
3334

3435
// Explicit AnyObject conformance vs derived same-type
3536
protocol P {
3637
associatedtype A where A == C
3738
}
3839

39-
// CHECK-LABEL: Generic signature: <T where T : P>
40+
// CHECK: .explicitAnyObjectIsRedundant@
41+
// CHECK-NEXT: Generic signature: <T where T : P>
4042
func explicitAnyObjectIsRedundant<T : P>(_: T) where T.A : AnyObject {}
4143
// expected-warning@-1 {{redundant constraint 'T.A' : 'AnyObject'}}
42-
// expected-note@-2 {{constraint 'T.A' : 'AnyObject' implied here}}

0 commit comments

Comments
 (0)