Skip to content

Commit b218c86

Browse files
authored
Merge pull request #72528 from slavapestov/cherry-picks-6.0
[6.0] Cherry pick name lookup fixes
2 parents 3382dc9 + bb547ce commit b218c86

30 files changed

+458
-576
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
> This is in reverse chronological order, so newer entries are added to the top.
55
66
## Swift 6.0
7+
8+
* Swift 5.10 missed a semantic check from [SE-0309][]. In type context, a reference to a
9+
protocol `P` that has associated types or `Self` requirements should use
10+
the `any` keyword, but this was not enforced in nested generic argument positions.
11+
This is now an error as required by the proposal:
12+
13+
```swift
14+
protocol P { associatedtype A }
15+
struct Outer<T> { struct Inner<U> { } }
16+
let x = Outer<P>.Inner<P>() // error
17+
```
18+
To correct the error, add `any` where appropriate, for example
19+
`Outer<any P>.Inner<any P>`.
20+
21+
* Swift 5.10 accepted certain invalid opaque return types from [SE-0346][].
22+
If a generic argument of a constrained opaque return type did not
23+
satisfy the requirements on the primary associated type, the generic
24+
argument was silently ignored and type checking would proceed as if it
25+
weren't stated. This now results in a diagnostic:
26+
27+
```swift
28+
protocol P<A> { associatedtype A: Sequence }
29+
struct G<A: Sequence>: P {}
30+
31+
func f() -> some P<Int> { return G<Array<Int>>() } // error
32+
```
33+
34+
The return type above should be written as `some P<Array<Int>>` to match
35+
the return statement. The old broken behavior in this situation can also
36+
be restored, by removing the erroneous constraint and using the more general
37+
upper bound `some P`.
38+
739
* [SE-0408][]:
840
A `for`-`in` loop statement can now accept a pack expansion expression,
941
enabling iteration over the elements of its respective value pack. This form

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: 6 additions & 15 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;
@@ -600,7 +598,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
600598
IsComputingSemanticMembers : 1
601599
);
602600

603-
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+8,
601+
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+8,
604602
/// Whether the \c RequiresClass bit is valid.
605603
RequiresClassValid : 1,
606604

@@ -627,6 +625,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
627625
/// Whether we've computed the inherited protocols list yet.
628626
InheritedProtocolsValid : 1,
629627

628+
/// Whether we have computed a requirement signature.
629+
HasRequirementSignature : 1,
630+
630631
/// Whether we have a lazy-loaded requirement signature.
631632
HasLazyRequirementSignature : 1,
632633

@@ -5124,15 +5125,11 @@ class ProtocolDecl final : public NominalTypeDecl {
51245125
/// The superclass decl and a bit to indicate whether the
51255126
/// superclass was computed yet or not.
51265127
llvm::PointerIntPair<ClassDecl *, 1, bool> SuperclassDecl;
5127-
5128-
/// The superclass type and a bit to indicate whether the
5129-
/// superclass was computed yet or not.
5130-
llvm::PointerIntPair<Type, 1, bool> SuperclassType;
51315128
} LazySemanticInfo;
51325129

51335130
/// The generic signature representing exactly the new requirements introduced
51345131
/// by this protocol.
5135-
std::optional<RequirementSignature> RequirementSig;
5132+
RequirementSignature RequirementSig;
51365133

51375134
/// Returns the cached result of \c requiresClass or \c None if it hasn't yet
51385135
/// been computed.
@@ -5219,16 +5216,10 @@ class ProtocolDecl final : public NominalTypeDecl {
52195216
/// Determine whether this protocol has a superclass.
52205217
bool hasSuperclass() const { return (bool)getSuperclassDecl(); }
52215218

5222-
/// Retrieve the superclass of this protocol, or null if there is no superclass.
5223-
Type getSuperclass() const;
5224-
52255219
/// Retrieve the ClassDecl for the superclass of this protocol, or null if there
52265220
/// is no superclass.
52275221
ClassDecl *getSuperclassDecl() const;
52285222

5229-
/// Set the superclass of this protocol.
5230-
void setSuperclass(Type superclass);
5231-
52325223
/// Retrieve the set of AssociatedTypeDecl members of this protocol; this
52335224
/// saves loading the set of members in cases where there's no possibility of
52345225
/// a protocol having nested types (ObjC protocols).
@@ -5447,7 +5438,7 @@ class ProtocolDecl final : public NominalTypeDecl {
54475438

54485439
/// Has the requirement signature been computed yet?
54495440
bool isRequirementSignatureComputed() const {
5450-
return RequirementSig.has_value();
5441+
return Bits.ProtocolDecl.HasRequirementSignature;
54515442
}
54525443

54535444
void setRequirementSignature(RequirementSignature requirementSig);

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/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class InheritedTypeRequest
113113
/// Request the superclass type for the given class.
114114
class SuperclassTypeRequest
115115
: public SimpleRequest<
116-
SuperclassTypeRequest, Type(NominalTypeDecl *, TypeResolutionStage),
116+
SuperclassTypeRequest, Type(ClassDecl *, TypeResolutionStage),
117117
RequestFlags::SeparatelyCached | RequestFlags::DependencySink> {
118118
public:
119119
using SimpleRequest::SimpleRequest;
@@ -123,7 +123,7 @@ class SuperclassTypeRequest
123123

124124
// Evaluation.
125125
Type
126-
evaluate(Evaluator &evaluator, NominalTypeDecl *classDecl,
126+
evaluate(Evaluator &evaluator, ClassDecl *classDecl,
127127
TypeResolutionStage stage) const;
128128

129129
public:

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: 3 additions & 2 deletions
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"
@@ -7767,7 +7766,9 @@ static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
77677766
llvm::SmallVectorImpl<InheritedEntry> &Results) {
77687767
auto &ctx = proto->getASTContext();
77697768

7770-
if (auto superclassTy = proto->getSuperclass()) {
7769+
auto genericSig = proto->getGenericSignature();
7770+
if (auto superclassTy = genericSig->getSuperclassBound(
7771+
proto->getSelfInterfaceType())) {
77717772
Results.emplace_back(TypeLoc::withoutLoc(superclassTy),
77727773
/*isUnchecked=*/false,
77737774
/*isRetroactive=*/false,

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 {

0 commit comments

Comments
 (0)