Skip to content

Commit 61a850d

Browse files
committed
[CSFix] Improve missing .rawValue fix in ambiguity conditions
1 parent f43aad2 commit 61a850d

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

include/swift/Sema/CSFix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,10 @@ class UseRawValue final : public ConstraintFix {
22872287

22882288
bool diagnose(const Solution &solution, bool asNote = false) const override;
22892289

2290+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
2291+
return diagnose(*commonFixes.front().first);
2292+
}
2293+
22902294
static UseRawValue *create(ConstraintSystem &cs, Type rawReprType,
22912295
Type expectedType, ConstraintLocator *locator);
22922296

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7328,7 +7328,11 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
73287328
TypeChecker::conformsToProtocol(rawValue, protocol,
73297329
DC->getParentModule())) {
73307330
auto *fix = UseRawValue::create(*this, type, protocolTy, loc);
7331-
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
7331+
// Since this is a conformance requirement failure (where the
7332+
// source is most likely an argument), let's increase its impact
7333+
// to disambiguate vs. conversion failure of the same kind.
7334+
return recordFix(fix, /*impact=*/2) ? SolutionKind::Error
7335+
: SolutionKind::Solved;
73327336
}
73337337
}
73347338

test/Sema/enum_raw_representable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ rdar32431165_2(42, E_32431165.bar)
146146
// or constructing raw representable type from second, both ways are valid.
147147
do {
148148
E_32431165.bar == "bar"
149-
// expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'E_32431165' and 'String'}} expected-note@-1 {{partially matching parameter lists: (String, String)}}
149+
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{17-17=.rawValue}}
150150

151151
"bar" == E_32431165.bar
152-
// expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'String' and 'E_32431165'}} expected-note@-1 {{partially matching parameter lists: (String, String)}}
152+
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{26-26=.rawValue}}
153153
}
154154

155155
func rdar32431165_overloaded() -> Int { 42 } // expected-note {{'rdar32431165_overloaded()' produces 'Int', not the expected contextual result type 'E_32431165'}}

0 commit comments

Comments
 (0)