Skip to content

Commit dcb0673

Browse files
authored
Merge pull request #12691 from DougGregor/gsb-restrict-infer-from-protocol-definitions
[GSB] Don't infer requirements from types in the definitions of protocols
2 parents ae5f930 + fe54e70 commit dcb0673

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,11 +2315,9 @@ static void addConditionalRequirements(GenericSignatureBuilder &builder,
23152315
// Abstract conformances don't have associated decl-contexts/modules, but also
23162316
// don't have conditional requirements.
23172317
if (conformance.isConcrete()) {
2318-
auto mod = conformance.getConcrete()->getDeclContext()->getParentModule();
23192318
auto source = FloatingRequirementSource::forInferred(nullptr);
23202319
for (auto requirement : conformance.getConditionalRequirements()) {
2321-
builder.addRequirement(requirement, source,
2322-
/*inferForModule=*/mod);
2320+
builder.addRequirement(requirement, source, /*inferForModule=*/nullptr);
23232321
++NumConditionalRequirementsAdded;
23242322
}
23252323
}
@@ -3370,16 +3368,14 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
33703368
return ConstraintResult::Resolved;
33713369
}
33723370

3373-
auto protoModule = proto->getParentModule();
3374-
33753371
if (!onlySameTypeConstraints) {
33763372
// Add all of the inherited protocol requirements, recursively.
33773373
if (auto resolver = getLazyResolver())
33783374
resolver->resolveInheritedProtocols(proto);
33793375

33803376
auto inheritedReqResult =
33813377
addInheritedRequirements(proto, selfType.getUnresolvedType(), source,
3382-
protoModule);
3378+
/*inferForModule=*/nullptr);
33833379
if (isErrorResult(inheritedReqResult))
33843380
return inheritedReqResult;
33853381
}
@@ -3394,7 +3390,8 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
33943390

33953391
auto innerSource = FloatingRequirementSource::viaProtocolRequirement(
33963392
source, proto, &req, /*inferred=*/false);
3397-
addRequirement(&req, innerSource, &protocolSubMap, protoModule);
3393+
addRequirement(&req, innerSource, &protocolSubMap,
3394+
/*inferForModule=*/nullptr);
33983395
}
33993396
}
34003397

@@ -3516,7 +3513,7 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
35163513
if (!onlySameTypeConstraints) {
35173514
auto assocResult =
35183515
addInheritedRequirements(assocTypeDecl, assocType, source,
3519-
protoModule);
3516+
/*inferForModule=*/nullptr);
35203517
if (isErrorResult(assocResult))
35213518
return assocResult;
35223519
}
@@ -3533,7 +3530,8 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
35333530
auto innerSource =
35343531
FloatingRequirementSource::viaProtocolRequirement(
35353532
source, proto, &req, /*inferred=*/false);
3536-
addRequirement(&req, innerSource, &protocolSubMap, protoModule);
3533+
addRequirement(&req, innerSource, &protocolSubMap,
3534+
/*inferForModule=*/nullptr);
35373535
}
35383536
}
35393537

test/Generics/requirement_inference.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ struct X22<T, U> {
299299
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X20<τ_0_0.B>, τ_0_0.B : P20>
300300
protocol P22 {
301301
associatedtype A
302-
associatedtype B where A == X20<B>
302+
associatedtype B: P20 where A == X20<B>
303303
}
304304

305305
// CHECK: Generic signature: <Self where Self : P23>
@@ -327,22 +327,27 @@ struct X24<T: P20> : P24 {
327327
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X24<τ_0_0.B>, τ_0_0.B : P20>
328328
protocol P25a {
329329
associatedtype A: P24 // expected-warning{{redundant conformance constraint 'Self.A': 'P24'}}
330-
associatedtype B where A == X24<B> // expected-note{{conformance constraint 'Self.A': 'P24' implied here}}
330+
associatedtype B: P20 where A == X24<B> // expected-note{{conformance constraint 'Self.A': 'P24' implied here}}
331331
}
332332

333333
// CHECK-LABEL: .P25b@
334334
// CHECK-NEXT: Requirement signature: <Self where Self.A == X24<Self.B>, Self.B : P20>
335335
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X24<τ_0_0.B>, τ_0_0.B : P20>
336336
protocol P25b {
337337
associatedtype A
338-
associatedtype B where A == X24<B>
338+
associatedtype B: P20 where A == X24<B>
339339
}
340340

341341
protocol P25c {
342342
associatedtype A: P24
343343
associatedtype B where A == X<B> // expected-error{{use of undeclared type 'X'}}
344344
}
345345

346+
protocol P25d {
347+
associatedtype A
348+
associatedtype B where A == X24<B> // expected-error{{type 'Self.B' does not conform to protocol 'P20'}}
349+
}
350+
346351
// Similar to the above, but with superclass constraints.
347352
protocol P26 {
348353
associatedtype C: X3
@@ -357,15 +362,15 @@ struct X26<T: X3> : P26 {
357362
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X26<τ_0_0.B>, τ_0_0.B : X3>
358363
protocol P27a {
359364
associatedtype A: P26 // expected-warning{{redundant conformance constraint 'Self.A': 'P26'}}
360-
associatedtype B where A == X26<B> // expected-note{{conformance constraint 'Self.A': 'P26' implied here}}
365+
associatedtype B: X3 where A == X26<B> // expected-note{{conformance constraint 'Self.A': 'P26' implied here}}
361366
}
362367

363368
// CHECK-LABEL: .P27b@
364369
// CHECK-NEXT: Requirement signature: <Self where Self.A == X26<Self.B>, Self.B : X3>
365370
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X26<τ_0_0.B>, τ_0_0.B : X3>
366371
protocol P27b {
367372
associatedtype A
368-
associatedtype B where A == X26<B>
373+
associatedtype B: X3 where A == X26<B>
369374
}
370375

371376
// ----------------------------------------------------------------------------
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
extension CountableRange{{}func a<a:A
1010
protocol A{typealias e:a{}class a

validation-test/compiler_crashers/28853-result-second.swift renamed to validation-test/compiler_crashers_fixed/28853-result-second.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
func a<a{
1111
extension{{}
1212
{

0 commit comments

Comments
 (0)