Skip to content

[AST] Bring 'mutating' and 'inout self' in sync. #10375

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

Merged
merged 1 commit into from
Jun 19, 2017
Merged
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
22 changes: 22 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,28 @@ class Verifier : public ASTWalker {
}
}

if (FD->isMutating()) {
if (!FD->isInstanceMember()) {
Out << "mutating function is not an instance member\n";
abort();
}
if (FD->getDeclContext()->getAsClassOrClassExtensionContext()) {
Out << "mutating function in a class\n";
abort();
}
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
if (!selfParam->getInterfaceType()->is<InOutType>()) {
Out << "mutating function does not have inout 'self'\n";
abort();
}
} else {
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
if (selfParam && selfParam->getInterfaceType()->is<InOutType>()) {
Out << "non-mutating function has inout 'self'\n";
abort();
}
}

verifyCheckedBase(FD);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3395,7 +3395,9 @@ bool AbstractStorageDecl::isSetterNonMutating() const {
switch (getStorageKind()) {
case AbstractStorageDecl::Stored:
case AbstractStorageDecl::StoredWithTrivialAccessors:
return false;
// Instance member setters are mutating; static property setters and
// top-level setters are not.
return !isInstanceMember();

case AbstractStorageDecl::StoredWithObservers:
case AbstractStorageDecl::InheritedWithObservers:
Expand Down
35 changes: 17 additions & 18 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3418,29 +3418,28 @@ static FuncDecl *createAccessorFunc(SourceLoc DeclLoc, ParameterList *param,
// Non-static set/willSet/didSet/materializeForSet/mutableAddress
// default to mutating. get/address default to
// non-mutating.
if (!D->isStatic()) {
switch (Kind) {
case AccessorKind::IsAddressor:
D->setAddressorKind(addressorKind);
break;
switch (Kind) {
case AccessorKind::IsAddressor:
D->setAddressorKind(addressorKind);
break;

case AccessorKind::IsGetter:
break;
case AccessorKind::IsGetter:
break;

case AccessorKind::IsMutableAddressor:
D->setAddressorKind(addressorKind);
LLVM_FALLTHROUGH;
case AccessorKind::IsMutableAddressor:
D->setAddressorKind(addressorKind);
LLVM_FALLTHROUGH;

case AccessorKind::IsSetter:
case AccessorKind::IsWillSet:
case AccessorKind::IsDidSet:
case AccessorKind::IsSetter:
case AccessorKind::IsWillSet:
case AccessorKind::IsDidSet:
if (D->isInstanceMember())
D->setMutating();
break;
break;

case AccessorKind::IsMaterializeForSet:
case AccessorKind::NotAccessor:
llvm_unreachable("not parseable accessors");
}
case AccessorKind::IsMaterializeForSet:
case AccessorKind::NotAccessor:
llvm_unreachable("not parseable accessors");
}

return D;
Expand Down
15 changes: 6 additions & 9 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,15 @@ static FuncDecl *createMaterializeForSetPrototype(AbstractStorageDecl *storage,
params, TypeLoc::withoutLoc(retTy), DC);
materializeForSet->setImplicit();

bool isStatic = storage->isStatic();

// Open-code the setMutating() calculation since we might run before
// the setter has been type checked. Also as a hack, always mark the
// setter mutating if we're inside a protocol, because it seems some
// things break otherwise -- the root cause should be fixed eventually.
// the setter has been type checked.
Type contextTy = DC->getDeclaredInterfaceType();
materializeForSet->setMutating(
storage->getDeclContext()->getAsProtocolOrProtocolExtensionContext() ||
(!setter->getAttrs().hasAttribute<NonMutatingAttr>() &&
!storage->isSetterNonMutating()));
contextTy && !contextTy->hasReferenceSemantics() &&
!setter->getAttrs().hasAttribute<NonMutatingAttr>() &&
!storage->isSetterNonMutating());

materializeForSet->setStatic(isStatic);
materializeForSet->setStatic(storage->isStatic());

// materializeForSet is final if the storage is.
if (storage->isFinal())
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5037,6 +5037,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
else if (FD->getAttrs().hasAttribute<NonMutatingAttr>())
FD->setMutating(false);

if (FD->isMutating()) {
Type contextTy = FD->getDeclContext()->getDeclaredInterfaceType();
if (contextTy->hasReferenceSemantics())
FD->setMutating(false);
}

// Check whether the return type is dynamic 'Self'.
if (checkDynamicSelfReturn(FD))
FD->setInvalid();
Expand Down
7 changes: 7 additions & 0 deletions test/SILGen/addressors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ struct A {
return base
}
}

static var staticProp: Int32 {
unsafeAddress {
// Just don't trip up the verifier.
fatalError()
}
}
}

// CHECK-LABEL: sil hidden @_T010addressors1AV9subscripts5Int32VAFcflu : $@convention(method) (Int32, A) -> UnsafePointer<Int32>
Expand Down
10 changes: 7 additions & 3 deletions test/SILGen/guaranteed_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,16 @@ struct S: Fooable {
// CHECK-NOT: destroy_addr [[SELF_ADDR]]

// Witness thunk for prop3 nonmutating materializeForSet
// CHECK-LABEL: sil private [transparent] [thunk] @_T015guaranteed_self1SVAA7FooableA2aDP5prop3SifmTW : $@convention(witness_method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout S) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
// CHECK-LABEL: sil private [transparent] [thunk] @_T015guaranteed_self1SVAA7FooableA2aDP5prop3SifmTW : $@convention(witness_method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @in_guaranteed S) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slavapestov Additional sanity check that this change is reasonable? It certainly seems desirable, but I don't want it to break something at the other end of the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense.

// CHECK: bb0({{.*}} [[SELF_ADDR:%.*]] : $*S):
// CHECK: [[SELF:%.*]] = load [copy] [[SELF_ADDR]]
// CHECK: [[SELF_COPY:%.*]] = alloc_stack $S
// CHECK: copy_addr [[SELF_ADDR]] to [initialization] [[SELF_COPY]]
// CHECK: [[SELF:%.*]] = load [take] [[SELF_COPY]]
// CHECK: destroy_value [[SELF]]
// CHECK-NOT: destroy_value [[SELF]]
// CHECK: }
// CHECK-NOT: destroy_addr [[SELF_COPY]]
// CHECK-NOT: destroy_addr [[SELF_ADDR]]
// CHECK: } // end sil function '_T015guaranteed_self1SVAA7FooableA2aDP5prop3SifmTW'

//
// TODO: Expected output for the other cases
Expand Down