Skip to content

RequirementMachine: Fix some trivial problems running validation tests with -requirement-machine-inferred-signatures=verify #41324

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
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
33 changes: 27 additions & 6 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8391,9 +8391,16 @@ AbstractGenericSignatureRequest::evaluate(
return rqmResult;

if (!rqmResult.getPointer()->isEqual(gsbResult.getPointer())) {
PrintOptions opts;
opts.ProtocolQualifiedDependentMemberTypes = true;

llvm::errs() << "RequirementMachine generic signature minimization is broken:\n";
llvm::errs() << "RequirementMachine says: " << rqmResult.getPointer() << "\n";
llvm::errs() << "GenericSignatureBuilder says: " << gsbResult.getPointer() << "\n";
llvm::errs() << "RequirementMachine says: ";
rqmResult.getPointer()->print(llvm::errs(), opts);
llvm::errs() << "\n";
llvm::errs() << "GenericSignatureBuilder says: ";
gsbResult.getPointer()->print(llvm::errs(), opts);
llvm::errs() << "\n";

abort();
}
Expand Down Expand Up @@ -8561,9 +8568,16 @@ InferredGenericSignatureRequest::evaluate(
return rqmResult;

if (!rqmResult.getPointer()->isEqual(gsbResult.getPointer())) {
PrintOptions opts;
opts.ProtocolQualifiedDependentMemberTypes = true;

llvm::errs() << "RequirementMachine generic signature minimization is broken:\n";
llvm::errs() << "RequirementMachine says: " << rqmResult.getPointer() << "\n";
llvm::errs() << "GenericSignatureBuilder says: " << gsbResult.getPointer() << "\n";
llvm::errs() << "RequirementMachine says: ";
rqmResult.getPointer()->print(llvm::errs(), opts);
llvm::errs() << "\n";
llvm::errs() << "GenericSignatureBuilder says: ";
gsbResult.getPointer()->print(llvm::errs(), opts);
llvm::errs() << "\n";

abort();
}
Expand Down Expand Up @@ -8663,16 +8677,23 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
auto gsbResult = buildViaGSB();

if (!compare(rqmResult, gsbResult)) {
PrintOptions opts;
opts.ProtocolQualifiedDependentMemberTypes = true;

llvm::errs() << "RequirementMachine protocol signature minimization is broken:\n";
llvm::errs() << "Protocol: " << proto->getName() << "\n";

llvm::errs() << "RequirementMachine says: ";
auto rqmSig = GenericSignature::get(
proto->getGenericSignature().getGenericParams(), rqmResult);
llvm::errs() << "RequirementMachine says: " << rqmSig << "\n";
rqmSig.print(llvm::errs(), opts);
llvm::errs() << "\n";

llvm::errs() << "GenericSignatureBuilder says: ";
auto gsbSig = GenericSignature::get(
proto->getGenericSignature().getGenericParams(), gsbResult);
llvm::errs() << "GenericSignatureBuilder says: " << gsbSig << "\n";
gsbSig.print(llvm::errs(), opts);
llvm::errs() << "\n";

abort();
}
Expand Down
46 changes: 31 additions & 15 deletions lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ static void desugarLayoutRequirement(Type subjectType,
result.emplace_back(RequirementKind::Layout, subjectType, layout);
}

static Type lookupMemberType(Type subjectType, ProtocolDecl *protoDecl,
AssociatedTypeDecl *assocType) {
if (subjectType->isTypeParameter())
return DependentMemberType::get(subjectType, assocType);

auto *M = protoDecl->getParentModule();
auto conformance = M->lookupConformance(
subjectType, protoDecl);
return conformance.getAssociatedType(subjectType,
assocType->getDeclaredInterfaceType());
}

/// Desugar a protocol conformance requirement by splitting up protocol
/// compositions on the right hand side into conformance and superclass
/// requirements.
Expand All @@ -124,7 +136,23 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,
// Fast path.
if (constraintType->is<ProtocolType>()) {
if (!subjectType->isTypeParameter()) {
// FIXME: Check conformance, diagnose redundancy or conflict upstream
// Check if the subject type actually conforms.
auto *protoDecl = constraintType->castTo<ProtocolType>()->getDecl();
auto *module = protoDecl->getParentModule();
auto conformance = module->lookupConformance(subjectType, protoDecl);
if (conformance.isInvalid()) {
// FIXME: Diagnose a conflict.
return;
}

// FIXME: Diagnose a redundancy.
assert(conformance.isConcrete());
auto *concrete = conformance.getConcrete();

// Introduce conditional requirements if the subject type is concrete.
for (auto req : concrete->getConditionalRequirements()) {
desugarRequirement(req, result);
}
return;
}

Expand All @@ -141,16 +169,7 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,

auto *assocType = protoDecl->getPrimaryAssociatedType();

Type memberType;
if (!subjectType->isTypeParameter()) {
auto *M = protoDecl->getParentModule();
auto conformance = M->lookupConformance(
subjectType, protoDecl);
memberType = conformance.getConcrete()->getTypeWitness(assocType);
} else {
memberType = DependentMemberType::get(subjectType, assocType);
}

auto memberType = lookupMemberType(subjectType, protoDecl, assocType);
desugarSameTypeRequirement(memberType, paramType->getArgumentType(),
result);
return;
Expand Down Expand Up @@ -278,10 +297,7 @@ struct InferRequirementsWalker : public TypeWalker {
auto addSameTypeConstraint = [&](Type firstType,
AssociatedTypeDecl *assocType) {
auto *protocol = assocType->getProtocol();
auto *module = protocol->getParentModule();
auto conf = module->lookupConformance(firstType, protocol);
auto secondType = conf.getAssociatedType(
firstType, assocType->getDeclaredInterfaceType());
auto secondType = lookupMemberType(firstType, protocol, assocType);
Requirement req(RequirementKind::SameType, firstType, secondType);
desugarRequirement(req, reqs);
};
Expand Down
32 changes: 27 additions & 5 deletions lib/AST/RequirementMachine/RequirementMachineRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,41 @@ RequirementMachine::buildRequirementsFromRules(
return;

case Symbol::Kind::Superclass: {
// For compatibility with the old GenericSignatureBuilder, drop requirements
// containing ErrorTypes.
// Requirements containing unresolved name symbols originate from
// invalid code and should not appear in the generic signature.
for (auto term : prop->getSubstitutions()) {
if (term.containsUnresolvedSymbols())
return;
}

// Requirements containing error types originate from invalid code
// and should not appear in the generic signature.
if (prop->getConcreteType()->hasError())
return;

auto superclassType = Map.getTypeFromSubstitutionSchema(
prop->getConcreteType(),
prop->getSubstitutions(),
genericParams, MutableTerm());
if (superclassType->hasError())
return;

reqs.emplace_back(RequirementKind::Superclass,
subjectType, superclassType);
return;
}

case Symbol::Kind::ConcreteType: {
// Requirements containing unresolved name symbols originate from
// invalid code and should not appear in the generic signature.
for (auto term : prop->getSubstitutions()) {
if (term.containsUnresolvedSymbols())
return;
}

// Requirements containing error types originate from invalid code
// and should not appear in the generic signature.
if (prop->getConcreteType()->hasError())
return;

auto concreteType = Map.getTypeFromSubstitutionSchema(
prop->getConcreteType(),
prop->getSubstitutions(),
Expand Down Expand Up @@ -465,12 +485,14 @@ InferredGenericSignatureRequestRQM::evaluate(
requirements);
}

auto *lookupDC = (*gpList->begin())->getDeclContext();

// Add the generic parameter list's 'where' clause to the builder.
//
// The only time generic parameter lists have a 'where' clause is
// in SIL mode; all other generic declarations have a free-standing
// 'where' clause, which will be visited below.
WhereClauseOwner(parentModule, gpList)
WhereClauseOwner(lookupDC, gpList)
.visitRequirements(TypeResolutionStage::Structural,
visitRequirement);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ SourceLoc WhereClauseOwner::getLoc() const {
if (auto attr = source.dyn_cast<SpecializeAttr *>())
return attr->getLocation();

if (auto attr = source.dyn_cast<DifferentiableAttr *>())
return attr->getLocation();

return source.get<GenericParamList *>()->getWhereLoc();
}

Expand All @@ -365,6 +368,8 @@ void swift::simple_display(llvm::raw_ostream &out,
simple_display(out, owner.dc->getAsDecl());
} else if (owner.source.is<SpecializeAttr *>()) {
out << "@_specialize";
} else if (owner.source.is<DifferentiableAttr *>()) {
out << "@_differentiable";
} else {
out << "(SIL generic parameter list)";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,

generic->setInvalid();
}
return type;
return ErrorType::get(ctx);
}

auto *unboundType = type->castTo<UnboundGenericType>();
Expand Down
16 changes: 14 additions & 2 deletions test/Generics/conditional_requirement_inference_2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ protocol Sequence {
extension Array : Sequence {}

struct EquatableSequenceBox<T : Sequence> where T.Element : Equatable {
// CHECK: Generic signature: <T, U where T == Array<Array<U>>, U : Equatable>
// CHECK-LABEL: EquatableSequenceBox.withArrayArray@
// CHECK-NEXT: Generic signature: <T, U where T == Array<Array<U>>, U : Equatable>
func withArrayArray<U>(_: U) where T == Array<Array<U>> {}
}
}

// Make sure requirement desugaring handles conditional conformances.
protocol Hashable : Equatable {}

struct Set<Element : Hashable> {}

extension Array : Hashable where Element : Hashable {}

// CHECK-LABEL: doStuff@
// CHECK-NEXT: Generic signature: <U where U : Hashable>
func doStuff<U>(_: Set<Array<U>>) {}
48 changes: 48 additions & 0 deletions test/Generics/rdar28544316.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// RUN: %target-swift-frontend %s -emit-ir -debug-generic-signatures -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
// REQUIRES: asserts

class PropertyDataSource<O: PropertyHosting> {
}

protocol TableViewCellFactoryType {
associatedtype Item
}

public protocol PropertyHosting {
associatedtype PType: Hashable, EntityOwned
}

public protocol EntityOwned: class {
associatedtype Owner
}

public protocol PropertyType: class {
}

func useType<T>(cellType: T.Type) {
}

// The GSB would reject this declaration because it was "too minimal",
// missing the Factory.Item : EntityOwned requirement that is
// required to get a conformance-valid rewrite system.
//
// The Requirement Machine correctly infers this requirement and adds
// it during minimization.

// CHECK-LABEL: rdar28544316.(file).PropertyTableViewAdapter@
// CHECK-NEXT: Generic signature: <Factory where Factory : TableViewCellFactoryType, Factory.[TableViewCellFactoryType]Item : EntityOwned, Factory.[TableViewCellFactoryType]Item : PropertyType, Factory.[TableViewCellFactoryType]Item == Factory.[TableViewCellFactoryType]Item.[EntityOwned]Owner.[PropertyHosting]PType, Factory.[TableViewCellFactoryType]Item.[EntityOwned]Owner : PropertyHosting>

final class PropertyTableViewAdapter<Factory: TableViewCellFactoryType>
where
Factory.Item: PropertyType,
Factory.Item.Owner: PropertyHosting,
Factory.Item.Owner.PType == Factory.Item
{
typealias Item = Factory.Item

let dataManager: PropertyDataSource<Factory.Item.Owner>
init(dataManager: PropertyDataSource<Factory.Item.Owner>) {
useType(cellType: Factory.Item.Owner.self)
self.dataManager = dataManager
}
}
38 changes: 0 additions & 38 deletions validation-test/compiler_crashers_2_fixed/0074-rdar28544316.swift

This file was deleted.