Skip to content

Commit 4ce4fc4

Browse files
committed
[rbi] Wrap use ActorIsolation::printForDiagnostics with our own SILIsolationInfo::printActorIsolationForDiagnostics.
I am doing this so that I can change how we emit the diagnostics just for SendNonSendable depending on if NonisolatedNonsendingByDefault is enabled without touching the rest of the compiler. This does not actually change any of the actual output though.
1 parent a6edf4f commit 4ce4fc4

File tree

4 files changed

+179
-55
lines changed

4 files changed

+179
-55
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -998,23 +998,23 @@ GROUPED_ERROR(regionbasedisolation_type_send_yields_race, SendingRisksDataRace,
998998
(Type))
999999
NOTE(regionbasedisolation_type_use_after_send, none,
10001000
"sending value of non-Sendable type %0 to %1 callee risks causing data races between %1 and local %2 uses",
1001-
(Type, ActorIsolation, ActorIsolation))
1001+
(Type, StringRef, StringRef))
10021002
NOTE(regionbasedisolation_type_use_after_send_callee, none,
10031003
"sending value of non-Sendable type %0 to %1 %kind2 risks causing data "
10041004
"races between %1 and local %3 uses",
1005-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
1005+
(Type, StringRef, const ValueDecl *, StringRef))
10061006

10071007
NOTE(regionbasedisolation_named_info_send_yields_race, none,
10081008
"sending %1%0 to %2 callee risks causing data races between %2 and local %3 uses",
1009-
(Identifier, StringRef, ActorIsolation, ActorIsolation))
1009+
(Identifier, StringRef, StringRef, StringRef))
10101010
NOTE(regionbasedisolation_named_info_send_yields_race_callee, none,
10111011
"sending %1%0 to %2 %kind3 risks causing data races between %2 and local %4 uses",
1012-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, ActorIsolation))
1012+
(Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
10131013

10141014
// Use after send closure.
10151015
NOTE(regionbasedisolation_type_isolated_capture_yields_race, none,
10161016
"sending value of non-Sendable type %0 to %1 closure due to closure capture risks causing races in between %1 and %2 uses",
1017-
(Type, ActorIsolation, ActorIsolation))
1017+
(Type, StringRef, StringRef))
10181018

10191019
// Value captured in async let and reused.
10201020
NOTE(regionbasedisolation_named_nonisolated_asynclet_name, none,
@@ -1026,7 +1026,7 @@ NOTE(regionbasedisolation_named_value_used_after_explicit_sending, none,
10261026
(Identifier))
10271027
NOTE(regionbasedisolation_named_isolated_closure_yields_race, none,
10281028
"%0%1 is captured by a %2 closure. %2 uses in closure may race against later %3 uses",
1029-
(StringRef, Identifier, ActorIsolation, ActorIsolation))
1029+
(StringRef, Identifier, StringRef, StringRef))
10301030

10311031
NOTE(regionbasedisolation_typed_use_after_sending, none,
10321032
"Passing value of non-Sendable type %0 as a 'sending' argument risks causing races in between local and caller code",
@@ -1040,10 +1040,10 @@ NOTE(regionbasedisolation_typed_use_after_sending_callee, none,
10401040

10411041
NOTE(regionbasedisolation_named_send_never_sendable, none,
10421042
"sending %1%0 to %2 callee risks causing data races between %2 and %3 uses",
1043-
(Identifier, StringRef, ActorIsolation, StringRef))
1043+
(Identifier, StringRef, StringRef, StringRef))
10441044
NOTE(regionbasedisolation_named_send_never_sendable_callee, none,
10451045
"sending %1%0 to %2 %kind3 risks causing data races between %2 and %4 uses",
1046-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, StringRef))
1046+
(Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
10471047

10481048
NOTE(regionbasedisolation_named_send_into_sending_param, none,
10491049
"%0%1 is passed as a 'sending' parameter; Uses in callee may race with "
@@ -1092,10 +1092,10 @@ NOTE(regionbasedisolation_named_send_nt_asynclet_capture, none,
10921092
(Identifier, StringRef))
10931093
NOTE(regionbasedisolation_typed_sendneversendable_via_arg, none,
10941094
"sending %0 value of non-Sendable type %1 to %2 callee risks causing races in between %0 and %2 uses",
1095-
(StringRef, Type, ActorIsolation))
1095+
(StringRef, Type, StringRef))
10961096
NOTE(regionbasedisolation_typed_sendneversendable_via_arg_callee, none,
10971097
"sending %0 value of non-Sendable type %1 to %2 %kind3 risks causing races in between %0 and %2 uses",
1098-
(StringRef, Type, ActorIsolation, const ValueDecl *))
1098+
(StringRef, Type, StringRef, const ValueDecl *))
10991099

