Skip to content

Commit 0cb4b14

Browse files
lorenteyAzoy
andcommitted
[AST] Generalize is_same_metatype builtin to take unconstrained metatypes
The builtin’s current is signature is: ``` (Any.Type, Any.Type) -> Bool ``` This needs to be changed to this: ``` (any (~Copyable & ~Escapable).Type, any (~Copyable & ~Escapable).Type) -> Bool ``` This requires a bit of support work in AST synthesis. rdar://145707064 Co-authored-by: Alejandro Alonso <[email protected]>
1 parent c00b985 commit 0cb4b14

File tree

7 files changed

+37
-4
lines changed

7 files changed

+37
-4
lines changed

include/swift/AST/ASTContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@ class ASTContext final {
682682
/// Retrieve the type Swift.Any as an existential type.
683683
CanType getAnyExistentialType() const;
684684

685+
/// Retrieve the existential type 'any ~Copyable & ~Escapable'.
686+
///
687+
/// This is the most permissive existential type.
688+
CanType getUnconstrainedAnyExistentialType() const;
689+
685690
/// Retrieve the type Swift.AnyObject as a constraint.
686691
CanType getAnyObjectConstraint() const;
687692

@@ -1043,6 +1048,9 @@ class ASTContext final {
10431048
const CanType TheEmptyTupleType; /// This is '()', aka Void
10441049
const CanType TheEmptyPackType;
10451050
const CanType TheAnyType; /// This is 'Any', the empty protocol composition
1051+
const CanType TheUnconstrainedAnyType; /// This is 'any ~Copyable & ~Escapable',
1052+
/// the empty protocol composition
1053+
/// without any implicit constraints.
10461054
#define SINGLETON_TYPE(SHORT_ID, ID) \
10471055
const CanType The##SHORT_ID##Type;
10481056
#include "swift/AST/TypeNodes.def"

include/swift/AST/ASTSynthesis.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ enum SingletonTypeSynthesizer {
5757
_taskExecutor, // the '_Concurrency.TaskExecutor' protocol
5858
_actor, // the '_Concurrency.Actor' protocol
5959
_distributedActor, // the 'Distributed.DistributedActor' protocol
60-
_unsafeRawBufferPointer // UnsafeRawBufferPointer
60+
_unsafeRawBufferPointer, // UnsafeRawBufferPointer
61+
_unconstrainedAny, // any ~Copyable & ~Escapable
6162
};
6263
inline Type synthesizeType(SynthesisContext &SC,
6364
SingletonTypeSynthesizer kind) {
@@ -98,6 +99,8 @@ inline Type synthesizeType(SynthesisContext &SC,
9899
case _escapable:
99100
return SC.Context.getProtocol(KnownProtocolKind::Escapable)
100101
->getDeclaredInterfaceType();
102+
case _unconstrainedAny:
103+
return SC.Context.getUnconstrainedAnyExistentialType();
101104
}
102105
}
103106

include/swift/AST/Builtins.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ BUILTIN_MISC_OPERATION(IsConcrete, "isConcrete", "n", Special)
582582
/// IsBitwiseTakable has type T.Type -> Bool
583583
BUILTIN_MISC_OPERATION(IsBitwiseTakable, "isbitwisetakable", "n", Special)
584584

585-
/// IsSameMetatype has type (Any.Type, Any.Type) -> Bool
585+
/// IsSameMetatype has type
586+
/// (any (~Copyable & ~Escapable).Type, any (~Copyable & ~Escapable).Type) -> Bool
586587
BUILTIN_MISC_OPERATION(IsSameMetatype, "is_same_metatype", "n", Special)
587588

588589
/// AllocRaw has type (Int, Int) -> Builtin.RawPointer

include/swift/AST/Types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6359,6 +6359,12 @@ class ProtocolCompositionType final : public TypeBase,
63596359
/// Constructs a protocol composition corresponding to the `Any` type.
63606360
static Type theAnyType(const ASTContext &C);
63616361

6362+
/// Constructs a protocol composition corresponding to the `any ~Copyable &
6363+
/// ~Escapable` type.
6364+
///
6365+
/// Note: This includes the inverse of all current invertible protocols.
6366+
static Type theUnconstrainedAnyType(const ASTContext &C);
6367+
63626368
/// Constructs a protocol composition corresponding to the `AnyObject` type.
63636369
static Type theAnyObjectType(const ASTContext &C);
63646370

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,8 @@ ASTContext::ASTContext(
815815
TheEmptyTupleType(TupleType::get(ArrayRef<TupleTypeElt>(), *this)),
816816
TheEmptyPackType(PackType::get(*this, {})),
817817
TheAnyType(ProtocolCompositionType::theAnyType(*this)),
818+
TheUnconstrainedAnyType(
819+
ProtocolCompositionType::theUnconstrainedAnyType(*this)),
818820
#define SINGLETON_TYPE(SHORT_ID, ID) \
819821
The##SHORT_ID##Type(new (*this, AllocationArena::Permanent) \
820822
ID##Type(*this)),
@@ -1326,6 +1328,10 @@ CanType ASTContext::getAnyExistentialType() const {
13261328
return ExistentialType::get(TheAnyType)->getCanonicalType();
13271329
}
13281330

1331+
CanType ASTContext::getUnconstrainedAnyExistentialType() const {
1332+
return ExistentialType::get(TheUnconstrainedAnyType)->getCanonicalType();
1333+
}
1334+
13291335
CanType ASTContext::getAnyObjectConstraint() const {
13301336
if (getImpl().AnyObjectType) {
13311337
return getImpl().AnyObjectType;

lib/AST/Builtins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,8 @@ static ValueDecl *getIsOptionalOperation(ASTContext &ctx, Identifier id) {
11081108

11091109
static ValueDecl *getIsSameMetatypeOperation(ASTContext &ctx, Identifier id) {
11101110
return getBuiltinFunction(ctx, id, _thin,
1111-
_parameters(_existentialMetatype(_any),
1112-
_existentialMetatype(_any)),
1111+
_parameters(_existentialMetatype(_unconstrainedAny),
1112+
_existentialMetatype(_unconstrainedAny)),
11131113
_int(1));
11141114
}
11151115

lib/AST/Type.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,15 @@ Type ProtocolCompositionType::theAnyType(const ASTContext &C) {
38603860
/*HasExplicitAnyObject=*/false);
38613861
}
38623862

3863+
/// Constructs a protocol composition corresponding to the `any ~Copyable &
3864+
/// ~Escapable` type.
3865+
///
3866+
/// Note: This includes the inverse of all current invertible protocols.
3867+
Type ProtocolCompositionType::theUnconstrainedAnyType(const ASTContext &C) {
3868+
return ProtocolCompositionType::get(C, {}, InvertibleProtocolSet::allKnown(),
3869+
/*HasExplicitAnyObject=*/false);
3870+
}
3871+
38633872
/// Constructs a protocol composition containing the `AnyObject` constraint.
38643873
Type ProtocolCompositionType::theAnyObjectType(const ASTContext &C) {
38653874
return ProtocolCompositionType::get(C, {}, /*Inverses=*/{},

0 commit comments

Comments
 (0)