Skip to content

Commit 4713880

Browse files
committed
[region-isolation] Add the ability to force the diagnostic emitter to emit typed diagnostics instead of named diagnostics.
We only use typed diagnostics if we are unable to pattern match a name for a SILValue. Since we generally do this (in most of our tests we do for instance), it is hard to test this. So I put in an option here that is enabled only in asserts that forces the diagnostic emitter to emit the typed diagnostic by forcing a name inference failure. This then allows me to write an asserts only test that validates the behavior of the typed diagnostics since I can guarantee the typed diagnostics will run.
1 parent 4e71e32 commit 4713880

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ using Region = PartitionPrimitives::Region;
5555

5656
} // namespace
5757

58+
// This option is used so we can test typed errors. Typed errors are a fallback
59+
// case which are emitted when we are unable to infer the name of a value. We in
60+
// most cases do succeed inferring, so it makes sense to add an asserts only
61+
// option that can be used by the compiler to test that we emit these correctly.
62+
static llvm::cl::opt<bool> ForceTypedErrors(
63+
"sil-regionbasedisolation-force-use-of-typed-errors",
64+
llvm::cl::desc("Force the usage of typed instead of named errors to make "
65+
"it easier to test typed errors"),
66+
llvm::cl::Hidden);
67+
5868
//===----------------------------------------------------------------------===//
5969
// MARK: Utilities
6070
//===----------------------------------------------------------------------===//
@@ -154,6 +164,23 @@ static Expr *inferArgumentExprFromApplyExpr(ApplyExpr *sourceApply,
154164
return foundExpr;
155165
}
156166

167+
/// Attempt to infer a name for \p value. Returns none if we fail or if we are
168+
/// asked to force typed errors since we are testing.
169+
static std::optional<Identifier> inferNameHelper(SILValue value) {
170+
if (ForceTypedErrors)
171+
return {};
172+
return VariableNameInferrer::inferName(value);
173+
}
174+
175+
/// Attempt to infer a name and root for \p value. Returns none if we fail or if
176+
/// we are asked to force typed errors since we are testing.
177+
static std::optional<std::pair<Identifier, SILValue>>
178+
inferNameAndRootHelper(SILValue value) {
179+
if (ForceTypedErrors)
180+
return {};
181+
return VariableNameInferrer::inferNameAndRoot(value);
182+
}
183+
157184
//===----------------------------------------------------------------------===//
158185
// MARK: Diagnostics
159186
//===----------------------------------------------------------------------===//
@@ -894,8 +921,7 @@ bool UseAfterTransferDiagnosticInferrer::initForIsolatedPartialApply(
894921
emittedDiagnostic = true;
895922

896923
auto &state = transferringOpToStateMap.get(transferOp);
897-
if (auto rootValueAndName =
898-
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
924+
if (auto rootValueAndName = inferNameAndRootHelper(transferOp->get())) {
899925
diagnosticEmitter.emitNamedIsolationCrossingDueToCapture(
900926
RegularLocation(std::get<0>(p).getLoc()), rootValueAndName->first,
901927
state.isolationInfo.getIsolationInfo(), std::get<2>(p));
@@ -1066,8 +1092,7 @@ void UseAfterTransferDiagnosticInferrer::infer() {
10661092
.hasOption(SILParameterInfo::Sending)) {
10671093

10681094
// First try to do the named diagnostic if we can find a name.
1069-
if (auto rootValueAndName =
1070-
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
1095+
if (auto rootValueAndName = inferNameAndRootHelper(transferOp->get())) {
10711096
return diagnosticEmitter.emitNamedUseOfStronglyTransferredValue(
10721097
baseLoc, rootValueAndName->first);
10731098
}
@@ -1093,8 +1118,7 @@ void UseAfterTransferDiagnosticInferrer::infer() {
10931118
if (auto *sourceApply = loc.getAsASTNode<ApplyExpr>()) {
10941119
// Before we do anything further, see if we can find a name and emit a name
10951120
// error.
1096-
if (auto rootValueAndName =
1097-
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
1121+
if (auto rootValueAndName = inferNameAndRootHelper(transferOp->get())) {
10981122
auto &state = transferringOpToStateMap.get(transferOp);
10991123
return diagnosticEmitter.emitNamedIsolationCrossingError(
11001124
baseLoc, rootValueAndName->first,
@@ -1579,7 +1603,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
15791603

15801604
// See if we can infer a name from the value.
15811605
SmallString<64> resultingName;
1582-
if (auto varName = VariableNameInferrer::inferName(op->get())) {
1606+
if (auto varName = inferNameHelper(op->get())) {
15831607
diagnosticEmitter.emitNamedFunctionArgumentApplyStronglyTransferred(
15841608
loc, *varName);
15851609
return true;
@@ -1619,7 +1643,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
16191643

16201644
// See if we can infer a name from the value.
16211645
SmallString<64> resultingName;
1622-
if (auto name = VariableNameInferrer::inferName(op->get())) {
1646+
if (auto name = inferNameHelper(op->get())) {
16231647
diagnosticEmitter.emitNamedIsolation(loc, *name, *isolation);
16241648
return true;
16251649
}
@@ -1667,7 +1691,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
16671691
"All result info must be the same... if that changes... update "
16681692
"this code!");
16691693
SmallString<64> resultingName;
1670-
if (auto name = VariableNameInferrer::inferName(op->get())) {
1694+
if (auto name = inferNameHelper(op->get())) {
16711695
diagnosticEmitter.emitNamedTransferringReturn(loc, *name);
16721696
return true;
16731697
}
@@ -1807,7 +1831,7 @@ class InOutSendingNotDisconnectedDiagnosticEmitter {
18071831
void InOutSendingNotDisconnectedDiagnosticEmitter::emit() {
18081832
// We should always be able to find a name for an inout sending param. If we
18091833
// do not, emit an unknown pattern error.
1810-
auto varName = VariableNameInferrer::inferName(info.inoutSendingParam);
1834+
auto varName = inferNameHelper(info.inoutSendingParam);
18111835
if (!varName) {
18121836
return emitUnknownPatternError();
18131837
}

0 commit comments

Comments
 (0)