11001100
// Error that is only used when the send non sendable emitter cannot discover any
11011101
// information to give a better diagnostic.
@@ -1137,10 +1137,10 @@ NOTE(regionbasedisolation_out_sending_cannot_be_actor_isolated_note_named, none,
11371137
// Example: returning main-actor isolated result to a custom-actor isolated context risks causing data races
11381138
ERROR(rbi_isolation_crossing_result, none,
11391139
"non-Sendable %0-typed result can not be returned from %1 %kind2 to %3 context",
1140-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
1140+
(Type, StringRef, const ValueDecl *, StringRef))
11411141
ERROR(rbi_isolation_crossing_result_no_decl, none,
11421142
"non-Sendable %0-typed result can not be returned from %1 function to %2 context",
1143-
(Type, ActorIsolation, ActorIsolation))
1143+
(Type, StringRef, StringRef))
11441144
NOTE(rbi_non_sendable_nominal,none,
11451145
"%kind0 does not conform to the 'Sendable' protocol",
11461146
(const ValueDecl *))

include/swift/SILOptimizer/Utils/SILIsolationInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ class SILIsolationInfo {
494494
/// that the isolation and the isolated value match.
495495
bool isEqual(const SILIsolationInfo &other) const;
496496

497+
/// A helper function that prints ActorIsolation like we normally do except
498+
/// that it prints nonisolated(nonsending) as nonisolated. This is needed in
499+
/// certain cases when talking about use-after-free uses in send non sendable.
500+
static void
501+
printActorIsolationForDiagnostics(ActorIsolation iso, llvm::raw_ostream &os,
502+
StringRef openingQuotationMark = "'",
503+
bool asNoun = false);
504+
497505
void Profile(llvm::FoldingSetNodeID &id) const;
498506

499507
private:

lib/SILOptimizer/Mandatory/SendNonSendable.cpp

Lines changed: 147 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -695,16 +695,29 @@ class UseAfterSendDiagnosticEmitter {
695695
}
696696
}
697697

698+
SmallString<64> calleeIsolationStr;
699+
{
700+
llvm::raw_svector_ostream os(calleeIsolationStr);
701+
SILIsolationInfo::printActorIsolationForDiagnostics(
702+
isolationCrossing.getCalleeIsolation(), os);
703+
}
704+
705+
SmallString<64> callerIsolationStr;
706+
{
707+
llvm::raw_svector_ostream os(callerIsolationStr);
708+
SILIsolationInfo::printActorIsolationForDiagnostics(
709+
isolationCrossing.getCallerIsolation(), os);
710+
}
711+
698712
if (auto callee = getSendingCallee()) {
699713
diagnoseNote(
700714
loc, diag::regionbasedisolation_named_info_send_yields_race_callee,
701-
name, descriptiveKindStr, isolationCrossing.getCalleeIsolation(),
702-
callee.value(), isolationCrossing.getCallerIsolation());
715+
name, descriptiveKindStr, calleeIsolationStr, callee.value(),
716+
callerIsolationStr);
703717
} else {
704718
diagnoseNote(loc, diag::regionbasedisolation_named_info_send_yields_race,
705-
name, descriptiveKindStr,
706-
isolationCrossing.getCalleeIsolation(),
707-
isolationCrossing.getCallerIsolation());
719+
name, descriptiveKindStr, calleeIsolationStr,
720+
callerIsolationStr);
708721
}
709722
emitRequireInstDiagnostics();
710723
}
@@ -729,10 +742,24 @@ class UseAfterSendDiagnosticEmitter {
729742
}
730743
}
731744

