Skip to content

Commit a8a0fa9

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

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

@@ -3241,6 +3244,28 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
32413244
}
32423245
};
32433246

3247+
class AllowAssociatedValueMismatch final : public ContextualMismatch {
3248+
AllowAssociatedValueMismatch(ConstraintSystem &cs, Type fromType, Type toType,
3249+
ConstraintLocator *locator)
3250+
: ContextualMismatch(cs, FixKind::AllowAssociatedValueMismatch, fromType,
3251+
toType, locator) {}
3252+
3253+
public:
3254+
std::string getName() const override {
3255+
return "allow associated value mismatch";
3256+
}
3257+
3258+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3259+
3260+
static AllowAssociatedValueMismatch *create(ConstraintSystem &cs,
3261+
Type fromType, Type toType,
3262+
ConstraintLocator *locator);
3263+
3264+
static bool classof(const ConstraintFix *fix) {
3265+
return fix->getKind() == FixKind::AllowAssociatedValueMismatch;
3266+
}
3267+
};
3268+
32443269
class AllowNonOptionalWeak final : public ConstraintFix {
32453270
AllowNonOptionalWeak(ConstraintSystem &cs, ConstraintLocator *locator)
32463271
: ConstraintFix(cs, FixKind::AllowNonOptionalWeak, locator) {}

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,9 +2497,6 @@ bool ContextualFailure::diagnoseAsError() {
24972497
return false;
24982498
}
24992499

2500-
if (diagnoseExtraneousAssociatedValues())
2501-
return true;
2502-
25032500
// Special case of some common conversions involving Swift.String
25042501
// indexes, catching cases where people attempt to index them with an integer.
25052502
if (isIntegerToStringIndexConversion()) {
@@ -2940,24 +2937,6 @@ void ContextualFailure::tryFixIts(InFlightDiagnostic &diagnostic) const {
29402937
return;
29412938
}
29422939

2943-
bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
2944-
if (auto match =
2945-
getLocator()->getLastElementAs<LocatorPathElt::PatternMatch>()) {
2946-
if (auto enumElementPattern =
2947-
dyn_cast<EnumElementPattern>(match->getPattern())) {
2948-
emitDiagnosticAt(enumElementPattern->getNameLoc(),
2949-
diag::enum_element_pattern_assoc_values_mismatch,
2950-
enumElementPattern->getName());
2951-
emitDiagnosticAt(enumElementPattern->getNameLoc(),
2952-
diag::enum_element_pattern_assoc_values_remove)
2953-
.fixItRemove(enumElementPattern->getSubPattern()->getSourceRange());
2954-
return true;
2955-
}
2956-
}
2957-
2958-
return false;
2959-
}
2960-
29612940
bool ContextualFailure::diagnoseCoercionToUnrelatedType() const {
29622941
auto anchor = getRawAnchor();
29632942
auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
@@ -8731,6 +8710,19 @@ bool TupleLabelMismatchWarning::diagnoseAsError() {
87318710
return true;
87328711
}
87338712

8713+
bool AssociatedValueMismatchFailure::diagnoseAsError() {
8714+
auto match = getLocator()->castLastElementTo<LocatorPathElt::PatternMatch>();
8715+
auto *enumElementPattern = dyn_cast<EnumElementPattern>(match.getPattern());
8716+
8717+
emitDiagnosticAt(enumElementPattern->getNameLoc(),
8718+
diag::enum_element_pattern_assoc_values_mismatch,
8719+
enumElementPattern->getName());
8720+
emitDiagnosticAt(enumElementPattern->getNameLoc(),
8721+
diag::enum_element_pattern_assoc_values_remove)
8722+
.fixItRemove(enumElementPattern->getSubPattern()->getSourceRange());
8723+
return true;
8724+
}
8725+
87348726
bool SwiftToCPointerConversionInInvalidContext::diagnoseAsError() {
87358727
auto argInfo = getFunctionArgApplyInfo(getLocator());
87368728
if (!argInfo)

lib/Sema/CSDiagnostics.h

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

677-
/// Diagnose cases where a pattern tried to match associated values but
678-
/// the enum case had none.
679-
bool diagnoseExtraneousAssociatedValues() const;
680-
681677
/// Produce a specialized diagnostic if this is an invalid conversion to Bool.
682678
bool diagnoseConversionToBool() const;
683679

@@ -2772,6 +2768,15 @@ class TupleLabelMismatchWarning final : public ContextualFailure {
27722768
bool diagnoseAsError() override;
27732769
};
27742770

2771+
class AssociatedValueMismatchFailure final : public ContextualFailure {
2772+
public:
2773+
AssociatedValueMismatchFailure(const Solution &solution, Type fromType,
2774+
Type toType, ConstraintLocator *locator)
2775+
: ContextualFailure(solution, fromType, toType, locator) {}
2776+
2777+
bool diagnoseAsError() override;
2778+
};
2779+
27752780
/// Diagnose situations where Swift -> C pointer implicit conversion
27762781
/// is attempted on a Swift function instead of one imported from C header.
27772782
///

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
@@ -6408,8 +6408,7 @@ bool ConstraintSystem::repairFailures(
64086408
if (isMemberMatch) {
64096409
recordAnyTypeVarAsPotentialHole(lhs);
64106410
recordAnyTypeVarAsPotentialHole(rhs);
6411-
6412-
conversionsOrFixes.push_back(ContextualMismatch::create(
6411+
conversionsOrFixes.push_back(AllowAssociatedValueMismatch::create(
64136412
*this, lhs, rhs, getConstraintLocator(locator)));
64146413
}
64156414

@@ -14661,6 +14660,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1466114660
case FixKind::AllowInvalidPackExpansion:
1466214661
case FixKind::MacroMissingPound:
1466314662
case FixKind::AllowGlobalActorMismatch:
14663+
case FixKind::AllowAssociatedValueMismatch:
1466414664
case FixKind::GenericArgumentsMismatch: {
1466514665
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1466614666
}

0 commit comments

Comments
 (0)