Skip to content

[Typechecker] Warn when casting a function type to an existential or archetype type #22822

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 4 commits into from
Mar 7, 2019
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ WARNING(is_expr_same_type,none,
"'!= nil'?", (Type, Type))
WARNING(downcast_to_unrelated,none,
"cast from %0 to unrelated type %1 always fails", (Type, Type))
NOTE(downcast_to_unrelated_fixit,none,
"did you mean to call %0 with '()'?", (Identifier))
ERROR(downcast_to_more_optional,none,
"cannot downcast from %0 to a more optional type %1",
(Type, Type))
Expand Down
47 changes: 47 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3896,6 +3896,53 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
}
}

assert(!toType->isAny() && "casts to 'Any' should've been handled above");
assert(!toType->isAnyObject() &&
"casts to 'AnyObject' should've been handled above");

// A cast from a function type to an existential type (except `Any`)
// or an archetype type (with constraints) cannot succeed
auto toArchetypeType = toType->is<ArchetypeType>();
auto fromFunctionType = fromType->is<FunctionType>();
auto toExistentialType = toType->isAnyExistentialType();

auto toConstrainedArchetype = false;
if (toArchetypeType) {
auto archetype = toType->castTo<ArchetypeType>();
toConstrainedArchetype = archetype && !archetype->getConformsTo().empty();
}

if (fromFunctionType &&
(toExistentialType || (toArchetypeType && toConstrainedArchetype))) {
switch (contextKind) {
case CheckedCastContextKind::ConditionalCast:
case CheckedCastContextKind::ForcedCast:
diagnose(diagLoc, diag::downcast_to_unrelated, origFromType, origToType)
.highlight(diagFromRange)
.highlight(diagToRange);

// If we're referring to a function with a return value (not Void) then
// emit a fix-it suggesting to add `()` to call the function
if (auto DRE = dyn_cast<DeclRefExpr>(fromExpr)) {
if (auto FD = dyn_cast<FuncDecl>(DRE->getDecl())) {
if (!FD->getResultInterfaceType()->isVoid()) {
diagnose(diagLoc, diag::downcast_to_unrelated_fixit, FD->getName())
.fixItInsertAfter(fromExpr->getEndLoc(), "()");
}
}
}

return CheckedCastKind::ValueCast;
break;

case CheckedCastContextKind::IsPattern:
case CheckedCastContextKind::EnumElementPattern:
case CheckedCastContextKind::IsExpr:
case CheckedCastContextKind::None:
break;
}
}

// If we can bridge through an Objective-C class, do so.
if (Type bridgedToClass = getDynamicBridgedThroughObjCClass(dc, fromType,
toType)) {
Expand Down
29 changes: 29 additions & 0 deletions test/expr/cast/as_coerce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,32 @@ f(1 as String) // expected-error{{cannot convert value of type 'Int' to type 'St
// <rdar://problem/19650402> Swift compiler segfaults while running the annotation tests
let s : AnyObject = C3()
s as C3 // expected-error{{'AnyObject' is not convertible to 'C3'; did you mean to use 'as!' to force downcast?}} {{3-5=as!}}

// SR-6022
func sr6022() -> Any { return 0 }
func sr6022_1() { return; }
protocol SR6022_P {}

_ = sr6022 as! SR6022_P // expected-warning {{cast from '() -> Any' to unrelated type 'SR6022_P' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{11-11=()}}
_ = sr6022 as? SR6022_P // expected-warning {{cast from '() -> Any' to unrelated type 'SR6022_P' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'}}{{11-11=()}}
_ = sr6022_1 as! SR6022_P // expected-warning {{cast from '() -> ()' to unrelated type 'SR6022_P' always fails}}
_ = sr6022_1 as? SR6022_P // expected-warning {{cast from '() -> ()' to unrelated type 'SR6022_P' always fails}}

func testSR6022_P<T: SR6022_P>(_: T.Type) {
_ = sr6022 as! T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{13-13=()}}
_ = sr6022 as? T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{13-13=()}}
_ = sr6022_1 as! T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
_ = sr6022_1 as? T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
}

func testSR6022_P_1<U>(_: U.Type) {
_ = sr6022 as! U // Okay
_ = sr6022 as? U // Okay
_ = sr6022_1 as! U // Okay
_ = sr6022_1 as? U // Okay
}

_ = sr6022 as! AnyObject // expected-warning {{forced cast from '() -> Any' to 'AnyObject' always succeeds; did you mean to use 'as'?}}
_ = sr6022 as? AnyObject // expected-warning {{conditional cast from '() -> Any' to 'AnyObject' always succeeds}}
_ = sr6022_1 as! Any // expected-warning {{forced cast from '() -> ()' to 'Any' always succeeds; did you mean to use 'as'?}}
_ = sr6022_1 as? Any // expected-warning {{conditional cast from '() -> ()' to 'Any' always succeeds}}