Skip to content

Commit c884632

Browse files
committed
AST: Consolidate inherited type resolution.
Move evaulation of `InheritedTypeRequest` into the `InheritedTypes` wrapper. NFC.
1 parent 0dd8f4c commit c884632

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/AST/RequirementSignature.h"
3636
#include "swift/AST/StorageImpl.h"
3737
#include "swift/AST/TypeAlignments.h"
38+
#include "swift/AST/TypeResolutionStage.h"
3839
#include "swift/AST/TypeWalker.h"
3940
#include "swift/AST/Types.h"
4041
#include "swift/Basic/ArrayRefView.h"
@@ -1572,7 +1573,11 @@ class InheritedTypes {
15721573
/// given index.
15731574
TypeRepr *getTypeRepr(unsigned i) const { return Entries[i].getTypeRepr(); }
15741575

1575-
/// Returns the array of inherited type entries.
1576+
/// Returns the `Type` for the entry of the inheritance clause at the given
1577+
/// index, resolved at the given stage, or `Type()` if resolution fails.
1578+
Type getResolvedType(unsigned i, TypeResolutionStage stage =
1579+
TypeResolutionStage::Interface) const;
1580+
15761581
/// Returns the underlying array of inherited type entries.
15771582
///
15781583
/// NOTE: The `Type` associated with an entry may not be resolved yet.

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,15 @@ InheritedTypes::InheritedTypes(const ExtensionDecl *extensionDecl)
15591559
Entries = extensionDecl->Inherited;
15601560
}
15611561

1562+
Type InheritedTypes::getResolvedType(unsigned i,
1563+
TypeResolutionStage stage) const {
1564+
ASTContext &ctx = Decl.is<const ExtensionDecl *>()
1565+
? Decl.get<const ExtensionDecl *>()->getASTContext()
1566+
: Decl.get<const TypeDecl *>()->getASTContext();
1567+
return evaluateOrDefault(ctx.evaluator, InheritedTypeRequest{Decl, i, stage},
1568+
Type());
1569+
}
1570+
15621571
ExtensionDecl::ExtensionDecl(SourceLoc extensionLoc,
15631572
TypeRepr *extendedType,
15641573
ArrayRef<InheritedEntry> inherited,

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,14 @@ void swift::rewriting::realizeInheritedRequirements(
729729
TypeDecl *decl, Type type, bool shouldInferRequirements,
730730
SmallVectorImpl<StructuralRequirement> &result,
731731
SmallVectorImpl<RequirementError> &errors) {
732-
auto &ctx = decl->getASTContext();
733732
auto inheritedTypes = decl->getInherited();
734733
auto *dc = decl->getInnermostDeclContext();
735734
auto *moduleForInference = dc->getParentModule();
736735

737-
for (unsigned index : inheritedTypes.getIndices()) {
738-
Type inheritedType
739-
= evaluateOrDefault(ctx.evaluator,
740-
InheritedTypeRequest{decl, index,
741-
TypeResolutionStage::Structural},
742-
Type());
736+
for (auto index : inheritedTypes.getIndices()) {
737+
Type inheritedType =
738+
inheritedTypes.getResolvedType(index, TypeResolutionStage::Structural);
739+
743740
if (!inheritedType) continue;
744741

745742
// Ignore trivially circular protocol refinement (protocol P : P)

lib/Sema/TypeCheckAccess.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,9 @@ class AccessControlChecker : public AccessControlCheckerBase,
918918
DescriptiveDeclKind declKind = DescriptiveDeclKind::Protocol;
919919

920920
// FIXME: Hack to ensure that we've computed the types involved here.
921-
ASTContext &ctx = proto->getASTContext();
922-
for (unsigned i : proto->getInherited().getIndices()) {
923-
(void)evaluateOrDefault(ctx.evaluator,
924-
InheritedTypeRequest{
925-
proto, i, TypeResolutionStage::Interface},
926-
Type());
921+
auto inheritedTypes = proto->getInherited();
922+
for (auto i : inheritedTypes.getIndices()) {
923+
(void)inheritedTypes.getResolvedType(i);
927924
}
928925

929926
auto declKindForType = [](Type type) -> DescriptiveDeclKind {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5592,13 +5592,9 @@ void swift::diagnoseConformanceFailure(Type T,
55925592
if (!classDecl)
55935593
return;
55945594

5595-
auto inheritedClause = classDecl->getInherited().getEntries();
5596-
for (unsigned i : indices(inheritedClause)) {
5597-
auto &inherited = inheritedClause[i];
5598-
5599-
// Find the inherited type.
5600-
InheritedTypeRequest request{classDecl, i, TypeResolutionStage::Interface};
5601-
Type inheritedTy = evaluateOrDefault(ctx.evaluator, request, Type());
5595+
auto inheritedTypes = classDecl->getInherited();
5596+
for (auto i : inheritedTypes.getIndices()) {
5597+
Type inheritedTy = inheritedTypes.getResolvedType(i);
56025598

56035599
// If it's a class, we cannot suggest a different class to inherit
56045600
// from.
@@ -5608,7 +5604,8 @@ void swift::diagnoseConformanceFailure(Type T,
56085604
// Is it the NSObject protocol?
56095605
if (auto protoTy = inheritedTy->getAs<ProtocolType>()) {
56105606
if (isNSObjectProtocol(protoTy->getDecl())) {
5611-
diag.fixItReplace(inherited.getSourceRange(), "NSObject");
5607+
diag.fixItReplace(inheritedTypes.getEntry(i).getSourceRange(),
5608+
"NSObject");
56125609
return;
56135610
}
56145611
}

0 commit comments

Comments
 (0)