Skip to content

Revert "Sema: Look for generic parameters first when inferring an associated type" #28289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3502,37 +3502,20 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
AssociatedTypeDecl *assocType) {
// Conformances constructed by the ClangImporter should have explicit type
// witnesses already.
if (isa<ClangModuleUnit>(DC->getModuleScopeContext())) {
if (isa<ClangModuleUnit>(Conformance->getDeclContext()->getModuleScopeContext())) {
llvm::errs() << "Cannot look up associated type for imported conformance:\n";
Conformance->getType().dump(llvm::errs());
assocType->dump(llvm::errs());
abort();
}

// If we fail to find a witness via lookup, check for a generic parameter.
auto checkForGenericParameter = [&]() {
// If there is a generic parameter of the named type, use that.
if (auto genericSig = DC->getGenericSignatureOfContext()) {
for (auto gp : genericSig->getInnermostGenericParams()) {
if (gp->getName() == assocType->getName()) {
if (!checkTypeWitness(DC, Proto, assocType, gp)) {
recordTypeWitness(assocType, gp, nullptr);
return ResolveWitnessResult::Success;
}
}
}
}

return ResolveWitnessResult::Missing;
};

// Look for a member type with the same name as the associated type.
auto candidates = TypeChecker::lookupMemberType(
DC, Adoptee, assocType->getName(), NameLookupFlags::ProtocolMembers);

// If there aren't any candidates, we're done.
if (!candidates) {
return checkForGenericParameter();
return ResolveWitnessResult::Missing;
}

// Determine which of the candidates is viable.
Expand Down Expand Up @@ -3566,7 +3549,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
return x.first->getDeclContext()
->getSelfProtocolDecl() == nullptr;
}) == nonViable.end())
return checkForGenericParameter();
return ResolveWitnessResult::Missing;

// If there is a single viable candidate, form a substitution for it.
if (viable.size() == 1) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,14 @@ AssociatedTypeInference::computeAbstractTypeWitness(
return derivedType;
}

// If there is a generic parameter of the named type, use that.
if (auto genericSig = dc->getGenericSignatureOfContext()) {
for (auto gp : genericSig->getInnermostGenericParams()) {
if (gp->getName() == assocType->getName())
return dc->mapTypeIntoContext(gp);
}
}

return Type();
}

Expand Down
3 changes: 0 additions & 3 deletions stdlib/public/core/NativeSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ extension _NativeSet {
}

extension _NativeSet: _SetBuffer {
@usableFromInline
internal typealias Element = Element

@usableFromInline
internal typealias Index = Set<Element>.Index

Expand Down
32 changes: 0 additions & 32 deletions test/decl/protocol/req/associated_type_inference_valid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,3 @@ struct R : P {
let x: Y? = nil
func foo(_: Y) {}
}

// SR-8813
protocol BaseProtocol {
associatedtype Value
typealias Closure = () -> Value

init(closure: Closure)
}

struct Base<Value>: BaseProtocol {
private var closure: Closure?

init(closure: Closure) {
withoutActuallyEscaping(closure) { new in
self.closure = new
}
}
}

// SR-11407
protocol _Drivable: AnyObject {
typealias Driver = Self
}
protocol Configurator {
associatedtype Drivable: _Drivable
typealias Driver = Drivable.Driver
func configure(driver: Driver)
}
struct AnyConfigurator<Drivable: _Drivable>: Configurator {
private let thing: Driver?
func configure(driver: AnyConfigurator.Driver) {}
}
18 changes: 18 additions & 0 deletions test/decl/protocol/typealias_inference.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-typecheck-verify-swift

// SR-8813
// By itself in this file because this particular expected error is omitted if there have been any other diagnostics.
protocol BaseProtocol {
associatedtype Value
typealias Closure = () -> Value

init(closure: Closure)
}

struct Base<Value>: BaseProtocol {
private let closure: Closure

init(closure: Closure) { //expected-error {{reference to invalid type alias 'Closure' of type 'Base<Value>'}}
self.closure = closure
}
}