Skip to content

Commit 245915d

Browse files
committed
[NFC] Replace calls of inferLifetimeDependenceKindFromType and isCompatibleOwnership to inferLifetimeDependenceKind
1 parent 562d7dc commit 245915d

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
@@ -138,16 +138,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
138138
}
139139
}
140140

141-
// Infer the kind of dependence that would be implied by assigning into a stored
142-
// property of 'sourceType'.
143-
static LifetimeDependenceKind
144-
inferLifetimeDependenceKindFromType(Type sourceType) {
145-
if (sourceType->isEscapable()) {
146-
return LifetimeDependenceKind::Scope;
147-
}
148-
return LifetimeDependenceKind::Inherit;
149-
}
150-
151141
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
152142
// be called for a Setter's 'self'.
153143
static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
@@ -829,15 +819,35 @@ class LifetimeDependenceChecker {
829819
return;
830820
}
831821
}
832-
auto kind = inferLifetimeDependenceKindFromType(selfTypeInContext);
833-
auto selfOwnership = afd->getImplicitSelfDecl()->getValueOwnership();
834-
if (!isCompatibleWithOwnership(kind, selfTypeInContext, selfOwnership)) {
822+
auto kind = inferLifetimeDependenceKind(
823+
selfTypeInContext, afd->getImplicitSelfDecl()->getValueOwnership());
824+
if (!kind) {
835825
diagnose(returnLoc,
836826
diag::lifetime_dependence_cannot_infer_scope_ownership,
837827
"self", diagnosticQualifier());
838828
return;
839829
}
840-
pushDeps(createDeps(resultIndex).add(selfIndex, kind));
830+
pushDeps(createDeps(resultIndex).add(selfIndex, *kind));
831+
}
832+
833+
std::optional<LifetimeDependenceKind>
834+
inferLifetimeDependenceKind(Type sourceType, ValueOwnership ownership) {
835+
if (!sourceType->isEscapable()) {
836+
return LifetimeDependenceKind::Inherit;
837+
}
838+
// Lifetime dependence always propagates through temporary BitwiseCopyable
839+
// values, even if the dependence is scoped.
840+
if (isBitwiseCopyable(sourceType, ctx)) {
841+
return LifetimeDependenceKind::Scope;
842+
}
843+
auto loweredOwnership = ownership != ValueOwnership::Default
844+
? ownership
845+
: getLoweredOwnership(afd);
846+
if (loweredOwnership != ValueOwnership::Shared &&
847+
loweredOwnership != ValueOwnership::InOut) {
848+
return std::nullopt;
849+
}
850+
return LifetimeDependenceKind::Scope;
841851
}
842852

843853
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -861,16 +871,15 @@ class LifetimeDependenceChecker {
861871
if (paramTypeInContext->hasError()) {
862872
continue;
863873
}
864-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
865-
auto paramOwnership = param->getValueOwnership();
866-
if (!isCompatibleWithOwnership(kind, paramTypeInContext, paramOwnership))
867-
{
874+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
875+
param->getValueOwnership());
876+
if (!kind) {
868877
diagnose(returnLoc,
869878
diag::lifetime_dependence_cannot_infer_scope_ownership,
870879
param->getParameterName().str(), diagnosticQualifier());
871880
continue;
872881
}
873-
targetDeps = std::move(targetDeps).add(paramIndex, kind);
882+
targetDeps = std::move(targetDeps).add(paramIndex, *kind);
874883
}
875884
pushDeps(std::move(targetDeps));
876885
}
@@ -954,9 +963,8 @@ class LifetimeDependenceChecker {
954963
}
955964

956965
candidateLifetimeKind =
957-
inferLifetimeDependenceKindFromType(paramTypeInContext);
958-
if (!isCompatibleWithOwnership(
959-
*candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
966+
inferLifetimeDependenceKind(paramTypeInContext, paramOwnership);
967+
if (!candidateLifetimeKind) {
960968
continue;
961969
}
962970
if (candidateParamIndex) {
@@ -1025,11 +1033,12 @@ class LifetimeDependenceChecker {
10251033
if (paramTypeInContext->hasError()) {
10261034
return;
10271035
}
1028-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
1036+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
1037+
param->getValueOwnership());
10291038

10301039
pushDeps(createDeps(selfIndex)
1031-
.add(selfIndex, LifetimeDependenceKind::Inherit)
1032-
.add(newValIdx, kind));
1040+
.add(selfIndex, LifetimeDependenceKind::Inherit)
1041+
.add(newValIdx, *kind));
10331042
break;
10341043
}
10351044
default:

0 commit comments

Comments
 (0)