Skip to content

Commit b57db86

Browse files
authored
Merge pull request #72364 from slavapestov/fix-rdar124498054
AST: Refine name lookup rule for protocol extension 'where' clauses
2 parents 76c6510 + e46a0c5 commit b57db86

14 files changed

+265
-426
lines changed

include/swift/AST/ASTScope.h

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -482,45 +482,33 @@ class Portion : public ASTAllocated<ASTScopeImpl> {
482482

483483
virtual NullablePtr<ASTScopeImpl>
484484
insertionPointForDeferredExpansion(IterableTypeScope *) const = 0;
485-
};
486-
487-
// For the whole Decl scope of a GenericType or an Extension
488-
class GenericTypeOrExtensionWholePortion final : public Portion {
489-
public:
490-
GenericTypeOrExtensionWholePortion() : Portion("Decl") {}
491-
virtual ~GenericTypeOrExtensionWholePortion() {}
492-
493-
// Just for TypeAlias
494-
ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
495-
ScopeCreator &) const override;
485+
};
496486

497-
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
498-
bool omitAssertions) const override;
487+
// For the whole Decl scope of a GenericType or an Extension
488+
class GenericTypeOrExtensionWholePortion final : public Portion {
489+
public:
490+
GenericTypeOrExtensionWholePortion() : Portion("Decl") {}
491+
virtual ~GenericTypeOrExtensionWholePortion() {}
499492

500-
NullablePtr<const ASTScopeImpl>
501-
getLookupLimitFor(const GenericTypeOrExtensionScope *) const override;
493+
// Just for TypeAlias
494+
ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
495+
ScopeCreator &) const override;
502496

503-
NullablePtr<ASTScopeImpl>
504-
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
505-
};
497+
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
498+
bool omitAssertions) const override;
506499

507-
/// GenericTypeOrExtension = GenericType or Extension
508-
class GenericTypeOrExtensionWhereOrBodyPortion : public Portion {
509-
public:
510-
GenericTypeOrExtensionWhereOrBodyPortion(const char *n) : Portion(n) {}
511-
virtual ~GenericTypeOrExtensionWhereOrBodyPortion() {}
500+
NullablePtr<const ASTScopeImpl>
501+
getLookupLimitFor(const GenericTypeOrExtensionScope *) const override;
512502

513-
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
514-
ASTScopeImpl::DeclConsumer consumer) const override;
503+
NullablePtr<ASTScopeImpl>
504+
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
515505
};
516506

