Skip to content

Commit 0312a93

Browse files
authored
Merge pull request #60810 from angela-laar/experiment_implicitsome
Implement `some` elision behind an experimental feature flag
2 parents e1f6c70 + dffafdb commit 0312a93

16 files changed

+464
-91
lines changed

include/swift/AST/Decl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,7 +2822,7 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
28222822
/// (opaque archetype 1))`.
28232823
class OpaqueTypeDecl final :
28242824
public GenericTypeDecl,
2825-
private llvm::TrailingObjects<OpaqueTypeDecl, OpaqueReturnTypeRepr *> {
2825+
private llvm::TrailingObjects<OpaqueTypeDecl, TypeRepr *> {
28262826
friend TrailingObjects;
28272827

28282828
public:
@@ -2866,7 +2866,7 @@ class OpaqueTypeDecl final :
28662866
OpaqueTypeDecl(ValueDecl *NamingDecl, GenericParamList *GenericParams,
28672867
DeclContext *DC,
28682868
GenericSignature OpaqueInterfaceGenericSignature,
2869-
ArrayRef<OpaqueReturnTypeRepr *> OpaqueReturnTypeReprs);
2869+
ArrayRef<TypeRepr *> OpaqueReturnTypeReprs);
28702870

28712871
unsigned getNumOpaqueReturnTypeReprs() const {
28722872
return NamingDeclAndHasOpaqueReturnTypeRepr.getInt()
@@ -2883,7 +2883,7 @@ class OpaqueTypeDecl final :
28832883
ValueDecl *NamingDecl, GenericParamList *GenericParams,
28842884
DeclContext *DC,
28852885
GenericSignature OpaqueInterfaceGenericSignature,
2886-
ArrayRef<OpaqueReturnTypeRepr *> OpaqueReturnTypeReprs);
2886+
ArrayRef<TypeRepr *> OpaqueReturnTypeReprs);
28872887

28882888
ValueDecl *getNamingDecl() const {
28892889
return NamingDeclAndHasOpaqueReturnTypeRepr.getPointer();
@@ -2904,7 +2904,7 @@ class OpaqueTypeDecl final :
29042904
/// repr `repr`, as introduce implicitly by an occurrence of "some" in return
29052905
/// position e.g. `func f() -> some P`. Returns -1 if `repr` is not found.
29062906
Optional<unsigned> getAnonymousOpaqueParamOrdinal(
2907-
OpaqueReturnTypeRepr *repr) const;
2907+
TypeRepr *repr) const;
29082908

