Skip to content

Don't use construction to convert literals in rawValue fix-it. #4943

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
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
15 changes: 8 additions & 7 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3571,7 +3571,7 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {
/// Return true if the given type conforms to a known protocol type.
static bool conformsToKnownProtocol(Type fromType,
KnownProtocolKind kind,
ConstraintSystem *CS) {
const ConstraintSystem *CS) {
auto proto = CS->TC.getProtocol(SourceLoc(), kind);
if (!proto)
return false;
Expand All @@ -3584,15 +3584,15 @@ static bool conformsToKnownProtocol(Type fromType,
return false;
}

static bool isIntegerType(Type fromType, ConstraintSystem *CS) {
static bool isIntegerType(Type fromType, const ConstraintSystem *CS) {
return conformsToKnownProtocol(fromType,
KnownProtocolKind::ExpressibleByIntegerLiteral,
CS);
}

/// Return true if the given type conforms to RawRepresentable.
static Type isRawRepresentable(Type fromType,
ConstraintSystem *CS) {
const ConstraintSystem *CS) {
auto rawReprType =
CS->TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
if (!rawReprType)
Expand All @@ -3615,7 +3615,7 @@ static Type isRawRepresentable(Type fromType,
/// underlying type conforming to the given known protocol.
static Type isRawRepresentable(Type fromType,
KnownProtocolKind kind,
ConstraintSystem *CS) {
const ConstraintSystem *CS) {
Type rawTy = isRawRepresentable(fromType, CS);
if (!rawTy || !conformsToKnownProtocol(rawTy, kind, CS))
return Type();
Expand Down Expand Up @@ -3646,11 +3646,11 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
///
/// This helps migration with SDK changes.
static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
ConstraintSystem *CS,
const ConstraintSystem *CS,
Type fromType,
Type toType,
KnownProtocolKind kind,
Expr *expr) {
const Expr *expr) {
// The following fixes apply for optional destination types as well.
bool toTypeIsOptional = !toType->getAnyOptionalObjectType().isNull();
toType = toType->lookThroughAllAnyOptionalTypes();
Expand Down Expand Up @@ -3690,7 +3690,8 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
std::string convWrapBefore = toType.getString();
convWrapBefore += "(rawValue: ";
std::string convWrapAfter = ")";
if (!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
if (!isa<LiteralExpr>(expr) &&
!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
// Only try to insert a converting construction if the protocol is a
// literal protocol and not some other known protocol.
switch (kind) {
Expand Down
4 changes: 4 additions & 0 deletions test/FixCode/fixits-apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func testMask12(a: MyEventMask2?) {
func testMask13(a: MyEventMask2?) {
testMask1(a: a) // no fix, nullability mismatch.
}
func testMask14() {
sendIt(1)
sendItOpt(2)
}

struct Wrapper {
typealias InnerMask = MyEventMask2
Expand Down
4 changes: 4 additions & 0 deletions test/FixCode/fixits-apply.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func testMask12(a: MyEventMask2?) {
func testMask13(a: MyEventMask2?) {
testMask1(a: a) // no fix, nullability mismatch.
}
func testMask14() {
sendIt(MyEventMask2(rawValue: 1))
sendItOpt(MyEventMask2(rawValue: 2))
}

struct Wrapper {
typealias InnerMask = MyEventMask2
Expand Down