Skip to content

[6.2] Fix LifetimeDependence type inference for setters. #81271

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
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
19 changes: 13 additions & 6 deletions lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,19 @@ class LifetimeDependenceChecker {
if (paramTypeInContext->hasError()) {
return;
}
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
param->getValueOwnership());

pushDeps(createDeps(selfIndex)
.add(selfIndex, LifetimeDependenceKind::Inherit)
.add(newValIdx, *kind));
auto targetDeps =
createDeps(selfIndex).add(selfIndex, LifetimeDependenceKind::Inherit);

// The 'newValue' dependence kind must match the getter's dependence kind
// because generated the implementation '_modify' accessor composes the
// getter's result with the setter's 'newValue'. In particular, if the
// result type is Escapable then the getter does not have any lifetime
// dependency, so the setter cannot depend on 'newValue'.
if (!paramTypeInContext->isEscapable()) {
targetDeps = std::move(targetDeps)
.add(newValIdx, LifetimeDependenceKind::Inherit);
}
pushDeps(std::move(targetDeps));
break;
}
case AccessorKind::MutableAddress:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ extension NE {
return NE(pointer: _pointer)
}
}

public struct NCNE<Element>: ~Swift.Copyable & ~Swift.Escapable {
var e: Element
}

extension NCNE where Element : Swift.BitwiseCopyable {
// Ensure that lifetime dependence diagnostics accessp the generated _modify accessor:
// the getter dependency must match the setter's mewValue dependence.
// In this case, the getter has no dependency because the result is BitwiseCopyable. The setter cannot, therefore,
// have a borrow dependency no 'newValue' which is produced by the getter.
public subscript() -> Element {
get {
return e
}
//@lifetime(self: copy self, self: copy newValue)
set {
e = newValue
}
}
}
#endif