29092909
GenericSignature getOpaqueInterfaceGenericSignature() const {
29102910
return OpaqueInterfaceGenericSignature;
@@ -2928,9 +2928,9 @@ class OpaqueTypeDecl final :
29282928

29292929
/// Retrieve the buffer containing the opaque return type
29302930
/// representations that correspond to the opaque generic parameters.
2931-
ArrayRef<OpaqueReturnTypeRepr *> getOpaqueReturnTypeReprs() const {
2931+
ArrayRef<TypeRepr *> getOpaqueReturnTypeReprs() const {
29322932
return {
2933-
getTrailingObjects<OpaqueReturnTypeRepr *>(),
2933+
getTrailingObjects<TypeRepr *>(),
29342934
getNumOpaqueReturnTypeReprs()
29352935
};
29362936
}
@@ -3163,7 +3163,7 @@ class AbstractTypeParamDecl : public TypeDecl {
31633163
/// \endcode
31643164
class GenericTypeParamDecl final :
31653165
public AbstractTypeParamDecl,
3166-
private llvm::TrailingObjects<GenericTypeParamDecl, OpaqueReturnTypeRepr *>{
3166+
private llvm::TrailingObjects<GenericTypeParamDecl, TypeRepr *>{
31673167
friend TrailingObjects;
31683168

31693169
size_t numTrailingObjects(OverloadToken<OpaqueReturnTypeRepr *>) const {
@@ -3180,7 +3180,7 @@ class GenericTypeParamDecl final :
31803180
/// \param nameLoc The location of the name.
31813181
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
31823182
bool isTypeSequence, unsigned depth, unsigned index,
3183-
bool isOpaqueType, OpaqueReturnTypeRepr *opaqueTypeRepr);
3183+
bool isOpaqueType, TypeRepr *typeRepr);
31843184

31853185
public:
31863186
/// Construct a new generic type parameter.
@@ -3201,7 +3201,7 @@ class GenericTypeParamDecl final :
32013201
static GenericTypeParamDecl *
32023202
create(DeclContext *dc, Identifier name, SourceLoc nameLoc,
32033203
bool isTypeSequence, unsigned depth, unsigned index,
3204-
bool isOpaqueType, OpaqueReturnTypeRepr *opaqueTypeRepr);
3204+
bool isOpaqueType, TypeRepr *typeRepr);
32053205

32063206
/// The depth of this generic type parameter, i.e., the number of outer
32073207
/// levels of generic parameter lists that enclose this type parameter.
@@ -3249,11 +3249,11 @@ class GenericTypeParamDecl final :
32493249
/// extension, meaning that it was specified explicitly
32503250
/// - the enclosing declaration was deserialized, in which case it lost
32513251
/// the source location information and has no type representation.
3252-
OpaqueReturnTypeRepr *getOpaqueTypeRepr() const {
3252+
TypeRepr *getOpaqueTypeRepr() const {
32533253
if (!isOpaqueType())
32543254
return nullptr;
32553255

3256-
return *getTrailingObjects<OpaqueReturnTypeRepr *>();
3256+
return *getTrailingObjects<TypeRepr *>();
32573257
}
32583258

32593259
/// The index of this generic type parameter within its generic parameter

include/swift/AST/NameLookup.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/Basic/SourceLoc.h"
2828
#include "swift/Basic/SourceManager.h"
2929
#include "llvm/ADT/SmallVector.h"
30+
#include "swift/AST/TypeRepr.h"
3031

3132
namespace swift {
3233
class ASTContext;
@@ -36,12 +37,15 @@ class TypeDecl;
3637
class ValueDecl;
3738
struct SelfBounds;
3839
class NominalTypeDecl;
39-
4040
namespace ast_scope {
4141
class ASTSourceFileScope;
4242
class ASTScopeImpl;
4343
} // namespace ast_scope
4444

45+
/// Walk the type representation recursively, collecting any
46+
/// `OpaqueReturnTypeRepr`s, `CompositionTypeRepr`s or `IdentTypeRepr`s.
47+
CollectedOpaqueReprs collectOpaqueReturnTypeReprs(TypeRepr *, ASTContext &ctx, DeclContext *dc);
48+
4549
/// LookupResultEntry - One result of unqualified lookup.
4650
struct LookupResultEntry {
4751
private:
@@ -637,8 +641,8 @@ class FindLocalVal : public StmtVisitor<FindLocalVal> {
637641
void visitDoCatchStmt(DoCatchStmt *S);
638642

639643
};
640-
641-
644+
645+
642646
/// The bridge between the legacy UnqualifiedLookupFactory and the new ASTScope
643647
/// lookup system
644648
class AbstractASTScopeDeclConsumer {

include/swift/AST/TypeRepr.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Support/TrailingObjects.h"
3434

3535
namespace swift {
36+
class ASTContext;
3637
class ASTWalker;
3738
class DeclContext;
3839
class IdentTypeRepr;
@@ -48,7 +49,7 @@ enum : unsigned { NumTypeReprKindBits =
4849
countBitsUsed(static_cast<unsigned>(TypeReprKind::Last_TypeRepr)) };
4950

5051
class OpaqueReturnTypeRepr;
51-
using CollectedOpaqueReprs = SmallVector<OpaqueReturnTypeRepr *, 2>;
52+
using CollectedOpaqueReprs = SmallVector<TypeRepr *, 2>;
5253

5354
/// Representation of a type as written in source.
5455
class alignas(1 << TypeReprAlignInBits) TypeRepr
@@ -118,6 +119,9 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
118119
return static_cast<TypeReprKind>(Bits.TypeRepr.Kind);
119120
}
120121

122+
/// Is this type representation a protocol?
123+
bool isProtocol(DeclContext *dc);
124+
121125
/// Is this type representation known to be invalid?
122126
bool isInvalid() const { return Bits.TypeRepr.Invalid; }
123127

@@ -165,10 +169,6 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
165169
/// opaque return type reprs.
166170
bool hasOpaque();
167171

168-
/// Walk the type representation recursively, collecting any
169-
/// `OpaqueReturnTypeRepr`s.
170-
CollectedOpaqueReprs collectOpaqueReturnTypeReprs();
171-
172172
/// Retrieve the type repr without any parentheses around it.
173173
///
174174
/// The use of this function must be restricted to contexts where
@@ -866,6 +866,12 @@ class CompositionTypeRepr final : public TypeRepr,
866866
SourceLoc getSourceLoc() const { return FirstTypeLoc; }
867867
SourceRange getCompositionRange() const { return CompositionRange; }
868868

869+
/// 'Any' is understood as CompositionTypeRepr by the compiler but its type array will be empty
870+
/// becasue it is a nonspecific type
871+
bool isTypeReprAny() {
872+
return getTypes().size() == 0 ? true : false;
873+
}
874+
869875
static CompositionTypeRepr *create(const ASTContext &C,
870876
ArrayRef<TypeRepr*> Protocols,
871877
SourceLoc FirstTypeLoc,

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ EXPERIMENTAL_FEATURE(ParserValidation)
138138
/// Swift Swift parser.
139139
EXPERIMENTAL_FEATURE(ParserSequenceFolding)
140140

141+
/// Enables implicit some while also enabling existential `any`
142+
EXPERIMENTAL_FEATURE(ImplicitSome)
143+
141144
#undef EXPERIMENTAL_FEATURE
142145
#undef UPCOMING_FEATURE
143146
#undef SUPPRESSIBLE_LANGUAGE_FEATURE

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ def enable_experimental_associated_type_inference :
579579
Flag<["-"], "enable-experimental-associated-type-inference">,
580580
HelpText<"Enable experimental associated type inference via type witness systems">;
581581

582+
def enable_experimental_implicit_some :
583+
Flag<["-"], "enable-experimental-implicit-some">,
584+
HelpText<"Enable experimental implicit some">;
585+
582586
def disable_availability_checking : Flag<["-"],
583587
"disable-availability-checking">,
584588
HelpText<"Disable checking for potentially unavailable APIs">;

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,10 @@ static bool usesFeatureExistentialAny(Decl *decl) {
30453045
return false;
30463046
}
30473047

3048+
static bool usesFeatureImplicitSome(Decl *decl) {
3049+
return false;
3050+
}
3051+
30483052
static bool usesFeatureForwardTrailingClosures(Decl *decl) {
30493053
return false;
30503054
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ NullablePtr<const GenericParamList> ASTScopeImpl::genericParams() const {
166166
}
167167
NullablePtr<const GenericParamList>
168168
AbstractFunctionDeclScope::genericParams() const {
169-
return decl->getGenericParams();
169+
return decl->getParsedGenericParams();
170170
}
171171
NullablePtr<const GenericParamList> SubscriptDeclScope::genericParams() const {
172-
return decl->getGenericParams();
172+
return decl->getParsedGenericParams();
173173
}
174174
NullablePtr<const GenericParamList> GenericTypeScope::genericParams() const {
175175
// For Decls:

lib/AST/Decl.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,8 +3053,13 @@ TypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
30533053
returnRepr = SD->getElementTypeRepr();
30543054
}
30553055

3056+
auto *dc = getDeclContext();
3057+
auto &ctx = dc->getASTContext();
3058+
30563059
if (returnRepr && returnRepr->hasOpaque()) {
30573060
return returnRepr;
3061+
} else if (returnRepr && ctx.LangOpts.hasFeature(Feature::ImplicitSome) && returnRepr->isProtocol(dc)) {
3062+
return returnRepr;
30583063
} else {
30593064
return nullptr;
30603065
}
@@ -4592,17 +4597,17 @@ AbstractTypeParamDecl::getConformingProtocols() const {
45924597
GenericTypeParamDecl::GenericTypeParamDecl(
45934598
DeclContext *dc, Identifier name, SourceLoc nameLoc,
45944599
bool isTypeSequence, unsigned depth, unsigned index,
4595-
bool isOpaqueType, OpaqueReturnTypeRepr *opaqueTypeRepr
4600+
bool isOpaqueType, TypeRepr *typeRepr
45964601
) : AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
45974602
Bits.GenericTypeParamDecl.Depth = depth;
45984603
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
45994604
Bits.GenericTypeParamDecl.Index = index;
46004605
assert(Bits.GenericTypeParamDecl.Index == index && "Truncation");
46014606
Bits.GenericTypeParamDecl.TypeSequence = isTypeSequence;
46024607
Bits.GenericTypeParamDecl.IsOpaqueType = isOpaqueType;
4603-
assert(isOpaqueType || !opaqueTypeRepr);
4608+
assert(isOpaqueType || !typeRepr);
46044609
if (isOpaqueType)
4605-
*getTrailingObjects<OpaqueReturnTypeRepr *>() = opaqueTypeRepr;
4610+
*getTrailingObjects<TypeRepr *>() = typeRepr;
46064611

46074612
auto &ctx = dc->getASTContext();
46084613
RecursiveTypeProperties props = RecursiveTypeProperties::HasTypeParameter;
@@ -4616,14 +4621,14 @@ GenericTypeParamDecl *
46164621
GenericTypeParamDecl::create(
46174622
DeclContext *dc, Identifier name, SourceLoc nameLoc,
46184623
bool isTypeSequence, unsigned depth, unsigned index,
4619-
bool isOpaqueType, OpaqueReturnTypeRepr *opaqueTypeRepr) {
4624+
bool isOpaqueType, TypeRepr *typeRepr) {
46204625
ASTContext &ctx = dc->getASTContext();
46214626
auto mem = ctx.Allocate(
4622-
totalSizeToAlloc<OpaqueReturnTypeRepr *>(isOpaqueType ? 1 : 0),
4627+
totalSizeToAlloc<TypeRepr *>(isOpaqueType ? 1 : 0),
46234628
alignof(GenericTypeParamDecl));
46244629
return new (mem) GenericTypeParamDecl(
46254630
dc, name, nameLoc, isTypeSequence, depth, index, isOpaqueType,
4626-
opaqueTypeRepr);
4631+
typeRepr);
46274632
}
46284633

46294634

@@ -8252,7 +8257,7 @@ void AbstractFunctionDecl::setParameters(ParameterList *BodyParams) {
82528257
OpaqueTypeDecl::OpaqueTypeDecl(ValueDecl *NamingDecl,
82538258
GenericParamList *GenericParams, DeclContext *DC,
82548259
GenericSignature OpaqueInterfaceGenericSignature,
8255-
ArrayRef<OpaqueReturnTypeRepr *>
8260+
ArrayRef<TypeRepr *>
82568261
OpaqueReturnTypeReprs)
82578262
: GenericTypeDecl(DeclKind::OpaqueType, DC, Identifier(), SourceLoc(), {},
82588263
GenericParams),
@@ -8269,16 +8274,16 @@ OpaqueTypeDecl::OpaqueTypeDecl(ValueDecl *NamingDecl,
82698274
OpaqueInterfaceGenericSignature.getInnermostGenericParams().size());
82708275
std::uninitialized_copy(
82718276
OpaqueReturnTypeReprs.begin(), OpaqueReturnTypeReprs.end(),
8272-
getTrailingObjects<OpaqueReturnTypeRepr *>());
8277+
getTrailingObjects<TypeRepr *>());
82738278
}
82748279

82758280
OpaqueTypeDecl *OpaqueTypeDecl::get(
82768281
ValueDecl *NamingDecl, GenericParamList *GenericParams,
82778282
DeclContext *DC,
82788283
GenericSignature OpaqueInterfaceGenericSignature,
8279-
ArrayRef<OpaqueReturnTypeRepr *> OpaqueReturnTypeReprs) {
8284+
ArrayRef<TypeRepr *> OpaqueReturnTypeReprs) {
82808285
ASTContext &ctx = DC->getASTContext();
8281-
auto size = totalSizeToAlloc<OpaqueReturnTypeRepr *>(
8286+
auto size = totalSizeToAlloc<TypeRepr *>(
82828287
OpaqueReturnTypeReprs.size());
82838288
auto mem = ctx.Allocate(size, alignof(OpaqueTypeDecl));
82848289
return new (mem) OpaqueTypeDecl(
@@ -8315,7 +8320,7 @@ GenericTypeParamDecl *OpaqueTypeDecl::getExplicitGenericParam(
83158320
}
83168321

83178322
Optional<unsigned> OpaqueTypeDecl::getAnonymousOpaqueParamOrdinal(
8318-
OpaqueReturnTypeRepr *repr) const {
8323+
TypeRepr *repr) const {
83198324
assert(NamingDeclAndHasOpaqueReturnTypeRepr.getInt() &&
83208325
"can't do opaque param lookup without underlying interface repr");
83218326
auto opaqueReprs = getOpaqueReturnTypeReprs();

0 commit comments

Comments
 (0)