Skip to content

Commit c510d35

Browse files
Implemeting fixit and custom failure diagnostics
1 parent 2a0bdf9 commit c510d35

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

Diff for: lib/Sema/CSDiagnostics.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -4334,3 +4334,12 @@ bool ThrowingFunctionConversionFailure::diagnoseAsError() {
43344334
getFromType(), getToType());
43354335
return true;
43364336
}
4337+
4338+
bool UnecessaryCoecionFailure::diagnoseAsError() {
4339+
auto expr = dyn_cast<CoerceExpr>(getAnchor());
4340+
4341+
emitDiagnostic(expr->getLoc(), diag::unecessary_same_type_coercion, getToType())
4342+
.fixItRemove(SourceRange(expr->getLoc(),
4343+
expr->getCastTypeLoc().getSourceRange().End));
4344+
return true;
4345+
}

Diff for: lib/Sema/CSDiagnostics.h

+19-5
Original file line numberDiff line numberDiff line change
@@ -1316,11 +1316,11 @@ class MutatingMemberRefOnImmutableBase final : public FailureDiagnostic {
13161316
bool diagnoseAsError() override;
13171317
};
13181318

1319-
// Diagnose an attempt to use AnyObject as the root type of a KeyPath
1320-
//
1321-
// ```swift
1322-
// let keyPath = \AnyObject.bar
1323-
// ```
1319+
/// Diagnose an attempt to use AnyObject as the root type of a KeyPath
1320+
///
1321+
/// ```swift
1322+
/// let keyPath = \AnyObject.bar
1323+
/// ```
13241324
class AnyObjectKeyPathRootFailure final : public FailureDiagnostic {
13251325

13261326
public:
@@ -1595,6 +1595,20 @@ class InvalidTupleSplatWithSingleParameterFailure final
15951595
bool diagnoseAsError() override;
15961596
};
15971597

1598+
class UnecessaryCoecionFailure final : public FailureDiagnostic {
1599+
Type toType;
1600+
1601+
public:
1602+
UnecessaryCoecionFailure(ConstraintSystem &cs,
1603+
Type toType,
1604+
ConstraintLocator *locator)
1605+
: FailureDiagnostic(locator->getAnchor(), cs, locator), toType(toType) {}
1606+
1607+
Type getToType() { return toType; }
1608+
1609+
bool diagnoseAsError() override;
1610+
};
1611+
15981612
/// Provides information about the application of a function argument to a
15991613
/// parameter.
16001614
class FunctionArgApplyInfo {

Diff for: lib/Sema/CSFix.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -775,15 +775,16 @@ IgnoreContextualType *IgnoreContextualType::create(ConstraintSystem &cs,
775775
}
776776

777777
bool RemoveUnecessaryCoercion::diagnose(Expr *root, bool asNote) const {
778-
// TODO: Create custom diag for that?
779-
return false;
778+
auto &cs = getConstraintSystem();
779+
780+
UnecessaryCoecionFailure failure(cs, getToType(), getLocator());
781+
return failure.diagnoseAsError();
780782
}
781783

782784
RemoveUnecessaryCoercion
783785
*RemoveUnecessaryCoercion::create(ConstraintSystem &cs,
784-
Type fromType,
785786
Type toType,
786787
ConstraintLocator *locator) {
787788
return new (cs.getAllocator())
788-
RemoveUnecessaryCoercion(cs, fromType, toType, locator);
789+
RemoveUnecessaryCoercion(cs, toType, locator);
789790
}

Diff for: lib/Sema/CSFix.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -1316,24 +1316,22 @@ class IgnoreContextualType : public ContextualMismatch {
13161316
};
13171317

13181318
class RemoveUnecessaryCoercion : public ConstraintFix {
1319-
Type LHS, RHS;
1319+
Type toType;
13201320

13211321
protected:
1322-
RemoveUnecessaryCoercion(ConstraintSystem &cs, Type lhs, Type rhs,
1323-
ConstraintLocator *locator)
1324-
: ConstraintFix(cs, FixKind::RemoveUnecessaryCoercion, locator), LHS(lhs),
1325-
RHS(rhs) {}
1322+
RemoveUnecessaryCoercion(ConstraintSystem &cs, Type toType,
1323+
ConstraintLocator *locator)
1324+
: ConstraintFix(cs, FixKind::RemoveUnecessaryCoercion, locator, /*isWarning*/ true), toType(toType) {}
13261325

13271326

13281327
public:
13291328
std::string getName() const override { return "remove unecessary explicit type coercion"; }
13301329

1331-
Type getFromType() const { return LHS; }
1332-
Type getToType() const { return RHS; }
1330+
Type getToType() const { return toType; }
13331331

13341332
bool diagnose(Expr *root, bool asNote = false) const override;
13351333

1336-
static RemoveUnecessaryCoercion *create(ConstraintSystem &cs, Type lhs, Type rhs,
1334+
static RemoveUnecessaryCoercion *create(ConstraintSystem &cs, Type toType,
13371335
ConstraintLocator *locator);
13381336
};
13391337

Diff for: lib/Sema/CSSimplify.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -2748,8 +2748,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
27482748

27492749
// If the types are obviously equivalent, we're done.
27502750
if (desugar1->isEqual(desugar2)) {
2751-
if (kind == ConstraintKind::Conversion) {
2752-
// TODO: Create fix it or emit diagnostic?
2751+
if (locator.getBaseLocator()->isLastElement(ConstraintLocator::PathElementKind::TypeCoercion)) {
2752+
auto *fix = RemoveUnecessaryCoercion::create(*this, type2,
2753+
locator.getBaseLocator());
2754+
recordFix(fix);
27532755
}
27542756
if (!isa<InOutType>(desugar2))
27552757
return getTypeMatchSuccess();

Diff for: lib/Sema/ConstraintLocator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
471471
bool isFunctionConversion() const {
472472
return (getSummaryFlags() & IsFunctionConversion);
473473
}
474-
474+
475475
/// Determine whether given locator points to the subscript reference
476476
/// e.g. `foo[0]` or `\Foo.[0]`
477477
bool isSubscriptMemberRef() const;

0 commit comments

Comments
 (0)