Skip to content

Commit 0ec2527

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. (cherry picked from commit 4ce4fc4)
1 parent f5d547c commit 0ec2527

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
@@ -983,23 +983,23 @@ GROUPED_ERROR(regionbasedisolation_type_send_yields_race, SendingRisksDataRace,
983983
(Type))
984984
NOTE(regionbasedisolation_type_use_after_send, none,
985985
"sending value of non-Sendable type %0 to %1 callee risks causing data races between %1 and local %2 uses",
986-
(Type, ActorIsolation, ActorIsolation))
986+
(Type, StringRef, StringRef))
987987
NOTE(regionbasedisolation_type_use_after_send_callee, none,
988988
"sending value of non-Sendable type %0 to %1 %kind2 risks causing data "
989989
"races between %1 and local %3 uses",
990-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
990+
(Type, StringRef, const ValueDecl *, StringRef))
991991

992992
NOTE(regionbasedisolation_named_info_send_yields_race, none,
993993
"sending %1%0 to %2 callee risks causing data races between %2 and local %3 uses",
994-
(Identifier, StringRef, ActorIsolation, ActorIsolation))
994+
(Identifier, StringRef, StringRef, StringRef))
995995
NOTE(regionbasedisolation_named_info_send_yields_race_callee, none,
996996
"sending %1%0 to %2 %kind3 risks causing data races between %2 and local %4 uses",
997-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, ActorIsolation))
997+
(Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
998998

999999
// Use after send closure.
10001000
NOTE(regionbasedisolation_type_isolated_capture_yields_race, none,
10011001
"sending value of non-Sendable type %0 to %1 closure due to closure capture risks causing races in between %1 and %2 uses",
1002-
(Type, ActorIsolation, ActorIsolation))
1002+
(Type, StringRef, StringRef))
10031003

10041004
// Value captured in async let and reused.
10051005
NOTE(regionbasedisolation_named_nonisolated_asynclet_name, none,
@@ -1011,7 +1011,7 @@ NOTE(regionbasedisolation_named_value_used_after_explicit_sending, none,
10111011
(Identifier))
10121012
NOTE(regionbasedisolation_named_isolated_closure_yields_race, none,
10131013
"%0%1 is captured by a %2 closure. %2 uses in closure may race against later %3 uses",
1014-
(StringRef, Identifier, ActorIsolation, ActorIsolation))
1014+
(StringRef, Identifier, StringRef, StringRef))
10151015

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

10261026
NOTE(regionbasedisolation_named_send_never_sendable, none,
10271027
"sending %1%0 to %2 callee risks causing data races between %2 and %3 uses",
1028-
(Identifier, StringRef, ActorIsolation, StringRef))
1028+
(Identifier, StringRef, StringRef, StringRef))
10291029
NOTE(regionbasedisolation_named_send_never_sendable_callee, none,
10301030
"sending %1%0 to %2 %kind3 risks causing data races between %2 and %4 uses",
1031-
(Identifier, StringRef, ActorIsolation, const ValueDecl *, StringRef))
1031+
(Identifier, StringRef, StringRef, const ValueDecl *, StringRef))
10321032

10331033
NOTE(regionbasedisolation_named_send_into_sending_param, none,
10341034
"%0%1 is passed as a 'sending' parameter; Uses in callee may race with "
@@ -1077,10 +1077,10 @@ NOTE(regionbasedisolation_named_send_nt_asynclet_capture, none,
10771077
(Identifier, StringRef))
10781078
NOTE(regionbasedisolation_typed_sendneversendable_via_arg, none,
10791079
"sending %0 value of non-Sendable type %1 to %2 callee risks causing races in between %0 and %2 uses",
1080-
(StringRef, Type, ActorIsolation))
1080+
(StringRef, Type, StringRef))
10811081
NOTE(regionbasedisolation_typed_sendneversendable_via_arg_callee, none,
10821082
"sending %0 value of non-Sendable type %1 to %2 %kind3 risks causing races in between %0 and %2 uses",
1083-
(StringRef, Type, ActorIsolation, const ValueDecl *))
1083+
(StringRef, Type, StringRef, const ValueDecl *))
10841084

