Skip to content

Commit e976584

Browse files
committed
Revert "Ensure that we do not turn rvalues into lvalues"
This reverts commit 4b59301.
1 parent 613b321 commit e976584

File tree

3 files changed

+22
-50
lines changed

3 files changed

+22
-50
lines changed

include/swift/AST/Decl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,8 +5757,9 @@ class AbstractStorageDecl : public ValueDecl {
57575757
/// Determine whether references to this storage declaration may appear
57585758
/// on the left-hand side of an assignment, as the operand of a
57595759
/// `&` or 'inout' operator, or as a component in a writable key path.
5760-
bool isSettable(const DeclContext *useDC) const {
5761-
switch (mutability(useDC)) {
5760+
bool isSettable(const DeclContext *useDC,
5761+
const DeclRefExpr *base = nullptr) const {
5762+
switch (mutability(useDC, base)) {
57625763
case StorageMutability::Immutable:
57635764
return false;
57645765
case StorageMutability::Mutable:
@@ -5769,9 +5770,8 @@ class AbstractStorageDecl : public ValueDecl {
57695770

57705771
/// Determine the mutability of this storage declaration when
57715772
/// accessed from a given declaration context.
5772-
StorageMutability mutability(
5773-
const DeclContext *useDC,
5774-
std::optional<const DeclRefExpr *> base = std::nullopt) const;
5773+
StorageMutability mutability(const DeclContext *useDC,
5774+
const DeclRefExpr *base = nullptr) const;
57755775

57765776
/// Determine the mutability of this storage declaration when
57775777
/// accessed from a given declaration context in Swift.
@@ -5781,7 +5781,7 @@ class AbstractStorageDecl : public ValueDecl {
57815781
/// writes in Swift.
57825782
StorageMutability mutabilityInSwift(
57835783
const DeclContext *useDC,
5784-
std::optional<const DeclRefExpr *> base = std::nullopt) const;
5784+
const DeclRefExpr *base = nullptr) const;
57855785

57865786
/// Determine whether references to this storage declaration in Swift may
57875787
/// appear on the left-hand side of an assignment, as the operand of a
@@ -5790,8 +5790,9 @@ class AbstractStorageDecl : public ValueDecl {
57905790
/// This method is equivalent to \c isSettable with the exception of
57915791
/// 'optional' storage requirements, which lack support for direct writes
57925792
/// in Swift.
5793-
bool isSettableInSwift(const DeclContext *useDC) const {
5794-
switch (mutabilityInSwift(useDC)) {
5793+
bool isSettableInSwift(const DeclContext *useDC,
5794+
const DeclRefExpr *base = nullptr) const {
5795+
switch (mutabilityInSwift(useDC, base)) {
57955796
case StorageMutability::Immutable:
57965797
return false;
57975798
case StorageMutability::Mutable:
@@ -6113,9 +6114,8 @@ class VarDecl : public AbstractStorageDecl {
61136114

61146115
/// Determine the mutability of this variable declaration when
61156116
/// accessed from a given declaration context.
6116-
StorageMutability mutability(
6117-
const DeclContext *useDC,
6118-
std::optional<const DeclRefExpr *> base = std::nullopt) const;
6117+
StorageMutability mutability(const DeclContext *useDC,
6118+
const DeclRefExpr *base = nullptr) const;
61196119

61206120
/// Return the parent pattern binding that may provide an initializer for this
61216121
/// VarDecl. This returns null if there is none associated with the VarDecl.

lib/AST/Decl.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,7 +3093,7 @@ bool AbstractStorageDecl::isSetterMutating() const {
30933093

30943094
StorageMutability
30953095
AbstractStorageDecl::mutability(const DeclContext *useDC,
3096-
std::optional<const DeclRefExpr *> base ) const {
3096+
const DeclRefExpr *base) const {
30973097
if (auto vd = dyn_cast<VarDecl>(this))
30983098
return vd->mutability(useDC, base);
30993099

@@ -3109,10 +3109,8 @@ AbstractStorageDecl::mutability(const DeclContext *useDC,
31093109
/// 'optional' storage requirements, which lack support for direct
31103110
/// writes in Swift.
31113111
StorageMutability
3112-
AbstractStorageDecl::mutabilityInSwift(
3113-
const DeclContext *useDC,
3114-
std::optional<const DeclRefExpr *> base
3115-
) const {
3112+
AbstractStorageDecl::mutabilityInSwift(const DeclContext *useDC,
3113+
const DeclRefExpr *base) const {
31163114
// TODO: Writing to an optional storage requirement is not supported in Swift.
31173115
if (getAttrs().hasAttribute<OptionalAttr>()) {
31183116
return StorageMutability::Immutable;
@@ -7302,7 +7300,7 @@ static StorageMutability storageIsMutable(bool isMutable) {
73027300
/// is a let member in an initializer.
73037301
StorageMutability
73047302
VarDecl::mutability(const DeclContext *UseDC,
7305-
std::optional<const DeclRefExpr *> base) const {
7303+
const DeclRefExpr *base) const {
73067304
// Parameters are settable or not depending on their ownership convention.
73077305
if (auto *PD = dyn_cast<ParamDecl>(this))
73087306
return storageIsMutable(!PD->isImmutableInFunctionBody());
@@ -7312,12 +7310,9 @@ VarDecl::mutability(const DeclContext *UseDC,
73127310
if (!isLet()) {
73137311
if (hasInitAccessor()) {
73147312
if (auto *ctor = dyn_cast_or_null<ConstructorDecl>(UseDC)) {
7315-
// If we're referencing 'self.', it's initializable.
7316-
if (!base ||
7317-
(*base && ctor->getImplicitSelfDecl() == (*base)->getDecl()))
7318-
return StorageMutability::Initializable;
7319-
7320-
return storageIsMutable(supportsMutation());
7313+
if (base && ctor->getImplicitSelfDecl() != base->getDecl())
7314+
return storageIsMutable(supportsMutation());
7315+
return StorageMutability::Initializable;
73217316
}
73227317
}
73237318

@@ -7375,18 +7370,17 @@ VarDecl::mutability(const DeclContext *UseDC,
73757370
getDeclContext()->getSelfNominalTypeDecl())
73767371
return StorageMutability::Immutable;
73777372

7373+
if (base && CD->getImplicitSelfDecl() != base->getDecl())
7374+
return StorageMutability::Immutable;
7375+
73787376
// If this is a convenience initializer (i.e. one that calls
73797377
// self.init), then let properties are never mutable in it. They are
73807378
// only mutable in designated initializers.
73817379
auto initKindAndExpr = CD->getDelegatingOrChainedInitKind();
73827380
if (initKindAndExpr.initKind == BodyInitKind::Delegating)
73837381
return StorageMutability::Immutable;
73847382

7385-
// If we were given a base and it is 'self', it's initializable.
7386-
if (!base || (*base && CD->getImplicitSelfDecl() == (*base)->getDecl()))
7387-
return StorageMutability::Initializable;
7388-
7389-
return StorageMutability::Immutable;
7383+
return StorageMutability::Initializable;
73907384
}
73917385

73927386
// If the 'let' has a value bound to it but has no PBD, then it is

test/decl/init/let-mutability.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)