@@ -143,16 +143,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
143
143
}
144
144
}
145
145
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
-
156
146
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
157
147
// be called for a Setter's 'self'.
158
148
static ValueOwnership getLoweredOwnership (AbstractFunctionDecl *afd) {
@@ -834,15 +824,35 @@ class LifetimeDependenceChecker {
834
824
return ;
835
825
}
836
826
}
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) {
840
830
diagnose (returnLoc,
841
831
diag::lifetime_dependence_cannot_infer_scope_ownership,
842
832
" self" , diagnosticQualifier ());
843
833
return ;
844
834
}
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;
846
856
}
847
857
848
858
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -866,16 +876,15 @@ class LifetimeDependenceChecker {
866
876
if (paramTypeInContext->hasError ()) {
867
877
continue ;
868
878
}
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) {
873
882
diagnose (returnLoc,
874
883
diag::lifetime_dependence_cannot_infer_scope_ownership,
875
884
param->getParameterName ().str (), diagnosticQualifier ());
876
885
continue ;
877
886
}
878
- targetDeps = std::move (targetDeps).add (paramIndex, kind);
887
+ targetDeps = std::move (targetDeps).add (paramIndex, * kind);
879
888
}
880
889
pushDeps (std::move (targetDeps));
881
890
}
@@ -959,9 +968,8 @@ class LifetimeDependenceChecker {
959
968
}
960
969
961
970
candidateLifetimeKind =
962
- inferLifetimeDependenceKindFromType (paramTypeInContext);
963
- if (!isCompatibleWithOwnership (
964
- *candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
971
+ inferLifetimeDependenceKind (paramTypeInContext, paramOwnership);
972
+ if (!candidateLifetimeKind) {
965
973
continue ;
966
974
}
967
975
if (candidateParamIndex) {
@@ -1037,11 +1045,12 @@ class LifetimeDependenceChecker {
1037
1045
if (paramTypeInContext->hasError ()) {
1038
1046
return ;
1039
1047
}
1040
- auto kind = inferLifetimeDependenceKindFromType (paramTypeInContext);
1048
+ auto kind = inferLifetimeDependenceKind (paramTypeInContext,
1049
+ param->getValueOwnership ());
1041
1050
1042
1051
pushDeps (createDeps (selfIndex)
1043
- .add (selfIndex, LifetimeDependenceKind::Inherit)
1044
- .add (newValIdx, kind));
1052
+ .add (selfIndex, LifetimeDependenceKind::Inherit)
1053
+ .add (newValIdx, * kind));
1045
1054
break ;
1046
1055
}
1047
1056
case AccessorKind::MutableAddress:
0 commit comments