@@ -156,7 +156,13 @@ class FailureDiagnostic {
156
156
}
157
157
158
158
// / \returns true is locator hasn't been simplified down to expression.
159
- bool hasComplexLocator () const { return HasComplexLocator; }
159
+ bool hasComplexLocator () const {
160
+ bool isExplicitCoercion =
161
+ getLocator ()
162
+ ->isLastElement <
163
+ ConstraintLocator::PathElement::ExplicitTypeCoercion>();
164
+ return HasComplexLocator && !isExplicitCoercion;
165
+ }
160
166
161
167
// / \returns A parent expression if sub-expression is contained anywhere
162
168
// / in the root expression or `nullptr` otherwise.
@@ -707,8 +713,8 @@ class ContextualFailure : public FailureDiagnostic {
707
713
// / If we're trying to convert something to `nil`.
708
714
bool diagnoseConversionToNil () const ;
709
715
710
- // If we're trying to convert something of type "() -> T" to T,
711
- // then we probably meant to call the value.
716
+ // / If we're trying to convert something of type "() -> T" to T,
717
+ // / then we probably meant to call the value.
712
718
bool diagnoseMissingFunctionCall () const ;
713
719
714
720
// / Produce a specialized diagnostic if this is an invalid conversion to Bool.
@@ -1460,11 +1466,11 @@ class MutatingMemberRefOnImmutableBase final : public FailureDiagnostic {
1460
1466
bool diagnoseAsError () override ;
1461
1467
};
1462
1468
1463
- // Diagnose an attempt to use AnyObject as the root type of a KeyPath
1464
- //
1465
- // ```swift
1466
- // let keyPath = \AnyObject.bar
1467
- // ```
1469
+ // / Diagnose an attempt to use AnyObject as the root type of a KeyPath
1470
+ // /
1471
+ // / ```swift
1472
+ // / let keyPath = \AnyObject.bar
1473
+ // / ```
1468
1474
class AnyObjectKeyPathRootFailure final : public FailureDiagnostic {
1469
1475
1470
1476
public:
@@ -1774,6 +1780,27 @@ class ExpandArrayIntoVarargsFailure final : public ContextualFailure {
1774
1780
void tryDropArrayBracketsFixIt (Expr *anchor) const ;
1775
1781
};
1776
1782
1783
+ // / Diagnose a situation where there is an explicit type coercion
1784
+ // / to the same type e.g.:
1785
+ // /
1786
+ // / ```swift
1787
+ // / Double(1) as Double // redundant cast to 'Double' has no effect
1788
+ // / 1 as Double as Double // redundant cast to 'Double' has no effect
1789
+ // / let string = "String"
1790
+ // / let s = string as String // redundant cast to 'String' has no effect
1791
+ // / ```
1792
+ class UnnecessaryCoercionFailure final
1793
+ : public ContextualFailure {
1794
+
1795
+ public:
1796
+ UnnecessaryCoercionFailure (Expr *root, ConstraintSystem &cs,
1797
+ Type fromType, Type toType,
1798
+ ConstraintLocator *locator)
1799
+ : ContextualFailure(root, cs, fromType, toType, locator) {}
1800
+
1801
+ bool diagnoseAsError () override ;
1802
+ };
1803
+
1777
1804
// / Diagnose a situation there is a mismatch between argument and parameter
1778
1805
// / types e.g.:
1779
1806
// /
0 commit comments