Skip to content

Commit 0a42548

Browse files
committed
[CSFix] NFC: Rename fix/diagnostic for checked casts that always succeed
1 parent 4c84597 commit 0a42548

File tree

5 files changed

+47
-42
lines changed

5 files changed

+47
-42
lines changed

include/swift/Sema/CSFix.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ enum class FixKind : uint8_t {
309309
/// know the result is always succeed.
310310
AllowCheckedCastCoercibleOptionalType,
311311

312-
/// Allow a runtime checked cast where we statically know the result
313-
/// is always succeed.
314-
AllowAlwaysSucceedCheckedCast,
312+
/// Warn about runtime checked cast that is statically known to always
313+
/// succeed.
314+
AllowNoopCheckedCast,
315315

316316
/// Allow a runtime checked cast where at compile time the from is
317317
/// convertible, but runtime does not support such convertions. e.g.
@@ -2681,23 +2681,26 @@ class AllowCheckedCastCoercibleOptionalType final
26812681
}
26822682
};
26832683

2684-
class AllowAlwaysSucceedCheckedCast final
2685-
: public CheckedCastContextualMismatchWarning {
2686-
AllowAlwaysSucceedCheckedCast(ConstraintSystem &cs, Type fromType,
2687-
Type toType, CheckedCastKind kind,
2688-
ConstraintLocator *locator)
2689-
: CheckedCastContextualMismatchWarning(
2690-
cs, FixKind::AllowUnsupportedRuntimeCheckedCast, fromType, toType,
2691-
kind, locator) {}
2684+
class AllowNoopCheckedCast final : public CheckedCastContextualMismatchWarning {
2685+
AllowNoopCheckedCast(ConstraintSystem &cs, Type fromType, Type toType,
2686+
CheckedCastKind kind, ConstraintLocator *locator)
2687+
: CheckedCastContextualMismatchWarning(cs, FixKind::AllowNoopCheckedCast,
2688+
fromType, toType, kind, locator) {}
26922689

26932690
public:
2694-
std::string getName() const override { return "checked cast always succeed"; }
2691+
std::string getName() const override {
2692+
return "checked cast always succeeds";
2693+
}
2694+
26952695
bool diagnose(const Solution &solution, bool asNote = false) const override;
26962696

2697-
static AllowAlwaysSucceedCheckedCast *create(ConstraintSystem &cs,
2698-
Type fromType, Type toType,
2699-
CheckedCastKind kind,
2700-
ConstraintLocator *locator);
2697+
static AllowNoopCheckedCast *create(ConstraintSystem &cs, Type fromType,
2698+
Type toType, CheckedCastKind kind,
2699+
ConstraintLocator *locator);
2700+
2701+
static bool classof(ConstraintFix *fix) {
2702+
return fix->getKind() == FixKind::AllowNoopCheckedCast;
2703+
}
27012704
};
27022705

27032706
class AllowUnsupportedRuntimeCheckedCast final
@@ -2721,6 +2724,10 @@ class AllowUnsupportedRuntimeCheckedCast final
27212724
static AllowUnsupportedRuntimeCheckedCast *
27222725
attempt(ConstraintSystem &cs, Type fromType, Type toType,
27232726
CheckedCastKind kind, ConstraintLocator *locator);
2727+
2728+
static bool classof(ConstraintFix *fix) {
2729+
return fix->getKind() == FixKind::AllowUnsupportedRuntimeCheckedCast;
2730+
}
27242731
};
27252732

27262733
class AllowInvalidStaticMemberRefOnProtocolMetatype final

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7698,7 +7698,7 @@ bool CoercibleOptionalCheckedCastFailure::diagnoseConditionalCastExpr() const {
76987698
return true;
76997699
}
77007700

