Skip to content

Commit 9d5ca00

Browse files
committed
[NFC] Replace calls of inferLifetimeDependenceKindFromType and isCompatibleOwnership to inferLifetimeDependenceKind
1 parent 98ce87c commit 9d5ca00

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
@@ -112,16 +112,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
112112
}
113113
}
114114

115-
// Infer the kind of dependence that would be implied by assigning into a stored
116-
// property of 'sourceType'.
117-
static LifetimeDependenceKind
118-
inferLifetimeDependenceKindFromType(Type sourceType) {
119-
if (sourceType->isEscapable()) {
120-
return LifetimeDependenceKind::Scope;
121-
}
122-
return LifetimeDependenceKind::Inherit;
123-
}
124-
125115
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
126116
// be called for a Setter's 'self'.
127117
static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
@@ -803,15 +793,35 @@ class LifetimeDependenceChecker {
803793
return;
804794
}
805795
}
806-
auto kind = inferLifetimeDependenceKindFromType(selfTypeInContext);
807-
auto selfOwnership = afd->getImplicitSelfDecl()->getValueOwnership();
808-
if (!isCompatibleWithOwnership(kind, selfTypeInContext, selfOwnership)) {
796+
auto kind = inferLifetimeDependenceKind(
797+
selfTypeInContext, afd->getImplicitSelfDecl()->getValueOwnership());
798+
if (!kind) {
809799
diagnose(returnLoc,
810800
diag::lifetime_dependence_cannot_infer_scope_ownership,
811801
"self", diagnosticQualifier());
812802
return;
813803
}
814-
pushDeps(createDeps(resultIndex).add(selfIndex, kind));
804+
pushDeps(createDeps(resultIndex).add(selfIndex, *kind));
805+
}
806+
807+
std::optional<LifetimeDependenceKind>
808+
inferLifetimeDependenceKind(Type sourceType, ValueOwnership ownership) {
809+
if (!sourceType->isEscapable()) {
810+
return LifetimeDependenceKind::Inherit;
811+
}
812+
// Lifetime dependence always propagates through temporary BitwiseCopyable
813+
// values, even if the dependence is scoped.
814+
if (isBitwiseCopyable(sourceType, ctx)) {
815+
return LifetimeDependenceKind::Scope;
816+
}
817+
auto loweredOwnership = ownership != ValueOwnership::Default
818+
? ownership
819+
: getLoweredOwnership(afd);
820+
if (loweredOwnership != ValueOwnership::Shared &&
821+
loweredOwnership != ValueOwnership::InOut) {
822+
return std::nullopt;
823+
}
824+
return LifetimeDependenceKind::Scope;
815825
}
816826

817827
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -835,16 +845,15 @@ class LifetimeDependenceChecker {
835845
if (paramTypeInContext->hasError()) {
836846
continue;
837847
}
838-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
839-
auto paramOwnership = param->getValueOwnership();
840-
if (!isCompatibleWithOwnership(kind, paramTypeInContext, paramOwnership))
841-
{
848+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
849+
param->getValueOwnership());
850+
if (!kind) {
842851
diagnose(returnLoc,
843852
diag::lifetime_dependence_cannot_infer_scope_ownership,
844853
param->getParameterName().str(), diagnosticQualifier());
845854
continue;
846855
}
847-
targetDeps = std::move(targetDeps).add(paramIndex, kind);
856+
targetDeps = std::move(targetDeps).add(paramIndex, *kind);
848857
}
849858
pushDeps(std::move(targetDeps));
850859
}
@@ -928,9 +937,8 @@ class LifetimeDependenceChecker {
928937
}
929938

930939
candidateLifetimeKind =
931-
inferLifetimeDependenceKindFromType(paramTypeInContext);
932-
if (!isCompatibleWithOwnership(
933-
*candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
940+
inferLifetimeDependenceKind(paramTypeInContext, paramOwnership);
941+
if (!candidateLifetimeKind) {
934942
continue;
935943
}
936944
if (candidateParamIndex) {
@@ -999,11 +1007,12 @@ class LifetimeDependenceChecker {
9991007
if (paramTypeInContext->hasError()) {
10001008
return;
10011009
}
1002-
auto kind = inferLifetimeDependenceKindFromType(paramTypeInContext);
1010+
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
1011+
param->getValueOwnership());
10031012

10041013
pushDeps(createDeps(selfIndex)
1005-
.add(selfIndex, LifetimeDependenceKind::Inherit)
1006-
.add(newValIdx, kind));
1014+
.add(selfIndex, LifetimeDependenceKind::Inherit)
1015+
.add(newValIdx, *kind));
10071016
break;
10081017
}
10091018
default:

0 commit comments

Comments
 (0)