732-
diagnoseNote(
733-
loc, diag::regionbasedisolation_named_info_send_yields_race_callee,
734-
name, descriptiveKindStr, isolationCrossing.getCalleeIsolation(),
735-
callee, isolationCrossing.getCallerIsolation());
745+
SmallString<64> calleeIsolationStr;
746+
{
747+
llvm::raw_svector_ostream os(calleeIsolationStr);
748+
SILIsolationInfo::printActorIsolationForDiagnostics(
749+
isolationCrossing.getCalleeIsolation(), os);
750+
}
751+
752+
SmallString<64> callerIsolationStr;
753+
{
754+
llvm::raw_svector_ostream os(callerIsolationStr);
755+
SILIsolationInfo::printActorIsolationForDiagnostics(
756+
isolationCrossing.getCallerIsolation(), os);
757+
}
758+
759+
diagnoseNote(loc,
760+
diag::regionbasedisolation_named_info_send_yields_race_callee,
761+
name, descriptiveKindStr, calleeIsolationStr, callee,
762+
callerIsolationStr);
736763
emitRequireInstDiagnostics();
737764
}
738765

@@ -755,14 +782,27 @@ class UseAfterSendDiagnosticEmitter {
755782
.highlight(loc.getSourceRange())
756783
.limitBehaviorIf(getBehaviorLimit());
757784

785+
SmallString<64> calleeIsolationStr;
786+
{
787+
llvm::raw_svector_ostream os(calleeIsolationStr);
788+
SILIsolationInfo::printActorIsolationForDiagnostics(
789+
isolationCrossing.getCalleeIsolation(), os);
790+
}
791+
792+
SmallString<64> callerIsolationStr;
793+
{
794+
llvm::raw_svector_ostream os(callerIsolationStr);
795+
SILIsolationInfo::printActorIsolationForDiagnostics(
796+
isolationCrossing.getCallerIsolation(), os);
797+
}
798+
758799
if (auto callee = getSendingCallee()) {
759800
diagnoseNote(loc, diag::regionbasedisolation_type_use_after_send_callee,
760-
inferredType, isolationCrossing.getCalleeIsolation(),
761-
callee.value(), isolationCrossing.getCallerIsolation());
801+
inferredType, calleeIsolationStr, callee.value(),
802+
callerIsolationStr);
762803
} else {
763804
diagnoseNote(loc, diag::regionbasedisolation_type_use_after_send,
764-
inferredType, isolationCrossing.getCalleeIsolation(),
765-
isolationCrossing.getCallerIsolation());
805+
inferredType, calleeIsolationStr, callerIsolationStr);
766806
}
767807
emitRequireInstDiagnostics();
768808
}
@@ -817,10 +857,23 @@ class UseAfterSendDiagnosticEmitter {
817857
}
818858
}
819859

