Skip to content

[WIP][Distributed] Implement _local, towards removing __secretlyKnownToBeLocal #42098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4745,6 +4745,10 @@ class AbstractStorageDecl : public ValueDecl {
}
bool isCompileTimeConst() const;

/// Is this type a '_local' distributed actor type?
/// I.e. it is known to be local and distributed checks need not apply.
bool isKnownToBeLocal() const;

/// \returns the way 'static'/'class' should be spelled for this declaration.
StaticSpellingKind getCorrectStaticSpelling() const;

Expand Down Expand Up @@ -5537,9 +5541,12 @@ class ParamDecl : public VarDecl {

/// Whether or not this parameter is '_const'.
IsCompileTimeConst = 1 << 1,

/// Whether or not this parameter is '_local'.
IsKnownToBeLocal = 1 << 2,
};

llvm::PointerIntPair<Identifier, 2, OptionSet<ArgumentNameFlags>>
llvm::PointerIntPair<Identifier, 3, OptionSet<ArgumentNameFlags>>
ArgumentNameAndFlags;
SourceLoc ParameterNameLoc;
SourceLoc ArgumentNameLoc;
Expand Down Expand Up @@ -5574,9 +5581,13 @@ class ParamDecl : public VarDecl {

/// Whether or not this parameter is 'isolated'.
IsIsolated = 1 << 2,

// /// Whether or not this parameter is '_local'.
// IsKnownToBeLocal = 1 << 3, // FIXME: can't because "PointerIntPair with integer size too large for pointer"
};

/// The default value, if any, along with flags.
// llvm::PointerIntPair<StoredDefaultArgument *, 4, OptionSet<Flags>>
llvm::PointerIntPair<StoredDefaultArgument *, 3, OptionSet<Flags>>
DefaultValueAndFlags;

Expand Down Expand Up @@ -5776,6 +5787,19 @@ class ParamDecl : public VarDecl {
: flags - Flags::IsIsolated);
}

/// Whether or not this parameter is marked with 'isolated'.
bool isKnownToBeLocal() const {
return ArgumentNameAndFlags.getInt().contains(
ArgumentNameFlags::IsKnownToBeLocal);
}

void setKnownToBeLocal(bool value = true) {
auto flags = ArgumentNameAndFlags.getInt();
flags = value ? flags | ArgumentNameFlags::IsKnownToBeLocal
: flags - ArgumentNameFlags::IsKnownToBeLocal;
ArgumentNameAndFlags.setInt(flags);
}

