Skip to content

Commit a69bcf8

Browse files
authored
Merge pull request swiftlang#67930 from kavon/copyable-requirement
Copyable as a Requirement Against the Machine
2 parents 975b5f3 + 80097bc commit a69bcf8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+706
-157
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,10 @@ ERROR(inferred_opaque_type,none,
23462346
"property definition has inferred type %0, involving the 'some' "
23472347
"return type of another declaration", (Type))
23482348

2349+
// Inverse Constraints
2350+
ERROR(inverse_type_not_invertable,none,
2351+
"type %0 is not invertable", (Type))
2352+
23492353
// Extensions
23502354
ERROR(non_nominal_extension,none,
23512355
"non-nominal type %0 cannot be extended", (Type))

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ PROTOCOL(AsyncIteratorProtocol)
126126

127127
PROTOCOL(FloatingPoint)
128128

129-
PROTOCOL_(Copyable)
129+
PROTOCOL(Copyable)
130130

131131
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral, "Array", false)
132132
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByBooleanLiteral, "BooleanLiteralType", true)

include/swift/AST/KnownProtocols.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_AST_KNOWNPROTOCOLS_H
1515

1616
#include "swift/Basic/InlineBitfield.h"
17+
#include "swift/Basic/FixedBitSet.h"
1718
#include "swift/Config.h"
1819

