@@ -88,6 +88,34 @@ static Expr *inferArgumentExprFromApplyExpr(ApplyExpr *sourceApply,
88
88
return foundExpr;
89
89
}
90
90
91
+ static std::optional<Identifier> inferNameFromValue (SILValue value) {
92
+ auto *fn = value->getFunction ();
93
+ if (!fn)
94
+ return {};
95
+ VariableNameInferrer::Options options;
96
+ options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
97
+ SmallString<64 > resultingName;
98
+ VariableNameInferrer inferrer (fn, options, resultingName);
99
+ if (!inferrer.inferByWalkingUsesToDefsReturningRoot (value))
100
+ return {};
101
+ return fn->getASTContext ().getIdentifier (resultingName);
102
+ }
103
+
104
+ static std::optional<std::pair<Identifier, SILValue>>
105
+ inferNameAndRootFromValue (SILValue value) {
106
+ auto *fn = value->getFunction ();
107
+ if (!fn)
108
+ return {};
109
+ VariableNameInferrer::Options options;
110
+ options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
111
+ SmallString<64 > resultingName;
112
+ VariableNameInferrer inferrer (fn, options, resultingName);
113
+ SILValue rootValue = inferrer.inferByWalkingUsesToDefsReturningRoot (value);
114
+ if (!rootValue)
115
+ return {};
116
+ return {{fn->getASTContext ().getIdentifier (resultingName), rootValue}};
117
+ }
118
+
91
119
// ===----------------------------------------------------------------------===//
92
120
// MARK: Diagnostics
93
121
// ===----------------------------------------------------------------------===//
@@ -748,25 +776,19 @@ void UseAfterTransferDiagnosticInferrer::init(const Operand *op) {
748
776
if (auto *sourceApply = loc.getAsASTNode <ApplyExpr>()) {
749
777
// Before we do anything further, see if we can find a name and emit a name
750
778
// error.
751
- SmallString<64 > resultingName;
752
- VariableNameInferrer::Options options;
753
- options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
754
- VariableNameInferrer inferrer (op->getFunction (), options, resultingName);
755
- auto &astContext = op->getFunction ()->getASTContext ();
756
- if (auto rootValue =
757
- inferrer.inferByWalkingUsesToDefsReturningRoot (op->get ())) {
758
- if (auto *svi = dyn_cast<SingleValueInstruction>(rootValue)) {
779
+ if (auto rootValueAndName = inferNameAndRootFromValue (op->get ())) {
780
+ if (auto *svi =
781
+ dyn_cast<SingleValueInstruction>(rootValueAndName->second )) {
759
782
return appendUseInfo (UseDiagnosticInfo::forNamedIsolationCrossing (
760
- baseLoc, svi->getLoc (),
761
- astContext.getIdentifier (inferrer.getName ()),
783
+ baseLoc, svi->getLoc (), rootValueAndName->first ,
762
784
*sourceApply->getIsolationCrossing ()));
763
785
}
764
786
765
- if (auto *fArg = dyn_cast<SILFunctionArgument>(rootValue)) {
787
+ if (auto *fArg =
788
+ dyn_cast<SILFunctionArgument>(rootValueAndName->second )) {
766
789
return appendUseInfo (UseDiagnosticInfo::forNamedIsolationCrossing (
767
790
baseLoc, RegularLocation (fArg ->getDecl ()->getLoc ()),
768
- astContext.getIdentifier (inferrer.getName ()),
769
- *sourceApply->getIsolationCrossing ()));
791
+ rootValueAndName->first , *sourceApply->getIsolationCrossing ()));
770
792
}
771
793
}
772
794
@@ -1105,13 +1127,8 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
1105
1127
1106
1128
// See if we can infer a name from the value.
1107
1129
SmallString<64 > resultingName;
1108
- VariableNameInferrer::Options options;
1109
- options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
1110
- VariableNameInferrer inferrer (op->getFunction (), options, resultingName);
1111
- if (inferrer.inferByWalkingUsesToDefsReturningRoot (op->get ())) {
1112
- auto &astContext = op->getFunction ()->getASTContext ();
1113
- diagnosticInfo = UseDiagnosticInfo::forNamed (
1114
- astContext.getIdentifier (inferrer.getName ()), *isolation);
1130
+ if (auto name = inferNameFromValue (op->get ())) {
1131
+ diagnosticInfo = UseDiagnosticInfo::forNamed (*name, *isolation);
1115
1132
return true ;
1116
1133
}
1117
1134
0 commit comments