Skip to content

Commit 043f275

Browse files
authored
Merge pull request #27880 from apple/revert-26710-SR-11295-warning-unecessary-casts
Revert "[Diagnostics][Qol] SR-11295 Emit diagnostics for same type coercion. "
2 parents d6117a4 + 58329e0 commit 043f275

38 files changed

+115
-295
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -949,22 +949,18 @@ ERROR(super_initializer_not_in_initializer,none,
949949
WARNING(isa_is_always_true,none, "'%0' test is always true",
950950
(StringRef))
951951
WARNING(isa_is_foreign_check,none,
952-
"'is' test is always true because %0 is a Core Foundation type",
953-
(Type))
952+
"'is' test is always true because %0 is a Core Foundation type",
953+
(Type))
954954
WARNING(conditional_downcast_coercion,none,
955-
"conditional cast from %0 to %1 always succeeds",
956-
(Type, Type))
957-
WARNING(unnecessary_same_type_coercion,none,
958-
"redundant cast to %0 has no effect",
959-
(Type))
960-
WARNING(unnecessary_same_typealias_type_coercion,none,
961-
"redundant cast from %0 to %1 has no effect",
962-
(Type, Type))
955+
"conditional cast from %0 to %1 always succeeds",
956+
(Type, Type))
957+
963958
WARNING(forced_downcast_noop,none,
964959
"forced cast of %0 to same type has no effect", (Type))
960+
965961
WARNING(forced_downcast_coercion,none,
966-
"forced cast from %0 to %1 always succeeds; did you mean to use 'as'?",
967-
(Type, Type))
962+
"forced cast from %0 to %1 always succeeds; did you mean to use 'as'?",
963+
(Type, Type))
968964

969965
// Note: the Boolean at the end indicates whether bridging is required after
970966
// the cast.

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,7 +4427,9 @@ bool InaccessibleMemberFailure::diagnoseAsError() {
44274427
auto &cs = getConstraintSystem();
44284428
auto *locator =
44294429
cs.getConstraintLocator(baseExpr, ConstraintLocator::Member);
4430-
if (cs.hasFixFor(locator))
4430+
if (llvm::any_of(cs.getFixes(), [&](const ConstraintFix *fix) {
4431+
return fix->getLocator() == locator;
4432+
}))
44314433
return false;
44324434
}
44334435

@@ -5056,36 +5058,6 @@ bool ThrowingFunctionConversionFailure::diagnoseAsError() {
50565058
return true;
50575059
}
50585060

