Files
swift-mirror/lib/AST/RequirementMachine/NameLookup.cpp
Slava Pestov 57d1600b99 RequirementMachine: Better tests for concrete type requirements with opaque archetypes
Also fix a weird latent bug. In lookupConcreteNestedType(), we would
push nullptr onto the concreteDecls vector if the opaque archetype
did not have a nested type with this name. However, this did not
turn out to be a problem, since in this code path we would only
have a single element in this vector, and the later call to
std::min_element() did not end up dereferencing the null pointer.

However this is very dodgy, so tweak the code to prevent this from
happening and add a test case (which already passed anyway).
2022-03-22 23:53:40 -04:00

69 lines
2.3 KiB
C++

//===--- NameLookup.cpp - Name lookup utilities ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "NameLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/Module.h"
#include "swift/AST/Types.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
using namespace swift;
using namespace rewriting;
void
swift::rewriting::lookupConcreteNestedType(
Type baseType,
Identifier name,
SmallVectorImpl<TypeDecl *> &concreteDecls) {
if (auto *decl = baseType->getAnyNominal())
lookupConcreteNestedType(decl, name, concreteDecls);
else if (auto *archetype = baseType->getAs<OpaqueTypeArchetypeType>()) {
// If our concrete type is an opaque result archetype, look into its
// generic environment recursively.
auto *genericEnv = archetype->getGenericEnvironment();
auto genericSig = genericEnv->getGenericSignature();
auto *typeDecl =
genericSig->lookupNestedType(archetype->getInterfaceType(), name);
if (typeDecl != nullptr)
concreteDecls.push_back(typeDecl);
}
}
void
swift::rewriting::lookupConcreteNestedType(
NominalTypeDecl *decl,
Identifier name,
SmallVectorImpl<TypeDecl *> &concreteDecls) {
SmallVector<ValueDecl *, 2> foundMembers;
decl->getParentModule()->lookupQualified(
decl, DeclNameRef(name),
NL_QualifiedDefault | NL_OnlyTypes | NL_ProtocolMembers,
foundMembers);
for (auto member : foundMembers)
concreteDecls.push_back(cast<TypeDecl>(member));
}
TypeDecl *
swift::rewriting::findBestConcreteNestedType(
SmallVectorImpl<TypeDecl *> &concreteDecls) {
if (concreteDecls.empty())
return nullptr;
return *std::min_element(concreteDecls.begin(), concreteDecls.end(),
[](TypeDecl *type1, TypeDecl *type2) {
return TypeDecl::compare(type1, type2) < 0;
});
}