Skip to content

Commit 49b706a

Browse files
Added experimental feature to allow weak let and immutable weak captures
1 parent a8c5389 commit 49b706a

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
@@ -511,6 +511,9 @@ SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)
511511
/// Allow use of `Module::name` syntax
512512
EXPERIMENTAL_FEATURE(ModuleSelector, false)
513513

514+
/// Allow `weak let` and make weak captures immutable
515+
EXPERIMENTAL_FEATURE(WeakLet, true)
516+
514517
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
515518
#undef EXPERIMENTAL_FEATURE
516519
#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
@@ -624,6 +624,15 @@ static bool usesFeatureExtensibleAttribute(Decl *decl) {
624624
return decl->getAttrs().hasAttribute<ExtensibleAttr>();
625625
}
626626

627+
static bool usesFeatureWeakLet(Decl *decl) {
628+
if (auto *VD = dyn_cast<VarDecl>(decl)) {
629+
if (auto *refAttr = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
630+
return VD->isLet() && refAttr->get() == ReferenceOwnership::Weak;
631+
}
632+
}
633+
return false;
634+
}
635+
627636
// ----------------------------------------------------------------------------
628637
// MARK: - FeatureSet
629638
// ----------------------------------------------------------------------------

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5285,7 +5285,7 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
52855285
case ReferenceOwnershipOptionality::Allowed:
52865286
break;
52875287
case ReferenceOwnershipOptionality::Required:
5288-
if (var->isLet()) {
5288+
if (var->isLet() && !ctx.LangOpts.hasFeature(Feature::WeakLet)) {
52895289
var->diagnose(diag::invalid_ownership_is_let, ownershipKind);
52905290
attr->setInvalid();
52915291
}

0 commit comments

Comments
 (0)