7701-
bool AlwaysSucceedCheckedCastFailure::diagnoseIfExpr() const {
7701+
bool NoopCheckedCast::diagnoseIfExpr() const {
77027702
auto *expr = getAsExpr<IsExpr>(CastExpr);
77037703
if (!expr)
77047704
return false;
@@ -7707,7 +7707,7 @@ bool AlwaysSucceedCheckedCastFailure::diagnoseIfExpr() const {
77077707
return true;
77087708
}
77097709

7710-
bool AlwaysSucceedCheckedCastFailure::diagnoseConditionalCastExpr() const {
7710+
bool NoopCheckedCast::diagnoseConditionalCastExpr() const {
77117711
auto *expr = getAsExpr<ConditionalCheckedCastExpr>(CastExpr);
77127712
if (!expr)
77137713
return false;
@@ -7717,7 +7717,7 @@ bool AlwaysSucceedCheckedCastFailure::diagnoseConditionalCastExpr() const {
77177717
return true;
77187718
}
77197719

7720-
bool AlwaysSucceedCheckedCastFailure::diagnoseForcedCastExpr() const {
7720+
bool NoopCheckedCast::diagnoseForcedCastExpr() const {
77217721
auto *expr = getAsExpr<ForcedCheckedCastExpr>(CastExpr);
77227722
if (!expr)
77237723
return false;
@@ -7742,7 +7742,7 @@ bool AlwaysSucceedCheckedCastFailure::diagnoseForcedCastExpr() const {
77427742
return true;
77437743
}
77447744

7745-
bool AlwaysSucceedCheckedCastFailure::diagnoseAsError() {
7745+
bool NoopCheckedCast::diagnoseAsError() {
77467746
if (diagnoseIfExpr())
77477747
return true;
77487748

lib/Sema/CSDiagnostics.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,11 +2537,10 @@ class CoercibleOptionalCheckedCastFailure final
25372537

25382538
/// Warn situations where the compiler can statically know a runtime
25392539
/// checked cast always succeed.
2540-
class AlwaysSucceedCheckedCastFailure final : public CheckedCastBaseFailure {
2540+
class NoopCheckedCast final : public CheckedCastBaseFailure {
25412541
public:
2542-
AlwaysSucceedCheckedCastFailure(const Solution &solution, Type fromType,
2543-
Type toType, CheckedCastKind kind,
2544-
ConstraintLocator *locator)
2542+
NoopCheckedCast(const Solution &solution, Type fromType, Type toType,
2543+
CheckedCastKind kind, ConstraintLocator *locator)
25452544
: CheckedCastBaseFailure(solution, fromType, toType, kind, locator) {}
25462545

25472546
bool diagnoseAsError() override;

lib/Sema/CSFix.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,19 +1920,19 @@ bool AllowCheckedCastCoercibleOptionalType::diagnose(const Solution &solution,
19201920
return failure.diagnose(asNote);
19211921
}
19221922

1923-
AllowAlwaysSucceedCheckedCast *
1924-
AllowAlwaysSucceedCheckedCast::create(ConstraintSystem &cs, Type fromType,
1925-
Type toType, CheckedCastKind kind,
1926-
ConstraintLocator *locator) {
1923+
AllowNoopCheckedCast *AllowNoopCheckedCast::create(ConstraintSystem &cs,
1924+
Type fromType, Type toType,
1925+
CheckedCastKind kind,
1926+
ConstraintLocator *locator) {
19271927
return new (cs.getAllocator())
1928-
AllowAlwaysSucceedCheckedCast(cs, fromType, toType, kind, locator);
1928+
AllowNoopCheckedCast(cs, fromType, toType, kind, locator);
19291929
}
19301930

1931-
bool AllowAlwaysSucceedCheckedCast::diagnose(const Solution &solution,
1932-
bool asNote) const {
1933-
AlwaysSucceedCheckedCastFailure failure(solution, getFromType(), getToType(),
1934-
CastKind, getLocator());
1935-
return failure.diagnose(asNote);
1931+
bool AllowNoopCheckedCast::diagnose(const Solution &solution,
1932+
bool asNote) const {
1933+
NoopCheckedCast warning(solution, getFromType(), getToType(), CastKind,
1934+
getLocator());
1935+
return warning.diagnose(asNote);
19361936
}
19371937

19381938
// Although function types maybe compile-time convertible because

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6678,9 +6678,9 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
66786678

66796679
// Always succeed
66806680
if (isCastToExpressibleByNilLiteral(cs, origFromType, toType)) {
6681-
return AllowAlwaysSucceedCheckedCast::create(
6682-
cs, fromType, toType, CheckedCastKind::Coercion,
6683-
cs.getConstraintLocator(locator));
6681+
return AllowNoopCheckedCast::create(cs, fromType, toType,
6682+
CheckedCastKind::Coercion,
6683+
cs.getConstraintLocator(locator));
66846684
}
66856685

66866686
// If both original are metatypes we have to use them because most of the
@@ -6715,10 +6715,9 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
67156715
cs, origFromType, origToType, castKind,
67166716
cs.getConstraintLocator(locator));
67176717
} else {
6718-
// No optionals, just a trivial cast that always succeed.
6719-
return AllowAlwaysSucceedCheckedCast::create(
6720-
cs, origFromType, origToType, castKind,
6721-
cs.getConstraintLocator(locator));
6718+
// No optionals, just a trivial cast that always succeeds.
6719+
return AllowNoopCheckedCast::create(cs, origFromType, origToType, castKind,
6720+
cs.getConstraintLocator(locator));
67226721
}
67236722
}
67246723

@@ -11558,8 +11557,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1155811557
case FixKind::AllowRefToInvalidDecl:
1155911558
case FixKind::SpecifyBaseTypeForOptionalUnresolvedMember:
1156011559
case FixKind::AllowCheckedCastCoercibleOptionalType:
11560+
case FixKind::AllowNoopCheckedCast:
1156111561
case FixKind::AllowUnsupportedRuntimeCheckedCast:
11562-
case FixKind::AllowAlwaysSucceedCheckedCast:
1156311562
case FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype:
1156411563
case FixKind::AllowWrappedValueMismatch:
1156511564
case FixKind::RemoveExtraneousArguments:

0 commit comments

Comments
 (0)