Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2842,6 +2842,9 @@ NOTE(force_optional_to_any,none,
NOTE(silence_optional_to_any,none,
"explicitly cast to %0 with '%1' to silence this warning",
(Type, StringRef))
NOTE(silence_implicitly_unwrapped_optional_to_any,none,
"explicitly cast to %0 with '%1' to silence this warning",
(Type, StringRef))
WARNING(optional_in_string_interpolation_segment,none,
"string interpolation produces a debug description for an optional "
"value; did you mean to make this explicit?",
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Migrator/FixitFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ struct FixitFilter {
Info.ID == diag::missing_unknown_case.ID ||
Info.ID == diag::paren_void_probably_void.ID ||
Info.ID == diag::make_decl_objc.ID ||
Info.ID == diag::optional_req_nonobjc_near_match_add_objc.ID)
Info.ID == diag::optional_req_nonobjc_near_match_add_objc.ID ||
Info.ID == diag::silence_implicitly_unwrapped_optional_to_any.ID)
return true;

return false;
Expand Down
14 changes: 12 additions & 2 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3710,12 +3710,22 @@ static void diagnoseUnintendedOptionalBehavior(TypeChecker &TC, const Expr *E,
}

void emitSilenceOptionalAnyWarningWithCoercion(Expr *E, Type destType) {
Diag<Type, StringRef> silenceDiag = diag::silence_optional_to_any;

if (auto declRefExpr = dyn_cast<DeclRefExpr>(E)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use hasImplicitlyUnwrappedResult which I added in #16434.

I handles function calls, subscripts, and member references.

auto decl = declRefExpr->getDecl();
if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) {
// Use a different diagnostic ID for IUO to differntiate them in the
// migrator
silenceDiag = diag::silence_implicitly_unwrapped_optional_to_any;
}
}

SmallString<16> coercionString;
coercionString += " as ";
coercionString += destType->getWithoutParens()->getString();

TC.diagnose(E->getLoc(), diag::silence_optional_to_any,
destType, coercionString.substr(1))
TC.diagnose(E->getLoc(), silenceDiag, destType, coercionString.substr(1))
.highlight(E->getSourceRange())
.fixItInsertAfter(E->getEndLoc(), coercionString);
}
Expand Down
9 changes: 9 additions & 0 deletions test/Migrator/iuo_any_coercion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// REQUIRES: objc_interop
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -api-diff-data-file %S/Inputs/API.json -emit-migrated-file-path %t/iuo_any_coercion.swift.result -emit-remap-file-path %t/iuo_any_coercion.swift.remap -o /dev/null
// RUN: diff -u %S/iuo_any_coercion.swift.expected %t/iuo_any_coercion.swift.result

let x: Int! = 1
let _: Any = x

let y: Int? = 1
let _: Any = y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for member references, function calls, subscripts, as well a test where the code already has 'as Any' (and thus we wouldn't expect the note, nor the migration).

9 changes: 9 additions & 0 deletions test/Migrator/iuo_any_coercion.swift.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// REQUIRES: objc_interop
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -api-diff-data-file %S/Inputs/API.json -emit-migrated-file-path %t/iuo_any_coercion.swift.result -emit-remap-file-path %t/iuo_any_coercion.swift.remap -o /dev/null
// RUN: diff -u %S/iuo_any_coercion.swift.expected %t/iuo_any_coercion.swift.result

let x: Int! = 1
let _: Any = x as Any

let y: Int? = 1
let _: Any = y