10851085
NOTE(regionbasedisolation_isolated_conformance_introduced, none,
10861086
"isolated conformance to %kind0 can be introduced here",
@@ -1126,10 +1126,10 @@ NOTE(regionbasedisolation_out_sending_cannot_be_actor_isolated_note_named, none,
11261126
// Example: returning main-actor isolated result to a custom-actor isolated context risks causing data races
11271127
ERROR(rbi_isolation_crossing_result, none,
11281128
"non-Sendable %0-typed result can not be returned from %1 %kind2 to %3 context",
1129-
(Type, ActorIsolation, const ValueDecl *, ActorIsolation))
1129+
(Type, StringRef, const ValueDecl *, StringRef))
11301130
ERROR(rbi_isolation_crossing_result_no_decl, none,
11311131
"non-Sendable %0-typed result can not be returned from %1 function to %2 context",
1132-
(Type, ActorIsolation, ActorIsolation))
1132+
(Type, StringRef, StringRef))
11331133
NOTE(rbi_non_sendable_nominal,none,
11341134
"%kind0 does not conform to the 'Sendable' protocol",
11351135
(const ValueDecl *))

include/swift/SILOptimizer/Utils/SILIsolationInfo.h

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

552+
/// A helper function that prints ActorIsolation like we normally do except
553+
/// that it prints nonisolated(nonsending) as nonisolated. This is needed in
554+
/// certain cases when talking about use-after-free uses in send non sendable.
555+
static void
556+
printActorIsolationForDiagnostics(ActorIsolation iso, llvm::raw_ostream &os,
557+
StringRef openingQuotationMark = "'",
558+
bool asNoun = false);
559+
552560
void Profile(llvm::FoldingSetNodeID &id) const;
553561

554562
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

@@ -1377,16 +1444,22 @@ class SendNeverSentDiagnosticEmitter {
13771444
getIsolationRegionInfo().printForDiagnostics(os);
13781445
}
13791446

1447+
SmallString<64> calleeIsolationStr;
1448+
{
1449+
llvm::raw_svector_ostream os(calleeIsolationStr);
1450+
SILIsolationInfo::printActorIsolationForDiagnostics(
1451+
crossing.getCalleeIsolation(), os);
1452+
}
1453+
13801454
if (auto callee = getSendingCallee()) {
13811455
diagnoseNote(
13821456
loc,
13831457
diag::regionbasedisolation_typed_sendneversendable_via_arg_callee,
1384-
descriptiveKindStr, inferredType, crossing.getCalleeIsolation(),
1385-
callee.value());
1458+
descriptiveKindStr, inferredType, calleeIsolationStr, callee.value());
13861459
} else {
1387-
diagnoseNote(
1388-
loc, diag::regionbasedisolation_typed_sendneversendable_via_arg,
1389-
descriptiveKindStr, inferredType, crossing.getCalleeIsolation());
1460+
diagnoseNote(loc,
1461+
diag::regionbasedisolation_typed_sendneversendable_via_arg,
1462+
descriptiveKindStr, inferredType, calleeIsolationStr);
13901463
}
13911464
}
13921465

@@ -1401,10 +1474,24 @@ class SendNeverSentDiagnosticEmitter {
14011474
os << ' ';
14021475
}
14031476
}
1404-
diagnoseNote(loc,
1405-
diag::regionbasedisolation_named_isolated_closure_yields_race,
1406-
descriptiveKindStr, name, crossing.getCalleeIsolation(),
1407-
crossing.getCallerIsolation())
1477+
1478+
SmallString<64> calleeIsolationStr;
1479+
{
1480+
llvm::raw_svector_ostream os(calleeIsolationStr);
1481+
SILIsolationInfo::printActorIsolationForDiagnostics(
1482+
crossing.getCalleeIsolation(), os);
1483+
}
1484+
1485+
SmallString<64> callerIsolationStr;
1486+
{
1487+
llvm::raw_svector_ostream os(callerIsolationStr);
1488+
SILIsolationInfo::printActorIsolationForDiagnostics(
1489+
crossing.getCallerIsolation(), os);
1490+
}
1491+
1492+
diagnoseNote(
1493+
loc, diag::regionbasedisolation_named_isolated_closure_yields_race,
1494+
descriptiveKindStr, name, calleeIsolationStr, callerIsolationStr)
14081495
.highlight(loc.getSourceRange());
14091496
}
14101497

