Skip to content

Assortment of small fixes and cleanups #78309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2024
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
4 changes: 4 additions & 0 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
/// context.
bool isInSpecializeExtensionContext() const;

/// Returns whether this declaration context is a protocol in an unsupported
/// context.
bool isUnsupportedNestedProtocol() const;

/// Get the most optimal resilience expansion for code in this context.
/// If the body is able to be inlined into functions in other resilience
/// domains, this ensures that only sufficiently-conservative access patterns
Expand Down
8 changes: 4 additions & 4 deletions include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ ABSTRACT_TYPE(ReferenceStorage, Type)
TYPE_RANGE(ReferenceStorage, First##Storage, Last##Storage)
#include "swift/AST/ReferenceStorage.def"
ABSTRACT_TYPE(AnyGeneric, Type)
ABSTRACT_TYPE(NominalOrBoundGenericNominal, Type)
ABSTRACT_TYPE(Nominal, Type)
ABSTRACT_TYPE(NominalOrBoundGenericNominal, AnyGenericType)
ABSTRACT_TYPE(Nominal, NominalOrBoundGenericNominalType)
TYPE(Enum, NominalType)
TYPE(Struct, NominalType)
TYPE(Class, NominalType)
TYPE(Protocol, NominalType)
TYPE(BuiltinTuple, NominalType)
TYPE_RANGE(Nominal, Enum, BuiltinTuple)
ABSTRACT_TYPE(BoundGeneric, Type)
ABSTRACT_TYPE(BoundGeneric, NominalOrBoundGenericNominalType)
TYPE(BoundGenericClass, BoundGenericType)
TYPE(BoundGenericEnum, BoundGenericType)
TYPE(BoundGenericStruct, BoundGenericType)
TYPE_RANGE(BoundGeneric, BoundGenericClass, BoundGenericStruct)
TYPE_RANGE(NominalOrBoundGenericNominal, Enum, BoundGenericStruct)
UNCHECKED_TYPE(UnboundGeneric, Type)
UNCHECKED_TYPE(UnboundGeneric, AnyGenericType)
TYPE_RANGE(AnyGeneric, Enum, UnboundGeneric)
ABSTRACT_TYPE(AnyMetatype, Type)
TYPE(Metatype, AnyMetatypeType)
Expand Down
42 changes: 7 additions & 35 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,12 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// type variables referenced by this type.
void getTypeVariables(SmallPtrSetImpl<TypeVariableType *> &typeVariables);

private:
/// If the receiver is a `DependentMemberType`, returns its root. Otherwise,
/// returns the receiver.
Type getDependentMemberRoot();

public:
/// Determine whether this type is a type parameter, which is either a
/// GenericTypeParamType or a DependentMemberType.
///
Expand Down Expand Up @@ -6970,8 +6976,6 @@ class OpenedArchetypeType final : public LocalArchetypeType,
friend ArchetypeType;
friend GenericEnvironment;

UUID ID;

/// Create a new opened archetype in the given environment representing
/// the interface type.
///
Expand All @@ -6987,10 +6991,7 @@ class OpenedArchetypeType final : public LocalArchetypeType,
/// of an existential value.
///
/// \param existential The existential type to open.
/// \param knownID When non-empty, the known ID of the archetype. When empty,
/// a fresh archetype with a unique ID will be opened.
static CanTypeWrapper<OpenedArchetypeType>
get(CanType existential, std::optional<UUID> knownID = std::nullopt);
static CanTypeWrapper<OpenedArchetypeType> get(CanType existential);

/// Create a new archetype that represents the opened type
/// of an existential value.
Expand Down Expand Up @@ -7065,8 +7066,6 @@ class ElementArchetypeType final : public LocalArchetypeType,
friend ArchetypeType;
friend GenericEnvironment;

UUID ID;

/// Create a new element archetype in the given environment representing
/// the interface type.
///
Expand Down Expand Up @@ -7862,38 +7861,11 @@ inline ASTContext &TypeBase::getASTContext() const {
return *const_cast<ASTContext*>(getCanonicalType()->Context);
}

inline bool TypeBase::isTypeVariableOrMember() {
Type t(this);

while (auto *memberTy = t->getAs<DependentMemberType>())
t = memberTy->getBase();

return t->is<TypeVariableType>();
}

inline bool TypeBase::isTypeParameter() {
Type t(this);

while (auto *memberTy = t->getAs<DependentMemberType>())
t = memberTy->getBase();

return t->is<GenericTypeParamType>();
}

// TODO: This will become redundant once InOutType is removed.
inline bool TypeBase::isMaterializable() {
return !(hasLValueType() || is<InOutType>());
}

inline GenericTypeParamType *TypeBase::getRootGenericParam() {
Type t(this);

while (auto *memberTy = t->getAs<DependentMemberType>())
t = memberTy->getBase();

return t->castTo<GenericTypeParamType>();
}

inline bool TypeBase::isConstraintType() const {
return getCanonicalType().isConstraintType();
}
Expand Down
12 changes: 3 additions & 9 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5561,19 +5561,13 @@ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew(
properties));
}

CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
std::optional<UUID> knownID) {
CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential) {
auto &ctx = existential->getASTContext();
auto existentialSig = ctx.getOpenedExistentialSignature(existential);

if (!knownID)
knownID = UUID::fromTime();

auto *genericEnv = GenericEnvironment::forOpenedExistential(
existentialSig.OpenedSig,
existentialSig.Shape,
existentialSig.Generalization,
*knownID);
existentialSig.OpenedSig, existentialSig.Shape,
existentialSig.Generalization, UUID::fromTime());

return cast<OpenedArchetypeType>(
genericEnv->mapTypeIntoContext(existentialSig.SelfType)
Expand Down
4 changes: 1 addition & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5162,10 +5162,8 @@ static Type computeNominalType(NominalTypeDecl *decl, DeclTypeKind kind) {
// If `decl` is a nested type, find the parent type.
Type ParentTy;
DeclContext *dc = decl->getDeclContext();
bool isUnsupportedNestedProtocol =
isa<ProtocolDecl>(decl) && decl->getParent()->isGenericContext();

if (!isUnsupportedNestedProtocol && dc->isTypeContext()) {
if (!decl->isUnsupportedNestedProtocol() && dc->isTypeContext()) {
switch (kind) {
case DeclTypeKind::DeclaredType: {
if (auto *nominal = dc->getSelfNominalTypeDecl())
Expand Down
6 changes: 5 additions & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ DeclContext *DeclContext::getParentForLookup() const {
// outer types.
return getModuleScopeContext();
}
if (isa<ProtocolDecl>(this) && getParent()->isGenericContext()) {
if (isUnsupportedNestedProtocol()) {
// Protocols in generic contexts must not look in to their parents,
// as the parents may contain types with inferred implicit
// generic parameters not present in the protocol's generic signature.
Expand Down Expand Up @@ -1532,6 +1532,10 @@ bool DeclContext::isInSpecializeExtensionContext() const {
return isSpecializeExtensionContext(this);
}

bool DeclContext::isUnsupportedNestedProtocol() const {
return isa<ProtocolDecl>(this) && getParent()->isGenericContext();
}

bool DeclContext::isAlwaysAvailableConformanceContext() const {
auto *ext = dyn_cast<ExtensionDecl>(this);
if (ext == nullptr)
Expand Down
7 changes: 1 addition & 6 deletions lib/AST/ParameterPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ void TypeBase::getTypeParameterPacks(
}

bool TypeBase::isParameterPack() {
Type t(this);

while (auto *memberTy = t->getAs<DependentMemberType>())
t = memberTy->getBase();

return t->isRootParameterPack();
return getDependentMemberRoot()->isRootParameterPack();
}

bool TypeBase::isRootParameterPack() {
Expand Down
21 changes: 21 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,27 @@ void TypeBase::getTypeVariables(
"Did not find type variables!");
}

Type TypeBase::getDependentMemberRoot() {
Type t(this);

while (auto *dmt = t->getAs<DependentMemberType>())
t = dmt->getBase();

return t;
}

bool TypeBase::isTypeVariableOrMember() {
return getDependentMemberRoot()->is<TypeVariableType>();
}

bool TypeBase::isTypeParameter() {
return getDependentMemberRoot()->is<GenericTypeParamType>();
}

GenericTypeParamType *TypeBase::getRootGenericParam() {
return getDependentMemberRoot()->castTo<GenericTypeParamType>();
}

static bool isLegalSILType(CanType type);

static bool isLegalSILTypeOrPackExpansion(CanType type) {
Expand Down
13 changes: 4 additions & 9 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2758,15 +2758,10 @@ ProtocolDecl *irgen::opaqueTypeRequiresWitnessTable(

// The type itself must be anchored on one of the generic parameters of
// the opaque type (not an outer context).
Type subject = req.getFirstType();
while (auto depMember = subject->getAs<DependentMemberType>()) {
subject = depMember->getBase();
}

if (auto genericParam = subject->getAs<GenericTypeParamType>()) {
unsigned opaqueDepth = opaque->getOpaqueGenericParams().front()->getDepth();
if (genericParam->getDepth() == opaqueDepth)
return proto;
auto *genericParam = req.getFirstType()->getRootGenericParam();
unsigned opaqueDepth = opaque->getOpaqueGenericParams().front()->getDepth();
if (genericParam->getDepth() == opaqueDepth) {
return proto;
}

return nullptr;
Expand Down
8 changes: 3 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,8 +1441,8 @@ class AllowLabelMismatches : public MatchCallArgumentListener {
}
};

static std::optional<std::tuple<GenericTypeParamType *, TypeVariableType *,
Type, OpenedExistentialAdjustments>>
static std::optional<
std::tuple<TypeVariableType *, Type, OpenedExistentialAdjustments>>
shouldOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
Type paramTy, Type argTy, Expr *argExpr,
ConstraintSystem &cs) {
Expand Down Expand Up @@ -1819,12 +1819,10 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
if (auto existentialArg = shouldOpenExistentialCallArgument(
callee, paramIdx, paramTy, argTy, argExpr, cs)) {
// My kingdom for a decent "if let" in C++.
GenericTypeParamType *openedGenericParam;
TypeVariableType *openedTypeVar;
Type existentialType;
OpenedExistentialAdjustments adjustments;
std::tie(openedGenericParam, openedTypeVar, existentialType,
adjustments) = *existentialArg;
std::tie(openedTypeVar, existentialType, adjustments) = *existentialArg;

OpenedArchetypeType *opened;
std::tie(argTy, opened) = cs.openExistentialType(
Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/OpenedExistentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,10 @@ swift::isMemberAvailableOnExistential(Type baseTy, const ValueDecl *member) {
return result;
}

std::optional<std::tuple<GenericTypeParamType *, TypeVariableType *,
Type, OpenedExistentialAdjustments>>
std::optional<
std::tuple<TypeVariableType *, Type, OpenedExistentialAdjustments>>
swift::canOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
Type paramTy, Type argTy) {
Type paramTy, Type argTy) {
if (!callee)
return std::nullopt;

Expand Down Expand Up @@ -728,7 +728,7 @@ swift::canOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
if (referenceInfo.hasNonCovariantRef())
return std::nullopt;

return std::make_tuple(genericParam, paramTypeVar, argTy, adjustments);
return std::make_tuple(paramTypeVar, argTy, adjustments);
}

/// For each occurrence of a type **type** in `refTy` that satisfies
Expand Down
12 changes: 6 additions & 6 deletions lib/Sema/OpenedExistentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ using OpenedExistentialAdjustments =
/// \param argTy The type of the argument.
///
/// \returns If the argument type is existential and opening it can bind a
/// generic parameter in the callee, returns the generic parameter, type
/// variable (from the opened parameter type) the existential type that needs
/// to be opened (from the argument type), and the adjustments that need to be
/// applied to the existential type after it is opened.
std::optional<std::tuple<GenericTypeParamType *, TypeVariableType *,
Type, OpenedExistentialAdjustments>>
/// generic parameter in the callee, returns the type variable (from the opened
/// parameter type) the existential type that needs to be opened (from the
/// argument type), and the adjustments that need to be applied to the
/// existential type after it is opened.
std::optional<
std::tuple<TypeVariableType *, Type, OpenedExistentialAdjustments>>
canOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
Type paramTy, Type argTy);

Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,

assert(foundDC);

// Protocols cannot be nested in generic contexts. Use the declared interface
// type, which won't have a parent.
if (auto *proto = dyn_cast<ProtocolDecl>(typeDecl)) {
if (proto->isUnsupportedNestedProtocol()) {
return typeDecl->getDeclaredInterfaceType();
}
}

// selfType is the self type of the context, unless the
// context is a protocol type, in which case we might have
// to use the existential type or superclass bound as a
Expand Down