1920
namespace llvm {
@@ -41,6 +42,12 @@ enum : uint8_t {
4142
enum : unsigned { NumKnownProtocolKindBits =
4243
countBitsUsed(static_cast<unsigned>(NumKnownProtocols - 1)) };
4344

45+
using KnownProtocolSet = FixedBitSet<NumKnownProtocols, KnownProtocolKind>;
46+
47+
/// Produces a set of all protocols that have an inverse, i.e., for every
48+
/// known protocol KP in the set, ~KP exists.
49+
KnownProtocolSet getInvertableProtocols();
50+
4451
/// Retrieve the name of the given known protocol.
4552
llvm::StringRef getProtocolName(KnownProtocolKind kind);
4653

include/swift/AST/TypeRepr.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,34 @@ class ExistentialTypeRepr: public TypeRepr {
13211321
friend class TypeRepr;
13221322
};
13231323

1324+
/// A type repr represeting the inverse of some constraint. For example,
1325+
/// ~Copyable
1326+
/// where `Copyable` is the constraint type.
1327+
class InverseTypeRepr : public TypeRepr {
1328+
TypeRepr *Constraint;
1329+
SourceLoc TildeLoc;
1330+
1331+
public:
1332+
InverseTypeRepr(SourceLoc tildeLoc, TypeRepr *constraint)
1333+
: TypeRepr(TypeReprKind::Inverse), Constraint(constraint),
1334+
TildeLoc(tildeLoc) {}
1335+
1336+
TypeRepr *getConstraint() const { return Constraint; }
1337+
SourceLoc getTildeLoc() const { return TildeLoc; }
1338+
1339+
static bool classof(const TypeRepr *T) {
1340+
return T->getKind() == TypeReprKind::Inverse;
1341+
}
1342+
static bool classof(const InverseTypeRepr *T) { return true; }
1343+
1344+
private:
1345+
SourceLoc getStartLocImpl() const { return TildeLoc; }
1346+
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
1347+
SourceLoc getLocImpl() const { return TildeLoc; }
1348+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1349+
friend class TypeRepr;
1350+
};
1351+
13241352
/// TypeRepr for a user-specified placeholder (essentially, a user-facing
13251353
/// representation of an anonymous type variable.
13261354
///
@@ -1459,6 +1487,7 @@ inline bool TypeRepr::isSimple() const {
14591487
case TypeReprKind::Dictionary:
14601488
case TypeReprKind::Optional:
14611489
case TypeReprKind::ImplicitlyUnwrappedOptional:
1490+
case TypeReprKind::Inverse:
14621491
case TypeReprKind::Vararg:
14631492
case TypeReprKind::PackExpansion:
14641493
case TypeReprKind::Pack:

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TYPEREPR(Protocol, TypeRepr)
6565
TYPEREPR(OpaqueReturn, TypeRepr)
6666
TYPEREPR(NamedOpaqueReturn, TypeRepr)
6767
TYPEREPR(Existential, TypeRepr)
68+
TYPEREPR(Inverse, TypeRepr)
6869
TYPEREPR(Pack, TypeRepr)
6970
TYPEREPR(PackElement, TypeRepr)
7071
TYPEREPR(Placeholder, TypeRepr)

include/swift/AST/Types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ class alignas(1 << TypeAlignInBits) TypeBase
634634

635635
bool isPlaceholder();
636636

637-
/// Returns true if this is a move-only type.
638-
bool isPureMoveOnly();
637+
/// Returns true if this is a noncopyable type.
638+
bool isNoncopyable();
639639

640640
/// Does the type have outer parenthesis?
641641
bool hasParenSugar() const { return getKind() == TypeKind::Paren; }
@@ -5351,7 +5351,7 @@ class SILMoveOnlyWrappedType final : public TypeBase,
53515351
: TypeBase(TypeKind::SILMoveOnlyWrapped, &innerType->getASTContext(),
53525352
innerType->getRecursiveProperties()),
53535353
innerType(innerType) {
5354-
assert(!innerType->isPureMoveOnly() && "Inner type must be copyable");
5354+
assert(!innerType->isNoncopyable() && "Inner type must be copyable");
53555355
}
53565356

53575357
public:

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ EXPERIMENTAL_FEATURE(RawLayout, true)
235235
/// Enables the "embedded" swift mode (no runtime).
236236
EXPERIMENTAL_FEATURE(Embedded, true)
237237

238+
/// Enables noncopyable generics
239+
EXPERIMENTAL_FEATURE(NoncopyableGenerics, false)
240+
238241
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
239242
#undef EXPERIMENTAL_FEATURE
240243
#undef UPCOMING_FEATURE

include/swift/Parse/Parser.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ class Parser {
158158
bool InSwiftKeyPath = false;
159159
bool InFreestandingMacroArgument = false;
160160

161+
// A cached answer to
162+
// Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)
163+
// to ensure there's no parsing performance regression.
164+
bool EnabledNoncopyableGenerics;
165+
161166
/// Whether we should delay parsing nominal type, extension, and function
162167
/// bodies.
163168
bool isDelayedParsingEnabled() const;
@@ -2058,6 +2063,11 @@ class Parser {
20582063

20592064
void performIDEInspectionSecondPassImpl(
20602065
IDEInspectionDelayedDeclState &info);
2066+
2067+
/// Returns true if the caller should skip calling `parseType` afterwards.
2068+
bool parseLegacyTildeCopyable(SourceLoc *parseTildeCopyable,
2069+
ParserStatus &Status,
2070+
SourceLoc &TildeCopyableLoc);
20612071
};
20622072

20632073
/// Describes a parsed declaration name.

include/swift/SIL/SILType.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,6 @@ class SILType {
748748
/// wrapped type.
749749
bool isMoveOnly() const;
750750

751-
/// Is this a type that is a first class move only type. This returns false
752-
/// for a move only wrapped type.
753-
bool isPureMoveOnly() const;
754-
755751
/// Return true if this is a value type (struct/enum) that requires
756752
/// deinitialization beyond destruction of its members.
757753
bool isValueTypeWithDeinit() const;

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ llvm::StringRef swift::getProtocolName(KnownProtocolKind kind) {
102102
llvm_unreachable("bad KnownProtocolKind");
103103
}
104104

105+
KnownProtocolSet swift::getInvertableProtocols() {
106+
return { KnownProtocolKind::Copyable };
107+
}
108+
105109
namespace {
106110
enum class SearchPathKind : uint8_t {
107111
Import = 1 << 0,

0 commit comments

Comments
 (0)