Skip to content

Commit fc1da4b

Browse files
Changing implementation to the PR suggestion
1 parent f4736d8 commit fc1da4b

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,15 +3533,6 @@ namespace {
35333533
Expr *sub = expr->getSubExpr();
35343534

35353535
cs.setExprTypes(sub);
3536-
3537-
if (!SuppressDiagnostics) {
3538-
// If we are trying to perform coercion to the same type emit a warning.
3539-
if (cs.getType(sub)->isEqual(toType)) {
3540-
tc.diagnose(expr->getLoc(), diag::unecessary_same_type_coercion, toType)
3541-
.fixItRemove(SourceRange(expr->getLoc(),
3542-
expr->getCastTypeLoc().getSourceRange().End));
3543-
}
3544-
}
35453536

35463537
if (tc.convertToType(sub, toType, cs.DC))
35473538
return nullptr;

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,9 @@ namespace {
26592659
CS.setType(expr->getCastTypeLoc(), toType);
26602660

26612661
auto fromType = CS.getType(expr->getSubExpr());
2662-
auto locator = CS.getConstraintLocator(expr);
2662+
2663+
auto pathElt = LocatorPathElt(ConstraintLocator::TypeCoercion);
2664+
auto locator = CS.getConstraintLocator(expr, pathElt);
26632665

26642666
// Add a conversion constraint for the direct conversion between
26652667
// types.

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,19 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
28112811
formUnsolvedResult);
28122812
}
28132813
}
2814+
2815+
if (locator.getBaseLocator()->isTypeCoercion()) {
2816+
auto expr = dyn_cast<CoerceExpr>(locator.getBaseLocator()->getAnchor());
2817+
if (!shouldSuppressDiagnostics()) {
2818+
// If we are trying to perform coercion to the same type emit a warning.
2819+
if (type1->getCanonicalType()->isEqual(type2->getCanonicalType())) {
2820+
TC.diagnose(expr->getLoc(), diag::unecessary_same_type_coercion, type2)
2821+
.fixItRemove(SourceRange(expr->getLoc(),
2822+
expr->getCastTypeLoc().getSourceRange().End));
2823+
}
2824+
}
2825+
}
2826+
28142827
return formUnsolvedResult();
28152828
}
28162829

lib/Sema/ConstraintLocator.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
8484
case KeyPathRoot:
8585
case KeyPathValue:
8686
case KeyPathComponentResult:
87+
case TypeCoercion:
8788
if (unsigned numValues = numNumericValuesInPathElement(elt.getKind())) {
8889
id.AddInteger(elt.getValue());
8990
if (numValues > 1)
@@ -94,6 +95,18 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
9495
}
9596
}
9697

98+
/// Determine whether given locator points to the type coercion
99+
/// e.g. "Hello" as String
100+
bool ConstraintLocator::isTypeCoercion() const {
101+
auto *anchor = getAnchor();
102+
auto path = getPath();
103+
104+
if (!anchor || path.empty())
105+
return false;
106+
107+
return path.back().getKind() == ConstraintLocator::TypeCoercion;
108+
}
109+
97110
/// Determine whether given locator points to the subscript reference
98111
/// e.g. `foo[0]` or `\Foo.[0]`
99112
bool ConstraintLocator::isSubscriptMemberRef() const {
@@ -402,6 +415,10 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
402415
case KeyPathComponentResult:
403416
out << "key path component result";
404417
break;
418+
419+
case TypeCoercion:
420+
out << "type coercion";
421+
break;
405422
}
406423
}
407424
out << ']';

lib/Sema/ConstraintLocator.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class ConstraintLocator : public llvm::FoldingSetNode {
139139
KeyPathValue,
140140
/// The result type of a key path component. Not used for subscripts.
141141
KeyPathComponentResult,
142+
/// A type coercion
143+
TypeCoercion,
142144
};
143145

144146
/// Determine the number of numeric values used for the given path
@@ -173,6 +175,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
173175
case KeyPathRoot:
174176
case KeyPathValue:
175177
case KeyPathComponentResult:
178+
case TypeCoercion:
176179
return 0;
177180

178181
case ContextualType:
@@ -244,6 +247,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
244247
case KeyPathRoot:
245248
case KeyPathValue:
246249
case KeyPathComponentResult:
250+
case TypeCoercion:
247251
return 0;
248252

249253
case FunctionArgument:
@@ -553,6 +557,10 @@ class ConstraintLocator : public llvm::FoldingSetNode {
553557
return (getSummaryFlags() & IsFunctionConversion);
554558
}
555559

560+
/// Determine whether given locator points to the type coercion
561+
/// e.g. "Hello" as String
562+
bool isTypeCoercion() const;
563+
556564
/// Determine whether given locator points to the subscript reference
557565
/// e.g. `foo[0]` or `\Foo.[0]`
558566
bool isSubscriptMemberRef() const;

0 commit comments

Comments
 (0)