Skip to content

Commit b0a4efb

Browse files
committed
RequirementMachine: Fix lookupConcreteNestedType() to cope with opaque archetypes
1 parent 6c0ccfc commit b0a4efb

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,10 @@ Optional<Type> ConcreteContraction::substTypeParameter(
235235
->substBaseType(module, *substBaseType);
236236
}
237237

238-
auto *decl = (*substBaseType)->getAnyNominal();
239-
if (decl == nullptr) {
240-
if (Debug) {
241-
llvm::dbgs() << "@@@ Not a nominal type: " << *substBaseType << "\n";
242-
}
243-
244-
return None;
245-
}
246-
247238
// An unresolved DependentMemberType stores an identifier. Handle this
248239
// by performing a name lookup into the base type.
249240
SmallVector<TypeDecl *> concreteDecls;
250-
lookupConcreteNestedType(decl, memberType->getName(), concreteDecls);
241+
lookupConcreteNestedType(*substBaseType, memberType->getName(), concreteDecls);
251242

252243
auto *typeDecl = findBestConcreteNestedType(concreteDecls);
253244
if (typeDecl == nullptr) {
@@ -261,8 +252,9 @@ Optional<Type> ConcreteContraction::substTypeParameter(
261252
}
262253

263254
// Substitute the base type into the member type.
255+
auto *dc = typeDecl->getDeclContext();
264256
auto subMap = (*substBaseType)->getContextSubstitutionMap(
265-
decl->getParentModule(), typeDecl->getDeclContext());
257+
dc->getParentModule(), dc);
266258
return typeDecl->getDeclaredInterfaceType().subst(subMap);
267259
}
268260

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/Decl.h"
20+
#include "swift/AST/GenericEnvironment.h"
2021
#include "swift/AST/GenericSignature.h"
2122
#include "swift/AST/Module.h"
2223
#include <vector>
@@ -659,8 +660,7 @@ RequirementMachine::lookupNestedType(Type depType, Identifier name) const {
659660
typeToSearch = props->getSuperclassBound();
660661

661662
if (typeToSearch)
662-
if (auto *decl = typeToSearch->getAnyNominal())
663-
lookupConcreteNestedType(decl, name, concreteDecls);
663+
lookupConcreteNestedType(typeToSearch, name, concreteDecls);
664664
}
665665

666666
if (bestAssocType) {

lib/AST/RequirementMachine/NameLookup.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,33 @@
1212

1313
#include "NameLookup.h"
1414
#include "swift/AST/Decl.h"
15+
#include "swift/AST/GenericEnvironment.h"
1516
#include "swift/AST/Module.h"
17+
#include "swift/AST/Types.h"
1618
#include "llvm/ADT/SmallVector.h"
1719
#include <algorithm>
1820

1921
using namespace swift;
2022
using namespace rewriting;
2123

24+
void
25+
swift::rewriting::lookupConcreteNestedType(
26+
Type baseType,
27+
Identifier name,
28+
SmallVectorImpl<TypeDecl *> &concreteDecls) {
29+
if (auto *decl = baseType->getAnyNominal())
30+
lookupConcreteNestedType(decl, name, concreteDecls);
31+
else if (auto *archetype = baseType->getAs<OpaqueTypeArchetypeType>()) {
32+
// If our concrete type is an opaque result archetype, look into its
33+
// generic environment recursively.
34+
auto *genericEnv = archetype->getGenericEnvironment();
35+
auto genericSig = genericEnv->getGenericSignature();
36+
37+
concreteDecls.push_back(
38+
genericSig->lookupNestedType(archetype->getInterfaceType(), name));
39+
}
40+
}
41+
2242
void
2343
swift::rewriting::lookupConcreteNestedType(
2444
NominalTypeDecl *decl,

lib/AST/RequirementMachine/NameLookup.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ namespace swift {
1919

2020
class Identifier;
2121
class NominalTypeDecl;
22+
class Type;
2223
class TypeDecl;
2324

2425
namespace rewriting {
2526

27+
void lookupConcreteNestedType(
28+
Type baseType,
29+
Identifier name,
30+
llvm::SmallVectorImpl<TypeDecl *> &concreteDecls);
31+
2632
void lookupConcreteNestedType(
2733
NominalTypeDecl *decl,
2834
Identifier name,

0 commit comments

Comments
 (0)