Skip to content

Improvements to optional-to-Any coercion warning. #4906

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 1 commit into from
Sep 21, 2016
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
6 changes: 3 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2341,11 +2341,11 @@ WARNING(optional_pattern_match_promotion,none,
WARNING(optional_to_any_coercion,none,
"expression implicitly coerced from %0 to Any", (Type))
NOTE(default_optional_to_any,none,
"provide a default value to avoid the warning", ())
"provide a default value to avoid this warning", ())
NOTE(force_optional_to_any,none,
"force-unwrap the value to avoid the warning", ())
"force-unwrap the value to avoid this warning", ())
NOTE(silence_optional_to_any,none,
"explicitly cast to Any with 'as Any' to silence the warning", ())
"explicitly cast to Any with 'as Any' to silence this warning", ())

ERROR(invalid_noescape_use,none,
"non-escaping %select{value|parameter}1 %0 may only be called",
Expand Down
9 changes: 4 additions & 5 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3593,7 +3593,7 @@ checkImplicitPromotionsInCondition(const StmtConditionElement &cond,

static void diagnoseOptionalToAnyCoercion(TypeChecker &TC, const Expr *E,
const DeclContext *DC) {
if (E == nullptr || isa<ErrorExpr>(E) || !E->getType())
if (!E || isa<ErrorExpr>(E) || !E->getType())
return;

class OptionalToAnyCoercionWalker : public ASTWalker {
Expand All @@ -3605,13 +3605,12 @@ static void diagnoseOptionalToAnyCoercion(TypeChecker &TC, const Expr *E,
return { false, E };

if (auto *coercion = dyn_cast<CoerceExpr>(E)) {
if (E->getType()->getDesugaredType()->isAny() &&
isa<ErasureExpr>(coercion->getSubExpr()))
if (E->getType()->isAny() && isa<ErasureExpr>(coercion->getSubExpr()))
ErasureCoercedToAny.insert(coercion->getSubExpr());
} else if (isa<ErasureExpr>(E) && !ErasureCoercedToAny.count(E) &&
E->getType()->getDesugaredType()->isAny()) {
E->getType()->isAny()) {
auto subExpr = cast<ErasureExpr>(E)->getSubExpr();
auto erasedTy = subExpr->getType()->getDesugaredType();
auto erasedTy = subExpr->getType();
if (erasedTy->getOptionalObjectType()) {
TC.diagnose(subExpr->getStartLoc(), diag::optional_to_any_coercion,
erasedTy)
Expand Down
75 changes: 34 additions & 41 deletions test/Sema/diag_optional_to_any.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,54 @@ func takeAny(_ left: Any, _ right: Any) -> Int? {
func throwing() throws -> Int? {}

func warnOptionalToAnyCoercion(value x: Int?) -> Any {
let a: Any = x // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
let a: Any = x // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

let b: Any = x as Any

let c: Any = takeAny(a, b) // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
let c: Any = takeAny(a, b) // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

let d: Any = takeAny(c, c) as Any
let _: Any = takeAny(c, c) as Any

let e: Any = (x) // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}

_ = takeAny(d, e)
let _: Any = (x) // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

let f: Any = (x as Any)
let g: Any = (x) as (Any)

_ = takeAny(f as? Int, g) // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}

let h: Any = takeAny(f as? Int, g) as Any // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
_ = takeAny(f as? Int, g) // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

let i: Any = takeAny(f as? Int as Any, g) as Any
let _: Any = takeAny(f as? Int, g) as Any // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

_ = takeAny(h, i)
let _: Any = takeAny(f as? Int as Any, g) as Any

let j: Any = x! == x! ? 1 : x // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
let _: Any = x! == x! ? 1 : x // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}

let k: Any
do {
k = try throwing() // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
let _: Any = try throwing() // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}
} catch {}

_ = takeAny(j, k)

return x // expected-warning {{expression implicitly coerced from 'Optional<Int>' to Any}}
// expected-note@-1 {{provide a default value to avoid the warning}}
// expected-note@-2 {{force-unwrap the value to avoid the warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence}}
return x // expected-warning {{expression implicitly coerced from 'Int?' to Any}}
// expected-note@-1 {{provide a default value to avoid this warning}}
// expected-note@-2 {{force-unwrap the value to avoid this warning}}
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}}
}