@@ -1623,16 +1710,23 @@ class SendNeverSentDiagnosticEmitter {
16231710
descriptiveKindStrWithSpace.push_back(' ');
16241711
}
16251712
}
1713+
1714+
SmallString<64> calleeIsolationStr;
1715+
{
1716+
llvm::raw_svector_ostream os(calleeIsolationStr);
1717+
SILIsolationInfo::printActorIsolationForDiagnostics(
1718+
isolationCrossing.getCalleeIsolation(), os);
1719+
}
1720+
16261721
if (auto callee = getSendingCallee()) {
16271722
diagnoseNote(loc,
16281723
diag::regionbasedisolation_named_send_never_sendable_callee,
1629-
name, descriptiveKindStrWithSpace,
1630-
isolationCrossing.getCalleeIsolation(), callee.value(),
1631-
descriptiveKindStr);
1724+
name, descriptiveKindStrWithSpace, calleeIsolationStr,
1725+
callee.value(), descriptiveKindStr);
16321726
} else {
16331727
diagnoseNote(loc, diag::regionbasedisolation_named_send_never_sendable,
1634-
name, descriptiveKindStrWithSpace,
1635-
isolationCrossing.getCalleeIsolation(), descriptiveKindStr);
1728+
name, descriptiveKindStrWithSpace, calleeIsolationStr,
1729+
descriptiveKindStr);
16361730
}
16371731
}
16381732

@@ -2623,17 +2717,31 @@ void NonSendableIsolationCrossingResultDiagnosticEmitter::emit() {
26232717
if (!isolationCrossing)
26242718
return emitUnknownPatternError();
26252719

2720+
SmallString<64> calleeIsolationStr;
2721+
{
2722+
llvm::raw_svector_ostream os(calleeIsolationStr);
2723+
SILIsolationInfo::printActorIsolationForDiagnostics(
2724+
isolationCrossing->getCalleeIsolation(), os);
2725+
}
2726+
2727+
SmallString<64> callerIsolationStr;
2728+
{
2729+
llvm::raw_svector_ostream os(callerIsolationStr);
2730+
SILIsolationInfo::printActorIsolationForDiagnostics(
2731+
isolationCrossing->getCallerIsolation(), os);
2732+
}
2733+
26262734
auto type = getType();
26272735
if (getCalledDecl()) {
2628-
diagnoseError(error.op->getSourceInst(), diag::rbi_isolation_crossing_result,
2629-
type, isolationCrossing->getCalleeIsolation(), getCalledDecl(),
2630-
isolationCrossing->getCallerIsolation())
2631-
.limitBehaviorIf(getBehaviorLimit());
2736+
diagnoseError(error.op->getSourceInst(),
2737+
diag::rbi_isolation_crossing_result, type, calleeIsolationStr,
2738+
getCalledDecl(), callerIsolationStr)
2739+
.limitBehaviorIf(getBehaviorLimit());
26322740
} else {
2633-
diagnoseError(error.op->getSourceInst(), diag::rbi_isolation_crossing_result_no_decl,
2634-
type, isolationCrossing->getCalleeIsolation(),
2635-
isolationCrossing->getCallerIsolation())
2636-
.limitBehaviorIf(getBehaviorLimit());
2741+
diagnoseError(error.op->getSourceInst(),
2742+
diag::rbi_isolation_crossing_result_no_decl, type,
2743+
calleeIsolationStr, callerIsolationStr)
2744+
.limitBehaviorIf(getBehaviorLimit());
26372745
}
26382746
if (type->is<FunctionType>()) {
26392747
diagnoseNote(error.op->getSourceInst(),

0 commit comments

Comments
 (0)