Skip to content

Commit a37ad9f

Browse files
committed
RequirementMachine: Implement GenericSignature::areSameTypeParametersInContext() query
1 parent 5d91b59 commit a37ad9f

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

include/swift/AST/RequirementMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ class RequirementMachine final {
5454
public:
5555
~RequirementMachine();
5656

57+
// Generic signature queries
5758
bool requiresClass(Type depType) const;
5859
LayoutConstraint getLayoutConstraint(Type depType) const;
5960
bool requiresProtocol(Type depType, const ProtocolDecl *proto) const;
6061
bool isConcreteType(Type depType) const;
62+
bool areSameTypeParameterInContext(Type depType1, Type depType2) const;
6163

6264
void dump(llvm::raw_ostream &out) const;
6365
};

lib/AST/GenericSignature.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,40 @@ bool GenericSignatureImpl::areSameTypeParameterInContext(Type type1,
543543
if (type1.getPointer() == type2.getPointer())
544544
return true;
545545

546-
return areSameTypeParameterInContext(type1, type2,
547-
*getGenericSignatureBuilder());
546+
auto computeViaGSB = [&]() {
547+
return areSameTypeParameterInContext(type1, type2,
548+
*getGenericSignatureBuilder());
549+
};
550+
551+
auto computeViaRQM = [&]() {
552+
auto *machine = getRequirementMachine();
553+
return machine->areSameTypeParameterInContext(type1, type2);
554+
};
555+
556+
auto &ctx = getASTContext();
557+
if (ctx.LangOpts.EnableRequirementMachine) {
558+
auto rqmResult = computeViaRQM();
559+
560+
#ifndef NDEBUG
561+
auto gsbResult = computeViaGSB();
562+
563+
if (gsbResult != rqmResult) {
564+
llvm::errs() << "RequirementMachine::areSameTypeParameterInContext() is broken\n";
565+
llvm::errs() << "Generic signature: " << GenericSignature(this) << "\n";
566+
llvm::errs() << "First dependent type: "; type1.dump(llvm::errs());
567+
llvm::errs() << "Second dependent type: "; type2.dump(llvm::errs());
568+
llvm::errs() << "\n";
569+
llvm::errs() << "GenericSignatureBuilder says: " << gsbResult << "\n";
570+
llvm::errs() << "RequirementMachine says: " << rqmResult << "\n";
571+
getRequirementMachine()->dump(llvm::errs());
572+
abort();
573+
}
574+
#endif
575+
576+
return rqmResult;
577+
} else {
578+
return computeViaGSB();
579+
}
548580
}
549581

550582
bool GenericSignatureImpl::areSameTypeParameterInContext(Type type1,

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,17 @@ bool RequirementMachine::isConcreteType(Type depType) const {
400400
return false;
401401

402402
return equivClass->isConcreteType();
403+
}
404+
405+
bool RequirementMachine::areSameTypeParameterInContext(Type depType1,
406+
Type depType2) const {
407+
auto term1 = Impl->Context.getMutableTermForType(depType1->getCanonicalType(),
408+
/*proto=*/nullptr);
409+
Impl->System.simplify(term1);
410+
411+
auto term2 = Impl->Context.getMutableTermForType(depType2->getCanonicalType(),
412+
/*proto=*/nullptr);
413+
Impl->System.simplify(term2);
414+
415+
return (term1 == term2);
403416
}

0 commit comments

Comments
 (0)