Skip to content

Commit aa3edb4

Browse files
committed
[NFC] Replace calls of inferLifetimeDependenceKindFromType and isCompatibleOwnership to inferLifetimeDependenceKind
1 parent 7be7640 commit aa3edb4

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
143143
}
144144
}
145145

146-
// Infer the kind of dependence that would be implied by assigning into a stored
147-
// property of 'sourceType'.
148-
static LifetimeDependenceKind
149-
inferLifetimeDependenceKindFromType(Type sourceType) {
150-
if (sourceType->isEscapable()) {
151-
return LifetimeDependenceKind::Scope;
152-
}
153-
return LifetimeDependenceKind::Inherit;
154-
}
155-
156146
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
157147
// be called for a Setter's 'self'.
158148
static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
@@ -834,15 +824,35 @@ class LifetimeDependenceChecker {
834824
return;
835825
}
836826
}
837-
auto kind = inferLifetimeDependenceKindFromType(selfTypeInContext);
838-
auto selfOwnership = afd->getImplicitSelfDecl()->getValueOwnership();
839-
if (!isCompatibleWithOwnership(kind, selfTypeInContext, selfOwnership)) {
827+
auto kind = inferLifetimeDependenceKind(
828+
selfTypeInContext, afd->getImplicitSelfDecl()->getValueOwnership());
829+
if (!kind) {
840830
diagnose(returnLoc,
841831
diag::lifetime_dependence_cannot_infer_scope_ownership,
842832
"self", diagnosticQualifier());
843833
return;
844834
}
845-
pushDeps(createDeps(resultIndex).add(selfIndex, kind));
835+
pushDeps(createDeps(resultIndex).add(selfIndex, *kind));
836+
}
837+
838+
std::optional<LifetimeDependenceKind>
839+
inferLifetimeDependenceKind(Type sourceType, ValueOwnership ownership) {
840+
if (!sourceType->isEscapable()) {
841+
return LifetimeDependenceKind::Inherit;
842+
}
843+
// Lifetime dependence always propagates through temporary BitwiseCopyable
844+
// values, even if the dependence is scoped.
845+
if (isBitwiseCopyable(sourceType, ctx)) {
846+
return LifetimeDependenceKind::Scope;
847+
}
848+
auto loweredOwnership = ownership != ValueOwnership::Default
849+
? ownership
850+
: getLoweredOwnership(afd);
851+
if (loweredOwnership != ValueOwnership::Shared &&
852+
loweredOwnership != ValueOwnership::InOut) {
853+
return std::nullopt;
854+
}
855+
return LifetimeDependenceKind::Scope;
846856
}
847857

848858
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -866,16 +876,15 @@ class LifetimeDependenceChecker {
866876
if (paramTypeInContext->hasError()) {
867877
continue;
868878
}
869-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
870-
auto paramOwnership = param->getValueOwnership();
871-
if (!isCompatibleWithOwnership(kind, paramTypeInContext, paramOwnership))
872-
{
879+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
880+
param->getValueOwnership());
881+
if (!kind) {
873882
diagnose(returnLoc,
874883
diag::lifetime_dependence_cannot_infer_scope_ownership,
875884
param->getParameterName().str(), diagnosticQualifier());
876885
continue;
877886
}
878-
targetDeps = std::move(targetDeps).add(paramIndex, kind);
887+
targetDeps = std::move(targetDeps).add(paramIndex, *kind);
879888
}
880889
pushDeps(std::move(targetDeps));
881890
}
@@ -959,9 +968,8 @@ class LifetimeDependenceChecker {
959968
}
960969

961970
candidateLifetimeKind =
962-
inferLifetimeDependenceKindFromType(paramTypeInContext);
963-
if (!isCompatibleWithOwnership(
964-
*candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
971+
inferLifetimeDependenceKind(paramTypeInContext, paramOwnership);
972+
if (!candidateLifetimeKind) {
965973
continue;
966974
}
967975
if (candidateParamIndex) {
@@ -1037,11 +1045,12 @@ class LifetimeDependenceChecker {
10371045
if (paramTypeInContext->hasError()) {
10381046
return;
10391047
}
1040-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
1048+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
1049+
param->getValueOwnership());
10411050

10421051
pushDeps(createDeps(selfIndex)
1043-
.add(selfIndex, LifetimeDependenceKind::Inherit)
1044-
.add(newValIdx, kind));
1052+
.add(selfIndex, LifetimeDependenceKind::Inherit)
1053+
.add(newValIdx, *kind));
10451054
break;
10461055
}
10471056
case AccessorKind::MutableAddress:

0 commit comments

Comments
 (0)