@@ -138,16 +138,6 @@ void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
138
138
}
139
139
}
140
140
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
-
151
141
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
152
142
// be called for a Setter's 'self'.
153
143
static ValueOwnership getLoweredOwnership (AbstractFunctionDecl *afd) {
@@ -829,15 +819,35 @@ class LifetimeDependenceChecker {
829
819
return ;
830
820
}
831
821
}
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) {
835
825
diagnose (returnLoc,
836
826
diag::lifetime_dependence_cannot_infer_scope_ownership,
837
827
" self" , diagnosticQualifier ());
838
828
return ;
839
829
}
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;
841
851
}
842
852
843
853
// Infer implicit initialization. The dependence kind can be inferred, similar
@@ -861,16 +871,15 @@ class LifetimeDependenceChecker {
861
871
if (paramTypeInContext->hasError ()) {
862
872
continue ;
863
873
}
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) {
868
877
diagnose (returnLoc,
869
878
diag::lifetime_dependence_cannot_infer_scope_ownership,
870
879
param->getParameterName ().str (), diagnosticQualifier ());
871
880
continue ;
872
881
}
873
- targetDeps = std::move (targetDeps).add (paramIndex, kind);
882
+ targetDeps = std::move (targetDeps).add (paramIndex, * kind);
874
883
}
875
884
pushDeps (std::move (targetDeps));
876
885
}
@@ -954,9 +963,8 @@ class LifetimeDependenceChecker {
954
963
}
955
964
956
965
candidateLifetimeKind =
957
- inferLifetimeDependenceKindFromType (paramTypeInContext);
958
- if (!isCompatibleWithOwnership (
959
- *candidateLifetimeKind, paramTypeInContext, paramOwnership)) {
966
+ inferLifetimeDependenceKind (paramTypeInContext, paramOwnership);
967
+ if (!candidateLifetimeKind) {
960
968
continue ;
961
969
}
962
970
if (candidateParamIndex) {
@@ -1025,11 +1033,12 @@ class LifetimeDependenceChecker {
1025
1033
if (paramTypeInContext->hasError ()) {
1026
1034
return ;
1027
1035
}
1028
- auto kind = inferLifetimeDependenceKindFromType (paramTypeInContext);
1036
+ auto kind = inferLifetimeDependenceKind (paramTypeInContext,
1037
+ param->getValueOwnership ());
1029
1038
1030
1039
pushDeps (createDeps (selfIndex)
1031
- .add (selfIndex, LifetimeDependenceKind::Inherit)
1032
- .add (newValIdx, kind));
1040
+ .add (selfIndex, LifetimeDependenceKind::Inherit)
1041
+ .add (newValIdx, * kind));
1033
1042
break ;
1034
1043
}
1035
1044
default :
0 commit comments