5059-
bool UnnecessaryCoercionFailure::diagnoseAsError() {
5060-
auto expr = cast<CoerceExpr>(getAnchor());
5061-
auto sourceRange =
5062-
SourceRange(expr->getLoc(), expr->getCastTypeLoc().getSourceRange().End);
5063-
5064-
if (isa<TypeAliasType>(getFromType().getPointer()) &&
5065-
isa<TypeAliasType>(getToType().getPointer())) {
5066-
auto fromTypeAlias = cast<TypeAliasType>(getFromType().getPointer());
5067-
auto toTypeAlias = cast<TypeAliasType>(getToType().getPointer());
5068-
// If the typealias are different, we need a warning
5069-
// mentioning both types.
5070-
if (fromTypeAlias->getDecl() != toTypeAlias->getDecl()) {
5071-
emitDiagnostic(expr->getLoc(),
5072-
diag::unnecessary_same_typealias_type_coercion,
5073-
getFromType(), getToType())
5074-
5075-
.fixItRemove(sourceRange);
5076-
} else {
5077-
emitDiagnostic(expr->getLoc(), diag::unnecessary_same_type_coercion,
5078-
getToType())
5079-
.fixItRemove(sourceRange);
5080-
}
5081-
} else {
5082-
emitDiagnostic(expr->getLoc(), diag::unnecessary_same_type_coercion,
5083-
getToType())
5084-
.fixItRemove(sourceRange);
5085-
}
5086-
return true;
5087-
}
5088-
50895061
bool InOutConversionFailure::diagnoseAsError() {
50905062
auto *anchor = getAnchor();
50915063
auto *locator = getLocator();

lib/Sema/CSDiagnostics.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,8 @@ class ContextualFailure : public FailureDiagnostic {
709709
/// If we're trying to convert something to `nil`.
710710
bool diagnoseConversionToNil() const;
711711

712-
/// If we're trying to convert something of type "() -> T" to T,
713-
/// then we probably meant to call the value.
712+
// If we're trying to convert something of type "() -> T" to T,
713+
// then we probably meant to call the value.
714714
bool diagnoseMissingFunctionCall() const;
715715

716716
/// Produce a specialized diagnostic if this is an invalid conversion to Bool.
@@ -1462,11 +1462,11 @@ class MutatingMemberRefOnImmutableBase final : public FailureDiagnostic {
14621462
bool diagnoseAsError() override;
14631463
};
14641464

1465-
/// Diagnose an attempt to use AnyObject as the root type of a KeyPath
1466-
///
1467-
/// ```swift
1468-
/// let keyPath = \AnyObject.bar
1469-
/// ```
1465+
// Diagnose an attempt to use AnyObject as the root type of a KeyPath
1466+
//
1467+
// ```swift
1468+
// let keyPath = \AnyObject.bar
1469+
// ```
14701470
class AnyObjectKeyPathRootFailure final : public FailureDiagnostic {
14711471

14721472
public:
@@ -1776,27 +1776,6 @@ class ExpandArrayIntoVarargsFailure final : public ContextualFailure {
17761776
void tryDropArrayBracketsFixIt(Expr *anchor) const;
17771777
};
17781778

1779-
/// Diagnose a situation where there is an explicit type coercion
1780-
/// to the same type e.g.:
1781-
///
1782-
/// ```swift
1783-
/// Double(1) as Double // redundant cast to 'Double' has no effect
1784-
/// 1 as Double as Double // redundant cast to 'Double' has no effect
1785-
/// let string = "String"
1786-
/// let s = string as String // redundant cast to 'String' has no effect
1787-
/// ```
1788-
class UnnecessaryCoercionFailure final
1789-
: public ContextualFailure {
1790-
1791-
public:
1792-
UnnecessaryCoercionFailure(Expr *root, ConstraintSystem &cs,
1793-
Type fromType, Type toType,
1794-
ConstraintLocator *locator)
1795-
: ContextualFailure(root, cs, fromType, toType, locator) {}
1796-
1797-
bool diagnoseAsError() override;
1798-
};
1799-
18001779
/// Diagnose a situation there is a mismatch between argument and parameter
18011780
/// types e.g.:
18021781
///

lib/Sema/CSFix.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -857,38 +857,6 @@ IgnoreContextualType *IgnoreContextualType::create(ConstraintSystem &cs,
857857
IgnoreContextualType(cs, resultTy, specifiedTy, locator);
858858
}
859859

860-
bool RemoveUnnecessaryCoercion::diagnose(Expr *root, bool asNote) const {
861-
auto &cs = getConstraintSystem();
862-
UnnecessaryCoercionFailure failure(root, cs, getFromType(), getToType(),
863-
getLocator());
864-
return failure.diagnose(asNote);
865-
}
866-
867-
bool RemoveUnnecessaryCoercion::attempt(ConstraintSystem &cs, Type fromType,
868-
Type toType,
869-
ConstraintLocatorBuilder locator) {
870-
auto last = locator.last();
871-
bool isExplicitCoercion =
872-
last && last->is<LocatorPathElt::ExplicitTypeCoercion>();
873-
if (!isExplicitCoercion)
874-
return false;
875-
876-
auto expr = cast<CoerceExpr>(locator.getAnchor());
877-
auto toTypeRepr = expr->getCastTypeLoc().getTypeRepr();
878-
879-
// only diagnosing for coercion where the left-side is a DeclRefExpr
880-
// or a explicit/implicit coercion e.g. Double(1) as Double
881-
if (!isa<ImplicitlyUnwrappedOptionalTypeRepr>(toTypeRepr) &&
882-
(isa<DeclRefExpr>(expr->getSubExpr()) ||
883-
isa<CoerceExpr>(expr->getSubExpr()))) {
884-
auto *fix = new (cs.getAllocator()) RemoveUnnecessaryCoercion(
885-
cs, fromType, toType, cs.getConstraintLocator(locator));
886-
887-
return cs.recordFix(fix);
888-
}
889-
return false;
890-
}
891-
892860
bool IgnoreAssignmentDestinationType::diagnose(Expr *root, bool asNote) const {
893861
auto &cs = getConstraintSystem();
894862
auto *AE = cast<AssignExpr>(getAnchor());

lib/Sema/CSFix.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ enum class FixKind : uint8_t {
196196
/// Allow a single tuple parameter to be matched with N arguments
197197
/// by forming all of the given arguments into a single tuple.
198198
AllowTupleSplatForSingleParameter,
199-
200-
/// Remove an unnecessary coercion ('as') if the types are already equal.
201-
/// e.g. Double(1) as Double
202-
RemoveUnnecessaryCoercion,
203199

204200
/// Allow a single argument type mismatch. This is the most generic
205201
/// failure related to argument-to-parameter conversions.
@@ -1344,24 +1340,6 @@ class IgnoreContextualType : public ContextualMismatch {
13441340
ConstraintLocator *locator);
13451341
};
13461342

1347-
class RemoveUnnecessaryCoercion : public ContextualMismatch {
1348-
protected:
1349-
RemoveUnnecessaryCoercion(ConstraintSystem &cs, Type fromType, Type toType,
1350-
ConstraintLocator *locator)
1351-
: ContextualMismatch(cs, FixKind::RemoveUnnecessaryCoercion, fromType,
1352-
toType, locator, /*isWarning*/ true) {}
1353-
1354-
public:
1355-
std::string getName() const override {
1356-
return "remove unnecessary explicit type coercion";
1357-
}
1358-
1359-
bool diagnose(Expr *root, bool asNote = false) const override;
1360-
1361-
static bool attempt(ConstraintSystem &cs, Type fromType, Type toType,
1362-
ConstraintLocatorBuilder locator);
1363-
};
1364-
13651343
class IgnoreAssignmentDestinationType final : public ContextualMismatch {
13661344
IgnoreAssignmentDestinationType(ConstraintSystem &cs, Type sourceTy,
13671345
Type destTy, ConstraintLocator *locator)

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,8 +2587,7 @@ bool ConstraintSystem::repairFailures(
25872587
});
25882588
};
25892589

2590-
if (path.empty() ||
2591-
path.back().is<LocatorPathElt::ExplicitTypeCoercion>()) {
2590+
if (path.empty()) {
25922591
if (!anchor)
25932592
return false;
25942593

@@ -3219,16 +3218,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
32193218
// let's defer it until later proper check.
32203219
if (!(desugar1->is<DependentMemberType>() &&
32213220
desugar2->is<DependentMemberType>())) {
3222-
if (desugar1->isEqual(desugar2)) {
3223-
if (kind >= ConstraintKind::Conversion &&
3224-
!flags.contains(TMF_ApplyingFix)) {
3225-
if (RemoveUnnecessaryCoercion::attempt(*this, type1, type2,
3226-
getConstraintLocator(locator))) {
3227-
return getTypeMatchFailure(locator);
3228-
}
3229-
}
3230-
if (!isa<InOutType>(desugar2))
3231-
return getTypeMatchSuccess();
3221+
// If the types are obviously equivalent, we're done.
3222+
if (desugar1->isEqual(desugar2) && !isa<InOutType>(desugar2)) {
3223+
return getTypeMatchSuccess();
32323224
}
32333225
}
32343226

@@ -8037,7 +8029,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
80378029
case FixKind::GenericArgumentsMismatch:
80388030
case FixKind::AllowMutatingMemberOnRValueBase:
80398031
case FixKind::AllowTupleSplatForSingleParameter:
8040-
case FixKind::RemoveUnnecessaryCoercion:
80418032
llvm_unreachable("handled elsewhere");
80428033
}
80438034

@@ -8344,20 +8335,11 @@ void ConstraintSystem::addExplicitConversionConstraint(
83448335
SmallVector<Constraint *, 3> constraints;
83458336

83468337
auto locatorPtr = getConstraintLocator(locator);
8347-
ConstraintLocator *coerceLocator = locatorPtr;
8348-
8349-
if (allowFixes && shouldAttemptFixes()) {
8350-
auto *anchor = locator.getAnchor();
8351-
if (isa<CoerceExpr>(anchor) && !anchor->isImplicit()) {
8352-
coerceLocator =
8353-
getConstraintLocator(anchor, LocatorPathElt::ExplicitTypeCoercion());
8354-
}
8355-
}
83568338

83578339
// Coercion (the common case).
83588340
Constraint *coerceConstraint =
83598341
Constraint::create(*this, ConstraintKind::Conversion,
8360-
fromType, toType, coerceLocator);
8342+
fromType, toType, locatorPtr);
83618343
coerceConstraint->setFavored();
83628344
constraints.push_back(coerceConstraint);
83638345

lib/Sema/ConstraintLocator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
8686
case KeyPathRoot:
8787
case KeyPathValue:
8888
case KeyPathComponentResult:
89-
case ExplicitTypeCoercion:
9089
case Condition:
9190
auto numValues = numNumericValuesInPathElement(elt.getKind());
9291
for (unsigned i = 0; i < numValues; ++i)
@@ -134,7 +133,6 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
134133
case ConstraintLocator::KeyPathRoot:
135134
case ConstraintLocator::KeyPathValue:
136135
case ConstraintLocator::KeyPathComponentResult:
137-
case ConstraintLocator::ExplicitTypeCoercion:
138136
case ConstraintLocator::Condition:
139137
return 0;
140138

@@ -460,11 +458,7 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
460458
case KeyPathComponentResult:
461459
out << "key path component result";
462460
break;
463-
464-
case ExplicitTypeCoercion:
465-
out << "type coercion";
466-
break;
467-
461+
468462
case Condition:
469463
out << "condition expression";
470464
break;

lib/Sema/ConstraintLocator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
8989
case KeyPathRoot:
9090
case KeyPathValue:
9191
case KeyPathComponentResult:
92-
case ExplicitTypeCoercion:
9392
case Condition:
9493
return 0;
9594

lib/Sema/ConstraintLocatorPathElts.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,6 @@ SIMPLE_LOCATOR_PATH_ELT(UnresolvedMember)
164164
/// The candidate witness during protocol conformance checking.
165165
CUSTOM_LOCATOR_PATH_ELT(Witness)
166166

167-
/// An explicit type coercion.
168-
SIMPLE_LOCATOR_PATH_ELT(ExplicitTypeCoercion)
169-
170167
/// The condition associated with 'if' expression or ternary operator.
171168
SIMPLE_LOCATOR_PATH_ELT(Condition)
172169

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,11 +3000,6 @@ void constraints::simplifyLocator(Expr *&anchor,
30003000
path = path.slice(1);
30013001
continue;
30023002
}
3003-
3004-
case ConstraintLocator::ExplicitTypeCoercion: {
3005-
path = path.slice(1);
3006-
continue;
3007-
}
30083003

30093004
default:
30103005
// FIXME: Lots of other cases to handle.

test/ClangImporter/Darwin_test.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ _ = nil as Fract? // expected-error{{use of undeclared type 'Fract'}}
99
_ = nil as Darwin.Fract? // okay
1010

1111
_ = 0 as OSErr
12-
// noErr is from the overlay
13-
_ = noErr as OSStatus // expected-warning {{redundant cast to 'OSStatus' (aka 'Int32') has no effect}} {{11-23=}}
12+
_ = noErr as OSStatus // noErr is from the overlay
1413
_ = 0 as UniChar
1514

1615
_ = ProcessSerialNumber()

test/ClangImporter/MixedSource/mixed-target-using-header.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ func testSuppressed() {
5353
#endif
5454

5555
func testMacro() {
56-
_ = CONSTANT as CInt // expected-warning {{redundant cast to 'CInt' (aka 'Int32') has no effect}} {{16-24=}}
56+
_ = CONSTANT as CInt
5757
}
5858

5959
func testFoundationOverlay() {
6060
_ = NSUTF8StringEncoding // no ambiguity
61-
// and we should get the overlay version
62-
_ = NSUTF8StringEncoding as UInt // expected-warning {{redundant cast to 'UInt' has no effect}} {{28-36=}}
61+
_ = NSUTF8StringEncoding as UInt // and we should get the overlay version
6362
}
6463

6564
#if !SILGEN

test/ClangImporter/Security_test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import Security
66

7-
_ = kSecClass as CFString // expected-warning {{redundant cast to 'CFString' has no effect}} {{15-27=}}
8-
_ = kSecClassGenericPassword as CFString // expected-warning {{redundant cast to 'CFString' has no effect}} {{30-42=}}
7+
_ = kSecClass as CFString
8+
_ = kSecClassGenericPassword as CFString
99
_ = kSecClassGenericPassword as CFDictionary // expected-error {{'CFString?' is not convertible to 'CFDictionary'}} {{30-32=as!}}
1010

1111
func testIntegration() {

test/ClangImporter/attr-swift_private.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public func testTopLevel() {
102102

103103
_ = __PrivAnonymousA
104104
_ = __E0PrivA
105-
_ = __PrivE1A as __PrivE1 // expected-warning {{redundant cast to '__PrivE1' has no effect}} {{15-27=}}
105+
_ = __PrivE1A as __PrivE1
106106
_ = NSEnum.__privA
107107
_ = NSEnum.B
108108
_ = NSOptions.__privA

test/ClangImporter/cf.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func testTollFree1(_ ccmduct: CCMutableDuct) {
9090
}
9191

9292
func testChainedAliases(_ fridge: CCRefrigerator) {
93-
_ = fridge as CCRefrigerator // expected-warning {{redundant cast to 'CCRefrigerator' has no effect}} {{14-32=}}
93+
_ = fridge as CCRefrigerator
9494

95-
_ = fridge as CCFridge // expected-warning {{redundant cast to 'CCFridge' (aka 'CCRefrigerator') has no effect}} {{14-26=}}
95+
_ = fridge as CCFridge
9696
_ = fridge as CCFridgeRef // expected-error{{'CCFridgeRef' has been renamed to 'CCFridge'}} {{17-28=CCFridge}}
9797
}
9898

test/ClangImporter/cfuncs_parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func test_cfunc2(_ i: Int) {
1515
#else
1616
let f = cfunc2(i, 17)
1717
#endif
18-
_ = f as Float // expected-warning {{redundant cast to 'Float' has no effect}} {{9-18=}}
18+
_ = f as Float
1919
cfunc2(b:17, a:i) // expected-error{{extraneous argument labels 'b:a:' in call}}
2020
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type 'Int32'}}
2121
cfunc2(17, i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'Int32'}}

0 commit comments

Comments
 (0)