Skip to content
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

[AST] Paren'd reference to an IUO function crashes the compiler in SILGen #26831

Merged
merged 14 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
20 changes: 19 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,9 +2189,27 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
openedFullType,
refType};

// If we have something like '(s.foo)()', where 's.foo()' returns an IUO,
// then we need to only create a single constraint that binds the
// type to an optional.
auto isIUOCallWrappedInParens = [&]() {
auto paren = getParentExpr(locator->getAnchor());
auto call = getParentExpr(paren);
theblixguy marked this conversation as resolved.
Show resolved Hide resolved

if (call && isa<CallExpr>(call)) {
auto callExpr = cast<CallExpr>(call);
if (callExpr->getFn() != callExpr->getSemanticFn()) {
return true;
}
}

return false;
};

// In some cases we already created the appropriate bind constraints.
if (!bindConstraintCreated) {
if (choice.isImplicitlyUnwrappedValueOrReturnValue()) {
if (choice.isImplicitlyUnwrappedValueOrReturnValue() &&
!isIUOCallWrappedInParens()) {
// Build the disjunction to attempt binding both T? and T (or
// function returning T? and function returning T).
buildDisjunctionForImplicitlyUnwrappedOptional(boundType, refType,
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/iuo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@ var y: Int = 2

let r = sr6988(x: x, y: y)
let _: Int = r

// SR-10492

struct SR_10492_S {
func foo() -> Int! { return 0 }
}

let sr_10492_s = SR_10492_S()
let sr_10492_int1: Int = (sr_10492_s.foo)() // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-1 {{coalesce}}{{44-44= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap}}{{44-44=!}}
let sr_10492_int2: Int? = (sr_10492_s.foo)() // Okay
8 changes: 8 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr10492.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: not %target-swift-frontend -emit-silgen %s

struct S {
func foo() -> Int! { return 0 }
}

let s = S()
let x: Int = (s.foo)()