Skip to content

Prefer RawRepresentable fix-its that don't require an extra conversion #17714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 59 additions & 25 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4500,27 +4500,42 @@ diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI, Expr *fnExpr,
.diagnose();
}

static bool isRawRepresentableMismatch(Type fromType, Type toType,
KnownProtocolKind kind,
const ConstraintSystem &CS) {
namespace {
enum class RawRepresentableMismatch {
NotApplicable,
Convertible,
ExactMatch
};
}

static RawRepresentableMismatch
checkRawRepresentableMismatch(Type fromType, Type toType,
KnownProtocolKind kind,
const ConstraintSystem &CS) {
toType = toType->lookThroughAllOptionalTypes();
fromType = fromType->lookThroughAllOptionalTypes();

// First check if this is an attempt to convert from something to
// raw representable.
if (conformsToKnownProtocol(fromType, kind, CS)) {
if (isRawRepresentable(toType, kind, CS))
return true;
if (auto rawType = isRawRepresentable(toType, kind, CS)) {
if (rawType->isEqual(fromType))
return RawRepresentableMismatch::ExactMatch;
return RawRepresentableMismatch::Convertible;
}
}

// Otherwise, it might be an attempt to convert from raw representable
// to its raw value.
if (isRawRepresentable(fromType, kind, CS)) {
if (conformsToKnownProtocol(toType, kind, CS))
return true;
if (auto rawType = isRawRepresentable(fromType, kind, CS)) {
if (conformsToKnownProtocol(toType, kind, CS)) {
if (rawType->isEqual(toType))
return RawRepresentableMismatch::ExactMatch;
return RawRepresentableMismatch::Convertible;
}
}

return false;
return RawRepresentableMismatch::NotApplicable;
}

static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
Expand All @@ -4545,13 +4560,17 @@ static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
if (!argType || argType->hasTypeVariable() || argType->hasUnresolvedType())
return false;

ArrayRef<KnownProtocolKind> rawRepresentableProtocols = {
KnownProtocolKind rawRepresentableProtocols[] = {
KnownProtocolKind::ExpressibleByStringLiteral,
KnownProtocolKind::ExpressibleByIntegerLiteral};

const auto &CS = CCI.CS;
auto arguments = decomposeArgType(argType, argLabels);
auto *tupleArgs = dyn_cast<TupleExpr>(argExpr);

auto bestMatchKind = RawRepresentableMismatch::NotApplicable;
const UncurriedCandidate *bestMatchCandidate = nullptr;
KnownProtocolKind bestMatchProtocol;
size_t bestMatchIndex;

for (auto &candidate : CCI.candidates) {
auto *decl = candidate.getDecl();
Expand All @@ -4562,6 +4581,7 @@ static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
continue;

auto parameters = candidate.getParameters();
// FIXME: Default arguments?
if (parameters.size() != arguments.size())
continue;

Expand All @@ -4572,24 +4592,38 @@ static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
for (auto kind : rawRepresentableProtocols) {
// If trying to convert from raw type to raw representable,
// or vice versa from raw representable (e.g. enum) to raw type.
if (!isRawRepresentableMismatch(argType, paramType, kind, CS))
continue;

auto *expr = argExpr;
if (tupleArgs)
expr = tupleArgs->getElement(i);

auto diag =
CS.TC.diagnose(expr->getLoc(), diag::cannot_convert_argument_value,
argType, paramType);

tryRawRepresentableFixIts(diag, CS, argType, paramType, kind, expr);
return true;
auto matchKind = checkRawRepresentableMismatch(argType, paramType, kind,
CS);
if (matchKind > bestMatchKind) {
bestMatchKind = matchKind;
bestMatchProtocol = kind;
bestMatchCandidate = &candidate;
bestMatchIndex = i;
}
}
}
}

return false;
if (bestMatchKind == RawRepresentableMismatch::NotApplicable)
return false;

const Expr *expr = argExpr;
if (auto *tupleArgs = dyn_cast<TupleExpr>(argExpr))
expr = tupleArgs->getElement(bestMatchIndex);
expr = expr->getValueProvidingExpr();

auto parameters = bestMatchCandidate->getParameters();
auto paramType = parameters[bestMatchIndex].getType();
auto singleArgType = arguments[bestMatchIndex].getType();

auto diag = CS.TC.diagnose(expr->getLoc(),
diag::cannot_convert_argument_value,
singleArgType, paramType);

tryRawRepresentableFixIts(diag, CS, singleArgType, paramType,
bestMatchProtocol, expr);
return true;

}

// Extract expression for failed argument number
Expand Down
27 changes: 25 additions & 2 deletions test/Sema/enum_raw_representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ rdar32431165_1(.baz)
// expected-error@-1 {{reference to member 'baz' cannot be resolved without a contextual type}}

rdar32431165_1("")
// expected-error@-1 {{cannot convert value of type 'String' to expected argument type 'E_32431165'}} {{15-15=E_32431165(rawValue: }} {{19-19=)}}
// expected-error@-1 {{cannot convert value of type 'String' to expected argument type 'E_32431165'}} {{16-16=E_32431165(rawValue: }} {{18-18=)}}
rdar32431165_1(42, "")
// expected-error@-1 {{cannot convert value of type 'String' to expected argument type 'E_32431165'}} {{20-20=E_32431165(rawValue: }} {{22-22=)}}

Expand All @@ -129,7 +129,7 @@ func rdar32431165_2(_: Int) {}
func rdar32431165_2(_: Int, _: String) {}

rdar32431165_2(E_32431165.bar)
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{15-15=}} {{31-31=.rawValue}}
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{16-16=}} {{30-30=.rawValue}}
rdar32431165_2(42, E_32431165.bar)
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{20-20=}} {{34-34=.rawValue}}

Expand All @@ -145,6 +145,29 @@ func rdar32432253(_ condition: Bool = false) {
// expected-error@-1 {{cannot convert value of type 'E_32431165' to expected argument type 'String'}} {{11-11=}} {{17-17=.rawValue}}
}

func sr8150_helper1(_: Int) {}
func sr8150_helper1(_: Double) {}

func sr8150_helper2(_: Double) {}
func sr8150_helper2(_: Int) {}

func sr8150_helper3(_: Foo) {}
func sr8150_helper3(_: Bar) {}

func sr8150_helper4(_: Bar) {}
func sr8150_helper4(_: Foo) {}

func sr8150(bar: Bar) {
sr8150_helper1(bar)
// expected-error@-1 {{cannot convert value of type 'Bar' to expected argument type 'Double'}} {{18-18=}} {{21-21=.rawValue}}
sr8150_helper2(bar)
// expected-error@-1 {{cannot convert value of type 'Bar' to expected argument type 'Double'}} {{18-18=}} {{21-21=.rawValue}}
sr8150_helper3(0.0)
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Bar'}} {{18-18=Bar(rawValue: }} {{21-21=)}}
sr8150_helper4(0.0)
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Bar'}} {{18-18=Bar(rawValue: }} {{21-21=)}}
}


struct NotEquatable { }

Expand Down