Skip to content

[6.0] Fix name lookup in nested protocol inheritance clauses #73286

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 1 commit into from
Apr 29, 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
81 changes: 54 additions & 27 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,8 @@ static DirectlyReferencedTypeDecls
directReferencesForTypeRepr(Evaluator &evaluator, ASTContext &ctx,
TypeRepr *typeRepr, DeclContext *dc,
bool allowUsableFromInline,
bool rhsOfSelfRequirement);
bool rhsOfSelfRequirement,
bool allowProtocolMembers);

/// Retrieve the set of type declarations that are directly referenced from
/// the given type.
Expand Down Expand Up @@ -1148,7 +1149,8 @@ SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
rhsDecls = directReferencesForTypeRepr(evaluator, ctx, typeRepr,
const_cast<DeclContext *>(dc),
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/true);
/*rhsOfSelfRequirement=*/true,
/*allowProtocolMembers=*/true);
}

SmallVector<ModuleDecl *, 2> modulesFound;
Expand Down Expand Up @@ -1212,7 +1214,8 @@ TypeDeclsFromWhereClauseRequest::evaluate(Evaluator &evaluator,
auto resolve = [&](TypeRepr *typeRepr) {
auto decls = directReferencesForTypeRepr(evaluator, ctx, typeRepr, ext,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);
result.first.insert(result.first.end(),
decls.first.begin(),
decls.first.end());
Expand Down Expand Up @@ -2843,10 +2846,11 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
SourceLoc loc, DeclContext *dc,
LookupOuterResults lookupOuter,
bool allowUsableFromInline,
bool rhsOfSelfRequirement) {
UnqualifiedLookupOptions options =
UnqualifiedLookupFlags::TypeLookup |
UnqualifiedLookupFlags::AllowProtocolMembers;
bool rhsOfSelfRequirement,
bool allowProtocolMembers) {
UnqualifiedLookupOptions options = UnqualifiedLookupFlags::TypeLookup;
if (allowProtocolMembers)
options |= UnqualifiedLookupFlags::AllowProtocolMembers;
if (lookupOuter == LookupOuterResults::Included)
options |= UnqualifiedLookupFlags::IncludeOuterResults;

Expand Down Expand Up @@ -2960,11 +2964,12 @@ static DirectlyReferencedTypeDecls
directReferencesForDeclRefTypeRepr(Evaluator &evaluator, ASTContext &ctx,
DeclRefTypeRepr *repr, DeclContext *dc,
bool allowUsableFromInline,
bool rhsOfSelfRequirement) {
bool rhsOfSelfRequirement,
bool allowProtocolMembers) {
if (auto *qualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(repr)) {
auto result = directReferencesForTypeRepr(
evaluator, ctx, qualIdentTR->getBase(), dc,
allowUsableFromInline, rhsOfSelfRequirement);
allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers);

// For a qualified identifier, perform qualified name lookup.
result.first = directReferencesForQualifiedTypeLookup(
Expand All @@ -2977,14 +2982,15 @@ directReferencesForDeclRefTypeRepr(Evaluator &evaluator, ASTContext &ctx,
// For an unqualified identifier, perform unqualified name lookup.
return directReferencesForUnqualifiedTypeLookup(
repr->getNameRef(), repr->getLoc(), dc, LookupOuterResults::Excluded,
allowUsableFromInline, rhsOfSelfRequirement);
allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers);
}

static DirectlyReferencedTypeDecls
directReferencesForTypeRepr(Evaluator &evaluator,
ASTContext &ctx, TypeRepr *typeRepr,
DeclContext *dc, bool allowUsableFromInline,
bool rhsOfSelfRequirement) {
bool rhsOfSelfRequirement,
bool allowProtocolMembers) {
DirectlyReferencedTypeDecls result;

switch (typeRepr->getKind()) {
Expand All @@ -2997,7 +3003,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
return directReferencesForTypeRepr(evaluator, ctx,
attributed->getTypeRepr(), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
}

case TypeReprKind::Composition: {
Expand All @@ -3006,7 +3013,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
auto componentResult =
directReferencesForTypeRepr(evaluator, ctx, component, dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
result.first.insert(result.first.end(),
componentResult.first.begin(),
componentResult.first.end());
Expand All @@ -3022,7 +3030,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
return directReferencesForDeclRefTypeRepr(evaluator, ctx,
cast<DeclRefTypeRepr>(typeRepr),
dc, allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);

case TypeReprKind::Dictionary:
result.first.push_back(ctx.getDictionaryDecl());
Expand All @@ -3034,7 +3043,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
result = directReferencesForTypeRepr(evaluator, ctx,
tupleRepr->getElementType(0), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
} else {
result.first.push_back(ctx.getBuiltinTupleDecl());
}
Expand All @@ -3046,23 +3056,26 @@ directReferencesForTypeRepr(Evaluator &evaluator,
return directReferencesForTypeRepr(evaluator, ctx,
packExpansionRepr->getElementType(), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
}

case TypeReprKind::PackExpansion: {
auto packExpansionRepr = cast<PackExpansionTypeRepr>(typeRepr);
return directReferencesForTypeRepr(evaluator, ctx,
packExpansionRepr->getPatternType(), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
}

case TypeReprKind::PackElement: {
auto packReferenceRepr = cast<PackElementTypeRepr>(typeRepr);
return directReferencesForTypeRepr(evaluator, ctx,
packReferenceRepr->getPackType(), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
}

case TypeReprKind::Inverse: {
Expand All @@ -3072,7 +3085,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
auto innerResult = directReferencesForTypeRepr(evaluator, ctx,
inverseRepr->getConstraint(), dc,
allowUsableFromInline,
rhsOfSelfRequirement);
rhsOfSelfRequirement,
allowProtocolMembers);
if (innerResult.first.size() == 1) {
if (auto *proto = dyn_cast<ProtocolDecl>(innerResult.first[0])) {
if (auto ip = proto->getInvertibleProtocolKind()) {
Expand Down Expand Up @@ -3182,10 +3196,16 @@ DirectlyReferencedTypeDecls InheritedDeclsReferencedRequest::evaluate(
else
dc = (DeclContext *)decl.get<const ExtensionDecl *>();

// If looking at a protocol's inheritance list,
// do not look at protocol members to avoid circularity.
// Protocols cannot inherit from any protocol members anyway.
bool allowProtocolMembers = (dc->getSelfProtocolDecl() == nullptr);

return directReferencesForTypeRepr(evaluator, dc->getASTContext(), typeRepr,
const_cast<DeclContext *>(dc),
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
allowProtocolMembers);
}

// Fall back to semantic types.
Expand All @@ -3206,7 +3226,8 @@ DirectlyReferencedTypeDecls UnderlyingTypeDeclsReferencedRequest::evaluate(
return directReferencesForTypeRepr(evaluator, typealias->getASTContext(),
typeRepr, typealias,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);
}

// Fall back to semantic types.
Expand Down Expand Up @@ -3373,7 +3394,8 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
DirectlyReferencedTypeDecls referenced =
directReferencesForTypeRepr(evaluator, ctx, typeRepr, ext->getParent(),
ext->isInSpecializeExtensionContext(),
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);

// Resolve those type declarations to nominal type declarations.
SmallVector<ModuleDecl *, 2> modulesFound;
Expand Down Expand Up @@ -3423,7 +3445,8 @@ bool TypeRepr::isProtocolOrProtocolComposition(DeclContext *dc) {
auto &ctx = dc->getASTContext();
auto references = directReferencesForTypeRepr(ctx.evaluator, ctx, this, dc,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);
return declsAreProtocols(references.first);
}

Expand Down Expand Up @@ -3458,7 +3481,8 @@ createTupleExtensionGenericParams(ASTContext &ctx,
extendedTypeRepr,
ext->getParent(),
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);
assert(referenced.second.empty() && "Implement me");
if (referenced.first.size() != 1 || !isa<TypeAliasDecl>(referenced.first[0]))
return nullptr;
Expand Down Expand Up @@ -3733,7 +3757,8 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
decls = directReferencesForTypeRepr(
evaluator, ctx, typeRepr, dc,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);
} else if (Type type = attr->getType()) {
decls = directReferencesForType(type);
}
Expand Down Expand Up @@ -3764,7 +3789,8 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
decls = directReferencesForUnqualifiedTypeLookup(
name, loc, dc, LookupOuterResults::Included,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers*/true);
nominals = resolveTypeDeclsToNominal(evaluator, ctx, decls.first,
ResolveToNominalOptions(),
modulesFound, anyObject);
Expand Down Expand Up @@ -4002,7 +4028,8 @@ ProtocolDecl *ImplementsAttrProtocolRequest::evaluate(
DirectlyReferencedTypeDecls referenced =
directReferencesForTypeRepr(evaluator, ctx, typeRepr, dc,
/*allowUsableFromInline=*/false,
/*rhsOfSelfRequirement=*/false);
/*rhsOfSelfRequirement=*/false,
/*allowProtocolMembers=*/true);

// Resolve those type declarations to nominal type declarations.
SmallVector<ModuleDecl *, 2> modulesFound;
Expand Down
42 changes: 41 additions & 1 deletion test/decl/nested/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ class OuterClass {
protocol InnerProtocol : OuterClass { }
}

// Name lookup circularity tests.

protocol SomeBaseProtocol {}

struct ConformanceOnDecl: ConformanceOnDecl.P {
protocol P: SomeBaseProtocol {}
}
struct ConformanceOnDecl_2: ConformanceOnDecl_2.P {
protocol P where Self: SomeBaseProtocol {}
}
struct ConformanceOnDecl_3: Self.P {
protocol P: SomeBaseProtocol {}
}
struct ConformanceOnDecl_4: ConformanceOnDecl_4.Q {
protocol P: SomeBaseProtocol {}
protocol Q: P {}
}


struct ConformanceInExtension {
protocol P: SomeBaseProtocol {}
}
extension ConformanceInExtension: ConformanceInExtension.P {}

struct ConformanceInExtension_2 {
protocol P where Self: SomeBaseProtocol {}
}
extension ConformanceInExtension_2: ConformanceInExtension_2.P {}

struct ConformanceInExtension_3 {
protocol P: SomeBaseProtocol {}
}
extension ConformanceInExtension_3: Self.P {}

struct ConformanceInExtension_4 {
protocol P: SomeBaseProtocol {}
protocol Q: P {}
}
extension ConformanceInExtension_4: ConformanceInExtension_4.Q {}

// Protocols can be nested in actors.

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
Expand Down Expand Up @@ -315,4 +355,4 @@ struct Outer {
typealias B = Int
}
}
}
}