Skip to content

Commit 462e37b

Browse files
committed
[Effects handling] Move "illegal context" strings into diagnostic text.
The "illegal context" diagnostics were passing strings from the C++ code directly into diagnostic text for things like "a default argument". That causes a bunch of duplicated code and defeats localization of diagnostics, so move to a %select in the diagnostic.
1 parent 90e42bb commit 462e37b

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4008,10 +4008,13 @@ ERROR(throw_in_nonexhaustive_catch,none,
40084008
"error is not handled because the enclosing catch is not exhaustive", ())
40094009

40104010
ERROR(throwing_call_in_illegal_context,none,
4011-
"call can throw, but errors cannot be thrown out of %0",
4012-
(StringRef))
4011+
"call can throw, but errors cannot be thrown out of "
4012+
"%select{<<ERROR>>|a default argument|a property initializer|a global variable initializer|an enum case raw value|a catch pattern|a catch guard expression|a defer body}0",
4013+
(unsigned))
40134014
ERROR(throw_in_illegal_context,none,
4014-
"errors cannot be thrown out of %0", (StringRef))
4015+
"errors cannot be thrown out of "
4016+
"%select{<<ERROR>>|a default argument|a property initializer|a global variable initializer|an enum case raw value|a catch pattern|a catch guard expression|a defer body}0",
4017+
(unsigned))
40154018

40164019
ERROR(throwing_operator_without_try,none,
40174020
"operator can throw but expression is not marked with 'try'", ())

lib/Sema/TypeCheckEffects.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,17 +1030,17 @@ class Context {
10301030

10311031
static void diagnoseThrowInIllegalContext(DiagnosticEngine &Diags,
10321032
ASTNode node,
1033-
StringRef description) {
1033+
Kind kind) {
10341034
if (auto *e = node.dyn_cast<Expr*>()) {
10351035
if (isa<ApplyExpr>(e)) {
10361036
Diags.diagnose(e->getLoc(), diag::throwing_call_in_illegal_context,
1037-
description);
1037+
static_cast<unsigned>(kind));
10381038
return;
10391039
}
10401040
}
10411041

10421042
Diags.diagnose(node.getStartLoc(), diag::throw_in_illegal_context,
1043-
description);
1043+
static_cast<unsigned>(kind));
10441044
}
10451045

10461046
static void maybeAddRethrowsNote(DiagnosticEngine &Diags, SourceLoc loc,
@@ -1174,31 +1174,15 @@ class Context {
11741174
return;
11751175

11761176
case Kind::EnumElementInitializer:
1177-
diagnoseThrowInIllegalContext(Diags, E, "an enum case raw value");
1178-
return;
1179-
11801177
case Kind::GlobalVarInitializer:
1181-
diagnoseThrowInIllegalContext(Diags, E, "a global variable initializer");
1182-
return;
1183-
11841178
case Kind::IVarInitializer:
1185-
diagnoseThrowInIllegalContext(Diags, E, "a property initializer");
1186-
return;
1187-
11881179
case Kind::DefaultArgument:
1189-
diagnoseThrowInIllegalContext(Diags, E, "a default argument");
1190-
return;
1191-
11921180
case Kind::CatchPattern:
1193-
diagnoseThrowInIllegalContext(Diags, E, "a catch pattern");
1194-
return;
1195-
11961181
case Kind::CatchGuard:
1197-
diagnoseThrowInIllegalContext(Diags, E, "a catch guard expression");
1198-
return;
11991182
case Kind::DeferBody:
1200-
diagnoseThrowInIllegalContext(Diags, E, "a defer body");
1183+
diagnoseThrowInIllegalContext(Diags, E, getKind());
12011184
return;
1185+
12021186
}
12031187
llvm_unreachable("bad context kind");
12041188
}

0 commit comments

Comments
 (0)