@@ -112,16 +112,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
112
112
}
113
113
}
114
114
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
-
125
115
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
126
116
// be called for a Setter's 'self'.
127
117
static ValueOwnership getLoweredOwnership (AbstractFunctionDecl *afd) {
@@ -803,15 +793,35 @@ class LifetimeDependenceChecker {
803
793
return ;
804
794
}
805
795
}
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) {
809
799
diagnose (returnLoc,
810
800
diag::lifetime_dependence_cannot_infer_scope_ownership,
811
801
" self" , diagnosticQualifier ());
812
802
return ;
813
803
}
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;
815
825
}
816
826
817
827
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -835,16 +845,15 @@ class LifetimeDependenceChecker {
835
845
if (paramTypeInContext->hasError ()) {
836
846
continue ;
837
847
}
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) {
842
851
diagnose (returnLoc,
843
852
diag::lifetime_dependence_cannot_infer_scope_ownership,
844
853
param->getParameterName ().str (), diagnosticQualifier ());
845
854
continue ;
846
855
}
847
- targetDeps = std::move (targetDeps).add (paramIndex, kind);
856
+ targetDeps = std::move (targetDeps).add (paramIndex, * kind);
848
857
}
849
858
pushDeps (std::move (targetDeps));
850
859
}
@@ -928,9 +937,8 @@ class LifetimeDependenceChecker {
928
937
}
929
938
930
939
candidateLifetimeKind =
931
- inferLifetimeDependenceKindFromType (paramTypeInContext);
932
- if (!isCompatibleWithOwnership (
933
- *candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
940
+ inferLifetimeDependenceKind (paramTypeInContext, paramOwnership);
941
+ if (!candidateLifetimeKind) {
934
942
continue ;
935
943
}
936
944
if (candidateParamIndex) {
@@ -999,11 +1007,12 @@ class LifetimeDependenceChecker {
999
1007
if (paramTypeInContext->hasError ()) {
1000
1008
return ;
1001
1009
}
1002
- auto kind = inferLifetimeDependenceKindFromType (paramTypeInContext);
1010
+ auto kind = inferLifetimeDependenceKind (paramTypeInContext,
1011
+ param->getValueOwnership ());
1003
1012
1004
1013
pushDeps (createDeps (selfIndex)
1005
- .add (selfIndex, LifetimeDependenceKind::Inherit)
1006
- .add (newValIdx, kind));
1014
+ .add (selfIndex, LifetimeDependenceKind::Inherit)
1015
+ .add (newValIdx, * kind));
1007
1016
break ;
1008
1017
}
1009
1018
default :
0 commit comments