/// Whether or not this parameter is marked with '_const'.
bool isCompileTimeConst() const {
return ArgumentNameAndFlags.getInt().contains(
Expand Down
6 changes: 5 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4611,7 +4611,7 @@ ERROR(distributed_actor_isolated_method,none,
"only 'distributed' instance methods can be called on a potentially remote distributed actor",
())
ERROR(distributed_local_cannot_be_used,none,
"'local' cannot be used in user-defined code currently",
"'_local' cannot be used in user-defined code currently",
())
ERROR(distributed_actor_func_param_not_codable,none,
"parameter '%0' of type %1 in %2 does not conform to serialization requirement '%3'",
Expand Down Expand Up @@ -4677,6 +4677,10 @@ NOTE(protocol_isolated_to_global_actor_here,none,

ERROR(isolated_parameter_not_actor,none,
"'isolated' parameter has non-actor type %0", (Type))
ERROR(local_parameter_not_distributed_actor,none,
"'_local' parameter has non-distributed-actor type %0", (Type))
ERROR(local_not_supported_in_pattern,none,
"'_local' is not support in patterns", ())

WARNING(non_sendable_param_type,none,
"non-sendable type %0 %select{passed in call to %4 %2 %3|"
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
bool printConstExprValue(llvm::raw_ostream *OS, llvm::function_ref<bool(Expr*)> additionalCheck) const;
bool isSemanticallyConstExpr(llvm::function_ref<bool(Expr*)> additionalCheck = nullptr) const;

bool isKnownToBeLocal() const;

/// Returns false if this expression needs to be wrapped in parens when
/// used inside of a any postfix expression, true otherwise.
///
Expand Down
27 changes: 22 additions & 5 deletions include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ class SpecifierTypeRepr : public TypeRepr {
return T->getKind() == TypeReprKind::InOut ||
T->getKind() == TypeReprKind::Shared ||
T->getKind() == TypeReprKind::Owned ||
T->getKind() == TypeReprKind::Isolated;
T->getKind() == TypeReprKind::Isolated ||
T->getKind() == TypeReprKind::KnownToBeLocal;
}
static bool classof(const SpecifierTypeRepr *T) { return true; }

Expand Down Expand Up @@ -1024,8 +1025,8 @@ class OwnedTypeRepr : public SpecifierTypeRepr {
/// \endcode
class IsolatedTypeRepr : public SpecifierTypeRepr {
public:
IsolatedTypeRepr(TypeRepr *Base, SourceLoc InOutLoc)
: SpecifierTypeRepr(TypeReprKind::Isolated, Base, InOutLoc) {}
IsolatedTypeRepr(TypeRepr *Base, SourceLoc Loc)
: SpecifierTypeRepr(TypeReprKind::Isolated, Base, Loc) {}

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::Isolated;
Expand All @@ -1039,15 +1040,30 @@ class IsolatedTypeRepr : public SpecifierTypeRepr {
/// \endcode
class CompileTimeConstTypeRepr : public SpecifierTypeRepr {
public:
CompileTimeConstTypeRepr(TypeRepr *Base, SourceLoc InOutLoc)
: SpecifierTypeRepr(TypeReprKind::CompileTimeConst, Base, InOutLoc) {}
CompileTimeConstTypeRepr(TypeRepr *Base, SourceLoc Loc)
: SpecifierTypeRepr(TypeReprKind::CompileTimeConst, Base, Loc) {}

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::CompileTimeConst;
}
static bool classof(const CompileTimeConstTypeRepr *T) { return true; }
};

/// A '_local' type.
/// \code
/// x : _local SomeDistributedActor
/// \endcode
class KnownToBeLocalTypeRepr : public SpecifierTypeRepr {
public:
KnownToBeLocalTypeRepr(TypeRepr *Base, SourceLoc Loc)
: SpecifierTypeRepr(TypeReprKind::KnownToBeLocal, Base, Loc) {}

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::KnownToBeLocal;
}
static bool classof(const KnownToBeLocalTypeRepr *T) { return true; }
};

/// A TypeRepr for a known, fixed type.
///
/// Fixed type representations should be used sparingly, in places
Expand Down Expand Up @@ -1348,6 +1364,7 @@ inline bool TypeRepr::isSimple() const {
case TypeReprKind::Isolated:
case TypeReprKind::Placeholder:
case TypeReprKind::CompileTimeConst:
case TypeReprKind::KnownToBeLocal:
return true;
}
llvm_unreachable("bad TypeRepr kind");
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/TypeReprNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ABSTRACT_TYPEREPR(Specifier, TypeRepr)
TYPEREPR(Owned, SpecifierTypeRepr)
TYPEREPR(Isolated, SpecifierTypeRepr)
TYPEREPR(CompileTimeConst, SpecifierTypeRepr)
TYPEREPR(KnownToBeLocal, SpecifierTypeRepr)
TYPEREPR(Fixed, TypeRepr)
TYPEREPR(SILBox, TypeRepr)
LAST_TYPEREPR(SILBox)
Expand Down
33 changes: 22 additions & 11 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ class alignas(1 << TypeAlignInBits) TypeBase

/// Determines whether this type conforms or inherits (if it's a protocol
/// type) from `DistributedActor`.
bool isDistributedActor();
bool isDistributedActorType();

/// Determines the element type of a known
/// [Autoreleasing]Unsafe[Mutable][Raw]Pointer variant, or returns null if the
Expand Down Expand Up @@ -1981,7 +1981,8 @@ class ParameterTypeFlags {
NoDerivative = 1 << 6,
Isolated = 1 << 7,
CompileTimeConst = 1 << 8,
NumBits = 9
KnownToBeLocal = 1 << 9,
NumBits = 10
};
OptionSet<ParameterFlags> value;
static_assert(NumBits <= 8*sizeof(OptionSet<ParameterFlags>), "overflowed");
Expand All @@ -1995,21 +1996,23 @@ class ParameterTypeFlags {
}

ParameterTypeFlags(bool variadic, bool autoclosure, bool nonEphemeral,
ValueOwnership ownership, bool isolated, bool noDerivative,
ValueOwnership ownership, bool isolated,
bool knownToBeLocal, bool noDerivative,
bool compileTimeConst)
: value((variadic ? Variadic : 0) | (autoclosure ? AutoClosure : 0) |
(nonEphemeral ? NonEphemeral : 0) |
uint8_t(ownership) << OwnershipShift |
(isolated ? Isolated : 0) |
(noDerivative ? NoDerivative : 0) |
(compileTimeConst ? CompileTimeConst : 0)){}
(compileTimeConst ? CompileTimeConst : 0) |
(knownToBeLocal ? KnownToBeLocal : 0)){}

/// Create one from what's present in the parameter type
inline static ParameterTypeFlags
fromParameterType(Type paramTy, bool isVariadic, bool isAutoClosure,
bool isNonEphemeral, ValueOwnership ownership,
bool isolated, bool isNoDerivative,
bool compileTimeConst);
bool isolated, bool knownToBeLocal,
bool isNoDerivative, bool compileTimeConst);

bool isNone() const { return !value; }
bool isVariadic() const { return value.contains(Variadic); }
Expand All @@ -2020,6 +2023,7 @@ class ParameterTypeFlags {
bool isOwned() const { return getValueOwnership() == ValueOwnership::Owned; }
bool isIsolated() const { return value.contains(Isolated); }
bool isCompileTimeConst() const { return value.contains(CompileTimeConst); }
bool isKnownToBeLocal() const { return value.contains(KnownToBeLocal); }
bool isNoDerivative() const { return value.contains(NoDerivative); }

ValueOwnership getValueOwnership() const {
Expand Down Expand Up @@ -2074,6 +2078,12 @@ class ParameterTypeFlags {
: value - ParameterTypeFlags::Isolated);
}

ParameterTypeFlags withKnownToBeLocal(bool local) const {
return ParameterTypeFlags(local
? value | ParameterTypeFlags::KnownToBeLocal
: value - ParameterTypeFlags::KnownToBeLocal);
}

ParameterTypeFlags withNoDerivative(bool noDerivative) const {
return ParameterTypeFlags(noDerivative
? value | ParameterTypeFlags::NoDerivative
Expand Down Expand Up @@ -2147,7 +2157,8 @@ class YieldTypeFlags {
return ParameterTypeFlags(/*variadic*/ false,
/*autoclosure*/ false,
/*nonEphemeral*/ false, getValueOwnership(),
/*isolated*/ false, /*noDerivative*/ false,
/*isolated*/ false, /*knownToBeLocal*/false,
/*noDerivative*/ false,
/*compileTimeConst*/false);
}

Expand Down Expand Up @@ -6625,8 +6636,8 @@ inline TupleTypeElt TupleTypeElt::getWithType(Type T) const {
/// Create one from what's present in the parameter decl and type
inline ParameterTypeFlags ParameterTypeFlags::fromParameterType(
Type paramTy, bool isVariadic, bool isAutoClosure, bool isNonEphemeral,
ValueOwnership ownership, bool isolated, bool isNoDerivative,
bool compileTimeConst) {
ValueOwnership ownership, bool isolated, bool knownToBeLocal,
bool isNoDerivative, bool compileTimeConst) {
// FIXME(Remove InOut): The last caller that needs this is argument
// decomposition. Start by enabling the assertion there and fixing up those
// callers, then remove this, then remove
Expand All @@ -6636,8 +6647,8 @@ inline ParameterTypeFlags ParameterTypeFlags::fromParameterType(
ownership == ValueOwnership::InOut);
ownership = ValueOwnership::InOut;
}
return {isVariadic, isAutoClosure, isNonEphemeral, ownership, isolated,
isNoDerivative, compileTimeConst};
return {isVariadic, isAutoClosure, isNonEphemeral, ownership,
isolated, knownToBeLocal, isNoDerivative, compileTimeConst};
}

inline const Type *BoundGenericType::getTrailingObjectsPointer() const {
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,8 @@ return {}; // Not Implemented!
hasParamFlags = true;
break;

// TODO: demangle _local as well?

case NodeKind::AutoClosureType:
case NodeKind::EscapingAutoClosureType:
param.setAutoClosure();
Expand Down
10 changes: 9 additions & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,22 +1121,26 @@ class Parser {
ParserStatus parseTypeAttributeList(ParamDecl::Specifier &Specifier,
SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc,
SourceLoc &KnownToBeLocalLoc,
SourceLoc &ConstLoc,
TypeAttributes &Attributes) {
if (Tok.isAny(tok::at_sign, tok::kw_inout) ||
(Tok.is(tok::identifier) &&
(Tok.getRawText().equals("__shared") ||
Tok.getRawText().equals("__owned") ||
Tok.isContextualKeyword("isolated") ||
Tok.isContextualKeyword("_local") ||
Tok.isContextualKeyword("_const"))))
return parseTypeAttributeListPresent(
Specifier, SpecifierLoc, IsolatedLoc, ConstLoc, Attributes);
Specifier, SpecifierLoc, IsolatedLoc, KnownToBeLocalLoc, ConstLoc,
Attributes);
return makeParserSuccess();
}

ParserStatus parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc,
SourceLoc &KnownToBeLocalLoc,
SourceLoc &ConstLoc,
TypeAttributes &Attributes);

Expand Down Expand Up @@ -1322,6 +1326,7 @@ class Parser {
ParamDecl::Specifier Specifier,
SourceLoc SpecifierLoc,
SourceLoc IsolatedLoc,
SourceLoc KnownToBeLocalLoc,
SourceLoc ConstLoc);

//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -1387,6 +1392,9 @@ class Parser {
/// The location of the '_const' keyword, if present.
SourceLoc CompileConstLoc;

/// The location of the '_local' keyword, if present.
SourceLoc KnownToBeLocalLoc;

/// The type following the ':'.
TypeRepr *Type = nullptr;

Expand Down
6 changes: 6 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3096,6 +3096,12 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitKnownToBeLocalTypeRepr(KnownToBeLocalTypeRepr *T) {
printCommon("_local") << '\n';
printRec(T->getBase());
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T) {
printCommon("_const") << '\n';
printRec(T->getBase());
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,9 @@ static void printParameterFlags(ASTPrinter &printer,
if (flags.isIsolated())
printer.printKeyword("isolated", options, " ");

if (flags.isKnownToBeLocal())
printer.printKeyword("_local", options, " ");

if (!options.excludeAttrKind(TAK_escaping) && escaping)
printer.printKeyword("@escaping", options, " ");

Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,10 @@ bool Traversal::visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T) {
return doIt(T->getBase());
}

bool Traversal::visitKnownToBeLocalTypeRepr(KnownToBeLocalTypeRepr *T) {
return doIt(T->getBase());
}

bool Traversal::visitOpaqueReturnTypeRepr(OpaqueReturnTypeRepr *T) {
return doIt(T->getConstraint());
}
Expand Down
8 changes: 6 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ bool AbstractStorageDecl::isCompileTimeConst() const {
return getAttrs().hasAttribute<CompileTimeConstAttr>();
}

bool AbstractStorageDecl::isKnownToBeLocal() const {
return getAttrs().hasAttribute<KnownToBeLocalAttr>();
}

bool AbstractStorageDecl::isTransparent() const {
return getAttrs().hasAttribute<TransparentAttr>();
}
Expand Down Expand Up @@ -6984,8 +6988,8 @@ AnyFunctionType::Param ParamDecl::toFunctionParam(Type type) const {
auto internalLabel = getParameterName();
auto flags = ParameterTypeFlags::fromParameterType(
type, isVariadic(), isAutoClosure(), isNonEphemeral(),
getValueOwnership(), isIsolated(), /*isNoDerivative*/ false,
isCompileTimeConst());
getValueOwnership(), isIsolated(), isKnownToBeLocal(),
/*isNoDerivative*/ false, isCompileTimeConst());
return AnyFunctionType::Param(type, label, flags, internalLabel);
}

Expand Down
1 change: 1 addition & 0 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,7 @@ directReferencesForTypeRepr(Evaluator &evaluator,
case TypeReprKind::InOut:
case TypeReprKind::Isolated:
case TypeReprKind::CompileTimeConst:
case TypeReprKind::KnownToBeLocal:
case TypeReprKind::Metatype:
case TypeReprKind::Owned:
case TypeReprKind::Protocol:
Expand Down
Loading