Skip to content

Commit 9cd8fa3

Browse files
Added experimental feature to allow weak let and immutable weak captures
1 parent fcec1cd commit 9cd8fa3

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ EXPERIMENTAL_FEATURE(ClosureBodyMacro, true)
519519
/// Allow declarations of Swift runtime symbols using @_silgen_name.
520520
EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)
521521

522+
/// Allow `weak let` and make weak captures immutable
523+
EXPERIMENTAL_FEATURE(WeakLet, true)
524+
522525
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
523526
#undef EXPERIMENTAL_FEATURE
524527
#undef UPCOMING_FEATURE

lib/AST/Expr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,8 @@ CaptureListEntry CaptureListEntry::createParsed(
13661366
SourceRange ownershipRange, Identifier name, SourceLoc nameLoc,
13671367
SourceLoc equalLoc, Expr *initializer, DeclContext *DC) {
13681368

1369-
auto introducer =
1370-
(ownershipKind != ReferenceOwnership::Weak ? VarDecl::Introducer::Let
1371-
: VarDecl::Introducer::Var);
1369+
bool forceVar = ownershipKind == ReferenceOwnership::Weak && !Ctx.LangOpts.hasFeature(Feature::WeakLet);
1370+
auto introducer = forceVar ? VarDecl::Introducer::Var : VarDecl::Introducer::Let;
13721371
auto *VD =
13731372
new (Ctx) VarDecl(/*isStatic==*/false, introducer, nameLoc, name, DC);
13741373

lib/AST/FeatureSet.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,15 @@ static bool usesFeatureExecutionAttribute(Decl *decl) {
543543
return false;
544544
}
545545

546+
static bool usesFeatureWeakLet(Decl *decl) {
547+
if (auto *VD = dyn_cast<VarDecl>(decl)) {
548+
if (auto *refAttr = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
549+
return VD->isLet() && refAttr->get() == ReferenceOwnership::Weak;
550+
}
551+
}
552+
return false;
553+
}
554+
546555
// ----------------------------------------------------------------------------
547556
// MARK: - FeatureSet
548557
// ----------------------------------------------------------------------------

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5214,7 +5214,7 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
52145214
case ReferenceOwnershipOptionality::Allowed:
52155215
break;
52165216
case ReferenceOwnershipOptionality::Required:
5217-
if (var->isLet()) {
5217+
if (var->isLet() && !ctx.LangOpts.hasFeature(Feature::WeakLet)) {
52185218
var->diagnose(diag::invalid_ownership_is_let, ownershipKind);
52195219
attr->setInvalid();
52205220
}

0 commit comments

Comments
 (0)