Skip to content

Commit 0ffd11c

Browse files
slavapestovDougGregor
authored andcommitted
RequirementMachine: New GenericSignature::isValidTypeInContext() query
1 parent 8df65e7 commit 0ffd11c

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
390390

391391
bool isCanonicalTypeInContext(Type type) const;
392392

393+
bool isValidTypeInContext(Type type) const;
394+
393395
/// Retrieve the conformance access path used to extract the conformance of
394396
/// interface \c type to the given \c protocol.
395397
///

lib/AST/GenericSignature.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,19 @@ CanType GenericSignatureImpl::getCanonicalTypeInContext(Type type) const {
466466
type, { })->getCanonicalType();
467467
}
468468

469+
bool GenericSignatureImpl::isValidTypeInContext(Type type) const {
470+
auto &ctx = getASTContext();
471+
switch (ctx.LangOpts.EnableRequirementMachine) {
472+
case RequirementMachineMode::Disabled:
473+
llvm::errs() << "Not implemented\n";
474+
abort();
475+
476+
case RequirementMachineMode::Enabled:
477+
case RequirementMachineMode::Verify:
478+
return getRequirementMachine()->isValidTypeInContext(type);
479+
}
480+
}
481+
469482
ArrayRef<CanTypeWrapper<GenericTypeParamType>>
470483
CanGenericSignature::getGenericParams() const {
471484
auto params = getPointer()->getGenericParams().getOriginalArray();

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,19 @@ Type RequirementMachine::getCanonicalTypeInContext(
431431
});
432432
}
433433

434+
/// Determine if the given type parameter is valid with respect to this
435+
/// requirement machine's generic signature.
436+
bool RequirementMachine::isValidTypeInContext(Type type) const {
437+
assert(type->isTypeParameter());
438+
439+
auto term = Context.getMutableTermForType(type->getCanonicalType(),
440+
/*proto=*/nullptr);
441+
System.simplify(term);
442+
443+
auto prefix = getLongestValidPrefix(term);
444+
return (prefix == term);
445+
}
446+
434447
/// Retrieve the conformance access path used to extract the conformance of
435448
/// interface \c type to the given \c protocol.
436449
///

lib/AST/RequirementMachine/RequirementMachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class RequirementMachine final {
129129
bool isCanonicalTypeInContext(Type type) const;
130130
Type getCanonicalTypeInContext(Type type,
131131
TypeArrayView<GenericTypeParamType> genericParams) const;
132+
bool isValidTypeInContext(Type type) const;
132133
ConformanceAccessPath getConformanceAccessPath(Type type,
133134
ProtocolDecl *protocol);
134135
TypeDecl *lookupNestedType(Type depType, Identifier name) const;

0 commit comments

Comments
 (0)