Skip to content

Commit 11fbb4e

Browse files
committed
[CS] NFC: Factor out AllowAssociatedValueMismatch
This seems better suited as its own fix, rather than as part of ContextualMismatch.
1 parent 9c30df5 commit 11fbb4e

File tree

5 files changed

+63
-27
lines changed

5 files changed

+63
-27
lines changed

include/swift/Sema/CSFix.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ enum class FixKind : uint8_t {
401401
/// Produce a warning for a tuple label mismatch.
402402
AllowTupleLabelMismatch,
403403

404+
/// Allow an associated value mismatch for an enum element pattern.
405+
AllowAssociatedValueMismatch,
406+
404407
/// Produce an error for not getting a compile-time constant
405408
NotCompileTimeConst,
406409

@@ -3231,6 +3234,28 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
32313234
}
32323235
};
32333236

3237+
class AllowAssociatedValueMismatch final : public ContextualMismatch {
3238+
AllowAssociatedValueMismatch(ConstraintSystem &cs, Type fromType, Type toType,
3239+
ConstraintLocator *locator)
3240+
: ContextualMismatch(cs, FixKind::AllowAssociatedValueMismatch, fromType,
3241+
toType, locator) {}
3242+
3243+
public:
3244+
std::string getName() const override {
3245+
return "allow associated value mismatch";
3246+
}
3247+
3248+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3249+
3250+
static AllowAssociatedValueMismatch *create(ConstraintSystem &cs,
3251+
Type fromType, Type toType,
3252+
ConstraintLocator *locator);
3253+
3254+
static bool classof(const ConstraintFix *fix) {
3255+
return fix->getKind() == FixKind::AllowAssociatedValueMismatch;
3256+
}
3257+
};
3258+
32343259
class AllowNonOptionalWeak final : public ConstraintFix {
32353260
AllowNonOptionalWeak(ConstraintSystem &cs, ConstraintLocator *locator)
32363261
: ConstraintFix(cs, FixKind::AllowNonOptionalWeak, locator) {}

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,9 +2448,6 @@ bool ContextualFailure::diagnoseAsError() {
24482448
return false;
24492449
}
24502450

2451-
if (diagnoseExtraneousAssociatedValues())
2452-
return true;
2453-
24542451
// Special case of some common conversions involving Swift.String
24552452
// indexes, catching cases where people attempt to index them with an integer.
24562453
if (isIntegerToStringIndexConversion()) {
@@ -2891,24 +2888,6 @@ void ContextualFailure::tryFixIts(InFlightDiagnostic &diagnostic) const {
28912888
return;
28922889
}
28932890

2894-
bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
2895-
if (auto match =
2896-
getLocator()->getLastElementAs<LocatorPathElt::PatternMatch>()) {
2897-
if (auto enumElementPattern =
2898-
dyn_cast<EnumElementPattern>(match->getPattern())) {
2899-
emitDiagnosticAt(enumElementPattern->getNameLoc(),
2900-
diag::enum_element_pattern_assoc_values_mismatch,
2901-
enumElementPattern->getName());
2902-
emitDiagnosticAt(enumElementPattern->getNameLoc(),
2903-
diag::enum_element_pattern_assoc_values_remove)
2904-
.fixItRemove(enumElementPattern->getSubPattern()->getSourceRange());
2905-
return true;
2906-
}
2907-
}
2908-
2909-
return false;
2910-
}
2911-
29122891
bool ContextualFailure::diagnoseCoercionToUnrelatedType() const {
29132892
auto anchor = getRawAnchor();
29142893
auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
@@ -8684,6 +8663,19 @@ bool TupleLabelMismatchWarning::diagnoseAsError() {
86848663
return true;
86858664
}
86868665

8666+
bool AssociatedValueMismatchFailure::diagnoseAsError() {
8667+
auto match = getLocator()->castLastElementTo<LocatorPathElt::PatternMatch>();
8668+
auto *enumElementPattern = dyn_cast<EnumElementPattern>(match.getPattern());
8669+
8670+
emitDiagnosticAt(enumElementPattern->getNameLoc(),
8671+
diag::enum_element_pattern_assoc_values_mismatch,
8672+
enumElementPattern->getName());
8673+
emitDiagnosticAt(enumElementPattern->getNameLoc(),
8674+
diag::enum_element_pattern_assoc_values_remove)
8675+
.fixItRemove(enumElementPattern->getSubPattern()->getSourceRange());
8676+
return true;
8677+
}
8678+
86878679
bool SwiftToCPointerConversionInInvalidContext::diagnoseAsError() {
86888680
auto argInfo = getFunctionArgApplyInfo(getLocator());
86898681
if (!argInfo)

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,6 @@ class ContextualFailure : public FailureDiagnostic {
696696
/// Diagnose failed conversion in a `CoerceExpr`.
697697
bool diagnoseCoercionToUnrelatedType() const;
698698

699-
/// Diagnose cases where a pattern tried to match associated values but
700-
/// the enum case had none.
701-
bool diagnoseExtraneousAssociatedValues() const;
702-
703699
/// Produce a specialized diagnostic if this is an invalid conversion to Bool.
704700
bool diagnoseConversionToBool() const;
705701

@@ -2794,6 +2790,15 @@ class TupleLabelMismatchWarning final : public ContextualFailure {
27942790
bool diagnoseAsError() override;
27952791
};
27962792

2793+
class AssociatedValueMismatchFailure final : public ContextualFailure {
2794+
public:
2795+
AssociatedValueMismatchFailure(const Solution &solution, Type fromType,
2796+
Type toType, ConstraintLocator *locator)
2797+
: ContextualFailure(solution, fromType, toType, locator) {}
2798+
2799+
bool diagnoseAsError() override;
2800+
};
2801+
27972802
/// Diagnose situations where Swift -> C pointer implicit conversion
27982803
/// is attempted on a Swift function instead of one imported from C header.
27992804
///

lib/Sema/CSFix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,20 @@ bool AllowTupleLabelMismatch::diagnose(const Solution &solution,
24262426
return warning.diagnose(asNote);
24272427
}
24282428

2429+
AllowAssociatedValueMismatch *
2430+
AllowAssociatedValueMismatch::create(ConstraintSystem &cs, Type fromType,
2431+
Type toType, ConstraintLocator *locator) {
2432+
return new (cs.getAllocator())
2433+
AllowAssociatedValueMismatch(cs, fromType, toType, locator);
2434+
}
2435+
2436+
bool AllowAssociatedValueMismatch::diagnose(const Solution &solution,
2437+
bool asNote) const {
2438+
AssociatedValueMismatchFailure failure(solution, getFromType(), getToType(),
2439+
getLocator());
2440+
return failure.diagnose(asNote);
2441+
}
2442+
24292443
bool AllowSwiftToCPointerConversion::diagnose(const Solution &solution,
24302444
bool asNote) const {
24312445
SwiftToCPointerConversionInInvalidContext failure(solution, getLocator());

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6306,8 +6306,7 @@ bool ConstraintSystem::repairFailures(
63066306
if (isMemberMatch) {
63076307
recordAnyTypeVarAsPotentialHole(lhs);
63086308
recordAnyTypeVarAsPotentialHole(rhs);
6309-
6310-
conversionsOrFixes.push_back(ContextualMismatch::create(
6309+
conversionsOrFixes.push_back(AllowAssociatedValueMismatch::create(
63116310
*this, lhs, rhs, getConstraintLocator(locator)));
63126311
}
63136312

@@ -14216,6 +14215,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1421614215
case FixKind::AllowInvalidPackExpansion:
1421714216
case FixKind::MacroMissingPound:
1421814217
case FixKind::AllowGlobalActorMismatch:
14218+
case FixKind::AllowAssociatedValueMismatch:
1421914219
case FixKind::GenericArgumentsMismatch: {
1422014220
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1422114221
}

0 commit comments

Comments
 (0)