@@ -1058,9 +1058,8 @@ bool TypeChecker::isObjCBridgedTo(Type type1, Type type2, DeclContext *dc,
1058
1058
}
1059
1059
1060
1060
bool TypeChecker::checkedCastMaySucceed (Type t1, Type t2, DeclContext *dc) {
1061
- auto kind = TypeChecker::typeCheckCheckedCast (t1, t2,
1062
- CheckedCastContextKind::None, dc,
1063
- SourceLoc (), nullptr , SourceRange ());
1061
+ auto kind = TypeChecker::typeCheckCheckedCast (
1062
+ t1, t2, CheckedCastContextKind::None, dc);
1064
1063
return (kind != CheckedCastKind::Unresolved);
1065
1064
}
1066
1065
@@ -1559,24 +1558,10 @@ void ConstraintSystem::print(raw_ostream &out) const {
1559
1558
}
1560
1559
1561
1560
// / Determine the semantics of a checked cast operation.
1562
- CheckedCastKind TypeChecker::typeCheckCheckedCast (Type fromType,
1563
- Type toType,
1564
- CheckedCastContextKind contextKind,
1565
- DeclContext *dc,
1566
- SourceLoc diagLoc,
1567
- Expr *fromExpr,
1568
- SourceRange diagToRange) {
1569
- // Determine whether we should suppress diagnostics.
1570
- const bool suppressDiagnostics =
1571
- contextKind == CheckedCastContextKind::None ||
1572
- contextKind == CheckedCastContextKind::Coercion;
1573
- assert ((suppressDiagnostics || diagLoc.isValid ()) &&
1574
- " diagnostics require a valid source location" );
1575
-
1576
- SourceRange diagFromRange;
1577
- if (fromExpr)
1578
- diagFromRange = fromExpr->getSourceRange ();
1579
-
1561
+ CheckedCastKind
1562
+ TypeChecker::typeCheckCheckedCast (Type fromType, Type toType,
1563
+ CheckedCastContextKind contextKind,
1564
+ DeclContext *dc) {
1580
1565
// If the from/to types are equivalent or convertible, this is a coercion.
1581
1566
bool unwrappedIUO = false ;
1582
1567
if (fromType->isEqual (toType) ||
@@ -1594,31 +1579,20 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1594
1579
return CheckedCastKind::BridgingCoercion;
1595
1580
}
1596
1581
1597
- Type origFromType = fromType;
1598
- Type origToType = toType;
1599
-
1600
1582
auto *module = dc->getParentModule ();
1601
-
1602
- auto &diags = dc->getASTContext ().Diags ;
1603
1583
bool optionalToOptionalCast = false ;
1604
1584
1605
1585
// Local function to indicate failure.
1606
1586
auto failed = [&] {
1607
- if (suppressDiagnostics) {
1587
+ if (contextKind == CheckedCastContextKind::Coercion)
1608
1588
return CheckedCastKind::Unresolved;
1609
- }
1610
1589
1611
1590
// Explicit optional-to-optional casts always succeed because a nil
1612
1591
// value of any optional type can be cast to any other optional type.
1613
1592
if (optionalToOptionalCast)
1614
1593
return CheckedCastKind::ValueCast;
1615
1594
1616
- diags.diagnose (diagLoc, diag::downcast_to_unrelated, origFromType,
1617
- origToType)
1618
- .highlight (diagFromRange)
1619
- .highlight (diagToRange);
1620
-
1621
- return CheckedCastKind::ValueCast;
1595
+ return CheckedCastKind::Unresolved;
1622
1596
};
1623
1597
1624
1598
// TODO: Explore optionals using the same strategy used by the
@@ -1647,9 +1621,8 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1647
1621
// downcast. Complain.
1648
1622
auto &Context = dc->getASTContext ();
1649
1623
if (extraFromOptionals > 0 ) {
1650
- switch (typeCheckCheckedCast (fromType, toType,
1651
- CheckedCastContextKind::None, dc,
1652
- SourceLoc (), nullptr , SourceRange ())) {
1624
+ switch (typeCheckCheckedCast (fromType, toType, CheckedCastContextKind::None,
1625
+ dc)) {
1653
1626
case CheckedCastKind::Coercion:
1654
1627
case CheckedCastKind::BridgingCoercion: {
1655
1628
// Treat this as a value cast so we preserve the semantics.
@@ -1670,7 +1643,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1670
1643
auto checkElementCast = [&](Type fromElt, Type toElt,
1671
1644
CheckedCastKind castKind) -> CheckedCastKind {
1672
1645
switch (typeCheckCheckedCast (fromElt, toElt, CheckedCastContextKind::None,
1673
- dc, SourceLoc (), nullptr , SourceRange () )) {
1646
+ dc)) {
1674
1647
case CheckedCastKind::Coercion:
1675
1648
return CheckedCastKind::Coercion;
1676
1649
@@ -1685,13 +1658,13 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1685
1658
1686
1659
case CheckedCastKind::Unresolved:
1687
1660
// Even though we know the elements cannot be downcast, we cannot return
1688
- // failed() here as it's possible for an empty Array, Set or Dictionary to
1689
- // be cast to any element type at runtime (SR-6192). The one exception to
1690
- // this is when we're checking whether we can treat a coercion as a checked
1691
- // cast because we don't want to tell the user to use as!, as it's probably
1692
- // the wrong suggestion.
1661
+ // Unresolved here as it's possible for an empty Array, Set or Dictionary
1662
+ // to be cast to any element type at runtime (SR-6192). The one exception
1663
+ // to this is when we're checking whether we can treat a coercion as a
1664
+ // checked cast because we don't want to tell the user to use as!, as it's
1665
+ // probably the wrong suggestion.
1693
1666
if (contextKind == CheckedCastContextKind::Coercion)
1694
- return failed () ;
1667
+ return CheckedCastKind::Unresolved ;
1695
1668
return castKind;
1696
1669
}
1697
1670
llvm_unreachable (" invalid cast type" );
@@ -1712,8 +1685,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1712
1685
hasBridgingConversion = NoBridging;
1713
1686
bool hasCast = false ;
1714
1687
switch (typeCheckCheckedCast (fromKeyValue->first , toKeyValue->first ,
1715
- CheckedCastContextKind::None, dc,
1716
- SourceLoc (), nullptr , SourceRange ())) {
1688
+ CheckedCastContextKind::None, dc)) {
1717
1689
case CheckedCastKind::Coercion:
1718
1690
hasCoercion = true ;
1719
1691
break ;
@@ -1727,7 +1699,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1727
1699
// Handled the same as in checkElementCast; see comment there for
1728
1700
// rationale.
1729
1701
if (contextKind == CheckedCastContextKind::Coercion)
1730
- return failed () ;
1702
+ return CheckedCastKind::Unresolved ;
1731
1703
LLVM_FALLTHROUGH;
1732
1704
1733
1705
case CheckedCastKind::ArrayDowncast:
@@ -1739,8 +1711,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1739
1711
}
1740
1712
1741
1713
switch (typeCheckCheckedCast (fromKeyValue->second , toKeyValue->second ,
1742
- CheckedCastContextKind::None, dc,
1743
- SourceLoc (), nullptr , SourceRange ())) {
1714
+ CheckedCastContextKind::None, dc)) {
1744
1715
case CheckedCastKind::Coercion:
1745
1716
hasCoercion = true ;
1746
1717
break ;
@@ -1754,7 +1725,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1754
1725
// Handled the same as in checkElementCast; see comment there for
1755
1726
// rationale.
1756
1727
if (contextKind == CheckedCastContextKind::Coercion)
1757
- return failed () ;
1728
+ return CheckedCastKind::Unresolved ;
1758
1729
LLVM_FALLTHROUGH;
1759
1730
1760
1731
case CheckedCastKind::ArrayDowncast:
@@ -1836,31 +1807,14 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1836
1807
if (fromFunctionType &&
1837
1808
(toExistentialType || (toArchetypeType && toConstrainedArchetype))) {
1838
1809
switch (contextKind) {
1810
+ case CheckedCastContextKind::None:
1839
1811
case CheckedCastContextKind::ConditionalCast:
1840
1812
case CheckedCastContextKind::ForcedCast:
1841
- diags.diagnose (diagLoc, diag::downcast_to_unrelated, origFromType,
1842
- origToType)
1843
- .highlight (diagFromRange)
1844
- .highlight (diagToRange);
1845
-
1846
- // If we're referring to a function with a return value (not Void) then
1847
- // emit a fix-it suggesting to add `()` to call the function
1848
- if (auto DRE = dyn_cast<DeclRefExpr>(fromExpr)) {
1849
- if (auto FD = dyn_cast<FuncDecl>(DRE->getDecl ())) {
1850
- if (!FD->getResultInterfaceType ()->isVoid ()) {
1851
- diags.diagnose (diagLoc, diag::downcast_to_unrelated_fixit,
1852
- FD->getBaseIdentifier ())
1853
- .fixItInsertAfter (fromExpr->getEndLoc (), " ()" );
1854
- }
1855
- }
1856
- }
1857
-
1858
- return CheckedCastKind::ValueCast;
1813
+ return CheckedCastKind::Unresolved;
1859
1814
1860
1815
case CheckedCastContextKind::IsPattern:
1861
1816
case CheckedCastContextKind::EnumElementPattern:
1862
1817
case CheckedCastContextKind::IsExpr:
1863
- case CheckedCastContextKind::None:
1864
1818
case CheckedCastContextKind::Coercion:
1865
1819
break ;
1866
1820
}
@@ -1870,8 +1824,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1870
1824
if (Type bridgedToClass = getDynamicBridgedThroughObjCClass (dc, fromType,
1871
1825
toType)) {
1872
1826
switch (typeCheckCheckedCast (bridgedToClass, fromType,
1873
- CheckedCastContextKind::None, dc, SourceLoc (),
1874
- nullptr , SourceRange ())) {
1827
+ CheckedCastContextKind::None, dc)) {
1875
1828
case CheckedCastKind::ArrayDowncast:
1876
1829
case CheckedCastKind::BridgingCoercion:
1877
1830
case CheckedCastKind::Coercion:
@@ -1889,8 +1842,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
1889
1842
if (Type bridgedFromClass = getDynamicBridgedThroughObjCClass (dc, toType,
1890
1843
fromType)) {
1891
1844
switch (typeCheckCheckedCast (toType, bridgedFromClass,
1892
- CheckedCastContextKind::None, dc, SourceLoc (),
1893
- nullptr , SourceRange ())) {
1845
+ CheckedCastContextKind::None, dc)) {
1894
1846
case CheckedCastKind::ArrayDowncast:
1895
1847
case CheckedCastKind::BridgingCoercion:
1896
1848
case CheckedCastKind::Coercion:
0 commit comments