860+
SmallString<64> calleeIsolationStr;
861+
{
862+
llvm::raw_svector_ostream os(calleeIsolationStr);
863+
SILIsolationInfo::printActorIsolationForDiagnostics(
864+
isolationCrossing.getCalleeIsolation(), os);
865+
}
866+
867+
SmallString<64> callerIsolationStr;
868+
{
869+
llvm::raw_svector_ostream os(callerIsolationStr);
870+
SILIsolationInfo::printActorIsolationForDiagnostics(
871+
isolationCrossing.getCallerIsolation(), os);
872+
}
873+
820874
diagnoseNote(
821875
loc, diag::regionbasedisolation_named_isolated_closure_yields_race,
822-
descriptiveKindStr, name, isolationCrossing.getCalleeIsolation(),
823-
isolationCrossing.getCallerIsolation())
876+
descriptiveKindStr, name, calleeIsolationStr, callerIsolationStr)
824877
.highlight(loc.getSourceRange());
825878
emitRequireInstDiagnostics();
826879
}
@@ -832,10 +885,24 @@ class UseAfterSendDiagnosticEmitter {
832885
inferredType)
833886
.highlight(loc.getSourceRange())
834887
.limitBehaviorIf(getBehaviorLimit());
888+
889+
SmallString<64> calleeIsolationStr;
890+
{
891+
llvm::raw_svector_ostream os(calleeIsolationStr);
892+
SILIsolationInfo::printActorIsolationForDiagnostics(
893+
isolationCrossing.getCalleeIsolation(), os);
894+
}
895+
896+
SmallString<64> callerIsolationStr;
897+
{
898+
llvm::raw_svector_ostream os(callerIsolationStr);
899+
SILIsolationInfo::printActorIsolationForDiagnostics(
900+
isolationCrossing.getCallerIsolation(), os);
901+
}
902+
835903
diagnoseNote(loc,
836904
diag::regionbasedisolation_type_isolated_capture_yields_race,
837-
inferredType, isolationCrossing.getCalleeIsolation(),
838-
isolationCrossing.getCallerIsolation());
905+
inferredType, calleeIsolationStr, callerIsolationStr);
839906
emitRequireInstDiagnostics();
840907
}
841908

@@ -1358,16 +1425,22 @@ class SendNeverSentDiagnosticEmitter {
13581425
getIsolationRegionInfo().printForDiagnostics(os);
13591426
}
13601427

1428+
SmallString<64> calleeIsolationStr;
1429+
{
1430+
llvm::raw_svector_ostream os(calleeIsolationStr);
1431+
SILIsolationInfo::printActorIsolationForDiagnostics(
1432+
crossing.getCalleeIsolation(), os);
1433+
}
1434+
13611435
if (auto callee = getSendingCallee()) {
13621436
diagnoseNote(
13631437
loc,
13641438
diag::regionbasedisolation_typed_sendneversendable_via_arg_callee,
1365-
descriptiveKindStr, inferredType, crossing.getCalleeIsolation(),
1366-
callee.value());
1439+
descriptiveKindStr, inferredType, calleeIsolationStr, callee.value());
13671440
} else {
1368-
diagnoseNote(
1369-
loc, diag::regionbasedisolation_typed_sendneversendable_via_arg,
1370-
descriptiveKindStr, inferredType, crossing.getCalleeIsolation());
1441+
diagnoseNote(loc,
1442+
diag::regionbasedisolation_typed_sendneversendable_via_arg,
1443+
descriptiveKindStr, inferredType, calleeIsolationStr);
13711444
}
13721445
}
13731446

@@ -1382,10 +1455,24 @@ class SendNeverSentDiagnosticEmitter {
13821455
os << ' ';
13831456
}
13841457
}
1385-
diagnoseNote(loc,
1386-
diag::regionbasedisolation_named_isolated_closure_yields_race,
1387-
descriptiveKindStr, name, crossing.getCalleeIsolation(),
1388-
crossing.getCallerIsolation())
1458+
1459+
SmallString<64> calleeIsolationStr;
1460+
{
1461+
llvm::raw_svector_ostream os(calleeIsolationStr);
1462+
SILIsolationInfo::printActorIsolationForDiagnostics(
1463+
crossing.getCalleeIsolation(), os);
1464+
}
1465+
1466+
SmallString<64> callerIsolationStr;
1467+
{
1468+
llvm::raw_svector_ostream os(callerIsolationStr);
1469+
SILIsolationInfo::printActorIsolationForDiagnostics(
1470+
crossing.getCallerIsolation(), os);
1471+
}
1472+
1473+
diagnoseNote(
1474+
loc, diag::regionbasedisolation_named_isolated_closure_yields_race,
1475+
descriptiveKindStr, name, calleeIsolationStr, callerIsolationStr)
13891476
.highlight(loc.getSourceRange());
13901477
}
13911478