517507
/// Behavior specific to representing the trailing where clause of a
518508
/// GenericTypeDecl or ExtensionDecl scope.
519-
class GenericTypeOrExtensionWherePortion final
520-
: public GenericTypeOrExtensionWhereOrBodyPortion {
509+
class GenericTypeOrExtensionWherePortion final : public Portion {
521510
public:
522-
GenericTypeOrExtensionWherePortion()
523-
: GenericTypeOrExtensionWhereOrBodyPortion("Where") {}
511+
GenericTypeOrExtensionWherePortion() : Portion("Where") {}
524512

525513
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
526514
ASTScopeImpl::DeclConsumer consumer) const override;
@@ -537,11 +525,12 @@ class GenericTypeOrExtensionWherePortion final
537525

538526
/// Behavior specific to representing the Body of a NominalTypeDecl or
539527
/// ExtensionDecl scope
540-
class IterableTypeBodyPortion final
541-
: public GenericTypeOrExtensionWhereOrBodyPortion {
528+
class IterableTypeBodyPortion final : public Portion {
542529
public:
543-
IterableTypeBodyPortion()
544-
: GenericTypeOrExtensionWhereOrBodyPortion("Body") {}
530+
IterableTypeBodyPortion() : Portion("Body") {}
531+
532+
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
533+
ASTScopeImpl::DeclConsumer consumer) const override;
545534

546535
ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
547536
ScopeCreator &) const override;

include/swift/AST/Decl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "swift/AST/IfConfigClause.h"
3333
#include "swift/AST/Import.h"
3434
#include "swift/AST/Initializer.h"
35-
#include "swift/AST/InverseMarking.h"
3635
#include "swift/AST/LayoutConstraint.h"
3736
#include "swift/AST/LifetimeAnnotation.h"
3837
#include "swift/AST/ReferenceCounting.h"
@@ -96,7 +95,6 @@ namespace swift {
9695
class NamedPattern;
9796
class EnumCaseDecl;
9897
class EnumElementDecl;
99-
struct InverseMarking;
10098
class ParameterList;
10199
class ParameterTypeFlags;
102100
class Pattern;

include/swift/AST/InverseMarking.h

Lines changed: 0 additions & 90 deletions
This file was deleted.

include/swift/AST/NameLookup.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ enum class UnqualifiedLookupFlags {
247247
MacroLookup = 1 << 7,
248248
/// This lookup should only return modules
249249
ModuleLookup = 1 << 8,
250+
/// This lookup should discard 'Self' requirements in protocol extension
251+
/// 'where' clauses.
252+
DisregardSelfBounds = 1 << 9
250253
};
251254

252255
using UnqualifiedLookupOptions = OptionSet<UnqualifiedLookupFlags>;

include/swift/AST/TypeRepr.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
137137
/// Is this type representation a protocol?
138138
bool isProtocolOrProtocolComposition(DeclContext *dc);
139139

140-
/// Is this `~<target>` representation.
141-
bool isInverseOf(InvertibleProtocolKind target,
142-
DeclContext *dc);
143-
144140
/// Is this type representation known to be invalid?
145141
bool isInvalid() const { return Bits.TypeRepr.Invalid; }
146142

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "swift/AST/GenericEnvironment.h"
3030
#include "swift/AST/GenericParamList.h"
3131
#include "swift/AST/GenericSignature.h"
32-
#include "swift/AST/InverseMarking.h"
3332
#include "swift/AST/MacroDefinition.h"
3433
#include "swift/AST/Module.h"
3534
#include "swift/AST/NameLookup.h"

lib/AST/ASTScopeLookup.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,24 @@ bool Portion::lookupMembersOf(const GenericTypeOrExtensionScope *,
274274
return false;
275275
}
276276

277-
bool GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
277+
bool GenericTypeOrExtensionWherePortion::lookupMembersOf(
278278
const GenericTypeOrExtensionScope *scope,
279279
ASTScopeImpl::DeclConsumer consumer) const {
280280
if (scope->getCorrespondingNominalTypeDecl().isNull())
281281
return false;
282+
283+
if (!scope->areMembersVisibleFromWhereClause())
284+
return false;
285+
282286
return consumer.lookInMembers(scope->getGenericContext());
283287
}
284288

285-
bool GenericTypeOrExtensionWherePortion::lookupMembersOf(
289+
bool IterableTypeBodyPortion::lookupMembersOf(
286290
const GenericTypeOrExtensionScope *scope,
287291
ASTScopeImpl::DeclConsumer consumer) const {
288-
if (!scope->areMembersVisibleFromWhereClause())
292+
if (scope->getCorrespondingNominalTypeDecl().isNull())
289293
return false;
290-
291-
return GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
292-
scope, consumer);
294+
return consumer.lookInMembers(scope->getGenericContext());
293295
}
294296

295297
bool GenericTypeOrExtensionScope::areMembersVisibleFromWhereClause() const {

lib/AST/Decl.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "swift/AST/GenericSignature.h"
3333
#include "swift/AST/ImportCache.h"
3434
#include "swift/AST/Initializer.h"
35-
#include "swift/AST/InverseMarking.h"
3635
#include "swift/AST/LazyResolver.h"
3736
#include "swift/AST/MacroDefinition.h"
3837
#include "swift/AST/MacroDiscriminatorContext.h"
@@ -6615,42 +6614,6 @@ bool ProtocolDecl::inheritsFrom(const ProtocolDecl *super) const {
66156614
return (llvm::find(allInherited, super) != allInherited.end());
66166615
}
66176616

6618-
static void findInheritedType(
6619-
InheritedTypes inherited,
6620-
llvm::function_ref<bool(Type, NullablePtr<TypeRepr>)> isMatch) {
6621-
for (size_t i = 0; i < inherited.size(); i++) {
6622-
auto type = inherited.getResolvedType(i, TypeResolutionStage::Structural);
6623-
if (!type)
6624-
continue;
6625-
6626-
if (isMatch(type, inherited.getTypeRepr(i)))
6627-
break;
6628-
}
6629-
}
6630-
6631-
static InverseMarking::Mark
6632-
findInverseInInheritance(InheritedTypes inherited,
6633-
InvertibleProtocolKind target) {
6634-
auto isInverseOfTarget = [&](Type t) {
6635-
if (auto pct = t->getAs<ProtocolCompositionType>())
6636-
return pct->getInverses().contains(target);
6637-
return false;
6638-
};
6639-
6640-
InverseMarking::Mark inverse;
6641-
findInheritedType(inherited,
6642-
[&](Type inheritedTy, NullablePtr<TypeRepr> repr) {
6643-
if (!isInverseOfTarget(inheritedTy))
6644-
return false;
6645-
6646-
inverse = InverseMarking::Mark(
6647-
InverseMarking::Kind::Explicit,
6648-
repr.isNull() ? SourceLoc() : repr.get()->getLoc());
6649-
return true;
6650-
});
6651-
return inverse;
6652-
}
6653-
66546617
bool ProtocolDecl::requiresClass() const {
66556618
return evaluateOrDefault(getASTContext().evaluator,
66566619
ProtocolRequiresClassRequest{const_cast<ProtocolDecl *>(this)}, false);

0 commit comments

Comments
 (0)