Skip to content

Commit

Permalink
Merge pull request #5259 from rudkx/fix-28722908
Browse files Browse the repository at this point in the history
Fix source location on collection cast optional-to-Any warning.
  • Loading branch information
rudkx committed Oct 12, 2016
2 parents 2a6e896 + 3054442 commit 623a163
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lib/Sema/CSApply.cpp
Expand Up @@ -5093,6 +5093,7 @@ maybeDiagnoseUnsupportedFunctionConversion(TypeChecker &tc, Expr *expr,

static CollectionUpcastConversionExpr::ConversionPair
buildElementConversion(ExprRewriter &rewriter,
SourceLoc srcLoc,
Type srcCollectionType,
Type destCollectionType,
ConstraintLocatorBuilder locator,
Expand All @@ -5105,7 +5106,7 @@ buildElementConversion(ExprRewriter &rewriter,

// Build the conversion.
ASTContext &ctx = rewriter.getConstraintSystem().getASTContext();
auto opaque = new (ctx) OpaqueValueExpr(SourceLoc(), srcType);
auto opaque = new (ctx) OpaqueValueExpr(srcLoc, srcType);
auto conversion =
rewriter.coerceToType(opaque, destType,
locator.withPathElement(
Expand Down Expand Up @@ -5265,8 +5266,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}

// Build the value conversion.
auto conv =
buildElementConversion(*this, expr->getType(), toType, locator, 0);
auto conv = buildElementConversion(*this, expr->getLoc(), expr->getType(),
toType, locator, 0);

// Form the upcast.
return new (tc.Context) CollectionUpcastConversionExpr(expr, toType,
Expand Down Expand Up @@ -5307,10 +5308,10 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}

// Build the key and value conversions.
auto keyConv =
buildElementConversion(*this, expr->getType(), toType, locator, 0);
auto valueConv =
buildElementConversion(*this, expr->getType(), toType, locator, 1);
auto keyConv = buildElementConversion(
*this, expr->getLoc(), expr->getType(), toType, locator, 0);
auto valueConv = buildElementConversion(
*this, expr->getLoc(), expr->getType(), toType, locator, 1);

// If the source key and value types are object types, this is an upcast.
// Otherwise, it's bridged.
Expand All @@ -5330,8 +5331,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}

// Build the value conversion.
auto conv =
buildElementConversion(*this, expr->getType(), toType, locator, 0);
auto conv = buildElementConversion(*this, expr->getLoc(), expr->getType(),
toType, locator, 0);

return new (tc.Context) CollectionUpcastConversionExpr(expr, toType,
{}, conv);
Expand Down
26 changes: 25 additions & 1 deletion test/Sema/diag_unintended_optional_behavior.swift
Expand Up @@ -59,6 +59,31 @@ func warnOptionalToAnyCoercion(value x: Int?) -> Any {
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}
}

func takesCollectionOfAny(_ a: [Any], _ d: [String : Any]) {
}

func warnCollectionOfAny(_ a: [Int?], _ d: [String : Int?]) {
// https://bugs.swift.org/browse/SR-2928 - Collection casts from collections of optionals to collections of Any need custom handling
takesCollectionOfAny(a, d) // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}{{25-25= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}{{25-25=!}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}{{25-25= as Any}}
// expected-warning@-4 {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-5 {{provide a default value to avoid this warning}}{{28-28= ?? <#default value#>}}
// expected-note@-6 {{force-unwrap the value to avoid this warning}}{{28-28=!}}
// expected-note@-7 {{explicitly cast to Any with 'as Any' to silence this warning}}{{28-28= as Any}}

// https://bugs.swift.org/browse/SR-2928 - Collection casts from collections of optionals to collections of Any need custom handling
takesCollectionOfAny(a as [Any], d as [String : Any]) // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}{{25-25= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}{{25-25=!}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}{{25-25= as Any}}
// expected-warning@-4 {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-5 {{provide a default value to avoid this warning}}{{37-37= ?? <#default value#>}}
// expected-note@-6 {{force-unwrap the value to avoid this warning}}{{37-37=!}}
// expected-note@-7 {{explicitly cast to Any with 'as Any' to silence this warning}}{{37-37= as Any}}
}

func warnOptionalInStringInterpolationSegment(_ o : Int?) {
print("Always some, Always some, Always some: \(o)")
// expected-warning@-1 {{string interpolation produces a debug description for an optional value; did you mean to make this explicit?}}
Expand All @@ -72,4 +97,3 @@ func warnOptionalInStringInterpolationSegment(_ o : Int?) {
print("Always some, Always some, Always some: \(o as Int?)") // No warning
print("Always some, Always some, Always some: \(o.debugDescription)") // No warning.
}

0 comments on commit 623a163

Please sign in to comment.