Skip to content

Tiny associated type inference fixes #28157

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
merged 5 commits into from
Nov 14, 2019
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
60 changes: 50 additions & 10 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,16 +2215,39 @@ ConformanceChecker::getReferencedAssociatedTypes(ValueDecl *req) {
return known->second;

// Collect the set of associated types rooted on Self in the
// signature.
// signature. Note that for references to nested types, we only
// want to consider the outermost dependent member type.
//
// For example, a requirement typed '(Iterator.Element) -> ()'
// is not considered to reference the associated type 'Iterator'.
auto &assocTypes = ReferencedAssociatedTypes[req];
llvm::SmallPtrSet<AssociatedTypeDecl *, 4> knownAssocTypes;
req->getInterfaceType()->getCanonicalType().visit([&](CanType type) {
if (auto assocType = getReferencedAssocTypeOfProtocol(type, Proto)) {
if (knownAssocTypes.insert(assocType).second) {
assocTypes.push_back(assocType);

class Walker : public TypeWalker {
ProtocolDecl *Proto;
llvm::SmallVectorImpl<AssociatedTypeDecl *> &assocTypes;
llvm::SmallPtrSet<AssociatedTypeDecl *, 4> knownAssocTypes;

public:
Walker(ProtocolDecl *Proto,
llvm::SmallVectorImpl<AssociatedTypeDecl *> &assocTypes)
: Proto(Proto), assocTypes(assocTypes) {}

Action walkToTypePre(Type type) override {
if (type->is<DependentMemberType>()) {
if (auto assocType = getReferencedAssocTypeOfProtocol(type, Proto)) {
if (knownAssocTypes.insert(assocType).second)
assocTypes.push_back(assocType);
}

return Action::SkipChildren;
}
});

return Action::Continue;
}
};

Walker walker(Proto, assocTypes);
req->getInterfaceType()->getCanonicalType().walk(walker);

return assocTypes;
}
Expand Down Expand Up @@ -3487,20 +3510,37 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
AssociatedTypeDecl *assocType) {
// Conformances constructed by the ClangImporter should have explicit type
// witnesses already.
if (isa<ClangModuleUnit>(Conformance->getDeclContext()->getModuleScopeContext())) {
if (isa<ClangModuleUnit>(DC->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 ResolveWitnessResult::Missing;
return checkForGenericParameter();
}

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

// If there is a single viable candidate, form a substitution for it.
if (viable.size() == 1) {
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,14 +918,6 @@ 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
28 changes: 4 additions & 24 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,18 +1406,13 @@ static Type resolveNestedIdentTypeComponent(

// Diagnose a bad conformance reference if we need to.
if (!options.contains(TypeResolutionFlags::SilenceErrors) &&
inferredAssocType && memberType && memberType->hasError()) {
inferredAssocType && memberType->hasError()) {
maybeDiagnoseBadConformanceRef(DC, parentTy, comp->getLoc(),
inferredAssocType);
}

// If we found a reference to an associated type or other member type that
// was marked invalid, just return ErrorType to silence downstream errors.
if (member->isInvalid())
return ErrorType::get(ctx);

// At this point, we need to have resolved the type of the member.
if (!memberType || memberType->hasError())
if (memberType->hasError())
return memberType;

// If there are generic arguments, apply them now.
Expand Down Expand Up @@ -2592,26 +2587,11 @@ Type TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
auto argTy = resolveType(repr->getGenericArguments()[i], options);
genericArgMap.insert({params[i], argTy->getCanonicalType()});
}

bool ok = true;

subMap = SubstitutionMap::get(
genericSig,
QueryTypeSubstitutionMap{genericArgMap},
[&](CanType depTy, Type replacement, ProtocolDecl *proto)
-> ProtocolConformanceRef {
auto result = TypeChecker::conformsToProtocol(
replacement, proto, DC,
ConformanceCheckOptions());
if (result.isInvalid()) {
ok = false;
return ProtocolConformanceRef(proto);
}

return result;
});

if (!ok)
return ErrorType::get(Context);
TypeChecker::LookUpConformance(DC));
}

auto layout = SILLayout::get(Context, genericSig, fields);
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/core/NativeSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ extension _NativeSet {
}

extension _NativeSet: _SetBuffer {
@usableFromInline
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes away in the next PR.

internal typealias Element = Element

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

Expand Down
5 changes: 2 additions & 3 deletions test/decl/nested/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ func testLookup(_ x: OuterForUFI.Inner) {
x.extMethod()
}

// N.B. Lookup fails here because OuterForUFI.Inner is marked invalid.
func testLookup<T: OuterForUFI.Inner>(_ x: T) {
x.req() // expected-error {{value of type 'T' has no member 'req'}}
x.extMethod() // expected-error {{value of type 'T' has no member 'extMethod'}}
x.req()
x.extMethod()
}
56 changes: 56 additions & 0 deletions test/decl/protocol/req/associated_type_inference_valid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// RUN: %target-swift-frontend -emit-silgen %s

// This is a SILGen test to ensure we can completely check these conformances
// and build valid AST.

protocol P {
associatedtype T : Q = S
typealias Y = T.X

func foo(_: T.X)
}

protocol Q {
associatedtype X
}

struct S : Q {
typealias X = ()
}

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: 0 additions & 18 deletions test/decl/protocol/typealias_inference.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Concrete<AType2: ProtoA>: ProtoB {
fatalError()
}

func protoFunc() -> Alias { // expected-error{{unsupported recursion for reference to type alias 'Alias' of type 'Concrete<AType2>'}}
func protoFunc() -> Alias {
fatalError()
}
}
28 changes: 28 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr11392.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-swift-frontend -verify -emit-ir %s

// Ideally this would type check successfully; we should be able to
// infer that X == Int using the same-type constraint 'A.X == X'.

protocol P1 {
associatedtype X
// expected-note@-1 {{protocol requires nested type 'X'; do you want to add it?}}
associatedtype A: P2 where A.X == X
}

protocol P2 {
associatedtype X
}

struct S {}

extension S {
struct A: P2 {
typealias X = Int
}
}


extension S: P1 {}
// expected-error@-1 {{type 'S' does not conform to protocol 'P1'}}

print(S.X.self)