Skip to content

Commit 2a0bdf9

Browse files
Updating implementation
1 parent 64f4efb commit 2a0bdf9

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

lib/Sema/CSFix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,17 @@ IgnoreContextualType *IgnoreContextualType::create(ConstraintSystem &cs,
773773
return new (cs.getAllocator())
774774
IgnoreContextualType(cs, resultTy, specifiedTy, locator);
775775
}
776+
777+
bool RemoveUnecessaryCoercion::diagnose(Expr *root, bool asNote) const {
778+
// TODO: Create custom diag for that?
779+
return false;
780+
}
781+
782+
RemoveUnecessaryCoercion
783+
*RemoveUnecessaryCoercion::create(ConstraintSystem &cs,
784+
Type fromType,
785+
Type toType,
786+
ConstraintLocator *locator) {
787+
return new (cs.getAllocator())
788+
RemoveUnecessaryCoercion(cs, fromType, toType, locator);
789+
}

lib/Sema/CSFix.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ enum class FixKind : uint8_t {
195195
/// Allow a single tuple parameter to be matched with N arguments
196196
/// by forming all of the given arguments into a single tuple.
197197
AllowTupleSplatForSingleParameter,
198+
199+
/// Remove a unecessary coercion ('as') if the types are already equal. e.g. "Hello" as String
200+
RemoveUnecessaryCoercion,
198201
};
199202

200203
class ConstraintFix {
@@ -1312,6 +1315,28 @@ class IgnoreContextualType : public ContextualMismatch {
13121315
ConstraintLocator *locator);
13131316
};
13141317

1318+
class RemoveUnecessaryCoercion : public ConstraintFix {
1319+
Type LHS, RHS;
1320+
1321+
protected:
1322+
RemoveUnecessaryCoercion(ConstraintSystem &cs, Type lhs, Type rhs,
1323+
ConstraintLocator *locator)
1324+
: ConstraintFix(cs, FixKind::RemoveUnecessaryCoercion, locator), LHS(lhs),
1325+
RHS(rhs) {}
1326+
1327+
1328+
public:
1329+
std::string getName() const override { return "remove unecessary explicit type coercion"; }
1330+
1331+
Type getFromType() const { return LHS; }
1332+
Type getToType() const { return RHS; }
1333+
1334+
bool diagnose(Expr *root, bool asNote = false) const override;
1335+
1336+
static RemoveUnecessaryCoercion *create(ConstraintSystem &cs, Type lhs, Type rhs,
1337+
ConstraintLocator *locator);
1338+
};
1339+
13151340
} // end namespace constraints
13161341
} // end namespace swift
13171342

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,9 +2747,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
27472747
auto desugar2 = type2->getDesugaredType();
27482748

27492749
// If the types are obviously equivalent, we're done.
2750-
if (desugar1->isEqual(desugar2) &&
2751-
!isa<InOutType>(desugar2))
2752-
return getTypeMatchSuccess();
2750+
if (desugar1->isEqual(desugar2)) {
2751+
if (kind == ConstraintKind::Conversion) {
2752+
// TODO: Create fix it or emit diagnostic?
2753+
}
2754+
if (!isa<InOutType>(desugar2))
2755+
return getTypeMatchSuccess();
2756+
}
27532757

27542758
// Local function that should be used to produce the return value whenever
27552759
// this function was unable to resolve the constraint. It should be used
@@ -2890,20 +2894,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
28902894
formUnsolvedResult);
28912895
}
28922896
}
2893-
2894-
// If we are trying to perform coercion to the same type emit a warning.
2895-
if (locator.getBaseLocator()->isTypeCoercion()) {
2896-
if (auto expr = dyn_cast<CoerceExpr>(locator.getBaseLocator()->getAnchor())) {
2897-
if (!shouldSuppressDiagnostics()) {
2898-
if (type1->getCanonicalType()->isEqual(type2->getCanonicalType())) {
2899-
TC.diagnose(expr->getLoc(), diag::unecessary_same_type_coercion, type2)
2900-
.fixItRemove(SourceRange(expr->getLoc(),
2901-
expr->getCastTypeLoc().getSourceRange().End));
2902-
}
2903-
}
2904-
}
2905-
}
2906-
2897+
29072898
return formUnsolvedResult();
29082899
}
29092900

@@ -7363,6 +7354,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
73637354
case FixKind::GenericArgumentsMismatch:
73647355
case FixKind::AllowMutatingMemberOnRValueBase:
73657356
case FixKind::AllowTupleSplatForSingleParameter:
7357+
case FixKind::RemoveUnecessaryCoercion:
73667358
llvm_unreachable("handled elsewhere");
73677359
}
73687360

@@ -7616,9 +7608,7 @@ void ConstraintSystem::addExplicitConversionConstraint(
76167608
auto locatorPtr = getConstraintLocator(locator);
76177609

76187610
// Coercion (the common case).
7619-
auto coercionPath = LocatorPathElt(ConstraintLocator::TypeCoercion);
7620-
auto coerceLocator = getConstraintLocator(locator.getBaseLocator(),
7621-
coercionPath);
7611+
auto coerceLocator = getConstraintLocator(locator.getBaseLocator(), LocatorPathElt::TypeCoercion());
76227612
Constraint *coerceConstraint =
76237613
Constraint::create(*this, ConstraintKind::Conversion,
76247614
fromType, toType, coerceLocator);

lib/Sema/ConstraintLocator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace swift {
3434

3535
class Expr;
3636
class SourceManager;
37+
class CoerceExpr;
3738

3839
namespace constraints {
3940
class ConstraintSystem;
@@ -367,6 +368,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
367368
class GenericParameter;
368369
class OpenedGeneric;
369370
class KeyPathDynamicMember;
371+
class TypeCoercion;
370372

371373
PathElement(PathElementKind kind)
372374
: storage(encodeStorage(kind, 0)), storedKind(StoredKindAndValue)
@@ -897,6 +899,16 @@ class LocatorPathElt::KeyPathDynamicMember final : public LocatorPathElt {
897899
}
898900
};
899901

902+
class LocatorPathElt::TypeCoercion final : public LocatorPathElt {
903+
public:
904+
TypeCoercion()
905+
: LocatorPathElt(ConstraintLocator::TypeCoercion) {}
906+
907+
static bool classof(const LocatorPathElt *elt) {
908+
return elt->getKind() == ConstraintLocator::TypeCoercion;
909+
}
910+
};
911+
900912
/// A simple stack-only builder object that constructs a
901913
/// constraint locator without allocating memory.
902914
///

0 commit comments

Comments
 (0)