Skip to content

Commit 4fc26b7

Browse files
[Diagnostics] Improving logic and removing old checks on RemoveUnnecessaryCoercion fix and adjusting some tests
1 parent 933075d commit 4fc26b7

File tree

9 files changed

+30
-37
lines changed

9 files changed

+30
-37
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,11 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
753753
break;
754754
}
755755

756+
case ConstraintLocator::ExplicitTypeCoercion: {
757+
diagnostic = diag::cannot_convert_coerce;
758+
break;
759+
}
760+
756761
default:
757762
break;
758763
}
@@ -2069,7 +2074,8 @@ bool ContextualFailure::diagnoseAsError() {
20692074
diagnostic = diag::cannot_convert_condition_value;
20702075
break;
20712076
}
2072-
2077+
2078+
case ConstraintLocator::ExplicitTypeCoercion:
20732079
case ConstraintLocator::InstanceType: {
20742080
if (diagnoseCoercionToUnrelatedType())
20752081
return true;
@@ -5556,16 +5562,13 @@ bool UnnecessaryCoercionFailure::diagnoseAsError() {
55565562
diag::unnecessary_same_typealias_type_coercion,
55575563
getFromType(), castType)
55585564
.fixItRemove(sourceRange);
5559-
} else {
5560-
emitDiagnostic(expr->getLoc(), diag::unnecessary_same_type_coercion,
5561-
castType)
5562-
.fixItRemove(sourceRange);
5565+
return true;
55635566
}
5564-
} else {
5565-
emitDiagnostic(expr->getLoc(), diag::unnecessary_same_type_coercion,
5566-
castType)
5567-
.fixItRemove(sourceRange);
55685567
}
5568+
5569+
emitDiagnostic(expr->getLoc(), diag::unnecessary_same_type_coercion,
5570+
castType)
5571+
.fixItRemove(sourceRange);
55695572
return true;
55705573
}
55715574

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,12 @@ bool RemoveUnnecessaryCoercion::attempt(ConstraintSystem &cs, Type fromType,
11651165

11661166
// Check to ensure the from and to types are equal the cast type.
11671167
// This is required to handle cases where the conversion is done
1168-
// using compiler intrinsics e.g. _HasCustomAnyHashableRepresentation
1168+
// using compiler intrinsics of _HasCustomAnyHashableRepresentation
11691169
// to AnyHashable where if we coerce a generic type that conforms to
11701170
// this protocol to AnyHashable we match equal types here, but the
11711171
// explicit coercion is still required.
11721172
auto castType = cs.getType(expr->getCastTypeLoc());
1173-
if (!fromType->isEqual(castType) && !castType->hasTypeVariable())
1173+
if (!fromType->isEqual(castType) && cs.isAnyHashableType(castType))
11741174
return false;
11751175

11761176
auto toTypeRepr = expr->getCastTypeLoc().getTypeRepr();
@@ -1196,22 +1196,13 @@ bool RemoveUnnecessaryCoercion::attempt(ConstraintSystem &cs, Type fromType,
11961196

11971197
auto coercedType = cs.getType(expr->getSubExpr());
11981198
if (auto *typeVariable = coercedType->getAs<TypeVariableType>()) {
1199-
// Only diagnose if we have all type variables resolved in the system.
1200-
if (cs.hasFreeTypeVariables())
1201-
return false;
1202-
12031199
auto representative = cs.getRepresentative(typeVariable);
12041200
if (auto *typeSourceLocator =
12051201
cs.getTypeVariableBindingLocator(representative)) {
12061202
// If the type variable binding source locator is the same the
12071203
// contextual type equality is coming from this coercion.
12081204
if (typeSourceLocator == cs.getConstraintLocator(locator))
12091205
return false;
1210-
1211-
// Not warn if locator points to a TupleElement to avoid
1212-
// incorrect warnings.
1213-
if (typeSourceLocator->isLastElement<LocatorPathElt::TupleElement>())
1214-
return false;
12151206
}
12161207
}
12171208

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10510,14 +10510,12 @@ void ConstraintSystem::addExplicitConversionConstraint(
1051010510
auto locatorPtr = getConstraintLocator(locator);
1051110511
ConstraintLocator *coerceLocator = locatorPtr;
1051210512

10513-
if (allowFixes && shouldAttemptFixes()) {
10514-
auto *anchor = locator.getAnchor();
10515-
if (isa<CoerceExpr>(anchor) && !anchor->isImplicit()) {
10516-
coerceLocator =
10517-
getConstraintLocator(anchor, LocatorPathElt::ExplicitTypeCoercion());
10518-
}
10513+
auto *anchor = locator.getAnchor();
10514+
if (anchor && isa<CoerceExpr>(anchor) && !anchor->isImplicit()) {
10515+
coerceLocator =
10516+
getConstraintLocator(anchor, LocatorPathElt::ExplicitTypeCoercion());
1051910517
}
10520-
10518+
1052110519
// Coercion (the common case).
1052210520
Constraint *coerceConstraint =
1052310521
Constraint::create(*this, ConstraintKind::Conversion,

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Solution ConstraintSystem::finalize() {
9191

9292
case FreeTypeVariableBinding::UnresolvedType:
9393
assignFixedType(tv, ctx.TheUnresolvedType,
94-
tv->getImpl().getLocator());
94+
/*locator*/nullptr);
9595
break;
9696
}
9797
}
@@ -206,8 +206,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
206206
// If we don't already have a fixed type for this type variable,
207207
// assign the fixed type from the solution.
208208
if (!getFixedType(binding.first) && !binding.second->hasTypeVariable()) {
209-
auto typeVar = binding.first;
210-
assignFixedType(typeVar, binding.second, typeVar->getImpl().getLocator(),
209+
assignFixedType(binding.first, binding.second, /*locator*/nullptr,
211210
/*updateState=*/false);
212211
}
213212
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
198198
}
199199
}
200200
}
201-
202-
// Recording fixed type source locator for type variable.
203-
recordTypeVariableBindingLocator(typeVar, locator);
201+
202+
if (locator) {
203+
// Recording fixed type source locator for type variable.
204+
recordTypeVariableBindingLocator(typeVar, locator);
205+
}
204206

205207
// Notify the constraint graph.
206208
CG.bindTypeVariable(typeVar, type);

test/expr/cast/as_coerce.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ c3 as C4 // expected-error {{'C3' is not convertible to 'C4'; did you mean to us
8181
// <rdar://problem/19495142> Various incorrect diagnostics for explicit type conversions
8282
1 as Double as Float // expected-error{{cannot convert value of type 'Double' to type 'Float' in coercion}}
8383
1 as Int as String // expected-error{{cannot convert value of type 'Int' to type 'String' in coercion}}
84-
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}}
84+
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}} expected-warning {{redundant cast to 'Double' has no effect}} {{11-21=}}
8585
["awd"] as [Int] // expected-error{{cannot convert value of type 'String' to expected element type 'Int'}}
8686
([1, 2, 1.0], 1) as ([String], Int)
8787
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}

test/expr/cast/dictionary_bridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -disable-redundant-coercion-warning
22

33
// REQUIRES: objc_interop
44

test/expr/cast/dictionary_coerce.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -disable-redundant-coercion-warning
22

33
class C : Hashable {
44
var x = 0

test/expr/cast/objc_coerce_array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen -verify %s
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen -verify %s -disable-redundant-coercion-warning
22
// REQUIRES: objc_interop
33
import Foundation
44

0 commit comments

Comments
 (0)