Skip to content

Commit c93390f

Browse files
authored
Merge pull request swiftlang#37932 from slavapestov/assert-failure-mapping-out-of-context
Sema: Avoid an assertion when diagnosing failures in some cases with invalid associated types
2 parents f538b49 + 70f038a commit c93390f

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,11 +874,16 @@ static Optional<RequirementMatch> findMissingGenericRequirementForSolutionFix(
874874
type = solution.simplifyType(type);
875875
missingType = solution.simplifyType(missingType);
876876

877-
missingType = missingType->mapTypeOutOfContext();
878-
if (missingType->hasTypeParameter())
879-
if (auto env = conformance->getGenericEnvironment())
880-
if (auto assocType = env->mapTypeIntoContext(missingType))
881-
missingType = assocType;
877+
if (auto *env = conformance->getGenericEnvironment()) {
878+
// We use subst() with LookUpConformanceInModule here, because
879+
// associated type inference failures mean that we can end up
880+
// here with a DependentMemberType with an ArchetypeType base.
881+
missingType = missingType.subst(
882+
[&](SubstitutableType *type) -> Type {
883+
return env->mapTypeIntoContext(type->mapTypeOutOfContext());
884+
},
885+
LookUpConformanceInModule(conformance->getDeclContext()->getParentModule()));
886+
}
882887

883888
auto missingRequirementMatch = [&](Type type) -> RequirementMatch {
884889
Requirement requirement(requirementKind, type, missingType);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend -emit-ir %s
2+
3+
public protocol PublicContent {
4+
associatedtype Model
5+
init(_ model: Model)
6+
}
7+
8+
public protocol PublicConvertible {
9+
associatedtype Public
10+
func toPublic() -> Public
11+
}
12+
13+
extension PublicConvertible where Public: PublicContent, Public.Model == Self {
14+
public func toPublic() -> Public {
15+
Public(self)
16+
}
17+
}
18+
19+
extension Array: PublicConvertible where Element: PublicConvertible {
20+
public func toPublic() -> [Element.Public] {
21+
map { $0.toPublic() }
22+
}
23+
}

0 commit comments

Comments
 (0)