@@ -1605,16 +1692,23 @@ class SendNeverSentDiagnosticEmitter {
16051692
descriptiveKindStrWithSpace.push_back(' ');
16061693
}
16071694
}
1695+
1696+
SmallString<64> calleeIsolationStr;
1697+
{
1698+
llvm::raw_svector_ostream os(calleeIsolationStr);
1699+
SILIsolationInfo::printActorIsolationForDiagnostics(
1700+
isolationCrossing.getCalleeIsolation(), os);
1701+
}
1702+
16081703
if (auto callee = getSendingCallee()) {
16091704
diagnoseNote(loc,
16101705
diag::regionbasedisolation_named_send_never_sendable_callee,
1611-
name, descriptiveKindStrWithSpace,
1612-
isolationCrossing.getCalleeIsolation(), callee.value(),
1613-
descriptiveKindStr);
1706+
name, descriptiveKindStrWithSpace, calleeIsolationStr,
1707+
callee.value(), descriptiveKindStr);
16141708
} else {
16151709
diagnoseNote(loc, diag::regionbasedisolation_named_send_never_sendable,
1616-
name, descriptiveKindStrWithSpace,
1617-
isolationCrossing.getCalleeIsolation(), descriptiveKindStr);
1710+
name, descriptiveKindStrWithSpace, calleeIsolationStr,
1711+
descriptiveKindStr);
16181712
}
16191713
}
16201714

@@ -2605,17 +2699,31 @@ void NonSendableIsolationCrossingResultDiagnosticEmitter::emit() {
26052699
if (!isolationCrossing)
26062700
return emitUnknownPatternError();
26072701

2702+
SmallString<64> calleeIsolationStr;
2703+
{
2704+
llvm::raw_svector_ostream os(calleeIsolationStr);
2705+
SILIsolationInfo::printActorIsolationForDiagnostics(
2706+
isolationCrossing->getCalleeIsolation(), os);
2707+
}
2708+
2709+
SmallString<64> callerIsolationStr;
2710+
{
2711+
llvm::raw_svector_ostream os(callerIsolationStr);
2712+
SILIsolationInfo::printActorIsolationForDiagnostics(
2713+
isolationCrossing->getCallerIsolation(), os);
2714+
}
2715+
26082716
auto type = getType();
26092717
if (getCalledDecl()) {
2610-
diagnoseError(error.op->getSourceInst(), diag::rbi_isolation_crossing_result,
2611-
type, isolationCrossing->getCalleeIsolation(), getCalledDecl(),
2612-
isolationCrossing->getCallerIsolation())
2613-
.limitBehaviorIf(getBehaviorLimit());
2718+
diagnoseError(error.op->getSourceInst(),
2719+
diag::rbi_isolation_crossing_result, type, calleeIsolationStr,
2720+
getCalledDecl(), callerIsolationStr)
2721+
.limitBehaviorIf(getBehaviorLimit());
26142722
} else {
2615-
diagnoseError(error.op->getSourceInst(), diag::rbi_isolation_crossing_result_no_decl,
2616-
type, isolationCrossing->getCalleeIsolation(),
2617-
isolationCrossing->getCallerIsolation())
2618-
.limitBehaviorIf(getBehaviorLimit());
2723+
diagnoseError(error.op->getSourceInst(),
2724+
diag::rbi_isolation_crossing_result_no_decl, type,
2725+
calleeIsolationStr, callerIsolationStr)
2726+
.limitBehaviorIf(getBehaviorLimit());
26192727
}
26202728
if (type->is<FunctionType>()) {
26212729
diagnoseNote(error.op->getSourceInst(),

0 commit comments

Comments
 (0)