Skip to content

Commit 55e3797

Browse files
committed
Revert "Sema: Look for generic parameters first when inferring an associated type"
This breaks source compatibility a little bit more than we'd like, so reverting it for now. Fixes <rdar://problem/57213598>. This reverts commit 04fbcc0.
1 parent 84e1d7e commit 55e3797

File tree

5 files changed

+29
-55
lines changed

5 files changed

+29
-55
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,37 +3502,20 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
35023502
AssociatedTypeDecl *assocType) {
35033503
// Conformances constructed by the ClangImporter should have explicit type
35043504
// witnesses already.
3505-
if (isa<ClangModuleUnit>(DC->getModuleScopeContext())) {
3505+
if (isa<ClangModuleUnit>(Conformance->getDeclContext()->getModuleScopeContext())) {
35063506
llvm::errs() << "Cannot look up associated type for imported conformance:\n";
35073507
Conformance->getType().dump(llvm::errs());
35083508
assocType->dump(llvm::errs());
35093509
abort();
35103510
}
35113511

3512-
// If we fail to find a witness via lookup, check for a generic parameter.
3513-
auto checkForGenericParameter = [&]() {
3514-
// If there is a generic parameter of the named type, use that.
3515-
if (auto genericSig = DC->getGenericSignatureOfContext()) {
3516-
for (auto gp : genericSig->getInnermostGenericParams()) {
3517-
if (gp->getName() == assocType->getName()) {
3518-
if (!checkTypeWitness(DC, Proto, assocType, gp)) {
3519-
recordTypeWitness(assocType, gp, nullptr);
3520-
return ResolveWitnessResult::Success;
3521-
}
3522-
}
3523-
}
3524-
}
3525-
3526-
return ResolveWitnessResult::Missing;
3527-
};
3528-
35293512
// Look for a member type with the same name as the associated type.
35303513
auto candidates = TypeChecker::lookupMemberType(
35313514
DC, Adoptee, assocType->getName(), NameLookupFlags::ProtocolMembers);
35323515

35333516
// If there aren't any candidates, we're done.
35343517
if (!candidates) {
3535-
return checkForGenericParameter();
3518+
return ResolveWitnessResult::Missing;
35363519
}
35373520

35383521
// Determine which of the candidates is viable.
@@ -3566,7 +3549,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
35663549
return x.first->getDeclContext()
35673550
->getSelfProtocolDecl() == nullptr;
35683551
}) == nonViable.end())
3569-
return checkForGenericParameter();
3552+
return ResolveWitnessResult::Missing;
35703553

35713554
// If there is a single viable candidate, form a substitution for it.
35723555
if (viable.size() == 1) {

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ AssociatedTypeInference::computeAbstractTypeWitness(
918918
return derivedType;
919919
}
920920

921+
// If there is a generic parameter of the named type, use that.
922+
if (auto genericSig = dc->getGenericSignatureOfContext()) {
923+
for (auto gp : genericSig->getInnermostGenericParams()) {
924+
if (gp->getName() == assocType->getName())
925+
return dc->mapTypeIntoContext(gp);
926+
}
927+
}
928+
921929
return Type();
922930
}
923931

stdlib/public/core/NativeSet.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,6 @@ extension _NativeSet {
282282
}
283283

284284
extension _NativeSet: _SetBuffer {
285-
@usableFromInline
286-
internal typealias Element = Element
287-
288285
@usableFromInline
289286
internal typealias Index = Set<Element>.Index
290287

test/decl/protocol/req/associated_type_inference_valid.swift

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,3 @@ struct R : P {
2222
let x: Y? = nil
2323
func foo(_: Y) {}
2424
}
25-
26-
// SR-8813
27-
protocol BaseProtocol {
28-
associatedtype Value
29-
typealias Closure = () -> Value
30-
31-
init(closure: Closure)
32-
}
33-
34-
struct Base<Value>: BaseProtocol {
35-
private var closure: Closure?
36-
37-
init(closure: Closure) {
38-
withoutActuallyEscaping(closure) { new in
39-
self.closure = new
40-
}
41-
}
42-
}
43-
44-
// SR-11407
45-
protocol _Drivable: AnyObject {
46-
typealias Driver = Self
47-
}
48-
protocol Configurator {
49-
associatedtype Drivable: _Drivable
50-
typealias Driver = Drivable.Driver
51-
func configure(driver: Driver)
52-
}
53-
struct AnyConfigurator<Drivable: _Drivable>: Configurator {
54-
private let thing: Driver?
55-
func configure(driver: AnyConfigurator.Driver) {}
56-
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// SR-8813
4+
// By itself in this file because this particular expected error is omitted if there have been any other diagnostics.
5+
protocol BaseProtocol {
6+
associatedtype Value
7+
typealias Closure = () -> Value
8+
9+
init(closure: Closure)
10+
}
11+
12+
struct Base<Value>: BaseProtocol {
13+
private let closure: Closure
14+
15+
init(closure: Closure) { //expected-error {{reference to invalid type alias 'Closure' of type 'Base<Value>'}}
16+
self.closure = closure
17+
}
18+
}

0 commit comments

Comments
 (0)