Skip to content

Commit 3939786

Browse files
authored
Merge pull request #31869 from gregomni/sr12759
[QoI, Crasher] Don't include type variables in missing generic requirement match info
2 parents c230622 + d764fe8 commit 3939786

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,8 @@ static const RequirementEnvironment &getOrCreateRequirementEnvironment(
751751
}
752752

753753
static Optional<RequirementMatch> findMissingGenericRequirementForSolutionFix(
754-
constraints::ConstraintFix *fix, ValueDecl *witness,
755-
ProtocolConformance *conformance,
754+
constraints::Solution &solution, constraints::ConstraintFix *fix,
755+
ValueDecl *witness, ProtocolConformance *conformance,
756756
const RequirementEnvironment &reqEnvironment) {
757757
Type type, missingType;
758758
RequirementKind requirementKind;
@@ -785,6 +785,15 @@ static Optional<RequirementMatch> findMissingGenericRequirementForSolutionFix(
785785
return Optional<RequirementMatch>();
786786
}
787787

788+
type = solution.simplifyType(type);
789+
missingType = solution.simplifyType(missingType);
790+
791+
missingType = missingType->mapTypeOutOfContext();
792+
if (missingType->hasTypeParameter())
793+
if (auto env = conformance->getGenericEnvironment())
794+
if (auto assocType = env->mapTypeIntoContext(missingType))
795+
missingType = assocType;
796+
788797
auto missingRequirementMatch = [&](Type type) -> RequirementMatch {
789798
Requirement requirement(requirementKind, type, missingType);
790799
return RequirementMatch(witness, MatchKind::MissingRequirement,
@@ -986,7 +995,7 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
986995
if (solution && conformance && solution->Fixes.size()) {
987996
for (auto fix : solution->Fixes) {
988997
if (auto result = findMissingGenericRequirementForSolutionFix(
989-
fix, witness, conformance, reqEnvironment))
998+
*solution, fix, witness, conformance, reqEnvironment))
990999
return *result;
9911000
}
9921001
}

test/decl/protocol/req/missing_conformance.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,31 @@ struct S3 : P12 { // expected-error {{type 'S3' does not conform to protocol 'P1
110110
// expected-note@-1 {{candidate can not infer 'A' = 'P11' because 'P11' is not a nominal type and so can't conform to 'P11'}}
111111
}
112112

113+
// SR-12759
114+
struct CountSteps1<T> : Collection {
115+
init(count: Int) { self.count = count }
116+
var count: Int
117+
118+
var startIndex: Int { 0 }
119+
var endIndex: Int { count }
120+
func index(after i: Int) -> Int {
121+
totalSteps += 1 // expected-error {{cannot find 'totalSteps' in scope}}
122+
return i + 1
123+
}
124+
subscript(i: Int) -> Int { return i }
125+
}
126+
127+
extension CountSteps1 // expected-error {{type 'CountSteps1<T>' does not conform to protocol 'RandomAccessCollection'}}
128+
// expected-error@-1 {{conditional conformance of type 'CountSteps1<T>' to protocol 'RandomAccessCollection' does not imply conformance to inherited protocol 'BidirectionalCollection'}}
129+
// expected-note@-2 {{did you mean to explicitly state the conformance like 'extension CountSteps1: BidirectionalCollection where ...'?}}
130+
// expected-error@-3 {{type 'CountSteps1<T>' does not conform to protocol 'BidirectionalCollection'}}
131+
: RandomAccessCollection
132+
where T : Equatable
133+
{
134+
typealias Index = Int
135+
func index(_ i: Index, offsetBy d: Int) -> Index {
136+
return i + d
137+
}
138+
}
139+
140+

0 commit comments

Comments
 (0)