Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,17 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// with a class type.
bool mayHaveSuperclass();

/// \brief Determine whether this type may have nested types, which holds for
/// nominal types, existentials and archetypes.
bool mayHaveMemberTypes() {
/// \brief Determine whether this type can be used as a base type for AST
/// name lookup, which is the case for nominal types, protocol compositions
/// and archetypes.
///
/// Generally, the static vs instanec and mutating vs nonmutating distinction
/// is handled elsewhere, so metatypes, lvalue types and inout types are not
/// allowed here.
///
/// Similarly, tuples formally have members, but this does not go through
/// name lookup.
bool mayHaveMembers() {
return (is<ArchetypeType>() ||
is<ModuleType>() ||
isExistentialType() ||
Expand Down
13 changes: 8 additions & 5 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
// the extension to the interface types of the base type's
// declaration.
TypeSubstitutionMap subMap;
if (!BaseType->isAnyExistentialType())
if (!BaseType->isExistentialType())
subMap = BaseType->getContextSubstitutions(Ext);
auto *M = DC->getParentModule();

Expand Down Expand Up @@ -453,7 +453,9 @@ void PrintOptions::clearSynthesizedExtension() {
}

TypeTransformContext::TypeTransformContext(Type T)
: BaseType(T.getPointer()) {}
: BaseType(T.getPointer()) {
assert(T->mayHaveMembers());
}

TypeTransformContext::TypeTransformContext(NominalTypeDecl *NTD)
: BaseType(NTD->getDeclaredTypeInContext().getPointer()), Nominal(NTD) {}
Expand Down Expand Up @@ -984,8 +986,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
Type OldType = CurrentType;
if (CurrentType && (Old != nullptr || Options.PrintAsMember)) {
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
CurrentType = CurrentType->getTypeOfMember(
Options.CurrentModule, NTD, nullptr);
CurrentType = NTD->getDeclaredInterfaceType().subst(
Options.CurrentModule,
CurrentType->getContextSubstitutions(NTD->getDeclContext()));
}
}

Expand Down Expand Up @@ -1259,7 +1262,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
ModuleDecl *M = nullptr;

if (CurrentType) {
if (!CurrentType->isAnyExistentialType()) {
if (!CurrentType->isExistentialType()) {
auto *DC = Current->getInnermostDeclContext()->getInnermostTypeContext();
subMap = CurrentType->getContextSubstitutions(DC);
M = DC->getParentModule();
Expand Down
13 changes: 2 additions & 11 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,17 +1316,6 @@ bool DeclContext::lookupQualified(Type type,
}
};

// Look through lvalue and inout types.
type = type->getLValueOrInOutObjectType();

// Look through metatypes.
if (auto metaTy = type->getAs<AnyMetatypeType>())
type = metaTy->getInstanceType();

// Look through DynamicSelf.
if (auto dynamicSelf = type->getAs<DynamicSelfType>())
type = dynamicSelf->getSelfType();

// Look for module references.
if (auto moduleTy = type->getAs<ModuleType>()) {
Module *module = moduleTy->getModule();
Expand Down Expand Up @@ -1438,6 +1427,8 @@ bool DeclContext::lookupQualified(Type type,
}
}
}
} else {
llvm_unreachable("Bad type for qualified lookup");
}

// Allow filtering of the visible declarations based on various
Expand Down
34 changes: 15 additions & 19 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3099,23 +3099,15 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
}

TypeSubstitutionMap TypeBase::getContextSubstitutions(const DeclContext *dc) {
assert(dc->isTypeContext());
Type baseTy(this);

// Ignore lvalues in the base type.
Type baseTy(getRValueType());

// Look through the metatype; it has no bearing on the result.
if (auto metaBase = baseTy->getAs<AnyMetatypeType>()) {
baseTy = metaBase->getInstanceType()->getRValueType();
}
assert(!baseTy->isLValueType() && !baseTy->is<AnyMetatypeType>());

// The resulting set of substitutions. Always use this to ensure we
// don't miss out on NRVO anywhere.
TypeSubstitutionMap substitutions;

// Look through non-type contexts.
while (!dc->isTypeContext())
dc = dc->getParent();

// If the member is part of a protocol or extension thereof, we need
// to substitute in the type of Self.
if (dc->getAsProtocolOrProtocolExtensionContext()) {
Expand All @@ -3127,19 +3119,24 @@ TypeSubstitutionMap TypeBase::getContextSubstitutions(const DeclContext *dc) {
return substitutions;
}

// If we found a member of a concrete type from a protocol extension,
// get the superclass out of the archetype.
if (auto *archetypeTy = baseTy->getAs<ArchetypeType>())
baseTy = archetypeTy->getSuperclass();

// Extract the lazy resolver.
LazyResolver *resolver = dc->getASTContext().getLazyResolver();

// Find the superclass type with the context matching that of the member.
//
// FIXME: Do this in the caller?
if (baseTy->getAnyNominal()) {
auto *ownerNominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
if (auto *ownerClass = dyn_cast<ClassDecl>(ownerNominal))
baseTy = baseTy->getSuperclassForDecl(ownerClass, resolver);
assert(baseTy->getAnyNominal());

assert(ownerNominal == baseTy->getAnyNominal());
}
auto *ownerNominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
if (auto *ownerClass = dyn_cast<ClassDecl>(ownerNominal))
baseTy = baseTy->getSuperclassForDecl(ownerClass, resolver);

assert(ownerNominal == baseTy->getAnyNominal());

// If the base type isn't specialized, there's nothing to substitute.
if (!baseTy->isSpecialized())
Expand Down Expand Up @@ -3177,8 +3174,7 @@ TypeSubstitutionMap TypeBase::getContextSubstitutions(const DeclContext *dc) {
continue;
}

// We're done.
break;
llvm_unreachable("Bad base type");
}

return substitutions;
Expand Down
37 changes: 25 additions & 12 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
Type ContextTy = VD->getDeclContext()->getDeclaredInterfaceType();
if (ContextTy) {
// Look through lvalue types and metatypes
Type MaybeNominalType = (*ExprType)->getRValueType()
->getRValueInstanceType();
Type MaybeNominalType = (*ExprType)->getRValueType();

if (auto Metatype = MaybeNominalType->getAs<MetatypeType>())
MaybeNominalType = Metatype->getInstanceType();

if (auto SelfType = MaybeNominalType->getAs<DynamicSelfType>())
MaybeNominalType = SelfType->getSelfType();

// For optional protocol requirements and dynamic dispatch,
// strip off optionality from the base type, but only if
Expand All @@ -1989,6 +1994,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
if (MaybeNominalType->isAnyObject())
return T;

// FIXME: Sometimes ExprType is the type of the member here,
// and not the type of the base. That is inconsistent and
// should be cleaned up.
if (!MaybeNominalType->mayHaveMembers())
return T;

// For everything else, substitute in the base type.
auto Subs = MaybeNominalType->getMemberSubstitutions(VD);

Expand Down Expand Up @@ -2865,23 +2876,24 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
addNominalTypeRef(NTD, Reason);
addConstructorCallsForType(NTD->getInterfaceType(), NTD->getName(),
Reason);
addConstructorCallsForType(NTD->getDeclaredInterfaceType(),
NTD->getName(), Reason);
return;
}

if (auto *TAD = dyn_cast<TypeAliasDecl>(D)) {
addTypeAliasRef(TAD, Reason);
auto type = TAD->mapTypeIntoContext(TAD->getUnderlyingTypeLoc().getType());
addConstructorCallsForType(type, TAD->getName(), Reason);
if (type->mayHaveMembers())
addConstructorCallsForType(type, TAD->getName(), Reason);
return;
}

if (auto *GP = dyn_cast<GenericTypeParamDecl>(D)) {
addGenericTypeParamRef(GP, Reason);
for (auto *protocol : GP->getConformingProtocols())
addConstructorCallsForType(protocol->getInterfaceType(), GP->getName(),
Reason);
addConstructorCallsForType(protocol->getDeclaredInterfaceType(),
GP->getName(), Reason);
return;
}

Expand Down Expand Up @@ -2934,23 +2946,24 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
addNominalTypeRef(NTD, Reason);
addConstructorCallsForType(NTD->getInterfaceType(), NTD->getName(),
Reason);
addConstructorCallsForType(NTD->getDeclaredInterfaceType(),
NTD->getName(), Reason);
return;
}

if (auto *TAD = dyn_cast<TypeAliasDecl>(D)) {
addTypeAliasRef(TAD, Reason);
auto type = TAD->mapTypeIntoContext(TAD->getDeclaredInterfaceType());
addConstructorCallsForType(type, TAD->getName(), Reason);
if (type->mayHaveMembers())
addConstructorCallsForType(type, TAD->getName(), Reason);
return;
}

if (auto *GP = dyn_cast<GenericTypeParamDecl>(D)) {
addGenericTypeParamRef(GP, Reason);
for (auto *protocol : GP->getConformingProtocols())
addConstructorCallsForType(protocol->getInterfaceType(), GP->getName(),
Reason);
addConstructorCallsForType(protocol->getDeclaredInterfaceType(),
GP->getName(), Reason);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenMaterializeForSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ struct MaterializeForSetEmitter {
/// Given part of the witness's interface type, produce its
/// substitution according to the witness substitutions.
CanType getSubstWitnessInterfaceType(CanType type) {
auto subs = SubstSelfType->getMemberSubstitutions(WitnessStorage);
auto subs = SubstSelfType->getRValueInstanceType()
->getMemberSubstitutions(WitnessStorage);
return type.subst(SGM.SwiftModule, subs)->getCanonicalType();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5852,7 +5852,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
auto toObjType = toType->getLValueOrInOutObjectType();

// Conversion to/from UnresolvedType (looking through @lvalue or inout).
if (fromObjType->is<UnresolvedType>() || toObjType->is<UnresolvedType>())
if (fromObjType->hasUnresolvedType() || toObjType->hasUnresolvedType())
return cs.cacheType(new (tc.Context)
UnresolvedTypeConversionExpr(expr, toType));

Expand Down
Loading